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