2 * Author: Christian Wuerdig
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
8 * Performs lowering of perm nodes and spill/reload optimization.
17 #include "bechordal_t.h"
25 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
27 /* lowering walker environment */
28 typedef struct _lower_env_t {
29 be_chordal_env_t *chord_env;
31 firm_dbg_module_t *dbg_module;
34 /* holds a perm register pair */
35 typedef struct _reg_pair_t {
36 const arch_register_t *in_reg; /**< a perm IN register */
37 ir_node *in_node; /**< the in node to which the register belongs */
39 const arch_register_t *out_reg; /**< a perm OUT register */
40 ir_node *out_node; /**< the out node to which the register belongs */
42 int checked; /**< indicates whether the pair was check for cycle or not */
45 typedef enum _perm_type_t {
52 /* structure to represent cycles or chains in a perm */
53 typedef struct _perm_cycle_t {
54 const arch_register_t **elems; /**< the registers in the cycle */
55 int n_elems; /**< number of elements in the cycle */
56 perm_type_t type; /**< type (CHAIN or CYCLE) */
59 /* Compare the in registers of two register pairs */
60 static int compare_reg_pair(const void *a, const void *b) {
61 const reg_pair_t *pair_a = a;
62 const reg_pair_t *pair_b = b;
64 if (pair_a->in_reg->index > pair_b->in_reg->index)
70 /* returns the number register pairs marked as checked */
71 static int get_n_checked_pairs(reg_pair_t *pairs, int n) {
74 for (i = 0; i < n; i++) {
83 * Gets the node corresponding to a register from an array of register pairs.
84 * NOTE: The given registers pairs and the register to look for must belong
85 * to the same register class.
87 * @param pairs The array of register pairs
88 * @param n The number of pairs
89 * @param reg The register to look for
90 * @param in_out 0 == look for IN register, 1 == look for OUT register
91 * @return The corresponding node or NULL if not found
93 static ir_node *get_node_for_register(reg_pair_t *pairs, int n, const arch_register_t *reg, int in_out) {
97 for (i = 0; i < n; i++) {
98 /* out register matches */
99 if (pairs[i].out_reg->index == reg->index)
100 return pairs[i].out_node;
104 for (i = 0; i < n; i++) {
105 /* in register matches */
106 if (pairs[i].in_reg->index == reg->index)
107 return pairs[i].in_node;
115 * Gets the index in the register pair array where the in/out register
116 * corresponds to reg_idx.
118 * @param pairs The array of register pairs
119 * @param n The number of pairs
120 * @param reg The register index to look for
121 * @param in_out 0 == look for IN register, 1 == look for OUT register
122 * @return The corresponding index in pairs or -1 if not found
124 static int get_pairidx_for_regidx(reg_pair_t *pairs, int n, int reg_idx, int in_out) {
128 for (i = 0; i < n; i++) {
129 /* out register matches */
130 if (pairs[i].out_reg->index == reg_idx)
135 for (i = 0; i < n; i++) {
136 /* in register matches */
137 if (pairs[i].in_reg->index == reg_idx)
146 * Gets an array of register pairs and tries to identify a cycle or chain starting
149 * @param cycle Variable to hold the cycle
150 * @param pairs Array of register pairs
151 * @param start Index to start
152 * @return The cycle or chain
154 static perm_cycle_t *get_perm_cycle(perm_cycle_t *cycle, reg_pair_t *pairs, int n, int start) {
155 int head = pairs[start].in_reg->index;
156 int cur_idx = pairs[start].out_reg->index;
157 int cur_pair_idx = start;
158 int n_pairs_done = get_n_checked_pairs(pairs, n) + 1;
161 /* assume worst case: all remaining pairs build a cycle or chain */
162 cycle->elems = calloc(n - n_pairs_done, sizeof(cycle->elems[0]));
163 cycle->n_elems = 2; /* initial number of elements is 2 */
164 cycle->elems[0] = pairs[start].in_reg;
165 cycle->elems[1] = pairs[start].out_reg;
166 cycle->type = PERM_CHAIN; /* default is CHAIN, only changed when we found a cycle */
168 /* mark the first pair as checked */
169 pairs[start].checked = 1;
172 /* check for cycle or end of a chain */
173 while (cur_idx != head && n_pairs_done < n) {
174 /* goto next register in cycle or chain */
175 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cur_idx, 0);
177 if (cur_pair_idx < 0)
180 cur_idx = pairs[cur_pair_idx].out_reg->index;
182 /* it's not the first element: insert it */
183 if (cur_idx != head) {
184 cycle->elems[idx++] = pairs[cur_pair_idx].out_reg;
190 /* we are there where we started -> CYCLE */
191 cycle->type = PERM_CYCLE;
194 /* mark the pair as checked */
195 pairs[cur_pair_idx].checked = 1;
202 * Lowers a perm node. Resolves cycles and creates a bunch of
203 * copy and swap operations to permute registers.
205 * @param irn The perm node
206 * @param block The block the perm node belongs to
207 * @param walk_env The environment
209 static void lower_perm_node(ir_node *irn, void *walk_env) {
210 const be_node_factory_t *fact;
211 const arch_register_class_t *reg_class;
212 const arch_env_t *arch_env;
213 firm_dbg_module_t *mod;
214 lower_env_t *env = walk_env;
216 const ir_edge_t *edge;
218 int n, i, pn, do_copy;
219 ir_node *sched_point, *block, *in[2];
220 ir_node *arg1, *arg2, *res1, *res2;
221 ir_node *cpyxchg = NULL;
226 fact = env->chord_env->main_env->node_factory;
227 arch_env = env->chord_env->main_env->arch_env;
228 do_copy = env->do_copy;
229 mod = env->dbg_module;
230 block = get_nodes_block(irn);
233 Note: A Proj on a Perm will cause is_Perm()
234 also to return true */
235 if (is_Proj(irn) || ! is_Perm(arch_env, irn))
239 Get the schedule predecessor node to the perm
240 NOTE: This works with auto-magic. If we insert the
241 new copy/exchange nodes after this node, everything
244 sched_point = sched_prev(irn);
245 DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
246 assert(sched_point && "Perm is not scheduled or has no predecessor");
248 n = get_irn_arity(irn);
249 assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
251 reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
252 pairs = calloc(n, sizeof(pairs[0]));
254 /* build the list of register pairs (in, out) */
256 foreach_out_edge(irn, edge) {
257 pairs[i].out_node = get_edge_src_irn(edge);
258 pn = get_Proj_proj(pairs[i].out_node);
259 pairs[i].in_node = get_irn_n(irn, pn);
261 pairs[i].in_reg = arch_get_irn_register(arch_env, pairs[i].in_node);
262 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
264 pairs[i].checked = 0;
268 /* sort the register pairs by the indices of the in registers */
269 qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
271 /* Mark all equal pairs as checked, and exchange the OUT proj with
273 for (i = 0; i < n; i++) {
274 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
275 DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
276 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
278 /* We have to check for a special case:
279 The in-node could be a Proj from a Perm. In this case,
280 we need to correct the projnum */
281 if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
282 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
285 /* remove the proj from the schedule */
286 sched_remove(pairs[i].out_node);
288 /* reroute the edges from the proj to the argument */
289 edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
291 pairs[i].checked = 1;
295 /* Set do_copy to 0 if it's on but we have no free register */
300 /* check for cycles and chains */
301 while (get_n_checked_pairs(pairs, n) < n) {
304 /* go to the first not-checked pair */
305 while (pairs[i].checked) i++;
306 cycle = calloc(1, sizeof(*cycle));
307 cycle = get_perm_cycle(cycle, pairs, n, i);
309 /* We don't need to do anything if we have a Perm with two
310 elements which represents a cycle, because those nodes
311 already represent exchange nodes */
312 if (n == 2 && cycle->type == PERM_CYCLE) {
317 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
318 // the copy cascade (this reduces the cycle into a chain)
320 /* build copy/swap nodes from back to front */
321 for (i = cycle->n_elems - 2; i >= 0; i--) {
322 arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
323 arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
325 res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
326 res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
329 If we have a cycle and don't copy: we need to create exchange nodes
330 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
331 IN_1 = in node with register i
332 IN_2 = in node with register i + 1
333 OUT_1 = out node with register i + 1
334 OUT_2 = out node with register i
336 if (cycle->type == PERM_CYCLE && !do_copy) {
340 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
341 irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
342 DBG((mod, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
343 irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
345 cpyxchg = new_Perm(fact, reg_class, env->chord_env->irg, block, 2, in);
350 set_Proj_pred(res2, cpyxchg);
351 set_Proj_proj(res2, 0);
352 set_Proj_pred(res1, cpyxchg);
353 set_Proj_proj(res1, 1);
355 sched_add_after(sched_point, res1);
356 sched_add_after(sched_point, res2);
358 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
359 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
362 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
363 irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
365 cpyxchg = new_Copy(fact, reg_class, env->chord_env->irg, block, arg1);
366 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
368 /* remove the proj from the schedule */
371 /* exchange copy node and proj */
372 exchange(res2, cpyxchg);
375 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
376 sched_add_after(sched_point, cpyxchg);
378 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
381 // free(cycle->elems);
385 /* remove the perm from schedule */
390 * Walks over all blocks in an irg and performs perm lowering.
392 * @param chord_env The chordal environment containing the irg
393 * @param do_copy 1 == resolve cycles with a free reg if available
395 void lower_perms(be_chordal_env_t *chord_env, int do_copy) {
398 env.chord_env = chord_env;
399 env.do_copy = do_copy;
400 env.dbg_module = firm_dbg_register("ir.be.lower");
402 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_perm_node, &env);