As a reminiscence to the famous MAC/65 assembler changed modifier + into > and -...
[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 #include "config.h"
28
29 #include <math.h>
30
31 #include "hashptr.h"
32 #include "debug.h"
33 #include "obst.h"
34 #include "set.h"
35 #include "list.h"
36 #include "pmap.h"
37
38 #include "irprintf.h"
39 #include "irgwalk.h"
40 #include "irdump_t.h"
41 #include "irnode_t.h"
42 #include "ircons_t.h"
43 #include "irloop_t.h"
44 #include "iredges.h"
45 #include "execfreq.h"
46 #include "irtools.h"
47
48 #include "be_t.h"
49 #include "belive_t.h"
50 #include "besched.h"
51 #include "beirgmod.h"
52 #include "bearch.h"
53 #include "benode.h"
54 #include "beutil.h"
55 #include "bespill.h"
56 #include "beirg.h"
57
58 #define MAXPRESSURE 128
59
60 typedef struct _regpressure_ana_t {
61         const arch_register_class_t  *cls;
62         const be_lv_t                *lv;
63         unsigned int                 *stat;
64         DEBUG_ONLY(firm_dbg_module_t *dbg);
65 } regpressure_ana_t;
66
67 static inline int has_reg_class(const regpressure_ana_t *ra, const ir_node *irn)
68 {
69         return arch_irn_consider_in_reg_alloc(ra->cls, irn);
70 }
71
72 static inline int regpressure(pset *live) {
73         int pressure = pset_count(live);
74         return MIN(pressure, MAXPRESSURE);
75 }
76
77 static void
78 regpressureanawalker(ir_node *bb, void *data)
79 {
80         regpressure_ana_t  *ra   = data;
81         pset               *live = pset_new_ptr_default();
82         const ir_node      *irn;
83         unsigned int       *stat = ra->stat;
84         int                i;
85         const be_lv_t      *lv   = ra->lv;
86
87         be_lv_foreach(lv, bb, be_lv_state_end, i) {
88                 ir_node *value = be_lv_get_irn(lv, bb, i);
89                 if (has_reg_class(ra, value)) {
90                         pset_insert_ptr(live, value);
91                 }
92         }
93         stat[regpressure(live)]++;
94
95         sched_foreach_reverse(bb, irn) {
96
97                 if (is_Phi(irn)) break;
98
99                 if (has_reg_class(ra, irn)) {
100                         pset_remove_ptr(live, irn);
101                 }
102
103                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
104                         ir_node  *arg = get_irn_n(irn, i);
105
106                         if (has_reg_class(ra, arg)) {
107                                 pset_insert_ptr(live, arg);
108                         }
109                 }
110
111                 if (! is_Proj(irn))
112                         stat[regpressure(live)]++;
113         }
114 }
115
116 void be_analyze_regpressure(be_irg_t *birg, const arch_register_class_t *cls, const char *suffix)
117 {
118         regpressure_ana_t ra;
119         unsigned int      stat[MAXPRESSURE+1];
120         unsigned int      i;
121         char              fname[256];
122         FILE              *f;
123         ir_graph          *irg = be_get_birg_irg(birg);
124
125         ir_snprintf(fname, sizeof(fname), "%F_%s%s_pressure.stat", irg, cls->name, suffix);
126         f = fopen(fname, "w");
127         assert(f);
128
129         be_liveness_assure_sets(be_assure_liveness(birg));
130
131         FIRM_DBG_REGISTER(ra.dbg, "firm.be.regpressureana");
132
133         ra.lv   = be_get_birg_liveness(birg);
134         ra.cls  = cls;
135         ra.stat = stat;
136
137         memset(stat, 0, sizeof(stat));
138
139         irg_block_walk_graph(irg, regpressureanawalker, NULL, &ra);
140
141         for (i = 0; i <= MAXPRESSURE; ++i) {
142                 fprintf(f, "%d\n", stat[i]);
143         }
144
145         fclose(f);
146 }