split irg and irp resources, add IRP_RESOURCE_TYPE_LINK
[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 its 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, its 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         size_t  mem_idx;
336         int     i;
337         long    static_link_arg;
338         int     res = 0;
339
340         /*
341          * First, clear the link field of all interesting entities.
342          */
343         frame_tp = get_irg_frame_type(irg);
344         for (mem_idx = get_class_n_members(frame_tp); mem_idx > 0;) {
345                 ir_entity *ent = get_class_member(frame_tp, --mem_idx);
346                 set_entity_link(ent, NULL);
347         }
348
349         /* check for inner functions:
350          * FIXME: need a way to get the argument position for the static link */
351         static_link_arg = 0;
352         for (mem_idx = get_class_n_members(frame_tp); mem_idx > 0;) {
353                 ir_entity *ent = get_class_member(frame_tp, --mem_idx);
354                 if (is_method_entity(ent)) {
355                         ir_graph *inner_irg = get_entity_irg(ent);
356                         ir_node  *args;
357                         int      j;
358
359                         assure_irg_outs(inner_irg);
360                         args = get_irg_args(inner_irg);
361                         for (j = get_irn_n_outs(args) - 1; j >= 0; --j) {
362                                 ir_node *arg = get_irn_out(args, j);
363
364                                 if (get_Proj_proj(arg) == static_link_arg) {
365                                         int k;
366                                         for (k = get_irn_n_outs(arg) - 1; k >= 0; --k) {
367                                                 ir_node *succ = get_irn_out(arg, k);
368
369                                                 if (is_Sel(succ)) {
370                                                         ir_entity *ent = get_Sel_entity(succ);
371
372                                                         if (get_entity_owner(ent) == frame_tp) {
373                                                                 /* found an access to the outer frame */
374                                                                 set_entity_link(ent, ADDRESS_TAKEN);
375                                                         }
376                                                 }
377                                         }
378                                 }
379                         }
380                 }
381         }
382
383         /*
384          * Check the ir_graph for Sel nodes. If the entity of Sel
385          * isn't a scalar replacement set the link of this entity
386          * equal ADDRESS_TAKEN.
387          */
388         irg_frame = get_irg_frame(irg);
389         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
390                 ir_node *succ = get_irn_out(irg_frame, i);
391
392                 if (is_Sel(succ)) {
393                         ir_entity *ent = get_Sel_entity(succ);
394                         ir_type *ent_type;
395
396                         /* we are only interested in entities on the frame, NOT
397                            on the value type */
398                         if (get_entity_owner(ent) != frame_tp)
399                                 continue;
400
401                         if (get_entity_link(ent) == ADDRESS_TAKEN)
402                                 continue;
403
404                         /*
405                          * Beware: in rare cases even entities on the frame might be
406                          * volatile. This might happen if the entity serves as a store
407                          * to a value that must survive a exception. Do not optimize
408                          * such entities away.
409                          */
410                         if (get_entity_volatility(ent) == volatility_is_volatile) {
411                                 set_entity_link(ent, ADDRESS_TAKEN);
412                                 continue;
413                         }
414
415                         ent_type = get_entity_type(ent);
416
417                         /* we can handle arrays, structs and atomic types yet */
418                         if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
419                                 if (is_address_taken(succ)) {
420                                          /* killing one */
421                                         if (get_entity_link(ent))
422                                                 --res;
423                                         set_entity_link(ent, ADDRESS_TAKEN);
424                                 } else {
425                                         /* possible found one */
426                                         if (get_entity_link(ent) == NULL)
427                                                 ++res;
428                                         link_all_leave_sels(ent, succ);
429                                 }
430                         }
431                 }
432         }
433
434         return res;
435 }
436
437 /**
438  * Return a path from the Sel node "sel" to its root.
439  *
440  * @param sel  the Sel node
441  * @param len  the length of the path so far
442  */
443 static path_t *find_path(ir_node *sel, size_t len)
444 {
445         size_t pos;
446         int i, n;
447         path_t *res;
448         ir_node *pred = get_Sel_ptr(sel);
449
450         /* the current Sel node will add some path elements */
451         n    = get_Sel_n_indexs(sel);
452         len += n + 1;
453
454         if (! is_Sel(pred)) {
455                 /* we found the root */
456                 res = XMALLOCF(path_t, path, len);
457                 res->path_len = len;
458         } else
459                 res = find_path(pred, len);
460
461         assert(len <= res->path_len);
462         pos = res->path_len - len;
463
464         res->path[pos++].ent = get_Sel_entity(sel);
465         for (i = 0; i < n; ++i) {
466                 ir_node *index = get_Sel_index(sel, i);
467
468                 res->path[pos++].tv = get_Const_tarval(index);
469         }
470         return res;
471 }
472
473
474 /**
475  * Allocate value numbers for the leaves
476  * in our found entities.
477  *
478  * @param sels  a set that will contain all Sels that have a value number
479  * @param ent   the entity that will be scalar replaced
480  * @param vnum  the first value number we can assign
481  * @param modes a flexible array, containing all the modes of
482  *              the value numbers.
483  *
484  * @return the next free value number
485  */
486 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
487 {
488         ir_node *sel, *next;
489         path_t *key, *path;
490         set *pathes = new_set(path_cmp, 8);
491
492         DB((dbg, SET_LEVEL_3, "  Visiting Sel nodes of entity %+F\n", ent));
493         /* visit all Sel nodes in the chain of the entity */
494         for (sel = (ir_node*)get_entity_link(ent); sel != NULL;
495              sel = next) {
496                 next = (ir_node*)get_irn_link(sel);
497
498                 /* we must mark this sel for later */
499                 pset_insert_ptr(sels, sel);
500
501                 key  = find_path(sel, 0);
502                 path = (path_t*)set_find(pathes, key, PATH_SIZE(key), path_hash(key));
503
504                 if (path) {
505                         SET_VNUM(sel, path->vnum);
506                         DB((dbg, SET_LEVEL_3, "  %+F represents value %u\n", sel, path->vnum));
507                 } else {
508                         key->vnum = vnum++;
509
510                         set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
511
512                         SET_VNUM(sel, key->vnum);
513                         DB((dbg, SET_LEVEL_3, "  %+F represents value %u\n", sel, key->vnum));
514
515                         ARR_EXTO(ir_mode *, *modes, (key->vnum + 15) & ~15);
516
517                         (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
518
519                         assert((*modes)[key->vnum] && "Value is not atomic");
520
521 #ifdef DEBUG_libfirm
522                         /* Debug output */
523                         {
524                                 unsigned i;
525                                 DB((dbg, SET_LEVEL_2, "  %s", get_entity_name(key->path[0].ent)));
526                                 for (i = 1; i < key->path_len; ++i) {
527                                         if (is_entity(key->path[i].ent))
528                                                 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
529                                         else
530                                                 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
531                                 }
532                                 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
533                         }
534 #endif /* DEBUG_libfirm */
535                 }
536                 free(key);
537         }
538
539         del_set(pathes);
540         set_entity_link(ent, NULL);
541         return vnum;
542 }
543
544 /**
545  * A list entry for the fixing lists
546  */
547 typedef struct list_entry_t {
548         ir_node  *node;   /**< the node that must be fixed */
549         unsigned vnum;    /**< the value number of this node */
550 } list_entry_t;
551
552 /**
553  * environment for memory walker
554  */
555 typedef struct env_t {
556         int          nvals;       /**< number of values */
557         ir_mode      **modes;     /**< the modes of the values */
558         pset         *sels;       /**< A set of all Sel nodes that have a value number */
559 } env_t;
560
561 /**
562  * topological post-walker.
563  */
564 static void topologic_walker(ir_node *node, void *ctx)
565 {
566         env_t    *env = (env_t*)ctx;
567         ir_graph *irg = get_irn_irg(node);
568         ir_node  *adr, *block, *mem, *val;
569         ir_mode  *mode;
570         unsigned vnum;
571
572         if (is_Load(node)) {
573                 /* a load, check if we can resolve it */
574                 adr = get_Load_ptr(node);
575
576                 DB((dbg, SET_LEVEL_3, "  checking %+F for replacement ", node));
577                 if (! is_Sel(adr)) {
578                         DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
579                         return;
580                 }
581
582                 if (! pset_find_ptr(env->sels, adr)) {
583                         DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
584                         return;
585                 }
586
587                 /* ok, we have a Load that will be replaced */
588                 vnum = GET_VNUM(adr);
589                 assert(vnum < (unsigned)env->nvals);
590
591                 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
592
593                 block = get_nodes_block(node);
594                 set_cur_block(block);
595
596                 /* check, if we can replace this Load */
597                 val = get_value(vnum, env->modes[vnum]);
598
599                 /* Beware: A Load can contain a hidden conversion in Firm.
600                 This happens for instance in the following code:
601
602                  int i;
603                  unsigned j = *(unsigned *)&i;
604
605                 Handle this here. */
606                 mode = get_Load_mode(node);
607                 if (mode != get_irn_mode(val))
608                         val = new_rd_Conv(get_irn_dbg_info(node), block, val, mode);
609
610                 mem = get_Load_mem(node);
611                 turn_into_tuple(node, pn_Load_max+1);
612                 set_Tuple_pred(node, pn_Load_M,         mem);
613                 set_Tuple_pred(node, pn_Load_res,       val);
614                 set_Tuple_pred(node, pn_Load_X_regular, new_r_Jmp(block));
615                 set_Tuple_pred(node, pn_Load_X_except,  new_r_Bad(irg, mode_X));
616         } else if (is_Store(node)) {
617                 DB((dbg, SET_LEVEL_3, "  checking %+F for replacement ", node));
618
619                 /* a Store always can be replaced */
620                 adr = get_Store_ptr(node);
621
622                 if (! is_Sel(adr)) {
623                         DB((dbg, SET_LEVEL_3, "no Sel input (%+F)\n", adr));
624                         return;
625                 }
626
627                 if (! pset_find_ptr(env->sels, adr)) {
628                         DB((dbg, SET_LEVEL_3, "Sel %+F has no VNUM\n", adr));
629                         return;
630                 }
631
632                 vnum = GET_VNUM(adr);
633                 assert(vnum < (unsigned)env->nvals);
634
635                 DB((dbg, SET_LEVEL_3, "replacing by value %u\n", vnum));
636
637                 block = get_nodes_block(node);
638                 set_cur_block(block);
639
640                 /* Beware: A Store can contain a hidden conversion in Firm. */
641                 val = get_Store_value(node);
642                 if (get_irn_mode(val) != env->modes[vnum])
643                         val = new_rd_Conv(get_irn_dbg_info(node), block, val, env->modes[vnum]);
644
645                 set_value(vnum, val);
646
647                 mem = get_Store_mem(node);
648                 turn_into_tuple(node, pn_Store_max+1);
649                 set_Tuple_pred(node, pn_Store_M,         mem);
650                 set_Tuple_pred(node, pn_Store_X_regular, new_r_Jmp(block));
651                 set_Tuple_pred(node, pn_Store_X_except,  new_r_Bad(irg, mode_X));
652         }
653 }
654
655 /**
656  * Make scalar replacement.
657  *
658  * @param sels    A set containing all Sel nodes that have a value number
659  * @param nvals   The number of scalars.
660  * @param modes   A flexible array, containing all the modes of
661  *                the value numbers.
662  */
663 static void do_scalar_replacements(ir_graph *irg, pset *sels, int nvals,
664                                    ir_mode **modes)
665 {
666         env_t env;
667
668         ssa_cons_start(irg, nvals);
669
670         env.nvals = nvals;
671         env.modes = modes;
672         env.sels  = sels;
673
674         /*
675          * second step: walk over the graph blockwise in topological order
676          * and fill the array as much as possible.
677          */
678         DB((dbg, SET_LEVEL_3, "Substituting Loads and Stores in %+F\n", irg));
679         irg_walk_blkwise_graph(irg, NULL, topologic_walker, &env);
680
681         ssa_cons_finish(irg);
682 }
683
684 /*
685  * Find possible scalar replacements
686  *
687  * @param irg  The current ir graph.
688  */
689 int scalar_replacement_opt(ir_graph *irg)
690 {
691         unsigned  nvals;
692         int       i;
693         scalars_t key, *value;
694         ir_node   *irg_frame;
695         ir_mode   **modes;
696         set       *set_ent;
697         pset      *sels;
698         ir_type   *ent_type, *frame_tp;
699         int       res = 0;
700
701         /* Call algorithm that computes the out edges */
702         assure_irg_outs(irg);
703
704         /* we use the link field to store the VNUM */
705         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
706         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
707
708         /* Find possible scalar replacements */
709         if (find_possible_replacements(irg)) {
710                 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %+F\n", irg));
711
712                 /* Insert in set the scalar replacements. */
713                 irg_frame = get_irg_frame(irg);
714                 nvals     = 0;
715                 modes     = NEW_ARR_F(ir_mode *, 16);
716                 set_ent   = new_set(ent_cmp, 8);
717                 sels      = pset_new_ptr(8);
718                 frame_tp  = get_irg_frame_type(irg);
719
720                 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
721                         ir_node *succ = get_irn_out(irg_frame, i);
722
723                         if (is_Sel(succ)) {
724                                 ir_entity *ent = get_Sel_entity(succ);
725
726                                 /* we are only interested in entities on the frame, NOT
727                                    on the value type */
728                                 if (get_entity_owner(ent) != frame_tp)
729                                         continue;
730
731                                 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
732                                         continue;
733
734                                 ent_type = get_entity_type(ent);
735
736                                 key.ent       = ent;
737                                 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
738
739 #ifdef DEBUG_libfirm
740                                 if (is_Array_type(ent_type)) {
741                                         DB((dbg, SET_LEVEL_1, "  found array %s\n", get_entity_name(ent)));
742                                 } else if (is_Struct_type(ent_type)) {
743                                         DB((dbg, SET_LEVEL_1, "  found struct %s\n", get_entity_name(ent)));
744                                 } else if (is_atomic_type(ent_type))
745                                         DB((dbg, SET_LEVEL_1, "  found atomic value %s\n", get_entity_name(ent)));
746                                 else {
747                                         panic("Neither an array nor a struct or atomic value found in scalar replace");
748                                 }
749 #endif /* DEBUG_libfirm */
750
751                                 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
752                         }
753                 }
754
755                 DB((dbg, SET_LEVEL_1, "  %u values will be needed\n", nvals));
756
757                 /* If scalars were found. */
758                 if (nvals > 0) {
759                         do_scalar_replacements(irg, sels, nvals, modes);
760
761                         foreach_set(set_ent, scalars_t*, value) {
762                                 free_entity(value->ent);
763                         }
764
765                         /*
766                          * We changed the graph, but did NOT introduce new blocks
767                          * neither changed control flow, cf-backedges should be still
768                          * consistent.
769                          */
770                         res = 1;
771                 }
772                 del_pset(sels);
773                 del_set(set_ent);
774                 DEL_ARR_F(modes);
775         }
776
777         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
778         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
779
780         return res;
781 }
782
783 ir_graph_pass_t *scalar_replacement_opt_pass(const char *name)
784 {
785         return def_graph_pass_ret(name ? name : "scalar_rep",
786                                   scalar_replacement_opt);
787 }
788
789 void firm_init_scalar_replace(void)
790 {
791         FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");
792 }