beifg: Factorise code to count interference components.
[libfirm] / ir / be / bespillutil.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       higher level abstraction for the creation of spill and reload
9  *              instructions and rematerialisation of values.
10  * @author      Daniel Grund, Sebastian Hack, Matthias Braun
11  * @date        29.09.2005
12  */
13 #ifndef FIRM_BE_BESPILLUTIL_H
14 #define FIRM_BE_BESPILLUTIL_H
15
16 #include "firm_types.h"
17 #include "debug.h"
18
19 #include "bearch.h"
20
21 typedef struct spill_env_t spill_env_t;
22
23 /**
24  * Creates a new spill environment.
25  */
26 spill_env_t *be_new_spill_env(ir_graph *irg);
27
28 /**
29  * Deletes a spill environment.
30  */
31 void be_delete_spill_env(spill_env_t *senv);
32
33 /**
34  * Return the last control flow node of a block.
35  */
36 ir_node *be_get_end_of_block_insertion_point(const ir_node *block);
37
38 /**
39  * Marks a point until which a node must be spilled.
40  */
41 void be_add_spill(spill_env_t *senv, ir_node *to_spill, ir_node *after);
42
43 /**
44  * Inserts a new entry into the list of reloads to place (the real nodes will
45  * be created when be_insert_spills_reloads is run). You don't have to
46  * explicitly create spill nodes, they will be created automatically after
47  * the definition of a value as soon as a reload is created. (we should add a
48  * possibility for explicit spill placement in the future)
49  *
50  * @param senv        The spill environment
51  * @param to_spill    The node which is about to be spilled
52  * @param before      The node before the reload should be added
53  * @param reload_cls  The register class the reloaded value will be put into
54  * @param allow_remat Set to 1 if the node may be rematerialized instead of
55  *                    reloaded
56  */
57 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
58                    const arch_register_class_t *reload_cls, int allow_remat);
59
60 /**
61  * Analog to be_add_reload, but places the reload "on an edge" between 2 blocks
62  * @see be_add_reload
63  */
64 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl,
65                            int pos, const arch_register_class_t *reload_cls,
66                            int allow_remat);
67
68 /**
69  * The main function that places real spills/reloads (or rematerializes values)
70  * for all values where be_add_reload was called. It then rebuilds the
71  * SSA-form and updates liveness information
72  */
73 void be_insert_spills_reloads(spill_env_t *senv);
74
75 /**
76  * There are 2 possibilities to spill a phi node: Only its value, or replacing
77  * the whole phi-node with a memory phi. Normally only the value of a phi will
78  * be spilled unless you mark the phi with be_spill_phi.
79  * (Remember that each phi needs a register, so you have to spill phis when
80  *  there are more phis than registers in a block)
81  */
82 void be_spill_phi(spill_env_t *env, ir_node *node);
83
84 /**
85  * Returns the estimated costs if a node would ge spilled. This does only return
86  * the costs for the spill instructions, not the costs for needed reload
87  * instructions. The value is weighted by the estimated execution frequency of
88  * the spill.
89  */
90 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before);
91
92 /**
93  * Returns the estimated costs if a node would get reloaded at a specific place
94  * This returns the costs for a reload instructions, or when possible the costs
95  * for a rematerialisation. The value is weighted by the estimated execution
96  * frequency of the reload/rematerialisation.
97  */
98 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill,
99                            ir_node *before);
100
101 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
102                                        const ir_node *before);
103
104
105 /**
106  * Analog to be_get_reload_costs but returns the cost if the reload would be
107  * placed "on an edge" between 2 blocks
108  */
109 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
110                                    ir_node *block, int pos);
111
112 typedef struct {
113         unsigned n_spills;
114         unsigned n_reloads;
115         double spill_costs;
116         double reload_costs;
117 } be_total_spill_costs_t;
118
119 /**
120  * Insert a spill after the definition of the given node if there is a reload that is not dominated by some spill.
121  * This function checks whether there is a reload that is not dominated by some spill for that node.
122  * If so, it inserts a spill right after the definition of the node.
123  * @param env The spill environment.
124  * @param irn The node to check for.
125  */
126 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn);
127
128 /**
129  * Collect spill/reload cost statistics for a graph.
130  * @param irg    The graph.
131  * @param costs  A struct which will be filled with the costs.
132  */
133 void be_get_total_spill_costs(ir_graph *irg, be_total_spill_costs_t *costs);
134
135 /**
136  * Check, if a node is rematerializable.
137  * @param env  The spill env.
138
139  */
140 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat, const ir_node *before);
141
142 /**
143  * Create a be_Spill node. This function is compatible to the
144  * arch_env->new_spill callback.
145  */
146 ir_node *be_new_spill(ir_node *value, ir_node *after);
147
148 /**
149  * Create a be_Reload node. This function is compatible to the
150  * arch_env->new_reload interface.
151  */
152 ir_node *be_new_reload(ir_node *value, ir_node *spilled, ir_node *before);
153
154 #endif