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