OSDN Git Service

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