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