1 /* Copyright (C) 1988-2022 Free Software Foundation, Inc.
2 
3 This file is part of GCC.
4 
5 GCC is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9 
10 GCC is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3.  If not see
17 <http://www.gnu.org/licenses/>.  */
18 
19 #define IN_TARGET_CODE 1
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "memmodel.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "cfgloop.h"
31 #include "df.h"
32 #include "tm_p.h"
33 #include "stringpool.h"
34 #include "expmed.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "emit-rtl.h"
38 #include "recog.h"
39 #include "cgraph.h"
40 #include "diagnostic.h"
41 #include "cfgbuild.h"
42 #include "alias.h"
43 #include "fold-const.h"
44 #include "attribs.h"
45 #include "calls.h"
46 #include "stor-layout.h"
47 #include "varasm.h"
48 #include "output.h"
49 #include "insn-attr.h"
50 #include "flags.h"
51 #include "except.h"
52 #include "explow.h"
53 #include "expr.h"
54 #include "cfgrtl.h"
55 #include "common/common-target.h"
56 #include "langhooks.h"
57 #include "reload.h"
58 #include "gimplify.h"
59 #include "dwarf2.h"
60 #include "tm-constrs.h"
61 #include "cselib.h"
62 #include "sched-int.h"
63 #include "opts.h"
64 #include "tree-pass.h"
65 #include "context.h"
66 #include "pass_manager.h"
67 #include "target-globals.h"
68 #include "gimple-iterator.h"
69 #include "tree-vectorizer.h"
70 #include "shrink-wrap.h"
71 #include "builtins.h"
72 #include "rtl-iter.h"
73 #include "tree-iterator.h"
74 #include "dbgcnt.h"
75 #include "case-cfn-macros.h"
76 #include "dojump.h"
77 #include "fold-const-call.h"
78 #include "tree-vrp.h"
79 #include "tree-ssanames.h"
80 #include "selftest.h"
81 #include "selftest-rtl.h"
82 #include "print-rtl.h"
83 #include "intl.h"
84 #include "ifcvt.h"
85 #include "symbol-summary.h"
86 #include "ipa-prop.h"
87 #include "ipa-fnsummary.h"
88 #include "wide-int-bitmask.h"
89 #include "tree-vector-builder.h"
90 #include "debug.h"
91 #include "dwarf2out.h"
92 #include "i386-options.h"
93 
94 #include "x86-tune-costs.h"
95 
96 #ifndef SUBTARGET32_DEFAULT_CPU
97 #define SUBTARGET32_DEFAULT_CPU "i386"
98 #endif
99 
100 /* Processor feature/optimization bitmasks.  */
101 #define m_NONE HOST_WIDE_INT_0U
102 #define m_ALL (~HOST_WIDE_INT_0U)
103 #define m_386 (HOST_WIDE_INT_1U<<PROCESSOR_I386)
104 #define m_486 (HOST_WIDE_INT_1U<<PROCESSOR_I486)
105 #define m_PENT (HOST_WIDE_INT_1U<<PROCESSOR_PENTIUM)
106 #define m_LAKEMONT (HOST_WIDE_INT_1U<<PROCESSOR_LAKEMONT)
107 #define m_PPRO (HOST_WIDE_INT_1U<<PROCESSOR_PENTIUMPRO)
108 #define m_PENT4 (HOST_WIDE_INT_1U<<PROCESSOR_PENTIUM4)
109 #define m_NOCONA (HOST_WIDE_INT_1U<<PROCESSOR_NOCONA)
110 #define m_P4_NOCONA (m_PENT4 | m_NOCONA)
111 #define m_CORE2 (HOST_WIDE_INT_1U<<PROCESSOR_CORE2)
112 #define m_NEHALEM (HOST_WIDE_INT_1U<<PROCESSOR_NEHALEM)
113 #define m_SANDYBRIDGE (HOST_WIDE_INT_1U<<PROCESSOR_SANDYBRIDGE)
114 #define m_HASWELL (HOST_WIDE_INT_1U<<PROCESSOR_HASWELL)
115 #define m_BONNELL (HOST_WIDE_INT_1U<<PROCESSOR_BONNELL)
116 #define m_SILVERMONT (HOST_WIDE_INT_1U<<PROCESSOR_SILVERMONT)
117 #define m_KNL (HOST_WIDE_INT_1U<<PROCESSOR_KNL)
118 #define m_KNM (HOST_WIDE_INT_1U<<PROCESSOR_KNM)
119 #define m_SKYLAKE (HOST_WIDE_INT_1U<<PROCESSOR_SKYLAKE)
120 #define m_SKYLAKE_AVX512 (HOST_WIDE_INT_1U<<PROCESSOR_SKYLAKE_AVX512)
121 #define m_CANNONLAKE (HOST_WIDE_INT_1U<<PROCESSOR_CANNONLAKE)
122 #define m_ICELAKE_CLIENT (HOST_WIDE_INT_1U<<PROCESSOR_ICELAKE_CLIENT)
123 #define m_ICELAKE_SERVER (HOST_WIDE_INT_1U<<PROCESSOR_ICELAKE_SERVER)
124 #define m_CASCADELAKE (HOST_WIDE_INT_1U<<PROCESSOR_CASCADELAKE)
125 #define m_TIGERLAKE (HOST_WIDE_INT_1U<<PROCESSOR_TIGERLAKE)
126 #define m_COOPERLAKE (HOST_WIDE_INT_1U<<PROCESSOR_COOPERLAKE)
127 #define m_SAPPHIRERAPIDS (HOST_WIDE_INT_1U<<PROCESSOR_SAPPHIRERAPIDS)
128 #define m_ALDERLAKE (HOST_WIDE_INT_1U<<PROCESSOR_ALDERLAKE)
129 #define m_ROCKETLAKE (HOST_WIDE_INT_1U<<PROCESSOR_ROCKETLAKE)
130 #define m_CORE_AVX512 (m_SKYLAKE_AVX512 | m_CANNONLAKE \
131 		       | m_ICELAKE_CLIENT | m_ICELAKE_SERVER | m_CASCADELAKE \
132 		       | m_TIGERLAKE | m_COOPERLAKE | m_SAPPHIRERAPIDS \
133 		       | m_ROCKETLAKE)
134 #define m_CORE_AVX2 (m_HASWELL | m_SKYLAKE | m_CORE_AVX512)
135 #define m_CORE_ALL (m_CORE2 | m_NEHALEM  | m_SANDYBRIDGE | m_CORE_AVX2)
136 #define m_GOLDMONT (HOST_WIDE_INT_1U<<PROCESSOR_GOLDMONT)
137 #define m_GOLDMONT_PLUS (HOST_WIDE_INT_1U<<PROCESSOR_GOLDMONT_PLUS)
138 #define m_TREMONT (HOST_WIDE_INT_1U<<PROCESSOR_TREMONT)
139 #define m_INTEL (HOST_WIDE_INT_1U<<PROCESSOR_INTEL)
140 
141 #define m_GEODE (HOST_WIDE_INT_1U<<PROCESSOR_GEODE)
142 #define m_K6 (HOST_WIDE_INT_1U<<PROCESSOR_K6)
143 #define m_K6_GEODE (m_K6 | m_GEODE)
144 #define m_K8 (HOST_WIDE_INT_1U<<PROCESSOR_K8)
145 #define m_ATHLON (HOST_WIDE_INT_1U<<PROCESSOR_ATHLON)
146 #define m_ATHLON_K8 (m_K8 | m_ATHLON)
147 #define m_AMDFAM10 (HOST_WIDE_INT_1U<<PROCESSOR_AMDFAM10)
148 #define m_BDVER1 (HOST_WIDE_INT_1U<<PROCESSOR_BDVER1)
149 #define m_BDVER2 (HOST_WIDE_INT_1U<<PROCESSOR_BDVER2)
150 #define m_BDVER3 (HOST_WIDE_INT_1U<<PROCESSOR_BDVER3)
151 #define m_BDVER4 (HOST_WIDE_INT_1U<<PROCESSOR_BDVER4)
152 #define m_ZNVER1 (HOST_WIDE_INT_1U<<PROCESSOR_ZNVER1)
153 #define m_ZNVER2 (HOST_WIDE_INT_1U<<PROCESSOR_ZNVER2)
154 #define m_ZNVER3 (HOST_WIDE_INT_1U<<PROCESSOR_ZNVER3)
155 #define m_ZNVER4 (HOST_WIDE_INT_1U<<PROCESSOR_ZNVER4)
156 #define m_BTVER1 (HOST_WIDE_INT_1U<<PROCESSOR_BTVER1)
157 #define m_BTVER2 (HOST_WIDE_INT_1U<<PROCESSOR_BTVER2)
158 #define m_BDVER	(m_BDVER1 | m_BDVER2 | m_BDVER3 | m_BDVER4)
159 #define m_BTVER (m_BTVER1 | m_BTVER2)
160 #define m_ZNVER	(m_ZNVER1 | m_ZNVER2 | m_ZNVER3 | m_ZNVER4)
161 #define m_AMD_MULTIPLE (m_ATHLON_K8 | m_AMDFAM10 | m_BDVER | m_BTVER \
162 			| m_ZNVER)
163 
164 #define m_GENERIC (HOST_WIDE_INT_1U<<PROCESSOR_GENERIC)
165 
166 const char* ix86_tune_feature_names[X86_TUNE_LAST] = {
167 #undef DEF_TUNE
168 #define DEF_TUNE(tune, name, selector) name,
169 #include "x86-tune.def"
170 #undef DEF_TUNE
171 };
172 
173 /* Feature tests against the various tunings.  */
174 unsigned char ix86_tune_features[X86_TUNE_LAST];
175 
176 /* Feature tests against the various tunings used to create ix86_tune_features
177    based on the processor mask.  */
178 static unsigned HOST_WIDE_INT initial_ix86_tune_features[X86_TUNE_LAST] = {
179 #undef DEF_TUNE
180 #define DEF_TUNE(tune, name, selector) selector,
181 #include "x86-tune.def"
182 #undef DEF_TUNE
183 };
184 
185 /* Feature tests against the various architecture variations.  */
186 unsigned char ix86_arch_features[X86_ARCH_LAST];
187 
188 struct ix86_target_opts
189 {
190   const char *option;		/* option string */
191   HOST_WIDE_INT mask;		/* isa mask options */
192 };
193 
194 /* This table is ordered so that options like -msse4.2 that imply other
195    ISAs come first.  Target string will be displayed in the same order.  */
196 static struct ix86_target_opts isa2_opts[] =
197 {
198   { "-mcx16",		OPTION_MASK_ISA2_CX16 },
199   { "-mvaes",		OPTION_MASK_ISA2_VAES },
200   { "-mrdpid",		OPTION_MASK_ISA2_RDPID },
201   { "-mpconfig",	OPTION_MASK_ISA2_PCONFIG },
202   { "-mwbnoinvd",	OPTION_MASK_ISA2_WBNOINVD },
203   { "-mavx512vp2intersect", OPTION_MASK_ISA2_AVX512VP2INTERSECT },
204   { "-msgx",		OPTION_MASK_ISA2_SGX },
205   { "-mavx5124vnniw",	OPTION_MASK_ISA2_AVX5124VNNIW },
206   { "-mavx5124fmaps",	OPTION_MASK_ISA2_AVX5124FMAPS },
207   { "-mhle",		OPTION_MASK_ISA2_HLE },
208   { "-mmovbe",		OPTION_MASK_ISA2_MOVBE },
209   { "-mclzero",		OPTION_MASK_ISA2_CLZERO },
210   { "-mmwaitx",		OPTION_MASK_ISA2_MWAITX },
211   { "-mmwait",		OPTION_MASK_ISA2_MWAIT },
212   { "-mmovdir64b",	OPTION_MASK_ISA2_MOVDIR64B },
213   { "-mwaitpkg",	OPTION_MASK_ISA2_WAITPKG },
214   { "-mcldemote",	OPTION_MASK_ISA2_CLDEMOTE },
215   { "-mptwrite",	OPTION_MASK_ISA2_PTWRITE },
216   { "-mavx512bf16",	OPTION_MASK_ISA2_AVX512BF16 },
217   { "-menqcmd",		OPTION_MASK_ISA2_ENQCMD },
218   { "-mserialize",	OPTION_MASK_ISA2_SERIALIZE },
219   { "-mtsxldtrk",	OPTION_MASK_ISA2_TSXLDTRK },
220   { "-mamx-tile",	OPTION_MASK_ISA2_AMX_TILE },
221   { "-mamx-int8",	OPTION_MASK_ISA2_AMX_INT8 },
222   { "-mamx-bf16",	OPTION_MASK_ISA2_AMX_BF16 },
223   { "-muintr",		OPTION_MASK_ISA2_UINTR },
224   { "-mhreset",		OPTION_MASK_ISA2_HRESET },
225   { "-mkl",		OPTION_MASK_ISA2_KL },
226   { "-mwidekl", 	OPTION_MASK_ISA2_WIDEKL },
227   { "-mavxvnni",	OPTION_MASK_ISA2_AVXVNNI },
228   { "-mavx512fp16",	OPTION_MASK_ISA2_AVX512FP16 }
229 };
230 static struct ix86_target_opts isa_opts[] =
231 {
232   { "-mavx512vpopcntdq", OPTION_MASK_ISA_AVX512VPOPCNTDQ },
233   { "-mavx512bitalg",	OPTION_MASK_ISA_AVX512BITALG },
234   { "-mvpclmulqdq",	OPTION_MASK_ISA_VPCLMULQDQ },
235   { "-mgfni",		OPTION_MASK_ISA_GFNI },
236   { "-mavx512vnni",	OPTION_MASK_ISA_AVX512VNNI },
237   { "-mavx512vbmi2",	OPTION_MASK_ISA_AVX512VBMI2 },
238   { "-mavx512vbmi",	OPTION_MASK_ISA_AVX512VBMI },
239   { "-mavx512ifma",	OPTION_MASK_ISA_AVX512IFMA },
240   { "-mavx512vl",	OPTION_MASK_ISA_AVX512VL },
241   { "-mavx512bw",	OPTION_MASK_ISA_AVX512BW },
242   { "-mavx512dq",	OPTION_MASK_ISA_AVX512DQ },
243   { "-mavx512er",	OPTION_MASK_ISA_AVX512ER },
244   { "-mavx512pf",	OPTION_MASK_ISA_AVX512PF },
245   { "-mavx512cd",	OPTION_MASK_ISA_AVX512CD },
246   { "-mavx512f",	OPTION_MASK_ISA_AVX512F },
247   { "-mavx2",		OPTION_MASK_ISA_AVX2 },
248   { "-mfma",		OPTION_MASK_ISA_FMA },
249   { "-mxop",		OPTION_MASK_ISA_XOP },
250   { "-mfma4",		OPTION_MASK_ISA_FMA4 },
251   { "-mf16c",		OPTION_MASK_ISA_F16C },
252   { "-mavx",		OPTION_MASK_ISA_AVX },
253 /*{ "-msse4"		OPTION_MASK_ISA_SSE4 }, */
254   { "-msse4.2",		OPTION_MASK_ISA_SSE4_2 },
255   { "-msse4.1",		OPTION_MASK_ISA_SSE4_1 },
256   { "-msse4a",		OPTION_MASK_ISA_SSE4A },
257   { "-mssse3",		OPTION_MASK_ISA_SSSE3 },
258   { "-msse3",		OPTION_MASK_ISA_SSE3 },
259   { "-maes",		OPTION_MASK_ISA_AES },
260   { "-msha",		OPTION_MASK_ISA_SHA },
261   { "-mpclmul",		OPTION_MASK_ISA_PCLMUL },
262   { "-msse2",		OPTION_MASK_ISA_SSE2 },
263   { "-msse",		OPTION_MASK_ISA_SSE },
264   { "-m3dnowa",		OPTION_MASK_ISA_3DNOW_A },
265   { "-m3dnow",		OPTION_MASK_ISA_3DNOW },
266   { "-mmmx",		OPTION_MASK_ISA_MMX },
267   { "-mrtm",		OPTION_MASK_ISA_RTM },
268   { "-mprfchw",		OPTION_MASK_ISA_PRFCHW },
269   { "-mrdseed",		OPTION_MASK_ISA_RDSEED },
270   { "-madx",		OPTION_MASK_ISA_ADX },
271   { "-mprefetchwt1",	OPTION_MASK_ISA_PREFETCHWT1 },
272   { "-mclflushopt",	OPTION_MASK_ISA_CLFLUSHOPT },
273   { "-mxsaves",		OPTION_MASK_ISA_XSAVES },
274   { "-mxsavec",		OPTION_MASK_ISA_XSAVEC },
275   { "-mxsaveopt",	OPTION_MASK_ISA_XSAVEOPT },
276   { "-mxsave",		OPTION_MASK_ISA_XSAVE },
277   { "-mabm",		OPTION_MASK_ISA_ABM },
278   { "-mbmi",		OPTION_MASK_ISA_BMI },
279   { "-mbmi2",		OPTION_MASK_ISA_BMI2 },
280   { "-mlzcnt",		OPTION_MASK_ISA_LZCNT },
281   { "-mtbm",		OPTION_MASK_ISA_TBM },
282   { "-mpopcnt",		OPTION_MASK_ISA_POPCNT },
283   { "-msahf",		OPTION_MASK_ISA_SAHF },
284   { "-mcrc32",		OPTION_MASK_ISA_CRC32 },
285   { "-mfsgsbase",	OPTION_MASK_ISA_FSGSBASE },
286   { "-mrdrnd",		OPTION_MASK_ISA_RDRND },
287   { "-mpku",		OPTION_MASK_ISA_PKU },
288   { "-mlwp",		OPTION_MASK_ISA_LWP },
289   { "-mfxsr",		OPTION_MASK_ISA_FXSR },
290   { "-mclwb",		OPTION_MASK_ISA_CLWB },
291   { "-mshstk",		OPTION_MASK_ISA_SHSTK },
292   { "-mmovdiri",	OPTION_MASK_ISA_MOVDIRI }
293 };
294 
295 /* Return 1 if TRAIT NAME is present in the OpenMP context's
296    device trait set, return 0 if not present in any OpenMP context in the
297    whole translation unit, or -1 if not present in the current OpenMP context
298    but might be present in another OpenMP context in the same TU.  */
299 
300 int
ix86_omp_device_kind_arch_isa(enum omp_device_kind_arch_isa trait,const char * name)301 ix86_omp_device_kind_arch_isa (enum omp_device_kind_arch_isa trait,
302 			       const char *name)
303 {
304   switch (trait)
305     {
306     case omp_device_kind:
307       return strcmp (name, "cpu") == 0;
308     case omp_device_arch:
309 #ifdef ACCEL_COMPILER
310       if (strcmp (name, "intel_mic") == 0)
311 	return 1;
312 #endif
313       if (strcmp (name, "x86") == 0)
314 	return 1;
315       if (TARGET_64BIT)
316 	{
317 	  if (TARGET_X32)
318 	    return strcmp (name, "x32") == 0;
319 	  else
320 	    return strcmp (name, "x86_64") == 0;
321 	}
322       if (strcmp (name, "ia32") == 0 || strcmp (name, "i386") == 0)
323 	return 1;
324       if (strcmp (name, "i486") == 0)
325 	return ix86_arch != PROCESSOR_I386 ? 1 : -1;
326       if (strcmp (name, "i586") == 0)
327 	return (ix86_arch != PROCESSOR_I386
328 		&& ix86_arch != PROCESSOR_I486) ? 1 : -1;
329       if (strcmp (name, "i686") == 0)
330 	return (ix86_arch != PROCESSOR_I386
331 		&& ix86_arch != PROCESSOR_I486
332 		&& ix86_arch != PROCESSOR_LAKEMONT
333 		&& ix86_arch != PROCESSOR_PENTIUM) ? 1 : -1;
334       return 0;
335     case omp_device_isa:
336       for (int i = 0; i < 2; i++)
337 	{
338 	  struct ix86_target_opts *opts = i ? isa2_opts : isa_opts;
339 	  size_t nopts = i ? ARRAY_SIZE (isa2_opts) : ARRAY_SIZE (isa_opts);
340 	  HOST_WIDE_INT mask = i ? ix86_isa_flags2 : ix86_isa_flags;
341 	  for (size_t n = 0; n < nopts; n++)
342 	    {
343 	      /* Handle sse4 as an alias to sse4.2.  */
344 	      if (opts[n].mask == OPTION_MASK_ISA_SSE4_2)
345 		{
346 		  if (strcmp (name, "sse4") == 0)
347 		    return (mask & opts[n].mask) != 0 ? 1 : -1;
348 		}
349 	      if (strcmp (name, opts[n].option + 2) == 0)
350 		return (mask & opts[n].mask) != 0 ? 1 : -1;
351 	    }
352 	}
353       return 0;
354     default:
355       gcc_unreachable ();
356     }
357 }
358 
359 /* Return a string that documents the current -m options.  The caller is
360    responsible for freeing the string.  */
361 
362 char *
ix86_target_string(HOST_WIDE_INT isa,HOST_WIDE_INT isa2,int flags,int flags2,const char * arch,const char * tune,enum fpmath_unit fpmath,enum prefer_vector_width pvw,enum prefer_vector_width move_max,enum prefer_vector_width store_max,bool add_nl_p,bool add_abi_p)363 ix86_target_string (HOST_WIDE_INT isa, HOST_WIDE_INT isa2,
364 		    int flags, int flags2,
365 		    const char *arch, const char *tune,
366 		    enum fpmath_unit fpmath,
367 		    enum prefer_vector_width pvw,
368 		    enum prefer_vector_width move_max,
369 		    enum prefer_vector_width store_max,
370 		    bool add_nl_p, bool add_abi_p)
371 {
372   /* Flag options.  */
373   static struct ix86_target_opts flag_opts[] =
374   {
375     { "-m128bit-long-double",		MASK_128BIT_LONG_DOUBLE },
376     { "-mlong-double-128",		MASK_LONG_DOUBLE_128 },
377     { "-mlong-double-64",		MASK_LONG_DOUBLE_64 },
378     { "-m80387",			MASK_80387 },
379     { "-maccumulate-outgoing-args",	MASK_ACCUMULATE_OUTGOING_ARGS },
380     { "-malign-double",			MASK_ALIGN_DOUBLE },
381     { "-mcld",				MASK_CLD },
382     { "-mfp-ret-in-387",		MASK_FLOAT_RETURNS },
383     { "-mieee-fp",			MASK_IEEE_FP },
384     { "-minline-all-stringops",		MASK_INLINE_ALL_STRINGOPS },
385     { "-minline-stringops-dynamically",	MASK_INLINE_STRINGOPS_DYNAMICALLY },
386     { "-mms-bitfields",			MASK_MS_BITFIELD_LAYOUT },
387     { "-mno-align-stringops",		MASK_NO_ALIGN_STRINGOPS },
388     { "-mno-fancy-math-387",		MASK_NO_FANCY_MATH_387 },
389     { "-mno-push-args",			MASK_NO_PUSH_ARGS },
390     { "-mno-red-zone",			MASK_NO_RED_ZONE },
391     { "-momit-leaf-frame-pointer",	MASK_OMIT_LEAF_FRAME_POINTER },
392     { "-mrecip",			MASK_RECIP },
393     { "-mrtd",				MASK_RTD },
394     { "-msseregparm",			MASK_SSEREGPARM },
395     { "-mstack-arg-probe",		MASK_STACK_PROBE },
396     { "-mtls-direct-seg-refs",		MASK_TLS_DIRECT_SEG_REFS },
397     { "-mvect8-ret-in-mem",		MASK_VECT8_RETURNS },
398     { "-m8bit-idiv",			MASK_USE_8BIT_IDIV },
399     { "-mvzeroupper",			MASK_VZEROUPPER },
400     { "-mstv",				MASK_STV },
401     { "-mavx256-split-unaligned-load",	MASK_AVX256_SPLIT_UNALIGNED_LOAD },
402     { "-mavx256-split-unaligned-store",	MASK_AVX256_SPLIT_UNALIGNED_STORE },
403     { "-mcall-ms2sysv-xlogues",		MASK_CALL_MS2SYSV_XLOGUES },
404     { "-mrelax-cmpxchg-loop",		MASK_RELAX_CMPXCHG_LOOP }
405   };
406 
407   /* Additional flag options.  */
408   static struct ix86_target_opts flag2_opts[] =
409   {
410     { "-mgeneral-regs-only",		OPTION_MASK_GENERAL_REGS_ONLY }
411   };
412 
413   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (isa2_opts)
414 		   + ARRAY_SIZE (flag_opts) + ARRAY_SIZE (flag2_opts) + 6][2];
415 
416   char isa_other[40];
417   char isa2_other[40];
418   char flags_other[40];
419   char flags2_other[40];
420   unsigned num = 0;
421   unsigned i, j;
422   char *ret;
423   char *ptr;
424   size_t len;
425   size_t line_len;
426   size_t sep_len;
427   const char *abi;
428 
429   memset (opts, '\0', sizeof (opts));
430 
431   /* Add -march= option.  */
432   if (arch)
433     {
434       opts[num][0] = "-march=";
435       opts[num++][1] = arch;
436     }
437 
438   /* Add -mtune= option.  */
439   if (tune)
440     {
441       opts[num][0] = "-mtune=";
442       opts[num++][1] = tune;
443     }
444 
445   /* Add -m32/-m64/-mx32.  */
446   if (add_abi_p)
447     {
448       if ((isa & OPTION_MASK_ISA_64BIT) != 0)
449 	{
450 	  if ((isa & OPTION_MASK_ABI_64) != 0)
451 	    abi = "-m64";
452 	  else
453 	    abi = "-mx32";
454 	}
455       else
456 	abi = "-m32";
457       opts[num++][0] = abi;
458     }
459   isa &= ~(OPTION_MASK_ISA_64BIT | OPTION_MASK_ABI_64 | OPTION_MASK_ABI_X32);
460 
461   /* Pick out the options in isa2 options.  */
462   for (i = 0; i < ARRAY_SIZE (isa2_opts); i++)
463     {
464       if ((isa2 & isa2_opts[i].mask) != 0)
465 	{
466 	  opts[num++][0] = isa2_opts[i].option;
467 	  isa2 &= ~ isa2_opts[i].mask;
468 	}
469     }
470 
471   if (isa2 && add_nl_p)
472     {
473       opts[num++][0] = isa2_other;
474       sprintf (isa2_other, "(other isa2: %#" HOST_WIDE_INT_PRINT "x)", isa2);
475     }
476 
477   /* Pick out the options in isa options.  */
478   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
479     {
480       if ((isa & isa_opts[i].mask) != 0)
481 	{
482 	  opts[num++][0] = isa_opts[i].option;
483 	  isa &= ~ isa_opts[i].mask;
484 	}
485     }
486 
487   if (isa && add_nl_p)
488     {
489       opts[num++][0] = isa_other;
490       sprintf (isa_other, "(other isa: %#" HOST_WIDE_INT_PRINT "x)", isa);
491     }
492 
493   /* Add flag options.  */
494   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
495     {
496       if ((flags & flag_opts[i].mask) != 0)
497 	{
498 	  opts[num++][0] = flag_opts[i].option;
499 	  flags &= ~ flag_opts[i].mask;
500 	}
501     }
502 
503   if (flags && add_nl_p)
504     {
505       opts[num++][0] = flags_other;
506       sprintf (flags_other, "(other flags: %#x)", flags);
507     }
508 
509     /* Add additional flag options.  */
510   for (i = 0; i < ARRAY_SIZE (flag2_opts); i++)
511     {
512       if ((flags2 & flag2_opts[i].mask) != 0)
513 	{
514 	  opts[num++][0] = flag2_opts[i].option;
515 	  flags2 &= ~ flag2_opts[i].mask;
516 	}
517     }
518 
519   if (flags2 && add_nl_p)
520     {
521       opts[num++][0] = flags2_other;
522       sprintf (flags2_other, "(other flags2: %#x)", flags2);
523     }
524 
525   /* Add -mfpmath= option.  */
526   if (fpmath)
527     {
528       opts[num][0] = "-mfpmath=";
529       switch ((int) fpmath)
530 	{
531 	case FPMATH_387:
532 	  opts[num++][1] = "387";
533 	  break;
534 
535 	case FPMATH_SSE:
536 	  opts[num++][1] = "sse";
537 	  break;
538 
539 	case FPMATH_387 | FPMATH_SSE:
540 	  opts[num++][1] = "sse+387";
541 	  break;
542 
543 	default:
544 	  gcc_unreachable ();
545 	}
546     }
547 
548   auto add_vector_width = [&opts, &num] (prefer_vector_width pvw,
549 					 const char *cmd)
550     {
551       opts[num][0] = cmd;
552       switch ((int) pvw)
553 	{
554 	case PVW_AVX128:
555 	  opts[num++][1] = "128";
556 	  break;
557 
558 	case PVW_AVX256:
559 	  opts[num++][1] = "256";
560 	  break;
561 
562 	case PVW_AVX512:
563 	  opts[num++][1] = "512";
564 	  break;
565 
566 	default:
567 	  gcc_unreachable ();
568 	}
569     };
570 
571   /* Add -mprefer-vector-width= option.  */
572   if (pvw)
573     add_vector_width (pvw, "-mprefer-vector-width=");
574 
575   /* Add -mmove-max= option.  */
576   if (move_max)
577     add_vector_width (move_max, "-mmove-max=");
578 
579   /* Add -mstore-max= option.  */
580   if (store_max)
581     add_vector_width (store_max, "-mstore-max=");
582 
583   /* Any options?  */
584   if (num == 0)
585     return NULL;
586 
587   gcc_assert (num < ARRAY_SIZE (opts));
588 
589   /* Size the string.  */
590   len = 0;
591   sep_len = (add_nl_p) ? 3 : 1;
592   for (i = 0; i < num; i++)
593     {
594       len += sep_len;
595       for (j = 0; j < 2; j++)
596 	if (opts[i][j])
597 	  len += strlen (opts[i][j]);
598     }
599 
600   /* Build the string.  */
601   ret = ptr = (char *) xmalloc (len);
602   line_len = 0;
603 
604   for (i = 0; i < num; i++)
605     {
606       size_t len2[2];
607 
608       for (j = 0; j < 2; j++)
609 	len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
610 
611       if (i != 0)
612 	{
613 	  *ptr++ = ' ';
614 	  line_len++;
615 
616 	  if (add_nl_p && line_len + len2[0] + len2[1] > 70)
617 	    {
618 	      *ptr++ = '\\';
619 	      *ptr++ = '\n';
620 	      line_len = 0;
621 	    }
622 	}
623 
624       for (j = 0; j < 2; j++)
625 	if (opts[i][j])
626 	  {
627 	    memcpy (ptr, opts[i][j], len2[j]);
628 	    ptr += len2[j];
629 	    line_len += len2[j];
630 	  }
631     }
632 
633   *ptr = '\0';
634   gcc_assert (ret + len >= ptr);
635 
636   return ret;
637 }
638 
639 /* Function that is callable from the debugger to print the current
640    options.  */
641 void ATTRIBUTE_UNUSED
ix86_debug_options(void)642 ix86_debug_options (void)
643 {
644   char *opts = ix86_target_string (ix86_isa_flags, ix86_isa_flags2,
645 				   target_flags, ix86_target_flags,
646 				   ix86_arch_string, ix86_tune_string,
647 				   ix86_fpmath, prefer_vector_width_type,
648 				   ix86_move_max, ix86_store_max,
649 				   true, true);
650 
651   if (opts)
652     {
653       fprintf (stderr, "%s\n\n", opts);
654       free (opts);
655     }
656   else
657     fputs ("<no options>\n\n", stderr);
658 
659   return;
660 }
661 
662 /* Save the current options */
663 
664 void
ix86_function_specific_save(struct cl_target_option * ptr,struct gcc_options * opts,struct gcc_options *)665 ix86_function_specific_save (struct cl_target_option *ptr,
666 			     struct gcc_options *opts,
667 			     struct gcc_options */* opts_set */)
668 {
669   ptr->arch = ix86_arch;
670   ptr->schedule = ix86_schedule;
671   ptr->prefetch_sse = ix86_prefetch_sse;
672   ptr->tune = ix86_tune;
673   ptr->branch_cost = ix86_branch_cost;
674   ptr->tune_defaulted = ix86_tune_defaulted;
675   ptr->arch_specified = ix86_arch_specified;
676   ptr->x_ix86_isa_flags_explicit = opts->x_ix86_isa_flags_explicit;
677   ptr->x_ix86_isa_flags2_explicit = opts->x_ix86_isa_flags2_explicit;
678   ptr->x_recip_mask_explicit = opts->x_recip_mask_explicit;
679   ptr->x_ix86_arch_string = opts->x_ix86_arch_string;
680   ptr->x_ix86_tune_string = opts->x_ix86_tune_string;
681   ptr->x_ix86_asm_dialect = opts->x_ix86_asm_dialect;
682   ptr->x_ix86_branch_cost = opts->x_ix86_branch_cost;
683   ptr->x_ix86_dump_tunes = opts->x_ix86_dump_tunes;
684   ptr->x_ix86_force_align_arg_pointer = opts->x_ix86_force_align_arg_pointer;
685   ptr->x_ix86_force_drap = opts->x_ix86_force_drap;
686   ptr->x_ix86_recip_name = opts->x_ix86_recip_name;
687   ptr->x_ix86_section_threshold = opts->x_ix86_section_threshold;
688   ptr->x_ix86_sse2avx = opts->x_ix86_sse2avx;
689   ptr->x_ix86_stack_protector_guard = opts->x_ix86_stack_protector_guard;
690   ptr->x_ix86_stringop_alg = opts->x_ix86_stringop_alg;
691   ptr->x_ix86_tls_dialect = opts->x_ix86_tls_dialect;
692   ptr->x_ix86_tune_ctrl_string = opts->x_ix86_tune_ctrl_string;
693   ptr->x_ix86_tune_memcpy_strategy = opts->x_ix86_tune_memcpy_strategy;
694   ptr->x_ix86_tune_memset_strategy = opts->x_ix86_tune_memset_strategy;
695   ptr->x_ix86_tune_no_default = opts->x_ix86_tune_no_default;
696 
697   /* The fields are char but the variables are not; make sure the
698      values fit in the fields.  */
699   gcc_assert (ptr->arch == ix86_arch);
700   gcc_assert (ptr->schedule == ix86_schedule);
701   gcc_assert (ptr->tune == ix86_tune);
702   gcc_assert (ptr->branch_cost == ix86_branch_cost);
703 }
704 
705 /* Feature tests against the various architecture variations, used to create
706    ix86_arch_features based on the processor mask.  */
707 static unsigned HOST_WIDE_INT initial_ix86_arch_features[X86_ARCH_LAST] = {
708   /* X86_ARCH_CMOV: Conditional move was added for pentiumpro.  */
709   ~(m_386 | m_486 | m_PENT | m_LAKEMONT | m_K6),
710 
711   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
712   ~m_386,
713 
714   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
715   ~(m_386 | m_486),
716 
717   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
718   ~m_386,
719 
720   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
721   ~m_386,
722 };
723 
724 /* This table must be in sync with enum processor_type in i386.h.  */
725 static const struct processor_costs *processor_cost_table[] =
726 {
727   &generic_cost,
728   &i386_cost,
729   &i486_cost,
730   &pentium_cost,
731   &lakemont_cost,
732   &pentiumpro_cost,
733   &pentium4_cost,
734   &nocona_cost,
735   &core_cost,
736   &core_cost,
737   &core_cost,
738   &core_cost,
739   &atom_cost,
740   &slm_cost,
741   &slm_cost,
742   &slm_cost,
743   &tremont_cost,
744   &slm_cost,
745   &slm_cost,
746   &skylake_cost,
747   &skylake_cost,
748   &icelake_cost,
749   &icelake_cost,
750   &icelake_cost,
751   &skylake_cost,
752   &icelake_cost,
753   &skylake_cost,
754   &icelake_cost,
755   &alderlake_cost,
756   &icelake_cost,
757   &intel_cost,
758   &geode_cost,
759   &k6_cost,
760   &athlon_cost,
761   &k8_cost,
762   &amdfam10_cost,
763   &bdver_cost,
764   &bdver_cost,
765   &bdver_cost,
766   &bdver_cost,
767   &btver1_cost,
768   &btver2_cost,
769   &znver1_cost,
770   &znver2_cost,
771   &znver3_cost,
772   &znver4_cost
773 };
774 
775 /* Guarantee that the array is aligned with enum processor_type.  */
776 STATIC_ASSERT (ARRAY_SIZE (processor_cost_table) == PROCESSOR_max);
777 
778 static bool
779 ix86_option_override_internal (bool main_args_p,
780 			       struct gcc_options *opts,
781 			       struct gcc_options *opts_set);
782 static void
783 set_ix86_tune_features (struct gcc_options *opts,
784 			enum processor_type ix86_tune, bool dump);
785 
786 /* Restore the current options */
787 
788 void
ix86_function_specific_restore(struct gcc_options * opts,struct gcc_options *,struct cl_target_option * ptr)789 ix86_function_specific_restore (struct gcc_options *opts,
790 				struct gcc_options */* opts_set */,
791 				struct cl_target_option *ptr)
792 {
793   enum processor_type old_tune = ix86_tune;
794   enum processor_type old_arch = ix86_arch;
795   unsigned HOST_WIDE_INT ix86_arch_mask;
796   int i;
797 
798   /* We don't change -fPIC.  */
799   opts->x_flag_pic = flag_pic;
800 
801   ix86_arch = (enum processor_type) ptr->arch;
802   ix86_schedule = (enum attr_cpu) ptr->schedule;
803   ix86_tune = (enum processor_type) ptr->tune;
804   ix86_prefetch_sse = ptr->prefetch_sse;
805   ix86_tune_defaulted = ptr->tune_defaulted;
806   ix86_arch_specified = ptr->arch_specified;
807   opts->x_ix86_isa_flags_explicit = ptr->x_ix86_isa_flags_explicit;
808   opts->x_ix86_isa_flags2_explicit = ptr->x_ix86_isa_flags2_explicit;
809   opts->x_recip_mask_explicit = ptr->x_recip_mask_explicit;
810   opts->x_ix86_arch_string = ptr->x_ix86_arch_string;
811   opts->x_ix86_tune_string = ptr->x_ix86_tune_string;
812   opts->x_ix86_asm_dialect = ptr->x_ix86_asm_dialect;
813   opts->x_ix86_branch_cost = ptr->x_ix86_branch_cost;
814   opts->x_ix86_dump_tunes = ptr->x_ix86_dump_tunes;
815   opts->x_ix86_force_align_arg_pointer = ptr->x_ix86_force_align_arg_pointer;
816   opts->x_ix86_force_drap = ptr->x_ix86_force_drap;
817   opts->x_ix86_recip_name = ptr->x_ix86_recip_name;
818   opts->x_ix86_section_threshold = ptr->x_ix86_section_threshold;
819   opts->x_ix86_sse2avx = ptr->x_ix86_sse2avx;
820   opts->x_ix86_stack_protector_guard = ptr->x_ix86_stack_protector_guard;
821   opts->x_ix86_stringop_alg = ptr->x_ix86_stringop_alg;
822   opts->x_ix86_tls_dialect = ptr->x_ix86_tls_dialect;
823   opts->x_ix86_tune_ctrl_string = ptr->x_ix86_tune_ctrl_string;
824   opts->x_ix86_tune_memcpy_strategy = ptr->x_ix86_tune_memcpy_strategy;
825   opts->x_ix86_tune_memset_strategy = ptr->x_ix86_tune_memset_strategy;
826   opts->x_ix86_tune_no_default = ptr->x_ix86_tune_no_default;
827   ix86_tune_cost = processor_cost_table[ix86_tune];
828   /* TODO: ix86_cost should be chosen at instruction or function granuality
829      so for cold code we use size_cost even in !optimize_size compilation.  */
830   if (opts->x_optimize_size)
831     ix86_cost = &ix86_size_cost;
832   else
833     ix86_cost = ix86_tune_cost;
834 
835   /* Recreate the arch feature tests if the arch changed */
836   if (old_arch != ix86_arch)
837     {
838       ix86_arch_mask = HOST_WIDE_INT_1U << ix86_arch;
839       for (i = 0; i < X86_ARCH_LAST; ++i)
840 	ix86_arch_features[i]
841 	  = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
842     }
843 
844   /* Recreate the tune optimization tests */
845   if (old_tune != ix86_tune)
846     set_ix86_tune_features (opts, ix86_tune, false);
847 }
848 
849 /* Adjust target options after streaming them in.  This is mainly about
850    reconciling them with global options.  */
851 
852 void
ix86_function_specific_post_stream_in(struct cl_target_option * ptr)853 ix86_function_specific_post_stream_in (struct cl_target_option *ptr)
854 {
855   /* flag_pic is a global option, but ix86_cmodel is target saved option
856      partly computed from flag_pic.  If flag_pic is on, adjust x_ix86_cmodel
857      for PIC, or error out.  */
858   if (flag_pic)
859     switch (ptr->x_ix86_cmodel)
860       {
861       case CM_SMALL:
862 	ptr->x_ix86_cmodel = CM_SMALL_PIC;
863 	break;
864 
865       case CM_MEDIUM:
866 	ptr->x_ix86_cmodel = CM_MEDIUM_PIC;
867 	break;
868 
869       case CM_LARGE:
870 	ptr->x_ix86_cmodel = CM_LARGE_PIC;
871 	break;
872 
873       case CM_KERNEL:
874 	error ("code model %s does not support PIC mode", "kernel");
875 	break;
876 
877       default:
878 	break;
879       }
880   else
881     switch (ptr->x_ix86_cmodel)
882       {
883       case CM_SMALL_PIC:
884 	ptr->x_ix86_cmodel = CM_SMALL;
885 	break;
886 
887       case CM_MEDIUM_PIC:
888 	ptr->x_ix86_cmodel = CM_MEDIUM;
889 	break;
890 
891       case CM_LARGE_PIC:
892 	ptr->x_ix86_cmodel = CM_LARGE;
893 	break;
894 
895       default:
896 	break;
897       }
898 }
899 
900 /* Print the current options */
901 
902 void
ix86_function_specific_print(FILE * file,int indent,struct cl_target_option * ptr)903 ix86_function_specific_print (FILE *file, int indent,
904 			      struct cl_target_option *ptr)
905 {
906   char *target_string
907     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_ix86_isa_flags2,
908 			  ptr->x_target_flags, ptr->x_ix86_target_flags,
909 			  NULL, NULL, ptr->x_ix86_fpmath,
910 			  ptr->x_prefer_vector_width_type,
911 			  ptr->x_ix86_move_max, ptr->x_ix86_store_max,
912 			  false, true);
913 
914   gcc_assert (ptr->arch < PROCESSOR_max);
915   fprintf (file, "%*sarch = %d (%s)\n",
916 	   indent, "",
917 	   ptr->arch, processor_names[ptr->arch]);
918 
919   gcc_assert (ptr->tune < PROCESSOR_max);
920   fprintf (file, "%*stune = %d (%s)\n",
921 	   indent, "",
922 	   ptr->tune, processor_names[ptr->tune]);
923 
924   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
925 
926   if (target_string)
927     {
928       fprintf (file, "%*s%s\n", indent, "", target_string);
929       free (target_string);
930     }
931 }
932 
933 
934 /* Inner function to process the attribute((target(...))), take an argument and
935    set the current options from the argument. If we have a list, recursively go
936    over the list.  */
937 
938 static bool
ix86_valid_target_attribute_inner_p(tree fndecl,tree args,char * p_strings[],struct gcc_options * opts,struct gcc_options * opts_set,struct gcc_options * enum_opts_set,bool target_clone_attr)939 ix86_valid_target_attribute_inner_p (tree fndecl, tree args, char *p_strings[],
940 				     struct gcc_options *opts,
941 				     struct gcc_options *opts_set,
942 				     struct gcc_options *enum_opts_set,
943 				     bool target_clone_attr)
944 {
945   char *next_optstr;
946   bool ret = true;
947 
948 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
949 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
950 #define IX86_ATTR_ENUM(S,O)  { S, sizeof (S)-1, ix86_opt_enum, O, 0 }
951 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
952 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
953 #define IX86_ATTR_IX86_YES(S,O,M) \
954   { S, sizeof (S)-1, ix86_opt_ix86_yes, O, M }
955 #define IX86_ATTR_IX86_NO(S,O,M) \
956   { S, sizeof (S)-1, ix86_opt_ix86_no,  O, M }
957 
958   enum ix86_opt_type
959   {
960     ix86_opt_unknown,
961     ix86_opt_yes,
962     ix86_opt_no,
963     ix86_opt_ix86_yes,
964     ix86_opt_ix86_no,
965     ix86_opt_str,
966     ix86_opt_enum,
967     ix86_opt_isa
968   };
969 
970   static const struct
971   {
972     const char *string;
973     size_t len;
974     enum ix86_opt_type type;
975     int opt;
976     int mask;
977   } attrs[] = {
978     /* isa options */
979     IX86_ATTR_ISA ("pconfig",	OPT_mpconfig),
980     IX86_ATTR_ISA ("wbnoinvd",	OPT_mwbnoinvd),
981     IX86_ATTR_ISA ("sgx",	OPT_msgx),
982     IX86_ATTR_ISA ("avx5124fmaps", OPT_mavx5124fmaps),
983     IX86_ATTR_ISA ("avx5124vnniw", OPT_mavx5124vnniw),
984     IX86_ATTR_ISA ("avx512vpopcntdq", OPT_mavx512vpopcntdq),
985     IX86_ATTR_ISA ("avx512vbmi2", OPT_mavx512vbmi2),
986     IX86_ATTR_ISA ("avx512vnni", OPT_mavx512vnni),
987     IX86_ATTR_ISA ("avx512bitalg", OPT_mavx512bitalg),
988     IX86_ATTR_ISA ("avx512vp2intersect", OPT_mavx512vp2intersect),
989 
990     IX86_ATTR_ISA ("avx512vbmi", OPT_mavx512vbmi),
991     IX86_ATTR_ISA ("avx512ifma", OPT_mavx512ifma),
992     IX86_ATTR_ISA ("avx512vl",	OPT_mavx512vl),
993     IX86_ATTR_ISA ("avx512bw",	OPT_mavx512bw),
994     IX86_ATTR_ISA ("avx512dq",	OPT_mavx512dq),
995     IX86_ATTR_ISA ("avx512er",	OPT_mavx512er),
996     IX86_ATTR_ISA ("avx512pf",	OPT_mavx512pf),
997     IX86_ATTR_ISA ("avx512cd",	OPT_mavx512cd),
998     IX86_ATTR_ISA ("avx512f",	OPT_mavx512f),
999     IX86_ATTR_ISA ("avx2",	OPT_mavx2),
1000     IX86_ATTR_ISA ("fma",	OPT_mfma),
1001     IX86_ATTR_ISA ("xop",	OPT_mxop),
1002     IX86_ATTR_ISA ("fma4",	OPT_mfma4),
1003     IX86_ATTR_ISA ("f16c",	OPT_mf16c),
1004     IX86_ATTR_ISA ("avx",	OPT_mavx),
1005     IX86_ATTR_ISA ("sse4",	OPT_msse4),
1006     IX86_ATTR_ISA ("sse4.2",	OPT_msse4_2),
1007     IX86_ATTR_ISA ("sse4.1",	OPT_msse4_1),
1008     IX86_ATTR_ISA ("sse4a",	OPT_msse4a),
1009     IX86_ATTR_ISA ("ssse3",	OPT_mssse3),
1010     IX86_ATTR_ISA ("sse3",	OPT_msse3),
1011     IX86_ATTR_ISA ("aes",	OPT_maes),
1012     IX86_ATTR_ISA ("sha",	OPT_msha),
1013     IX86_ATTR_ISA ("pclmul",	OPT_mpclmul),
1014     IX86_ATTR_ISA ("sse2",	OPT_msse2),
1015     IX86_ATTR_ISA ("sse",	OPT_msse),
1016     IX86_ATTR_ISA ("3dnowa",	OPT_m3dnowa),
1017     IX86_ATTR_ISA ("3dnow",	OPT_m3dnow),
1018     IX86_ATTR_ISA ("mmx",	OPT_mmmx),
1019     IX86_ATTR_ISA ("rtm",	OPT_mrtm),
1020     IX86_ATTR_ISA ("prfchw",	OPT_mprfchw),
1021     IX86_ATTR_ISA ("rdseed",	OPT_mrdseed),
1022     IX86_ATTR_ISA ("adx",	OPT_madx),
1023     IX86_ATTR_ISA ("prefetchwt1", OPT_mprefetchwt1),
1024     IX86_ATTR_ISA ("clflushopt", OPT_mclflushopt),
1025     IX86_ATTR_ISA ("xsaves",	OPT_mxsaves),
1026     IX86_ATTR_ISA ("xsavec",	OPT_mxsavec),
1027     IX86_ATTR_ISA ("xsaveopt",	OPT_mxsaveopt),
1028     IX86_ATTR_ISA ("xsave",	OPT_mxsave),
1029     IX86_ATTR_ISA ("abm",	OPT_mabm),
1030     IX86_ATTR_ISA ("bmi",	OPT_mbmi),
1031     IX86_ATTR_ISA ("bmi2",	OPT_mbmi2),
1032     IX86_ATTR_ISA ("lzcnt",	OPT_mlzcnt),
1033     IX86_ATTR_ISA ("tbm",	OPT_mtbm),
1034     IX86_ATTR_ISA ("popcnt",	OPT_mpopcnt),
1035     IX86_ATTR_ISA ("cx16",	OPT_mcx16),
1036     IX86_ATTR_ISA ("sahf",	OPT_msahf),
1037     IX86_ATTR_ISA ("movbe",	OPT_mmovbe),
1038     IX86_ATTR_ISA ("crc32",	OPT_mcrc32),
1039     IX86_ATTR_ISA ("fsgsbase",	OPT_mfsgsbase),
1040     IX86_ATTR_ISA ("rdrnd",	OPT_mrdrnd),
1041     IX86_ATTR_ISA ("mwaitx",	OPT_mmwaitx),
1042     IX86_ATTR_ISA ("mwait",	OPT_mmwait),
1043     IX86_ATTR_ISA ("clzero",	OPT_mclzero),
1044     IX86_ATTR_ISA ("pku",	OPT_mpku),
1045     IX86_ATTR_ISA ("lwp",	OPT_mlwp),
1046     IX86_ATTR_ISA ("hle",	OPT_mhle),
1047     IX86_ATTR_ISA ("fxsr",	OPT_mfxsr),
1048     IX86_ATTR_ISA ("clwb",	OPT_mclwb),
1049     IX86_ATTR_ISA ("rdpid",	OPT_mrdpid),
1050     IX86_ATTR_ISA ("gfni",	OPT_mgfni),
1051     IX86_ATTR_ISA ("shstk",	OPT_mshstk),
1052     IX86_ATTR_ISA ("vaes",	OPT_mvaes),
1053     IX86_ATTR_ISA ("vpclmulqdq", OPT_mvpclmulqdq),
1054     IX86_ATTR_ISA ("movdiri", OPT_mmovdiri),
1055     IX86_ATTR_ISA ("movdir64b", OPT_mmovdir64b),
1056     IX86_ATTR_ISA ("waitpkg", OPT_mwaitpkg),
1057     IX86_ATTR_ISA ("cldemote", OPT_mcldemote),
1058     IX86_ATTR_ISA ("uintr", OPT_muintr),
1059     IX86_ATTR_ISA ("ptwrite",   OPT_mptwrite),
1060     IX86_ATTR_ISA ("kl", OPT_mkl),
1061     IX86_ATTR_ISA ("widekl",	OPT_mwidekl),
1062     IX86_ATTR_ISA ("avx512bf16",   OPT_mavx512bf16),
1063     IX86_ATTR_ISA ("enqcmd", OPT_menqcmd),
1064     IX86_ATTR_ISA ("serialize", OPT_mserialize),
1065     IX86_ATTR_ISA ("tsxldtrk", OPT_mtsxldtrk),
1066     IX86_ATTR_ISA ("amx-tile", OPT_mamx_tile),
1067     IX86_ATTR_ISA ("amx-int8", OPT_mamx_int8),
1068     IX86_ATTR_ISA ("amx-bf16", OPT_mamx_bf16),
1069     IX86_ATTR_ISA ("hreset", OPT_mhreset),
1070     IX86_ATTR_ISA ("avxvnni",   OPT_mavxvnni),
1071     IX86_ATTR_ISA ("avx512fp16", OPT_mavx512fp16),
1072 
1073     /* enum options */
1074     IX86_ATTR_ENUM ("fpmath=",	OPT_mfpmath_),
1075     IX86_ATTR_ENUM ("prefer-vector-width=", OPT_mprefer_vector_width_),
1076 
1077     /* string options */
1078     IX86_ATTR_STR ("arch=",	IX86_FUNCTION_SPECIFIC_ARCH),
1079     IX86_ATTR_STR ("tune=",	IX86_FUNCTION_SPECIFIC_TUNE),
1080 
1081     /* flag options */
1082     IX86_ATTR_YES ("cld",
1083 		   OPT_mcld,
1084 		   MASK_CLD),
1085 
1086     IX86_ATTR_NO ("fancy-math-387",
1087 		  OPT_mfancy_math_387,
1088 		  MASK_NO_FANCY_MATH_387),
1089 
1090     IX86_ATTR_YES ("ieee-fp",
1091 		   OPT_mieee_fp,
1092 		   MASK_IEEE_FP),
1093 
1094     IX86_ATTR_YES ("inline-all-stringops",
1095 		   OPT_minline_all_stringops,
1096 		   MASK_INLINE_ALL_STRINGOPS),
1097 
1098     IX86_ATTR_YES ("inline-stringops-dynamically",
1099 		   OPT_minline_stringops_dynamically,
1100 		   MASK_INLINE_STRINGOPS_DYNAMICALLY),
1101 
1102     IX86_ATTR_NO ("align-stringops",
1103 		  OPT_mno_align_stringops,
1104 		  MASK_NO_ALIGN_STRINGOPS),
1105 
1106     IX86_ATTR_YES ("recip",
1107 		   OPT_mrecip,
1108 		   MASK_RECIP),
1109 
1110     IX86_ATTR_IX86_YES ("general-regs-only",
1111 			OPT_mgeneral_regs_only,
1112 			OPTION_MASK_GENERAL_REGS_ONLY),
1113 
1114     IX86_ATTR_YES ("relax-cmpxchg-loop",
1115 		   OPT_mrelax_cmpxchg_loop,
1116 		   MASK_RELAX_CMPXCHG_LOOP),
1117   };
1118 
1119   location_t loc
1120     = fndecl == NULL ? UNKNOWN_LOCATION : DECL_SOURCE_LOCATION (fndecl);
1121   const char *attr_name = target_clone_attr ? "target_clone" : "target";
1122 
1123   /* If this is a list, recurse to get the options.  */
1124   if (TREE_CODE (args) == TREE_LIST)
1125     {
1126       for (; args; args = TREE_CHAIN (args))
1127 	if (TREE_VALUE (args)
1128 	    && !ix86_valid_target_attribute_inner_p (fndecl, TREE_VALUE (args),
1129 						     p_strings, opts, opts_set,
1130 						     enum_opts_set,
1131 						     target_clone_attr))
1132 	  ret = false;
1133 
1134       return ret;
1135     }
1136 
1137   else if (TREE_CODE (args) != STRING_CST)
1138     {
1139       error_at (loc, "attribute %qs argument is not a string", attr_name);
1140       return false;
1141     }
1142 
1143   /* Handle multiple arguments separated by commas.  */
1144   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
1145 
1146   while (next_optstr && *next_optstr != '\0')
1147     {
1148       char *p = next_optstr;
1149       char *orig_p = p;
1150       char *comma = strchr (next_optstr, ',');
1151       size_t len, opt_len;
1152       int opt;
1153       bool opt_set_p;
1154       char ch;
1155       unsigned i;
1156       enum ix86_opt_type type = ix86_opt_unknown;
1157       int mask = 0;
1158 
1159       if (comma)
1160 	{
1161 	  *comma = '\0';
1162 	  len = comma - next_optstr;
1163 	  next_optstr = comma + 1;
1164 	}
1165       else
1166 	{
1167 	  len = strlen (p);
1168 	  next_optstr = NULL;
1169 	}
1170 
1171       /* Recognize no-xxx.  */
1172       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
1173 	{
1174 	  opt_set_p = false;
1175 	  p += 3;
1176 	  len -= 3;
1177 	}
1178       else
1179 	opt_set_p = true;
1180 
1181       /* Find the option.  */
1182       ch = *p;
1183       opt = N_OPTS;
1184       for (i = 0; i < ARRAY_SIZE (attrs); i++)
1185 	{
1186 	  type = attrs[i].type;
1187 	  opt_len = attrs[i].len;
1188 	  if (ch == attrs[i].string[0]
1189 	      && ((type != ix86_opt_str && type != ix86_opt_enum)
1190 		  ? len == opt_len
1191 		  : len > opt_len)
1192 	      && memcmp (p, attrs[i].string, opt_len) == 0)
1193 	    {
1194 	      opt = attrs[i].opt;
1195 	      mask = attrs[i].mask;
1196 	      break;
1197 	    }
1198 	}
1199 
1200       /* Process the option.  */
1201       if (opt == N_OPTS)
1202 	{
1203 	  error_at (loc, "attribute %qs argument %qs is unknown",
1204 		    attr_name, orig_p);
1205 	  ret = false;
1206 	}
1207 
1208       else if (type == ix86_opt_isa)
1209 	{
1210 	  struct cl_decoded_option decoded;
1211 
1212 	  generate_option (opt, NULL, opt_set_p, CL_TARGET, &decoded);
1213 	  ix86_handle_option (opts, opts_set,
1214 			      &decoded, input_location);
1215 	}
1216 
1217       else if (type == ix86_opt_yes || type == ix86_opt_no)
1218 	{
1219 	  if (type == ix86_opt_no)
1220 	    opt_set_p = !opt_set_p;
1221 
1222 	  if (opt_set_p)
1223 	    opts->x_target_flags |= mask;
1224 	  else
1225 	    opts->x_target_flags &= ~mask;
1226 	}
1227 
1228       else if (type == ix86_opt_ix86_yes || type == ix86_opt_ix86_no)
1229 	{
1230 	  if (mask == OPTION_MASK_GENERAL_REGS_ONLY)
1231 	    {
1232 	      if (!opt_set_p)
1233 		{
1234 		  error_at (loc, "pragma or attribute %<target(\"%s\")%> "
1235 			    "does not allow a negated form", p);
1236 		  return false;
1237 		}
1238 
1239 	      if (type != ix86_opt_ix86_yes)
1240 		gcc_unreachable ();
1241 
1242 	      opts->x_ix86_target_flags |= mask;
1243 
1244 	      struct cl_decoded_option decoded;
1245 	      generate_option (opt, NULL, opt_set_p, CL_TARGET,
1246 			       &decoded);
1247 	      ix86_handle_option (opts, opts_set, &decoded,
1248 				  input_location);
1249 	    }
1250 	  else
1251 	    {
1252 	      if (type == ix86_opt_ix86_no)
1253 		opt_set_p = !opt_set_p;
1254 
1255 	      if (opt_set_p)
1256 		opts->x_ix86_target_flags |= mask;
1257 	      else
1258 		opts->x_ix86_target_flags &= ~mask;
1259 	    }
1260 	}
1261 
1262       else if (type == ix86_opt_str)
1263 	{
1264 	  if (p_strings[opt])
1265 	    {
1266 	      error_at (loc, "attribute value %qs was already specified "
1267 			"in %qs attribute", orig_p, attr_name);
1268 	      ret = false;
1269 	    }
1270 	  else
1271 	    {
1272 	      p_strings[opt] = xstrdup (p + opt_len);
1273 	      if (opt == IX86_FUNCTION_SPECIFIC_ARCH)
1274 		{
1275 		  /* If arch= is set,  clear all bits in x_ix86_isa_flags,
1276 		     except for ISA_64BIT, ABI_64, ABI_X32, and CODE16
1277 		     and all bits in x_ix86_isa_flags2.  */
1278 		  opts->x_ix86_isa_flags &= (OPTION_MASK_ISA_64BIT
1279 					     | OPTION_MASK_ABI_64
1280 					     | OPTION_MASK_ABI_X32
1281 					     | OPTION_MASK_CODE16);
1282 		  opts->x_ix86_isa_flags_explicit &= (OPTION_MASK_ISA_64BIT
1283 						      | OPTION_MASK_ABI_64
1284 						      | OPTION_MASK_ABI_X32
1285 						      | OPTION_MASK_CODE16);
1286 		  opts->x_ix86_isa_flags2 = 0;
1287 		  opts->x_ix86_isa_flags2_explicit = 0;
1288 		}
1289 	    }
1290 	}
1291 
1292       else if (type == ix86_opt_enum)
1293 	{
1294 	  bool arg_ok;
1295 	  int value;
1296 
1297 	  arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
1298 	  if (arg_ok)
1299 	    set_option (opts, enum_opts_set, opt, value,
1300 			p + opt_len, DK_UNSPECIFIED, input_location,
1301 			global_dc);
1302 	  else
1303 	    {
1304 	      error_at (loc, "attribute value %qs is unknown in %qs attribute",
1305 			orig_p, attr_name);
1306 	      ret = false;
1307 	    }
1308 	}
1309 
1310       else
1311 	gcc_unreachable ();
1312     }
1313 
1314   return ret;
1315 }
1316 
1317 /* Release allocated strings.  */
1318 static void
release_options_strings(char ** option_strings)1319 release_options_strings (char **option_strings)
1320 {
1321   /* Free up memory allocated to hold the strings */
1322   for (unsigned i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
1323     free (option_strings[i]);
1324 }
1325 
1326 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
1327 
1328 tree
ix86_valid_target_attribute_tree(tree fndecl,tree args,struct gcc_options * opts,struct gcc_options * opts_set,bool target_clone_attr)1329 ix86_valid_target_attribute_tree (tree fndecl, tree args,
1330 				  struct gcc_options *opts,
1331 				  struct gcc_options *opts_set,
1332 				  bool target_clone_attr)
1333 {
1334   const char *orig_arch_string = opts->x_ix86_arch_string;
1335   const char *orig_tune_string = opts->x_ix86_tune_string;
1336   enum fpmath_unit orig_fpmath_set = opts_set->x_ix86_fpmath;
1337   enum prefer_vector_width orig_pvw_set = opts_set->x_prefer_vector_width_type;
1338   enum prefer_vector_width orig_ix86_move_max_set
1339     = opts_set->x_ix86_move_max;
1340   enum prefer_vector_width orig_ix86_store_max_set
1341     = opts_set->x_ix86_store_max;
1342   int orig_tune_defaulted = ix86_tune_defaulted;
1343   int orig_arch_specified = ix86_arch_specified;
1344   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL };
1345   tree t = NULL_TREE;
1346   struct cl_target_option *def
1347     = TREE_TARGET_OPTION (target_option_default_node);
1348   struct gcc_options enum_opts_set;
1349 
1350   memset (&enum_opts_set, 0, sizeof (enum_opts_set));
1351 
1352   /* Process each of the options on the chain.  */
1353   if (!ix86_valid_target_attribute_inner_p (fndecl, args, option_strings, opts,
1354 					    opts_set, &enum_opts_set,
1355 					    target_clone_attr))
1356     return error_mark_node;
1357 
1358   /* If the changed options are different from the default, rerun
1359      ix86_option_override_internal, and then save the options away.
1360      The string options are attribute options, and will be undone
1361      when we copy the save structure.  */
1362   if (opts->x_ix86_isa_flags != def->x_ix86_isa_flags
1363       || opts->x_ix86_isa_flags2 != def->x_ix86_isa_flags2
1364       || opts->x_target_flags != def->x_target_flags
1365       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
1366       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
1367       || enum_opts_set.x_ix86_fpmath
1368       || enum_opts_set.x_prefer_vector_width_type)
1369     {
1370       /* If we are using the default tune= or arch=, undo the string assigned,
1371 	 and use the default.  */
1372       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
1373 	opts->x_ix86_arch_string
1374 	  = ggc_strdup (option_strings[IX86_FUNCTION_SPECIFIC_ARCH]);
1375       else if (!orig_arch_specified)
1376 	opts->x_ix86_arch_string = NULL;
1377 
1378       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
1379 	opts->x_ix86_tune_string
1380 	  = ggc_strdup (option_strings[IX86_FUNCTION_SPECIFIC_TUNE]);
1381       else if (orig_tune_defaulted)
1382 	opts->x_ix86_tune_string = NULL;
1383 
1384       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
1385       if (enum_opts_set.x_ix86_fpmath)
1386 	opts_set->x_ix86_fpmath = (enum fpmath_unit) 1;
1387       if (enum_opts_set.x_prefer_vector_width_type)
1388 	opts_set->x_prefer_vector_width_type = (enum prefer_vector_width) 1;
1389 
1390       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
1391       bool r = ix86_option_override_internal (false, opts, opts_set);
1392       if (!r)
1393 	{
1394 	  release_options_strings (option_strings);
1395 	  return error_mark_node;
1396 	}
1397 
1398       /* Add any builtin functions with the new isa if any.  */
1399       ix86_add_new_builtins (opts->x_ix86_isa_flags, opts->x_ix86_isa_flags2);
1400 
1401       enum excess_precision orig_ix86_excess_precision
1402 	= opts->x_ix86_excess_precision;
1403       bool orig_ix86_unsafe_math_optimizations
1404 	= opts->x_ix86_unsafe_math_optimizations;
1405       opts->x_ix86_excess_precision = opts->x_flag_excess_precision;
1406       opts->x_ix86_unsafe_math_optimizations
1407 	= opts->x_flag_unsafe_math_optimizations;
1408 
1409       /* Save the current options unless we are validating options for
1410 	 #pragma.  */
1411       t = build_target_option_node (opts, opts_set);
1412 
1413       opts->x_ix86_arch_string = orig_arch_string;
1414       opts->x_ix86_tune_string = orig_tune_string;
1415       opts_set->x_ix86_fpmath = orig_fpmath_set;
1416       opts_set->x_prefer_vector_width_type = orig_pvw_set;
1417       opts_set->x_ix86_move_max = orig_ix86_move_max_set;
1418       opts_set->x_ix86_store_max = orig_ix86_store_max_set;
1419       opts->x_ix86_excess_precision = orig_ix86_excess_precision;
1420       opts->x_ix86_unsafe_math_optimizations
1421 	= orig_ix86_unsafe_math_optimizations;
1422 
1423       release_options_strings (option_strings);
1424     }
1425 
1426   return t;
1427 }
1428 
1429 static GTY(()) tree target_attribute_cache[3];
1430 
1431 /* Hook to validate attribute((target("string"))).  */
1432 
1433 bool
ix86_valid_target_attribute_p(tree fndecl,tree ARG_UNUSED (name),tree args,int flags)1434 ix86_valid_target_attribute_p (tree fndecl,
1435 			       tree ARG_UNUSED (name),
1436 			       tree args,
1437 			       int flags)
1438 {
1439   struct gcc_options func_options, func_options_set;
1440   tree new_target, new_optimize;
1441   bool ret = true;
1442 
1443   /* attribute((target("default"))) does nothing, beyond
1444      affecting multi-versioning.  */
1445   if (TREE_VALUE (args)
1446       && TREE_CODE (TREE_VALUE (args)) == STRING_CST
1447       && TREE_CHAIN (args) == NULL_TREE
1448       && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
1449     return true;
1450 
1451   if ((DECL_FUNCTION_SPECIFIC_TARGET (fndecl) == target_attribute_cache[1]
1452        || DECL_FUNCTION_SPECIFIC_TARGET (fndecl) == NULL_TREE)
1453       && (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
1454 	  == target_attribute_cache[2]
1455 	  || DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) == NULL_TREE)
1456       && simple_cst_list_equal (args, target_attribute_cache[0]))
1457     {
1458       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = target_attribute_cache[1];
1459       DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl)
1460 	= target_attribute_cache[2];
1461       return true;
1462     }
1463 
1464   tree old_optimize = build_optimization_node (&global_options,
1465 					       &global_options_set);
1466 
1467   /* Get the optimization options of the current function.  */
1468   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
1469 
1470   if (!func_optimize)
1471     func_optimize = old_optimize;
1472 
1473   /* Init func_options.  */
1474   memset (&func_options, 0, sizeof (func_options));
1475   init_options_struct (&func_options, NULL);
1476   lang_hooks.init_options_struct (&func_options);
1477   memset (&func_options_set, 0, sizeof (func_options_set));
1478 
1479   cl_optimization_restore (&func_options, &func_options_set,
1480 			   TREE_OPTIMIZATION (func_optimize));
1481 
1482   /* Initialize func_options to the default before its target options can
1483      be set.  */
1484   tree old_target = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
1485   if (old_target == NULL_TREE)
1486     old_target = target_option_default_node;
1487   cl_target_option_restore (&func_options, &func_options_set,
1488 			    TREE_TARGET_OPTION (old_target));
1489 
1490   /* FLAGS == 1 is used for target_clones attribute.  */
1491   new_target
1492     = ix86_valid_target_attribute_tree (fndecl, args, &func_options,
1493 					&func_options_set, flags == 1);
1494 
1495   new_optimize = build_optimization_node (&func_options, &func_options_set);
1496 
1497   if (new_target == error_mark_node)
1498     ret = false;
1499 
1500   else if (new_target)
1501     {
1502       if (DECL_FUNCTION_SPECIFIC_TARGET (fndecl) == NULL_TREE
1503 	  && DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) == NULL_TREE)
1504 	{
1505 	  target_attribute_cache[0] = copy_list (args);
1506 	  target_attribute_cache[1] = new_target;
1507 	  target_attribute_cache[2]
1508 	    = old_optimize != new_optimize ? new_optimize : NULL_TREE;
1509 	}
1510 
1511       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
1512 
1513       if (old_optimize != new_optimize)
1514 	DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
1515     }
1516 
1517   return ret;
1518 }
1519 
1520 const char *stringop_alg_names[] = {
1521 #define DEF_ALG(alg, name) #name,
1522 #include "stringop.def"
1523 #undef DEF_ALG
1524 };
1525 
1526 /* Parse parameter string passed to -mmemcpy-strategy= or -mmemset-strategy=.
1527    The string is of the following form (or comma separated list of it):
1528 
1529      strategy_alg:max_size:[align|noalign]
1530 
1531    where the full size range for the strategy is either [0, max_size] or
1532    [min_size, max_size], in which min_size is the max_size + 1 of the
1533    preceding range.  The last size range must have max_size == -1.
1534 
1535    Examples:
1536 
1537     1.
1538        -mmemcpy-strategy=libcall:-1:noalign
1539 
1540       this is equivalent to (for known size memcpy) -mstringop-strategy=libcall
1541 
1542 
1543    2.
1544       -mmemset-strategy=rep_8byte:16:noalign,vector_loop:2048:align,libcall:-1:noalign
1545 
1546       This is to tell the compiler to use the following strategy for memset
1547       1) when the expected size is between [1, 16], use rep_8byte strategy;
1548       2) when the size is between [17, 2048], use vector_loop;
1549       3) when the size is > 2048, use libcall.  */
1550 
1551 struct stringop_size_range
1552 {
1553   int max;
1554   stringop_alg alg;
1555   bool noalign;
1556 };
1557 
1558 static void
ix86_parse_stringop_strategy_string(char * strategy_str,bool is_memset)1559 ix86_parse_stringop_strategy_string (char *strategy_str, bool is_memset)
1560 {
1561   const struct stringop_algs *default_algs;
1562   stringop_size_range input_ranges[MAX_STRINGOP_ALGS];
1563   char *curr_range_str, *next_range_str;
1564   const char *opt = is_memset ? "-mmemset_strategy=" : "-mmemcpy_strategy=";
1565   int i = 0, n = 0;
1566 
1567   if (is_memset)
1568     default_algs = &ix86_cost->memset[TARGET_64BIT != 0];
1569   else
1570     default_algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
1571 
1572   curr_range_str = strategy_str;
1573 
1574   do
1575     {
1576       int maxs;
1577       char alg_name[128];
1578       char align[16];
1579       next_range_str = strchr (curr_range_str, ',');
1580       if (next_range_str)
1581         *next_range_str++ = '\0';
1582 
1583       if (sscanf (curr_range_str, "%20[^:]:%d:%10s", alg_name, &maxs,
1584 		  align) != 3)
1585         {
1586 	  error ("wrong argument %qs to option %qs", curr_range_str, opt);
1587           return;
1588         }
1589 
1590       if (n > 0 && (maxs < (input_ranges[n - 1].max + 1) && maxs != -1))
1591         {
1592 	  error ("size ranges of option %qs should be increasing", opt);
1593           return;
1594         }
1595 
1596       for (i = 0; i < last_alg; i++)
1597 	if (!strcmp (alg_name, stringop_alg_names[i]))
1598 	  break;
1599 
1600       if (i == last_alg)
1601         {
1602 	  error ("wrong strategy name %qs specified for option %qs",
1603 		 alg_name, opt);
1604 
1605 	  auto_vec <const char *> candidates;
1606 	  for (i = 0; i < last_alg; i++)
1607 	    if ((stringop_alg) i != rep_prefix_8_byte || TARGET_64BIT)
1608 	      candidates.safe_push (stringop_alg_names[i]);
1609 
1610 	  char *s;
1611 	  const char *hint
1612 	    = candidates_list_and_hint (alg_name, s, candidates);
1613 	  if (hint)
1614 	    inform (input_location,
1615 		    "valid arguments to %qs are: %s; did you mean %qs?",
1616 		    opt, s, hint);
1617 	  else
1618 	    inform (input_location, "valid arguments to %qs are: %s",
1619 		    opt, s);
1620 	  XDELETEVEC (s);
1621           return;
1622         }
1623 
1624       if ((stringop_alg) i == rep_prefix_8_byte
1625 	  && !TARGET_64BIT)
1626 	{
1627 	  /* rep; movq isn't available in 32-bit code.  */
1628 	  error ("strategy name %qs specified for option %qs "
1629 		 "not supported for 32-bit code", alg_name, opt);
1630 	  return;
1631 	}
1632 
1633       input_ranges[n].max = maxs;
1634       input_ranges[n].alg = (stringop_alg) i;
1635       if (!strcmp (align, "align"))
1636         input_ranges[n].noalign = false;
1637       else if (!strcmp (align, "noalign"))
1638         input_ranges[n].noalign = true;
1639       else
1640         {
1641 	  error ("unknown alignment %qs specified for option %qs", align, opt);
1642           return;
1643         }
1644       n++;
1645       curr_range_str = next_range_str;
1646     }
1647   while (curr_range_str);
1648 
1649   if (input_ranges[n - 1].max != -1)
1650     {
1651       error ("the max value for the last size range should be -1"
1652              " for option %qs", opt);
1653       return;
1654     }
1655 
1656   if (n > MAX_STRINGOP_ALGS)
1657     {
1658       error ("too many size ranges specified in option %qs", opt);
1659       return;
1660     }
1661 
1662   /* Now override the default algs array.  */
1663   for (i = 0; i < n; i++)
1664     {
1665       *const_cast<int *>(&default_algs->size[i].max) = input_ranges[i].max;
1666       *const_cast<stringop_alg *>(&default_algs->size[i].alg)
1667           = input_ranges[i].alg;
1668       *const_cast<int *>(&default_algs->size[i].noalign)
1669           = input_ranges[i].noalign;
1670     }
1671 }
1672 
1673 
1674 /* parse -mtune-ctrl= option. When DUMP is true,
1675    print the features that are explicitly set.  */
1676 
1677 static void
parse_mtune_ctrl_str(struct gcc_options * opts,bool dump)1678 parse_mtune_ctrl_str (struct gcc_options *opts, bool dump)
1679 {
1680   if (!opts->x_ix86_tune_ctrl_string)
1681     return;
1682 
1683   char *next_feature_string = NULL;
1684   char *curr_feature_string = xstrdup (opts->x_ix86_tune_ctrl_string);
1685   char *orig = curr_feature_string;
1686   int i;
1687   do
1688     {
1689       bool clear = false;
1690 
1691       next_feature_string = strchr (curr_feature_string, ',');
1692       if (next_feature_string)
1693         *next_feature_string++ = '\0';
1694       if (*curr_feature_string == '^')
1695         {
1696           curr_feature_string++;
1697           clear = true;
1698         }
1699       for (i = 0; i < X86_TUNE_LAST; i++)
1700         {
1701           if (!strcmp (curr_feature_string, ix86_tune_feature_names[i]))
1702             {
1703               ix86_tune_features[i] = !clear;
1704               if (dump)
1705                 fprintf (stderr, "Explicitly %s feature %s\n",
1706                          clear ? "clear" : "set", ix86_tune_feature_names[i]);
1707               break;
1708             }
1709         }
1710       if (i == X86_TUNE_LAST)
1711 	error ("unknown parameter to option %<-mtune-ctrl%>: %s",
1712 	       clear ? curr_feature_string - 1 : curr_feature_string);
1713       curr_feature_string = next_feature_string;
1714     }
1715   while (curr_feature_string);
1716   free (orig);
1717 }
1718 
1719 /* Helper function to set ix86_tune_features. IX86_TUNE is the
1720    processor type.  */
1721 
1722 static void
set_ix86_tune_features(struct gcc_options * opts,enum processor_type ix86_tune,bool dump)1723 set_ix86_tune_features (struct gcc_options *opts,
1724 			enum processor_type ix86_tune, bool dump)
1725 {
1726   unsigned HOST_WIDE_INT ix86_tune_mask = HOST_WIDE_INT_1U << ix86_tune;
1727   int i;
1728 
1729   for (i = 0; i < X86_TUNE_LAST; ++i)
1730     {
1731       if (ix86_tune_no_default)
1732         ix86_tune_features[i] = 0;
1733       else
1734 	ix86_tune_features[i]
1735 	  = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
1736     }
1737 
1738   if (dump)
1739     {
1740       fprintf (stderr, "List of x86 specific tuning parameter names:\n");
1741       for (i = 0; i < X86_TUNE_LAST; i++)
1742         fprintf (stderr, "%s : %s\n", ix86_tune_feature_names[i],
1743                  ix86_tune_features[i] ? "on" : "off");
1744     }
1745 
1746   parse_mtune_ctrl_str (opts, dump);
1747 }
1748 
1749 
1750 /* Default align_* from the processor table.  */
1751 
1752 static void
ix86_default_align(struct gcc_options * opts)1753 ix86_default_align (struct gcc_options *opts)
1754 {
1755   /* -falign-foo without argument: supply one.  */
1756   if (opts->x_flag_align_loops && !opts->x_str_align_loops)
1757     opts->x_str_align_loops = processor_cost_table[ix86_tune]->align_loop;
1758   if (opts->x_flag_align_jumps && !opts->x_str_align_jumps)
1759     opts->x_str_align_jumps = processor_cost_table[ix86_tune]->align_jump;
1760   if (opts->x_flag_align_labels && !opts->x_str_align_labels)
1761     opts->x_str_align_labels = processor_cost_table[ix86_tune]->align_label;
1762   if (opts->x_flag_align_functions && !opts->x_str_align_functions)
1763     opts->x_str_align_functions = processor_cost_table[ix86_tune]->align_func;
1764 }
1765 
1766 #ifndef USE_IX86_FRAME_POINTER
1767 #define USE_IX86_FRAME_POINTER 0
1768 #endif
1769 
1770 /* (Re)compute option overrides affected by optimization levels in
1771    target-specific ways.  */
1772 
1773 static void
ix86_recompute_optlev_based_flags(struct gcc_options * opts,struct gcc_options * opts_set)1774 ix86_recompute_optlev_based_flags (struct gcc_options *opts,
1775 				   struct gcc_options *opts_set)
1776 {
1777   /* Set the default values for switches whose default depends on TARGET_64BIT
1778      in case they weren't overwritten by command line options.  */
1779   if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
1780     {
1781       if (opts->x_optimize >= 1)
1782 	SET_OPTION_IF_UNSET (opts, opts_set, flag_omit_frame_pointer,
1783 			     !USE_IX86_FRAME_POINTER);
1784       if (opts->x_flag_asynchronous_unwind_tables
1785 	  && TARGET_64BIT_MS_ABI)
1786 	SET_OPTION_IF_UNSET (opts, opts_set, flag_unwind_tables, 1);
1787       if (opts->x_flag_asynchronous_unwind_tables == 2)
1788 	opts->x_flag_unwind_tables
1789 	  = opts->x_flag_asynchronous_unwind_tables = 1;
1790       if (opts->x_flag_pcc_struct_return == 2)
1791 	opts->x_flag_pcc_struct_return = 0;
1792     }
1793   else
1794     {
1795       if (opts->x_optimize >= 1)
1796 	  SET_OPTION_IF_UNSET (opts, opts_set, flag_omit_frame_pointer,
1797 			       !(USE_IX86_FRAME_POINTER || opts->x_optimize_size));
1798       if (opts->x_flag_asynchronous_unwind_tables == 2)
1799 	opts->x_flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
1800       if (opts->x_flag_pcc_struct_return == 2)
1801 	{
1802 	  /* Intel MCU psABI specifies that -freg-struct-return should
1803 	     be on.  Instead of setting DEFAULT_PCC_STRUCT_RETURN to 0,
1804 	     we check -miamcu so that -freg-struct-return is always
1805 	     turned on if -miamcu is used.  */
1806 	  if (TARGET_IAMCU_P (opts->x_target_flags))
1807 	    opts->x_flag_pcc_struct_return = 0;
1808 	  else
1809 	    opts->x_flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1810 	}
1811     }
1812 }
1813 
1814 /* Implement TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE hook.  */
1815 
1816 void
ix86_override_options_after_change(void)1817 ix86_override_options_after_change (void)
1818 {
1819   ix86_default_align (&global_options);
1820   ix86_recompute_optlev_based_flags (&global_options, &global_options_set);
1821 }
1822 
1823 /* Clear stack slot assignments remembered from previous functions.
1824    This is called from INIT_EXPANDERS once before RTL is emitted for each
1825    function.  */
1826 
1827 static struct machine_function *
ix86_init_machine_status(void)1828 ix86_init_machine_status (void)
1829 {
1830   struct machine_function *f;
1831 
1832   f = ggc_cleared_alloc<machine_function> ();
1833   f->call_abi = ix86_abi;
1834   f->stack_frame_required = true;
1835   f->silent_p = true;
1836 
1837   return f;
1838 }
1839 
1840 /* Override various settings based on options.  If MAIN_ARGS_P, the
1841    options are from the command line, otherwise they are from
1842    attributes.  Return true if there's an error related to march
1843    option.  */
1844 
1845 static bool
ix86_option_override_internal(bool main_args_p,struct gcc_options * opts,struct gcc_options * opts_set)1846 ix86_option_override_internal (bool main_args_p,
1847 			       struct gcc_options *opts,
1848 			       struct gcc_options *opts_set)
1849 {
1850   unsigned int i;
1851   unsigned HOST_WIDE_INT ix86_arch_mask;
1852   const bool ix86_tune_specified = (opts->x_ix86_tune_string != NULL);
1853 
1854   /* -mrecip options.  */
1855   static struct
1856     {
1857       const char *string;           /* option name */
1858       unsigned int mask;            /* mask bits to set */
1859     }
1860   const recip_options[] =
1861     {
1862       { "all",       RECIP_MASK_ALL },
1863       { "none",      RECIP_MASK_NONE },
1864       { "div",       RECIP_MASK_DIV },
1865       { "sqrt",      RECIP_MASK_SQRT },
1866       { "vec-div",   RECIP_MASK_VEC_DIV },
1867       { "vec-sqrt",  RECIP_MASK_VEC_SQRT },
1868     };
1869 
1870 
1871   /* Turn off both OPTION_MASK_ABI_64 and OPTION_MASK_ABI_X32 if
1872      TARGET_64BIT_DEFAULT is true and TARGET_64BIT is false.  */
1873   if (TARGET_64BIT_DEFAULT && !TARGET_64BIT_P (opts->x_ix86_isa_flags))
1874     opts->x_ix86_isa_flags &= ~(OPTION_MASK_ABI_64 | OPTION_MASK_ABI_X32);
1875 #ifdef TARGET_BI_ARCH
1876   else
1877     {
1878 #if TARGET_BI_ARCH == 1
1879       /* When TARGET_BI_ARCH == 1, by default, OPTION_MASK_ABI_64
1880 	 is on and OPTION_MASK_ABI_X32 is off.  We turn off
1881 	 OPTION_MASK_ABI_64 if OPTION_MASK_ABI_X32 is turned on by
1882 	 -mx32.  */
1883       if (TARGET_X32_P (opts->x_ix86_isa_flags))
1884 	opts->x_ix86_isa_flags &= ~OPTION_MASK_ABI_64;
1885 #else
1886       /* When TARGET_BI_ARCH == 2, by default, OPTION_MASK_ABI_X32 is
1887 	 on and OPTION_MASK_ABI_64 is off.  We turn off
1888 	 OPTION_MASK_ABI_X32 if OPTION_MASK_ABI_64 is turned on by
1889 	 -m64 or OPTION_MASK_CODE16 is turned on by -m16.  */
1890       if (TARGET_LP64_P (opts->x_ix86_isa_flags)
1891 	  || TARGET_16BIT_P (opts->x_ix86_isa_flags))
1892 	opts->x_ix86_isa_flags &= ~OPTION_MASK_ABI_X32;
1893 #endif
1894       if (TARGET_64BIT_P (opts->x_ix86_isa_flags)
1895 	  && TARGET_IAMCU_P (opts->x_target_flags))
1896 	sorry ("Intel MCU psABI isn%'t supported in %s mode",
1897 	       TARGET_X32_P (opts->x_ix86_isa_flags) ? "x32" : "64-bit");
1898     }
1899 #endif
1900 
1901   if (TARGET_X32_P (opts->x_ix86_isa_flags))
1902     {
1903       /* Always turn on OPTION_MASK_ISA_64BIT and turn off
1904 	 OPTION_MASK_ABI_64 for TARGET_X32.  */
1905       opts->x_ix86_isa_flags |= OPTION_MASK_ISA_64BIT;
1906       opts->x_ix86_isa_flags &= ~OPTION_MASK_ABI_64;
1907     }
1908   else if (TARGET_16BIT_P (opts->x_ix86_isa_flags))
1909     opts->x_ix86_isa_flags &= ~(OPTION_MASK_ISA_64BIT
1910 				| OPTION_MASK_ABI_X32
1911 				| OPTION_MASK_ABI_64);
1912   else if (TARGET_LP64_P (opts->x_ix86_isa_flags))
1913     {
1914       /* Always turn on OPTION_MASK_ISA_64BIT and turn off
1915 	 OPTION_MASK_ABI_X32 for TARGET_LP64.  */
1916       opts->x_ix86_isa_flags |= OPTION_MASK_ISA_64BIT;
1917       opts->x_ix86_isa_flags &= ~OPTION_MASK_ABI_X32;
1918     }
1919 
1920 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1921   SUBTARGET_OVERRIDE_OPTIONS;
1922 #endif
1923 
1924 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1925   SUBSUBTARGET_OVERRIDE_OPTIONS;
1926 #endif
1927 
1928 #ifdef HAVE_LD_BROKEN_PE_DWARF5
1929   /* If the PE linker has broken DWARF 5 support, make
1930      DWARF 4 the default.  */
1931   if (TARGET_PECOFF)
1932     SET_OPTION_IF_UNSET (opts, opts_set, dwarf_version, 4);
1933 #endif
1934 
1935   /* -fPIC is the default for x86_64.  */
1936   if (TARGET_MACHO && TARGET_64BIT_P (opts->x_ix86_isa_flags))
1937     opts->x_flag_pic = 2;
1938 
1939   /* Need to check -mtune=generic first.  */
1940   if (opts->x_ix86_tune_string)
1941     {
1942       /* As special support for cross compilers we read -mtune=native
1943 	     as -mtune=generic.  With native compilers we won't see the
1944 	     -mtune=native, as it was changed by the driver.  */
1945       if (!strcmp (opts->x_ix86_tune_string, "native"))
1946 	opts->x_ix86_tune_string = "generic";
1947       else if (!strcmp (opts->x_ix86_tune_string, "x86-64"))
1948         warning (OPT_Wdeprecated,
1949 		 main_args_p
1950 		 ? G_("%<-mtune=x86-64%> is deprecated; use %<-mtune=k8%> "
1951 		      "or %<-mtune=generic%> instead as appropriate")
1952 		 : G_("%<target(\"tune=x86-64\")%> is deprecated; use "
1953 		      "%<target(\"tune=k8\")%> or %<target(\"tune=generic\")%>"
1954 		      " instead as appropriate"));
1955     }
1956   else
1957     {
1958       if (opts->x_ix86_arch_string)
1959 	opts->x_ix86_tune_string = opts->x_ix86_arch_string;
1960       if (!opts->x_ix86_tune_string)
1961 	{
1962 	  opts->x_ix86_tune_string = processor_names[TARGET_CPU_DEFAULT];
1963 	  ix86_tune_defaulted = 1;
1964 	}
1965 
1966       /* opts->x_ix86_tune_string is set to opts->x_ix86_arch_string
1967 	 or defaulted.  We need to use a sensible tune option.  */
1968       if (startswith (opts->x_ix86_tune_string, "x86-64")
1969 	  && (opts->x_ix86_tune_string[6] == '\0'
1970 	      || (!strcmp (opts->x_ix86_tune_string + 6, "-v2")
1971 		  || !strcmp (opts->x_ix86_tune_string + 6, "-v3")
1972 		  || !strcmp (opts->x_ix86_tune_string + 6, "-v4"))))
1973 	opts->x_ix86_tune_string = "generic";
1974     }
1975 
1976   if (opts->x_ix86_stringop_alg == rep_prefix_8_byte
1977       && !TARGET_64BIT_P (opts->x_ix86_isa_flags))
1978     {
1979       /* rep; movq isn't available in 32-bit code.  */
1980       error ("%<-mstringop-strategy=rep_8byte%> not supported for 32-bit code");
1981       opts->x_ix86_stringop_alg = no_stringop;
1982     }
1983 
1984   if (TARGET_UINTR && !TARGET_64BIT)
1985     error ("%<-muintr%> not supported for 32-bit code");
1986 
1987   if (!opts->x_ix86_arch_string)
1988     opts->x_ix86_arch_string
1989       = TARGET_64BIT_P (opts->x_ix86_isa_flags)
1990 	? "x86-64" : SUBTARGET32_DEFAULT_CPU;
1991   else
1992     ix86_arch_specified = 1;
1993 
1994   if (opts_set->x_ix86_pmode)
1995     {
1996       if ((TARGET_LP64_P (opts->x_ix86_isa_flags)
1997 	   && opts->x_ix86_pmode == PMODE_SI)
1998 	  || (!TARGET_64BIT_P (opts->x_ix86_isa_flags)
1999 	       && opts->x_ix86_pmode == PMODE_DI))
2000 	error ("address mode %qs not supported in the %s bit mode",
2001 	       TARGET_64BIT_P (opts->x_ix86_isa_flags) ? "short" : "long",
2002 	       TARGET_64BIT_P (opts->x_ix86_isa_flags) ? "64" : "32");
2003     }
2004   else
2005     opts->x_ix86_pmode = TARGET_LP64_P (opts->x_ix86_isa_flags)
2006 			 ? PMODE_DI : PMODE_SI;
2007 
2008   SET_OPTION_IF_UNSET (opts, opts_set, ix86_abi, DEFAULT_ABI);
2009 
2010   if (opts->x_ix86_abi == MS_ABI && TARGET_X32_P (opts->x_ix86_isa_flags))
2011     error ("%<-mabi=ms%> not supported with X32 ABI");
2012   gcc_assert (opts->x_ix86_abi == SYSV_ABI || opts->x_ix86_abi == MS_ABI);
2013 
2014   const char *abi_name = opts->x_ix86_abi == MS_ABI ? "ms" : "sysv";
2015   if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS)
2016       && opts->x_ix86_abi != DEFAULT_ABI)
2017     error ("%<-mabi=%s%> not supported with %<-fsanitize=address%>", abi_name);
2018   if ((opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS)
2019       && opts->x_ix86_abi != DEFAULT_ABI)
2020     error ("%<-mabi=%s%> not supported with %<-fsanitize=kernel-address%>",
2021 	   abi_name);
2022   if ((opts->x_flag_sanitize & SANITIZE_THREAD)
2023       && opts->x_ix86_abi != DEFAULT_ABI)
2024     error ("%<-mabi=%s%> not supported with %<-fsanitize=thread%>", abi_name);
2025 
2026   /* For targets using ms ABI enable ms-extensions, if not
2027      explicit turned off.  For non-ms ABI we turn off this
2028      option.  */
2029   SET_OPTION_IF_UNSET (opts, opts_set, flag_ms_extensions,
2030 		       (MS_ABI == DEFAULT_ABI));
2031 
2032   if (opts_set->x_ix86_cmodel)
2033     {
2034       switch (opts->x_ix86_cmodel)
2035 	{
2036 	case CM_SMALL:
2037 	case CM_SMALL_PIC:
2038 	  if (opts->x_flag_pic)
2039 	    opts->x_ix86_cmodel = CM_SMALL_PIC;
2040 	  if (!TARGET_64BIT_P (opts->x_ix86_isa_flags))
2041 	    error ("code model %qs not supported in the %s bit mode",
2042 		   "small", "32");
2043 	  break;
2044 
2045 	case CM_MEDIUM:
2046 	case CM_MEDIUM_PIC:
2047 	  if (opts->x_flag_pic)
2048 	    opts->x_ix86_cmodel = CM_MEDIUM_PIC;
2049 	  if (!TARGET_64BIT_P (opts->x_ix86_isa_flags))
2050 	    error ("code model %qs not supported in the %s bit mode",
2051 		   "medium", "32");
2052 	  else if (TARGET_X32_P (opts->x_ix86_isa_flags))
2053 	    error ("code model %qs not supported in x32 mode",
2054 		   "medium");
2055 	  break;
2056 
2057 	case CM_LARGE:
2058 	case CM_LARGE_PIC:
2059 	  if (opts->x_flag_pic)
2060 	    opts->x_ix86_cmodel = CM_LARGE_PIC;
2061 	  if (!TARGET_64BIT_P (opts->x_ix86_isa_flags))
2062 	    error ("code model %qs not supported in the %s bit mode",
2063 		   "large", "32");
2064 	  else if (TARGET_X32_P (opts->x_ix86_isa_flags))
2065 	    error ("code model %qs not supported in x32 mode",
2066 		   "large");
2067 	  break;
2068 
2069 	case CM_32:
2070 	  if (opts->x_flag_pic)
2071 	    error ("code model %s does not support PIC mode", "32");
2072 	  if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
2073 	    error ("code model %qs not supported in the %s bit mode",
2074 		   "32", "64");
2075 	  break;
2076 
2077 	case CM_KERNEL:
2078 	  if (opts->x_flag_pic)
2079 	    {
2080 	      error ("code model %s does not support PIC mode", "kernel");
2081 	      opts->x_ix86_cmodel = CM_32;
2082 	    }
2083 	  if (!TARGET_64BIT_P (opts->x_ix86_isa_flags))
2084 	    error ("code model %qs not supported in the %s bit mode",
2085 		   "kernel", "32");
2086 	  break;
2087 
2088 	default:
2089 	  gcc_unreachable ();
2090 	}
2091     }
2092   else
2093     {
2094       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
2095 	 use of rip-relative addressing.  This eliminates fixups that
2096 	 would otherwise be needed if this object is to be placed in a
2097 	 DLL, and is essentially just as efficient as direct addressing.  */
2098       if (TARGET_64BIT_P (opts->x_ix86_isa_flags)
2099 	  && (TARGET_RDOS || TARGET_PECOFF))
2100 	opts->x_ix86_cmodel = CM_MEDIUM_PIC, opts->x_flag_pic = 1;
2101       else if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
2102 	opts->x_ix86_cmodel = opts->x_flag_pic ? CM_SMALL_PIC : CM_SMALL;
2103       else
2104 	opts->x_ix86_cmodel = CM_32;
2105     }
2106   if (TARGET_MACHO && opts->x_ix86_asm_dialect == ASM_INTEL)
2107     {
2108       error ("%<-masm=intel%> not supported in this configuration");
2109       opts->x_ix86_asm_dialect = ASM_ATT;
2110     }
2111   if ((TARGET_64BIT_P (opts->x_ix86_isa_flags) != 0)
2112       != ((opts->x_ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
2113     sorry ("%i-bit mode not compiled in",
2114 	   (opts->x_ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
2115 
2116   /* Last processor_alias_table must point to "generic" entry.  */
2117   gcc_checking_assert (strcmp (processor_alias_table[pta_size - 1].name,
2118 			       "generic") == 0);
2119   for (i = 0; i < pta_size; i++)
2120     if (! strcmp (opts->x_ix86_arch_string, processor_alias_table[i].name))
2121       {
2122 	if (!strcmp (opts->x_ix86_arch_string, "generic"))
2123 	  {
2124 	    error (main_args_p
2125 		   ? G_("%<generic%> CPU can be used only for %<-mtune=%> "
2126 			"switch")
2127 		   : G_("%<generic%> CPU can be used only for "
2128 			"%<target(\"tune=\")%> attribute"));
2129 	    return false;
2130 	  }
2131 	else if (!strcmp (opts->x_ix86_arch_string, "intel"))
2132 	  {
2133 	    error (main_args_p
2134 		   ? G_("%<intel%> CPU can be used only for %<-mtune=%> "
2135 			"switch")
2136 		   : G_("%<intel%> CPU can be used only for "
2137 			"%<target(\"tune=\")%> attribute"));
2138 	    return false;
2139 	  }
2140 
2141 	if (TARGET_64BIT_P (opts->x_ix86_isa_flags)
2142 	    && !((processor_alias_table[i].flags & PTA_64BIT) != 0))
2143 	  {
2144 	    error ("CPU you selected does not support x86-64 "
2145 		   "instruction set");
2146 	    return false;
2147 	  }
2148 
2149 	ix86_schedule = processor_alias_table[i].schedule;
2150 	ix86_arch = processor_alias_table[i].processor;
2151 
2152 	/* Default cpu tuning to the architecture, unless the table
2153 	   entry requests not to do this.  Used by the x86-64 psABI
2154 	   micro-architecture levels.  */
2155 	if ((processor_alias_table[i].flags & PTA_NO_TUNE) == 0)
2156 	  ix86_tune = ix86_arch;
2157 	else
2158 	  ix86_tune = PROCESSOR_GENERIC;
2159 
2160 	/* Enable PTA flags that are enabled by default by a -march option.  */
2161 #define TARGET_EXPLICIT_NO_SAHF_P(opts) (false)
2162 #define SET_TARGET_NO_SAHF(opts) {}
2163 #define TARGET_EXPLICIT_PREFETCH_SSE_P(opts) (false)
2164 #define SET_TARGET_PREFETCH_SSE(opts) {}
2165 #define TARGET_EXPLICIT_NO_TUNE_P(opts) (false)
2166 #define SET_TARGET_NO_TUNE(opts) {}
2167 #define TARGET_EXPLICIT_NO_80387_P(opts) (false)
2168 #define SET_TARGET_NO_80387(opts) {}
2169 
2170 #define DEF_PTA(NAME) \
2171 	if (((processor_alias_table[i].flags & PTA_ ## NAME) != 0) \
2172 	    && PTA_ ## NAME != PTA_64BIT \
2173 	    && (TARGET_64BIT || PTA_ ## NAME != PTA_UINTR) \
2174 	    && !TARGET_EXPLICIT_ ## NAME ## _P (opts)) \
2175 	  SET_TARGET_ ## NAME (opts);
2176 #include "i386-isa.def"
2177 #undef DEF_PTA
2178 
2179 
2180        if (!(TARGET_64BIT_P (opts->x_ix86_isa_flags)
2181 	     && ((processor_alias_table[i].flags & PTA_NO_SAHF) != 0))
2182 	   && !TARGET_EXPLICIT_SAHF_P (opts))
2183 	    SET_TARGET_SAHF (opts);
2184 
2185 	if (((processor_alias_table[i].flags & PTA_ABM) != 0)
2186 	    && !TARGET_EXPLICIT_ABM_P (opts))
2187 	  {
2188 	    if (!TARGET_EXPLICIT_LZCNT_P (opts))
2189 	      SET_TARGET_LZCNT (opts);
2190 	    if (!TARGET_EXPLICIT_POPCNT_P (opts))
2191 	      SET_TARGET_POPCNT (opts);
2192 	  }
2193 
2194 	if ((processor_alias_table[i].flags
2195 	   & (PTA_PREFETCH_SSE | PTA_SSE)) != 0)
2196 	  ix86_prefetch_sse = true;
2197 
2198 	/* Don't enable x87 instructions if only general registers are
2199 	   allowed by target("general-regs-only") function attribute or
2200 	   -mgeneral-regs-only.  */
2201 	if (!(opts->x_ix86_target_flags & OPTION_MASK_GENERAL_REGS_ONLY)
2202 	    && !(opts_set->x_target_flags & MASK_80387))
2203 	  {
2204 	    if (((processor_alias_table[i].flags & PTA_NO_80387) != 0))
2205 	      opts->x_target_flags &= ~MASK_80387;
2206 	    else
2207 	      opts->x_target_flags |= MASK_80387;
2208 	  }
2209 	break;
2210       }
2211 
2212   if (i == pta_size)
2213     {
2214       error (main_args_p
2215 	     ? G_("bad value %qs for %<-march=%> switch")
2216 	     : G_("bad value %qs for %<target(\"arch=\")%> attribute"),
2217 	     opts->x_ix86_arch_string);
2218 
2219       auto_vec <const char *> candidates;
2220       for (i = 0; i < pta_size; i++)
2221 	if (strcmp (processor_alias_table[i].name, "generic")
2222 	    && strcmp (processor_alias_table[i].name, "intel")
2223 	    && (!TARGET_64BIT_P (opts->x_ix86_isa_flags)
2224 		|| ((processor_alias_table[i].flags & PTA_64BIT) != 0)))
2225 	  candidates.safe_push (processor_alias_table[i].name);
2226 
2227 #ifdef HAVE_LOCAL_CPU_DETECT
2228       /* Add also "native" as possible value.  */
2229       candidates.safe_push ("native");
2230 #endif
2231 
2232       char *s;
2233       const char *hint
2234 	= candidates_list_and_hint (opts->x_ix86_arch_string, s, candidates);
2235       if (hint)
2236 	inform (input_location,
2237 		main_args_p
2238 		? G_("valid arguments to %<-march=%> switch are: "
2239 		     "%s; did you mean %qs?")
2240 		: G_("valid arguments to %<target(\"arch=\")%> attribute are: "
2241 		     "%s; did you mean %qs?"), s, hint);
2242       else
2243 	inform (input_location,
2244 		main_args_p
2245 		? G_("valid arguments to %<-march=%> switch are: %s")
2246 		: G_("valid arguments to %<target(\"arch=\")%> attribute "
2247 		     "are: %s"), s);
2248       XDELETEVEC (s);
2249     }
2250 
2251   ix86_arch_mask = HOST_WIDE_INT_1U << ix86_arch;
2252   for (i = 0; i < X86_ARCH_LAST; ++i)
2253     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
2254 
2255   for (i = 0; i < pta_size; i++)
2256     if (! strcmp (opts->x_ix86_tune_string, processor_alias_table[i].name)
2257 	&& (processor_alias_table[i].flags & PTA_NO_TUNE) == 0)
2258       {
2259 	ix86_schedule = processor_alias_table[i].schedule;
2260 	ix86_tune = processor_alias_table[i].processor;
2261 	if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
2262 	  {
2263 	    if (!((processor_alias_table[i].flags & PTA_64BIT) != 0))
2264 	      {
2265 		if (ix86_tune_defaulted)
2266 		  {
2267 		    opts->x_ix86_tune_string = "x86-64";
2268 		    for (i = 0; i < pta_size; i++)
2269 		      if (! strcmp (opts->x_ix86_tune_string,
2270 				    processor_alias_table[i].name))
2271 			break;
2272 		    ix86_schedule = processor_alias_table[i].schedule;
2273 		    ix86_tune = processor_alias_table[i].processor;
2274 		  }
2275 		else
2276 		  error ("CPU you selected does not support x86-64 "
2277 			 "instruction set");
2278 	      }
2279 	  }
2280 	/* Intel CPUs have always interpreted SSE prefetch instructions as
2281 	   NOPs; so, we can enable SSE prefetch instructions even when
2282 	   -mtune (rather than -march) points us to a processor that has them.
2283 	   However, the VIA C3 gives a SIGILL, so we only do that for i686 and
2284 	   higher processors.  */
2285 	if (TARGET_CMOV
2286 	    && ((processor_alias_table[i].flags
2287 	      & (PTA_PREFETCH_SSE | PTA_SSE)) != 0))
2288 	  ix86_prefetch_sse = true;
2289 	break;
2290       }
2291 
2292   if (ix86_tune_specified && i == pta_size)
2293     {
2294       error (main_args_p
2295 	     ? G_("bad value %qs for %<-mtune=%> switch")
2296 	     : G_("bad value %qs for %<target(\"tune=\")%> attribute"),
2297 	     opts->x_ix86_tune_string);
2298 
2299       auto_vec <const char *> candidates;
2300       for (i = 0; i < pta_size; i++)
2301 	if ((!TARGET_64BIT_P (opts->x_ix86_isa_flags)
2302 	     || ((processor_alias_table[i].flags & PTA_64BIT) != 0))
2303 	    && (processor_alias_table[i].flags & PTA_NO_TUNE) == 0)
2304 	  candidates.safe_push (processor_alias_table[i].name);
2305 
2306 #ifdef HAVE_LOCAL_CPU_DETECT
2307       /* Add also "native" as possible value.  */
2308       candidates.safe_push ("native");
2309 #endif
2310 
2311       char *s;
2312       const char *hint
2313 	= candidates_list_and_hint (opts->x_ix86_tune_string, s, candidates);
2314       if (hint)
2315 	inform (input_location,
2316 		main_args_p
2317 		? G_("valid arguments to %<-mtune=%> switch are: "
2318 		     "%s; did you mean %qs?")
2319 		: G_("valid arguments to %<target(\"tune=\")%> attribute are: "
2320 		     "%s; did you mean %qs?"), s, hint);
2321       else
2322 	inform (input_location,
2323 		main_args_p
2324 		? G_("valid arguments to %<-mtune=%> switch are: %s")
2325 		: G_("valid arguments to %<target(\"tune=\")%> attribute "
2326 		     "are: %s"), s);
2327       XDELETEVEC (s);
2328     }
2329 
2330   set_ix86_tune_features (opts, ix86_tune, opts->x_ix86_dump_tunes);
2331 
2332   ix86_recompute_optlev_based_flags (opts, opts_set);
2333 
2334   ix86_tune_cost = processor_cost_table[ix86_tune];
2335   /* TODO: ix86_cost should be chosen at instruction or function granuality
2336      so for cold code we use size_cost even in !optimize_size compilation.  */
2337   if (opts->x_optimize_size)
2338     ix86_cost = &ix86_size_cost;
2339   else
2340     ix86_cost = ix86_tune_cost;
2341 
2342   /* Arrange to set up i386_stack_locals for all functions.  */
2343   init_machine_status = ix86_init_machine_status;
2344 
2345   /* Validate -mregparm= value.  */
2346   if (opts_set->x_ix86_regparm)
2347     {
2348       if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
2349 	warning (0, "%<-mregparm%> is ignored in 64-bit mode");
2350       else if (TARGET_IAMCU_P (opts->x_target_flags))
2351 	warning (0, "%<-mregparm%> is ignored for Intel MCU psABI");
2352       if (opts->x_ix86_regparm > REGPARM_MAX)
2353 	{
2354 	  error ("%<-mregparm=%d%> is not between 0 and %d",
2355 		 opts->x_ix86_regparm, REGPARM_MAX);
2356 	  opts->x_ix86_regparm = 0;
2357 	}
2358     }
2359   if (TARGET_IAMCU_P (opts->x_target_flags)
2360       || TARGET_64BIT_P (opts->x_ix86_isa_flags))
2361     opts->x_ix86_regparm = REGPARM_MAX;
2362 
2363   /* Default align_* from the processor table.  */
2364   ix86_default_align (opts);
2365 
2366   /* Provide default for -mbranch-cost= value.  */
2367   SET_OPTION_IF_UNSET (opts, opts_set, ix86_branch_cost,
2368 		       ix86_tune_cost->branch_cost);
2369 
2370   if (TARGET_64BIT_P (opts->x_ix86_isa_flags))
2371     {
2372       opts->x_target_flags
2373 	|= TARGET_SUBTARGET64_DEFAULT & ~opts_set->x_target_flags;
2374 
2375       if (!ix86_arch_specified)
2376 	opts->x_ix86_isa_flags
2377 	  |= TARGET_SUBTARGET64_ISA_DEFAULT & ~opts->x_ix86_isa_flags_explicit;
2378 
2379       if (!TARGET_128BIT_LONG_DOUBLE_P (opts->x_target_flags))
2380 	error ("%<-m96bit-long-double%> is not compatible with this target");
2381 
2382       if (TARGET_RTD_P (opts->x_target_flags))
2383 	warning (0,
2384 		 main_args_p
2385 		 ? G_("%<-mrtd%> is ignored in 64bit mode")
2386 		 : G_("%<target(\"rtd\")%> is ignored in 64bit mode"));
2387     }
2388   else
2389     {
2390       opts->x_target_flags
2391 	|= TARGET_SUBTARGET32_DEFAULT & ~opts_set->x_target_flags;
2392 
2393       if (!ix86_arch_specified)
2394         opts->x_ix86_isa_flags
2395 	  |= TARGET_SUBTARGET32_ISA_DEFAULT & ~opts->x_ix86_isa_flags_explicit;
2396 
2397       /* i386 ABI does not specify red zone.  It still makes sense to use it
2398          when programmer takes care to stack from being destroyed.  */
2399       if (!(opts_set->x_target_flags & MASK_NO_RED_ZONE))
2400         opts->x_target_flags |= MASK_NO_RED_ZONE;
2401     }
2402 
2403   /* Keep nonleaf frame pointers.  */
2404   if (opts->x_flag_omit_frame_pointer)
2405     opts->x_target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2406   else if (TARGET_OMIT_LEAF_FRAME_POINTER_P (opts->x_target_flags))
2407     opts->x_flag_omit_frame_pointer = 1;
2408 
2409   /* If we're doing fast math, we don't care about comparison order
2410      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2411   if (opts->x_flag_finite_math_only)
2412     opts->x_target_flags &= ~MASK_IEEE_FP;
2413 
2414   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2415      since the insns won't need emulation.  */
2416   if (ix86_tune_features [X86_TUNE_ALWAYS_FANCY_MATH_387])
2417     opts->x_target_flags &= ~MASK_NO_FANCY_MATH_387;
2418 
2419   /* Likewise, if the target doesn't have a 387, or we've specified
2420      software floating point, don't use 387 inline intrinsics.  */
2421   if (!TARGET_80387_P (opts->x_target_flags))
2422     opts->x_target_flags |= MASK_NO_FANCY_MATH_387;
2423 
2424   /* Turn on MMX builtins for -msse.  */
2425   if (TARGET_SSE_P (opts->x_ix86_isa_flags))
2426     opts->x_ix86_isa_flags
2427       |= OPTION_MASK_ISA_MMX & ~opts->x_ix86_isa_flags_explicit;
2428 
2429   /* Enable SSE prefetch.  */
2430   if (TARGET_SSE_P (opts->x_ix86_isa_flags)
2431       || (TARGET_PRFCHW_P (opts->x_ix86_isa_flags)
2432 	  && !TARGET_3DNOW_P (opts->x_ix86_isa_flags))
2433       || TARGET_PREFETCHWT1_P (opts->x_ix86_isa_flags))
2434     ix86_prefetch_sse = true;
2435 
2436   /* Enable mwait/monitor instructions for -msse3.  */
2437   if (TARGET_SSE3_P (opts->x_ix86_isa_flags))
2438     opts->x_ix86_isa_flags2
2439       |= OPTION_MASK_ISA2_MWAIT & ~opts->x_ix86_isa_flags2_explicit;
2440 
2441   /* Enable popcnt instruction for -msse4.2 or -mabm.  */
2442   if (TARGET_SSE4_2_P (opts->x_ix86_isa_flags)
2443       || TARGET_ABM_P (opts->x_ix86_isa_flags))
2444     opts->x_ix86_isa_flags
2445       |= OPTION_MASK_ISA_POPCNT & ~opts->x_ix86_isa_flags_explicit;
2446 
2447   /* Enable crc32 instruction for -msse4.2.  */
2448   if (TARGET_SSE4_2_P (opts->x_ix86_isa_flags))
2449     opts->x_ix86_isa_flags
2450       |= OPTION_MASK_ISA_CRC32 & ~opts->x_ix86_isa_flags_explicit;
2451 
2452   /* Enable lzcnt instruction for -mabm.  */
2453   if (TARGET_ABM_P(opts->x_ix86_isa_flags))
2454     opts->x_ix86_isa_flags
2455       |= OPTION_MASK_ISA_LZCNT & ~opts->x_ix86_isa_flags_explicit;
2456 
2457   /* Disable BMI, BMI2 and TBM instructions for -m16.  */
2458   if (TARGET_16BIT_P(opts->x_ix86_isa_flags))
2459     opts->x_ix86_isa_flags
2460       &= ~((OPTION_MASK_ISA_BMI | OPTION_MASK_ISA_BMI2 | OPTION_MASK_ISA_TBM)
2461 	   & ~opts->x_ix86_isa_flags_explicit);
2462 
2463   /* Validate -mpreferred-stack-boundary= value or default it to
2464      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
2465   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
2466   if (opts_set->x_ix86_preferred_stack_boundary_arg)
2467     {
2468       int min = TARGET_64BIT_P (opts->x_ix86_isa_flags)? 3 : 2;
2469       int max = TARGET_SEH ? 4 : 12;
2470 
2471       if (opts->x_ix86_preferred_stack_boundary_arg < min
2472 	  || opts->x_ix86_preferred_stack_boundary_arg > max)
2473 	{
2474 	  if (min == max)
2475 	    error ("%<-mpreferred-stack-boundary%> is not supported "
2476 		   "for this target");
2477 	  else
2478 	    error ("%<-mpreferred-stack-boundary=%d%> is not between %d and %d",
2479 		   opts->x_ix86_preferred_stack_boundary_arg, min, max);
2480 	}
2481       else
2482 	ix86_preferred_stack_boundary
2483 	  = (1 << opts->x_ix86_preferred_stack_boundary_arg) * BITS_PER_UNIT;
2484     }
2485 
2486   /* Set the default value for -mstackrealign.  */
2487   SET_OPTION_IF_UNSET (opts, opts_set, ix86_force_align_arg_pointer,
2488 		       STACK_REALIGN_DEFAULT);
2489 
2490   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
2491 
2492   /* Validate -mincoming-stack-boundary= value or default it to
2493      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
2494   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
2495   if (opts_set->x_ix86_incoming_stack_boundary_arg)
2496     {
2497       int min = TARGET_64BIT_P (opts->x_ix86_isa_flags) ? 3 : 2;
2498 
2499       if (opts->x_ix86_incoming_stack_boundary_arg < min
2500 	  || opts->x_ix86_incoming_stack_boundary_arg > 12)
2501 	error ("%<-mincoming-stack-boundary=%d%> is not between %d and 12",
2502 	       opts->x_ix86_incoming_stack_boundary_arg, min);
2503       else
2504 	{
2505 	  ix86_user_incoming_stack_boundary
2506 	    = (1 << opts->x_ix86_incoming_stack_boundary_arg) * BITS_PER_UNIT;
2507 	  ix86_incoming_stack_boundary
2508 	    = ix86_user_incoming_stack_boundary;
2509 	}
2510     }
2511 
2512 #ifndef NO_PROFILE_COUNTERS
2513   if (flag_nop_mcount)
2514     error ("%<-mnop-mcount%> is not compatible with this target");
2515 #endif
2516   if (flag_nop_mcount && flag_pic)
2517     error ("%<-mnop-mcount%> is not implemented for %<-fPIC%>");
2518 
2519   /* Accept -msseregparm only if at least SSE support is enabled.  */
2520   if (TARGET_SSEREGPARM_P (opts->x_target_flags)
2521       && ! TARGET_SSE_P (opts->x_ix86_isa_flags))
2522     error (main_args_p
2523 	   ? G_("%<-msseregparm%> used without SSE enabled")
2524 	   : G_("%<target(\"sseregparm\")%> used without SSE enabled"));
2525 
2526   if (opts_set->x_ix86_fpmath)
2527     {
2528       if (opts->x_ix86_fpmath & FPMATH_SSE)
2529 	{
2530 	  if (!TARGET_SSE_P (opts->x_ix86_isa_flags))
2531 	    {
2532 	      if (TARGET_80387_P (opts->x_target_flags))
2533 		{
2534 		  warning (0, "SSE instruction set disabled, using 387 arithmetics");
2535 		  opts->x_ix86_fpmath = FPMATH_387;
2536 		}
2537 	    }
2538 	  else if ((opts->x_ix86_fpmath & FPMATH_387)
2539 		   && !TARGET_80387_P (opts->x_target_flags))
2540 	    {
2541 	      warning (0, "387 instruction set disabled, using SSE arithmetics");
2542 	      opts->x_ix86_fpmath = FPMATH_SSE;
2543 	    }
2544 	}
2545     }
2546   /* For all chips supporting SSE2, -mfpmath=sse performs better than
2547      fpmath=387.  The second is however default at many targets since the
2548      extra 80bit precision of temporaries is considered to be part of ABI.
2549      Overwrite the default at least for -ffast-math.
2550      TODO: -mfpmath=both seems to produce same performing code with bit
2551      smaller binaries.  It is however not clear if register allocation is
2552      ready for this setting.
2553      Also -mfpmath=387 is overall a lot more compact (bout 4-5%) than SSE
2554      codegen.  We may switch to 387 with -ffast-math for size optimized
2555      functions. */
2556   else if (fast_math_flags_set_p (&global_options)
2557 	   && TARGET_SSE2_P (opts->x_ix86_isa_flags))
2558     opts->x_ix86_fpmath = FPMATH_SSE;
2559   else
2560     opts->x_ix86_fpmath = TARGET_FPMATH_DEFAULT_P (opts->x_ix86_isa_flags);
2561 
2562   /* Use external vectorized library in vectorizing intrinsics.  */
2563   if (opts_set->x_ix86_veclibabi_type)
2564     switch (opts->x_ix86_veclibabi_type)
2565       {
2566       case ix86_veclibabi_type_svml:
2567 	ix86_veclib_handler = &ix86_veclibabi_svml;
2568 	break;
2569 
2570       case ix86_veclibabi_type_acml:
2571 	ix86_veclib_handler = &ix86_veclibabi_acml;
2572 	break;
2573 
2574       default:
2575 	gcc_unreachable ();
2576       }
2577 
2578   if (ix86_tune_features [X86_TUNE_ACCUMULATE_OUTGOING_ARGS]
2579       && !(opts_set->x_target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2580     opts->x_target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2581 
2582   /* If stack probes are required, the space used for large function
2583      arguments on the stack must also be probed, so enable
2584      -maccumulate-outgoing-args so this happens in the prologue.  */
2585   if (TARGET_STACK_PROBE_P (opts->x_target_flags)
2586       && !(opts->x_target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2587     {
2588       if (opts_set->x_target_flags & MASK_ACCUMULATE_OUTGOING_ARGS)
2589 	warning (0,
2590 		 main_args_p
2591 		 ? G_("stack probing requires %<-maccumulate-outgoing-args%> "
2592 		      "for correctness")
2593 		 : G_("stack probing requires "
2594 		      "%<target(\"accumulate-outgoing-args\")%> for "
2595 		      "correctness"));
2596       opts->x_target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2597     }
2598 
2599   /* Stack realignment without -maccumulate-outgoing-args requires %ebp,
2600      so enable -maccumulate-outgoing-args when %ebp is fixed.  */
2601   if (fixed_regs[BP_REG]
2602       && !(opts->x_target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2603     {
2604       if (opts_set->x_target_flags & MASK_ACCUMULATE_OUTGOING_ARGS)
2605 	warning (0,
2606 		 main_args_p
2607 		 ? G_("fixed ebp register requires "
2608 		      "%<-maccumulate-outgoing-args%>")
2609 		 : G_("fixed ebp register requires "
2610 		      "%<target(\"accumulate-outgoing-args\")%>"));
2611       opts->x_target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2612     }
2613 
2614   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2615   {
2616     char *p;
2617     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2618     p = strchr (internal_label_prefix, 'X');
2619     internal_label_prefix_len = p - internal_label_prefix;
2620     *p = '\0';
2621   }
2622 
2623   /* When scheduling description is not available, disable scheduler pass
2624      so it won't slow down the compilation and make x87 code slower.  */
2625   if (!TARGET_SCHEDULE)
2626     opts->x_flag_schedule_insns_after_reload = opts->x_flag_schedule_insns = 0;
2627 
2628   SET_OPTION_IF_UNSET (opts, opts_set, param_simultaneous_prefetches,
2629 		       ix86_tune_cost->simultaneous_prefetches);
2630   SET_OPTION_IF_UNSET (opts, opts_set, param_l1_cache_line_size,
2631 		       ix86_tune_cost->prefetch_block);
2632   SET_OPTION_IF_UNSET (opts, opts_set, param_l1_cache_size,
2633 		       ix86_tune_cost->l1_cache_size);
2634   SET_OPTION_IF_UNSET (opts, opts_set, param_l2_cache_size,
2635 		       ix86_tune_cost->l2_cache_size);
2636 
2637   /* 64B is the accepted value for these for all x86.  */
2638   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2639 		       param_destruct_interfere_size, 64);
2640   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2641 		       param_construct_interfere_size, 64);
2642 
2643   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
2644   if (opts->x_flag_prefetch_loop_arrays < 0
2645       && HAVE_prefetch
2646       && (opts->x_optimize >= 3 || opts->x_flag_profile_use)
2647       && !opts->x_optimize_size
2648       && TARGET_SOFTWARE_PREFETCHING_BENEFICIAL)
2649     opts->x_flag_prefetch_loop_arrays = 1;
2650 
2651   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
2652      can be opts->x_optimized to ap = __builtin_next_arg (0).  */
2653   if (!TARGET_64BIT_P (opts->x_ix86_isa_flags) && !opts->x_flag_split_stack)
2654     targetm.expand_builtin_va_start = NULL;
2655 
2656 #ifdef USE_IX86_CLD
2657   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
2658   if (!TARGET_64BIT_P (opts->x_ix86_isa_flags))
2659     opts->x_target_flags |= MASK_CLD & ~opts_set->x_target_flags;
2660 #endif
2661 
2662   /* Set the default value for -mfentry.  */
2663   if (!opts_set->x_flag_fentry)
2664     opts->x_flag_fentry = TARGET_SEH;
2665   else
2666     {
2667       if (!TARGET_64BIT_P (opts->x_ix86_isa_flags) && opts->x_flag_pic
2668 	  && opts->x_flag_fentry)
2669 	sorry ("%<-mfentry%> isn%'t supported for 32-bit in combination "
2670 	       "with %<-fpic%>");
2671       else if (TARGET_SEH && !opts->x_flag_fentry)
2672 	sorry ("%<-mno-fentry%> isn%'t compatible with SEH");
2673     }
2674 
2675   if (TARGET_SEH && TARGET_CALL_MS2SYSV_XLOGUES)
2676     sorry ("%<-mcall-ms2sysv-xlogues%> isn%'t currently supported with SEH");
2677 
2678   if (!(opts_set->x_target_flags & MASK_VZEROUPPER)
2679       && TARGET_EMIT_VZEROUPPER)
2680     opts->x_target_flags |= MASK_VZEROUPPER;
2681   if (!(opts_set->x_target_flags & MASK_STV))
2682     opts->x_target_flags |= MASK_STV;
2683   /* Disable STV if -mpreferred-stack-boundary={2,3} or
2684      -mincoming-stack-boundary={2,3} or -mstackrealign - the needed
2685      stack realignment will be extra cost the pass doesn't take into
2686      account and the pass can't realign the stack.  */
2687   if (ix86_preferred_stack_boundary < 128
2688       || ix86_incoming_stack_boundary < 128
2689       || opts->x_ix86_force_align_arg_pointer)
2690     opts->x_target_flags &= ~MASK_STV;
2691   if (!ix86_tune_features[X86_TUNE_AVX256_UNALIGNED_LOAD_OPTIMAL]
2692       && !(opts_set->x_target_flags & MASK_AVX256_SPLIT_UNALIGNED_LOAD))
2693     opts->x_target_flags |= MASK_AVX256_SPLIT_UNALIGNED_LOAD;
2694   else if (!main_args_p
2695 	   && ix86_tune_features[X86_TUNE_AVX256_UNALIGNED_LOAD_OPTIMAL])
2696     opts->x_target_flags &= ~MASK_AVX256_SPLIT_UNALIGNED_LOAD;
2697 
2698   if (!ix86_tune_features[X86_TUNE_AVX256_UNALIGNED_STORE_OPTIMAL]
2699       && !(opts_set->x_target_flags & MASK_AVX256_SPLIT_UNALIGNED_STORE))
2700     opts->x_target_flags |= MASK_AVX256_SPLIT_UNALIGNED_STORE;
2701   else if (!main_args_p
2702 	   && ix86_tune_features[X86_TUNE_AVX256_UNALIGNED_STORE_OPTIMAL])
2703     opts->x_target_flags &= ~MASK_AVX256_SPLIT_UNALIGNED_STORE;
2704 
2705   /* Enable 128-bit AVX instruction generation
2706      for the auto-vectorizer.  */
2707   if (ix86_tune_features[X86_TUNE_AVX128_OPTIMAL]
2708       && (opts_set->x_prefer_vector_width_type == PVW_NONE))
2709     opts->x_prefer_vector_width_type = PVW_AVX128;
2710 
2711   /* Use 256-bit AVX instruction generation
2712      in the auto-vectorizer.  */
2713   if (ix86_tune_features[X86_TUNE_AVX256_OPTIMAL]
2714       && (opts_set->x_prefer_vector_width_type == PVW_NONE))
2715     opts->x_prefer_vector_width_type = PVW_AVX256;
2716 
2717   if (opts_set->x_ix86_move_max == PVW_NONE)
2718     {
2719       /* Set the maximum number of bits can be moved from memory to
2720 	 memory efficiently.  */
2721       if (ix86_tune_features[X86_TUNE_AVX512_MOVE_BY_PIECES])
2722 	opts->x_ix86_move_max = PVW_AVX512;
2723       else if (ix86_tune_features[X86_TUNE_AVX256_MOVE_BY_PIECES])
2724 	opts->x_ix86_move_max = PVW_AVX256;
2725       else
2726 	{
2727 	  opts->x_ix86_move_max = opts->x_prefer_vector_width_type;
2728 	  if (opts_set->x_ix86_move_max == PVW_NONE)
2729 	    {
2730 	      if (TARGET_AVX512F_P (opts->x_ix86_isa_flags))
2731 		opts->x_ix86_move_max = PVW_AVX512;
2732 	      else
2733 		opts->x_ix86_move_max = PVW_AVX128;
2734 	    }
2735 	}
2736     }
2737 
2738   if (opts_set->x_ix86_store_max == PVW_NONE)
2739     {
2740       /* Set the maximum number of bits can be stored to memory
2741 	 efficiently.  */
2742       if (ix86_tune_features[X86_TUNE_AVX512_STORE_BY_PIECES])
2743 	opts->x_ix86_store_max = PVW_AVX512;
2744       else if (ix86_tune_features[X86_TUNE_AVX256_STORE_BY_PIECES])
2745 	opts->x_ix86_store_max = PVW_AVX256;
2746       else
2747 	{
2748 	  opts->x_ix86_store_max = opts->x_prefer_vector_width_type;
2749 	  if (opts_set->x_ix86_store_max == PVW_NONE)
2750 	    {
2751 	      if (TARGET_AVX512F_P (opts->x_ix86_isa_flags))
2752 		opts->x_ix86_store_max = PVW_AVX512;
2753 	      else
2754 		opts->x_ix86_store_max = PVW_AVX128;
2755 	    }
2756 	}
2757     }
2758 
2759   if (opts->x_ix86_recip_name)
2760     {
2761       char *p = ASTRDUP (opts->x_ix86_recip_name);
2762       char *q;
2763       unsigned int mask;
2764       bool invert;
2765 
2766       while ((q = strtok (p, ",")) != NULL)
2767 	{
2768 	  p = NULL;
2769 	  if (*q == '!')
2770 	    {
2771 	      invert = true;
2772 	      q++;
2773 	    }
2774 	  else
2775 	    invert = false;
2776 
2777 	  if (!strcmp (q, "default"))
2778 	    mask = RECIP_MASK_ALL;
2779 	  else
2780 	    {
2781 	      for (i = 0; i < ARRAY_SIZE (recip_options); i++)
2782 		if (!strcmp (q, recip_options[i].string))
2783 		  {
2784 		    mask = recip_options[i].mask;
2785 		    break;
2786 		  }
2787 
2788 	      if (i == ARRAY_SIZE (recip_options))
2789 		{
2790 		  error ("unknown option for %<-mrecip=%s%>", q);
2791 		  invert = false;
2792 		  mask = RECIP_MASK_NONE;
2793 		}
2794 	    }
2795 
2796 	  opts->x_recip_mask_explicit |= mask;
2797 	  if (invert)
2798 	    opts->x_recip_mask &= ~mask;
2799 	  else
2800 	    opts->x_recip_mask |= mask;
2801 	}
2802     }
2803 
2804   if (TARGET_RECIP_P (opts->x_target_flags))
2805     opts->x_recip_mask |= RECIP_MASK_ALL & ~opts->x_recip_mask_explicit;
2806   else if (opts_set->x_target_flags & MASK_RECIP)
2807     opts->x_recip_mask &= ~(RECIP_MASK_ALL & ~opts->x_recip_mask_explicit);
2808 
2809   /* Default long double to 64-bit for 32-bit Bionic and to __float128
2810      for 64-bit Bionic.  Also default long double to 64-bit for Intel
2811      MCU psABI.  */
2812   if ((TARGET_HAS_BIONIC || TARGET_IAMCU)
2813       && !(opts_set->x_target_flags
2814 	   & (MASK_LONG_DOUBLE_64 | MASK_LONG_DOUBLE_128)))
2815     opts->x_target_flags |= (TARGET_64BIT
2816 			     ? MASK_LONG_DOUBLE_128
2817 			     : MASK_LONG_DOUBLE_64);
2818 
2819   /* Only one of them can be active.  */
2820   gcc_assert ((opts->x_target_flags & MASK_LONG_DOUBLE_64) == 0
2821 	      || (opts->x_target_flags & MASK_LONG_DOUBLE_128) == 0);
2822 
2823   /* Handle stack protector */
2824   if (!opts_set->x_ix86_stack_protector_guard)
2825     {
2826 #ifdef TARGET_THREAD_SSP_OFFSET
2827       if (!TARGET_HAS_BIONIC)
2828 	opts->x_ix86_stack_protector_guard = SSP_TLS;
2829       else
2830 #endif
2831 	opts->x_ix86_stack_protector_guard = SSP_GLOBAL;
2832     }
2833 
2834   if (opts_set->x_ix86_stack_protector_guard_offset_str)
2835     {
2836       char *endp;
2837       const char *str = opts->x_ix86_stack_protector_guard_offset_str;
2838 
2839       errno = 0;
2840       int64_t offset;
2841 
2842 #if defined(INT64_T_IS_LONG)
2843       offset = strtol (str, &endp, 0);
2844 #else
2845       offset = strtoll (str, &endp, 0);
2846 #endif
2847 
2848       if (!*str || *endp || errno)
2849 	error ("%qs is not a valid number "
2850 	       "in %<-mstack-protector-guard-offset=%>", str);
2851 
2852       if (!IN_RANGE (offset, HOST_WIDE_INT_C (-0x80000000),
2853 		     HOST_WIDE_INT_C (0x7fffffff)))
2854 	error ("%qs is not a valid offset "
2855 	       "in %<-mstack-protector-guard-offset=%>", str);
2856 
2857       opts->x_ix86_stack_protector_guard_offset = offset;
2858     }
2859 #ifdef TARGET_THREAD_SSP_OFFSET
2860   else
2861     opts->x_ix86_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
2862 #endif
2863 
2864   if (opts_set->x_ix86_stack_protector_guard_reg_str)
2865     {
2866       const char *str = opts->x_ix86_stack_protector_guard_reg_str;
2867       addr_space_t seg = ADDR_SPACE_GENERIC;
2868 
2869       /* Discard optional register prefix.  */
2870       if (str[0] == '%')
2871 	str++;
2872 
2873       if (strlen (str) == 2 && str[1] == 's')
2874 	{
2875 	  if (str[0] == 'f')
2876 	    seg = ADDR_SPACE_SEG_FS;
2877 	  else if (str[0] == 'g')
2878 	    seg = ADDR_SPACE_SEG_GS;
2879 	}
2880 
2881       if (seg == ADDR_SPACE_GENERIC)
2882 	error ("%qs is not a valid base register "
2883 	       "in %<-mstack-protector-guard-reg=%>",
2884 	       opts->x_ix86_stack_protector_guard_reg_str);
2885 
2886       opts->x_ix86_stack_protector_guard_reg = seg;
2887     }
2888   else
2889     {
2890       opts->x_ix86_stack_protector_guard_reg = DEFAULT_TLS_SEG_REG;
2891 
2892       /* The kernel uses a different segment register for performance
2893 	 reasons; a system call would not have to trash the userspace
2894 	 segment register, which would be expensive.  */
2895       if (opts->x_ix86_cmodel == CM_KERNEL)
2896 	opts->x_ix86_stack_protector_guard_reg = ADDR_SPACE_SEG_GS;
2897     }
2898 
2899   /* Handle -mmemcpy-strategy= and -mmemset-strategy=  */
2900   if (opts->x_ix86_tune_memcpy_strategy)
2901     {
2902       char *str = xstrdup (opts->x_ix86_tune_memcpy_strategy);
2903       ix86_parse_stringop_strategy_string (str, false);
2904       free (str);
2905     }
2906 
2907   if (opts->x_ix86_tune_memset_strategy)
2908     {
2909       char *str = xstrdup (opts->x_ix86_tune_memset_strategy);
2910       ix86_parse_stringop_strategy_string (str, true);
2911       free (str);
2912     }
2913 
2914   /* Save the initial options in case the user does function specific
2915      options.  */
2916   if (main_args_p)
2917     {
2918       opts->x_ix86_excess_precision
2919 	= opts->x_flag_excess_precision;
2920       opts->x_ix86_unsafe_math_optimizations
2921 	= opts->x_flag_unsafe_math_optimizations;
2922       target_option_default_node = target_option_current_node
2923         = build_target_option_node (opts, opts_set);
2924     }
2925 
2926   if (opts->x_flag_cf_protection != CF_NONE)
2927     {
2928       if ((opts->x_flag_cf_protection & CF_BRANCH) == CF_BRANCH
2929 	  && !TARGET_64BIT && !TARGET_CMOV)
2930 	error ("%<-fcf-protection%> is not compatible with this target");
2931 
2932       opts->x_flag_cf_protection
2933       = (cf_protection_level) (opts->x_flag_cf_protection | CF_SET);
2934     }
2935 
2936   if (ix86_tune_features [X86_TUNE_AVOID_256FMA_CHAINS])
2937     SET_OPTION_IF_UNSET (opts, opts_set, param_avoid_fma_max_bits, 512);
2938   else if (ix86_tune_features [X86_TUNE_AVOID_256FMA_CHAINS])
2939     SET_OPTION_IF_UNSET (opts, opts_set, param_avoid_fma_max_bits, 256);
2940   else if (ix86_tune_features [X86_TUNE_AVOID_128FMA_CHAINS])
2941     SET_OPTION_IF_UNSET (opts, opts_set, param_avoid_fma_max_bits, 128);
2942 
2943   /* PR86952: jump table usage with retpolines is slow.
2944      The PR provides some numbers about the slowness.  */
2945   if (ix86_indirect_branch != indirect_branch_keep)
2946     SET_OPTION_IF_UNSET (opts, opts_set, flag_jump_tables, 0);
2947 
2948   SET_OPTION_IF_UNSET (opts, opts_set, param_ira_consider_dup_in_all_alts, 0);
2949 
2950   /* Fully masking the main or the epilogue vectorized loop is not
2951      profitable generally so leave it disabled until we get more
2952      fine grained control & costing.  */
2953   SET_OPTION_IF_UNSET (opts, opts_set, param_vect_partial_vector_usage, 0);
2954 
2955   return true;
2956 }
2957 
2958 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
2959 
2960 void
ix86_option_override(void)2961 ix86_option_override (void)
2962 {
2963   ix86_option_override_internal (true, &global_options, &global_options_set);
2964 }
2965 
2966 /* Remember the last target of ix86_set_current_function.  */
2967 static GTY(()) tree ix86_previous_fndecl;
2968 
2969 /* Set targets globals to the default (or current #pragma GCC target
2970    if active).  Invalidate ix86_previous_fndecl cache.  */
2971 
2972 void
ix86_reset_previous_fndecl(void)2973 ix86_reset_previous_fndecl (void)
2974 {
2975   tree new_tree = target_option_current_node;
2976   cl_target_option_restore (&global_options, &global_options_set,
2977 			    TREE_TARGET_OPTION (new_tree));
2978   if (TREE_TARGET_GLOBALS (new_tree))
2979     restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
2980   else if (new_tree == target_option_default_node)
2981     restore_target_globals (&default_target_globals);
2982   else
2983     TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
2984   ix86_previous_fndecl = NULL_TREE;
2985 }
2986 
2987 /* Add target attribute to SIMD clone NODE if needed.  */
2988 
2989 void
ix86_simd_clone_adjust(struct cgraph_node * node)2990 ix86_simd_clone_adjust (struct cgraph_node *node)
2991 {
2992   const char *str = NULL;
2993 
2994   /* Attributes need to be adjusted for definitions, not declarations.  */
2995   if (!node->definition)
2996     return;
2997 
2998   gcc_assert (node->decl == cfun->decl);
2999   switch (node->simdclone->vecsize_mangle)
3000     {
3001     case 'b':
3002       if (!TARGET_SSE2)
3003 	str = "sse2";
3004       break;
3005     case 'c':
3006       if (TARGET_PREFER_AVX128)
3007 	{
3008 	  if (!TARGET_AVX)
3009 	    str = "avx,prefer-vector-width=256";
3010 	  else
3011 	    str = "prefer-vector-width=256";
3012 	}
3013       else if (!TARGET_AVX)
3014 	str = "avx";
3015       break;
3016     case 'd':
3017       if (TARGET_PREFER_AVX128)
3018 	{
3019 	  if (!TARGET_AVX2)
3020 	    str = "avx2,prefer-vector-width=256";
3021 	  else
3022 	    str = "prefer-vector-width=256";
3023 	}
3024       else if (!TARGET_AVX2)
3025 	str = "avx2";
3026       break;
3027     case 'e':
3028       if (TARGET_PREFER_AVX256)
3029 	{
3030 	  if (!TARGET_AVX512F)
3031 	    str = "avx512f,prefer-vector-width=512";
3032 	  else
3033 	    str = "prefer-vector-width=512";
3034 	}
3035       else if (!TARGET_AVX512F)
3036 	str = "avx512f";
3037       break;
3038     default:
3039       gcc_unreachable ();
3040     }
3041   if (str == NULL)
3042     return;
3043   push_cfun (NULL);
3044   tree args = build_tree_list (NULL_TREE, build_string (strlen (str), str));
3045   bool ok = ix86_valid_target_attribute_p (node->decl, NULL, args, 0);
3046   gcc_assert (ok);
3047   pop_cfun ();
3048   ix86_reset_previous_fndecl ();
3049   ix86_set_current_function (node->decl);
3050 }
3051 
3052 
3053 
3054 /* Set the func_type field from the function FNDECL.  */
3055 
3056 static void
ix86_set_func_type(tree fndecl)3057 ix86_set_func_type (tree fndecl)
3058 {
3059   if (cfun->machine->func_type == TYPE_UNKNOWN)
3060     {
3061       if (lookup_attribute ("interrupt",
3062 			    TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
3063 	{
3064 	  if (ix86_function_naked (fndecl))
3065 	    error_at (DECL_SOURCE_LOCATION (fndecl),
3066 		      "interrupt and naked attributes are not compatible");
3067 
3068 	  int nargs = 0;
3069 	  for (tree arg = DECL_ARGUMENTS (fndecl);
3070 	       arg;
3071 	       arg = TREE_CHAIN (arg))
3072 	    nargs++;
3073 	  cfun->machine->no_caller_saved_registers = true;
3074 	  cfun->machine->func_type
3075 	    = nargs == 2 ? TYPE_EXCEPTION : TYPE_INTERRUPT;
3076 
3077 	  ix86_optimize_mode_switching[X86_DIRFLAG] = 1;
3078 
3079 	  /* Only dwarf2out.cc can handle -WORD(AP) as a pointer argument.  */
3080 	  if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
3081 	    sorry ("only DWARF debug format is supported for interrupt "
3082 		   "service routine");
3083 	}
3084       else
3085 	{
3086 	  cfun->machine->func_type = TYPE_NORMAL;
3087 	  if (lookup_attribute ("no_caller_saved_registers",
3088 				TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
3089 	    cfun->machine->no_caller_saved_registers = true;
3090 	}
3091     }
3092 }
3093 
3094 /* Set the indirect_branch_type field from the function FNDECL.  */
3095 
3096 static void
ix86_set_indirect_branch_type(tree fndecl)3097 ix86_set_indirect_branch_type (tree fndecl)
3098 {
3099   if (cfun->machine->indirect_branch_type == indirect_branch_unset)
3100     {
3101       tree attr = lookup_attribute ("indirect_branch",
3102 				    DECL_ATTRIBUTES (fndecl));
3103       if (attr != NULL)
3104 	{
3105 	  tree args = TREE_VALUE (attr);
3106 	  if (args == NULL)
3107 	    gcc_unreachable ();
3108 	  tree cst = TREE_VALUE (args);
3109 	  if (strcmp (TREE_STRING_POINTER (cst), "keep") == 0)
3110 	    cfun->machine->indirect_branch_type = indirect_branch_keep;
3111 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk") == 0)
3112 	    cfun->machine->indirect_branch_type = indirect_branch_thunk;
3113 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-inline") == 0)
3114 	    cfun->machine->indirect_branch_type = indirect_branch_thunk_inline;
3115 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-extern") == 0)
3116 	    cfun->machine->indirect_branch_type = indirect_branch_thunk_extern;
3117 	  else
3118 	    gcc_unreachable ();
3119 	}
3120       else
3121 	cfun->machine->indirect_branch_type = ix86_indirect_branch;
3122 
3123       /* -mcmodel=large is not compatible with -mindirect-branch=thunk
3124 	 nor -mindirect-branch=thunk-extern.  */
3125       if ((ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
3126 	  && ((cfun->machine->indirect_branch_type
3127 	       == indirect_branch_thunk_extern)
3128 	      || (cfun->machine->indirect_branch_type
3129 		  == indirect_branch_thunk)))
3130 	error ("%<-mindirect-branch=%s%> and %<-mcmodel=large%> are not "
3131 	       "compatible",
3132 	       ((cfun->machine->indirect_branch_type
3133 		 == indirect_branch_thunk_extern)
3134 		? "thunk-extern" : "thunk"));
3135 
3136       if (cfun->machine->indirect_branch_type != indirect_branch_keep
3137 	  && (cfun->machine->indirect_branch_type
3138 	      != indirect_branch_thunk_extern)
3139 	  && (flag_cf_protection & CF_RETURN))
3140 	error ("%<-mindirect-branch%> and %<-fcf-protection%> are not "
3141 	       "compatible");
3142     }
3143 
3144   if (cfun->machine->function_return_type == indirect_branch_unset)
3145     {
3146       tree attr = lookup_attribute ("function_return",
3147 				    DECL_ATTRIBUTES (fndecl));
3148       if (attr != NULL)
3149 	{
3150 	  tree args = TREE_VALUE (attr);
3151 	  if (args == NULL)
3152 	    gcc_unreachable ();
3153 	  tree cst = TREE_VALUE (args);
3154 	  if (strcmp (TREE_STRING_POINTER (cst), "keep") == 0)
3155 	    cfun->machine->function_return_type = indirect_branch_keep;
3156 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk") == 0)
3157 	    cfun->machine->function_return_type = indirect_branch_thunk;
3158 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-inline") == 0)
3159 	    cfun->machine->function_return_type = indirect_branch_thunk_inline;
3160 	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-extern") == 0)
3161 	    cfun->machine->function_return_type = indirect_branch_thunk_extern;
3162 	  else
3163 	    gcc_unreachable ();
3164 	}
3165       else
3166 	cfun->machine->function_return_type = ix86_function_return;
3167 
3168       /* -mcmodel=large is not compatible with -mfunction-return=thunk
3169 	 nor -mfunction-return=thunk-extern.  */
3170       if ((ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
3171 	  && ((cfun->machine->function_return_type
3172 	       == indirect_branch_thunk_extern)
3173 	      || (cfun->machine->function_return_type
3174 		  == indirect_branch_thunk)))
3175 	error ("%<-mfunction-return=%s%> and %<-mcmodel=large%> are not "
3176 	       "compatible",
3177 	       ((cfun->machine->function_return_type
3178 		 == indirect_branch_thunk_extern)
3179 		? "thunk-extern" : "thunk"));
3180 
3181       if (cfun->machine->function_return_type != indirect_branch_keep
3182 	  && (cfun->machine->function_return_type
3183 	      != indirect_branch_thunk_extern)
3184 	  && (flag_cf_protection & CF_RETURN))
3185 	error ("%<-mfunction-return%> and %<-fcf-protection%> are not "
3186 	       "compatible");
3187     }
3188 }
3189 
3190 /* Establish appropriate back-end context for processing the function
3191    FNDECL.  The argument might be NULL to indicate processing at top
3192    level, outside of any function scope.  */
3193 void
ix86_set_current_function(tree fndecl)3194 ix86_set_current_function (tree fndecl)
3195 {
3196   /* Only change the context if the function changes.  This hook is called
3197      several times in the course of compiling a function, and we don't want to
3198      slow things down too much or call target_reinit when it isn't safe.  */
3199   if (fndecl == ix86_previous_fndecl)
3200     {
3201       /* There may be 2 function bodies for the same function FNDECL,
3202 	 one is extern inline and one isn't.  Call ix86_set_func_type
3203 	 to set the func_type field.  */
3204       if (fndecl != NULL_TREE)
3205 	{
3206 	  ix86_set_func_type (fndecl);
3207 	  ix86_set_indirect_branch_type (fndecl);
3208 	}
3209       return;
3210     }
3211 
3212   tree old_tree;
3213   if (ix86_previous_fndecl == NULL_TREE)
3214     old_tree = target_option_current_node;
3215   else if (DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl))
3216     old_tree = DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl);
3217   else
3218     old_tree = target_option_default_node;
3219 
3220   if (fndecl == NULL_TREE)
3221     {
3222       if (old_tree != target_option_current_node)
3223 	ix86_reset_previous_fndecl ();
3224       return;
3225     }
3226 
3227   ix86_set_func_type (fndecl);
3228   ix86_set_indirect_branch_type (fndecl);
3229 
3230   tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
3231   if (new_tree == NULL_TREE)
3232     new_tree = target_option_default_node;
3233 
3234   bool fp_flag_change
3235     = (flag_unsafe_math_optimizations
3236        != TREE_TARGET_OPTION (new_tree)->x_ix86_unsafe_math_optimizations
3237        || (flag_excess_precision
3238 	   != TREE_TARGET_OPTION (new_tree)->x_ix86_excess_precision));
3239   if (old_tree != new_tree || fp_flag_change)
3240     {
3241       cl_target_option_restore (&global_options, &global_options_set,
3242 				TREE_TARGET_OPTION (new_tree));
3243       if (fp_flag_change)
3244 	{
3245 	  ix86_excess_precision = flag_excess_precision;
3246 	  ix86_unsafe_math_optimizations = flag_unsafe_math_optimizations;
3247 	  DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_tree
3248 	    = build_target_option_node (&global_options, &global_options_set);
3249 	}
3250       if (TREE_TARGET_GLOBALS (new_tree))
3251 	restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
3252       else if (new_tree == target_option_default_node)
3253 	restore_target_globals (&default_target_globals);
3254       else
3255 	TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
3256     }
3257   ix86_previous_fndecl = fndecl;
3258 
3259   static bool prev_no_caller_saved_registers;
3260 
3261   /* 64-bit MS and SYSV ABI have different set of call used registers.
3262      Avoid expensive re-initialization of init_regs each time we switch
3263      function context.  */
3264   if (TARGET_64BIT
3265       && (call_used_or_fixed_reg_p (SI_REG)
3266 	  == (cfun->machine->call_abi == MS_ABI)))
3267     reinit_regs ();
3268   /* Need to re-initialize init_regs if caller-saved registers are
3269      changed.  */
3270   else if (prev_no_caller_saved_registers
3271 	   != cfun->machine->no_caller_saved_registers)
3272     reinit_regs ();
3273 
3274   if (cfun->machine->func_type != TYPE_NORMAL
3275       || cfun->machine->no_caller_saved_registers)
3276     {
3277       /* Don't allow SSE, MMX nor x87 instructions since they
3278 	 may change processor state.  */
3279       const char *isa;
3280       if (TARGET_SSE)
3281 	isa = "SSE";
3282       else if (TARGET_MMX)
3283 	isa = "MMX/3Dnow";
3284       else if (TARGET_80387)
3285 	isa = "80387";
3286       else
3287 	isa = NULL;
3288       if (isa != NULL)
3289 	{
3290 	  if (cfun->machine->func_type != TYPE_NORMAL)
3291 	    sorry (cfun->machine->func_type == TYPE_EXCEPTION
3292 		   ? G_("%s instructions aren%'t allowed in an"
3293 			" exception service routine")
3294 		   : G_("%s instructions aren%'t allowed in an"
3295 			" interrupt service routine"),
3296 		   isa);
3297 	  else
3298 	    sorry ("%s instructions aren%'t allowed in a function with "
3299 		   "the %<no_caller_saved_registers%> attribute", isa);
3300 	  /* Don't issue the same error twice.  */
3301 	  cfun->machine->func_type = TYPE_NORMAL;
3302 	  cfun->machine->no_caller_saved_registers = false;
3303 	}
3304     }
3305 
3306   prev_no_caller_saved_registers
3307     = cfun->machine->no_caller_saved_registers;
3308 }
3309 
3310 /* Implement the TARGET_OFFLOAD_OPTIONS hook.  */
3311 char *
ix86_offload_options(void)3312 ix86_offload_options (void)
3313 {
3314   if (TARGET_LP64)
3315     return xstrdup ("-foffload-abi=lp64");
3316   return xstrdup ("-foffload-abi=ilp32");
3317 }
3318 
3319 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
3320    and "sseregparm" calling convention attributes;
3321    arguments as in struct attribute_spec.handler.  */
3322 
3323 static tree
ix86_handle_cconv_attribute(tree * node,tree name,tree args,int,bool * no_add_attrs)3324 ix86_handle_cconv_attribute (tree *node, tree name, tree args, int,
3325 			     bool *no_add_attrs)
3326 {
3327   if (TREE_CODE (*node) != FUNCTION_TYPE
3328       && TREE_CODE (*node) != METHOD_TYPE
3329       && TREE_CODE (*node) != FIELD_DECL
3330       && TREE_CODE (*node) != TYPE_DECL)
3331     {
3332       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3333 	       name);
3334       *no_add_attrs = true;
3335       return NULL_TREE;
3336     }
3337 
3338   /* Can combine regparm with all attributes but fastcall, and thiscall.  */
3339   if (is_attribute_p ("regparm", name))
3340     {
3341       tree cst;
3342 
3343       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3344         {
3345 	  error ("fastcall and regparm attributes are not compatible");
3346 	}
3347 
3348       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
3349 	{
3350 	  error ("regparam and thiscall attributes are not compatible");
3351 	}
3352 
3353       cst = TREE_VALUE (args);
3354       if (TREE_CODE (cst) != INTEGER_CST)
3355 	{
3356 	  warning (OPT_Wattributes,
3357 		   "%qE attribute requires an integer constant argument",
3358 		   name);
3359 	  *no_add_attrs = true;
3360 	}
3361       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
3362 	{
3363 	  warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
3364 		   name, REGPARM_MAX);
3365 	  *no_add_attrs = true;
3366 	}
3367 
3368       return NULL_TREE;
3369     }
3370 
3371   if (TARGET_64BIT)
3372     {
3373       /* Do not warn when emulating the MS ABI.  */
3374       if ((TREE_CODE (*node) != FUNCTION_TYPE
3375 	   && TREE_CODE (*node) != METHOD_TYPE)
3376 	  || ix86_function_type_abi (*node) != MS_ABI)
3377 	warning (OPT_Wattributes, "%qE attribute ignored",
3378 	         name);
3379       *no_add_attrs = true;
3380       return NULL_TREE;
3381     }
3382 
3383   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
3384   if (is_attribute_p ("fastcall", name))
3385     {
3386       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
3387         {
3388 	  error ("fastcall and cdecl attributes are not compatible");
3389 	}
3390       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
3391         {
3392 	  error ("fastcall and stdcall attributes are not compatible");
3393 	}
3394       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
3395         {
3396 	  error ("fastcall and regparm attributes are not compatible");
3397 	}
3398       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
3399 	{
3400 	  error ("fastcall and thiscall attributes are not compatible");
3401 	}
3402     }
3403 
3404   /* Can combine stdcall with fastcall (redundant), regparm and
3405      sseregparm.  */
3406   else if (is_attribute_p ("stdcall", name))
3407     {
3408       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
3409         {
3410 	  error ("stdcall and cdecl attributes are not compatible");
3411 	}
3412       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3413         {
3414 	  error ("stdcall and fastcall attributes are not compatible");
3415 	}
3416       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
3417 	{
3418 	  error ("stdcall and thiscall attributes are not compatible");
3419 	}
3420     }
3421 
3422   /* Can combine cdecl with regparm and sseregparm.  */
3423   else if (is_attribute_p ("cdecl", name))
3424     {
3425       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
3426         {
3427 	  error ("stdcall and cdecl attributes are not compatible");
3428 	}
3429       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3430         {
3431 	  error ("fastcall and cdecl attributes are not compatible");
3432 	}
3433       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
3434 	{
3435 	  error ("cdecl and thiscall attributes are not compatible");
3436 	}
3437     }
3438   else if (is_attribute_p ("thiscall", name))
3439     {
3440       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
3441 	warning (OPT_Wattributes, "%qE attribute is used for non-class method",
3442 	         name);
3443       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
3444 	{
3445 	  error ("stdcall and thiscall attributes are not compatible");
3446 	}
3447       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3448 	{
3449 	  error ("fastcall and thiscall attributes are not compatible");
3450 	}
3451       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
3452 	{
3453 	  error ("cdecl and thiscall attributes are not compatible");
3454 	}
3455     }
3456 
3457   /* Can combine sseregparm with all attributes.  */
3458 
3459   return NULL_TREE;
3460 }
3461 
3462 #ifndef CHECK_STACK_LIMIT
3463 #define CHECK_STACK_LIMIT (-1)
3464 #endif
3465 
3466 /* The transactional memory builtins are implicitly regparm or fastcall
3467    depending on the ABI.  Override the generic do-nothing attribute that
3468    these builtins were declared with, and replace it with one of the two
3469    attributes that we expect elsewhere.  */
3470 
3471 static tree
ix86_handle_tm_regparm_attribute(tree * node,tree,tree,int flags,bool * no_add_attrs)3472 ix86_handle_tm_regparm_attribute (tree *node, tree, tree,
3473 				  int flags, bool *no_add_attrs)
3474 {
3475   tree alt;
3476 
3477   /* In no case do we want to add the placeholder attribute.  */
3478   *no_add_attrs = true;
3479 
3480   /* The 64-bit ABI is unchanged for transactional memory.  */
3481   if (TARGET_64BIT)
3482     return NULL_TREE;
3483 
3484   /* ??? Is there a better way to validate 32-bit windows?  We have
3485      cfun->machine->call_abi, but that seems to be set only for 64-bit.  */
3486   if (CHECK_STACK_LIMIT > 0)
3487     alt = tree_cons (get_identifier ("fastcall"), NULL, NULL);
3488   else
3489     {
3490       alt = tree_cons (NULL, build_int_cst (NULL, 2), NULL);
3491       alt = tree_cons (get_identifier ("regparm"), alt, NULL);
3492     }
3493   decl_attributes (node, alt, flags);
3494 
3495   return NULL_TREE;
3496 }
3497 
3498 /* Handle a "force_align_arg_pointer" attribute.  */
3499 
3500 static tree
ix86_handle_force_align_arg_pointer_attribute(tree * node,tree name,tree,int,bool * no_add_attrs)3501 ix86_handle_force_align_arg_pointer_attribute (tree *node, tree name,
3502 					       tree, int, bool *no_add_attrs)
3503 {
3504   if (TREE_CODE (*node) != FUNCTION_TYPE
3505       && TREE_CODE (*node) != METHOD_TYPE
3506       && TREE_CODE (*node) != FIELD_DECL
3507       && TREE_CODE (*node) != TYPE_DECL)
3508     {
3509       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3510 	       name);
3511       *no_add_attrs = true;
3512     }
3513 
3514   return NULL_TREE;
3515 }
3516 
3517 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
3518    struct attribute_spec.handler.  */
3519 
3520 static tree
ix86_handle_struct_attribute(tree * node,tree name,tree,int,bool * no_add_attrs)3521 ix86_handle_struct_attribute (tree *node, tree name, tree, int,
3522 			      bool *no_add_attrs)
3523 {
3524   tree *type = NULL;
3525   if (DECL_P (*node))
3526     {
3527       if (TREE_CODE (*node) == TYPE_DECL)
3528 	type = &TREE_TYPE (*node);
3529     }
3530   else
3531     type = node;
3532 
3533   if (!(type && RECORD_OR_UNION_TYPE_P (*type)))
3534     {
3535       warning (OPT_Wattributes, "%qE attribute ignored",
3536 	       name);
3537       *no_add_attrs = true;
3538     }
3539 
3540   else if ((is_attribute_p ("ms_struct", name)
3541 	    && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
3542 	   || ((is_attribute_p ("gcc_struct", name)
3543 		&& lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
3544     {
3545       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
3546                name);
3547       *no_add_attrs = true;
3548     }
3549 
3550   return NULL_TREE;
3551 }
3552 
3553 /* Handle a "callee_pop_aggregate_return" attribute; arguments as
3554    in struct attribute_spec handler.  */
3555 
3556 static tree
ix86_handle_callee_pop_aggregate_return(tree * node,tree name,tree args,int,bool * no_add_attrs)3557 ix86_handle_callee_pop_aggregate_return (tree *node, tree name, tree args, int,
3558 					 bool *no_add_attrs)
3559 {
3560   if (TREE_CODE (*node) != FUNCTION_TYPE
3561       && TREE_CODE (*node) != METHOD_TYPE
3562       && TREE_CODE (*node) != FIELD_DECL
3563       && TREE_CODE (*node) != TYPE_DECL)
3564     {
3565       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3566 	       name);
3567       *no_add_attrs = true;
3568       return NULL_TREE;
3569     }
3570   if (TARGET_64BIT)
3571     {
3572       warning (OPT_Wattributes, "%qE attribute only available for 32-bit",
3573 	       name);
3574       *no_add_attrs = true;
3575       return NULL_TREE;
3576     }
3577   if (is_attribute_p ("callee_pop_aggregate_return", name))
3578     {
3579       tree cst;
3580 
3581       cst = TREE_VALUE (args);
3582       if (TREE_CODE (cst) != INTEGER_CST)
3583 	{
3584 	  warning (OPT_Wattributes,
3585 		   "%qE attribute requires an integer constant argument",
3586 		   name);
3587 	  *no_add_attrs = true;
3588 	}
3589       else if (compare_tree_int (cst, 0) != 0
3590 	       && compare_tree_int (cst, 1) != 0)
3591 	{
3592 	  warning (OPT_Wattributes,
3593 		   "argument to %qE attribute is neither zero, nor one",
3594 		   name);
3595 	  *no_add_attrs = true;
3596 	}
3597 
3598       return NULL_TREE;
3599     }
3600 
3601   return NULL_TREE;
3602 }
3603 
3604 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
3605    struct attribute_spec.handler.  */
3606 
3607 static tree
ix86_handle_abi_attribute(tree * node,tree name,tree,int,bool * no_add_attrs)3608 ix86_handle_abi_attribute (tree *node, tree name, tree, int,
3609 			   bool *no_add_attrs)
3610 {
3611   if (TREE_CODE (*node) != FUNCTION_TYPE
3612       && TREE_CODE (*node) != METHOD_TYPE
3613       && TREE_CODE (*node) != FIELD_DECL
3614       && TREE_CODE (*node) != TYPE_DECL)
3615     {
3616       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3617 	       name);
3618       *no_add_attrs = true;
3619       return NULL_TREE;
3620     }
3621 
3622   /* Can combine regparm with all attributes but fastcall.  */
3623   if (is_attribute_p ("ms_abi", name))
3624     {
3625       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
3626         {
3627 	  error ("%qs and %qs attributes are not compatible",
3628 		 "ms_abi", "sysv_abi");
3629 	}
3630 
3631       return NULL_TREE;
3632     }
3633   else if (is_attribute_p ("sysv_abi", name))
3634     {
3635       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
3636         {
3637 	  error ("%qs and %qs attributes are not compatible",
3638 		 "ms_abi", "sysv_abi");
3639 	}
3640 
3641       return NULL_TREE;
3642     }
3643 
3644   return NULL_TREE;
3645 }
3646 
3647 static tree
ix86_handle_fndecl_attribute(tree * node,tree name,tree args,int,bool * no_add_attrs)3648 ix86_handle_fndecl_attribute (tree *node, tree name, tree args, int,
3649 			      bool *no_add_attrs)
3650 {
3651   if (TREE_CODE (*node) != FUNCTION_DECL)
3652     {
3653       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3654                name);
3655       *no_add_attrs = true;
3656     }
3657 
3658   if (is_attribute_p ("indirect_branch", name))
3659     {
3660       tree cst = TREE_VALUE (args);
3661       if (TREE_CODE (cst) != STRING_CST)
3662 	{
3663 	  warning (OPT_Wattributes,
3664 		   "%qE attribute requires a string constant argument",
3665 		   name);
3666 	  *no_add_attrs = true;
3667 	}
3668       else if (strcmp (TREE_STRING_POINTER (cst), "keep") != 0
3669 	       && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0
3670 	       && strcmp (TREE_STRING_POINTER (cst), "thunk-inline") != 0
3671 	       && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0)
3672 	{
3673 	  warning (OPT_Wattributes,
3674 		   "argument to %qE attribute is not "
3675 		   "(keep|thunk|thunk-inline|thunk-extern)", name);
3676 	  *no_add_attrs = true;
3677 	}
3678     }
3679 
3680   if (is_attribute_p ("function_return", name))
3681     {
3682       tree cst = TREE_VALUE (args);
3683       if (TREE_CODE (cst) != STRING_CST)
3684 	{
3685 	  warning (OPT_Wattributes,
3686 		   "%qE attribute requires a string constant argument",
3687 		   name);
3688 	  *no_add_attrs = true;
3689 	}
3690       else if (strcmp (TREE_STRING_POINTER (cst), "keep") != 0
3691 	       && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0
3692 	       && strcmp (TREE_STRING_POINTER (cst), "thunk-inline") != 0
3693 	       && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0)
3694 	{
3695 	  warning (OPT_Wattributes,
3696 		   "argument to %qE attribute is not "
3697 		   "(keep|thunk|thunk-inline|thunk-extern)", name);
3698 	  *no_add_attrs = true;
3699 	}
3700     }
3701 
3702   return NULL_TREE;
3703 }
3704 
3705 static tree
ix86_handle_no_caller_saved_registers_attribute(tree *,tree,tree,int,bool *)3706 ix86_handle_no_caller_saved_registers_attribute (tree *, tree, tree,
3707 						 int, bool *)
3708 {
3709   return NULL_TREE;
3710 }
3711 
3712 static tree
ix86_handle_interrupt_attribute(tree * node,tree,tree,int,bool *)3713 ix86_handle_interrupt_attribute (tree *node, tree, tree, int, bool *)
3714 {
3715   /* DECL_RESULT and DECL_ARGUMENTS do not exist there yet,
3716      but the function type contains args and return type data.  */
3717   tree func_type = *node;
3718   tree return_type = TREE_TYPE (func_type);
3719 
3720   int nargs = 0;
3721   tree current_arg_type = TYPE_ARG_TYPES (func_type);
3722   while (current_arg_type
3723 	 && ! VOID_TYPE_P (TREE_VALUE (current_arg_type)))
3724     {
3725       if (nargs == 0)
3726 	{
3727 	  if (! POINTER_TYPE_P (TREE_VALUE (current_arg_type)))
3728 	    error ("interrupt service routine should have a pointer "
3729 		   "as the first argument");
3730 	}
3731       else if (nargs == 1)
3732 	{
3733 	  if (TREE_CODE (TREE_VALUE (current_arg_type)) != INTEGER_TYPE
3734 	      || TYPE_MODE (TREE_VALUE (current_arg_type)) != word_mode)
3735 	    error ("interrupt service routine should have %qs "
3736 		   "as the second argument",
3737 		   TARGET_64BIT
3738 		   ? (TARGET_X32 ? "unsigned long long int"
3739 				 : "unsigned long int")
3740 		   : "unsigned int");
3741 	}
3742       nargs++;
3743       current_arg_type = TREE_CHAIN (current_arg_type);
3744     }
3745   if (!nargs || nargs > 2)
3746     error ("interrupt service routine can only have a pointer argument "
3747 	   "and an optional integer argument");
3748   if (! VOID_TYPE_P (return_type))
3749     error ("interrupt service routine must return %<void%>");
3750 
3751   return NULL_TREE;
3752 }
3753 
3754 /* Handle fentry_name / fentry_section attribute.  */
3755 
3756 static tree
ix86_handle_fentry_name(tree * node,tree name,tree args,int,bool * no_add_attrs)3757 ix86_handle_fentry_name (tree *node, tree name, tree args,
3758 			 int, bool *no_add_attrs)
3759 {
3760   if (TREE_CODE (*node) == FUNCTION_DECL
3761       && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
3762     /* Do nothing else, just set the attribute.  We'll get at
3763        it later with lookup_attribute.  */
3764     ;
3765   else
3766     {
3767       warning (OPT_Wattributes, "%qE attribute ignored", name);
3768       *no_add_attrs = true;
3769     }
3770 
3771   return NULL_TREE;
3772 }
3773 
3774 /* Handle a "nodirect_extern_access" attribute; arguments as in
3775    struct attribute_spec.handler.  */
3776 
3777 static tree
handle_nodirect_extern_access_attribute(tree * pnode,tree name,tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * no_add_attrs)3778 handle_nodirect_extern_access_attribute (tree *pnode, tree name,
3779 					 tree ARG_UNUSED (args),
3780 					 int ARG_UNUSED (flags),
3781 					 bool *no_add_attrs)
3782 {
3783   tree node = *pnode;
3784 
3785   if (VAR_OR_FUNCTION_DECL_P (node))
3786     {
3787       if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
3788 	   && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
3789 	{
3790 	  warning (OPT_Wattributes,
3791 		   "%qE attribute have effect only on public objects", name);
3792 	  *no_add_attrs = true;
3793 	}
3794     }
3795   else
3796     {
3797       warning (OPT_Wattributes, "%qE attribute ignored", name);
3798       *no_add_attrs = true;
3799     }
3800 
3801   return NULL_TREE;
3802 }
3803 
3804 /* Table of valid machine attributes.  */
3805 const struct attribute_spec ix86_attribute_table[] =
3806 {
3807   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
3808        affects_type_identity, handler, exclude } */
3809   /* Stdcall attribute says callee is responsible for popping arguments
3810      if they are not variable.  */
3811   { "stdcall",   0, 0, false, true,  true,  true, ix86_handle_cconv_attribute,
3812     NULL },
3813   /* Fastcall attribute says callee is responsible for popping arguments
3814      if they are not variable.  */
3815   { "fastcall",  0, 0, false, true,  true,  true, ix86_handle_cconv_attribute,
3816     NULL },
3817   /* Thiscall attribute says callee is responsible for popping arguments
3818      if they are not variable.  */
3819   { "thiscall",  0, 0, false, true,  true,  true, ix86_handle_cconv_attribute,
3820     NULL },
3821   /* Cdecl attribute says the callee is a normal C declaration */
3822   { "cdecl",     0, 0, false, true,  true,  true, ix86_handle_cconv_attribute,
3823     NULL },
3824   /* Regparm attribute specifies how many integer arguments are to be
3825      passed in registers.  */
3826   { "regparm",   1, 1, false, true,  true,  true, ix86_handle_cconv_attribute,
3827     NULL },
3828   /* Sseregparm attribute says we are using x86_64 calling conventions
3829      for FP arguments.  */
3830   { "sseregparm", 0, 0, false, true, true,  true, ix86_handle_cconv_attribute,
3831     NULL },
3832   /* The transactional memory builtins are implicitly regparm or fastcall
3833      depending on the ABI.  Override the generic do-nothing attribute that
3834      these builtins were declared with.  */
3835   { "*tm regparm", 0, 0, false, true, true, true,
3836     ix86_handle_tm_regparm_attribute, NULL },
3837   /* force_align_arg_pointer says this function realigns the stack at entry.  */
3838   { "force_align_arg_pointer", 0, 0,
3839     false, true,  true, false, ix86_handle_force_align_arg_pointer_attribute,
3840     NULL },
3841 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3842   { "dllimport", 0, 0, false, false, false, false, handle_dll_attribute,
3843     NULL },
3844   { "dllexport", 0, 0, false, false, false, false, handle_dll_attribute,
3845     NULL },
3846   { "shared",    0, 0, true,  false, false, false,
3847     ix86_handle_shared_attribute, NULL },
3848 #endif
3849   { "ms_struct", 0, 0, false, false,  false, false,
3850     ix86_handle_struct_attribute, NULL },
3851   { "gcc_struct", 0, 0, false, false,  false, false,
3852     ix86_handle_struct_attribute, NULL },
3853 #ifdef SUBTARGET_ATTRIBUTE_TABLE
3854   SUBTARGET_ATTRIBUTE_TABLE,
3855 #endif
3856   /* ms_abi and sysv_abi calling convention function attributes.  */
3857   { "ms_abi", 0, 0, false, true, true, true, ix86_handle_abi_attribute, NULL },
3858   { "sysv_abi", 0, 0, false, true, true, true, ix86_handle_abi_attribute,
3859     NULL },
3860   { "ms_abi va_list", 0, 0, false, false, false, false, NULL, NULL },
3861   { "sysv_abi va_list", 0, 0, false, false, false, false, NULL, NULL },
3862   { "ms_hook_prologue", 0, 0, true, false, false, false,
3863     ix86_handle_fndecl_attribute, NULL },
3864   { "callee_pop_aggregate_return", 1, 1, false, true, true, true,
3865     ix86_handle_callee_pop_aggregate_return, NULL },
3866   { "interrupt", 0, 0, false, true, true, false,
3867     ix86_handle_interrupt_attribute, NULL },
3868   { "no_caller_saved_registers", 0, 0, false, true, true, false,
3869     ix86_handle_no_caller_saved_registers_attribute, NULL },
3870   { "naked", 0, 0, true, false, false, false,
3871     ix86_handle_fndecl_attribute, NULL },
3872   { "indirect_branch", 1, 1, true, false, false, false,
3873     ix86_handle_fndecl_attribute, NULL },
3874   { "function_return", 1, 1, true, false, false, false,
3875     ix86_handle_fndecl_attribute, NULL },
3876   { "indirect_return", 0, 0, false, true, true, false,
3877     NULL, NULL },
3878   { "fentry_name", 1, 1, true, false, false, false,
3879     ix86_handle_fentry_name, NULL },
3880   { "fentry_section", 1, 1, true, false, false, false,
3881     ix86_handle_fentry_name, NULL },
3882   { "cf_check", 0, 0, true, false, false, false,
3883     ix86_handle_fndecl_attribute, NULL },
3884   { "nodirect_extern_access", 0, 0, true, false, false, false,
3885     handle_nodirect_extern_access_attribute, NULL },
3886 
3887   /* End element.  */
3888   { NULL, 0, 0, false, false, false, false, NULL, NULL }
3889 };
3890 
3891 #include "gt-i386-options.h"
3892