Debian Codegraph @

gcc/config/rs6000/rs6000-c.cc

raw
1 /* Subroutines for the C front end on the PowerPC architecture.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3
4 Contributed by Zack Weinberg <[email protected]>
5 and Paolo Bonzini <[email protected]>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published
11 by the Free Software Foundation; either version 3, or (at your
12 option) any later version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #define IN_TARGET_CODE 1
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "target.h"
29 #include "c-family/c-common.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "stor-layout.h"
34 #include "c-family/c-pragma.h"
35 #include "langhooks.h"
36 #include "c/c-tree.h"
37
38 #include "rs6000-internal.h"
39
40 /* Handle the machine specific pragma longcall. Its syntax is
41
42 # pragma longcall ( TOGGLE )
43
44 where TOGGLE is either 0 or 1.
45
46 rs6000_default_long_calls is set to the value of TOGGLE, changing
47 whether or not new function declarations receive a longcall
48 attribute by default. */
49
50 void
51 rs6000_pragma_longcall (cpp_reader *pfile ATTRIBUTE_UNUSED)
52 {
53 #define SYNTAX_ERROR(gmsgid) do { \
54 warning (OPT_Wpragmas, gmsgid); \
55 warning (OPT_Wpragmas, "ignoring malformed %<#pragma longcall%>"); \
56 return; \
57 } while (0)
58
59
60
61 tree x, n;
62
63 /* If we get here, generic code has already scanned the directive
64 leader and the word "longcall". */
65
66 if (pragma_lex (&x) != CPP_OPEN_PAREN)
67 SYNTAX_ERROR ("missing open paren");
68 if (pragma_lex (&n) != CPP_NUMBER)
69 SYNTAX_ERROR ("missing number");
70 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
71 SYNTAX_ERROR ("missing close paren");
72
73 if (n != integer_zero_node && n != integer_one_node)
74 SYNTAX_ERROR ("number must be 0 or 1");
75
76 if (pragma_lex (&x) != CPP_EOF)
77 warning (OPT_Wpragmas, "junk at end of %<#pragma longcall%>");
78
79 rs6000_default_long_calls = (n == integer_one_node);
80 }
81
82 /* Handle defining many CPP flags based on TARGET_xxx. As a general
83 policy, rather than trying to guess what flags a user might want a
84 #define for, it's better to define a flag for everything. */
85
86 #define builtin_define(TXT) cpp_define (pfile, TXT)
87 #define builtin_assert(TXT) cpp_assert (pfile, TXT)
88
89 /* Keep the AltiVec keywords handy for fast comparisons. */
90 static GTY(()) tree __vector_keyword;
91 static GTY(()) tree vector_keyword;
92 static GTY(()) tree __pixel_keyword;
93 static GTY(()) tree pixel_keyword;
94 static GTY(()) tree __bool_keyword;
95 static GTY(()) tree bool_keyword;
96 static GTY(()) tree _Bool_keyword;
97 static GTY(()) tree __int128_type;
98 static GTY(()) tree __uint128_type;
99
100 /* Preserved across calls. */
101 static tree expand_bool_pixel;
102
103 static cpp_hashnode *
104 altivec_categorize_keyword (const cpp_token *tok)
105 {
106 if (tok->type == CPP_NAME)
107 {
108 cpp_hashnode *ident = tok->val.node.node;
109
110 if (ident == C_CPP_HASHNODE (vector_keyword))
111 return C_CPP_HASHNODE (__vector_keyword);
112
113 if (ident == C_CPP_HASHNODE (pixel_keyword))
114 return C_CPP_HASHNODE (__pixel_keyword);
115
116 if (ident == C_CPP_HASHNODE (bool_keyword))
117 return C_CPP_HASHNODE (__bool_keyword);
118
119 if (ident == C_CPP_HASHNODE (_Bool_keyword))
120 return C_CPP_HASHNODE (__bool_keyword);
121
122 return ident;
123 }
124
125 return 0;
126 }
127
128 static void
129 init_vector_keywords (void)
130 {
131 /* Keywords without two leading underscores are context-sensitive, and hence
132 implemented as conditional macros, controlled by the
133 rs6000_macro_to_expand() function below. If we have ISA 2.07 64-bit
134 support, record the __int128_t and __uint128_t types. */
135
136 __vector_keyword = get_identifier ("__vector");
137 C_CPP_HASHNODE (__vector_keyword)->flags |= NODE_CONDITIONAL;
138
139 __pixel_keyword = get_identifier ("__pixel");
140 C_CPP_HASHNODE (__pixel_keyword)->flags |= NODE_CONDITIONAL;
141
142 __bool_keyword = get_identifier ("__bool");
143 C_CPP_HASHNODE (__bool_keyword)->flags |= NODE_CONDITIONAL;
144
145 vector_keyword = get_identifier ("vector");
146 C_CPP_HASHNODE (vector_keyword)->flags |= NODE_CONDITIONAL;
147
148 pixel_keyword = get_identifier ("pixel");
149 C_CPP_HASHNODE (pixel_keyword)->flags |= NODE_CONDITIONAL;
150
151 bool_keyword = get_identifier ("bool");
152 C_CPP_HASHNODE (bool_keyword)->flags |= NODE_CONDITIONAL;
153
154 _Bool_keyword = get_identifier ("_Bool");
155 C_CPP_HASHNODE (_Bool_keyword)->flags |= NODE_CONDITIONAL;
156
157 if (TARGET_VADDUQM)
158 {
159 __int128_type = get_identifier ("__int128_t");
160 __uint128_type = get_identifier ("__uint128_t");
161 }
162 }
163
164 /* Helper function to find out which RID_INT_N_* code is the one for
165 __int128, if any. Returns RID_MAX+1 if none apply, which is safe
166 (for our purposes, since we always expect to have __int128) to
167 compare against. */
168 static int
169 rid_int128(void)
170 {
171 int i;
172
173 for (i = 0; i < NUM_INT_N_ENTS; i ++)
174 if (int_n_enabled_p[i]
175 && int_n_data[i].bitsize == 128)
176 return RID_INT_N_0 + i;
177
178 return RID_MAX + 1;
179 }
180
181 /* Called to decide whether a conditional macro should be expanded
182 by peeking two or more tokens(_bool/_pixel/int/long/double/...). */
183
184 static cpp_hashnode *
185 rs6000_macro_to_expand (cpp_reader *pfile, const cpp_token *tok)
186 {
187 cpp_hashnode *expand_this = tok->val.node.node;
188 cpp_hashnode *ident;
189
190 /* If the current machine does not have altivec, don't look for the
191 keywords. */
192 if (!TARGET_ALTIVEC)
193 return NULL;
194
195 ident = altivec_categorize_keyword (tok);
196
197 if (ident != expand_this)
198 expand_this = NULL;
199
200 if (ident == C_CPP_HASHNODE (__vector_keyword))
201 {
202 int idx = 0;
203 do
204 tok = cpp_peek_token (pfile, idx++);
205 while (tok->type == CPP_PADDING);
206 ident = altivec_categorize_keyword (tok);
207
208 if (ident == C_CPP_HASHNODE (__pixel_keyword))
209 {
210 expand_this = C_CPP_HASHNODE (__vector_keyword);
211 expand_bool_pixel = __pixel_keyword;
212 }
213 else if (ident == C_CPP_HASHNODE (__bool_keyword))
214 {
215 expand_this = C_CPP_HASHNODE (__vector_keyword);
216 expand_bool_pixel = __bool_keyword;
217 }
218 /* The boost libraries have code with Iterator::vector vector in it. If
219 we allow the normal handling, this module will be called recursively,
220 and the vector will be skipped.; */
221 else if (ident && (ident != C_CPP_HASHNODE (__vector_keyword)))
222 {
223 enum rid rid_code = (enum rid)(ident->rid_code);
224 bool is_macro = cpp_macro_p (ident);
225
226 /* If there is a function-like macro, check if it is going to be
227 invoked with or without arguments. Without following ( treat
228 it like non-macro, otherwise the following cpp_get_token eats
229 what should be preserved. */
230 if (is_macro && cpp_fun_like_macro_p (ident))
231 {
232 int idx2 = idx;
233 do
234 tok = cpp_peek_token (pfile, idx2++);
235 while (tok->type == CPP_PADDING);
236 if (tok->type != CPP_OPEN_PAREN)
237 is_macro = false;
238 }
239
240 if (is_macro)
241 {
242 do
243 (void) cpp_get_token (pfile);
244 while (--idx > 0);
245 do
246 tok = cpp_peek_token (pfile, idx++);
247 while (tok->type == CPP_PADDING);
248 ident = altivec_categorize_keyword (tok);
249 if (ident == C_CPP_HASHNODE (__pixel_keyword))
250 {
251 expand_this = C_CPP_HASHNODE (__vector_keyword);
252 expand_bool_pixel = __pixel_keyword;
253 rid_code = RID_MAX;
254 }
255 else if (ident == C_CPP_HASHNODE (__bool_keyword))
256 {
257 expand_this = C_CPP_HASHNODE (__vector_keyword);
258 expand_bool_pixel = __bool_keyword;
259 rid_code = RID_MAX;
260 }
261 else if (ident)
262 rid_code = (enum rid)(ident->rid_code);
263 }
264
265 if (rid_code == RID_UNSIGNED || rid_code == RID_LONG
266 || rid_code == RID_SHORT || rid_code == RID_SIGNED
267 || rid_code == RID_INT || rid_code == RID_CHAR
268 || rid_code == RID_FLOAT
269 || (rid_code == RID_DOUBLE && TARGET_VSX)
270 || (rid_code == rid_int128 () && TARGET_VADDUQM))
271 {
272 expand_this = C_CPP_HASHNODE (__vector_keyword);
273 /* If the next keyword is bool or pixel, it
274 will need to be expanded as well. */
275 do
276 tok = cpp_peek_token (pfile, idx++);
277 while (tok->type == CPP_PADDING);
278 ident = altivec_categorize_keyword (tok);
279
280 if (ident == C_CPP_HASHNODE (__pixel_keyword))
281 expand_bool_pixel = __pixel_keyword;
282 else if (ident == C_CPP_HASHNODE (__bool_keyword))
283 expand_bool_pixel = __bool_keyword;
284
285 /* If there are more tokens to check. */
286 else if (ident)
287 {
288 /* Try two tokens down, too. */
289 do
290 tok = cpp_peek_token (pfile, idx++);
291 while (tok->type == CPP_PADDING);
292 ident = altivec_categorize_keyword (tok);
293 if (ident == C_CPP_HASHNODE (__pixel_keyword))
294 expand_bool_pixel = __pixel_keyword;
295 else if (ident == C_CPP_HASHNODE (__bool_keyword))
296 expand_bool_pixel = __bool_keyword;
297 }
298 }
299
300 /* Support vector __int128_t, but we don't need to worry about bool
301 or pixel on this type. */
302 else if (TARGET_VADDUQM
303 && (ident == C_CPP_HASHNODE (__int128_type)
304 || ident == C_CPP_HASHNODE (__uint128_type)))
305 expand_this = C_CPP_HASHNODE (__vector_keyword);
306 }
307 }
308 else if (expand_bool_pixel && ident == C_CPP_HASHNODE (__pixel_keyword))
309 {
310 expand_this = C_CPP_HASHNODE (__pixel_keyword);
311 expand_bool_pixel = 0;
312 }
313 else if (expand_bool_pixel && ident == C_CPP_HASHNODE (__bool_keyword))
314 {
315 expand_this = C_CPP_HASHNODE (__bool_keyword);
316 expand_bool_pixel = 0;
317 }
318
319 return expand_this;
320 }
321
322
323 /* Define or undefine a single macro. */
324
325 static void
326 rs6000_define_or_undefine_macro (bool define_p, const char *name)
327 {
328 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
329 fprintf (stderr, "#%s %s\n", (define_p) ? "define" : "undef", name);
330
331 if (define_p)
332 cpp_define (parse_in, name);
333 else
334 cpp_undef (parse_in, name);
335 }
336
337 /* Define or undefine macros based on the current target. If the user does
338 #pragma GCC target, we need to adjust the macros dynamically. */
339
340 void
341 rs6000_target_modify_macros (bool define_p, HOST_WIDE_INT flags)
342 {
343 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
344 fprintf (stderr,
345 "rs6000_target_modify_macros (%s, " HOST_WIDE_INT_PRINT_HEX ")\n",
346 (define_p) ? "define" : "undef",
347 flags);
348
349 /* Each of the flags mentioned below controls whether certain
350 preprocessor macros will be automatically defined when
351 preprocessing source files for compilation by this compiler.
352 While most of these flags can be enabled or disabled
353 explicitly by specifying certain command-line options when
354 invoking the compiler, there are also many ways in which these
355 flags are enabled or disabled implicitly, based on compiler
356 defaults, configuration choices, and on the presence of certain
357 related command-line options. Many, but not all, of these
358 implicit behaviors can be found in file "rs6000.cc", the
359 rs6000_option_override_internal() function.
360
361 In general, each of the flags may be automatically enabled in
362 any of the following conditions:
363
364 1. If no -mcpu target is specified on the command line and no
365 --with-cpu target is specified to the configure command line
366 and the TARGET_DEFAULT macro for this default cpu host
367 includes the flag, and the flag has not been explicitly disabled
368 by command-line options.
369
370 2. If the target specified with -mcpu=target on the command line, or
371 in the absence of a -mcpu=target command-line option, if the
372 target specified using --with-cpu=target on the configure
373 command line, is disqualified because the associated binary
374 tools (e.g. the assembler) lack support for the requested cpu,
375 and the TARGET_DEFAULT macro for this default cpu host
376 includes the flag, and the flag has not been explicitly disabled
377 by command-line options.
378
379 3. If either of the above two conditions apply except that the
380 TARGET_DEFAULT macro is defined to equal zero, and
381 TARGET_POWERPC64 and
382 a) BYTES_BIG_ENDIAN and the flag to be enabled is either
383 OPTION_MASK_PPC_GFXOPT or MASK_POWERPC64 (flags for "powerpc64"
384 target), or
385 b) !BYTES_BIG_ENDIAN and the flag to be enabled is either
386 MASK_POWERPC64 or it is one of the flags included in
387 ISA_2_7_MASKS_SERVER (flags for "powerpc64le" target).
388
389 4. If a cpu has been requested with a -mcpu=target command-line option
390 and this cpu has not been disqualified due to shortcomings of the
391 binary tools, and the set of flags associated with the requested cpu
392 include the flag to be enabled. See rs6000-cpus.def for macro
393 definitions that represent various ABI standards
394 (e.g. ISA_2_1_MASKS, ISA_3_0_MASKS_SERVER) and for a list of
395 the specific flags that are associated with each of the cpu
396 choices that can be specified as the target of a -mcpu=target
397 compile option, or as the target of a --with-cpu=target
398 configure option. Target flags that are specified in either
399 of these two ways are considered "implicit" since the flags
400 are not mentioned specifically by name.
401
402 Additional documentation describing behavior specific to
403 particular flags is provided below, immediately preceding the
404 use of each relevant flag.
405
406 5. If there is no -mcpu=target command-line option, and the cpu
407 requested by a --with-cpu=target command-line option has not
408 been disqualified due to shortcomings of the binary tools, and
409 the set of flags associated with the specified target include
410 the flag to be enabled. See the notes immediately above for a
411 summary of the flags associated with particular cpu
412 definitions. */
413
414 /* rs6000_isa_flags based options. */
415 rs6000_define_or_undefine_macro (define_p, "_ARCH_PPC");
416 if ((flags & OPTION_MASK_PPC_GPOPT) != 0)
417 rs6000_define_or_undefine_macro (define_p, "_ARCH_PPCSQ");
418 if ((flags & OPTION_MASK_PPC_GFXOPT) != 0)
419 rs6000_define_or_undefine_macro (define_p, "_ARCH_PPCGR");
420 if ((flags & OPTION_MASK_POWERPC64) != 0)
421 rs6000_define_or_undefine_macro (define_p, "_ARCH_PPC64");
422 if ((flags & OPTION_MASK_MFCRF) != 0)
423 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR4");
424 if ((flags & OPTION_MASK_POPCNTB) != 0)
425 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR5");
426 if ((flags & OPTION_MASK_FPRND) != 0)
427 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR5X");
428 if ((flags & OPTION_MASK_CMPB) != 0)
429 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR6");
430 if ((flags & OPTION_MASK_POPCNTD) != 0)
431 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR7");
432 if ((flags & OPTION_MASK_POWER8) != 0)
433 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR8");
434 if ((flags & OPTION_MASK_MODULO) != 0)
435 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR9");
436 if ((flags & OPTION_MASK_POWER10) != 0)
437 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR10");
438 if ((flags & OPTION_MASK_POWER11) != 0)
439 rs6000_define_or_undefine_macro (define_p, "_ARCH_PWR11");
440 if ((flags & OPTION_MASK_SOFT_FLOAT) != 0)
441 rs6000_define_or_undefine_macro (define_p, "_SOFT_FLOAT");
442 if ((flags & OPTION_MASK_RECIP_PRECISION) != 0)
443 rs6000_define_or_undefine_macro (define_p, "__RECIP_PRECISION__");
444 /* Note that the OPTION_MASK_ALTIVEC flag is automatically turned on
445 in any of the following conditions:
446 1. The operating system is Darwin and it is configured for 64
447 bit. (See darwin_rs6000_override_options.)
448 2. The operating system is Darwin and the operating system
449 version is 10.5 or higher and the user has not explicitly
450 disabled ALTIVEC by specifying -mcpu=G3 or -mno-altivec and
451 the compiler is not producing code for integration within the
452 kernel. (See darwin_rs6000_override_options.)
453 Note that the OPTION_MASK_ALTIVEC flag is automatically turned
454 off in any of the following conditions:
455 1. The operating system does not support saving of AltiVec
456 registers (OS_MISSING_ALTIVEC).
457 2. If an inner context (as introduced by
458 __attribute__((__target__())) or #pragma GCC target()
459 requests a target that normally enables the
460 OPTION_MASK_ALTIVEC flag but the outer-most "main target"
461 does not support the rs6000_altivec_abi, this flag is
462 turned off for the inner context unless OPTION_MASK_ALTIVEC
463 was explicitly enabled for the inner context. */
464 if ((flags & OPTION_MASK_ALTIVEC) != 0)
465 {
466 const char *vec_str = (define_p) ? "__VEC__=10206" : "__VEC__";
467 rs6000_define_or_undefine_macro (define_p, "__ALTIVEC__");
468 rs6000_define_or_undefine_macro (define_p, vec_str);
469
470 /* Define this when supporting context-sensitive keywords. */
471 if (!flag_iso)
472 rs6000_define_or_undefine_macro (define_p, "__APPLE_ALTIVEC__");
473 if (rs6000_aix_extabi)
474 rs6000_define_or_undefine_macro (define_p, "__EXTABI__");
475 }
476 /* Note that the OPTION_MASK_VSX flag is automatically turned on in
477 the following conditions:
478 1. TARGET_P8_VECTOR is explicitly turned on and the OPTION_MASK_VSX
479 was not explicitly turned off. Hereafter, the OPTION_MASK_VSX
480 flag is considered to have been explicitly turned on.
481 Note that the OPTION_MASK_VSX flag is automatically turned off in
482 the following conditions:
483 1. The operating system does not support saving of AltiVec
484 registers (OS_MISSING_ALTIVEC).
485 2. If the option TARGET_HARD_FLOAT is turned off. Hereafter, the
486 OPTION_MASK_VSX flag is considered to have been turned off
487 explicitly.
488 3. If TARGET_AVOID_XFORM is turned on explicitly at the outermost
489 compilation context, or if it is turned on by any means in an
490 inner compilation context. Hereafter, the OPTION_MASK_VSX
491 flag is considered to have been turned off explicitly.
492 4. If TARGET_ALTIVEC was explicitly disabled. Hereafter, the
493 OPTION_MASK_VSX flag is considered to have been turned off
494 explicitly.
495 5. If an inner context (as introduced by
496 __attribute__((__target__())) or #pragma GCC target()
497 requests a target that normally enables the
498 OPTION_MASK_VSX flag but the outer-most "main target"
499 does not support the rs6000_altivec_abi, this flag is
500 turned off for the inner context unless OPTION_MASK_VSX
501 was explicitly enabled for the inner context. */
502 if ((flags & OPTION_MASK_VSX) != 0)
503 rs6000_define_or_undefine_macro (define_p, "__VSX__");
504 if ((flags & OPTION_MASK_HTM) != 0)
505 {
506 rs6000_define_or_undefine_macro (define_p, "__HTM__");
507 /* Tell the user that our HTM insn patterns act as memory barriers. */
508 rs6000_define_or_undefine_macro (define_p, "__TM_FENCE__");
509 }
510 /* Note that the OPTION_MASK_P8_VECTOR flag is automatically turned
511 on in the following conditions:
512 1. TARGET_P9_VECTOR is explicitly turned on and
513 OPTION_MASK_P8_VECTOR is not explicitly turned off.
514 Hereafter, the OPTION_MASK_P8_VECTOR flag is considered to
515 have been turned off explicitly.
516 Note that the OPTION_MASK_P8_VECTOR flag is automatically turned
517 off in the following conditions:
518 1. If any of TARGET_HARD_FLOAT, TARGET_ALTIVEC, or TARGET_VSX
519 were turned off explicitly and OPTION_MASK_P8_VECTOR flag was
520 not turned on explicitly.
521 2. If TARGET_ALTIVEC is turned off. Hereafter, the
522 OPTION_MASK_P8_VECTOR flag is considered to have been turned off
523 explicitly.
524 3. If TARGET_VSX is turned off and OPTION_MASK_P8_VECTOR was not
525 explicitly enabled. If TARGET_VSX is explicitly enabled, the
526 OPTION_MASK_P8_VECTOR flag is hereafter also considered to
527 have been turned off explicitly. */
528 if ((flags & OPTION_MASK_P8_VECTOR) != 0)
529 rs6000_define_or_undefine_macro (define_p, "__POWER8_VECTOR__");
530 /* Note that the OPTION_MASK_P9_VECTOR flag is automatically turned
531 off in the following conditions:
532 1. If TARGET_P8_VECTOR is turned off and OPTION_MASK_P9_VECTOR is
533 not turned on explicitly. Hereafter, if OPTION_MASK_P8_VECTOR
534 was turned on explicitly, the OPTION_MASK_P9_VECTOR flag is
535 also considered to have been turned off explicitly.
536 Note that the OPTION_MASK_P9_VECTOR is automatically turned on
537 in the following conditions:
538 1. If TARGET_P9_MINMAX was turned on explicitly.
539 Hereafter, THE OPTION_MASK_P9_VECTOR flag is considered to
540 have been turned on explicitly. */
541 if ((flags & OPTION_MASK_P9_VECTOR) != 0)
542 rs6000_define_or_undefine_macro (define_p, "__POWER9_VECTOR__");
543 /* Note that the OPTION_MASK_QUAD_MEMORY flag is automatically
544 turned off in the following conditions:
545 1. If TARGET_POWERPC64 is turned off.
546 2. If WORDS_BIG_ENDIAN is false (non-atomic quad memory
547 load/store are disabled on little endian). */
548 if ((flags & OPTION_MASK_QUAD_MEMORY) != 0)
549 rs6000_define_or_undefine_macro (define_p, "__QUAD_MEMORY__");
550 /* Note that the OPTION_MASK_QUAD_MEMORY_ATOMIC flag is automatically
551 turned off in the following conditions:
552 1. If TARGET_POWERPC64 is turned off.
553 Note that the OPTION_MASK_QUAD_MEMORY_ATOMIC flag is
554 automatically turned on in the following conditions:
555 1. If TARGET_QUAD_MEMORY and this flag was not explicitly
556 disabled. */
557 if ((flags & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
558 rs6000_define_or_undefine_macro (define_p, "__QUAD_MEMORY_ATOMIC__");
559 /* Note that the OPTION_MASK_CRYPTO flag is automatically turned off
560 in the following conditions:
561 1. If any of TARGET_HARD_FLOAT or TARGET_ALTIVEC or TARGET_VSX
562 are turned off explicitly and OPTION_MASK_CRYPTO is not turned
563 on explicitly.
564 2. If TARGET_ALTIVEC is turned off. */
565 if ((flags & OPTION_MASK_CRYPTO) != 0)
566 rs6000_define_or_undefine_macro (define_p, "__CRYPTO__");
567 if ((flags & OPTION_MASK_FLOAT128_KEYWORD) != 0)
568 {
569 rs6000_define_or_undefine_macro (define_p, "__FLOAT128__");
570 if (define_p)
571 rs6000_define_or_undefine_macro (true, "__float128=__ieee128");
572 else
573 rs6000_define_or_undefine_macro (false, "__float128");
574 if (ieee128_float_type_node && define_p)
575 rs6000_define_or_undefine_macro (true, "__SIZEOF_FLOAT128__=16");
576 else
577 rs6000_define_or_undefine_macro (false, "__SIZEOF_FLOAT128__");
578 }
579 /* OPTION_MASK_FLOAT128_HARDWARE can be turned on if -mcpu=power9 is used or
580 via the target attribute/pragma. */
581 if ((flags & OPTION_MASK_FLOAT128_HW) != 0)
582 rs6000_define_or_undefine_macro (define_p, "__FLOAT128_HARDWARE__");
583
584 /* Tell the user if we are targeting CELL. */
585 if (rs6000_cpu == PROCESSOR_CELL)
586 rs6000_define_or_undefine_macro (define_p, "__PPU__");
587
588 /* Tell the user if we support the MMA instructions. */
589 if ((flags & OPTION_MASK_MMA) != 0)
590 rs6000_define_or_undefine_macro (define_p, "__MMA__");
591 /* Whether pc-relative code is being generated. */
592 if ((flags & OPTION_MASK_PCREL) != 0)
593 rs6000_define_or_undefine_macro (define_p, "__PCREL__");
594 /* Tell the user -mrop-protect is in play. */
595 if (rs6000_rop_protect)
596 rs6000_define_or_undefine_macro (define_p, "__ROP_PROTECT__");
597 /* Tell the user __builtin_set_fpscr_rn now returns the FPSCR fields
598 in a double. Originally the built-in returned void. */
599 if ((flags & OPTION_MASK_SOFT_FLOAT) == 0)
600 rs6000_define_or_undefine_macro (define_p,
601 "__SET_FPSCR_RN_RETURNS_FPSCR__");
602 }
603
604 void
605 rs6000_cpu_cpp_builtins (cpp_reader *pfile)
606 {
607 /* Define all of the common macros. */
608 rs6000_target_modify_macros (true, rs6000_isa_flags);
609
610 if (TARGET_FRE)
611 builtin_define ("__RECIP__");
612 if (TARGET_FRES)
613 builtin_define ("__RECIPF__");
614 if (TARGET_FRSQRTE)
615 builtin_define ("__RSQRTE__");
616 if (TARGET_FRSQRTES)
617 builtin_define ("__RSQRTEF__");
618 if (TARGET_FLOAT128_TYPE)
619 builtin_define ("__FLOAT128_TYPE__");
620 if (ibm128_float_type_node)
621 builtin_define ("__SIZEOF_IBM128__=16");
622 if (ieee128_float_type_node)
623 builtin_define ("__SIZEOF_IEEE128__=16");
624 #ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
625 builtin_define ("__BUILTIN_CPU_SUPPORTS__");
626 #endif
627
628 if (TARGET_EXTRA_BUILTINS && cpp_get_options (pfile)->lang != CLK_ASM)
629 {
630 /* Define the AltiVec syntactic elements. */
631 builtin_define ("__vector=__attribute__((altivec(vector__)))");
632 builtin_define ("__pixel=__attribute__((altivec(pixel__))) unsigned short");
633 builtin_define ("__bool=__attribute__((altivec(bool__))) unsigned");
634
635 if (!flag_iso)
636 {
637 builtin_define ("vector=vector");
638 builtin_define ("pixel=pixel");
639 builtin_define ("bool=bool");
640 builtin_define ("_Bool=_Bool");
641 init_vector_keywords ();
642
643 /* Enable context-sensitive macros. */
644 cpp_get_callbacks (pfile)->macro_to_expand = rs6000_macro_to_expand;
645 }
646 }
647 if (!TARGET_HARD_FLOAT)
648 builtin_define ("_SOFT_DOUBLE");
649 /* Used by lwarx/stwcx. errata work-around. */
650 if (rs6000_cpu == PROCESSOR_PPC405)
651 builtin_define ("__PPC405__");
652 /* Used by libstdc++. */
653 if (TARGET_NO_LWSYNC)
654 builtin_define ("__NO_LWSYNC__");
655
656 if (TARGET_EXTRA_BUILTINS)
657 {
658 /* For the VSX builtin functions identical to Altivec functions, just map
659 the altivec builtin into the vsx version (the altivec functions
660 generate VSX code if -mvsx). */
661 builtin_define ("__builtin_vsx_xxland=__builtin_vec_and");
662 builtin_define ("__builtin_vsx_xxlandc=__builtin_vec_andc");
663 builtin_define ("__builtin_vsx_xxlnor=__builtin_vec_nor");
664 builtin_define ("__builtin_vsx_xxlor=__builtin_vec_or");
665 builtin_define ("__builtin_vsx_xxlxor=__builtin_vec_xor");
666 builtin_define ("__builtin_vsx_xxsel=__builtin_vec_sel");
667 builtin_define ("__builtin_vsx_vperm=__builtin_vec_perm");
668
669 /* Also map the a and m versions of the multiply/add instructions to the
670 builtin for people blindly going off the instruction manual. */
671 builtin_define ("__builtin_vsx_xvmaddadp=__builtin_vsx_xvmadddp");
672 builtin_define ("__builtin_vsx_xvmaddmdp=__builtin_vsx_xvmadddp");
673 builtin_define ("__builtin_vsx_xvmaddasp=__builtin_vsx_xvmaddsp");
674 builtin_define ("__builtin_vsx_xvmaddmsp=__builtin_vsx_xvmaddsp");
675 builtin_define ("__builtin_vsx_xvmsubadp=__builtin_vsx_xvmsubdp");
676 builtin_define ("__builtin_vsx_xvmsubmdp=__builtin_vsx_xvmsubdp");
677 builtin_define ("__builtin_vsx_xvmsubasp=__builtin_vsx_xvmsubsp");
678 builtin_define ("__builtin_vsx_xvmsubmsp=__builtin_vsx_xvmsubsp");
679 builtin_define ("__builtin_vsx_xvnmaddadp=__builtin_vsx_xvnmadddp");
680 builtin_define ("__builtin_vsx_xvnmaddmdp=__builtin_vsx_xvnmadddp");
681 builtin_define ("__builtin_vsx_xvnmaddasp=__builtin_vsx_xvnmaddsp");
682 builtin_define ("__builtin_vsx_xvnmaddmsp=__builtin_vsx_xvnmaddsp");
683 builtin_define ("__builtin_vsx_xvnmsubadp=__builtin_vsx_xvnmsubdp");
684 builtin_define ("__builtin_vsx_xvnmsubmdp=__builtin_vsx_xvnmsubdp");
685 builtin_define ("__builtin_vsx_xvnmsubasp=__builtin_vsx_xvnmsubsp");
686 builtin_define ("__builtin_vsx_xvnmsubmsp=__builtin_vsx_xvnmsubsp");
687 }
688
689 /* Map the old _Float128 'q' builtins into the new 'f128' builtins. */
690 if (TARGET_FLOAT128_TYPE)
691 {
692 builtin_define ("__builtin_fabsq=__builtin_fabsf128");
693 builtin_define ("__builtin_copysignq=__builtin_copysignf128");
694 builtin_define ("__builtin_nanq=__builtin_nanf128");
695 builtin_define ("__builtin_nansq=__builtin_nansf128");
696 builtin_define ("__builtin_infq=__builtin_inff128");
697 builtin_define ("__builtin_huge_valq=__builtin_huge_valf128");
698 }
699
700 /* Tell users they can use __builtin_bswap{16,64}. */
701 builtin_define ("__HAVE_BSWAP__");
702
703 /* May be overridden by target configuration. */
704 RS6000_CPU_CPP_ENDIAN_BUILTINS();
705
706 if (TARGET_LONG_DOUBLE_128)
707 {
708 builtin_define ("__LONG_DOUBLE_128__");
709 builtin_define ("__LONGDOUBLE128");
710
711 if (TARGET_IEEEQUAD)
712 {
713 /* Older versions of GLIBC used __attribute__((__KC__)) to create the
714 IEEE 128-bit floating point complex type for C++ (which does not
715 support _Float128 _Complex). If the default for long double is
716 IEEE 128-bit mode, the library would need to use
717 __attribute__((__TC__)) instead. Defining __KF__ and __KC__
718 is a stop-gap to build with the older libraries, until we
719 get an updated library. */
720 builtin_define ("__LONG_DOUBLE_IEEE128__");
721 builtin_define ("__KF__=__TF__");
722 builtin_define ("__KC__=__TC__");
723 }
724 else
725 builtin_define ("__LONG_DOUBLE_IBM128__");
726 }
727
728 switch (TARGET_CMODEL)
729 {
730 /* Deliberately omit __CMODEL_SMALL__ since that was the default
731 before --mcmodel support was added. */
732 case CMODEL_MEDIUM:
733 builtin_define ("__CMODEL_MEDIUM__");
734 break;
735 case CMODEL_LARGE:
736 builtin_define ("__CMODEL_LARGE__");
737 break;
738 default:
739 break;
740 }
741
742 switch (rs6000_current_abi)
743 {
744 case ABI_V4:
745 builtin_define ("_CALL_SYSV");
746 break;
747 case ABI_AIX:
748 builtin_define ("_CALL_AIXDESC");
749 builtin_define ("_CALL_AIX");
750 builtin_define ("_CALL_ELF=1");
751 break;
752 case ABI_ELFv2:
753 builtin_define ("_CALL_ELF=2");
754 break;
755 case ABI_DARWIN:
756 builtin_define ("_CALL_DARWIN");
757 break;
758 default:
759 break;
760 }
761
762 /* Vector element order. */
763 if (BYTES_BIG_ENDIAN)
764 builtin_define ("__VEC_ELEMENT_REG_ORDER__=__ORDER_BIG_ENDIAN__");
765 else
766 builtin_define ("__VEC_ELEMENT_REG_ORDER__=__ORDER_LITTLE_ENDIAN__");
767
768 /* Let the compiled code know if 'f' class registers will not be available. */
769 if (TARGET_SOFT_FLOAT)
770 builtin_define ("__NO_FPRS__");
771
772 /* Whether aggregates passed by value are aligned to a 16 byte boundary
773 if their alignment is 16 bytes or larger. */
774 if ((TARGET_MACHO && rs6000_darwin64_abi)
775 || DEFAULT_ABI == ABI_ELFv2
776 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
777 builtin_define ("__STRUCT_PARM_ALIGN__=16");
778 }
779
780
781
782 /* Convert a type stored into a struct altivec_builtin_types as ID,
783 into a tree. The types are in rs6000_builtin_types: negative values
784 create a pointer type for the type associated to ~ID. Note it is
785 a logical NOT, rather than a negation, otherwise you cannot represent
786 a pointer type for ID 0. */
787
788 static inline tree
789 rs6000_builtin_type (int id)
790 {
791 tree t;
792 t = rs6000_builtin_types[id < 0 ? ~id : id];
793 return id < 0 ? build_pointer_type (t) : t;
794 }
795
796 /* Check whether the type of an argument, T, is compatible with a type ID
797 stored into a struct altivec_builtin_types. Integer types are considered
798 compatible; otherwise, the language hook lang_hooks.types_compatible_p makes
799 the decision. Also allow long double and _Float128 to be compatible if
800 -mabi=ieeelongdouble. */
801
802 static inline bool
803 is_float128_p (tree t)
804 {
805 return (t == float128_type_node
806 || (t && t == float128t_type_node)
807 || (TARGET_IEEEQUAD
808 && TARGET_LONG_DOUBLE_128
809 && t == long_double_type_node));
810 }
811
812
813 /* Return true iff ARGTYPE can be compatibly passed as PARMTYPE. */
814 static bool
815 rs6000_builtin_type_compatible (tree parmtype, tree argtype)
816 {
817 if (parmtype == error_mark_node)
818 return false;
819
820 if (INTEGRAL_TYPE_P (parmtype) && INTEGRAL_TYPE_P (argtype))
821 return true;
822
823 if (TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
824 && is_float128_p (parmtype) && is_float128_p (argtype))
825 return true;
826
827 if (POINTER_TYPE_P (parmtype) && POINTER_TYPE_P (argtype))
828 {
829 parmtype = TREE_TYPE (parmtype);
830 argtype = TREE_TYPE (argtype);
831 if (TYPE_READONLY (argtype))
832 parmtype = build_qualified_type (parmtype, TYPE_QUAL_CONST);
833 }
834
835 return lang_hooks.types_compatible_p (parmtype, argtype);
836 }
837
838 /* Return fold_convert (TYPE, EXPR) for C and convert (TYPE, EXPR)
839 for C++. The latter is needed because resolve_overloaded_builtin
840 can be called when parsing templates too if they don't have type
841 dependent operands, but the nested trees might not be usable in
842 GENERIC folding. */
843
844 static tree
845 c_fold_convert (tree type, tree expr)
846 {
847 return c_dialect_cxx () ? convert (type, expr) : fold_convert (type, expr);
848 }
849
850 /* Similar wrapper for fold_build2_loc. For C++ just call build2_loc. */
851
852 static tree
853 c_fold_build2_loc (location_t loc, enum tree_code code, tree type, tree arg0,
854 tree arg1)
855 {
856 if (!c_dialect_cxx ())
857 return fold_build2_loc (loc, code, type, arg0, arg1);
858 else if (loc != UNKNOWN_LOCATION)
859 return build2_loc (loc, code, type, arg0, arg1);
860 else
861 return build2 (code, type, arg0, arg1);
862 }
863
864 /* In addition to calling c_fold_convert for EXPR of type TYPE, also
865 call c_fully_fold to remove any C_MAYBE_CONST_EXPRs that could be
866 hiding there (PR47197). */
867
868 static tree
869 fully_fold_convert (tree type, tree expr)
870 {
871 tree result = c_fold_convert (type, expr);
872 bool maybe_const = true;
873
874 if (!c_dialect_cxx ())
875 result = c_fully_fold (result, false, &maybe_const);
876
877 return result;
878 }
879
880 /* Build a tree for a function call to an Altivec non-overloaded builtin.
881 The overloaded builtin that matched the types and args is described
882 by DESC. The N arguments are given in ARGS, respectively.
883
884 Actually the only thing it does is calling c_fold_convert on ARGS, with
885 a small exception for vec_{all,any}_{ge,le} predicates. */
886
887 static tree
888 altivec_build_resolved_builtin (tree *args, int n, tree fntype, tree ret_type,
889 rs6000_gen_builtins bif_id,
890 rs6000_gen_builtins ovld_id)
891 {
892 tree argtypes = TYPE_ARG_TYPES (fntype);
893 tree arg_type[MAX_OVLD_ARGS];
894 tree fndecl = rs6000_builtin_decls[bif_id];
895
896 for (int i = 0; i < n; i++)
897 {
898 arg_type[i] = TREE_VALUE (argtypes);
899 argtypes = TREE_CHAIN (argtypes);
900 }
901
902 /* The AltiVec overloading implementation is overall gross, but this
903 is particularly disgusting. The vec_{all,any}_{ge,le} builtins
904 are completely different for floating-point vs. integer vector
905 types, because the former has vcmpgefp, but the latter should use
906 vcmpgtXX.
907
908 In practice, the second and third arguments are swapped, and the
909 condition (LT vs. EQ, which is recognizable by bit 1 of the first
910 argument) is reversed. Patch the arguments here before building
911 the resolved CALL_EXPR. */
912 if (n == 3
913 && ovld_id == RS6000_OVLD_VEC_CMPGE_P
914 && bif_id != RS6000_BIF_VCMPGEFP_P
915 && bif_id != RS6000_BIF_XVCMPGEDP_P)
916 {
917 std::swap (args[1], args[2]);
918 std::swap (arg_type[1], arg_type[2]);
919
920 args[0] = c_fold_build2_loc (UNKNOWN_LOCATION, BIT_XOR_EXPR,
921 TREE_TYPE (args[0]), args[0],
922 build_int_cst (NULL_TREE, 2));
923 }
924
925 for (int j = 0; j < n; j++)
926 args[j] = fully_fold_convert (arg_type[j], args[j]);
927
928 /* If the number of arguments to an overloaded function increases,
929 we must expand this switch. */
930 gcc_assert (MAX_OVLD_ARGS <= 4);
931
932 tree call;
933 switch (n)
934 {
935 case 0:
936 call = build_call_expr (fndecl, 0);
937 break;
938 case 1:
939 call = build_call_expr (fndecl, 1, args[0]);
940 break;
941 case 2:
942 call = build_call_expr (fndecl, 2, args[0], args[1]);
943 break;
944 case 3:
945 call = build_call_expr (fndecl, 3, args[0], args[1], args[2]);
946 break;
947 case 4:
948 call = build_call_expr (fndecl, 4, args[0], args[1], args[2], args[3]);
949 break;
950 default:
951 gcc_unreachable ();
952 }
953 return c_fold_convert (ret_type, call);
954 }
955
956 /* Enumeration of possible results from attempted overload resolution.
957 This is used by special-case helper functions to tell their caller
958 whether they succeeded and what still needs to be done.
959
960 unresolved = Still needs processing
961 resolved = Resolved (but may be an error_mark_node)
962 resolved_bad = An error that needs handling by the caller. */
963
964 enum resolution { unresolved, resolved, resolved_bad };
965
966 /* Resolve an overloaded vec_mul call and return a tree expression for the
967 resolved call if successful. ARGS contains the arguments to the call.
968 TYPES contains their types. RES must be set to indicate the status of
969 the resolution attempt. LOC contains statement location information. */
970
971 static tree
972 resolve_vec_mul (resolution *res, tree *args, tree *types, location_t loc)
973 {
974 /* vec_mul needs to be special cased because there are no instructions for it
975 for the {un}signed char, {un}signed short, and {un}signed int types. */
976
977 /* Both arguments must be vectors and the types must be compatible. */
978 if (TREE_CODE (types[0]) != VECTOR_TYPE
979 || !lang_hooks.types_compatible_p (types[0], types[1]))
980 {
981 *res = resolved_bad;
982 return error_mark_node;
983 }
984
985 switch (TYPE_MODE (TREE_TYPE (types[0])))
986 {
987 case E_QImode:
988 case E_HImode:
989 case E_SImode:
990 case E_DImode:
991 case E_TImode:
992 /* For scalar types just use a multiply expression. */
993 *res = resolved;
994 return c_fold_build2_loc (loc, MULT_EXPR, types[0], args[0],
995 c_fold_convert (types[0], args[1]));
996 case E_SFmode:
997 {
998 /* For floats use the xvmulsp instruction directly. */
999 *res = resolved;
1000 tree call = rs6000_builtin_decls[RS6000_BIF_XVMULSP];
1001 return build_call_expr (call, 2, args[0], args[1]);
1002 }
1003 case E_DFmode:
1004 {
1005 /* For doubles use the xvmuldp instruction directly. */
1006 *res = resolved;
1007 tree call = rs6000_builtin_decls[RS6000_BIF_XVMULDP];
1008 return build_call_expr (call, 2, args[0], args[1]);
1009 }
1010 /* Other types are errors. */
1011 default:
1012 *res = resolved_bad;
1013 return error_mark_node;
1014 }
1015 }
1016
1017 /* Resolve an overloaded vec_cmpne call and return a tree expression for the
1018 resolved call if successful. ARGS contains the arguments to the call.
1019 TYPES contains their types. RES must be set to indicate the status of
1020 the resolution attempt. LOC contains statement location information. */
1021
1022 static tree
1023 resolve_vec_cmpne (resolution *res, tree *args, tree *types, location_t loc)
1024 {
1025 /* vec_cmpne needs to be special cased because there are no instructions
1026 for it (prior to power 9). */
1027
1028 /* Both arguments must be vectors and the types must be compatible. */
1029 if (TREE_CODE (types[0]) != VECTOR_TYPE
1030 || !lang_hooks.types_compatible_p (types[0], types[1]))
1031 {
1032 *res = resolved_bad;
1033 return error_mark_node;
1034 }
1035
1036 machine_mode arg0_elt_mode = TYPE_MODE (TREE_TYPE (types[0]));
1037
1038 /* Power9 instructions provide the most efficient implementation of
1039 ALTIVEC_BUILTIN_VEC_CMPNE if the mode is not DImode or TImode
1040 or SFmode or DFmode. */
1041 if (!TARGET_P9_VECTOR
1042 || arg0_elt_mode == DImode
1043 || arg0_elt_mode == TImode
1044 || arg0_elt_mode == SFmode
1045 || arg0_elt_mode == DFmode)
1046 {
1047 switch (arg0_elt_mode)
1048 {
1049 /* vec_cmpneq (va, vb) == vec_nor (vec_cmpeq (va, vb),
1050 vec_cmpeq (va, vb)). */
1051 /* Note: vec_nand also works but opt changes vec_nand's
1052 to vec_nor's anyway. */
1053 case E_QImode:
1054 case E_HImode:
1055 case E_SImode:
1056 case E_DImode:
1057 case E_TImode:
1058 case E_SFmode:
1059 case E_DFmode:
1060 {
1061 /* call = vec_cmpeq (va, vb)
1062 result = vec_nor (call, call). */
1063 vec<tree, va_gc> *params = make_tree_vector ();
1064 vec_safe_push (params, args[0]);
1065 vec_safe_push (params, args[1]);
1066 tree decl = rs6000_builtin_decls[RS6000_OVLD_VEC_CMPEQ];
1067 tree call = altivec_resolve_overloaded_builtin (loc, decl, params);
1068 /* Use save_expr to ensure that operands used more than once
1069 that may have side effects (like calls) are only evaluated
1070 once. */
1071 call = save_expr (call);
1072 params = make_tree_vector ();
1073 vec_safe_push (params, call);
1074 vec_safe_push (params, call);
1075 decl = rs6000_builtin_decls[RS6000_OVLD_VEC_NOR];
1076 *res = resolved;
1077 return altivec_resolve_overloaded_builtin (loc, decl, params);
1078 }
1079 /* Other types are errors. */
1080 default:
1081 *res = resolved_bad;
1082 return error_mark_node;
1083 }
1084 }
1085
1086 /* Otherwise this call is unresolved, and altivec_resolve_overloaded_builtin
1087 will later process the Power9 alternative. */
1088 *res = unresolved;
1089 return error_mark_node;
1090 }
1091
1092 /* Resolve an overloaded vec_adde or vec_sube call and return a tree expression
1093 for the resolved call if successful. ARGS contains the arguments to the
1094 call. TYPES contains their arguments. RES must be set to indicate the
1095 status of the resolution attempt. LOC contains statement location
1096 information. */
1097
1098 static tree
1099 resolve_vec_adde_sube (resolution *res, rs6000_gen_builtins fcode,
1100 tree *args, tree *types, location_t loc)
1101 {
1102 /* vec_adde needs to be special cased because there is no instruction
1103 for the {un}signed int version. */
1104
1105 /* All 3 arguments must be vectors of (signed or unsigned) (int or
1106 __int128) and the types must be compatible. */
1107 if (TREE_CODE (types[0]) != VECTOR_TYPE
1108 || !lang_hooks.types_compatible_p (types[0], types[1])
1109 || !lang_hooks.types_compatible_p (types[1], types[2]))
1110 {
1111 *res = resolved_bad;
1112 return error_mark_node;
1113 }
1114
1115 switch (TYPE_MODE (TREE_TYPE (types[0])))
1116 {
1117 /* For {un}signed ints,
1118 vec_adde (va, vb, carryv) == vec_add (vec_add (va, vb),
1119 vec_and (carryv, 1)).
1120 vec_sube (va, vb, carryv) == vec_sub (vec_sub (va, vb),
1121 vec_and (carryv, 1)). */
1122 case E_SImode:
1123 {
1124 vec<tree, va_gc> *params = make_tree_vector ();
1125 vec_safe_push (params, args[0]);
1126 vec_safe_push (params, args[1]);
1127
1128 tree add_sub_builtin;
1129 if (fcode == RS6000_OVLD_VEC_ADDE)
1130 add_sub_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_ADD];
1131 else
1132 add_sub_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_SUB];
1133
1134 tree call = altivec_resolve_overloaded_builtin (loc, add_sub_builtin,
1135 params);
1136 tree const1 = build_int_cstu (TREE_TYPE (types[0]), 1);
1137 tree ones_vector = build_vector_from_val (types[0], const1);
1138 tree and_expr = c_fold_build2_loc (loc, BIT_AND_EXPR, types[0],
1139 args[2], ones_vector);
1140 params = make_tree_vector ();
1141 vec_safe_push (params, call);
1142 vec_safe_push (params, and_expr);
1143 *res = resolved;
1144 return altivec_resolve_overloaded_builtin (loc, add_sub_builtin,
1145 params);
1146 }
1147 /* For {un}signed __int128s use the vaddeuqm/vsubeuqm instruction
1148 directly using the standard machinery. */
1149 case E_TImode:
1150 *res = unresolved;
1151 break;
1152
1153 /* Types other than {un}signed int and {un}signed __int128
1154 are errors. */
1155 default:
1156 *res = resolved_bad;
1157 }
1158
1159 return error_mark_node;
1160 }
1161
1162 /* Resolve an overloaded vec_addec or vec_subec call and return a tree
1163 expression for the resolved call if successful. ARGS contains the arguments
1164 to the call. TYPES contains their types. RES must be set to indicate the
1165 status of the resolution attempt. LOC contains statement location
1166 information. */
1167
1168 static tree
1169 resolve_vec_addec_subec (resolution *res, rs6000_gen_builtins fcode,
1170 tree *args, tree *types, location_t loc)
1171 {
1172 /* vec_addec and vec_subec needs to be special cased because there is
1173 no instruction for the (un)signed int version. */
1174
1175 /* All 3 arguments must be vectors of (signed or unsigned) (int or
1176 __int128) and the types must be compatible. */
1177 if (TREE_CODE (types[0]) != VECTOR_TYPE
1178 || !lang_hooks.types_compatible_p (types[0], types[1])
1179 || !lang_hooks.types_compatible_p (types[1], types[2]))
1180 {
1181 *res = resolved_bad;
1182 return error_mark_node;
1183 }
1184
1185 switch (TYPE_MODE (TREE_TYPE (types[0])))
1186 {
1187 /* For {un}signed ints,
1188 vec_addec (va, vb, carryv) ==
1189 vec_or (vec_addc (va, vb),
1190 vec_addc (vec_add (va, vb),
1191 vec_and (carryv, 0x1))). */
1192 case E_SImode:
1193 {
1194 /* Use save_expr to ensure that operands used more than once that may
1195 have side effects (like calls) are only evaluated once. */
1196 args[0] = save_expr (args[0]);
1197 args[1] = save_expr (args[1]);
1198 vec<tree, va_gc> *params = make_tree_vector ();
1199 vec_safe_push (params, args[0]);
1200 vec_safe_push (params, args[1]);
1201
1202 tree as_c_builtin;
1203 if (fcode == RS6000_OVLD_VEC_ADDEC)
1204 as_c_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_ADDC];
1205 else
1206 as_c_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_SUBC];
1207
1208 tree call1 = altivec_resolve_overloaded_builtin (loc, as_c_builtin,
1209 params);
1210 params = make_tree_vector ();
1211 vec_safe_push (params, args[0]);
1212 vec_safe_push (params, args[1]);
1213
1214 tree as_builtin;
1215 if (fcode == RS6000_OVLD_VEC_ADDEC)
1216 as_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_ADD];
1217 else
1218 as_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_SUB];
1219
1220 tree call2 = altivec_resolve_overloaded_builtin (loc, as_builtin,
1221 params);
1222 tree const1 = build_int_cstu (TREE_TYPE (types[0]), 1);
1223 tree ones_vector = build_vector_from_val (types[0], const1);
1224 tree and_expr = c_fold_build2_loc (loc, BIT_AND_EXPR, types[0],
1225 args[2], ones_vector);
1226 params = make_tree_vector ();
1227 vec_safe_push (params, call2);
1228 vec_safe_push (params, and_expr);
1229 call2 = altivec_resolve_overloaded_builtin (loc, as_c_builtin, params);
1230 params = make_tree_vector ();
1231 vec_safe_push (params, call1);
1232 vec_safe_push (params, call2);
1233 tree or_builtin = rs6000_builtin_decls[RS6000_OVLD_VEC_OR];
1234 *res = resolved;
1235 return altivec_resolve_overloaded_builtin (loc, or_builtin, params);
1236 }
1237 /* For {un}signed __int128s use the vaddecuq/vsubbecuq
1238 instructions. This occurs through normal processing. */
1239 case E_TImode:
1240 *res = unresolved;
1241 break;
1242
1243 /* Types other than {un}signed int and {un}signed __int128
1244 are errors. */
1245 default:
1246 *res = resolved_bad;
1247 }
1248
1249 return error_mark_node;
1250 }
1251
1252 /* Resolve an overloaded vec_splats or vec_promote call and return a tree
1253 expression for the resolved call if successful. NARGS is the number of
1254 arguments to the call. ARGLIST contains the arguments. RES must be set
1255 to indicate the status of the resolution attempt. */
1256
1257 static tree
1258 resolve_vec_splats (resolution *res, rs6000_gen_builtins fcode,
1259 vec<tree, va_gc> *arglist, unsigned nargs)
1260 {
1261 const char *name;
1262 name = fcode == RS6000_OVLD_VEC_SPLATS ? "vec_splats" : "vec_promote";
1263
1264 if (fcode == RS6000_OVLD_VEC_SPLATS && nargs != 1)
1265 {
1266 error ("builtin %qs only accepts 1 argument", name);
1267 *res = resolved;
1268 return error_mark_node;
1269 }
1270
1271 if (fcode == RS6000_OVLD_VEC_PROMOTE && nargs != 2)
1272 {
1273 error ("builtin %qs only accepts 2 arguments", name);
1274 *res = resolved;
1275 return error_mark_node;
1276 }
1277
1278 /* Ignore promote's element argument. */
1279 if (fcode == RS6000_OVLD_VEC_PROMOTE
1280 && !INTEGRAL_TYPE_P (TREE_TYPE ((*arglist)[1])))
1281 {
1282 *res = resolved_bad;
1283 return error_mark_node;
1284 }
1285
1286 tree arg = (*arglist)[0];
1287 tree type = TREE_TYPE (arg);
1288
1289 if (!SCALAR_FLOAT_TYPE_P (type) && !INTEGRAL_TYPE_P (type))
1290 {
1291 *res = resolved_bad;
1292 return error_mark_node;
1293 }
1294
1295 bool unsigned_p = TYPE_UNSIGNED (type);
1296 int size;
1297
1298 switch (TYPE_MODE (type))
1299 {
1300 case E_TImode:
1301 type = unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node;
1302 size = 1;
1303 break;
1304 case E_DImode:
1305 type = unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node;
1306 size = 2;
1307 break;
1308 case E_SImode:
1309 type = unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node;
1310 size = 4;
1311 break;
1312 case E_HImode:
1313 type = unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node;
1314 size = 8;
1315 break;
1316 case E_QImode:
1317 type = unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node;
1318 size = 16;
1319 break;
1320 case E_SFmode:
1321 type = V4SF_type_node;
1322 size = 4;
1323 break;
1324 case E_DFmode:
1325 type = V2DF_type_node;
1326 size = 2;
1327 break;
1328 default:
1329 *res = resolved_bad;
1330 return error_mark_node;
1331 }
1332
1333 arg = save_expr (c_fold_convert (TREE_TYPE (type), arg));
1334 vec<constructor_elt, va_gc> *vec;
1335 vec_alloc (vec, size);
1336
1337 for (int i = 0; i < size; i++)
1338 {
1339 constructor_elt elt = {NULL_TREE, arg};
1340 vec->quick_push (elt);
1341 }
1342
1343 *res = resolved;
1344 return build_constructor (type, vec);
1345 }
1346
1347 /* Resolve an overloaded vec_extract call and return a tree expression for
1348 the resolved call if successful. NARGS is the number of arguments to
1349 the call. ARGLIST contains the arguments. RES must be set to indicate
1350 the status of the resolution attempt. LOC contains statement location
1351 information. */
1352
1353 static tree
1354 resolve_vec_extract (resolution *res, vec<tree, va_gc> *arglist,
1355 unsigned nargs, location_t loc)
1356 {
1357 if (nargs != 2)
1358 {
1359 error ("builtin %qs only accepts 2 arguments", "vec_extract");
1360 *res = resolved;
1361 return error_mark_node;
1362 }
1363
1364 tree arg1 = (*arglist)[0];
1365 tree arg1_type = TREE_TYPE (arg1);
1366 tree arg2 = (*arglist)[1];
1367
1368 if (TREE_CODE (arg1_type) != VECTOR_TYPE
1369 || !INTEGRAL_TYPE_P (TREE_TYPE (arg2)))
1370 {
1371 *res = resolved_bad;
1372 return error_mark_node;
1373 }
1374
1375 /* See if we can optimize vec_extract with the current VSX instruction
1376 set. */
1377 machine_mode mode = TYPE_MODE (arg1_type);
1378 tree arg1_inner_type;
1379
1380 if (VECTOR_MEM_VSX_P (mode))
1381 {
1382 tree call = NULL_TREE;
1383 int nunits = GET_MODE_NUNITS (mode);
1384 arg2 = fold_for_warn (arg2);
1385
1386 /* If the second argument is an integer constant, generate
1387 the built-in code if we can. We need 64-bit and direct
1388 move to extract the small integer vectors. */
1389 if (TREE_CODE (arg2) == INTEGER_CST)
1390 {
1391 wide_int selector = wi::to_wide (arg2);
1392 selector = wi::umod_trunc (selector, nunits);
1393 arg2 = wide_int_to_tree (TREE_TYPE (arg2), selector);
1394 switch (mode)
1395 {
1396 case E_V1TImode:
1397 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V1TI];
1398 break;
1399
1400 case E_V2DFmode:
1401 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V2DF];
1402 break;
1403
1404 case E_V2DImode:
1405 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V2DI];
1406 break;
1407
1408 case E_V4SFmode:
1409 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V4SF];
1410 break;
1411
1412 case E_V4SImode:
1413 if (TARGET_DIRECT_MOVE_64BIT)
1414 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V4SI];
1415 break;
1416
1417 case E_V8HImode:
1418 if (TARGET_DIRECT_MOVE_64BIT)
1419 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V8HI];
1420 break;
1421
1422 case E_V16QImode:
1423 if (TARGET_DIRECT_MOVE_64BIT)
1424 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V16QI];
1425 break;
1426
1427 default:
1428 break;
1429 }
1430 }
1431
1432 /* If the second argument is variable, we can optimize it if we are
1433 generating 64-bit code on a machine with direct move. */
1434 else if (TREE_CODE (arg2) != INTEGER_CST && TARGET_DIRECT_MOVE_64BIT)
1435 {
1436 switch (mode)
1437 {
1438 case E_V2DFmode:
1439 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V2DF];
1440 break;
1441
1442 case E_V2DImode:
1443 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V2DI];
1444 break;
1445
1446 case E_V4SFmode:
1447 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V4SF];
1448 break;
1449
1450 case E_V4SImode:
1451 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V4SI];
1452 break;
1453
1454 case E_V8HImode:
1455 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V8HI];
1456 break;
1457
1458 case E_V16QImode:
1459 call = rs6000_builtin_decls[RS6000_BIF_VEC_EXT_V16QI];
1460 break;
1461
1462 default:
1463 break;
1464 }
1465 }
1466
1467 if (call)
1468 {
1469 tree result = build_call_expr (call, 2, arg1, arg2);
1470 /* Coerce the result to vector element type. May be no-op. */
1471 arg1_inner_type = TREE_TYPE (arg1_type);
1472 result = c_fold_convert (arg1_inner_type, result);
1473 *res = resolved;
1474 return result;
1475 }
1476 }
1477
1478 /* Build *(((arg1_inner_type*) & (vector type){arg1}) + arg2). */
1479 arg1_inner_type = TREE_TYPE (arg1_type);
1480 tree subp = build_int_cst (TREE_TYPE (arg2),
1481 TYPE_VECTOR_SUBPARTS (arg1_type) - 1);
1482 arg2 = build_binary_op (loc, BIT_AND_EXPR, arg2, subp, 0);
1483
1484 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, arg1_type);
1485 DECL_EXTERNAL (decl) = 0;
1486 TREE_PUBLIC (decl) = 0;
1487 DECL_CONTEXT (decl) = current_function_decl;
1488 TREE_USED (decl) = 1;
1489 TREE_TYPE (decl) = arg1_type;
1490 TREE_READONLY (decl) = TYPE_READONLY (arg1_type);
1491
1492 tree stmt;
1493 if (c_dialect_cxx ())
1494 {
1495 stmt = build4 (TARGET_EXPR, arg1_type, decl, arg1, NULL_TREE, NULL_TREE);
1496 SET_EXPR_LOCATION (stmt, loc);
1497 }
1498 else
1499 {
1500 DECL_INITIAL (decl) = arg1;
1501 stmt = build1 (DECL_EXPR, arg1_type, decl);
1502 TREE_ADDRESSABLE (decl) = 1;
1503 SET_EXPR_LOCATION (stmt, loc);
1504 stmt = build1 (COMPOUND_LITERAL_EXPR, arg1_type, stmt);
1505 }
1506
1507 tree innerptrtype = build_pointer_type (arg1_inner_type);
1508 stmt = build_unary_op (loc, ADDR_EXPR, stmt, 0);
1509 stmt = convert (innerptrtype, stmt);
1510 stmt = build_binary_op (loc, PLUS_EXPR, stmt, arg2, 1);
1511 stmt = build_indirect_ref (loc, stmt, RO_NULL);
1512
1513 /* PR83660: We mark this as having side effects so that downstream in
1514 fold_build_cleanup_point_expr () it will get a CLEANUP_POINT_EXPR. If it
1515 does not we can run into an ICE later in gimplify_cleanup_point_expr ().
1516 Potentially this causes missed optimization because there actually is no
1517 side effect. */
1518 if (c_dialect_cxx ())
1519 TREE_SIDE_EFFECTS (stmt) = 1;
1520
1521 *res = resolved;
1522 return stmt;
1523 }
1524
1525 /* Resolve an overloaded vec_insert call and return a tree expression for
1526 the resolved call if successful. NARGS is the number of arguments to
1527 the call. ARGLIST contains the arguments. RES must be set to indicate
1528 the status of the resolution attempt. LOC contains statement location
1529 information. */
1530
1531 static tree
1532 resolve_vec_insert (resolution *res, vec<tree, va_gc> *arglist,
1533 unsigned nargs, location_t loc)
1534 {
1535 if (nargs != 3)
1536 {
1537 error ("builtin %qs only accepts 3 arguments", "vec_insert");
1538 *res = resolved;
1539 return error_mark_node;
1540 }
1541
1542 tree arg0 = (*arglist)[0];
1543 tree arg1 = (*arglist)[1];
1544 tree arg1_type = TREE_TYPE (arg1);
1545 tree arg2 = fold_for_warn ((*arglist)[2]);
1546
1547 if (TREE_CODE (arg1_type) != VECTOR_TYPE
1548 || !INTEGRAL_TYPE_P (TREE_TYPE (arg2)))
1549 {
1550 *res = resolved_bad;
1551 return error_mark_node;
1552 }
1553
1554 /* Build *(((arg1_inner_type*) & (vector type){arg1}) + arg2) = arg0 with
1555 VIEW_CONVERT_EXPR. i.e.:
1556 D.3192 = v1;
1557 _1 = n & 3;
1558 VIEW_CONVERT_EXPR<int[4]>(D.3192)[_1] = i;
1559 v1 = D.3192;
1560 D.3194 = v1; */
1561 if (TYPE_VECTOR_SUBPARTS (arg1_type) == 1)
1562 arg2 = build_int_cst (TREE_TYPE (arg2), 0);
1563 else
1564 {
1565 tree c = build_int_cst (TREE_TYPE (arg2),
1566 TYPE_VECTOR_SUBPARTS (arg1_type) - 1);
1567 arg2 = build_binary_op (loc, BIT_AND_EXPR, arg2, c, 0);
1568 }
1569
1570 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, arg1_type);
1571 DECL_EXTERNAL (decl) = 0;
1572 TREE_PUBLIC (decl) = 0;
1573 DECL_CONTEXT (decl) = current_function_decl;
1574 TREE_USED (decl) = 1;
1575 TREE_TYPE (decl) = arg1_type;
1576 TREE_READONLY (decl) = TYPE_READONLY (arg1_type);
1577 TREE_ADDRESSABLE (decl) = 1;
1578
1579 tree stmt;
1580 if (c_dialect_cxx ())
1581 {
1582 stmt = build4 (TARGET_EXPR, arg1_type, decl, arg1, NULL_TREE, NULL_TREE);
1583 SET_EXPR_LOCATION (stmt, loc);
1584 }
1585 else
1586 {
1587 DECL_INITIAL (decl) = arg1;
1588 stmt = build1 (DECL_EXPR, arg1_type, decl);
1589 SET_EXPR_LOCATION (stmt, loc);
1590 stmt = build1 (COMPOUND_LITERAL_EXPR, arg1_type, stmt);
1591 }
1592
1593 if (TARGET_VSX)
1594 {
1595 stmt = build_array_ref (loc, stmt, arg2);
1596 stmt = c_fold_build2_loc (UNKNOWN_LOCATION, MODIFY_EXPR,
1597 TREE_TYPE (arg0), stmt,
1598 convert (TREE_TYPE (stmt), arg0));
1599 stmt = build2 (COMPOUND_EXPR, arg1_type, stmt, decl);
1600 }
1601 else
1602 {
1603 tree arg1_inner_type = TREE_TYPE (arg1_type);
1604 tree innerptrtype = build_pointer_type (arg1_inner_type);
1605 stmt = build_unary_op (loc, ADDR_EXPR, stmt, 0);
1606 stmt = convert (innerptrtype, stmt);
1607 stmt = build_binary_op (loc, PLUS_EXPR, stmt, arg2, 1);
1608 stmt = build_indirect_ref (loc, stmt, RO_NULL);
1609 stmt = build2 (MODIFY_EXPR, TREE_TYPE (stmt), stmt,
1610 convert (TREE_TYPE (stmt), arg0));
1611 stmt = build2 (COMPOUND_EXPR, arg1_type, stmt, decl);
1612 }
1613
1614 *res = resolved;
1615 return stmt;
1616 }
1617
1618 /* Resolve an overloaded vec_step call and return a tree expression for
1619 the resolved call if successful. NARGS is the number of arguments to
1620 the call. ARGLIST contains the arguments. RES must be set to indicate
1621 the status of the resolution attempt. */
1622
1623 static tree
1624 resolve_vec_step (resolution *res, vec<tree, va_gc> *arglist, unsigned nargs)
1625 {
1626 if (nargs != 1)
1627 {
1628 error ("builtin %qs only accepts 1 argument", "vec_step");
1629 *res = resolved;
1630 return error_mark_node;
1631 }
1632
1633 tree arg0 = (*arglist)[0];
1634 tree arg0_type = TREE_TYPE (arg0);
1635
1636 if (TREE_CODE (arg0_type) != VECTOR_TYPE)
1637 {
1638 *res = resolved_bad;
1639 return error_mark_node;
1640 }
1641
1642 *res = resolved;
1643 return build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (arg0_type));
1644 }
1645
1646 /* Look for a matching instance in a chain of instances. INSTANCE points to
1647 the chain of instances; INSTANCE_CODE is the code identifying the specific
1648 built-in being searched for; FCODE is the overloaded function code; TYPES
1649 contains an array of NARGS types that must match the types of the
1650 instance's parameters; ARGS contains an array of NARGS arguments to be
1651 passed to the instance; and NARGS is the number of built-in arguments to
1652 check. If found, resolve the built-in and return it, unless the built-in
1653 is not supported in context. In that case, set UNSUPPORTED_BUILTIN to
1654 true. If we don't match, return error_mark_node and leave
1655 UNSUPPORTED_BUILTIN alone. */
1656
1657 static tree
1658 find_instance (bool *unsupported_builtin, int *instance,
1659 rs6000_gen_builtins instance_code,
1660 rs6000_gen_builtins fcode,
1661 tree *types, tree *args, int nargs)
1662 {
1663 while (*instance != -1
1664 && rs6000_instance_info[*instance].bifid != instance_code)
1665 *instance = rs6000_instance_info[*instance].next;
1666
1667 int inst = *instance;
1668 gcc_assert (inst != -1);
1669 /* It is possible for an instance to require a data type that isn't
1670 defined on this target, in which case rs6000_instance_info_fntype[inst]
1671 will be NULL. */
1672 if (!rs6000_instance_info_fntype[inst])
1673 return error_mark_node;
1674 rs6000_gen_builtins bifid = rs6000_instance_info[inst].bifid;
1675 tree fntype = rs6000_builtin_info_fntype[bifid];
1676 tree argtype = TYPE_ARG_TYPES (fntype);
1677 bool args_compatible = true;
1678
1679 for (int i = 0; i < nargs; i++)
1680 {
1681 tree parmtype = TREE_VALUE (argtype);
1682 if (!rs6000_builtin_type_compatible (types[i], parmtype))
1683 {
1684 args_compatible = false;
1685 break;
1686 }
1687 argtype = TREE_CHAIN (argtype);
1688 }
1689
1690 if (args_compatible)
1691 {
1692 if (rs6000_builtin_decl (bifid, false) != error_mark_node
1693 && rs6000_builtin_is_supported (bifid))
1694 {
1695 tree ret_type = TREE_TYPE (rs6000_instance_info_fntype[inst]);
1696 return altivec_build_resolved_builtin (args, nargs, fntype, ret_type,
1697 bifid, fcode);
1698 }
1699 else
1700 *unsupported_builtin = true;
1701 }
1702
1703 return error_mark_node;
1704 }
1705
1706 /* Implementation of the resolve_overloaded_builtin target hook, to
1707 support Altivec's overloaded builtins. */
1708
1709 tree
1710 altivec_resolve_overloaded_builtin (location_t loc, tree fndecl,
1711 void *passed_arglist, bool)
1712 {
1713 rs6000_gen_builtins fcode
1714 = (rs6000_gen_builtins) DECL_MD_FUNCTION_CODE (fndecl);
1715
1716 /* Return immediately if this isn't an overload. */
1717 if (fcode <= RS6000_OVLD_NONE)
1718 return NULL_TREE;
1719
1720 if (TARGET_DEBUG_BUILTIN)
1721 fprintf (stderr, "altivec_resolve_overloaded_builtin, code = %4d, %s\n",
1722 (int) fcode, IDENTIFIER_POINTER (DECL_NAME (fndecl)));
1723
1724 /* vec_lvsl and vec_lvsr are deprecated for use with LE element order. */
1725 if (fcode == RS6000_OVLD_VEC_LVSL && !BYTES_BIG_ENDIAN)
1726 warning (OPT_Wdeprecated,
1727 "%<vec_lvsl%> is deprecated for little endian; use "
1728 "assignment for unaligned loads and stores");
1729 else if (fcode == RS6000_OVLD_VEC_LVSR && !BYTES_BIG_ENDIAN)
1730 warning (OPT_Wdeprecated,
1731 "%<vec_lvsr%> is deprecated for little endian; use "
1732 "assignment for unaligned loads and stores");
1733
1734 /* Gather the arguments and their types into arrays for easier handling. */
1735 tree fnargs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1736 tree types[MAX_OVLD_ARGS];
1737 tree args[MAX_OVLD_ARGS];
1738 unsigned int n;
1739
1740 /* Count the number of expected arguments. */
1741 unsigned expected_args = 0;
1742 for (tree chain = fnargs;
1743 chain && !VOID_TYPE_P (TREE_VALUE (chain));
1744 chain = TREE_CHAIN (chain))
1745 expected_args++;
1746
1747 vec<tree, va_gc> *arglist = static_cast<vec<tree, va_gc> *> (passed_arglist);
1748 unsigned int nargs = vec_safe_length (arglist);
1749
1750 /* If the number of arguments did not match the prototype, return NULL
1751 and the generic code will issue the appropriate error message. Skip
1752 this test for functions where we don't fully describe all the possible
1753 overload signatures in rs6000-overload.def (because they aren't relevant
1754 to the expansion here). If we don't, we get confusing error messages. */
1755 /* As an example, for vec_splats we have:
1756
1757 ; There are no actual builtins for vec_splats. There is special handling for
1758 ; this in altivec_resolve_overloaded_builtin in rs6000-c.cc, where the call
1759 ; is replaced by a constructor. The single overload here causes
1760 ; __builtin_vec_splats to be registered with the front end so that can happen.
1761 [VEC_SPLATS, vec_splats, __builtin_vec_splats]
1762 vsi __builtin_vec_splats (vsi);
1763 ABS_V4SI SPLATS_FAKERY
1764
1765 So even though __builtin_vec_splats accepts all vector types, the
1766 infrastructure cheats and just records one prototype. We end up getting
1767 an error message that refers to this specific prototype even when we
1768 are handling a different argument type. That is completely confusing
1769 to the user, so it's best to let these cases be handled individually
1770 in the resolve_vec_splats, etc., helper functions. */
1771
1772 if (expected_args != nargs
1773 && !(fcode == RS6000_OVLD_VEC_PROMOTE
1774 || fcode == RS6000_OVLD_VEC_SPLATS
1775 || fcode == RS6000_OVLD_VEC_EXTRACT
1776 || fcode == RS6000_OVLD_VEC_INSERT
1777 || fcode == RS6000_OVLD_VEC_STEP))
1778 return NULL;
1779
1780 for (n = 0;
1781 !VOID_TYPE_P (TREE_VALUE (fnargs)) && n < nargs;
1782 fnargs = TREE_CHAIN (fnargs), n++)
1783 {
1784 tree decl_type = TREE_VALUE (fnargs);
1785 tree arg = (*arglist)[n];
1786
1787 if (arg == error_mark_node)
1788 return error_mark_node;
1789
1790 if (n >= MAX_OVLD_ARGS)
1791 abort ();
1792
1793 arg = default_conversion (arg);
1794 tree type = TREE_TYPE (arg);
1795
1796 /* The C++ front-end converts float * to const void * using
1797 NOP_EXPR<const void *> (NOP_EXPR<void *> (x)). */
1798 if (POINTER_TYPE_P (type)
1799 && TREE_CODE (arg) == NOP_EXPR
1800 && lang_hooks.types_compatible_p (TREE_TYPE (arg),
1801 const_ptr_type_node)
1802 && lang_hooks.types_compatible_p (TREE_TYPE (TREE_OPERAND (arg, 0)),
1803 ptr_type_node))
1804 {
1805 arg = TREE_OPERAND (arg, 0);
1806 type = TREE_TYPE (arg);
1807 }
1808
1809 /* Remove the const from the pointers to simplify the overload
1810 matching further down. */
1811 if (POINTER_TYPE_P (decl_type)
1812 && POINTER_TYPE_P (type)
1813 && TYPE_QUALS (TREE_TYPE (type)) != 0)
1814 {
1815 if (TYPE_READONLY (TREE_TYPE (type))
1816 && !TYPE_READONLY (TREE_TYPE (decl_type)))
1817 warning (0, "passing argument %d of %qE discards %qs "
1818 "qualifier from pointer target type", n + 1, fndecl,
1819 "const");
1820 type = build_qualified_type (TREE_TYPE (type), 0);
1821 type = build_pointer_type (type);
1822 arg = c_fold_convert (type, arg);
1823 }
1824
1825 /* For RS6000_OVLD_VEC_LXVL, convert any const * to its non constant
1826 equivalent to simplify the overload matching below. */
1827 if (fcode == RS6000_OVLD_VEC_LXVL
1828 && POINTER_TYPE_P (type)
1829 && TYPE_READONLY (TREE_TYPE (type)))
1830 {
1831 type = build_qualified_type (TREE_TYPE (type), 0);
1832 type = build_pointer_type (type);
1833 arg = c_fold_convert (type, arg);
1834 }
1835
1836 args[n] = arg;
1837 types[n] = type;
1838 }
1839
1840 /* Some overloads require special handling. */
1841 tree returned_expr = NULL;
1842 resolution res = unresolved;
1843
1844 if (fcode == RS6000_OVLD_VEC_MUL)
1845 returned_expr = resolve_vec_mul (&res, args, types, loc);
1846 else if (fcode == RS6000_OVLD_VEC_CMPNE)
1847 returned_expr = resolve_vec_cmpne (&res, args, types, loc);
1848 else if (fcode == RS6000_OVLD_VEC_ADDE || fcode == RS6000_OVLD_VEC_SUBE)
1849 returned_expr = resolve_vec_adde_sube (&res, fcode, args, types, loc);
1850 else if (fcode == RS6000_OVLD_VEC_ADDEC || fcode == RS6000_OVLD_VEC_SUBEC)
1851 returned_expr = resolve_vec_addec_subec (&res, fcode, args, types, loc);
1852 else if (fcode == RS6000_OVLD_VEC_SPLATS || fcode == RS6000_OVLD_VEC_PROMOTE)
1853 returned_expr = resolve_vec_splats (&res, fcode, arglist, nargs);
1854 else if (fcode == RS6000_OVLD_VEC_EXTRACT)
1855 returned_expr = resolve_vec_extract (&res, arglist, nargs, loc);
1856 else if (fcode == RS6000_OVLD_VEC_INSERT)
1857 returned_expr = resolve_vec_insert (&res, arglist, nargs, loc);
1858 else if (fcode == RS6000_OVLD_VEC_STEP)
1859 returned_expr = resolve_vec_step (&res, arglist, nargs);
1860
1861 if (res == resolved)
1862 return returned_expr;
1863
1864 /* "Regular" built-in functions and overloaded functions share a namespace
1865 for some arrays, like rs6000_builtin_decls. But rs6000_overload_info
1866 only has information for the overloaded functions, so we need an
1867 adjusted index for that. */
1868 unsigned int adj_fcode = fcode - RS6000_OVLD_NONE;
1869
1870 if (res == resolved_bad)
1871 {
1872 const char *name = rs6000_overload_info[adj_fcode].ovld_name;
1873 error ("invalid parameter combination for AltiVec intrinsic %qs", name);
1874 return error_mark_node;
1875 }
1876
1877 bool unsupported_builtin = false;
1878 rs6000_gen_builtins instance_code;
1879 bool supported = false;
1880 int instance = rs6000_overload_info[adj_fcode].first_instance;
1881 gcc_assert (instance != -1);
1882
1883 /* Functions with no arguments can have only one overloaded instance. */
1884 gcc_assert (nargs > 0 || rs6000_instance_info[instance].next == -1);
1885
1886 /* Standard overload processing involves determining whether an instance
1887 exists that is type-compatible with the overloaded function call. In
1888 a couple of cases, we need to do some extra processing to disambiguate
1889 between multiple compatible instances. */
1890 switch (fcode)
1891 {
1892 /* Need to special case __builtin_cmpb because the overloaded forms
1893 of this function take (unsigned int, unsigned int) or (unsigned
1894 long long int, unsigned long long int). Since C conventions
1895 allow the respective argument types to be implicitly coerced into
1896 each other, the default handling does not provide adequate
1897 discrimination between the desired forms of the function. */
1898 case RS6000_OVLD_SCAL_CMPB:
1899 {
1900 machine_mode arg1_mode = TYPE_MODE (types[0]);
1901 machine_mode arg2_mode = TYPE_MODE (types[1]);
1902
1903 /* If any supplied arguments are wider than 32 bits, resolve to
1904 64-bit variant of built-in function. */
1905 if (GET_MODE_PRECISION (arg1_mode) > 32
1906 || GET_MODE_PRECISION (arg2_mode) > 32)
1907 /* Assure all argument and result types are compatible with
1908 the built-in function represented by RS6000_BIF_CMPB. */
1909 instance_code = RS6000_BIF_CMPB;
1910 else
1911 /* Assure all argument and result types are compatible with
1912 the built-in function represented by RS6000_BIF_CMPB_32. */
1913 instance_code = RS6000_BIF_CMPB_32;
1914
1915 tree call = find_instance (&unsupported_builtin, &instance,
1916 instance_code, fcode, types, args, nargs);
1917 if (call != error_mark_node)
1918 return call;
1919 break;
1920 }
1921 case RS6000_OVLD_VEC_VSIE:
1922 {
1923 machine_mode arg1_mode = TYPE_MODE (types[0]);
1924
1925 /* If supplied first argument is wider than 64 bits, resolve to
1926 128-bit variant of built-in function. */
1927 if (GET_MODE_PRECISION (arg1_mode) > 64)
1928 {
1929 /* If first argument is of float variety, choose the variant that
1930 expects __ieee128 argument. If the first argument is vector
1931 int, choose the variant that expects vector unsigned
1932 __int128 argument. Otherwise, expect scalar __int128 argument.
1933 */
1934 if (GET_MODE_CLASS (arg1_mode) == MODE_FLOAT)
1935 instance_code = RS6000_BIF_VSIEQPF;
1936 else if (GET_MODE_CLASS (arg1_mode) == MODE_VECTOR_INT)
1937 instance_code = RS6000_BIF_VSIEQPV;
1938 else
1939 instance_code = RS6000_BIF_VSIEQP;
1940 }
1941 else
1942 {
1943 /* If first argument is of float variety, choose variant
1944 that expects double argument. Otherwise, expect
1945 long long int argument. */
1946 if (GET_MODE_CLASS (arg1_mode) == MODE_FLOAT)
1947 instance_code = RS6000_BIF_VSIEDPF;
1948 else
1949 instance_code = RS6000_BIF_VSIEDP;
1950 }
1951
1952 tree call = find_instance (&unsupported_builtin, &instance,
1953 instance_code, fcode, types, args, nargs);
1954 if (call != error_mark_node)
1955 return call;
1956 break;
1957 }
1958 case RS6000_OVLD_VEC_REPLACE_UN:
1959 {
1960 machine_mode arg2_mode = TYPE_MODE (types[1]);
1961
1962 if (arg2_mode == SImode)
1963 /* Signed and unsigned are handled the same. */
1964 instance_code = RS6000_BIF_VREPLACE_UN_USI;
1965 else if (arg2_mode == SFmode)
1966 instance_code = RS6000_BIF_VREPLACE_UN_SF;
1967 else if (arg2_mode == DImode)
1968 /* Signed and unsigned are handled the same. */
1969 instance_code = RS6000_BIF_VREPLACE_UN_UDI;
1970 else if (arg2_mode == DFmode)
1971 instance_code = RS6000_BIF_VREPLACE_UN_DF;
1972 else
1973 break;
1974
1975 tree call = find_instance (&unsupported_builtin, &instance,
1976 instance_code, fcode, types, args, nargs);
1977 if (call != error_mark_node)
1978 return call;
1979 break;
1980 }
1981 default:
1982 /* Standard overload processing. Look for an instance with compatible
1983 parameter types. If it is supported in the current context, resolve
1984 the overloaded call to that instance. */
1985 for (; instance != -1; instance = rs6000_instance_info[instance].next)
1986 {
1987 tree fntype = rs6000_instance_info_fntype[instance];
1988 rs6000_gen_builtins bifid = rs6000_instance_info[instance].bifid;
1989 /* It is possible for an instance to require a data type that isn't
1990 defined on this target, in which case fntype will be
1991 NULL. */
1992 if (!fntype)
1993 continue;
1994
1995 bool mismatch = false;
1996 tree nextparm = TYPE_ARG_TYPES (fntype);
1997
1998 for (unsigned int arg_i = 0;
1999 arg_i < nargs && nextparm != NULL;
2000 arg_i++)
2001 {
2002 tree parmtype = TREE_VALUE (nextparm);
2003 if (!rs6000_builtin_type_compatible (types[arg_i], parmtype))
2004 {
2005 mismatch = true;
2006 break;
2007 }
2008 nextparm = TREE_CHAIN (nextparm);
2009 }
2010
2011 if (mismatch)
2012 continue;
2013
2014 supported = rs6000_builtin_is_supported (bifid);
2015 if (rs6000_builtin_decl (bifid, false) != error_mark_node
2016 && supported)
2017 {
2018 tree ret_type = TREE_TYPE (fntype);
2019 fntype = rs6000_builtin_info_fntype[bifid];
2020 return altivec_build_resolved_builtin (args, nargs, fntype,
2021 ret_type, bifid, fcode);
2022 }
2023 else
2024 {
2025 unsupported_builtin = true;
2026 break;
2027 }
2028 }
2029 }
2030
2031 if (unsupported_builtin)
2032 {
2033 const char *name = rs6000_overload_info[adj_fcode].ovld_name;
2034 if (!supported)
2035 {
2036 /* Indicate that the instantiation of the overloaded builtin
2037 name is not available with the target flags in effect. */
2038 rs6000_gen_builtins bifid = rs6000_instance_info[instance].bifid;
2039 rs6000_gen_builtins fcode = (rs6000_gen_builtins) bifid;
2040 rs6000_invalid_builtin (fcode);
2041 /* Provide clarity of the relationship between the overload
2042 and the instantiation. */
2043 const char *internal_name = rs6000_builtin_info[bifid].bifname;
2044 rich_location richloc (line_table, input_location);
2045 inform (&richloc,
2046 "overloaded builtin %qs is implemented by builtin %qs",
2047 name, internal_name);
2048 }
2049 else
2050 error ("%qs is not supported in this compiler configuration", name);
2051
2052 return error_mark_node;
2053 }
2054
2055 /* If we fall through to here, there were no compatible instances. */
2056 const char *name = rs6000_overload_info[adj_fcode].ovld_name;
2057 error ("invalid parameter combination for AltiVec intrinsic %qs", name);
2058 return error_mark_node;
2059 }