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