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