fix fehler172
[libfirm] / ir / lower / lower_mux.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   Replaces Mux nodes with control-flow
23  * @author  Olaf Liebe
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "lowering.h"
31 #include "array.h"
32 #include "irnode_t.h"
33 #include "irgraph_t.h"
34 #include "irgwalk.h"
35 #include "irgmod.h"
36 #include "ircons.h"
37 #include "irvrfy.h"
38
39 typedef struct walk_env {
40         lower_mux_callback *cb_func;
41         ir_node            **muxes;
42 } walk_env_t;
43
44 static void find_mux_nodes(ir_node *mux, void *ctx)
45 {
46         walk_env_t *env = ctx;
47
48         /* Skip non-mux nodes. */
49         if (!is_Mux(mux))
50                 return;
51
52         /* Skip nodes, depending on the callback function. */
53         if (env->cb_func != NULL && !env->cb_func(mux)) {
54                 return;
55         }
56
57         /* Store the node. */
58         ARR_APP1(ir_node*, env->muxes, mux);
59 }
60
61 static void lower_mux_node(ir_node* mux)
62 {
63         ir_node *upper_block;
64         ir_node *lower_block;
65         ir_node *cond;
66         ir_node *trueProj;
67         ir_node *falseProj;
68         ir_node *falseBlock;
69         ir_node *mux_jmps[2];
70         ir_node *mux_values[2];
71         ir_node *phi;
72         ir_node *mbh;
73
74         /* Split the block in two halfs, with the mux in the upper block. */
75         lower_block = get_nodes_block(mux);
76         assert(lower_block != 0);
77         part_block(mux);
78         upper_block = get_cur_block();
79
80         /* Create a cond node with two projs and a phi as mux replacement. The
81          * true proj jumps directly to the lower block, the false proj uses a
82          * block in-between, so that the phi can be used to select the result
83          * value from the old mux node in the lower block. */
84         cond        = new_Cond(get_Mux_sel(mux));
85         trueProj    = new_Proj(cond, mode_X, pn_Cond_true);
86         falseProj   = new_Proj(cond, mode_X, pn_Cond_false);
87         falseBlock  = new_Block(1, &falseProj);
88         mux_jmps[0] = trueProj;
89         mux_jmps[1] = new_r_Jmp(falseBlock);
90
91         /* Kill the jump from upper to lower block and replace the in array. */
92         assert(get_Block_n_cfgpreds(lower_block) == 1);
93         kill_node(get_Block_cfgpred(lower_block, 0));
94         set_irn_in(lower_block, 2, mux_jmps);
95
96         /* Combine the two control flows with a phi to select the correct value
97          * and use it to replace the mux. */
98         set_cur_block(lower_block);
99
100         mux_values[0] = get_Mux_true(mux);
101         mux_values[1] = get_Mux_false(mux);
102         phi = new_Phi(2, mux_values, get_irn_mode(mux));
103         exchange(mux, phi);
104
105         /* Add links and update phi node lists, for the next part_block() call.
106          * lower_block and upper_block have been updated by part_block(). Link
107          * the projs with the cond, and the new block with the macroblock. */
108         set_irn_link(trueProj,   get_irn_link(cond));
109         set_irn_link(falseProj,  get_irn_link(cond));
110         set_irn_link(cond,       trueProj);
111         set_irn_link(cond,       falseProj);
112
113         mbh = get_Block_MacroBlock(falseBlock);
114         set_irn_link(falseBlock, get_irn_link(mbh));
115         set_irn_link(mbh,        falseBlock);
116
117         add_Block_phi(lower_block, phi);
118 }
119
120 void lower_mux(ir_graph *irg, lower_mux_callback *cb_func)
121 {
122         int            i;
123         walk_env_t     env;
124         ir_resources_t resources = IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST;
125
126         /* Scan the graph for mux nodes to lower. */
127         env.cb_func = cb_func;
128         env.muxes   = NEW_ARR_F(ir_node*, 0);
129         irg_walk_graph(irg, find_mux_nodes, 0, &env);
130
131         /* This is required by part_block() later. */
132         ir_reserve_resources(irg, resources);
133         collect_phiprojs(irg);
134
135         for (i = 0; i < ARR_LEN(env.muxes); i++) {
136                 lower_mux_node(env.muxes[i]);
137         }
138
139         /* Cleanup, verify the graph. */
140         ir_free_resources(irg, resources);
141
142         set_irg_outs_inconsistent(irg);
143         set_irg_doms_inconsistent(irg);
144         set_irg_extblk_inconsistent(irg);
145         set_irg_loopinfo_inconsistent(irg);
146 }