- moved pass constructors from irtools to irpass
[libfirm] / ir / opt / scalar_replace.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
27
28 #include <string.h>
29
30 #include "iroptimize.h"
31 #include "scalar_replace.h"
32 #include "irflag_t.h"
33 #include "irouts.h"
34 #include "set.h"
35 #include "pset.h"
36 #include "array.h"
37 #include "tv.h"
38 #include "ircons_t.h"
39 #include "hashptr.h"
40 #include "irgwalk.h"
41 #include "irgmod.h"
42 #include "irnode_t.h"
43 #include "irpass.h"
44 #include "irtools.h"
45 #include "xmalloc.h"
46 #include "debug.h"
47 #include "error.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 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
81
82 /**
83  * Compare two pathes.
84  *
85  * @return 0 if they are identically
86  */
87 static int path_cmp(const void *elt, const void *key, size_t size) {
88         const path_t *p1 = elt;
89         const path_t *p2 = key;
90         (void) size;
91
92         /* we can use memcmp here, because identical tarvals should have identical addresses */
93         return memcmp(p1->path, p2->path, p1->path_len * sizeof(p1->path[0]));
94 }
95
96 /**
97  * Compare two elements of the scalars_t set.
98  *
99  * @return 0 if they are identically
100  */
101 static int ent_cmp(const void *elt, const void *key, size_t size) {
102         const scalars_t *c1 = elt;
103         const scalars_t *c2 = key;
104         (void) size;
105
106         return c1->ent != c2->ent;
107 }
108
109 /**
110  * Calculate a hash value for a path.
111  */
112 static unsigned path_hash(const path_t *path) {
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 (!is_Const(idx))
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, input_nr, k;
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                         /* do not remove volatile variables */
188                         if (get_Load_volatility(succ) == volatility_is_volatile)
189                                 return 1;
190                         /* check if this load is not a hidden conversion */
191                         mode = get_Load_mode(succ);
192                         ent = get_Sel_entity(sel);
193                         emode = get_type_mode(get_entity_type(ent));
194                         if (! check_load_store_mode(mode, emode))
195                                 return 1;
196                         break;
197
198                 case iro_Store:
199                         /* check that Sel is not the Store's value */
200                         value = get_Store_value(succ);
201                         if (value == sel)
202                                 return 1;
203                         /* do not remove volatile variables */
204                         if (get_Store_volatility(succ) == volatility_is_volatile)
205                                 return 1;
206                         /* check if this Store is not a hidden conversion */
207                         mode = get_irn_mode(value);
208                         ent = get_Sel_entity(sel);
209                         emode = get_type_mode(get_entity_type(ent));
210                         if (! check_load_store_mode(mode, emode))
211                                 return 1;
212                         break;
213
214                 case iro_Sel: {
215                         int       res;
216                         ir_entity *entity = get_Sel_entity(succ);
217                         /* we can't handle unions correctly yet -> address taken */
218                         if (is_Union_type(get_entity_owner(entity)))
219                                 return 1;
220
221                         /* Check the Sel successor of Sel */
222                         res = is_address_taken(succ);
223                         if (res)
224                                 return 1;
225                         break;
226                 }
227
228                 case iro_Call:
229                         /* The address of an entity is given as a parameter.
230                          * As long as we do not have analyses that can tell what
231                          * is done with parameters, think is taken.
232                          * One special case: If the Call type tells that it's a
233                          * value parameter, the address is NOT taken.
234                          */
235                         return 1;
236
237                 case iro_Id: {
238                         int res = is_address_taken(succ);
239                         if (res)
240                                 return 1;
241                         break;
242                 }
243
244                 case iro_Tuple:
245                         /* Non-optimized Tuple, happens in inlining */
246                         for (input_nr = get_Tuple_n_preds(succ) - 1; input_nr >= 0; --input_nr) {
247                                 ir_node *pred = get_Tuple_pred(succ, input_nr);
248
249                                 if (pred == sel) {
250                                         /* we found one input */
251                                         for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
252                                                 ir_node *proj = get_irn_out(succ, k);
253
254                                                 if (is_Proj(proj) && get_Proj_proj(proj) == input_nr) {
255                                                         int res = is_address_taken(proj);
256                                                         if (res)
257                                                                 return 1;
258                                                 }
259                                         }
260                                 }
261                         }
262                         break;
263
264                 default:
265                         /* another op, the address is taken */
266                         return 1;
267                 }
268         }
269         return 0;
270 }
271
272 /**
273  * Link all leave Sels with the entity.
274  *
275  * @param ent  the entity that will be scalar replaced
276  * @param sel  a Sel node that selects some fields of this entity
277  */
278 static int link_all_leave_sels(ir_entity *ent, ir_node *sel) {
279         int i, is_leave = 1;
280
281         for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
282                 ir_node *succ = get_irn_out(sel, i);
283
284                 if (is_Sel(succ)) {
285                         /* the current leave has further Sel's, no leave */
286                         is_leave = 0;
287                         link_all_leave_sels(ent, succ);
288                 } else if (is_Id(succ)) {
289                         is_leave &= link_all_leave_sels(ent, succ);
290                 }
291         }
292
293         if (is_leave) {
294                 /* beware of Id's */
295                 sel = skip_Id(sel);
296
297                 /* we know we are at a leave, because this function is only
298                  * called if the address is NOT taken, so sel's successor(s)
299                  * must be Loads or Stores
300                  */
301                 set_irn_link(sel, get_entity_link(ent));
302                 set_entity_link(ent, sel);
303         }
304         return is_leave;
305 }
306
307 /* we need a special address that serves as an address taken marker */
308 static char _x;
309 static void *ADDRESS_TAKEN = &_x;
310
311 /**
312  * Find possible scalar replacements.
313  *
314  * @param irg  an IR graph
315  *
316  * This function finds variables on the (members of the) frame type
317  * that can be scalar replaced, because their address is never taken.
318  * If such a variable is found, it's entity link will hold a list of all
319  * Sel nodes, that selects the atomic fields of this entity.
320  * Otherwise, the link will be ADDRESS_TAKEN or NULL.
321  *
322  * @return  non-zero if at least one entity could be replaced
323  *          potentially
324  */
325 static int find_possible_replacements(ir_graph *irg) {
326         ir_node *irg_frame;
327         ir_type *frame_tp;
328         int     i, j, k, static_link_arg;
329         int     res = 0;
330
331         /*
332          * First, clear the link field of all interesting entities.
333          */
334         frame_tp = get_irg_frame_type(irg);
335         for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
336                 ir_entity *ent = get_class_member(frame_tp, i);
337                 set_entity_link(ent, NULL);
338         }
339
340         /* check for inner functions:
341          * FIXME: need a way to get the argument position for the static link */
342         static_link_arg = 0;
343         for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
344                 ir_entity *ent = get_class_member(frame_tp, i);
345                 if (is_method_entity(ent)) {
346                         ir_graph *inner_irg = get_entity_irg(ent);
347                         ir_node  *args;
348
349                         assure_irg_outs(inner_irg);
350                         args = get_irg_args(inner_irg);
351                         for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
352                                 ir_node *arg = get_irn_out(args, j);
353
354                                 if (get_Proj_proj(arg) == static_link_arg) {
355                                         for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
356                                                 ir_node *succ = get_irn_out(arg, k);
357
358                                                 if (is_Sel(succ)) {
359                                                         ir_entity *ent = get_Sel_entity(succ);
360
361                                                         if (get_entity_owner(ent) == frame_tp) {
362                                                                 /* found an access to the outer frame */
363                                                                 set_entity_link(ent, ADDRESS_TAKEN);
364                                                         }
365                                                 }
366                                         }
367                                 }
368                         }
369                 }
370         }
371
372         /*
373          * Check the ir_graph for Sel nodes. If the entity of Sel
374          * isn't a scalar replacement set the link of this entity
375          * equal ADDRESS_TAKEN.
376          */
377         irg_frame = get_irg_frame(irg);
378         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
379                 ir_node *succ = get_irn_out(irg_frame, i);
380
381                 if (is_Sel(succ)) {
382                         ir_entity *ent = get_Sel_entity(succ);
383                         ir_type *ent_type;
384
385                         /* we are only interested in entities on the frame, NOT
386                            on the value type */
387                         if (get_entity_owner(ent) != frame_tp)
388                                 continue;
389
390                         if (get_entity_link(ent) == ADDRESS_TAKEN)
391                                 continue;
392
393                         /*
394                          * Beware: in rare cases even entities on the frame might be
395                          * volatile. This might happen if the entity serves as a store
396                          * to a value that must survive a exception. Do not optimize
397                          * such entities away.
398                          */
399                         if (get_entity_volatility(ent) == volatility_is_volatile) {
400                                 set_entity_link(ent, ADDRESS_TAKEN);
401                                 continue;
402                         }
403
404                         ent_type = get_entity_type(ent);
405
406                         /* we can handle arrays, structs and atomic types yet */
407                         if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
408                                 if (is_address_taken(succ)) {
409                                          /* killing one */
410                                         if (get_entity_link(ent))
411                                                 --res;
412                                         set_entity_link(ent, ADDRESS_TAKEN);
413                                 } else {
414                                         /* possible found one */
415                                         if (get_entity_link(ent) == NULL)
416                                                 ++res;
417                                         link_all_leave_sels(ent, succ);
418                                 }
419                         }
420                 }
421         }
422
423         return res;
424 }
425
426 /**
427  * Return a path from the Sel node sel to it's root.
428  *
429  * @param sel  the Sel node
430  * @param len  the length of the path so far
431  */
432 static path_t *find_path(ir_node *sel, unsigned len) {
433         int pos, i, n;
434         path_t *res;
435         ir_node *pred = get_Sel_ptr(sel);
436
437         /* the current Sel node will add some path elements */
438         n    = get_Sel_n_indexs(sel);
439         len += n + 1;
440
441         if (! is_Sel(pred)) {
442                 /* we found the root */
443                 res = XMALLOCF(path_t, path, len);
444                 res->path_len = len;
445         } else
446                 res = find_path(pred, len);
447
448         pos = res->path_len - len;
449
450         res->path[pos++].ent = get_Sel_entity(sel);
451         for (i = 0; i < n; ++i) {
452                 ir_node *index = get_Sel_index(sel, i);
453
454                 res->path[pos++].tv = get_Const_tarval(index);
455         }
456         return res;
457 }
458
459
460 /**
461  * Allocate value numbers for the leaves
462  * in our found entities.
463  *
464  * @param sels  a set that will contain all Sels that have a value number
465  * @param ent   the entity that will be scalar replaced
466  * @param vnum  the first value number we can assign
467  * @param modes a flexible array, containing all the modes of
468  *              the value numbers.
469  *
470  * @return the next free value number
471  */
472 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
473 {
474         ir_node *sel, *next;
475         path_t *key, *path;
476         set *pathes = new_set(path_cmp, 8);
477
478         DB((dbg, SET_LEVEL_3, "  Visiting Sel nodes of entity %+F\n", ent));
479         /* visit all Sel nodes in the chain of the entity */
480         for (sel = get_entity_link(ent); sel; sel = next) {
481                 next = get_irn_link(sel);
482
483                 /* we must mark this sel for later */
484                 pset_insert_ptr(sels, sel);
485
486                 key  = find_path(sel, 0);
487                 path = set_find(pathes, key, PATH_SIZE(key), path_hash(key));
488
489                 if (path) {
490                         SET_VNUM(sel, path->vnum);
491                         DB((dbg, SET_LEVEL_3, "  %+F represents value %u\n", sel, path->vnum));
492                 } else {
493                         key->vnum = vnum++;
494
495                         set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
496
497                         SET_VNUM(sel, key->vnum);
498                         DB((dbg, SET_LEVEL_3, "  %+F represents value %u\n", sel, key->vnum));
499
500                         ARR_EXTO(ir_mode *, *modes, (int)((key->vnum + 15) & ~15));
501
502                         (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
503
504                         assert((*modes)[key->vnum] && "Value is not atomic");
505
506 #ifdef DEBUG_libfirm
507                         /* Debug output */
508                         {
509                                 unsigned i;
510                                 DB((dbg, SET_LEVEL_2, "  %s", get_entity_name(key->path[0].ent)));
511                                 for (i = 1; i < key->path_len; ++i) {
512                                         if (is_entity(key->path[i].ent))
513                                                 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
514                                         else
515                                                 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
516                                 }
517                                 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
518                         }
519 #endif /* DEBUG_libfirm */
520                 }
521                 free(key);
522         }
523
524         del_set(pathes);
525         set_entity_link(ent, NULL);
526         return vnum;
527 }
528
529 /**
530  * A list entry for the fixing lists
531  */
532 typedef struct _list_entry_t {
533         ir_node  *node;   /**< the node that must be fixed */
534         unsigned vnum;    /**< the value number of this node */
535 } list_entry_t;
536
537 /**
538  * environment for memory walker
539  */
540 typedef struct _env_t {
541         int          nvals;       /**< number of values */
542         ir_mode      **modes;     /**< the modes of the values */
543         pset         *sels;       /**< A set of all Sel nodes that have a value number */
544 } env_t;
545
546 /**
547  * topological post-walker.
548  */
549 static void topologic_walker(ir_node *node, void *ctx) {
550         env_t        *env = ctx;
551         ir_node      *adr, *block, *mem, *val;
552         ir_mode      *mode;
553         unsigned     vnum;
554
555         if (is_Load(node)) {
556                 /* a load, check if we can resolve it */
557                 adr = get_Load_ptr(node);
558
559                 DB((dbg, SET_LEVEL_3, "  checking %+F for replacement ", node));
560                 if (! is_Sel(adr)) {
561                         DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
562                         return;
563                 }
564
565                 if (! pset_find_ptr(env->sels, adr)) {
566                         DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
567                         return;
568                 }
569
570                 /* ok, we have a Load that will be replaced */
571                 vnum = GET_VNUM(adr);
572                 assert(vnum < (unsigned)env->nvals);
573
574                 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
575
576                 block = get_nodes_block(node);
577                 set_cur_block(block);
578
579                 /* check, if we can replace this Load */
580                 val = get_value(vnum, env->modes[vnum]);
581
582                 /* Beware: A Load can contain a hidden conversion in Firm.
583                 This happens for instance in the following code:
584
585                  int i;
586                  unsigned j = *(unsigned *)&i;
587
588                 Handle this here. */
589                 mode = get_Load_mode(node);
590                 if (mode != get_irn_mode(val))
591                         val = new_d_Conv(get_irn_dbg_info(node), val, mode);
592
593                 mem = get_Load_mem(node);
594                 turn_into_tuple(node, pn_Load_max);
595                 set_Tuple_pred(node, pn_Load_M,         mem);
596                 set_Tuple_pred(node, pn_Load_res,       val);
597                 set_Tuple_pred(node, pn_Load_X_regular, new_Jmp());
598                 set_Tuple_pred(node, pn_Load_X_except,  new_Bad());
599         } else if (is_Store(node)) {
600                 DB((dbg, SET_LEVEL_3, "  checking %+F for replacement ", node));
601
602                 /* a Store always can be replaced */
603                 adr = get_Store_ptr(node);
604
605                 if (! is_Sel(adr)) {
606                         DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
607                         return;
608                 }
609
610                 if (! pset_find_ptr(env->sels, adr)) {
611                         DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
612                         return;
613                 }
614
615                 vnum = GET_VNUM(adr);
616                 assert(vnum < (unsigned)env->nvals);
617
618                 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
619
620                 block = get_nodes_block(node);
621                 set_cur_block(block);
622
623                 /* Beware: A Store can contain a hidden conversion in Firm. */
624                 val = get_Store_value(node);
625                 if (get_irn_mode(val) != env->modes[vnum])
626                         val = new_d_Conv(get_irn_dbg_info(node), val, env->modes[vnum]);
627
628                 set_value(vnum, val);
629
630                 mem = get_Store_mem(node);
631                 turn_into_tuple(node, pn_Store_max);
632                 set_Tuple_pred(node, pn_Store_M,         mem);
633                 set_Tuple_pred(node, pn_Store_X_regular, new_Jmp());
634                 set_Tuple_pred(node, pn_Store_X_except,  new_Bad());
635         }
636 }
637
638 /**
639  * Make scalar replacement.
640  *
641  * @param sels    A set containing all Sel nodes that have a value number
642  * @param nvals   The number of scalars.
643  * @param modes   A flexible array, containing all the modes of
644  *                the value numbers.
645  */
646 static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes) {
647         env_t env;
648
649         ssa_cons_start(current_ir_graph, nvals);
650
651         env.nvals     = nvals;
652         env.modes     = modes;
653         env.sels      = sels;
654
655         /*
656          * second step: walk over the graph blockwise in topological order
657          * and fill the array as much as possible.
658          */
659         DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", current_ir_graph));
660         irg_walk_blkwise_graph(current_ir_graph, NULL, topologic_walker, &env);
661
662         ssa_cons_finish(current_ir_graph);
663 }
664
665 /*
666  * Find possible scalar replacements
667  *
668  * @param irg  The current ir graph.
669  */
670 int scalar_replacement_opt(ir_graph *irg) {
671         unsigned  nvals;
672         int       i;
673         scalars_t key, *value;
674         ir_node   *irg_frame;
675         ir_mode   **modes;
676         set       *set_ent;
677         pset      *sels;
678         ir_type   *ent_type, *frame_tp;
679         ir_graph  *rem;
680         int       res = 0;
681
682         if (! get_opt_scalar_replacement())
683                 return 0;
684
685         rem = current_ir_graph;
686         current_ir_graph = irg;
687
688         /* Call algorithm that computes the out edges */
689         assure_irg_outs(irg);
690
691         /* we use the link field to store the VNUM */
692         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
693         irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
694
695         /* Find possible scalar replacements */
696         if (find_possible_replacements(irg)) {
697                 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %+F\n", irg));
698
699                 /* Insert in set the scalar replacements. */
700                 irg_frame = get_irg_frame(irg);
701                 nvals     = 0;
702                 modes     = NEW_ARR_F(ir_mode *, 16);
703                 set_ent   = new_set(ent_cmp, 8);
704                 sels      = pset_new_ptr(8);
705                 frame_tp  = get_irg_frame_type(irg);
706
707                 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
708                         ir_node *succ = get_irn_out(irg_frame, i);
709
710                         if (is_Sel(succ)) {
711                                 ir_entity *ent = get_Sel_entity(succ);
712
713                                 /* we are only interested in entities on the frame, NOT
714                                    on the value type */
715                                 if (get_entity_owner(ent) != frame_tp)
716                                         continue;
717
718                                 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
719                                         continue;
720
721                                 ent_type = get_entity_type(ent);
722
723                                 key.ent       = ent;
724                                 key.ent_owner = get_entity_owner(ent);
725                                 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
726
727 #ifdef DEBUG_libfirm
728                                 if (is_Array_type(ent_type)) {
729                                         DB((dbg, SET_LEVEL_1, "  found array %s\n", get_entity_name(ent)));
730                                 } else if (is_Struct_type(ent_type)) {
731                                         DB((dbg, SET_LEVEL_1, "  found struct %s\n", get_entity_name(ent)));
732                                 } else if (is_atomic_type(ent_type))
733                                         DB((dbg, SET_LEVEL_1, "  found atomic value %s\n", get_entity_name(ent)));
734                                 else {
735                                         panic("Neither an array nor a struct or atomic value found in scalar replace");
736                                 }
737 #endif /* DEBUG_libfirm */
738
739                                 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
740                         }
741                 }
742
743                 DB((dbg, SET_LEVEL_1, "  %u values will be needed\n", nvals));
744
745                 /* If scalars were found. */
746                 if (nvals > 0) {
747                         do_scalar_replacements(sels, nvals, modes);
748
749                         foreach_set(set_ent, value) {
750                                 remove_class_member(value->ent_owner, value->ent);
751                         }
752
753                         /*
754                          * We changed the graph, but did NOT introduce new blocks
755                          * neither changed control flow, cf-backedges should be still
756                          * consistent.
757                          */
758                         set_irg_outs_inconsistent(irg);
759                         set_irg_loopinfo_inconsistent(irg);
760
761                         res = 1;
762                 }
763                 del_pset(sels);
764                 del_set(set_ent);
765                 DEL_ARR_F(modes);
766         }
767
768         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
769         irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
770
771         current_ir_graph = rem;
772         return res;
773 }
774
775 ir_graph_pass_t *scalar_replacement_opt_pass(const char *name) {
776         return def_graph_pass_ret(name ? name : "scalar_rep",
777                                   scalar_replacement_opt);
778 }
779
780 void firm_init_scalar_replace(void) {
781         FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");
782 }