OSDN Git Service

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