OSDN Git Service

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