* lto-streamer-out.c (lto_output_location): Stream
[official-gcc.git] / gcc / lto-opts.c
blob2b2a61fa6deafce39d3742d3ef08cdd448367481
1 /* LTO IL options.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 Contributed by Simon Baldwin <simonb@google.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "bitmap.h"
43 #include "flags.h"
44 #include "opts.h"
45 #include "options.h"
46 #include "common/common-target.h"
47 #include "diagnostic.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "lto-streamer.h"
52 #include "lto-section-names.h"
53 #include "toplev.h"
55 /* Append the option piece OPT to the COLLECT_GCC_OPTIONS string
56 set up by OB, appropriately quoted and separated by spaces
57 (if !*FIRST_P). */
59 static void
60 append_to_collect_gcc_options (struct obstack *ob,
61 bool *first_p, const char *opt)
63 const char *p, *q = opt;
64 if (!*first_p)
65 obstack_grow (ob, " ", 1);
66 obstack_grow (ob, "'", 1);
67 while ((p = strchr (q, '\'')))
69 obstack_grow (ob, q, p - q);
70 obstack_grow (ob, "'\\''", 4);
71 q = ++p;
73 obstack_grow (ob, q, strlen (q));
74 obstack_grow (ob, "'", 1);
75 *first_p = false;
78 /* Write currently held options to an LTO IL section. */
80 void
81 lto_write_options (void)
83 char *section_name;
84 struct obstack temporary_obstack;
85 unsigned int i, j;
86 char *args;
87 bool first_p = true;
89 section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
90 lto_begin_section (section_name, false);
92 obstack_init (&temporary_obstack);
94 /* Output options that affect GIMPLE IL semantics and are implicitly
95 enabled by the frontend.
96 This for now includes an explicit set of options that we also handle
97 explicitly in lto-wrapper.c. In the end the effects on GIMPLE IL
98 semantics should be explicitely encoded in the IL or saved per
99 function rather than per compilation unit. */
100 /* -fexceptions causes the EH machinery to be initialized, enabling
101 generation of unwind data so that explicit throw() calls work. */
102 if (!global_options_set.x_flag_exceptions
103 && global_options.x_flag_exceptions)
104 append_to_collect_gcc_options (&temporary_obstack, &first_p,
105 "-fexceptions");
106 /* -fnon-call-exceptions changes the generation of exception
107 regions. It is enabled implicitly by the Go frontend. */
108 if (!global_options_set.x_flag_non_call_exceptions
109 && global_options.x_flag_non_call_exceptions)
110 append_to_collect_gcc_options (&temporary_obstack, &first_p,
111 "-fnon-call-exceptions");
112 /* The default -ffp-contract changes depending on the language
113 standard. Pass thru conservative standard settings. */
114 if (!global_options_set.x_flag_fp_contract_mode)
115 switch (global_options.x_flag_fp_contract_mode)
117 case FP_CONTRACT_OFF:
118 append_to_collect_gcc_options (&temporary_obstack, &first_p,
119 "-ffp-contract=off");
120 break;
121 case FP_CONTRACT_ON:
122 append_to_collect_gcc_options (&temporary_obstack, &first_p,
123 "-ffp-contract=on");
124 break;
125 case FP_CONTRACT_FAST:
126 /* Nothing. That merges conservatively and is the default for LTO. */
127 break;
128 default:
129 gcc_unreachable ();
131 /* The default -fmath-errno, -fsigned-zeros and -ftrapping-math change
132 depending on the language (they can be disabled by the Ada and Java
133 front-ends). Pass thru conservative standard settings. */
134 if (!global_options_set.x_flag_errno_math)
135 append_to_collect_gcc_options (&temporary_obstack, &first_p,
136 global_options.x_flag_errno_math
137 ? "-fmath-errno"
138 : "-fno-math-errno");
139 if (!global_options_set.x_flag_signed_zeros)
140 append_to_collect_gcc_options (&temporary_obstack, &first_p,
141 global_options.x_flag_signed_zeros
142 ? "-fsigned-zeros"
143 : "-fno-signed-zeros");
144 if (!global_options_set.x_flag_trapping_math)
145 append_to_collect_gcc_options (&temporary_obstack, &first_p,
146 global_options.x_flag_trapping_math
147 ? "-ftrapping-math"
148 : "-fno-trapping-math");
149 /* We need to merge -f[no-]strict-overflow, -f[no-]wrapv and -f[no-]trapv
150 conservatively, so stream out their defaults. */
151 if (!global_options_set.x_flag_wrapv
152 && global_options.x_flag_wrapv)
153 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fwrapv");
154 if (!global_options_set.x_flag_trapv
155 && !global_options.x_flag_trapv)
156 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-trapv");
157 if (!global_options_set.x_flag_strict_overflow
158 && !global_options.x_flag_strict_overflow)
159 append_to_collect_gcc_options (&temporary_obstack, &first_p,
160 "-fno-strict-overflow");
162 if (!global_options_set.x_flag_openmp
163 && !global_options.x_flag_openmp)
164 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-openmp");
165 if (!global_options_set.x_flag_openacc
166 && !global_options.x_flag_openacc)
167 append_to_collect_gcc_options (&temporary_obstack, &first_p,
168 "-fno-openacc");
170 /* Append options from target hook and store them to offload_lto section. */
171 if (lto_stream_offload_p)
173 char *offload_opts = targetm.offload_options ();
174 char *offload_ptr = offload_opts;
175 while (offload_ptr)
177 char *next = strchr (offload_ptr, ' ');
178 if (next)
179 *next++ = '\0';
180 append_to_collect_gcc_options (&temporary_obstack, &first_p,
181 offload_ptr);
182 offload_ptr = next;
184 free (offload_opts);
187 /* Output explicitly passed options. */
188 for (i = 1; i < save_decoded_options_count; ++i)
190 struct cl_decoded_option *option = &save_decoded_options[i];
192 /* Skip explicitly some common options that we do not need. */
193 switch (option->opt_index)
195 case OPT_dumpbase:
196 case OPT_SPECIAL_unknown:
197 case OPT_SPECIAL_ignore:
198 case OPT_SPECIAL_program_name:
199 case OPT_SPECIAL_input_file:
200 continue;
202 default:
203 break;
206 /* Skip frontend and driver specific options here. */
207 if (!(cl_options[option->opt_index].flags & (CL_COMMON|CL_TARGET|CL_LTO)))
208 continue;
210 /* Do not store target-specific options in offload_lto section. */
211 if ((cl_options[option->opt_index].flags & CL_TARGET)
212 && lto_stream_offload_p)
213 continue;
215 /* Drop options created from the gcc driver that will be rejected
216 when passed on to the driver again. */
217 if (cl_options[option->opt_index].cl_reject_driver)
218 continue;
220 /* Also drop all options that are handled by the driver as well,
221 which includes things like -o and -v or -fhelp for example.
222 We do not need those. The only exception is -foffload option, if we
223 write it in offload_lto section. Also drop all diagnostic options. */
224 if ((cl_options[option->opt_index].flags & (CL_DRIVER|CL_WARNING))
225 && (!lto_stream_offload_p || option->opt_index != OPT_foffload_))
226 continue;
228 for (j = 0; j < option->canonical_option_num_elements; ++j)
229 append_to_collect_gcc_options (&temporary_obstack, &first_p,
230 option->canonical_option[j]);
232 obstack_grow (&temporary_obstack, "\0", 1);
233 args = XOBFINISH (&temporary_obstack, char *);
234 lto_write_data (args, strlen (args) + 1);
235 lto_end_section ();
237 obstack_free (&temporary_obstack, NULL);
238 free (section_name);