1 /* Struct-reorg optimization.
2 Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Olga Golovanevsky <olga@il.ibm.com>
4 (Initial version of this code was developed
5 by Caroline Tice and Mostafa Hagog.)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
31 #include "tree-inline.h"
32 #include "tree-flow.h"
33 #include "tree-flow-inline.h"
34 #include "langhooks.h"
35 #include "pointer-set.h"
42 #include "diagnostic.h"
48 #include "basic-block.h"
49 #include "tree-iterator.h"
50 #include "tree-pass.h"
51 #include "ipa-struct-reorg.h"
53 #include "ipa-type-escape.h"
54 #include "tree-dump.h"
57 /* This optimization implements structure peeling.
59 For example, given a structure type:
67 it can be peeled into two structure types as follows:
69 typedef struct and typedef struct
75 or can be fully peeled:
92 When structure type is peeled all instances and their accesses
93 in the program are updated accordingly. For example, if there is
98 and structure type str_t was peeled into two structures str_t_0
99 and str_t_1 as it was shown above, then array A will be replaced
100 by two arrays as follows:
105 The field access of field a of element i of array A: A[i].a will be
106 replaced by an access to field a of element i of array A_0: A_0[i].a.
108 This optimization also supports dynamically allocated arrays.
109 If array of structures was allocated by malloc function:
111 str_t * p = (str_t *) malloc (sizeof (str_t) * N)
113 the allocation site will be replaced to reflect new structure types:
115 str_t_0 * p_0 = (str_t_0 *) malloc (sizeof (str_t_0) * N)
116 str_t_1 * p_1 = (str_t_1 *) malloc (sizeof (str_t_1) * N)
118 The field access through the pointer p[i].a will be changed by p_0[i].a.
120 The goal of structure peeling is to improve spatial locality.
121 For example, if one of the fields of a structure is accessed frequently
124 for (i = 0; i < N; i++)
129 the allocation of field a of str_t contiguously in memory will
130 increase the chances of fetching the field from cache.
132 The analysis part of this optimization is based on the frequency of
133 field accesses, which are collected all over the program.
134 Then the fields with the frequencies that satisfy the following condition
135 get peeled out of the structure:
137 freq(f) > C * max_field_freq_in_struct
139 where max_field_freq_in_struct is the maximum field frequency
140 in the structure. C is a constant defining which portion of
141 max_field_freq_in_struct the fields should have in order to be peeled.
143 If profiling information is provided, it is used to calculate the
144 frequency of field accesses. Otherwise, the structure is fully peeled.
146 IPA type-escape analysis is used to determine when it is safe
149 The optimization is activated by flag -fipa-struct-reorg. */
151 /* New variables created by this optimization.
152 When doing struct peeling, each variable of
153 the original struct type will be replaced by
154 the set of new variables corresponding to
155 the new structure types. */
156 struct new_var_data
{
157 /* VAR_DECL for original struct type. */
159 /* Vector of new variables. */
160 VEC(tree
, heap
) *new_vars
;
163 typedef struct new_var_data
*new_var
;
164 typedef const struct new_var_data
*const_new_var
;
166 /* This structure represents allocation site of the structure. */
167 typedef struct alloc_site
173 DEF_VEC_O (alloc_site_t
);
174 DEF_VEC_ALLOC_O (alloc_site_t
, heap
);
176 /* Allocation sites that belong to the same function. */
177 struct func_alloc_sites
180 /* Vector of allocation sites for function. */
181 VEC (alloc_site_t
, heap
) *allocs
;
184 typedef struct func_alloc_sites
*fallocs_t
;
185 typedef const struct func_alloc_sites
*const_fallocs_t
;
187 /* All allocation sites in the program. */
188 htab_t alloc_sites
= NULL
;
190 /* New global variables. Generated once for whole program. */
191 htab_t new_global_vars
;
193 /* New local variables. Generated per-function. */
194 htab_t new_local_vars
;
196 /* Vector of structures to be transformed. */
197 typedef struct data_structure structure
;
198 DEF_VEC_O (structure
);
199 DEF_VEC_ALLOC_O (structure
, heap
);
200 VEC (structure
, heap
) *structures
;
202 /* Forward declarations. */
203 static bool is_equal_types (tree
, tree
);
205 /* Strip structure TYPE from pointers and arrays. */
208 strip_type (tree type
)
210 gcc_assert (TYPE_P (type
));
212 while (POINTER_TYPE_P (type
)
213 || TREE_CODE (type
) == ARRAY_TYPE
)
214 type
= TREE_TYPE (type
);
219 /* This function returns type of VAR. */
222 get_type_of_var (tree var
)
227 if (TREE_CODE (var
) == PARM_DECL
)
228 return DECL_ARG_TYPE (var
);
230 return TREE_TYPE (var
);
233 /* Set of actions we do for each newly generated STMT. */
236 finalize_stmt (gimple stmt
)
239 mark_symbols_for_renaming (stmt
);
242 /* This function finalizes STMT and appends it to the list STMTS. */
245 finalize_stmt_and_append (gimple_seq
*stmts
, gimple stmt
)
247 gimple_seq_add_stmt (stmts
, stmt
);
248 finalize_stmt (stmt
);
251 /* Given structure type SRT_TYPE and field FIELD,
252 this function is looking for a field with the same name
253 and type as FIELD in STR_TYPE. It returns it if found,
254 or NULL_TREE otherwise. */
257 find_field_in_struct_1 (tree str_type
, tree field
)
261 for (str_field
= TYPE_FIELDS (str_type
); str_field
;
262 str_field
= TREE_CHAIN (str_field
))
264 const char * str_field_name
;
265 const char * field_name
;
267 str_field_name
= IDENTIFIER_POINTER (DECL_NAME (str_field
));
268 field_name
= IDENTIFIER_POINTER (DECL_NAME (field
));
270 gcc_assert (str_field_name
);
271 gcc_assert (field_name
);
273 if (!strcmp (str_field_name
, field_name
))
275 /* Check field types. */
276 if (is_equal_types (TREE_TYPE (str_field
), TREE_TYPE (field
)))
284 /* Given a field declaration FIELD_DECL, this function
285 returns corresponding field entry in structure STR. */
287 static struct field_entry
*
288 find_field_in_struct (d_str str
, tree field_decl
)
292 tree field
= find_field_in_struct_1 (str
->decl
, field_decl
);
294 for (i
= 0; i
< str
->num_fields
; i
++)
295 if (str
->fields
[i
].decl
== field
)
296 return &(str
->fields
[i
]);
301 /* This function checks whether ARG is a result of multiplication
302 of some number by STRUCT_SIZE. If yes, the function returns true
303 and this number is filled into NUM. */
306 is_result_of_mult (tree arg
, tree
*num
, tree struct_size
)
308 gimple size_def_stmt
= SSA_NAME_DEF_STMT (arg
);
310 /* If the allocation statement was of the form
311 D.2229_10 = <alloc_func> (D.2228_9);
312 then size_def_stmt can be D.2228_9 = num.3_8 * 8; */
314 if (size_def_stmt
&& is_gimple_assign (size_def_stmt
))
316 tree lhs
= gimple_assign_lhs (size_def_stmt
);
318 /* We expect temporary here. */
319 if (!is_gimple_reg (lhs
))
322 if (gimple_assign_rhs_code (size_def_stmt
) == MULT_EXPR
)
324 tree arg0
= gimple_assign_rhs1 (size_def_stmt
);
325 tree arg1
= gimple_assign_rhs2 (size_def_stmt
);
327 if (operand_equal_p (arg0
, struct_size
, OEP_ONLY_CONST
))
333 if (operand_equal_p (arg1
, struct_size
, OEP_ONLY_CONST
))
346 /* This function returns true if access ACC corresponds to the pattern
347 generated by compiler when an address of element i of an array
348 of structures STR_DECL (pointed by p) is calculated (p[i]). If this
349 pattern is recognized correctly, this function returns true
350 and fills missing fields in ACC. Otherwise it returns false. */
353 decompose_indirect_ref_acc (tree str_decl
, struct field_access_site
*acc
)
356 tree struct_size
, op0
, op1
;
358 enum tree_code rhs_code
;
360 ref_var
= TREE_OPERAND (acc
->ref
, 0);
362 if (TREE_CODE (ref_var
) != SSA_NAME
)
365 acc
->ref_def_stmt
= SSA_NAME_DEF_STMT (ref_var
);
366 if (!(acc
->ref_def_stmt
)
367 || (gimple_code (acc
->ref_def_stmt
) != GIMPLE_ASSIGN
))
370 rhs_code
= gimple_assign_rhs_code (acc
->ref_def_stmt
);
372 if (rhs_code
!= PLUS_EXPR
373 && rhs_code
!= MINUS_EXPR
374 && rhs_code
!= POINTER_PLUS_EXPR
)
377 op0
= gimple_assign_rhs1 (acc
->ref_def_stmt
);
378 op1
= gimple_assign_rhs2 (acc
->ref_def_stmt
);
380 if (!is_array_access_through_pointer_and_index (rhs_code
, op0
, op1
,
381 &acc
->base
, &acc
->offset
,
386 before_cast
= SINGLE_SSA_TREE_OPERAND (acc
->cast_stmt
, SSA_OP_USE
);
388 before_cast
= acc
->offset
;
394 if (SSA_NAME_IS_DEFAULT_DEF (before_cast
))
397 struct_size
= TYPE_SIZE_UNIT (str_decl
);
399 if (!is_result_of_mult (before_cast
, &acc
->num
, struct_size
))
406 /* This function checks whether the access ACC of structure type STR
407 is of the form suitable for transformation. If yes, it returns true.
411 decompose_access (tree str_decl
, struct field_access_site
*acc
)
413 gcc_assert (acc
->ref
);
415 if (TREE_CODE (acc
->ref
) == INDIRECT_REF
)
416 return decompose_indirect_ref_acc (str_decl
, acc
);
417 else if (TREE_CODE (acc
->ref
) == ARRAY_REF
)
419 else if (TREE_CODE (acc
->ref
) == VAR_DECL
)
425 /* This function creates empty field_access_site node. */
427 static inline struct field_access_site
*
428 make_field_acc_node (void)
430 int size
= sizeof (struct field_access_site
);
432 return (struct field_access_site
*) xcalloc (1, size
);
435 /* This function returns the structure field access, defined by STMT,
436 if it is already in hashtable of function accesses F_ACCS. */
438 static struct field_access_site
*
439 is_in_field_accs (gimple stmt
, htab_t f_accs
)
441 return (struct field_access_site
*)
442 htab_find_with_hash (f_accs
, stmt
, htab_hash_pointer (stmt
));
445 /* This function adds an access ACC to the hashtable
446 F_ACCS of field accesses. */
449 add_field_acc_to_acc_sites (struct field_access_site
*acc
,
454 gcc_assert (!is_in_field_accs (acc
->stmt
, f_accs
));
455 slot
= htab_find_slot_with_hash (f_accs
, acc
->stmt
,
456 htab_hash_pointer (acc
->stmt
),
461 /* This function adds the VAR to vector of variables of
462 an access site defined by statement STMT. If access entry
463 with statement STMT does not exist in hashtable of
464 accesses ACCS, this function creates it. */
467 add_access_to_acc_sites (gimple stmt
, tree var
, htab_t accs
)
469 struct access_site
*acc
;
471 acc
= (struct access_site
*)
472 htab_find_with_hash (accs
, stmt
, htab_hash_pointer (stmt
));
478 acc
= (struct access_site
*) xmalloc (sizeof (struct access_site
));
480 acc
->vars
= VEC_alloc (tree
, heap
, 10);
481 slot
= htab_find_slot_with_hash (accs
, stmt
,
482 htab_hash_pointer (stmt
), INSERT
);
486 VEC_safe_push (tree
, heap
, acc
->vars
, var
);
489 /* This function adds NEW_DECL to function
490 referenced vars, and marks it for renaming. */
493 finalize_var_creation (tree new_decl
)
495 add_referenced_var (new_decl
);
496 mark_sym_for_renaming (new_decl
);
499 /* This function finalizes VAR creation if it is a global VAR_DECL. */
502 finalize_global_creation (tree var
)
504 if (TREE_CODE (var
) == VAR_DECL
505 && is_global_var (var
))
506 finalize_var_creation (var
);
509 /* This function inserts NEW_DECL to varpool. */
512 insert_global_to_varpool (tree new_decl
)
514 struct varpool_node
*new_node
;
516 new_node
= varpool_node (new_decl
);
517 notice_global_symbol (new_decl
);
518 varpool_mark_needed_node (new_node
);
519 varpool_finalize_decl (new_decl
);
522 /* This function finalizes the creation of new variables,
523 defined by *SLOT->new_vars. */
526 finalize_new_vars_creation (void **slot
, void *data ATTRIBUTE_UNUSED
)
528 new_var n_var
= *(new_var
*) slot
;
532 for (i
= 0; VEC_iterate (tree
, n_var
->new_vars
, i
, var
); i
++)
533 finalize_var_creation (var
);
537 /* This function looks for the variable of NEW_TYPE type, stored in VAR.
538 It returns it, if found, and NULL_TREE otherwise. */
541 find_var_in_new_vars_vec (new_var var
, tree new_type
)
546 for (i
= 0; VEC_iterate (tree
, var
->new_vars
, i
, n_var
); i
++)
548 tree type
= strip_type(get_type_of_var (n_var
));
551 if (type
== new_type
)
558 /* This function returns new_var node, the orig_var of which is DECL.
559 It looks for new_var's in NEW_VARS_HTAB. If not found,
560 the function returns NULL. */
563 is_in_new_vars_htab (tree decl
, htab_t new_vars_htab
)
565 return (new_var
) htab_find_with_hash (new_vars_htab
, decl
,
566 htab_hash_pointer (decl
));
569 /* Given original variable ORIG_VAR, this function returns
570 new variable corresponding to it of NEW_TYPE type. */
573 find_new_var_of_type (tree orig_var
, tree new_type
)
576 gcc_assert (orig_var
&& new_type
);
578 if (TREE_CODE (orig_var
) == SSA_NAME
)
579 orig_var
= SSA_NAME_VAR (orig_var
);
581 var
= is_in_new_vars_htab (orig_var
, new_global_vars
);
583 var
= is_in_new_vars_htab (orig_var
, new_local_vars
);
585 return find_var_in_new_vars_vec (var
, new_type
);
588 /* This function generates stmt:
589 res = NUM * sizeof(TYPE) and returns it.
590 res is filled into RES. */
593 gen_size (tree num
, tree type
, tree
*res
)
595 tree struct_size
= TYPE_SIZE_UNIT (type
);
596 HOST_WIDE_INT struct_size_int
= TREE_INT_CST_LOW (struct_size
);
599 *res
= create_tmp_var (TREE_TYPE (num
), NULL
);
602 add_referenced_var (*res
);
604 if (exact_log2 (struct_size_int
) == -1)
606 tree size
= build_int_cst (TREE_TYPE (num
), struct_size_int
);
607 new_stmt
= gimple_build_assign (*res
, fold_build2 (MULT_EXPR
,
613 tree C
= build_int_cst (TREE_TYPE (num
), exact_log2 (struct_size_int
));
615 new_stmt
= gimple_build_assign (*res
, fold_build2 (LSHIFT_EXPR
,
620 finalize_stmt (new_stmt
);
624 /* This function generates and returns a statement, that cast variable
625 BEFORE_CAST to NEW_TYPE. The cast result variable is stored
626 into RES_P. ORIG_CAST_STMT is the original cast statement. */
629 gen_cast_stmt (tree before_cast
, tree new_type
, gimple orig_cast_stmt
,
635 lhs
= gimple_assign_lhs (orig_cast_stmt
);
636 new_lhs
= find_new_var_of_type (lhs
, new_type
);
637 gcc_assert (new_lhs
);
639 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_lhs
, before_cast
, 0);
640 finalize_stmt (new_stmt
);
645 /* This function builds an edge between BB and E->dest and updates
646 phi nodes of E->dest. It returns newly created edge. */
649 make_edge_and_fix_phis_of_dest (basic_block bb
, edge e
)
653 gimple_stmt_iterator si
;
655 new_e
= make_edge (bb
, e
->dest
, e
->flags
);
657 for (si
= gsi_start_phis (new_e
->dest
); !gsi_end_p (si
); gsi_next (&si
))
659 gimple phi
= gsi_stmt (si
);
660 arg
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
661 add_phi_arg (phi
, arg
, new_e
);
667 /* This function inserts NEW_STMT before STMT. */
670 insert_before_stmt (gimple stmt
, gimple new_stmt
)
672 gimple_stmt_iterator bsi
;
674 if (!stmt
|| !new_stmt
)
677 bsi
= gsi_for_stmt (stmt
);
678 gsi_insert_before (&bsi
, new_stmt
, GSI_SAME_STMT
);
681 /* Insert NEW_STMTS after STMT. */
684 insert_seq_after_stmt (gimple stmt
, gimple_seq new_stmts
)
686 gimple_stmt_iterator bsi
;
688 if (!stmt
|| !new_stmts
)
691 bsi
= gsi_for_stmt (stmt
);
692 gsi_insert_seq_after (&bsi
, new_stmts
, GSI_SAME_STMT
);
695 /* Insert NEW_STMT after STMT. */
698 insert_after_stmt (gimple stmt
, gimple new_stmt
)
700 gimple_stmt_iterator bsi
;
702 if (!stmt
|| !new_stmt
)
705 bsi
= gsi_for_stmt (stmt
);
706 gsi_insert_after (&bsi
, new_stmt
, GSI_SAME_STMT
);
709 /* This function returns vector of allocation sites
710 that appear in function FN_DECL. */
713 get_fallocs (tree fn_decl
)
715 return (fallocs_t
) htab_find_with_hash (alloc_sites
, fn_decl
,
716 htab_hash_pointer (fn_decl
));
719 /* If ALLOC_STMT is D.2225_7 = <alloc_func> (D.2224_6);
720 and it is a part of allocation of a structure,
721 then it is usually followed by a cast stmt
722 p_8 = (struct str_t *) D.2225_7;
723 which is returned by this function. */
726 get_final_alloc_stmt (gimple alloc_stmt
)
735 if (!is_gimple_call (alloc_stmt
))
738 alloc_res
= gimple_get_lhs (alloc_stmt
);
740 if (TREE_CODE (alloc_res
) != SSA_NAME
)
743 if (!single_imm_use (alloc_res
, &use_p
, &final_stmt
))
749 /* This function returns true if STMT is one of allocation
750 sites of function FN_DECL. It returns false otherwise. */
753 is_part_of_malloc (gimple stmt
, tree fn_decl
)
755 fallocs_t fallocs
= get_fallocs (fn_decl
);
762 for (i
= 0; VEC_iterate (alloc_site_t
, fallocs
->allocs
, i
, call
); i
++)
763 if (call
->stmt
== stmt
764 || get_final_alloc_stmt (call
->stmt
) == stmt
)
770 /* Auxiliary structure for a lookup over field accesses. */
771 struct find_stmt_data
777 /* This function looks for DATA->stmt among
778 the statements involved in the field access,
779 defined by SLOT. It stops when it's found. */
782 find_in_field_accs (void **slot
, void *data
)
784 struct field_access_site
*f_acc
= *(struct field_access_site
**) slot
;
785 gimple stmt
= ((struct find_stmt_data
*)data
)->stmt
;
787 if (f_acc
->stmt
== stmt
788 || f_acc
->ref_def_stmt
== stmt
789 || f_acc
->cast_stmt
== stmt
)
791 ((struct find_stmt_data
*)data
)->found
= true;
798 /* This function checks whether STMT is part of field
799 accesses of structure STR. It returns true, if found,
800 and false otherwise. */
803 is_part_of_field_access (gimple stmt
, d_str str
)
807 for (i
= 0; i
< str
->num_fields
; i
++)
809 struct find_stmt_data data
;
813 if (str
->fields
[i
].acc_sites
)
814 htab_traverse (str
->fields
[i
].acc_sites
, find_in_field_accs
, &data
);
823 /* Auxiliary data for exclude_from_accs function. */
831 /* This function returns component_ref with the BASE and
832 field named FIELD_ID from structure TYPE. */
835 build_comp_ref (tree base
, tree field_id
, tree type
)
841 /* Find field of structure type with the same name as field_id. */
842 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
844 if (DECL_NAME (field
) == field_id
)
853 return build3 (COMPONENT_REF
, TREE_TYPE (field
), base
, field
, NULL_TREE
);
857 /* This struct represent data used for walk_tree
858 called from function find_pos_in_stmt.
859 - ref is a tree to be found,
860 - and pos is a pointer that points to ref in stmt. */
868 /* This is a callback function for walk_tree, called from
869 collect_accesses_in_bb function. DATA is a pointer to ref_pos structure.
870 When *TP is equal to DATA->ref, the walk_tree stops,
871 and found position, equal to TP, is assigned to DATA->pos. */
874 find_pos_in_stmt_1 (tree
*tp
, int *walk_subtrees
, void * data
)
876 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
877 struct ref_pos
*r_pos
= (struct ref_pos
*) wi
->info
;
878 tree ref
= r_pos
->ref
;
881 if (t
== ref
|| (TREE_CODE (t
) == SSA_NAME
&& SSA_NAME_VAR (t
) == ref
))
892 /* This function looks for the pointer of REF in STMT,
893 It returns it, if found, and NULL otherwise. */
896 find_pos_in_stmt (gimple stmt
, tree ref
)
898 struct ref_pos r_pos
;
899 struct walk_stmt_info wi
;
903 memset (&wi
, 0, sizeof (wi
));
905 walk_gimple_op (stmt
, find_pos_in_stmt_1
, &wi
);
910 /* This structure is used to represent array
911 or pointer-to wrappers of structure type.
912 For example, if type1 is structure type,
913 then for type1 ** we generate two type_wrapper
914 structures with wrap = 0 each one.
915 It's used to unwind the original type up to
916 structure type, replace it with the new structure type
917 and wrap it back in the opposite order. */
919 typedef struct type_wrapper
921 /* 0 stand for pointer wrapper, and 1 for array wrapper. */
924 /* Relevant for arrays as domain or index. */
928 DEF_VEC_O (type_wrapper_t
);
929 DEF_VEC_ALLOC_O (type_wrapper_t
, heap
);
931 /* This function replace field access ACC by the new
932 field access of structure type NEW_TYPE. */
935 replace_field_acc (struct field_access_site
*acc
, tree new_type
)
937 tree ref_var
= acc
->ref
;
942 tree field_id
= DECL_NAME (acc
->field_decl
);
943 VEC (type_wrapper_t
, heap
) *wrapper
= VEC_alloc (type_wrapper_t
, heap
, 10);
944 type_wrapper_t
*wr_p
= NULL
;
946 while (TREE_CODE (ref_var
) == INDIRECT_REF
947 || TREE_CODE (ref_var
) == ARRAY_REF
)
951 if ( TREE_CODE (ref_var
) == INDIRECT_REF
)
959 wr
.domain
= TREE_OPERAND (ref_var
, 1);
962 VEC_safe_push (type_wrapper_t
, heap
, wrapper
, &wr
);
963 ref_var
= TREE_OPERAND (ref_var
, 0);
966 new_ref
= find_new_var_of_type (ref_var
, new_type
);
967 finalize_global_creation (new_ref
);
969 while (VEC_length (type_wrapper_t
, wrapper
) != 0)
971 tree type
= TREE_TYPE (TREE_TYPE (new_ref
));
973 wr_p
= VEC_last (type_wrapper_t
, wrapper
);
974 if (wr_p
->wrap
) /* Array. */
975 new_ref
= build4 (ARRAY_REF
, type
, new_ref
,
976 wr_p
->domain
, NULL_TREE
, NULL_TREE
);
978 new_ref
= build1 (INDIRECT_REF
, type
, new_ref
);
979 VEC_pop (type_wrapper_t
, wrapper
);
982 new_acc
= build_comp_ref (new_ref
, field_id
, new_type
);
983 VEC_free (type_wrapper_t
, heap
, wrapper
);
985 if (is_gimple_assign (acc
->stmt
))
987 lhs
= gimple_assign_lhs (acc
->stmt
);
988 rhs
= gimple_assign_rhs1 (acc
->stmt
);
990 if (lhs
== acc
->comp_ref
)
991 gimple_assign_set_lhs (acc
->stmt
, new_acc
);
992 else if (rhs
== acc
->comp_ref
)
993 gimple_assign_set_rhs1 (acc
->stmt
, new_acc
);
996 pos
= find_pos_in_stmt (acc
->stmt
, acc
->comp_ref
);
1003 pos
= find_pos_in_stmt (acc
->stmt
, acc
->comp_ref
);
1008 finalize_stmt (acc
->stmt
);
1011 /* This function replace field access ACC by a new field access
1012 of structure type NEW_TYPE. */
1015 replace_field_access_stmt (struct field_access_site
*acc
, tree new_type
)
1018 if (TREE_CODE (acc
->ref
) == INDIRECT_REF
1019 ||TREE_CODE (acc
->ref
) == ARRAY_REF
1020 ||TREE_CODE (acc
->ref
) == VAR_DECL
)
1021 replace_field_acc (acc
, new_type
);
1026 /* This function looks for d_str, represented by TYPE, in the structures
1027 vector. If found, it returns an index of found structure. Otherwise
1028 it returns a length of the structures vector. */
1031 find_structure (tree type
)
1036 type
= TYPE_MAIN_VARIANT (type
);
1038 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
1039 if (is_equal_types (str
->decl
, type
))
1042 return VEC_length (structure
, structures
);
1045 /* In this function we create new statements that have the same
1046 form as ORIG_STMT, but of type NEW_TYPE. The statements
1047 treated by this function are simple assignments,
1048 like assignments: p.8_7 = p; or statements with rhs of
1049 tree codes PLUS_EXPR and MINUS_EXPR. */
1052 create_base_plus_offset (gimple orig_stmt
, tree new_type
, tree offset
)
1057 tree new_op0
= NULL_TREE
, new_op1
= NULL_TREE
;
1059 lhs
= gimple_assign_lhs (orig_stmt
);
1061 gcc_assert (TREE_CODE (lhs
) == VAR_DECL
1062 || TREE_CODE (lhs
) == SSA_NAME
);
1064 new_lhs
= find_new_var_of_type (lhs
, new_type
);
1065 gcc_assert (new_lhs
);
1066 finalize_var_creation (new_lhs
);
1068 switch (gimple_assign_rhs_code (orig_stmt
))
1072 case POINTER_PLUS_EXPR
:
1074 tree op0
= gimple_assign_rhs1 (orig_stmt
);
1075 tree op1
= gimple_assign_rhs2 (orig_stmt
);
1076 unsigned str0
, str1
;
1077 unsigned length
= VEC_length (structure
, structures
);
1080 str0
= find_structure (strip_type (get_type_of_var (op0
)));
1081 str1
= find_structure (strip_type (get_type_of_var (op1
)));
1082 gcc_assert ((str0
!= length
) || (str1
!= length
));
1085 new_op0
= find_new_var_of_type (op0
, new_type
);
1087 new_op1
= find_new_var_of_type (op1
, new_type
);
1100 new_stmt
= gimple_build_assign_with_ops (gimple_assign_rhs_code (orig_stmt
),
1101 new_lhs
, new_op0
, new_op1
);
1102 finalize_stmt (new_stmt
);
1107 /* Given a field access F_ACC of the FIELD, this function
1108 replaces it by the new field access. */
1111 create_new_field_access (struct field_access_site
*f_acc
,
1112 struct field_entry field
)
1114 tree new_type
= field
.field_mapping
;
1119 tree cast_res
= NULL
;
1123 mult_stmt
= gen_size (f_acc
->num
, new_type
, &size_res
);
1124 insert_before_stmt (f_acc
->ref_def_stmt
, mult_stmt
);
1127 if (f_acc
->cast_stmt
)
1129 cast_stmt
= gen_cast_stmt (size_res
, new_type
,
1130 f_acc
->cast_stmt
, &cast_res
);
1131 insert_after_stmt (f_acc
->cast_stmt
, cast_stmt
);
1134 if (f_acc
->ref_def_stmt
)
1142 new_stmt
= create_base_plus_offset (f_acc
->ref_def_stmt
,
1144 insert_after_stmt (f_acc
->ref_def_stmt
, new_stmt
);
1147 /* In stmt D.2163_19 = D.2162_18->b; we replace variable
1148 D.2162_18 by an appropriate variable of new_type type. */
1149 replace_field_access_stmt (f_acc
, new_type
);
1152 /* This function creates a new condition statement
1153 corresponding to the original COND_STMT, adds new basic block
1154 and redirects condition edges. NEW_VAR is a new condition
1155 variable located in the condition statement at the position POS. */
1158 create_new_stmts_for_cond_expr_1 (tree new_var
, gimple cond_stmt
, unsigned pos
)
1161 edge true_e
= NULL
, false_e
= NULL
;
1163 gimple_stmt_iterator si
;
1165 extract_true_false_edges_from_block (gimple_bb (cond_stmt
),
1168 new_stmt
= gimple_build_cond (gimple_cond_code (cond_stmt
),
1169 pos
== 0 ? new_var
: gimple_cond_lhs (cond_stmt
),
1170 pos
== 1 ? new_var
: gimple_cond_rhs (cond_stmt
),
1174 finalize_stmt (new_stmt
);
1176 /* Create new basic block after bb. */
1177 new_bb
= create_empty_bb (gimple_bb (cond_stmt
));
1179 /* Add new condition stmt to the new_bb. */
1180 si
= gsi_start_bb (new_bb
);
1181 gsi_insert_after (&si
, new_stmt
, GSI_NEW_STMT
);
1183 /* Create false and true edges from new_bb. */
1184 make_edge_and_fix_phis_of_dest (new_bb
, true_e
);
1185 make_edge_and_fix_phis_of_dest (new_bb
, false_e
);
1187 /* Redirect one of original edges to point to new_bb. */
1188 if (gimple_cond_code (cond_stmt
) == NE_EXPR
)
1189 redirect_edge_succ (true_e
, new_bb
);
1191 redirect_edge_succ (false_e
, new_bb
);
1194 /* This function creates new condition statements corresponding
1195 to original condition STMT, one for each new type, and
1196 recursively redirect edges to newly generated basic blocks. */
1199 create_new_stmts_for_cond_expr (gimple stmt
)
1201 tree arg0
, arg1
, arg
;
1202 unsigned str0
, str1
;
1208 unsigned length
= VEC_length (structure
, structures
);
1210 gcc_assert (gimple_cond_code (stmt
) == EQ_EXPR
1211 || gimple_cond_code (stmt
) == NE_EXPR
);
1213 arg0
= gimple_cond_lhs (stmt
);
1214 arg1
= gimple_cond_rhs (stmt
);
1216 str0
= find_structure (strip_type (get_type_of_var (arg0
)));
1217 str1
= find_structure (strip_type (get_type_of_var (arg1
)));
1219 s0
= (str0
!= length
) ? true : false;
1220 s1
= (str1
!= length
) ? true : false;
1222 gcc_assert (s0
|| s1
);
1223 /* For now we allow only comparison with 0 or NULL. */
1224 gcc_assert (integer_zerop (arg0
) || integer_zerop (arg1
));
1226 str
= integer_zerop (arg0
) ?
1227 VEC_index (structure
, structures
, str1
):
1228 VEC_index (structure
, structures
, str0
);
1229 arg
= integer_zerop (arg0
) ? arg1
: arg0
;
1230 pos
= integer_zerop (arg0
) ? 1 : 0;
1232 for (i
= 0; VEC_iterate (tree
, str
->new_types
, i
, type
); i
++)
1236 new_arg
= find_new_var_of_type (arg
, type
);
1237 create_new_stmts_for_cond_expr_1 (new_arg
, stmt
, pos
);
1241 /* Create a new general access to replace original access ACC
1242 for structure type NEW_TYPE. */
1245 create_general_new_stmt (struct access_site
*acc
, tree new_type
)
1247 gimple old_stmt
= acc
->stmt
;
1249 gimple new_stmt
= gimple_copy (old_stmt
);
1252 /* We are really building a new stmt, clear the virtual operands. */
1253 if (gimple_has_mem_ops (new_stmt
))
1255 gimple_set_vuse (new_stmt
, NULL_TREE
);
1256 gimple_set_vdef (new_stmt
, NULL_TREE
);
1259 for (i
= 0; VEC_iterate (tree
, acc
->vars
, i
, var
); i
++)
1262 tree new_var
= find_new_var_of_type (var
, new_type
);
1263 tree lhs
, rhs
= NULL_TREE
;
1265 gcc_assert (new_var
);
1266 finalize_var_creation (new_var
);
1268 if (is_gimple_assign (new_stmt
))
1270 lhs
= gimple_assign_lhs (new_stmt
);
1272 if (TREE_CODE (lhs
) == SSA_NAME
)
1273 lhs
= SSA_NAME_VAR (lhs
);
1274 if (gimple_assign_rhs_code (new_stmt
) == SSA_NAME
)
1275 rhs
= SSA_NAME_VAR (gimple_assign_rhs1 (new_stmt
));
1277 /* It can happen that rhs is a constructor.
1278 Then we have to replace it to be of new_type. */
1279 if (gimple_assign_rhs_code (new_stmt
) == CONSTRUCTOR
)
1281 /* Dealing only with empty constructors right now. */
1282 gcc_assert (VEC_empty (constructor_elt
,
1283 CONSTRUCTOR_ELTS (rhs
)));
1284 rhs
= build_constructor (new_type
, 0);
1285 gimple_assign_set_rhs1 (new_stmt
, rhs
);
1289 gimple_assign_set_lhs (new_stmt
, new_var
);
1290 else if (rhs
== var
)
1291 gimple_assign_set_rhs1 (new_stmt
, new_var
);
1294 pos
= find_pos_in_stmt (new_stmt
, var
);
1296 /* ??? This misses adjustments to the type of the
1297 INDIRECT_REF we possibly replace the operand of. */
1303 pos
= find_pos_in_stmt (new_stmt
, var
);
1309 finalize_stmt (new_stmt
);
1313 /* For each new type in STR this function creates new general accesses
1314 corresponding to the original access ACC. */
1317 create_new_stmts_for_general_acc (struct access_site
*acc
, d_str str
)
1320 gimple stmt
= acc
->stmt
;
1323 for (i
= 0; VEC_iterate (tree
, str
->new_types
, i
, type
); i
++)
1327 new_stmt
= create_general_new_stmt (acc
, type
);
1328 insert_after_stmt (stmt
, new_stmt
);
1332 /* This function creates a new general access of structure STR
1333 to replace the access ACC. */
1336 create_new_general_access (struct access_site
*acc
, d_str str
)
1338 gimple stmt
= acc
->stmt
;
1339 switch (gimple_code (stmt
))
1342 create_new_stmts_for_cond_expr (stmt
);
1346 create_new_stmts_for_general_acc (acc
, str
);
1350 /* Auxiliary data for creation of accesses. */
1351 struct create_acc_data
1358 /* This function creates a new general access, defined by SLOT.
1359 DATA is a pointer to create_acc_data structure. */
1362 create_new_acc (void **slot
, void *data
)
1364 struct access_site
*acc
= *(struct access_site
**) slot
;
1365 basic_block bb
= ((struct create_acc_data
*)data
)->bb
;
1366 d_str str
= ((struct create_acc_data
*)data
)->str
;
1368 if (gimple_bb (acc
->stmt
) == bb
)
1369 create_new_general_access (acc
, str
);
1373 /* This function creates a new field access, defined by SLOT.
1374 DATA is a pointer to create_acc_data structure. */
1377 create_new_field_acc (void **slot
, void *data
)
1379 struct field_access_site
*f_acc
= *(struct field_access_site
**) slot
;
1380 basic_block bb
= ((struct create_acc_data
*)data
)->bb
;
1381 d_str str
= ((struct create_acc_data
*)data
)->str
;
1382 int i
= ((struct create_acc_data
*)data
)->field_index
;
1384 if (gimple_bb (f_acc
->stmt
) == bb
)
1385 create_new_field_access (f_acc
, str
->fields
[i
]);
1389 /* This function creates new accesses for the structure
1390 type STR in basic block BB. */
1393 create_new_accs_for_struct (d_str str
, basic_block bb
)
1396 struct create_acc_data dt
;
1400 dt
.field_index
= -1;
1402 for (i
= 0; i
< str
->num_fields
; i
++)
1406 if (str
->fields
[i
].acc_sites
)
1407 htab_traverse (str
->fields
[i
].acc_sites
,
1408 create_new_field_acc
, &dt
);
1411 htab_traverse (str
->accs
, create_new_acc
, &dt
);
1414 /* This function inserts new variables from new_var,
1415 defined by SLOT, into varpool. */
1418 update_varpool_with_new_var (void **slot
, void *data ATTRIBUTE_UNUSED
)
1420 new_var n_var
= *(new_var
*) slot
;
1424 for (i
= 0; VEC_iterate (tree
, n_var
->new_vars
, i
, var
); i
++)
1425 insert_global_to_varpool (var
);
1429 /* This function prints a field access site, defined by SLOT. */
1432 dump_field_acc (void **slot
, void *data ATTRIBUTE_UNUSED
)
1434 struct field_access_site
*f_acc
=
1435 *(struct field_access_site
**) slot
;
1437 fprintf(dump_file
, "\n");
1439 print_gimple_stmt (dump_file
, f_acc
->stmt
, 0, 0);
1440 if (f_acc
->ref_def_stmt
)
1441 print_gimple_stmt (dump_file
, f_acc
->ref_def_stmt
, 0, 0);
1442 if (f_acc
->cast_stmt
)
1443 print_gimple_stmt (dump_file
, f_acc
->cast_stmt
, 0, 0);
1447 /* Print field accesses from hashtable F_ACCS. */
1450 dump_field_acc_sites (htab_t f_accs
)
1456 htab_traverse (f_accs
, dump_field_acc
, NULL
);
1459 /* Hash value for fallocs_t. */
1462 malloc_hash (const void *x
)
1464 return htab_hash_pointer (((const_fallocs_t
)x
)->func
);
1467 /* This function returns nonzero if function of func_alloc_sites' X
1471 malloc_eq (const void *x
, const void *y
)
1473 return ((const_fallocs_t
)x
)->func
== (const_tree
)y
;
1476 /* This function is a callback for traversal over a structure accesses.
1477 It frees an access represented by SLOT. */
1480 free_accs (void **slot
, void *data ATTRIBUTE_UNUSED
)
1482 struct access_site
* acc
= *(struct access_site
**) slot
;
1484 VEC_free (tree
, heap
, acc
->vars
);
1489 /* This is a callback function for traversal over field accesses.
1490 It frees a field access represented by SLOT. */
1493 free_field_accs (void **slot
, void *data ATTRIBUTE_UNUSED
)
1495 struct field_access_site
*f_acc
= *(struct field_access_site
**) slot
;
1501 /* This function inserts TYPE into vector of UNSUITABLE_TYPES,
1502 if it is not there yet. */
1505 add_unsuitable_type (VEC (tree
, heap
) **unsuitable_types
, tree type
)
1513 type
= TYPE_MAIN_VARIANT (type
);
1515 for (i
= 0; VEC_iterate (tree
, *unsuitable_types
, i
, t
); i
++)
1516 if (is_equal_types (t
, type
))
1519 if (i
== VEC_length (tree
, *unsuitable_types
))
1520 VEC_safe_push (tree
, heap
, *unsuitable_types
, type
);
1523 /* Given a type TYPE, this function returns the name of the type. */
1526 get_type_name (tree type
)
1528 if (! TYPE_NAME (type
))
1531 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
1532 return IDENTIFIER_POINTER (TYPE_NAME (type
));
1533 else if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
1534 && DECL_NAME (TYPE_NAME (type
)))
1535 return IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
1540 /* This function is a temporary hack to overcome the types problem.
1541 When several compilation units are compiled together
1542 with -combine, the TYPE_MAIN_VARIANT of the same type
1543 can appear differently in different compilation units.
1544 Therefore this function first compares type names.
1545 If there are no names, structure bodies are recursively
1549 is_equal_types (tree type1
, tree type2
)
1551 const char * name1
,* name2
;
1553 if ((!type1
&& type2
)
1554 ||(!type2
&& type1
))
1557 if (!type1
&& !type2
)
1560 if (TREE_CODE (type1
) != TREE_CODE (type2
))
1566 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
1569 name1
= get_type_name (type1
);
1570 name2
= get_type_name (type2
);
1572 if (name1
&& name2
&& !strcmp (name1
, name2
))
1575 if (name1
&& name2
&& strcmp (name1
, name2
))
1578 switch (TREE_CODE (type1
))
1581 case REFERENCE_TYPE
:
1583 return is_equal_types (TREE_TYPE (type1
), TREE_TYPE (type2
));
1589 case QUAL_UNION_TYPE
:
1593 /* Compare fields of structure. */
1594 for (field1
= TYPE_FIELDS (type1
); field1
;
1595 field1
= TREE_CHAIN (field1
))
1597 tree field2
= find_field_in_struct_1 (type2
, field1
);
1607 if (TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
1608 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
))
1616 tree max1
, min1
, max2
, min2
;
1618 if (!is_equal_types (TREE_TYPE (type1
), TREE_TYPE (type2
)))
1621 d1
= TYPE_DOMAIN (type1
);
1622 d2
= TYPE_DOMAIN (type2
);
1627 max1
= TYPE_MAX_VALUE (d1
);
1628 max2
= TYPE_MAX_VALUE (d2
);
1629 min1
= TYPE_MIN_VALUE (d1
);
1630 min2
= TYPE_MIN_VALUE (d2
);
1632 if (max1
&& max2
&& min1
&& min2
1633 && TREE_CODE (max1
) == TREE_CODE (max2
)
1634 && TREE_CODE (max1
) == INTEGER_CST
1635 && TREE_CODE (min1
) == TREE_CODE (min2
)
1636 && TREE_CODE (min1
) == INTEGER_CST
1637 && tree_int_cst_equal (max1
, max2
)
1638 && tree_int_cst_equal (min1
, min2
))
1650 /* This function free non-field accesses from hashtable ACCS. */
1653 free_accesses (htab_t accs
)
1656 htab_traverse (accs
, free_accs
, NULL
);
1660 /* This function free field accesses hashtable F_ACCS. */
1663 free_field_accesses (htab_t f_accs
)
1666 htab_traverse (f_accs
, free_field_accs
, NULL
);
1667 htab_delete (f_accs
);
1670 /* Update call graph with new edge generated by new MALLOC_STMT.
1671 The edge origin is CONTEXT function. */
1674 update_cgraph_with_malloc_call (gimple malloc_stmt
, tree context
)
1676 struct cgraph_node
*src
, *dest
;
1677 tree malloc_fn_decl
;
1682 malloc_fn_decl
= gimple_call_fndecl (malloc_stmt
);
1684 src
= cgraph_node (context
);
1685 dest
= cgraph_node (malloc_fn_decl
);
1686 cgraph_create_edge (src
, dest
, malloc_stmt
,
1687 0, 0, gimple_bb (malloc_stmt
)->loop_depth
);
1690 /* This function generates set of statements required
1691 to allocate number NUM of structures of type NEW_TYPE.
1692 The statements are stored in NEW_STMTS. The statement that contain
1693 call to malloc is returned. MALLOC_STMT is an original call to malloc. */
1696 create_new_malloc (gimple malloc_stmt
, tree new_type
, gimple_seq
*new_stmts
,
1699 tree new_malloc_size
;
1700 tree malloc_fn_decl
;
1703 gimple call_stmt
, final_stmt
;
1706 gcc_assert (num
&& malloc_stmt
&& new_type
);
1707 *new_stmts
= gimple_seq_alloc ();
1709 /* Generate argument to malloc as multiplication of num
1710 and size of new_type. */
1711 new_stmt
= gen_size (num
, new_type
, &new_malloc_size
);
1712 gimple_seq_add_stmt (new_stmts
, new_stmt
);
1714 /* Generate new call for malloc. */
1715 malloc_res
= create_tmp_var (ptr_type_node
, NULL
);
1716 add_referenced_var (malloc_res
);
1718 malloc_fn_decl
= gimple_call_fndecl (malloc_stmt
);
1719 call_stmt
= gimple_build_call (malloc_fn_decl
, 1, new_malloc_size
);
1720 gimple_call_set_lhs (call_stmt
, malloc_res
);
1721 finalize_stmt_and_append (new_stmts
, call_stmt
);
1723 /* Create new cast statement. */
1724 final_stmt
= get_final_alloc_stmt (malloc_stmt
);
1725 gcc_assert (final_stmt
);
1726 new_stmt
= gen_cast_stmt (malloc_res
, new_type
, final_stmt
, &cast_res
);
1727 gimple_seq_add_stmt (new_stmts
, new_stmt
);
1732 /* This function returns a tree representing
1733 the number of instances of structure STR_DECL allocated
1734 by allocation STMT. If new statements are generated,
1735 they are filled into NEW_STMTS_P. */
1738 gen_num_of_structs_in_malloc (gimple stmt
, tree str_decl
,
1739 gimple_seq
*new_stmts_p
)
1743 HOST_WIDE_INT struct_size_int
;
1748 /* Get malloc argument. */
1749 if (!is_gimple_call (stmt
))
1752 arg
= gimple_call_arg (stmt
, 0);
1754 if (TREE_CODE (arg
) != SSA_NAME
1755 && !TREE_CONSTANT (arg
))
1758 struct_size
= TYPE_SIZE_UNIT (str_decl
);
1759 struct_size_int
= TREE_INT_CST_LOW (struct_size
);
1761 gcc_assert (struct_size
);
1763 if (TREE_CODE (arg
) == SSA_NAME
)
1768 if (is_result_of_mult (arg
, &num
, struct_size
))
1771 num
= create_tmp_var (integer_type_node
, NULL
);
1774 add_referenced_var (num
);
1776 if (exact_log2 (struct_size_int
) == -1)
1777 div_stmt
= gimple_build_assign_with_ops (TRUNC_DIV_EXPR
, num
, arg
,
1781 tree C
= build_int_cst (integer_type_node
,
1782 exact_log2 (struct_size_int
));
1784 div_stmt
= gimple_build_assign_with_ops (RSHIFT_EXPR
, num
, arg
, C
);
1786 gimple_seq_add_stmt (new_stmts_p
, div_stmt
);
1787 finalize_stmt (div_stmt
);
1791 if (CONSTANT_CLASS_P (arg
)
1792 && multiple_of_p (TREE_TYPE (struct_size
), arg
, struct_size
))
1793 return int_const_binop (TRUNC_DIV_EXPR
, arg
, struct_size
, 0);
1798 /* This function is a callback for traversal on new_var's hashtable.
1799 SLOT is a pointer to new_var. This function prints to dump_file
1800 an original variable and all new variables from the new_var
1801 pointed by *SLOT. */
1804 dump_new_var (void **slot
, void *data ATTRIBUTE_UNUSED
)
1806 new_var n_var
= *(new_var
*) slot
;
1811 var_type
= get_type_of_var (n_var
->orig_var
);
1813 fprintf (dump_file
, "\nOrig var: ");
1814 print_generic_expr (dump_file
, n_var
->orig_var
, 0);
1815 fprintf (dump_file
, " of type ");
1816 print_generic_expr (dump_file
, var_type
, 0);
1817 fprintf (dump_file
, "\n");
1820 VEC_iterate (tree
, n_var
->new_vars
, i
, var
); i
++)
1822 var_type
= get_type_of_var (var
);
1824 fprintf (dump_file
, " ");
1825 print_generic_expr (dump_file
, var
, 0);
1826 fprintf (dump_file
, " of type ");
1827 print_generic_expr (dump_file
, var_type
, 0);
1828 fprintf (dump_file
, "\n");
1833 /* This function copies attributes form ORIG_DECL to NEW_DECL. */
1836 copy_decl_attributes (tree new_decl
, tree orig_decl
)
1839 DECL_ARTIFICIAL (new_decl
) = 1;
1840 DECL_EXTERNAL (new_decl
) = DECL_EXTERNAL (orig_decl
);
1841 TREE_STATIC (new_decl
) = TREE_STATIC (orig_decl
);
1842 TREE_PUBLIC (new_decl
) = TREE_PUBLIC (orig_decl
);
1843 TREE_USED (new_decl
) = TREE_USED (orig_decl
);
1844 DECL_CONTEXT (new_decl
) = DECL_CONTEXT (orig_decl
);
1845 TREE_THIS_VOLATILE (new_decl
) = TREE_THIS_VOLATILE (orig_decl
);
1846 TREE_ADDRESSABLE (new_decl
) = TREE_ADDRESSABLE (orig_decl
);
1848 if (TREE_CODE (orig_decl
) == VAR_DECL
)
1850 TREE_READONLY (new_decl
) = TREE_READONLY (orig_decl
);
1851 DECL_TLS_MODEL (new_decl
) = DECL_TLS_MODEL (orig_decl
);
1855 /* This function wraps NEW_STR_TYPE in pointers or arrays wrapper
1856 the same way as a structure type is wrapped in DECL.
1857 It returns the generated type. */
1860 gen_struct_type (tree decl
, tree new_str_type
)
1862 tree type_orig
= get_type_of_var (decl
);
1863 tree new_type
= new_str_type
;
1864 VEC (type_wrapper_t
, heap
) *wrapper
= VEC_alloc (type_wrapper_t
, heap
, 10);
1866 type_wrapper_t
*wr_p
;
1868 while (POINTER_TYPE_P (type_orig
)
1869 || TREE_CODE (type_orig
) == ARRAY_TYPE
)
1871 if (POINTER_TYPE_P (type_orig
))
1874 wr
.domain
= NULL_TREE
;
1878 gcc_assert (TREE_CODE (type_orig
) == ARRAY_TYPE
);
1880 wr
.domain
= TYPE_DOMAIN (type_orig
);
1882 VEC_safe_push (type_wrapper_t
, heap
, wrapper
, &wr
);
1883 type_orig
= TREE_TYPE (type_orig
);
1886 while (VEC_length (type_wrapper_t
, wrapper
) != 0)
1888 wr_p
= VEC_last (type_wrapper_t
, wrapper
);
1890 if (wr_p
->wrap
) /* Array. */
1891 new_type
= build_array_type (new_type
, wr_p
->domain
);
1893 new_type
= build_pointer_type (new_type
);
1895 VEC_pop (type_wrapper_t
, wrapper
);
1898 VEC_free (type_wrapper_t
, heap
, wrapper
);
1902 /* This function generates and returns new variable name based on
1903 ORIG_DECL name, combined with index I.
1904 The form of the new name is <orig_name>.<I> . */
1907 gen_var_name (tree orig_decl
, unsigned HOST_WIDE_INT i
)
1909 const char *old_name
;
1913 if (!DECL_NAME (orig_decl
)
1914 || !IDENTIFIER_POINTER (DECL_NAME (orig_decl
)))
1917 /* If the original variable has a name, create an
1918 appropriate new name for the new variable. */
1920 old_name
= IDENTIFIER_POINTER (DECL_NAME (orig_decl
));
1921 prefix
= XALLOCAVEC (char, strlen (old_name
) + 1);
1922 strcpy (prefix
, old_name
);
1923 ASM_FORMAT_PRIVATE_NAME (new_name
, prefix
, i
);
1924 return get_identifier (new_name
);
1927 /* This function adds NEW_NODE to hashtable of new_var's NEW_VARS_HTAB. */
1930 add_to_new_vars_htab (new_var new_node
, htab_t new_vars_htab
)
1934 slot
= htab_find_slot_with_hash (new_vars_htab
, new_node
->orig_var
,
1935 htab_hash_pointer (new_node
->orig_var
),
1940 /* This function creates and returns new_var_data node
1941 with empty new_vars and orig_var equal to VAR. */
1944 create_new_var_node (tree var
, d_str str
)
1948 node
= (new_var
) xmalloc (sizeof (struct new_var_data
));
1949 node
->orig_var
= var
;
1950 node
->new_vars
= VEC_alloc (tree
, heap
, VEC_length (tree
, str
->new_types
));
1954 /* Check whether the type of VAR is potential candidate for peeling.
1955 Returns true if yes, false otherwise. If yes, TYPE_P will contain
1956 candidate type. If VAR is initialized, the type of VAR will be added
1957 to UNSUITABLE_TYPES. */
1960 is_candidate (tree var
, tree
*type_p
, VEC (tree
, heap
) **unsuitable_types
)
1963 bool initialized
= false;
1970 /* There is no support of initialized vars. */
1971 if (TREE_CODE (var
) == VAR_DECL
1972 && DECL_INITIAL (var
) != NULL_TREE
)
1975 type
= get_type_of_var (var
);
1979 type
= TYPE_MAIN_VARIANT (strip_type (type
));
1980 if (TREE_CODE (type
) != RECORD_TYPE
)
1984 if (initialized
&& unsuitable_types
&& *unsuitable_types
)
1988 fprintf (dump_file
, "The type ");
1989 print_generic_expr (dump_file
, type
, 0);
1990 fprintf (dump_file
, " is initialized...Excluded.");
1992 add_unsuitable_type (unsuitable_types
, type
);
2002 /* Hash value for field_access_site. */
2005 field_acc_hash (const void *x
)
2007 return htab_hash_pointer (((const struct field_access_site
*)x
)->stmt
);
2010 /* This function returns nonzero if stmt of field_access_site X
2014 field_acc_eq (const void *x
, const void *y
)
2016 return ((const struct field_access_site
*)x
)->stmt
== (const_gimple
)y
;
2019 /* This function prints an access site, defined by SLOT. */
2022 dump_acc (void **slot
, void *data ATTRIBUTE_UNUSED
)
2024 struct access_site
*acc
= *(struct access_site
**) slot
;
2028 fprintf(dump_file
, "\n");
2030 print_gimple_stmt (dump_file
, acc
->stmt
, 0, 0);
2031 fprintf(dump_file
, " : ");
2033 for (i
= 0; VEC_iterate (tree
, acc
->vars
, i
, var
); i
++)
2035 print_generic_expr (dump_file
, var
, 0);
2036 fprintf(dump_file
, ", ");
2041 /* This function frees memory allocated for structure clusters,
2042 starting from CLUSTER. */
2045 free_struct_cluster (struct field_cluster
* cluster
)
2049 if (cluster
->fields_in_cluster
)
2050 sbitmap_free (cluster
->fields_in_cluster
);
2051 if (cluster
->sibling
)
2052 free_struct_cluster (cluster
->sibling
);
2057 /* Free all allocated memory under the structure node pointed by D_NODE. */
2060 free_data_struct (d_str d_node
)
2069 fprintf (dump_file
, "\nRemoving data structure \"");
2070 print_generic_expr (dump_file
, d_node
->decl
, 0);
2071 fprintf (dump_file
, "\" from data_struct_list.");
2074 /* Free all space under d_node. */
2077 for (i
= 0; i
< d_node
->num_fields
; i
++)
2078 free_field_accesses (d_node
->fields
[i
].acc_sites
);
2079 free (d_node
->fields
);
2083 free_accesses (d_node
->accs
);
2085 if (d_node
->struct_clustering
)
2086 free_struct_cluster (d_node
->struct_clustering
);
2088 if (d_node
->new_types
)
2089 VEC_free (tree
, heap
, d_node
->new_types
);
2092 /* This function creates new general and field accesses in BB. */
2095 create_new_accesses_in_bb (basic_block bb
)
2100 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
2101 create_new_accs_for_struct (str
, bb
);
2104 /* This function adds allocation sites for peeled structures.
2105 M_DATA is vector of allocation sites of function CONTEXT. */
2108 create_new_alloc_sites (fallocs_t m_data
, tree context
)
2113 for (j
= 0; VEC_iterate (alloc_site_t
, m_data
->allocs
, j
, call
); j
++)
2115 gimple stmt
= call
->stmt
;
2116 d_str str
= call
->str
;
2118 gimple_seq new_stmts
= NULL
;
2119 gimple last_stmt
= get_final_alloc_stmt (stmt
);
2123 num
= gen_num_of_structs_in_malloc (stmt
, str
->decl
, &new_stmts
);
2126 gimple last_stmt_tmp
= gimple_seq_last_stmt (new_stmts
);
2127 insert_seq_after_stmt (last_stmt
, new_stmts
);
2128 last_stmt
= last_stmt_tmp
;
2131 /* Generate an allocation sites for each new structure type. */
2132 for (i
= 0; VEC_iterate (tree
, str
->new_types
, i
, type
); i
++)
2134 gimple new_malloc_stmt
= NULL
;
2135 gimple last_stmt_tmp
= NULL
;
2138 new_malloc_stmt
= create_new_malloc (stmt
, type
, &new_stmts
, num
);
2139 last_stmt_tmp
= gimple_seq_last_stmt (new_stmts
);
2140 insert_seq_after_stmt (last_stmt
, new_stmts
);
2141 update_cgraph_with_malloc_call (new_malloc_stmt
, context
);
2142 last_stmt
= last_stmt_tmp
;
2147 /* This function prints new variables from hashtable
2148 NEW_VARS_HTAB to dump_file. */
2151 dump_new_vars (htab_t new_vars_htab
)
2157 htab_traverse (new_vars_htab
, dump_new_var
, NULL
);
2160 /* Given an original variable ORIG_DECL of structure type STR,
2161 this function generates new variables of the types defined
2162 by STR->new_type. Generated types are saved in new_var node NODE.
2163 ORIG_DECL should has VAR_DECL tree_code. */
2166 create_new_var_1 (tree orig_decl
, d_str str
, new_var node
)
2172 VEC_iterate (tree
, str
->new_types
, i
, type
); i
++)
2174 tree new_decl
= NULL
;
2177 new_name
= gen_var_name (orig_decl
, i
);
2178 type
= gen_struct_type (orig_decl
, type
);
2180 if (is_global_var (orig_decl
))
2181 new_decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
2182 VAR_DECL
, new_name
, type
);
2185 const char *name
= new_name
? IDENTIFIER_POINTER (new_name
) : NULL
;
2186 new_decl
= create_tmp_var (type
, name
);
2189 copy_decl_attributes (new_decl
, orig_decl
);
2190 VEC_safe_push (tree
, heap
, node
->new_vars
, new_decl
);
2194 /* This function creates new variables to
2195 substitute the original variable VAR_DECL and adds
2196 them to the new_var's hashtable NEW_VARS_HTAB. */
2199 create_new_var (tree var_decl
, htab_t new_vars_htab
)
2206 if (!var_decl
|| is_in_new_vars_htab (var_decl
, new_vars_htab
))
2209 if (!is_candidate (var_decl
, &type
, NULL
))
2212 i
= find_structure (type
);
2213 if (i
== VEC_length (structure
, structures
))
2216 str
= VEC_index (structure
, structures
, i
);
2217 node
= create_new_var_node (var_decl
, str
);
2218 create_new_var_1 (var_decl
, str
, node
);
2219 add_to_new_vars_htab (node
, new_vars_htab
);
2222 /* Hash value for new_var. */
2225 new_var_hash (const void *x
)
2227 return htab_hash_pointer (((const_new_var
)x
)->orig_var
);
2230 /* This function returns nonzero if orig_var of new_var X is equal to Y. */
2233 new_var_eq (const void *x
, const void *y
)
2235 return ((const_new_var
)x
)->orig_var
== (const_tree
)y
;
2238 /* This function check whether a structure type represented by STR
2239 escapes due to ipa-type-escape analysis. If yes, this type is added
2240 to UNSUITABLE_TYPES vector. */
2243 check_type_escape (d_str str
, VEC (tree
, heap
) **unsuitable_types
)
2245 tree type
= str
->decl
;
2247 if (!ipa_type_escape_type_contained_p (type
))
2251 fprintf (dump_file
, "\nEscaping type is ");
2252 print_generic_expr (dump_file
, type
, 0);
2254 add_unsuitable_type (unsuitable_types
, type
);
2258 /* Hash value for access_site. */
2261 acc_hash (const void *x
)
2263 return htab_hash_pointer (((const struct access_site
*)x
)->stmt
);
2266 /* Return nonzero if stmt of access_site X is equal to Y. */
2269 acc_eq (const void *x
, const void *y
)
2271 return ((const struct access_site
*)x
)->stmt
== (const_gimple
)y
;
2274 /* Given a structure declaration STRUCT_DECL, and number of fields
2275 in the structure NUM_FIELDS, this function creates and returns
2276 corresponding field_entry's. */
2278 static struct field_entry
*
2279 get_fields (tree struct_decl
, int num_fields
)
2281 struct field_entry
*list
;
2282 tree t
= TYPE_FIELDS (struct_decl
);
2286 (struct field_entry
*) xmalloc (num_fields
* sizeof (struct field_entry
));
2288 for (idx
= 0 ; t
; t
= TREE_CHAIN (t
), idx
++)
2289 if (TREE_CODE (t
) == FIELD_DECL
)
2291 list
[idx
].index
= idx
;
2293 list
[idx
].acc_sites
=
2294 htab_create (32, field_acc_hash
, field_acc_eq
, NULL
);
2295 list
[idx
].count
= 0;
2296 list
[idx
].field_mapping
= NULL_TREE
;
2302 /* Print non-field accesses from hashtable ACCS of structure. */
2305 dump_access_sites (htab_t accs
)
2311 htab_traverse (accs
, dump_acc
, NULL
);
2314 /* This function is a callback for alloc_sites hashtable
2315 traversal. SLOT is a pointer to fallocs_t. This function
2316 removes all allocations of the structure defined by DATA. */
2319 remove_str_allocs_in_func (void **slot
, void *data
)
2321 fallocs_t fallocs
= *(fallocs_t
*) slot
;
2325 while (VEC_iterate (alloc_site_t
, fallocs
->allocs
, i
, call
))
2327 if (call
->str
== (d_str
) data
)
2328 VEC_ordered_remove (alloc_site_t
, fallocs
->allocs
, i
);
2336 /* This function remove all entries corresponding to the STR structure
2337 from alloc_sites hashtable. */
2340 remove_str_allocs (d_str str
)
2346 htab_traverse (alloc_sites
, remove_str_allocs_in_func
, str
);
2349 /* This function removes the structure with index I from structures vector. */
2352 remove_structure (unsigned i
)
2356 if (i
>= VEC_length (structure
, structures
))
2359 str
= VEC_index (structure
, structures
, i
);
2361 /* Before removing the structure str, we have to remove its
2362 allocations from alloc_sites hashtable. */
2363 remove_str_allocs (str
);
2364 free_data_struct (str
);
2365 VEC_ordered_remove (structure
, structures
, i
);
2368 /* Currently we support only EQ_EXPR or NE_EXPR conditions.
2369 COND_STMT is a condition statement to check. */
2372 is_safe_cond_expr (gimple cond_stmt
)
2375 unsigned str0
, str1
;
2377 unsigned length
= VEC_length (structure
, structures
);
2379 if (gimple_cond_code (cond_stmt
) != EQ_EXPR
2380 && gimple_cond_code (cond_stmt
) != NE_EXPR
)
2383 arg0
= gimple_cond_lhs (cond_stmt
);
2384 arg1
= gimple_cond_rhs (cond_stmt
);
2386 str0
= find_structure (strip_type (get_type_of_var (arg0
)));
2387 str1
= find_structure (strip_type (get_type_of_var (arg1
)));
2389 s0
= (str0
!= length
) ? true : false;
2390 s1
= (str1
!= length
) ? true : false;
2395 /* For now we allow only comparison with 0 or NULL. */
2396 if (!integer_zerop (arg0
) && !integer_zerop (arg1
))
2402 /* This function excludes statements, that are
2403 part of allocation sites or field accesses, from the
2404 hashtable of general accesses. SLOT represents general
2405 access that will be checked. DATA is a pointer to
2406 exclude_data structure. */
2409 exclude_from_accs (void **slot
, void *data
)
2411 struct access_site
*acc
= *(struct access_site
**) slot
;
2412 tree fn_decl
= ((struct exclude_data
*)data
)->fn_decl
;
2413 d_str str
= ((struct exclude_data
*)data
)->str
;
2415 if (is_part_of_malloc (acc
->stmt
, fn_decl
)
2416 || is_part_of_field_access (acc
->stmt
, str
))
2418 VEC_free (tree
, heap
, acc
->vars
);
2420 htab_clear_slot (str
->accs
, slot
);
2425 /* Callback function for walk_tree called from collect_accesses_in_bb
2426 function. DATA is the statement which is analyzed. */
2429 get_stmt_accesses (tree
*tp
, int *walk_subtrees
, void *data
)
2431 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
2432 gimple stmt
= (gimple
) wi
->info
;
2438 switch (TREE_CODE (t
))
2442 tree var
= TREE_OPERAND(t
, 0);
2443 tree type
= TYPE_MAIN_VARIANT (strip_type (get_type_of_var (var
)));
2444 unsigned i
= find_structure (type
);
2446 if (i
!= VEC_length (structure
, structures
))
2450 fprintf (dump_file
, "\nThe type ");
2451 print_generic_expr (dump_file
, type
, 0);
2452 fprintf (dump_file
, " has bitfield.");
2454 remove_structure (i
);
2461 tree ref
= TREE_OPERAND (t
, 0);
2462 tree field_decl
= TREE_OPERAND (t
, 1);
2465 if ((TREE_CODE (ref
) == INDIRECT_REF
2466 || TREE_CODE (ref
) == ARRAY_REF
2467 || TREE_CODE (ref
) == VAR_DECL
)
2468 && TREE_CODE (field_decl
) == FIELD_DECL
)
2470 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (ref
));
2471 unsigned i
= find_structure (type
);
2473 if (i
!= VEC_length (structure
, structures
))
2475 d_str str
= VEC_index (structure
, structures
, i
);
2476 struct field_entry
* field
=
2477 find_field_in_struct (str
, field_decl
);
2481 struct field_access_site
*acc
= make_field_acc_node ();
2488 acc
->field_decl
= field_decl
;
2490 /* Check whether the access is of the form
2491 we can deal with. */
2492 if (!decompose_access (str
->decl
, acc
))
2496 fprintf (dump_file
, "\nThe type ");
2497 print_generic_expr (dump_file
, type
, 0);
2499 " has complicate access in statement ");
2500 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2503 remove_structure (i
);
2508 /* Increase count of field. */
2509 basic_block bb
= gimple_bb (stmt
);
2510 field
->count
+= bb
->count
;
2512 /* Add stmt to the acc_sites of field. */
2513 add_field_acc_to_acc_sites (acc
, field
->acc_sites
);
2524 tree cond
= COND_EXPR_COND (t
);
2526 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (cond
)); i
++)
2528 tree t
= TREE_OPERAND (cond
, i
);
2531 walk_tree (&t
, get_stmt_accesses
, data
, NULL
);
2542 if (TREE_CODE (t
) == SSA_NAME
)
2543 t
= SSA_NAME_VAR (t
);
2545 i
= find_structure (strip_type (get_type_of_var (t
)));
2546 if (i
!= VEC_length (structure
, structures
))
2550 str
= VEC_index (structure
, structures
, i
);
2551 add_access_to_acc_sites (stmt
, t
, str
->accs
);
2564 /* Free structures hashtable. */
2567 free_structures (void)
2572 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
2573 free_data_struct (str
);
2575 VEC_free (structure
, heap
, structures
);
2579 /* This function is a callback for traversal over new_var's hashtable.
2580 SLOT is a pointer to new_var. This function frees memory allocated
2581 for new_var and pointed by *SLOT. */
2584 free_new_var (void **slot
, void *data ATTRIBUTE_UNUSED
)
2586 new_var n_var
= *(new_var
*) slot
;
2588 /* Free vector of new_vars. */
2589 VEC_free (tree
, heap
, n_var
->new_vars
);
2594 /* Free new_vars hashtable NEW_VARS_HTAB. */
2597 free_new_vars_htab (htab_t new_vars_htab
)
2600 htab_traverse (new_vars_htab
, free_new_var
, NULL
);
2601 htab_delete (new_vars_htab
);
2602 new_vars_htab
= NULL
;
2605 /* This function creates new general and field accesses that appear in cfun. */
2608 create_new_accesses_for_func (void)
2612 FOR_EACH_BB_FN (bb
, cfun
)
2613 create_new_accesses_in_bb (bb
);
2616 /* Create new allocation sites for the function represented by NODE. */
2619 create_new_alloc_sites_for_func (struct cgraph_node
*node
)
2621 fallocs_t fallocs
= get_fallocs (node
->decl
);
2624 create_new_alloc_sites (fallocs
, node
->decl
);
2627 /* For each local variable of structure type from the vector of structures
2628 this function generates new variable(s) to replace it. */
2631 create_new_local_vars (void)
2634 referenced_var_iterator rvi
;
2636 new_local_vars
= htab_create (num_referenced_vars
,
2637 new_var_hash
, new_var_eq
, NULL
);
2639 FOR_EACH_REFERENCED_VAR (var
, rvi
)
2641 if (!is_global_var (var
))
2642 create_new_var (var
, new_local_vars
);
2646 htab_traverse (new_local_vars
, finalize_new_vars_creation
, NULL
);
2647 dump_new_vars (new_local_vars
);
2650 /* This function prints the SHIFT number of spaces to the DUMP_FILE. */
2653 print_shift (unsigned HOST_WIDE_INT shift
)
2655 unsigned HOST_WIDE_INT sh
= shift
;
2658 fprintf (dump_file
, " ");
2661 /* This function updates field_mapping of FIELDS in CLUSTER with NEW_TYPE. */
2664 update_fields_mapping (struct field_cluster
*cluster
, tree new_type
,
2665 struct field_entry
* fields
, int num_fields
)
2669 for (i
= 0; i
< num_fields
; i
++)
2670 if (TEST_BIT (cluster
->fields_in_cluster
, i
))
2671 fields
[i
].field_mapping
= new_type
;
2674 /* This functions builds structure with FIELDS,
2675 NAME and attributes similar to ORIG_STRUCT.
2676 It returns the newly created structure. */
2679 build_basic_struct (tree fields
, tree name
, tree orig_struct
)
2681 tree attributes
= NULL_TREE
;
2685 if (TYPE_ATTRIBUTES (orig_struct
))
2686 attributes
= unshare_expr (TYPE_ATTRIBUTES (orig_struct
));
2687 ref
= make_node (RECORD_TYPE
);
2688 TYPE_SIZE (ref
) = 0;
2689 decl_attributes (&ref
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
2690 TYPE_PACKED (ref
) = TYPE_PACKED (orig_struct
);
2691 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
2693 DECL_CONTEXT (x
) = ref
;
2694 DECL_PACKED (x
) |= TYPE_PACKED (ref
);
2696 TYPE_FIELDS (ref
) = fields
;
2698 TYPE_NAME (ref
) = name
;
2703 /* This function copies FIELDS from CLUSTER into TREE_CHAIN as part
2704 of preparation for new structure building. NUM_FIELDS is a total
2705 number of fields in the structure. The function returns newly
2706 generated fields. */
2709 create_fields (struct field_cluster
* cluster
,
2710 struct field_entry
* fields
, int num_fields
)
2713 tree new_types
= NULL_TREE
;
2714 tree last
= NULL_TREE
;
2716 for (i
= 0; i
< num_fields
; i
++)
2717 if (TEST_BIT (cluster
->fields_in_cluster
, i
))
2719 tree new_decl
= unshare_expr (fields
[i
].decl
);
2722 new_types
= new_decl
;
2724 TREE_CHAIN (last
) = new_decl
;
2728 TREE_CHAIN (last
) = NULL_TREE
;
2733 /* This function creates a cluster name. The name is based on
2734 the original structure name, if it is present. It has a form:
2736 <original_struct_name>_sub.<CLUST_NUM>
2738 The original structure name is taken from the type of DECL.
2739 If an original structure name is not present, it's generated to be:
2743 The function returns identifier of the new cluster name. */
2746 gen_cluster_name (tree decl
, int clust_num
, int str_num
)
2748 const char * orig_name
= get_type_name (decl
);
2749 char * tmp_name
= NULL
;
2755 ASM_FORMAT_PRIVATE_NAME(tmp_name
, "struct", str_num
);
2757 len
= strlen (tmp_name
? tmp_name
: orig_name
) + strlen ("_sub");
2758 prefix
= XALLOCAVEC (char, len
+ 1);
2759 memcpy (prefix
, tmp_name
? tmp_name
: orig_name
,
2760 strlen (tmp_name
? tmp_name
: orig_name
));
2761 strcpy (prefix
+ strlen (tmp_name
? tmp_name
: orig_name
), "_sub");
2763 ASM_FORMAT_PRIVATE_NAME (new_name
, prefix
, clust_num
);
2764 return get_identifier (new_name
);
2767 /* This function checks whether the structure STR has bitfields.
2768 If yes, this structure type is added to UNSUITABLE_TYPES vector. */
2771 check_bitfields (d_str str
, VEC (tree
, heap
) **unsuitable_types
)
2773 tree type
= str
->decl
;
2776 for (i
= 0; i
< str
->num_fields
; i
++)
2777 if (DECL_BIT_FIELD (str
->fields
[i
].decl
))
2779 add_unsuitable_type (unsuitable_types
, type
);
2782 fprintf (dump_file
, "\nType ");
2783 print_generic_expr (dump_file
, type
, 0);
2784 fprintf (dump_file
, "\nescapes due to bitfield ");
2785 print_generic_expr (dump_file
, str
->fields
[i
].decl
, 0);
2791 /* This function adds to UNSUITABLE_TYPES those types that escape
2792 due to results of ipa-type-escape analysis. See ipa-type-escape.[c,h]. */
2795 exclude_escaping_types_1 (VEC (tree
, heap
) **unsuitable_types
)
2800 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
2801 check_type_escape (str
, unsuitable_types
);
2804 /* If a structure type is a return type of any function,
2805 we cannot transform it. Such type is added to UNSUITABLE_TYPES vector. */
2808 exclude_returned_types (VEC (tree
, heap
) **unsuitable_types
)
2810 struct cgraph_node
*c_node
;
2812 for (c_node
= cgraph_nodes
; c_node
; c_node
= c_node
->next
)
2814 tree ret_t
= TREE_TYPE (TREE_TYPE (c_node
->decl
));
2818 ret_t
= strip_type (ret_t
);
2819 if (TREE_CODE (ret_t
) == RECORD_TYPE
)
2821 add_unsuitable_type (unsuitable_types
, TYPE_MAIN_VARIANT (ret_t
));
2824 fprintf (dump_file
, "\nThe type \"");
2825 print_generic_expr (dump_file
, ret_t
, 0);
2827 "\" is return type of function...Excluded.");
2834 /* This function looks for parameters of local functions
2835 which are of structure types, or derived from them (arrays
2836 of structures, pointers to structures, or their combinations).
2837 We are not handling peeling of such structures right now.
2838 The found structures types are added to UNSUITABLE_TYPES vector. */
2841 exclude_types_passed_to_local_func (VEC (tree
, heap
) **unsuitable_types
)
2843 struct cgraph_node
*c_node
;
2845 for (c_node
= cgraph_nodes
; c_node
; c_node
= c_node
->next
)
2846 if (cgraph_function_body_availability (c_node
) == AVAIL_LOCAL
)
2848 tree fn
= c_node
->decl
;
2851 for (arg
= DECL_ARGUMENTS (fn
); arg
; arg
= TREE_CHAIN (arg
))
2853 tree type
= TREE_TYPE (arg
);
2855 type
= strip_type (type
);
2856 if (TREE_CODE (type
) == RECORD_TYPE
)
2858 add_unsuitable_type (unsuitable_types
,
2859 TYPE_MAIN_VARIANT (type
));
2862 fprintf (dump_file
, "\nPointer to type \"");
2863 print_generic_expr (dump_file
, type
, 0);
2865 "\" is passed to local function...Excluded.");
2872 /* This function analyzes structure form of structures
2873 potential for transformation. If we are not capable to transform
2874 structure of some form, we remove it from the structures hashtable.
2875 Right now we cannot handle nested structs, when nesting is
2876 through any level of pointers or arrays.
2878 TBD: release these constrains in future.
2880 Note, that in this function we suppose that all structures
2881 in the program are members of the structures hashtable right now,
2882 without excluding escaping types. */
2885 check_struct_form (d_str str
, VEC (tree
, heap
) **unsuitable_types
)
2889 for (i
= 0; i
< str
->num_fields
; i
++)
2891 tree f_type
= strip_type(TREE_TYPE (str
->fields
[i
].decl
));
2893 if (TREE_CODE (f_type
) == RECORD_TYPE
)
2895 add_unsuitable_type (unsuitable_types
, TYPE_MAIN_VARIANT (f_type
));
2896 add_unsuitable_type (unsuitable_types
, str
->decl
);
2899 fprintf (dump_file
, "\nType ");
2900 print_generic_expr (dump_file
, f_type
, 0);
2901 fprintf (dump_file
, " is a field in the structure ");
2902 print_generic_expr (dump_file
, str
->decl
, 0);
2903 fprintf (dump_file
, ". Escaping...");
2909 /* This function adds a structure TYPE to the vector of structures,
2910 if it's not already there. */
2913 add_structure (tree type
)
2915 struct data_structure node
;
2919 type
= TYPE_MAIN_VARIANT (type
);
2921 i
= find_structure (type
);
2923 if (i
!= VEC_length (structure
, structures
))
2926 num_fields
= fields_length (type
);
2928 node
.num_fields
= num_fields
;
2929 node
.fields
= get_fields (type
, num_fields
);
2930 node
.struct_clustering
= NULL
;
2931 node
.accs
= htab_create (32, acc_hash
, acc_eq
, NULL
);
2932 node
.new_types
= VEC_alloc (tree
, heap
, num_fields
);
2935 VEC_safe_push (structure
, heap
, structures
, &node
);
2939 fprintf (dump_file
, "\nAdding data structure \"");
2940 print_generic_expr (dump_file
, type
, 0);
2941 fprintf (dump_file
, "\" to data_struct_list.");
2945 /* This function adds an allocation site to alloc_sites hashtable.
2946 The allocation site appears in STMT of function FN_DECL and
2947 allocates the structure represented by STR. */
2950 add_alloc_site (tree fn_decl
, gimple stmt
, d_str str
)
2952 fallocs_t fallocs
= NULL
;
2953 alloc_site_t m_call
;
2959 (fallocs_t
) htab_find_with_hash (alloc_sites
,
2960 fn_decl
, htab_hash_pointer (fn_decl
));
2966 fallocs
= (fallocs_t
)
2967 xmalloc (sizeof (struct func_alloc_sites
));
2968 fallocs
->func
= fn_decl
;
2969 fallocs
->allocs
= VEC_alloc (alloc_site_t
, heap
, 1);
2970 slot
= htab_find_slot_with_hash (alloc_sites
, fn_decl
,
2971 htab_hash_pointer (fn_decl
), INSERT
);
2974 VEC_safe_push (alloc_site_t
, heap
,
2975 fallocs
->allocs
, &m_call
);
2979 fprintf (dump_file
, "\nAdding stmt ");
2980 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2981 fprintf (dump_file
, " to list of mallocs.");
2985 /* This function returns true if the result of STMT, that contains a call
2986 to an allocation function, is cast to one of the structure types.
2987 STMT should be of the form: T.2 = <alloc_func> (T.1);
2988 If true, I_P contains an index of an allocated structure.
2989 Otherwise I_P contains the length of the vector of structures. */
2992 is_alloc_of_struct (gimple stmt
, unsigned *i_p
)
2998 final_stmt
= get_final_alloc_stmt (stmt
);
3003 /* final_stmt should be of the form:
3004 T.3 = (struct_type *) T.2; */
3006 if (gimple_code (final_stmt
) != GIMPLE_ASSIGN
)
3009 lhs
= gimple_assign_lhs (final_stmt
);
3011 type
= get_type_of_var (lhs
);
3016 if (!POINTER_TYPE_P (type
)
3017 || TREE_CODE (strip_type (type
)) != RECORD_TYPE
)
3020 *i_p
= find_structure (strip_type (type
));
3022 if (*i_p
== VEC_length (structure
, structures
))
3028 /* This function prints non-field and field accesses
3029 of the structure STR. */
3032 dump_accs (d_str str
)
3036 fprintf (dump_file
, "\nAccess sites of struct ");
3037 print_generic_expr (dump_file
, str
->decl
, 0);
3039 for (i
= 0; i
< str
->num_fields
; i
++)
3041 fprintf (dump_file
, "\nAccess site of field ");
3042 print_generic_expr (dump_file
, str
->fields
[i
].decl
, 0);
3043 dump_field_acc_sites (str
->fields
[i
].acc_sites
);
3044 fprintf (dump_file
, ":\n");
3046 fprintf (dump_file
, "\nGeneral access sites\n");
3047 dump_access_sites (str
->accs
);
3050 /* This function checks whether an access statement, pointed by SLOT,
3051 is a condition we are capable to transform. It returns false if not,
3052 setting bool *DATA to false. */
3055 safe_cond_expr_check (void **slot
, void *data
)
3057 struct access_site
*acc
= *(struct access_site
**) slot
;
3059 if (gimple_code (acc
->stmt
) == GIMPLE_COND
3060 && !is_safe_cond_expr (acc
->stmt
))
3064 fprintf (dump_file
, "\nUnsafe conditional statement ");
3065 print_gimple_stmt (dump_file
, acc
->stmt
, 0, 0);
3067 *(bool *) data
= false;
3073 /* This function excludes statements that are part of allocation sites and
3074 field accesses from the hashtable of general accesses of the structure
3075 type STR. Only accesses that belong to the function represented by
3076 NODE are treated. */
3079 exclude_alloc_and_field_accs_1 (d_str str
, struct cgraph_node
*node
)
3081 struct exclude_data dt
;
3083 dt
.fn_decl
= node
->decl
;
3086 htab_traverse (dt
.str
->accs
, exclude_from_accs
, &dt
);
3089 /* Collect accesses to the structure types that appear in basic block BB. */
3092 collect_accesses_in_bb (basic_block bb
)
3094 gimple_stmt_iterator bsi
;
3095 struct walk_stmt_info wi
;
3097 memset (&wi
, 0, sizeof (wi
));
3099 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
3101 gimple stmt
= gsi_stmt (bsi
);
3103 /* In asm stmt we cannot always track the arguments,
3104 so we just give up. */
3105 if (gimple_code (stmt
) == GIMPLE_ASM
)
3111 wi
.info
= (void *) stmt
;
3112 walk_gimple_op (stmt
, get_stmt_accesses
, &wi
);
3116 /* This function generates cluster substructure that contains FIELDS.
3117 The cluster added to the set of clusters of the structure STR. */
3120 gen_cluster (sbitmap fields
, d_str str
)
3122 struct field_cluster
*crr_cluster
= NULL
;
3125 (struct field_cluster
*) xcalloc (1, sizeof (struct field_cluster
));
3126 crr_cluster
->sibling
= str
->struct_clustering
;
3127 str
->struct_clustering
= crr_cluster
;
3128 crr_cluster
->fields_in_cluster
= fields
;
3131 /* This function peels a field with the index I from the structure DS. */
3134 peel_field (int i
, d_str ds
)
3136 struct field_cluster
*crr_cluster
= NULL
;
3139 (struct field_cluster
*) xcalloc (1, sizeof (struct field_cluster
));
3140 crr_cluster
->sibling
= ds
->struct_clustering
;
3141 ds
->struct_clustering
= crr_cluster
;
3142 crr_cluster
->fields_in_cluster
=
3143 sbitmap_alloc ((unsigned int) ds
->num_fields
);
3144 sbitmap_zero (crr_cluster
->fields_in_cluster
);
3145 SET_BIT (crr_cluster
->fields_in_cluster
, i
);
3148 /* This function calculates maximum field count in
3149 the structure STR. */
3152 get_max_field_count (d_str str
)
3157 for (i
= 0; i
< str
->num_fields
; i
++)
3158 if (str
->fields
[i
].count
> max
)
3159 max
= str
->fields
[i
].count
;
3164 /* Do struct-reorg transformation for individual function
3165 represented by NODE. All structure types relevant
3166 for this function are transformed. */
3169 do_reorg_for_func (struct cgraph_node
*node
)
3171 create_new_local_vars ();
3172 create_new_alloc_sites_for_func (node
);
3173 create_new_accesses_for_func ();
3174 update_ssa (TODO_update_ssa
);
3175 cleanup_tree_cfg ();
3177 /* Free auxiliary data representing local variables. */
3178 free_new_vars_htab (new_local_vars
);
3181 /* Print structure TYPE, its name, if it exists, and body.
3182 INDENT defines the level of indentation (similar
3183 to the option -i of indent command). SHIFT parameter
3184 defines a number of spaces by which a structure will
3185 be shifted right. */
3188 dump_struct_type (tree type
, unsigned HOST_WIDE_INT indent
,
3189 unsigned HOST_WIDE_INT shift
)
3191 const char *struct_name
;
3194 if (!type
|| !dump_file
)
3197 if (TREE_CODE (type
) != RECORD_TYPE
)
3199 print_generic_expr (dump_file
, type
, 0);
3203 print_shift (shift
);
3204 struct_name
= get_type_name (type
);
3205 fprintf (dump_file
, "struct ");
3207 fprintf (dump_file
, "%s\n",struct_name
);
3208 print_shift (shift
);
3209 fprintf (dump_file
, "{\n");
3211 for (field
= TYPE_FIELDS (type
); field
;
3212 field
= TREE_CHAIN (field
))
3214 unsigned HOST_WIDE_INT s
= indent
;
3215 tree f_type
= TREE_TYPE (field
);
3217 print_shift (shift
);
3219 fprintf (dump_file
, " ");
3220 dump_struct_type (f_type
, indent
, shift
+ indent
);
3221 fprintf(dump_file
, " ");
3222 print_generic_expr (dump_file
, field
, 0);
3223 fprintf(dump_file
, ";\n");
3225 print_shift (shift
);
3226 fprintf (dump_file
, "}\n");
3229 /* This function creates new structure types to replace original type,
3230 indicated by STR->decl. The names of the new structure types are
3231 derived from the original structure type. If the original structure
3232 type has no name, we assume that its name is 'struct.<STR_NUM>'. */
3235 create_new_type (d_str str
, int *str_num
)
3237 int cluster_num
= 0;
3239 struct field_cluster
*cluster
= str
->struct_clustering
;
3242 tree name
= gen_cluster_name (str
->decl
, cluster_num
,
3248 fields
= create_fields (cluster
, str
->fields
,
3250 new_type
= build_basic_struct (fields
, name
, str
->decl
);
3252 update_fields_mapping (cluster
, new_type
,
3253 str
->fields
, str
->num_fields
);
3255 VEC_safe_push (tree
, heap
, str
->new_types
, new_type
);
3256 cluster
= cluster
->sibling
;
3261 /* This function is a callback for alloc_sites hashtable
3262 traversal. SLOT is a pointer to fallocs_t.
3263 This function frees memory pointed by *SLOT. */
3266 free_falloc_sites (void **slot
, void *data ATTRIBUTE_UNUSED
)
3268 fallocs_t fallocs
= *(fallocs_t
*) slot
;
3270 VEC_free (alloc_site_t
, heap
, fallocs
->allocs
);
3275 /* Remove structures collected in UNSUITABLE_TYPES
3276 from structures vector. */
3279 remove_unsuitable_types (VEC (tree
, heap
) *unsuitable_types
)
3285 for (j
= 0; VEC_iterate (tree
, unsuitable_types
, j
, type
); j
++)
3286 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3287 if (is_equal_types (str
->decl
, type
))
3289 remove_structure (i
);
3294 /* Exclude structure types with bitfields.
3295 We would not want to interfere with other optimizations
3296 that can be done in this case. The structure types with
3297 bitfields are added to UNSUITABLE_TYPES vector. */
3300 exclude_types_with_bit_fields (VEC (tree
, heap
) **unsuitable_types
)
3305 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3306 check_bitfields (str
, unsuitable_types
);
3309 /* This function checks three types of escape. A structure type escapes:
3311 1. if it's a type of parameter of a local function.
3312 2. if it's a type of function return value.
3313 3. if it escapes as a result of ipa-type-escape analysis.
3315 The escaping structure types are added to UNSUITABLE_TYPES vector. */
3318 exclude_escaping_types (VEC (tree
, heap
) **unsuitable_types
)
3320 exclude_types_passed_to_local_func (unsuitable_types
);
3321 exclude_returned_types (unsuitable_types
);
3322 exclude_escaping_types_1 (unsuitable_types
);
3325 /* This function analyzes whether the form of
3326 structure is such that we are capable to transform it.
3327 Nested structures are checked here. Unsuitable structure
3328 types are added to UNSUITABLE_TYPE vector. */
3331 analyze_struct_form (VEC (tree
, heap
) **unsuitable_types
)
3336 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3337 check_struct_form (str
, unsuitable_types
);
3340 /* This function looks for structure types instantiated in the program.
3341 The candidate types are added to the structures vector.
3342 Unsuitable types are collected into UNSUITABLE_TYPES vector. */
3345 build_data_structure (VEC (tree
, heap
) **unsuitable_types
)
3349 struct varpool_node
*current_varpool
;
3350 struct cgraph_node
*c_node
;
3352 /* Check global variables. */
3353 FOR_EACH_STATIC_VARIABLE (current_varpool
)
3355 var
= current_varpool
->decl
;
3356 if (is_candidate (var
, &type
, unsuitable_types
))
3357 add_structure (type
);
3360 /* Now add structures that are types of function parameters and
3362 for (c_node
= cgraph_nodes
; c_node
; c_node
= c_node
->next
)
3364 enum availability avail
=
3365 cgraph_function_body_availability (c_node
);
3367 /* We need AVAIL_AVAILABLE for main function. */
3368 if (avail
== AVAIL_LOCAL
|| avail
== AVAIL_AVAILABLE
)
3370 struct function
*fn
= DECL_STRUCT_FUNCTION (c_node
->decl
);
3372 for (var
= DECL_ARGUMENTS (c_node
->decl
); var
;
3373 var
= TREE_CHAIN (var
))
3374 if (is_candidate (var
, &type
, unsuitable_types
))
3375 add_structure (type
);
3377 /* Check function local variables. */
3378 for (var_list
= fn
->local_decls
; var_list
;
3379 var_list
= TREE_CHAIN (var_list
))
3381 var
= TREE_VALUE (var_list
);
3383 if (is_candidate (var
, &type
, unsuitable_types
))
3384 add_structure (type
);
3390 /* This function returns true if the program contains
3391 a call to user defined allocation function, or other
3392 functions that can interfere with struct-reorg optimizations. */
3395 program_redefines_malloc_p (void)
3397 struct cgraph_node
*c_node
;
3398 struct cgraph_node
*c_node2
;
3399 struct cgraph_edge
*c_edge
;
3403 for (c_node
= cgraph_nodes
; c_node
; c_node
= c_node
->next
)
3405 fndecl
= c_node
->decl
;
3407 for (c_edge
= c_node
->callees
; c_edge
; c_edge
= c_edge
->next_callee
)
3409 c_node2
= c_edge
->callee
;
3410 fndecl2
= c_node2
->decl
;
3411 if (is_gimple_call (c_edge
->call_stmt
))
3413 const char * fname
= get_name (fndecl2
);
3415 if ((gimple_call_flags (c_edge
->call_stmt
) & ECF_MALLOC
)
3416 && (DECL_FUNCTION_CODE (fndecl2
) != BUILT_IN_MALLOC
)
3417 && (DECL_FUNCTION_CODE (fndecl2
) != BUILT_IN_CALLOC
)
3418 && (DECL_FUNCTION_CODE (fndecl2
) != BUILT_IN_ALLOCA
))
3421 /* Check that there is no __builtin_object_size,
3422 __builtin_offsetof, or realloc's in the program. */
3423 if (DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_OBJECT_SIZE
3424 || !strcmp (fname
, "__builtin_offsetof")
3425 || !strcmp (fname
, "realloc"))
3434 /* In this function we assume that an allocation statement
3436 var = (type_cast) malloc (size);
3438 is converted into the following set of statements:
3442 T.3 = (type_cast) T.2;
3445 In this function we collect into alloc_sites the allocation
3446 sites of variables of structure types that are present
3447 in structures vector. */
3450 collect_alloc_sites (void)
3452 struct cgraph_node
*node
;
3453 struct cgraph_edge
*cs
;
3455 for (node
= cgraph_nodes
; node
; node
= node
->next
)
3456 if (node
->analyzed
&& node
->decl
)
3458 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
3460 gimple stmt
= cs
->call_stmt
;
3466 if (is_gimple_call (stmt
)
3467 && (decl
= gimple_call_fndecl (stmt
))
3468 && gimple_call_lhs (stmt
))
3472 if (is_alloc_of_struct (stmt
, &i
))
3474 /* We support only malloc now. */
3475 if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_MALLOC
)
3479 str
= VEC_index (structure
, structures
, i
);
3480 add_alloc_site (node
->decl
, stmt
, str
);
3487 "\nUnsupported allocation function ");
3488 print_gimple_stmt (dump_file
, stmt
, 0, 0);
3490 remove_structure (i
);
3499 /* Print collected accesses. */
3502 dump_accesses (void)
3510 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3514 /* This function checks whether the accesses of structures in condition
3515 expressions are of the kind we are capable to transform.
3516 If not, such structures are removed from the vector of structures. */
3519 check_cond_exprs (void)
3525 while (VEC_iterate (structure
, structures
, i
, str
))
3530 htab_traverse (str
->accs
, safe_cond_expr_check
, &safe_p
);
3532 remove_structure (i
);
3538 /* We exclude from non-field accesses of the structure
3539 all statements that will be treated as part of the structure
3540 allocation sites or field accesses. */
3543 exclude_alloc_and_field_accs (struct cgraph_node
*node
)
3548 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3549 exclude_alloc_and_field_accs_1 (str
, node
);
3552 /* This function collects accesses of the fields of the structures
3553 that appear at function FN. */
3556 collect_accesses_in_func (struct function
*fn
)
3563 /* Collect accesses for each basic blocks separately. */
3564 FOR_EACH_BB_FN (bb
, fn
)
3565 collect_accesses_in_bb (bb
);
3568 /* This function summarizes counts of the fields into the structure count. */
3571 sum_counts (d_str str
, gcov_type
*hottest
)
3576 for (i
= 0; i
< str
->num_fields
; i
++)
3580 fprintf (dump_file
, "\nCounter of field \"");
3581 print_generic_expr (dump_file
, str
->fields
[i
].decl
, 0);
3582 fprintf (dump_file
, "\" is " HOST_WIDEST_INT_PRINT_DEC
,
3583 str
->fields
[i
].count
);
3585 str
->count
+= str
->fields
[i
].count
;
3590 fprintf (dump_file
, "\nCounter of struct \"");
3591 print_generic_expr (dump_file
, str
->decl
, 0);
3592 fprintf (dump_file
, "\" is " HOST_WIDEST_INT_PRINT_DEC
, str
->count
);
3595 if (str
->count
> *hottest
)
3596 *hottest
= str
->count
;
3599 /* This function peels the field into separate structure if it's
3600 sufficiently hot, i.e. if its count provides at least 90% of
3601 the maximum field count in the structure. */
3604 peel_hot_fields (d_str str
)
3606 gcov_type max_field_count
;
3607 sbitmap fields_left
= sbitmap_alloc (str
->num_fields
);
3610 sbitmap_ones (fields_left
);
3612 (gcov_type
) (get_max_field_count (str
)/100)*90;
3614 str
->struct_clustering
= NULL
;
3616 for (i
= 0; i
< str
->num_fields
; i
++)
3618 if (str
->count
>= max_field_count
)
3620 RESET_BIT (fields_left
, i
);
3621 peel_field (i
, str
);
3625 i
= sbitmap_first_set_bit (fields_left
);
3627 gen_cluster (fields_left
, str
);
3629 sbitmap_free (fields_left
);
3632 /* This function is a helper for do_reorg. It goes over
3633 functions in call graph and performs actual transformation
3639 struct cgraph_node
*node
;
3641 /* Initialize the default bitmap obstack. */
3642 bitmap_obstack_initialize (NULL
);
3644 for (node
= cgraph_nodes
; node
; node
= node
->next
)
3645 if (node
->analyzed
&& node
->decl
)
3647 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
3648 current_function_decl
= node
->decl
;
3650 fprintf (dump_file
, "\nFunction to do reorg is %s: \n",
3651 (const char *) IDENTIFIER_POINTER (DECL_NAME (node
->decl
)));
3652 do_reorg_for_func (node
);
3653 free_dominance_info (CDI_DOMINATORS
);
3654 free_dominance_info (CDI_POST_DOMINATORS
);
3655 current_function_decl
= NULL
;
3660 bitmap_obstack_release (NULL
);
3663 /* This function creates new global struct variables.
3664 For each original variable, the set of new variables
3665 is created with the new structure types corresponding
3666 to the structure type of original variable.
3667 Only VAR_DECL variables are treated by this function. */
3670 create_new_global_vars (void)
3672 struct varpool_node
*current_varpool
;
3673 unsigned HOST_WIDE_INT i
;
3674 unsigned HOST_WIDE_INT varpool_size
= 0;
3676 for (i
= 0; i
< 2; i
++)
3679 new_global_vars
= htab_create (varpool_size
,
3680 new_var_hash
, new_var_eq
, NULL
);
3681 FOR_EACH_STATIC_VARIABLE(current_varpool
)
3683 tree var_decl
= current_varpool
->decl
;
3685 if (!var_decl
|| TREE_CODE (var_decl
) != VAR_DECL
)
3690 create_new_var (var_decl
, new_global_vars
);
3694 if (new_global_vars
)
3695 htab_traverse (new_global_vars
, update_varpool_with_new_var
, NULL
);
3698 /* Dump all new types generated by this optimization. */
3701 dump_new_types (void)
3710 fprintf (dump_file
, "\nThe following are the new types generated by"
3711 " this optimization:\n");
3713 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3717 fprintf (dump_file
, "\nFor type ");
3718 dump_struct_type (str
->decl
, 2, 0);
3719 fprintf (dump_file
, "\nthe number of new types is %d\n",
3720 VEC_length (tree
, str
->new_types
));
3722 for (j
= 0; VEC_iterate (tree
, str
->new_types
, j
, type
); j
++)
3723 dump_struct_type (type
, 2, 0);
3727 /* This function creates new types to replace old structure types. */
3730 create_new_types (void)
3736 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3737 create_new_type (str
, &str_num
);
3740 /* Free allocation sites hashtable. */
3743 free_alloc_sites (void)
3746 htab_traverse (alloc_sites
, free_falloc_sites
, NULL
);
3747 htab_delete (alloc_sites
);
3751 /* This function collects structures potential
3752 for peeling transformation, and inserts
3753 them into structures hashtable. */
3756 collect_structures (void)
3758 VEC (tree
, heap
) *unsuitable_types
= VEC_alloc (tree
, heap
, 32);
3760 structures
= VEC_alloc (structure
, heap
, 32);
3762 /* If program contains user defined mallocs, we give up. */
3763 if (program_redefines_malloc_p ())
3766 /* Build data structures hashtable of all data structures
3768 build_data_structure (&unsuitable_types
);
3770 /* This function analyzes whether the form of
3771 structure is such that we are capable to transform it.
3772 Nested structures are checked here. */
3773 analyze_struct_form (&unsuitable_types
);
3775 /* This function excludes those structure types
3776 that escape compilation unit. */
3777 exclude_escaping_types (&unsuitable_types
);
3779 /* We do not want to change data layout of the structures with bitfields. */
3780 exclude_types_with_bit_fields (&unsuitable_types
);
3782 remove_unsuitable_types (unsuitable_types
);
3783 VEC_free (tree
, heap
, unsuitable_types
);
3786 /* Collect structure allocation sites. In case of arrays
3787 we have nothing to do. */
3790 collect_allocation_sites (void)
3792 alloc_sites
= htab_create (32, malloc_hash
, malloc_eq
, NULL
);
3793 collect_alloc_sites ();
3796 /* This function collects data accesses for the
3797 structures to be transformed. For each structure
3798 field it updates the count field in field_entry. */
3801 collect_data_accesses (void)
3803 struct cgraph_node
*c_node
;
3805 for (c_node
= cgraph_nodes
; c_node
; c_node
= c_node
->next
)
3807 enum availability avail
= cgraph_function_body_availability (c_node
);
3809 if (avail
== AVAIL_LOCAL
|| avail
== AVAIL_AVAILABLE
)
3811 struct function
*func
= DECL_STRUCT_FUNCTION (c_node
->decl
);
3813 collect_accesses_in_func (func
);
3814 exclude_alloc_and_field_accs (c_node
);
3818 check_cond_exprs ();
3819 /* Print collected accesses. */
3823 /* We do not bother to transform cold structures.
3824 Coldness of the structure is defined relatively
3825 to the highest structure count among the structures
3826 to be transformed. It's triggered by the compiler parameter
3828 --param struct-reorg-cold-struct-ratio=<value>
3830 where <value> ranges from 0 to 100. Structures with count ratios
3831 that are less than this parameter are considered to be cold. */
3834 exclude_cold_structs (void)
3836 gcov_type hottest
= 0;
3840 /* We summarize counts of fields of a structure into the structure count. */
3841 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3842 sum_counts (str
, &hottest
);
3844 /* Remove cold structures from structures vector. */
3846 while (VEC_iterate (structure
, structures
, i
, str
))
3847 if (str
->count
* 100 < (hottest
* STRUCT_REORG_COLD_STRUCT_RATIO
))
3851 fprintf (dump_file
, "\nThe structure ");
3852 print_generic_expr (dump_file
, str
->decl
, 0);
3853 fprintf (dump_file
, " is cold.");
3855 remove_structure (i
);
3861 /* This function decomposes original structure into substructures,
3870 for (i
= 0; VEC_iterate (structure
, structures
, i
, str
); i
++)
3871 peel_hot_fields (str
);
3875 /* Do the actual transformation for each structure
3876 from the structures hashtable. */
3881 /* Check that there is a work to do. */
3882 if (!VEC_length (structure
, structures
))
3885 fprintf (dump_file
, "\nNo structures to transform. Exiting...");
3892 fprintf (dump_file
, "\nNumber of structures to transform is %d",
3893 VEC_length (structure
, structures
));
3897 /* Generate new types. */
3898 create_new_types ();
3901 /* Create new global variables. */
3902 create_new_global_vars ();
3903 dump_new_vars (new_global_vars
);
3905 /* Decompose structures for each function separately. */
3908 /* Free auxiliary data collected for global variables. */
3909 free_new_vars_htab (new_global_vars
);
3912 /* Free all auxiliary data used by this optimization. */
3915 free_data_structs (void)
3918 free_alloc_sites ();
3921 /* Perform structure decomposition (peeling). */
3924 reorg_structs (void)
3928 /* Collect structure types. */
3929 collect_structures ();
3931 /* Collect structure allocation sites. */
3932 collect_allocation_sites ();
3934 /* Collect structure accesses. */
3935 collect_data_accesses ();
3937 /* We transform only hot structures. */
3938 exclude_cold_structs ();
3941 /* Decompose structures into substructures, i.e. clusters. */
3945 /* Do the actual transformation for each structure
3946 from the structures hashtable. */
3949 /* Free all auxiliary data used by this optimization. */
3950 free_data_structs ();
3953 /* Struct-reorg optimization entry point function. */
3956 reorg_structs_drive (void)
3962 /* Struct-reorg optimization gate function. */
3965 struct_reorg_gate (void)
3967 return flag_ipa_struct_reorg
3968 && flag_whole_program
3972 struct simple_ipa_opt_pass pass_ipa_struct_reorg
=
3976 "ipa_struct_reorg", /* name */
3977 struct_reorg_gate
, /* gate */
3978 reorg_structs_drive
, /* execute */
3981 0, /* static_pass_number */
3982 TV_INTEGRATION
, /* tv_id */
3983 0, /* properties_required */
3984 0, /* properties_provided */
3985 0, /* properties_destroyed */
3986 TODO_verify_ssa
, /* todo_flags_start */
3987 TODO_dump_func
| TODO_verify_ssa
/* todo_flags_finish */