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