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