Change bogus PR in ChangeLog (15803->15804)
[official-gcc.git] / gcc / ipa-struct-reorg.c
blob1985cec5e2a693b3b8fbe1483c4b5df6b83c727f
1 /* Struct-reorg optimization.
2 Copyright (C) 2007 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 2, or (at your option) any later
12 version.
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
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "ggc.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "tree-gimple.h"
32 #include "tree-inline.h"
33 #include "tree-flow.h"
34 #include "tree-flow-inline.h"
35 #include "langhooks.h"
36 #include "pointer-set.h"
37 #include "hashtab.h"
38 #include "c-tree.h"
39 #include "toplev.h"
40 #include "flags.h"
41 #include "debug.h"
42 #include "target.h"
43 #include "cgraph.h"
44 #include "diagnostic.h"
45 #include "timevar.h"
46 #include "params.h"
47 #include "fibheap.h"
48 #include "intl.h"
49 #include "function.h"
50 #include "basic-block.h"
51 #include "tree-iterator.h"
52 #include "tree-pass.h"
53 #include "ipa-struct-reorg.h"
54 #include "opts.h"
55 #include "ipa-type-escape.h"
56 #include "tree-dump.h"
57 #include "c-common.h"
59 /* This optimization implements structure peeling.
61 For example, given a structure type:
62 typedef struct
64 int a;
65 float b;
66 int c;
67 }str_t;
69 it can be peeled into two structure types as follows:
71 typedef struct and typedef struct
72 { {
73 int a; float b;
74 int c; } str_t_1;
75 }str_t_0;
77 or can be fully peeled:
79 typedef struct
81 int a;
82 }str_t_0;
84 typedef struct
86 float b;
87 }str_t_1;
89 typedef struct
91 int c;
92 }str_t_2;
94 When structure type is peeled all instances and their accesses
95 in the program are updated accordingly. For example, if there is
96 array of structures:
98 str_t A[N];
100 and structure type str_t was peeled into two structures str_t_0
101 and str_t_1 as it was shown above, then array A will be replaced
102 by two arrays as follows:
104 str_t_0 A_0[N];
105 str_t_1 A_1[N];
107 The field access of field a of element i of array A: A[i].a will be
108 replaced by an access to field a of element i of array A_0: A_0[i].a.
110 This optimization also supports dynamically allocated arrays.
111 If array of structures was allocated by malloc function:
113 str_t * p = (str_t *) malloc (sizeof (str_t) * N)
115 the allocation site will be replaced to reflect new structure types:
117 str_t_0 * p_0 = (str_t_0 *) malloc (sizeof (str_t_0) * N)
118 str_t_1 * p_1 = (str_t_1 *) malloc (sizeof (str_t_1) * N)
120 The field access through the pointer p[i].a will be changed by p_0[i].a.
122 The goal of structure peeling is to improve spatial locality.
123 For example, if one of the fields of a structure is accessed frequently
124 in the loop:
126 for (i = 0; i < N; i++)
128 ... = A[i].a;
131 the allocation of field a of str_t contiguously in memory will
132 increase the chances of fetching the field from cache.
134 The analysis part of this optimization is based on the frequency of
135 field accesses, which are collected all over the program.
136 Then the fields with the frequencies that satisfy the following condition
137 get peeled out of the structure:
139 freq(f) > C * max_field_freq_in_struct
141 where max_field_freq_in_struct is the maximum field frequency
142 in the structure. C is a constant defining which portion of
143 max_field_freq_in_struct the fields should have in order to be peeled.
145 If profiling information is provided, it is used to calculate the
146 frequency of field accesses. Otherwise, the structure is fully peeled.
148 IPA type-escape analysis is used to determine when it is safe
149 to peel a structure.
151 The optimization is activated by flag -fipa-struct-reorg. */
153 /* New variables created by this optimization.
154 When doing struct peeling, each variable of
155 the original struct type will be replaced by
156 the set of new variables corresponding to
157 the new structure types. */
158 struct new_var_data {
159 /* VAR_DECL for original struct type. */
160 tree orig_var;
161 /* Vector of new variables. */
162 VEC(tree, heap) *new_vars;
165 typedef struct new_var_data *new_var;
166 typedef const struct new_var_data *const_new_var;
168 /* This structure represents allocation site of the structure. */
169 typedef struct alloc_site
171 tree stmt;
172 d_str str;
173 } alloc_site_t;
175 DEF_VEC_O (alloc_site_t);
176 DEF_VEC_ALLOC_O (alloc_site_t, heap);
178 /* Allocation sites that belong to the same function. */
179 struct func_alloc_sites
181 tree func;
182 /* Vector of allocation sites for function. */
183 VEC (alloc_site_t, heap) *allocs;
186 typedef struct func_alloc_sites *fallocs_t;
187 typedef const struct func_alloc_sites *const_fallocs_t;
189 /* All allocation sites in the program. */
190 htab_t alloc_sites;
192 /* New global variables. Generated once for whole program. */
193 htab_t new_global_vars;
195 /* New local variables. Generated per-function. */
196 htab_t new_local_vars;
198 /* Vector of structures to be transformed. */
199 typedef struct data_structure structure;
200 DEF_VEC_O (structure);
201 DEF_VEC_ALLOC_O (structure, heap);
202 VEC (structure, heap) *structures;
204 /* Forward declarations. */
205 static bool is_equal_types (tree, tree);
207 /* Strip structure TYPE from pointers and arrays. */
209 static inline tree
210 strip_type (tree type)
212 gcc_assert (TYPE_P (type));
214 while (POINTER_TYPE_P (type)
215 || TREE_CODE (type) == ARRAY_TYPE)
216 type = TREE_TYPE (type);
218 return type;
221 /* This function returns type of VAR. */
223 static inline tree
224 get_type_of_var (tree var)
226 if (!var)
227 return NULL;
229 if (TREE_CODE (var) == PARM_DECL)
230 return DECL_ARG_TYPE (var);
231 else
232 return TREE_TYPE (var);
235 /* Set of actions we do for each newly generated STMT. */
237 static inline void
238 finalize_stmt (tree stmt)
240 update_stmt (stmt);
241 mark_symbols_for_renaming (stmt);
244 /* This function finalizes STMT and appends it to the list STMTS. */
246 static inline void
247 finalize_stmt_and_append (tree *stmts, tree stmt)
249 append_to_statement_list (stmt, stmts);
250 finalize_stmt (stmt);
253 /* Given structure type SRT_TYPE and field FIELD,
254 this function is looking for a field with the same name
255 and type as FIELD in STR_TYPE. It returns it if found,
256 or NULL_TREE otherwise. */
258 static tree
259 find_field_in_struct_1 (tree str_type, tree field)
261 tree str_field;
263 for (str_field = TYPE_FIELDS (str_type); str_field;
264 str_field = TREE_CHAIN (str_field))
266 const char * str_field_name;
267 const char * field_name;
269 str_field_name = IDENTIFIER_POINTER (DECL_NAME (str_field));
270 field_name = IDENTIFIER_POINTER (DECL_NAME (field));
272 gcc_assert (str_field_name);
273 gcc_assert (field_name);
275 if (!strcmp (str_field_name, field_name))
277 /* Check field types. */
278 if (is_equal_types (TREE_TYPE (str_field), TREE_TYPE (field)))
279 return str_field;
283 return NULL_TREE;
286 /* Given a field declaration FIELD_DECL, this function
287 returns corresponding field entry in structure STR. */
289 static struct field_entry *
290 find_field_in_struct (d_str str, tree field_decl)
292 int i;
294 tree field = find_field_in_struct_1 (str->decl, field_decl);
296 for (i = 0; i < str->num_fields; i++)
297 if (str->fields[i].decl == field)
298 return &(str->fields[i]);
300 return NULL;
303 /* This function checks whether ARG is a result of multiplication
304 of some number by STRUCT_SIZE. If yes, the function returns true
305 and this number is filled into NUM. */
307 static bool
308 is_result_of_mult (tree arg, tree *num, tree struct_size)
310 tree size_def_stmt = SSA_NAME_DEF_STMT (arg);
312 /* If allocation statementt was of the form
313 D.2229_10 = <alloc_func> (D.2228_9);
314 then size_def_stmt can be D.2228_9 = num.3_8 * 8; */
316 if (size_def_stmt && TREE_CODE (size_def_stmt) == GIMPLE_MODIFY_STMT)
318 tree lhs = GIMPLE_STMT_OPERAND (size_def_stmt, 0);
319 tree rhs = GIMPLE_STMT_OPERAND (size_def_stmt, 1);
321 /* We expect temporary here. */
322 if (!is_gimple_reg (lhs))
323 return false;
325 if (TREE_CODE (rhs) == MULT_EXPR)
327 tree arg0 = TREE_OPERAND (rhs, 0);
328 tree arg1 = TREE_OPERAND (rhs, 1);
330 if (operand_equal_p (arg0, struct_size, OEP_ONLY_CONST))
332 *num = arg1;
333 return true;
336 if (operand_equal_p (arg1, struct_size, OEP_ONLY_CONST))
338 *num = arg0;
339 return true;
344 *num = NULL_TREE;
345 return false;
349 /* This function returns true if access ACC corresponds to the pattern
350 generated by compiler when an address of element i of an array
351 of structures STR_DECL (pointed by p) is calculated (p[i]). If this
352 pattern is recognized correctly, this function returns true
353 and fills missing fields in ACC. Otherwise it returns false. */
355 static bool
356 decompose_indirect_ref_acc (tree str_decl, struct field_access_site *acc)
358 tree ref_var;
359 tree rhs, struct_size, op0, op1;
360 tree before_cast;
362 ref_var = TREE_OPERAND (acc->ref, 0);
364 if (TREE_CODE (ref_var) != SSA_NAME)
365 return false;
367 acc->ref_def_stmt = SSA_NAME_DEF_STMT (ref_var);
368 if (!(acc->ref_def_stmt)
369 || (TREE_CODE (acc->ref_def_stmt) != GIMPLE_MODIFY_STMT))
370 return false;
372 rhs = GIMPLE_STMT_OPERAND (acc->ref_def_stmt, 1);
374 if (TREE_CODE (rhs) != PLUS_EXPR
375 && TREE_CODE (rhs)!= MINUS_EXPR
376 && TREE_CODE (rhs) != POINTER_PLUS_EXPR)
377 return false;
379 op0 = TREE_OPERAND (rhs, 0);
380 op1 = TREE_OPERAND (rhs, 1);
382 if (!is_array_access_through_pointer_and_index (TREE_CODE (rhs), op0, op1,
383 &acc->base, &acc->offset,
384 &acc->cast_stmt))
385 return false;
387 if (acc->cast_stmt)
388 before_cast = SINGLE_SSA_TREE_OPERAND (acc->cast_stmt, SSA_OP_USE);
389 else
390 before_cast = acc->offset;
392 if (!before_cast)
393 return false;
396 if (SSA_NAME_IS_DEFAULT_DEF (before_cast))
397 return false;
399 struct_size = TYPE_SIZE_UNIT (str_decl);
401 if (!is_result_of_mult (before_cast, &acc->num, struct_size))
402 return false;
404 return true;
408 /* This function checks whether the access ACC of structure type STR
409 is of the form suitable for tranformation. If yes, it returns true.
410 False otherwise. */
412 static bool
413 decompose_access (tree str_decl, struct field_access_site *acc)
415 gcc_assert (acc->ref);
417 if (TREE_CODE (acc->ref) == INDIRECT_REF)
418 return decompose_indirect_ref_acc (str_decl, acc);
419 else if (TREE_CODE (acc->ref) == ARRAY_REF)
420 return true;
421 else if (TREE_CODE (acc->ref) == VAR_DECL)
422 return true;
424 return false;
427 /* This function creates empty field_access_site node. */
429 static inline struct field_access_site *
430 make_field_acc_node (void)
432 int size = sizeof (struct field_access_site);
434 return (struct field_access_site *) xcalloc (1, size);
437 /* This function returns the structure field access, defined by STMT,
438 if it is aready in hashtable of function accesses F_ACCS. */
440 static struct field_access_site *
441 is_in_field_accs (tree stmt, htab_t f_accs)
443 return (struct field_access_site *)
444 htab_find_with_hash (f_accs, stmt, htab_hash_pointer (stmt));
447 /* This function adds an access ACC to the hashtable
448 F_ACCS of field accesses. */
450 static void
451 add_field_acc_to_acc_sites (struct field_access_site *acc,
452 htab_t f_accs)
454 void **slot;
456 gcc_assert (!is_in_field_accs (acc->stmt, f_accs));
457 slot = htab_find_slot_with_hash (f_accs, acc->stmt,
458 htab_hash_pointer (acc->stmt),
459 INSERT);
460 *slot = acc;
463 /* This function adds the VAR to vector of variables of
464 an access site defined by statement STMT. If access entry
465 with statement STMT does not exist in hashtable of
466 accesses ACCS, this function creates it. */
468 static void
469 add_access_to_acc_sites (tree stmt, tree var, htab_t accs)
471 struct access_site *acc;
473 acc = (struct access_site *)
474 htab_find_with_hash (accs, stmt, htab_hash_pointer (stmt));
476 if (!acc)
478 void **slot;
480 acc = (struct access_site *) xmalloc (sizeof (struct access_site));
481 acc->stmt = stmt;
482 acc->vars = VEC_alloc (tree, heap, 10);
483 slot = htab_find_slot_with_hash (accs, stmt,
484 htab_hash_pointer (stmt), INSERT);
485 *slot = acc;
488 VEC_safe_push (tree, heap, acc->vars, var);
491 /* This function adds NEW_DECL to function
492 referenced vars, and marks it for renaming. */
494 static void
495 finalize_var_creation (tree new_decl)
497 add_referenced_var (new_decl);
498 if (is_global_var (new_decl))
499 mark_call_clobbered (new_decl, ESCAPE_UNKNOWN);
500 mark_sym_for_renaming (new_decl);
503 /* This function finalizes VAR creation if it is a global VAR_DECL. */
505 static void
506 finalize_global_creation (tree var)
508 if (TREE_CODE (var) == VAR_DECL
509 && is_global_var (var))
510 finalize_var_creation (var);
513 /* This function inserts NEW_DECL to varpool. */
515 static inline void
516 insert_global_to_varpool (tree new_decl)
518 struct varpool_node *new_node;
520 new_node = varpool_node (new_decl);
521 notice_global_symbol (new_decl);
522 varpool_mark_needed_node (new_node);
523 varpool_finalize_decl (new_decl);
526 /* This function finalizes the creation of new variables,
527 defined by *SLOT->new_vars. */
529 static int
530 finalize_new_vars_creation (void **slot, void *data ATTRIBUTE_UNUSED)
532 new_var n_var = *(new_var *) slot;
533 unsigned i;
534 tree var;
536 for (i = 0; VEC_iterate (tree, n_var->new_vars, i, var); i++)
537 finalize_var_creation (var);
538 return 1;
541 /* This funciton updates statements in STMT_LIST with BB info. */
543 static void
544 add_bb_info (basic_block bb, tree stmt_list)
546 if (TREE_CODE (stmt_list) == STATEMENT_LIST)
548 tree_stmt_iterator tsi;
549 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
551 tree stmt = tsi_stmt (tsi);
553 set_bb_for_stmt (stmt, bb);
558 /* This function looks for the variable of NEW_TYPE type, stored in VAR.
559 It returns it, if found, and NULL_TREE otherwise. */
561 static tree
562 find_var_in_new_vars_vec (new_var var, tree new_type)
564 tree n_var;
565 unsigned i;
567 for (i = 0; VEC_iterate (tree, var->new_vars, i, n_var); i++)
569 tree type = strip_type(get_type_of_var (n_var));
570 gcc_assert (type);
572 if (type == new_type)
573 return n_var;
576 return NULL_TREE;
579 /* This function returns new_var node, the orig_var of which is DECL.
580 It looks for new_var's in NEW_VARS_HTAB. If not found,
581 the function returns NULL. */
583 static new_var
584 is_in_new_vars_htab (tree decl, htab_t new_vars_htab)
586 return (new_var) htab_find_with_hash (new_vars_htab, decl,
587 htab_hash_pointer (decl));
590 /* Given original varaiable ORIG_VAR, this function returns
591 new variable corresponding to it of NEW_TYPE type. */
593 static tree
594 find_new_var_of_type (tree orig_var, tree new_type)
596 new_var var;
597 gcc_assert (orig_var && new_type);
599 if (TREE_CODE (orig_var) == SSA_NAME)
600 orig_var = SSA_NAME_VAR (orig_var);
602 var = is_in_new_vars_htab (orig_var, new_global_vars);
603 if (!var)
604 var = is_in_new_vars_htab (orig_var, new_local_vars);
605 gcc_assert (var);
606 return find_var_in_new_vars_vec (var, new_type);
609 /* This function generates stmt:
610 res = NUM * sizeof(TYPE) and returns it.
611 res is filled into RES. */
613 static tree
614 gen_size (tree num, tree type, tree *res)
616 tree struct_size = TYPE_SIZE_UNIT (type);
617 HOST_WIDE_INT struct_size_int = TREE_INT_CST_LOW (struct_size);
618 tree new_stmt;
620 *res = create_tmp_var (TREE_TYPE (num), NULL);
622 if (*res)
623 add_referenced_var (*res);
625 if (exact_log2 (struct_size_int) == -1)
626 new_stmt = build_gimple_modify_stmt (num, struct_size);
627 else
629 tree C = build_int_cst (TREE_TYPE (num), exact_log2 (struct_size_int));
631 new_stmt = build_gimple_modify_stmt (*res, build2 (LSHIFT_EXPR,
632 TREE_TYPE (num),
633 num, C));
636 finalize_stmt (new_stmt);
637 return new_stmt;
640 /* This function generates and returns a statement, that cast variable
641 BEFORE_CAST to NEW_TYPE. The cast result variable is stored
642 into RES_P. ORIG_CAST_STMT is the original cast statement. */
644 static tree
645 gen_cast_stmt (tree before_cast, tree new_type, tree orig_cast_stmt,
646 tree *res_p)
648 tree lhs, new_lhs, new_stmt;
649 gcc_assert (TREE_CODE (orig_cast_stmt) == GIMPLE_MODIFY_STMT);
651 lhs = GIMPLE_STMT_OPERAND (orig_cast_stmt, 0);
652 new_lhs = find_new_var_of_type (lhs, new_type);
653 gcc_assert (new_lhs);
655 new_stmt = build_gimple_modify_stmt (new_lhs,
656 build1 (NOP_EXPR,
657 TREE_TYPE (new_lhs),
658 before_cast));
659 finalize_stmt (new_stmt);
660 *res_p = new_lhs;
661 return new_stmt;
664 /* This function builds an edge between BB and E->dest and updates
665 phi nodes of E->dest. It returns newly created edge. */
667 static edge
668 make_edge_and_fix_phis_of_dest (basic_block bb, edge e)
670 edge new_e;
671 tree phi, arg;
673 new_e = make_edge (bb, e->dest, e->flags);
675 for (phi = phi_nodes (new_e->dest); phi; phi = PHI_CHAIN (phi))
677 arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
678 add_phi_arg (phi, arg, new_e);
681 return new_e;
684 /* This function inserts NEW_STMTS before STMT. */
686 static void
687 insert_before_stmt (tree stmt, tree new_stmts)
689 block_stmt_iterator bsi;
691 if (!stmt || !new_stmts)
692 return;
694 bsi = bsi_for_stmt (stmt);
695 bsi_insert_before (&bsi, new_stmts, BSI_SAME_STMT);
698 /* Insert NEW_STMTS after STMT. */
700 static void
701 insert_after_stmt (tree stmt, tree new_stmts)
703 block_stmt_iterator bsi;
705 if (!stmt || !new_stmts)
706 return;
708 bsi = bsi_for_stmt (stmt);
709 bsi_insert_after (&bsi, new_stmts, BSI_SAME_STMT);
712 /* This function returns vector of allocation sites
713 that appear in function FN_DECL. */
715 static fallocs_t
716 get_fallocs (tree fn_decl)
718 return (fallocs_t) htab_find_with_hash (alloc_sites, fn_decl,
719 htab_hash_pointer (fn_decl));
722 /* If ALLOC_STMT is D.2225_7 = <alloc_func> (D.2224_6);
723 and it is a part of allocation of a structure,
724 then it is usually followed by a cast stmt
725 p_8 = (struct str_t *) D.2225_7;
726 which is returned by this function. */
728 static tree
729 get_final_alloc_stmt (tree alloc_stmt)
731 tree final_stmt;
732 use_operand_p use_p;
733 tree alloc_res;
735 if (!alloc_stmt)
736 return NULL;
738 if (TREE_CODE (alloc_stmt) != GIMPLE_MODIFY_STMT)
739 return NULL;
741 alloc_res = GIMPLE_STMT_OPERAND (alloc_stmt, 0);
743 if (TREE_CODE (alloc_res) != SSA_NAME)
744 return NULL;
746 if (!single_imm_use (alloc_res, &use_p, &final_stmt))
747 return NULL;
748 else
749 return final_stmt;
752 /* This function returns true if STMT is one of allocation
753 sites of function FN_DECL. It returns false otherwise. */
755 static bool
756 is_part_of_malloc (tree stmt, tree fn_decl)
758 fallocs_t fallocs = get_fallocs (fn_decl);
760 if (fallocs)
762 alloc_site_t *call;
763 unsigned i;
765 for (i = 0;
766 VEC_iterate (alloc_site_t, fallocs->allocs, i, call); i++)
767 if (call->stmt == stmt
768 || get_final_alloc_stmt (call->stmt) == stmt)
769 return true;
771 return false;
774 /* Auxiliary structure for a lookup over field accesses. */
775 struct find_stmt_data
777 bool found;
778 tree stmt;
781 /* This function looks for DATA->stmt among
782 the statements involved in the field access,
783 defined by SLOT. It stops when it's found. */
785 static int
786 find_in_field_accs (void **slot, void *data)
788 struct field_access_site *f_acc =
789 *(struct field_access_site **) slot;
790 tree stmt = ((struct find_stmt_data *)data)->stmt;
792 if (f_acc->stmt == stmt
793 || f_acc->ref_def_stmt == stmt
794 || f_acc->cast_stmt == stmt)
796 ((struct find_stmt_data *)data)->found = true;
797 return 0;
799 else
800 return 1;
803 /* This function checks whether STMT is part of field
804 accesses of structure STR. It returns true, if found,
805 and false otherwise. */
807 static bool
808 is_part_of_field_access (tree stmt, d_str str)
810 int i;
812 for (i = 0; i < str->num_fields; i++)
814 struct find_stmt_data data;
815 data.found = false;
816 data.stmt = stmt;
818 if (str->fields[i].acc_sites)
819 htab_traverse (str->fields[i].acc_sites, find_in_field_accs, &data);
821 if (data.found)
822 return true;
825 return false;
828 /* Auxiliary data for exclude_from_accs function. */
830 struct exclude_data
832 tree fn_decl;
833 d_str str;
836 /* This function returns component_ref with the BASE and
837 field named FIELD_ID from structure TYPE. */
839 static inline tree
840 build_comp_ref (tree base, tree field_id, tree type)
842 tree field;
843 bool found = false;
846 /* Find field of structure type with the same name as field_id. */
847 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
849 if (DECL_NAME (field) == field_id)
851 found = true;
852 break;
856 gcc_assert (found);
858 return build3 (COMPONENT_REF, TREE_TYPE (field), base, field, NULL_TREE);
862 /* This struct represent data used for walk_tree
863 called from function find_pos_in_stmt.
864 - ref is a tree to be found,
865 - and pos is a pointer that points to ref in stmt. */
866 struct ref_pos
868 tree *pos;
869 tree ref;
873 /* This is a callback function for walk_tree, called from
874 collect_accesses_in_bb function. DATA is a pointer to ref_pos structure.
875 When *TP is equal to DATA->ref, the walk_tree stops,
876 and found position, equal to TP, is assigned to DATA->pos. */
878 static tree
879 find_pos_in_stmt_1 (tree *tp, int *walk_subtrees, void * data)
881 struct ref_pos * r_pos = (struct ref_pos *) data;
882 tree ref = r_pos->ref;
883 tree t = *tp;
885 if (t == ref)
887 r_pos->pos = tp;
888 return t;
891 switch (TREE_CODE (t))
893 case GIMPLE_MODIFY_STMT:
895 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
896 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
897 *walk_subtrees = 1;
898 walk_tree (&lhs, find_pos_in_stmt_1, data, NULL);
899 walk_tree (&rhs, find_pos_in_stmt_1, data, NULL);
900 *walk_subtrees = 0;
902 break;
904 default:
905 *walk_subtrees = 1;
907 return NULL_TREE;
911 /* This function looks for the pointer of REF in STMT,
912 It returns it, if found, and NULL otherwise. */
914 static tree *
915 find_pos_in_stmt (tree stmt, tree ref)
917 struct ref_pos r_pos;
919 r_pos.ref = ref;
920 r_pos.pos = NULL;
921 walk_tree (&stmt, find_pos_in_stmt_1, &r_pos, NULL);
923 return r_pos.pos;
926 /* This structure is used to represent array
927 or pointer-to wrappers of structure type.
928 For example, if type1 is structure type,
929 then for type1 ** we generate two type_wrapper
930 structures with wrap = 0 each one.
931 It's used to unwind the original type up to
932 structure type, replace it with the new structure type
933 and wrap it back in the opposite order. */
935 typedef struct type_wrapper
937 /* 0 stand for pointer wrapper, and 1 for array wrapper. */
938 bool wrap;
940 /* Relevant for arrays as domain or index. */
941 tree domain;
942 }type_wrapper_t;
944 DEF_VEC_O (type_wrapper_t);
945 DEF_VEC_ALLOC_O (type_wrapper_t, heap);
947 /* This function replace field access ACC by the new
948 field access of structure type NEW_TYPE. */
950 static void
951 replace_field_acc (struct field_access_site *acc, tree new_type)
953 tree ref_var = acc->ref;
954 tree new_ref;
955 tree lhs, rhs;
956 tree *pos;
957 tree new_acc;
958 tree field_id = DECL_NAME (acc->field_decl);
959 VEC (type_wrapper_t, heap) *wrapper = VEC_alloc (type_wrapper_t, heap, 10);
960 type_wrapper_t *wr_p = NULL;
962 while (TREE_CODE (ref_var) == INDIRECT_REF
963 || TREE_CODE (ref_var) == ARRAY_REF)
965 type_wrapper_t wr;
967 if ( TREE_CODE (ref_var) == INDIRECT_REF)
969 wr.wrap = 0;
970 wr.domain = 0;
972 else
974 wr.wrap = 1;
975 wr.domain = TREE_OPERAND (ref_var, 1);
978 VEC_safe_push (type_wrapper_t, heap, wrapper, &wr);
979 ref_var = TREE_OPERAND (ref_var, 0);
982 new_ref = find_new_var_of_type (ref_var, new_type);
983 finalize_global_creation (new_ref);
985 while (VEC_length (type_wrapper_t, wrapper) != 0)
987 tree type = TREE_TYPE (TREE_TYPE (new_ref));
989 wr_p = VEC_last (type_wrapper_t, wrapper);
990 if (wr_p->wrap) /* Array. */
991 new_ref = build4 (ARRAY_REF, type, new_ref,
992 wr_p->domain, NULL_TREE, NULL_TREE);
993 else /* Pointer. */
994 new_ref = build1 (INDIRECT_REF, type, new_ref);
995 VEC_pop (type_wrapper_t, wrapper);
998 new_acc = build_comp_ref (new_ref, field_id, new_type);
999 VEC_free (type_wrapper_t, heap, wrapper);
1001 if (TREE_CODE (acc->stmt) == GIMPLE_MODIFY_STMT)
1003 lhs = GIMPLE_STMT_OPERAND (acc->stmt, 0);
1004 rhs = GIMPLE_STMT_OPERAND (acc->stmt, 1);
1007 if (lhs == acc->comp_ref)
1008 GIMPLE_STMT_OPERAND (acc->stmt, 0) = new_acc;
1009 else if (rhs == acc->comp_ref)
1010 GIMPLE_STMT_OPERAND (acc->stmt, 1) = new_acc;
1011 else
1013 pos = find_pos_in_stmt (acc->stmt, acc->comp_ref);
1014 gcc_assert (pos);
1015 *pos = new_acc;
1018 else
1020 pos = find_pos_in_stmt (acc->stmt, acc->comp_ref);
1021 gcc_assert (pos);
1022 *pos = new_acc;
1025 finalize_stmt (acc->stmt);
1028 /* This function replace field access ACC by a new field access
1029 of structure type NEW_TYPE. */
1031 static void
1032 replace_field_access_stmt (struct field_access_site *acc, tree new_type)
1035 if (TREE_CODE (acc->ref) == INDIRECT_REF
1036 ||TREE_CODE (acc->ref) == ARRAY_REF
1037 ||TREE_CODE (acc->ref) == VAR_DECL)
1038 replace_field_acc (acc, new_type);
1039 else
1040 gcc_unreachable ();
1043 /* This function looks for d_str, represented by TYPE, in the structures
1044 vector. If found, it returns an index of found structure. Otherwise
1045 it returns a length of the structures vector. */
1047 static unsigned
1048 find_structure (tree type)
1050 d_str str;
1051 unsigned i;
1053 type = TYPE_MAIN_VARIANT (type);
1055 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
1056 if (is_equal_types (str->decl, type))
1057 return i;
1059 return VEC_length (structure, structures);
1062 /* In this function we create new statements that have the same
1063 form as ORIG_STMT, but of type NEW_TYPE. The statements
1064 treated by this function are simple assignments,
1065 like assignments: p.8_7 = p; or statements with rhs of
1066 tree codes PLUS_EXPR and MINUS_EXPR. */
1068 static tree
1069 create_base_plus_offset (tree orig_stmt, tree new_type,
1070 tree offset)
1072 tree lhs, rhs;
1073 tree new_lhs, new_rhs;
1074 tree new_stmt;
1076 gcc_assert (TREE_CODE (orig_stmt) == GIMPLE_MODIFY_STMT);
1078 lhs = GIMPLE_STMT_OPERAND (orig_stmt, 0);
1079 rhs = GIMPLE_STMT_OPERAND (orig_stmt, 1);
1081 gcc_assert (TREE_CODE (lhs) == VAR_DECL
1082 || TREE_CODE (lhs) == SSA_NAME);
1084 new_lhs = find_new_var_of_type (lhs, new_type);
1085 gcc_assert (new_lhs);
1086 finalize_var_creation (new_lhs);
1088 switch (TREE_CODE (rhs))
1090 case PLUS_EXPR:
1091 case MINUS_EXPR:
1092 case POINTER_PLUS_EXPR:
1094 tree op0 = TREE_OPERAND (rhs, 0);
1095 tree op1 = TREE_OPERAND (rhs, 1);
1096 tree new_op0 = NULL_TREE, new_op1 = NULL_TREE;
1097 unsigned str0, str1;
1098 unsigned length = VEC_length (structure, structures);
1101 str0 = find_structure (strip_type (get_type_of_var (op0)));
1102 str1 = find_structure (strip_type (get_type_of_var (op1)));
1103 gcc_assert ((str0 != length) || (str1 != length));
1105 if (str0 != length)
1106 new_op0 = find_new_var_of_type (op0, new_type);
1107 if (str1 != length)
1108 new_op1 = find_new_var_of_type (op1, new_type);
1110 if (!new_op0)
1111 new_op0 = offset;
1112 if (!new_op1)
1113 new_op1 = offset;
1115 new_rhs = build2 (TREE_CODE (rhs), TREE_TYPE (new_op0),
1116 new_op0, new_op1);
1118 break;
1120 default:
1121 gcc_unreachable();
1124 new_stmt = build_gimple_modify_stmt (new_lhs, new_rhs);
1125 finalize_stmt (new_stmt);
1127 return new_stmt;
1130 /* Given a field access F_ACC of the FIELD, this function
1131 replaces it by the new field access. */
1133 static void
1134 create_new_field_access (struct field_access_site *f_acc,
1135 struct field_entry field)
1137 tree new_type = field.field_mapping;
1138 tree new_stmt;
1139 tree size_res;
1140 tree mult_stmt, cast_stmt;
1141 tree cast_res = NULL;
1143 if (f_acc->num)
1145 mult_stmt = gen_size (f_acc->num, new_type, &size_res);
1146 insert_before_stmt (f_acc->ref_def_stmt, mult_stmt);
1149 if (f_acc->cast_stmt)
1151 cast_stmt = gen_cast_stmt (size_res, new_type,
1152 f_acc->cast_stmt, &cast_res);
1153 insert_after_stmt (f_acc->cast_stmt, cast_stmt);
1156 if (f_acc->ref_def_stmt)
1158 tree offset;
1159 if (cast_res)
1160 offset = cast_res;
1161 else
1162 offset = size_res;
1164 new_stmt = create_base_plus_offset (f_acc->ref_def_stmt,
1165 new_type, offset);
1166 insert_after_stmt (f_acc->ref_def_stmt, new_stmt);
1169 /* In stmt D.2163_19 = D.2162_18->b; we replace variable
1170 D.2162_18 by an appropriate variable of new_type type. */
1171 replace_field_access_stmt (f_acc, new_type);
1174 /* This function creates a new condition statement
1175 corresponding to the original COND_STMT, adds new basic block
1176 and redirects condition edges. NEW_VAR is a new condition
1177 variable located in the condition statement at the position POS. */
1179 static void
1180 create_new_stmts_for_cond_expr_1 (tree new_var, tree cond_stmt, bool pos)
1182 tree new_cond;
1183 tree new_stmt;
1184 edge true_e = NULL, false_e = NULL;
1185 basic_block new_bb;
1186 tree stmt_list;
1188 extract_true_false_edges_from_block (bb_for_stmt (cond_stmt),
1189 &true_e, &false_e);
1191 new_cond = unshare_expr (COND_EXPR_COND (cond_stmt));
1193 TREE_OPERAND (new_cond, pos) = new_var;
1195 new_stmt = build3 (COND_EXPR, TREE_TYPE (cond_stmt),
1196 new_cond, NULL_TREE, NULL_TREE);
1198 finalize_stmt (new_stmt);
1200 /* Create new basic block after bb. */
1201 new_bb = create_empty_bb (bb_for_stmt (cond_stmt));
1203 /* Add new condition stmt to the new_bb. */
1204 stmt_list = bb_stmt_list (new_bb);
1205 append_to_statement_list (new_stmt, &stmt_list);
1206 add_bb_info (new_bb, stmt_list);
1209 /* Create false and true edges from new_bb. */
1210 make_edge_and_fix_phis_of_dest (new_bb, true_e);
1211 make_edge_and_fix_phis_of_dest (new_bb, false_e);
1213 /* Redirect one of original edges to point to new_bb. */
1214 if (TREE_CODE (cond_stmt) == NE_EXPR)
1215 redirect_edge_succ (true_e, new_bb);
1216 else
1217 redirect_edge_succ (false_e, new_bb);
1220 /* This function creates new condition statements corresponding
1221 to original condition STMT, one for each new type, and
1222 recursively redirect edges to newly generated basic blocks. */
1224 static void
1225 create_new_stmts_for_cond_expr (tree stmt)
1227 tree cond = COND_EXPR_COND (stmt);
1228 tree arg0, arg1, arg;
1229 unsigned str0, str1;
1230 bool s0, s1;
1231 d_str str;
1232 tree type;
1233 bool pos;
1234 int i;
1235 unsigned length = VEC_length (structure, structures);
1237 gcc_assert (TREE_CODE (cond) == EQ_EXPR
1238 || TREE_CODE (cond) == NE_EXPR);
1240 arg0 = TREE_OPERAND (cond, 0);
1241 arg1 = TREE_OPERAND (cond, 1);
1243 str0 = find_structure (strip_type (get_type_of_var (arg0)));
1244 str1 = find_structure (strip_type (get_type_of_var (arg1)));
1246 s0 = (str0 != length) ? true : false;
1247 s1 = (str1 != length) ? true : false;
1249 gcc_assert ((!s0 && s1) || (!s1 && s0));
1251 str = s0 ? VEC_index (structure, structures, str0):
1252 VEC_index (structure, structures, str1);
1253 arg = s0 ? arg0 : arg1;
1254 pos = s0 ? 0 : 1;
1256 for (i = 0; VEC_iterate (tree, str->new_types, i, type); i++)
1258 tree new_arg;
1260 new_arg = find_new_var_of_type (arg, type);
1261 create_new_stmts_for_cond_expr_1 (new_arg, stmt, pos);
1265 /* Create a new general access to replace original access ACC
1266 for structure type NEW_TYPE. */
1268 static tree
1269 create_general_new_stmt (struct access_site *acc, tree new_type)
1271 tree old_stmt = acc->stmt;
1272 tree var;
1273 tree new_stmt = unshare_expr (old_stmt);
1274 unsigned i;
1277 for (i = 0; VEC_iterate (tree, acc->vars, i, var); i++)
1279 tree *pos;
1280 tree new_var = find_new_var_of_type (var, new_type);
1281 tree lhs, rhs;
1283 gcc_assert (new_var);
1284 finalize_var_creation (new_var);
1286 if (TREE_CODE (new_stmt) == GIMPLE_MODIFY_STMT)
1289 lhs = GIMPLE_STMT_OPERAND (new_stmt, 0);
1290 rhs = GIMPLE_STMT_OPERAND (new_stmt, 1);
1292 if (TREE_CODE (lhs) == SSA_NAME)
1293 lhs = SSA_NAME_VAR (lhs);
1294 if (TREE_CODE (rhs) == SSA_NAME)
1295 rhs = SSA_NAME_VAR (rhs);
1297 /* It can happen that rhs is a constructor.
1298 Then we have to replace it to be of new_type. */
1299 if (TREE_CODE (rhs) == CONSTRUCTOR)
1301 /* Dealing only with empty constructors right now. */
1302 gcc_assert (VEC_empty (constructor_elt,
1303 CONSTRUCTOR_ELTS (rhs)));
1304 rhs = build_constructor (new_type, 0);
1305 GIMPLE_STMT_OPERAND (new_stmt, 1) = rhs;
1308 if (lhs == var)
1309 GIMPLE_STMT_OPERAND (new_stmt, 0) = new_var;
1310 else if (rhs == var)
1311 GIMPLE_STMT_OPERAND (new_stmt, 1) = new_var;
1312 else
1314 pos = find_pos_in_stmt (new_stmt, var);
1315 gcc_assert (pos);
1316 *pos = new_var;
1319 else
1321 pos = find_pos_in_stmt (new_stmt, var);
1322 gcc_assert (pos);
1323 *pos = new_var;
1327 finalize_stmt (new_stmt);
1328 return new_stmt;
1331 /* For each new type in STR this function creates new general accesses
1332 corresponding to the original access ACC. */
1334 static void
1335 create_new_stmts_for_general_acc (struct access_site *acc, d_str str)
1337 tree type;
1338 tree stmt = acc->stmt;
1339 unsigned i;
1341 for (i = 0; VEC_iterate (tree, str->new_types, i, type); i++)
1343 tree new_stmt;
1345 new_stmt = create_general_new_stmt (acc, type);
1346 insert_after_stmt (stmt, new_stmt);
1350 /* This function creates a new general access of structure STR
1351 to replace the access ACC. */
1353 static void
1354 create_new_general_access (struct access_site *acc, d_str str)
1356 tree stmt = acc->stmt;
1357 switch (TREE_CODE (stmt))
1359 case COND_EXPR:
1360 create_new_stmts_for_cond_expr (stmt);
1361 break;
1363 default:
1364 create_new_stmts_for_general_acc (acc, str);
1368 /* Auxiliary data for creation of accesses. */
1369 struct create_acc_data
1371 basic_block bb;
1372 d_str str;
1373 int field_index;
1376 /* This function creates a new general access, defined by SLOT.
1377 DATA is a pointer to create_acc_data structure. */
1379 static int
1380 create_new_acc (void **slot, void *data)
1382 struct access_site *acc = *(struct access_site **) slot;
1383 basic_block bb = ((struct create_acc_data *)data)->bb;
1384 d_str str = ((struct create_acc_data *)data)->str;
1386 if (bb_for_stmt (acc->stmt) == bb)
1387 create_new_general_access (acc, str);
1388 return 1;
1391 /* This function creates a new field access, defined by SLOT.
1392 DATA is a pointer to create_acc_data structure. */
1394 static int
1395 create_new_field_acc (void **slot, void *data)
1397 struct field_access_site *f_acc = *(struct field_access_site **) slot;
1398 basic_block bb = ((struct create_acc_data *)data)->bb;
1399 d_str str = ((struct create_acc_data *)data)->str;
1400 int i = ((struct create_acc_data *)data)->field_index;
1402 if (bb_for_stmt (f_acc->stmt) == bb)
1403 create_new_field_access (f_acc, str->fields[i]);
1404 return 1;
1407 /* This function creates new accesses for the structure
1408 type STR in basic block BB. */
1410 static void
1411 create_new_accs_for_struct (d_str str, basic_block bb)
1413 int i;
1414 struct create_acc_data dt;
1416 dt.str = str;
1417 dt.bb = bb;
1418 dt.field_index = -1;
1420 for (i = 0; i < str->num_fields; i++)
1422 dt.field_index = i;
1424 if (str->fields[i].acc_sites)
1425 htab_traverse (str->fields[i].acc_sites,
1426 create_new_field_acc, &dt);
1428 if (str->accs)
1429 htab_traverse (str->accs, create_new_acc, &dt);
1432 /* This function inserts new variables from new_var,
1433 defined by SLOT, into varpool. */
1435 static int
1436 update_varpool_with_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
1438 new_var n_var = *(new_var *) slot;
1439 tree var;
1440 unsigned i;
1442 for (i = 0; VEC_iterate (tree, n_var->new_vars, i, var); i++)
1443 insert_global_to_varpool (var);
1444 return 1;
1447 /* This function prints a field access site, defined by SLOT. */
1449 static int
1450 dump_field_acc (void **slot, void *data ATTRIBUTE_UNUSED)
1452 struct field_access_site *f_acc =
1453 *(struct field_access_site **) slot;
1455 fprintf(dump_file, "\n");
1456 if (f_acc->stmt)
1457 print_generic_stmt (dump_file, f_acc->stmt, 0);
1458 if (f_acc->ref_def_stmt)
1459 print_generic_stmt (dump_file, f_acc->ref_def_stmt, 0);
1460 if (f_acc->cast_stmt)
1461 print_generic_stmt (dump_file, f_acc->cast_stmt, 0);
1462 return 1;
1465 /* Print field accesses from hashtable F_ACCS. */
1467 static void
1468 dump_field_acc_sites (htab_t f_accs)
1470 if (!dump_file)
1471 return;
1473 if (f_accs)
1474 htab_traverse (f_accs, dump_field_acc, NULL);
1477 /* Hash value for fallocs_t. */
1479 static hashval_t
1480 malloc_hash (const void *x)
1482 return htab_hash_pointer (((const_fallocs_t)x)->func);
1485 /* This function returns nonzero if function of func_alloc_sites' X
1486 is equal to Y. */
1488 static int
1489 malloc_eq (const void *x, const void *y)
1491 return ((const_fallocs_t)x)->func == (const_tree)y;
1494 /* This function is a callback for traversal over a structure accesses.
1495 It frees an access represented by SLOT. */
1497 static int
1498 free_accs (void **slot, void *data ATTRIBUTE_UNUSED)
1500 struct access_site * acc = *(struct access_site **) slot;
1502 VEC_free (tree, heap, acc->vars);
1503 free (acc);
1504 return 1;
1507 /* This is a callback function for traversal over field accesses.
1508 It frees a field access represented by SLOT. */
1510 static int
1511 free_field_accs (void **slot, void *data ATTRIBUTE_UNUSED)
1513 struct field_access_site *f_acc = *(struct field_access_site **) slot;
1515 free (f_acc);
1516 return 1;
1519 /* This function inserts TYPE into vector of UNSUITABLE_TYPES,
1520 if it is not there yet. */
1522 static void
1523 add_unsuitable_type (VEC (tree, heap) **unsuitable_types, tree type)
1525 unsigned i;
1526 tree t;
1528 if (!type)
1529 return;
1531 type = TYPE_MAIN_VARIANT (type);
1533 for (i = 0; VEC_iterate (tree, *unsuitable_types, i, t); i++)
1534 if (is_equal_types (t, type))
1535 break;
1537 if (i == VEC_length (tree, *unsuitable_types))
1538 VEC_safe_push (tree, heap, *unsuitable_types, type);
1541 /* Given a type TYPE, this function returns the name of the type. */
1543 static const char *
1544 get_type_name (tree type)
1546 if (! TYPE_NAME (type))
1547 return NULL;
1549 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1550 return IDENTIFIER_POINTER (TYPE_NAME (type));
1551 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1552 && DECL_NAME (TYPE_NAME (type)))
1553 return IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1554 else
1555 return NULL;
1558 /* This function is a temporary hack to overcome the types problem.
1559 When several compilation units are compiled together
1560 with -combine, the TYPE_MAIN_VARIANT of the same type
1561 can appear differently in different compilation units.
1562 Therefore this function first compares type names.
1563 If there are no names, structure bodies are recursively
1564 compared. */
1566 static bool
1567 is_equal_types (tree type1, tree type2)
1569 const char * name1,* name2;
1571 if ((!type1 && type2)
1572 ||(!type2 && type1))
1573 return false;
1575 if (!type1 && !type2)
1576 return true;
1578 if (TREE_CODE (type1) != TREE_CODE (type2))
1579 return false;
1581 if (type1 == type2)
1582 return true;
1584 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
1585 return true;
1587 name1 = get_type_name (type1);
1588 name2 = get_type_name (type2);
1590 if (name1 && name2 && !strcmp (name1, name2))
1591 return true;
1593 if (name1 && name2 && strcmp (name1, name2))
1594 return false;
1596 switch (TREE_CODE (type1))
1598 case POINTER_TYPE:
1599 case REFERENCE_TYPE:
1601 return is_equal_types (TREE_TYPE (type1), TREE_TYPE (type2));
1603 break;
1605 case RECORD_TYPE:
1606 case UNION_TYPE:
1607 case QUAL_UNION_TYPE:
1608 case ENUMERAL_TYPE:
1610 tree field1;
1611 /* Compare fields of struture. */
1612 for (field1 = TYPE_FIELDS (type1); field1;
1613 field1 = TREE_CHAIN (field1))
1615 tree field2 = find_field_in_struct_1 (type2, field1);
1616 if (!field2)
1617 return false;
1619 return true;
1621 break;
1623 case INTEGER_TYPE:
1625 if (TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1626 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2))
1627 return true;
1629 break;
1631 case ARRAY_TYPE:
1633 tree d1, d2;
1634 tree max1, min1, max2, min2;
1636 if (!is_equal_types (TREE_TYPE (type1), TREE_TYPE (type2)))
1637 return false;
1639 d1 = TYPE_DOMAIN (type1);
1640 d2 = TYPE_DOMAIN (type2);
1642 if (!d1 || !d2)
1643 return false;
1645 max1 = TYPE_MAX_VALUE (d1);
1646 max2 = TYPE_MAX_VALUE (d2);
1647 min1 = TYPE_MIN_VALUE (d1);
1648 min2 = TYPE_MIN_VALUE (d2);
1650 if (max1 && max2 && min1 && min2
1651 && TREE_CODE (max1) == TREE_CODE (max2)
1652 && TREE_CODE (max1) == INTEGER_CST
1653 && TREE_CODE (min1) == TREE_CODE (min2)
1654 && TREE_CODE (min1) == INTEGER_CST
1655 && tree_int_cst_equal (max1, max2)
1656 && tree_int_cst_equal (min1, min2))
1657 return true;
1659 break;
1661 default:
1662 gcc_unreachable();
1665 return false;
1668 /* This function free non-field accesses from hashtable ACCS. */
1670 static void
1671 free_accesses (htab_t accs)
1673 if (accs)
1674 htab_traverse (accs, free_accs, NULL);
1675 htab_delete (accs);
1678 /* This function free field accesses hashtable F_ACCS. */
1680 static void
1681 free_field_accesses (htab_t f_accs)
1683 if (f_accs)
1684 htab_traverse (f_accs, free_field_accs, NULL);
1685 htab_delete (f_accs);
1688 /* Update call graph with new edge generated by new MALLOC_STMT.
1689 The edge origin is CONTEXT function. */
1691 static void
1692 update_cgraph_with_malloc_call (tree malloc_stmt, tree context)
1694 tree call_expr;
1695 struct cgraph_node *src, *dest;
1696 tree malloc_fn_decl;
1698 if (!malloc_stmt)
1699 return;
1701 call_expr = get_call_expr_in (malloc_stmt);
1702 malloc_fn_decl = get_callee_fndecl (call_expr);
1704 src = cgraph_node (context);
1705 dest = cgraph_node (malloc_fn_decl);
1706 cgraph_create_edge (src, dest, malloc_stmt,
1707 0, 0, bb_for_stmt (malloc_stmt)->loop_depth);
1710 /* This function generates set of statements required
1711 to allocate number NUM of structures of type NEW_TYPE.
1712 The statements are stored in NEW_STMTS. The statement that contain
1713 call to malloc is returned. MALLOC_STMT is an original call to malloc. */
1715 static tree
1716 create_new_malloc (tree malloc_stmt, tree new_type, tree *new_stmts, tree num)
1718 tree new_malloc_size;
1719 tree call_expr, malloc_fn_decl;
1720 tree new_stmt, malloc_res;
1721 tree call_stmt, final_stmt;
1722 tree cast_res;
1724 gcc_assert (num && malloc_stmt && new_type);
1725 *new_stmts = alloc_stmt_list ();
1727 /* Generate argument to malloc as multiplication of num
1728 and size of new_type. */
1729 new_stmt = gen_size (num, new_type, &new_malloc_size);
1730 append_to_statement_list (new_stmt, new_stmts);
1732 /* Generate new call for malloc. */
1733 malloc_res = create_tmp_var (integer_type_node, NULL);
1735 if (malloc_res)
1736 add_referenced_var (malloc_res);
1738 call_expr = get_call_expr_in (malloc_stmt);
1739 malloc_fn_decl = get_callee_fndecl (call_expr);
1740 call_expr = build_call_expr (malloc_fn_decl, 1, new_malloc_size);
1741 call_stmt = build_gimple_modify_stmt (malloc_res, call_expr);
1742 finalize_stmt_and_append (new_stmts, call_stmt);
1744 /* Create new cast statement. */
1745 final_stmt = get_final_alloc_stmt (malloc_stmt);
1746 gcc_assert (final_stmt);
1747 new_stmt = gen_cast_stmt (malloc_res, new_type, final_stmt, &cast_res);
1748 append_to_statement_list (new_stmt, new_stmts);
1750 return call_stmt;
1753 /* This function returns a tree representing
1754 the number of instances of structure STR_DECL allocated
1755 by allocation STMT. If new statments are generated,
1756 they are filled into NEW_STMTS_P. */
1758 static tree
1759 gen_num_of_structs_in_malloc (tree stmt, tree str_decl, tree *new_stmts_p)
1761 call_expr_arg_iterator iter;
1762 tree arg;
1763 tree call_expr;
1764 tree struct_size;
1765 HOST_WIDE_INT struct_size_int;
1767 if (!stmt)
1768 return NULL_TREE;
1770 /* Get malloc argument. */
1771 call_expr = get_call_expr_in (stmt);
1772 if (!call_expr)
1773 return NULL_TREE;
1775 arg = first_call_expr_arg (call_expr, &iter);
1777 if (TREE_CODE (arg) != SSA_NAME
1778 && !TREE_CONSTANT (arg))
1779 return NULL_TREE;
1781 struct_size = TYPE_SIZE_UNIT (str_decl);
1782 struct_size_int = TREE_INT_CST_LOW (struct_size);
1784 gcc_assert (struct_size);
1786 if (TREE_CODE (arg) == SSA_NAME)
1788 tree num, div_stmt;
1790 if (is_result_of_mult (arg, &num, struct_size))
1791 return num;
1793 num = create_tmp_var (integer_type_node, NULL);
1795 if (num)
1796 add_referenced_var (num);
1798 if (exact_log2 (struct_size_int) == -1)
1799 div_stmt = build_gimple_modify_stmt (num,
1800 build2 (TRUNC_DIV_EXPR,
1801 integer_type_node,
1802 arg, struct_size));
1803 else
1805 tree C = build_int_cst (integer_type_node,
1806 exact_log2 (struct_size_int));
1808 div_stmt =
1809 build_gimple_modify_stmt (num, build2 (RSHIFT_EXPR,
1810 integer_type_node,
1811 arg, C));
1813 *new_stmts_p = alloc_stmt_list ();
1814 append_to_statement_list (div_stmt,
1815 new_stmts_p);
1816 finalize_stmt (div_stmt);
1817 return num;
1820 if (CONSTANT_CLASS_P (arg)
1821 && multiple_of_p (TREE_TYPE (struct_size), arg, struct_size))
1822 return int_const_binop (TRUNC_DIV_EXPR, arg, struct_size, 0);
1824 return NULL_TREE;
1827 /* This function is a callback for traversal on new_var's hashtable.
1828 SLOT is a pointer to new_var. This function prints to dump_file
1829 an original variable and all new variables from the new_var
1830 pointed by *SLOT. */
1832 static int
1833 dump_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
1835 new_var n_var = *(new_var *) slot;
1836 tree var_type;
1837 tree var;
1838 unsigned i;
1840 var_type = get_type_of_var (n_var->orig_var);
1842 fprintf (dump_file, "\nOrig var: ");
1843 print_generic_expr (dump_file, n_var->orig_var, 0);
1844 fprintf (dump_file, " of type ");
1845 print_generic_expr (dump_file, var_type, 0);
1846 fprintf (dump_file, "\n");
1848 for (i = 0;
1849 VEC_iterate (tree, n_var->new_vars, i, var); i++)
1851 var_type = get_type_of_var (var);
1853 fprintf (dump_file, " ");
1854 print_generic_expr (dump_file, var, 0);
1855 fprintf (dump_file, " of type ");
1856 print_generic_expr (dump_file, var_type, 0);
1857 fprintf (dump_file, "\n");
1859 return 1;
1862 /* This function copies attributes form ORIG_DECL to NEW_DECL. */
1864 static inline void
1865 copy_decl_attributes (tree new_decl, tree orig_decl)
1868 DECL_ARTIFICIAL (new_decl) = 1;
1869 DECL_EXTERNAL (new_decl) = DECL_EXTERNAL (orig_decl);
1870 TREE_STATIC (new_decl) = TREE_STATIC (orig_decl);
1871 TREE_PUBLIC (new_decl) = TREE_PUBLIC (orig_decl);
1872 TREE_USED (new_decl) = TREE_USED (orig_decl);
1873 DECL_CONTEXT (new_decl) = DECL_CONTEXT (orig_decl);
1874 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (orig_decl);
1875 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (orig_decl);
1877 if (TREE_CODE (orig_decl) == VAR_DECL)
1879 TREE_READONLY (new_decl) = TREE_READONLY (orig_decl);
1880 DECL_TLS_MODEL (new_decl) = DECL_TLS_MODEL (orig_decl);
1884 /* This function wraps NEW_STR_TYPE in pointers or arrays wrapper
1885 the same way as a structure type is wrapped in DECL.
1886 It returns the generated type. */
1888 static inline tree
1889 gen_struct_type (tree decl, tree new_str_type)
1891 tree type_orig = get_type_of_var (decl);
1892 tree new_type = new_str_type;
1893 VEC (type_wrapper_t, heap) *wrapper = VEC_alloc (type_wrapper_t, heap, 10);
1894 type_wrapper_t wr;
1895 type_wrapper_t *wr_p;
1897 while (POINTER_TYPE_P (type_orig)
1898 || TREE_CODE (type_orig) == ARRAY_TYPE)
1900 if (POINTER_TYPE_P (type_orig))
1902 wr.wrap = 0;
1903 wr.domain = NULL_TREE;
1905 else if (TREE_CODE (type_orig) == ARRAY_TYPE)
1907 wr.wrap = 1;
1908 wr.domain = TYPE_DOMAIN (type_orig);
1910 VEC_safe_push (type_wrapper_t, heap, wrapper, &wr);
1911 type_orig = TREE_TYPE (type_orig);
1914 while (VEC_length (type_wrapper_t, wrapper) != 0)
1916 wr_p = VEC_last (type_wrapper_t, wrapper);
1918 if (wr_p->wrap) /* Array. */
1919 new_type = build_array_type (new_type, wr_p->domain);
1920 else /* Pointer. */
1921 new_type = build_pointer_type (new_type);
1923 VEC_pop (type_wrapper_t, wrapper);
1926 VEC_free (type_wrapper_t, heap, wrapper);
1927 return new_type;
1930 /* This function generates and returns new variable name based on
1931 ORIG_DECL name, combined with index I.
1932 The form of the new name is <orig_name>.<I> . */
1934 static tree
1935 gen_var_name (tree orig_decl, unsigned HOST_WIDE_INT i)
1937 const char *old_name;
1938 char *prefix;
1939 char *new_name;
1941 if (!DECL_NAME (orig_decl)
1942 || !IDENTIFIER_POINTER (DECL_NAME (orig_decl)))
1943 return NULL;
1945 /* If the original variable has a name, create an
1946 appropriate new name for the new variable. */
1948 old_name = IDENTIFIER_POINTER (DECL_NAME (orig_decl));
1949 prefix = alloca (strlen (old_name) + 1);
1950 strcpy (prefix, old_name);
1951 ASM_FORMAT_PRIVATE_NAME (new_name, prefix, i);
1952 return get_identifier (new_name);
1955 /* This function adds NEW_NODE to hashtable of new_var's NEW_VARS_HTAB. */
1957 static void
1958 add_to_new_vars_htab (new_var new_node, htab_t new_vars_htab)
1960 void **slot;
1962 slot = htab_find_slot_with_hash (new_vars_htab, new_node->orig_var,
1963 htab_hash_pointer (new_node->orig_var),
1964 INSERT);
1965 *slot = new_node;
1968 /* This function creates and returns new_var_data node
1969 with empty new_vars and orig_var equal to VAR. */
1971 static new_var
1972 create_new_var_node (tree var, d_str str)
1974 new_var node;
1976 node = (new_var) xmalloc (sizeof (struct new_var_data));
1977 node->orig_var = var;
1978 node->new_vars = VEC_alloc (tree, heap, VEC_length (tree, str->new_types));
1979 return node;
1982 /* Check whether the type of VAR is potential candidate for peeling.
1983 Returns true if yes, false otherwise. If yes, TYPE_P will contain
1984 candidate type. If VAR is initialized, the type of VAR will be added
1985 to UNSUITABLE_TYPES. */
1987 static bool
1988 is_candidate (tree var, tree *type_p, VEC (tree, heap) **unsuitable_types)
1990 tree type;
1991 bool initialized = false;
1993 *type_p = NULL;
1995 if (!var)
1996 return false;
1998 /* There is no support of initialized vars. */
1999 if (TREE_CODE (var) == VAR_DECL
2000 && DECL_INITIAL (var) != NULL_TREE)
2001 initialized = true;
2003 type = get_type_of_var (var);
2005 if (type)
2007 type = TYPE_MAIN_VARIANT (strip_type (type));
2008 if (TREE_CODE (type) != RECORD_TYPE)
2009 return false;
2010 else
2012 if (initialized && unsuitable_types && *unsuitable_types)
2013 add_unsuitable_type (unsuitable_types, type);
2014 *type_p = type;
2015 return true;
2018 else
2019 return false;
2022 /* Hash value for field_access_site. */
2024 static hashval_t
2025 field_acc_hash (const void *x)
2027 return htab_hash_pointer (((const struct field_access_site *)x)->stmt);
2030 /* This function returns nonzero if stmt of field_access_site X
2031 is equal to Y. */
2033 static int
2034 field_acc_eq (const void *x, const void *y)
2036 return ((const struct field_access_site *)x)->stmt == (const_tree)y;
2039 /* This function prints an access site, defined by SLOT. */
2041 static int
2042 dump_acc (void **slot, void *data ATTRIBUTE_UNUSED)
2044 struct access_site *acc = *(struct access_site **) slot;
2045 tree var;
2046 unsigned i;
2048 fprintf(dump_file, "\n");
2049 if (acc->stmt)
2050 print_generic_stmt (dump_file, acc->stmt, 0);
2051 fprintf(dump_file, " : ");
2053 for (i = 0; VEC_iterate (tree, acc->vars, i, var); i++)
2055 print_generic_expr (dump_file, var, 0);
2056 fprintf(dump_file, ", ");
2058 return 1;
2061 /* This function frees memory allocated for strcuture clusters,
2062 starting from CLUSTER. */
2064 static void
2065 free_struct_cluster (struct field_cluster* cluster)
2067 if (cluster)
2069 if (cluster->fields_in_cluster)
2070 sbitmap_free (cluster->fields_in_cluster);
2071 if (cluster->sibling)
2072 free_struct_cluster (cluster->sibling);
2073 free (cluster);
2077 /* Free all allocated memory under the structure node pointed by D_NODE. */
2079 static void
2080 free_data_struct (d_str d_node)
2082 int i;
2084 if (!d_node)
2085 return;
2087 if (dump_file)
2089 fprintf (dump_file, "\nRemoving data structure \"");
2090 print_generic_expr (dump_file, d_node->decl, 0);
2091 fprintf (dump_file, "\" from data_struct_list.");
2094 /* Free all space under d_node. */
2095 if (d_node->fields)
2097 for (i = 0; i < d_node->num_fields; i++)
2098 free_field_accesses (d_node->fields[i].acc_sites);
2099 free (d_node->fields);
2102 if (d_node->accs)
2103 free_accesses (d_node->accs);
2105 if (d_node->struct_clustering)
2106 free_struct_cluster (d_node->struct_clustering);
2108 if (d_node->new_types)
2109 VEC_free (tree, heap, d_node->new_types);
2112 /* This function creates new general and field accesses in BB. */
2114 static void
2115 create_new_accesses_in_bb (basic_block bb)
2117 d_str str;
2118 unsigned i;
2120 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2121 create_new_accs_for_struct (str, bb);
2124 /* This function adds allocation sites for peeled structures.
2125 M_DATA is vector of allocation sites of function CONTEXT. */
2127 static void
2128 create_new_alloc_sites (fallocs_t m_data, tree context)
2130 alloc_site_t *call;
2131 unsigned j;
2133 for (j = 0;
2134 VEC_iterate (alloc_site_t, m_data->allocs, j, call); j++)
2136 tree stmt = call->stmt;
2137 d_str str = call->str;
2138 tree num;
2139 tree new_stmts = NULL_TREE;
2140 tree last_stmt = get_final_alloc_stmt (stmt);
2141 unsigned i;
2142 tree type;
2144 num = gen_num_of_structs_in_malloc (stmt, str->decl, &new_stmts);
2145 if (new_stmts)
2147 last_stmt = tsi_stmt (tsi_last (new_stmts));
2148 insert_after_stmt (last_stmt, new_stmts);
2151 /* Generate an allocation sites for each new structure type. */
2152 for (i = 0;
2153 VEC_iterate (tree, str->new_types, i, type); i++)
2155 tree new_malloc_stmt = NULL_TREE;
2156 tree last_stmt_tmp = NULL_TREE;
2158 new_stmts = NULL_TREE;
2159 new_malloc_stmt = create_new_malloc (stmt, type, &new_stmts, num);
2160 last_stmt_tmp = tsi_stmt (tsi_last (new_stmts));
2161 insert_after_stmt (last_stmt, new_stmts);
2162 update_cgraph_with_malloc_call (new_malloc_stmt, context);
2163 last_stmt = last_stmt_tmp;
2168 /* This function prints new variables from hashtable
2169 NEW_VARS_HTAB to dump_file. */
2171 static void
2172 dump_new_vars (htab_t new_vars_htab)
2174 if (!dump_file)
2175 return;
2177 if (new_vars_htab)
2178 htab_traverse (new_vars_htab, dump_new_var, NULL);
2181 /* Given an original variable ORIG_DECL of structure type STR,
2182 this function generates new variables of the types defined
2183 by STR->new_type. Generated types are saved in new_var node NODE.
2184 ORIG_DECL should has VAR_DECL tree_code. */
2186 static void
2187 create_new_var_1 (tree orig_decl, d_str str, new_var node)
2189 unsigned i;
2190 tree type;
2192 for (i = 0;
2193 VEC_iterate (tree, str->new_types, i, type); i++)
2195 tree new_decl = NULL;
2196 tree new_name;
2198 new_name = gen_var_name (orig_decl, i);
2199 type = gen_struct_type (orig_decl, type);
2201 if (is_global_var (orig_decl))
2202 new_decl = build_decl (VAR_DECL, new_name, type);
2203 else
2205 const char *name = new_name ? IDENTIFIER_POINTER (new_name) : NULL;
2206 new_decl = create_tmp_var (type, name);
2209 copy_decl_attributes (new_decl, orig_decl);
2210 VEC_safe_push (tree, heap, node->new_vars, new_decl);
2214 /* This function creates new variables to
2215 substitute the original variable VAR_DECL and adds
2216 them to the new_var's hashtable NEW_VARS_HTAB. */
2218 static void
2219 create_new_var (tree var_decl, htab_t new_vars_htab)
2221 new_var node;
2222 d_str str;
2223 tree type;
2224 unsigned i;
2226 if (!var_decl || is_in_new_vars_htab (var_decl, new_vars_htab))
2227 return;
2229 if (!is_candidate (var_decl, &type, NULL))
2230 return;
2232 i = find_structure (type);
2233 if (i == VEC_length (structure, structures))
2234 return;
2236 str = VEC_index (structure, structures, i);
2237 node = create_new_var_node (var_decl, str);
2238 create_new_var_1 (var_decl, str, node);
2239 add_to_new_vars_htab (node, new_vars_htab);
2242 /* Hash value for new_var. */
2244 static hashval_t
2245 new_var_hash (const void *x)
2247 return htab_hash_pointer (((const_new_var)x)->orig_var);
2250 /* This function returns nonzero if orig_var of new_var X is equal to Y. */
2252 static int
2253 new_var_eq (const void *x, const void *y)
2255 return ((const_new_var)x)->orig_var == (const_tree)y;
2258 /* This function check whether a structure type represented by STR
2259 escapes due to ipa-type-escape analysis. If yes, this type is added
2260 to UNSUITABLE_TYPES vector. */
2262 static void
2263 check_type_escape (d_str str, VEC (tree, heap) **unsuitable_types)
2265 tree type = str->decl;
2267 if (!ipa_type_escape_type_contained_p (type))
2269 if (dump_file)
2271 fprintf (dump_file, "\nEscaping type is ");
2272 print_generic_expr (dump_file, type, 0);
2274 add_unsuitable_type (unsuitable_types, type);
2278 /* Hash value for access_site. */
2280 static hashval_t
2281 acc_hash (const void *x)
2283 return htab_hash_pointer (((const struct access_site *)x)->stmt);
2286 /* Return nonzero if stmt of access_site X is equal to Y. */
2288 static int
2289 acc_eq (const void *x, const void *y)
2291 return ((const struct access_site *)x)->stmt == (const_tree)y;
2294 /* Given a structure declaration STRUCT_DECL, and number of fields
2295 in the structure NUM_FIELDS, this function creates and returns
2296 corresponding field_entry's. */
2298 static struct field_entry *
2299 get_fields (tree struct_decl, int num_fields)
2301 struct field_entry *list;
2302 tree t = TYPE_FIELDS (struct_decl);
2303 int idx = 0;
2305 list =
2306 (struct field_entry *) xmalloc (num_fields * sizeof (struct field_entry));
2308 for (idx = 0 ; t; t = TREE_CHAIN (t), idx++)
2309 if (TREE_CODE (t) == FIELD_DECL)
2311 list[idx].index = idx;
2312 list[idx].decl = t;
2313 list[idx].acc_sites =
2314 htab_create (32, field_acc_hash, field_acc_eq, NULL);
2315 list[idx].count = 0;
2316 list[idx].field_mapping = NULL_TREE;
2319 return list;
2322 /* Print non-field accesses from hashtable ACCS of structure. */
2324 static void
2325 dump_access_sites (htab_t accs)
2327 if (!dump_file)
2328 return;
2330 if (accs)
2331 htab_traverse (accs, dump_acc, NULL);
2334 /* This function removes the structure with index I from structures vector. */
2336 static void
2337 remove_structure (unsigned i)
2339 d_str str;
2341 if (i >= VEC_length (structure, structures))
2342 return;
2344 str = VEC_index (structure, structures, i);
2345 free_data_struct (str);
2346 VEC_ordered_remove (structure, structures, i);
2349 /* Currently we support only EQ_EXPR or NE_EXPR conditions.
2350 COND_STNT is a condition statement to check. */
2352 static bool
2353 is_safe_cond_expr (tree cond_stmt)
2356 tree arg0, arg1;
2357 unsigned str0, str1;
2358 bool s0, s1;
2359 unsigned length = VEC_length (structure, structures);
2361 tree cond = COND_EXPR_COND (cond_stmt);
2363 if (TREE_CODE (cond) != EQ_EXPR
2364 && TREE_CODE (cond) != NE_EXPR)
2365 return false;
2367 if (TREE_CODE_LENGTH (TREE_CODE (cond)) != 2)
2368 return false;
2370 arg0 = TREE_OPERAND (cond, 0);
2371 arg1 = TREE_OPERAND (cond, 1);
2373 str0 = find_structure (strip_type (get_type_of_var (arg0)));
2374 str1 = find_structure (strip_type (get_type_of_var (arg1)));
2376 s0 = (str0 != length) ? true : false;
2377 s1 = (str1 != length) ? true : false;
2379 if (!((!s0 && s1) || (!s1 && s0)))
2380 return false;
2382 return true;
2385 /* This function excludes statements, that are
2386 part of allocation sites or field accesses, from the
2387 hashtable of general accesses. SLOT represents general
2388 access that will be checked. DATA is a pointer to
2389 exclude_data structure. */
2391 static int
2392 exclude_from_accs (void **slot, void *data)
2394 struct access_site *acc = *(struct access_site **) slot;
2395 tree fn_decl = ((struct exclude_data *)data)->fn_decl;
2396 d_str str = ((struct exclude_data *)data)->str;
2398 if (is_part_of_malloc (acc->stmt, fn_decl)
2399 || is_part_of_field_access (acc->stmt, str))
2401 VEC_free (tree, heap, acc->vars);
2402 free (acc);
2403 htab_clear_slot (str->accs, slot);
2405 return 1;
2408 /* Callback function for walk_tree called from collect_accesses_in_bb
2409 function. DATA is the statement which is analyzed. */
2411 static tree
2412 get_stmt_accesses (tree *tp, int *walk_subtrees, void *data)
2414 tree stmt = (tree) data;
2415 tree t = *tp;
2417 if (!t)
2418 return NULL;
2420 switch (TREE_CODE (t))
2422 case GIMPLE_MODIFY_STMT:
2424 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
2425 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
2426 *walk_subtrees = 1;
2427 walk_tree (&lhs, get_stmt_accesses, data, NULL);
2428 walk_tree (&rhs, get_stmt_accesses, data, NULL);
2429 *walk_subtrees = 0;
2431 break;
2433 case BIT_FIELD_REF:
2435 tree var = TREE_OPERAND(t, 0);
2436 tree type = TYPE_MAIN_VARIANT (strip_type (get_type_of_var (var)));
2437 unsigned i = find_structure (type);
2439 if (i != VEC_length (structure, structures))
2440 remove_structure (i);
2442 break;
2444 case COMPONENT_REF:
2446 tree ref = TREE_OPERAND (t, 0);
2447 tree field_decl = TREE_OPERAND (t, 1);
2450 if ((TREE_CODE (ref) == INDIRECT_REF
2451 || TREE_CODE (ref) == ARRAY_REF
2452 || TREE_CODE (ref) == VAR_DECL)
2453 && TREE_CODE (field_decl) == FIELD_DECL)
2455 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
2456 unsigned i = find_structure (type);
2458 if (i != VEC_length (structure, structures))
2460 d_str str = VEC_index (structure, structures, i);
2461 struct field_entry * field =
2462 find_field_in_struct (str, field_decl);
2464 if (field)
2466 struct field_access_site *acc = make_field_acc_node ();
2468 gcc_assert (acc);
2470 acc->stmt = stmt;
2471 acc->comp_ref = t;
2472 acc->ref = ref;
2473 acc->field_decl = field_decl;
2475 /* Check whether the access is of the form
2476 we can deal with. */
2477 if (!decompose_access (str->decl, acc))
2479 remove_structure (i);
2480 free (acc);
2482 else
2484 /* Increase count of field. */
2485 basic_block bb = bb_for_stmt (stmt);
2486 field->count += bb->count;
2488 /* Add stmt to the acc_sites of field. */
2489 add_field_acc_to_acc_sites (acc, field->acc_sites);
2491 *walk_subtrees = 0;
2496 break;
2498 case MINUS_EXPR:
2499 case PLUS_EXPR:
2501 tree op0 = TREE_OPERAND (t, 0);
2502 tree op1 = TREE_OPERAND (t, 1);
2503 *walk_subtrees = 1;
2504 walk_tree (&op0, get_stmt_accesses, data, NULL);
2505 walk_tree (&op1, get_stmt_accesses, data, NULL);
2506 *walk_subtrees = 0;
2508 break;
2510 case COND_EXPR:
2512 tree cond = COND_EXPR_COND (t);
2513 int i;
2514 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (cond)); i++)
2516 tree t = TREE_OPERAND (cond, i);
2518 *walk_subtrees = 1;
2519 walk_tree (&t, get_stmt_accesses, data, NULL);
2521 *walk_subtrees = 0;
2523 break;
2525 case VAR_DECL:
2526 case SSA_NAME:
2528 unsigned i;
2530 if (TREE_CODE (t) == SSA_NAME)
2531 t = SSA_NAME_VAR (t);
2533 i = find_structure (strip_type (get_type_of_var (t)));
2534 if (i != VEC_length (structure, structures))
2536 d_str str;
2538 str = VEC_index (structure, structures, i);
2539 add_access_to_acc_sites (stmt, t, str->accs);
2541 *walk_subtrees = 0;
2543 break;
2545 case CALL_EXPR:
2547 /* It was checked as part of stage1 that structures
2548 to be transformed cannot be passed as parameters of functions. */
2549 *walk_subtrees = 0;
2551 break;
2553 default:
2554 return NULL;
2557 return NULL;
2560 /* Free structures hashtable. */
2562 static void
2563 free_structures (void)
2565 d_str str;
2566 unsigned i;
2568 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2569 free_data_struct (str);
2571 VEC_free (structure, heap, structures);
2572 structures = NULL;
2575 /* This function is a callback for traversal over new_var's hashtable.
2576 SLOT is a pointer to new_var. This function frees memory allocated
2577 for new_var and pointed by *SLOT. */
2579 static int
2580 free_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
2582 new_var n_var = *(new_var *) slot;
2584 /* Free vector of new_vars. */
2585 VEC_free (tree, heap, n_var->new_vars);
2586 free (n_var);
2587 return 1;
2590 /* Free new_vars hashtable NEW_VARS_HTAB. */
2592 static void
2593 free_new_vars_htab (htab_t new_vars_htab)
2595 if (new_vars_htab)
2596 htab_traverse (new_vars_htab, free_new_var, NULL);
2597 htab_delete (new_vars_htab);
2598 new_vars_htab = NULL;
2601 /* This function creates new general and field accesses that appear in cfun. */
2603 static void
2604 create_new_accesses_for_func (void)
2606 basic_block bb;
2608 FOR_EACH_BB_FN (bb, cfun)
2609 create_new_accesses_in_bb (bb);
2612 /* Create new allocation sites for the function represented by NODE. */
2614 static void
2615 create_new_alloc_sites_for_func (struct cgraph_node *node)
2617 fallocs_t fallocs = get_fallocs (node->decl);
2619 if (fallocs)
2620 create_new_alloc_sites (fallocs, node->decl);
2623 /* For each local variable of structure type from the vector of structures
2624 this function generates new variable(s) to replace it. */
2626 static void
2627 create_new_local_vars (void)
2629 tree var;
2630 referenced_var_iterator rvi;
2632 new_local_vars = htab_create (num_referenced_vars,
2633 new_var_hash, new_var_eq, NULL);
2635 FOR_EACH_REFERENCED_VAR (var, rvi)
2637 if (!is_global_var (var))
2638 create_new_var (var, new_local_vars);
2641 if (new_local_vars)
2642 htab_traverse (new_local_vars, finalize_new_vars_creation, NULL);
2643 dump_new_vars (new_local_vars);
2646 /* This function prints the SHIFT number of spaces to the DUMP_FILE. */
2648 static inline void
2649 print_shift (unsigned HOST_WIDE_INT shift)
2651 unsigned HOST_WIDE_INT sh = shift;
2653 while (sh--)
2654 fprintf (dump_file, " ");
2657 /* This function updates field_mapping of FIELDS in CLUSTER with NEW_TYPE. */
2659 static inline void
2660 update_fields_mapping (struct field_cluster *cluster, tree new_type,
2661 struct field_entry * fields, int num_fields)
2663 int i;
2665 for (i = 0; i < num_fields; i++)
2666 if (TEST_BIT (cluster->fields_in_cluster, i))
2667 fields[i].field_mapping = new_type;
2670 /* This functions builds structure with FIELDS,
2671 NAME and attributes similar to ORIG_STRUCT.
2672 It returns the newly created structure. */
2674 static tree
2675 build_basic_struct (tree fields, tree name, tree orig_struct)
2677 tree attributes = NULL_TREE;
2678 tree ref = 0;
2679 tree x;
2681 if (TYPE_ATTRIBUTES (orig_struct))
2682 attributes = unshare_expr (TYPE_ATTRIBUTES (orig_struct));
2683 ref = make_node (RECORD_TYPE);
2684 TYPE_SIZE (ref) = 0;
2685 decl_attributes (&ref, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
2686 TYPE_PACKED (ref) = TYPE_PACKED (orig_struct);
2687 for (x = fields; x; x = TREE_CHAIN (x))
2689 DECL_CONTEXT (x) = ref;
2690 DECL_PACKED (x) |= TYPE_PACKED (ref);
2692 TYPE_FIELDS (ref) = fields;
2693 layout_type (ref);
2694 TYPE_NAME (ref) = name;
2696 return ref;
2699 /* This function copies FIELDS from CLUSTER into TREE_CHAIN as part
2700 of preparation for new structure building. NUM_FIELDS is a total
2701 number of fields in the structure. The function returns newly
2702 generated fields. */
2704 static tree
2705 create_fields (struct field_cluster * cluster,
2706 struct field_entry * fields, int num_fields)
2708 int i;
2709 tree new_types = NULL_TREE;
2710 tree last = NULL_TREE;
2712 for (i = 0; i < num_fields; i++)
2713 if (TEST_BIT (cluster->fields_in_cluster, i))
2715 tree new_decl = unshare_expr (fields[i].decl);
2717 if (!new_types)
2718 new_types = new_decl;
2719 else
2720 TREE_CHAIN (last) = new_decl;
2721 last = new_decl;
2724 TREE_CHAIN (last) = NULL_TREE;
2725 return new_types;
2729 /* This function creates a cluster name. The name is based on
2730 the original structure name, if it is present. It has a form:
2732 <original_struct_name>_sub.<CLUST_NUM>
2734 The original structure name is taken from the type of DECL.
2735 If an original structure name is not present, it's generated to be:
2737 struct.<STR_NUM>
2739 The function returns identifier of the new cluster name. */
2741 static inline tree
2742 gen_cluster_name (tree decl, int clust_num, int str_num)
2744 const char * orig_name = get_type_name (decl);
2745 char * tmp_name = NULL;
2746 char * prefix;
2747 char * new_name;
2748 size_t len;
2750 if (!orig_name)
2751 ASM_FORMAT_PRIVATE_NAME(tmp_name, "struct", str_num);
2753 len = strlen (tmp_name ? tmp_name : orig_name) + strlen ("_sub");
2754 prefix = alloca (len + 1);
2755 memcpy (prefix, tmp_name ? tmp_name : orig_name,
2756 strlen (tmp_name ? tmp_name : orig_name));
2757 strcpy (prefix + strlen (tmp_name ? tmp_name : orig_name), "_sub");
2759 ASM_FORMAT_PRIVATE_NAME (new_name, prefix, clust_num);
2760 return get_identifier (new_name);
2763 /* This function checks whether the structure STR has bitfields.
2764 If yes, this structure type is added to UNSUITABLE_TYPES vector. */
2766 static void
2767 check_bitfields (d_str str, VEC (tree, heap) **unsuitable_types)
2769 tree type = str->decl;
2770 int i;
2772 for (i = 0; i < str->num_fields; i++)
2773 if (DECL_BIT_FIELD (str->fields[i].decl))
2775 add_unsuitable_type (unsuitable_types, type);
2776 if (dump_file)
2778 fprintf (dump_file, "\nType ");
2779 print_generic_expr (dump_file, type, 0);
2780 fprintf (dump_file, "\nescapes due to bitfield ");
2781 print_generic_expr (dump_file, str->fields[i].decl, 0);
2783 break;
2787 /* This function adds to UNSUITABLE_TYPES those types that escape
2788 due to results of ipa-type-escpae analysis. See ipa-type-escpae.[c,h]. */
2790 static void
2791 exclude_escaping_types_1 (VEC (tree, heap) **unsuitable_types)
2793 d_str str;
2794 unsigned i;
2796 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2797 check_type_escape (str, unsuitable_types);
2800 /* If a structure type is a return type of any function,
2801 we cannot transform it. Such type is added to UNSUITABLE_TYPES vector. */
2803 static void
2804 exclude_returned_types (VEC (tree, heap) **unsuitable_types)
2806 struct cgraph_node *c_node;
2808 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
2810 tree ret_t = TREE_TYPE (TREE_TYPE (c_node->decl));
2812 if (ret_t)
2814 ret_t = strip_type (ret_t);
2815 if (TREE_CODE (ret_t) == RECORD_TYPE)
2817 add_unsuitable_type (unsuitable_types, TYPE_MAIN_VARIANT (ret_t));
2818 if (dump_file)
2820 fprintf (dump_file, "\nThe type \"");
2821 print_generic_expr (dump_file, ret_t, 0);
2822 fprintf (dump_file,
2823 "\" is return type of function...Excluded.");
2830 /* This function looks for parameters of local functions
2831 which are of structure types, or derived from them (arrays
2832 of structures, pointers to structures, or their combinations).
2833 We are not handling peeling of such structures right now.
2834 The found structures types are added to UNSUITABLE_TYPES vector. */
2836 static void
2837 exclude_types_passed_to_local_func (VEC (tree, heap) **unsuitable_types)
2839 struct cgraph_node *c_node;
2841 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
2842 if (cgraph_function_body_availability (c_node) == AVAIL_LOCAL)
2844 tree fn = c_node->decl;
2845 tree arg;
2847 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
2849 tree type = TREE_TYPE (arg);
2851 type = strip_type (type);
2852 if (TREE_CODE (type) == RECORD_TYPE)
2854 add_unsuitable_type (unsuitable_types,
2855 TYPE_MAIN_VARIANT (type));
2856 if (dump_file)
2858 fprintf (dump_file, "\nPointer to type \"");
2859 print_generic_expr (dump_file, type, 0);
2860 fprintf (dump_file,
2861 "\" is passed to local function...Excluded.");
2868 /* This function analyzes structure form of structures
2869 potential for transformation. If we are not capable to transform
2870 structure of some form, we remove it from the structures hashtable.
2871 Right now we cannot handle nested structs, when nesting is
2872 through any level of pointers or arrays.
2874 TBD: release these constrains in future.
2876 Note, that in this function we suppose that all structures
2877 in the program are members of the structures hashtable right now,
2878 without excluding escaping types. */
2880 static void
2881 check_struct_form (d_str str, VEC (tree, heap) **unsuitable_types)
2883 int i;
2885 for (i = 0; i < str->num_fields; i++)
2887 tree f_type = strip_type(TREE_TYPE (str->fields[i].decl));
2889 if (TREE_CODE (f_type) == RECORD_TYPE)
2891 add_unsuitable_type (unsuitable_types, TYPE_MAIN_VARIANT (f_type));
2892 add_unsuitable_type (unsuitable_types, str->decl);
2893 if (dump_file)
2895 fprintf (dump_file, "\nType ");
2896 print_generic_expr (dump_file, f_type, 0);
2897 fprintf (dump_file, " is a field in the structure ");
2898 print_generic_expr (dump_file, str->decl, 0);
2899 fprintf (dump_file, ". Escaping...");
2905 /* This function adds a structure TYPE to the vector of structures,
2906 if it's not already there. */
2908 static void
2909 add_structure (tree type)
2911 struct data_structure node;
2912 unsigned i;
2913 int num_fields;
2915 type = TYPE_MAIN_VARIANT (type);
2917 i = find_structure (type);
2919 if (i != VEC_length (structure, structures))
2920 return;
2922 num_fields = fields_length (type);
2923 node.decl = type;
2924 node.num_fields = num_fields;
2925 node.fields = get_fields (type, num_fields);
2926 node.struct_clustering = NULL;
2927 node.accs = htab_create (32, acc_hash, acc_eq, NULL);
2928 node.new_types = VEC_alloc (tree, heap, num_fields);
2929 node.count = 0;
2931 VEC_safe_push (structure, heap, structures, &node);
2933 if (dump_file)
2935 fprintf (dump_file, "\nAdding data structure \"");
2936 print_generic_expr (dump_file, type, 0);
2937 fprintf (dump_file, "\" to data_struct_list.");
2941 /* This function adds an allocation site to alloc_sites hashtable.
2942 The allocation site appears in STMT of function FN_DECL and
2943 allocates the structure represented by STR. */
2945 static void
2946 add_alloc_site (tree fn_decl, tree stmt, d_str str)
2948 fallocs_t fallocs = NULL;
2949 alloc_site_t m_call;
2951 m_call.stmt = stmt;
2952 m_call.str = str;
2954 fallocs =
2955 (fallocs_t) htab_find_with_hash (alloc_sites,
2956 fn_decl, htab_hash_pointer (fn_decl));
2958 if (!fallocs)
2960 void **slot;
2962 fallocs = (fallocs_t)
2963 xmalloc (sizeof (struct func_alloc_sites));
2964 fallocs->func = fn_decl;
2965 fallocs->allocs = VEC_alloc (alloc_site_t, heap, 1);
2966 slot = htab_find_slot_with_hash (alloc_sites, fn_decl,
2967 htab_hash_pointer (fn_decl), INSERT);
2968 *slot = fallocs;
2970 VEC_safe_push (alloc_site_t, heap,
2971 fallocs->allocs, &m_call);
2973 if (dump_file)
2975 fprintf (dump_file, "\nAdding stmt ");
2976 print_generic_stmt (dump_file, stmt, 0);
2977 fprintf (dump_file, " to list of mallocs.");
2981 /* This function returns true if the result of STMT, that contains a call
2982 to an allocation function, is cast to one of the structure types.
2983 STMT should be of the form: T.2 = <alloc_func> (T.1);
2984 If true, I_P contains an index of an allocated structure.
2985 Otherwise I_P contains the length of the vector of structures. */
2987 static bool
2988 is_alloc_of_struct (tree stmt, unsigned *i_p)
2990 tree lhs;
2991 tree type;
2992 tree final_stmt;
2994 final_stmt = get_final_alloc_stmt (stmt);
2996 if (!final_stmt)
2997 return false;
2999 /* final_stmt should be of the form:
3000 T.3 = (struct_type *) T.2; */
3002 if (TREE_CODE (final_stmt) != GIMPLE_MODIFY_STMT)
3003 return false;
3005 lhs = GIMPLE_STMT_OPERAND (final_stmt, 0);
3007 type = get_type_of_var (lhs);
3009 if (!type)
3010 return false;
3012 if (!POINTER_TYPE_P (type)
3013 || TREE_CODE (strip_type (type)) != RECORD_TYPE)
3014 return false;
3016 *i_p = find_structure (strip_type (type));
3018 if (*i_p == VEC_length (structure, structures))
3019 return false;
3021 return true;
3024 /* This function prints non-field and field accesses
3025 of the structure STR. */
3027 static void
3028 dump_accs (d_str str)
3030 int i;
3032 fprintf (dump_file, "\nAccess sites of struct ");
3033 print_generic_expr (dump_file, str->decl, 0);
3035 for (i = 0; i < str->num_fields; i++)
3037 fprintf (dump_file, "\nAccess site of field ");
3038 print_generic_expr (dump_file, str->fields[i].decl, 0);
3039 dump_field_acc_sites (str->fields[i].acc_sites);
3040 fprintf (dump_file, ":\n");
3042 fprintf (dump_file, "\nGeneral access sites\n");
3043 dump_access_sites (str->accs);
3046 /* This function checks whether an access statement, pointed by SLOT,
3047 is a condition we are capable to transform. If not, it removes
3048 the structure with index, represented by DATA, from the vector
3049 of structures. */
3051 static int
3052 safe_cond_expr_check (void **slot, void *data)
3054 struct access_site *acc = *(struct access_site **) slot;
3056 if (TREE_CODE (acc->stmt) == COND_EXPR)
3058 if (!is_safe_cond_expr (acc->stmt))
3059 remove_structure (*(unsigned *) data);
3061 return 1;
3064 /* This function excludes statements that are part of allocation sites and
3065 field accesses from the hashtable of general accesses of the structure
3066 type STR. Only accesses that belong to the function represented by
3067 NODE are treated. */
3069 static void
3070 exclude_alloc_and_field_accs_1 (d_str str, struct cgraph_node *node)
3072 struct exclude_data dt;
3073 dt.str = str;
3074 dt.fn_decl = node->decl;
3076 if (dt.str->accs)
3077 htab_traverse (dt.str->accs, exclude_from_accs, &dt);
3080 /* Collect accesses to the structure types that apear in basic bloack BB. */
3082 static void
3083 collect_accesses_in_bb (basic_block bb)
3085 block_stmt_iterator bsi;
3087 for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
3089 tree stmt = bsi_stmt (bsi);
3091 /* In asm stmt we cannot always track the arguments,
3092 so we just give up. */
3093 if (TREE_CODE (stmt) == ASM_EXPR)
3095 free_structures ();
3096 break;
3099 walk_tree (&stmt, get_stmt_accesses, stmt, NULL);
3103 /* This function generates cluster substructure that cointains FIELDS.
3104 The cluster added to the set of clusters of the structure SRT. */
3106 static void
3107 gen_cluster (sbitmap fields, d_str str)
3109 struct field_cluster *crr_cluster = NULL;
3111 crr_cluster =
3112 (struct field_cluster *) xcalloc (1, sizeof (struct field_cluster));
3113 crr_cluster->sibling = str->struct_clustering;
3114 str->struct_clustering = crr_cluster;
3115 crr_cluster->fields_in_cluster = fields;
3118 /* This function peels a field with the index I from the structure DS. */
3120 static void
3121 peel_field (int i, d_str ds)
3123 struct field_cluster *crr_cluster = NULL;
3125 crr_cluster =
3126 (struct field_cluster *) xcalloc (1, sizeof (struct field_cluster));
3127 crr_cluster->sibling = ds->struct_clustering;
3128 ds->struct_clustering = crr_cluster;
3129 crr_cluster->fields_in_cluster =
3130 sbitmap_alloc ((unsigned int) ds->num_fields);
3131 sbitmap_zero (crr_cluster->fields_in_cluster);
3132 SET_BIT (crr_cluster->fields_in_cluster, i);
3135 /* This function calculates maximum field count in
3136 the structure STR. */
3138 static gcov_type
3139 get_max_field_count (d_str str)
3141 gcov_type max = 0;
3142 int i;
3144 for (i = 0; i < str->num_fields; i++)
3145 if (str->fields[i].count > max)
3146 max = str->fields[i].count;
3148 return max;
3151 /* Do struct-reorg transformation for individual function
3152 represented by NODE. All structure types relevant
3153 for this function are transformed. */
3155 static void
3156 do_reorg_for_func (struct cgraph_node *node)
3158 create_new_local_vars ();
3159 create_new_alloc_sites_for_func (node);
3160 create_new_accesses_for_func ();
3161 update_ssa (TODO_update_ssa);
3162 cleanup_tree_cfg ();
3164 /* Free auxiliary data representing local variables. */
3165 free_new_vars_htab (new_local_vars);
3168 /* Print structure TYPE, its name, if it exists, and body.
3169 INDENT defines the level of indentation (similar
3170 to the option -i of indent command). SHIFT parameter
3171 defines a number of spaces by which a structure will
3172 be shifted right. */
3174 static void
3175 dump_struct_type (tree type, unsigned HOST_WIDE_INT indent,
3176 unsigned HOST_WIDE_INT shift)
3178 const char *struct_name;
3179 tree field;
3181 if (!type || !dump_file)
3182 return;
3184 if (TREE_CODE (type) != RECORD_TYPE)
3186 print_generic_expr (dump_file, type, 0);
3187 return;
3190 print_shift (shift);
3191 struct_name = get_type_name (type);
3192 fprintf (dump_file, "struct ");
3193 if (struct_name)
3194 fprintf (dump_file, "%s\n",struct_name);
3195 print_shift (shift);
3196 fprintf (dump_file, "{\n");
3198 for (field = TYPE_FIELDS (type); field;
3199 field = TREE_CHAIN (field))
3201 unsigned HOST_WIDE_INT s = indent;
3202 tree f_type = TREE_TYPE (field);
3204 print_shift (shift);
3205 while (s--)
3206 fprintf (dump_file, " ");
3207 dump_struct_type (f_type, indent, shift + indent);
3208 fprintf(dump_file, " ");
3209 print_generic_expr (dump_file, field, 0);
3210 fprintf(dump_file, ";\n");
3212 print_shift (shift);
3213 fprintf (dump_file, "}\n");
3216 /* This function creates new structure types to replace original type,
3217 indicated by STR->decl. The names of the new structure types are
3218 derived from the original structure type. If the original structure
3219 type has no name, we assume that its name is 'struct.<STR_NUM>'. */
3221 static void
3222 create_new_type (d_str str, int *str_num)
3224 int cluster_num = 0;
3226 struct field_cluster *cluster = str->struct_clustering;
3227 while (cluster)
3229 tree name = gen_cluster_name (str->decl, cluster_num,
3230 *str_num);
3231 tree fields;
3232 tree new_type;
3233 cluster_num++;
3235 fields = create_fields (cluster, str->fields,
3236 str->num_fields);
3237 new_type = build_basic_struct (fields, name, str->decl);
3239 update_fields_mapping (cluster, new_type,
3240 str->fields, str->num_fields);
3242 VEC_safe_push (tree, heap, str->new_types, new_type);
3243 cluster = cluster->sibling;
3245 (*str_num)++;
3248 /* This function is a callback for alloc_sites hashtable
3249 traversal. SLOT is a pointer to fallocs_t.
3250 This function frees memory pointed by *SLOT. */
3252 static int
3253 free_falloc_sites (void **slot, void *data ATTRIBUTE_UNUSED)
3255 fallocs_t fallocs = *(fallocs_t *) slot;
3257 VEC_free (alloc_site_t, heap, fallocs->allocs);
3258 free (fallocs);
3259 return 1;
3262 /* Remove structures collected in UNSUITABLE_TYPES
3263 from structures vector. */
3265 static void
3266 remove_unsuitable_types (VEC (tree, heap) *unsuitable_types)
3268 d_str str;
3269 tree type;
3270 unsigned i, j;
3272 for (j = 0; VEC_iterate (tree, unsuitable_types, j, type); j++)
3273 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3274 if (is_equal_types (str->decl, type))
3276 remove_structure (i);
3277 break;
3281 /* Exclude structure types with bitfields.
3282 We would not want to interfere with other optimizations
3283 that can be done in this case. The structure types with
3284 bitfields are added to UNSUITABLE_TYPES vector. */
3286 static void
3287 exclude_types_with_bit_fields (VEC (tree, heap) **unsuitable_types)
3289 d_str str;
3290 unsigned i;
3292 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3293 check_bitfields (str, unsuitable_types);
3296 /* This function checks three types of escape. A structure type escapes:
3298 1. if it's a type of parameter of a local function.
3299 2. if it's a type of function return value.
3300 3. if it escapes as a result of ipa-type-escape analysis.
3302 The escaping structure types are added to UNSUITABLE_TYPES vector. */
3304 static void
3305 exclude_escaping_types (VEC (tree, heap) **unsuitable_types)
3307 exclude_types_passed_to_local_func (unsuitable_types);
3308 exclude_returned_types (unsuitable_types);
3309 exclude_escaping_types_1 (unsuitable_types);
3312 /* This function analyzes whether the form of
3313 structure is such that we are capable to transform it.
3314 Nested structures are checked here. Unsuitable structure
3315 types are added to UNSUITABLE_TYPE vector. */
3317 static void
3318 analyze_struct_form (VEC (tree, heap) **unsuitable_types)
3320 d_str str;
3321 unsigned i;
3323 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3324 check_struct_form (str, unsuitable_types);
3327 /* This function looks for structure types instantiated in the program.
3328 The candidate types are added to the structures vector.
3329 Unsuitable types are collected into UNSUITABLE_TYPES vector. */
3331 static void
3332 build_data_structure (VEC (tree, heap) **unsuitable_types)
3334 tree var, type;
3335 tree var_list;
3336 struct varpool_node *current_varpool;
3337 struct cgraph_node *c_node;
3339 /* Check global variables. */
3340 FOR_EACH_STATIC_VARIABLE (current_varpool)
3342 var = current_varpool->decl;
3343 if (is_candidate (var, &type, unsuitable_types))
3344 add_structure (type);
3347 /* Now add structures that are types of function parameters and
3348 local variables. */
3349 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3351 enum availability avail =
3352 cgraph_function_body_availability (c_node);
3354 /* We need AVAIL_AVAILABLE for main function. */
3355 if (avail == AVAIL_LOCAL || avail == AVAIL_AVAILABLE)
3357 struct function *fn = DECL_STRUCT_FUNCTION (c_node->decl);
3359 for (var = DECL_ARGUMENTS (c_node->decl); var;
3360 var = TREE_CHAIN (var))
3361 if (is_candidate (var, &type, unsuitable_types))
3362 add_structure (type);
3364 /* Check function local variables. */
3365 for (var_list = fn->unexpanded_var_list; var_list;
3366 var_list = TREE_CHAIN (var_list))
3368 var = TREE_VALUE (var_list);
3370 if (is_candidate (var, &type, unsuitable_types))
3371 add_structure (type);
3377 /* This function returns true if the program contains
3378 a call to user defined allocation function, or other
3379 functions that can interfere with struct-reorg optimizations. */
3381 static bool
3382 program_redefines_malloc_p (void)
3384 struct cgraph_node *c_node;
3385 struct cgraph_node *c_node2;
3386 struct cgraph_edge *c_edge;
3387 tree fndecl;
3388 tree fndecl2;
3389 tree call_expr;
3391 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3393 fndecl = c_node->decl;
3395 for (c_edge = c_node->callees; c_edge; c_edge = c_edge->next_callee)
3397 call_expr = get_call_expr_in (c_edge->call_stmt);
3398 c_node2 = c_edge->callee;
3399 fndecl2 = c_node2->decl;
3400 if (call_expr)
3402 const char * fname = get_name (fndecl2);
3404 if ((call_expr_flags (call_expr) & ECF_MALLOC) &&
3405 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_MALLOC) &&
3406 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_CALLOC) &&
3407 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_ALLOCA))
3408 return true;
3410 /* Check that there is no __builtin_object_size,
3411 __builtin_offsetof, or realloc's in the program. */
3412 if (DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_OBJECT_SIZE
3413 || !strcmp (fname, "__builtin_offsetof")
3414 || !strcmp (fname, "realloc"))
3415 return true;
3420 return false;
3423 /* In this function we assume that an allocation statement
3425 var = (type_cast) malloc (size);
3427 is converted into the following set of statements:
3429 T.1 = size;
3430 T.2 = malloc (T.1);
3431 T.3 = (type_cast) T.2;
3432 var = T.3;
3434 In this function we collect into alloc_sites the allocation
3435 sites of variables of structure types that are present
3436 in structures vector. */
3438 static void
3439 collect_alloc_sites (void)
3441 struct cgraph_node *node;
3442 struct cgraph_edge *cs;
3444 for (node = cgraph_nodes; node; node = node->next)
3445 if (node->analyzed && node->decl)
3447 for (cs = node->callees; cs; cs = cs->next_callee)
3449 tree stmt = cs->call_stmt;
3451 if (stmt)
3453 tree call = get_call_expr_in (stmt);
3454 tree decl;
3456 if (call && (decl = get_callee_fndecl (call))
3457 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3459 unsigned i;
3461 if (is_alloc_of_struct (stmt, &i))
3463 /* We support only malloc now. */
3464 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MALLOC)
3466 d_str str;
3468 str = VEC_index (structure, structures, i);
3469 add_alloc_site (node->decl, stmt, str);
3471 else
3472 remove_structure (i);
3480 /* Print collected accesses. */
3482 static void
3483 dump_accesses (void)
3485 d_str str;
3486 unsigned i;
3488 if (!dump_file)
3489 return;
3491 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3492 dump_accs (str);
3495 /* This function checks whether the accesses of structures in condition
3496 expressions are of the kind we are capable to transform.
3497 If not, such structures are removed from the vector of structures. */
3499 static void
3500 check_cond_exprs (void)
3502 d_str str;
3503 unsigned i;
3505 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3506 if (str->accs)
3507 htab_traverse (str->accs, safe_cond_expr_check, &i);
3510 /* We exclude from non-field accesses of the structure
3511 all statements that will be treated as part of the structure
3512 allocation sites or field accesses. */
3514 static void
3515 exclude_alloc_and_field_accs (struct cgraph_node *node)
3517 d_str str;
3518 unsigned i;
3520 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3521 exclude_alloc_and_field_accs_1 (str, node);
3524 /* This function collects accesses of the fields of the structures
3525 that appear at function FN. */
3527 static void
3528 collect_accesses_in_func (struct function *fn)
3530 basic_block bb;
3532 if (! fn)
3533 return;
3535 /* Collect accesses for each basic blocks separately. */
3536 FOR_EACH_BB_FN (bb, fn)
3537 collect_accesses_in_bb (bb);
3540 /* This function summarizes counts of the fields into the structure count. */
3542 static void
3543 sum_counts (d_str str, gcov_type *hotest)
3545 int i;
3547 str->count = 0;
3548 for (i = 0; i < str->num_fields; i++)
3550 if (dump_file)
3552 fprintf (dump_file, "\nCounter of field \"");
3553 print_generic_expr (dump_file, str->fields[i].decl, 0);
3554 fprintf (dump_file, "\" is " HOST_WIDEST_INT_PRINT_DEC,
3555 str->fields[i].count);
3557 str->count += str->fields[i].count;
3560 if (dump_file)
3562 fprintf (dump_file, "\nCounter of struct \"");
3563 print_generic_expr (dump_file, str->decl, 0);
3564 fprintf (dump_file, "\" is " HOST_WIDEST_INT_PRINT_DEC, str->count);
3567 if (str->count > *hotest)
3568 *hotest = str->count;
3571 /* This function peels the field into separate structure if it's
3572 sufficiently hot, i.e. if its count provides at least 90% of
3573 the maximum field count in the structure. */
3575 static void
3576 peel_hot_fields (d_str str)
3578 gcov_type max_field_count;
3579 sbitmap fields_left = sbitmap_alloc (str->num_fields);
3580 int i;
3582 sbitmap_ones (fields_left);
3583 max_field_count =
3584 (gcov_type) (get_max_field_count (str)/100)*90;
3586 str->struct_clustering = NULL;
3588 for (i = 0; i < str->num_fields; i++)
3590 if (str->count >= max_field_count)
3592 RESET_BIT (fields_left, i);
3593 peel_field (i, str);
3597 i = sbitmap_first_set_bit (fields_left);
3598 if (i != -1)
3599 gen_cluster (fields_left, str);
3600 else
3601 sbitmap_free (fields_left);
3604 /* This function is a helper for do_reorg. It goes over
3605 functions in call graph and performs actual transformation
3606 on them. */
3608 static void
3609 do_reorg_1 (void)
3611 struct cgraph_node *node;
3613 /* Initialize the default bitmap obstack. */
3614 bitmap_obstack_initialize (NULL);
3616 for (node = cgraph_nodes; node; node = node->next)
3617 if (node->analyzed && node->decl && !node->next_clone)
3619 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3620 current_function_decl = node->decl;
3621 if (dump_file)
3622 fprintf (dump_file, "\nFunction to do reorg is %s: \n",
3623 (const char *) IDENTIFIER_POINTER (DECL_NAME (node->decl)));
3624 do_reorg_for_func (node);
3625 free_dominance_info (CDI_DOMINATORS);
3626 free_dominance_info (CDI_POST_DOMINATORS);
3627 current_function_decl = NULL;
3628 pop_cfun ();
3631 set_cfun (NULL);
3634 /* This function creates new global struct variables.
3635 For each original variable, the set of new variables
3636 is created with the new structure types corresponding
3637 to the structure type of original variable.
3638 Only VAR_DECL variables are treated by this function. */
3640 static void
3641 create_new_global_vars (void)
3643 struct varpool_node *current_varpool;
3644 unsigned HOST_WIDE_INT i;
3645 unsigned HOST_WIDE_INT varpool_size = 0;
3647 for (i = 0; i < 2; i++)
3649 if (i)
3650 new_global_vars = htab_create (varpool_size,
3651 new_var_hash, new_var_eq, NULL);
3652 FOR_EACH_STATIC_VARIABLE(current_varpool)
3654 tree var_decl = current_varpool->decl;
3656 if (!var_decl || TREE_CODE (var_decl) != VAR_DECL)
3657 continue;
3658 if (!i)
3659 varpool_size++;
3660 else
3661 create_new_var (var_decl, new_global_vars);
3665 if (new_global_vars)
3666 htab_traverse (new_global_vars, update_varpool_with_new_var, NULL);
3669 /* Dump all new types generated by this optimization. */
3671 static void
3672 dump_new_types (void)
3674 d_str str;
3675 tree type;
3676 unsigned i, j;
3678 if (!dump_file)
3679 return;
3681 fprintf (dump_file, "\nThe following are the new types generated by"
3682 " this optimization:\n");
3684 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3685 for (j = 0; VEC_iterate (tree, str->new_types, j, type); j++)
3686 dump_struct_type (type, 2, 0);
3689 /* This function creates new types to replace old structure types. */
3691 static void
3692 create_new_types (void)
3694 d_str str;
3695 unsigned i;
3696 int str_num = 0;
3698 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3699 create_new_type (str, &str_num);
3702 /* Free allocation sites hashtable. */
3704 static void
3705 free_alloc_sites (void)
3707 if (alloc_sites)
3708 htab_traverse (alloc_sites, free_falloc_sites, NULL);
3709 htab_delete (alloc_sites);
3710 alloc_sites = NULL;
3713 /* This function collects structures potential
3714 for peeling transformation, and inserts
3715 them into structures hashtable. */
3717 static void
3718 collect_structures (void)
3720 VEC (tree, heap) *unsuitable_types = VEC_alloc (tree, heap, 32);
3722 structures = VEC_alloc (structure, heap, 32);
3724 /* If program contains user defined mallocs, we give up. */
3725 if (program_redefines_malloc_p ())
3726 return;
3728 /* Build data structures hashtable of all data structures
3729 in the program. */
3730 build_data_structure (&unsuitable_types);
3732 /* This function analyzes whether the form of
3733 structure is such that we are capable to transform it.
3734 Nested structures are checked here. */
3735 analyze_struct_form (&unsuitable_types);
3737 /* This function excludes those structure types
3738 that escape compilation unit. */
3739 exclude_escaping_types (&unsuitable_types);
3741 /* We do not want to change data layout of the structures with bitfields. */
3742 exclude_types_with_bit_fields (&unsuitable_types);
3744 remove_unsuitable_types (unsuitable_types);
3745 VEC_free (tree, heap, unsuitable_types);
3747 if (!VEC_length (structure, structures))
3749 if (dump_file)
3750 fprintf (dump_file, "\nNo structures to transform. Exiting...");
3751 return;
3755 /* Collect structure allocation sites. In case of arrays
3756 we have nothing to do. */
3758 static void
3759 collect_allocation_sites (void)
3761 alloc_sites = htab_create (32, malloc_hash, malloc_eq, NULL);
3762 collect_alloc_sites ();
3765 /* This function collects data accesses for the
3766 structures to be transformed. For each structure
3767 field it updates the count field in field_entry. */
3769 static void
3770 collect_data_accesses (void)
3772 struct cgraph_node *c_node;
3774 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3776 enum availability avail = cgraph_function_body_availability (c_node);
3778 if (avail == AVAIL_LOCAL || avail == AVAIL_AVAILABLE)
3780 struct function *func = DECL_STRUCT_FUNCTION (c_node->decl);
3782 if (!c_node->next_clone)
3783 collect_accesses_in_func (func);
3784 exclude_alloc_and_field_accs (c_node);
3788 check_cond_exprs ();
3789 /* Print collected accesses. */
3790 dump_accesses ();
3793 /* We do not bother to transform cold structures.
3794 Coldness of the structure is defined relatively
3795 to the highest structure count among the structures
3796 to be transformed. It's triggered by the compiler parameter
3798 --param struct-reorg-cold-struct-ratio=<value>
3800 where <value> ranges from 0 to 100. Structures with count ratios
3801 that are less than this parameter are considered to be cold. */
3803 static void
3804 exclude_cold_structs (void)
3806 gcov_type hotest = 0;
3807 unsigned i;
3808 d_str str;
3810 /* We summarize counts of fields of a structure into the structure count. */
3811 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3812 sum_counts (str, &hotest);
3814 /* Remove cold structures from structures vector. */
3815 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3816 if (str->count * 100 < (hotest * STRUCT_REORG_COLD_STRUCT_RATIO))
3817 remove_structure (i);
3820 /* This function decomposes original structure into substructures,
3821 i.e.clusters. */
3823 static void
3824 peel_structs (void)
3826 d_str str;
3827 unsigned i;
3829 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3830 peel_hot_fields (str);
3833 /* Stage 3. */
3834 /* Do the actual transformation for each structure
3835 from the structures hashtable. */
3837 static void
3838 do_reorg (void)
3840 /* Check that there is a work to do. */
3841 if (!VEC_length (structure, structures))
3842 return;
3844 /* Generate new types. */
3845 create_new_types ();
3846 dump_new_types ();
3848 /* Create new global variables. */
3849 create_new_global_vars ();
3850 dump_new_vars (new_global_vars);
3852 /* Decompose structures for each function separately. */
3853 do_reorg_1 ();
3855 /* Free auxiliary data collected for global variables. */
3856 free_new_vars_htab (new_global_vars);
3859 /* Free all auxiliary data used by this optimization. */
3861 static void
3862 free_data_structs (void)
3864 free_structures ();
3865 free_alloc_sites ();
3868 /* Perform structure decomposition (peeling). */
3870 static void
3871 reorg_structs (void)
3874 /* Stage1. */
3875 /* Collect structure types. */
3876 collect_structures ();
3878 /* Collect structure allocation sites. */
3879 collect_allocation_sites ();
3881 /* Collect structure accesses. */
3882 collect_data_accesses ();
3884 /* We transform only hot structures. */
3885 exclude_cold_structs ();
3887 /* Stage2. */
3888 /* Decompose structures into substructures, i.e. clusters. */
3889 peel_structs ();
3891 /* Stage3. */
3892 /* Do the actual transformation for each structure
3893 from the structures hashtable. */
3894 do_reorg ();
3896 /* Free all auxiliary data used by this optimization. */
3897 free_data_structs ();
3900 /* Struct-reorg optimization entry point function. */
3902 static unsigned int
3903 reorg_structs_drive (void)
3905 reorg_structs ();
3906 return 0;
3909 /* Struct-reorg optimization gate function. */
3911 static bool
3912 struct_reorg_gate (void)
3914 return flag_ipa_struct_reorg && flag_whole_program
3915 && (optimize > 0);
3918 struct tree_opt_pass pass_ipa_struct_reorg =
3920 "ipa_struct_reorg", /* name */
3921 struct_reorg_gate, /* gate */
3922 reorg_structs_drive, /* execute */
3923 NULL, /* sub */
3924 NULL, /* next */
3925 0, /* static_pass_number */
3926 TV_INTEGRATION, /* tv_id */
3927 0, /* properties_required */
3928 0, /* properties_provided */
3929 0, /* properties_destroyed */
3930 TODO_verify_ssa, /* todo_flags_start */
3931 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
3932 0 /* letter */