- removed obstack from be_main_env_t, it was only used to allocate one arch_env,...
[libfirm] / ir / be / bepressurestat.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       Register Pressure Statistics.
23  * @author      Adam M. Szalkowski
24  * @date        06.04.2006
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <math.h>
32
33 #include "hashptr.h"
34 #include "debug.h"
35 #include "obst.h"
36 #include "set.h"
37 #include "list.h"
38 #include "pmap.h"
39
40 #include "irprintf.h"
41 #include "irgwalk.h"
42 #include "irdump_t.h"
43 #include "irnode_t.h"
44 #include "ircons_t.h"
45 #include "irloop_t.h"
46 #include "phiclass.h"
47 #include "iredges.h"
48 #include "execfreq.h"
49 #include "irtools.h"
50
51 #include "be_t.h"
52 #include "belive_t.h"
53 #include "besched_t.h"
54 #include "beirgmod.h"
55 #include "bearch_t.h"
56 #include "benode_t.h"
57 #include "beutil.h"
58 #include "bespillremat.h"
59 #include "bespill.h"
60 #include "beirg_t.h"
61
62 #define MAXPRESSURE 128
63
64 typedef struct _regpressure_ana_t {
65         arch_env_t                   *arch_env;
66         const arch_register_class_t  *cls;
67         const be_lv_t                *lv;
68         unsigned int                 *stat;
69         DEBUG_ONLY(firm_dbg_module_t *dbg);
70 } regpressure_ana_t;
71
72 static INLINE int has_reg_class(const regpressure_ana_t *ra, const ir_node *irn)
73 {
74         return arch_irn_consider_in_reg_alloc(ra->arch_env, ra->cls, irn);
75 }
76
77 static INLINE int regpressure(pset *live) {
78         int pressure = pset_count(live);
79         return MIN(pressure, MAXPRESSURE);
80 }
81
82 static void
83 regpressureanawalker(ir_node *bb, void *data)
84 {
85         regpressure_ana_t  *ra   = data;
86         pset               *live = pset_new_ptr_default();
87         const ir_node      *irn;
88         unsigned int       *stat = ra->stat;
89         int                i;
90         const be_lv_t      *lv   = ra->lv;
91
92         be_lv_foreach(lv, bb, be_lv_state_end, i) {
93                 ir_node *value = be_lv_get_irn(lv, bb, i);
94                 if (has_reg_class(ra, value)) {
95                         pset_insert_ptr(live, value);
96                 }
97         }
98         stat[regpressure(live)]++;
99
100         sched_foreach_reverse(bb, irn) {
101
102                 if (is_Phi(irn)) break;
103
104                 if (has_reg_class(ra, irn)) {
105                         pset_remove_ptr(live, irn);
106                 }
107
108                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
109                         ir_node  *arg = get_irn_n(irn, i);
110
111                         if (has_reg_class(ra, arg)) {
112                                 pset_insert_ptr(live, arg);
113                         }
114                 }
115
116                 if (! is_Proj(irn))
117                         stat[regpressure(live)]++;
118         }
119 }
120
121 void be_analyze_regpressure(be_irg_t *birg, const arch_register_class_t *cls, const char *suffix)
122 {
123         regpressure_ana_t ra;
124         unsigned int      stat[MAXPRESSURE+1];
125         unsigned int      i;
126         char              fname[256];
127         FILE              *f;
128         ir_graph          *irg = be_get_birg_irg(birg);
129
130         ir_snprintf(fname, sizeof(fname), "%F_%s%s_pressure.stat", irg, cls->name, suffix);
131         f = fopen(fname, "w");
132         assert(f);
133
134         be_liveness_assure_sets(be_assure_liveness(birg));
135
136         FIRM_DBG_REGISTER(ra.dbg, "firm.be.regpressureana");
137
138         ra.arch_env = &birg->main_env->arch_env;
139         ra.lv       = be_get_birg_liveness(birg);
140         ra.cls      = cls;
141         ra.stat     = stat;
142
143         memset(stat, 0, sizeof(stat));
144
145         irg_block_walk_graph(irg, regpressureanawalker, NULL, &ra);
146
147         for (i = 0; i <= MAXPRESSURE; ++i) {
148                 fprintf(f, "%d\n", stat[i]);
149         }
150
151         fclose(f);
152 }