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