OSDN Git Service

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