ipa-struct-reorg.c (create_new_malloc): Use pointer type as malloc result type.
[gcc.git] / gcc / ipa-struct-reorg.c
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.)
6
7 This file is part of GCC.
8
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.
13
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.
18
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. */
23
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"
58
59 /* This optimization implements structure peeling.
60
61 For example, given a structure type:
62 typedef struct
63 {
64 int a;
65 float b;
66 int c;
67 }str_t;
68
69 it can be peeled into two structure types as follows:
70
71 typedef struct and typedef struct
72 { {
73 int a; float b;
74 int c; } str_t_1;
75 }str_t_0;
76
77 or can be fully peeled:
78
79 typedef struct
80 {
81 int a;
82 }str_t_0;
83
84 typedef struct
85 {
86 float b;
87 }str_t_1;
88
89 typedef struct
90 {
91 int c;
92 }str_t_2;
93
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:
97
98 str_t A[N];
99
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:
103
104 str_t_0 A_0[N];
105 str_t_1 A_1[N];
106
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.
109
110 This optimization also supports dynamically allocated arrays.
111 If array of structures was allocated by malloc function:
112
113 str_t * p = (str_t *) malloc (sizeof (str_t) * N)
114
115 the allocation site will be replaced to reflect new structure types:
116
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)
119
120 The field access through the pointer p[i].a will be changed by p_0[i].a.
121
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:
125
126 for (i = 0; i < N; i++)
127 {
128 ... = A[i].a;
129 }
130
131 the allocation of field a of str_t contiguously in memory will
132 increase the chances of fetching the field from cache.
133
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:
138
139 freq(f) > C * max_field_freq_in_struct
140
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.
144
145 If profiling information is provided, it is used to calculate the
146 frequency of field accesses. Otherwise, the structure is fully peeled.
147
148 IPA type-escape analysis is used to determine when it is safe
149 to peel a structure.
150
151 The optimization is activated by flag -fipa-struct-reorg. */
152
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;
163 };
164
165 typedef struct new_var_data *new_var;
166 typedef const struct new_var_data *const_new_var;
167
168 /* This structure represents allocation site of the structure. */
169 typedef struct alloc_site
170 {
171 tree stmt;
172 d_str str;
173 } alloc_site_t;
174
175 DEF_VEC_O (alloc_site_t);
176 DEF_VEC_ALLOC_O (alloc_site_t, heap);
177
178 /* Allocation sites that belong to the same function. */
179 struct func_alloc_sites
180 {
181 tree func;
182 /* Vector of allocation sites for function. */
183 VEC (alloc_site_t, heap) *allocs;
184 };
185
186 typedef struct func_alloc_sites *fallocs_t;
187 typedef const struct func_alloc_sites *const_fallocs_t;
188
189 /* All allocation sites in the program. */
190 htab_t alloc_sites;
191
192 /* New global variables. Generated once for whole program. */
193 htab_t new_global_vars;
194
195 /* New local variables. Generated per-function. */
196 htab_t new_local_vars;
197
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;
203
204 /* Forward declarations. */
205 static bool is_equal_types (tree, tree);
206
207 /* Strip structure TYPE from pointers and arrays. */
208
209 static inline tree
210 strip_type (tree type)
211 {
212 gcc_assert (TYPE_P (type));
213
214 while (POINTER_TYPE_P (type)
215 || TREE_CODE (type) == ARRAY_TYPE)
216 type = TREE_TYPE (type);
217
218 return type;
219 }
220
221 /* This function returns type of VAR. */
222
223 static inline tree
224 get_type_of_var (tree var)
225 {
226 if (!var)
227 return NULL;
228
229 if (TREE_CODE (var) == PARM_DECL)
230 return DECL_ARG_TYPE (var);
231 else
232 return TREE_TYPE (var);
233 }
234
235 /* Set of actions we do for each newly generated STMT. */
236
237 static inline void
238 finalize_stmt (tree stmt)
239 {
240 update_stmt (stmt);
241 mark_symbols_for_renaming (stmt);
242 }
243
244 /* This function finalizes STMT and appends it to the list STMTS. */
245
246 static inline void
247 finalize_stmt_and_append (tree *stmts, tree stmt)
248 {
249 append_to_statement_list (stmt, stmts);
250 finalize_stmt (stmt);
251 }
252
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. */
257
258 static tree
259 find_field_in_struct_1 (tree str_type, tree field)
260 {
261 tree str_field;
262
263 for (str_field = TYPE_FIELDS (str_type); str_field;
264 str_field = TREE_CHAIN (str_field))
265 {
266 const char * str_field_name;
267 const char * field_name;
268
269 str_field_name = IDENTIFIER_POINTER (DECL_NAME (str_field));
270 field_name = IDENTIFIER_POINTER (DECL_NAME (field));
271
272 gcc_assert (str_field_name);
273 gcc_assert (field_name);
274
275 if (!strcmp (str_field_name, field_name))
276 {
277 /* Check field types. */
278 if (is_equal_types (TREE_TYPE (str_field), TREE_TYPE (field)))
279 return str_field;
280 }
281 }
282
283 return NULL_TREE;
284 }
285
286 /* Given a field declaration FIELD_DECL, this function
287 returns corresponding field entry in structure STR. */
288
289 static struct field_entry *
290 find_field_in_struct (d_str str, tree field_decl)
291 {
292 int i;
293
294 tree field = find_field_in_struct_1 (str->decl, field_decl);
295
296 for (i = 0; i < str->num_fields; i++)
297 if (str->fields[i].decl == field)
298 return &(str->fields[i]);
299
300 return NULL;
301 }
302
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. */
306
307 static bool
308 is_result_of_mult (tree arg, tree *num, tree struct_size)
309 {
310 tree size_def_stmt = SSA_NAME_DEF_STMT (arg);
311
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; */
315
316 if (size_def_stmt && TREE_CODE (size_def_stmt) == GIMPLE_MODIFY_STMT)
317 {
318 tree lhs = GIMPLE_STMT_OPERAND (size_def_stmt, 0);
319 tree rhs = GIMPLE_STMT_OPERAND (size_def_stmt, 1);
320
321 /* We expect temporary here. */
322 if (!is_gimple_reg (lhs))
323 return false;
324
325 if (TREE_CODE (rhs) == MULT_EXPR)
326 {
327 tree arg0 = TREE_OPERAND (rhs, 0);
328 tree arg1 = TREE_OPERAND (rhs, 1);
329
330 if (operand_equal_p (arg0, struct_size, OEP_ONLY_CONST))
331 {
332 *num = arg1;
333 return true;
334 }
335
336 if (operand_equal_p (arg1, struct_size, OEP_ONLY_CONST))
337 {
338 *num = arg0;
339 return true;
340 }
341 }
342 }
343
344 *num = NULL_TREE;
345 return false;
346 }
347
348
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. */
354
355 static bool
356 decompose_indirect_ref_acc (tree str_decl, struct field_access_site *acc)
357 {
358 tree ref_var;
359 tree rhs, struct_size, op0, op1;
360 tree before_cast;
361
362 ref_var = TREE_OPERAND (acc->ref, 0);
363
364 if (TREE_CODE (ref_var) != SSA_NAME)
365 return false;
366
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;
371
372 rhs = GIMPLE_STMT_OPERAND (acc->ref_def_stmt, 1);
373
374 if (TREE_CODE (rhs) != PLUS_EXPR
375 && TREE_CODE (rhs)!= MINUS_EXPR
376 && TREE_CODE (rhs) != POINTER_PLUS_EXPR)
377 return false;
378
379 op0 = TREE_OPERAND (rhs, 0);
380 op1 = TREE_OPERAND (rhs, 1);
381
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;
386
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;
391
392 if (!before_cast)
393 return false;
394
395
396 if (SSA_NAME_IS_DEFAULT_DEF (before_cast))
397 return false;
398
399 struct_size = TYPE_SIZE_UNIT (str_decl);
400
401 if (!is_result_of_mult (before_cast, &acc->num, struct_size))
402 return false;
403
404 return true;
405 }
406
407
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. */
411
412 static bool
413 decompose_access (tree str_decl, struct field_access_site *acc)
414 {
415 gcc_assert (acc->ref);
416
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;
423
424 return false;
425 }
426
427 /* This function creates empty field_access_site node. */
428
429 static inline struct field_access_site *
430 make_field_acc_node (void)
431 {
432 int size = sizeof (struct field_access_site);
433
434 return (struct field_access_site *) xcalloc (1, size);
435 }
436
437 /* This function returns the structure field access, defined by STMT,
438 if it is aready in hashtable of function accesses F_ACCS. */
439
440 static struct field_access_site *
441 is_in_field_accs (tree stmt, htab_t f_accs)
442 {
443 return (struct field_access_site *)
444 htab_find_with_hash (f_accs, stmt, htab_hash_pointer (stmt));
445 }
446
447 /* This function adds an access ACC to the hashtable
448 F_ACCS of field accesses. */
449
450 static void
451 add_field_acc_to_acc_sites (struct field_access_site *acc,
452 htab_t f_accs)
453 {
454 void **slot;
455
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;
461 }
462
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. */
467
468 static void
469 add_access_to_acc_sites (tree stmt, tree var, htab_t accs)
470 {
471 struct access_site *acc;
472
473 acc = (struct access_site *)
474 htab_find_with_hash (accs, stmt, htab_hash_pointer (stmt));
475
476 if (!acc)
477 {
478 void **slot;
479
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;
486
487 }
488 VEC_safe_push (tree, heap, acc->vars, var);
489 }
490
491 /* This function adds NEW_DECL to function
492 referenced vars, and marks it for renaming. */
493
494 static void
495 finalize_var_creation (tree new_decl)
496 {
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);
501 }
502
503 /* This function finalizes VAR creation if it is a global VAR_DECL. */
504
505 static void
506 finalize_global_creation (tree var)
507 {
508 if (TREE_CODE (var) == VAR_DECL
509 && is_global_var (var))
510 finalize_var_creation (var);
511 }
512
513 /* This function inserts NEW_DECL to varpool. */
514
515 static inline void
516 insert_global_to_varpool (tree new_decl)
517 {
518 struct varpool_node *new_node;
519
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);
524 }
525
526 /* This function finalizes the creation of new variables,
527 defined by *SLOT->new_vars. */
528
529 static int
530 finalize_new_vars_creation (void **slot, void *data ATTRIBUTE_UNUSED)
531 {
532 new_var n_var = *(new_var *) slot;
533 unsigned i;
534 tree var;
535
536 for (i = 0; VEC_iterate (tree, n_var->new_vars, i, var); i++)
537 finalize_var_creation (var);
538 return 1;
539 }
540
541 /* This funciton updates statements in STMT_LIST with BB info. */
542
543 static void
544 add_bb_info (basic_block bb, tree stmt_list)
545 {
546 if (TREE_CODE (stmt_list) == STATEMENT_LIST)
547 {
548 tree_stmt_iterator tsi;
549 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
550 {
551 tree stmt = tsi_stmt (tsi);
552
553 set_bb_for_stmt (stmt, bb);
554 }
555 }
556 }
557
558 /* This function looks for the variable of NEW_TYPE type, stored in VAR.
559 It returns it, if found, and NULL_TREE otherwise. */
560
561 static tree
562 find_var_in_new_vars_vec (new_var var, tree new_type)
563 {
564 tree n_var;
565 unsigned i;
566
567 for (i = 0; VEC_iterate (tree, var->new_vars, i, n_var); i++)
568 {
569 tree type = strip_type(get_type_of_var (n_var));
570 gcc_assert (type);
571
572 if (type == new_type)
573 return n_var;
574 }
575
576 return NULL_TREE;
577 }
578
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. */
582
583 static new_var
584 is_in_new_vars_htab (tree decl, htab_t new_vars_htab)
585 {
586 return (new_var) htab_find_with_hash (new_vars_htab, decl,
587 htab_hash_pointer (decl));
588 }
589
590 /* Given original varaiable ORIG_VAR, this function returns
591 new variable corresponding to it of NEW_TYPE type. */
592
593 static tree
594 find_new_var_of_type (tree orig_var, tree new_type)
595 {
596 new_var var;
597 gcc_assert (orig_var && new_type);
598
599 if (TREE_CODE (orig_var) == SSA_NAME)
600 orig_var = SSA_NAME_VAR (orig_var);
601
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);
607 }
608
609 /* This function generates stmt:
610 res = NUM * sizeof(TYPE) and returns it.
611 res is filled into RES. */
612
613 static tree
614 gen_size (tree num, tree type, tree *res)
615 {
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;
619
620 *res = create_tmp_var (TREE_TYPE (num), NULL);
621
622 if (*res)
623 add_referenced_var (*res);
624
625 if (exact_log2 (struct_size_int) == -1)
626 {
627 tree size = build_int_cst (TREE_TYPE (num), struct_size_int);
628 new_stmt = build_gimple_modify_stmt (*res, build2 (MULT_EXPR,
629 TREE_TYPE (num),
630 num, size));
631 }
632 else
633 {
634 tree C = build_int_cst (TREE_TYPE (num), exact_log2 (struct_size_int));
635
636 new_stmt = build_gimple_modify_stmt (*res, build2 (LSHIFT_EXPR,
637 TREE_TYPE (num),
638 num, C));
639 }
640
641 finalize_stmt (new_stmt);
642 return new_stmt;
643 }
644
645 /* This function generates and returns a statement, that cast variable
646 BEFORE_CAST to NEW_TYPE. The cast result variable is stored
647 into RES_P. ORIG_CAST_STMT is the original cast statement. */
648
649 static tree
650 gen_cast_stmt (tree before_cast, tree new_type, tree orig_cast_stmt,
651 tree *res_p)
652 {
653 tree lhs, new_lhs, new_stmt;
654 gcc_assert (TREE_CODE (orig_cast_stmt) == GIMPLE_MODIFY_STMT);
655
656 lhs = GIMPLE_STMT_OPERAND (orig_cast_stmt, 0);
657 new_lhs = find_new_var_of_type (lhs, new_type);
658 gcc_assert (new_lhs);
659
660 new_stmt = build_gimple_modify_stmt (new_lhs,
661 build1 (NOP_EXPR,
662 TREE_TYPE (new_lhs),
663 before_cast));
664 finalize_stmt (new_stmt);
665 *res_p = new_lhs;
666 return new_stmt;
667 }
668
669 /* This function builds an edge between BB and E->dest and updates
670 phi nodes of E->dest. It returns newly created edge. */
671
672 static edge
673 make_edge_and_fix_phis_of_dest (basic_block bb, edge e)
674 {
675 edge new_e;
676 tree phi, arg;
677
678 new_e = make_edge (bb, e->dest, e->flags);
679
680 for (phi = phi_nodes (new_e->dest); phi; phi = PHI_CHAIN (phi))
681 {
682 arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
683 add_phi_arg (phi, arg, new_e);
684 }
685
686 return new_e;
687 }
688
689 /* This function inserts NEW_STMTS before STMT. */
690
691 static void
692 insert_before_stmt (tree stmt, tree new_stmts)
693 {
694 block_stmt_iterator bsi;
695
696 if (!stmt || !new_stmts)
697 return;
698
699 bsi = bsi_for_stmt (stmt);
700 bsi_insert_before (&bsi, new_stmts, BSI_SAME_STMT);
701 }
702
703 /* Insert NEW_STMTS after STMT. */
704
705 static void
706 insert_after_stmt (tree stmt, tree new_stmts)
707 {
708 block_stmt_iterator bsi;
709
710 if (!stmt || !new_stmts)
711 return;
712
713 bsi = bsi_for_stmt (stmt);
714 bsi_insert_after (&bsi, new_stmts, BSI_SAME_STMT);
715 }
716
717 /* This function returns vector of allocation sites
718 that appear in function FN_DECL. */
719
720 static fallocs_t
721 get_fallocs (tree fn_decl)
722 {
723 return (fallocs_t) htab_find_with_hash (alloc_sites, fn_decl,
724 htab_hash_pointer (fn_decl));
725 }
726
727 /* If ALLOC_STMT is D.2225_7 = <alloc_func> (D.2224_6);
728 and it is a part of allocation of a structure,
729 then it is usually followed by a cast stmt
730 p_8 = (struct str_t *) D.2225_7;
731 which is returned by this function. */
732
733 static tree
734 get_final_alloc_stmt (tree alloc_stmt)
735 {
736 tree final_stmt;
737 use_operand_p use_p;
738 tree alloc_res;
739
740 if (!alloc_stmt)
741 return NULL;
742
743 if (TREE_CODE (alloc_stmt) != GIMPLE_MODIFY_STMT)
744 return NULL;
745
746 alloc_res = GIMPLE_STMT_OPERAND (alloc_stmt, 0);
747
748 if (TREE_CODE (alloc_res) != SSA_NAME)
749 return NULL;
750
751 if (!single_imm_use (alloc_res, &use_p, &final_stmt))
752 return NULL;
753 else
754 return final_stmt;
755 }
756
757 /* This function returns true if STMT is one of allocation
758 sites of function FN_DECL. It returns false otherwise. */
759
760 static bool
761 is_part_of_malloc (tree stmt, tree fn_decl)
762 {
763 fallocs_t fallocs = get_fallocs (fn_decl);
764
765 if (fallocs)
766 {
767 alloc_site_t *call;
768 unsigned i;
769
770 for (i = 0;
771 VEC_iterate (alloc_site_t, fallocs->allocs, i, call); i++)
772 if (call->stmt == stmt
773 || get_final_alloc_stmt (call->stmt) == stmt)
774 return true;
775 }
776 return false;
777 }
778
779 /* Auxiliary structure for a lookup over field accesses. */
780 struct find_stmt_data
781 {
782 bool found;
783 tree stmt;
784 };
785
786 /* This function looks for DATA->stmt among
787 the statements involved in the field access,
788 defined by SLOT. It stops when it's found. */
789
790 static int
791 find_in_field_accs (void **slot, void *data)
792 {
793 struct field_access_site *f_acc =
794 *(struct field_access_site **) slot;
795 tree stmt = ((struct find_stmt_data *)data)->stmt;
796
797 if (f_acc->stmt == stmt
798 || f_acc->ref_def_stmt == stmt
799 || f_acc->cast_stmt == stmt)
800 {
801 ((struct find_stmt_data *)data)->found = true;
802 return 0;
803 }
804 else
805 return 1;
806 }
807
808 /* This function checks whether STMT is part of field
809 accesses of structure STR. It returns true, if found,
810 and false otherwise. */
811
812 static bool
813 is_part_of_field_access (tree stmt, d_str str)
814 {
815 int i;
816
817 for (i = 0; i < str->num_fields; i++)
818 {
819 struct find_stmt_data data;
820 data.found = false;
821 data.stmt = stmt;
822
823 if (str->fields[i].acc_sites)
824 htab_traverse (str->fields[i].acc_sites, find_in_field_accs, &data);
825
826 if (data.found)
827 return true;
828 }
829
830 return false;
831 }
832
833 /* Auxiliary data for exclude_from_accs function. */
834
835 struct exclude_data
836 {
837 tree fn_decl;
838 d_str str;
839 };
840
841 /* This function returns component_ref with the BASE and
842 field named FIELD_ID from structure TYPE. */
843
844 static inline tree
845 build_comp_ref (tree base, tree field_id, tree type)
846 {
847 tree field;
848 bool found = false;
849
850
851 /* Find field of structure type with the same name as field_id. */
852 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
853 {
854 if (DECL_NAME (field) == field_id)
855 {
856 found = true;
857 break;
858 }
859 }
860
861 gcc_assert (found);
862
863 return build3 (COMPONENT_REF, TREE_TYPE (field), base, field, NULL_TREE);
864 }
865
866
867 /* This struct represent data used for walk_tree
868 called from function find_pos_in_stmt.
869 - ref is a tree to be found,
870 - and pos is a pointer that points to ref in stmt. */
871 struct ref_pos
872 {
873 tree *pos;
874 tree ref;
875 };
876
877
878 /* This is a callback function for walk_tree, called from
879 collect_accesses_in_bb function. DATA is a pointer to ref_pos structure.
880 When *TP is equal to DATA->ref, the walk_tree stops,
881 and found position, equal to TP, is assigned to DATA->pos. */
882
883 static tree
884 find_pos_in_stmt_1 (tree *tp, int *walk_subtrees, void * data)
885 {
886 struct ref_pos * r_pos = (struct ref_pos *) data;
887 tree ref = r_pos->ref;
888 tree t = *tp;
889
890 if (t == ref)
891 {
892 r_pos->pos = tp;
893 return t;
894 }
895
896 switch (TREE_CODE (t))
897 {
898 case GIMPLE_MODIFY_STMT:
899 {
900 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
901 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
902 *walk_subtrees = 1;
903 walk_tree (&lhs, find_pos_in_stmt_1, data, NULL);
904 walk_tree (&rhs, find_pos_in_stmt_1, data, NULL);
905 *walk_subtrees = 0;
906 }
907 break;
908
909 default:
910 *walk_subtrees = 1;
911 }
912 return NULL_TREE;
913 }
914
915
916 /* This function looks for the pointer of REF in STMT,
917 It returns it, if found, and NULL otherwise. */
918
919 static tree *
920 find_pos_in_stmt (tree stmt, tree ref)
921 {
922 struct ref_pos r_pos;
923
924 r_pos.ref = ref;
925 r_pos.pos = NULL;
926 walk_tree (&stmt, find_pos_in_stmt_1, &r_pos, NULL);
927
928 return r_pos.pos;
929 }
930
931 /* This structure is used to represent array
932 or pointer-to wrappers of structure type.
933 For example, if type1 is structure type,
934 then for type1 ** we generate two type_wrapper
935 structures with wrap = 0 each one.
936 It's used to unwind the original type up to
937 structure type, replace it with the new structure type
938 and wrap it back in the opposite order. */
939
940 typedef struct type_wrapper
941 {
942 /* 0 stand for pointer wrapper, and 1 for array wrapper. */
943 bool wrap;
944
945 /* Relevant for arrays as domain or index. */
946 tree domain;
947 }type_wrapper_t;
948
949 DEF_VEC_O (type_wrapper_t);
950 DEF_VEC_ALLOC_O (type_wrapper_t, heap);
951
952 /* This function replace field access ACC by the new
953 field access of structure type NEW_TYPE. */
954
955 static void
956 replace_field_acc (struct field_access_site *acc, tree new_type)
957 {
958 tree ref_var = acc->ref;
959 tree new_ref;
960 tree lhs, rhs;
961 tree *pos;
962 tree new_acc;
963 tree field_id = DECL_NAME (acc->field_decl);
964 VEC (type_wrapper_t, heap) *wrapper = VEC_alloc (type_wrapper_t, heap, 10);
965 type_wrapper_t *wr_p = NULL;
966
967 while (TREE_CODE (ref_var) == INDIRECT_REF
968 || TREE_CODE (ref_var) == ARRAY_REF)
969 {
970 type_wrapper_t wr;
971
972 if ( TREE_CODE (ref_var) == INDIRECT_REF)
973 {
974 wr.wrap = 0;
975 wr.domain = 0;
976 }
977 else
978 {
979 wr.wrap = 1;
980 wr.domain = TREE_OPERAND (ref_var, 1);
981 }
982
983 VEC_safe_push (type_wrapper_t, heap, wrapper, &wr);
984 ref_var = TREE_OPERAND (ref_var, 0);
985 }
986
987 new_ref = find_new_var_of_type (ref_var, new_type);
988 finalize_global_creation (new_ref);
989
990 while (VEC_length (type_wrapper_t, wrapper) != 0)
991 {
992 tree type = TREE_TYPE (TREE_TYPE (new_ref));
993
994 wr_p = VEC_last (type_wrapper_t, wrapper);
995 if (wr_p->wrap) /* Array. */
996 new_ref = build4 (ARRAY_REF, type, new_ref,
997 wr_p->domain, NULL_TREE, NULL_TREE);
998 else /* Pointer. */
999 new_ref = build1 (INDIRECT_REF, type, new_ref);
1000 VEC_pop (type_wrapper_t, wrapper);
1001 }
1002
1003 new_acc = build_comp_ref (new_ref, field_id, new_type);
1004 VEC_free (type_wrapper_t, heap, wrapper);
1005
1006 if (TREE_CODE (acc->stmt) == GIMPLE_MODIFY_STMT)
1007 {
1008 lhs = GIMPLE_STMT_OPERAND (acc->stmt, 0);
1009 rhs = GIMPLE_STMT_OPERAND (acc->stmt, 1);
1010
1011
1012 if (lhs == acc->comp_ref)
1013 GIMPLE_STMT_OPERAND (acc->stmt, 0) = new_acc;
1014 else if (rhs == acc->comp_ref)
1015 GIMPLE_STMT_OPERAND (acc->stmt, 1) = new_acc;
1016 else
1017 {
1018 pos = find_pos_in_stmt (acc->stmt, acc->comp_ref);
1019 gcc_assert (pos);
1020 *pos = new_acc;
1021 }
1022 }
1023 else
1024 {
1025 pos = find_pos_in_stmt (acc->stmt, acc->comp_ref);
1026 gcc_assert (pos);
1027 *pos = new_acc;
1028 }
1029
1030 finalize_stmt (acc->stmt);
1031 }
1032
1033 /* This function replace field access ACC by a new field access
1034 of structure type NEW_TYPE. */
1035
1036 static void
1037 replace_field_access_stmt (struct field_access_site *acc, tree new_type)
1038 {
1039
1040 if (TREE_CODE (acc->ref) == INDIRECT_REF
1041 ||TREE_CODE (acc->ref) == ARRAY_REF
1042 ||TREE_CODE (acc->ref) == VAR_DECL)
1043 replace_field_acc (acc, new_type);
1044 else
1045 gcc_unreachable ();
1046 }
1047
1048 /* This function looks for d_str, represented by TYPE, in the structures
1049 vector. If found, it returns an index of found structure. Otherwise
1050 it returns a length of the structures vector. */
1051
1052 static unsigned
1053 find_structure (tree type)
1054 {
1055 d_str str;
1056 unsigned i;
1057
1058 type = TYPE_MAIN_VARIANT (type);
1059
1060 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
1061 if (is_equal_types (str->decl, type))
1062 return i;
1063
1064 return VEC_length (structure, structures);
1065 }
1066
1067 /* In this function we create new statements that have the same
1068 form as ORIG_STMT, but of type NEW_TYPE. The statements
1069 treated by this function are simple assignments,
1070 like assignments: p.8_7 = p; or statements with rhs of
1071 tree codes PLUS_EXPR and MINUS_EXPR. */
1072
1073 static tree
1074 create_base_plus_offset (tree orig_stmt, tree new_type,
1075 tree offset)
1076 {
1077 tree lhs, rhs;
1078 tree new_lhs, new_rhs;
1079 tree new_stmt;
1080
1081 gcc_assert (TREE_CODE (orig_stmt) == GIMPLE_MODIFY_STMT);
1082
1083 lhs = GIMPLE_STMT_OPERAND (orig_stmt, 0);
1084 rhs = GIMPLE_STMT_OPERAND (orig_stmt, 1);
1085
1086 gcc_assert (TREE_CODE (lhs) == VAR_DECL
1087 || TREE_CODE (lhs) == SSA_NAME);
1088
1089 new_lhs = find_new_var_of_type (lhs, new_type);
1090 gcc_assert (new_lhs);
1091 finalize_var_creation (new_lhs);
1092
1093 switch (TREE_CODE (rhs))
1094 {
1095 case PLUS_EXPR:
1096 case MINUS_EXPR:
1097 case POINTER_PLUS_EXPR:
1098 {
1099 tree op0 = TREE_OPERAND (rhs, 0);
1100 tree op1 = TREE_OPERAND (rhs, 1);
1101 tree new_op0 = NULL_TREE, new_op1 = NULL_TREE;
1102 unsigned str0, str1;
1103 unsigned length = VEC_length (structure, structures);
1104
1105
1106 str0 = find_structure (strip_type (get_type_of_var (op0)));
1107 str1 = find_structure (strip_type (get_type_of_var (op1)));
1108 gcc_assert ((str0 != length) || (str1 != length));
1109
1110 if (str0 != length)
1111 new_op0 = find_new_var_of_type (op0, new_type);
1112 if (str1 != length)
1113 new_op1 = find_new_var_of_type (op1, new_type);
1114
1115 if (!new_op0)
1116 new_op0 = offset;
1117 if (!new_op1)
1118 new_op1 = offset;
1119
1120 new_rhs = build2 (TREE_CODE (rhs), TREE_TYPE (new_op0),
1121 new_op0, new_op1);
1122 }
1123 break;
1124
1125 default:
1126 gcc_unreachable();
1127 }
1128
1129 new_stmt = build_gimple_modify_stmt (new_lhs, new_rhs);
1130 finalize_stmt (new_stmt);
1131
1132 return new_stmt;
1133 }
1134
1135 /* Given a field access F_ACC of the FIELD, this function
1136 replaces it by the new field access. */
1137
1138 static void
1139 create_new_field_access (struct field_access_site *f_acc,
1140 struct field_entry field)
1141 {
1142 tree new_type = field.field_mapping;
1143 tree new_stmt;
1144 tree size_res;
1145 tree mult_stmt, cast_stmt;
1146 tree cast_res = NULL;
1147
1148 if (f_acc->num)
1149 {
1150 mult_stmt = gen_size (f_acc->num, new_type, &size_res);
1151 insert_before_stmt (f_acc->ref_def_stmt, mult_stmt);
1152 }
1153
1154 if (f_acc->cast_stmt)
1155 {
1156 cast_stmt = gen_cast_stmt (size_res, new_type,
1157 f_acc->cast_stmt, &cast_res);
1158 insert_after_stmt (f_acc->cast_stmt, cast_stmt);
1159 }
1160
1161 if (f_acc->ref_def_stmt)
1162 {
1163 tree offset;
1164 if (cast_res)
1165 offset = cast_res;
1166 else
1167 offset = size_res;
1168
1169 new_stmt = create_base_plus_offset (f_acc->ref_def_stmt,
1170 new_type, offset);
1171 insert_after_stmt (f_acc->ref_def_stmt, new_stmt);
1172 }
1173
1174 /* In stmt D.2163_19 = D.2162_18->b; we replace variable
1175 D.2162_18 by an appropriate variable of new_type type. */
1176 replace_field_access_stmt (f_acc, new_type);
1177 }
1178
1179 /* This function creates a new condition statement
1180 corresponding to the original COND_STMT, adds new basic block
1181 and redirects condition edges. NEW_VAR is a new condition
1182 variable located in the condition statement at the position POS. */
1183
1184 static void
1185 create_new_stmts_for_cond_expr_1 (tree new_var, tree cond_stmt, bool pos)
1186 {
1187 tree new_cond;
1188 tree new_stmt;
1189 edge true_e = NULL, false_e = NULL;
1190 basic_block new_bb;
1191 tree stmt_list;
1192
1193 extract_true_false_edges_from_block (bb_for_stmt (cond_stmt),
1194 &true_e, &false_e);
1195
1196 new_cond = unshare_expr (COND_EXPR_COND (cond_stmt));
1197
1198 TREE_OPERAND (new_cond, pos) = new_var;
1199
1200 new_stmt = build3 (COND_EXPR, TREE_TYPE (cond_stmt),
1201 new_cond, NULL_TREE, NULL_TREE);
1202
1203 finalize_stmt (new_stmt);
1204
1205 /* Create new basic block after bb. */
1206 new_bb = create_empty_bb (bb_for_stmt (cond_stmt));
1207
1208 /* Add new condition stmt to the new_bb. */
1209 stmt_list = bb_stmt_list (new_bb);
1210 append_to_statement_list (new_stmt, &stmt_list);
1211 add_bb_info (new_bb, stmt_list);
1212
1213
1214 /* Create false and true edges from new_bb. */
1215 make_edge_and_fix_phis_of_dest (new_bb, true_e);
1216 make_edge_and_fix_phis_of_dest (new_bb, false_e);
1217
1218 /* Redirect one of original edges to point to new_bb. */
1219 if (TREE_CODE (cond_stmt) == NE_EXPR)
1220 redirect_edge_succ (true_e, new_bb);
1221 else
1222 redirect_edge_succ (false_e, new_bb);
1223 }
1224
1225 /* This function creates new condition statements corresponding
1226 to original condition STMT, one for each new type, and
1227 recursively redirect edges to newly generated basic blocks. */
1228
1229 static void
1230 create_new_stmts_for_cond_expr (tree stmt)
1231 {
1232 tree cond = COND_EXPR_COND (stmt);
1233 tree arg0, arg1, arg;
1234 unsigned str0, str1;
1235 bool s0, s1;
1236 d_str str;
1237 tree type;
1238 bool pos;
1239 int i;
1240 unsigned length = VEC_length (structure, structures);
1241
1242 gcc_assert (TREE_CODE (cond) == EQ_EXPR
1243 || TREE_CODE (cond) == NE_EXPR);
1244
1245 arg0 = TREE_OPERAND (cond, 0);
1246 arg1 = TREE_OPERAND (cond, 1);
1247
1248 str0 = find_structure (strip_type (get_type_of_var (arg0)));
1249 str1 = find_structure (strip_type (get_type_of_var (arg1)));
1250
1251 s0 = (str0 != length) ? true : false;
1252 s1 = (str1 != length) ? true : false;
1253
1254 gcc_assert ((!s0 && s1) || (!s1 && s0));
1255
1256 str = s0 ? VEC_index (structure, structures, str0):
1257 VEC_index (structure, structures, str1);
1258 arg = s0 ? arg0 : arg1;
1259 pos = s0 ? 0 : 1;
1260
1261 for (i = 0; VEC_iterate (tree, str->new_types, i, type); i++)
1262 {
1263 tree new_arg;
1264
1265 new_arg = find_new_var_of_type (arg, type);
1266 create_new_stmts_for_cond_expr_1 (new_arg, stmt, pos);
1267 }
1268 }
1269
1270 /* Create a new general access to replace original access ACC
1271 for structure type NEW_TYPE. */
1272
1273 static tree
1274 create_general_new_stmt (struct access_site *acc, tree new_type)
1275 {
1276 tree old_stmt = acc->stmt;
1277 tree var;
1278 tree new_stmt = unshare_expr (old_stmt);
1279 unsigned i;
1280
1281
1282 for (i = 0; VEC_iterate (tree, acc->vars, i, var); i++)
1283 {
1284 tree *pos;
1285 tree new_var = find_new_var_of_type (var, new_type);
1286 tree lhs, rhs;
1287
1288 gcc_assert (new_var);
1289 finalize_var_creation (new_var);
1290
1291 if (TREE_CODE (new_stmt) == GIMPLE_MODIFY_STMT)
1292 {
1293
1294 lhs = GIMPLE_STMT_OPERAND (new_stmt, 0);
1295 rhs = GIMPLE_STMT_OPERAND (new_stmt, 1);
1296
1297 if (TREE_CODE (lhs) == SSA_NAME)
1298 lhs = SSA_NAME_VAR (lhs);
1299 if (TREE_CODE (rhs) == SSA_NAME)
1300 rhs = SSA_NAME_VAR (rhs);
1301
1302 /* It can happen that rhs is a constructor.
1303 Then we have to replace it to be of new_type. */
1304 if (TREE_CODE (rhs) == CONSTRUCTOR)
1305 {
1306 /* Dealing only with empty constructors right now. */
1307 gcc_assert (VEC_empty (constructor_elt,
1308 CONSTRUCTOR_ELTS (rhs)));
1309 rhs = build_constructor (new_type, 0);
1310 GIMPLE_STMT_OPERAND (new_stmt, 1) = rhs;
1311 }
1312
1313 if (lhs == var)
1314 GIMPLE_STMT_OPERAND (new_stmt, 0) = new_var;
1315 else if (rhs == var)
1316 GIMPLE_STMT_OPERAND (new_stmt, 1) = new_var;
1317 else
1318 {
1319 pos = find_pos_in_stmt (new_stmt, var);
1320 gcc_assert (pos);
1321 *pos = new_var;
1322 }
1323 }
1324 else
1325 {
1326 pos = find_pos_in_stmt (new_stmt, var);
1327 gcc_assert (pos);
1328 *pos = new_var;
1329 }
1330 }
1331
1332 finalize_stmt (new_stmt);
1333 return new_stmt;
1334 }
1335
1336 /* For each new type in STR this function creates new general accesses
1337 corresponding to the original access ACC. */
1338
1339 static void
1340 create_new_stmts_for_general_acc (struct access_site *acc, d_str str)
1341 {
1342 tree type;
1343 tree stmt = acc->stmt;
1344 unsigned i;
1345
1346 for (i = 0; VEC_iterate (tree, str->new_types, i, type); i++)
1347 {
1348 tree new_stmt;
1349
1350 new_stmt = create_general_new_stmt (acc, type);
1351 insert_after_stmt (stmt, new_stmt);
1352 }
1353 }
1354
1355 /* This function creates a new general access of structure STR
1356 to replace the access ACC. */
1357
1358 static void
1359 create_new_general_access (struct access_site *acc, d_str str)
1360 {
1361 tree stmt = acc->stmt;
1362 switch (TREE_CODE (stmt))
1363 {
1364 case COND_EXPR:
1365 create_new_stmts_for_cond_expr (stmt);
1366 break;
1367
1368 default:
1369 create_new_stmts_for_general_acc (acc, str);
1370 }
1371 }
1372
1373 /* Auxiliary data for creation of accesses. */
1374 struct create_acc_data
1375 {
1376 basic_block bb;
1377 d_str str;
1378 int field_index;
1379 };
1380
1381 /* This function creates a new general access, defined by SLOT.
1382 DATA is a pointer to create_acc_data structure. */
1383
1384 static int
1385 create_new_acc (void **slot, void *data)
1386 {
1387 struct access_site *acc = *(struct access_site **) slot;
1388 basic_block bb = ((struct create_acc_data *)data)->bb;
1389 d_str str = ((struct create_acc_data *)data)->str;
1390
1391 if (bb_for_stmt (acc->stmt) == bb)
1392 create_new_general_access (acc, str);
1393 return 1;
1394 }
1395
1396 /* This function creates a new field access, defined by SLOT.
1397 DATA is a pointer to create_acc_data structure. */
1398
1399 static int
1400 create_new_field_acc (void **slot, void *data)
1401 {
1402 struct field_access_site *f_acc = *(struct field_access_site **) slot;
1403 basic_block bb = ((struct create_acc_data *)data)->bb;
1404 d_str str = ((struct create_acc_data *)data)->str;
1405 int i = ((struct create_acc_data *)data)->field_index;
1406
1407 if (bb_for_stmt (f_acc->stmt) == bb)
1408 create_new_field_access (f_acc, str->fields[i]);
1409 return 1;
1410 }
1411
1412 /* This function creates new accesses for the structure
1413 type STR in basic block BB. */
1414
1415 static void
1416 create_new_accs_for_struct (d_str str, basic_block bb)
1417 {
1418 int i;
1419 struct create_acc_data dt;
1420
1421 dt.str = str;
1422 dt.bb = bb;
1423 dt.field_index = -1;
1424
1425 for (i = 0; i < str->num_fields; i++)
1426 {
1427 dt.field_index = i;
1428
1429 if (str->fields[i].acc_sites)
1430 htab_traverse (str->fields[i].acc_sites,
1431 create_new_field_acc, &dt);
1432 }
1433 if (str->accs)
1434 htab_traverse (str->accs, create_new_acc, &dt);
1435 }
1436
1437 /* This function inserts new variables from new_var,
1438 defined by SLOT, into varpool. */
1439
1440 static int
1441 update_varpool_with_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
1442 {
1443 new_var n_var = *(new_var *) slot;
1444 tree var;
1445 unsigned i;
1446
1447 for (i = 0; VEC_iterate (tree, n_var->new_vars, i, var); i++)
1448 insert_global_to_varpool (var);
1449 return 1;
1450 }
1451
1452 /* This function prints a field access site, defined by SLOT. */
1453
1454 static int
1455 dump_field_acc (void **slot, void *data ATTRIBUTE_UNUSED)
1456 {
1457 struct field_access_site *f_acc =
1458 *(struct field_access_site **) slot;
1459
1460 fprintf(dump_file, "\n");
1461 if (f_acc->stmt)
1462 print_generic_stmt (dump_file, f_acc->stmt, 0);
1463 if (f_acc->ref_def_stmt)
1464 print_generic_stmt (dump_file, f_acc->ref_def_stmt, 0);
1465 if (f_acc->cast_stmt)
1466 print_generic_stmt (dump_file, f_acc->cast_stmt, 0);
1467 return 1;
1468 }
1469
1470 /* Print field accesses from hashtable F_ACCS. */
1471
1472 static void
1473 dump_field_acc_sites (htab_t f_accs)
1474 {
1475 if (!dump_file)
1476 return;
1477
1478 if (f_accs)
1479 htab_traverse (f_accs, dump_field_acc, NULL);
1480 }
1481
1482 /* Hash value for fallocs_t. */
1483
1484 static hashval_t
1485 malloc_hash (const void *x)
1486 {
1487 return htab_hash_pointer (((const_fallocs_t)x)->func);
1488 }
1489
1490 /* This function returns nonzero if function of func_alloc_sites' X
1491 is equal to Y. */
1492
1493 static int
1494 malloc_eq (const void *x, const void *y)
1495 {
1496 return ((const_fallocs_t)x)->func == (const_tree)y;
1497 }
1498
1499 /* This function is a callback for traversal over a structure accesses.
1500 It frees an access represented by SLOT. */
1501
1502 static int
1503 free_accs (void **slot, void *data ATTRIBUTE_UNUSED)
1504 {
1505 struct access_site * acc = *(struct access_site **) slot;
1506
1507 VEC_free (tree, heap, acc->vars);
1508 free (acc);
1509 return 1;
1510 }
1511
1512 /* This is a callback function for traversal over field accesses.
1513 It frees a field access represented by SLOT. */
1514
1515 static int
1516 free_field_accs (void **slot, void *data ATTRIBUTE_UNUSED)
1517 {
1518 struct field_access_site *f_acc = *(struct field_access_site **) slot;
1519
1520 free (f_acc);
1521 return 1;
1522 }
1523
1524 /* This function inserts TYPE into vector of UNSUITABLE_TYPES,
1525 if it is not there yet. */
1526
1527 static void
1528 add_unsuitable_type (VEC (tree, heap) **unsuitable_types, tree type)
1529 {
1530 unsigned i;
1531 tree t;
1532
1533 if (!type)
1534 return;
1535
1536 type = TYPE_MAIN_VARIANT (type);
1537
1538 for (i = 0; VEC_iterate (tree, *unsuitable_types, i, t); i++)
1539 if (is_equal_types (t, type))
1540 break;
1541
1542 if (i == VEC_length (tree, *unsuitable_types))
1543 VEC_safe_push (tree, heap, *unsuitable_types, type);
1544 }
1545
1546 /* Given a type TYPE, this function returns the name of the type. */
1547
1548 static const char *
1549 get_type_name (tree type)
1550 {
1551 if (! TYPE_NAME (type))
1552 return NULL;
1553
1554 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1555 return IDENTIFIER_POINTER (TYPE_NAME (type));
1556 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1557 && DECL_NAME (TYPE_NAME (type)))
1558 return IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1559 else
1560 return NULL;
1561 }
1562
1563 /* This function is a temporary hack to overcome the types problem.
1564 When several compilation units are compiled together
1565 with -combine, the TYPE_MAIN_VARIANT of the same type
1566 can appear differently in different compilation units.
1567 Therefore this function first compares type names.
1568 If there are no names, structure bodies are recursively
1569 compared. */
1570
1571 static bool
1572 is_equal_types (tree type1, tree type2)
1573 {
1574 const char * name1,* name2;
1575
1576 if ((!type1 && type2)
1577 ||(!type2 && type1))
1578 return false;
1579
1580 if (!type1 && !type2)
1581 return true;
1582
1583 if (TREE_CODE (type1) != TREE_CODE (type2))
1584 return false;
1585
1586 if (type1 == type2)
1587 return true;
1588
1589 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
1590 return true;
1591
1592 name1 = get_type_name (type1);
1593 name2 = get_type_name (type2);
1594
1595 if (name1 && name2 && !strcmp (name1, name2))
1596 return true;
1597
1598 if (name1 && name2 && strcmp (name1, name2))
1599 return false;
1600
1601 switch (TREE_CODE (type1))
1602 {
1603 case POINTER_TYPE:
1604 case REFERENCE_TYPE:
1605 {
1606 return is_equal_types (TREE_TYPE (type1), TREE_TYPE (type2));
1607 }
1608 break;
1609
1610 case RECORD_TYPE:
1611 case UNION_TYPE:
1612 case QUAL_UNION_TYPE:
1613 case ENUMERAL_TYPE:
1614 {
1615 tree field1;
1616 /* Compare fields of struture. */
1617 for (field1 = TYPE_FIELDS (type1); field1;
1618 field1 = TREE_CHAIN (field1))
1619 {
1620 tree field2 = find_field_in_struct_1 (type2, field1);
1621 if (!field2)
1622 return false;
1623 }
1624 return true;
1625 }
1626 break;
1627
1628 case INTEGER_TYPE:
1629 {
1630 if (TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1631 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2))
1632 return true;
1633 }
1634 break;
1635
1636 case ARRAY_TYPE:
1637 {
1638 tree d1, d2;
1639 tree max1, min1, max2, min2;
1640
1641 if (!is_equal_types (TREE_TYPE (type1), TREE_TYPE (type2)))
1642 return false;
1643
1644 d1 = TYPE_DOMAIN (type1);
1645 d2 = TYPE_DOMAIN (type2);
1646
1647 if (!d1 || !d2)
1648 return false;
1649
1650 max1 = TYPE_MAX_VALUE (d1);
1651 max2 = TYPE_MAX_VALUE (d2);
1652 min1 = TYPE_MIN_VALUE (d1);
1653 min2 = TYPE_MIN_VALUE (d2);
1654
1655 if (max1 && max2 && min1 && min2
1656 && TREE_CODE (max1) == TREE_CODE (max2)
1657 && TREE_CODE (max1) == INTEGER_CST
1658 && TREE_CODE (min1) == TREE_CODE (min2)
1659 && TREE_CODE (min1) == INTEGER_CST
1660 && tree_int_cst_equal (max1, max2)
1661 && tree_int_cst_equal (min1, min2))
1662 return true;
1663 }
1664 break;
1665
1666 default:
1667 gcc_unreachable();
1668 }
1669
1670 return false;
1671 }
1672
1673 /* This function free non-field accesses from hashtable ACCS. */
1674
1675 static void
1676 free_accesses (htab_t accs)
1677 {
1678 if (accs)
1679 htab_traverse (accs, free_accs, NULL);
1680 htab_delete (accs);
1681 }
1682
1683 /* This function free field accesses hashtable F_ACCS. */
1684
1685 static void
1686 free_field_accesses (htab_t f_accs)
1687 {
1688 if (f_accs)
1689 htab_traverse (f_accs, free_field_accs, NULL);
1690 htab_delete (f_accs);
1691 }
1692
1693 /* Update call graph with new edge generated by new MALLOC_STMT.
1694 The edge origin is CONTEXT function. */
1695
1696 static void
1697 update_cgraph_with_malloc_call (tree malloc_stmt, tree context)
1698 {
1699 tree call_expr;
1700 struct cgraph_node *src, *dest;
1701 tree malloc_fn_decl;
1702
1703 if (!malloc_stmt)
1704 return;
1705
1706 call_expr = get_call_expr_in (malloc_stmt);
1707 malloc_fn_decl = get_callee_fndecl (call_expr);
1708
1709 src = cgraph_node (context);
1710 dest = cgraph_node (malloc_fn_decl);
1711 cgraph_create_edge (src, dest, malloc_stmt,
1712 0, 0, bb_for_stmt (malloc_stmt)->loop_depth);
1713 }
1714
1715 /* This function generates set of statements required
1716 to allocate number NUM of structures of type NEW_TYPE.
1717 The statements are stored in NEW_STMTS. The statement that contain
1718 call to malloc is returned. MALLOC_STMT is an original call to malloc. */
1719
1720 static tree
1721 create_new_malloc (tree malloc_stmt, tree new_type, tree *new_stmts, tree num)
1722 {
1723 tree new_malloc_size;
1724 tree call_expr, malloc_fn_decl;
1725 tree new_stmt, malloc_res;
1726 tree call_stmt, final_stmt;
1727 tree cast_res;
1728
1729 gcc_assert (num && malloc_stmt && new_type);
1730 *new_stmts = alloc_stmt_list ();
1731
1732 /* Generate argument to malloc as multiplication of num
1733 and size of new_type. */
1734 new_stmt = gen_size (num, new_type, &new_malloc_size);
1735 append_to_statement_list (new_stmt, new_stmts);
1736
1737 /* Generate new call for malloc. */
1738 malloc_res = create_tmp_var (ptr_type_node, NULL);
1739
1740 if (malloc_res)
1741 add_referenced_var (malloc_res);
1742
1743 call_expr = get_call_expr_in (malloc_stmt);
1744 malloc_fn_decl = get_callee_fndecl (call_expr);
1745 call_expr = build_call_expr (malloc_fn_decl, 1, new_malloc_size);
1746 call_stmt = build_gimple_modify_stmt (malloc_res, call_expr);
1747 finalize_stmt_and_append (new_stmts, call_stmt);
1748
1749 /* Create new cast statement. */
1750 final_stmt = get_final_alloc_stmt (malloc_stmt);
1751 gcc_assert (final_stmt);
1752 new_stmt = gen_cast_stmt (malloc_res, new_type, final_stmt, &cast_res);
1753 append_to_statement_list (new_stmt, new_stmts);
1754
1755 return call_stmt;
1756 }
1757
1758 /* This function returns a tree representing
1759 the number of instances of structure STR_DECL allocated
1760 by allocation STMT. If new statments are generated,
1761 they are filled into NEW_STMTS_P. */
1762
1763 static tree
1764 gen_num_of_structs_in_malloc (tree stmt, tree str_decl, tree *new_stmts_p)
1765 {
1766 call_expr_arg_iterator iter;
1767 tree arg;
1768 tree call_expr;
1769 tree struct_size;
1770 HOST_WIDE_INT struct_size_int;
1771
1772 if (!stmt)
1773 return NULL_TREE;
1774
1775 /* Get malloc argument. */
1776 call_expr = get_call_expr_in (stmt);
1777 if (!call_expr)
1778 return NULL_TREE;
1779
1780 arg = first_call_expr_arg (call_expr, &iter);
1781
1782 if (TREE_CODE (arg) != SSA_NAME
1783 && !TREE_CONSTANT (arg))
1784 return NULL_TREE;
1785
1786 struct_size = TYPE_SIZE_UNIT (str_decl);
1787 struct_size_int = TREE_INT_CST_LOW (struct_size);
1788
1789 gcc_assert (struct_size);
1790
1791 if (TREE_CODE (arg) == SSA_NAME)
1792 {
1793 tree num, div_stmt;
1794
1795 if (is_result_of_mult (arg, &num, struct_size))
1796 return num;
1797
1798 num = create_tmp_var (integer_type_node, NULL);
1799
1800 if (num)
1801 add_referenced_var (num);
1802
1803 if (exact_log2 (struct_size_int) == -1)
1804 div_stmt = build_gimple_modify_stmt (num,
1805 build2 (TRUNC_DIV_EXPR,
1806 integer_type_node,
1807 arg, struct_size));
1808 else
1809 {
1810 tree C = build_int_cst (integer_type_node,
1811 exact_log2 (struct_size_int));
1812
1813 div_stmt =
1814 build_gimple_modify_stmt (num, build2 (RSHIFT_EXPR,
1815 integer_type_node,
1816 arg, C));
1817 }
1818 *new_stmts_p = alloc_stmt_list ();
1819 append_to_statement_list (div_stmt,
1820 new_stmts_p);
1821 finalize_stmt (div_stmt);
1822 return num;
1823 }
1824
1825 if (CONSTANT_CLASS_P (arg)
1826 && multiple_of_p (TREE_TYPE (struct_size), arg, struct_size))
1827 return int_const_binop (TRUNC_DIV_EXPR, arg, struct_size, 0);
1828
1829 return NULL_TREE;
1830 }
1831
1832 /* This function is a callback for traversal on new_var's hashtable.
1833 SLOT is a pointer to new_var. This function prints to dump_file
1834 an original variable and all new variables from the new_var
1835 pointed by *SLOT. */
1836
1837 static int
1838 dump_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
1839 {
1840 new_var n_var = *(new_var *) slot;
1841 tree var_type;
1842 tree var;
1843 unsigned i;
1844
1845 var_type = get_type_of_var (n_var->orig_var);
1846
1847 fprintf (dump_file, "\nOrig var: ");
1848 print_generic_expr (dump_file, n_var->orig_var, 0);
1849 fprintf (dump_file, " of type ");
1850 print_generic_expr (dump_file, var_type, 0);
1851 fprintf (dump_file, "\n");
1852
1853 for (i = 0;
1854 VEC_iterate (tree, n_var->new_vars, i, var); i++)
1855 {
1856 var_type = get_type_of_var (var);
1857
1858 fprintf (dump_file, " ");
1859 print_generic_expr (dump_file, var, 0);
1860 fprintf (dump_file, " of type ");
1861 print_generic_expr (dump_file, var_type, 0);
1862 fprintf (dump_file, "\n");
1863 }
1864 return 1;
1865 }
1866
1867 /* This function copies attributes form ORIG_DECL to NEW_DECL. */
1868
1869 static inline void
1870 copy_decl_attributes (tree new_decl, tree orig_decl)
1871 {
1872
1873 DECL_ARTIFICIAL (new_decl) = 1;
1874 DECL_EXTERNAL (new_decl) = DECL_EXTERNAL (orig_decl);
1875 TREE_STATIC (new_decl) = TREE_STATIC (orig_decl);
1876 TREE_PUBLIC (new_decl) = TREE_PUBLIC (orig_decl);
1877 TREE_USED (new_decl) = TREE_USED (orig_decl);
1878 DECL_CONTEXT (new_decl) = DECL_CONTEXT (orig_decl);
1879 TREE_THIS_VOLATILE (new_decl) = TREE_THIS_VOLATILE (orig_decl);
1880 TREE_ADDRESSABLE (new_decl) = TREE_ADDRESSABLE (orig_decl);
1881
1882 if (TREE_CODE (orig_decl) == VAR_DECL)
1883 {
1884 TREE_READONLY (new_decl) = TREE_READONLY (orig_decl);
1885 DECL_TLS_MODEL (new_decl) = DECL_TLS_MODEL (orig_decl);
1886 }
1887 }
1888
1889 /* This function wraps NEW_STR_TYPE in pointers or arrays wrapper
1890 the same way as a structure type is wrapped in DECL.
1891 It returns the generated type. */
1892
1893 static inline tree
1894 gen_struct_type (tree decl, tree new_str_type)
1895 {
1896 tree type_orig = get_type_of_var (decl);
1897 tree new_type = new_str_type;
1898 VEC (type_wrapper_t, heap) *wrapper = VEC_alloc (type_wrapper_t, heap, 10);
1899 type_wrapper_t wr;
1900 type_wrapper_t *wr_p;
1901
1902 while (POINTER_TYPE_P (type_orig)
1903 || TREE_CODE (type_orig) == ARRAY_TYPE)
1904 {
1905 if (POINTER_TYPE_P (type_orig))
1906 {
1907 wr.wrap = 0;
1908 wr.domain = NULL_TREE;
1909 }
1910 else if (TREE_CODE (type_orig) == ARRAY_TYPE)
1911 {
1912 wr.wrap = 1;
1913 wr.domain = TYPE_DOMAIN (type_orig);
1914 }
1915 VEC_safe_push (type_wrapper_t, heap, wrapper, &wr);
1916 type_orig = TREE_TYPE (type_orig);
1917 }
1918
1919 while (VEC_length (type_wrapper_t, wrapper) != 0)
1920 {
1921 wr_p = VEC_last (type_wrapper_t, wrapper);
1922
1923 if (wr_p->wrap) /* Array. */
1924 new_type = build_array_type (new_type, wr_p->domain);
1925 else /* Pointer. */
1926 new_type = build_pointer_type (new_type);
1927
1928 VEC_pop (type_wrapper_t, wrapper);
1929 }
1930
1931 VEC_free (type_wrapper_t, heap, wrapper);
1932 return new_type;
1933 }
1934
1935 /* This function generates and returns new variable name based on
1936 ORIG_DECL name, combined with index I.
1937 The form of the new name is <orig_name>.<I> . */
1938
1939 static tree
1940 gen_var_name (tree orig_decl, unsigned HOST_WIDE_INT i)
1941 {
1942 const char *old_name;
1943 char *prefix;
1944 char *new_name;
1945
1946 if (!DECL_NAME (orig_decl)
1947 || !IDENTIFIER_POINTER (DECL_NAME (orig_decl)))
1948 return NULL;
1949
1950 /* If the original variable has a name, create an
1951 appropriate new name for the new variable. */
1952
1953 old_name = IDENTIFIER_POINTER (DECL_NAME (orig_decl));
1954 prefix = alloca (strlen (old_name) + 1);
1955 strcpy (prefix, old_name);
1956 ASM_FORMAT_PRIVATE_NAME (new_name, prefix, i);
1957 return get_identifier (new_name);
1958 }
1959
1960 /* This function adds NEW_NODE to hashtable of new_var's NEW_VARS_HTAB. */
1961
1962 static void
1963 add_to_new_vars_htab (new_var new_node, htab_t new_vars_htab)
1964 {
1965 void **slot;
1966
1967 slot = htab_find_slot_with_hash (new_vars_htab, new_node->orig_var,
1968 htab_hash_pointer (new_node->orig_var),
1969 INSERT);
1970 *slot = new_node;
1971 }
1972
1973 /* This function creates and returns new_var_data node
1974 with empty new_vars and orig_var equal to VAR. */
1975
1976 static new_var
1977 create_new_var_node (tree var, d_str str)
1978 {
1979 new_var node;
1980
1981 node = (new_var) xmalloc (sizeof (struct new_var_data));
1982 node->orig_var = var;
1983 node->new_vars = VEC_alloc (tree, heap, VEC_length (tree, str->new_types));
1984 return node;
1985 }
1986
1987 /* Check whether the type of VAR is potential candidate for peeling.
1988 Returns true if yes, false otherwise. If yes, TYPE_P will contain
1989 candidate type. If VAR is initialized, the type of VAR will be added
1990 to UNSUITABLE_TYPES. */
1991
1992 static bool
1993 is_candidate (tree var, tree *type_p, VEC (tree, heap) **unsuitable_types)
1994 {
1995 tree type;
1996 bool initialized = false;
1997
1998 *type_p = NULL;
1999
2000 if (!var)
2001 return false;
2002
2003 /* There is no support of initialized vars. */
2004 if (TREE_CODE (var) == VAR_DECL
2005 && DECL_INITIAL (var) != NULL_TREE)
2006 initialized = true;
2007
2008 type = get_type_of_var (var);
2009
2010 if (type)
2011 {
2012 type = TYPE_MAIN_VARIANT (strip_type (type));
2013 if (TREE_CODE (type) != RECORD_TYPE)
2014 return false;
2015 else
2016 {
2017 if (initialized && unsuitable_types && *unsuitable_types)
2018 {
2019 if (dump_file)
2020 {
2021 fprintf (dump_file, "The type ");
2022 print_generic_expr (dump_file, type, 0);
2023 fprintf (dump_file, " is initialized...Excluded.");
2024 }
2025 add_unsuitable_type (unsuitable_types, type);
2026 }
2027 *type_p = type;
2028 return true;
2029 }
2030 }
2031 else
2032 return false;
2033 }
2034
2035 /* Hash value for field_access_site. */
2036
2037 static hashval_t
2038 field_acc_hash (const void *x)
2039 {
2040 return htab_hash_pointer (((const struct field_access_site *)x)->stmt);
2041 }
2042
2043 /* This function returns nonzero if stmt of field_access_site X
2044 is equal to Y. */
2045
2046 static int
2047 field_acc_eq (const void *x, const void *y)
2048 {
2049 return ((const struct field_access_site *)x)->stmt == (const_tree)y;
2050 }
2051
2052 /* This function prints an access site, defined by SLOT. */
2053
2054 static int
2055 dump_acc (void **slot, void *data ATTRIBUTE_UNUSED)
2056 {
2057 struct access_site *acc = *(struct access_site **) slot;
2058 tree var;
2059 unsigned i;
2060
2061 fprintf(dump_file, "\n");
2062 if (acc->stmt)
2063 print_generic_stmt (dump_file, acc->stmt, 0);
2064 fprintf(dump_file, " : ");
2065
2066 for (i = 0; VEC_iterate (tree, acc->vars, i, var); i++)
2067 {
2068 print_generic_expr (dump_file, var, 0);
2069 fprintf(dump_file, ", ");
2070 }
2071 return 1;
2072 }
2073
2074 /* This function frees memory allocated for strcuture clusters,
2075 starting from CLUSTER. */
2076
2077 static void
2078 free_struct_cluster (struct field_cluster* cluster)
2079 {
2080 if (cluster)
2081 {
2082 if (cluster->fields_in_cluster)
2083 sbitmap_free (cluster->fields_in_cluster);
2084 if (cluster->sibling)
2085 free_struct_cluster (cluster->sibling);
2086 free (cluster);
2087 }
2088 }
2089
2090 /* Free all allocated memory under the structure node pointed by D_NODE. */
2091
2092 static void
2093 free_data_struct (d_str d_node)
2094 {
2095 int i;
2096
2097 if (!d_node)
2098 return;
2099
2100 if (dump_file)
2101 {
2102 fprintf (dump_file, "\nRemoving data structure \"");
2103 print_generic_expr (dump_file, d_node->decl, 0);
2104 fprintf (dump_file, "\" from data_struct_list.");
2105 }
2106
2107 /* Free all space under d_node. */
2108 if (d_node->fields)
2109 {
2110 for (i = 0; i < d_node->num_fields; i++)
2111 free_field_accesses (d_node->fields[i].acc_sites);
2112 free (d_node->fields);
2113 }
2114
2115 if (d_node->accs)
2116 free_accesses (d_node->accs);
2117
2118 if (d_node->struct_clustering)
2119 free_struct_cluster (d_node->struct_clustering);
2120
2121 if (d_node->new_types)
2122 VEC_free (tree, heap, d_node->new_types);
2123 }
2124
2125 /* This function creates new general and field accesses in BB. */
2126
2127 static void
2128 create_new_accesses_in_bb (basic_block bb)
2129 {
2130 d_str str;
2131 unsigned i;
2132
2133 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2134 create_new_accs_for_struct (str, bb);
2135 }
2136
2137 /* This function adds allocation sites for peeled structures.
2138 M_DATA is vector of allocation sites of function CONTEXT. */
2139
2140 static void
2141 create_new_alloc_sites (fallocs_t m_data, tree context)
2142 {
2143 alloc_site_t *call;
2144 unsigned j;
2145
2146 for (j = 0;
2147 VEC_iterate (alloc_site_t, m_data->allocs, j, call); j++)
2148 {
2149 tree stmt = call->stmt;
2150 d_str str = call->str;
2151 tree num;
2152 tree new_stmts = NULL_TREE;
2153 tree last_stmt = get_final_alloc_stmt (stmt);
2154 unsigned i;
2155 tree type;
2156
2157 num = gen_num_of_structs_in_malloc (stmt, str->decl, &new_stmts);
2158 if (new_stmts)
2159 {
2160 last_stmt = tsi_stmt (tsi_last (new_stmts));
2161 insert_after_stmt (last_stmt, new_stmts);
2162 }
2163
2164 /* Generate an allocation sites for each new structure type. */
2165 for (i = 0;
2166 VEC_iterate (tree, str->new_types, i, type); i++)
2167 {
2168 tree new_malloc_stmt = NULL_TREE;
2169 tree last_stmt_tmp = NULL_TREE;
2170
2171 new_stmts = NULL_TREE;
2172 new_malloc_stmt = create_new_malloc (stmt, type, &new_stmts, num);
2173 last_stmt_tmp = tsi_stmt (tsi_last (new_stmts));
2174 insert_after_stmt (last_stmt, new_stmts);
2175 update_cgraph_with_malloc_call (new_malloc_stmt, context);
2176 last_stmt = last_stmt_tmp;
2177 }
2178 }
2179 }
2180
2181 /* This function prints new variables from hashtable
2182 NEW_VARS_HTAB to dump_file. */
2183
2184 static void
2185 dump_new_vars (htab_t new_vars_htab)
2186 {
2187 if (!dump_file)
2188 return;
2189
2190 if (new_vars_htab)
2191 htab_traverse (new_vars_htab, dump_new_var, NULL);
2192 }
2193
2194 /* Given an original variable ORIG_DECL of structure type STR,
2195 this function generates new variables of the types defined
2196 by STR->new_type. Generated types are saved in new_var node NODE.
2197 ORIG_DECL should has VAR_DECL tree_code. */
2198
2199 static void
2200 create_new_var_1 (tree orig_decl, d_str str, new_var node)
2201 {
2202 unsigned i;
2203 tree type;
2204
2205 for (i = 0;
2206 VEC_iterate (tree, str->new_types, i, type); i++)
2207 {
2208 tree new_decl = NULL;
2209 tree new_name;
2210
2211 new_name = gen_var_name (orig_decl, i);
2212 type = gen_struct_type (orig_decl, type);
2213
2214 if (is_global_var (orig_decl))
2215 new_decl = build_decl (VAR_DECL, new_name, type);
2216 else
2217 {
2218 const char *name = new_name ? IDENTIFIER_POINTER (new_name) : NULL;
2219 new_decl = create_tmp_var (type, name);
2220 }
2221
2222 copy_decl_attributes (new_decl, orig_decl);
2223 VEC_safe_push (tree, heap, node->new_vars, new_decl);
2224 }
2225 }
2226
2227 /* This function creates new variables to
2228 substitute the original variable VAR_DECL and adds
2229 them to the new_var's hashtable NEW_VARS_HTAB. */
2230
2231 static void
2232 create_new_var (tree var_decl, htab_t new_vars_htab)
2233 {
2234 new_var node;
2235 d_str str;
2236 tree type;
2237 unsigned i;
2238
2239 if (!var_decl || is_in_new_vars_htab (var_decl, new_vars_htab))
2240 return;
2241
2242 if (!is_candidate (var_decl, &type, NULL))
2243 return;
2244
2245 i = find_structure (type);
2246 if (i == VEC_length (structure, structures))
2247 return;
2248
2249 str = VEC_index (structure, structures, i);
2250 node = create_new_var_node (var_decl, str);
2251 create_new_var_1 (var_decl, str, node);
2252 add_to_new_vars_htab (node, new_vars_htab);
2253 }
2254
2255 /* Hash value for new_var. */
2256
2257 static hashval_t
2258 new_var_hash (const void *x)
2259 {
2260 return htab_hash_pointer (((const_new_var)x)->orig_var);
2261 }
2262
2263 /* This function returns nonzero if orig_var of new_var X is equal to Y. */
2264
2265 static int
2266 new_var_eq (const void *x, const void *y)
2267 {
2268 return ((const_new_var)x)->orig_var == (const_tree)y;
2269 }
2270
2271 /* This function check whether a structure type represented by STR
2272 escapes due to ipa-type-escape analysis. If yes, this type is added
2273 to UNSUITABLE_TYPES vector. */
2274
2275 static void
2276 check_type_escape (d_str str, VEC (tree, heap) **unsuitable_types)
2277 {
2278 tree type = str->decl;
2279
2280 if (!ipa_type_escape_type_contained_p (type))
2281 {
2282 if (dump_file)
2283 {
2284 fprintf (dump_file, "\nEscaping type is ");
2285 print_generic_expr (dump_file, type, 0);
2286 }
2287 add_unsuitable_type (unsuitable_types, type);
2288 }
2289 }
2290
2291 /* Hash value for access_site. */
2292
2293 static hashval_t
2294 acc_hash (const void *x)
2295 {
2296 return htab_hash_pointer (((const struct access_site *)x)->stmt);
2297 }
2298
2299 /* Return nonzero if stmt of access_site X is equal to Y. */
2300
2301 static int
2302 acc_eq (const void *x, const void *y)
2303 {
2304 return ((const struct access_site *)x)->stmt == (const_tree)y;
2305 }
2306
2307 /* Given a structure declaration STRUCT_DECL, and number of fields
2308 in the structure NUM_FIELDS, this function creates and returns
2309 corresponding field_entry's. */
2310
2311 static struct field_entry *
2312 get_fields (tree struct_decl, int num_fields)
2313 {
2314 struct field_entry *list;
2315 tree t = TYPE_FIELDS (struct_decl);
2316 int idx = 0;
2317
2318 list =
2319 (struct field_entry *) xmalloc (num_fields * sizeof (struct field_entry));
2320
2321 for (idx = 0 ; t; t = TREE_CHAIN (t), idx++)
2322 if (TREE_CODE (t) == FIELD_DECL)
2323 {
2324 list[idx].index = idx;
2325 list[idx].decl = t;
2326 list[idx].acc_sites =
2327 htab_create (32, field_acc_hash, field_acc_eq, NULL);
2328 list[idx].count = 0;
2329 list[idx].field_mapping = NULL_TREE;
2330 }
2331
2332 return list;
2333 }
2334
2335 /* Print non-field accesses from hashtable ACCS of structure. */
2336
2337 static void
2338 dump_access_sites (htab_t accs)
2339 {
2340 if (!dump_file)
2341 return;
2342
2343 if (accs)
2344 htab_traverse (accs, dump_acc, NULL);
2345 }
2346
2347 /* This function removes the structure with index I from structures vector. */
2348
2349 static void
2350 remove_structure (unsigned i)
2351 {
2352 d_str str;
2353
2354 if (i >= VEC_length (structure, structures))
2355 return;
2356
2357 str = VEC_index (structure, structures, i);
2358 free_data_struct (str);
2359 VEC_ordered_remove (structure, structures, i);
2360 }
2361
2362 /* Currently we support only EQ_EXPR or NE_EXPR conditions.
2363 COND_STNT is a condition statement to check. */
2364
2365 static bool
2366 is_safe_cond_expr (tree cond_stmt)
2367 {
2368
2369 tree arg0, arg1;
2370 unsigned str0, str1;
2371 bool s0, s1;
2372 unsigned length = VEC_length (structure, structures);
2373
2374 tree cond = COND_EXPR_COND (cond_stmt);
2375
2376 if (TREE_CODE (cond) != EQ_EXPR
2377 && TREE_CODE (cond) != NE_EXPR)
2378 return false;
2379
2380 if (TREE_CODE_LENGTH (TREE_CODE (cond)) != 2)
2381 return false;
2382
2383 arg0 = TREE_OPERAND (cond, 0);
2384 arg1 = TREE_OPERAND (cond, 1);
2385
2386 str0 = find_structure (strip_type (get_type_of_var (arg0)));
2387 str1 = find_structure (strip_type (get_type_of_var (arg1)));
2388
2389 s0 = (str0 != length) ? true : false;
2390 s1 = (str1 != length) ? true : false;
2391
2392 if (!((!s0 && s1) || (!s1 && s0)))
2393 return false;
2394
2395 return true;
2396 }
2397
2398 /* This function excludes statements, that are
2399 part of allocation sites or field accesses, from the
2400 hashtable of general accesses. SLOT represents general
2401 access that will be checked. DATA is a pointer to
2402 exclude_data structure. */
2403
2404 static int
2405 exclude_from_accs (void **slot, void *data)
2406 {
2407 struct access_site *acc = *(struct access_site **) slot;
2408 tree fn_decl = ((struct exclude_data *)data)->fn_decl;
2409 d_str str = ((struct exclude_data *)data)->str;
2410
2411 if (is_part_of_malloc (acc->stmt, fn_decl)
2412 || is_part_of_field_access (acc->stmt, str))
2413 {
2414 VEC_free (tree, heap, acc->vars);
2415 free (acc);
2416 htab_clear_slot (str->accs, slot);
2417 }
2418 return 1;
2419 }
2420
2421 /* Callback function for walk_tree called from collect_accesses_in_bb
2422 function. DATA is the statement which is analyzed. */
2423
2424 static tree
2425 get_stmt_accesses (tree *tp, int *walk_subtrees, void *data)
2426 {
2427 tree stmt = (tree) data;
2428 tree t = *tp;
2429
2430 if (!t)
2431 return NULL;
2432
2433 switch (TREE_CODE (t))
2434 {
2435 case GIMPLE_MODIFY_STMT:
2436 {
2437 tree lhs = GIMPLE_STMT_OPERAND (t, 0);
2438 tree rhs = GIMPLE_STMT_OPERAND (t, 1);
2439 *walk_subtrees = 1;
2440 walk_tree (&lhs, get_stmt_accesses, data, NULL);
2441 walk_tree (&rhs, get_stmt_accesses, data, NULL);
2442 *walk_subtrees = 0;
2443 }
2444 break;
2445
2446 case BIT_FIELD_REF:
2447 {
2448 tree var = TREE_OPERAND(t, 0);
2449 tree type = TYPE_MAIN_VARIANT (strip_type (get_type_of_var (var)));
2450 unsigned i = find_structure (type);
2451
2452 if (i != VEC_length (structure, structures))
2453 {
2454 if (dump_file)
2455 {
2456 fprintf (dump_file, "\nThe type ");
2457 print_generic_expr (dump_file, type, 0);
2458 fprintf (dump_file, " has bitfield.");
2459 }
2460 remove_structure (i);
2461 }
2462 }
2463 break;
2464
2465 case COMPONENT_REF:
2466 {
2467 tree ref = TREE_OPERAND (t, 0);
2468 tree field_decl = TREE_OPERAND (t, 1);
2469
2470
2471 if ((TREE_CODE (ref) == INDIRECT_REF
2472 || TREE_CODE (ref) == ARRAY_REF
2473 || TREE_CODE (ref) == VAR_DECL)
2474 && TREE_CODE (field_decl) == FIELD_DECL)
2475 {
2476 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
2477 unsigned i = find_structure (type);
2478
2479 if (i != VEC_length (structure, structures))
2480 {
2481 d_str str = VEC_index (structure, structures, i);
2482 struct field_entry * field =
2483 find_field_in_struct (str, field_decl);
2484
2485 if (field)
2486 {
2487 struct field_access_site *acc = make_field_acc_node ();
2488
2489 gcc_assert (acc);
2490
2491 acc->stmt = stmt;
2492 acc->comp_ref = t;
2493 acc->ref = ref;
2494 acc->field_decl = field_decl;
2495
2496 /* Check whether the access is of the form
2497 we can deal with. */
2498 if (!decompose_access (str->decl, acc))
2499 {
2500 if (dump_file)
2501 {
2502 fprintf (dump_file, "\nThe type ");
2503 print_generic_expr (dump_file, type, 0);
2504 fprintf (dump_file,
2505 " has complicate access in statement ");
2506 print_generic_stmt (dump_file, stmt, 0);
2507 }
2508
2509 remove_structure (i);
2510 free (acc);
2511 }
2512 else
2513 {
2514 /* Increase count of field. */
2515 basic_block bb = bb_for_stmt (stmt);
2516 field->count += bb->count;
2517
2518 /* Add stmt to the acc_sites of field. */
2519 add_field_acc_to_acc_sites (acc, field->acc_sites);
2520 }
2521 *walk_subtrees = 0;
2522 }
2523 }
2524 }
2525 }
2526 break;
2527
2528 case MINUS_EXPR:
2529 case PLUS_EXPR:
2530 {
2531 tree op0 = TREE_OPERAND (t, 0);
2532 tree op1 = TREE_OPERAND (t, 1);
2533 *walk_subtrees = 1;
2534 walk_tree (&op0, get_stmt_accesses, data, NULL);
2535 walk_tree (&op1, get_stmt_accesses, data, NULL);
2536 *walk_subtrees = 0;
2537 }
2538 break;
2539
2540 case COND_EXPR:
2541 {
2542 tree cond = COND_EXPR_COND (t);
2543 int i;
2544 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (cond)); i++)
2545 {
2546 tree t = TREE_OPERAND (cond, i);
2547
2548 *walk_subtrees = 1;
2549 walk_tree (&t, get_stmt_accesses, data, NULL);
2550 }
2551 *walk_subtrees = 0;
2552 }
2553 break;
2554
2555 case VAR_DECL:
2556 case SSA_NAME:
2557 {
2558 unsigned i;
2559
2560 if (TREE_CODE (t) == SSA_NAME)
2561 t = SSA_NAME_VAR (t);
2562
2563 i = find_structure (strip_type (get_type_of_var (t)));
2564 if (i != VEC_length (structure, structures))
2565 {
2566 d_str str;
2567
2568 str = VEC_index (structure, structures, i);
2569 add_access_to_acc_sites (stmt, t, str->accs);
2570 }
2571 *walk_subtrees = 0;
2572 }
2573 break;
2574
2575 case CALL_EXPR:
2576 {
2577 /* It was checked as part of stage1 that structures
2578 to be transformed cannot be passed as parameters of functions. */
2579 *walk_subtrees = 0;
2580 }
2581 break;
2582
2583 default:
2584 return NULL;
2585 }
2586
2587 return NULL;
2588 }
2589
2590 /* Free structures hashtable. */
2591
2592 static void
2593 free_structures (void)
2594 {
2595 d_str str;
2596 unsigned i;
2597
2598 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2599 free_data_struct (str);
2600
2601 VEC_free (structure, heap, structures);
2602 structures = NULL;
2603 }
2604
2605 /* This function is a callback for traversal over new_var's hashtable.
2606 SLOT is a pointer to new_var. This function frees memory allocated
2607 for new_var and pointed by *SLOT. */
2608
2609 static int
2610 free_new_var (void **slot, void *data ATTRIBUTE_UNUSED)
2611 {
2612 new_var n_var = *(new_var *) slot;
2613
2614 /* Free vector of new_vars. */
2615 VEC_free (tree, heap, n_var->new_vars);
2616 free (n_var);
2617 return 1;
2618 }
2619
2620 /* Free new_vars hashtable NEW_VARS_HTAB. */
2621
2622 static void
2623 free_new_vars_htab (htab_t new_vars_htab)
2624 {
2625 if (new_vars_htab)
2626 htab_traverse (new_vars_htab, free_new_var, NULL);
2627 htab_delete (new_vars_htab);
2628 new_vars_htab = NULL;
2629 }
2630
2631 /* This function creates new general and field accesses that appear in cfun. */
2632
2633 static void
2634 create_new_accesses_for_func (void)
2635 {
2636 basic_block bb;
2637
2638 FOR_EACH_BB_FN (bb, cfun)
2639 create_new_accesses_in_bb (bb);
2640 }
2641
2642 /* Create new allocation sites for the function represented by NODE. */
2643
2644 static void
2645 create_new_alloc_sites_for_func (struct cgraph_node *node)
2646 {
2647 fallocs_t fallocs = get_fallocs (node->decl);
2648
2649 if (fallocs)
2650 create_new_alloc_sites (fallocs, node->decl);
2651 }
2652
2653 /* For each local variable of structure type from the vector of structures
2654 this function generates new variable(s) to replace it. */
2655
2656 static void
2657 create_new_local_vars (void)
2658 {
2659 tree var;
2660 referenced_var_iterator rvi;
2661
2662 new_local_vars = htab_create (num_referenced_vars,
2663 new_var_hash, new_var_eq, NULL);
2664
2665 FOR_EACH_REFERENCED_VAR (var, rvi)
2666 {
2667 if (!is_global_var (var))
2668 create_new_var (var, new_local_vars);
2669 }
2670
2671 if (new_local_vars)
2672 htab_traverse (new_local_vars, finalize_new_vars_creation, NULL);
2673 dump_new_vars (new_local_vars);
2674 }
2675
2676 /* This function prints the SHIFT number of spaces to the DUMP_FILE. */
2677
2678 static inline void
2679 print_shift (unsigned HOST_WIDE_INT shift)
2680 {
2681 unsigned HOST_WIDE_INT sh = shift;
2682
2683 while (sh--)
2684 fprintf (dump_file, " ");
2685 }
2686
2687 /* This function updates field_mapping of FIELDS in CLUSTER with NEW_TYPE. */
2688
2689 static inline void
2690 update_fields_mapping (struct field_cluster *cluster, tree new_type,
2691 struct field_entry * fields, int num_fields)
2692 {
2693 int i;
2694
2695 for (i = 0; i < num_fields; i++)
2696 if (TEST_BIT (cluster->fields_in_cluster, i))
2697 fields[i].field_mapping = new_type;
2698 }
2699
2700 /* This functions builds structure with FIELDS,
2701 NAME and attributes similar to ORIG_STRUCT.
2702 It returns the newly created structure. */
2703
2704 static tree
2705 build_basic_struct (tree fields, tree name, tree orig_struct)
2706 {
2707 tree attributes = NULL_TREE;
2708 tree ref = 0;
2709 tree x;
2710
2711 if (TYPE_ATTRIBUTES (orig_struct))
2712 attributes = unshare_expr (TYPE_ATTRIBUTES (orig_struct));
2713 ref = make_node (RECORD_TYPE);
2714 TYPE_SIZE (ref) = 0;
2715 decl_attributes (&ref, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
2716 TYPE_PACKED (ref) = TYPE_PACKED (orig_struct);
2717 for (x = fields; x; x = TREE_CHAIN (x))
2718 {
2719 DECL_CONTEXT (x) = ref;
2720 DECL_PACKED (x) |= TYPE_PACKED (ref);
2721 }
2722 TYPE_FIELDS (ref) = fields;
2723 layout_type (ref);
2724 TYPE_NAME (ref) = name;
2725
2726 return ref;
2727 }
2728
2729 /* This function copies FIELDS from CLUSTER into TREE_CHAIN as part
2730 of preparation for new structure building. NUM_FIELDS is a total
2731 number of fields in the structure. The function returns newly
2732 generated fields. */
2733
2734 static tree
2735 create_fields (struct field_cluster * cluster,
2736 struct field_entry * fields, int num_fields)
2737 {
2738 int i;
2739 tree new_types = NULL_TREE;
2740 tree last = NULL_TREE;
2741
2742 for (i = 0; i < num_fields; i++)
2743 if (TEST_BIT (cluster->fields_in_cluster, i))
2744 {
2745 tree new_decl = unshare_expr (fields[i].decl);
2746
2747 if (!new_types)
2748 new_types = new_decl;
2749 else
2750 TREE_CHAIN (last) = new_decl;
2751 last = new_decl;
2752 }
2753
2754 TREE_CHAIN (last) = NULL_TREE;
2755 return new_types;
2756
2757 }
2758
2759 /* This function creates a cluster name. The name is based on
2760 the original structure name, if it is present. It has a form:
2761
2762 <original_struct_name>_sub.<CLUST_NUM>
2763
2764 The original structure name is taken from the type of DECL.
2765 If an original structure name is not present, it's generated to be:
2766
2767 struct.<STR_NUM>
2768
2769 The function returns identifier of the new cluster name. */
2770
2771 static inline tree
2772 gen_cluster_name (tree decl, int clust_num, int str_num)
2773 {
2774 const char * orig_name = get_type_name (decl);
2775 char * tmp_name = NULL;
2776 char * prefix;
2777 char * new_name;
2778 size_t len;
2779
2780 if (!orig_name)
2781 ASM_FORMAT_PRIVATE_NAME(tmp_name, "struct", str_num);
2782
2783 len = strlen (tmp_name ? tmp_name : orig_name) + strlen ("_sub");
2784 prefix = alloca (len + 1);
2785 memcpy (prefix, tmp_name ? tmp_name : orig_name,
2786 strlen (tmp_name ? tmp_name : orig_name));
2787 strcpy (prefix + strlen (tmp_name ? tmp_name : orig_name), "_sub");
2788
2789 ASM_FORMAT_PRIVATE_NAME (new_name, prefix, clust_num);
2790 return get_identifier (new_name);
2791 }
2792
2793 /* This function checks whether the structure STR has bitfields.
2794 If yes, this structure type is added to UNSUITABLE_TYPES vector. */
2795
2796 static void
2797 check_bitfields (d_str str, VEC (tree, heap) **unsuitable_types)
2798 {
2799 tree type = str->decl;
2800 int i;
2801
2802 for (i = 0; i < str->num_fields; i++)
2803 if (DECL_BIT_FIELD (str->fields[i].decl))
2804 {
2805 add_unsuitable_type (unsuitable_types, type);
2806 if (dump_file)
2807 {
2808 fprintf (dump_file, "\nType ");
2809 print_generic_expr (dump_file, type, 0);
2810 fprintf (dump_file, "\nescapes due to bitfield ");
2811 print_generic_expr (dump_file, str->fields[i].decl, 0);
2812 }
2813 break;
2814 }
2815 }
2816
2817 /* This function adds to UNSUITABLE_TYPES those types that escape
2818 due to results of ipa-type-escpae analysis. See ipa-type-escpae.[c,h]. */
2819
2820 static void
2821 exclude_escaping_types_1 (VEC (tree, heap) **unsuitable_types)
2822 {
2823 d_str str;
2824 unsigned i;
2825
2826 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
2827 check_type_escape (str, unsuitable_types);
2828 }
2829
2830 /* If a structure type is a return type of any function,
2831 we cannot transform it. Such type is added to UNSUITABLE_TYPES vector. */
2832
2833 static void
2834 exclude_returned_types (VEC (tree, heap) **unsuitable_types)
2835 {
2836 struct cgraph_node *c_node;
2837
2838 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
2839 {
2840 tree ret_t = TREE_TYPE (TREE_TYPE (c_node->decl));
2841
2842 if (ret_t)
2843 {
2844 ret_t = strip_type (ret_t);
2845 if (TREE_CODE (ret_t) == RECORD_TYPE)
2846 {
2847 add_unsuitable_type (unsuitable_types, TYPE_MAIN_VARIANT (ret_t));
2848 if (dump_file)
2849 {
2850 fprintf (dump_file, "\nThe type \"");
2851 print_generic_expr (dump_file, ret_t, 0);
2852 fprintf (dump_file,
2853 "\" is return type of function...Excluded.");
2854 }
2855 }
2856 }
2857 }
2858 }
2859
2860 /* This function looks for parameters of local functions
2861 which are of structure types, or derived from them (arrays
2862 of structures, pointers to structures, or their combinations).
2863 We are not handling peeling of such structures right now.
2864 The found structures types are added to UNSUITABLE_TYPES vector. */
2865
2866 static void
2867 exclude_types_passed_to_local_func (VEC (tree, heap) **unsuitable_types)
2868 {
2869 struct cgraph_node *c_node;
2870
2871 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
2872 if (cgraph_function_body_availability (c_node) == AVAIL_LOCAL)
2873 {
2874 tree fn = c_node->decl;
2875 tree arg;
2876
2877 for (arg = DECL_ARGUMENTS (fn); arg; arg = TREE_CHAIN (arg))
2878 {
2879 tree type = TREE_TYPE (arg);
2880
2881 type = strip_type (type);
2882 if (TREE_CODE (type) == RECORD_TYPE)
2883 {
2884 add_unsuitable_type (unsuitable_types,
2885 TYPE_MAIN_VARIANT (type));
2886 if (dump_file)
2887 {
2888 fprintf (dump_file, "\nPointer to type \"");
2889 print_generic_expr (dump_file, type, 0);
2890 fprintf (dump_file,
2891 "\" is passed to local function...Excluded.");
2892 }
2893 }
2894 }
2895 }
2896 }
2897
2898 /* This function analyzes structure form of structures
2899 potential for transformation. If we are not capable to transform
2900 structure of some form, we remove it from the structures hashtable.
2901 Right now we cannot handle nested structs, when nesting is
2902 through any level of pointers or arrays.
2903
2904 TBD: release these constrains in future.
2905
2906 Note, that in this function we suppose that all structures
2907 in the program are members of the structures hashtable right now,
2908 without excluding escaping types. */
2909
2910 static void
2911 check_struct_form (d_str str, VEC (tree, heap) **unsuitable_types)
2912 {
2913 int i;
2914
2915 for (i = 0; i < str->num_fields; i++)
2916 {
2917 tree f_type = strip_type(TREE_TYPE (str->fields[i].decl));
2918
2919 if (TREE_CODE (f_type) == RECORD_TYPE)
2920 {
2921 add_unsuitable_type (unsuitable_types, TYPE_MAIN_VARIANT (f_type));
2922 add_unsuitable_type (unsuitable_types, str->decl);
2923 if (dump_file)
2924 {
2925 fprintf (dump_file, "\nType ");
2926 print_generic_expr (dump_file, f_type, 0);
2927 fprintf (dump_file, " is a field in the structure ");
2928 print_generic_expr (dump_file, str->decl, 0);
2929 fprintf (dump_file, ". Escaping...");
2930 }
2931 }
2932 }
2933 }
2934
2935 /* This function adds a structure TYPE to the vector of structures,
2936 if it's not already there. */
2937
2938 static void
2939 add_structure (tree type)
2940 {
2941 struct data_structure node;
2942 unsigned i;
2943 int num_fields;
2944
2945 type = TYPE_MAIN_VARIANT (type);
2946
2947 i = find_structure (type);
2948
2949 if (i != VEC_length (structure, structures))
2950 return;
2951
2952 num_fields = fields_length (type);
2953 node.decl = type;
2954 node.num_fields = num_fields;
2955 node.fields = get_fields (type, num_fields);
2956 node.struct_clustering = NULL;
2957 node.accs = htab_create (32, acc_hash, acc_eq, NULL);
2958 node.new_types = VEC_alloc (tree, heap, num_fields);
2959 node.count = 0;
2960
2961 VEC_safe_push (structure, heap, structures, &node);
2962
2963 if (dump_file)
2964 {
2965 fprintf (dump_file, "\nAdding data structure \"");
2966 print_generic_expr (dump_file, type, 0);
2967 fprintf (dump_file, "\" to data_struct_list.");
2968 }
2969 }
2970
2971 /* This function adds an allocation site to alloc_sites hashtable.
2972 The allocation site appears in STMT of function FN_DECL and
2973 allocates the structure represented by STR. */
2974
2975 static void
2976 add_alloc_site (tree fn_decl, tree stmt, d_str str)
2977 {
2978 fallocs_t fallocs = NULL;
2979 alloc_site_t m_call;
2980
2981 m_call.stmt = stmt;
2982 m_call.str = str;
2983
2984 fallocs =
2985 (fallocs_t) htab_find_with_hash (alloc_sites,
2986 fn_decl, htab_hash_pointer (fn_decl));
2987
2988 if (!fallocs)
2989 {
2990 void **slot;
2991
2992 fallocs = (fallocs_t)
2993 xmalloc (sizeof (struct func_alloc_sites));
2994 fallocs->func = fn_decl;
2995 fallocs->allocs = VEC_alloc (alloc_site_t, heap, 1);
2996 slot = htab_find_slot_with_hash (alloc_sites, fn_decl,
2997 htab_hash_pointer (fn_decl), INSERT);
2998 *slot = fallocs;
2999 }
3000 VEC_safe_push (alloc_site_t, heap,
3001 fallocs->allocs, &m_call);
3002
3003 if (dump_file)
3004 {
3005 fprintf (dump_file, "\nAdding stmt ");
3006 print_generic_stmt (dump_file, stmt, 0);
3007 fprintf (dump_file, " to list of mallocs.");
3008 }
3009 }
3010
3011 /* This function returns true if the result of STMT, that contains a call
3012 to an allocation function, is cast to one of the structure types.
3013 STMT should be of the form: T.2 = <alloc_func> (T.1);
3014 If true, I_P contains an index of an allocated structure.
3015 Otherwise I_P contains the length of the vector of structures. */
3016
3017 static bool
3018 is_alloc_of_struct (tree stmt, unsigned *i_p)
3019 {
3020 tree lhs;
3021 tree type;
3022 tree final_stmt;
3023
3024 final_stmt = get_final_alloc_stmt (stmt);
3025
3026 if (!final_stmt)
3027 return false;
3028
3029 /* final_stmt should be of the form:
3030 T.3 = (struct_type *) T.2; */
3031
3032 if (TREE_CODE (final_stmt) != GIMPLE_MODIFY_STMT)
3033 return false;
3034
3035 lhs = GIMPLE_STMT_OPERAND (final_stmt, 0);
3036
3037 type = get_type_of_var (lhs);
3038
3039 if (!type)
3040 return false;
3041
3042 if (!POINTER_TYPE_P (type)
3043 || TREE_CODE (strip_type (type)) != RECORD_TYPE)
3044 return false;
3045
3046 *i_p = find_structure (strip_type (type));
3047
3048 if (*i_p == VEC_length (structure, structures))
3049 return false;
3050
3051 return true;
3052 }
3053
3054 /* This function prints non-field and field accesses
3055 of the structure STR. */
3056
3057 static void
3058 dump_accs (d_str str)
3059 {
3060 int i;
3061
3062 fprintf (dump_file, "\nAccess sites of struct ");
3063 print_generic_expr (dump_file, str->decl, 0);
3064
3065 for (i = 0; i < str->num_fields; i++)
3066 {
3067 fprintf (dump_file, "\nAccess site of field ");
3068 print_generic_expr (dump_file, str->fields[i].decl, 0);
3069 dump_field_acc_sites (str->fields[i].acc_sites);
3070 fprintf (dump_file, ":\n");
3071 }
3072 fprintf (dump_file, "\nGeneral access sites\n");
3073 dump_access_sites (str->accs);
3074 }
3075
3076 /* This function checks whether an access statement, pointed by SLOT,
3077 is a condition we are capable to transform. If not, it removes
3078 the structure with index, represented by DATA, from the vector
3079 of structures. */
3080
3081 static int
3082 safe_cond_expr_check (void **slot, void *data)
3083 {
3084 struct access_site *acc = *(struct access_site **) slot;
3085
3086 if (TREE_CODE (acc->stmt) == COND_EXPR)
3087 {
3088 if (!is_safe_cond_expr (acc->stmt))
3089 {
3090 if (dump_file)
3091 {
3092 fprintf (dump_file, "\nUnsafe conditional statement ");
3093 print_generic_stmt (dump_file, acc->stmt, 0);
3094 }
3095 remove_structure (*(unsigned *) data);
3096 }
3097 }
3098 return 1;
3099 }
3100
3101 /* This function excludes statements that are part of allocation sites and
3102 field accesses from the hashtable of general accesses of the structure
3103 type STR. Only accesses that belong to the function represented by
3104 NODE are treated. */
3105
3106 static void
3107 exclude_alloc_and_field_accs_1 (d_str str, struct cgraph_node *node)
3108 {
3109 struct exclude_data dt;
3110 dt.str = str;
3111 dt.fn_decl = node->decl;
3112
3113 if (dt.str->accs)
3114 htab_traverse (dt.str->accs, exclude_from_accs, &dt);
3115 }
3116
3117 /* Collect accesses to the structure types that apear in basic bloack BB. */
3118
3119 static void
3120 collect_accesses_in_bb (basic_block bb)
3121 {
3122 block_stmt_iterator bsi;
3123
3124 for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
3125 {
3126 tree stmt = bsi_stmt (bsi);
3127
3128 /* In asm stmt we cannot always track the arguments,
3129 so we just give up. */
3130 if (TREE_CODE (stmt) == ASM_EXPR)
3131 {
3132 free_structures ();
3133 break;
3134 }
3135
3136 walk_tree (&stmt, get_stmt_accesses, stmt, NULL);
3137 }
3138 }
3139
3140 /* This function generates cluster substructure that cointains FIELDS.
3141 The cluster added to the set of clusters of the structure SRT. */
3142
3143 static void
3144 gen_cluster (sbitmap fields, d_str str)
3145 {
3146 struct field_cluster *crr_cluster = NULL;
3147
3148 crr_cluster =
3149 (struct field_cluster *) xcalloc (1, sizeof (struct field_cluster));
3150 crr_cluster->sibling = str->struct_clustering;
3151 str->struct_clustering = crr_cluster;
3152 crr_cluster->fields_in_cluster = fields;
3153 }
3154
3155 /* This function peels a field with the index I from the structure DS. */
3156
3157 static void
3158 peel_field (int i, d_str ds)
3159 {
3160 struct field_cluster *crr_cluster = NULL;
3161
3162 crr_cluster =
3163 (struct field_cluster *) xcalloc (1, sizeof (struct field_cluster));
3164 crr_cluster->sibling = ds->struct_clustering;
3165 ds->struct_clustering = crr_cluster;
3166 crr_cluster->fields_in_cluster =
3167 sbitmap_alloc ((unsigned int) ds->num_fields);
3168 sbitmap_zero (crr_cluster->fields_in_cluster);
3169 SET_BIT (crr_cluster->fields_in_cluster, i);
3170 }
3171
3172 /* This function calculates maximum field count in
3173 the structure STR. */
3174
3175 static gcov_type
3176 get_max_field_count (d_str str)
3177 {
3178 gcov_type max = 0;
3179 int i;
3180
3181 for (i = 0; i < str->num_fields; i++)
3182 if (str->fields[i].count > max)
3183 max = str->fields[i].count;
3184
3185 return max;
3186 }
3187
3188 /* Do struct-reorg transformation for individual function
3189 represented by NODE. All structure types relevant
3190 for this function are transformed. */
3191
3192 static void
3193 do_reorg_for_func (struct cgraph_node *node)
3194 {
3195 create_new_local_vars ();
3196 create_new_alloc_sites_for_func (node);
3197 create_new_accesses_for_func ();
3198 update_ssa (TODO_update_ssa);
3199 cleanup_tree_cfg ();
3200
3201 /* Free auxiliary data representing local variables. */
3202 free_new_vars_htab (new_local_vars);
3203 }
3204
3205 /* Print structure TYPE, its name, if it exists, and body.
3206 INDENT defines the level of indentation (similar
3207 to the option -i of indent command). SHIFT parameter
3208 defines a number of spaces by which a structure will
3209 be shifted right. */
3210
3211 static void
3212 dump_struct_type (tree type, unsigned HOST_WIDE_INT indent,
3213 unsigned HOST_WIDE_INT shift)
3214 {
3215 const char *struct_name;
3216 tree field;
3217
3218 if (!type || !dump_file)
3219 return;
3220
3221 if (TREE_CODE (type) != RECORD_TYPE)
3222 {
3223 print_generic_expr (dump_file, type, 0);
3224 return;
3225 }
3226
3227 print_shift (shift);
3228 struct_name = get_type_name (type);
3229 fprintf (dump_file, "struct ");
3230 if (struct_name)
3231 fprintf (dump_file, "%s\n",struct_name);
3232 print_shift (shift);
3233 fprintf (dump_file, "{\n");
3234
3235 for (field = TYPE_FIELDS (type); field;
3236 field = TREE_CHAIN (field))
3237 {
3238 unsigned HOST_WIDE_INT s = indent;
3239 tree f_type = TREE_TYPE (field);
3240
3241 print_shift (shift);
3242 while (s--)
3243 fprintf (dump_file, " ");
3244 dump_struct_type (f_type, indent, shift + indent);
3245 fprintf(dump_file, " ");
3246 print_generic_expr (dump_file, field, 0);
3247 fprintf(dump_file, ";\n");
3248 }
3249 print_shift (shift);
3250 fprintf (dump_file, "}\n");
3251 }
3252
3253 /* This function creates new structure types to replace original type,
3254 indicated by STR->decl. The names of the new structure types are
3255 derived from the original structure type. If the original structure
3256 type has no name, we assume that its name is 'struct.<STR_NUM>'. */
3257
3258 static void
3259 create_new_type (d_str str, int *str_num)
3260 {
3261 int cluster_num = 0;
3262
3263 struct field_cluster *cluster = str->struct_clustering;
3264 while (cluster)
3265 {
3266 tree name = gen_cluster_name (str->decl, cluster_num,
3267 *str_num);
3268 tree fields;
3269 tree new_type;
3270 cluster_num++;
3271
3272 fields = create_fields (cluster, str->fields,
3273 str->num_fields);
3274 new_type = build_basic_struct (fields, name, str->decl);
3275
3276 update_fields_mapping (cluster, new_type,
3277 str->fields, str->num_fields);
3278
3279 VEC_safe_push (tree, heap, str->new_types, new_type);
3280 cluster = cluster->sibling;
3281 }
3282 (*str_num)++;
3283 }
3284
3285 /* This function is a callback for alloc_sites hashtable
3286 traversal. SLOT is a pointer to fallocs_t.
3287 This function frees memory pointed by *SLOT. */
3288
3289 static int
3290 free_falloc_sites (void **slot, void *data ATTRIBUTE_UNUSED)
3291 {
3292 fallocs_t fallocs = *(fallocs_t *) slot;
3293
3294 VEC_free (alloc_site_t, heap, fallocs->allocs);
3295 free (fallocs);
3296 return 1;
3297 }
3298
3299 /* Remove structures collected in UNSUITABLE_TYPES
3300 from structures vector. */
3301
3302 static void
3303 remove_unsuitable_types (VEC (tree, heap) *unsuitable_types)
3304 {
3305 d_str str;
3306 tree type;
3307 unsigned i, j;
3308
3309 for (j = 0; VEC_iterate (tree, unsuitable_types, j, type); j++)
3310 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3311 if (is_equal_types (str->decl, type))
3312 {
3313 remove_structure (i);
3314 break;
3315 }
3316 }
3317
3318 /* Exclude structure types with bitfields.
3319 We would not want to interfere with other optimizations
3320 that can be done in this case. The structure types with
3321 bitfields are added to UNSUITABLE_TYPES vector. */
3322
3323 static void
3324 exclude_types_with_bit_fields (VEC (tree, heap) **unsuitable_types)
3325 {
3326 d_str str;
3327 unsigned i;
3328
3329 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3330 check_bitfields (str, unsuitable_types);
3331 }
3332
3333 /* This function checks three types of escape. A structure type escapes:
3334
3335 1. if it's a type of parameter of a local function.
3336 2. if it's a type of function return value.
3337 3. if it escapes as a result of ipa-type-escape analysis.
3338
3339 The escaping structure types are added to UNSUITABLE_TYPES vector. */
3340
3341 static void
3342 exclude_escaping_types (VEC (tree, heap) **unsuitable_types)
3343 {
3344 exclude_types_passed_to_local_func (unsuitable_types);
3345 exclude_returned_types (unsuitable_types);
3346 exclude_escaping_types_1 (unsuitable_types);
3347 }
3348
3349 /* This function analyzes whether the form of
3350 structure is such that we are capable to transform it.
3351 Nested structures are checked here. Unsuitable structure
3352 types are added to UNSUITABLE_TYPE vector. */
3353
3354 static void
3355 analyze_struct_form (VEC (tree, heap) **unsuitable_types)
3356 {
3357 d_str str;
3358 unsigned i;
3359
3360 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3361 check_struct_form (str, unsuitable_types);
3362 }
3363
3364 /* This function looks for structure types instantiated in the program.
3365 The candidate types are added to the structures vector.
3366 Unsuitable types are collected into UNSUITABLE_TYPES vector. */
3367
3368 static void
3369 build_data_structure (VEC (tree, heap) **unsuitable_types)
3370 {
3371 tree var, type;
3372 tree var_list;
3373 struct varpool_node *current_varpool;
3374 struct cgraph_node *c_node;
3375
3376 /* Check global variables. */
3377 FOR_EACH_STATIC_VARIABLE (current_varpool)
3378 {
3379 var = current_varpool->decl;
3380 if (is_candidate (var, &type, unsuitable_types))
3381 add_structure (type);
3382 }
3383
3384 /* Now add structures that are types of function parameters and
3385 local variables. */
3386 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3387 {
3388 enum availability avail =
3389 cgraph_function_body_availability (c_node);
3390
3391 /* We need AVAIL_AVAILABLE for main function. */
3392 if (avail == AVAIL_LOCAL || avail == AVAIL_AVAILABLE)
3393 {
3394 struct function *fn = DECL_STRUCT_FUNCTION (c_node->decl);
3395
3396 for (var = DECL_ARGUMENTS (c_node->decl); var;
3397 var = TREE_CHAIN (var))
3398 if (is_candidate (var, &type, unsuitable_types))
3399 add_structure (type);
3400
3401 /* Check function local variables. */
3402 for (var_list = fn->unexpanded_var_list; var_list;
3403 var_list = TREE_CHAIN (var_list))
3404 {
3405 var = TREE_VALUE (var_list);
3406
3407 if (is_candidate (var, &type, unsuitable_types))
3408 add_structure (type);
3409 }
3410 }
3411 }
3412 }
3413
3414 /* This function returns true if the program contains
3415 a call to user defined allocation function, or other
3416 functions that can interfere with struct-reorg optimizations. */
3417
3418 static bool
3419 program_redefines_malloc_p (void)
3420 {
3421 struct cgraph_node *c_node;
3422 struct cgraph_node *c_node2;
3423 struct cgraph_edge *c_edge;
3424 tree fndecl;
3425 tree fndecl2;
3426 tree call_expr;
3427
3428 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3429 {
3430 fndecl = c_node->decl;
3431
3432 for (c_edge = c_node->callees; c_edge; c_edge = c_edge->next_callee)
3433 {
3434 call_expr = get_call_expr_in (c_edge->call_stmt);
3435 c_node2 = c_edge->callee;
3436 fndecl2 = c_node2->decl;
3437 if (call_expr)
3438 {
3439 const char * fname = get_name (fndecl2);
3440
3441 if ((call_expr_flags (call_expr) & ECF_MALLOC) &&
3442 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_MALLOC) &&
3443 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_CALLOC) &&
3444 (DECL_FUNCTION_CODE (fndecl2) != BUILT_IN_ALLOCA))
3445 return true;
3446
3447 /* Check that there is no __builtin_object_size,
3448 __builtin_offsetof, or realloc's in the program. */
3449 if (DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_OBJECT_SIZE
3450 || !strcmp (fname, "__builtin_offsetof")
3451 || !strcmp (fname, "realloc"))
3452 return true;
3453 }
3454 }
3455 }
3456
3457 return false;
3458 }
3459
3460 /* In this function we assume that an allocation statement
3461
3462 var = (type_cast) malloc (size);
3463
3464 is converted into the following set of statements:
3465
3466 T.1 = size;
3467 T.2 = malloc (T.1);
3468 T.3 = (type_cast) T.2;
3469 var = T.3;
3470
3471 In this function we collect into alloc_sites the allocation
3472 sites of variables of structure types that are present
3473 in structures vector. */
3474
3475 static void
3476 collect_alloc_sites (void)
3477 {
3478 struct cgraph_node *node;
3479 struct cgraph_edge *cs;
3480
3481 for (node = cgraph_nodes; node; node = node->next)
3482 if (node->analyzed && node->decl)
3483 {
3484 for (cs = node->callees; cs; cs = cs->next_callee)
3485 {
3486 tree stmt = cs->call_stmt;
3487
3488 if (stmt)
3489 {
3490 tree call = get_call_expr_in (stmt);
3491 tree decl;
3492
3493 if (call && (decl = get_callee_fndecl (call))
3494 && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3495 {
3496 unsigned i;
3497
3498 if (is_alloc_of_struct (stmt, &i))
3499 {
3500 /* We support only malloc now. */
3501 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MALLOC)
3502 {
3503 d_str str;
3504
3505 str = VEC_index (structure, structures, i);
3506 add_alloc_site (node->decl, stmt, str);
3507 }
3508 else
3509 {
3510 if (dump_file)
3511 {
3512 fprintf (dump_file,
3513 "\nUnsupported allocation function ");
3514 print_generic_stmt (dump_file, stmt, 0);
3515 }
3516 remove_structure (i);
3517 }
3518 }
3519 }
3520 }
3521 }
3522 }
3523 }
3524
3525 /* Print collected accesses. */
3526
3527 static void
3528 dump_accesses (void)
3529 {
3530 d_str str;
3531 unsigned i;
3532
3533 if (!dump_file)
3534 return;
3535
3536 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3537 dump_accs (str);
3538 }
3539
3540 /* This function checks whether the accesses of structures in condition
3541 expressions are of the kind we are capable to transform.
3542 If not, such structures are removed from the vector of structures. */
3543
3544 static void
3545 check_cond_exprs (void)
3546 {
3547 d_str str;
3548 unsigned i;
3549
3550 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3551 if (str->accs)
3552 htab_traverse (str->accs, safe_cond_expr_check, &i);
3553 }
3554
3555 /* We exclude from non-field accesses of the structure
3556 all statements that will be treated as part of the structure
3557 allocation sites or field accesses. */
3558
3559 static void
3560 exclude_alloc_and_field_accs (struct cgraph_node *node)
3561 {
3562 d_str str;
3563 unsigned i;
3564
3565 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3566 exclude_alloc_and_field_accs_1 (str, node);
3567 }
3568
3569 /* This function collects accesses of the fields of the structures
3570 that appear at function FN. */
3571
3572 static void
3573 collect_accesses_in_func (struct function *fn)
3574 {
3575 basic_block bb;
3576
3577 if (! fn)
3578 return;
3579
3580 /* Collect accesses for each basic blocks separately. */
3581 FOR_EACH_BB_FN (bb, fn)
3582 collect_accesses_in_bb (bb);
3583 }
3584
3585 /* This function summarizes counts of the fields into the structure count. */
3586
3587 static void
3588 sum_counts (d_str str, gcov_type *hotest)
3589 {
3590 int i;
3591
3592 str->count = 0;
3593 for (i = 0; i < str->num_fields; i++)
3594 {
3595 if (dump_file)
3596 {
3597 fprintf (dump_file, "\nCounter of field \"");
3598 print_generic_expr (dump_file, str->fields[i].decl, 0);
3599 fprintf (dump_file, "\" is " HOST_WIDEST_INT_PRINT_DEC,
3600 str->fields[i].count);
3601 }
3602 str->count += str->fields[i].count;
3603 }
3604
3605 if (dump_file)
3606 {
3607 fprintf (dump_file, "\nCounter of struct \"");
3608 print_generic_expr (dump_file, str->decl, 0);
3609 fprintf (dump_file, "\" is " HOST_WIDEST_INT_PRINT_DEC, str->count);
3610 }
3611
3612 if (str->count > *hotest)
3613 *hotest = str->count;
3614 }
3615
3616 /* This function peels the field into separate structure if it's
3617 sufficiently hot, i.e. if its count provides at least 90% of
3618 the maximum field count in the structure. */
3619
3620 static void
3621 peel_hot_fields (d_str str)
3622 {
3623 gcov_type max_field_count;
3624 sbitmap fields_left = sbitmap_alloc (str->num_fields);
3625 int i;
3626
3627 sbitmap_ones (fields_left);
3628 max_field_count =
3629 (gcov_type) (get_max_field_count (str)/100)*90;
3630
3631 str->struct_clustering = NULL;
3632
3633 for (i = 0; i < str->num_fields; i++)
3634 {
3635 if (str->count >= max_field_count)
3636 {
3637 RESET_BIT (fields_left, i);
3638 peel_field (i, str);
3639 }
3640 }
3641
3642 i = sbitmap_first_set_bit (fields_left);
3643 if (i != -1)
3644 gen_cluster (fields_left, str);
3645 else
3646 sbitmap_free (fields_left);
3647 }
3648
3649 /* This function is a helper for do_reorg. It goes over
3650 functions in call graph and performs actual transformation
3651 on them. */
3652
3653 static void
3654 do_reorg_1 (void)
3655 {
3656 struct cgraph_node *node;
3657
3658 /* Initialize the default bitmap obstack. */
3659 bitmap_obstack_initialize (NULL);
3660
3661 for (node = cgraph_nodes; node; node = node->next)
3662 if (node->analyzed && node->decl && !node->next_clone)
3663 {
3664 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3665 current_function_decl = node->decl;
3666 if (dump_file)
3667 fprintf (dump_file, "\nFunction to do reorg is %s: \n",
3668 (const char *) IDENTIFIER_POINTER (DECL_NAME (node->decl)));
3669 do_reorg_for_func (node);
3670 free_dominance_info (CDI_DOMINATORS);
3671 free_dominance_info (CDI_POST_DOMINATORS);
3672 current_function_decl = NULL;
3673 pop_cfun ();
3674 }
3675
3676 set_cfun (NULL);
3677 }
3678
3679 /* This function creates new global struct variables.
3680 For each original variable, the set of new variables
3681 is created with the new structure types corresponding
3682 to the structure type of original variable.
3683 Only VAR_DECL variables are treated by this function. */
3684
3685 static void
3686 create_new_global_vars (void)
3687 {
3688 struct varpool_node *current_varpool;
3689 unsigned HOST_WIDE_INT i;
3690 unsigned HOST_WIDE_INT varpool_size = 0;
3691
3692 for (i = 0; i < 2; i++)
3693 {
3694 if (i)
3695 new_global_vars = htab_create (varpool_size,
3696 new_var_hash, new_var_eq, NULL);
3697 FOR_EACH_STATIC_VARIABLE(current_varpool)
3698 {
3699 tree var_decl = current_varpool->decl;
3700
3701 if (!var_decl || TREE_CODE (var_decl) != VAR_DECL)
3702 continue;
3703 if (!i)
3704 varpool_size++;
3705 else
3706 create_new_var (var_decl, new_global_vars);
3707 }
3708 }
3709
3710 if (new_global_vars)
3711 htab_traverse (new_global_vars, update_varpool_with_new_var, NULL);
3712 }
3713
3714 /* Dump all new types generated by this optimization. */
3715
3716 static void
3717 dump_new_types (void)
3718 {
3719 d_str str;
3720 tree type;
3721 unsigned i, j;
3722
3723 if (!dump_file)
3724 return;
3725
3726 fprintf (dump_file, "\nThe following are the new types generated by"
3727 " this optimization:\n");
3728
3729 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3730 {
3731 if (dump_file)
3732 {
3733 fprintf (dump_file, "\nFor type ");
3734 dump_struct_type (str->decl, 2, 0);
3735 fprintf (dump_file, "\nthe number of new types is %d\n",
3736 VEC_length (tree, str->new_types));
3737 }
3738 for (j = 0; VEC_iterate (tree, str->new_types, j, type); j++)
3739 dump_struct_type (type, 2, 0);
3740 }
3741 }
3742
3743 /* This function creates new types to replace old structure types. */
3744
3745 static void
3746 create_new_types (void)
3747 {
3748 d_str str;
3749 unsigned i;
3750 int str_num = 0;
3751
3752 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3753 create_new_type (str, &str_num);
3754 }
3755
3756 /* Free allocation sites hashtable. */
3757
3758 static void
3759 free_alloc_sites (void)
3760 {
3761 if (alloc_sites)
3762 htab_traverse (alloc_sites, free_falloc_sites, NULL);
3763 htab_delete (alloc_sites);
3764 alloc_sites = NULL;
3765 }
3766
3767 /* This function collects structures potential
3768 for peeling transformation, and inserts
3769 them into structures hashtable. */
3770
3771 static void
3772 collect_structures (void)
3773 {
3774 VEC (tree, heap) *unsuitable_types = VEC_alloc (tree, heap, 32);
3775
3776 structures = VEC_alloc (structure, heap, 32);
3777
3778 /* If program contains user defined mallocs, we give up. */
3779 if (program_redefines_malloc_p ())
3780 return;
3781
3782 /* Build data structures hashtable of all data structures
3783 in the program. */
3784 build_data_structure (&unsuitable_types);
3785
3786 /* This function analyzes whether the form of
3787 structure is such that we are capable to transform it.
3788 Nested structures are checked here. */
3789 analyze_struct_form (&unsuitable_types);
3790
3791 /* This function excludes those structure types
3792 that escape compilation unit. */
3793 exclude_escaping_types (&unsuitable_types);
3794
3795 /* We do not want to change data layout of the structures with bitfields. */
3796 exclude_types_with_bit_fields (&unsuitable_types);
3797
3798 remove_unsuitable_types (unsuitable_types);
3799 VEC_free (tree, heap, unsuitable_types);
3800 }
3801
3802 /* Collect structure allocation sites. In case of arrays
3803 we have nothing to do. */
3804
3805 static void
3806 collect_allocation_sites (void)
3807 {
3808 alloc_sites = htab_create (32, malloc_hash, malloc_eq, NULL);
3809 collect_alloc_sites ();
3810 }
3811
3812 /* This function collects data accesses for the
3813 structures to be transformed. For each structure
3814 field it updates the count field in field_entry. */
3815
3816 static void
3817 collect_data_accesses (void)
3818 {
3819 struct cgraph_node *c_node;
3820
3821 for (c_node = cgraph_nodes; c_node; c_node = c_node->next)
3822 {
3823 enum availability avail = cgraph_function_body_availability (c_node);
3824
3825 if (avail == AVAIL_LOCAL || avail == AVAIL_AVAILABLE)
3826 {
3827 struct function *func = DECL_STRUCT_FUNCTION (c_node->decl);
3828
3829 if (!c_node->next_clone)
3830 collect_accesses_in_func (func);
3831 exclude_alloc_and_field_accs (c_node);
3832 }
3833 }
3834
3835 check_cond_exprs ();
3836 /* Print collected accesses. */
3837 dump_accesses ();
3838 }
3839
3840 /* We do not bother to transform cold structures.
3841 Coldness of the structure is defined relatively
3842 to the highest structure count among the structures
3843 to be transformed. It's triggered by the compiler parameter
3844
3845 --param struct-reorg-cold-struct-ratio=<value>
3846
3847 where <value> ranges from 0 to 100. Structures with count ratios
3848 that are less than this parameter are considered to be cold. */
3849
3850 static void
3851 exclude_cold_structs (void)
3852 {
3853 gcov_type hotest = 0;
3854 unsigned i;
3855 d_str str;
3856
3857 /* We summarize counts of fields of a structure into the structure count. */
3858 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3859 sum_counts (str, &hotest);
3860
3861 /* Remove cold structures from structures vector. */
3862 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3863 if (str->count * 100 < (hotest * STRUCT_REORG_COLD_STRUCT_RATIO))
3864 {
3865 if (dump_file)
3866 {
3867 fprintf (dump_file, "\nThe structure ");
3868 print_generic_expr (dump_file, str->decl, 0);
3869 fprintf (dump_file, " is cold.");
3870 }
3871 remove_structure (i);
3872 }
3873 }
3874
3875 /* This function decomposes original structure into substructures,
3876 i.e.clusters. */
3877
3878 static void
3879 peel_structs (void)
3880 {
3881 d_str str;
3882 unsigned i;
3883
3884 for (i = 0; VEC_iterate (structure, structures, i, str); i++)
3885 peel_hot_fields (str);
3886 }
3887
3888 /* Stage 3. */
3889 /* Do the actual transformation for each structure
3890 from the structures hashtable. */
3891
3892 static void
3893 do_reorg (void)
3894 {
3895 /* Check that there is a work to do. */
3896 if (!VEC_length (structure, structures))
3897 {
3898 if (dump_file)
3899 fprintf (dump_file, "\nNo structures to transform. Exiting...");
3900 return;
3901 }
3902 else
3903 {
3904 if (dump_file)
3905 {
3906 fprintf (dump_file, "\nNumber of structures to transform is %d",
3907 VEC_length (structure, structures));
3908 }
3909 }
3910
3911 /* Generate new types. */
3912 create_new_types ();
3913 dump_new_types ();
3914
3915 /* Create new global variables. */
3916 create_new_global_vars ();
3917 dump_new_vars (new_global_vars);
3918
3919 /* Decompose structures for each function separately. */
3920 do_reorg_1 ();
3921
3922 /* Free auxiliary data collected for global variables. */
3923 free_new_vars_htab (new_global_vars);
3924 }
3925
3926 /* Free all auxiliary data used by this optimization. */
3927
3928 static void
3929 free_data_structs (void)
3930 {
3931 free_structures ();
3932 free_alloc_sites ();
3933 }
3934
3935 /* Perform structure decomposition (peeling). */
3936
3937 static void
3938 reorg_structs (void)
3939 {
3940
3941 /* Stage1. */
3942 /* Collect structure types. */
3943 collect_structures ();
3944
3945 /* Collect structure allocation sites. */
3946 collect_allocation_sites ();
3947
3948 /* Collect structure accesses. */
3949 collect_data_accesses ();
3950
3951 /* We transform only hot structures. */
3952 exclude_cold_structs ();
3953
3954 /* Stage2. */
3955 /* Decompose structures into substructures, i.e. clusters. */
3956 peel_structs ();
3957
3958 /* Stage3. */
3959 /* Do the actual transformation for each structure
3960 from the structures hashtable. */
3961 do_reorg ();
3962
3963 /* Free all auxiliary data used by this optimization. */
3964 free_data_structs ();
3965 }
3966
3967 /* Struct-reorg optimization entry point function. */
3968
3969 static unsigned int
3970 reorg_structs_drive (void)
3971 {
3972 reorg_structs ();
3973 return 0;
3974 }
3975
3976 /* Struct-reorg optimization gate function. */
3977
3978 static bool
3979 struct_reorg_gate (void)
3980 {
3981 return flag_ipa_struct_reorg && flag_whole_program
3982 && (optimize > 0);
3983 }
3984
3985 struct tree_opt_pass pass_ipa_struct_reorg =
3986 {
3987 "ipa_struct_reorg", /* name */
3988 struct_reorg_gate, /* gate */
3989 reorg_structs_drive, /* execute */
3990 NULL, /* sub */
3991 NULL, /* next */
3992 0, /* static_pass_number */
3993 TV_INTEGRATION, /* tv_id */
3994 0, /* properties_required */
3995 0, /* properties_provided */
3996 0, /* properties_destroyed */
3997 TODO_verify_ssa, /* todo_flags_start */
3998 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
3999 0 /* letter */
4000 };