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