2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Replaces Mux nodes with control-flow
33 #include "irgraph_t.h"
39 typedef struct walk_env {
40 lower_mux_callback *cb_func;
44 static void find_mux_nodes(ir_node *mux, void *ctx)
46 walk_env_t *env = ctx;
48 /* Skip non-mux nodes. */
52 /* Skip nodes, depending on the callback function. */
53 if (env->cb_func != NULL && !env->cb_func(mux)) {
58 ARR_APP1(ir_node*, env->muxes, mux);
61 static void lower_mux_node(ir_node* mux)
70 ir_node *mux_values[2];
73 /* Split the block in two halfs, with the mux in the upper block. */
74 lower_block = get_nodes_block(mux);
75 assert(lower_block != 0);
77 upper_block = get_nodes_block(mux);
79 /* Create a cond node with two projs and a phi as mux replacement. The
80 * true proj jumps directly to the lower block, the false proj uses a
81 * block in-between, so that the phi can be used to select the result
82 * value from the old mux node in the lower block. */
83 cond = new_r_Cond(upper_block, get_Mux_sel(mux));
84 trueProj = new_r_Proj(upper_block, cond, mode_X, pn_Cond_true);
85 falseProj = new_r_Proj(upper_block, cond, mode_X, pn_Cond_false);
86 falseBlock = new_r_Block(current_ir_graph, 1, &falseProj);
87 mux_jmps[0] = trueProj;
88 mux_jmps[1] = new_r_Jmp(falseBlock);
90 /* Kill the jump from upper to lower block and replace the in array. */
91 assert(get_Block_n_cfgpreds(lower_block) == 1);
92 kill_node(get_Block_cfgpred(lower_block, 0));
93 set_irn_in(lower_block, 2, mux_jmps);
95 /* Combine the two control flows with a phi to select the correct value
96 * and use it to replace the mux. */
97 mux_values[0] = get_Mux_true(mux);
98 mux_values[1] = get_Mux_false(mux);
99 phi = new_r_Phi(lower_block, 2, mux_values, get_irn_mode(mux));
102 /* Add links and update phi node lists, for the next part_block() call.
103 * lower_block and upper_block have been updated by part_block(). Link
104 * the projs with the cond. */
105 set_irn_link(trueProj, get_irn_link(cond));
106 set_irn_link(falseProj, trueProj);
107 set_irn_link(cond, falseProj);
109 add_Block_phi(lower_block, phi);
112 void lower_mux(ir_graph *irg, lower_mux_callback *cb_func)
117 /* Scan the graph for mux nodes to lower. */
118 env.cb_func = cb_func;
119 env.muxes = NEW_ARR_F(ir_node*, 0);
120 irg_walk_graph(irg, find_mux_nodes, 0, &env);
122 n_muxes = ARR_LEN(env.muxes);
124 ir_resources_t resources = IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST;
126 /* This is required by part_block() later. */
127 ir_reserve_resources(irg, resources);
128 collect_phiprojs(irg);
130 for (i = 0; i < n_muxes; ++i) {
131 lower_mux_node(env.muxes[i]);
134 /* Cleanup, verify the graph. */
135 ir_free_resources(irg, resources);
137 set_irg_outs_inconsistent(irg);
138 set_irg_doms_inconsistent(irg);
139 set_irg_extblk_inconsistent(irg);
140 set_irg_loopinfo_inconsistent(irg);
142 DEL_ARR_F(env.muxes);