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