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