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