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