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