alias.c: Reorder #include statements and remove duplicates.
[gcc.git] / gcc / lto-opts.c
1 /* LTO IL options.
2
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 Contributed by Simon Baldwin <simonb@google.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "internal-fn.h"
34 #include "flags.h"
35 #include "opts.h"
36 #include "common/common-target.h"
37 #include "lto-section-names.h"
38 #include "toplev.h"
39
40 /* Append the option piece OPT to the COLLECT_GCC_OPTIONS string
41 set up by OB, appropriately quoted and separated by spaces
42 (if !*FIRST_P). */
43
44 static void
45 append_to_collect_gcc_options (struct obstack *ob,
46 bool *first_p, const char *opt)
47 {
48 const char *p, *q = opt;
49 if (!*first_p)
50 obstack_grow (ob, " ", 1);
51 obstack_grow (ob, "'", 1);
52 while ((p = strchr (q, '\'')))
53 {
54 obstack_grow (ob, q, p - q);
55 obstack_grow (ob, "'\\''", 4);
56 q = ++p;
57 }
58 obstack_grow (ob, q, strlen (q));
59 obstack_grow (ob, "'", 1);
60 *first_p = false;
61 }
62
63 /* Write currently held options to an LTO IL section. */
64
65 void
66 lto_write_options (void)
67 {
68 char *section_name;
69 struct obstack temporary_obstack;
70 unsigned int i, j;
71 char *args;
72 bool first_p = true;
73
74 section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
75 lto_begin_section (section_name, false);
76
77 obstack_init (&temporary_obstack);
78
79 /* Output options that affect GIMPLE IL semantics and are implicitly
80 enabled by the frontend.
81 This for now includes an explicit set of options that we also handle
82 explicitly in lto-wrapper.c. In the end the effects on GIMPLE IL
83 semantics should be explicitely encoded in the IL or saved per
84 function rather than per compilation unit. */
85 /* -fexceptions causes the EH machinery to be initialized, enabling
86 generation of unwind data so that explicit throw() calls work. */
87 if (!global_options_set.x_flag_exceptions
88 && global_options.x_flag_exceptions)
89 append_to_collect_gcc_options (&temporary_obstack, &first_p,
90 "-fexceptions");
91 /* -fnon-call-exceptions changes the generation of exception
92 regions. It is enabled implicitly by the Go frontend. */
93 if (!global_options_set.x_flag_non_call_exceptions
94 && global_options.x_flag_non_call_exceptions)
95 append_to_collect_gcc_options (&temporary_obstack, &first_p,
96 "-fnon-call-exceptions");
97 /* The default -ffp-contract changes depending on the language
98 standard. Pass thru conservative standard settings. */
99 if (!global_options_set.x_flag_fp_contract_mode)
100 switch (global_options.x_flag_fp_contract_mode)
101 {
102 case FP_CONTRACT_OFF:
103 append_to_collect_gcc_options (&temporary_obstack, &first_p,
104 "-ffp-contract=off");
105 break;
106 case FP_CONTRACT_ON:
107 append_to_collect_gcc_options (&temporary_obstack, &first_p,
108 "-ffp-contract=on");
109 break;
110 case FP_CONTRACT_FAST:
111 /* Nothing. That merges conservatively and is the default for LTO. */
112 break;
113 default:
114 gcc_unreachable ();
115 }
116 /* The default -fmath-errno, -fsigned-zeros and -ftrapping-math change
117 depending on the language (they can be disabled by the Ada and Java
118 front-ends). Pass thru conservative standard settings. */
119 if (!global_options_set.x_flag_errno_math)
120 append_to_collect_gcc_options (&temporary_obstack, &first_p,
121 global_options.x_flag_errno_math
122 ? "-fmath-errno"
123 : "-fno-math-errno");
124 if (!global_options_set.x_flag_signed_zeros)
125 append_to_collect_gcc_options (&temporary_obstack, &first_p,
126 global_options.x_flag_signed_zeros
127 ? "-fsigned-zeros"
128 : "-fno-signed-zeros");
129 if (!global_options_set.x_flag_trapping_math)
130 append_to_collect_gcc_options (&temporary_obstack, &first_p,
131 global_options.x_flag_trapping_math
132 ? "-ftrapping-math"
133 : "-fno-trapping-math");
134 /* We need to merge -f[no-]strict-overflow, -f[no-]wrapv and -f[no-]trapv
135 conservatively, so stream out their defaults. */
136 if (!global_options_set.x_flag_wrapv
137 && global_options.x_flag_wrapv)
138 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fwrapv");
139 if (!global_options_set.x_flag_trapv
140 && !global_options.x_flag_trapv)
141 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-trapv");
142 if (!global_options_set.x_flag_strict_overflow
143 && !global_options.x_flag_strict_overflow)
144 append_to_collect_gcc_options (&temporary_obstack, &first_p,
145 "-fno-strict-overflow");
146
147 if (!global_options_set.x_flag_openmp
148 && !global_options.x_flag_openmp)
149 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-openmp");
150 if (!global_options_set.x_flag_openacc
151 && !global_options.x_flag_openacc)
152 append_to_collect_gcc_options (&temporary_obstack, &first_p,
153 "-fno-openacc");
154
155 /* Append options from target hook and store them to offload_lto section. */
156 if (lto_stream_offload_p)
157 {
158 char *offload_opts = targetm.offload_options ();
159 char *offload_ptr = offload_opts;
160 while (offload_ptr)
161 {
162 char *next = strchr (offload_ptr, ' ');
163 if (next)
164 *next++ = '\0';
165 append_to_collect_gcc_options (&temporary_obstack, &first_p,
166 offload_ptr);
167 offload_ptr = next;
168 }
169 free (offload_opts);
170 }
171
172 /* Output explicitly passed options. */
173 for (i = 1; i < save_decoded_options_count; ++i)
174 {
175 struct cl_decoded_option *option = &save_decoded_options[i];
176
177 /* Skip explicitly some common options that we do not need. */
178 switch (option->opt_index)
179 {
180 case OPT_dumpbase:
181 case OPT_SPECIAL_unknown:
182 case OPT_SPECIAL_ignore:
183 case OPT_SPECIAL_program_name:
184 case OPT_SPECIAL_input_file:
185 continue;
186
187 default:
188 break;
189 }
190
191 /* Skip frontend and driver specific options here. */
192 if (!(cl_options[option->opt_index].flags & (CL_COMMON|CL_TARGET|CL_LTO)))
193 continue;
194
195 /* Do not store target-specific options in offload_lto section. */
196 if ((cl_options[option->opt_index].flags & CL_TARGET)
197 && lto_stream_offload_p)
198 continue;
199
200 /* Drop options created from the gcc driver that will be rejected
201 when passed on to the driver again. */
202 if (cl_options[option->opt_index].cl_reject_driver)
203 continue;
204
205 /* Also drop all options that are handled by the driver as well,
206 which includes things like -o and -v or -fhelp for example.
207 We do not need those. The only exception is -foffload option, if we
208 write it in offload_lto section. Also drop all diagnostic options. */
209 if ((cl_options[option->opt_index].flags & (CL_DRIVER|CL_WARNING))
210 && (!lto_stream_offload_p || option->opt_index != OPT_foffload_))
211 continue;
212
213 for (j = 0; j < option->canonical_option_num_elements; ++j)
214 append_to_collect_gcc_options (&temporary_obstack, &first_p,
215 option->canonical_option[j]);
216 }
217 obstack_grow (&temporary_obstack, "\0", 1);
218 args = XOBFINISH (&temporary_obstack, char *);
219 lto_write_data (args, strlen (args) + 1);
220 lto_end_section ();
221
222 obstack_free (&temporary_obstack, NULL);
223 free (section_name);
224 }