copy 'n' paste error fixed
[libfirm] / ir / opt / scalar_replace.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Scalar replacement of compounds.
23  * @author  Beyhan Veliev, Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #include "scalar_replace.h"
35 #include "irflag_t.h"
36 #include "irouts.h"
37 #include "set.h"
38 #include "pset.h"
39 #include "array.h"
40 #include "tv.h"
41 #include "ircons_t.h"
42 #include "hashptr.h"
43 #include "irgwalk.h"
44 #include "irgmod.h"
45 #include "irnode_t.h"
46 #include "irtools.h"
47 #include "xmalloc.h"
48
49 #define SET_VNUM(node, vnum) set_irn_link(node, INT_TO_PTR(vnum))
50 #define GET_VNUM(node)       (unsigned)PTR_TO_INT(get_irn_link(node))
51
52 /**
53  * A path element entry: it is either an entity
54  * or a tarval, because we evaluate only constant array
55  * accesses like a.b.c[8].d
56  */
57 typedef union {
58   ir_entity *ent;
59   tarval *tv;
60 } path_elem_t;
61
62 /**
63  * An access path, used to assign value numbers
64  * to variables that will be scalar replaced.
65  */
66 typedef struct _path_t {
67   unsigned    vnum;      /**< The value number. */
68   unsigned    path_len;  /**< The length of the access path. */
69   path_elem_t path[1];   /**< The path. */
70 } path_t;
71
72 /** The size of a path in bytes. */
73 #define PATH_SIZE(p)  (sizeof(*(p)) + sizeof((p)->path[0]) * ((p)->path_len - 1))
74
75 typedef struct _scalars_t {
76   ir_entity *ent;              /**< A entity for scalar replacement. */
77   ir_type *ent_owner;          /**< The owner of this entity. */
78 } scalars_t;
79
80
81 /**
82  * Compare two pathes.
83  *
84  * @return 0 if they are identically
85  */
86 static int path_cmp(const void *elt, const void *key, size_t size)
87 {
88   const path_t *p1 = elt;
89   const path_t *p2 = key;
90
91   /* we can use memcmp here, because identical tarvals should have identical addresses */
92   return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
93 }
94
95 /**
96  * Compare two elements of the scalars_t set.
97  *
98  * @return 0 if they are identically
99  */
100 static int ent_cmp(const void *elt, const void *key, size_t size)
101 {
102   const scalars_t *c1 = elt;
103   const scalars_t *c2 = key;
104
105   return c1->ent != c2->ent;
106 }
107
108 /**
109  * Calculate a hash value for a path.
110  */
111 static unsigned path_hash(const path_t *path)
112 {
113   unsigned hash = 0;
114   unsigned i;
115
116   for (i = 0; i < path->path_len; ++i)
117     hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
118
119   return hash >> 4;
120 }
121
122 /**
123  * Returns non-zero, if all indeces of a Sel node are constants.
124  *
125  * @param sel  the Sel node that will be checked
126  */
127 static int is_const_sel(ir_node *sel) {
128   int i, n = get_Sel_n_indexs(sel);
129
130   for (i = 0; i < n; ++i) {
131     ir_node *idx = get_Sel_index(sel, i);
132
133     if (get_irn_op(idx) != op_Const)
134       return 0;
135   }
136   return 1;
137 }
138
139 /**
140  * Check the mode of a Load/Store with the mode of the entity
141  * that is accessed.
142  * If the mode of the entity and the Load/Store mode do not match, we
143  * have the bad reinterpret case:
144  *
145  * int i;
146  * char b = *(char *)&i;
147  *
148  * We do NOT count this as one value and return address_taken
149  * in that case.
150  * However, we support an often used case. If the mode is two-complement
151  * we allow casts between signed/unsigned.
152  *
153  * @param mode     the mode of the Load/Store
154  * @param ent_mode the mode of the accessed entity
155  */
156 static int check_load_store_mode(ir_mode *mode, ir_mode *ent_mode) {
157   if (ent_mode != mode) {
158     if (ent_mode == NULL ||
159         get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
160         get_mode_sort(ent_mode) != get_mode_sort(mode) ||
161         get_mode_arithmetic(ent_mode) != irma_twos_complement ||
162         get_mode_arithmetic(mode) != irma_twos_complement)
163       return 0;
164   }
165   return 1;
166 }
167
168 /*
169  * Returns non-zero, if the address of an entity
170  * represented by a Sel node (or it's successor Sels) is taken.
171  */
172 int is_address_taken(ir_node *sel)
173 {
174   int     i;
175   ir_mode *emode, *mode;
176   ir_node *value;
177   ir_entity *ent;
178
179   if (! is_const_sel(sel))
180     return 1;
181
182   for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
183     ir_node *succ = get_irn_out(sel, i);
184
185     switch (get_irn_opcode(succ)) {
186     case iro_Load:
187       /* check if this load is not a hidden conversion */
188       mode = get_Load_mode(succ);
189       ent = get_Sel_entity(sel);
190       emode = get_type_mode(get_entity_type(ent));
191       if (! check_load_store_mode(mode, emode))
192         return 1;
193       break;
194
195     case iro_Store:
196       /* check that Sel is not the Store's value */
197       value = get_Store_value(succ);
198       if (value == sel)
199         return 1;
200       /* check if this Store is not a hidden conversion */
201       mode = get_irn_mode(value);
202       ent = get_Sel_entity(sel);
203       emode = get_type_mode(get_entity_type(ent));
204       if (! check_load_store_mode(mode, emode))
205         return 1;
206       break;
207
208     case iro_Sel: {
209       /* Check the Sel successor of Sel */
210       int res = is_address_taken(succ);
211
212       if (res)
213         return 1;
214       break;
215     }
216
217     case iro_Call:
218       /* The address of an entity is given as a parameter.
219        * As long as we do not have analyses that can tell what
220        * is done with parameters, think is taken.
221        */
222       return 1;
223
224     default:
225       /* another op, the address is taken */
226       return 1;
227     }
228   }
229   return 0;
230 }
231
232 /**
233  * Link all leave Sels with the entity.
234  *
235  * @param ent  the entity that will be scalar replaced
236  * @param sel  a Sel node that selects some fields of this entity
237  */
238 static void link_all_leave_sels(ir_entity *ent, ir_node *sel)
239 {
240   int i, n, flag = 1;
241
242   n = get_irn_n_outs(sel);
243   for (i = 0; i < n; ++i) {
244     ir_node *succ = get_irn_out(sel, i);
245
246     if (is_Sel(succ)) {
247       link_all_leave_sels(ent, succ);
248       flag = 0;
249     }
250   }
251
252   if (flag) {
253     /* if Sel nodes with memory inputs are used, a entity can be
254      * visited more than once causing a ring here, so we use the
255      * node flag to mark linked nodes
256      */
257     if (irn_visited(sel))
258       return;
259
260     /* we know we are at a leave, because this function is only
261      * called if the address is NOT taken, so succ must be a Load
262      * or a Store node
263      */
264     set_irn_link(sel, get_entity_link(ent));
265     set_entity_link(ent, sel);
266
267     mark_irn_visited(sel);
268   }
269 }
270
271 /* we need a special address that serves as an address taken marker */
272 static char _x;
273 static void *ADDRESS_TAKEN = &_x;
274
275 /**
276  * Find possible scalar replacements.
277  *
278  * @param irg  an IR graph
279  *
280  * This function finds variables on the (members of the) frame type
281  * that can be scalar replaced, because their address is never taken.
282  * If such a variable is found, it's entity link will hold a list of all
283  * Sel nodes, that selects the atomic fields of this entity.
284  * Otherwise, the link will be ADDRESS_TAKEN or NULL.
285  *
286  * @return  non-zero if at least one entity could be replaced
287  *          potentially
288  */
289 static int find_possible_replacements(ir_graph *irg)
290 {
291   ir_node *irg_frame = get_irg_frame(irg);
292   int i, n;
293   int res = 0;
294
295   inc_irg_visited(irg);
296
297   n = get_irn_n_outs(irg_frame);
298
299   /*
300    * First, clear the link field of all interesting entities.
301    * Note that we did not rely on the fact that there is only
302    * one Sel node per entity, so we might access one entity
303    * more than once here.
304    * That's why we have need two loops.
305    */
306   for (i = 0; i < n; ++i) {
307     ir_node *succ = get_irn_out(irg_frame, i);
308
309     if (is_Sel(succ)) {
310       ir_entity *ent = get_Sel_entity(succ);
311       set_entity_link(ent, NULL);
312     }
313   }
314
315   /*
316    * Check the ir_graph for Sel nodes. If the entity of Sel
317    * isn't a scalar replacement set the link of this entity
318    * equal ADDRESS_TAKEN.
319    */
320   for (i = 0; i < n; ++i) {
321     ir_node *succ = get_irn_out(irg_frame, i);
322
323     if (is_Sel(succ)) {
324       ir_entity *ent = get_Sel_entity(succ);
325       ir_type *ent_type;
326
327       if (get_entity_link(ent) == ADDRESS_TAKEN)
328         continue;
329
330       /*
331        * Beware: in rare cases even entities on the frame might be
332        * volatile. This might happen if the entity serves as a store
333        * to a value that must survive a exception. Do not optimize
334        * such entities away.
335        */
336       if (get_entity_volatility(ent) == volatility_is_volatile) {
337         set_entity_link(ent, ADDRESS_TAKEN);
338         continue;
339       }
340
341       ent_type = get_entity_type(ent);
342
343       /* we can handle arrays, structs and atomic types yet */
344       if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
345         if (is_address_taken(succ)) {
346           if (get_entity_link(ent)) /* killing one */
347             --res;
348           set_entity_link(ent, ADDRESS_TAKEN);
349         }
350         else {
351           /* possible found one */
352           if (get_entity_link(ent) == NULL)
353             ++res;
354           link_all_leave_sels(ent, succ);
355         }
356       }
357     }
358   }
359
360   return res;
361 }
362
363 /**
364  * Return a path from the Sel node sel to it's root.
365  *
366  * @param sel  the Sel node
367  * @param len  the length of the path so far
368  */
369 static path_t *find_path(ir_node *sel, unsigned len)
370 {
371   int pos, i, n;
372   path_t *res;
373   ir_node *pred = get_Sel_ptr(sel);
374
375   /* the current Sel node will add some path elements */
376   n    = get_Sel_n_indexs(sel);
377   len += n + 1;
378
379   if (! is_Sel(pred)) {
380     /* we found the root */
381
382     res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
383     res->path_len = len;
384   }
385   else
386     res = find_path(pred, len);
387
388   pos = res->path_len - len;
389
390   res->path[pos++].ent = get_Sel_entity(sel);
391   for (i = 0; i < n; ++i) {
392     ir_node *index = get_Sel_index(sel, i);
393
394     res->path[pos++].tv = get_Const_tarval(index);
395   }
396   return res;
397 }
398
399
400 /**
401  * Allocate value numbers for the leaves
402  * in our found entities.
403  *
404  * @param sels  a set that will contain all Sels that have a value number
405  * @param ent   the entity that will be scalar replaced
406  * @param vnum  the first value number we can assign
407  * @param modes a flexible array, containing all the modes of
408  *              the value numbers.
409  *
410  * @return the next free value number
411  */
412 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
413 {
414   ir_node *sel, *next;
415   path_t *key, *path;
416   set *pathes = new_set(path_cmp, 8);
417
418   /* visit all Sel nodes in the chain of the entity */
419   for (sel = get_entity_link(ent); sel; sel = next) {
420     next = get_irn_link(sel);
421
422     /* we must mark this sel for later */
423     pset_insert_ptr(sels, sel);
424
425     key  = find_path(sel, 0);
426     path = set_find(pathes, key, PATH_SIZE(key), path_hash(key));
427
428     if (path)
429       SET_VNUM(sel, path->vnum);
430     else {
431       key->vnum = vnum++;
432
433       set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
434
435       SET_VNUM(sel, key->vnum);
436       ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15);
437
438       (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
439
440       assert((*modes)[key->vnum] && "Value is not atomic");
441
442 #ifdef DEBUG_libfirm
443       /* Debug output */
444       if (get_opt_scalar_replacement_verbose() && get_firm_verbosity() > 1) {
445                 unsigned i;
446         printf("  %s", get_entity_name(key->path[0].ent));
447         for (i = 1; i < key->path_len; ++i) {
448           if (is_entity(key->path[i].ent))
449             printf(".%s", get_entity_name(key->path[i].ent));
450           else
451             printf("[%ld]", get_tarval_long(key->path[i].tv));
452         }
453         printf(" = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum]));
454       }
455 #endif /* DEBUG_libfirm */
456     }
457     free(key);
458   }
459
460   del_set(pathes);
461   set_entity_link(ent, NULL);
462   return vnum;
463 }
464
465 /**
466  * A list entry for the fixing lists
467  */
468 typedef struct _list_entry_t {
469   ir_node  *node;   /**< the node that must be fixed */
470   unsigned vnum;    /**< the value number of this node */
471 } list_entry_t;
472
473 /**
474  * environment for memory walker
475  */
476 typedef struct _env_t {
477   struct obstack obst;      /**< a obstack for the value blocks */
478   int          nvals;       /**< number of values */
479   ir_mode      **modes;     /**< the modes of the values */
480   list_entry_t *fix_phis;   /**< list of all Phi nodes that must be fixed */
481   list_entry_t *fix_loads;  /**< list of all Load nodes that must be fixed */
482   pset         *sels;       /**< A set of all Sel nodes that have a value number */
483 } env_t;
484
485 /**
486  * topological walker.
487  */
488 static void topologic_walker(ir_node *node, void *ctx)
489 {
490   env_t        *env = ctx;
491   ir_op        *op = get_irn_op(node);
492   ir_node      *adr, *block, *mem, *unk, **value_arr, **in, *val;
493   ir_mode      *mode;
494   unsigned     vnum;
495   int          i, j, n;
496   list_entry_t *l;
497
498   if (op == op_Load) {
499     /* a load, check if we can resolve it */
500     adr = get_Load_ptr(node);
501
502     if (! is_Sel(adr))
503       return;
504
505     if (! pset_find_ptr(env->sels, adr))
506       return;
507
508     /* ok, we have a Load that will be replaced */
509     vnum = GET_VNUM(adr);
510
511     assert(vnum < (unsigned)env->nvals);
512
513     block     = get_nodes_block(node);
514     value_arr = get_irn_link(block);
515
516     /* check, if we can replace this Load */
517     if (value_arr[vnum]) {
518       mem = get_Load_mem(node);
519
520       /* Beware: A Load can contain a hidden conversion in Firm.
521          This happens for instance in the following code:
522
523          int i;
524          unsigned j = *(unsigned *)&i;
525
526          Handle this here. */
527       val = value_arr[vnum];
528       mode = get_Load_mode(node);
529       if (mode != get_irn_mode(val))
530         val = new_d_Conv(get_irn_dbg_info(node), val, mode);
531
532       turn_into_tuple(node, pn_Load_max);
533       set_Tuple_pred(node, pn_Load_M,         mem);
534       set_Tuple_pred(node, pn_Load_res,       val);
535       set_Tuple_pred(node, pn_Load_X_regular, new_r_Jmp(current_ir_graph, block));
536       set_Tuple_pred(node, pn_Load_X_except,  new_Bad());
537     } else {
538       l = obstack_alloc(&env->obst, sizeof(*l));
539       l->node = node;
540       l->vnum = vnum;
541
542       set_irn_link(node, env->fix_loads);
543       env->fix_loads = l;
544     }
545   } else if (op == op_Store) {
546     /* a Store always can be replaced */
547     adr = get_Store_ptr(node);
548
549     if (! is_Sel(adr))
550       return;
551
552     if (! pset_find_ptr(env->sels, adr))
553       return;
554
555     vnum = GET_VNUM(adr);
556
557     assert(vnum < (unsigned)env->nvals);
558
559     block     = get_nodes_block(node);
560     value_arr = get_irn_link(block);
561
562     /* Beware: A Store can contain a hidden conversion in Firm. */
563     val = get_Store_value(node);
564     if (get_irn_mode(val) != env->modes[vnum])
565       val = new_d_Conv(get_irn_dbg_info(node), val, env->modes[vnum]);
566     value_arr[vnum] = val;
567
568     mem = get_Store_mem(node);
569         block = get_nodes_block(node);
570
571     turn_into_tuple(node, pn_Store_max);
572     set_Tuple_pred(node, pn_Store_M,         mem);
573     set_Tuple_pred(node, pn_Store_X_regular, new_r_Jmp(current_ir_graph, block));
574     set_Tuple_pred(node, pn_Store_X_except,  new_Bad());
575   } else if (op == op_Phi && get_irn_mode(node) == mode_M) {
576     /*
577      * found a memory Phi: Here, we must create new Phi nodes
578      */
579     block     = get_nodes_block(node);
580     value_arr = get_irn_link(block);
581
582     n = get_Block_n_cfgpreds(block);
583
584     in = alloca(sizeof(*in) * n);
585
586     for (i = env->nvals - 1; i >= 0; --i) {
587       unk = new_Unknown(env->modes[i]);
588       for (j = n - 1; j >= 0; --j)
589         in[j] = unk;
590
591       value_arr[i] = new_r_Phi(current_ir_graph, block, n, in, env->modes[i]);
592
593       l = obstack_alloc(&env->obst, sizeof(*l));
594       l->node = value_arr[i];
595       l->vnum = i;
596
597       set_irn_link(value_arr[i], env->fix_phis);
598       env->fix_phis = l;
599     }
600   }
601 }
602
603 /**
604  * Walker: allocate the value array for every block.
605  */
606 static void alloc_value_arr(ir_node *block, void *ctx)
607 {
608   env_t *env = ctx;
609   ir_node **var_arr = obstack_alloc(&env->obst, sizeof(*var_arr) * env->nvals);
610
611   /* the value array is empty at start */
612   memset(var_arr, 0, sizeof(*var_arr) * env->nvals);
613   set_irn_link(block, var_arr);
614 }
615
616 /**
617  * searches through blocks beginning from block for value
618  * vnum and return it.
619  */
620 static ir_node *find_vnum_value(ir_node *block, unsigned vnum)
621 {
622   ir_node **value_arr;
623   int i;
624   ir_node *res;
625
626   if (Block_not_block_visited(block)) {
627     mark_Block_block_visited(block);
628
629     value_arr = get_irn_link(block);
630
631     if (value_arr[vnum])
632       return value_arr[vnum];
633
634     for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
635       ir_node *pred = get_Block_cfgpred(block, i);
636
637       res = find_vnum_value(get_nodes_block(pred), vnum);
638       if (res)
639         return res;
640     }
641   }
642   return NULL;
643 }
644
645 /**
646  * fix the Phi list
647  */
648 static void fix_phis(env_t *env)
649 {
650   list_entry_t *l;
651   ir_node      *phi, *block, *pred, *val;
652   int          i;
653
654   for (l = env->fix_phis; l; l = get_irn_link(phi)) {
655     phi = l->node;
656
657     block = get_nodes_block(phi);
658     for (i = get_irn_arity(phi) - 1; i >= 0; --i) {
659       pred = get_Block_cfgpred(block, i);
660       pred = get_nodes_block(pred);
661
662       inc_irg_block_visited(current_ir_graph);
663       val = find_vnum_value(pred, l->vnum);
664
665       if (val)
666         set_irn_n(phi, i, val);
667     }
668   }
669 }
670
671 /**
672  * fix the Load list
673  */
674 static void fix_loads(env_t *env)
675 {
676   list_entry_t *l;
677   ir_node      *load, *block, *pred, *val = NULL, *mem;
678   ir_mode      *mode;
679   int          i;
680
681   for (l = env->fix_loads; l; l = get_irn_link(load)) {
682     load = l->node;
683
684     block = get_nodes_block(load);
685     for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
686       pred = get_Block_cfgpred(block, i);
687       pred = get_nodes_block(pred);
688
689       inc_irg_block_visited(current_ir_graph);
690       val = find_vnum_value(pred, l->vnum);
691
692       if (val)
693         break;
694     }
695
696     if (! val) {
697       /* access of an uninitialized value */
698       val = new_Unknown(env->modes[l->vnum]);
699     }
700
701     /* Beware: A Load can contain a hidden conversion in Firm.
702        Handle this here. */
703     mode = get_Load_mode(load);
704     if (mode != get_irn_mode(val))
705       val = new_d_Conv(get_irn_dbg_info(load), val, mode);
706
707             mem = get_Load_mem(load);
708
709     turn_into_tuple(load, pn_Load_max);
710     set_Tuple_pred(load, pn_Load_M,         mem);
711     set_Tuple_pred(load, pn_Load_res,       val);
712     set_Tuple_pred(load, pn_Load_X_regular, new_r_Jmp(current_ir_graph, block));
713     set_Tuple_pred(load, pn_Load_X_except,  new_Bad());
714   }
715 }
716
717 /**
718  *  Make scalar replacement.
719  *
720  * @param sels    A set containing all Sel nodes that have a value number
721  * @param nvals   The number of scalars.
722  * @param modes   A flexible array, containing all the modes of
723  *                the value numbers.
724  */
725 static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes)
726 {
727   env_t env;
728
729   obstack_init(&env.obst);
730   env.nvals     = nvals;
731   env.modes     = modes;
732   env.fix_phis  = NULL;
733   env.fix_loads = NULL;
734   env.sels      = sels;
735
736   /* first step: allocate the value arrays for every block */
737   irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
738
739   /*
740    * second step: walk over the graph blockwise in topological order
741    * and fill the array as much as possible.
742    */
743   irg_walk_blkwise_graph(current_ir_graph, NULL, topologic_walker, &env);
744
745   /* third, fix the list of Phis, then the list of Loads */
746   fix_phis(&env);
747   fix_loads(&env);
748
749   obstack_free(&env.obst, NULL);
750 }
751
752 /*
753  * Find possible scalar replacements
754  *
755  * @param irg  The current ir graph.
756  */
757 void scalar_replacement_opt(ir_graph *irg)
758 {
759   unsigned  nvals;
760   int       i;
761   scalars_t key, *value;
762   ir_node   *irg_frame;
763   ir_mode   **modes;
764   set       *set_ent;
765   pset      *sels;
766   ir_type   *ent_type;
767   ir_graph  *rem;
768
769   if (! get_opt_scalar_replacement())
770     return;
771
772   rem = current_ir_graph;
773
774   /* Call algorithm that computes the out edges */
775   assure_irg_outs(irg);
776
777   /* Find possible scalar replacements */
778   if (find_possible_replacements(irg)) {
779
780     if (get_opt_scalar_replacement_verbose()) {
781       printf("Scalar Replacement: %s\n", get_entity_name(get_irg_entity(irg)));
782     }
783
784     /* Insert in set the scalar replacements. */
785     irg_frame = get_irg_frame(irg);
786     nvals = 0;
787     modes = NEW_ARR_F(ir_mode *, 16);
788     set_ent = new_set(ent_cmp, 8);
789     sels    = pset_new_ptr(8);
790
791     for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
792       ir_node *succ = get_irn_out(irg_frame, i);
793
794       if (is_Sel(succ)) {
795         ir_entity *ent = get_Sel_entity(succ);
796
797         if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
798           continue;
799
800         ent_type = get_entity_type(ent);
801
802         key.ent       = ent;
803         key.ent_owner = get_entity_owner(ent);
804         set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
805
806         if (get_opt_scalar_replacement_verbose()) {
807           if (is_Array_type(ent_type)) {
808             printf("  found array %s\n", get_entity_name(ent));
809           }
810           else if (is_Struct_type(ent_type)) {
811             printf("  found struct %s\n", get_entity_name(ent));
812           }
813           else if (is_atomic_type(ent_type))
814             printf("  found atomic value %s\n", get_entity_name(ent));
815           else {
816             assert(0 && "Neither an array nor a struct or atomic value");
817           }
818         }
819
820         nvals = allocate_value_numbers(sels, ent, nvals, &modes);
821       }
822     }
823
824     if (get_opt_scalar_replacement_verbose()) {
825       printf("  %u values will be needed\n", nvals);
826     }
827
828     /* If scalars were found. */
829     if (nvals) {
830       do_scalar_replacements(sels, nvals, modes);
831
832       for (value = set_first(set_ent); value; value = set_next(set_ent)) {
833         remove_class_member(value->ent_owner, value->ent);
834       }
835     }
836
837     del_pset(sels);
838     del_set(set_ent);
839     DEL_ARR_F(modes);
840
841     if (nvals) {
842       /*
843        * We changed the graph, but did NOT introduce new blocks
844        * neither changed control flow, cf-backedges should be still
845        * consistent.
846        */
847       set_irg_outs_inconsistent(irg);
848       set_irg_loopinfo_inconsistent(irg);
849     }
850   }
851
852   current_ir_graph = rem;
853 }