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