copy result mode on final transformations (lea->add and sub->neg-add)
[libfirm] / ir / be / belower.c
1 /**
2  * Author:      Christian Wuerdig
3  * Date:        2005/12/14
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  * CVS-Id:      $Id$
7  *
8  * Performs lowering of perm nodes and spill/reload optimization.
9  */
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdlib.h>
15
16 #include "ircons.h"
17 #include "debug.h"
18 #include "irhooks.h"
19
20 #include "bearch.h"
21 #include "belower.h"
22 #include "benode_t.h"
23 #include "bechordal_t.h"
24 #include "besched_t.h"
25
26 #include "irgmod.h"
27 #include "iredges_t.h"
28 #include "irgwalk.h"
29
30 #ifdef HAVE_MALLOC_H
31  #include <malloc.h>
32 #endif
33 #ifdef HAVE_ALLOCA_H
34  #include <alloca.h>
35 #endif
36
37 #undef is_Perm
38 #define is_Perm(arch_env, irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
39
40 /* collect static data about perms */
41 typedef struct _perm_stat_t {
42         const arch_register_class_t *cls; /**< the current register class */
43     int  *perm_size_ar;       /**< the sizes of all perms in an irg */
44     int  *real_perm_size_ar;  /**< the sizes of all perms in an irg */
45         int  *chain_len_ar;       /**< the sizes of all chains for all perms */
46         int  *cycle_len_ar;       /**< the siyes of all cycles for all perms */
47         int   num_perms;          /**< number of all perms */
48         int   num_real_perms;     /**< number of all perms */
49         int   num_chains;         /**< the number of all chains */
50         int   num_cycles;         /**< the number of all cycles */
51 } perm_stat_t;
52
53 /* lowering walker environment */
54 typedef struct _lower_env_t {
55         be_chordal_env_t  *chord_env;
56         unsigned           do_copy:1;
57         unsigned           do_stat:1;
58         unsigned           pstat_n:30;
59         perm_stat_t      **pstat;
60         DEBUG_ONLY(firm_dbg_module_t *dbg_module;)
61 } lower_env_t;
62
63 /* holds a perm register pair */
64 typedef struct _reg_pair_t {
65         const arch_register_t *in_reg;    /**< a perm IN register */
66         ir_node               *in_node;   /**< the in node to which the register belongs */
67
68         const arch_register_t *out_reg;   /**< a perm OUT register */
69         ir_node               *out_node;  /**< the out node to which the register belongs */
70
71         int                    checked;   /**< indicates whether the pair was check for cycle or not */
72 } reg_pair_t;
73
74 typedef enum _perm_type_t {
75         PERM_CYCLE,
76         PERM_CHAIN,
77         PERM_SWAP,
78         PERM_COPY
79 } perm_type_t;
80
81 /* structure to represent cycles or chains in a perm */
82 typedef struct _perm_cycle_t {
83         const arch_register_t **elems;       /**< the registers in the cycle */
84         int                     n_elems;     /**< number of elements in the cycle */
85         perm_type_t             type;        /**< type (CHAIN or CYCLE) */
86 } perm_cycle_t;
87
88 /* Compare the in registers of two register pairs */
89 static int compare_reg_pair(const void *a, const void *b) {
90         const reg_pair_t *pair_a = a;
91         const reg_pair_t *pair_b = b;
92
93         if (pair_a->in_reg->index > pair_b->in_reg->index)
94                 return 1;
95         else
96                 return -1;
97 }
98
99 /* returns the number register pairs marked as checked */
100 static int get_n_checked_pairs(reg_pair_t *pairs, int n) {
101         int i, n_checked = 0;
102
103         for (i = 0; i < n; i++) {
104                 if (pairs[i].checked)
105                         n_checked++;
106         }
107
108         return n_checked;
109 }
110
111 /**
112  * Gets the node corresponding to a register from an array of register pairs.
113  * NOTE: The given registers pairs and the register to look for must belong
114  *       to the same register class.
115  *
116  * @param pairs  The array of register pairs
117  * @param n      The number of pairs
118  * @param reg    The register to look for
119  * @param in_out 0 == look for IN register, 1 == look for OUT register
120  * @return The corresponding node or NULL if not found
121  */
122 static ir_node *get_node_for_register(reg_pair_t *pairs, int n, const arch_register_t *reg, int in_out) {
123         int i;
124
125         if (in_out) {
126                 for (i = 0; i < n; i++) {
127                         /* out register matches */
128                         if (pairs[i].out_reg->index == reg->index)
129                                 return pairs[i].out_node;
130                 }
131         }
132         else {
133                 for (i = 0; i < n; i++) {
134                         /* in register matches */
135                         if (pairs[i].in_reg->index == reg->index)
136                                 return pairs[i].in_node;
137                 }
138         }
139
140         return NULL;
141 }
142
143 /**
144  * Gets the index in the register pair array where the in/out register
145  * corresponds to reg_idx.
146  *
147  * @param pairs  The array of register pairs
148  * @param n      The number of pairs
149  * @param reg    The register index to look for
150  * @param in_out 0 == look for IN register, 1 == look for OUT register
151  * @return The corresponding index in pairs or -1 if not found
152  */
153 static int get_pairidx_for_regidx(reg_pair_t *pairs, int n, int reg_idx, int in_out) {
154         int i;
155
156         if (in_out) {
157                 for (i = 0; i < n; i++) {
158                         /* out register matches */
159                         if (pairs[i].out_reg->index == reg_idx)
160                                 return i;
161                 }
162         }
163         else {
164                 for (i = 0; i < n; i++) {
165                         /* in register matches */
166                         if (pairs[i].in_reg->index == reg_idx)
167                                 return i;
168                 }
169         }
170
171         return -1;
172 }
173
174 /**
175  * Gets an array of register pairs and tries to identify a cycle or chain starting
176  * at position start.
177  *
178  * @param cycle Variable to hold the cycle
179  * @param pairs Array of register pairs
180  * @param start Index to start
181  * @return The cycle or chain
182  */
183 static perm_cycle_t *get_perm_cycle(perm_cycle_t *cycle, reg_pair_t *pairs, int n, int start) {
184         int head         = pairs[start].in_reg->index;
185         int cur_idx      = pairs[start].out_reg->index;
186         int cur_pair_idx = start;
187         int n_pairs_done = get_n_checked_pairs(pairs, n);
188         int idx;
189         perm_type_t cycle_tp = PERM_CYCLE;
190
191         /* We could be right in the middle of a chain, so we need to find the start */
192         while (head != cur_idx) {
193                 /* goto previous register in cycle or chain */
194                 cur_pair_idx = get_pairidx_for_regidx(pairs, n, head, 1);
195
196                 if (cur_pair_idx < 0) {
197                         cycle_tp = PERM_CHAIN;
198                         break;
199                 }
200                 else {
201                         head  = pairs[cur_pair_idx].in_reg->index;
202                         start = cur_pair_idx;
203                 }
204         }
205
206         /* assume worst case: all remaining pairs build a cycle or chain */
207         cycle->elems    = xcalloc((n - n_pairs_done) * 2, sizeof(cycle->elems[0]));
208         cycle->n_elems  = 2;  /* initial number of elements is 2 */
209         cycle->elems[0] = pairs[start].in_reg;
210         cycle->elems[1] = pairs[start].out_reg;
211         cycle->type     = cycle_tp;
212         cur_idx         = pairs[start].out_reg->index;
213
214         idx = 2;
215         /* check for cycle or end of a chain */
216         while (cur_idx != head) {
217                 /* goto next register in cycle or chain */
218                 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cur_idx, 0);
219
220                 if (cur_pair_idx < 0)
221                         break;
222
223                 cur_idx = pairs[cur_pair_idx].out_reg->index;
224
225                 /* it's not the first element: insert it */
226                 if (cur_idx != head) {
227                         cycle->elems[idx++] = pairs[cur_pair_idx].out_reg;
228                         cycle->n_elems++;
229                 }
230                 else {
231                         /* we are there where we started -> CYCLE */
232                         cycle->type = PERM_CYCLE;
233                 }
234         }
235
236         /* mark all pairs having one in/out register with cycle in common as checked */
237         for (idx = 0; idx < cycle->n_elems; idx++) {
238                 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 0);
239
240                 if (cur_pair_idx >= 0)
241                         pairs[cur_pair_idx].checked = 1;
242
243                 cur_pair_idx = get_pairidx_for_regidx(pairs, n, cycle->elems[idx]->index, 1);
244
245                 if (cur_pair_idx >= 0)
246                         pairs[cur_pair_idx].checked = 1;
247         }
248
249         return cycle;
250 }
251
252 /**
253  * Lowers a perm node.  Resolves cycles and creates a bunch of
254  * copy and swap operations to permute registers.
255  * Note: The caller of this function has to make sure, that irn
256  *       is a Perm node.
257  *
258  * @param irn      The perm node
259  * @param block    The block the perm node belongs to
260  * @param walk_env The environment
261  */
262 static void lower_perm_node(ir_node *irn, void *walk_env) {
263         const arch_register_class_t *reg_class;
264         const arch_env_t            *arch_env;
265         lower_env_t     *env         = walk_env;
266         perm_stat_t    **pstat       = env->pstat;
267         int              pstat_idx   = -1;
268         int              real_size   = 0;
269         int              n, i, pn, do_copy, j, n_ops;
270         reg_pair_t      *pairs;
271         const ir_edge_t *edge;
272         perm_cycle_t    *cycle;
273         ir_node         *sched_point, *block, *in[2];
274         ir_node         *arg1, *arg2, *res1, *res2;
275         ir_node         *cpyxchg = NULL;
276         DEBUG_ONLY(firm_dbg_module_t *mod;)
277
278         arch_env = env->chord_env->birg->main_env->arch_env;
279         do_copy  = env->do_copy;
280         DEBUG_ONLY(mod = env->dbg_module;)
281         block    = get_nodes_block(irn);
282
283         /*
284                 Get the schedule predecessor node to the perm
285                 NOTE: This works with auto-magic. If we insert the
286                         new copy/exchange nodes after this node, everything
287                         should be ok.
288         */
289         sched_point = sched_prev(irn);
290         DBG((mod, LEVEL_1, "sched point is %+F\n", sched_point));
291         assert(sched_point && "Perm is not scheduled or has no predecessor");
292
293         n = get_irn_arity(irn);
294         assert(n == get_irn_n_edges(irn) && "perm's in and out numbers different");
295
296         reg_class = arch_get_irn_register(arch_env, get_irn_n(irn, 0))->reg_class;
297         pairs     = alloca(n * sizeof(pairs[0]));
298
299         if (env->do_stat) {
300                 unsigned i;
301
302                 /* determine index in statistics */
303                 for (i = 0; i < env->pstat_n; i++) {
304                         if (strcmp(pstat[i]->cls->name, reg_class->name) == 0) {
305                                 pstat_idx = i;
306                                 break;
307                         }
308                 }
309                 assert(pstat_idx >= 0 && "could not determine class index for statistics");
310
311                 pstat[pstat_idx]->num_perms++;
312                 pstat[pstat_idx]->perm_size_ar[n - 1]++;
313         }
314
315         /* build the list of register pairs (in, out) */
316         i = 0;
317         foreach_out_edge(irn, edge) {
318                 pairs[i].out_node = get_edge_src_irn(edge);
319                 pn                = get_Proj_proj(pairs[i].out_node);
320                 pairs[i].in_node  = get_irn_n(irn, pn);
321
322                 pairs[i].in_reg  = arch_get_irn_register(arch_env, pairs[i].in_node);
323                 pairs[i].out_reg = arch_get_irn_register(arch_env, pairs[i].out_node);
324
325                 pairs[i].checked = 0;
326                 i++;
327         }
328
329         /* sort the register pairs by the indices of the in registers */
330         qsort(pairs, n, sizeof(pairs[0]), compare_reg_pair);
331
332         /* Mark all equal pairs as checked, and exchange the OUT proj with
333                 the IN node. */
334         for (i = 0; i < n; i++) {
335                 if (pairs[i].in_reg->index == pairs[i].out_reg->index) {
336                         DBG((mod, LEVEL_1, "%+F removing equal perm register pair (%+F, %+F, %s)\n",
337                                 irn, pairs[i].in_node, pairs[i].out_node, pairs[i].out_reg->name));
338
339                         /* We have to check for a special case:
340                                 The in-node could be a Proj from a Perm. In this case,
341                                 we need to correct the projnum */
342                         if (is_Perm(arch_env, pairs[i].in_node) && is_Proj(pairs[i].in_node)) {
343                                 set_Proj_proj(pairs[i].out_node, get_Proj_proj(pairs[i].in_node));
344                         }
345
346                         /* remove the proj from the schedule */
347                         sched_remove(pairs[i].out_node);
348
349                         /* reroute the edges from the proj to the argument */
350                         edges_reroute(pairs[i].out_node, pairs[i].in_node, env->chord_env->irg);
351
352                         pairs[i].checked = 1;
353                 }
354         }
355
356         /* Set do_copy to 0 if it's on but we have no free register */
357         if (do_copy) {
358                 do_copy = 0;
359         }
360
361         if (env->do_stat && get_n_checked_pairs(pairs, n) < n) {
362                 pstat[pstat_idx]->num_real_perms++;
363                 pstat[pstat_idx]->real_perm_size_ar[n - 1]++;
364                 real_size = n - get_n_checked_pairs(pairs, n);
365         }
366
367         hook_be_block_stat_perm(reg_class->name, reg_class->n_regs, irn, block, n, real_size);
368
369         /* check for cycles and chains */
370         while (get_n_checked_pairs(pairs, n) < n) {
371                 i = n_ops = 0;
372
373                 /* go to the first not-checked pair */
374                 while (pairs[i].checked) i++;
375                 cycle = xcalloc(1, sizeof(*cycle));
376                 cycle = get_perm_cycle(cycle, pairs, n, i);
377
378                 DB((mod, LEVEL_1, "%+F: following %s created:\n  ", irn, cycle->type == PERM_CHAIN ? "chain" : "cycle"));
379                 for (j = 0; j < cycle->n_elems; j++) {
380                         DB((mod, LEVEL_1, " %s", cycle->elems[j]->name));
381                 }
382                 DB((mod, LEVEL_1, "\n"));
383
384                 /* statistics */
385                 if (env->do_stat) {
386                         int n_idx = cycle->n_elems - 1;
387                         if (cycle->type == PERM_CHAIN) {
388                                 pstat[pstat_idx]->num_chains++;
389                                 pstat[pstat_idx]->chain_len_ar[n_idx]++;
390                         }
391                         else {
392                                 pstat[pstat_idx]->num_cycles++;
393                                 pstat[pstat_idx]->cycle_len_ar[n_idx]++;
394                         }
395                 }
396
397                 /* We don't need to do anything if we have a Perm with two
398                         elements which represents a cycle, because those nodes
399                         already represent exchange nodes */
400                 if (n == 2 && cycle->type == PERM_CYCLE) {
401                         free(cycle);
402                         continue;
403                 }
404
405 //TODO: - iff PERM_CYCLE && do_copy -> determine free temp reg and insert copy to/from it before/after
406 //        the copy cascade (this reduces the cycle into a chain)
407
408                 /* build copy/swap nodes from back to front */
409                 for (i = cycle->n_elems - 2; i >= 0; i--) {
410                         arg1 = get_node_for_register(pairs, n, cycle->elems[i], 0);
411                         arg2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 0);
412
413                         res1 = get_node_for_register(pairs, n, cycle->elems[i], 1);
414                         res2 = get_node_for_register(pairs, n, cycle->elems[i + 1], 1);
415                         /*
416                                 If we have a cycle and don't copy: we need to create exchange nodes
417                                 NOTE: An exchange node is a perm node with 2 INs and 2 OUTs
418                                 IN_1  = in node with register i
419                                 IN_2  = in node with register i + 1
420                                 OUT_1 = out node with register i + 1
421                                 OUT_2 = out node with register i
422                         */
423                         if (cycle->type == PERM_CYCLE && !do_copy) {
424                                 in[0] = arg1;
425                                 in[1] = arg2;
426
427                                 /* At this point we have to handle the following problem:     */
428                                 /*                                                            */
429                                 /* If we have a cycle with more than two elements, then       */
430                                 /* this could correspond to the following Perm node:          */
431                                 /*                                                            */
432                                 /*   +----+   +----+   +----+                                 */
433                                 /*   | r1 |   | r2 |   | r3 |                                 */
434                                 /*   +-+--+   +-+--+   +--+-+                                 */
435                                 /*     |        |         |                                   */
436                                 /*     |        |         |                                   */
437                                 /*   +-+--------+---------+-+                                 */
438                                 /*   |         Perm         |                                 */
439                                 /*   +-+--------+---------+-+                                 */
440                                 /*     |        |         |                                   */
441                                 /*     |        |         |                                   */
442                                 /*   +-+--+   +-+--+   +--+-+                                 */
443                                 /*   |Proj|   |Proj|   |Proj|                                 */
444                                 /*   | r2 |   | r3 |   | r1 |                                 */
445                                 /*   +----+   +----+   +----+                                 */
446                                 /*                                                            */
447                                 /* This node is about to be split up into two 2x Perm's       */
448                                 /* for which we need 4 Proj's and the one additional Proj     */
449                                 /* of the first Perm has to be one IN of the second. So in    */
450                                 /* general we need to create one additional Proj for each     */
451                                 /* "middle" Perm and set this to one in node of the successor */
452                                 /* Perm.                                                      */
453
454                                 DBG((mod, LEVEL_1, "%+F creating exchange node (%+F, %s) and (%+F, %s) with\n",
455                                         irn, arg1, cycle->elems[i]->name, arg2, cycle->elems[i + 1]->name));
456                                 DBG((mod, LEVEL_1, "%+F                        (%+F, %s) and (%+F, %s)\n",
457                                         irn, res1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
458
459                                 cpyxchg = be_new_Perm(reg_class, env->chord_env->irg, block, 2, in);
460                                 n_ops++;
461
462                                 if (i > 0) {
463                                         /* cycle is not done yet */
464                                         int pidx = get_pairidx_for_regidx(pairs, n, cycle->elems[i]->index, 0);
465
466                                         /* create intermediate proj */
467                                         res1 = new_r_Proj(get_irn_irg(irn), block, cpyxchg, get_irn_mode(res1), 0);
468
469                                         /* set as in for next Perm */
470                                         pairs[pidx].in_node = res1;
471                                 }
472                                 else {
473                                         sched_remove(res1);
474                                 }
475
476                                 sched_remove(res2);
477
478                                 set_Proj_pred(res2, cpyxchg);
479                                 set_Proj_proj(res2, 0);
480                                 set_Proj_pred(res1, cpyxchg);
481                                 set_Proj_proj(res1, 1);
482
483                                 sched_add_after(sched_point, res1);
484                                 sched_add_after(sched_point, res2);
485
486                                 arch_set_irn_register(arch_env, res2, cycle->elems[i + 1]);
487                                 arch_set_irn_register(arch_env, res1, cycle->elems[i]);
488
489                                 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
490                                 sched_add_after(sched_point, cpyxchg);
491
492                                 DBG((mod, LEVEL_1, "replacing %+F with %+F, placed new node after %+F\n", irn, cpyxchg, sched_point));
493
494                                 /* set the new scheduling point */
495                                 sched_point = res1;
496                         }
497                         else {
498                                 DBG((mod, LEVEL_1, "%+F creating copy node (%+F, %s) -> (%+F, %s)\n",
499                                         irn, arg1, cycle->elems[i]->name, res2, cycle->elems[i + 1]->name));
500
501                                 cpyxchg = be_new_Copy(reg_class, env->chord_env->irg, block, arg1);
502                                 arch_set_irn_register(arch_env, cpyxchg, cycle->elems[i + 1]);
503                                 n_ops++;
504
505                                 /* remove the proj from the schedule */
506                                 sched_remove(res2);
507
508                                 /* exchange copy node and proj */
509                                 exchange(res2, cpyxchg);
510
511                                 /* insert the copy/exchange node in schedule after the magic schedule node (see above) */
512                                 sched_add_after(sched_point, cpyxchg);
513
514                                 /* set the new scheduling point */
515                                 sched_point = cpyxchg;
516                         }
517                 }
518
519                 if (env->do_stat) {
520                         hook_be_block_stat_permcycle(reg_class->name, irn, block, \
521                                 cycle->type == PERM_CHAIN, cycle->n_elems, n_ops);
522                 }
523
524                 free((void *) cycle->elems);
525                 free(cycle);
526         }
527
528
529
530         /* remove the perm from schedule */
531         sched_remove(irn);
532 }
533
534
535
536 static int get_n_out_edges(const ir_node *irn) {
537         const ir_edge_t *edge;
538         int cnt = 0;
539
540         foreach_out_edge(irn, edge) {
541                 cnt++;
542         }
543
544         return cnt;
545 }
546
547 static ir_node *belower_skip_proj(ir_node *irn) {
548         while(is_Proj(irn))
549                 irn = get_Proj_pred(irn);
550         return irn;
551 }
552
553 static void fix_in(ir_node *irn, ir_node *old, ir_node *nw) {
554         int i, n;
555
556         irn = belower_skip_proj(irn);
557         n   = get_irn_arity(irn);
558
559         for (i = 0; i < n; i++) {
560                 if (get_irn_n(irn, i) == old) {
561                         set_irn_n(irn, i, nw);
562                         break;
563                 }
564         }
565 }
566
567 static void gen_assure_different_pattern(ir_node *irn, be_irg_t *birg, ir_node *other_different) {
568         const arch_env_t          *arch_env = birg->main_env->arch_env;
569         ir_node                   *in[2], *keep, *cpy, *temp;
570         ir_node                   *block = get_nodes_block(irn);
571         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, other_different, -1);
572         FIRM_DBG_REGISTER(firm_dbg_module_t *mod, "firm.be.lower");
573
574         if (arch_irn_is(arch_env, other_different, ignore) || ! mode_is_datab(get_irn_mode(other_different))) {
575                 DBG((mod, LEVEL_1, "ignore constraint for %+F because other_irn is ignore or not a datab node\n", irn));
576                 return;
577         }
578
579         /* Make a not spillable copy of the different node   */
580         /* this is needed because the different irn could be */
581         /* in block far far away                             */
582         /* The copy is optimized later if not needed         */
583
584         temp = new_rd_Unknown(birg->irg, get_irn_mode(other_different));
585         cpy = be_new_Copy(cls, birg->irg, block, temp);
586         be_node_set_flags(cpy, BE_OUT_POS(0), arch_irn_flags_dont_spill);
587
588         in[0] = irn;
589         in[1] = cpy;
590
591         /* Let the irn use the copy instead of the old other_different */
592         fix_in(irn, other_different, cpy);
593
594         /* Add the Keep resp. CopyKeep and reroute the users */
595         /* of the other_different irn in case of CopyKeep.   */
596         if (get_n_out_edges(other_different) == 0) {
597                 keep = be_new_Keep(cls, birg->irg, block, 2, in);
598         }
599         else {
600                 keep = be_new_CopyKeep_single(cls, birg->irg, block, cpy, irn, get_irn_mode(other_different));
601                 be_node_set_reg_class(keep, 1, cls);
602                 edges_reroute(other_different, keep, birg->irg);
603         }
604
605         /* after rerouting: let the copy point to the other_different irn */
606         set_irn_n(cpy, 0, other_different);
607
608         DBG((mod, LEVEL_1, "created %+F for %+F to assure should_be_different\n", keep, irn));
609 }
610
611 /**
612  * Checks if node has a should_be_different constraint in output
613  * and adds a Keep then to assure the constraint.
614  */
615 static void assure_different_constraints(ir_node *irn, be_irg_t *birg) {
616         const arch_env_t          *arch_env = birg->main_env->arch_env;
617         const arch_register_req_t *req;
618         arch_register_req_t        req_temp;
619         int i, n;
620
621         req = arch_get_register_req(arch_env, &req_temp, irn, -1);
622
623         if (req) {
624                 if (arch_register_req_is(req, should_be_different)) {
625                         gen_assure_different_pattern(irn, birg, req->other_different);
626                 }
627                 else if (arch_register_req_is(req, should_be_different_from_all)) {
628                         n = get_irn_arity(belower_skip_proj(irn));
629                         for (i = 0; i < n; i++) {
630                                 gen_assure_different_pattern(irn, birg, get_irn_n(belower_skip_proj(irn), i));
631                         }
632                 }
633         }
634 }
635
636
637
638 /**
639  * Calls the functions to assure register constraints.
640  *
641  * @param irn      The node to be checked for lowering
642  * @param walk_env The walker environment
643  */
644 static void assure_constraints_walker(ir_node *irn, void *walk_env) {
645         if (is_Block(irn))
646                 return;
647
648         if (mode_is_datab(get_irn_mode(irn)))
649                 assure_different_constraints(irn, walk_env);
650
651         return;
652 }
653
654
655
656 /**
657  * Walks over all nodes to assure register constraints.
658  *
659  * @param birg  The birg structure containing the irg
660  */
661 void assure_constraints(be_irg_t *birg) {
662         irg_walk_blkwise_graph(birg->irg, NULL, assure_constraints_walker, birg);
663 }
664
665
666
667 /**
668  * Calls the corresponding lowering function for the node.
669  *
670  * @param irn      The node to be checked for lowering
671  * @param walk_env The walker environment
672  */
673 static void lower_nodes_after_ra_walker(ir_node *irn, void *walk_env) {
674         lower_env_t      *env      = walk_env;
675         const arch_env_t *arch_env = env->chord_env->birg->main_env->arch_env;
676
677         if (!is_Block(irn) && !is_Proj(irn)) {
678                 if (is_Perm(arch_env, irn)) {
679                         lower_perm_node(irn, walk_env);
680                 }
681         }
682
683         return;
684 }
685
686 static void lower_print_perm_stat(lower_env_t *env) {
687         int j, total_len_chain, total_len_cycle, total_size_perm, total_size_real_perm;
688         unsigned i;
689
690         printf("=== IRG: %s ===\n", get_entity_name(get_irg_entity(env->chord_env->irg)));
691         for (i = 0; i < env->pstat_n; i++) {
692                 if (env->pstat[i]->num_perms == 0)
693                         continue;
694
695                 printf("CLASS: %s\n", env->pstat[i]->cls->name);
696                 printf("# total perms:      %d (size:num   -> 1:%d", env->pstat[i]->num_perms, env->pstat[i]->perm_size_ar[0]);
697
698                 total_size_perm = env->pstat[i]->perm_size_ar[0];
699                 for (j = 1; j < env->pstat[i]->cls->n_regs; j++) {
700                         total_size_perm += (j + 1) * env->pstat[i]->perm_size_ar[j];
701                         printf(", %d:%d", j + 1, env->pstat[i]->perm_size_ar[j]);
702                 }
703                 printf(")\n");
704                 printf("avg perm size:      %.2f\n", env->pstat[i]->num_perms ? (float)total_size_perm / (float)env->pstat[i]->num_perms : 0);
705
706                 printf("# real perms:       %d (size:num  -> 1:%d", env->pstat[i]->num_real_perms, env->pstat[i]->real_perm_size_ar[0]);
707
708                 total_size_real_perm = env->pstat[i]->real_perm_size_ar[0];
709                 for (j = 1; j < env->pstat[i]->cls->n_regs; j++) {
710                         total_size_real_perm += (j + 1) * env->pstat[i]->real_perm_size_ar[j];
711                         printf(", %d:%d", j + 1, env->pstat[i]->real_perm_size_ar[j]);
712                 }
713                 printf(")\n");
714                 printf("avg real perm size: %.2f\n", env->pstat[i]->num_real_perms ? (float)total_size_real_perm / (float)env->pstat[i]->num_real_perms : 0);
715
716                 printf("# total chains:     %d (lenght:num -> 1:%d", env->pstat[i]->num_chains, env->pstat[i]->chain_len_ar[0]);
717
718                 total_len_chain = env->pstat[i]->chain_len_ar[0];
719
720                 for (j = 1; j < env->pstat[i]->cls->n_regs; j++) {
721                         total_len_chain += (j + 1) * env->pstat[i]->chain_len_ar[j];
722                         printf(", %d:%d", j + 1, env->pstat[i]->chain_len_ar[j]);
723                 }
724                 printf(")\n");
725                 printf("avg chain length:   %.2f\n", env->pstat[i]->num_chains ? (float)total_len_chain / (float)env->pstat[i]->num_chains : 0);
726                 printf("avg chains/perm:    %.2f\n", env->pstat[i]->num_real_perms ? (float)env->pstat[i]->num_chains / (float)env->pstat[i]->num_real_perms : 0);
727
728                 printf("# total cycles:     %d (length:num -> 1:%d", env->pstat[i]->num_cycles, env->pstat[i]->cycle_len_ar[0]);
729
730                 total_len_cycle = env->pstat[i]->cycle_len_ar[0];
731                 for (j = 1; j < env->pstat[i]->cls->n_regs; j++) {
732                         total_len_cycle += (j + 1) * env->pstat[i]->cycle_len_ar[j];
733                         printf(", %d:%d", j + 1, env->pstat[i]->cycle_len_ar[j]);
734                 }
735                 printf(")\n");
736                 printf("avg cycle length:   %.2f\n", env->pstat[i]->num_cycles ? (float)total_len_cycle / (float)env->pstat[i]->num_cycles : 0);
737                 printf("avg cycles/perm:    %.2f\n", env->pstat[i]->num_real_perms ? (float)env->pstat[i]->num_cycles / (float)env->pstat[i]->num_real_perms : 0);
738         }
739 }
740
741 /**
742  * Walks over all blocks in an irg and performs lowering need to be
743  * done after register allocation (e.g. perm lowering).
744  *
745  * @param chord_env The chordal environment containing the irg
746  * @param do_copy   1 == resolve cycles with a free reg if available
747  */
748 void lower_nodes_after_ra(be_chordal_env_t *chord_env, int do_copy, int do_stat) {
749         lower_env_t env;
750
751         env.chord_env  = chord_env;
752         env.do_copy    = do_copy;
753         env.do_stat    = do_stat;
754         FIRM_DBG_REGISTER(env.dbg_module, "firm.be.lower");
755
756         /* if we want statistics: allocate memory for the data and initialize with 0 */
757         if (do_stat) {
758                 const arch_isa_t *isa = chord_env->birg->main_env->arch_env->isa;
759                 int i, n = arch_isa_get_n_reg_class(isa);
760
761                 env.pstat   = alloca(n * sizeof(env.pstat[0]));
762                 env.pstat_n = n;
763
764                 for (i = 0; i < n; i++) {
765                         const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
766                         int                       n_regs = cls->n_regs;
767
768                         env.pstat[i] = alloca(sizeof(*(env.pstat[0])));
769                         memset(env.pstat[i], 0, sizeof(*(env.pstat[0])));
770
771                         env.pstat[i]->perm_size_ar      = alloca(n_regs * sizeof(env.pstat[i]->perm_size_ar[0]));
772                         env.pstat[i]->real_perm_size_ar = alloca(n_regs * sizeof(env.pstat[i]->real_perm_size_ar[0]));
773                         env.pstat[i]->chain_len_ar      = alloca(n_regs * sizeof(env.pstat[i]->chain_len_ar[0]));
774                         env.pstat[i]->cycle_len_ar      = alloca(n_regs * sizeof(env.pstat[i]->cycle_len_ar[0]));
775
776                         memset(env.pstat[i]->perm_size_ar, 0, n_regs * sizeof(env.pstat[i]->perm_size_ar[0]));
777                         memset(env.pstat[i]->real_perm_size_ar, 0, n_regs * sizeof(env.pstat[i]->real_perm_size_ar[0]));
778                         memset(env.pstat[i]->chain_len_ar, 0, n_regs * sizeof(env.pstat[i]->chain_len_ar[0]));
779                         memset(env.pstat[i]->cycle_len_ar, 0, n_regs * sizeof(env.pstat[i]->cycle_len_ar[0]));
780
781                         env.pstat[i]->cls = cls;
782                 }
783         }
784
785         irg_walk_blkwise_graph(chord_env->irg, NULL, lower_nodes_after_ra_walker, &env);
786
787         if (do_stat) {
788                 lower_print_perm_stat(&env);
789         }
790 }
791
792 #undef is_Perm