common spilling things
[libfirm] / ir / be / bespill.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                29.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7
8 #include "pset.h"
9 #include "irnode_t.h"
10 #include "ircons_t.h"
11
12 #include "besched.h"
13 #include "bespill.h"
14 #include "benode_t.h"
15
16 typedef struct _spill_ctx_t {
17         ir_node *spilled;  /**< The spilled node. */
18         ir_node *user;     /**< The node this spill is for. */
19         ir_node *spill;    /**< The spill itself. */
20 } spill_ctx_t;
21
22 int be_set_cmp_spillctx(const void *a, const void *b, size_t n) {
23         const spill_ctx_t *p = a;
24         const spill_ctx_t *q = b;
25         return !(p->user == q->user && p->spilled == q->spilled);
26 }
27
28 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
29         spill_ctx_t templ;
30
31         templ.spilled = to_spill;
32         templ.user    = ctx_irn;
33         templ.spill   = NULL;
34
35         return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
36 }
37
38 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
39         spill_ctx_t *ctx;
40 //      DBG((dbg, DBG_SPILL, "spill_irn %+F\n", irn));
41
42         ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
43         if(!ctx->spill) {
44                 const be_main_env_t *env = senv->session->main_env;
45                 ctx->spill = be_spill(env->node_factory, env->arch_env, irn);
46         }
47
48         return ctx->spill;
49 }
50
51 /**
52  * If the first usage of a phi result would be out of memory
53  * there is no sense in allocating a register for it.
54  * Thus we spill it and all its operands to the same spill slot.
55  * Therefore the phi/dataB becomes a phi/Memory
56  */
57 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn) {
58         int i, n = get_irn_arity(phi);
59         ir_node **ins, *bl = get_nodes_block(phi);
60         ir_graph *irg = get_irn_irg(bl);
61         spill_ctx_t *ctx;
62
63         assert(is_Phi(phi));
64 //      DBG((dbg, DBG_SPILL, "spill_phi %+F\n", phi));
65
66         /* search an existing spill for this context */
67         ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
68
69         /* if not found spill the phi */
70         if(!ctx->spill) {
71                 /* build a new PhiM with dummy in-array */
72                 ins  = malloc(n * sizeof(ins[0]));
73                 for(i=0; i<n; ++i)
74                         ins[i] = new_r_Unknown(irg, mode_M);
75                 ctx->spill = new_r_Phi(senv->session->irg, bl, n, ins, mode_M);
76                 free(ins);
77
78                 /* re-wire the phiM */
79                 for(i=0; i<n; ++i) {
80                         ir_node *arg = get_irn_n(phi, i);
81                         ir_node *sub_res;
82
83                         if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg))
84                                 sub_res = be_spill_phi(senv, arg, ctx_irn);
85                         else
86                                 sub_res = be_spill_irn(senv, arg, ctx_irn);
87
88                         set_irn_n(ctx->spill, i, sub_res);
89                 }
90         }
91         return ctx->spill;
92 }
93
94 ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
95         ir_node *res;
96         if (pset_find_ptr(senv->mem_phis, to_spill))
97                 res = be_spill_phi(senv, to_spill, to_spill);
98         else
99                 res = be_spill_irn(senv, to_spill, to_spill);
100
101         return res;
102 }
103
104 void be_remove_spilled_phis(spill_env_t *senv) {
105         ir_node *irn;
106         /* remove all special phis form the irg and the schedule */
107         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
108                 int i, n;
109                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
110                         set_irn_n(irn, i, new_r_Bad(senv->session->irg));
111                 sched_remove(irn);
112         }
113 }