Backport from GCC mainline.
[platform/upstream/linaro-gcc.git] / gcc / lto-opts.c
1 /* LTO IL options.
2
3    Copyright (C) 2009-2016 Free Software Foundation, Inc.
4    Contributed by Simon Baldwin <simonb@google.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
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"
33
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).  */
37
38 static void
39 append_to_collect_gcc_options (struct obstack *ob,
40                                bool *first_p, const char *opt)
41 {
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, '\'')))
47     {
48       obstack_grow (ob, q, p - q);
49       obstack_grow (ob, "'\\''", 4);
50       q = ++p;
51     }
52   obstack_grow (ob, q, strlen (q));
53   obstack_grow (ob, "'", 1);
54   *first_p = false;
55 }
56
57 /* Write currently held options to an LTO IL section.  */
58
59 void
60 lto_write_options (void)
61 {
62   char *section_name;
63   struct obstack temporary_obstack;
64   unsigned int i, j;
65   char *args;
66   bool first_p = true;
67
68   section_name = lto_get_section_name (LTO_section_opts, NULL, NULL);
69   lto_begin_section (section_name, false);
70
71   obstack_init (&temporary_obstack);
72
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)
95       {
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 ();
109       }
110   /* The default -fmath-errno, -fsigned-zeros and -ftrapping-math change
111      depending on the language (they can be disabled by the Ada and Java
112      front-ends).  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");
136   if (!global_options_set.x_flag_strict_overflow
137       && !global_options.x_flag_strict_overflow)
138     append_to_collect_gcc_options (&temporary_obstack, &first_p,
139                                "-fno-strict-overflow");
140
141   if (!global_options_set.x_flag_openmp
142       && !global_options.x_flag_openmp)
143     append_to_collect_gcc_options (&temporary_obstack, &first_p, "-fno-openmp");
144   if (!global_options_set.x_flag_openacc
145       && !global_options.x_flag_openacc)
146     append_to_collect_gcc_options (&temporary_obstack, &first_p,
147                                    "-fno-openacc");
148
149   /* Append options from target hook and store them to offload_lto section.  */
150   if (lto_stream_offload_p)
151     {
152       char *offload_opts = targetm.offload_options ();
153       char *offload_ptr = offload_opts;
154       while (offload_ptr)
155        {
156          char *next = strchr (offload_ptr, ' ');
157          if (next)
158            *next++ = '\0';
159          append_to_collect_gcc_options (&temporary_obstack, &first_p,
160                                         offload_ptr);
161          offload_ptr = next;
162        }
163       free (offload_opts);
164     }
165
166   /* Output explicitly passed options.  */
167   for (i = 1; i < save_decoded_options_count; ++i)
168     {
169       struct cl_decoded_option *option = &save_decoded_options[i];
170
171       /* Skip explicitly some common options that we do not need.  */
172       switch (option->opt_index)
173       {
174         case OPT_dumpbase:
175         case OPT_SPECIAL_unknown:
176         case OPT_SPECIAL_ignore:
177         case OPT_SPECIAL_program_name:
178         case OPT_SPECIAL_input_file:
179           continue;
180
181         default:
182           break;
183       }
184
185       /* Skip frontend and driver specific options here.  */
186       if (!(cl_options[option->opt_index].flags & (CL_COMMON|CL_TARGET|CL_LTO)))
187         continue;
188
189       /* Do not store target-specific options in offload_lto section.  */
190       if ((cl_options[option->opt_index].flags & CL_TARGET)
191           && lto_stream_offload_p)
192        continue;
193
194       /* Drop options created from the gcc driver that will be rejected
195          when passed on to the driver again.  */
196       if (cl_options[option->opt_index].cl_reject_driver)
197         continue;
198
199       /* Also drop all options that are handled by the driver as well,
200          which includes things like -o and -v or -fhelp for example.
201          We do not need those.  The only exception is -foffload option, if we
202          write it in offload_lto section.  Also drop all diagnostic options.  */
203       if ((cl_options[option->opt_index].flags & (CL_DRIVER|CL_WARNING))
204           && (!lto_stream_offload_p || option->opt_index != OPT_foffload_))
205         continue;
206
207       for (j = 0; j < option->canonical_option_num_elements; ++j)
208         append_to_collect_gcc_options (&temporary_obstack, &first_p,
209                                        option->canonical_option[j]);
210     }
211   obstack_grow (&temporary_obstack, "\0", 1);
212   args = XOBFINISH (&temporary_obstack, char *);
213   lto_write_data (args, strlen (args) + 1);
214   lto_end_section ();
215
216   obstack_free (&temporary_obstack, NULL);
217   free (section_name);
218 }