switch scalar replacement to new style debug output
[libfirm] / ir / opt / scalar_replace.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Scalar replacement of compounds.
23  * @author  Beyhan Veliev, Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include "iroptimize.h"
33 #include "scalar_replace.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 "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         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         unsigned    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         ir_type *ent_owner;          /**< The owner of this entity. */
79 } scalars_t;
80
81 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
82
83 /**
84  * Compare two pathes.
85  *
86  * @return 0 if they are identically
87  */
88 static int path_cmp(const void *elt, const void *key, size_t size) {
89         const path_t *p1 = elt;
90         const path_t *p2 = 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         const scalars_t *c1 = elt;
104         const scalars_t *c2 = key;
105         (void) size;
106
107         return c1->ent != c2->ent;
108 }
109
110 /**
111  * Calculate a hash value for a path.
112  */
113 static unsigned path_hash(const path_t *path) {
114         unsigned hash = 0;
115         unsigned i;
116
117         for (i = 0; i < path->path_len; ++i)
118                 hash ^= (unsigned)PTR_TO_INT(path->path[i].ent);
119
120         return hash >> 4;
121 }
122
123 /**
124  * Returns non-zero, if all indeces of a Sel node are constants.
125  *
126  * @param sel  the Sel node that will be checked
127  */
128 static int is_const_sel(ir_node *sel) {
129         int i, n = get_Sel_n_indexs(sel);
130
131         for (i = 0; i < n; ++i) {
132                 ir_node *idx = get_Sel_index(sel, i);
133
134                 if (get_irn_op(idx) != op_Const)
135                         return 0;
136         }
137         return 1;
138 }
139
140 /**
141  * Check the mode of a Load/Store with the mode of the entity
142  * that is accessed.
143  * If the mode of the entity and the Load/Store mode do not match, we
144  * have the bad reinterpret case:
145  *
146  * int i;
147  * char b = *(char *)&i;
148  *
149  * We do NOT count this as one value and return address_taken
150  * in that case.
151  * However, we support an often used case. If the mode is two-complement
152  * we allow casts between signed/unsigned.
153  *
154  * @param mode     the mode of the Load/Store
155  * @param ent_mode the mode of the accessed entity
156  */
157 static int check_load_store_mode(ir_mode *mode, ir_mode *ent_mode) {
158         if (ent_mode != mode) {
159                 if (ent_mode == NULL ||
160                     get_mode_size_bits(ent_mode) != get_mode_size_bits(mode) ||
161                     get_mode_sort(ent_mode) != get_mode_sort(mode) ||
162                     get_mode_arithmetic(ent_mode) != irma_twos_complement ||
163                     get_mode_arithmetic(mode) != irma_twos_complement)
164                         return 0;
165         }
166         return 1;
167 }
168
169 /*
170  * Returns non-zero, if the address of an entity
171  * represented by a Sel node (or it's successor Sels) is taken.
172  */
173 int is_address_taken(ir_node *sel)
174 {
175         int     i;
176         ir_mode *emode, *mode;
177         ir_node *value;
178         ir_entity *ent;
179
180         if (! is_const_sel(sel))
181                 return 1;
182
183         for (i = get_irn_n_outs(sel) - 1; i >= 0; --i) {
184                 ir_node *succ = get_irn_out(sel, i);
185
186                 switch (get_irn_opcode(succ)) {
187                 case iro_Load:
188                         /* check if this load is not a hidden conversion */
189                         mode = get_Load_mode(succ);
190                         ent = get_Sel_entity(sel);
191                         emode = get_type_mode(get_entity_type(ent));
192                         if (! check_load_store_mode(mode, emode))
193                                 return 1;
194                         /* do not remove volatile variables */
195                         if (get_Load_volatility(succ) == volatility_is_volatile)
196                                 return 1;
197                         break;
198
199                 case iro_Store:
200                         /* check that Sel is not the Store's value */
201                         value = get_Store_value(succ);
202                         if (value == sel)
203                                 return 1;
204                         /* check if this Store is not a hidden conversion */
205                         mode = get_irn_mode(value);
206                         ent = get_Sel_entity(sel);
207                         emode = get_type_mode(get_entity_type(ent));
208                         if (! check_load_store_mode(mode, emode))
209                                 return 1;
210                         /* do not remove volatile variables */
211                         if (get_Store_volatility(succ) == volatility_is_volatile)
212                                 return 1;
213                         break;
214
215                 case iro_Sel: {
216                         /* Check the Sel successor of Sel */
217                         int res = is_address_taken(succ);
218
219                         if (res)
220                                 return 1;
221                         break;
222                 }
223
224                 case iro_Call:
225                         /* The address of an entity is given as a parameter.
226                          * As long as we do not have analyses that can tell what
227                          * is done with parameters, think is taken.
228                          * One special case: If the Call type tells that it's a
229                          * value parameter, the address is NOT taken.
230                          */
231                         return 1;
232
233                 default:
234                         /* another op, the address is taken */
235                         return 1;
236                 }
237         }
238         return 0;
239 }
240
241 /**
242  * Link all leave Sels with the entity.
243  *
244  * @param ent  the entity that will be scalar replaced
245  * @param sel  a Sel node that selects some fields of this entity
246  */
247 static void link_all_leave_sels(ir_entity *ent, ir_node *sel) {
248         int i, n, flag = 1;
249
250         n = get_irn_n_outs(sel);
251         for (i = 0; i < n; ++i) {
252                 ir_node *succ = get_irn_out(sel, i);
253
254                 if (is_Sel(succ)) {
255                         link_all_leave_sels(ent, succ);
256                         flag = 0;
257                 }
258         }
259
260         if (flag) {
261                 /* if Sel nodes with memory inputs are used, a entity can be
262                  * visited more than once causing a ring here, so we use the
263                  * node flag to mark linked nodes
264                  */
265                 if (irn_visited(sel))
266                         return;
267
268                 /* we know we are at a leave, because this function is only
269                  * called if the address is NOT taken, so succ must be a Load
270                  * or a Store node
271                  */
272                 set_irn_link(sel, get_entity_link(ent));
273                 set_entity_link(ent, sel);
274
275                 mark_irn_visited(sel);
276         }
277 }
278
279 /* we need a special address that serves as an address taken marker */
280 static char _x;
281 static void *ADDRESS_TAKEN = &_x;
282
283 /**
284  * Find possible scalar replacements.
285  *
286  * @param irg  an IR graph
287  *
288  * This function finds variables on the (members of the) frame type
289  * that can be scalar replaced, because their address is never taken.
290  * If such a variable is found, it's entity link will hold a list of all
291  * Sel nodes, that selects the atomic fields of this entity.
292  * Otherwise, the link will be ADDRESS_TAKEN or NULL.
293  *
294  * @return  non-zero if at least one entity could be replaced
295  *          potentially
296  */
297 static int find_possible_replacements(ir_graph *irg) {
298         ir_node *irg_frame = get_irg_frame(irg);
299         int i, n;
300         int res = 0;
301
302         inc_irg_visited(irg);
303
304         n = get_irn_n_outs(irg_frame);
305
306         /*
307          * First, clear the link field of all interesting entities.
308          * Note that we did not rely on the fact that there is only
309          * one Sel node per entity, so we might access one entity
310          * more than once here.
311          * That's why we have need two loops.
312          */
313         for (i = 0; i < n; ++i) {
314                 ir_node *succ = get_irn_out(irg_frame, i);
315
316                 if (is_Sel(succ)) {
317                         ir_entity *ent = get_Sel_entity(succ);
318                         set_entity_link(ent, NULL);
319                 }
320         }
321
322         /*
323          * Check the ir_graph for Sel nodes. If the entity of Sel
324          * isn't a scalar replacement set the link of this entity
325          * equal ADDRESS_TAKEN.
326          */
327         for (i = 0; i < n; ++i) {
328                 ir_node *succ = get_irn_out(irg_frame, i);
329
330                 if (is_Sel(succ)) {
331                         ir_entity *ent = get_Sel_entity(succ);
332                         ir_type *ent_type;
333
334                         if (get_entity_link(ent) == ADDRESS_TAKEN)
335                                 continue;
336
337                         /*
338                          * Beware: in rare cases even entities on the frame might be
339                          * volatile. This might happen if the entity serves as a store
340                          * to a value that must survive a exception. Do not optimize
341                          * such entities away.
342                          */
343                         if (get_entity_volatility(ent) == volatility_is_volatile) {
344                                 set_entity_link(ent, ADDRESS_TAKEN);
345                                 continue;
346                         }
347
348                         ent_type = get_entity_type(ent);
349
350                         /* we can handle arrays, structs and atomic types yet */
351                         if (is_Array_type(ent_type) || is_Struct_type(ent_type) || is_atomic_type(ent_type)) {
352                                 if (is_address_taken(succ)) {
353                                         if (get_entity_link(ent)) /* killing one */
354                                                 --res;
355                                         set_entity_link(ent, ADDRESS_TAKEN);
356                                 } else {
357                                         /* possible found one */
358                                         if (get_entity_link(ent) == NULL)
359                                                 ++res;
360                                         link_all_leave_sels(ent, succ);
361                                 }
362                         }
363                 }
364         }
365
366         return res;
367 }
368
369 /**
370  * Return a path from the Sel node sel to it's root.
371  *
372  * @param sel  the Sel node
373  * @param len  the length of the path so far
374  */
375 static path_t *find_path(ir_node *sel, unsigned len) {
376         int pos, i, n;
377         path_t *res;
378         ir_node *pred = get_Sel_ptr(sel);
379
380         /* the current Sel node will add some path elements */
381         n    = get_Sel_n_indexs(sel);
382         len += n + 1;
383
384         if (! is_Sel(pred)) {
385                 /* we found the root */
386
387                 res = xmalloc(sizeof(*res) + (len - 1) * sizeof(res->path));
388                 res->path_len = len;
389         } else
390                 res = find_path(pred, len);
391
392         pos = res->path_len - len;
393
394         res->path[pos++].ent = get_Sel_entity(sel);
395         for (i = 0; i < n; ++i) {
396                 ir_node *index = get_Sel_index(sel, i);
397
398                 res->path[pos++].tv = get_Const_tarval(index);
399         }
400         return res;
401 }
402
403
404 /**
405  * Allocate value numbers for the leaves
406  * in our found entities.
407  *
408  * @param sels  a set that will contain all Sels that have a value number
409  * @param ent   the entity that will be scalar replaced
410  * @param vnum  the first value number we can assign
411  * @param modes a flexible array, containing all the modes of
412  *              the value numbers.
413  *
414  * @return the next free value number
415  */
416 static unsigned allocate_value_numbers(pset *sels, ir_entity *ent, unsigned vnum, ir_mode ***modes)
417 {
418         ir_node *sel, *next;
419         path_t *key, *path;
420         set *pathes = new_set(path_cmp, 8);
421
422         /* visit all Sel nodes in the chain of the entity */
423         for (sel = get_entity_link(ent); sel; sel = next) {
424                 next = get_irn_link(sel);
425
426                 /* we must mark this sel for later */
427                 pset_insert_ptr(sels, sel);
428
429                 key  = find_path(sel, 0);
430                 path = set_find(pathes, key, PATH_SIZE(key), path_hash(key));
431
432                 if (path)
433                         SET_VNUM(sel, path->vnum);
434                 else {
435                         key->vnum = vnum++;
436
437                         set_insert(pathes, key, PATH_SIZE(key), path_hash(key));
438
439                         SET_VNUM(sel, key->vnum);
440                         ARR_EXTO(ir_mode *, *modes, (int)((key->vnum + 15) & ~15));
441
442                         (*modes)[key->vnum] = get_type_mode(get_entity_type(get_Sel_entity(sel)));
443
444                         assert((*modes)[key->vnum] && "Value is not atomic");
445
446 #ifdef DEBUG_libfirm
447                         /* Debug output */
448                         {
449                                 unsigned i;
450                                 DB((dbg, SET_LEVEL_2, "  %s", get_entity_name(key->path[0].ent)));
451                                 for (i = 1; i < key->path_len; ++i) {
452                                         if (is_entity(key->path[i].ent))
453                                                 DB((dbg, SET_LEVEL_2, ".%s", get_entity_name(key->path[i].ent)));
454                                         else
455                                                 DB((dbg, SET_LEVEL_2, "[%ld]", get_tarval_long(key->path[i].tv)));
456                                 }
457                                 DB((dbg, SET_LEVEL_2, " = %u (%s)\n", PTR_TO_INT(get_irn_link(sel)), get_mode_name((*modes)[key->vnum])));
458                         }
459 #endif /* DEBUG_libfirm */
460                 }
461                 free(key);
462         }
463
464         del_set(pathes);
465         set_entity_link(ent, NULL);
466         return vnum;
467 }
468
469 /**
470  * A list entry for the fixing lists
471  */
472 typedef struct _list_entry_t {
473         ir_node  *node;   /**< the node that must be fixed */
474         unsigned vnum;    /**< the value number of this node */
475 } list_entry_t;
476
477 /**
478  * environment for memory walker
479  */
480 typedef struct _env_t {
481         struct obstack obst;      /**< a obstack for the value blocks */
482         int          nvals;       /**< number of values */
483         ir_mode      **modes;     /**< the modes of the values */
484         list_entry_t *fix_phis;   /**< list of all Phi nodes that must be fixed */
485         list_entry_t *fix_loads;  /**< list of all Load nodes that must be fixed */
486         pset         *sels;       /**< A set of all Sel nodes that have a value number */
487 } env_t;
488
489 /**
490  * topological walker.
491  */
492 static void topologic_walker(ir_node *node, void *ctx) {
493         env_t        *env = ctx;
494         ir_op        *op = get_irn_op(node);
495         ir_node      *adr, *block, *mem, *unk, **value_arr, **in, *val;
496         ir_mode      *mode;
497         unsigned     vnum;
498         int          i, j, n;
499         list_entry_t *l;
500
501         if (op == op_Load) {
502                 /* a load, check if we can resolve it */
503                 adr = get_Load_ptr(node);
504
505                 if (! is_Sel(adr))
506                         return;
507
508                 if (! pset_find_ptr(env->sels, adr))
509                         return;
510
511                 /* ok, we have a Load that will be replaced */
512                 vnum = GET_VNUM(adr);
513
514                 assert(vnum < (unsigned)env->nvals);
515
516                 block     = get_nodes_block(node);
517                 value_arr = get_irn_link(block);
518
519                 /* check, if we can replace this Load */
520                 if (value_arr[vnum]) {
521                         mem = get_Load_mem(node);
522
523                         /* Beware: A Load can contain a hidden conversion in Firm.
524                         This happens for instance in the following code:
525
526                          int i;
527                          unsigned j = *(unsigned *)&i;
528
529                         Handle this here. */
530                         val = value_arr[vnum];
531                         mode = get_Load_mode(node);
532                         if (mode != get_irn_mode(val))
533                                 val = new_d_Conv(get_irn_dbg_info(node), val, mode);
534
535                         turn_into_tuple(node, pn_Load_max);
536                         set_Tuple_pred(node, pn_Load_M,         mem);
537                         set_Tuple_pred(node, pn_Load_res,       val);
538                         set_Tuple_pred(node, pn_Load_X_regular, new_r_Jmp(current_ir_graph, block));
539                         set_Tuple_pred(node, pn_Load_X_except,  new_Bad());
540                 } else {
541                         l = obstack_alloc(&env->obst, sizeof(*l));
542                         l->node = node;
543                         l->vnum = vnum;
544
545                         set_irn_link(node, env->fix_loads);
546                         env->fix_loads = l;
547                 }
548         } else if (op == op_Store) {
549                 /* a Store always can be replaced */
550                 adr = get_Store_ptr(node);
551
552                 if (! is_Sel(adr))
553                         return;
554
555                 if (! pset_find_ptr(env->sels, adr))
556                         return;
557
558                 vnum = GET_VNUM(adr);
559
560                 assert(vnum < (unsigned)env->nvals);
561
562                 block     = get_nodes_block(node);
563                 value_arr = get_irn_link(block);
564
565                 /* Beware: A Store can contain a hidden conversion in Firm. */
566                 val = get_Store_value(node);
567                 if (get_irn_mode(val) != env->modes[vnum])
568                         val = new_d_Conv(get_irn_dbg_info(node), val, env->modes[vnum]);
569                 value_arr[vnum] = val;
570
571                 mem = get_Store_mem(node);
572                 block = get_nodes_block(node);
573
574                 turn_into_tuple(node, pn_Store_max);
575                 set_Tuple_pred(node, pn_Store_M,         mem);
576                 set_Tuple_pred(node, pn_Store_X_regular, new_r_Jmp(current_ir_graph, block));
577                 set_Tuple_pred(node, pn_Store_X_except,  new_Bad());
578         } else if (op == op_Phi && get_irn_mode(node) == mode_M) {
579                 /*
580                  * found a memory Phi: Here, we must create new Phi nodes
581                  */
582                 block     = get_nodes_block(node);
583                 value_arr = get_irn_link(block);
584
585                 n = get_Block_n_cfgpreds(block);
586
587                 in = alloca(sizeof(*in) * n);
588
589                 for (i = env->nvals - 1; i >= 0; --i) {
590                         unk = new_Unknown(env->modes[i]);
591                         for (j = n - 1; j >= 0; --j)
592                                 in[j] = unk;
593
594                         value_arr[i] = new_r_Phi(current_ir_graph, block, n, in, env->modes[i]);
595
596                         l = obstack_alloc(&env->obst, sizeof(*l));
597                         l->node = value_arr[i];
598                         l->vnum = i;
599
600                         set_irn_link(value_arr[i], env->fix_phis);
601                         env->fix_phis = l;
602                 }
603         }
604 }
605
606 /**
607  * Walker: allocate the value array for every block.
608  */
609 static void alloc_value_arr(ir_node *block, void *ctx) {
610         env_t *env = ctx;
611         ir_node **var_arr = obstack_alloc(&env->obst, sizeof(*var_arr) * env->nvals);
612
613         /* the value array is empty at start */
614         memset(var_arr, 0, sizeof(*var_arr) * env->nvals);
615         set_irn_link(block, var_arr);
616 }
617
618 /**
619  * searches through blocks beginning from block for value
620  * vnum and return it.
621  */
622 static ir_node *find_vnum_value(ir_node *block, unsigned vnum) {
623         ir_node **value_arr;
624         int i;
625         ir_node *res;
626
627         if (Block_not_block_visited(block)) {
628                 mark_Block_block_visited(block);
629
630                 value_arr = get_irn_link(block);
631
632                 if (value_arr[vnum])
633                         return value_arr[vnum];
634
635                 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
636                         ir_node *pred = get_Block_cfgpred(block, i);
637
638                         res = find_vnum_value(get_nodes_block(pred), vnum);
639                         if (res)
640                                 return res;
641                 }
642         }
643         return NULL;
644 }
645
646 /**
647  * fix the Phi list
648  */
649 static void fix_phis(env_t *env) {
650         list_entry_t *l;
651         ir_node      *phi, *block, *pred, *val;
652         int          i;
653
654         for (l = env->fix_phis; l; l = get_irn_link(phi)) {
655                 phi = l->node;
656
657                 block = get_nodes_block(phi);
658                 for (i = get_irn_arity(phi) - 1; i >= 0; --i) {
659                         pred = get_Block_cfgpred(block, i);
660                         pred = get_nodes_block(pred);
661
662                         inc_irg_block_visited(current_ir_graph);
663                         val = find_vnum_value(pred, l->vnum);
664
665                         if (val)
666                                 set_irn_n(phi, i, val);
667                 }
668         }
669 }
670
671 /**
672  * fix the Load list
673  */
674 static void fix_loads(env_t *env) {
675         list_entry_t *l;
676         ir_node      *load, *block, *pred, *val = NULL, *mem;
677         ir_mode      *mode;
678         int          i;
679
680         for (l = env->fix_loads; l; l = get_irn_link(load)) {
681                 load = l->node;
682
683                 block = get_nodes_block(load);
684                 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
685                         pred = get_Block_cfgpred(block, i);
686                         pred = get_nodes_block(pred);
687
688                         inc_irg_block_visited(current_ir_graph);
689                         val = find_vnum_value(pred, l->vnum);
690
691                         if (val)
692                                 break;
693                 }
694
695                 if (! val) {
696                         /* access of an uninitialized value */
697                         val = new_Unknown(env->modes[l->vnum]);
698                 }
699
700                 /* Beware: A Load can contain a hidden conversion in Firm.
701                    Handle this here. */
702                 mode = get_Load_mode(load);
703                 if (mode != get_irn_mode(val))
704                         val = new_d_Conv(get_irn_dbg_info(load), val, mode);
705
706                 mem = get_Load_mem(load);
707
708                 turn_into_tuple(load, pn_Load_max);
709                 set_Tuple_pred(load, pn_Load_M,         mem);
710                 set_Tuple_pred(load, pn_Load_res,       val);
711                 set_Tuple_pred(load, pn_Load_X_regular, new_r_Jmp(current_ir_graph, block));
712                 set_Tuple_pred(load, pn_Load_X_except,  new_Bad());
713         }
714 }
715
716 /**
717  *  Make scalar replacement.
718  *
719  * @param sels    A set containing all Sel nodes that have a value number
720  * @param nvals   The number of scalars.
721  * @param modes   A flexible array, containing all the modes of
722  *                the value numbers.
723  */
724 static void do_scalar_replacements(pset *sels, int nvals, ir_mode **modes) {
725         env_t env;
726
727         obstack_init(&env.obst);
728         env.nvals     = nvals;
729         env.modes     = modes;
730         env.fix_phis  = NULL;
731         env.fix_loads = NULL;
732         env.sels      = sels;
733
734         /* first step: allocate the value arrays for every block */
735         irg_block_walk_graph(current_ir_graph, NULL, alloc_value_arr, &env);
736
737         /*
738          * second step: walk over the graph blockwise in topological order
739          * and fill the array as much as possible.
740          */
741         irg_walk_blkwise_graph(current_ir_graph, NULL, topologic_walker, &env);
742
743         /* third, fix the list of Phis, then the list of Loads */
744         fix_phis(&env);
745         fix_loads(&env);
746
747         obstack_free(&env.obst, NULL);
748 }
749
750 /*
751  * Find possible scalar replacements
752  *
753  * @param irg  The current ir graph.
754  */
755 void scalar_replacement_opt(ir_graph *irg) {
756         unsigned  nvals;
757         int       i;
758         scalars_t key, *value;
759         ir_node   *irg_frame;
760         ir_mode   **modes;
761         set       *set_ent;
762         pset      *sels;
763         ir_type   *ent_type;
764         ir_graph  *rem;
765
766         if (! get_opt_scalar_replacement())
767                 return;
768
769         rem = current_ir_graph;
770
771         /* Call algorithm that computes the out edges */
772         assure_irg_outs(irg);
773
774         /* Find possible scalar replacements */
775         if (find_possible_replacements(irg)) {
776                 DB((dbg, SET_LEVEL_1, "Scalar Replacement: %s\n", get_entity_name(get_irg_entity(irg))));
777
778                 /* Insert in set the scalar replacements. */
779                 irg_frame = get_irg_frame(irg);
780                 nvals = 0;
781                 modes = NEW_ARR_F(ir_mode *, 16);
782                 set_ent = new_set(ent_cmp, 8);
783                 sels    = pset_new_ptr(8);
784
785                 for (i = 0 ; i < get_irn_n_outs(irg_frame); i++) {
786                         ir_node *succ = get_irn_out(irg_frame, i);
787
788                         if (is_Sel(succ)) {
789                                 ir_entity *ent = get_Sel_entity(succ);
790
791                                 if (get_entity_link(ent) == NULL || get_entity_link(ent) == ADDRESS_TAKEN)
792                                         continue;
793
794                                 ent_type = get_entity_type(ent);
795
796                                 key.ent       = ent;
797                                 key.ent_owner = get_entity_owner(ent);
798                                 set_insert(set_ent, &key, sizeof(key), HASH_PTR(key.ent));
799
800 #ifdef DEBUG_libfirm
801                                 if (is_Array_type(ent_type)) {
802                                         DB((dbg, SET_LEVEL_1, "  found array %s\n", get_entity_name(ent)));
803                                 } else if (is_Struct_type(ent_type)) {
804                                         DB((dbg, SET_LEVEL_1, "  found struct %s\n", get_entity_name(ent)));
805                                 } else if (is_atomic_type(ent_type))
806                                         DB((dbg, SET_LEVEL_1, "  found atomic value %s\n", get_entity_name(ent)));
807                                 else {
808                                         panic("Neither an array nor a struct or atomic value found in scalar replace");
809                                 }
810 #endif /* DEBUG_libfirm */
811
812                                 nvals = allocate_value_numbers(sels, ent, nvals, &modes);
813                         }
814                 }
815
816                 DB((dbg, SET_LEVEL_1, "  %u values will be needed\n", nvals));
817
818                 /* If scalars were found. */
819                 if (nvals > 0) {
820                         do_scalar_replacements(sels, nvals, modes);
821
822                         foreach_set(set_ent, value) {
823                                 remove_class_member(value->ent_owner, value->ent);
824                         }
825
826                         /*
827                          * We changed the graph, but did NOT introduce new blocks
828                          * neither changed control flow, cf-backedges should be still
829                          * consistent.
830                          */
831                         set_irg_outs_inconsistent(irg);
832                         set_irg_loopinfo_inconsistent(irg);
833                 }
834                 del_pset(sels);
835                 del_set(set_ent);
836                 DEL_ARR_F(modes);
837
838         }
839
840         current_ir_graph = rem;
841 }
842
843 void firm_init_scalar_replace(void) {
844         FIRM_DBG_REGISTER(dbg, "firm.opt.scalar_replace");
845 }