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.
22 #include "bechordal_t.h"
23 #include "besched_t.h"
26 #include "iredges_t.h"
36 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
39 #define is_Call(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_call)
41 /* lowering walker environment */
42 typedef struct _lower_env_t {
43 be_chordal_env_t *chord_env;
45 firm_dbg_module_t *dbg_module;
48 /* holds a perm register pair */
49 typedef struct _reg_pair_t {
50 const arch_register_t *in_reg; /**< a perm IN register */
51 ir_node *in_node; /**< the in node to which the register belongs */
53 const arch_register_t *out_reg; /**< a perm OUT register */
54 ir_node *out_node; /**< the out node to which the register belongs */
56 int checked; /**< indicates whether the pair was check for cycle or not */
59 typedef enum _perm_type_t {
66 /* structure to represent cycles or chains in a perm */
67 typedef struct _perm_cycle_t {
68 const arch_register_t **elems; /**< the registers in the cycle */
69 int n_elems; /**< number of elements in the cycle */
70 perm_type_t type; /**< type (CHAIN or CYCLE) */
73 /* Compare the in registers of two register pairs */
74 static int compare_reg_pair(const void *a, const void *b) {
75 const reg_pair_t *pair_a = a;
76 const reg_pair_t *pair_b = b;
78 if (pair_a->in_reg->index > pair_b->in_reg->index)
84 /* returns the number register pairs marked as checked */
85 static int get_n_checked_pairs(reg_pair_t *pairs, int n) {
88 for (i = 0; i < n; i++) {
97 * Gets the node corresponding to a register from an array of register pairs.
98 * NOTE: The given registers pairs and the register to look for must belong
99 * to the same register class.
101 * @param pairs The array of register pairs
102 * @param n The number of pairs
103 * @param reg The register to look for
104 * @param in_out 0 == look for IN register, 1 == look for OUT register
105 * @return The corresponding node or NULL if not found
107 static ir_node *get_node_for_register(reg_pair_t *pairs, int n, const arch_register_t *reg, int in_out) {
111 for (i = 0; i < n; i++) {
112 /* out register matches */
113 if (pairs[i].out_reg->index == reg->index)
114 return pairs[i].out_node;
118 for (i = 0; i < n; i++) {
119 /* in register matches */
120 if (pairs[i].in_reg->index == reg->index)
121 return pairs[i].in_node;
129 * Gets the index in the register pair array where the in/out register
130 * corresponds to reg_idx.
132 * @param pairs The array of register pairs
133 * @param n The number of pairs
134 * @param reg The register index to look for
135 * @param in_out 0 == look for IN register, 1 == look for OUT register
136 * @return The corresponding index in pairs or -1 if not found
138 static int get_pairidx_for_regidx(reg_pair_t *pairs, int n, int reg_idx, int in_out) {
142 for (i = 0; i < n; i++) {
143 /* out register matches */
144 if (pairs[i].out_reg->index == reg_idx)
149 for (i = 0; i < n; i++) {
150 /* in register matches */
151 if (pairs[i].in_reg->index == reg_idx)
160 * Gets an array of register pairs and tries to identify a cycle or chain starting
163 * @param cycle Variable to hold the cycle
164 * @param pairs Array of register pairs
165 * @param start Index to start
166 * @return The cycle or chain
168 static perm_cycle_t *get_perm_cycle(perm_cycle_t *cycle, reg_pair_t *pairs, int n, int start) {
169 int head = pairs[start].in_reg->index;
170 int cur_idx = pairs[start].out_reg->index;
171 int cur_pair_idx = start;
172 int n_pairs_done = get_n_checked_pairs(pairs, n);
174 perm_type_t cycle_tp = PERM_CYCLE;
176 /* We could be right in the middle of a chain, so we need to find the start */
177 while (head != cur_idx) {
178 /* goto previous register in cycle or chain */
179 cur_pair_idx = get_pairidx_for_regidx(pairs, n, head, 1);
181 if (cur_pair_idx < 0) {
182 cycle_tp = PERM_CHAIN;
186 head = pairs[cur_pair_idx].in_reg->index;
187 start = cur_pair_idx;
191 /* assume worst case: all remaining pairs build a cycle or chain */
192 cycle->elems = xcalloc((n - n_pairs_done) * 2, sizeof(cycle->elems[0]));
193 cycle->n_elems = 2; /* initial number of elements is 2 */
194 cycle->elems[0] = pairs[start].in_reg;
195 cycle->elems[1] = pairs[start].out_reg;
196 cycle->type = cycle_tp;
197 cur_idx = pairs[start].out_reg->index;
200 /* check for cycle or end of a chain */
201 while (cur_idx != head) {
202 /* goto next register in cycle or chain */
203 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cur_idx, 0);
205 if (cur_pair_idx < 0)
208 cur_idx = pairs[cur_pair_idx].out_reg->index;
210 /* it's not the first element: insert it */
211 if (cur_idx != head) {
212 cycle->elems[idx++] = pairs[cur_pair_idx].out_reg;
216 /* we are there where we started -> CYCLE */
217 cycle->type = PERM_CYCLE;
221 /* mark all pairs having one in/out register with cycle in common as checked */
222 for (idx = 0; idx < cycle->n_elems; idx++) {
223 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 0);
225 if (cur_pair_idx >= 0)
226 pairs[cur_pair_idx].checked = 1;
228 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 1);
230 if (cur_pair_idx >= 0)
231 pairs[cur_pair_idx].checked = 1;
238 * Lowers a perm node. Resolves cycles and creates a bunch of
239 * copy and swap operations to permute registers.
240 * Note: The caller of this function has to make sure, that irn
243 * @param irn The perm node
244 * @param block The block the perm node belongs to
245 * @param walk_env The environment
247 static void lower_perm_node(ir_node *irn, void *walk_env) {
248 const arch_register_class_t *reg_class;
249 const arch_env_t *arch_env;
250 firm_dbg_module_t *mod;
251 lower_env_t *env = walk_env;
253 const ir_edge_t *edge;
255 int n, i, pn, do_copy, j;
256 ir_node *sched_point, *block, *in[2];
257 ir_node *arg1, *arg2, *res1, *res2;
258 ir_node *cpyxchg = NULL;
260 arch_env = env->chord_env->birg->main_env->arch_env;
261 do_copy = env->do_copy;
262 mod = env->dbg_module;
263 block = get_nodes_block(irn);
266 Get the schedule predecessor node to the perm
267 NOTE: This works with auto-magic. If we insert the
268 new copy/exchange nodes after this node, everything
271 sched_point = sched_prev(irn);
272 DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
273 assert(sched_point && "Perm is not scheduled or has no predecessor");
275 n = get_irn_arity(irn);
276 assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
278 reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
279 pairs = alloca(n * sizeof(pairs[0]));
281 /* build the list of register pairs (in, out) */
283 foreach_out_edge(irn, edge) {
284 pairs[i].out_node = get_edge_src_irn(edge);
285 pn = get_Proj_proj(pairs[i].out_node);
286 pairs[i].in_node = get_irn_n(irn, pn);
288 pairs[i].in_reg = arch_get_irn_register(arch_env, pairs[i].in_node);
289 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
291 pairs[i].checked = 0;
295 /* sort the register pairs by the indices of the in registers */
296 qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
298 /* Mark all equal pairs as checked, and exchange the OUT proj with
300 for (i = 0; i < n; i++) {
301 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
302 DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
303 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
305 /* We have to check for a special case:
306 The in-node could be a Proj from a Perm. In this case,
307 we need to correct the projnum */
308 if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
309 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
312 /* remove the proj from the schedule */
313 sched_remove(pairs[i].out_node);
315 /* reroute the edges from the proj to the argument */
316 edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
318 pairs[i].checked = 1;
322 /* Set do_copy to 0 if it's on but we have no free register */
327 /* check for cycles and chains */
328 while (get_n_checked_pairs(pairs, n) < n) {
331 /* go to the first not-checked pair */
332 while (pairs[i].checked) i++;
333 cycle = xcalloc(1, sizeof(*cycle));
334 cycle = get_perm_cycle(cycle, pairs, n, i);
336 DB((mod, LEVEL_1, "%+F: following %s created:\n ", irn, cycle->type == PERM_CHAIN ? "chain" : "cycle"));
337 for (j = 0; j < cycle->n_elems; j++) {
338 DB((mod, LEVEL_1, " %s", cycle->elems[j]->name));
340 DB((mod, LEVEL_1, "\n"));
342 /* We don't need to do anything if we have a Perm with two
343 elements which represents a cycle, because those nodes
344 already represent exchange nodes */
345 if (n == 2 && cycle->type == PERM_CYCLE) {
350 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
351 // the copy cascade (this reduces the cycle into a chain)
353 /* build copy/swap nodes from back to front */
354 for (i = cycle->n_elems - 2; i >= 0; i--) {
355 arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
356 arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
358 res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
359 res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
362 If we have a cycle and don't copy: we need to create exchange nodes
363 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
364 IN_1 = in node with register i
365 IN_2 = in node with register i + 1
366 OUT_1 = out node with register i + 1
367 OUT_2 = out node with register i
369 if (cycle->type == PERM_CYCLE && !do_copy) {
373 /* At this point we have to handle the following problem: */
375 /* If we have a cycle with more than two elements, then */
376 /* this could correspond to the following Perm node: */
378 /* +----+ +----+ +----+ */
379 /* | r1 | | r2 | | r3 | */
380 /* +-+--+ +-+--+ +--+-+ */
383 /* +-+--------+---------+-+ */
385 /* +-+--------+---------+-+ */
388 /* +-+--+ +-+--+ +--+-+ */
389 /* |Proj| |Proj| |Proj| */
390 /* | r2 | | r3 | | r1 | */
391 /* +----+ +----+ +----+ */
393 /* This node is about to be split up into two 2x Perm's */
394 /* for which we need 4 Proj's and the one additional Proj */
395 /* of the first Perm has to be one IN of the second. So in */
396 /* general we need to create one additional Proj for each */
397 /* "middle" Perm and set this to one in node of the successor */
400 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
401 irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
402 DBG((mod, LEVEL_1, "%+F (%+F, %s) and (%+F, %s)\n",
403 irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
405 cpyxchg = be_new_Perm(reg_class, env->chord_env->irg, block, 2, in);
408 /* cycle is not done yet */
409 int pidx = get_pairidx_for_regidx(pairs, n, cycle->elems[i]->index, 0);
411 /* create intermediate proj */
412 res2 = new_r_Proj(get_irn_irg(irn), block, cpyxchg, get_irn_mode(res1), 0);
414 /* set as in for next Perm */
415 pairs[pidx].in_node = res2;
423 set_Proj_pred(res2, cpyxchg);
424 set_Proj_proj(res2, 0);
425 set_Proj_pred(res1, cpyxchg);
426 set_Proj_proj(res1, 1);
428 sched_add_after(sched_point, res1);
429 sched_add_after(sched_point, res2);
431 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
432 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
435 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
436 irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
438 cpyxchg = be_new_Copy(reg_class, env->chord_env->irg, block, arg1);
439 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
441 /* remove the proj from the schedule */
444 /* exchange copy node and proj */
445 exchange(res2, cpyxchg);
448 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
449 sched_add_after(sched_point, cpyxchg);
450 /* set the new scheduling point */
451 sched_point = cpyxchg;
453 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
456 free((void *) cycle->elems);
460 /* remove the perm from schedule */
467 * Checks if node has a should_be_different constraint in output
468 * and adds a Keep then to assure the constraint.
470 static void assure_different_constraint(ir_node *irn, be_irg_t *birg) {
471 const arch_env_t *arch_env = birg->main_env->arch_env;
472 const arch_register_req_t *req;
473 arch_register_req_t req_temp;
474 ir_node *in[2], *keep;
475 firm_dbg_module_t *mod = firm_dbg_register("firm.be.lower");
477 req = arch_get_register_req(arch_env, &req_temp, irn, -1);
479 if (req && arch_register_req_is(req, should_be_different)) {
480 /* We found a should_be_different constraint. */
481 assert(req->other_different && "missing irn for constraint");
484 in[1] = req->other_different;
486 keep = be_new_Keep(req->cls, birg->irg, get_nodes_block(irn), 2, in);
487 DBG((mod, LEVEL_1, "created %+F for %+F to assure should_be_different\n", keep, irn));
494 * Calls the functions to assure register constraints.
496 * @param irn The node to be checked for lowering
497 * @param walk_env The walker environment
499 static void assure_constraints_walker(ir_node *irn, void *walk_env) {
503 if (mode_is_datab(get_irn_mode(irn)))
504 assure_different_constraint(irn, walk_env);
512 * Walks over all nodes to assure register constraints.
514 * @param birg The birg structure containing the irg
516 void assure_constraints(be_irg_t *birg) {
517 irg_walk_blkwise_graph(birg->irg, NULL, assure_constraints_walker, birg);
523 * Calls the corresponding lowering function for the node.
525 * @param irn The node to be checked for lowering
526 * @param walk_env The walker environment
528 static void lower_nodes_after_ra_walker(ir_node *irn, void *walk_env) {
529 lower_env_t *env = walk_env;
530 const arch_env_t *arch_env = env->chord_env->birg->main_env->arch_env;
532 if (!is_Block(irn) && !is_Proj(irn)) {
533 if (is_Perm(arch_env, irn)) {
534 lower_perm_node(irn, walk_env);
544 * Walks over all blocks in an irg and performs lowering need to be
545 * done after register allocation (e.g. perm lowering).
547 * @param chord_env The chordal environment containing the irg
548 * @param do_copy 1 == resolve cycles with a free reg if available
550 void lower_nodes_after_ra(be_chordal_env_t *chord_env, int do_copy) {
553 env.chord_env = chord_env;
554 env.do_copy = do_copy;
555 env.dbg_module = firm_dbg_register("firm.be.lower");
557 irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_after_ra_walker, &env);