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