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