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