4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
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. */
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);
28 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
31 templ.spilled = to_spill;
35 return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
38 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
40 // DBG((dbg, DBG_SPILL, "spill_irn %+F\n", irn));
42 ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
44 const be_main_env_t *env = senv->session->main_env;
45 ctx->spill = be_spill(env->node_factory, env->arch_env, irn);
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
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);
64 // DBG((dbg, DBG_SPILL, "spill_phi %+F\n", phi));
66 /* search an existing spill for this context */
67 ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
69 /* if not found spill the phi */
71 /* build a new PhiM with dummy in-array */
72 ins = malloc(n * sizeof(ins[0]));
74 ins[i] = new_r_Unknown(irg, mode_M);
75 ctx->spill = new_r_Phi(senv->session->irg, bl, n, ins, mode_M);
78 /* re-wire the phiM */
80 ir_node *arg = get_irn_n(phi, i);
83 if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg))
84 sub_res = be_spill_phi(senv, arg, ctx_irn);
86 sub_res = be_spill_irn(senv, arg, ctx_irn);
88 set_irn_n(ctx->spill, i, sub_res);
94 ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
96 if (pset_find_ptr(senv->mem_phis, to_spill))
97 res = be_spill_phi(senv, to_spill, to_spill);
99 res = be_spill_irn(senv, to_spill, to_spill);
104 void be_remove_spilled_phis(spill_env_t *senv) {
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)) {
109 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
110 set_irn_n(irn, i, new_r_Bad(senv->session->irg));