69101501269814fd6f3efeef0f15b5901a35f3b3
[libfirm] / ir / ir / irflag.c
1 /*
2  * Copyright (C) 1995-2007 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   Flags to control optimizations.
23  * @author  Christian Schaefer, Goetz Lindenmaier
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef WITH_LIBCORE
33 #include <libcore/lc_opts.h>
34 #endif
35
36 #include "firm_common.h"
37 #include "irtools.h"
38 #include "irflag_t.h"
39
40 /* DISABLE - don't do this optimization
41    ENABLE  - lets see, if there is a better graph */
42 #define ON      (-1)
43 #define OFF  (0)
44
45 #define FLAG(name, value, def)     (irf_##name & def) |
46 #define E_FLAG(name, value, def)    FLAG(name, value, def)
47 #define I_FLAG(name, value, def)    FLAG(name, value, def)
48 #define R_FLAG(name, value)
49
50 optimization_state_t libFIRM_opt =
51 #include "irflag_t.def"
52   0;
53
54 #undef FLAG
55 #undef E_FLAG
56 #undef I_FLAG
57 #undef R_FLAG
58
59 /** The bitset of currently running phases. */
60 optimization_state_t libFIRM_running = 0;
61
62 /* verbose is always off on default */
63 optimization_state_t libFIRM_verb = 0;
64
65 /** The Firm verbosity level */
66 int firm_verbosity_level;
67
68 /* an external flag can be set and get from outside */
69 #define E_FLAG(name, value, def)           \
70 void set_opt_##name(int flag) {            \
71   if (flag) libFIRM_opt |= irf_##name;     \
72   else      libFIRM_opt &= ~irf_##name;    \
73 }                                          \
74 void set_opt_##name##_verbose(int flag) {  \
75   if (flag) libFIRM_verb |= irf_##name;    \
76   else      libFIRM_verb &= ~irf_##name;   \
77 }                                          \
78 int (get_opt_##name)(void) {               \
79   return _get_opt_##name();                \
80 }
81
82 /* an internal flag can only be set from outside */
83 #define I_FLAG(name, value, def)          \
84 void set_opt_##name(int flag) {           \
85   if (flag) libFIRM_opt |= irf_##name;    \
86   else      libFIRM_opt &= ~irf_##name;   \
87 }                                         \
88 void set_opt_##name##_verbose(int flag) { \
89   if (flag) libFIRM_verb |= irf_##name;   \
90   else      libFIRM_verb &= ~irf_##name;  \
91 }
92
93 #define R_FLAG(name, value)
94
95 /* generate them */
96 #include "irflag_t.def"
97
98 #undef I_FLAG
99 #undef E_FLAG
100 #undef R_FLAG
101
102 /* for compatibility reasons */
103 void set_optimize(int value) {
104   if (value) libFIRM_opt |= irf_optimize;
105   else       libFIRM_opt &= ~irf_optimize;
106 }
107
108 int (get_optimize)(void) {
109   return get_opt_optimize();
110 }
111
112 void set_opt_control_flow(int value)
113 {
114   set_opt_control_flow_straightening(value);
115   set_opt_control_flow_weak_simplification(value);
116   set_opt_control_flow_strong_simplification(value);
117 }
118
119 void set_firm_verbosity (int value) {
120   firm_verbosity_level = value;
121 }
122
123 int  (get_firm_verbosity) (void) {
124   return _get_firm_verbosity();
125 }
126
127 /* Save the current optimization state. */
128 void save_optimization_state(optimization_state_t *state)
129 {
130   *state = libFIRM_opt;
131 }
132
133 /* Restore the current optimization state. */
134 void restore_optimization_state(const optimization_state_t *state)
135 {
136   libFIRM_opt = *state;
137 }
138
139 /* Switches ALL optimizations off */
140 void all_optimizations_off(void)
141 {
142   libFIRM_opt = 0;
143 }
144
145 #ifdef _DEBUG
146 /* only for debugging */
147 void firm_show_flags(FILE *f) {
148   if (! f)
149     f = stdout;
150   printf("Firm optimization state:\n");
151 #define E_FLAG(name, value, def) printf(" %-20s = %s\n", #name, get_opt_##name() ? "ON" : "OFF");
152 #define I_FLAG(name, value, def) printf(" %-20s = %s\n", #name, get_opt_##name() ? "ON" : "OFF");
153 #define R_FLAG(name, value)      printf(" %-20s = %s\n", #name, is_##name##_running() ? "is running" : "not running");
154 #include "irflag_t.def"
155 #undef I_FLAG
156 #undef E_FLAG
157 #undef R_FLAG
158   printf("\n");
159 }
160 #endif
161
162 #ifdef WITH_LIBCORE
163 static const lc_opt_table_entry_t firm_flags[] = {
164 #define I_FLAG(name, val, def) LC_OPT_ENT_BIT(#name, #name, &libFIRM_opt, (1 << val)),
165 #define E_FLAG(name, val, def) LC_OPT_ENT_BIT(#name, #name, &libFIRM_opt, (1 << val)),
166 #define R_FLAG(name, val)
167 #include "irflag_t.def"
168 #undef I_FLAG
169 #undef E_FLAG
170 #undef R_FLAG
171         LC_OPT_LAST
172 };
173 #endif
174
175 void firm_init_flags(void) {
176 #ifdef WITH_LIBCORE
177         lc_opt_entry_t *grp = lc_opt_get_grp(firm_opt_get_root(), "opt");
178         lc_opt_add_table(grp, firm_flags);
179 #endif
180 }
181
182 firm_verification_t opt_do_node_verification = FIRM_VERIFICATION_ON;
183
184 void do_node_verification(firm_verification_t mode) {
185   opt_do_node_verification = mode;
186 }