Use module mechanism to register copy minimization algorithms.
[libfirm] / ir / be / becopyopt.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Copy minimization driver.
23  * @author      Daniel Grund
24  * @date        11.04.2005
25  * @version     $Id$
26  *
27  * Main file for the optimization reducing the copies needed for:
28  * - Phi coalescing
29  * - Register-constrained nodes
30  * - Two-address code instructions
31  */
32 #ifndef FIRM_BE_BECOPYOPT_H
33 #define FIRM_BE_BECOPYOPT_H
34
35 #include <stdio.h>
36
37 #include "firm_types.h"
38 #include "bechordal.h"
39 #include "beutil.h"
40
41 /**
42  * Flags for dumping the IFG.
43  */
44 enum {
45         CO_IFG_DUMP_COLORS = 1, /**< Dump the graph colored. */
46         CO_IFG_DUMP_LABELS = 2, /**< Dump node/edge labels. */
47         CO_IFG_DUMP_SHAPE  = 4, /**< Give constrained nodes special shapes. */
48         CO_IFG_DUMP_CONSTR = 8  /**< Dump the node constraints in the label. */
49 };
50
51 /**
52  * Algorithms.
53  */
54 enum {
55         CO_ALGO_NONE,
56         CO_ALGO_HEUR,
57         CO_ALGO_HEUR2,
58         CO_ALGO_HEUR4,
59         CO_ALGO_ILP,
60 #ifdef FIRM_KAPS
61         CO_ALGO_PBQP,
62 #endif
63         CO_ALGO_LAST
64 };
65
66 typedef struct _copy_opt_t copy_opt_t;
67
68 typedef int(*cost_fct_t)(const copy_opt_t *, ir_node *, ir_node *, int);
69
70 typedef struct {
71         int (*copyopt)(copy_opt_t *co); /**< function ptr to run copyopt */
72         int        can_improve_existing;
73 } co_algo_info;
74
75
76 /**
77  * Register a new copy optimazation algorithm.
78  *
79  * @param name     the name of the copy optimazation algorithm,
80  *                 used to select it
81  * @param spiller  a copy optimazation entry
82  */
83 void be_register_copyopt(const char *name, co_algo_info *copyopt);
84
85 /** The driver for copy minimization. */
86 void co_driver(be_chordal_env_t *cenv);
87
88 /** A coalescing algorithm. */
89 typedef int (co_algo_t)(copy_opt_t *);
90
91 /**
92  * Generate the problem. Collect all information and optimizable nodes.
93  */
94 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs);
95
96 /**
97  * Free the space used...
98  */
99 void free_copy_opt(copy_opt_t *co);
100
101 /**
102  * Computes the costs of a copy according to loop depth
103  * @param co   The copy opt object.
104  * @param pos   the argument position of arg in the root arguments
105  * @return Must be >= 0 in all cases.
106  */
107 int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
108
109 /**
110  * Computes the costs of a copy according to execution frequency
111  * @param co   The copy opt object.
112  * @param pos   the argument position of arg in the root arguments
113  * @return Must be >= 0 in all cases.
114  */
115 int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
116
117 /**
118  * All costs equal 1. Using this will reduce the _number_ of copies.
119  * @param co   The copy opt object.
120  * @return Must be >= 0 in all cases.
121  */
122 int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos);
123
124 /**
125  * Statistics over a copy optimization module.
126  */
127 typedef struct {
128         ulong64 aff_edges;            /**< number of affinity edges. */
129         ulong64 aff_nodes;            /**< number of nodes with incident affinity edges. */
130         ulong64 aff_int;              /**< number of affinity edges whose nodes also interfere. */
131         ulong64 inevit_costs;         /**< costs which cannot be evited (due to interfering affinities). */
132         ulong64 max_costs;            /**< all costs of the affinities. */
133         ulong64 costs;                /**< The costs of the current coloring. */
134         ulong64 unsatisfied_edges;    /**< The number of unequally colored affinity edges. */
135 } co_complete_stats_t;
136
137 /**
138  * Collect statistics of a copy optimization module.
139  * @param co    The copy optimization environment.
140  * @param stat  Where to put the stats.
141  * @note  This requires the graph info to be computed.
142  */
143 void co_complete_stats(const copy_opt_t *co, co_complete_stats_t *stat);
144
145
146 /**
147  * Build internal optimization units structure
148  */
149 void co_build_ou_structure(copy_opt_t *co);
150
151 /**
152  * Frees the space used by the opt unit representation.
153  * Does NOT free the whole copyopt structure
154  */
155 void co_free_ou_structure(copy_opt_t *co);
156
157 /**
158  * Solves the problem using a heuristic approach
159  * Uses the OU data structure
160  */
161 int co_solve_heuristic(copy_opt_t *co);
162
163 /**
164  * Apply Park/Moon coalescing to the graph.
165  * @param co The copy optimization data structure.
166  */
167 void co_solve_park_moon(copy_opt_t *co);
168
169 /**
170  * Solves the copy minimization problem using another heuristic approach.
171  * Uses the OU and the GRAPH data structure.
172  */
173 int co_solve_heuristic_new(copy_opt_t *co);
174
175 /**
176  * Returns the maximal costs possible, i.e. the costs if all
177  * pairs would be assigned different registers.
178  * Uses the OU data structure
179  */
180 int co_get_max_copy_costs(const copy_opt_t *co);
181
182 /**
183  * Returns the inevitable costs, i.e. the costs of
184  * all copy pairs which interfere.
185  * Uses the OU data structure
186  */
187 int co_get_inevit_copy_costs(const copy_opt_t *co);
188
189 /**
190  * Returns the current costs the copies are causing.
191  * The result includes inevitable costs and the costs
192  * of the copies regarding the current register allocation
193  * Uses the OU data structure
194  */
195 int co_get_copy_costs(const copy_opt_t *co);
196
197 /**
198  * Returns a lower bound for the costs of copies in this ou.
199  * The result includes inevitable costs and the costs of a
200  * minimal costs caused by the nodes of the ou.
201  * Uses the OU data structure
202  */
203 int co_get_lower_bound(const copy_opt_t *co);
204
205 /**
206  * Dump the interference graph according to the Appel/George coalescing contest file format.
207  * See: http://www.cs.princeton.edu/~appel/coalesce/format.html
208  * @note Requires graph structure.
209  * @param co The copy opt object.
210  * @param f  A file to dump to.
211  */
212 void co_dump_appel_graph(const copy_opt_t *co, FILE *f);
213
214 /**
215  * Dumps the IFG of the program splitting after each instruction in the Appel format.
216  * @param co The copy opt object.
217  * @param f  The file to dump to.
218  */
219 void co_dump_appel_graph_cliques(const copy_opt_t *co, FILE *f);
220 /**
221  * Dump the interference graph with the affinity edges and the coloring.
222  * @param co    The copy opt structure.
223  * @param f     The file to dump to.
224  * @param flags The dump flags (see enum above).
225  */
226 void co_dump_ifg_dot(const copy_opt_t *co, FILE *f, unsigned flags);
227
228 /**
229  * Constructs another internal representation of the affinity edges
230  */
231 void co_build_graph_structure(copy_opt_t *co);
232
233 /**
234  * Frees the space used by the graph representation.
235  * Does NOT free the whole copyopt structure
236  */
237 void co_free_graph_structure(copy_opt_t *co);
238
239 /**
240  * Solves the problem using mixed integer programming
241  * @returns 1 iff solution state was optimal
242  * NYI
243  */
244 int co_solve_ilp1(copy_opt_t *co, double time_limit);
245
246 /**
247  * Solves the problem using mixed integer programming
248  * @returns 1 iff solution state was optimal
249  * Uses the OU and the GRAPH data structure
250  * Dependency of the OU structure can be removed
251  */
252 int co_solve_ilp2(copy_opt_t *co);
253
254 /**
255  * Checks if a node is optimizable, viz has something to do with coalescing.
256  * Uses the GRAPH data structure
257  */
258 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn);
259
260 #endif /* FIRM_BE_BECOPYOPT_H */