1c36a177468b6146e4cee6184e9783cb5a750528
[libfirm] / ir / be / becopystat.c
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 node statistics.
23  * @author      Daniel Grund
24  * @date        19.04.2005
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "timing.h"
32 #include "irgraph.h"
33 #include "irgwalk.h"
34 #include "irprog.h"
35 #include "iredges_t.h"
36 #include "irnodeset.h"
37
38 #include "bechordal_t.h"
39 #include "benode.h"
40 #include "beutil.h"
41 #include "becopyopt_t.h"
42 #include "becopystat.h"
43 #include "beirg.h"
44 #include "bemodule.h"
45 #include "beintlive_t.h"
46
47 #define DEBUG_LVL SET_LEVEL_1
48 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49
50 #define MAX_ARITY 20
51 #define MAX_CLS_SIZE 20
52 #define MAX_CLS_PHIS 20
53
54 /**
55  * For an explanation of these values see the code of copystat_dump_pretty
56  */
57 enum vals_t {
58         /* FROM HERE: PROBLEM CHARACTERIZATION */
59
60         I_ALL_NODES = 0,
61         I_BLOCKS,
62
63         /* phi nodes */
64         I_PHI_CNT,          /* number of phi nodes */
65         I_PHI_ARG_CNT,      /* number of arguments of phis */
66         I_PHI_ARG_SELF,     /* number of arguments of phis being the phi itself */
67         I_PHI_ARG_CONST,    /* number of arguments of phis being consts */
68         I_PHI_ARG_PRED,     /* ... being defined in a cf-pred */
69         I_PHI_ARG_GLOB,     /* ... being defined elsewhere */
70         I_PHI_ARITY_S,
71         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
72
73         /* copy nodes */
74         I_CPY_CNT,          /* number of copynodes */
75
76         /* phi classes */
77         I_CLS_CNT,          /* number of phi classes */
78         I_CLS_IF_FREE,      /* number of pc having no interference */
79         I_CLS_IF_MAX,       /* number of possible interferences in all classes */
80         I_CLS_IF_CNT,       /* number of actual interferences in all classes */
81         I_CLS_SIZE_S,
82         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
83         I_CLS_PHIS_S,
84         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
85
86         /* FROM HERE: RESULT VLAUES */
87         /* all of them are external set */
88
89         /* ilp values */
90         I_HEUR_TIME,        /* solving time in milli seconds */
91         I_ILP_TIME,         /* solving time in milli seconds */
92         I_ILP_VARS,
93         I_ILP_CSTR,
94         I_ILP_ITER,         /* number of simplex iterations */
95
96         /* copy instructions */
97         I_COPIES_MAX,       /* max possible costs of copies*/
98         I_COPIES_INIT,      /* number of copies in initial allocation */
99         I_COPIES_HEUR,      /* number of copies after heuristic */
100         I_COPIES_5SEC,      /* number of copies after ilp with max n sec */
101         I_COPIES_30SEC,     /* number of copies after ilp with max n sec */
102         I_COPIES_OPT,       /* number of copies after ilp */
103         I_COPIES_IF,        /* number of copies inevitable due to root-arg-interf */
104
105         ASIZE
106 };
107
108 /**
109  * Holds current values. Values are added till next copystat_reset
110  */
111 int curr_vals[ASIZE];
112
113 static ir_nodeset_t *all_phi_nodes;
114 static ir_nodeset_t *all_copy_nodes;
115
116 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat)
117 void be_init_copystat(void)
118 {
119         FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
120
121         all_phi_nodes  = ir_nodeset_new(64);
122         all_copy_nodes = ir_nodeset_new(64);
123         memset(curr_vals, 0, sizeof(curr_vals));
124 }
125
126 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat)
127 void be_quit_copystat(void)
128 {
129         if (all_phi_nodes != NULL) {
130                 ir_nodeset_del(all_phi_nodes);
131                 all_phi_nodes = NULL;
132         }
133         if (all_copy_nodes != NULL) {
134                 ir_nodeset_del(all_copy_nodes);
135                 all_copy_nodes = NULL;
136         }
137 }
138
139 /**
140  * @return 1 if the block at pos @p pos removed a critical edge
141  *         0 else
142  */
143 static inline int was_edge_critical(const ir_node *bl, int pos)
144 {
145         const ir_edge_t *edge;
146         const ir_node *bl_at_pos, *bl_before;
147         assert(is_Block(bl));
148
149         /* Does bl have several predecessors ?*/
150         if (get_irn_arity(bl) <= 1)
151                 return 0;
152
153         /* Does the pred have exactly one predecessor */
154         bl_at_pos = get_irn_n(bl, pos);
155         if (get_irn_arity(bl_at_pos) != 1)
156                 return 0;
157
158         /* Does the pred of the pred have several successors */
159         bl_before = get_irn_n(bl_at_pos, 0);
160         edge = get_block_succ_first(bl_before);
161         return get_block_succ_next(bl_before, edge) ? 1 : 0;
162 }
163
164 void copystat_add_max_costs(int costs)
165 {
166         curr_vals[I_COPIES_MAX] += costs;
167 }
168 void copystat_add_inevit_costs(int costs)
169 {
170         curr_vals[I_COPIES_IF] += costs;
171 }
172 void copystat_add_init_costs(int costs)
173 {
174         curr_vals[I_COPIES_INIT] += costs;
175 }
176 void copystat_add_heur_costs(int costs)
177 {
178         curr_vals[I_COPIES_HEUR] += costs;
179 }
180 void copystat_add_opt_costs(int costs)
181 {
182         curr_vals[I_COPIES_OPT] += costs;
183 }
184 void copystat_add_heur_time(int time)
185 {
186         curr_vals[I_HEUR_TIME] += time;
187 }
188
189 void copystat_add_ilp_5_sec_costs(int costs)
190 {
191         curr_vals[I_COPIES_5SEC] += costs;
192 }
193 void copystat_add_ilp_30_sec_costs(int costs)
194 {
195         curr_vals[I_COPIES_30SEC] += costs;
196 }
197 void copystat_add_ilp_time(int time)
198 {
199         curr_vals[I_ILP_TIME] += time;
200 }
201 void copystat_add_ilp_vars(int vars)
202 {
203         curr_vals[I_ILP_VARS] += vars;
204 }
205 void copystat_add_ilp_csts(int csts)
206 {
207         curr_vals[I_ILP_CSTR] += csts;
208 }
209 void copystat_add_ilp_iter(int iters)
210 {
211         curr_vals[I_ILP_ITER] += iters;
212 }
213
214 /**
215  * Opens a file named base.ext with the mode mode.
216  */
217 static FILE *be_ffopen(const char *base, const char *ext, const char *mode)
218 {
219         FILE *out;
220         char buf[1024];
221
222         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
223         buf[sizeof(buf) - 1] = '\0';
224         if (! (out = fopen(buf, mode))) {
225                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
226                 return NULL;
227         }
228         return out;
229 }
230
231 void copystat_dump(ir_graph *irg)
232 {
233         int i;
234         char buf[1024];
235         FILE *out;
236
237         snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
238         buf[sizeof(buf) - 1] = '\0';
239         out = be_ffopen(buf, "stat", "wt");
240
241         fprintf(out, "%d\n", (int)ASIZE);
242         for (i = 0; i < ASIZE; i++) {
243 #if 0
244                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
245                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
246                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
247                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
248                 else
249 #endif
250                         fprintf(out, "%i\n", curr_vals[i]);
251         }
252
253         fclose(out);
254 }
255
256 void copystat_dump_pretty(ir_graph *irg)
257 {
258         int i;
259         char buf[1024];
260         FILE *out;
261
262         snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
263         buf[sizeof(buf) - 1] = '\0';
264         out = be_ffopen(buf, "pstat", "wt");
265
266         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
267         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
268         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
269
270         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
271         fprintf(out, "... argument types\n");
272         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
273         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
274         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
275         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
276         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
277         fprintf(out, "... arities\n");
278         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
279                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
280
281         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
282         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
283         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
284         fprintf(out, "... sizes\n");
285         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
286                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
287         fprintf(out, "... contained phis\n");
288         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
289                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
290
291         fprintf(out, "\nILP stat\n");
292         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
293         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
294
295         fprintf(out, "\nCopy stat\n");
296         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
297         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
298         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
299         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
300         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
301
302         fclose(out);
303 }