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