ae4cd287900d0ee67de3f56993c11d29a9d2809f
[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 #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 int (get_opt_##name)(void) {               \
68   return _get_opt_##name();                \
69 }
70
71 /* an internal flag can only be set from outside */
72 #define I_FLAG(name, value, def)          \
73 void set_opt_##name(int flag) {           \
74   if (flag) libFIRM_opt |= irf_##name;    \
75   else      libFIRM_opt &= ~irf_##name;   \
76 }                                         \
77
78 #define R_FLAG(name, value)
79
80 /* generate them */
81 #include "irflag_t.def"
82
83 #undef I_FLAG
84 #undef E_FLAG
85 #undef R_FLAG
86
87 /* for compatibility reasons */
88 void set_optimize(int value)
89 {
90         if (value) libFIRM_opt |= irf_optimize;
91         else       libFIRM_opt &= ~irf_optimize;
92 }
93
94 int (get_optimize)(void)
95 {
96         return get_opt_optimize();
97 }
98
99 void set_opt_control_flow(int value)
100 {
101         set_opt_control_flow_straightening(value);
102         set_opt_control_flow_weak_simplification(value);
103         set_opt_control_flow_strong_simplification(value);
104 }
105
106 /* Save the current optimization state. */
107 void save_optimization_state(optimization_state_t *state)
108 {
109         *state = libFIRM_opt;
110 }
111
112 /* Restore the current optimization state. */
113 void restore_optimization_state(const optimization_state_t *state)
114 {
115         libFIRM_opt = *state;
116 }
117
118 /* Switches ALL optimizations off */
119 void all_optimizations_off(void)
120 {
121         libFIRM_opt = 0;
122 }
123
124 #ifdef _DEBUG
125 /* only for debugging */
126 void firm_show_flags(FILE *f)
127 {
128         if (! f)
129                 f = stdout;
130         printf("Firm optimization state:\n");
131 #define E_FLAG(name, value, def) printf(" %-20s = %s\n", #name, get_opt_##name() ? "ON" : "OFF");
132 #define I_FLAG(name, value, def) printf(" %-20s = %s\n", #name, get_opt_##name() ? "ON" : "OFF");
133 #define R_FLAG(name, value)      printf(" %-20s = %s\n", #name, is_##name##_running() ? "is running" : "not running");
134 #include "irflag_t.def"
135 #undef I_FLAG
136 #undef E_FLAG
137 #undef R_FLAG
138         printf("\n");
139 }
140 #endif
141
142 static const lc_opt_table_entry_t firm_flags[] = {
143 #define I_FLAG(name, val, def) LC_OPT_ENT_BIT(#name, #name, &libFIRM_opt, (1 << val)),
144 #define E_FLAG(name, val, def) LC_OPT_ENT_BIT(#name, #name, &libFIRM_opt, (1 << val)),
145 #define R_FLAG(name, val)
146 #include "irflag_t.def"
147 #undef I_FLAG
148 #undef E_FLAG
149 #undef R_FLAG
150         LC_OPT_LAST
151 };
152
153 void firm_init_flags(void)
154 {
155         lc_opt_entry_t *grp = lc_opt_get_grp(firm_opt_get_root(), "opt");
156         lc_opt_add_table(grp, firm_flags);
157 }
158
159 firm_verification_t opt_do_node_verification = FIRM_VERIFICATION_ON;
160
161 void do_node_verification(firm_verification_t mode)
162 {
163         opt_do_node_verification = mode;
164 }