2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lto-opts.c
blobffc8a3fac51bccbe0baa34113eca4e9a16943f1f
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 "plugin-api.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "lto-streamer.h"
49 #include "lto-section-names.h"
50 #include "toplev.h"
52 /* Append the option piece OPT to the COLLECT_GCC_OPTIONS string
53 set up by OB, appropriately quoted and separated by spaces
54 (if !*FIRST_P). */
56 static void
57 append_to_collect_gcc_options (struct obstack *ob,
58 bool *first_p, const char *opt)
60 const char *p, *q = opt;
61 if (!*first_p)
62 obstack_grow (ob, " ", 1);
63 obstack_grow (ob, "'", 1);
64 while ((p = strchr (q, '\'')))
66 obstack_grow (ob, q, p - q);
67 obstack_grow (ob, "'\\''", 4);
68 q = ++p;
70 obstack_grow (ob, q, strlen (q));
71 obstack_grow (ob, "'", 1);
72 *first_p = false;
75 /* Write currently held options to an LTO IL section. */
77 void
78 lto_write_options (void)
80 char *section_name;
81 struct obstack temporary_obstack;
82 unsigned int i, j;
83 char *args;
84 bool first_p = true;
86 section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
87 lto_begin_section (section_name, false);
89 obstack_init (&temporary_obstack);
91 /* Output options that affect GIMPLE IL semantics and are implicitly
92 enabled by the frontend.
93 This for now includes an explicit set of options that we also handle
94 explicitly in lto-wrapper.c. In the end the effects on GIMPLE IL
95 semantics should be explicitely encoded in the IL or saved per
96 function rather than per compilation unit. */
97 /* -fexceptions causes the EH machinery to be initialized, enabling
98 generation of unwind data so that explicit throw() calls work. */
99 if (!global_options_set.x_flag_exceptions
100 && global_options.x_flag_exceptions)
101 append_to_collect_gcc_options (&temporary_obstack, &first_p,
102 "-fexceptions");
103 /* -fnon-call-exceptions changes the generation of exception
104 regions. It is enabled implicitly by the Go frontend. */
105 if (!global_options_set.x_flag_non_call_exceptions
106 && global_options.x_flag_non_call_exceptions)
107 append_to_collect_gcc_options (&temporary_obstack, &first_p,
108 "-fnon-call-exceptions");
109 /* The default -ffp-contract changes depending on the language
110 standard. Pass thru conservative standard settings. */
111 if (!global_options_set.x_flag_fp_contract_mode)
112 switch (global_options.x_flag_fp_contract_mode)
114 case FP_CONTRACT_OFF:
115 append_to_collect_gcc_options (&temporary_obstack, &first_p,
116 "-ffp-contract=off");
117 break;
118 case FP_CONTRACT_ON:
119 append_to_collect_gcc_options (&temporary_obstack, &first_p,
120 "-ffp-contract=on");
121 break;
122 case FP_CONTRACT_FAST:
123 /* Nothing. That merges conservatively and is the default for LTO. */
124 break;
125 default:
126 gcc_unreachable ();
128 /* The default -fmath-errno, -fsigned-zeros and -ftrapping-math change
129 depending on the language (they can be disabled by the Ada and Java
130 front-ends). Pass thru conservative standard settings. */
131 if (!global_options_set.x_flag_errno_math)
132 append_to_collect_gcc_options (&temporary_obstack, &first_p,
133 global_options.x_flag_errno_math
134 ? "-fmath-errno"
135 : "-fno-math-errno");
136 if (!global_options_set.x_flag_signed_zeros)
137 append_to_collect_gcc_options (&temporary_obstack, &first_p,
138 global_options.x_flag_signed_zeros
139 ? "-fsigned-zeros"
140 : "-fno-signed-zeros");
141 if (!global_options_set.x_flag_trapping_math)
142 append_to_collect_gcc_options (&temporary_obstack, &first_p,
143 global_options.x_flag_trapping_math
144 ? "-ftrapping-math"
145 : "-fno-trapping-math");
146 /* We need to merge -f[no-]strict-overflow, -f[no-]wrapv and -f[no-]trapv
147 conservatively, so stream out their defaults. */
148 if (!global_options_set.x_flag_wrapv
149 && global_options.x_flag_wrapv)
150 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fwrapv");
151 if (!global_options_set.x_flag_trapv
152 && !global_options.x_flag_trapv)
153 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-trapv");
154 if (!global_options_set.x_flag_strict_overflow
155 && !global_options.x_flag_strict_overflow)
156 append_to_collect_gcc_options (&temporary_obstack, &first_p,
157 "-fno-strict-overflow");
159 if (!global_options_set.x_flag_openmp
160 && !global_options.x_flag_openmp)
161 append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-openmp");
162 if (!global_options_set.x_flag_openacc
163 && !global_options.x_flag_openacc)
164 append_to_collect_gcc_options (&temporary_obstack, &first_p,
165 "-fno-openacc");
167 /* Append options from target hook and store them to offload_lto section. */
168 if (lto_stream_offload_p)
170 char *offload_opts = targetm.offload_options ();
171 char *offload_ptr = offload_opts;
172 while (offload_ptr)
174 char *next = strchr (offload_ptr, ' ');
175 if (next)
176 *next++ = '\0';
177 append_to_collect_gcc_options (&temporary_obstack, &first_p,
178 offload_ptr);
179 offload_ptr = next;
181 free (offload_opts);
184 /* Output explicitly passed options. */
185 for (i = 1; i < save_decoded_options_count; ++i)
187 struct cl_decoded_option *option = &save_decoded_options[i];
189 /* Skip explicitly some common options that we do not need. */
190 switch (option->opt_index)
192 case OPT_dumpbase:
193 case OPT_SPECIAL_unknown:
194 case OPT_SPECIAL_ignore:
195 case OPT_SPECIAL_program_name:
196 case OPT_SPECIAL_input_file:
197 continue;
199 default:
200 break;
203 /* Skip frontend and driver specific options here. */
204 if (!(cl_options[option->opt_index].flags & (CL_COMMON|CL_TARGET|CL_LTO)))
205 continue;
207 /* Do not store target-specific options in offload_lto section. */
208 if ((cl_options[option->opt_index].flags & CL_TARGET)
209 && lto_stream_offload_p)
210 continue;
212 /* Drop options created from the gcc driver that will be rejected
213 when passed on to the driver again. */
214 if (cl_options[option->opt_index].cl_reject_driver)
215 continue;
217 /* Also drop all options that are handled by the driver as well,
218 which includes things like -o and -v or -fhelp for example.
219 We do not need those. The only exception is -foffload option, if we
220 write it in offload_lto section. Also drop all diagnostic options. */
221 if ((cl_options[option->opt_index].flags & (CL_DRIVER|CL_WARNING))
222 && (!lto_stream_offload_p || option->opt_index != OPT_foffload_))
223 continue;
225 for (j = 0; j < option->canonical_option_num_elements; ++j)
226 append_to_collect_gcc_options (&temporary_obstack, &first_p,
227 option->canonical_option[j]);
229 obstack_grow (&temporary_obstack, "\0", 1);
230 args = XOBFINISH (&temporary_obstack, char *);
231 lto_write_data (args, strlen (args) + 1);
232 lto_end_section ();
234 obstack_free (&temporary_obstack, NULL);
235 free (section_name);