Debian Codegraph @

gcc/builtins.cc

raw
1 /* Expand builtin functions.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.cc instead. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "memmodel.h"
32 #include "gimple.h"
33 #include "predict.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "tree-vrp.h"
37 #include "tree-ssanames.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "diagnostic-core.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "fold-const-call.h"
46 #include "gimple-ssa-warn-access.h"
47 #include "stor-layout.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "tree-object-size.h"
51 #include "tree-ssa-strlen.h"
52 #include "realmpfr.h"
53 #include "cfgrtl.h"
54 #include "except.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "stmt.h"
58 #include "expr.h"
59 #include "libfuncs.h"
60 #include "output.h"
61 #include "typeclass.h"
62 #include "langhooks.h"
63 #include "value-prof.h"
64 #include "builtins.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "asan.h"
68 #include "internal-fn.h"
69 #include "case-cfn-macros.h"
70 #include "gimple-iterator.h"
71 #include "gimple-fold.h"
72 #include "intl.h"
73 #include "file-prefix-map.h" /* remap_macro_filename() */
74 #include "ipa-strub.h" /* strub_watermark_parm() */
75 #include "gomp-constants.h"
76 #include "omp-general.h"
77 #include "tree-dfa.h"
78 #include "gimple-ssa.h"
79 #include "tree-ssa-live.h"
80 #include "tree-outof-ssa.h"
81 #include "attr-fnspec.h"
82 #include "demangle.h"
83 #include "gimple-range.h"
84 #include "pointer-query.h"
85
86 struct target_builtins default_target_builtins;
87 #if SWITCHABLE_TARGET
88 struct target_builtins *this_target_builtins = &default_target_builtins;
89 #endif
90
91 /* Define the names of the builtin function types and codes. */
92 const char *const built_in_class_names[BUILT_IN_LAST]
93 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
94
95 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
96 const char * built_in_names[(int) END_BUILTINS] =
97 {
98 #include "builtins.def"
99 };
100
101 /* Setup an array of builtin_info_type, make sure each element decl is
102 initialized to NULL_TREE. */
103 builtin_info_type builtin_info[(int)END_BUILTINS];
104
105 /* Non-zero if __builtin_constant_p should be folded right away. */
106 bool force_folding_builtin_constant_p;
107
108 static int target_char_cast (tree, char *);
109 static int apply_args_size (void);
110 static int apply_result_size (void);
111 static rtx result_vector (int, rtx);
112 static void expand_builtin_prefetch (tree);
113 static rtx expand_builtin_apply_args (void);
114 static rtx expand_builtin_apply_args_1 (void);
115 static rtx expand_builtin_apply (rtx, rtx, rtx);
116 static void expand_builtin_return (rtx);
117 static rtx expand_builtin_classify_type (tree);
118 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
119 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
120 static rtx expand_builtin_interclass_mathfn (tree, rtx);
121 static rtx expand_builtin_sincos (tree);
122 static rtx expand_builtin_fegetround (tree, rtx, machine_mode);
123 static rtx expand_builtin_feclear_feraise_except (tree, rtx, machine_mode,
124 optab);
125 static rtx expand_builtin_cexpi (tree, rtx);
126 static rtx expand_builtin_issignaling (tree, rtx);
127 static rtx expand_builtin_int_roundingfn (tree, rtx);
128 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
129 static rtx expand_builtin_next_arg (void);
130 static rtx expand_builtin_va_start (tree);
131 static rtx expand_builtin_va_end (tree);
132 static rtx expand_builtin_va_copy (tree);
133 static rtx inline_expand_builtin_bytecmp (tree, rtx);
134 static rtx expand_builtin_strcmp (tree, rtx);
135 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
136 static rtx expand_builtin_memcpy (tree, rtx);
137 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
138 rtx target, tree exp,
139 memop_ret retmode,
140 bool might_overlap);
141 static rtx expand_builtin_memmove (tree, rtx);
142 static rtx expand_builtin_mempcpy (tree, rtx);
143 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, memop_ret);
144 static rtx expand_builtin_strcpy (tree, rtx);
145 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx);
146 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
147 static rtx expand_builtin_strncpy (tree, rtx);
148 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
149 static rtx expand_builtin_bzero (tree);
150 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
151 static rtx expand_builtin_strnlen (tree, rtx, machine_mode);
152 static rtx expand_builtin_alloca (tree);
153 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
154 static rtx expand_builtin_frame_address (tree, tree);
155 static rtx expand_builtin_stack_address ();
156 static tree stabilize_va_list_loc (location_t, tree, int);
157 static rtx expand_builtin_expect (tree, rtx);
158 static rtx expand_builtin_expect_with_probability (tree, rtx);
159 static tree fold_builtin_constant_p (tree);
160 static tree fold_builtin_classify_type (tree);
161 static tree fold_builtin_strlen (location_t, tree, tree, tree);
162 static tree fold_builtin_inf (location_t, tree, int);
163 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
164 static bool validate_arg (const_tree, enum tree_code code);
165 static rtx expand_builtin_fabs (tree, rtx, rtx);
166 static rtx expand_builtin_signbit (tree, rtx);
167 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
168 static tree fold_builtin_isascii (location_t, tree);
169 static tree fold_builtin_toascii (location_t, tree);
170 static tree fold_builtin_isdigit (location_t, tree);
171 static tree fold_builtin_fabs (location_t, tree, tree);
172 static tree fold_builtin_abs (location_t, tree, tree);
173 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
174 enum tree_code);
175 static tree fold_builtin_iseqsig (location_t, tree, tree);
176 static tree fold_builtin_varargs (location_t, tree, tree*, int);
177
178 static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
179 static tree fold_builtin_strspn (location_t, tree, tree, tree, tree);
180 static tree fold_builtin_strcspn (location_t, tree, tree, tree, tree);
181
182 static rtx expand_builtin_object_size (tree);
183 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
184 enum built_in_function);
185 static void maybe_emit_chk_warning (tree, enum built_in_function);
186 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
187 static tree fold_builtin_object_size (tree, tree, enum built_in_function);
188
189 unsigned HOST_WIDE_INT target_newline;
190 unsigned HOST_WIDE_INT target_percent;
191 static unsigned HOST_WIDE_INT target_c;
192 static unsigned HOST_WIDE_INT target_s;
193 char target_percent_c[3];
194 char target_percent_s[3];
195 char target_percent_s_newline[4];
196 static tree do_mpfr_remquo (tree, tree, tree);
197 static tree do_mpfr_lgamma_r (tree, tree, tree);
198 static void expand_builtin_sync_synchronize (void);
199
200 /* Return true if NAME starts with __builtin_ or __sync_. */
201
202 static bool
203 is_builtin_name (const char *name)
204 {
205 return (startswith (name, "__builtin_")
206 || startswith (name, "__sync_")
207 || startswith (name, "__atomic_"));
208 }
209
210 /* Return true if NODE should be considered for inline expansion regardless
211 of the optimization level. This means whenever a function is invoked with
212 its "internal" name, which normally contains the prefix "__builtin". */
213
214 bool
215 called_as_built_in (tree node)
216 {
217 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
218 we want the name used to call the function, not the name it
219 will have. */
220 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
221 return is_builtin_name (name);
222 }
223
224 /* Compute values M and N such that M divides (address of EXP - N) and such
225 that N < M. If these numbers can be determined, store M in alignp and N in
226 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
227 *alignp and any bit-offset to *bitposp.
228
229 Note that the address (and thus the alignment) computed here is based
230 on the address to which a symbol resolves, whereas DECL_ALIGN is based
231 on the address at which an object is actually located. These two
232 addresses are not always the same. For example, on ARM targets,
233 the address &foo of a Thumb function foo() has the lowest bit set,
234 whereas foo() itself starts on an even address.
235
236 If ADDR_P is true we are taking the address of the memory reference EXP
237 and thus cannot rely on the access taking place. */
238
239 bool
240 get_object_alignment_2 (tree exp, unsigned int *alignp,
241 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
242 {
243 poly_int64 bitsize, bitpos;
244 tree offset;
245 machine_mode mode;
246 int unsignedp, reversep, volatilep;
247 unsigned int align = BITS_PER_UNIT;
248 bool known_alignment = false;
249
250 /* Get the innermost object and the constant (bitpos) and possibly
251 variable (offset) offset of the access. */
252 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
253 &unsignedp, &reversep, &volatilep);
254
255 /* Extract alignment information from the innermost object and
256 possibly adjust bitpos and offset. */
257 if (TREE_CODE (exp) == FUNCTION_DECL)
258 {
259 /* Function addresses can encode extra information besides their
260 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
261 allows the low bit to be used as a virtual bit, we know
262 that the address itself must be at least 2-byte aligned. */
263 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
264 align = 2 * BITS_PER_UNIT;
265 }
266 else if (TREE_CODE (exp) == LABEL_DECL)
267 ;
268 else if (TREE_CODE (exp) == CONST_DECL)
269 {
270 /* The alignment of a CONST_DECL is determined by its initializer. */
271 exp = DECL_INITIAL (exp);
272 align = TYPE_ALIGN (TREE_TYPE (exp));
273 if (CONSTANT_CLASS_P (exp))
274 align = targetm.constant_alignment (exp, align);
275
276 known_alignment = true;
277 }
278 else if (DECL_P (exp))
279 {
280 align = DECL_ALIGN (exp);
281 known_alignment = true;
282 }
283 else if (TREE_CODE (exp) == INDIRECT_REF
284 || TREE_CODE (exp) == MEM_REF
285 || TREE_CODE (exp) == TARGET_MEM_REF)
286 {
287 tree addr = TREE_OPERAND (exp, 0);
288 unsigned ptr_align;
289 unsigned HOST_WIDE_INT ptr_bitpos;
290 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
291
292 /* If the address is explicitely aligned, handle that. */
293 if (TREE_CODE (addr) == BIT_AND_EXPR
294 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
295 {
296 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
297 ptr_bitmask *= BITS_PER_UNIT;
298 align = least_bit_hwi (ptr_bitmask);
299 addr = TREE_OPERAND (addr, 0);
300 }
301
302 known_alignment
303 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
304 align = MAX (ptr_align, align);
305
306 /* Re-apply explicit alignment to the bitpos. */
307 ptr_bitpos &= ptr_bitmask;
308
309 /* The alignment of the pointer operand in a TARGET_MEM_REF
310 has to take the variable offset parts into account. */
311 if (TREE_CODE (exp) == TARGET_MEM_REF)
312 {
313 if (TMR_INDEX (exp))
314 {
315 unsigned HOST_WIDE_INT step = 1;
316 if (TMR_STEP (exp))
317 step = TREE_INT_CST_LOW (TMR_STEP (exp));
318 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
319 }
320 if (TMR_INDEX2 (exp))
321 align = BITS_PER_UNIT;
322 known_alignment = false;
323 }
324
325 /* When EXP is an actual memory reference then we can use
326 TYPE_ALIGN of a pointer indirection to derive alignment.
327 Do so only if get_pointer_alignment_1 did not reveal absolute
328 alignment knowledge and if using that alignment would
329 improve the situation. */
330 unsigned int talign;
331 if (!addr_p && !known_alignment
332 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
333 && talign > align)
334 align = talign;
335 else
336 {
337 /* Else adjust bitpos accordingly. */
338 bitpos += ptr_bitpos;
339 if (TREE_CODE (exp) == MEM_REF
340 || TREE_CODE (exp) == TARGET_MEM_REF)
341 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
342 }
343 }
344 else if (TREE_CODE (exp) == STRING_CST)
345 {
346 /* STRING_CST are the only constant objects we allow to be not
347 wrapped inside a CONST_DECL. */
348 align = TYPE_ALIGN (TREE_TYPE (exp));
349 if (CONSTANT_CLASS_P (exp))
350 align = targetm.constant_alignment (exp, align);
351
352 known_alignment = true;
353 }
354
355 /* If there is a non-constant offset part extract the maximum
356 alignment that can prevail. */
357 if (offset)
358 {
359 unsigned int trailing_zeros = tree_ctz (offset);
360 if (trailing_zeros < HOST_BITS_PER_INT)
361 {
362 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
363 if (inner)
364 align = MIN (align, inner);
365 }
366 }
367
368 /* Account for the alignment of runtime coefficients, so that the constant
369 bitpos is guaranteed to be accurate. */
370 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
371 if (alt_align != 0 && alt_align < align)
372 {
373 align = alt_align;
374 known_alignment = false;
375 }
376
377 *alignp = align;
378 *bitposp = bitpos.coeffs[0] & (align - 1);
379 return known_alignment;
380 }
381
382 /* For a memory reference expression EXP compute values M and N such that M
383 divides (&EXP - N) and such that N < M. If these numbers can be determined,
384 store M in alignp and N in *BITPOSP and return true. Otherwise return false
385 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
386
387 bool
388 get_object_alignment_1 (tree exp, unsigned int *alignp,
389 unsigned HOST_WIDE_INT *bitposp)
390 {
391 /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
392 with it. */
393 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
394 exp = TREE_OPERAND (exp, 0);
395 return get_object_alignment_2 (exp, alignp, bitposp, false);
396 }
397
398 /* Return the alignment in bits of EXP, an object. */
399
400 unsigned int
401 get_object_alignment (tree exp)
402 {
403 unsigned HOST_WIDE_INT bitpos = 0;
404 unsigned int align;
405
406 get_object_alignment_1 (exp, &align, &bitpos);
407
408 /* align and bitpos now specify known low bits of the pointer.
409 ptr & (align - 1) == bitpos. */
410
411 if (bitpos != 0)
412 align = least_bit_hwi (bitpos);
413 return align;
414 }
415
416 /* For a pointer valued expression EXP compute values M and N such that M
417 divides (EXP - N) and such that N < M. If these numbers can be determined,
418 store M in alignp and N in *BITPOSP and return true. Return false if
419 the results are just a conservative approximation.
420
421 If EXP is not a pointer, false is returned too. */
422
423 bool
424 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
425 unsigned HOST_WIDE_INT *bitposp)
426 {
427 STRIP_NOPS (exp);
428
429 if (TREE_CODE (exp) == ADDR_EXPR)
430 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
431 alignp, bitposp, true);
432 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
433 {
434 unsigned int align;
435 unsigned HOST_WIDE_INT bitpos;
436 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
437 &align, &bitpos);
438 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
439 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
440 else
441 {
442 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
443 if (trailing_zeros < HOST_BITS_PER_INT)
444 {
445 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
446 if (inner)
447 align = MIN (align, inner);
448 }
449 }
450 *alignp = align;
451 *bitposp = bitpos & (align - 1);
452 return res;
453 }
454 else if (TREE_CODE (exp) == SSA_NAME
455 && POINTER_TYPE_P (TREE_TYPE (exp)))
456 {
457 unsigned int ptr_align, ptr_misalign;
458 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
459
460 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
461 {
462 *bitposp = ptr_misalign * BITS_PER_UNIT;
463 *alignp = ptr_align * BITS_PER_UNIT;
464 /* Make sure to return a sensible alignment when the multiplication
465 by BITS_PER_UNIT overflowed. */
466 if (*alignp == 0)
467 *alignp = 1u << (HOST_BITS_PER_INT - 1);
468 /* We cannot really tell whether this result is an approximation. */
469 return false;
470 }
471 else
472 {
473 *bitposp = 0;
474 *alignp = BITS_PER_UNIT;
475 return false;
476 }
477 }
478 else if (TREE_CODE (exp) == INTEGER_CST)
479 {
480 *alignp = BIGGEST_ALIGNMENT;
481 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
482 & (BIGGEST_ALIGNMENT - 1));
483 return true;
484 }
485
486 *bitposp = 0;
487 *alignp = BITS_PER_UNIT;
488 return false;
489 }
490
491 /* Return the alignment in bits of EXP, a pointer valued expression.
492 The alignment returned is, by default, the alignment of the thing that
493 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
494
495 Otherwise, look at the expression to see if we can do better, i.e., if the
496 expression is actually pointing at an object whose alignment is tighter. */
497
498 unsigned int
499 get_pointer_alignment (tree exp)
500 {
501 unsigned HOST_WIDE_INT bitpos = 0;
502 unsigned int align;
503
504 get_pointer_alignment_1 (exp, &align, &bitpos);
505
506 /* align and bitpos now specify known low bits of the pointer.
507 ptr & (align - 1) == bitpos. */
508
509 if (bitpos != 0)
510 align = least_bit_hwi (bitpos);
511
512 return align;
513 }
514
515 /* Return the number of leading non-zero elements in the sequence
516 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
517 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
518
519 unsigned
520 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
521 {
522 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
523
524 unsigned n;
525
526 if (eltsize == 1)
527 {
528 /* Optimize the common case of plain char. */
529 for (n = 0; n < maxelts; n++)
530 {
531 const char *elt = (const char*) ptr + n;
532 if (!*elt)
533 break;
534 }
535 }
536 else
537 {
538 for (n = 0; n < maxelts; n++)
539 {
540 const char *elt = (const char*) ptr + n * eltsize;
541 if (!memcmp (elt, "\0\0\0\0", eltsize))
542 break;
543 }
544 }
545 return n;
546 }
547
548 /* Compute the length of a null-terminated character string or wide
549 character string handling character sizes of 1, 2, and 4 bytes.
550 TREE_STRING_LENGTH is not the right way because it evaluates to
551 the size of the character array in bytes (as opposed to characters)
552 and because it can contain a zero byte in the middle.
553
554 ONLY_VALUE should be nonzero if the result is not going to be emitted
555 into the instruction stream and zero if it is going to be expanded.
556 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
557 is returned, otherwise NULL, since
558 len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
559 evaluate the side-effects.
560
561 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
562 accesses. Note that this implies the result is not going to be emitted
563 into the instruction stream.
564
565 Additional information about the string accessed may be recorded
566 in DATA. For example, if ARG references an unterminated string,
567 then the declaration will be stored in the DECL field. If the
568 length of the unterminated string can be determined, it'll be
569 stored in the LEN field. Note this length could well be different
570 than what a C strlen call would return.
571
572 ELTSIZE is 1 for normal single byte character strings, and 2 or
573 4 for wide characer strings. ELTSIZE is by default 1.
574
575 The value returned is of type `ssizetype'. */
576
577 tree
578 c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
579 {
580 /* If we were not passed a DATA pointer, then get one to a local
581 structure. That avoids having to check DATA for NULL before
582 each time we want to use it. */
583 c_strlen_data local_strlen_data = { };
584 if (!data)
585 data = &local_strlen_data;
586
587 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
588
589 tree src = STRIP_NOPS (arg);
590 if (TREE_CODE (src) == COND_EXPR
591 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 {
593 tree len1, len2;
594
595 len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
596 len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
597 if (tree_int_cst_equal (len1, len2))
598 return len1;
599 }
600
601 if (TREE_CODE (src) == COMPOUND_EXPR
602 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
603 return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
604
605 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
606
607 /* Offset from the beginning of the string in bytes. */
608 tree byteoff;
609 tree memsize;
610 tree decl;
611 src = string_constant (src, &byteoff, &memsize, &decl);
612 if (src == 0)
613 return NULL_TREE;
614
615 /* Determine the size of the string element. */
616 if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src)))))
617 return NULL_TREE;
618
619 /* Set MAXELTS to ARRAY_SIZE (SRC) - 1, the maximum possible
620 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
621 in case the latter is less than the size of the array, such as when
622 SRC refers to a short string literal used to initialize a large array.
623 In that case, the elements of the array after the terminating NUL are
624 all NUL. */
625 HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
626 strelts = strelts / eltsize;
627
628 if (!tree_fits_uhwi_p (memsize))
629 return NULL_TREE;
630
631 HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize;
632
633 /* PTR can point to the byte representation of any string type, including
634 char* and wchar_t*. */
635 const char *ptr = TREE_STRING_POINTER (src);
636
637 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
638 {
639 /* The code below works only for single byte character types. */
640 if (eltsize != 1)
641 return NULL_TREE;
642
643 /* If the string has an internal NUL character followed by any
644 non-NUL characters (e.g., "foo\0bar"), we can't compute
645 the offset to the following NUL if we don't know where to
646 start searching for it. */
647 unsigned len = string_length (ptr, eltsize, strelts);
648
649 /* Return when an embedded null character is found or none at all.
650 In the latter case, set the DECL/LEN field in the DATA structure
651 so that callers may examine them. */
652 if (len + 1 < strelts)
653 return NULL_TREE;
654 else if (len >= maxelts)
655 {
656 data->decl = decl;
657 data->off = byteoff;
658 data->minlen = ssize_int (len);
659 return NULL_TREE;
660 }
661
662 /* For empty strings the result should be zero. */
663 if (len == 0)
664 return ssize_int (0);
665
666 /* We don't know the starting offset, but we do know that the string
667 has no internal zero bytes. If the offset falls within the bounds
668 of the string subtract the offset from the length of the string,
669 and return that. Otherwise the length is zero. Take care to
670 use SAVE_EXPR in case the OFFSET has side-effects. */
671 tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
672 : byteoff;
673 offsave = fold_convert_loc (loc, sizetype, offsave);
674 tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
675 size_int (len));
676 tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
677 offsave);
678 lenexp = fold_convert_loc (loc, ssizetype, lenexp);
679 return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
680 build_zero_cst (ssizetype));
681 }
682
683 /* Offset from the beginning of the string in elements. */
684 HOST_WIDE_INT eltoff;
685
686 /* We have a known offset into the string. Start searching there for
687 a null character if we can represent it as a single HOST_WIDE_INT. */
688 if (byteoff == 0)
689 eltoff = 0;
690 else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
691 eltoff = -1;
692 else
693 eltoff = tree_to_uhwi (byteoff) / eltsize;
694
695 /* If the offset is known to be out of bounds, warn, and call strlen at
696 runtime. */
697 if (eltoff < 0 || eltoff >= maxelts)
698 {
699 /* Suppress multiple warnings for propagated constant strings. */
700 if (only_value != 2
701 && !warning_suppressed_p (arg, OPT_Warray_bounds_)
702 && warning_at (loc, OPT_Warray_bounds_,
703 "offset %qwi outside bounds of constant string",
704 eltoff))
705 {
706 if (decl)
707 inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
708 suppress_warning (arg, OPT_Warray_bounds_);
709 }
710 return NULL_TREE;
711 }
712
713 /* If eltoff is larger than strelts but less than maxelts the
714 string length is zero, since the excess memory will be zero. */
715 if (eltoff > strelts)
716 return ssize_int (0);
717
718 /* Use strlen to search for the first zero byte. Since any strings
719 constructed with build_string will have nulls appended, we win even
720 if we get handed something like (char[4])"abcd".
721
722 Since ELTOFF is our starting index into the string, no further
723 calculation is needed. */
724 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
725 strelts - eltoff);
726
727 /* Don't know what to return if there was no zero termination.
728 Ideally this would turn into a gcc_checking_assert over time.
729 Set DECL/LEN so callers can examine them. */
730 if (len >= maxelts - eltoff)
731 {
732 data->decl = decl;
733 data->off = byteoff;
734 data->minlen = ssize_int (len);
735 return NULL_TREE;
736 }
737
738 return ssize_int (len);
739 }
740
741 /* Return a constant integer corresponding to target reading
742 GET_MODE_BITSIZE (MODE) bits from string constant STR. If
743 NULL_TERMINATED_P, reading stops after '\0' character, all further ones
744 are assumed to be zero, otherwise it reads as many characters
745 as needed. */
746
747 rtx
748 c_readstr (const char *str, fixed_size_mode mode,
749 bool null_terminated_p/*=true*/)
750 {
751 auto_vec<target_unit, MAX_BITSIZE_MODE_ANY_INT / BITS_PER_UNIT> bytes;
752
753 bytes.reserve (GET_MODE_SIZE (mode));
754
755 target_unit ch = 1;
756 for (unsigned int i = 0; i < GET_MODE_SIZE (mode); ++i)
757 {
758 if (ch || !null_terminated_p)
759 ch = (unsigned char) str[i];
760 bytes.quick_push (ch);
761 }
762
763 return native_decode_rtx (mode, bytes, 0);
764 }
765
766 /* Cast a target constant CST to target CHAR and if that value fits into
767 host char type, return zero and put that value into variable pointed to by
768 P. */
769
770 static int
771 target_char_cast (tree cst, char *p)
772 {
773 unsigned HOST_WIDE_INT val, hostval;
774
775 if (TREE_CODE (cst) != INTEGER_CST
776 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
777 return 1;
778
779 /* Do not care if it fits or not right here. */
780 val = TREE_INT_CST_LOW (cst);
781
782 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
783 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
784
785 hostval = val;
786 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
787 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
788
789 if (val != hostval)
790 return 1;
791
792 *p = hostval;
793 return 0;
794 }
795
796 /* Similar to save_expr, but assumes that arbitrary code is not executed
797 in between the multiple evaluations. In particular, we assume that a
798 non-addressable local variable will not be modified. */
799
800 static tree
801 builtin_save_expr (tree exp)
802 {
803 if (TREE_CODE (exp) == SSA_NAME
804 || (TREE_ADDRESSABLE (exp) == 0
805 && (TREE_CODE (exp) == PARM_DECL
806 || (VAR_P (exp) && !TREE_STATIC (exp)))))
807 return exp;
808
809 return save_expr (exp);
810 }
811
812 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
813 times to get the address of either a higher stack frame, or a return
814 address located within it (depending on FNDECL_CODE). */
815
816 static rtx
817 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
818 {
819 int i;
820 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
821 if (tem == NULL_RTX)
822 {
823 /* For a zero count with __builtin_return_address, we don't care what
824 frame address we return, because target-specific definitions will
825 override us. Therefore frame pointer elimination is OK, and using
826 the soft frame pointer is OK.
827
828 For a nonzero count, or a zero count with __builtin_frame_address,
829 we require a stable offset from the current frame pointer to the
830 previous one, so we must use the hard frame pointer, and
831 we must disable frame pointer elimination. */
832 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
833 tem = frame_pointer_rtx;
834 else
835 {
836 tem = hard_frame_pointer_rtx;
837
838 /* Tell reload not to eliminate the frame pointer. */
839 crtl->accesses_prior_frames = 1;
840 }
841 }
842
843 if (count > 0)
844 SETUP_FRAME_ADDRESSES ();
845
846 /* On the SPARC, the return address is not in the frame, it is in a
847 register. There is no way to access it off of the current frame
848 pointer, but it can be accessed off the previous frame pointer by
849 reading the value from the register window save area. */
850 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
851 count--;
852
853 /* Scan back COUNT frames to the specified frame. */
854 for (i = 0; i < count; i++)
855 {
856 /* Assume the dynamic chain pointer is in the word that the
857 frame address points to, unless otherwise specified. */
858 tem = DYNAMIC_CHAIN_ADDRESS (tem);
859 tem = memory_address (Pmode, tem);
860 tem = gen_frame_mem (Pmode, tem);
861 tem = copy_to_reg (tem);
862 }
863
864 /* For __builtin_frame_address, return what we've got. But, on
865 the SPARC for example, we may have to add a bias. */
866 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
867 return FRAME_ADDR_RTX (tem);
868
869 /* For __builtin_return_address, get the return address from that frame. */
870 #ifdef RETURN_ADDR_RTX
871 tem = RETURN_ADDR_RTX (count, tem);
872 #else
873 tem = memory_address (Pmode,
874 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
875 tem = gen_frame_mem (Pmode, tem);
876 #endif
877 return tem;
878 }
879
880 /* Alias set used for setjmp buffer. */
881 static alias_set_type setjmp_alias_set = -1;
882
883 /* Construct the leading half of a __builtin_setjmp call. Control will
884 return to RECEIVER_LABEL. This is also called directly by the SJLJ
885 exception handling code. */
886
887 void
888 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
889 {
890 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
891 rtx stack_save;
892 rtx mem;
893
894 if (setjmp_alias_set == -1)
895 setjmp_alias_set = new_alias_set ();
896
897 buf_addr = convert_memory_address (Pmode, buf_addr);
898
899 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
900
901 /* We store the frame pointer and the address of receiver_label in
902 the buffer and use the rest of it for the stack save area, which
903 is machine-dependent. */
904
905 mem = gen_rtx_MEM (Pmode, buf_addr);
906 set_mem_alias_set (mem, setjmp_alias_set);
907 emit_move_insn (mem, hard_frame_pointer_rtx);
908
909 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
910 GET_MODE_SIZE (Pmode))),
911 set_mem_alias_set (mem, setjmp_alias_set);
912
913 emit_move_insn (validize_mem (mem),
914 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
915
916 stack_save = gen_rtx_MEM (sa_mode,
917 plus_constant (Pmode, buf_addr,
918 2 * GET_MODE_SIZE (Pmode)));
919 set_mem_alias_set (stack_save, setjmp_alias_set);
920 emit_stack_save (SAVE_NONLOCAL, &stack_save);
921
922 /* If there is further processing to do, do it. */
923 if (targetm.have_builtin_setjmp_setup ())
924 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
925
926 /* We have a nonlocal label. */
927 cfun->has_nonlocal_label = 1;
928 }
929
930 /* Construct the trailing part of a __builtin_setjmp call. This is
931 also called directly by the SJLJ exception handling code.
932 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
933
934 void
935 expand_builtin_setjmp_receiver (rtx receiver_label)
936 {
937 rtx chain;
938
939 /* Mark the FP as used when we get here, so we have to make sure it's
940 marked as used by this function. */
941 emit_use (hard_frame_pointer_rtx);
942
943 /* Mark the static chain as clobbered here so life information
944 doesn't get messed up for it. */
945 chain = rtx_for_static_chain (current_function_decl, true);
946 if (chain && REG_P (chain))
947 emit_clobber (chain);
948
949 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
950 {
951 /* If the argument pointer can be eliminated in favor of the
952 frame pointer, we don't need to restore it. We assume here
953 that if such an elimination is present, it can always be used.
954 This is the case on all known machines; if we don't make this
955 assumption, we do unnecessary saving on many machines. */
956 size_t i;
957 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
958
959 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
960 if (elim_regs[i].from == ARG_POINTER_REGNUM
961 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
962 break;
963
964 if (i == ARRAY_SIZE (elim_regs))
965 {
966 /* Now restore our arg pointer from the address at which it
967 was saved in our stack frame. */
968 emit_move_insn (crtl->args.internal_arg_pointer,
969 copy_to_reg (get_arg_pointer_save_area ()));
970 }
971 }
972
973 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
974 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
975 else if (targetm.have_nonlocal_goto_receiver ())
976 emit_insn (targetm.gen_nonlocal_goto_receiver ());
977 else
978 { /* Nothing */ }
979
980 /* We must not allow the code we just generated to be reordered by
981 scheduling. Specifically, the update of the frame pointer must
982 happen immediately, not later. */
983 emit_insn (gen_blockage ());
984 }
985
986 /* __builtin_longjmp is passed a pointer to an array of five words (not
987 all will be used on all machines). It operates similarly to the C
988 library function of the same name, but is more efficient. Much of
989 the code below is copied from the handling of non-local gotos. */
990
991 static void
992 expand_builtin_longjmp (rtx buf_addr, rtx value)
993 {
994 rtx fp, lab, stack;
995 rtx_insn *insn, *last;
996 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
997
998 /* DRAP is needed for stack realign if longjmp is expanded to current
999 function */
1000 if (SUPPORTS_STACK_ALIGNMENT)
1001 crtl->need_drap = true;
1002
1003 if (setjmp_alias_set == -1)
1004 setjmp_alias_set = new_alias_set ();
1005
1006 buf_addr = convert_memory_address (Pmode, buf_addr);
1007
1008 buf_addr = force_reg (Pmode, buf_addr);
1009
1010 /* We require that the user must pass a second argument of 1, because
1011 that is what builtin_setjmp will return. */
1012 gcc_assert (value == const1_rtx);
1013
1014 last = get_last_insn ();
1015 if (targetm.have_builtin_longjmp ())
1016 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1017 else
1018 {
1019 fp = gen_rtx_MEM (Pmode, buf_addr);
1020 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1021 GET_MODE_SIZE (Pmode)));
1022
1023 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1024 2 * GET_MODE_SIZE (Pmode)));
1025 set_mem_alias_set (fp, setjmp_alias_set);
1026 set_mem_alias_set (lab, setjmp_alias_set);
1027 set_mem_alias_set (stack, setjmp_alias_set);
1028
1029 /* Pick up FP, label, and SP from the block and jump. This code is
1030 from expand_goto in stmt.cc; see there for detailed comments. */
1031 if (targetm.have_nonlocal_goto ())
1032 /* We have to pass a value to the nonlocal_goto pattern that will
1033 get copied into the static_chain pointer, but it does not matter
1034 what that value is, because builtin_setjmp does not use it. */
1035 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1036 else
1037 {
1038 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1039 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1040
1041 lab = copy_to_reg (lab);
1042
1043 /* Restore the frame pointer and stack pointer. We must use a
1044 temporary since the setjmp buffer may be a local. */
1045 fp = copy_to_reg (fp);
1046 emit_stack_restore (SAVE_NONLOCAL, stack);
1047
1048 /* Ensure the frame pointer move is not optimized. */
1049 emit_insn (gen_blockage ());
1050 emit_clobber (hard_frame_pointer_rtx);
1051 emit_clobber (frame_pointer_rtx);
1052 emit_move_insn (hard_frame_pointer_rtx, fp);
1053
1054 emit_use (hard_frame_pointer_rtx);
1055 emit_use (stack_pointer_rtx);
1056 emit_indirect_jump (lab);
1057 }
1058 }
1059
1060 /* Search backwards and mark the jump insn as a non-local goto.
1061 Note that this precludes the use of __builtin_longjmp to a
1062 __builtin_setjmp target in the same function. However, we've
1063 already cautioned the user that these functions are for
1064 internal exception handling use only. */
1065 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1066 {
1067 gcc_assert (insn != last);
1068
1069 if (JUMP_P (insn))
1070 {
1071 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1072 break;
1073 }
1074 else if (CALL_P (insn))
1075 break;
1076 }
1077 }
1078
1079 static inline bool
1080 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1081 {
1082 return (iter->i < iter->n);
1083 }
1084
1085 /* This function validates the types of a function call argument list
1086 against a specified list of tree_codes. If the last specifier is a 0,
1087 that represents an ellipsis, otherwise the last specifier must be a
1088 VOID_TYPE. */
1089
1090 static bool
1091 validate_arglist (const_tree callexpr, ...)
1092 {
1093 enum tree_code code;
1094 bool res = 0;
1095 va_list ap;
1096 const_call_expr_arg_iterator iter;
1097 const_tree arg;
1098
1099 va_start (ap, callexpr);
1100 init_const_call_expr_arg_iterator (callexpr, &iter);
1101
1102 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1103 tree fn = CALL_EXPR_FN (callexpr);
1104 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1105
1106 for (unsigned argno = 1; ; ++argno)
1107 {
1108 code = (enum tree_code) va_arg (ap, int);
1109
1110 switch (code)
1111 {
1112 case 0:
1113 /* This signifies an ellipses, any further arguments are all ok. */
1114 res = true;
1115 goto end;
1116 case VOID_TYPE:
1117 /* This signifies an endlink, if no arguments remain, return
1118 true, otherwise return false. */
1119 res = !more_const_call_expr_args_p (&iter);
1120 goto end;
1121 case POINTER_TYPE:
1122 /* The actual argument must be nonnull when either the whole
1123 called function has been declared nonnull, or when the formal
1124 argument corresponding to the actual argument has been. */
1125 if (argmap
1126 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1127 {
1128 arg = next_const_call_expr_arg (&iter);
1129 if (!validate_arg (arg, code) || integer_zerop (arg))
1130 goto end;
1131 break;
1132 }
1133 /* FALLTHRU */
1134 default:
1135 /* If no parameters remain or the parameter's code does not
1136 match the specified code, return false. Otherwise continue
1137 checking any remaining arguments. */
1138 arg = next_const_call_expr_arg (&iter);
1139 if (!validate_arg (arg, code))
1140 goto end;
1141 break;
1142 }
1143 }
1144
1145 /* We need gotos here since we can only have one VA_CLOSE in a
1146 function. */
1147 end: ;
1148 va_end (ap);
1149
1150 BITMAP_FREE (argmap);
1151
1152 if (res)
1153 for (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (TREE_TYPE (fn)));
1154 (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
1155 attrs = TREE_CHAIN (attrs))
1156 {
1157 tree args = TREE_VALUE (attrs);
1158 unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
1159 unsigned int idx2
1160 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
1161 if (idx < (unsigned) call_expr_nargs (callexpr)
1162 && idx2 < (unsigned) call_expr_nargs (callexpr)
1163 && POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx)))
1164 && integer_zerop (CALL_EXPR_ARG (callexpr, idx))
1165 && INTEGRAL_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (callexpr, idx2)))
1166 && integer_nonzerop (CALL_EXPR_ARG (callexpr, idx2)))
1167 return false;
1168 }
1169
1170 return res;
1171 }
1172
1173 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1174 and the address of the save area. */
1175
1176 static rtx
1177 expand_builtin_nonlocal_goto (tree exp)
1178 {
1179 tree t_label, t_save_area;
1180 rtx r_label, r_save_area, r_fp, r_sp;
1181 rtx_insn *insn;
1182
1183 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1184 return NULL_RTX;
1185
1186 t_label = CALL_EXPR_ARG (exp, 0);
1187 t_save_area = CALL_EXPR_ARG (exp, 1);
1188
1189 r_label = expand_normal (t_label);
1190 r_label = convert_memory_address (Pmode, r_label);
1191 r_save_area = expand_normal (t_save_area);
1192 r_save_area = convert_memory_address (Pmode, r_save_area);
1193 /* Copy the address of the save location to a register just in case it was
1194 based on the frame pointer. */
1195 r_save_area = copy_to_reg (r_save_area);
1196 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1197 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1198 plus_constant (Pmode, r_save_area,
1199 GET_MODE_SIZE (Pmode)));
1200
1201 crtl->has_nonlocal_goto = 1;
1202
1203 /* ??? We no longer need to pass the static chain value, afaik. */
1204 if (targetm.have_nonlocal_goto ())
1205 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1206 else
1207 {
1208 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1209 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1210
1211 r_label = copy_to_reg (r_label);
1212
1213 /* Restore the frame pointer and stack pointer. We must use a
1214 temporary since the setjmp buffer may be a local. */
1215 r_fp = copy_to_reg (r_fp);
1216 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1217
1218 /* Ensure the frame pointer move is not optimized. */
1219 emit_insn (gen_blockage ());
1220 emit_clobber (hard_frame_pointer_rtx);
1221 emit_clobber (frame_pointer_rtx);
1222 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1223
1224 /* USE of hard_frame_pointer_rtx added for consistency;
1225 not clear if really needed. */
1226 emit_use (hard_frame_pointer_rtx);
1227 emit_use (stack_pointer_rtx);
1228
1229 /* If the architecture is using a GP register, we must
1230 conservatively assume that the target function makes use of it.
1231 The prologue of functions with nonlocal gotos must therefore
1232 initialize the GP register to the appropriate value, and we
1233 must then make sure that this value is live at the point
1234 of the jump. (Note that this doesn't necessarily apply
1235 to targets with a nonlocal_goto pattern; they are free
1236 to implement it in their own way. Note also that this is
1237 a no-op if the GP register is a global invariant.) */
1238 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1239 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1240 emit_use (pic_offset_table_rtx);
1241
1242 emit_indirect_jump (r_label);
1243 }
1244
1245 /* Search backwards to the jump insn and mark it as a
1246 non-local goto. */
1247 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1248 {
1249 if (JUMP_P (insn))
1250 {
1251 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1252 break;
1253 }
1254 else if (CALL_P (insn))
1255 break;
1256 }
1257
1258 return const0_rtx;
1259 }
1260
1261 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1262 (not all will be used on all machines) that was passed to __builtin_setjmp.
1263 It updates the stack pointer in that block to the current value. This is
1264 also called directly by the SJLJ exception handling code. */
1265
1266 void
1267 expand_builtin_update_setjmp_buf (rtx buf_addr)
1268 {
1269 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1270 buf_addr = convert_memory_address (Pmode, buf_addr);
1271 rtx stack_save
1272 = gen_rtx_MEM (sa_mode,
1273 memory_address
1274 (sa_mode,
1275 plus_constant (Pmode, buf_addr,
1276 2 * GET_MODE_SIZE (Pmode))));
1277
1278 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1279 }
1280
1281 /* Expand a call to __builtin_prefetch. For a target that does not support
1282 data prefetch, evaluate the memory address argument in case it has side
1283 effects. */
1284
1285 static void
1286 expand_builtin_prefetch (tree exp)
1287 {
1288 tree arg0, arg1, arg2;
1289 int nargs;
1290 rtx op0, op1, op2;
1291
1292 if (!validate_arglist (exp, POINTER_TYPE, 0))
1293 return;
1294
1295 arg0 = CALL_EXPR_ARG (exp, 0);
1296
1297 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1298 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1299 locality). */
1300 nargs = call_expr_nargs (exp);
1301 arg1 = nargs > 1 ? CALL_EXPR_ARG (exp, 1) : NULL_TREE;
1302 arg2 = nargs > 2 ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
1303
1304 /* Argument 0 is an address. */
1305 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1306
1307 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1308 if (arg1 == NULL_TREE)
1309 op1 = const0_rtx;
1310 else if (TREE_CODE (arg1) != INTEGER_CST)
1311 {
1312 error ("second argument to %<__builtin_prefetch%> must be a constant");
1313 op1 = const0_rtx;
1314 }
1315 else
1316 op1 = expand_normal (arg1);
1317 /* Argument 1 must be 0, 1 or 2. */
1318 if (!IN_RANGE (INTVAL (op1), 0, 2))
1319 {
1320 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1321 " using zero");
1322 op1 = const0_rtx;
1323 }
1324
1325 /* Argument 2 (locality) must be a compile-time constant int. */
1326 if (arg2 == NULL_TREE)
1327 op2 = GEN_INT (3);
1328 else if (TREE_CODE (arg2) != INTEGER_CST)
1329 {
1330 error ("third argument to %<__builtin_prefetch%> must be a constant");
1331 op2 = const0_rtx;
1332 }
1333 else
1334 op2 = expand_normal (arg2);
1335 /* Argument 2 must be 0, 1, 2, or 3. */
1336 if (!IN_RANGE (INTVAL (op2), 0, 3))
1337 {
1338 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1339 op2 = const0_rtx;
1340 }
1341
1342 if (targetm.have_prefetch ())
1343 {
1344 class expand_operand ops[3];
1345
1346 create_address_operand (&ops[0], op0);
1347 create_integer_operand (&ops[1], INTVAL (op1));
1348 create_integer_operand (&ops[2], INTVAL (op2));
1349 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1350 return;
1351 }
1352
1353 /* Don't do anything with direct references to volatile memory, but
1354 generate code to handle other side effects. */
1355 if (!MEM_P (op0) && side_effects_p (op0))
1356 emit_insn (op0);
1357 }
1358
1359 /* Get a MEM rtx for expression EXP which is the address of an operand
1360 to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1361 the maximum length of the block of memory that might be accessed or
1362 NULL if unknown. */
1363
1364 rtx
1365 get_memory_rtx (tree exp, tree len)
1366 {
1367 tree orig_exp = exp, base;
1368 rtx addr, mem;
1369
1370 gcc_checking_assert
1371 (ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))));
1372
1373 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1374 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1375 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1376 exp = TREE_OPERAND (exp, 0);
1377
1378 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1379 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1380
1381 /* Get an expression we can use to find the attributes to assign to MEM.
1382 First remove any nops. */
1383 while (CONVERT_EXPR_P (exp)
1384 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1385 exp = TREE_OPERAND (exp, 0);
1386
1387 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1388 (as builtin stringops may alias with anything). */
1389 exp = fold_build2 (MEM_REF,
1390 build_array_type (char_type_node,
1391 build_range_type (sizetype,
1392 size_one_node, len)),
1393 exp, build_int_cst (ptr_type_node, 0));
1394
1395 /* If the MEM_REF has no acceptable address, try to get the base object
1396 from the original address we got, and build an all-aliasing
1397 unknown-sized access to that one. */
1398 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1399 set_mem_attributes (mem, exp, 0);
1400 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1401 && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1402 0))))
1403 {
1404 unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1405 exp = build_fold_addr_expr (base);
1406 exp = fold_build2 (MEM_REF,
1407 build_array_type (char_type_node,
1408 build_range_type (sizetype,
1409 size_zero_node,
1410 NULL)),
1411 exp, build_int_cst (ptr_type_node, 0));
1412 set_mem_attributes (mem, exp, 0);
1413 /* Since we stripped parts make sure the offset is unknown and the
1414 alignment is computed from the original address. */
1415 clear_mem_offset (mem);
1416 set_mem_align (mem, align);
1417 }
1418 set_mem_alias_set (mem, 0);
1419 return mem;
1420 }
1421
1422 /* Built-in functions to perform an untyped call and return. */
1423
1424 /* Wrapper that implicitly applies a delta when getting or setting the
1425 enclosed value. */
1426 template <typename T>
1427 class delta_type
1428 {
1429 T &value; T const delta;
1430 public:
1431 delta_type (T &val, T dlt) : value (val), delta (dlt) {}
1432 operator T () const { return value + delta; }
1433 T operator = (T val) const { value = val - delta; return val; }
1434 };
1435
1436 #define saved_apply_args_size \
1437 (delta_type<int> (this_target_builtins->x_apply_args_size_plus_one, -1))
1438 #define apply_args_mode \
1439 (this_target_builtins->x_apply_args_mode)
1440 #define saved_apply_result_size \
1441 (delta_type<int> (this_target_builtins->x_apply_result_size_plus_one, -1))
1442 #define apply_result_mode \
1443 (this_target_builtins->x_apply_result_mode)
1444
1445 /* Return the size required for the block returned by __builtin_apply_args,
1446 and initialize apply_args_mode. */
1447
1448 static int
1449 apply_args_size (void)
1450 {
1451 int size = saved_apply_args_size;
1452 int align;
1453 unsigned int regno;
1454
1455 /* The values computed by this function never change. */
1456 if (size < 0)
1457 {
1458 /* The first value is the incoming arg-pointer. */
1459 size = GET_MODE_SIZE (Pmode);
1460
1461 /* The second value is the structure value address unless this is
1462 passed as an "invisible" first argument. */
1463 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1464 size += GET_MODE_SIZE (Pmode);
1465
1466 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1467 if (FUNCTION_ARG_REGNO_P (regno))
1468 {
1469 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1470
1471 if (mode != VOIDmode)
1472 {
1473 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1474 if (size % align != 0)
1475 size = CEIL (size, align) * align;
1476 size += GET_MODE_SIZE (mode);
1477 apply_args_mode[regno] = mode;
1478 }
1479 else
1480 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1481 }
1482 else
1483 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1484
1485 saved_apply_args_size = size;
1486 }
1487 return size;
1488 }
1489
1490 /* Return the size required for the block returned by __builtin_apply,
1491 and initialize apply_result_mode. */
1492
1493 static int
1494 apply_result_size (void)
1495 {
1496 int size = saved_apply_result_size;
1497 int align, regno;
1498
1499 /* The values computed by this function never change. */
1500 if (size < 0)
1501 {
1502 size = 0;
1503
1504 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1505 if (targetm.calls.function_value_regno_p (regno))
1506 {
1507 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1508
1509 if (mode != VOIDmode)
1510 {
1511 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1512 if (size % align != 0)
1513 size = CEIL (size, align) * align;
1514 size += GET_MODE_SIZE (mode);
1515 apply_result_mode[regno] = mode;
1516 }
1517 else
1518 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1519 }
1520 else
1521 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1522
1523 /* Allow targets that use untyped_call and untyped_return to override
1524 the size so that machine-specific information can be stored here. */
1525 #ifdef APPLY_RESULT_SIZE
1526 size = APPLY_RESULT_SIZE;
1527 #endif
1528
1529 saved_apply_result_size = size;
1530 }
1531 return size;
1532 }
1533
1534 /* Create a vector describing the result block RESULT. If SAVEP is true,
1535 the result block is used to save the values; otherwise it is used to
1536 restore the values. */
1537
1538 static rtx
1539 result_vector (int savep, rtx result)
1540 {
1541 int regno, size, align, nelts;
1542 fixed_size_mode mode;
1543 rtx reg, mem;
1544 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1545
1546 size = nelts = 0;
1547 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1548 if ((mode = apply_result_mode[regno]) != VOIDmode)
1549 {
1550 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1551 if (size % align != 0)
1552 size = CEIL (size, align) * align;
1553 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1554 mem = adjust_address (result, mode, size);
1555 savevec[nelts++] = (savep
1556 ? gen_rtx_SET (mem, reg)
1557 : gen_rtx_SET (reg, mem));
1558 size += GET_MODE_SIZE (mode);
1559 }
1560 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1561 }
1562
1563 /* Save the state required to perform an untyped call with the same
1564 arguments as were passed to the current function. */
1565
1566 static rtx
1567 expand_builtin_apply_args_1 (void)
1568 {
1569 rtx registers, tem;
1570 int size, align, regno;
1571 fixed_size_mode mode;
1572 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1573
1574 /* Create a block where the arg-pointer, structure value address,
1575 and argument registers can be saved. */
1576 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1577
1578 /* Walk past the arg-pointer and structure value address. */
1579 size = GET_MODE_SIZE (Pmode);
1580 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1581 size += GET_MODE_SIZE (Pmode);
1582
1583 /* Save each register used in calling a function to the block. */
1584 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1585 if ((mode = apply_args_mode[regno]) != VOIDmode)
1586 {
1587 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1588 if (size % align != 0)
1589 size = CEIL (size, align) * align;
1590
1591 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1592
1593 emit_move_insn (adjust_address (registers, mode, size), tem);
1594 size += GET_MODE_SIZE (mode);
1595 }
1596
1597 /* Save the arg pointer to the block. */
1598 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1599 /* We need the pointer as the caller actually passed them to us, not
1600 as we might have pretended they were passed. Make sure it's a valid
1601 operand, as emit_move_insn isn't expected to handle a PLUS. */
1602 if (STACK_GROWS_DOWNWARD)
1603 tem
1604 = force_operand (plus_constant (Pmode, tem,
1605 crtl->args.pretend_args_size),
1606 NULL_RTX);
1607 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1608
1609 size = GET_MODE_SIZE (Pmode);
1610
1611 /* Save the structure value address unless this is passed as an
1612 "invisible" first argument. */
1613 if (struct_incoming_value)
1614 emit_move_insn (adjust_address (registers, Pmode, size),
1615 copy_to_reg (struct_incoming_value));
1616
1617 /* Return the address of the block. */
1618 return copy_addr_to_reg (XEXP (registers, 0));
1619 }
1620
1621 /* __builtin_apply_args returns block of memory allocated on
1622 the stack into which is stored the arg pointer, structure
1623 value address, static chain, and all the registers that might
1624 possibly be used in performing a function call. The code is
1625 moved to the start of the function so the incoming values are
1626 saved. */
1627
1628 static rtx
1629 expand_builtin_apply_args (void)
1630 {
1631 /* Don't do __builtin_apply_args more than once in a function.
1632 Save the result of the first call and reuse it. */
1633 if (apply_args_value != 0)
1634 return apply_args_value;
1635 {
1636 /* When this function is called, it means that registers must be
1637 saved on entry to this function. So we migrate the
1638 call to the first insn of this function. */
1639 rtx temp;
1640
1641 start_sequence ();
1642 temp = expand_builtin_apply_args_1 ();
1643 rtx_insn *seq = get_insns ();
1644 end_sequence ();
1645
1646 apply_args_value = temp;
1647
1648 /* Put the insns after the NOTE that starts the function.
1649 If this is inside a start_sequence, make the outer-level insn
1650 chain current, so the code is placed at the start of the
1651 function. If internal_arg_pointer is a non-virtual pseudo,
1652 it needs to be placed after the function that initializes
1653 that pseudo. */
1654 push_topmost_sequence ();
1655 if (REG_P (crtl->args.internal_arg_pointer)
1656 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1657 emit_insn_before (seq, parm_birth_insn);
1658 else
1659 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1660 pop_topmost_sequence ();
1661 return temp;
1662 }
1663 }
1664
1665 /* Perform an untyped call and save the state required to perform an
1666 untyped return of whatever value was returned by the given function. */
1667
1668 static rtx
1669 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1670 {
1671 int size, align, regno;
1672 fixed_size_mode mode;
1673 rtx incoming_args, result, reg, dest, src;
1674 rtx_call_insn *call_insn;
1675 rtx old_stack_level = 0;
1676 rtx call_fusage = 0;
1677 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1678
1679 arguments = convert_memory_address (Pmode, arguments);
1680
1681 /* Create a block where the return registers can be saved. */
1682 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1683
1684 /* Fetch the arg pointer from the ARGUMENTS block. */
1685 incoming_args = gen_reg_rtx (Pmode);
1686 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1687 if (!STACK_GROWS_DOWNWARD)
1688 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1689 incoming_args, 0, OPTAB_LIB_WIDEN);
1690
1691 /* Push a new argument block and copy the arguments. Do not allow
1692 the (potential) memcpy call below to interfere with our stack
1693 manipulations. */
1694 do_pending_stack_adjust ();
1695 NO_DEFER_POP;
1696
1697 /* Save the stack with nonlocal if available. */
1698 if (targetm.have_save_stack_nonlocal ())
1699 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1700 else
1701 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1702
1703 /* Allocate a block of memory onto the stack and copy the memory
1704 arguments to the outgoing arguments address. We can pass TRUE
1705 as the 4th argument because we just saved the stack pointer
1706 and will restore it right after the call. */
1707 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1708
1709 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1710 may have already set current_function_calls_alloca to true.
1711 current_function_calls_alloca won't be set if argsize is zero,
1712 so we have to guarantee need_drap is true here. */
1713 if (SUPPORTS_STACK_ALIGNMENT)
1714 crtl->need_drap = true;
1715
1716 dest = virtual_outgoing_args_rtx;
1717 if (!STACK_GROWS_DOWNWARD)
1718 {
1719 if (CONST_INT_P (argsize))
1720 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1721 else
1722 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1723 }
1724 dest = gen_rtx_MEM (BLKmode, dest);
1725 set_mem_align (dest, PARM_BOUNDARY);
1726 src = gen_rtx_MEM (BLKmode, incoming_args);
1727 set_mem_align (src, PARM_BOUNDARY);
1728 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1729
1730 /* Refer to the argument block. */
1731 apply_args_size ();
1732 arguments = gen_rtx_MEM (BLKmode, arguments);
1733 set_mem_align (arguments, PARM_BOUNDARY);
1734
1735 /* Walk past the arg-pointer and structure value address. */
1736 size = GET_MODE_SIZE (Pmode);
1737 if (struct_value)
1738 size += GET_MODE_SIZE (Pmode);
1739
1740 /* Restore each of the registers previously saved. Make USE insns
1741 for each of these registers for use in making the call. */
1742 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1743 if ((mode = apply_args_mode[regno]) != VOIDmode)
1744 {
1745 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1746 if (size % align != 0)
1747 size = CEIL (size, align) * align;
1748 reg = gen_rtx_REG (mode, regno);
1749 emit_move_insn (reg, adjust_address (arguments, mode, size));
1750 use_reg (&call_fusage, reg);
1751 size += GET_MODE_SIZE (mode);
1752 }
1753
1754 /* Restore the structure value address unless this is passed as an
1755 "invisible" first argument. */
1756 size = GET_MODE_SIZE (Pmode);
1757 if (struct_value)
1758 {
1759 rtx value = gen_reg_rtx (Pmode);
1760 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1761 emit_move_insn (struct_value, value);
1762 if (REG_P (struct_value))
1763 use_reg (&call_fusage, struct_value);
1764 }
1765
1766 /* All arguments and registers used for the call are set up by now! */
1767 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1768
1769 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1770 and we don't want to load it into a register as an optimization,
1771 because prepare_call_address already did it if it should be done. */
1772 if (GET_CODE (function) != SYMBOL_REF)
1773 function = memory_address (FUNCTION_MODE, function);
1774
1775 /* Generate the actual call instruction and save the return value. */
1776 if (targetm.have_untyped_call ())
1777 {
1778 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1779 rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1780 result_vector (1, result));
1781 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1782 if (CALL_P (insn))
1783 add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1784 emit_insn (seq);
1785 }
1786 else if (targetm.have_call_value ())
1787 {
1788 rtx valreg = 0;
1789
1790 /* Locate the unique return register. It is not possible to
1791 express a call that sets more than one return register using
1792 call_value; use untyped_call for that. In fact, untyped_call
1793 only needs to save the return registers in the given block. */
1794 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1795 if ((mode = apply_result_mode[regno]) != VOIDmode)
1796 {
1797 gcc_assert (!valreg); /* have_untyped_call required. */
1798
1799 valreg = gen_rtx_REG (mode, regno);
1800 }
1801
1802 emit_insn (targetm.gen_call_value (valreg,
1803 gen_rtx_MEM (FUNCTION_MODE, function),
1804 const0_rtx, NULL_RTX, const0_rtx));
1805
1806 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1807 }
1808 else
1809 gcc_unreachable ();
1810
1811 /* Find the CALL insn we just emitted, and attach the register usage
1812 information. */
1813 call_insn = last_call_insn ();
1814 add_function_usage_to (call_insn, call_fusage);
1815
1816 /* Restore the stack. */
1817 if (targetm.have_save_stack_nonlocal ())
1818 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1819 else
1820 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1821 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1822
1823 OK_DEFER_POP;
1824
1825 /* Return the address of the result block. */
1826 result = copy_addr_to_reg (XEXP (result, 0));
1827 return convert_memory_address (ptr_mode, result);
1828 }
1829
1830 /* Perform an untyped return. */
1831
1832 static void
1833 expand_builtin_return (rtx result)
1834 {
1835 int size, align, regno;
1836 fixed_size_mode mode;
1837 rtx reg;
1838 rtx_insn *call_fusage = 0;
1839
1840 result = convert_memory_address (Pmode, result);
1841
1842 apply_result_size ();
1843 result = gen_rtx_MEM (BLKmode, result);
1844
1845 if (targetm.have_untyped_return ())
1846 {
1847 rtx vector = result_vector (0, result);
1848 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1849 emit_barrier ();
1850 return;
1851 }
1852
1853 /* Restore the return value and note that each value is used. */
1854 size = 0;
1855 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1856 if ((mode = apply_result_mode[regno]) != VOIDmode)
1857 {
1858 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1859 if (size % align != 0)
1860 size = CEIL (size, align) * align;
1861 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1862 emit_move_insn (reg, adjust_address (result, mode, size));
1863
1864 push_to_sequence (call_fusage);
1865 emit_use (reg);
1866 call_fusage = get_insns ();
1867 end_sequence ();
1868 size += GET_MODE_SIZE (mode);
1869 }
1870
1871 /* Put the USE insns before the return. */
1872 emit_insn (call_fusage);
1873
1874 /* Return whatever values was restored by jumping directly to the end
1875 of the function. */
1876 expand_naked_return ();
1877 }
1878
1879 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1880
1881 int
1882 type_to_class (tree type)
1883 {
1884 switch (TREE_CODE (type))
1885 {
1886 case VOID_TYPE: return void_type_class;
1887 case INTEGER_TYPE: return integer_type_class;
1888 case ENUMERAL_TYPE: return enumeral_type_class;
1889 case BOOLEAN_TYPE: return boolean_type_class;
1890 case POINTER_TYPE: return pointer_type_class;
1891 case REFERENCE_TYPE: return reference_type_class;
1892 case OFFSET_TYPE: return offset_type_class;
1893 case REAL_TYPE: return real_type_class;
1894 case COMPLEX_TYPE: return complex_type_class;
1895 case FUNCTION_TYPE: return function_type_class;
1896 case METHOD_TYPE: return method_type_class;
1897 case RECORD_TYPE: return record_type_class;
1898 case UNION_TYPE:
1899 case QUAL_UNION_TYPE: return union_type_class;
1900 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1901 ? string_type_class : array_type_class);
1902 case LANG_TYPE: return lang_type_class;
1903 case OPAQUE_TYPE: return opaque_type_class;
1904 case BITINT_TYPE: return bitint_type_class;
1905 case VECTOR_TYPE: return vector_type_class;
1906 default: return no_type_class;
1907 }
1908 }
1909
1910 /* Expand a call EXP to __builtin_classify_type. */
1911
1912 static rtx
1913 expand_builtin_classify_type (tree exp)
1914 {
1915 if (call_expr_nargs (exp))
1916 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1917 return GEN_INT (no_type_class);
1918 }
1919
1920 /* This helper macro, meant to be used in mathfn_built_in below, determines
1921 which among a set of builtin math functions is appropriate for a given type
1922 mode. The `F' (float) and `L' (long double) are automatically generated
1923 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1924 types, there are additional types that are considered with 'F32', 'F64',
1925 'F128', etc. suffixes. */
1926 #define CASE_MATHFN(MATHFN) \
1927 CASE_CFN_##MATHFN: \
1928 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1929 fcodel = BUILT_IN_##MATHFN##L ; break;
1930 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1931 types. */
1932 #define CASE_MATHFN_FLOATN(MATHFN) \
1933 CASE_CFN_##MATHFN: \
1934 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1935 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1936 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1937 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1938 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1939 break;
1940 /* Similar to above, but appends _R after any F/L suffix. */
1941 #define CASE_MATHFN_REENT(MATHFN) \
1942 case CFN_BUILT_IN_##MATHFN##_R: \
1943 case CFN_BUILT_IN_##MATHFN##F_R: \
1944 case CFN_BUILT_IN_##MATHFN##L_R: \
1945 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1946 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1947
1948 /* Return a function equivalent to FN but operating on floating-point
1949 values of type TYPE, or END_BUILTINS if no such function exists.
1950 This is purely an operation on function codes; it does not guarantee
1951 that the target actually has an implementation of the function. */
1952
1953 static built_in_function
1954 mathfn_built_in_2 (tree type, combined_fn fn)
1955 {
1956 tree mtype;
1957 built_in_function fcode, fcodef, fcodel;
1958 built_in_function fcodef16 = END_BUILTINS;
1959 built_in_function fcodef32 = END_BUILTINS;
1960 built_in_function fcodef64 = END_BUILTINS;
1961 built_in_function fcodef128 = END_BUILTINS;
1962 built_in_function fcodef32x = END_BUILTINS;
1963 built_in_function fcodef64x = END_BUILTINS;
1964 built_in_function fcodef128x = END_BUILTINS;
1965
1966 /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1967 break the uses below. */
1968 #undef HUGE_VAL
1969 #undef NAN
1970
1971 switch (fn)
1972 {
1973 #define SEQ_OF_CASE_MATHFN \
1974 CASE_MATHFN_FLOATN (ACOS) \
1975 CASE_MATHFN_FLOATN (ACOSH) \
1976 CASE_MATHFN_FLOATN (ASIN) \
1977 CASE_MATHFN_FLOATN (ASINH) \
1978 CASE_MATHFN_FLOATN (ATAN) \
1979 CASE_MATHFN_FLOATN (ATAN2) \
1980 CASE_MATHFN_FLOATN (ATANH) \
1981 CASE_MATHFN_FLOATN (CBRT) \
1982 CASE_MATHFN_FLOATN (CEIL) \
1983 CASE_MATHFN (CEXPI) \
1984 CASE_MATHFN_FLOATN (COPYSIGN) \
1985 CASE_MATHFN_FLOATN (COS) \
1986 CASE_MATHFN_FLOATN (COSH) \
1987 CASE_MATHFN (DREM) \
1988 CASE_MATHFN_FLOATN (ERF) \
1989 CASE_MATHFN_FLOATN (ERFC) \
1990 CASE_MATHFN_FLOATN (EXP) \
1991 CASE_MATHFN (EXP10) \
1992 CASE_MATHFN_FLOATN (EXP2) \
1993 CASE_MATHFN_FLOATN (EXPM1) \
1994 CASE_MATHFN_FLOATN (FABS) \
1995 CASE_MATHFN_FLOATN (FDIM) \
1996 CASE_MATHFN_FLOATN (FLOOR) \
1997 CASE_MATHFN_FLOATN (FMA) \
1998 CASE_MATHFN_FLOATN (FMAX) \
1999 CASE_MATHFN_FLOATN (FMIN) \
2000 CASE_MATHFN_FLOATN (FMOD) \
2001 CASE_MATHFN_FLOATN (FREXP) \
2002 CASE_MATHFN (GAMMA) \
2003 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
2004 CASE_MATHFN_FLOATN (HUGE_VAL) \
2005 CASE_MATHFN_FLOATN (HYPOT) \
2006 CASE_MATHFN_FLOATN (ILOGB) \
2007 CASE_MATHFN (ICEIL) \
2008 CASE_MATHFN (IFLOOR) \
2009 CASE_MATHFN_FLOATN (INF) \
2010 CASE_MATHFN (IRINT) \
2011 CASE_MATHFN (IROUND) \
2012 CASE_MATHFN (ISINF) \
2013 CASE_MATHFN (J0) \
2014 CASE_MATHFN (J1) \
2015 CASE_MATHFN (JN) \
2016 CASE_MATHFN (LCEIL) \
2017 CASE_MATHFN_FLOATN (LDEXP) \
2018 CASE_MATHFN (LFLOOR) \
2019 CASE_MATHFN_FLOATN (LGAMMA) \
2020 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
2021 CASE_MATHFN (LLCEIL) \
2022 CASE_MATHFN (LLFLOOR) \
2023 CASE_MATHFN_FLOATN (LLRINT) \
2024 CASE_MATHFN_FLOATN (LLROUND) \
2025 CASE_MATHFN_FLOATN (LOG) \
2026 CASE_MATHFN_FLOATN (LOG10) \
2027 CASE_MATHFN_FLOATN (LOG1P) \
2028 CASE_MATHFN_FLOATN (LOG2) \
2029 CASE_MATHFN_FLOATN (LOGB) \
2030 CASE_MATHFN_FLOATN (LRINT) \
2031 CASE_MATHFN_FLOATN (LROUND) \
2032 CASE_MATHFN_FLOATN (MODF) \
2033 CASE_MATHFN_FLOATN (NAN) \
2034 CASE_MATHFN_FLOATN (NANS) \
2035 CASE_MATHFN_FLOATN (NEARBYINT) \
2036 CASE_MATHFN_FLOATN (NEXTAFTER) \
2037 CASE_MATHFN (NEXTTOWARD) \
2038 CASE_MATHFN_FLOATN (POW) \
2039 CASE_MATHFN (POWI) \
2040 CASE_MATHFN (POW10) \
2041 CASE_MATHFN_FLOATN (REMAINDER) \
2042 CASE_MATHFN_FLOATN (REMQUO) \
2043 CASE_MATHFN_FLOATN (RINT) \
2044 CASE_MATHFN_FLOATN (ROUND) \
2045 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2046 CASE_MATHFN (SCALB) \
2047 CASE_MATHFN_FLOATN (SCALBLN) \
2048 CASE_MATHFN_FLOATN (SCALBN) \
2049 CASE_MATHFN (SIGNBIT) \
2050 CASE_MATHFN (SIGNIFICAND) \
2051 CASE_MATHFN_FLOATN (SIN) \
2052 CASE_MATHFN (SINCOS) \
2053 CASE_MATHFN_FLOATN (SINH) \
2054 CASE_MATHFN_FLOATN (SQRT) \
2055 CASE_MATHFN_FLOATN (TAN) \
2056 CASE_MATHFN_FLOATN (TANH) \
2057 CASE_MATHFN_FLOATN (TGAMMA) \
2058 CASE_MATHFN_FLOATN (TRUNC) \
2059 CASE_MATHFN (Y0) \
2060 CASE_MATHFN (Y1) \
2061 CASE_MATHFN (YN)
2062
2063 SEQ_OF_CASE_MATHFN
2064
2065 default:
2066 return END_BUILTINS;
2067 }
2068
2069 mtype = TYPE_MAIN_VARIANT (type);
2070 if (mtype == double_type_node)
2071 return fcode;
2072 else if (mtype == float_type_node)
2073 return fcodef;
2074 else if (mtype == long_double_type_node)
2075 return fcodel;
2076 else if (mtype == float16_type_node)
2077 return fcodef16;
2078 else if (mtype == float32_type_node)
2079 return fcodef32;
2080 else if (mtype == float64_type_node)
2081 return fcodef64;
2082 else if (mtype == float128_type_node)
2083 return fcodef128;
2084 else if (mtype == float32x_type_node)
2085 return fcodef32x;
2086 else if (mtype == float64x_type_node)
2087 return fcodef64x;
2088 else if (mtype == float128x_type_node)
2089 return fcodef128x;
2090 else
2091 return END_BUILTINS;
2092 }
2093
2094 #undef CASE_MATHFN
2095 #undef CASE_MATHFN_FLOATN
2096 #undef CASE_MATHFN_REENT
2097
2098 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2099 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2100 otherwise use the explicit declaration. If we can't do the conversion,
2101 return null. */
2102
2103 static tree
2104 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2105 {
2106 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2107 if (fcode2 == END_BUILTINS)
2108 return NULL_TREE;
2109
2110 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2111 return NULL_TREE;
2112
2113 return builtin_decl_explicit (fcode2);
2114 }
2115
2116 /* Like mathfn_built_in_1, but always use the implicit array. */
2117
2118 tree
2119 mathfn_built_in (tree type, combined_fn fn)
2120 {
2121 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2122 }
2123
2124 /* Like mathfn_built_in_1, but always use the explicit array. */
2125
2126 tree
2127 mathfn_built_in_explicit (tree type, combined_fn fn)
2128 {
2129 return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2130 }
2131
2132 /* Like mathfn_built_in_1, but take a built_in_function and
2133 always use the implicit array. */
2134
2135 tree
2136 mathfn_built_in (tree type, enum built_in_function fn)
2137 {
2138 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2139 }
2140
2141 /* Return the type associated with a built in function, i.e., the one
2142 to be passed to mathfn_built_in to get the type-specific
2143 function. */
2144
2145 tree
2146 mathfn_built_in_type (combined_fn fn)
2147 {
2148 #define CASE_MATHFN(MATHFN) \
2149 case CFN_BUILT_IN_##MATHFN: \
2150 return double_type_node; \
2151 case CFN_BUILT_IN_##MATHFN##F: \
2152 return float_type_node; \
2153 case CFN_BUILT_IN_##MATHFN##L: \
2154 return long_double_type_node;
2155
2156 #define CASE_MATHFN_FLOATN(MATHFN) \
2157 CASE_MATHFN(MATHFN) \
2158 case CFN_BUILT_IN_##MATHFN##F16: \
2159 return float16_type_node; \
2160 case CFN_BUILT_IN_##MATHFN##F32: \
2161 return float32_type_node; \
2162 case CFN_BUILT_IN_##MATHFN##F64: \
2163 return float64_type_node; \
2164 case CFN_BUILT_IN_##MATHFN##F128: \
2165 return float128_type_node; \
2166 case CFN_BUILT_IN_##MATHFN##F32X: \
2167 return float32x_type_node; \
2168 case CFN_BUILT_IN_##MATHFN##F64X: \
2169 return float64x_type_node; \
2170 case CFN_BUILT_IN_##MATHFN##F128X: \
2171 return float128x_type_node;
2172
2173 /* Similar to above, but appends _R after any F/L suffix. */
2174 #define CASE_MATHFN_REENT(MATHFN) \
2175 case CFN_BUILT_IN_##MATHFN##_R: \
2176 return double_type_node; \
2177 case CFN_BUILT_IN_##MATHFN##F_R: \
2178 return float_type_node; \
2179 case CFN_BUILT_IN_##MATHFN##L_R: \
2180 return long_double_type_node;
2181
2182 switch (fn)
2183 {
2184 SEQ_OF_CASE_MATHFN
2185
2186 default:
2187 return NULL_TREE;
2188 }
2189
2190 #undef CASE_MATHFN
2191 #undef CASE_MATHFN_FLOATN
2192 #undef CASE_MATHFN_REENT
2193 #undef SEQ_OF_CASE_MATHFN
2194 }
2195
2196 /* Check whether there is an internal function associated with function FN
2197 and return type RETURN_TYPE. Return the function if so, otherwise return
2198 IFN_LAST.
2199
2200 Note that this function only tests whether the function is defined in
2201 internals.def, not whether it is actually available on the target. */
2202
2203 static internal_fn
2204 associated_internal_fn (built_in_function fn, tree return_type)
2205 {
2206 switch (fn)
2207 {
2208 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2209 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2210 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2211 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2212 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2213 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2214 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2215 #include "internal-fn.def"
2216
2217 CASE_FLT_FN (BUILT_IN_POW10):
2218 return IFN_EXP10;
2219
2220 CASE_FLT_FN (BUILT_IN_DREM):
2221 return IFN_REMAINDER;
2222
2223 CASE_FLT_FN (BUILT_IN_SCALBN):
2224 CASE_FLT_FN (BUILT_IN_SCALBLN):
2225 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2226 return IFN_LDEXP;
2227 return IFN_LAST;
2228 case BUILT_IN_CRC8_DATA8:
2229 case BUILT_IN_CRC16_DATA8:
2230 case BUILT_IN_CRC16_DATA16:
2231 case BUILT_IN_CRC32_DATA8:
2232 case BUILT_IN_CRC32_DATA16:
2233 case BUILT_IN_CRC32_DATA32:
2234 case BUILT_IN_CRC64_DATA8:
2235 case BUILT_IN_CRC64_DATA16:
2236 case BUILT_IN_CRC64_DATA32:
2237 case BUILT_IN_CRC64_DATA64:
2238 return IFN_CRC;
2239 case BUILT_IN_REV_CRC8_DATA8:
2240 case BUILT_IN_REV_CRC16_DATA8:
2241 case BUILT_IN_REV_CRC16_DATA16:
2242 case BUILT_IN_REV_CRC32_DATA8:
2243 case BUILT_IN_REV_CRC32_DATA16:
2244 case BUILT_IN_REV_CRC32_DATA32:
2245 case BUILT_IN_REV_CRC64_DATA8:
2246 case BUILT_IN_REV_CRC64_DATA16:
2247 case BUILT_IN_REV_CRC64_DATA32:
2248 case BUILT_IN_REV_CRC64_DATA64:
2249 return IFN_CRC_REV;
2250 default:
2251 return IFN_LAST;
2252 }
2253 }
2254
2255 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2256 return its code, otherwise return IFN_LAST. Note that this function
2257 only tests whether the function is defined in internals.def, not whether
2258 it is actually available on the target. */
2259
2260 internal_fn
2261 associated_internal_fn (tree fndecl)
2262 {
2263 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2264 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2265 TREE_TYPE (TREE_TYPE (fndecl)));
2266 }
2267
2268 /* Check whether there is an internal function associated with function CFN
2269 and return type RETURN_TYPE. Return the function if so, otherwise return
2270 IFN_LAST.
2271
2272 Note that this function only tests whether the function is defined in
2273 internals.def, not whether it is actually available on the target. */
2274
2275 internal_fn
2276 associated_internal_fn (combined_fn cfn, tree return_type)
2277 {
2278 if (internal_fn_p (cfn))
2279 return as_internal_fn (cfn);
2280 return associated_internal_fn (as_builtin_fn (cfn), return_type);
2281 }
2282
2283 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2284 on the current target by a call to an internal function, return the
2285 code of that internal function, otherwise return IFN_LAST. The caller
2286 is responsible for ensuring that any side-effects of the built-in
2287 call are dealt with correctly. E.g. if CALL sets errno, the caller
2288 must decide that the errno result isn't needed or make it available
2289 in some other way. */
2290
2291 internal_fn
2292 replacement_internal_fn (gcall *call)
2293 {
2294 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2295 {
2296 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2297 if (ifn != IFN_LAST)
2298 {
2299 tree_pair types = direct_internal_fn_types (ifn, call);
2300 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2301 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2302 return ifn;
2303 }
2304 }
2305 return IFN_LAST;
2306 }
2307
2308 /* Expand a call to the builtin trinary math functions (fma).
2309 Return NULL_RTX if a normal call should be emitted rather than expanding the
2310 function in-line. EXP is the expression that is a call to the builtin
2311 function; if convenient, the result should be placed in TARGET.
2312 SUBTARGET may be used as the target for computing one of EXP's
2313 operands. */
2314
2315 static rtx
2316 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2317 {
2318 optab builtin_optab;
2319 rtx op0, op1, op2, result;
2320 rtx_insn *insns;
2321 tree fndecl = get_callee_fndecl (exp);
2322 tree arg0, arg1, arg2;
2323 machine_mode mode;
2324
2325 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2326 return NULL_RTX;
2327
2328 arg0 = CALL_EXPR_ARG (exp, 0);
2329 arg1 = CALL_EXPR_ARG (exp, 1);
2330 arg2 = CALL_EXPR_ARG (exp, 2);
2331
2332 switch (DECL_FUNCTION_CODE (fndecl))
2333 {
2334 CASE_FLT_FN (BUILT_IN_FMA):
2335 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2336 builtin_optab = fma_optab; break;
2337 default:
2338 gcc_unreachable ();
2339 }
2340
2341 /* Make a suitable register to place result in. */
2342 mode = TYPE_MODE (TREE_TYPE (exp));
2343
2344 /* Before working hard, check whether the instruction is available. */
2345 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2346 return NULL_RTX;
2347
2348 result = gen_reg_rtx (mode);
2349
2350 /* Always stabilize the argument list. */
2351 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2352 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2353 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2354
2355 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2356 op1 = expand_normal (arg1);
2357 op2 = expand_normal (arg2);
2358
2359 start_sequence ();
2360
2361 /* Compute into RESULT.
2362 Set RESULT to wherever the result comes back. */
2363 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2364 result, 0);
2365
2366 /* If we were unable to expand via the builtin, stop the sequence
2367 (without outputting the insns) and call to the library function
2368 with the stabilized argument list. */
2369 if (result == 0)
2370 {
2371 end_sequence ();
2372 return expand_call (exp, target, target == const0_rtx);
2373 }
2374
2375 /* Output the entire sequence. */
2376 insns = get_insns ();
2377 end_sequence ();
2378 emit_insn (insns);
2379
2380 return result;
2381 }
2382
2383 /* Expand a call to the builtin sin and cos math functions.
2384 Return NULL_RTX if a normal call should be emitted rather than expanding the
2385 function in-line. EXP is the expression that is a call to the builtin
2386 function; if convenient, the result should be placed in TARGET.
2387 SUBTARGET may be used as the target for computing one of EXP's
2388 operands. */
2389
2390 static rtx
2391 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2392 {
2393 optab builtin_optab;
2394 rtx op0;
2395 rtx_insn *insns;
2396 tree fndecl = get_callee_fndecl (exp);
2397 machine_mode mode;
2398 tree arg;
2399
2400 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2401 return NULL_RTX;
2402
2403 arg = CALL_EXPR_ARG (exp, 0);
2404
2405 switch (DECL_FUNCTION_CODE (fndecl))
2406 {
2407 CASE_FLT_FN (BUILT_IN_SIN):
2408 CASE_FLT_FN (BUILT_IN_COS):
2409 builtin_optab = sincos_optab; break;
2410 default:
2411 gcc_unreachable ();
2412 }
2413
2414 /* Make a suitable register to place result in. */
2415 mode = TYPE_MODE (TREE_TYPE (exp));
2416
2417 /* Check if sincos insn is available, otherwise fallback
2418 to sin or cos insn. */
2419 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2420 switch (DECL_FUNCTION_CODE (fndecl))
2421 {
2422 CASE_FLT_FN (BUILT_IN_SIN):
2423 builtin_optab = sin_optab; break;
2424 CASE_FLT_FN (BUILT_IN_COS):
2425 builtin_optab = cos_optab; break;
2426 default:
2427 gcc_unreachable ();
2428 }
2429
2430 /* Before working hard, check whether the instruction is available. */
2431 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2432 {
2433 rtx result = gen_reg_rtx (mode);
2434
2435 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2436 need to expand the argument again. This way, we will not perform
2437 side-effects more the once. */
2438 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2439
2440 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2441
2442 start_sequence ();
2443
2444 /* Compute into RESULT.
2445 Set RESULT to wherever the result comes back. */
2446 if (builtin_optab == sincos_optab)
2447 {
2448 int ok;
2449
2450 switch (DECL_FUNCTION_CODE (fndecl))
2451 {
2452 CASE_FLT_FN (BUILT_IN_SIN):
2453 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2454 break;
2455 CASE_FLT_FN (BUILT_IN_COS):
2456 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2457 break;
2458 default:
2459 gcc_unreachable ();
2460 }
2461 gcc_assert (ok);
2462 }
2463 else
2464 result = expand_unop (mode, builtin_optab, op0, result, 0);
2465
2466 if (result != 0)
2467 {
2468 /* Output the entire sequence. */
2469 insns = get_insns ();
2470 end_sequence ();
2471 emit_insn (insns);
2472 return result;
2473 }
2474
2475 /* If we were unable to expand via the builtin, stop the sequence
2476 (without outputting the insns) and call to the library function
2477 with the stabilized argument list. */
2478 end_sequence ();
2479 }
2480
2481 return expand_call (exp, target, target == const0_rtx);
2482 }
2483
2484 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2485 return an RTL instruction code that implements the functionality.
2486 If that isn't possible or available return CODE_FOR_nothing. */
2487
2488 static enum insn_code
2489 interclass_mathfn_icode (tree arg, tree fndecl)
2490 {
2491 bool errno_set = false;
2492 optab builtin_optab = unknown_optab;
2493 machine_mode mode;
2494
2495 switch (DECL_FUNCTION_CODE (fndecl))
2496 {
2497 CASE_FLT_FN (BUILT_IN_ILOGB):
2498 errno_set = true; builtin_optab = ilogb_optab; break;
2499 CASE_FLT_FN (BUILT_IN_ISINF):
2500 builtin_optab = isinf_optab; break;
2501 case BUILT_IN_ISFINITE:
2502 builtin_optab = isfinite_optab;
2503 break;
2504 case BUILT_IN_ISNORMAL:
2505 builtin_optab = isnormal_optab;
2506 break;
2507 CASE_FLT_FN (BUILT_IN_FINITE):
2508 case BUILT_IN_FINITED32:
2509 case BUILT_IN_FINITED64:
2510 case BUILT_IN_FINITED128:
2511 case BUILT_IN_ISINFD32:
2512 case BUILT_IN_ISINFD64:
2513 case BUILT_IN_ISINFD128:
2514 /* These builtins have no optabs (yet). */
2515 break;
2516 default:
2517 gcc_unreachable ();
2518 }
2519
2520 /* There's no easy way to detect the case we need to set EDOM. */
2521 if (flag_errno_math && errno_set)
2522 return CODE_FOR_nothing;
2523
2524 /* Optab mode depends on the mode of the input argument. */
2525 mode = TYPE_MODE (TREE_TYPE (arg));
2526
2527 if (builtin_optab)
2528 return optab_handler (builtin_optab, mode);
2529 return CODE_FOR_nothing;
2530 }
2531
2532 /* Expand a call to one of the builtin math functions that operate on
2533 floating point argument and output an integer result (ilogb, isinf,
2534 isnan, etc).
2535 Return 0 if a normal call should be emitted rather than expanding the
2536 function in-line. EXP is the expression that is a call to the builtin
2537 function; if convenient, the result should be placed in TARGET. */
2538
2539 static rtx
2540 expand_builtin_interclass_mathfn (tree exp, rtx target)
2541 {
2542 enum insn_code icode = CODE_FOR_nothing;
2543 rtx op0;
2544 tree fndecl = get_callee_fndecl (exp);
2545 machine_mode mode;
2546 tree arg;
2547
2548 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2549 return NULL_RTX;
2550
2551 arg = CALL_EXPR_ARG (exp, 0);
2552 icode = interclass_mathfn_icode (arg, fndecl);
2553 mode = TYPE_MODE (TREE_TYPE (arg));
2554
2555 if (icode != CODE_FOR_nothing)
2556 {
2557 class expand_operand ops[1];
2558 rtx_insn *last = get_last_insn ();
2559 tree orig_arg = arg;
2560
2561 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2562 need to expand the argument again. This way, we will not perform
2563 side-effects more the once. */
2564 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2565
2566 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2567
2568 if (mode != GET_MODE (op0))
2569 op0 = convert_to_mode (mode, op0, 0);
2570
2571 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2572 if (maybe_legitimize_operands (icode, 0, 1, ops)
2573 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2574 return ops[0].value;
2575
2576 delete_insns_since (last);
2577 CALL_EXPR_ARG (exp, 0) = orig_arg;
2578 }
2579
2580 return NULL_RTX;
2581 }
2582
2583 /* Expand a call to the builtin sincos math function.
2584 Return NULL_RTX if a normal call should be emitted rather than expanding the
2585 function in-line. EXP is the expression that is a call to the builtin
2586 function. */
2587
2588 static rtx
2589 expand_builtin_sincos (tree exp)
2590 {
2591 rtx op0, op1, op2, target1, target2;
2592 machine_mode mode;
2593 tree arg, sinp, cosp;
2594 int result;
2595 location_t loc = EXPR_LOCATION (exp);
2596 tree alias_type, alias_off;
2597
2598 if (!validate_arglist (exp, REAL_TYPE,
2599 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2600 return NULL_RTX;
2601
2602 arg = CALL_EXPR_ARG (exp, 0);
2603 sinp = CALL_EXPR_ARG (exp, 1);
2604 cosp = CALL_EXPR_ARG (exp, 2);
2605
2606 /* Make a suitable register to place result in. */
2607 mode = TYPE_MODE (TREE_TYPE (arg));
2608
2609 /* Check if sincos insn is available, otherwise emit the call. */
2610 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2611 return NULL_RTX;
2612
2613 target1 = gen_reg_rtx (mode);
2614 target2 = gen_reg_rtx (mode);
2615
2616 op0 = expand_normal (arg);
2617 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2618 alias_off = build_int_cst (alias_type, 0);
2619 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2620 sinp, alias_off));
2621 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2622 cosp, alias_off));
2623
2624 /* Compute into target1 and target2.
2625 Set TARGET to wherever the result comes back. */
2626 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2627 gcc_assert (result);
2628
2629 /* Move target1 and target2 to the memory locations indicated
2630 by op1 and op2. */
2631 emit_move_insn (op1, target1);
2632 emit_move_insn (op2, target2);
2633
2634 return const0_rtx;
2635 }
2636
2637 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2638 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2639 static rtx
2640 expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2641 {
2642 if (!validate_arglist (exp, VOID_TYPE))
2643 return NULL_RTX;
2644
2645 insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2646 if (icode == CODE_FOR_nothing)
2647 return NULL_RTX;
2648
2649 if (target == 0
2650 || GET_MODE (target) != target_mode
2651 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2652 target = gen_reg_rtx (target_mode);
2653
2654 rtx pat = GEN_FCN (icode) (target);
2655 if (!pat)
2656 return NULL_RTX;
2657 emit_insn (pat);
2658
2659 return target;
2660 }
2661
2662 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2663 fenv.h), returning the result and setting it in TARGET. Otherwise return
2664 NULL_RTX on failure. */
2665 static rtx
2666 expand_builtin_feclear_feraise_except (tree exp, rtx target,
2667 machine_mode target_mode, optab op_optab)
2668 {
2669 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2670 return NULL_RTX;
2671 rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2672
2673 insn_code icode = direct_optab_handler (op_optab, SImode);
2674 if (icode == CODE_FOR_nothing)
2675 return NULL_RTX;
2676
2677 if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2678 return NULL_RTX;
2679
2680 if (target == 0
2681 || GET_MODE (target) != target_mode
2682 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2683 target = gen_reg_rtx (target_mode);
2684
2685 rtx pat = GEN_FCN (icode) (target, op0);
2686 if (!pat)
2687 return NULL_RTX;
2688 emit_insn (pat);
2689
2690 return target;
2691 }
2692
2693 /* Expand a call to the internal cexpi builtin to the sincos math function.
2694 EXP is the expression that is a call to the builtin function; if convenient,
2695 the result should be placed in TARGET. */
2696
2697 static rtx
2698 expand_builtin_cexpi (tree exp, rtx target)
2699 {
2700 tree fndecl = get_callee_fndecl (exp);
2701 tree arg, type;
2702 machine_mode mode;
2703 rtx op0, op1, op2;
2704 location_t loc = EXPR_LOCATION (exp);
2705
2706 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2707 return NULL_RTX;
2708
2709 arg = CALL_EXPR_ARG (exp, 0);
2710 type = TREE_TYPE (arg);
2711 mode = TYPE_MODE (TREE_TYPE (arg));
2712
2713 /* Try expanding via a sincos optab, fall back to emitting a libcall
2714 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2715 is only generated from sincos, cexp or if we have either of them. */
2716 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2717 {
2718 op1 = gen_reg_rtx (mode);
2719 op2 = gen_reg_rtx (mode);
2720
2721 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2722
2723 /* Compute into op1 and op2. */
2724 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2725 }
2726 else if (targetm.libc_has_function (function_sincos, type))
2727 {
2728 tree call, fn = NULL_TREE;
2729 tree top1, top2;
2730 rtx op1a, op2a;
2731
2732 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2733 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2734 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2735 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2736 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2737 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2738 else
2739 gcc_unreachable ();
2740
2741 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2742 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2743 op1a = copy_addr_to_reg (XEXP (op1, 0));
2744 op2a = copy_addr_to_reg (XEXP (op2, 0));
2745 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2746 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2747
2748 /* Make sure not to fold the sincos call again. */
2749 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2750 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2751 call, 3, arg, top1, top2));
2752 }
2753 else
2754 {
2755 tree call, fn = NULL_TREE, narg;
2756 tree ctype = build_complex_type (type);
2757
2758 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2759 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2760 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2761 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2762 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2763 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2764 else
2765 gcc_unreachable ();
2766
2767 /* If we don't have a decl for cexp create one. This is the
2768 friendliest fallback if the user calls __builtin_cexpi
2769 without full target C99 function support. */
2770 if (fn == NULL_TREE)
2771 {
2772 tree fntype;
2773 const char *name = NULL;
2774
2775 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2776 name = "cexpf";
2777 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2778 name = "cexp";
2779 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2780 name = "cexpl";
2781
2782 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2783 fn = build_fn_decl (name, fntype);
2784 }
2785
2786 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2787 build_real (type, dconst0), arg);
2788
2789 /* Make sure not to fold the cexp call again. */
2790 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2791 return expand_expr (build_call_nary (ctype, call, 1, narg),
2792 target, VOIDmode, EXPAND_NORMAL);
2793 }
2794
2795 /* Now build the proper return type. */
2796 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2797 make_tree (TREE_TYPE (arg), op2),
2798 make_tree (TREE_TYPE (arg), op1)),
2799 target, VOIDmode, EXPAND_NORMAL);
2800 }
2801
2802 /* Conveniently construct a function call expression. FNDECL names the
2803 function to be called, N is the number of arguments, and the "..."
2804 parameters are the argument expressions. Unlike build_call_exr
2805 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2806
2807 static tree
2808 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2809 {
2810 va_list ap;
2811 tree fntype = TREE_TYPE (fndecl);
2812 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2813
2814 va_start (ap, n);
2815 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2816 va_end (ap);
2817 SET_EXPR_LOCATION (fn, loc);
2818 return fn;
2819 }
2820
2821 /* Expand the __builtin_issignaling builtin. This needs to handle
2822 all floating point formats that do support NaNs (for those that
2823 don't it just sets target to 0). */
2824
2825 static rtx
2826 expand_builtin_issignaling (tree exp, rtx target)
2827 {
2828 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2829 return NULL_RTX;
2830
2831 tree arg = CALL_EXPR_ARG (exp, 0);
2832 scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2833 const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2834
2835 /* Expand the argument yielding a RTX expression. */
2836 rtx temp = expand_normal (arg);
2837
2838 /* If mode doesn't support NaN, always return 0.
2839 Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2840 __builtin_issignaling working without -fsignaling-nans. Especially
2841 when -fno-signaling-nans is the default.
2842 On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2843 -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2844 fold to 0 or non-NaN/Inf classification. */
2845 if (!HONOR_NANS (fmode))
2846 {
2847 emit_move_insn (target, const0_rtx);
2848 return target;
2849 }
2850
2851 /* Check if the back end provides an insn that handles issignaling for the
2852 argument's mode. */
2853 enum insn_code icode = optab_handler (issignaling_optab, fmode);
2854 if (icode != CODE_FOR_nothing)
2855 {
2856 rtx_insn *last = get_last_insn ();
2857 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2858 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2859 return this_target;
2860 delete_insns_since (last);
2861 }
2862
2863 if (DECIMAL_FLOAT_MODE_P (fmode))
2864 {
2865 scalar_int_mode imode;
2866 rtx hi;
2867 switch (fmt->ieee_bits)
2868 {
2869 case 32:
2870 case 64:
2871 imode = int_mode_for_mode (fmode).require ();
2872 temp = gen_lowpart (imode, temp);
2873 break;
2874 case 128:
2875 imode = int_mode_for_size (64, 1).require ();
2876 hi = NULL_RTX;
2877 /* For decimal128, TImode support isn't always there and even when
2878 it is, working on the DImode high part is usually better. */
2879 if (!MEM_P (temp))
2880 {
2881 if (rtx t = force_highpart_subreg (imode, temp, fmode))
2882 hi = t;
2883 else
2884 {
2885 scalar_int_mode imode2;
2886 if (int_mode_for_mode (fmode).exists (&imode2))
2887 {
2888 rtx temp2 = gen_lowpart (imode2, temp);
2889 if (rtx t = force_highpart_subreg (imode, temp2, imode2))
2890 hi = t;
2891 }
2892 }
2893 if (!hi)
2894 {
2895 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2896 emit_move_insn (mem, temp);
2897 temp = mem;
2898 }
2899 }
2900 if (!hi)
2901 {
2902 poly_int64 offset
2903 = subreg_highpart_offset (imode, GET_MODE (temp));
2904 hi = adjust_address (temp, imode, offset);
2905 }
2906 temp = hi;
2907 break;
2908 default:
2909 gcc_unreachable ();
2910 }
2911 /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2912 have 6 bits below it all set. */
2913 rtx val
2914 = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2915 temp = expand_binop (imode, and_optab, temp, val,
2916 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2917 temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2918 return temp;
2919 }
2920
2921 /* Only PDP11 has these defined differently but doesn't support NaNs. */
2922 gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2923 gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2924 gcc_assert (MODE_COMPOSITE_P (fmode)
2925 || (fmt->pnan == fmt->p
2926 && fmt->signbit_ro == fmt->signbit_rw));
2927
2928 switch (fmt->p)
2929 {
2930 case 106: /* IBM double double */
2931 /* For IBM double double, recurse on the most significant double. */
2932 gcc_assert (MODE_COMPOSITE_P (fmode));
2933 temp = convert_modes (DFmode, fmode, temp, 0);
2934 fmode = DFmode;
2935 fmt = REAL_MODE_FORMAT (DFmode);
2936 /* FALLTHRU */
2937 case 8: /* bfloat */
2938 case 11: /* IEEE half */
2939 case 24: /* IEEE single */
2940 case 53: /* IEEE double or Intel extended with rounding to double */
2941 if (fmt->p == 53 && fmt->signbit_ro == 79)
2942 goto extended;
2943 {
2944 scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2945 temp = gen_lowpart (imode, temp);
2946 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2947 & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2948 if (fmt->qnan_msb_set)
2949 {
2950 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2951 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2952 /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2953 ((temp ^ bit) & mask) > val. */
2954 temp = expand_binop (imode, xor_optab, temp, bit,
2955 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2956 temp = expand_binop (imode, and_optab, temp, mask,
2957 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2958 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2959 1, 1);
2960 }
2961 else
2962 {
2963 /* For MIPS/PA IEEE single/double, expand to:
2964 (temp & val) == val. */
2965 temp = expand_binop (imode, and_optab, temp, val,
2966 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2967 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2968 1, 1);
2969 }
2970 }
2971 break;
2972 case 113: /* IEEE quad */
2973 {
2974 rtx hi = NULL_RTX, lo = NULL_RTX;
2975 scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2976 /* For IEEE quad, TImode support isn't always there and even when
2977 it is, working on DImode parts is usually better. */
2978 if (!MEM_P (temp))
2979 {
2980 hi = force_highpart_subreg (imode, temp, fmode);
2981 lo = force_lowpart_subreg (imode, temp, fmode);
2982 if (!hi || !lo)
2983 {
2984 scalar_int_mode imode2;
2985 if (int_mode_for_mode (fmode).exists (&imode2))
2986 {
2987 rtx temp2 = gen_lowpart (imode2, temp);
2988 hi = force_highpart_subreg (imode, temp2, imode2);
2989 lo = force_lowpart_subreg (imode, temp2, imode2);
2990 }
2991 }
2992 if (!hi || !lo)
2993 {
2994 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2995 emit_move_insn (mem, temp);
2996 temp = mem;
2997 }
2998 }
2999 if (!hi || !lo)
3000 {
3001 poly_int64 offset
3002 = subreg_highpart_offset (imode, GET_MODE (temp));
3003 hi = adjust_address (temp, imode, offset);
3004 offset = subreg_lowpart_offset (imode, GET_MODE (temp));
3005 lo = adjust_address (temp, imode, offset);
3006 }
3007 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
3008 & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
3009 if (fmt->qnan_msb_set)
3010 {
3011 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
3012 - 64)));
3013 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
3014 /* For non-MIPS/PA IEEE quad, expand to:
3015 (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
3016 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3017 lo = expand_binop (imode, ior_optab, lo, nlo,
3018 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3019 lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
3020 temp = expand_binop (imode, xor_optab, hi, bit,
3021 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3022 temp = expand_binop (imode, ior_optab, temp, lo,
3023 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3024 temp = expand_binop (imode, and_optab, temp, mask,
3025 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3026 temp = emit_store_flag_force (target, GTU, temp, val, imode,
3027 1, 1);
3028 }
3029 else
3030 {
3031 /* For MIPS/PA IEEE quad, expand to:
3032 (hi & val) == val. */
3033 temp = expand_binop (imode, and_optab, hi, val,
3034 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3035 temp = emit_store_flag_force (target, EQ, temp, val, imode,
3036 1, 1);
3037 }
3038 }
3039 break;
3040 case 64: /* Intel or Motorola extended */
3041 extended:
3042 {
3043 rtx ex, hi, lo;
3044 scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3045 scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3046 if (!MEM_P (temp))
3047 {
3048 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3049 emit_move_insn (mem, temp);
3050 temp = mem;
3051 }
3052 if (fmt->signbit_ro == 95)
3053 {
3054 /* Motorola, always big endian, with 16-bit gap in between
3055 16-bit sign+exponent and 64-bit mantissa. */
3056 ex = adjust_address (temp, iemode, 0);
3057 hi = adjust_address (temp, imode, 4);
3058 lo = adjust_address (temp, imode, 8);
3059 }
3060 else if (!WORDS_BIG_ENDIAN)
3061 {
3062 /* Intel little endian, 64-bit mantissa followed by 16-bit
3063 sign+exponent and then either 16 or 48 bits of gap. */
3064 ex = adjust_address (temp, iemode, 8);
3065 hi = adjust_address (temp, imode, 4);
3066 lo = adjust_address (temp, imode, 0);
3067 }
3068 else
3069 {
3070 /* Big endian Itanium. */
3071 ex = adjust_address (temp, iemode, 0);
3072 hi = adjust_address (temp, imode, 2);
3073 lo = adjust_address (temp, imode, 6);
3074 }
3075 rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3076 gcc_assert (fmt->qnan_msb_set);
3077 rtx mask = GEN_INT (0x7fff);
3078 rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3079 /* For Intel/Motorola extended format, expand to:
3080 (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3081 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3082 lo = expand_binop (imode, ior_optab, lo, nlo,
3083 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3084 lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3085 temp = expand_binop (imode, xor_optab, hi, bit,
3086 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3087 temp = expand_binop (imode, ior_optab, temp, lo,
3088 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3089 temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3090 ex = expand_binop (iemode, and_optab, ex, mask,
3091 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3092 ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3093 ex, mask, iemode, 1, 1);
3094 temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3095 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3096 }
3097 break;
3098 default:
3099 gcc_unreachable ();
3100 }
3101
3102 return temp;
3103 }
3104
3105 /* Expand a call to one of the builtin rounding functions gcc defines
3106 as an extension (lfloor and lceil). As these are gcc extensions we
3107 do not need to worry about setting errno to EDOM.
3108 If expanding via optab fails, lower expression to (int)(floor(x)).
3109 EXP is the expression that is a call to the builtin function;
3110 if convenient, the result should be placed in TARGET. */
3111
3112 static rtx
3113 expand_builtin_int_roundingfn (tree exp, rtx target)
3114 {
3115 convert_optab builtin_optab;
3116 rtx op0, tmp;
3117 rtx_insn *insns;
3118 tree fndecl = get_callee_fndecl (exp);
3119 enum built_in_function fallback_fn;
3120 tree fallback_fndecl;
3121 machine_mode mode;
3122 tree arg;
3123
3124 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3125 return NULL_RTX;
3126
3127 arg = CALL_EXPR_ARG (exp, 0);
3128
3129 switch (DECL_FUNCTION_CODE (fndecl))
3130 {
3131 CASE_FLT_FN (BUILT_IN_ICEIL):
3132 CASE_FLT_FN (BUILT_IN_LCEIL):
3133 CASE_FLT_FN (BUILT_IN_LLCEIL):
3134 builtin_optab = lceil_optab;
3135 fallback_fn = BUILT_IN_CEIL;
3136 break;
3137
3138 CASE_FLT_FN (BUILT_IN_IFLOOR):
3139 CASE_FLT_FN (BUILT_IN_LFLOOR):
3140 CASE_FLT_FN (BUILT_IN_LLFLOOR):
3141 builtin_optab = lfloor_optab;
3142 fallback_fn = BUILT_IN_FLOOR;
3143 break;
3144
3145 default:
3146 gcc_unreachable ();
3147 }
3148
3149 /* Make a suitable register to place result in. */
3150 mode = TYPE_MODE (TREE_TYPE (exp));
3151
3152 target = gen_reg_rtx (mode);
3153
3154 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3155 need to expand the argument again. This way, we will not perform
3156 side-effects more the once. */
3157 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3158
3159 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3160
3161 start_sequence ();
3162
3163 /* Compute into TARGET. */
3164 if (expand_sfix_optab (target, op0, builtin_optab))
3165 {
3166 /* Output the entire sequence. */
3167 insns = get_insns ();
3168 end_sequence ();
3169 emit_insn (insns);
3170 return target;
3171 }
3172
3173 /* If we were unable to expand via the builtin, stop the sequence
3174 (without outputting the insns). */
3175 end_sequence ();
3176
3177 /* Fall back to floating point rounding optab. */
3178 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3179
3180 /* For non-C99 targets we may end up without a fallback fndecl here
3181 if the user called __builtin_lfloor directly. In this case emit
3182 a call to the floor/ceil variants nevertheless. This should result
3183 in the best user experience for not full C99 targets. */
3184 if (fallback_fndecl == NULL_TREE)
3185 {
3186 tree fntype;
3187 const char *name = NULL;
3188
3189 switch (DECL_FUNCTION_CODE (fndecl))
3190 {
3191 case BUILT_IN_ICEIL:
3192 case BUILT_IN_LCEIL:
3193 case BUILT_IN_LLCEIL:
3194 name = "ceil";
3195 break;
3196 case BUILT_IN_ICEILF:
3197 case BUILT_IN_LCEILF:
3198 case BUILT_IN_LLCEILF:
3199 name = "ceilf";
3200 break;
3201 case BUILT_IN_ICEILL:
3202 case BUILT_IN_LCEILL:
3203 case BUILT_IN_LLCEILL:
3204 name = "ceill";
3205 break;
3206 case BUILT_IN_IFLOOR:
3207 case BUILT_IN_LFLOOR:
3208 case BUILT_IN_LLFLOOR:
3209 name = "floor";
3210 break;
3211 case BUILT_IN_IFLOORF:
3212 case BUILT_IN_LFLOORF:
3213 case BUILT_IN_LLFLOORF:
3214 name = "floorf";
3215 break;
3216 case BUILT_IN_IFLOORL:
3217 case BUILT_IN_LFLOORL:
3218 case BUILT_IN_LLFLOORL:
3219 name = "floorl";
3220 break;
3221 default:
3222 gcc_unreachable ();
3223 }
3224
3225 fntype = build_function_type_list (TREE_TYPE (arg),
3226 TREE_TYPE (arg), NULL_TREE);
3227 fallback_fndecl = build_fn_decl (name, fntype);
3228 }
3229
3230 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3231
3232 tmp = expand_normal (exp);
3233 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3234
3235 /* Truncate the result of floating point optab to integer
3236 via expand_fix (). */
3237 target = gen_reg_rtx (mode);
3238 expand_fix (target, tmp, 0);
3239
3240 return target;
3241 }
3242
3243 /* Expand a call to one of the builtin math functions doing integer
3244 conversion (lrint).
3245 Return 0 if a normal call should be emitted rather than expanding the
3246 function in-line. EXP is the expression that is a call to the builtin
3247 function; if convenient, the result should be placed in TARGET. */
3248
3249 static rtx
3250 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3251 {
3252 convert_optab builtin_optab;
3253 rtx op0;
3254 rtx_insn *insns;
3255 tree fndecl = get_callee_fndecl (exp);
3256 tree arg;
3257 machine_mode mode;
3258 enum built_in_function fallback_fn = BUILT_IN_NONE;
3259
3260 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3261 return NULL_RTX;
3262
3263 arg = CALL_EXPR_ARG (exp, 0);
3264
3265 switch (DECL_FUNCTION_CODE (fndecl))
3266 {
3267 CASE_FLT_FN (BUILT_IN_IRINT):
3268 fallback_fn = BUILT_IN_LRINT;
3269 gcc_fallthrough ();
3270 CASE_FLT_FN (BUILT_IN_LRINT):
3271 CASE_FLT_FN (BUILT_IN_LLRINT):
3272 builtin_optab = lrint_optab;
3273 break;
3274
3275 CASE_FLT_FN (BUILT_IN_IROUND):
3276 fallback_fn = BUILT_IN_LROUND;
3277 gcc_fallthrough ();
3278 CASE_FLT_FN (BUILT_IN_LROUND):
3279 CASE_FLT_FN (BUILT_IN_LLROUND):
3280 builtin_optab = lround_optab;
3281 break;
3282
3283 default:
3284 gcc_unreachable ();
3285 }
3286
3287 /* There's no easy way to detect the case we need to set EDOM. */
3288 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3289 return NULL_RTX;
3290
3291 /* Make a suitable register to place result in. */
3292 mode = TYPE_MODE (TREE_TYPE (exp));
3293
3294 /* There's no easy way to detect the case we need to set EDOM. */
3295 if (!flag_errno_math)
3296 {
3297 rtx result = gen_reg_rtx (mode);
3298
3299 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3300 need to expand the argument again. This way, we will not perform
3301 side-effects more the once. */
3302 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3303
3304 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3305
3306 start_sequence ();
3307
3308 if (expand_sfix_optab (result, op0, builtin_optab))
3309 {
3310 /* Output the entire sequence. */
3311 insns = get_insns ();
3312 end_sequence ();
3313 emit_insn (insns);
3314 return result;
3315 }
3316
3317 /* If we were unable to expand via the builtin, stop the sequence
3318 (without outputting the insns) and call to the library function
3319 with the stabilized argument list. */
3320 end_sequence ();
3321 }
3322
3323 if (fallback_fn != BUILT_IN_NONE)
3324 {
3325 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3326 targets, (int) round (x) should never be transformed into
3327 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3328 a call to lround in the hope that the target provides at least some
3329 C99 functions. This should result in the best user experience for
3330 not full C99 targets.
3331 As scalar float conversions with same mode are useless in GIMPLE,
3332 we can end up e.g. with _Float32 argument passed to float builtin,
3333 try to get the type from the builtin prototype first. */
3334 tree fallback_fndecl = NULL_TREE;
3335 if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3336 fallback_fndecl
3337 = mathfn_built_in_1 (TREE_VALUE (argtypes),
3338 as_combined_fn (fallback_fn), 0);
3339 if (fallback_fndecl == NULL_TREE)
3340 fallback_fndecl
3341 = mathfn_built_in_1 (TREE_TYPE (arg),
3342 as_combined_fn (fallback_fn), 0);
3343 if (fallback_fndecl)
3344 {
3345 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3346 fallback_fndecl, 1, arg);
3347
3348 target = expand_call (exp, NULL_RTX, target == const0_rtx);
3349 target = maybe_emit_group_store (target, TREE_TYPE (exp));
3350 return convert_to_mode (mode, target, 0);
3351 }
3352 }
3353
3354 return expand_call (exp, target, target == const0_rtx);
3355 }
3356
3357 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3358 a normal call should be emitted rather than expanding the function
3359 in-line. EXP is the expression that is a call to the builtin
3360 function; if convenient, the result should be placed in TARGET. */
3361
3362 static rtx
3363 expand_builtin_powi (tree exp, rtx target)
3364 {
3365 tree arg0, arg1;
3366 rtx op0, op1;
3367 machine_mode mode;
3368 machine_mode mode2;
3369
3370 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3371 return NULL_RTX;
3372
3373 arg0 = CALL_EXPR_ARG (exp, 0);
3374 arg1 = CALL_EXPR_ARG (exp, 1);
3375 mode = TYPE_MODE (TREE_TYPE (exp));
3376
3377 /* Emit a libcall to libgcc. */
3378
3379 /* Mode of the 2nd argument must match that of an int. */
3380 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3381
3382 if (target == NULL_RTX)
3383 target = gen_reg_rtx (mode);
3384
3385 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3386 if (GET_MODE (op0) != mode)
3387 op0 = convert_to_mode (mode, op0, 0);
3388 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3389 if (GET_MODE (op1) != mode2)
3390 op1 = convert_to_mode (mode2, op1, 0);
3391
3392 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3393 target, LCT_CONST, mode,
3394 op0, mode, op1, mode2);
3395
3396 return target;
3397 }
3398
3399 /* Expand expression EXP which is a call to the strlen builtin. Return
3400 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3401 try to get the result in TARGET, if convenient. */
3402
3403 static rtx
3404 expand_builtin_strlen (tree exp, rtx target,
3405 machine_mode target_mode)
3406 {
3407 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3408 return NULL_RTX;
3409
3410 tree src = CALL_EXPR_ARG (exp, 0);
3411
3412 /* If the length can be computed at compile-time, return it. */
3413 if (tree len = c_strlen (src, 0))
3414 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3415
3416 /* If the length can be computed at compile-time and is constant
3417 integer, but there are side-effects in src, evaluate
3418 src for side-effects, then return len.
3419 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3420 can be optimized into: i++; x = 3; */
3421 tree len = c_strlen (src, 1);
3422 if (len && TREE_CODE (len) == INTEGER_CST)
3423 {
3424 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3425 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3426 }
3427
3428 unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3429
3430 /* If SRC is not a pointer type, don't do this operation inline. */
3431 if (align == 0)
3432 return NULL_RTX;
3433
3434 /* Bail out if we can't compute strlen in the right mode. */
3435 machine_mode insn_mode;
3436 enum insn_code icode = CODE_FOR_nothing;
3437 FOR_EACH_MODE_FROM (insn_mode, target_mode)
3438 {
3439 icode = optab_handler (strlen_optab, insn_mode);
3440 if (icode != CODE_FOR_nothing)
3441 break;
3442 }
3443 if (insn_mode == VOIDmode)
3444 return NULL_RTX;
3445
3446 /* Make a place to hold the source address. We will not expand
3447 the actual source until we are sure that the expansion will
3448 not fail -- there are trees that cannot be expanded twice. */
3449 rtx src_reg = gen_reg_rtx (Pmode);
3450
3451 /* Mark the beginning of the strlen sequence so we can emit the
3452 source operand later. */
3453 rtx_insn *before_strlen = get_last_insn ();
3454
3455 class expand_operand ops[4];
3456 create_output_operand (&ops[0], target, insn_mode);
3457 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3458 create_integer_operand (&ops[2], 0);
3459 create_integer_operand (&ops[3], align);
3460 if (!maybe_expand_insn (icode, 4, ops))
3461 return NULL_RTX;
3462
3463 /* Check to see if the argument was declared attribute nonstring
3464 and if so, issue a warning since at this point it's not known
3465 to be nul-terminated. */
3466 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3467
3468 /* Now that we are assured of success, expand the source. */
3469 start_sequence ();
3470 rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3471 if (pat != src_reg)
3472 {
3473 #ifdef POINTERS_EXTEND_UNSIGNED
3474 if (GET_MODE (pat) != Pmode)
3475 pat = convert_to_mode (Pmode, pat,
3476 POINTERS_EXTEND_UNSIGNED);
3477 #endif
3478 emit_move_insn (src_reg, pat);
3479 }
3480 pat = get_insns ();
3481 end_sequence ();
3482
3483 if (before_strlen)
3484 emit_insn_after (pat, before_strlen);
3485 else
3486 emit_insn_before (pat, get_insns ());
3487
3488 /* Return the value in the proper mode for this function. */
3489 if (GET_MODE (ops[0].value) == target_mode)
3490 target = ops[0].value;
3491 else if (target != 0)
3492 convert_move (target, ops[0].value, 0);
3493 else
3494 target = convert_to_mode (target_mode, ops[0].value, 0);
3495
3496 return target;
3497 }
3498
3499 /* Expand call EXP to the strnlen built-in, returning the result
3500 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3501
3502 static rtx
3503 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3504 {
3505 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3506 return NULL_RTX;
3507
3508 tree src = CALL_EXPR_ARG (exp, 0);
3509 tree bound = CALL_EXPR_ARG (exp, 1);
3510
3511 if (!bound)
3512 return NULL_RTX;
3513
3514 location_t loc = UNKNOWN_LOCATION;
3515 if (EXPR_HAS_LOCATION (exp))
3516 loc = EXPR_LOCATION (exp);
3517
3518 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3519 so these conversions aren't necessary. */
3520 c_strlen_data lendata = { };
3521 tree len = c_strlen (src, 0, &lendata, 1);
3522 if (len)
3523 len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3524
3525 if (TREE_CODE (bound) == INTEGER_CST)
3526 {
3527 if (!len)
3528 return NULL_RTX;
3529
3530 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3531 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3532 }
3533
3534 if (TREE_CODE (bound) != SSA_NAME)
3535 return NULL_RTX;
3536
3537 wide_int min, max;
3538 int_range_max r;
3539 get_global_range_query ()->range_of_expr (r, bound);
3540 if (r.varying_p () || r.undefined_p ())
3541 return NULL_RTX;
3542 min = r.lower_bound ();
3543 max = r.upper_bound ();
3544
3545 if (!len || TREE_CODE (len) != INTEGER_CST)
3546 {
3547 bool exact;
3548 lendata.decl = unterminated_array (src, &len, &exact);
3549 if (!lendata.decl)
3550 return NULL_RTX;
3551 }
3552
3553 if (lendata.decl)
3554 return NULL_RTX;
3555
3556 if (wi::gtu_p (min, wi::to_wide (len)))
3557 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3558
3559 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3560 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3561 }
3562
3563 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3564 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3565 a target constant. */
3566
3567 static rtx
3568 builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3569 fixed_size_mode mode)
3570 {
3571 /* The REPresentation pointed to by DATA need not be a nul-terminated
3572 string but the caller guarantees it's large enough for MODE. */
3573 const char *rep = (const char *) data;
3574
3575 return c_readstr (rep + offset, mode, /*nul_terminated=*/false);
3576 }
3577
3578 /* LEN specify length of the block of memcpy/memset operation.
3579 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3580 In some cases we can make very likely guess on max size, then we
3581 set it into PROBABLE_MAX_SIZE. */
3582
3583 static void
3584 determine_block_size (tree len, rtx len_rtx,
3585 unsigned HOST_WIDE_INT *min_size,
3586 unsigned HOST_WIDE_INT *max_size,
3587 unsigned HOST_WIDE_INT *probable_max_size)
3588 {
3589 if (CONST_INT_P (len_rtx))
3590 {
3591 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3592 return;
3593 }
3594 else
3595 {
3596 wide_int min, max;
3597 enum value_range_kind range_type = VR_UNDEFINED;
3598
3599 /* Determine bounds from the type. */
3600 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3601 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3602 else
3603 *min_size = 0;
3604 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3605 *probable_max_size = *max_size
3606 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3607 else
3608 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3609
3610 if (TREE_CODE (len) == SSA_NAME)
3611 {
3612 int_range_max r;
3613 tree tmin, tmax;
3614 get_global_range_query ()->range_of_expr (r, len);
3615 range_type = get_legacy_range (r, tmin, tmax);
3616 if (range_type != VR_UNDEFINED)
3617 {
3618 min = wi::to_wide (tmin);
3619 max = wi::to_wide (tmax);
3620 }
3621 }
3622 if (range_type == VR_RANGE)
3623 {
3624 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3625 *min_size = min.to_uhwi ();
3626 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3627 *probable_max_size = *max_size = max.to_uhwi ();
3628 }
3629 else if (range_type == VR_ANTI_RANGE)
3630 {
3631 /* Code like
3632
3633 int n;
3634 if (n < 100)
3635 memcpy (a, b, n)
3636
3637 Produce anti range allowing negative values of N. We still
3638 can use the information and make a guess that N is not negative.
3639 */
3640 if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3641 *probable_max_size = min.to_uhwi () - 1;
3642 }
3643 }
3644 gcc_checking_assert (*max_size <=
3645 (unsigned HOST_WIDE_INT)
3646 GET_MODE_MASK (GET_MODE (len_rtx)));
3647 }
3648
3649 /* Expand a call EXP to the memcpy builtin.
3650 Return NULL_RTX if we failed, the caller should emit a normal call,
3651 otherwise try to get the result in TARGET, if convenient (and in
3652 mode MODE if that's convenient). */
3653
3654 static rtx
3655 expand_builtin_memcpy (tree exp, rtx target)
3656 {
3657 if (!validate_arglist (exp,
3658 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3659 return NULL_RTX;
3660
3661 tree dest = CALL_EXPR_ARG (exp, 0);
3662 tree src = CALL_EXPR_ARG (exp, 1);
3663 tree len = CALL_EXPR_ARG (exp, 2);
3664
3665 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3666 /*retmode=*/ RETURN_BEGIN, false);
3667 }
3668
3669 /* Check a call EXP to the memmove built-in for validity.
3670 Return NULL_RTX on both success and failure. */
3671
3672 static rtx
3673 expand_builtin_memmove (tree exp, rtx target)
3674 {
3675 if (!validate_arglist (exp,
3676 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3677 return NULL_RTX;
3678
3679 tree dest = CALL_EXPR_ARG (exp, 0);
3680 tree src = CALL_EXPR_ARG (exp, 1);
3681 tree len = CALL_EXPR_ARG (exp, 2);
3682
3683 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3684 /*retmode=*/ RETURN_BEGIN, true);
3685 }
3686
3687 /* Expand a call EXP to the mempcpy builtin.
3688 Return NULL_RTX if we failed; the caller should emit a normal call,
3689 otherwise try to get the result in TARGET, if convenient (and in
3690 mode MODE if that's convenient). */
3691
3692 static rtx
3693 expand_builtin_mempcpy (tree exp, rtx target)
3694 {
3695 if (!validate_arglist (exp,
3696 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3697 return NULL_RTX;
3698
3699 tree dest = CALL_EXPR_ARG (exp, 0);
3700 tree src = CALL_EXPR_ARG (exp, 1);
3701 tree len = CALL_EXPR_ARG (exp, 2);
3702
3703 /* Policy does not generally allow using compute_objsize (which
3704 is used internally by check_memop_size) to change code generation
3705 or drive optimization decisions.
3706
3707 In this instance it is safe because the code we generate has
3708 the same semantics regardless of the return value of
3709 check_memop_sizes. Exactly the same amount of data is copied
3710 and the return value is exactly the same in both cases.
3711
3712 Furthermore, check_memop_size always uses mode 0 for the call to
3713 compute_objsize, so the imprecise nature of compute_objsize is
3714 avoided. */
3715
3716 /* Avoid expanding mempcpy into memcpy when the call is determined
3717 to overflow the buffer. This also prevents the same overflow
3718 from being diagnosed again when expanding memcpy. */
3719
3720 return expand_builtin_mempcpy_args (dest, src, len,
3721 target, exp, /*retmode=*/ RETURN_END);
3722 }
3723
3724 /* Helper function to do the actual work for expand of memory copy family
3725 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3726 of memory from SRC to DEST and assign to TARGET if convenient. Return
3727 value is based on RETMODE argument. */
3728
3729 static rtx
3730 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3731 rtx target, tree exp, memop_ret retmode,
3732 bool might_overlap)
3733 {
3734 unsigned int src_align = get_pointer_alignment (src);
3735 unsigned int dest_align = get_pointer_alignment (dest);
3736 rtx dest_mem, src_mem, dest_addr, len_rtx;
3737 HOST_WIDE_INT expected_size = -1;
3738 unsigned int expected_align = 0;
3739 unsigned HOST_WIDE_INT min_size;
3740 unsigned HOST_WIDE_INT max_size;
3741 unsigned HOST_WIDE_INT probable_max_size;
3742
3743 bool is_move_done;
3744
3745 /* If DEST is not a pointer type, call the normal function. */
3746 if (dest_align == 0)
3747 return NULL_RTX;
3748
3749 /* If either SRC is not a pointer type, don't do this
3750 operation in-line. */
3751 if (src_align == 0)
3752 return NULL_RTX;
3753
3754 if (currently_expanding_gimple_stmt)
3755 stringop_block_profile (currently_expanding_gimple_stmt,
3756 &expected_align, &expected_size);
3757
3758 if (expected_align < dest_align)
3759 expected_align = dest_align;
3760 dest_mem = get_memory_rtx (dest, len);
3761 set_mem_align (dest_mem, dest_align);
3762 len_rtx = expand_normal (len);
3763 determine_block_size (len, len_rtx, &min_size, &max_size,
3764 &probable_max_size);
3765
3766 /* Try to get the byte representation of the constant SRC points to,
3767 with its byte size in NBYTES. */
3768 unsigned HOST_WIDE_INT nbytes;
3769 const char *rep = getbyterep (src, &nbytes);
3770
3771 /* If the function's constant bound LEN_RTX is less than or equal
3772 to the byte size of the representation of the constant argument,
3773 and if block move would be done by pieces, we can avoid loading
3774 the bytes from memory and only store the computed constant.
3775 This works in the overlap (memmove) case as well because
3776 store_by_pieces just generates a series of stores of constants
3777 from the representation returned by getbyterep(). */
3778 if (rep
3779 && CONST_INT_P (len_rtx)
3780 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3781 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3782 CONST_CAST (char *, rep),
3783 dest_align, false))
3784 {
3785 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3786 builtin_memcpy_read_str,
3787 CONST_CAST (char *, rep),
3788 dest_align, false, retmode);
3789 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3790 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3791 return dest_mem;
3792 }
3793
3794 src_mem = get_memory_rtx (src, len);
3795 set_mem_align (src_mem, src_align);
3796
3797 /* Copy word part most expediently. */
3798 enum block_op_methods method = BLOCK_OP_NORMAL;
3799 if (CALL_EXPR_TAILCALL (exp)
3800 && (retmode == RETURN_BEGIN || target == const0_rtx))
3801 method = BLOCK_OP_TAILCALL;
3802 bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3803 && retmode == RETURN_END
3804 && !might_overlap
3805 && target != const0_rtx);
3806 if (use_mempcpy_call)
3807 method = BLOCK_OP_NO_LIBCALL_RET;
3808 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3809 expected_align, expected_size,
3810 min_size, max_size, probable_max_size,
3811 use_mempcpy_call, &is_move_done,
3812 might_overlap, tree_ctz (len));
3813
3814 /* Bail out when a mempcpy call would be expanded as libcall and when
3815 we have a target that provides a fast implementation
3816 of mempcpy routine. */
3817 if (!is_move_done)
3818 return NULL_RTX;
3819
3820 if (dest_addr == pc_rtx)
3821 return NULL_RTX;
3822
3823 if (dest_addr == 0)
3824 {
3825 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3826 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3827 }
3828
3829 if (retmode != RETURN_BEGIN && target != const0_rtx)
3830 {
3831 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3832 /* stpcpy pointer to last byte. */
3833 if (retmode == RETURN_END_MINUS_ONE)
3834 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3835 }
3836
3837 return dest_addr;
3838 }
3839
3840 static rtx
3841 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3842 rtx target, tree orig_exp, memop_ret retmode)
3843 {
3844 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3845 retmode, false);
3846 }
3847
3848 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3849 we failed, the caller should emit a normal call, otherwise try to
3850 get the result in TARGET, if convenient.
3851 Return value is based on RETMODE argument. */
3852
3853 static rtx
3854 expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3855 {
3856 class expand_operand ops[3];
3857 rtx dest_mem;
3858 rtx src_mem;
3859
3860 if (!targetm.have_movstr ())
3861 return NULL_RTX;
3862
3863 dest_mem = get_memory_rtx (dest, NULL);
3864 src_mem = get_memory_rtx (src, NULL);
3865 if (retmode == RETURN_BEGIN)
3866 {
3867 target = force_reg (Pmode, XEXP (dest_mem, 0));
3868 dest_mem = replace_equiv_address (dest_mem, target);
3869 }
3870
3871 create_output_operand (&ops[0],
3872 retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3873 create_fixed_operand (&ops[1], dest_mem);
3874 create_fixed_operand (&ops[2], src_mem);
3875 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3876 return NULL_RTX;
3877
3878 if (retmode != RETURN_BEGIN && target != const0_rtx)
3879 {
3880 target = ops[0].value;
3881 /* movstr is supposed to set end to the address of the NUL
3882 terminator. If the caller requested a mempcpy-like return value,
3883 adjust it. */
3884 if (retmode == RETURN_END)
3885 {
3886 rtx tem = plus_constant (GET_MODE (target),
3887 gen_lowpart (GET_MODE (target), target), 1);
3888 emit_move_insn (target, force_operand (tem, NULL_RTX));
3889 }
3890 }
3891 return target;
3892 }
3893
3894 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3895 NULL_RTX if we failed the caller should emit a normal call, otherwise
3896 try to get the result in TARGET, if convenient (and in mode MODE if that's
3897 convenient). */
3898
3899 static rtx
3900 expand_builtin_strcpy (tree exp, rtx target)
3901 {
3902 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3903 return NULL_RTX;
3904
3905 tree dest = CALL_EXPR_ARG (exp, 0);
3906 tree src = CALL_EXPR_ARG (exp, 1);
3907
3908 return expand_builtin_strcpy_args (exp, dest, src, target);
3909 }
3910
3911 /* Helper function to do the actual work for expand_builtin_strcpy. The
3912 arguments to the builtin_strcpy call DEST and SRC are broken out
3913 so that this can also be called without constructing an actual CALL_EXPR.
3914 The other arguments and return value are the same as for
3915 expand_builtin_strcpy. */
3916
3917 static rtx
3918 expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3919 {
3920 return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3921 }
3922
3923 /* Expand a call EXP to the stpcpy builtin.
3924 Return NULL_RTX if we failed the caller should emit a normal call,
3925 otherwise try to get the result in TARGET, if convenient (and in
3926 mode MODE if that's convenient). */
3927
3928 static rtx
3929 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3930 {
3931 tree dst, src;
3932 location_t loc = EXPR_LOCATION (exp);
3933
3934 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3935 return NULL_RTX;
3936
3937 dst = CALL_EXPR_ARG (exp, 0);
3938 src = CALL_EXPR_ARG (exp, 1);
3939
3940 /* If return value is ignored, transform stpcpy into strcpy. */
3941 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3942 {
3943 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3944 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3945 return expand_expr (result, target, mode, EXPAND_NORMAL);
3946 }
3947 else
3948 {
3949 tree len, lenp1;
3950 rtx ret;
3951
3952 /* Ensure we get an actual string whose length can be evaluated at
3953 compile-time, not an expression containing a string. This is
3954 because the latter will potentially produce pessimized code
3955 when used to produce the return value. */
3956 c_strlen_data lendata = { };
3957 if (!c_getstr (src)
3958 || !(len = c_strlen (src, 0, &lendata, 1)))
3959 return expand_movstr (dst, src, target,
3960 /*retmode=*/ RETURN_END_MINUS_ONE);
3961
3962 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3963 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3964 target, exp,
3965 /*retmode=*/ RETURN_END_MINUS_ONE);
3966
3967 if (ret)
3968 return ret;
3969
3970 if (TREE_CODE (len) == INTEGER_CST)
3971 {
3972 rtx len_rtx = expand_normal (len);
3973
3974 if (CONST_INT_P (len_rtx))
3975 {
3976 ret = expand_builtin_strcpy_args (exp, dst, src, target);
3977
3978 if (ret)
3979 {
3980 if (! target)
3981 {
3982 if (mode != VOIDmode)
3983 target = gen_reg_rtx (mode);
3984 else
3985 target = gen_reg_rtx (GET_MODE (ret));
3986 }
3987 if (GET_MODE (target) != GET_MODE (ret))
3988 ret = gen_lowpart (GET_MODE (target), ret);
3989
3990 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3991 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3992 gcc_assert (ret);
3993
3994 return target;
3995 }
3996 }
3997 }
3998
3999 return expand_movstr (dst, src, target,
4000 /*retmode=*/ RETURN_END_MINUS_ONE);
4001 }
4002 }
4003
4004 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
4005 arguments while being careful to avoid duplicate warnings (which could
4006 be issued if the expander were to expand the call, resulting in it
4007 being emitted in expand_call(). */
4008
4009 static rtx
4010 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
4011 {
4012 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
4013 {
4014 /* The call has been successfully expanded. Check for nonstring
4015 arguments and issue warnings as appropriate. */
4016 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
4017 return ret;
4018 }
4019
4020 return NULL_RTX;
4021 }
4022
4023 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4024 bytes from constant string DATA + OFFSET and return it as target
4025 constant. */
4026
4027 rtx
4028 builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
4029 fixed_size_mode mode)
4030 {
4031 const char *str = (const char *) data;
4032
4033 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
4034 return const0_rtx;
4035
4036 return c_readstr (str + offset, mode);
4037 }
4038
4039 /* Helper to check the sizes of sequences and the destination of calls
4040 to __builtin_strncat and __builtin___strncat_chk. Returns true on
4041 success (no overflow or invalid sizes), false otherwise. */
4042
4043 static bool
4044 check_strncat_sizes (tree exp, tree objsize)
4045 {
4046 tree dest = CALL_EXPR_ARG (exp, 0);
4047 tree src = CALL_EXPR_ARG (exp, 1);
4048 tree maxread = CALL_EXPR_ARG (exp, 2);
4049
4050 /* Try to determine the range of lengths that the source expression
4051 refers to. */
4052 c_strlen_data lendata = { };
4053 get_range_strlen (src, &lendata, /* eltsize = */ 1);
4054
4055 /* Try to verify that the destination is big enough for the shortest
4056 string. */
4057
4058 access_data data (nullptr, exp, access_read_write, maxread, true);
4059 if (!objsize && warn_stringop_overflow)
4060 {
4061 /* If it hasn't been provided by __strncat_chk, try to determine
4062 the size of the destination object into which the source is
4063 being copied. */
4064 objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4065 }
4066
4067 /* Add one for the terminating nul. */
4068 tree srclen = (lendata.minlen
4069 ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4070 size_one_node)
4071 : NULL_TREE);
4072
4073 /* The strncat function copies at most MAXREAD bytes and always appends
4074 the terminating nul so the specified upper bound should never be equal
4075 to (or greater than) the size of the destination. */
4076 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4077 && tree_int_cst_equal (objsize, maxread))
4078 {
4079 location_t loc = EXPR_LOCATION (exp);
4080 warning_at (loc, OPT_Wstringop_overflow_,
4081 "%qD specified bound %E equals destination size",
4082 get_callee_fndecl (exp), maxread);
4083
4084 return false;
4085 }
4086
4087 if (!srclen
4088 || (maxread && tree_fits_uhwi_p (maxread)
4089 && tree_fits_uhwi_p (srclen)
4090 && tree_int_cst_lt (maxread, srclen)))
4091 srclen = maxread;
4092
4093 /* The number of bytes to write is LEN but check_access will alsoa
4094 check SRCLEN if LEN's value isn't known. */
4095 return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4096 objsize, data.mode, &data);
4097 }
4098
4099 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4100 NULL_RTX if we failed the caller should emit a normal call. */
4101
4102 static rtx
4103 expand_builtin_strncpy (tree exp, rtx target)
4104 {
4105 location_t loc = EXPR_LOCATION (exp);
4106
4107 if (!validate_arglist (exp,
4108 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4109 return NULL_RTX;
4110 tree dest = CALL_EXPR_ARG (exp, 0);
4111 tree src = CALL_EXPR_ARG (exp, 1);
4112 /* The number of bytes to write (not the maximum). */
4113 tree len = CALL_EXPR_ARG (exp, 2);
4114
4115 /* The length of the source sequence. */
4116 tree slen = c_strlen (src, 1);
4117
4118 /* We must be passed a constant len and src parameter. */
4119 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4120 return NULL_RTX;
4121
4122 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4123
4124 /* We're required to pad with trailing zeros if the requested
4125 len is greater than strlen(s2)+1. In that case try to
4126 use store_by_pieces, if it fails, punt. */
4127 if (tree_int_cst_lt (slen, len))
4128 {
4129 unsigned int dest_align = get_pointer_alignment (dest);
4130 const char *p = c_getstr (src);
4131 rtx dest_mem;
4132
4133 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4134 || !can_store_by_pieces (tree_to_uhwi (len),
4135 builtin_strncpy_read_str,
4136 CONST_CAST (char *, p),
4137 dest_align, false))
4138 return NULL_RTX;
4139
4140 dest_mem = get_memory_rtx (dest, len);
4141 store_by_pieces (dest_mem, tree_to_uhwi (len),
4142 builtin_strncpy_read_str,
4143 CONST_CAST (char *, p), dest_align, false,
4144 RETURN_BEGIN);
4145 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4146 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4147 return dest_mem;
4148 }
4149
4150 return NULL_RTX;
4151 }
4152
4153 /* Return the RTL of a register in MODE generated from PREV in the
4154 previous iteration. */
4155
4156 static rtx
4157 gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4158 {
4159 rtx target = nullptr;
4160 if (prev != nullptr && prev->data != nullptr)
4161 {
4162 /* Use the previous data in the same mode. */
4163 if (prev->mode == mode)
4164 return prev->data;
4165
4166 fixed_size_mode prev_mode = prev->mode;
4167
4168 /* Don't use the previous data to write QImode if it is in a
4169 vector mode. */
4170 if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4171 return target;
4172
4173 rtx prev_rtx = prev->data;
4174
4175 if (REG_P (prev_rtx)
4176 && HARD_REGISTER_P (prev_rtx)
4177 && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4178 {
4179 /* This case occurs when PREV_MODE is a vector and when
4180 MODE is too small to store using vector operations.
4181 After register allocation, the code will need to move the
4182 lowpart of the vector register into a non-vector register.
4183
4184 Also, the target has chosen to use a hard register
4185 instead of going with the default choice of using a
4186 pseudo register. We should respect that choice and try to
4187 avoid creating a pseudo register with the same mode as the
4188 current hard register.
4189
4190 In principle, we could just use a lowpart MODE subreg of
4191 the vector register. However, the vector register mode might
4192 be too wide for non-vector registers, and we already know
4193 that the non-vector mode is too small for vector registers.
4194 It's therefore likely that we'd need to spill to memory in
4195 the vector mode and reload the non-vector value from there.
4196
4197 Try to avoid that by reducing the vector register to the
4198 smallest size that it can hold. This should increase the
4199 chances that non-vector registers can hold both the inner
4200 and outer modes of the subreg that we generate later. */
4201 machine_mode m;
4202 fixed_size_mode candidate;
4203 FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4204 if (is_a<fixed_size_mode> (m, &candidate))
4205 {
4206 if (GET_MODE_SIZE (candidate)
4207 >= GET_MODE_SIZE (prev_mode))
4208 break;
4209 if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4210 && lowpart_subreg_regno (REGNO (prev_rtx),
4211 prev_mode, candidate) >= 0)
4212 {
4213 target = lowpart_subreg (candidate, prev_rtx,
4214 prev_mode);
4215 prev_rtx = target;
4216 prev_mode = candidate;
4217 break;
4218 }
4219 }
4220 if (target == nullptr)
4221 prev_rtx = copy_to_reg (prev_rtx);
4222 }
4223
4224 target = lowpart_subreg (mode, prev_rtx, prev_mode);
4225 }
4226 return target;
4227 }
4228
4229 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4230 bytes from constant string DATA + OFFSET and return it as target
4231 constant. If PREV isn't nullptr, it has the RTL info from the
4232 previous iteration. */
4233
4234 rtx
4235 builtin_memset_read_str (void *data, void *prev,
4236 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4237 fixed_size_mode mode)
4238 {
4239 const char *c = (const char *) data;
4240 unsigned int size = GET_MODE_SIZE (mode);
4241
4242 rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4243 mode);
4244 if (target != nullptr)
4245 return target;
4246 rtx src = gen_int_mode (*c, QImode);
4247
4248 if (VECTOR_MODE_P (mode))
4249 {
4250 gcc_assert (GET_MODE_INNER (mode) == QImode);
4251
4252 rtx const_vec = gen_const_vec_duplicate (mode, src);
4253 if (prev == NULL)
4254 /* Return CONST_VECTOR when called by a query function. */
4255 return const_vec;
4256
4257 /* Use the move expander with CONST_VECTOR. */
4258 target = gen_reg_rtx (mode);
4259 emit_move_insn (target, const_vec);
4260 return target;
4261 }
4262
4263 char *p = XALLOCAVEC (char, size);
4264
4265 memset (p, *c, size);
4266
4267 return c_readstr (p, mode);
4268 }
4269
4270 /* Callback routine for store_by_pieces. Return the RTL of a register
4271 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4272 char value given in the RTL register data. For example, if mode is
4273 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4274 nullptr, it has the RTL info from the previous iteration. */
4275
4276 static rtx
4277 builtin_memset_gen_str (void *data, void *prev,
4278 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4279 fixed_size_mode mode)
4280 {
4281 rtx target, coeff;
4282 size_t size;
4283 char *p;
4284
4285 size = GET_MODE_SIZE (mode);
4286 if (size == 1)
4287 return (rtx) data;
4288
4289 target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4290 if (target != nullptr)
4291 return target;
4292
4293 if (VECTOR_MODE_P (mode))
4294 {
4295 gcc_assert (GET_MODE_INNER (mode) == QImode);
4296
4297 /* vec_duplicate_optab is a precondition to pick a vector mode for
4298 the memset expander. */
4299 insn_code icode = optab_handler (vec_duplicate_optab, mode);
4300
4301 target = gen_reg_rtx (mode);
4302 class expand_operand ops[2];
4303 create_output_operand (&ops[0], target, mode);
4304 create_input_operand (&ops[1], (rtx) data, QImode);
4305 expand_insn (icode, 2, ops);
4306 if (!rtx_equal_p (target, ops[0].value))
4307 emit_move_insn (target, ops[0].value);
4308
4309 return target;
4310 }
4311
4312 p = XALLOCAVEC (char, size);
4313 memset (p, 1, size);
4314 coeff = c_readstr (p, mode);
4315
4316 target = convert_to_mode (mode, (rtx) data, 1);
4317 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4318 return force_reg (mode, target);
4319 }
4320
4321 /* Expand expression EXP, which is a call to the memset builtin. Return
4322 NULL_RTX if we failed the caller should emit a normal call, otherwise
4323 try to get the result in TARGET, if convenient (and in mode MODE if that's
4324 convenient). */
4325
4326 rtx
4327 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4328 {
4329 if (!validate_arglist (exp,
4330 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4331 return NULL_RTX;
4332
4333 tree dest = CALL_EXPR_ARG (exp, 0);
4334 tree val = CALL_EXPR_ARG (exp, 1);
4335 tree len = CALL_EXPR_ARG (exp, 2);
4336
4337 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4338 }
4339
4340 /* Check that store_by_pieces allows BITS + LEN (so that we don't
4341 expand something too unreasonably long), and every power of 2 in
4342 BITS. It is assumed that LEN has already been tested by
4343 itself. */
4344 static bool
4345 can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
4346 by_pieces_constfn constfun,
4347 void *constfundata, unsigned int align,
4348 bool memsetp,
4349 unsigned HOST_WIDE_INT len)
4350 {
4351 if (bits
4352 && !can_store_by_pieces (bits + len, constfun, constfundata,
4353 align, memsetp))
4354 return false;
4355
4356 /* BITS set are expected to be generally in the low range and
4357 contiguous. We do NOT want to repeat the test above in case BITS
4358 has a single bit set, so we terminate the loop when BITS == BIT.
4359 In the unlikely case that BITS has the MSB set, also terminate in
4360 case BIT gets shifted out. */
4361 for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
4362 {
4363 if ((bits & bit) == 0)
4364 continue;
4365
4366 if (!can_store_by_pieces (bit, constfun, constfundata,
4367 align, memsetp))
4368 return false;
4369 }
4370
4371 return true;
4372 }
4373
4374 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4375 Return TRUE if successful, FALSE otherwise. TO is assumed to be
4376 aligned at an ALIGN-bits boundary. LEN must be a multiple of
4377 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4378
4379 The strategy is to issue one store_by_pieces for each power of two,
4380 from most to least significant, guarded by a test on whether there
4381 are at least that many bytes left to copy in LEN.
4382
4383 ??? Should we skip some powers of two in favor of loops? Maybe start
4384 at the max of TO/LEN/word alignment, at least when optimizing for
4385 size, instead of ensuring O(log len) dynamic compares? */
4386
4387 bool
4388 try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4389 unsigned HOST_WIDE_INT min_len,
4390 unsigned HOST_WIDE_INT max_len,
4391 rtx val, char valc, unsigned int align)
4392 {
4393 int max_bits = floor_log2 (max_len);
4394 int min_bits = floor_log2 (min_len);
4395 int sctz_len = ctz_len;
4396
4397 gcc_checking_assert (sctz_len >= 0);
4398
4399 if (val)
4400 valc = 1;
4401
4402 /* Bits more significant than TST_BITS are part of the shared prefix
4403 in the binary representation of both min_len and max_len. Since
4404 they're identical, we don't need to test them in the loop. */
4405 int tst_bits = (max_bits != min_bits ? max_bits
4406 : floor_log2 (max_len ^ min_len));
4407
4408 /* Save the pre-blksize values. */
4409 int orig_max_bits = max_bits;
4410 int orig_tst_bits = tst_bits;
4411
4412 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4413 bytes, to lower max_bits. In the unlikely case of a constant LEN
4414 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4415 single store_by_pieces, but otherwise, select the minimum multiple
4416 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4417 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4418 unsigned HOST_WIDE_INT blksize;
4419 if (max_len > min_len)
4420 {
4421 unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4422 align / BITS_PER_UNIT);
4423 blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4424 blksize &= ~(alrng - 1);
4425 }
4426 else if (max_len == min_len)
4427 blksize = max_len;
4428 else
4429 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4430 return false;
4431 if (min_len >= blksize
4432 /* ??? Maybe try smaller fixed-prefix blksizes before
4433 punting? */
4434 && can_store_by_pieces (blksize, builtin_memset_read_str,
4435 &valc, align, true))
4436 {
4437 min_len -= blksize;
4438 min_bits = floor_log2 (min_len);
4439 max_len -= blksize;
4440 max_bits = floor_log2 (max_len);
4441
4442 tst_bits = (max_bits != min_bits ? max_bits
4443 : floor_log2 (max_len ^ min_len));
4444 }
4445 else
4446 blksize = 0;
4447
4448 /* Check that we can use store by pieces for the maximum store count
4449 we may issue (initial fixed-size block, plus conditional
4450 power-of-two-sized from max_bits to ctz_len. */
4451 unsigned HOST_WIDE_INT xlenest = blksize;
4452 if (max_bits >= 0)
4453 xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4454 - (HOST_WIDE_INT_1U << ctz_len));
4455 bool max_loop = false;
4456 bool use_store_by_pieces = true;
4457 /* Skip the test in case of overflow in xlenest. It shouldn't
4458 happen because of the way max_bits and blksize are related, but
4459 it doesn't hurt to test. */
4460 if (blksize > xlenest
4461 || !can_store_by_multiple_pieces (xlenest - blksize,
4462 builtin_memset_read_str,
4463 &valc, align, true, blksize))
4464 {
4465 if (!(flag_inline_stringops & ILSOP_MEMSET))
4466 return false;
4467
4468 for (max_bits = orig_max_bits;
4469 max_bits >= sctz_len;
4470 --max_bits)
4471 {
4472 xlenest = ((HOST_WIDE_INT_1U << max_bits) * 2
4473 - (HOST_WIDE_INT_1U << ctz_len));
4474 /* Check that blksize plus the bits to be stored as blocks
4475 sized at powers of two can be stored by pieces. This is
4476 like the test above, but with smaller max_bits. Skip
4477 orig_max_bits (it would be redundant). Also skip in case
4478 of overflow. */
4479 if (max_bits < orig_max_bits
4480 && xlenest + blksize >= xlenest
4481 && can_store_by_multiple_pieces (xlenest,
4482 builtin_memset_read_str,
4483 &valc, align, true, blksize))
4484 {
4485 max_loop = true;
4486 break;
4487 }
4488 if (blksize
4489 && can_store_by_multiple_pieces (xlenest,
4490 builtin_memset_read_str,
4491 &valc, align, true, 0))
4492 {
4493 max_len += blksize;
4494 min_len += blksize;
4495 tst_bits = orig_tst_bits;
4496 blksize = 0;
4497 max_loop = true;
4498 break;
4499 }
4500 if (max_bits == sctz_len)
4501 {
4502 /* We'll get here if can_store_by_pieces refuses to
4503 store even a single QImode. We'll fall back to
4504 QImode stores then. */
4505 if (!sctz_len)
4506 {
4507 blksize = 0;
4508 max_loop = true;
4509 use_store_by_pieces = false;
4510 break;
4511 }
4512 --sctz_len;
4513 --ctz_len;
4514 }
4515 }
4516 if (!max_loop)
4517 return false;
4518 /* If the boundaries are such that min and max may run a
4519 different number of trips in the initial loop, the remainder
4520 needs not be between the moduli, so set tst_bits to cover all
4521 bits. Otherwise, if the trip counts are the same, max_len
4522 has the common prefix, and the previously-computed tst_bits
4523 is usable. */
4524 if (max_len >> max_bits > min_len >> max_bits)
4525 tst_bits = max_bits;
4526 }
4527
4528 by_pieces_constfn constfun;
4529 void *constfundata;
4530 if (val)
4531 {
4532 constfun = builtin_memset_gen_str;
4533 constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4534 val);
4535 }
4536 else
4537 {
4538 constfun = builtin_memset_read_str;
4539 constfundata = &valc;
4540 }
4541
4542 rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4543 rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4544 to = replace_equiv_address (to, ptr);
4545 set_mem_align (to, align);
4546
4547 if (blksize)
4548 {
4549 to = store_by_pieces (to, blksize,
4550 constfun, constfundata,
4551 align, true,
4552 max_len != 0 ? RETURN_END : RETURN_BEGIN);
4553 if (max_len == 0)
4554 return true;
4555
4556 /* Adjust PTR, TO and REM. Since TO's address is likely
4557 PTR+offset, we have to replace it. */
4558 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4559 to = replace_equiv_address (to, ptr);
4560 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4561 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4562 }
4563
4564 /* Iterate over power-of-two block sizes from the maximum length to
4565 the least significant bit possibly set in the length. */
4566 for (int i = max_bits; i >= sctz_len; i--)
4567 {
4568 rtx_code_label *loop_label = NULL;
4569 rtx_code_label *label = NULL;
4570
4571 blksize = HOST_WIDE_INT_1U << i;
4572
4573 /* If we're past the bits shared between min_ and max_len, expand
4574 a test on the dynamic length, comparing it with the
4575 BLKSIZE. */
4576 if (i <= tst_bits)
4577 {
4578 label = gen_label_rtx ();
4579 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4580 ptr_mode, 1, label,
4581 profile_probability::even ());
4582 }
4583 /* If we are at a bit that is in the prefix shared by min_ and
4584 max_len, skip the current BLKSIZE if the bit is clear, but do
4585 not skip the loop, even if it doesn't require
4586 prechecking. */
4587 else if ((max_len & blksize) == 0
4588 && !(max_loop && i == max_bits))
4589 continue;
4590
4591 if (max_loop && i == max_bits)
4592 {
4593 loop_label = gen_label_rtx ();
4594 emit_label (loop_label);
4595 /* Since we may run this multiple times, don't assume we
4596 know anything about the offset. */
4597 clear_mem_offset (to);
4598 }
4599
4600 bool update_needed = i != sctz_len || loop_label;
4601 rtx next_ptr = NULL_RTX;
4602 if (!use_store_by_pieces)
4603 {
4604 gcc_checking_assert (blksize == 1);
4605 if (!val)
4606 val = gen_int_mode (valc, QImode);
4607 to = change_address (to, QImode, 0);
4608 emit_move_insn (to, val);
4609 if (update_needed)
4610 next_ptr = plus_constant (GET_MODE (ptr), ptr, blksize);
4611 }
4612 else
4613 {
4614 /* Issue a store of BLKSIZE bytes. */
4615 to = store_by_pieces (to, blksize,
4616 constfun, constfundata,
4617 align, true,
4618 update_needed ? RETURN_END : RETURN_BEGIN);
4619 next_ptr = XEXP (to, 0);
4620 }
4621 /* Adjust REM and PTR, unless this is the last iteration. */
4622 if (update_needed)
4623 {
4624 emit_move_insn (ptr, force_operand (next_ptr, NULL_RTX));
4625 to = replace_equiv_address (to, ptr);
4626 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4627 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4628 }
4629
4630 if (loop_label)
4631 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), GE, NULL,
4632 ptr_mode, 1, loop_label,
4633 profile_probability::likely ());
4634
4635 if (label)
4636 {
4637 emit_label (label);
4638
4639 /* Given conditional stores, the offset can no longer be
4640 known, so clear it. */
4641 clear_mem_offset (to);
4642 }
4643 }
4644
4645 return true;
4646 }
4647
4648 /* Helper function to do the actual work for expand_builtin_memset. The
4649 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4650 so that this can also be called without constructing an actual CALL_EXPR.
4651 The other arguments and return value are the same as for
4652 expand_builtin_memset. */
4653
4654 static rtx
4655 expand_builtin_memset_args (tree dest, tree val, tree len,
4656 rtx target, machine_mode mode, tree orig_exp)
4657 {
4658 tree fndecl, fn;
4659 enum built_in_function fcode;
4660 machine_mode val_mode;
4661 char c;
4662 unsigned int dest_align;
4663 rtx dest_mem, dest_addr, len_rtx;
4664 HOST_WIDE_INT expected_size = -1;
4665 unsigned int expected_align = 0;
4666 unsigned HOST_WIDE_INT min_size;
4667 unsigned HOST_WIDE_INT max_size;
4668 unsigned HOST_WIDE_INT probable_max_size;
4669
4670 dest_align = get_pointer_alignment (dest);
4671
4672 /* If DEST is not a pointer type, don't do this operation in-line. */
4673 if (dest_align == 0)
4674 return NULL_RTX;
4675
4676 if (currently_expanding_gimple_stmt)
4677 stringop_block_profile (currently_expanding_gimple_stmt,
4678 &expected_align, &expected_size);
4679
4680 if (expected_align < dest_align)
4681 expected_align = dest_align;
4682
4683 /* If the LEN parameter is zero, return DEST. */
4684 if (integer_zerop (len))
4685 {
4686 /* Evaluate and ignore VAL in case it has side-effects. */
4687 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4688 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4689 }
4690
4691 /* Stabilize the arguments in case we fail. */
4692 dest = builtin_save_expr (dest);
4693 val = builtin_save_expr (val);
4694 len = builtin_save_expr (len);
4695
4696 len_rtx = expand_normal (len);
4697 determine_block_size (len, len_rtx, &min_size, &max_size,
4698 &probable_max_size);
4699 dest_mem = get_memory_rtx (dest, len);
4700 val_mode = TYPE_MODE (unsigned_char_type_node);
4701
4702 if (TREE_CODE (val) != INTEGER_CST
4703 || target_char_cast (val, &c))
4704 {
4705 rtx val_rtx;
4706
4707 val_rtx = expand_normal (val);
4708 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4709
4710 /* Assume that we can memset by pieces if we can store
4711 * the coefficients by pieces (in the required modes).
4712 * We can't pass builtin_memset_gen_str as that emits RTL. */
4713 c = 1;
4714 if (tree_fits_uhwi_p (len)
4715 && can_store_by_pieces (tree_to_uhwi (len),
4716 builtin_memset_read_str, &c, dest_align,
4717 true))
4718 {
4719 val_rtx = force_reg (val_mode, val_rtx);
4720 store_by_pieces (dest_mem, tree_to_uhwi (len),
4721 builtin_memset_gen_str, val_rtx, dest_align,
4722 true, RETURN_BEGIN);
4723 }
4724 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4725 dest_align, expected_align,
4726 expected_size, min_size, max_size,
4727 probable_max_size)
4728 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4729 tree_ctz (len),
4730 min_size, max_size,
4731 val_rtx, 0,
4732 dest_align))
4733 goto do_libcall;
4734
4735 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4736 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4737 return dest_mem;
4738 }
4739
4740 if (c)
4741 {
4742 if (tree_fits_uhwi_p (len)
4743 && can_store_by_pieces (tree_to_uhwi (len),
4744 builtin_memset_read_str, &c, dest_align,
4745 true))
4746 store_by_pieces (dest_mem, tree_to_uhwi (len),
4747 builtin_memset_read_str, &c, dest_align, true,
4748 RETURN_BEGIN);
4749 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4750 gen_int_mode (c, val_mode),
4751 dest_align, expected_align,
4752 expected_size, min_size, max_size,
4753 probable_max_size)
4754 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4755 tree_ctz (len),
4756 min_size, max_size,
4757 NULL_RTX, c,
4758 dest_align))
4759 goto do_libcall;
4760
4761 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4762 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4763 return dest_mem;
4764 }
4765
4766 set_mem_align (dest_mem, dest_align);
4767 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4768 CALL_EXPR_TAILCALL (orig_exp)
4769 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4770 expected_align, expected_size,
4771 min_size, max_size,
4772 probable_max_size, tree_ctz (len));
4773
4774 if (dest_addr == 0)
4775 {
4776 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4777 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4778 }
4779
4780 return dest_addr;
4781
4782 do_libcall:
4783 fndecl = get_callee_fndecl (orig_exp);
4784 fcode = DECL_FUNCTION_CODE (fndecl);
4785 if (fcode == BUILT_IN_MEMSET)
4786 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4787 dest, val, len);
4788 else if (fcode == BUILT_IN_BZERO)
4789 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4790 dest, len);
4791 else
4792 gcc_unreachable ();
4793 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4794 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4795 return expand_call (fn, target, target == const0_rtx);
4796 }
4797
4798 /* Expand expression EXP, which is a call to the bzero builtin. Return
4799 NULL_RTX if we failed the caller should emit a normal call. */
4800
4801 static rtx
4802 expand_builtin_bzero (tree exp)
4803 {
4804 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4805 return NULL_RTX;
4806
4807 tree dest = CALL_EXPR_ARG (exp, 0);
4808 tree size = CALL_EXPR_ARG (exp, 1);
4809
4810 /* New argument list transforming bzero(ptr x, int y) to
4811 memset(ptr x, int 0, size_t y). This is done this way
4812 so that if it isn't expanded inline, we fallback to
4813 calling bzero instead of memset. */
4814
4815 location_t loc = EXPR_LOCATION (exp);
4816
4817 return expand_builtin_memset_args (dest, integer_zero_node,
4818 fold_convert_loc (loc,
4819 size_type_node, size),
4820 const0_rtx, VOIDmode, exp);
4821 }
4822
4823 /* Try to expand cmpstr operation ICODE with the given operands.
4824 Return the result rtx on success, otherwise return null. */
4825
4826 static rtx
4827 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4828 HOST_WIDE_INT align)
4829 {
4830 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4831
4832 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4833 target = NULL_RTX;
4834
4835 class expand_operand ops[4];
4836 create_output_operand (&ops[0], target, insn_mode);
4837 create_fixed_operand (&ops[1], arg1_rtx);
4838 create_fixed_operand (&ops[2], arg2_rtx);
4839 create_integer_operand (&ops[3], align);
4840 if (maybe_expand_insn (icode, 4, ops))
4841 return ops[0].value;
4842 return NULL_RTX;
4843 }
4844
4845 /* Expand expression EXP, which is a call to the memcmp built-in function.
4846 Return NULL_RTX if we failed and the caller should emit a normal call,
4847 otherwise try to get the result in TARGET, if convenient.
4848 RESULT_EQ is true if we can relax the returned value to be either zero
4849 or nonzero, without caring about the sign. */
4850
4851 static rtx
4852 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4853 {
4854 if (!validate_arglist (exp,
4855 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4856 return NULL_RTX;
4857
4858 tree arg1 = CALL_EXPR_ARG (exp, 0);
4859 tree arg2 = CALL_EXPR_ARG (exp, 1);
4860 tree len = CALL_EXPR_ARG (exp, 2);
4861
4862 /* Due to the performance benefit, always inline the calls first
4863 when result_eq is false. */
4864 rtx result = NULL_RTX;
4865 enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4866 if (!result_eq && fcode != BUILT_IN_BCMP)
4867 {
4868 result = inline_expand_builtin_bytecmp (exp, target);
4869 if (result)
4870 return result;
4871 }
4872
4873 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4874 location_t loc = EXPR_LOCATION (exp);
4875
4876 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4877 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4878
4879 /* If we don't have POINTER_TYPE, call the function. */
4880 if (arg1_align == 0 || arg2_align == 0)
4881 return NULL_RTX;
4882
4883 rtx arg1_rtx = get_memory_rtx (arg1, len);
4884 rtx arg2_rtx = get_memory_rtx (arg2, len);
4885 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4886
4887 /* Set MEM_SIZE as appropriate. */
4888 if (CONST_INT_P (len_rtx))
4889 {
4890 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4891 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4892 }
4893
4894 by_pieces_constfn constfn = NULL;
4895
4896 /* Try to get the byte representation of the constant ARG2 (or, only
4897 when the function's result is used for equality to zero, ARG1)
4898 points to, with its byte size in NBYTES. */
4899 unsigned HOST_WIDE_INT nbytes;
4900 const char *rep = getbyterep (arg2, &nbytes);
4901 if (result_eq && rep == NULL)
4902 {
4903 /* For equality to zero the arguments are interchangeable. */
4904 rep = getbyterep (arg1, &nbytes);
4905 if (rep != NULL)
4906 std::swap (arg1_rtx, arg2_rtx);
4907 }
4908
4909 /* If the function's constant bound LEN_RTX is less than or equal
4910 to the byte size of the representation of the constant argument,
4911 and if block move would be done by pieces, we can avoid loading
4912 the bytes from memory and only store the computed constant result. */
4913 if (rep
4914 && CONST_INT_P (len_rtx)
4915 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4916 constfn = builtin_memcpy_read_str;
4917
4918 result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4919 TREE_TYPE (len), target,
4920 result_eq, constfn,
4921 CONST_CAST (char *, rep),
4922 tree_ctz (len));
4923
4924 if (result)
4925 {
4926 /* Return the value in the proper mode for this function. */
4927 if (GET_MODE (result) == mode)
4928 return result;
4929
4930 if (target != 0)
4931 {
4932 convert_move (target, result, 0);
4933 return target;
4934 }
4935
4936 return convert_to_mode (mode, result, 0);
4937 }
4938
4939 return NULL_RTX;
4940 }
4941
4942 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4943 if we failed the caller should emit a normal call, otherwise try to get
4944 the result in TARGET, if convenient. */
4945
4946 static rtx
4947 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4948 {
4949 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4950 return NULL_RTX;
4951
4952 tree arg1 = CALL_EXPR_ARG (exp, 0);
4953 tree arg2 = CALL_EXPR_ARG (exp, 1);
4954
4955 /* Due to the performance benefit, always inline the calls first. */
4956 rtx result = NULL_RTX;
4957 result = inline_expand_builtin_bytecmp (exp, target);
4958 if (result)
4959 return result;
4960
4961 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4962 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4963 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4964 return NULL_RTX;
4965
4966 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4967 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4968
4969 /* If we don't have POINTER_TYPE, call the function. */
4970 if (arg1_align == 0 || arg2_align == 0)
4971 return NULL_RTX;
4972
4973 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4974 arg1 = builtin_save_expr (arg1);
4975 arg2 = builtin_save_expr (arg2);
4976
4977 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4978 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4979
4980 /* Try to call cmpstrsi. */
4981 if (cmpstr_icode != CODE_FOR_nothing)
4982 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4983 MIN (arg1_align, arg2_align));
4984
4985 /* Try to determine at least one length and call cmpstrnsi. */
4986 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4987 {
4988 tree len;
4989 rtx arg3_rtx;
4990
4991 tree len1 = c_strlen (arg1, 1);
4992 tree len2 = c_strlen (arg2, 1);
4993
4994 if (len1)
4995 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4996 if (len2)
4997 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4998
4999 /* If we don't have a constant length for the first, use the length
5000 of the second, if we know it. We don't require a constant for
5001 this case; some cost analysis could be done if both are available
5002 but neither is constant. For now, assume they're equally cheap,
5003 unless one has side effects. If both strings have constant lengths,
5004 use the smaller. */
5005
5006 if (!len1)
5007 len = len2;
5008 else if (!len2)
5009 len = len1;
5010 else if (TREE_SIDE_EFFECTS (len1))
5011 len = len2;
5012 else if (TREE_SIDE_EFFECTS (len2))
5013 len = len1;
5014 else if (TREE_CODE (len1) != INTEGER_CST)
5015 len = len2;
5016 else if (TREE_CODE (len2) != INTEGER_CST)
5017 len = len1;
5018 else if (tree_int_cst_lt (len1, len2))
5019 len = len1;
5020 else
5021 len = len2;
5022
5023 /* If both arguments have side effects, we cannot optimize. */
5024 if (len && !TREE_SIDE_EFFECTS (len))
5025 {
5026 arg3_rtx = expand_normal (len);
5027 result = expand_cmpstrn_or_cmpmem
5028 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
5029 arg3_rtx, MIN (arg1_align, arg2_align));
5030 }
5031 }
5032
5033 tree fndecl = get_callee_fndecl (exp);
5034 if (result)
5035 {
5036 /* Return the value in the proper mode for this function. */
5037 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
5038 if (GET_MODE (result) == mode)
5039 return result;
5040 if (target == 0)
5041 return convert_to_mode (mode, result, 0);
5042 convert_move (target, result, 0);
5043 return target;
5044 }
5045
5046 /* Expand the library call ourselves using a stabilized argument
5047 list to avoid re-evaluating the function's arguments twice. */
5048 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
5049 copy_warning (fn, exp);
5050 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
5051 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5052 return expand_call (fn, target, target == const0_rtx);
5053 }
5054
5055 /* Expand expression EXP, which is a call to the strncmp builtin. Return
5056 NULL_RTX if we failed the caller should emit a normal call, otherwise
5057 try to get the result in TARGET, if convenient. */
5058
5059 static rtx
5060 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
5061 ATTRIBUTE_UNUSED machine_mode mode)
5062 {
5063 if (!validate_arglist (exp,
5064 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5065 return NULL_RTX;
5066
5067 tree arg1 = CALL_EXPR_ARG (exp, 0);
5068 tree arg2 = CALL_EXPR_ARG (exp, 1);
5069 tree arg3 = CALL_EXPR_ARG (exp, 2);
5070
5071 location_t loc = EXPR_LOCATION (exp);
5072 tree len1 = c_strlen (arg1, 1);
5073 tree len2 = c_strlen (arg2, 1);
5074
5075 /* Due to the performance benefit, always inline the calls first. */
5076 rtx result = NULL_RTX;
5077 result = inline_expand_builtin_bytecmp (exp, target);
5078 if (result)
5079 return result;
5080
5081 /* If c_strlen can determine an expression for one of the string
5082 lengths, and it doesn't have side effects, then emit cmpstrnsi
5083 using length MIN(strlen(string)+1, arg3). */
5084 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
5085 if (cmpstrn_icode == CODE_FOR_nothing)
5086 return NULL_RTX;
5087
5088 tree len;
5089
5090 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
5091 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
5092
5093 if (len1)
5094 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
5095 if (len2)
5096 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
5097
5098 tree len3 = fold_convert_loc (loc, sizetype, arg3);
5099
5100 /* If we don't have a constant length for the first, use the length
5101 of the second, if we know it. If neither string is constant length,
5102 use the given length argument. We don't require a constant for
5103 this case; some cost analysis could be done if both are available
5104 but neither is constant. For now, assume they're equally cheap,
5105 unless one has side effects. If both strings have constant lengths,
5106 use the smaller. */
5107
5108 if (!len1 && !len2)
5109 len = len3;
5110 else if (!len1)
5111 len = len2;
5112 else if (!len2)
5113 len = len1;
5114 else if (TREE_SIDE_EFFECTS (len1))
5115 len = len2;
5116 else if (TREE_SIDE_EFFECTS (len2))
5117 len = len1;
5118 else if (TREE_CODE (len1) != INTEGER_CST)
5119 len = len2;
5120 else if (TREE_CODE (len2) != INTEGER_CST)
5121 len = len1;
5122 else if (tree_int_cst_lt (len1, len2))
5123 len = len1;
5124 else
5125 len = len2;
5126
5127 /* If we are not using the given length, we must incorporate it here.
5128 The actual new length parameter will be MIN(len,arg3) in this case. */
5129 if (len != len3)
5130 {
5131 len = fold_convert_loc (loc, sizetype, len);
5132 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
5133 }
5134 rtx arg1_rtx = get_memory_rtx (arg1, len);
5135 rtx arg2_rtx = get_memory_rtx (arg2, len);
5136 rtx arg3_rtx = expand_normal (len);
5137 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
5138 arg2_rtx, TREE_TYPE (len), arg3_rtx,
5139 MIN (arg1_align, arg2_align));
5140
5141 tree fndecl = get_callee_fndecl (exp);
5142 if (result)
5143 {
5144 /* Return the value in the proper mode for this function. */
5145 mode = TYPE_MODE (TREE_TYPE (exp));
5146 if (GET_MODE (result) == mode)
5147 return result;
5148 if (target == 0)
5149 return convert_to_mode (mode, result, 0);
5150 convert_move (target, result, 0);
5151 return target;
5152 }
5153
5154 /* Expand the library call ourselves using a stabilized argument
5155 list to avoid re-evaluating the function's arguments twice. */
5156 tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
5157 copy_warning (call, exp);
5158 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5159 CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
5160 return expand_call (call, target, target == const0_rtx);
5161 }
5162
5163 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
5164 if that's convenient. */
5165
5166 rtx
5167 expand_builtin_saveregs (void)
5168 {
5169 rtx val;
5170 rtx_insn *seq;
5171
5172 /* Don't do __builtin_saveregs more than once in a function.
5173 Save the result of the first call and reuse it. */
5174 if (saveregs_value != 0)
5175 return saveregs_value;
5176
5177 /* When this function is called, it means that registers must be
5178 saved on entry to this function. So we migrate the call to the
5179 first insn of this function. */
5180
5181 start_sequence ();
5182
5183 /* Do whatever the machine needs done in this case. */
5184 val = targetm.calls.expand_builtin_saveregs ();
5185
5186 seq = get_insns ();
5187 end_sequence ();
5188
5189 saveregs_value = val;
5190
5191 /* Put the insns after the NOTE that starts the function. If this
5192 is inside a start_sequence, make the outer-level insn chain current, so
5193 the code is placed at the start of the function. */
5194 push_topmost_sequence ();
5195 emit_insn_after (seq, entry_of_function ());
5196 pop_topmost_sequence ();
5197
5198 return val;
5199 }
5200
5201 /* Expand a call to __builtin_next_arg. */
5202
5203 static rtx
5204 expand_builtin_next_arg (void)
5205 {
5206 /* Checking arguments is already done in fold_builtin_next_arg
5207 that must be called before this function. */
5208 return expand_binop (ptr_mode, add_optab,
5209 crtl->args.internal_arg_pointer,
5210 crtl->args.arg_offset_rtx,
5211 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5212 }
5213
5214 /* Make it easier for the backends by protecting the valist argument
5215 from multiple evaluations. */
5216
5217 static tree
5218 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5219 {
5220 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5221
5222 /* The current way of determining the type of valist is completely
5223 bogus. We should have the information on the va builtin instead. */
5224 if (!vatype)
5225 vatype = targetm.fn_abi_va_list (cfun->decl);
5226
5227 if (TREE_CODE (vatype) == ARRAY_TYPE)
5228 {
5229 if (TREE_SIDE_EFFECTS (valist))
5230 valist = save_expr (valist);
5231
5232 /* For this case, the backends will be expecting a pointer to
5233 vatype, but it's possible we've actually been given an array
5234 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5235 So fix it. */
5236 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5237 {
5238 tree p1 = build_pointer_type (TREE_TYPE (vatype));
5239 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5240 }
5241 }
5242 else
5243 {
5244 tree pt = build_pointer_type (vatype);
5245
5246 if (! needs_lvalue)
5247 {
5248 if (! TREE_SIDE_EFFECTS (valist))
5249 return valist;
5250
5251 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5252 TREE_SIDE_EFFECTS (valist) = 1;
5253 }
5254
5255 if (TREE_SIDE_EFFECTS (valist))
5256 valist = save_expr (valist);
5257 valist = fold_build2_loc (loc, MEM_REF,
5258 vatype, valist, build_int_cst (pt, 0));
5259 }
5260
5261 return valist;
5262 }
5263
5264 /* The "standard" definition of va_list is void*. */
5265
5266 tree
5267 std_build_builtin_va_list (void)
5268 {
5269 return ptr_type_node;
5270 }
5271
5272 /* The "standard" abi va_list is va_list_type_node. */
5273
5274 tree
5275 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5276 {
5277 return va_list_type_node;
5278 }
5279
5280 /* The "standard" type of va_list is va_list_type_node. */
5281
5282 tree
5283 std_canonical_va_list_type (tree type)
5284 {
5285 tree wtype, htype;
5286
5287 wtype = va_list_type_node;
5288 htype = type;
5289
5290 if (TREE_CODE (wtype) == ARRAY_TYPE)
5291 {
5292 /* If va_list is an array type, the argument may have decayed
5293 to a pointer type, e.g. by being passed to another function.
5294 In that case, unwrap both types so that we can compare the
5295 underlying records. */
5296 if (TREE_CODE (htype) == ARRAY_TYPE
5297 || POINTER_TYPE_P (htype))
5298 {
5299 wtype = TREE_TYPE (wtype);
5300 htype = TREE_TYPE (htype);
5301 }
5302 }
5303 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5304 return va_list_type_node;
5305
5306 return NULL_TREE;
5307 }
5308
5309 /* The "standard" implementation of va_start: just assign `nextarg' to
5310 the variable. */
5311
5312 void
5313 std_expand_builtin_va_start (tree valist, rtx nextarg)
5314 {
5315 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5316 convert_move (va_r, nextarg, 0);
5317 }
5318
5319 /* Expand EXP, a call to __builtin_va_start. */
5320
5321 static rtx
5322 expand_builtin_va_start (tree exp)
5323 {
5324 rtx nextarg;
5325 tree valist;
5326 location_t loc = EXPR_LOCATION (exp);
5327
5328 if (call_expr_nargs (exp) < 2)
5329 {
5330 error_at (loc, "too few arguments to function %<va_start%>");
5331 return const0_rtx;
5332 }
5333
5334 if (fold_builtin_next_arg (exp, true))
5335 return const0_rtx;
5336
5337 nextarg = expand_builtin_next_arg ();
5338 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5339
5340 if (targetm.expand_builtin_va_start)
5341 targetm.expand_builtin_va_start (valist, nextarg);
5342 else
5343 std_expand_builtin_va_start (valist, nextarg);
5344
5345 return const0_rtx;
5346 }
5347
5348 /* Expand EXP, a call to __builtin_va_end. */
5349
5350 static rtx
5351 expand_builtin_va_end (tree exp)
5352 {
5353 tree valist = CALL_EXPR_ARG (exp, 0);
5354
5355 /* Evaluate for side effects, if needed. I hate macros that don't
5356 do that. */
5357 if (TREE_SIDE_EFFECTS (valist))
5358 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5359
5360 return const0_rtx;
5361 }
5362
5363 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5364 builtin rather than just as an assignment in stdarg.h because of the
5365 nastiness of array-type va_list types. */
5366
5367 static rtx
5368 expand_builtin_va_copy (tree exp)
5369 {
5370 tree dst, src, t;
5371 location_t loc = EXPR_LOCATION (exp);
5372
5373 dst = CALL_EXPR_ARG (exp, 0);
5374 src = CALL_EXPR_ARG (exp, 1);
5375
5376 dst = stabilize_va_list_loc (loc, dst, 1);
5377 src = stabilize_va_list_loc (loc, src, 0);
5378
5379 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5380
5381 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5382 {
5383 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5384 TREE_SIDE_EFFECTS (t) = 1;
5385 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5386 }
5387 else
5388 {
5389 rtx dstb, srcb, size;
5390
5391 /* Evaluate to pointers. */
5392 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5393 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5394 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5395 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5396
5397 dstb = convert_memory_address (Pmode, dstb);
5398 srcb = convert_memory_address (Pmode, srcb);
5399
5400 /* "Dereference" to BLKmode memories. */
5401 dstb = gen_rtx_MEM (BLKmode, dstb);
5402 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5403 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5404 srcb = gen_rtx_MEM (BLKmode, srcb);
5405 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5406 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5407
5408 /* Copy. */
5409 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5410 }
5411
5412 return const0_rtx;
5413 }
5414
5415 /* Expand a call to one of the builtin functions __builtin_frame_address or
5416 __builtin_return_address. */
5417
5418 static rtx
5419 expand_builtin_frame_address (tree fndecl, tree exp)
5420 {
5421 /* The argument must be a nonnegative integer constant.
5422 It counts the number of frames to scan up the stack.
5423 The value is either the frame pointer value or the return
5424 address saved in that frame. */
5425 if (call_expr_nargs (exp) == 0)
5426 /* Warning about missing arg was already issued. */
5427 return const0_rtx;
5428 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5429 {
5430 error ("invalid argument to %qD", fndecl);
5431 return const0_rtx;
5432 }
5433 else
5434 {
5435 /* Number of frames to scan up the stack. */
5436 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5437
5438 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5439
5440 /* Some ports cannot access arbitrary stack frames. */
5441 if (tem == NULL)
5442 {
5443 warning (0, "unsupported argument to %qD", fndecl);
5444 return const0_rtx;
5445 }
5446
5447 if (count)
5448 {
5449 /* Warn since no effort is made to ensure that any frame
5450 beyond the current one exists or can be safely reached. */
5451 warning (OPT_Wframe_address, "calling %qD with "
5452 "a nonzero argument is unsafe", fndecl);
5453 }
5454
5455 /* For __builtin_frame_address, return what we've got. */
5456 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5457 return tem;
5458
5459 if (!REG_P (tem)
5460 && ! CONSTANT_P (tem))
5461 tem = copy_addr_to_reg (tem);
5462 return tem;
5463 }
5464 }
5465
5466 #if ! STACK_GROWS_DOWNWARD
5467 # define STACK_TOPS GT
5468 #else
5469 # define STACK_TOPS LT
5470 #endif
5471
5472 #ifdef POINTERS_EXTEND_UNSIGNED
5473 # define STACK_UNSIGNED POINTERS_EXTEND_UNSIGNED
5474 #else
5475 # define STACK_UNSIGNED true
5476 #endif
5477
5478 /* Expand a call to builtin function __builtin_stack_address. */
5479
5480 static rtx
5481 expand_builtin_stack_address ()
5482 {
5483 rtx ret = convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
5484 STACK_UNSIGNED);
5485
5486 #ifdef STACK_ADDRESS_OFFSET
5487 /* Unbias the stack pointer, bringing it to the boundary between the
5488 stack area claimed by the active function calling this builtin,
5489 and stack ranges that could get clobbered if it called another
5490 function. It should NOT encompass any stack red zone, that is
5491 used in leaf functions.
5492
5493 On SPARC, the register save area is *not* considered active or
5494 used by the active function, but rather as akin to the area in
5495 which call-preserved registers are saved by callees. This
5496 enables __strub_leave to clear what would otherwise overlap with
5497 its own register save area.
5498
5499 If the address is computed too high or too low, parts of a stack
5500 range that should be scrubbed may be left unscrubbed, scrubbing
5501 may corrupt active portions of the stack frame, and stack ranges
5502 may be doubly-scrubbed by caller and callee.
5503
5504 In order for it to be just right, the area delimited by
5505 @code{__builtin_stack_address} and @code{__builtin_frame_address
5506 (0)} should encompass caller's registers saved by the function,
5507 local on-stack variables and @code{alloca} stack areas.
5508 Accumulated outgoing on-stack arguments, preallocated as part of
5509 a function's own prologue, are to be regarded as part of the
5510 (caller) function's active area as well, whereas those pushed or
5511 allocated temporarily for a call are regarded as part of the
5512 callee's stack range, rather than the caller's. */
5513 ret = plus_constant (ptr_mode, ret, STACK_ADDRESS_OFFSET);
5514 #endif
5515
5516 return force_reg (ptr_mode, ret);
5517 }
5518
5519 /* Expand a call to builtin function __builtin_strub_enter. */
5520
5521 static rtx
5522 expand_builtin_strub_enter (tree exp)
5523 {
5524 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5525 return NULL_RTX;
5526
5527 if (optimize < 1 || flag_no_inline)
5528 return NULL_RTX;
5529
5530 rtx stktop = expand_builtin_stack_address ();
5531
5532 tree wmptr = CALL_EXPR_ARG (exp, 0);
5533 tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5534 tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5535 build_int_cst (TREE_TYPE (wmptr), 0));
5536 rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5537
5538 emit_move_insn (wmark, stktop);
5539
5540 return const0_rtx;
5541 }
5542
5543 /* Expand a call to builtin function __builtin_strub_update. */
5544
5545 static rtx
5546 expand_builtin_strub_update (tree exp)
5547 {
5548 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5549 return NULL_RTX;
5550
5551 if (optimize < 2 || flag_no_inline)
5552 return NULL_RTX;
5553
5554 rtx stktop = expand_builtin_stack_address ();
5555
5556 #ifdef RED_ZONE_SIZE
5557 /* Here's how the strub enter, update and leave functions deal with red zones.
5558
5559 If it weren't for red zones, update, called from within a strub context,
5560 would bump the watermark to the top of the stack. Enter and leave, running
5561 in the caller, would use the caller's top of stack address both to
5562 initialize the watermark passed to the callee, and to start strubbing the
5563 stack afterwards.
5564
5565 Ideally, we'd update the watermark so as to cover the used amount of red
5566 zone, and strub starting at the caller's other end of the (presumably
5567 unused) red zone. Normally, only leaf functions use the red zone, but at
5568 this point we can't tell whether a function is a leaf, nor can we tell how
5569 much of the red zone it uses. Furthermore, some strub contexts may have
5570 been inlined so that update and leave are called from the same stack frame,
5571 and the strub builtins may all have been inlined, turning a strub function
5572 into a leaf.
5573
5574 So cleaning the range from the caller's stack pointer (one end of the red
5575 zone) to the (potentially inlined) callee's (other end of the) red zone
5576 could scribble over the caller's own red zone.
5577
5578 We avoid this possibility by arranging for callers that are strub contexts
5579 to use their own watermark as the strub starting point. So, if A calls B,
5580 and B calls C, B will tell A to strub up to the end of B's red zone, and
5581 will strub itself only the part of C's stack frame and red zone that
5582 doesn't overlap with B's. With that, we don't need to know who's leaf and
5583 who isn't: inlined calls will shrink their strub window to zero, each
5584 remaining call will strub some portion of the stack, and eventually the
5585 strub context will return to a caller that isn't a strub context itself,
5586 that will therefore use its own stack pointer as the strub starting point.
5587 It's not a leaf, because strub contexts can't be inlined into non-strub
5588 contexts, so it doesn't use the red zone, and it will therefore correctly
5589 strub up the callee's stack frame up to the end of the callee's red zone.
5590 Neat! */
5591 if (true /* (flags_from_decl_or_type (current_function_decl) & ECF_LEAF) */)
5592 {
5593 poly_int64 red_zone_size = RED_ZONE_SIZE;
5594 #if STACK_GROWS_DOWNWARD
5595 red_zone_size = -red_zone_size;
5596 #endif
5597 stktop = plus_constant (ptr_mode, stktop, red_zone_size);
5598 stktop = force_reg (ptr_mode, stktop);
5599 }
5600 #endif
5601
5602 tree wmptr = CALL_EXPR_ARG (exp, 0);
5603 tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5604 tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5605 build_int_cst (TREE_TYPE (wmptr), 0));
5606 rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5607
5608 rtx wmarkr = force_reg (ptr_mode, wmark);
5609
5610 rtx_code_label *lab = gen_label_rtx ();
5611 do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5612 ptr_mode, NULL_RTX, lab, NULL,
5613 profile_probability::very_likely ());
5614 emit_move_insn (wmark, stktop);
5615
5616 /* If this is an inlined strub function, also bump the watermark for the
5617 enclosing function. This avoids a problem with the following scenario: A
5618 calls B and B calls C, and both B and C get inlined into A. B allocates
5619 temporary stack space before calling C. If we don't update A's watermark,
5620 we may use an outdated baseline for the post-C strub_leave, erasing B's
5621 temporary stack allocation. We only need this if we're fully expanding
5622 strub_leave inline. */
5623 tree xwmptr = (optimize > 2
5624 ? strub_watermark_parm (current_function_decl)
5625 : wmptr);
5626 if (wmptr != xwmptr)
5627 {
5628 wmptr = xwmptr;
5629 wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5630 wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5631 build_int_cst (TREE_TYPE (wmptr), 0));
5632 wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5633 wmarkr = force_reg (ptr_mode, wmark);
5634
5635 do_compare_rtx_and_jump (stktop, wmarkr, STACK_TOPS, STACK_UNSIGNED,
5636 ptr_mode, NULL_RTX, lab, NULL,
5637 profile_probability::very_likely ());
5638 emit_move_insn (wmark, stktop);
5639 }
5640
5641 emit_label (lab);
5642
5643 return const0_rtx;
5644 }
5645
5646
5647 /* Expand a call to builtin function __builtin_strub_leave. */
5648
5649 static rtx
5650 expand_builtin_strub_leave (tree exp)
5651 {
5652 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5653 return NULL_RTX;
5654
5655 if (optimize < 2 || optimize_size || flag_no_inline)
5656 return NULL_RTX;
5657
5658 rtx stktop = NULL_RTX;
5659
5660 if (tree wmptr = (optimize
5661 ? strub_watermark_parm (current_function_decl)
5662 : NULL_TREE))
5663 {
5664 tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5665 tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5666 build_int_cst (TREE_TYPE (wmptr), 0));
5667 rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5668 stktop = force_reg (ptr_mode, wmark);
5669 }
5670
5671 if (!stktop)
5672 stktop = expand_builtin_stack_address ();
5673
5674 tree wmptr = CALL_EXPR_ARG (exp, 0);
5675 tree wmtype = TREE_TYPE (TREE_TYPE (wmptr));
5676 tree wmtree = fold_build2 (MEM_REF, wmtype, wmptr,
5677 build_int_cst (TREE_TYPE (wmptr), 0));
5678 rtx wmark = expand_expr (wmtree, NULL_RTX, ptr_mode, EXPAND_MEMORY);
5679
5680 rtx wmarkr = force_reg (ptr_mode, wmark);
5681
5682 #if ! STACK_GROWS_DOWNWARD
5683 rtx base = stktop;
5684 rtx end = wmarkr;
5685 #else
5686 rtx base = wmarkr;
5687 rtx end = stktop;
5688 #endif
5689
5690 /* We're going to modify it, so make sure it's not e.g. the stack pointer. */
5691 base = copy_to_reg (base);
5692
5693 rtx_code_label *done = gen_label_rtx ();
5694 do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5695 ptr_mode, NULL_RTX, done, NULL,
5696 profile_probability::very_likely ());
5697
5698 if (optimize < 3)
5699 expand_call (exp, NULL_RTX, true);
5700 else
5701 {
5702 /* Ok, now we've determined we want to copy the block, so convert the
5703 addresses to Pmode, as needed to dereference them to access ptr_mode
5704 memory locations, so that we don't have to convert anything within the
5705 loop. */
5706 base = memory_address (ptr_mode, base);
5707 end = memory_address (ptr_mode, end);
5708
5709 rtx zero = force_operand (const0_rtx, NULL_RTX);
5710 int ulen = GET_MODE_SIZE (ptr_mode);
5711
5712 /* ??? It would be nice to use setmem or similar patterns here,
5713 but they do not necessarily obey the stack growth direction,
5714 which has security implications. We also have to avoid calls
5715 (memset, bzero or any machine-specific ones), which are
5716 likely unsafe here (see TARGET_STRUB_MAY_USE_MEMSET). */
5717 #if ! STACK_GROWS_DOWNWARD
5718 rtx incr = plus_constant (Pmode, base, ulen);
5719 rtx dstm = gen_rtx_MEM (ptr_mode, base);
5720
5721 rtx_code_label *loop = gen_label_rtx ();
5722 emit_label (loop);
5723 emit_move_insn (dstm, zero);
5724 emit_move_insn (base, force_operand (incr, NULL_RTX));
5725 #else
5726 rtx decr = plus_constant (Pmode, end, -ulen);
5727 rtx dstm = gen_rtx_MEM (ptr_mode, end);
5728
5729 rtx_code_label *loop = gen_label_rtx ();
5730 emit_label (loop);
5731 emit_move_insn (end, force_operand (decr, NULL_RTX));
5732 emit_move_insn (dstm, zero);
5733 #endif
5734 do_compare_rtx_and_jump (base, end, LT, STACK_UNSIGNED,
5735 Pmode, NULL_RTX, NULL, loop,
5736 profile_probability::very_likely ());
5737 }
5738
5739 emit_label (done);
5740
5741 return const0_rtx;
5742 }
5743
5744 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5745 failed and the caller should emit a normal call. */
5746
5747 static rtx
5748 expand_builtin_alloca (tree exp)
5749 {
5750 rtx op0;
5751 rtx result;
5752 unsigned int align;
5753 tree fndecl = get_callee_fndecl (exp);
5754 HOST_WIDE_INT max_size;
5755 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5756 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5757 bool valid_arglist
5758 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5759 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5760 VOID_TYPE)
5761 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5762 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5763 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5764
5765 if (!valid_arglist)
5766 return NULL_RTX;
5767
5768 /* Compute the argument. */
5769 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5770
5771 /* Compute the alignment. */
5772 align = (fcode == BUILT_IN_ALLOCA
5773 ? BIGGEST_ALIGNMENT
5774 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5775
5776 /* Compute the maximum size. */
5777 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5778 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5779 : -1);
5780
5781 /* Allocate the desired space. If the allocation stems from the declaration
5782 of a variable-sized object, it cannot accumulate. */
5783 result
5784 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5785 result = convert_memory_address (ptr_mode, result);
5786
5787 /* Dynamic allocations for variables are recorded during gimplification. */
5788 if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5789 record_dynamic_alloc (exp);
5790
5791 return result;
5792 }
5793
5794 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5795 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5796 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5797 handle_builtin_stack_restore function. */
5798
5799 static rtx
5800 expand_asan_emit_allocas_unpoison (tree exp)
5801 {
5802 tree arg0 = CALL_EXPR_ARG (exp, 0);
5803 tree arg1 = CALL_EXPR_ARG (exp, 1);
5804 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5805 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5806 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5807 stack_pointer_rtx, NULL_RTX, 0,
5808 OPTAB_LIB_WIDEN);
5809 off = convert_modes (ptr_mode, Pmode, off, 0);
5810 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5811 OPTAB_LIB_WIDEN);
5812 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5813 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5814 top, ptr_mode, bot, ptr_mode);
5815 return ret;
5816 }
5817
5818 /* Expand a call to bswap builtin in EXP.
5819 Return NULL_RTX if a normal call should be emitted rather than expanding the
5820 function in-line. If convenient, the result should be placed in TARGET.
5821 SUBTARGET may be used as the target for computing one of EXP's operands. */
5822
5823 static rtx
5824 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5825 rtx subtarget)
5826 {
5827 tree arg;
5828 rtx op0;
5829
5830 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5831 return NULL_RTX;
5832
5833 arg = CALL_EXPR_ARG (exp, 0);
5834 op0 = expand_expr (arg,
5835 subtarget && GET_MODE (subtarget) == target_mode
5836 ? subtarget : NULL_RTX,
5837 target_mode, EXPAND_NORMAL);
5838 if (GET_MODE (op0) != target_mode)
5839 op0 = convert_to_mode (target_mode, op0, 1);
5840
5841 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5842
5843 gcc_assert (target);
5844
5845 return convert_to_mode (target_mode, target, 1);
5846 }
5847
5848 /* Expand a call to a unary builtin in EXP.
5849 Return NULL_RTX if a normal call should be emitted rather than expanding the
5850 function in-line. If convenient, the result should be placed in TARGET.
5851 SUBTARGET may be used as the target for computing one of EXP's operands. */
5852
5853 static rtx
5854 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5855 rtx subtarget, optab op_optab)
5856 {
5857 rtx op0;
5858
5859 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5860 return NULL_RTX;
5861
5862 /* Compute the argument. */
5863 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5864 (subtarget
5865 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5866 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5867 VOIDmode, EXPAND_NORMAL);
5868 /* Compute op, into TARGET if possible.
5869 Set TARGET to wherever the result comes back. */
5870 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5871 op_optab, op0, target, op_optab != clrsb_optab);
5872 gcc_assert (target);
5873
5874 return convert_to_mode (target_mode, target, 0);
5875 }
5876
5877 /* Expand a call to __builtin_expect. We just return our argument
5878 as the builtin_expect semantic should've been already executed by
5879 tree branch prediction pass. */
5880
5881 static rtx
5882 expand_builtin_expect (tree exp, rtx target)
5883 {
5884 tree arg;
5885
5886 if (call_expr_nargs (exp) < 2)
5887 return const0_rtx;
5888 arg = CALL_EXPR_ARG (exp, 0);
5889
5890 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5891 /* When guessing was done, the hints should be already stripped away. */
5892 gcc_assert (!flag_guess_branch_prob
5893 || optimize == 0 || seen_error ());
5894 return target;
5895 }
5896
5897 /* Expand a call to __builtin_expect_with_probability. We just return our
5898 argument as the builtin_expect semantic should've been already executed by
5899 tree branch prediction pass. */
5900
5901 static rtx
5902 expand_builtin_expect_with_probability (tree exp, rtx target)
5903 {
5904 tree arg;
5905
5906 if (call_expr_nargs (exp) < 3)
5907 return const0_rtx;
5908 arg = CALL_EXPR_ARG (exp, 0);
5909
5910 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5911 /* When guessing was done, the hints should be already stripped away. */
5912 gcc_assert (!flag_guess_branch_prob
5913 || optimize == 0 || seen_error ());
5914 return target;
5915 }
5916
5917
5918 /* Expand a call to __builtin_assume_aligned. We just return our first
5919 argument as the builtin_assume_aligned semantic should've been already
5920 executed by CCP. */
5921
5922 static rtx
5923 expand_builtin_assume_aligned (tree exp, rtx target)
5924 {
5925 if (call_expr_nargs (exp) < 2)
5926 return const0_rtx;
5927 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5928 EXPAND_NORMAL);
5929 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5930 && (call_expr_nargs (exp) < 3
5931 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5932 return target;
5933 }
5934
5935 void
5936 expand_builtin_trap (void)
5937 {
5938 if (targetm.have_trap ())
5939 {
5940 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5941 /* For trap insns when not accumulating outgoing args force
5942 REG_ARGS_SIZE note to prevent crossjumping of calls with
5943 different args sizes. */
5944 if (!ACCUMULATE_OUTGOING_ARGS)
5945 add_args_size_note (insn, stack_pointer_delta);
5946 }
5947 else
5948 {
5949 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5950 tree call_expr = build_call_expr (fn, 0);
5951 expand_call (call_expr, NULL_RTX, false);
5952 }
5953
5954 emit_barrier ();
5955 }
5956
5957 /* Expand a call to __builtin_unreachable. We do nothing except emit
5958 a barrier saying that control flow will not pass here.
5959
5960 It is the responsibility of the program being compiled to ensure
5961 that control flow does never reach __builtin_unreachable. */
5962 static void
5963 expand_builtin_unreachable (void)
5964 {
5965 /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5966 to avoid this. */
5967 gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5968 emit_barrier ();
5969 }
5970
5971 /* Expand EXP, a call to fabs, fabsf or fabsl.
5972 Return NULL_RTX if a normal call should be emitted rather than expanding
5973 the function inline. If convenient, the result should be placed
5974 in TARGET. SUBTARGET may be used as the target for computing
5975 the operand. */
5976
5977 static rtx
5978 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5979 {
5980 machine_mode mode;
5981 tree arg;
5982 rtx op0;
5983
5984 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5985 return NULL_RTX;
5986
5987 arg = CALL_EXPR_ARG (exp, 0);
5988 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5989 mode = TYPE_MODE (TREE_TYPE (arg));
5990 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5991 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5992 }
5993
5994 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5995 Return NULL is a normal call should be emitted rather than expanding the
5996 function inline. If convenient, the result should be placed in TARGET.
5997 SUBTARGET may be used as the target for computing the operand. */
5998
5999 static rtx
6000 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
6001 {
6002 rtx op0, op1;
6003 tree arg;
6004
6005 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6006 return NULL_RTX;
6007
6008 arg = CALL_EXPR_ARG (exp, 0);
6009 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6010
6011 arg = CALL_EXPR_ARG (exp, 1);
6012 op1 = expand_normal (arg);
6013
6014 return expand_copysign (op0, op1, target);
6015 }
6016
6017 /* Emit a call to __builtin___clear_cache. */
6018
6019 void
6020 default_emit_call_builtin___clear_cache (rtx begin, rtx end)
6021 {
6022 rtx callee = gen_rtx_SYMBOL_REF (Pmode,
6023 BUILTIN_ASM_NAME_PTR
6024 (BUILT_IN_CLEAR_CACHE));
6025
6026 emit_library_call (callee,
6027 LCT_NORMAL, VOIDmode,
6028 convert_memory_address (ptr_mode, begin), ptr_mode,
6029 convert_memory_address (ptr_mode, end), ptr_mode);
6030 }
6031
6032 /* Emit a call to __builtin___clear_cache, unless the target specifies
6033 it as do-nothing. This function can be used by trampoline
6034 finalizers to duplicate the effects of expanding a call to the
6035 clear_cache builtin. */
6036
6037 void
6038 maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
6039 {
6040 gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
6041 || CONST_INT_P (begin))
6042 && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
6043 || CONST_INT_P (end)));
6044
6045 if (targetm.have_clear_cache ())
6046 {
6047 /* We have a "clear_cache" insn, and it will handle everything. */
6048 class expand_operand ops[2];
6049
6050 create_address_operand (&ops[0], begin);
6051 create_address_operand (&ops[1], end);
6052
6053 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
6054 return;
6055 }
6056 else
6057 {
6058 #ifndef CLEAR_INSN_CACHE
6059 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
6060 does nothing. There is no need to call it. Do nothing. */
6061 return;
6062 #endif /* CLEAR_INSN_CACHE */
6063 }
6064
6065 targetm.calls.emit_call_builtin___clear_cache (begin, end);
6066 }
6067
6068 /* Expand a call to __builtin___clear_cache. */
6069
6070 static void
6071 expand_builtin___clear_cache (tree exp)
6072 {
6073 tree begin, end;
6074 rtx begin_rtx, end_rtx;
6075
6076 /* We must not expand to a library call. If we did, any
6077 fallback library function in libgcc that might contain a call to
6078 __builtin___clear_cache() would recurse infinitely. */
6079 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6080 {
6081 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
6082 return;
6083 }
6084
6085 begin = CALL_EXPR_ARG (exp, 0);
6086 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
6087
6088 end = CALL_EXPR_ARG (exp, 1);
6089 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
6090
6091 maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
6092 }
6093
6094 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
6095
6096 static rtx
6097 round_trampoline_addr (rtx tramp)
6098 {
6099 rtx temp, addend, mask;
6100
6101 /* If we don't need too much alignment, we'll have been guaranteed
6102 proper alignment by get_trampoline_type. */
6103 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
6104 return tramp;
6105
6106 /* Round address up to desired boundary. */
6107 temp = gen_reg_rtx (Pmode);
6108 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
6109 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
6110
6111 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
6112 temp, 0, OPTAB_LIB_WIDEN);
6113 tramp = expand_simple_binop (Pmode, AND, temp, mask,
6114 temp, 0, OPTAB_LIB_WIDEN);
6115
6116 return tramp;
6117 }
6118
6119 static rtx
6120 expand_builtin_init_trampoline (tree exp, bool onstack)
6121 {
6122 tree t_tramp, t_func, t_chain;
6123 rtx m_tramp, r_tramp, r_chain, tmp;
6124
6125 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
6126 POINTER_TYPE, VOID_TYPE))
6127 return NULL_RTX;
6128
6129 t_tramp = CALL_EXPR_ARG (exp, 0);
6130 t_func = CALL_EXPR_ARG (exp, 1);
6131 t_chain = CALL_EXPR_ARG (exp, 2);
6132
6133 r_tramp = expand_normal (t_tramp);
6134 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
6135 MEM_NOTRAP_P (m_tramp) = 1;
6136
6137 /* If ONSTACK, the TRAMP argument should be the address of a field
6138 within the local function's FRAME decl. Either way, let's see if
6139 we can fill in the MEM_ATTRs for this memory. */
6140 if (TREE_CODE (t_tramp) == ADDR_EXPR)
6141 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
6142
6143 /* Creator of a heap trampoline is responsible for making sure the
6144 address is aligned to at least STACK_BOUNDARY. Normally malloc
6145 will ensure this anyhow. */
6146 tmp = round_trampoline_addr (r_tramp);
6147 if (tmp != r_tramp)
6148 {
6149 m_tramp = change_address (m_tramp, BLKmode, tmp);
6150 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
6151 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
6152 }
6153
6154 /* The FUNC argument should be the address of the nested function.
6155 Extract the actual function decl to pass to the hook. */
6156 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
6157 t_func = TREE_OPERAND (t_func, 0);
6158 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
6159
6160 r_chain = expand_normal (t_chain);
6161
6162 /* Generate insns to initialize the trampoline. */
6163 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
6164
6165 if (onstack)
6166 {
6167 trampolines_created = 1;
6168
6169 if (targetm.calls.custom_function_descriptors != 0)
6170 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
6171 "trampoline generated for nested function %qD", t_func);
6172 }
6173
6174 return const0_rtx;
6175 }
6176
6177 static rtx
6178 expand_builtin_adjust_trampoline (tree exp)
6179 {
6180 rtx tramp;
6181
6182 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6183 return NULL_RTX;
6184
6185 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6186 tramp = round_trampoline_addr (tramp);
6187 if (targetm.calls.trampoline_adjust_address)
6188 tramp = targetm.calls.trampoline_adjust_address (tramp);
6189
6190 return tramp;
6191 }
6192
6193 /* Expand a call to the builtin descriptor initialization routine.
6194 A descriptor is made up of a couple of pointers to the static
6195 chain and the code entry in this order. */
6196
6197 static rtx
6198 expand_builtin_init_descriptor (tree exp)
6199 {
6200 tree t_descr, t_func, t_chain;
6201 rtx m_descr, r_descr, r_func, r_chain;
6202
6203 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
6204 VOID_TYPE))
6205 return NULL_RTX;
6206
6207 t_descr = CALL_EXPR_ARG (exp, 0);
6208 t_func = CALL_EXPR_ARG (exp, 1);
6209 t_chain = CALL_EXPR_ARG (exp, 2);
6210
6211 r_descr = expand_normal (t_descr);
6212 m_descr = gen_rtx_MEM (BLKmode, r_descr);
6213 MEM_NOTRAP_P (m_descr) = 1;
6214 set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
6215
6216 r_func = expand_normal (t_func);
6217 r_chain = expand_normal (t_chain);
6218
6219 /* Generate insns to initialize the descriptor. */
6220 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
6221 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
6222 POINTER_SIZE / BITS_PER_UNIT), r_func);
6223
6224 return const0_rtx;
6225 }
6226
6227 /* Expand a call to the builtin descriptor adjustment routine. */
6228
6229 static rtx
6230 expand_builtin_adjust_descriptor (tree exp)
6231 {
6232 rtx tramp;
6233
6234 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6235 return NULL_RTX;
6236
6237 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
6238
6239 /* Unalign the descriptor to allow runtime identification. */
6240 tramp = plus_constant (ptr_mode, tramp,
6241 targetm.calls.custom_function_descriptors);
6242
6243 return force_operand (tramp, NULL_RTX);
6244 }
6245
6246 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
6247 function. The function first checks whether the back end provides
6248 an insn to implement signbit for the respective mode. If not, it
6249 checks whether the floating point format of the value is such that
6250 the sign bit can be extracted. If that is not the case, error out.
6251 EXP is the expression that is a call to the builtin function; if
6252 convenient, the result should be placed in TARGET. */
6253 static rtx
6254 expand_builtin_signbit (tree exp, rtx target)
6255 {
6256 const struct real_format *fmt;
6257 scalar_float_mode fmode;
6258 scalar_int_mode rmode, imode;
6259 tree arg;
6260 int word, bitpos;
6261 enum insn_code icode;
6262 rtx temp;
6263 location_t loc = EXPR_LOCATION (exp);
6264
6265 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
6266 return NULL_RTX;
6267
6268 arg = CALL_EXPR_ARG (exp, 0);
6269 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
6270 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
6271 fmt = REAL_MODE_FORMAT (fmode);
6272
6273 arg = builtin_save_expr (arg);
6274
6275 /* Expand the argument yielding a RTX expression. */
6276 temp = expand_normal (arg);
6277
6278 /* Check if the back end provides an insn that handles signbit for the
6279 argument's mode. */
6280 icode = optab_handler (signbit_optab, fmode);
6281 if (icode != CODE_FOR_nothing)
6282 {
6283 rtx_insn *last = get_last_insn ();
6284 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6285 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
6286 return this_target;
6287 delete_insns_since (last);
6288 }
6289
6290 /* For floating point formats without a sign bit, implement signbit
6291 as "ARG < 0.0". */
6292 bitpos = fmt->signbit_ro;
6293 if (bitpos < 0)
6294 {
6295 /* But we can't do this if the format supports signed zero. */
6296 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
6297
6298 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
6299 build_real (TREE_TYPE (arg), dconst0));
6300 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
6301 }
6302
6303 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
6304 {
6305 imode = int_mode_for_mode (fmode).require ();
6306 temp = gen_lowpart (imode, temp);
6307 }
6308 else
6309 {
6310 imode = word_mode;
6311 /* Handle targets with different FP word orders. */
6312 if (FLOAT_WORDS_BIG_ENDIAN)
6313 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
6314 else
6315 word = bitpos / BITS_PER_WORD;
6316 temp = operand_subword_force (temp, word, fmode);
6317 bitpos = bitpos % BITS_PER_WORD;
6318 }
6319
6320 /* Force the intermediate word_mode (or narrower) result into a
6321 register. This avoids attempting to create paradoxical SUBREGs
6322 of floating point modes below. */
6323 temp = force_reg (imode, temp);
6324
6325 /* If the bitpos is within the "result mode" lowpart, the operation
6326 can be implement with a single bitwise AND. Otherwise, we need
6327 a right shift and an AND. */
6328
6329 if (bitpos < GET_MODE_BITSIZE (rmode))
6330 {
6331 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
6332
6333 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
6334 temp = gen_lowpart (rmode, temp);
6335 temp = expand_binop (rmode, and_optab, temp,
6336 immed_wide_int_const (mask, rmode),
6337 NULL_RTX, 1, OPTAB_LIB_WIDEN);
6338 }
6339 else
6340 {
6341 /* Perform a logical right shift to place the signbit in the least
6342 significant bit, then truncate the result to the desired mode
6343 and mask just this bit. */
6344 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
6345 temp = gen_lowpart (rmode, temp);
6346 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
6347 NULL_RTX, 1, OPTAB_LIB_WIDEN);
6348 }
6349
6350 return temp;
6351 }
6352
6353 /* Expand fork or exec calls. TARGET is the desired target of the
6354 call. EXP is the call. FN is the
6355 identificator of the actual function. IGNORE is nonzero if the
6356 value is to be ignored. */
6357
6358 static rtx
6359 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
6360 {
6361 tree id, decl;
6362 tree call;
6363
6364 /* If we are not profiling, just call the function. */
6365 if (!coverage_instrumentation_p ())
6366 return NULL_RTX;
6367
6368 /* Otherwise call the wrapper. This should be equivalent for the rest of
6369 compiler, so the code does not diverge, and the wrapper may run the
6370 code necessary for keeping the profiling sane. */
6371
6372 switch (DECL_FUNCTION_CODE (fn))
6373 {
6374 case BUILT_IN_FORK:
6375 id = get_identifier ("__gcov_fork");
6376 break;
6377
6378 case BUILT_IN_EXECL:
6379 id = get_identifier ("__gcov_execl");
6380 break;
6381
6382 case BUILT_IN_EXECV:
6383 id = get_identifier ("__gcov_execv");
6384 break;
6385
6386 case BUILT_IN_EXECLP:
6387 id = get_identifier ("__gcov_execlp");
6388 break;
6389
6390 case BUILT_IN_EXECLE:
6391 id = get_identifier ("__gcov_execle");
6392 break;
6393
6394 case BUILT_IN_EXECVP:
6395 id = get_identifier ("__gcov_execvp");
6396 break;
6397
6398 case BUILT_IN_EXECVE:
6399 id = get_identifier ("__gcov_execve");
6400 break;
6401
6402 default:
6403 gcc_unreachable ();
6404 }
6405
6406 decl = build_decl (DECL_SOURCE_LOCATION (fn),
6407 FUNCTION_DECL, id, TREE_TYPE (fn));
6408 DECL_EXTERNAL (decl) = 1;
6409 TREE_PUBLIC (decl) = 1;
6410 DECL_ARTIFICIAL (decl) = 1;
6411 TREE_NOTHROW (decl) = 1;
6412 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6413 DECL_VISIBILITY_SPECIFIED (decl) = 1;
6414 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
6415 return expand_call (call, target, ignore);
6416 }
6417
6418
6419
6420 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
6421 the pointer in these functions is void*, the tree optimizers may remove
6422 casts. The mode computed in expand_builtin isn't reliable either, due
6423 to __sync_bool_compare_and_swap.
6424
6425 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
6426 group of builtins. This gives us log2 of the mode size. */
6427
6428 static inline machine_mode
6429 get_builtin_sync_mode (int fcode_diff)
6430 {
6431 /* The size is not negotiable, so ask not to get BLKmode in return
6432 if the target indicates that a smaller size would be better. */
6433 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
6434 }
6435
6436 /* Expand the memory expression LOC and return the appropriate memory operand
6437 for the builtin_sync operations. */
6438
6439 static rtx
6440 get_builtin_sync_mem (tree loc, machine_mode mode)
6441 {
6442 rtx addr, mem;
6443 int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
6444 ? TREE_TYPE (TREE_TYPE (loc))
6445 : TREE_TYPE (loc));
6446 scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
6447
6448 addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
6449 addr = convert_memory_address (addr_mode, addr);
6450
6451 /* Note that we explicitly do not want any alias information for this
6452 memory, so that we kill all other live memories. Otherwise we don't
6453 satisfy the full barrier semantics of the intrinsic. */
6454 mem = gen_rtx_MEM (mode, addr);
6455
6456 set_mem_addr_space (mem, addr_space);
6457
6458 mem = validize_mem (mem);
6459
6460 /* The alignment needs to be at least according to that of the mode. */
6461 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6462 get_pointer_alignment (loc)));
6463 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6464 MEM_VOLATILE_P (mem) = 1;
6465
6466 return mem;
6467 }
6468
6469 /* Make sure an argument is in the right mode.
6470 EXP is the tree argument.
6471 MODE is the mode it should be in. */
6472
6473 static rtx
6474 expand_expr_force_mode (tree exp, machine_mode mode)
6475 {
6476 rtx val;
6477 machine_mode old_mode;
6478
6479 if (TREE_CODE (exp) == SSA_NAME
6480 && TYPE_MODE (TREE_TYPE (exp)) != mode)
6481 {
6482 /* Undo argument promotion if possible, as combine might not
6483 be able to do it later due to MEM_VOLATILE_P uses in the
6484 patterns. */
6485 gimple *g = get_gimple_for_ssa_name (exp);
6486 if (g && gimple_assign_cast_p (g))
6487 {
6488 tree rhs = gimple_assign_rhs1 (g);
6489 tree_code code = gimple_assign_rhs_code (g);
6490 if (CONVERT_EXPR_CODE_P (code)
6491 && TYPE_MODE (TREE_TYPE (rhs)) == mode
6492 && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6493 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6494 && (TYPE_PRECISION (TREE_TYPE (exp))
6495 > TYPE_PRECISION (TREE_TYPE (rhs))))
6496 exp = rhs;
6497 }
6498 }
6499
6500 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6501 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6502 of CONST_INTs, where we know the old_mode only from the call argument. */
6503
6504 old_mode = GET_MODE (val);
6505 if (old_mode == VOIDmode)
6506 old_mode = TYPE_MODE (TREE_TYPE (exp));
6507 val = convert_modes (mode, old_mode, val, 1);
6508 return val;
6509 }
6510
6511
6512 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6513 EXP is the CALL_EXPR. CODE is the rtx code
6514 that corresponds to the arithmetic or logical operation from the name;
6515 an exception here is that NOT actually means NAND. TARGET is an optional
6516 place for us to store the results; AFTER is true if this is the
6517 fetch_and_xxx form. */
6518
6519 static rtx
6520 expand_builtin_sync_operation (machine_mode mode, tree exp,
6521 enum rtx_code code, bool after,
6522 rtx target)
6523 {
6524 rtx val, mem;
6525 location_t loc = EXPR_LOCATION (exp);
6526
6527 if (code == NOT && warn_sync_nand)
6528 {
6529 tree fndecl = get_callee_fndecl (exp);
6530 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6531
6532 static bool warned_f_a_n, warned_n_a_f;
6533
6534 switch (fcode)
6535 {
6536 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6537 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6538 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6539 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6540 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6541 if (warned_f_a_n)
6542 break;
6543
6544 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6545 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6546 warned_f_a_n = true;
6547 break;
6548
6549 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6550 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6551 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6552 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6553 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6554 if (warned_n_a_f)
6555 break;
6556
6557 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6558 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6559 warned_n_a_f = true;
6560 break;
6561
6562 default:
6563 gcc_unreachable ();
6564 }
6565 }
6566
6567 /* Expand the operands. */
6568 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6569 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6570
6571 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6572 after);
6573 }
6574
6575 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6576 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6577 true if this is the boolean form. TARGET is a place for us to store the
6578 results; this is NOT optional if IS_BOOL is true. */
6579
6580 static rtx
6581 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6582 bool is_bool, rtx target)
6583 {
6584 rtx old_val, new_val, mem;
6585 rtx *pbool, *poval;
6586
6587 /* Expand the operands. */
6588 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6589 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6590 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6591
6592 pbool = poval = NULL;
6593 if (target != const0_rtx)
6594 {
6595 if (is_bool)
6596 pbool = &target;
6597 else
6598 poval = &target;
6599 }
6600 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6601 false, MEMMODEL_SYNC_SEQ_CST,
6602 MEMMODEL_SYNC_SEQ_CST))
6603 return NULL_RTX;
6604
6605 return target;
6606 }
6607
6608 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6609 general form is actually an atomic exchange, and some targets only
6610 support a reduced form with the second argument being a constant 1.
6611 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6612 the results. */
6613
6614 static rtx
6615 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6616 rtx target)
6617 {
6618 rtx val, mem;
6619
6620 /* Expand the operands. */
6621 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6622 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6623
6624 return expand_sync_lock_test_and_set (target, mem, val);
6625 }
6626
6627 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6628
6629 static rtx
6630 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6631 {
6632 rtx mem;
6633
6634 /* Expand the operands. */
6635 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6636
6637 return expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6638 }
6639
6640 /* Given an integer representing an ``enum memmodel'', verify its
6641 correctness and return the memory model enum. */
6642
6643 static enum memmodel
6644 get_memmodel (tree exp)
6645 {
6646 /* If the parameter is not a constant, it's a run time value so we'll just
6647 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6648 if (TREE_CODE (exp) != INTEGER_CST)
6649 return MEMMODEL_SEQ_CST;
6650
6651 rtx op = expand_normal (exp);
6652
6653 unsigned HOST_WIDE_INT val = INTVAL (op);
6654 if (targetm.memmodel_check)
6655 val = targetm.memmodel_check (val);
6656 else if (val & ~MEMMODEL_MASK)
6657 return MEMMODEL_SEQ_CST;
6658
6659 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6660 if (memmodel_base (val) >= MEMMODEL_LAST)
6661 return MEMMODEL_SEQ_CST;
6662
6663 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6664 be conservative and promote consume to acquire. */
6665 if (val == MEMMODEL_CONSUME)
6666 val = MEMMODEL_ACQUIRE;
6667
6668 return (enum memmodel) val;
6669 }
6670
6671 /* Expand the __atomic_exchange intrinsic:
6672 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6673 EXP is the CALL_EXPR.
6674 TARGET is an optional place for us to store the results. */
6675
6676 static rtx
6677 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6678 {
6679 rtx val, mem;
6680 enum memmodel model;
6681
6682 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6683
6684 if (!flag_inline_atomics)
6685 return NULL_RTX;
6686
6687 /* Expand the operands. */
6688 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6689 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6690
6691 return expand_atomic_exchange (target, mem, val, model);
6692 }
6693
6694 /* Expand the __atomic_compare_exchange intrinsic:
6695 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6696 TYPE desired, BOOL weak,
6697 enum memmodel success,
6698 enum memmodel failure)
6699 EXP is the CALL_EXPR.
6700 TARGET is an optional place for us to store the results. */
6701
6702 static rtx
6703 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6704 rtx target)
6705 {
6706 rtx expect, desired, mem, oldval;
6707 rtx_code_label *label;
6708 tree weak;
6709 bool is_weak;
6710
6711 memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6712 memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6713
6714 if (failure > success)
6715 success = MEMMODEL_SEQ_CST;
6716
6717 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6718 {
6719 failure = MEMMODEL_SEQ_CST;
6720 success = MEMMODEL_SEQ_CST;
6721 }
6722
6723
6724 if (!flag_inline_atomics)
6725 return NULL_RTX;
6726
6727 /* Expand the operands. */
6728 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6729
6730 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6731 expect = convert_memory_address (Pmode, expect);
6732 expect = gen_rtx_MEM (mode, expect);
6733 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6734
6735 weak = CALL_EXPR_ARG (exp, 3);
6736 is_weak = false;
6737 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6738 is_weak = true;
6739
6740 if (target == const0_rtx)
6741 target = NULL;
6742
6743 /* Lest the rtl backend create a race condition with an imporoper store
6744 to memory, always create a new pseudo for OLDVAL. */
6745 oldval = NULL;
6746
6747 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6748 is_weak, success, failure))
6749 return NULL_RTX;
6750
6751 /* Conditionally store back to EXPECT, lest we create a race condition
6752 with an improper store to memory. */
6753 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6754 the normal case where EXPECT is totally private, i.e. a register. At
6755 which point the store can be unconditional. */
6756 label = gen_label_rtx ();
6757 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6758 GET_MODE (target), 1, label);
6759 emit_move_insn (expect, oldval);
6760 emit_label (label);
6761
6762 return target;
6763 }
6764
6765 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6766 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6767 call. The weak parameter must be dropped to match the expected parameter
6768 list and the expected argument changed from value to pointer to memory
6769 slot. */
6770
6771 static void
6772 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6773 {
6774 unsigned int z;
6775 vec<tree, va_gc> *vec;
6776
6777 vec_alloc (vec, 5);
6778 vec->quick_push (gimple_call_arg (call, 0));
6779 tree expected = gimple_call_arg (call, 1);
6780 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6781 TREE_TYPE (expected));
6782 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6783 if (expd != x)
6784 emit_move_insn (x, expd);
6785 tree v = make_tree (TREE_TYPE (expected), x);
6786 vec->quick_push (build1 (ADDR_EXPR,
6787 build_pointer_type (TREE_TYPE (expected)), v));
6788 vec->quick_push (gimple_call_arg (call, 2));
6789 /* Skip the boolean weak parameter. */
6790 for (z = 4; z < 6; z++)
6791 vec->quick_push (gimple_call_arg (call, z));
6792 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6793 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6794 gcc_assert (bytes_log2 < 5);
6795 built_in_function fncode
6796 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6797 + bytes_log2);
6798 tree fndecl = builtin_decl_explicit (fncode);
6799 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6800 fndecl);
6801 tree exp = build_call_vec (boolean_type_node, fn, vec);
6802 tree lhs = gimple_call_lhs (call);
6803 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6804 if (lhs)
6805 {
6806 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6807 if (GET_MODE (boolret) != mode)
6808 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6809 x = force_reg (mode, x);
6810 write_complex_part (target, boolret, true, true);
6811 write_complex_part (target, x, false, false);
6812 }
6813 }
6814
6815 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6816
6817 void
6818 expand_ifn_atomic_compare_exchange (gcall *call)
6819 {
6820 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6821 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6822 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6823
6824 memmodel success = get_memmodel (gimple_call_arg (call, 4));
6825 memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6826
6827 if (failure > success)
6828 success = MEMMODEL_SEQ_CST;
6829
6830 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6831 {
6832 failure = MEMMODEL_SEQ_CST;
6833 success = MEMMODEL_SEQ_CST;
6834 }
6835
6836 if (!flag_inline_atomics)
6837 {
6838 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6839 return;
6840 }
6841
6842 /* Expand the operands. */
6843 rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6844
6845 rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6846 rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6847
6848 bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6849
6850 rtx boolret = NULL;
6851 rtx oldval = NULL;
6852
6853 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6854 is_weak, success, failure))
6855 {
6856 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6857 return;
6858 }
6859
6860 tree lhs = gimple_call_lhs (call);
6861 if (lhs)
6862 {
6863 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6864 if (GET_MODE (boolret) != mode)
6865 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6866 write_complex_part (target, boolret, true, true);
6867 write_complex_part (target, oldval, false, false);
6868 }
6869 }
6870
6871 /* Expand the __atomic_load intrinsic:
6872 TYPE __atomic_load (TYPE *object, enum memmodel)
6873 EXP is the CALL_EXPR.
6874 TARGET is an optional place for us to store the results. */
6875
6876 static rtx
6877 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6878 {
6879 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6880 if (is_mm_release (model) || is_mm_acq_rel (model))
6881 model = MEMMODEL_SEQ_CST;
6882
6883 if (!flag_inline_atomics)
6884 return NULL_RTX;
6885
6886 /* Expand the operand. */
6887 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6888
6889 return expand_atomic_load (target, mem, model);
6890 }
6891
6892
6893 /* Expand the __atomic_store intrinsic:
6894 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6895 EXP is the CALL_EXPR.
6896 TARGET is an optional place for us to store the results. */
6897
6898 static rtx
6899 expand_builtin_atomic_store (machine_mode mode, tree exp)
6900 {
6901 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6902 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6903 || is_mm_release (model)))
6904 model = MEMMODEL_SEQ_CST;
6905
6906 if (!flag_inline_atomics)
6907 return NULL_RTX;
6908
6909 /* Expand the operands. */
6910 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6911 rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6912
6913 return expand_atomic_store (mem, val, model, false);
6914 }
6915
6916 /* Expand the __atomic_fetch_XXX intrinsic:
6917 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6918 EXP is the CALL_EXPR.
6919 TARGET is an optional place for us to store the results.
6920 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6921 FETCH_AFTER is true if returning the result of the operation.
6922 FETCH_AFTER is false if returning the value before the operation.
6923 IGNORE is true if the result is not used.
6924 EXT_CALL is the correct builtin for an external call if this cannot be
6925 resolved to an instruction sequence. */
6926
6927 static rtx
6928 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6929 enum rtx_code code, bool fetch_after,
6930 bool ignore, enum built_in_function ext_call)
6931 {
6932 rtx val, mem, ret;
6933 enum memmodel model;
6934 tree fndecl;
6935 tree addr;
6936
6937 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6938
6939 /* Expand the operands. */
6940 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6941 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6942
6943 /* Only try generating instructions if inlining is turned on. */
6944 if (flag_inline_atomics)
6945 {
6946 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6947 if (ret)
6948 return ret;
6949 }
6950
6951 /* Return if a different routine isn't needed for the library call. */
6952 if (ext_call == BUILT_IN_NONE)
6953 return NULL_RTX;
6954
6955 /* Change the call to the specified function. */
6956 fndecl = get_callee_fndecl (exp);
6957 addr = CALL_EXPR_FN (exp);
6958 STRIP_NOPS (addr);
6959
6960 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6961 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6962
6963 /* If we will emit code after the call, the call cannot be a tail call.
6964 If it is emitted as a tail call, a barrier is emitted after it, and
6965 then all trailing code is removed. */
6966 if (!ignore)
6967 CALL_EXPR_TAILCALL (exp) = 0;
6968
6969 /* Expand the call here so we can emit trailing code. */
6970 ret = expand_call (exp, target, ignore);
6971
6972 /* Replace the original function just in case it matters. */
6973 TREE_OPERAND (addr, 0) = fndecl;
6974
6975 /* Then issue the arithmetic correction to return the right result. */
6976 if (!ignore)
6977 {
6978 if (code == NOT)
6979 {
6980 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6981 OPTAB_LIB_WIDEN);
6982 ret = expand_simple_unop (mode, NOT, ret, target, true);
6983 }
6984 else
6985 ret = expand_simple_binop (mode, code, ret, val, target, true,
6986 OPTAB_LIB_WIDEN);
6987 }
6988 return ret;
6989 }
6990
6991 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6992
6993 void
6994 expand_ifn_atomic_bit_test_and (gcall *call)
6995 {
6996 tree ptr = gimple_call_arg (call, 0);
6997 tree bit = gimple_call_arg (call, 1);
6998 tree flag = gimple_call_arg (call, 2);
6999 tree lhs = gimple_call_lhs (call);
7000 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7001 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
7002 enum rtx_code code;
7003 optab optab;
7004 class expand_operand ops[5];
7005
7006 gcc_assert (flag_inline_atomics);
7007
7008 if (gimple_call_num_args (call) == 5)
7009 model = get_memmodel (gimple_call_arg (call, 3));
7010
7011 rtx mem = get_builtin_sync_mem (ptr, mode);
7012 rtx val = expand_expr_force_mode (bit, mode);
7013
7014 switch (gimple_call_internal_fn (call))
7015 {
7016 case IFN_ATOMIC_BIT_TEST_AND_SET:
7017 code = IOR;
7018 optab = atomic_bit_test_and_set_optab;
7019 break;
7020 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
7021 code = XOR;
7022 optab = atomic_bit_test_and_complement_optab;
7023 break;
7024 case IFN_ATOMIC_BIT_TEST_AND_RESET:
7025 code = AND;
7026 optab = atomic_bit_test_and_reset_optab;
7027 break;
7028 default:
7029 gcc_unreachable ();
7030 }
7031
7032 if (lhs == NULL_TREE)
7033 {
7034 rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
7035 val, NULL_RTX, true, OPTAB_DIRECT);
7036 if (code == AND)
7037 val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
7038 if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
7039 return;
7040 }
7041
7042 rtx target;
7043 if (lhs)
7044 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7045 else
7046 target = gen_reg_rtx (mode);
7047 enum insn_code icode = direct_optab_handler (optab, mode);
7048 gcc_assert (icode != CODE_FOR_nothing);
7049 create_output_operand (&ops[0], target, mode);
7050 create_fixed_operand (&ops[1], mem);
7051 create_convert_operand_to (&ops[2], val, mode, true);
7052 create_integer_operand (&ops[3], model);
7053 create_integer_operand (&ops[4], integer_onep (flag));
7054 if (maybe_expand_insn (icode, 5, ops))
7055 return;
7056
7057 rtx bitval = val;
7058 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
7059 val, NULL_RTX, true, OPTAB_DIRECT);
7060 rtx maskval = val;
7061 if (code == AND)
7062 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
7063 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
7064 code, model, false);
7065 if (!result)
7066 {
7067 bool is_atomic = gimple_call_num_args (call) == 5;
7068 tree tcall = gimple_call_arg (call, 3 + is_atomic);
7069 tree fndecl = gimple_call_addr_fndecl (tcall);
7070 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7071 tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
7072 make_tree (type, val),
7073 is_atomic
7074 ? gimple_call_arg (call, 3)
7075 : integer_zero_node);
7076 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7077 mode, !lhs);
7078 }
7079 if (!lhs)
7080 return;
7081 if (integer_onep (flag))
7082 {
7083 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
7084 NULL_RTX, true, OPTAB_DIRECT);
7085 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
7086 true, OPTAB_DIRECT);
7087 }
7088 else
7089 result = expand_simple_binop (mode, AND, result, maskval, target, true,
7090 OPTAB_DIRECT);
7091 if (result != target)
7092 emit_move_insn (target, result);
7093 }
7094
7095 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
7096
7097 void
7098 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
7099 {
7100 tree cmp = gimple_call_arg (call, 0);
7101 tree ptr = gimple_call_arg (call, 1);
7102 tree arg = gimple_call_arg (call, 2);
7103 tree lhs = gimple_call_lhs (call);
7104 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
7105 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
7106 optab optab;
7107 rtx_code code;
7108 class expand_operand ops[5];
7109
7110 gcc_assert (flag_inline_atomics);
7111
7112 if (gimple_call_num_args (call) == 5)
7113 model = get_memmodel (gimple_call_arg (call, 3));
7114
7115 rtx mem = get_builtin_sync_mem (ptr, mode);
7116 rtx op = expand_expr_force_mode (arg, mode);
7117
7118 switch (gimple_call_internal_fn (call))
7119 {
7120 case IFN_ATOMIC_ADD_FETCH_CMP_0:
7121 code = PLUS;
7122 optab = atomic_add_fetch_cmp_0_optab;
7123 break;
7124 case IFN_ATOMIC_SUB_FETCH_CMP_0:
7125 code = MINUS;
7126 optab = atomic_sub_fetch_cmp_0_optab;
7127 break;
7128 case IFN_ATOMIC_AND_FETCH_CMP_0:
7129 code = AND;
7130 optab = atomic_and_fetch_cmp_0_optab;
7131 break;
7132 case IFN_ATOMIC_OR_FETCH_CMP_0:
7133 code = IOR;
7134 optab = atomic_or_fetch_cmp_0_optab;
7135 break;
7136 case IFN_ATOMIC_XOR_FETCH_CMP_0:
7137 code = XOR;
7138 optab = atomic_xor_fetch_cmp_0_optab;
7139 break;
7140 default:
7141 gcc_unreachable ();
7142 }
7143
7144 enum rtx_code comp = UNKNOWN;
7145 switch (tree_to_uhwi (cmp))
7146 {
7147 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
7148 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
7149 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
7150 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
7151 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
7152 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
7153 default: gcc_unreachable ();
7154 }
7155
7156 rtx target;
7157 if (lhs == NULL_TREE)
7158 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
7159 else
7160 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
7161 enum insn_code icode = direct_optab_handler (optab, mode);
7162 gcc_assert (icode != CODE_FOR_nothing);
7163 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
7164 create_fixed_operand (&ops[1], mem);
7165 create_convert_operand_to (&ops[2], op, mode, true);
7166 create_integer_operand (&ops[3], model);
7167 create_integer_operand (&ops[4], comp);
7168 if (maybe_expand_insn (icode, 5, ops))
7169 return;
7170
7171 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
7172 code, model, true);
7173 if (!result)
7174 {
7175 bool is_atomic = gimple_call_num_args (call) == 5;
7176 tree tcall = gimple_call_arg (call, 3 + is_atomic);
7177 tree fndecl = gimple_call_addr_fndecl (tcall);
7178 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7179 tree exp = build_call_nary (type, tcall,
7180 2 + is_atomic, ptr, arg,
7181 is_atomic
7182 ? gimple_call_arg (call, 3)
7183 : integer_zero_node);
7184 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
7185 mode, !lhs);
7186 }
7187
7188 if (lhs)
7189 {
7190 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
7191 0, 1);
7192 if (result != target)
7193 emit_move_insn (target, result);
7194 }
7195 }
7196
7197 /* Expand an atomic clear operation.
7198 void _atomic_clear (BOOL *obj, enum memmodel)
7199 EXP is the call expression. */
7200
7201 static rtx
7202 expand_builtin_atomic_clear (tree exp)
7203 {
7204 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7205 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7206 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7207
7208 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
7209 model = MEMMODEL_SEQ_CST;
7210
7211 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
7212 Failing that, a store is issued by __atomic_store. The only way this can
7213 fail is if the bool type is larger than a word size. Unlikely, but
7214 handle it anyway for completeness. Assume a single threaded model since
7215 there is no atomic support in this case, and no barriers are required. */
7216 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
7217 if (!ret)
7218 emit_move_insn (mem, const0_rtx);
7219 return const0_rtx;
7220 }
7221
7222 /* Expand an atomic test_and_set operation.
7223 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
7224 EXP is the call expression. */
7225
7226 static rtx
7227 expand_builtin_atomic_test_and_set (tree exp, rtx target)
7228 {
7229 rtx mem;
7230 enum memmodel model;
7231 machine_mode mode;
7232
7233 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
7234 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
7235 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
7236
7237 return expand_atomic_test_and_set (target, mem, model);
7238 }
7239
7240
7241 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
7242 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
7243
7244 static tree
7245 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
7246 {
7247 int size;
7248 machine_mode mode;
7249 unsigned int mode_align, type_align;
7250
7251 if (TREE_CODE (arg0) != INTEGER_CST)
7252 return NULL_TREE;
7253
7254 /* We need a corresponding integer mode for the access to be lock-free. */
7255 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
7256 if (!int_mode_for_size (size, 0).exists (&mode))
7257 return boolean_false_node;
7258
7259 mode_align = GET_MODE_ALIGNMENT (mode);
7260
7261 if (TREE_CODE (arg1) == INTEGER_CST)
7262 {
7263 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
7264
7265 /* Either this argument is null, or it's a fake pointer encoding
7266 the alignment of the object. */
7267 val = least_bit_hwi (val);
7268 val *= BITS_PER_UNIT;
7269
7270 if (val == 0 || mode_align < val)
7271 type_align = mode_align;
7272 else
7273 type_align = val;
7274 }
7275 else
7276 {
7277 tree ttype = TREE_TYPE (arg1);
7278
7279 /* This function is usually invoked and folded immediately by the front
7280 end before anything else has a chance to look at it. The pointer
7281 parameter at this point is usually cast to a void *, so check for that
7282 and look past the cast. */
7283 if (CONVERT_EXPR_P (arg1)
7284 && POINTER_TYPE_P (ttype)
7285 && VOID_TYPE_P (TREE_TYPE (ttype))
7286 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
7287 arg1 = TREE_OPERAND (arg1, 0);
7288
7289 ttype = TREE_TYPE (arg1);
7290 gcc_assert (POINTER_TYPE_P (ttype));
7291
7292 /* Get the underlying type of the object. */
7293 ttype = TREE_TYPE (ttype);
7294 type_align = TYPE_ALIGN (ttype);
7295 }
7296
7297 /* If the object has smaller alignment, the lock free routines cannot
7298 be used. */
7299 if (type_align < mode_align)
7300 return boolean_false_node;
7301
7302 /* Check if a compare_and_swap pattern exists for the mode which represents
7303 the required size. The pattern is not allowed to fail, so the existence
7304 of the pattern indicates support is present. Also require that an
7305 atomic load exists for the required size. */
7306 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
7307 return boolean_true_node;
7308 else
7309 return boolean_false_node;
7310 }
7311
7312 /* Return true if the parameters to call EXP represent an object which will
7313 always generate lock free instructions. The first argument represents the
7314 size of the object, and the second parameter is a pointer to the object
7315 itself. If NULL is passed for the object, then the result is based on
7316 typical alignment for an object of the specified size. Otherwise return
7317 false. */
7318
7319 static rtx
7320 expand_builtin_atomic_always_lock_free (tree exp)
7321 {
7322 tree size;
7323 tree arg0 = CALL_EXPR_ARG (exp, 0);
7324 tree arg1 = CALL_EXPR_ARG (exp, 1);
7325
7326 if (TREE_CODE (arg0) != INTEGER_CST)
7327 {
7328 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
7329 return const0_rtx;
7330 }
7331
7332 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
7333 if (size == boolean_true_node)
7334 return const1_rtx;
7335 return const0_rtx;
7336 }
7337
7338 /* Return a one or zero if it can be determined that object ARG1 of size ARG
7339 is lock free on this architecture. */
7340
7341 static tree
7342 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
7343 {
7344 if (!flag_inline_atomics)
7345 return NULL_TREE;
7346
7347 /* If it isn't always lock free, don't generate a result. */
7348 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
7349 return boolean_true_node;
7350
7351 return NULL_TREE;
7352 }
7353
7354 /* Return true if the parameters to call EXP represent an object which will
7355 always generate lock free instructions. The first argument represents the
7356 size of the object, and the second parameter is a pointer to the object
7357 itself. If NULL is passed for the object, then the result is based on
7358 typical alignment for an object of the specified size. Otherwise return
7359 NULL*/
7360
7361 static rtx
7362 expand_builtin_atomic_is_lock_free (tree exp)
7363 {
7364 tree size;
7365 tree arg0 = CALL_EXPR_ARG (exp, 0);
7366 tree arg1 = CALL_EXPR_ARG (exp, 1);
7367
7368 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
7369 {
7370 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
7371 return NULL_RTX;
7372 }
7373
7374 if (!flag_inline_atomics)
7375 return NULL_RTX;
7376
7377 /* If the value is known at compile time, return the RTX for it. */
7378 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
7379 if (size == boolean_true_node)
7380 return const1_rtx;
7381
7382 return NULL_RTX;
7383 }
7384
7385 /* Expand the __atomic_thread_fence intrinsic:
7386 void __atomic_thread_fence (enum memmodel)
7387 EXP is the CALL_EXPR. */
7388
7389 static void
7390 expand_builtin_atomic_thread_fence (tree exp)
7391 {
7392 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7393 expand_mem_thread_fence (model);
7394 }
7395
7396 /* Expand the __atomic_signal_fence intrinsic:
7397 void __atomic_signal_fence (enum memmodel)
7398 EXP is the CALL_EXPR. */
7399
7400 static void
7401 expand_builtin_atomic_signal_fence (tree exp)
7402 {
7403 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
7404 expand_mem_signal_fence (model);
7405 }
7406
7407 /* Expand the __sync_synchronize intrinsic. */
7408
7409 static void
7410 expand_builtin_sync_synchronize (void)
7411 {
7412 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
7413 }
7414
7415 static rtx
7416 expand_builtin_thread_pointer (tree exp, rtx target)
7417 {
7418 enum insn_code icode;
7419 if (!validate_arglist (exp, VOID_TYPE))
7420 return const0_rtx;
7421 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
7422 if (icode != CODE_FOR_nothing)
7423 {
7424 class expand_operand op;
7425 /* If the target is not sutitable then create a new target. */
7426 if (target == NULL_RTX
7427 || !REG_P (target)
7428 || GET_MODE (target) != Pmode)
7429 target = gen_reg_rtx (Pmode);
7430 create_output_operand (&op, target, Pmode);
7431 expand_insn (icode, 1, &op);
7432 return target;
7433 }
7434 error ("%<__builtin_thread_pointer%> is not supported on this target");
7435 return const0_rtx;
7436 }
7437
7438 static void
7439 expand_builtin_set_thread_pointer (tree exp)
7440 {
7441 enum insn_code icode;
7442 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7443 return;
7444 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
7445 if (icode != CODE_FOR_nothing)
7446 {
7447 class expand_operand op;
7448 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
7449 Pmode, EXPAND_NORMAL);
7450 create_input_operand (&op, val, Pmode);
7451 expand_insn (icode, 1, &op);
7452 return;
7453 }
7454 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
7455 }
7456
7457
7458 /* Emit code to restore the current value of stack. */
7459
7460 static void
7461 expand_stack_restore (tree var)
7462 {
7463 rtx_insn *prev;
7464 rtx sa = expand_normal (var);
7465
7466 sa = convert_memory_address (Pmode, sa);
7467
7468 prev = get_last_insn ();
7469 emit_stack_restore (SAVE_BLOCK, sa);
7470
7471 record_new_stack_level ();
7472
7473 fixup_args_size_notes (prev, get_last_insn (), 0);
7474 }
7475
7476 /* Emit code to save the current value of stack. */
7477
7478 static rtx
7479 expand_stack_save (void)
7480 {
7481 rtx ret = NULL_RTX;
7482
7483 emit_stack_save (SAVE_BLOCK, &ret);
7484 return ret;
7485 }
7486
7487 /* Emit code to get the openacc gang, worker or vector id or size. */
7488
7489 static rtx
7490 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7491 {
7492 const char *name;
7493 rtx fallback_retval;
7494 rtx_insn *(*gen_fn) (rtx, rtx);
7495 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7496 {
7497 case BUILT_IN_GOACC_PARLEVEL_ID:
7498 name = "__builtin_goacc_parlevel_id";
7499 fallback_retval = const0_rtx;
7500 gen_fn = targetm.gen_oacc_dim_pos;
7501 break;
7502 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7503 name = "__builtin_goacc_parlevel_size";
7504 fallback_retval = const1_rtx;
7505 gen_fn = targetm.gen_oacc_dim_size;
7506 break;
7507 default:
7508 gcc_unreachable ();
7509 }
7510
7511 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7512 {
7513 error ("%qs only supported in OpenACC code", name);
7514 return const0_rtx;
7515 }
7516
7517 tree arg = CALL_EXPR_ARG (exp, 0);
7518 if (TREE_CODE (arg) != INTEGER_CST)
7519 {
7520 error ("non-constant argument 0 to %qs", name);
7521 return const0_rtx;
7522 }
7523
7524 int dim = TREE_INT_CST_LOW (arg);
7525 switch (dim)
7526 {
7527 case GOMP_DIM_GANG:
7528 case GOMP_DIM_WORKER:
7529 case GOMP_DIM_VECTOR:
7530 break;
7531 default:
7532 error ("illegal argument 0 to %qs", name);
7533 return const0_rtx;
7534 }
7535
7536 if (ignore)
7537 return target;
7538
7539 if (target == NULL_RTX)
7540 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7541
7542 if (!targetm.have_oacc_dim_size ())
7543 {
7544 emit_move_insn (target, fallback_retval);
7545 return target;
7546 }
7547
7548 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7549 emit_insn (gen_fn (reg, GEN_INT (dim)));
7550 if (reg != target)
7551 emit_move_insn (target, reg);
7552
7553 return target;
7554 }
7555
7556 /* Expand a string compare operation using a sequence of char comparison
7557 to get rid of the calling overhead, with result going to TARGET if
7558 that's convenient.
7559
7560 VAR_STR is the variable string source;
7561 CONST_STR is the constant string source;
7562 LENGTH is the number of chars to compare;
7563 CONST_STR_N indicates which source string is the constant string;
7564 IS_MEMCMP indicates whether it's a memcmp or strcmp.
7565
7566 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7567
7568 target = (int) (unsigned char) var_str[0]
7569 - (int) (unsigned char) const_str[0];
7570 if (target != 0)
7571 goto ne_label;
7572 ...
7573 target = (int) (unsigned char) var_str[length - 2]
7574 - (int) (unsigned char) const_str[length - 2];
7575 if (target != 0)
7576 goto ne_label;
7577 target = (int) (unsigned char) var_str[length - 1]
7578 - (int) (unsigned char) const_str[length - 1];
7579 ne_label:
7580 */
7581
7582 static rtx
7583 inline_string_cmp (rtx target, tree var_str, const char *const_str,
7584 unsigned HOST_WIDE_INT length,
7585 int const_str_n, machine_mode mode)
7586 {
7587 HOST_WIDE_INT offset = 0;
7588 rtx var_rtx_array
7589 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7590 rtx var_rtx = NULL_RTX;
7591 rtx const_rtx = NULL_RTX;
7592 rtx result = target ? target : gen_reg_rtx (mode);
7593 rtx_code_label *ne_label = gen_label_rtx ();
7594 tree unit_type_node = unsigned_char_type_node;
7595 scalar_int_mode unit_mode
7596 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7597
7598 start_sequence ();
7599
7600 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7601 {
7602 var_rtx
7603 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7604 const_rtx = c_readstr (const_str + offset, unit_mode);
7605 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7606 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7607
7608 op0 = convert_modes (mode, unit_mode, op0, 1);
7609 op1 = convert_modes (mode, unit_mode, op1, 1);
7610 rtx diff = expand_simple_binop (mode, MINUS, op0, op1,
7611 result, 1, OPTAB_WIDEN);
7612
7613 /* Force the difference into result register. We cannot reassign
7614 result here ("result = diff") or we may end up returning
7615 uninitialized result when expand_simple_binop allocates a new
7616 pseudo-register for returning. */
7617 if (diff != result)
7618 emit_move_insn (result, diff);
7619
7620 if (i < length - 1)
7621 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7622 mode, true, ne_label);
7623 offset += GET_MODE_SIZE (unit_mode);
7624 }
7625
7626 emit_label (ne_label);
7627 rtx_insn *insns = get_insns ();
7628 end_sequence ();
7629 emit_insn (insns);
7630
7631 return result;
7632 }
7633
7634 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7635 to TARGET if that's convenient.
7636 If the call is not been inlined, return NULL_RTX. */
7637
7638 static rtx
7639 inline_expand_builtin_bytecmp (tree exp, rtx target)
7640 {
7641 tree fndecl = get_callee_fndecl (exp);
7642 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7643 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7644
7645 /* Do NOT apply this inlining expansion when optimizing for size or
7646 optimization level below 2 or if unused *cmp hasn't been DCEd. */
7647 if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7648 return NULL_RTX;
7649
7650 gcc_checking_assert (fcode == BUILT_IN_STRCMP
7651 || fcode == BUILT_IN_STRNCMP
7652 || fcode == BUILT_IN_MEMCMP);
7653
7654 /* On a target where the type of the call (int) has same or narrower presicion
7655 than unsigned char, give up the inlining expansion. */
7656 if (TYPE_PRECISION (unsigned_char_type_node)
7657 >= TYPE_PRECISION (TREE_TYPE (exp)))
7658 return NULL_RTX;
7659
7660 tree arg1 = CALL_EXPR_ARG (exp, 0);
7661 tree arg2 = CALL_EXPR_ARG (exp, 1);
7662 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7663
7664 unsigned HOST_WIDE_INT len1 = 0;
7665 unsigned HOST_WIDE_INT len2 = 0;
7666 unsigned HOST_WIDE_INT len3 = 0;
7667
7668 /* Get the object representation of the initializers of ARG1 and ARG2
7669 as strings, provided they refer to constant objects, with their byte
7670 sizes in LEN1 and LEN2, respectively. */
7671 const char *bytes1 = getbyterep (arg1, &len1);
7672 const char *bytes2 = getbyterep (arg2, &len2);
7673
7674 /* Fail if neither argument refers to an initialized constant. */
7675 if (!bytes1 && !bytes2)
7676 return NULL_RTX;
7677
7678 if (is_ncmp)
7679 {
7680 /* Fail if the memcmp/strncmp bound is not a constant. */
7681 if (!tree_fits_uhwi_p (len3_tree))
7682 return NULL_RTX;
7683
7684 len3 = tree_to_uhwi (len3_tree);
7685
7686 if (fcode == BUILT_IN_MEMCMP)
7687 {
7688 /* Fail if the memcmp bound is greater than the size of either
7689 of the two constant objects. */
7690 if ((bytes1 && len1 < len3)
7691 || (bytes2 && len2 < len3))
7692 return NULL_RTX;
7693 }
7694 }
7695
7696 if (fcode != BUILT_IN_MEMCMP)
7697 {
7698 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7699 and LEN2 to the length of the nul-terminated string stored
7700 in each. */
7701 if (bytes1 != NULL)
7702 len1 = strnlen (bytes1, len1) + 1;
7703 if (bytes2 != NULL)
7704 len2 = strnlen (bytes2, len2) + 1;
7705 }
7706
7707 /* See inline_string_cmp. */
7708 int const_str_n;
7709 if (!len1)
7710 const_str_n = 2;
7711 else if (!len2)
7712 const_str_n = 1;
7713 else if (len2 > len1)
7714 const_str_n = 1;
7715 else
7716 const_str_n = 2;
7717
7718 /* For strncmp only, compute the new bound as the smallest of
7719 the lengths of the two strings (plus 1) and the bound provided
7720 to the function. */
7721 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7722 if (is_ncmp && len3 < bound)
7723 bound = len3;
7724
7725 /* If the bound of the comparison is larger than the threshold,
7726 do nothing. */
7727 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7728 return NULL_RTX;
7729
7730 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7731
7732 /* Now, start inline expansion the call. */
7733 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7734 (const_str_n == 1) ? bytes1 : bytes2, bound,
7735 const_str_n, mode);
7736 }
7737
7738 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7739 represents the size of the first argument to that call, or VOIDmode
7740 if the argument is a pointer. IGNORE will be true if the result
7741 isn't used. */
7742 static rtx
7743 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7744 bool ignore)
7745 {
7746 rtx val, failsafe;
7747 unsigned nargs = call_expr_nargs (exp);
7748
7749 tree arg0 = CALL_EXPR_ARG (exp, 0);
7750
7751 if (mode == VOIDmode)
7752 {
7753 mode = TYPE_MODE (TREE_TYPE (arg0));
7754 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7755 }
7756
7757 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7758
7759 /* An optional second argument can be used as a failsafe value on
7760 some machines. If it isn't present, then the failsafe value is
7761 assumed to be 0. */
7762 if (nargs > 1)
7763 {
7764 tree arg1 = CALL_EXPR_ARG (exp, 1);
7765 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7766 }
7767 else
7768 failsafe = const0_rtx;
7769
7770 /* If the result isn't used, the behavior is undefined. It would be
7771 nice to emit a warning here, but path splitting means this might
7772 happen with legitimate code. So simply drop the builtin
7773 expansion in that case; we've handled any side-effects above. */
7774 if (ignore)
7775 return const0_rtx;
7776
7777 /* If we don't have a suitable target, create one to hold the result. */
7778 if (target == NULL || GET_MODE (target) != mode)
7779 target = gen_reg_rtx (mode);
7780
7781 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7782 val = convert_modes (mode, VOIDmode, val, false);
7783
7784 return targetm.speculation_safe_value (mode, target, val, failsafe);
7785 }
7786
7787 /* Expand CRC* or REV_CRC* built-ins. */
7788
7789 rtx
7790 expand_builtin_crc_table_based (internal_fn fn, scalar_mode crc_mode,
7791 scalar_mode data_mode, machine_mode mode,
7792 tree exp, rtx target)
7793 {
7794 tree rhs1 = CALL_EXPR_ARG (exp, 0); // crc
7795 tree rhs2 = CALL_EXPR_ARG (exp, 1); // data
7796 tree rhs3 = CALL_EXPR_ARG (exp, 2); // polynomial
7797
7798 if (!target || mode == VOIDmode)
7799 target = gen_reg_rtx (crc_mode);
7800
7801 rtx op1 = expand_normal (rhs1);
7802 rtx op2 = expand_normal (rhs2);
7803 rtx op3;
7804 if (TREE_CODE (rhs3) != INTEGER_CST)
7805 {
7806 error ("third argument to %<crc%> builtins must be a constant");
7807 op3 = const0_rtx;
7808 }
7809 else
7810 op3 = convert_to_mode (crc_mode, expand_normal (rhs3), 0);
7811
7812 if (CONST_INT_P (op2))
7813 op2 = convert_to_mode (crc_mode, op2, 0);
7814
7815 if (fn == IFN_CRC)
7816 expand_crc_table_based (target, op1, op2, op3, data_mode);
7817 else
7818 /* If it's IFN_CRC_REV generate bit-reversed CRC. */
7819 expand_reversed_crc_table_based (target, op1, op2, op3,
7820 data_mode,
7821 generate_reflecting_code_standard);
7822 return target;
7823 }
7824
7825 /* Expand an expression EXP that calls a built-in function,
7826 with result going to TARGET if that's convenient
7827 (and in mode MODE if that's convenient).
7828 SUBTARGET may be used as the target for computing one of EXP's operands.
7829 IGNORE is nonzero if the value is to be ignored. */
7830
7831 rtx
7832 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7833 int ignore)
7834 {
7835 tree fndecl = get_callee_fndecl (exp);
7836 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7837 int flags;
7838
7839 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7840 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7841
7842 /* When ASan is enabled, we don't want to expand some memory/string
7843 builtins and rely on libsanitizer's hooks. This allows us to avoid
7844 redundant checks and be sure, that possible overflow will be detected
7845 by ASan. */
7846
7847 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7848 if (param_asan_kernel_mem_intrinsic_prefix
7849 && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7850 | SANITIZE_KERNEL_HWADDRESS))
7851 switch (fcode)
7852 {
7853 rtx save_decl_rtl, ret;
7854 case BUILT_IN_MEMCPY:
7855 case BUILT_IN_MEMMOVE:
7856 case BUILT_IN_MEMSET:
7857 save_decl_rtl = DECL_RTL (fndecl);
7858 DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7859 ret = expand_call (exp, target, ignore);
7860 DECL_RTL (fndecl) = save_decl_rtl;
7861 return ret;
7862 default:
7863 break;
7864 }
7865 if (sanitize_flags_p (SANITIZE_ADDRESS | SANITIZE_HWADDRESS)
7866 && asan_intercepted_p (fcode))
7867 return expand_call (exp, target, ignore);
7868
7869 /* When not optimizing, generate calls to library functions for a certain
7870 set of builtins. */
7871 if (!optimize
7872 && !called_as_built_in (fndecl)
7873 && fcode != BUILT_IN_FORK
7874 && fcode != BUILT_IN_EXECL
7875 && fcode != BUILT_IN_EXECV
7876 && fcode != BUILT_IN_EXECLP
7877 && fcode != BUILT_IN_EXECLE
7878 && fcode != BUILT_IN_EXECVP
7879 && fcode != BUILT_IN_EXECVE
7880 && fcode != BUILT_IN_CLEAR_CACHE
7881 && !ALLOCA_FUNCTION_CODE_P (fcode)
7882 && fcode != BUILT_IN_FREE
7883 && (fcode != BUILT_IN_MEMSET
7884 || !(flag_inline_stringops & ILSOP_MEMSET))
7885 && (fcode != BUILT_IN_MEMCPY
7886 || !(flag_inline_stringops & ILSOP_MEMCPY))
7887 && (fcode != BUILT_IN_MEMMOVE
7888 || !(flag_inline_stringops & ILSOP_MEMMOVE))
7889 && (fcode != BUILT_IN_MEMCMP
7890 || !(flag_inline_stringops & ILSOP_MEMCMP)))
7891 return expand_call (exp, target, ignore);
7892
7893 /* The built-in function expanders test for target == const0_rtx
7894 to determine whether the function's result will be ignored. */
7895 if (ignore)
7896 target = const0_rtx;
7897
7898 /* If the result of a pure or const built-in function is ignored, and
7899 none of its arguments are volatile, we can avoid expanding the
7900 built-in call and just evaluate the arguments for side-effects. */
7901 if (target == const0_rtx
7902 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7903 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7904 {
7905 bool volatilep = false;
7906 tree arg;
7907 call_expr_arg_iterator iter;
7908
7909 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7910 if (TREE_THIS_VOLATILE (arg))
7911 {
7912 volatilep = true;
7913 break;
7914 }
7915
7916 if (! volatilep)
7917 {
7918 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7919 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7920 return const0_rtx;
7921 }
7922 }
7923
7924 switch (fcode)
7925 {
7926 CASE_FLT_FN (BUILT_IN_FABS):
7927 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7928 case BUILT_IN_FABSD32:
7929 case BUILT_IN_FABSD64:
7930 case BUILT_IN_FABSD128:
7931 case BUILT_IN_FABSD64X:
7932 target = expand_builtin_fabs (exp, target, subtarget);
7933 if (target)
7934 return target;
7935 break;
7936
7937 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7938 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7939 target = expand_builtin_copysign (exp, target, subtarget);
7940 if (target)
7941 return target;
7942 break;
7943
7944 /* Just do a normal library call if we were unable to fold
7945 the values. */
7946 CASE_FLT_FN (BUILT_IN_CABS):
7947 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7948 break;
7949
7950 CASE_FLT_FN (BUILT_IN_FMA):
7951 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7952 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7953 if (target)
7954 return target;
7955 break;
7956
7957 CASE_FLT_FN (BUILT_IN_ILOGB):
7958 if (! flag_unsafe_math_optimizations)
7959 break;
7960 gcc_fallthrough ();
7961 CASE_FLT_FN (BUILT_IN_ISINF):
7962 CASE_FLT_FN (BUILT_IN_FINITE):
7963 case BUILT_IN_ISFINITE:
7964 case BUILT_IN_ISNORMAL:
7965 target = expand_builtin_interclass_mathfn (exp, target);
7966 if (target)
7967 return target;
7968 break;
7969
7970 case BUILT_IN_ISSIGNALING:
7971 target = expand_builtin_issignaling (exp, target);
7972 if (target)
7973 return target;
7974 break;
7975
7976 CASE_FLT_FN (BUILT_IN_ICEIL):
7977 CASE_FLT_FN (BUILT_IN_LCEIL):
7978 CASE_FLT_FN (BUILT_IN_LLCEIL):
7979 CASE_FLT_FN (BUILT_IN_LFLOOR):
7980 CASE_FLT_FN (BUILT_IN_IFLOOR):
7981 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7982 target = expand_builtin_int_roundingfn (exp, target);
7983 if (target)
7984 return target;
7985 break;
7986
7987 CASE_FLT_FN (BUILT_IN_IRINT):
7988 CASE_FLT_FN (BUILT_IN_LRINT):
7989 CASE_FLT_FN (BUILT_IN_LLRINT):
7990 CASE_FLT_FN (BUILT_IN_IROUND):
7991 CASE_FLT_FN (BUILT_IN_LROUND):
7992 CASE_FLT_FN (BUILT_IN_LLROUND):
7993 target = expand_builtin_int_roundingfn_2 (exp, target);
7994 if (target)
7995 return target;
7996 break;
7997
7998 CASE_FLT_FN (BUILT_IN_POWI):
7999 target = expand_builtin_powi (exp, target);
8000 if (target)
8001 return target;
8002 break;
8003
8004 CASE_FLT_FN (BUILT_IN_CEXPI):
8005 target = expand_builtin_cexpi (exp, target);
8006 gcc_assert (target);
8007 return target;
8008
8009 CASE_FLT_FN (BUILT_IN_SIN):
8010 CASE_FLT_FN (BUILT_IN_COS):
8011 if (! flag_unsafe_math_optimizations)
8012 break;
8013 target = expand_builtin_mathfn_3 (exp, target, subtarget);
8014 if (target)
8015 return target;
8016 break;
8017
8018 CASE_FLT_FN (BUILT_IN_SINCOS):
8019 if (! flag_unsafe_math_optimizations)
8020 break;
8021 target = expand_builtin_sincos (exp);
8022 if (target)
8023 return target;
8024 break;
8025
8026 case BUILT_IN_FEGETROUND:
8027 target = expand_builtin_fegetround (exp, target, target_mode);
8028 if (target)
8029 return target;
8030 break;
8031
8032 case BUILT_IN_FECLEAREXCEPT:
8033 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8034 feclearexcept_optab);
8035 if (target)
8036 return target;
8037 break;
8038
8039 case BUILT_IN_FERAISEEXCEPT:
8040 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
8041 feraiseexcept_optab);
8042 if (target)
8043 return target;
8044 break;
8045
8046 case BUILT_IN_APPLY_ARGS:
8047 return expand_builtin_apply_args ();
8048
8049 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
8050 FUNCTION with a copy of the parameters described by
8051 ARGUMENTS, and ARGSIZE. It returns a block of memory
8052 allocated on the stack into which is stored all the registers
8053 that might possibly be used for returning the result of a
8054 function. ARGUMENTS is the value returned by
8055 __builtin_apply_args. ARGSIZE is the number of bytes of
8056 arguments that must be copied. ??? How should this value be
8057 computed? We'll also need a safe worst case value for varargs
8058 functions. */
8059 case BUILT_IN_APPLY:
8060 if (!validate_arglist (exp, POINTER_TYPE,
8061 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
8062 && !validate_arglist (exp, REFERENCE_TYPE,
8063 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8064 return const0_rtx;
8065 else
8066 {
8067 rtx ops[3];
8068
8069 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
8070 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
8071 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
8072
8073 return expand_builtin_apply (ops[0], ops[1], ops[2]);
8074 }
8075
8076 /* __builtin_return (RESULT) causes the function to return the
8077 value described by RESULT. RESULT is address of the block of
8078 memory returned by __builtin_apply. */
8079 case BUILT_IN_RETURN:
8080 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8081 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
8082 return const0_rtx;
8083
8084 case BUILT_IN_SAVEREGS:
8085 return expand_builtin_saveregs ();
8086
8087 case BUILT_IN_VA_ARG_PACK:
8088 /* All valid uses of __builtin_va_arg_pack () are removed during
8089 inlining. */
8090 error ("invalid use of %<__builtin_va_arg_pack ()%>");
8091 return const0_rtx;
8092
8093 case BUILT_IN_VA_ARG_PACK_LEN:
8094 /* All valid uses of __builtin_va_arg_pack_len () are removed during
8095 inlining. */
8096 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
8097 return const0_rtx;
8098
8099 /* Return the address of the first anonymous stack arg. */
8100 case BUILT_IN_NEXT_ARG:
8101 if (fold_builtin_next_arg (exp, false))
8102 return const0_rtx;
8103 return expand_builtin_next_arg ();
8104
8105 case BUILT_IN_CLEAR_CACHE:
8106 expand_builtin___clear_cache (exp);
8107 return const0_rtx;
8108
8109 case BUILT_IN_CLASSIFY_TYPE:
8110 return expand_builtin_classify_type (exp);
8111
8112 case BUILT_IN_CONSTANT_P:
8113 return const0_rtx;
8114
8115 case BUILT_IN_FRAME_ADDRESS:
8116 case BUILT_IN_RETURN_ADDRESS:
8117 return expand_builtin_frame_address (fndecl, exp);
8118
8119 case BUILT_IN_STACK_ADDRESS:
8120 return expand_builtin_stack_address ();
8121
8122 case BUILT_IN___STRUB_ENTER:
8123 target = expand_builtin_strub_enter (exp);
8124 if (target)
8125 return target;
8126 break;
8127
8128 case BUILT_IN___STRUB_UPDATE:
8129 target = expand_builtin_strub_update (exp);
8130 if (target)
8131 return target;
8132 break;
8133
8134 case BUILT_IN___STRUB_LEAVE:
8135 target = expand_builtin_strub_leave (exp);
8136 if (target)
8137 return target;
8138 break;
8139
8140 /* Returns the address of the area where the structure is returned.
8141 0 otherwise. */
8142 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
8143 if (call_expr_nargs (exp) != 0
8144 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
8145 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
8146 return const0_rtx;
8147 else
8148 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
8149
8150 CASE_BUILT_IN_ALLOCA:
8151 target = expand_builtin_alloca (exp);
8152 if (target)
8153 return target;
8154 break;
8155
8156 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
8157 return expand_asan_emit_allocas_unpoison (exp);
8158
8159 case BUILT_IN_STACK_SAVE:
8160 return expand_stack_save ();
8161
8162 case BUILT_IN_STACK_RESTORE:
8163 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
8164 return const0_rtx;
8165
8166 case BUILT_IN_BSWAP16:
8167 case BUILT_IN_BSWAP32:
8168 case BUILT_IN_BSWAP64:
8169 case BUILT_IN_BSWAP128:
8170 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
8171 if (target)
8172 return target;
8173 break;
8174
8175 CASE_INT_FN (BUILT_IN_FFS):
8176 target = expand_builtin_unop (target_mode, exp, target,
8177 subtarget, ffs_optab);
8178 if (target)
8179 return target;
8180 break;
8181
8182 CASE_INT_FN (BUILT_IN_CLZ):
8183 target = expand_builtin_unop (target_mode, exp, target,
8184 subtarget, clz_optab);
8185 if (target)
8186 return target;
8187 break;
8188
8189 CASE_INT_FN (BUILT_IN_CTZ):
8190 target = expand_builtin_unop (target_mode, exp, target,
8191 subtarget, ctz_optab);
8192 if (target)
8193 return target;
8194 break;
8195
8196 CASE_INT_FN (BUILT_IN_CLRSB):
8197 target = expand_builtin_unop (target_mode, exp, target,
8198 subtarget, clrsb_optab);
8199 if (target)
8200 return target;
8201 break;
8202
8203 CASE_INT_FN (BUILT_IN_POPCOUNT):
8204 target = expand_builtin_unop (target_mode, exp, target,
8205 subtarget, popcount_optab);
8206 if (target)
8207 return target;
8208 break;
8209
8210 CASE_INT_FN (BUILT_IN_PARITY):
8211 target = expand_builtin_unop (target_mode, exp, target,
8212 subtarget, parity_optab);
8213 if (target)
8214 return target;
8215 break;
8216
8217 case BUILT_IN_STRLEN:
8218 target = expand_builtin_strlen (exp, target, target_mode);
8219 if (target)
8220 return target;
8221 break;
8222
8223 case BUILT_IN_STRNLEN:
8224 target = expand_builtin_strnlen (exp, target, target_mode);
8225 if (target)
8226 return target;
8227 break;
8228
8229 case BUILT_IN_STRCPY:
8230 target = expand_builtin_strcpy (exp, target);
8231 if (target)
8232 return target;
8233 break;
8234
8235 case BUILT_IN_STRNCPY:
8236 target = expand_builtin_strncpy (exp, target);
8237 if (target)
8238 return target;
8239 break;
8240
8241 case BUILT_IN_STPCPY:
8242 target = expand_builtin_stpcpy (exp, target, mode);
8243 if (target)
8244 return target;
8245 break;
8246
8247 case BUILT_IN_MEMCPY:
8248 target = expand_builtin_memcpy (exp, target);
8249 if (target)
8250 return target;
8251 break;
8252
8253 case BUILT_IN_MEMMOVE:
8254 target = expand_builtin_memmove (exp, target);
8255 if (target)
8256 return target;
8257 break;
8258
8259 case BUILT_IN_MEMPCPY:
8260 target = expand_builtin_mempcpy (exp, target);
8261 if (target)
8262 return target;
8263 break;
8264
8265 case BUILT_IN_MEMSET:
8266 target = expand_builtin_memset (exp, target, mode);
8267 if (target)
8268 return target;
8269 break;
8270
8271 case BUILT_IN_BZERO:
8272 target = expand_builtin_bzero (exp);
8273 if (target)
8274 return target;
8275 break;
8276
8277 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8278 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
8279 when changing it to a strcmp call. */
8280 case BUILT_IN_STRCMP_EQ:
8281 target = expand_builtin_memcmp (exp, target, true);
8282 if (target)
8283 return target;
8284
8285 /* Change this call back to a BUILT_IN_STRCMP. */
8286 TREE_OPERAND (exp, 1)
8287 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
8288
8289 /* Delete the last parameter. */
8290 unsigned int i;
8291 vec<tree, va_gc> *arg_vec;
8292 vec_alloc (arg_vec, 2);
8293 for (i = 0; i < 2; i++)
8294 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
8295 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
8296 /* FALLTHROUGH */
8297
8298 case BUILT_IN_STRCMP:
8299 target = expand_builtin_strcmp (exp, target);
8300 if (target)
8301 return target;
8302 break;
8303
8304 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
8305 back to a BUILT_IN_STRNCMP. */
8306 case BUILT_IN_STRNCMP_EQ:
8307 target = expand_builtin_memcmp (exp, target, true);
8308 if (target)
8309 return target;
8310
8311 /* Change it back to a BUILT_IN_STRNCMP. */
8312 TREE_OPERAND (exp, 1)
8313 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
8314 /* FALLTHROUGH */
8315
8316 case BUILT_IN_STRNCMP:
8317 target = expand_builtin_strncmp (exp, target, mode);
8318 if (target)
8319 return target;
8320 break;
8321
8322 case BUILT_IN_BCMP:
8323 case BUILT_IN_MEMCMP:
8324 case BUILT_IN_MEMCMP_EQ:
8325 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
8326 if (target)
8327 return target;
8328 if (fcode == BUILT_IN_MEMCMP_EQ)
8329 {
8330 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
8331 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
8332 }
8333 break;
8334
8335 case BUILT_IN_SETJMP:
8336 /* This should have been lowered to the builtins below. */
8337 gcc_unreachable ();
8338
8339 case BUILT_IN_SETJMP_SETUP:
8340 /* __builtin_setjmp_setup is passed a pointer to an array of five words
8341 and the receiver label. */
8342 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8343 {
8344 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8345 VOIDmode, EXPAND_NORMAL);
8346 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
8347 rtx_insn *label_r = label_rtx (label);
8348
8349 expand_builtin_setjmp_setup (buf_addr, label_r);
8350 return const0_rtx;
8351 }
8352 break;
8353
8354 case BUILT_IN_SETJMP_RECEIVER:
8355 /* __builtin_setjmp_receiver is passed the receiver label. */
8356 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8357 {
8358 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
8359 rtx_insn *label_r = label_rtx (label);
8360
8361 expand_builtin_setjmp_receiver (label_r);
8362 nonlocal_goto_handler_labels
8363 = gen_rtx_INSN_LIST (VOIDmode, label_r,
8364 nonlocal_goto_handler_labels);
8365 /* ??? Do not let expand_label treat us as such since we would
8366 not want to be both on the list of non-local labels and on
8367 the list of forced labels. */
8368 FORCED_LABEL (label) = 0;
8369 return const0_rtx;
8370 }
8371 break;
8372
8373 /* __builtin_longjmp is passed a pointer to an array of five words.
8374 It's similar to the C library longjmp function but works with
8375 __builtin_setjmp above. */
8376 case BUILT_IN_LONGJMP:
8377 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8378 {
8379 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8380 VOIDmode, EXPAND_NORMAL);
8381 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
8382
8383 if (value != const1_rtx)
8384 {
8385 error ("%<__builtin_longjmp%> second argument must be 1");
8386 return const0_rtx;
8387 }
8388
8389 expand_builtin_longjmp (buf_addr, value);
8390 return const0_rtx;
8391 }
8392 break;
8393
8394 case BUILT_IN_NONLOCAL_GOTO:
8395 target = expand_builtin_nonlocal_goto (exp);
8396 if (target)
8397 return target;
8398 break;
8399
8400 /* This updates the setjmp buffer that is its argument with the value
8401 of the current stack pointer. */
8402 case BUILT_IN_UPDATE_SETJMP_BUF:
8403 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
8404 {
8405 rtx buf_addr
8406 = expand_normal (CALL_EXPR_ARG (exp, 0));
8407
8408 expand_builtin_update_setjmp_buf (buf_addr);
8409 return const0_rtx;
8410 }
8411 break;
8412
8413 case BUILT_IN_TRAP:
8414 case BUILT_IN_UNREACHABLE_TRAP:
8415 expand_builtin_trap ();
8416 return const0_rtx;
8417
8418 case BUILT_IN_UNREACHABLE:
8419 expand_builtin_unreachable ();
8420 return const0_rtx;
8421
8422 CASE_FLT_FN (BUILT_IN_SIGNBIT):
8423 case BUILT_IN_SIGNBITD32:
8424 case BUILT_IN_SIGNBITD64:
8425 case BUILT_IN_SIGNBITD128:
8426 target = expand_builtin_signbit (exp, target);
8427 if (target)
8428 return target;
8429 break;
8430
8431 /* Various hooks for the DWARF 2 __throw routine. */
8432 case BUILT_IN_UNWIND_INIT:
8433 expand_builtin_unwind_init ();
8434 return const0_rtx;
8435 case BUILT_IN_DWARF_CFA:
8436 return virtual_cfa_rtx;
8437 #ifdef DWARF2_UNWIND_INFO
8438 case BUILT_IN_DWARF_SP_COLUMN:
8439 return expand_builtin_dwarf_sp_column ();
8440 case BUILT_IN_INIT_DWARF_REG_SIZES:
8441 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
8442 return const0_rtx;
8443 #endif
8444 case BUILT_IN_FROB_RETURN_ADDR:
8445 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
8446 case BUILT_IN_EXTRACT_RETURN_ADDR:
8447 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
8448 case BUILT_IN_EH_RETURN:
8449 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
8450 CALL_EXPR_ARG (exp, 1));
8451 return const0_rtx;
8452 case BUILT_IN_EH_RETURN_DATA_REGNO:
8453 return expand_builtin_eh_return_data_regno (exp);
8454 case BUILT_IN_EXTEND_POINTER:
8455 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
8456 case BUILT_IN_EH_POINTER:
8457 return expand_builtin_eh_pointer (exp);
8458 case BUILT_IN_EH_FILTER:
8459 return expand_builtin_eh_filter (exp);
8460 case BUILT_IN_EH_COPY_VALUES:
8461 return expand_builtin_eh_copy_values (exp);
8462
8463 case BUILT_IN_VA_START:
8464 return expand_builtin_va_start (exp);
8465 case BUILT_IN_VA_END:
8466 return expand_builtin_va_end (exp);
8467 case BUILT_IN_VA_COPY:
8468 return expand_builtin_va_copy (exp);
8469 case BUILT_IN_EXPECT:
8470 return expand_builtin_expect (exp, target);
8471 case BUILT_IN_EXPECT_WITH_PROBABILITY:
8472 return expand_builtin_expect_with_probability (exp, target);
8473 case BUILT_IN_ASSUME_ALIGNED:
8474 return expand_builtin_assume_aligned (exp, target);
8475 case BUILT_IN_PREFETCH:
8476 expand_builtin_prefetch (exp);
8477 return const0_rtx;
8478
8479 case BUILT_IN_INIT_TRAMPOLINE:
8480 return expand_builtin_init_trampoline (exp, true);
8481 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
8482 return expand_builtin_init_trampoline (exp, false);
8483 case BUILT_IN_ADJUST_TRAMPOLINE:
8484 return expand_builtin_adjust_trampoline (exp);
8485
8486 case BUILT_IN_INIT_DESCRIPTOR:
8487 return expand_builtin_init_descriptor (exp);
8488 case BUILT_IN_ADJUST_DESCRIPTOR:
8489 return expand_builtin_adjust_descriptor (exp);
8490
8491 case BUILT_IN_GCC_NESTED_PTR_CREATED:
8492 case BUILT_IN_GCC_NESTED_PTR_DELETED:
8493 break; /* At present, no expansion, just call the function. */
8494
8495 case BUILT_IN_FORK:
8496 case BUILT_IN_EXECL:
8497 case BUILT_IN_EXECV:
8498 case BUILT_IN_EXECLP:
8499 case BUILT_IN_EXECLE:
8500 case BUILT_IN_EXECVP:
8501 case BUILT_IN_EXECVE:
8502 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
8503 if (target)
8504 return target;
8505 break;
8506
8507 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
8508 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
8509 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
8510 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
8511 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
8512 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
8513 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
8514 if (target)
8515 return target;
8516 break;
8517
8518 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
8519 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
8520 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
8521 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
8522 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
8523 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
8524 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
8525 if (target)
8526 return target;
8527 break;
8528
8529 case BUILT_IN_SYNC_FETCH_AND_OR_1:
8530 case BUILT_IN_SYNC_FETCH_AND_OR_2:
8531 case BUILT_IN_SYNC_FETCH_AND_OR_4:
8532 case BUILT_IN_SYNC_FETCH_AND_OR_8:
8533 case BUILT_IN_SYNC_FETCH_AND_OR_16:
8534 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8535 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8536 if (target)
8537 return target;
8538 break;
8539
8540 case BUILT_IN_SYNC_FETCH_AND_AND_1:
8541 case BUILT_IN_SYNC_FETCH_AND_AND_2:
8542 case BUILT_IN_SYNC_FETCH_AND_AND_4:
8543 case BUILT_IN_SYNC_FETCH_AND_AND_8:
8544 case BUILT_IN_SYNC_FETCH_AND_AND_16:
8545 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8546 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8547 if (target)
8548 return target;
8549 break;
8550
8551 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8552 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8553 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8554 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8555 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8556 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8557 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8558 if (target)
8559 return target;
8560 break;
8561
8562 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8563 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8564 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8565 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8566 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8567 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8568 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8569 if (target)
8570 return target;
8571 break;
8572
8573 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8574 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8575 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8576 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8577 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8578 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8579 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8580 if (target)
8581 return target;
8582 break;
8583
8584 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8585 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8586 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8587 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8588 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8589 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8590 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8591 if (target)
8592 return target;
8593 break;
8594
8595 case BUILT_IN_SYNC_OR_AND_FETCH_1:
8596 case BUILT_IN_SYNC_OR_AND_FETCH_2:
8597 case BUILT_IN_SYNC_OR_AND_FETCH_4:
8598 case BUILT_IN_SYNC_OR_AND_FETCH_8:
8599 case BUILT_IN_SYNC_OR_AND_FETCH_16:
8600 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8601 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8602 if (target)
8603 return target;
8604 break;
8605
8606 case BUILT_IN_SYNC_AND_AND_FETCH_1:
8607 case BUILT_IN_SYNC_AND_AND_FETCH_2:
8608 case BUILT_IN_SYNC_AND_AND_FETCH_4:
8609 case BUILT_IN_SYNC_AND_AND_FETCH_8:
8610 case BUILT_IN_SYNC_AND_AND_FETCH_16:
8611 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8612 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8613 if (target)
8614 return target;
8615 break;
8616
8617 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8618 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8619 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8620 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8621 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8622 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8623 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8624 if (target)
8625 return target;
8626 break;
8627
8628 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8629 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8630 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8631 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8632 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8633 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8634 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8635 if (target)
8636 return target;
8637 break;
8638
8639 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8640 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8641 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8642 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8643 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8644 if (mode == VOIDmode)
8645 mode = TYPE_MODE (boolean_type_node);
8646 if (!target || !register_operand (target, mode))
8647 target = gen_reg_rtx (mode);
8648
8649 mode = get_builtin_sync_mode
8650 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8651 target = expand_builtin_compare_and_swap (mode, exp, true, target);
8652 if (target)
8653 return target;
8654 break;
8655
8656 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8657 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8658 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8659 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8660 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8661 mode = get_builtin_sync_mode
8662 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8663 target = expand_builtin_compare_and_swap (mode, exp, false, target);
8664 if (target)
8665 return target;
8666 break;
8667
8668 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8669 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8670 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8671 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8672 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8673 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8674 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8675 if (target)
8676 return target;
8677 break;
8678
8679 case BUILT_IN_SYNC_LOCK_RELEASE_1:
8680 case BUILT_IN_SYNC_LOCK_RELEASE_2:
8681 case BUILT_IN_SYNC_LOCK_RELEASE_4:
8682 case BUILT_IN_SYNC_LOCK_RELEASE_8:
8683 case BUILT_IN_SYNC_LOCK_RELEASE_16:
8684 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8685 if (expand_builtin_sync_lock_release (mode, exp))
8686 return const0_rtx;
8687 break;
8688
8689 case BUILT_IN_SYNC_SYNCHRONIZE:
8690 expand_builtin_sync_synchronize ();
8691 return const0_rtx;
8692
8693 case BUILT_IN_ATOMIC_EXCHANGE_1:
8694 case BUILT_IN_ATOMIC_EXCHANGE_2:
8695 case BUILT_IN_ATOMIC_EXCHANGE_4:
8696 case BUILT_IN_ATOMIC_EXCHANGE_8:
8697 case BUILT_IN_ATOMIC_EXCHANGE_16:
8698 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8699 target = expand_builtin_atomic_exchange (mode, exp, target);
8700 if (target)
8701 return target;
8702 break;
8703
8704 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8705 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8706 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8707 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8708 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8709 {
8710 unsigned int nargs, z;
8711 vec<tree, va_gc> *vec;
8712
8713 mode =
8714 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8715 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8716 if (target)
8717 return target;
8718
8719 /* If this is turned into an external library call, the weak parameter
8720 must be dropped to match the expected parameter list. */
8721 nargs = call_expr_nargs (exp);
8722 vec_alloc (vec, nargs - 1);
8723 for (z = 0; z < 3; z++)
8724 vec->quick_push (CALL_EXPR_ARG (exp, z));
8725 /* Skip the boolean weak parameter. */
8726 for (z = 4; z < 6; z++)
8727 vec->quick_push (CALL_EXPR_ARG (exp, z));
8728 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8729 break;
8730 }
8731
8732 case BUILT_IN_ATOMIC_LOAD_1:
8733 case BUILT_IN_ATOMIC_LOAD_2:
8734 case BUILT_IN_ATOMIC_LOAD_4:
8735 case BUILT_IN_ATOMIC_LOAD_8:
8736 case BUILT_IN_ATOMIC_LOAD_16:
8737 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8738 target = expand_builtin_atomic_load (mode, exp, target);
8739 if (target)
8740 return target;
8741 break;
8742
8743 case BUILT_IN_ATOMIC_STORE_1:
8744 case BUILT_IN_ATOMIC_STORE_2:
8745 case BUILT_IN_ATOMIC_STORE_4:
8746 case BUILT_IN_ATOMIC_STORE_8:
8747 case BUILT_IN_ATOMIC_STORE_16:
8748 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8749 target = expand_builtin_atomic_store (mode, exp);
8750 if (target)
8751 return const0_rtx;
8752 break;
8753
8754 case BUILT_IN_ATOMIC_ADD_FETCH_1:
8755 case BUILT_IN_ATOMIC_ADD_FETCH_2:
8756 case BUILT_IN_ATOMIC_ADD_FETCH_4:
8757 case BUILT_IN_ATOMIC_ADD_FETCH_8:
8758 case BUILT_IN_ATOMIC_ADD_FETCH_16:
8759 {
8760 enum built_in_function lib;
8761 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8762 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8763 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8764 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8765 ignore, lib);
8766 if (target)
8767 return target;
8768 break;
8769 }
8770 case BUILT_IN_ATOMIC_SUB_FETCH_1:
8771 case BUILT_IN_ATOMIC_SUB_FETCH_2:
8772 case BUILT_IN_ATOMIC_SUB_FETCH_4:
8773 case BUILT_IN_ATOMIC_SUB_FETCH_8:
8774 case BUILT_IN_ATOMIC_SUB_FETCH_16:
8775 {
8776 enum built_in_function lib;
8777 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8778 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8779 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8780 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8781 ignore, lib);
8782 if (target)
8783 return target;
8784 break;
8785 }
8786 case BUILT_IN_ATOMIC_AND_FETCH_1:
8787 case BUILT_IN_ATOMIC_AND_FETCH_2:
8788 case BUILT_IN_ATOMIC_AND_FETCH_4:
8789 case BUILT_IN_ATOMIC_AND_FETCH_8:
8790 case BUILT_IN_ATOMIC_AND_FETCH_16:
8791 {
8792 enum built_in_function lib;
8793 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8794 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8795 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8796 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8797 ignore, lib);
8798 if (target)
8799 return target;
8800 break;
8801 }
8802 case BUILT_IN_ATOMIC_NAND_FETCH_1:
8803 case BUILT_IN_ATOMIC_NAND_FETCH_2:
8804 case BUILT_IN_ATOMIC_NAND_FETCH_4:
8805 case BUILT_IN_ATOMIC_NAND_FETCH_8:
8806 case BUILT_IN_ATOMIC_NAND_FETCH_16:
8807 {
8808 enum built_in_function lib;
8809 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8810 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8811 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8812 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8813 ignore, lib);
8814 if (target)
8815 return target;
8816 break;
8817 }
8818 case BUILT_IN_ATOMIC_XOR_FETCH_1:
8819 case BUILT_IN_ATOMIC_XOR_FETCH_2:
8820 case BUILT_IN_ATOMIC_XOR_FETCH_4:
8821 case BUILT_IN_ATOMIC_XOR_FETCH_8:
8822 case BUILT_IN_ATOMIC_XOR_FETCH_16:
8823 {
8824 enum built_in_function lib;
8825 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8826 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8827 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8828 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8829 ignore, lib);
8830 if (target)
8831 return target;
8832 break;
8833 }
8834 case BUILT_IN_ATOMIC_OR_FETCH_1:
8835 case BUILT_IN_ATOMIC_OR_FETCH_2:
8836 case BUILT_IN_ATOMIC_OR_FETCH_4:
8837 case BUILT_IN_ATOMIC_OR_FETCH_8:
8838 case BUILT_IN_ATOMIC_OR_FETCH_16:
8839 {
8840 enum built_in_function lib;
8841 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8842 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8843 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8844 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8845 ignore, lib);
8846 if (target)
8847 return target;
8848 break;
8849 }
8850 case BUILT_IN_ATOMIC_FETCH_ADD_1:
8851 case BUILT_IN_ATOMIC_FETCH_ADD_2:
8852 case BUILT_IN_ATOMIC_FETCH_ADD_4:
8853 case BUILT_IN_ATOMIC_FETCH_ADD_8:
8854 case BUILT_IN_ATOMIC_FETCH_ADD_16:
8855 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8856 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8857 ignore, BUILT_IN_NONE);
8858 if (target)
8859 return target;
8860 break;
8861
8862 case BUILT_IN_ATOMIC_FETCH_SUB_1:
8863 case BUILT_IN_ATOMIC_FETCH_SUB_2:
8864 case BUILT_IN_ATOMIC_FETCH_SUB_4:
8865 case BUILT_IN_ATOMIC_FETCH_SUB_8:
8866 case BUILT_IN_ATOMIC_FETCH_SUB_16:
8867 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8868 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8869 ignore, BUILT_IN_NONE);
8870 if (target)
8871 return target;
8872 break;
8873
8874 case BUILT_IN_ATOMIC_FETCH_AND_1:
8875 case BUILT_IN_ATOMIC_FETCH_AND_2:
8876 case BUILT_IN_ATOMIC_FETCH_AND_4:
8877 case BUILT_IN_ATOMIC_FETCH_AND_8:
8878 case BUILT_IN_ATOMIC_FETCH_AND_16:
8879 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8880 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8881 ignore, BUILT_IN_NONE);
8882 if (target)
8883 return target;
8884 break;
8885
8886 case BUILT_IN_ATOMIC_FETCH_NAND_1:
8887 case BUILT_IN_ATOMIC_FETCH_NAND_2:
8888 case BUILT_IN_ATOMIC_FETCH_NAND_4:
8889 case BUILT_IN_ATOMIC_FETCH_NAND_8:
8890 case BUILT_IN_ATOMIC_FETCH_NAND_16:
8891 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8892 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8893 ignore, BUILT_IN_NONE);
8894 if (target)
8895 return target;
8896 break;
8897
8898 case BUILT_IN_ATOMIC_FETCH_XOR_1:
8899 case BUILT_IN_ATOMIC_FETCH_XOR_2:
8900 case BUILT_IN_ATOMIC_FETCH_XOR_4:
8901 case BUILT_IN_ATOMIC_FETCH_XOR_8:
8902 case BUILT_IN_ATOMIC_FETCH_XOR_16:
8903 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8904 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8905 ignore, BUILT_IN_NONE);
8906 if (target)
8907 return target;
8908 break;
8909
8910 case BUILT_IN_ATOMIC_FETCH_OR_1:
8911 case BUILT_IN_ATOMIC_FETCH_OR_2:
8912 case BUILT_IN_ATOMIC_FETCH_OR_4:
8913 case BUILT_IN_ATOMIC_FETCH_OR_8:
8914 case BUILT_IN_ATOMIC_FETCH_OR_16:
8915 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8916 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8917 ignore, BUILT_IN_NONE);
8918 if (target)
8919 return target;
8920 break;
8921
8922 case BUILT_IN_ATOMIC_TEST_AND_SET:
8923 target = expand_builtin_atomic_test_and_set (exp, target);
8924 if (target)
8925 return target;
8926 break;
8927
8928 case BUILT_IN_ATOMIC_CLEAR:
8929 return expand_builtin_atomic_clear (exp);
8930
8931 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8932 return expand_builtin_atomic_always_lock_free (exp);
8933
8934 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8935 target = expand_builtin_atomic_is_lock_free (exp);
8936 if (target)
8937 return target;
8938 break;
8939
8940 case BUILT_IN_ATOMIC_THREAD_FENCE:
8941 expand_builtin_atomic_thread_fence (exp);
8942 return const0_rtx;
8943
8944 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8945 expand_builtin_atomic_signal_fence (exp);
8946 return const0_rtx;
8947
8948 case BUILT_IN_OBJECT_SIZE:
8949 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8950 return expand_builtin_object_size (exp);
8951
8952 case BUILT_IN_MEMCPY_CHK:
8953 case BUILT_IN_MEMPCPY_CHK:
8954 case BUILT_IN_MEMMOVE_CHK:
8955 case BUILT_IN_MEMSET_CHK:
8956 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8957 if (target)
8958 return target;
8959 break;
8960
8961 case BUILT_IN_STRCPY_CHK:
8962 case BUILT_IN_STPCPY_CHK:
8963 case BUILT_IN_STRNCPY_CHK:
8964 case BUILT_IN_STPNCPY_CHK:
8965 case BUILT_IN_STRCAT_CHK:
8966 case BUILT_IN_STRNCAT_CHK:
8967 case BUILT_IN_SNPRINTF_CHK:
8968 case BUILT_IN_VSNPRINTF_CHK:
8969 maybe_emit_chk_warning (exp, fcode);
8970 break;
8971
8972 case BUILT_IN_SPRINTF_CHK:
8973 case BUILT_IN_VSPRINTF_CHK:
8974 maybe_emit_sprintf_chk_warning (exp, fcode);
8975 break;
8976
8977 case BUILT_IN_THREAD_POINTER:
8978 return expand_builtin_thread_pointer (exp, target);
8979
8980 case BUILT_IN_SET_THREAD_POINTER:
8981 expand_builtin_set_thread_pointer (exp);
8982 return const0_rtx;
8983
8984 case BUILT_IN_ACC_ON_DEVICE:
8985 /* Do library call, if we failed to expand the builtin when
8986 folding. */
8987 break;
8988
8989 case BUILT_IN_GOACC_PARLEVEL_ID:
8990 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8991 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8992
8993 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8994 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8995
8996 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8997 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8998 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8999 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
9000 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
9001 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
9002 return expand_speculation_safe_value (mode, exp, target, ignore);
9003
9004 case BUILT_IN_CRC8_DATA8:
9005 return expand_builtin_crc_table_based (IFN_CRC, QImode, QImode, mode,
9006 exp, target);
9007 case BUILT_IN_CRC16_DATA8:
9008 return expand_builtin_crc_table_based (IFN_CRC, HImode, QImode, mode,
9009 exp, target);
9010 case BUILT_IN_CRC16_DATA16:
9011 return expand_builtin_crc_table_based (IFN_CRC, HImode, HImode, mode,
9012 exp, target);
9013 case BUILT_IN_CRC32_DATA8:
9014 return expand_builtin_crc_table_based (IFN_CRC, SImode, QImode, mode,
9015 exp, target);
9016 case BUILT_IN_CRC32_DATA16:
9017 return expand_builtin_crc_table_based (IFN_CRC, SImode, HImode, mode,
9018 exp, target);
9019 case BUILT_IN_CRC32_DATA32:
9020 return expand_builtin_crc_table_based (IFN_CRC, SImode, SImode, mode,
9021 exp, target);
9022 case BUILT_IN_CRC64_DATA8:
9023 return expand_builtin_crc_table_based (IFN_CRC, DImode, QImode, mode,
9024 exp, target);
9025 case BUILT_IN_CRC64_DATA16:
9026 return expand_builtin_crc_table_based (IFN_CRC, DImode, HImode, mode,
9027 exp, target);
9028 case BUILT_IN_CRC64_DATA32:
9029 return expand_builtin_crc_table_based (IFN_CRC, DImode, SImode, mode,
9030 exp, target);
9031 case BUILT_IN_CRC64_DATA64:
9032 return expand_builtin_crc_table_based (IFN_CRC, DImode, DImode, mode,
9033 exp, target);
9034 case BUILT_IN_REV_CRC8_DATA8:
9035 return expand_builtin_crc_table_based (IFN_CRC_REV, QImode, QImode,
9036 mode, exp, target);
9037 case BUILT_IN_REV_CRC16_DATA8:
9038 return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, QImode,
9039 mode, exp, target);
9040 case BUILT_IN_REV_CRC16_DATA16:
9041 return expand_builtin_crc_table_based (IFN_CRC_REV, HImode, HImode,
9042 mode, exp, target);
9043 case BUILT_IN_REV_CRC32_DATA8:
9044 return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, QImode,
9045 mode, exp, target);
9046 case BUILT_IN_REV_CRC32_DATA16:
9047 return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, HImode,
9048 mode, exp, target);
9049 case BUILT_IN_REV_CRC32_DATA32:
9050 return expand_builtin_crc_table_based (IFN_CRC_REV, SImode, SImode,
9051 mode, exp, target);
9052 case BUILT_IN_REV_CRC64_DATA8:
9053 return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, QImode,
9054 mode, exp, target);
9055 case BUILT_IN_REV_CRC64_DATA16:
9056 return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, HImode,
9057 mode, exp, target);
9058 case BUILT_IN_REV_CRC64_DATA32:
9059 return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, SImode,
9060 mode, exp, target);
9061 case BUILT_IN_REV_CRC64_DATA64:
9062 return expand_builtin_crc_table_based (IFN_CRC_REV, DImode, DImode,
9063 mode, exp, target);
9064 default: /* just do library call, if unknown builtin */
9065 break;
9066 }
9067
9068 /* The switch statement above can drop through to cause the function
9069 to be called normally. */
9070 return expand_call (exp, target, ignore);
9071 }
9072
9073 /* Determine whether a tree node represents a call to a built-in
9074 function. If the tree T is a call to a built-in function with
9075 the right number of arguments of the appropriate types, return
9076 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
9077 Otherwise the return value is END_BUILTINS. */
9078
9079 enum built_in_function
9080 builtin_mathfn_code (const_tree t)
9081 {
9082 const_tree fndecl, arg, parmlist;
9083 const_tree argtype, parmtype;
9084 const_call_expr_arg_iterator iter;
9085
9086 if (TREE_CODE (t) != CALL_EXPR)
9087 return END_BUILTINS;
9088
9089 fndecl = get_callee_fndecl (t);
9090 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
9091 return END_BUILTINS;
9092
9093 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
9094 init_const_call_expr_arg_iterator (t, &iter);
9095 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
9096 {
9097 /* If a function doesn't take a variable number of arguments,
9098 the last element in the list will have type `void'. */
9099 parmtype = TREE_VALUE (parmlist);
9100 if (VOID_TYPE_P (parmtype))
9101 {
9102 if (more_const_call_expr_args_p (&iter))
9103 return END_BUILTINS;
9104 return DECL_FUNCTION_CODE (fndecl);
9105 }
9106
9107 if (! more_const_call_expr_args_p (&iter))
9108 return END_BUILTINS;
9109
9110 arg = next_const_call_expr_arg (&iter);
9111 argtype = TREE_TYPE (arg);
9112
9113 if (SCALAR_FLOAT_TYPE_P (parmtype))
9114 {
9115 if (! SCALAR_FLOAT_TYPE_P (argtype))
9116 return END_BUILTINS;
9117 }
9118 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
9119 {
9120 if (! COMPLEX_FLOAT_TYPE_P (argtype))
9121 return END_BUILTINS;
9122 }
9123 else if (POINTER_TYPE_P (parmtype))
9124 {
9125 if (! POINTER_TYPE_P (argtype))
9126 return END_BUILTINS;
9127 }
9128 else if (INTEGRAL_TYPE_P (parmtype))
9129 {
9130 if (! INTEGRAL_TYPE_P (argtype))
9131 return END_BUILTINS;
9132 }
9133 else
9134 return END_BUILTINS;
9135 }
9136
9137 /* Variable-length argument list. */
9138 return DECL_FUNCTION_CODE (fndecl);
9139 }
9140
9141 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
9142 evaluate to a constant. */
9143
9144 static tree
9145 fold_builtin_constant_p (tree arg)
9146 {
9147 /* We return 1 for a numeric type that's known to be a constant
9148 value at compile-time or for an aggregate type that's a
9149 literal constant. */
9150 STRIP_NOPS (arg);
9151
9152 /* If we know this is a constant, emit the constant of one. */
9153 if (CONSTANT_CLASS_P (arg)
9154 || (TREE_CODE (arg) == CONSTRUCTOR
9155 && TREE_CONSTANT (arg)))
9156 return integer_one_node;
9157 if (TREE_CODE (arg) == ADDR_EXPR)
9158 {
9159 tree op = TREE_OPERAND (arg, 0);
9160 if (TREE_CODE (op) == STRING_CST
9161 || (TREE_CODE (op) == ARRAY_REF
9162 && integer_zerop (TREE_OPERAND (op, 1))
9163 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
9164 return integer_one_node;
9165 }
9166
9167 /* If this expression has side effects, show we don't know it to be a
9168 constant. Likewise if it's a pointer or aggregate type since in
9169 those case we only want literals, since those are only optimized
9170 when generating RTL, not later.
9171 And finally, if we are compiling an initializer, not code, we
9172 need to return a definite result now; there's not going to be any
9173 more optimization done. */
9174 if (TREE_SIDE_EFFECTS (arg)
9175 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
9176 || POINTER_TYPE_P (TREE_TYPE (arg))
9177 || cfun == 0
9178 || folding_initializer
9179 || force_folding_builtin_constant_p)
9180 return integer_zero_node;
9181
9182 return NULL_TREE;
9183 }
9184
9185 /* Create builtin_expect or builtin_expect_with_probability
9186 with PRED and EXPECTED as its arguments and return it as a truthvalue.
9187 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
9188 builtin_expect_with_probability instead uses third argument as PROBABILITY
9189 value. */
9190
9191 static tree
9192 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
9193 tree predictor, tree probability)
9194 {
9195 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
9196
9197 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
9198 : BUILT_IN_EXPECT_WITH_PROBABILITY);
9199 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
9200 ret_type = TREE_TYPE (TREE_TYPE (fn));
9201 pred_type = TREE_VALUE (arg_types);
9202 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
9203
9204 pred = fold_convert_loc (loc, pred_type, pred);
9205 expected = fold_convert_loc (loc, expected_type, expected);
9206
9207 if (probability)
9208 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
9209 else
9210 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
9211 predictor);
9212
9213 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
9214 build_int_cst (ret_type, 0));
9215 }
9216
9217 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
9218 NULL_TREE if no simplification is possible. */
9219
9220 tree
9221 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
9222 tree arg3)
9223 {
9224 tree inner, fndecl, inner_arg0;
9225 enum tree_code code;
9226
9227 /* Distribute the expected value over short-circuiting operators.
9228 See through the cast from truthvalue_type_node to long. */
9229 inner_arg0 = arg0;
9230 while (CONVERT_EXPR_P (inner_arg0)
9231 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
9232 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
9233 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
9234
9235 /* If this is a builtin_expect within a builtin_expect keep the
9236 inner one. See through a comparison against a constant. It
9237 might have been added to create a thruthvalue. */
9238 inner = inner_arg0;
9239
9240 if (COMPARISON_CLASS_P (inner)
9241 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
9242 inner = TREE_OPERAND (inner, 0);
9243
9244 if (TREE_CODE (inner) == CALL_EXPR
9245 && (fndecl = get_callee_fndecl (inner))
9246 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
9247 BUILT_IN_EXPECT_WITH_PROBABILITY))
9248 return arg0;
9249
9250 inner = inner_arg0;
9251 code = TREE_CODE (inner);
9252 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
9253 {
9254 tree op0 = TREE_OPERAND (inner, 0);
9255 tree op1 = TREE_OPERAND (inner, 1);
9256 arg1 = save_expr (arg1);
9257
9258 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
9259 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
9260 inner = build2 (code, TREE_TYPE (inner), op0, op1);
9261
9262 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
9263 }
9264
9265 /* If the argument isn't invariant then there's nothing else we can do. */
9266 if (!TREE_CONSTANT (inner_arg0))
9267 return NULL_TREE;
9268
9269 /* If we expect that a comparison against the argument will fold to
9270 a constant return the constant. In practice, this means a true
9271 constant or the address of a non-weak symbol. */
9272 inner = inner_arg0;
9273 STRIP_NOPS (inner);
9274 if (TREE_CODE (inner) == ADDR_EXPR)
9275 {
9276 do
9277 {
9278 inner = TREE_OPERAND (inner, 0);
9279 }
9280 while (TREE_CODE (inner) == COMPONENT_REF
9281 || TREE_CODE (inner) == ARRAY_REF);
9282 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
9283 return NULL_TREE;
9284 }
9285
9286 /* Otherwise, ARG0 already has the proper type for the return value. */
9287 return arg0;
9288 }
9289
9290 /* Fold a call to __builtin_classify_type with argument ARG. */
9291
9292 static tree
9293 fold_builtin_classify_type (tree arg)
9294 {
9295 if (arg == 0)
9296 return build_int_cst (integer_type_node, no_type_class);
9297
9298 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
9299 }
9300
9301 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
9302 ARG. */
9303
9304 static tree
9305 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
9306 {
9307 if (!validate_arg (arg, POINTER_TYPE))
9308 return NULL_TREE;
9309 else
9310 {
9311 c_strlen_data lendata = { };
9312 tree len = c_strlen (arg, 0, &lendata);
9313
9314 if (len)
9315 return fold_convert_loc (loc, type, len);
9316
9317 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
9318 also early enough to detect invalid reads in multimensional
9319 arrays and struct members. */
9320 if (!lendata.decl)
9321 c_strlen (arg, 1, &lendata);
9322
9323 if (lendata.decl)
9324 {
9325 if (EXPR_HAS_LOCATION (arg))
9326 loc = EXPR_LOCATION (arg);
9327 else if (loc == UNKNOWN_LOCATION)
9328 loc = input_location;
9329 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
9330 }
9331
9332 return NULL_TREE;
9333 }
9334 }
9335
9336 /* Fold a call to __builtin_inf or __builtin_huge_val. */
9337
9338 static tree
9339 fold_builtin_inf (location_t loc, tree type, int warn)
9340 {
9341 /* __builtin_inff is intended to be usable to define INFINITY on all
9342 targets. If an infinity is not available, INFINITY expands "to a
9343 positive constant of type float that overflows at translation
9344 time", footnote "In this case, using INFINITY will violate the
9345 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
9346 Thus we pedwarn to ensure this constraint violation is
9347 diagnosed. */
9348 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
9349 pedwarn (loc, 0, "target format does not support infinity");
9350
9351 return build_real (type, dconstinf);
9352 }
9353
9354 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
9355 NULL_TREE if no simplification can be made. */
9356
9357 static tree
9358 fold_builtin_sincos (location_t loc,
9359 tree arg0, tree arg1, tree arg2)
9360 {
9361 tree type;
9362 tree fndecl, call = NULL_TREE;
9363
9364 if (!validate_arg (arg0, REAL_TYPE)
9365 || !validate_arg (arg1, POINTER_TYPE)
9366 || !validate_arg (arg2, POINTER_TYPE))
9367 return NULL_TREE;
9368
9369 type = TREE_TYPE (arg0);
9370
9371 /* Calculate the result when the argument is a constant. */
9372 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
9373 if (fn == END_BUILTINS)
9374 return NULL_TREE;
9375
9376 /* Canonicalize sincos to cexpi. */
9377 if (TREE_CODE (arg0) == REAL_CST)
9378 {
9379 tree complex_type = build_complex_type (type);
9380 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
9381 }
9382 if (!call)
9383 {
9384 if (!targetm.libc_has_function (function_c99_math_complex, type)
9385 || !builtin_decl_implicit_p (fn))
9386 return NULL_TREE;
9387 fndecl = builtin_decl_explicit (fn);
9388 call = build_call_expr_loc (loc, fndecl, 1, arg0);
9389 call = builtin_save_expr (call);
9390 }
9391
9392 tree ptype = build_pointer_type (type);
9393 arg1 = fold_convert (ptype, arg1);
9394 arg2 = fold_convert (ptype, arg2);
9395 return build2 (COMPOUND_EXPR, void_type_node,
9396 build2 (MODIFY_EXPR, void_type_node,
9397 build_fold_indirect_ref_loc (loc, arg1),
9398 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
9399 build2 (MODIFY_EXPR, void_type_node,
9400 build_fold_indirect_ref_loc (loc, arg2),
9401 fold_build1_loc (loc, REALPART_EXPR, type, call)));
9402 }
9403
9404 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9405 Return NULL_TREE if no simplification can be made. */
9406
9407 static tree
9408 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9409 {
9410 if (!validate_arg (arg1, POINTER_TYPE)
9411 || !validate_arg (arg2, POINTER_TYPE)
9412 || !validate_arg (len, INTEGER_TYPE))
9413 return NULL_TREE;
9414
9415 /* If the LEN parameter is zero, return zero. */
9416 if (integer_zerop (len))
9417 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9418 arg1, arg2);
9419
9420 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9421 if (operand_equal_p (arg1, arg2, 0))
9422 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9423
9424 /* If len parameter is one, return an expression corresponding to
9425 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9426 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9427 {
9428 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9429 tree cst_uchar_ptr_node
9430 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9431
9432 tree ind1
9433 = fold_convert_loc (loc, integer_type_node,
9434 build1 (INDIRECT_REF, cst_uchar_node,
9435 fold_convert_loc (loc,
9436 cst_uchar_ptr_node,
9437 arg1)));
9438 tree ind2
9439 = fold_convert_loc (loc, integer_type_node,
9440 build1 (INDIRECT_REF, cst_uchar_node,
9441 fold_convert_loc (loc,
9442 cst_uchar_ptr_node,
9443 arg2)));
9444 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9445 }
9446
9447 return NULL_TREE;
9448 }
9449
9450 /* Fold a call to builtin isascii with argument ARG. */
9451
9452 static tree
9453 fold_builtin_isascii (location_t loc, tree arg)
9454 {
9455 if (!validate_arg (arg, INTEGER_TYPE))
9456 return NULL_TREE;
9457 else
9458 {
9459 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9460 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9461 build_int_cst (integer_type_node,
9462 ~ HOST_WIDE_INT_UC (0x7f)));
9463 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9464 arg, integer_zero_node);
9465 }
9466 }
9467
9468 /* Fold a call to builtin toascii with argument ARG. */
9469
9470 static tree
9471 fold_builtin_toascii (location_t loc, tree arg)
9472 {
9473 if (!validate_arg (arg, INTEGER_TYPE))
9474 return NULL_TREE;
9475
9476 /* Transform toascii(c) -> (c & 0x7f). */
9477 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9478 build_int_cst (integer_type_node, 0x7f));
9479 }
9480
9481 /* Fold a call to builtin isdigit with argument ARG. */
9482
9483 static tree
9484 fold_builtin_isdigit (location_t loc, tree arg)
9485 {
9486 if (!validate_arg (arg, INTEGER_TYPE))
9487 return NULL_TREE;
9488 else
9489 {
9490 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9491 /* According to the C standard, isdigit is unaffected by locale.
9492 However, it definitely is affected by the target character set. */
9493 unsigned HOST_WIDE_INT target_digit0
9494 = lang_hooks.to_target_charset ('0');
9495
9496 if (target_digit0 == 0)
9497 return NULL_TREE;
9498
9499 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9500 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9501 build_int_cst (unsigned_type_node, target_digit0));
9502 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9503 build_int_cst (unsigned_type_node, 9));
9504 }
9505 }
9506
9507 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9508
9509 static tree
9510 fold_builtin_fabs (location_t loc, tree arg, tree type)
9511 {
9512 if (!validate_arg (arg, REAL_TYPE))
9513 return NULL_TREE;
9514
9515 arg = fold_convert_loc (loc, type, arg);
9516 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9517 }
9518
9519 /* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
9520 with argument ARG. */
9521
9522 static tree
9523 fold_builtin_abs (location_t loc, tree arg, tree type)
9524 {
9525 if (!validate_arg (arg, INTEGER_TYPE) || !INTEGRAL_TYPE_P (type))
9526 return NULL_TREE;
9527
9528 if (TYPE_UNSIGNED (type))
9529 {
9530 if (TYPE_PRECISION (TREE_TYPE (arg))
9531 != TYPE_PRECISION (type)
9532 || TYPE_UNSIGNED (TREE_TYPE (arg)))
9533 return NULL_TREE;
9534 return fold_build1_loc (loc, ABSU_EXPR, type, arg);
9535 }
9536 arg = fold_convert_loc (loc, type, arg);
9537 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9538 }
9539
9540 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9541
9542 static tree
9543 fold_builtin_carg (location_t loc, tree arg, tree type)
9544 {
9545 if (validate_arg (arg, COMPLEX_TYPE)
9546 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
9547 {
9548 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9549
9550 if (atan2_fn)
9551 {
9552 tree new_arg = builtin_save_expr (arg);
9553 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9554 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9555 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9556 }
9557 }
9558
9559 return NULL_TREE;
9560 }
9561
9562 /* Fold a call to builtin frexp, we can assume the base is 2. */
9563
9564 static tree
9565 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9566 {
9567 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9568 return NULL_TREE;
9569
9570 STRIP_NOPS (arg0);
9571
9572 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9573 return NULL_TREE;
9574
9575 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9576
9577 /* Proceed if a valid pointer type was passed in. */
9578 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9579 {
9580 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9581 tree frac, exp, res;
9582
9583 switch (value->cl)
9584 {
9585 case rvc_zero:
9586 case rvc_nan:
9587 case rvc_inf:
9588 /* For +-0, return (*exp = 0, +-0). */
9589 /* For +-NaN or +-Inf, *exp is unspecified, but something should
9590 be stored there so that it isn't read from uninitialized object.
9591 As glibc and newlib store *exp = 0 for +-Inf/NaN, storing
9592 0 here as well is easiest. */
9593 exp = integer_zero_node;
9594 frac = arg0;
9595 break;
9596 case rvc_normal:
9597 {
9598 /* Since the frexp function always expects base 2, and in
9599 GCC normalized significands are already in the range
9600 [0.5, 1.0), we have exactly what frexp wants. */
9601 REAL_VALUE_TYPE frac_rvt = *value;
9602 SET_REAL_EXP (&frac_rvt, 0);
9603 frac = build_real (rettype, frac_rvt);
9604 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9605 }
9606 break;
9607 default:
9608 gcc_unreachable ();
9609 }
9610
9611 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9612 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9613 TREE_SIDE_EFFECTS (arg1) = 1;
9614 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9615 suppress_warning (res, OPT_Wunused_value);
9616 return res;
9617 }
9618
9619 return NULL_TREE;
9620 }
9621
9622 /* Fold a call to builtin modf. */
9623
9624 static tree
9625 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9626 {
9627 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9628 return NULL_TREE;
9629
9630 STRIP_NOPS (arg0);
9631
9632 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9633 return NULL_TREE;
9634
9635 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9636
9637 /* Proceed if a valid pointer type was passed in. */
9638 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9639 {
9640 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9641 REAL_VALUE_TYPE trunc, frac;
9642 tree res;
9643
9644 switch (value->cl)
9645 {
9646 case rvc_nan:
9647 case rvc_zero:
9648 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9649 trunc = frac = *value;
9650 break;
9651 case rvc_inf:
9652 /* For +-Inf, return (*arg1 = arg0, +-0). */
9653 frac = dconst0;
9654 frac.sign = value->sign;
9655 trunc = *value;
9656 break;
9657 case rvc_normal:
9658 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9659 real_trunc (&trunc, VOIDmode, value);
9660 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9661 /* If the original number was negative and already
9662 integral, then the fractional part is -0.0. */
9663 if (value->sign && frac.cl == rvc_zero)
9664 frac.sign = value->sign;
9665 break;
9666 }
9667
9668 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9669 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9670 build_real (rettype, trunc));
9671 TREE_SIDE_EFFECTS (arg1) = 1;
9672 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9673 build_real (rettype, frac));
9674 suppress_warning (res, OPT_Wunused_value);
9675 return res;
9676 }
9677
9678 return NULL_TREE;
9679 }
9680
9681 /* Given a location LOC, an interclass builtin function decl FNDECL
9682 and its single argument ARG, return an folded expression computing
9683 the same, or NULL_TREE if we either couldn't or didn't want to fold
9684 (the latter happen if there's an RTL instruction available). */
9685
9686 static tree
9687 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9688 {
9689 machine_mode mode;
9690
9691 if (!validate_arg (arg, REAL_TYPE))
9692 return NULL_TREE;
9693
9694 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9695 return NULL_TREE;
9696
9697 mode = TYPE_MODE (TREE_TYPE (arg));
9698
9699 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9700
9701 /* If there is no optab, try generic code. */
9702 switch (DECL_FUNCTION_CODE (fndecl))
9703 {
9704 tree result;
9705
9706 CASE_FLT_FN (BUILT_IN_ISINF):
9707 {
9708 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9709 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9710 tree type = TREE_TYPE (arg);
9711 REAL_VALUE_TYPE r;
9712 char buf[128];
9713
9714 if (is_ibm_extended)
9715 {
9716 /* NaN and Inf are encoded in the high-order double value
9717 only. The low-order value is not significant. */
9718 type = double_type_node;
9719 mode = DFmode;
9720 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9721 }
9722 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9723 real_from_string3 (&r, buf, mode);
9724 result = build_call_expr (isgr_fn, 2,
9725 fold_build1_loc (loc, ABS_EXPR, type, arg),
9726 build_real (type, r));
9727 return result;
9728 }
9729 CASE_FLT_FN (BUILT_IN_FINITE):
9730 case BUILT_IN_ISFINITE:
9731 {
9732 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9733 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9734 tree type = TREE_TYPE (arg);
9735 REAL_VALUE_TYPE r;
9736 char buf[128];
9737
9738 if (is_ibm_extended)
9739 {
9740 /* NaN and Inf are encoded in the high-order double value
9741 only. The low-order value is not significant. */
9742 type = double_type_node;
9743 mode = DFmode;
9744 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9745 }
9746 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9747 real_from_string3 (&r, buf, mode);
9748 result = build_call_expr (isle_fn, 2,
9749 fold_build1_loc (loc, ABS_EXPR, type, arg),
9750 build_real (type, r));
9751 /*result = fold_build2_loc (loc, UNGT_EXPR,
9752 TREE_TYPE (TREE_TYPE (fndecl)),
9753 fold_build1_loc (loc, ABS_EXPR, type, arg),
9754 build_real (type, r));
9755 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9756 TREE_TYPE (TREE_TYPE (fndecl)),
9757 result);*/
9758 return result;
9759 }
9760 case BUILT_IN_ISNORMAL:
9761 {
9762 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9763 islessequal(fabs(x),DBL_MAX). */
9764 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9765 tree type = TREE_TYPE (arg);
9766 tree orig_arg, max_exp, min_exp;
9767 machine_mode orig_mode = mode;
9768 REAL_VALUE_TYPE rmax, rmin;
9769 char buf[128];
9770
9771 orig_arg = arg = builtin_save_expr (arg);
9772 if (is_ibm_extended)
9773 {
9774 /* Use double to test the normal range of IBM extended
9775 precision. Emin for IBM extended precision is
9776 different to emin for IEEE double, being 53 higher
9777 since the low double exponent is at least 53 lower
9778 than the high double exponent. */
9779 type = double_type_node;
9780 mode = DFmode;
9781 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9782 }
9783 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9784
9785 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9786 real_from_string3 (&rmax, buf, mode);
9787 if (DECIMAL_FLOAT_MODE_P (mode))
9788 sprintf (buf, "1E%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9789 else
9790 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9791 real_from_string3 (&rmin, buf, orig_mode);
9792 max_exp = build_real (type, rmax);
9793 min_exp = build_real (type, rmin);
9794
9795 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9796 if (is_ibm_extended)
9797 {
9798 /* Testing the high end of the range is done just using
9799 the high double, using the same test as isfinite().
9800 For the subnormal end of the range we first test the
9801 high double, then if its magnitude is equal to the
9802 limit of 0x1p-969, we test whether the low double is
9803 non-zero and opposite sign to the high double. */
9804 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9805 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9806 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9807 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9808 arg, min_exp);
9809 tree as_complex = build1 (VIEW_CONVERT_EXPR,
9810 complex_double_type_node, orig_arg);
9811 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9812 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9813 tree zero = build_real (type, dconst0);
9814 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9815 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9816 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9817 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9818 fold_build3 (COND_EXPR,
9819 integer_type_node,
9820 hilt, logt, lolt));
9821 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9822 eq_min, ok_lo);
9823 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9824 gt_min, eq_min);
9825 }
9826 else
9827 {
9828 tree const isge_fn
9829 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9830 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9831 }
9832 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9833 max_exp, min_exp);
9834 return result;
9835 }
9836 default:
9837 break;
9838 }
9839
9840 return NULL_TREE;
9841 }
9842
9843 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9844 ARG is the argument for the call. */
9845
9846 static tree
9847 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9848 {
9849 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9850
9851 if (!validate_arg (arg, REAL_TYPE))
9852 return NULL_TREE;
9853
9854 switch (builtin_index)
9855 {
9856 case BUILT_IN_ISINF:
9857 if (tree_expr_infinite_p (arg))
9858 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9859 if (!tree_expr_maybe_infinite_p (arg))
9860 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9861 return NULL_TREE;
9862
9863 case BUILT_IN_ISINF_SIGN:
9864 {
9865 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9866 /* In a boolean context, GCC will fold the inner COND_EXPR to
9867 1. So e.g. "if (isinf_sign(x))" would be folded to just
9868 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9869 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9870 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9871 tree tmp = NULL_TREE;
9872
9873 arg = builtin_save_expr (arg);
9874
9875 if (signbit_fn && isinf_fn)
9876 {
9877 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9878 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9879
9880 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9881 signbit_call, integer_zero_node);
9882 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9883 isinf_call, integer_zero_node);
9884
9885 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9886 integer_minus_one_node, integer_one_node);
9887 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9888 isinf_call, tmp,
9889 integer_zero_node);
9890 }
9891
9892 return tmp;
9893 }
9894
9895 case BUILT_IN_ISFINITE:
9896 if (tree_expr_finite_p (arg))
9897 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9898 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9899 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9900 return NULL_TREE;
9901
9902 case BUILT_IN_ISNAN:
9903 if (tree_expr_nan_p (arg))
9904 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9905 if (!tree_expr_maybe_nan_p (arg))
9906 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9907
9908 {
9909 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9910 if (is_ibm_extended)
9911 {
9912 /* NaN and Inf are encoded in the high-order double value
9913 only. The low-order value is not significant. */
9914 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9915 }
9916 }
9917 arg = builtin_save_expr (arg);
9918 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9919
9920 case BUILT_IN_ISSIGNALING:
9921 /* Folding to true for REAL_CST is done in fold_const_call_ss.
9922 Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9923 and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9924 here, so there is some possibility of __builtin_issignaling working
9925 without -fsignaling-nans. Especially when -fno-signaling-nans is
9926 the default. */
9927 if (!tree_expr_maybe_nan_p (arg))
9928 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9929 return NULL_TREE;
9930
9931 default:
9932 gcc_unreachable ();
9933 }
9934 }
9935
9936 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9937 This builtin will generate code to return the appropriate floating
9938 point classification depending on the value of the floating point
9939 number passed in. The possible return values must be supplied as
9940 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9941 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9942 one floating point argument which is "type generic". */
9943
9944 static tree
9945 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9946 {
9947 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9948 arg, type, res, tmp;
9949 machine_mode mode;
9950 REAL_VALUE_TYPE r;
9951 char buf[128];
9952
9953 /* Verify the required arguments in the original call. */
9954 if (nargs != 6
9955 || !validate_arg (args[0], INTEGER_TYPE)
9956 || !validate_arg (args[1], INTEGER_TYPE)
9957 || !validate_arg (args[2], INTEGER_TYPE)
9958 || !validate_arg (args[3], INTEGER_TYPE)
9959 || !validate_arg (args[4], INTEGER_TYPE)
9960 || !validate_arg (args[5], REAL_TYPE))
9961 return NULL_TREE;
9962
9963 fp_nan = args[0];
9964 fp_infinite = args[1];
9965 fp_normal = args[2];
9966 fp_subnormal = args[3];
9967 fp_zero = args[4];
9968 arg = args[5];
9969 type = TREE_TYPE (arg);
9970 mode = TYPE_MODE (type);
9971 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9972
9973 /* fpclassify(x) ->
9974 isnan(x) ? FP_NAN :
9975 (fabs(x) == Inf ? FP_INFINITE :
9976 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9977 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9978
9979 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9980 build_real (type, dconst0));
9981 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9982 tmp, fp_zero, fp_subnormal);
9983
9984 if (DECIMAL_FLOAT_MODE_P (mode))
9985 sprintf (buf, "1E%d", REAL_MODE_FORMAT (mode)->emin - 1);
9986 else
9987 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9988 real_from_string3 (&r, buf, mode);
9989 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9990 arg, build_real (type, r));
9991 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9992 fp_normal, res);
9993
9994 if (tree_expr_maybe_infinite_p (arg))
9995 {
9996 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9997 build_real (type, dconstinf));
9998 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9999 fp_infinite, res);
10000 }
10001
10002 if (tree_expr_maybe_nan_p (arg))
10003 {
10004 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10005 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10006 res, fp_nan);
10007 }
10008
10009 return res;
10010 }
10011
10012 /* Fold a call to an unordered comparison function such as
10013 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10014 being called and ARG0 and ARG1 are the arguments for the call.
10015 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10016 the opposite of the desired result. UNORDERED_CODE is used
10017 for modes that can hold NaNs and ORDERED_CODE is used for
10018 the rest. */
10019
10020 static tree
10021 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10022 enum tree_code unordered_code,
10023 enum tree_code ordered_code)
10024 {
10025 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10026 enum tree_code code;
10027 tree type0, type1;
10028 enum tree_code code0, code1;
10029 tree cmp_type = NULL_TREE;
10030
10031 type0 = TREE_TYPE (arg0);
10032 type1 = TREE_TYPE (arg1);
10033
10034 code0 = TREE_CODE (type0);
10035 code1 = TREE_CODE (type1);
10036
10037 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10038 /* Choose the wider of two real types. */
10039 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10040 ? type0 : type1;
10041 else if (code0 == REAL_TYPE
10042 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10043 cmp_type = type0;
10044 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10045 && code1 == REAL_TYPE)
10046 cmp_type = type1;
10047
10048 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10049 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10050
10051 if (unordered_code == UNORDERED_EXPR)
10052 {
10053 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
10054 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
10055 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
10056 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10057 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10058 }
10059
10060 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
10061 ? unordered_code : ordered_code;
10062 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10063 fold_build2_loc (loc, code, type, arg0, arg1));
10064 }
10065
10066 /* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments.
10067 After choosing the wider floating-point type for the comparison,
10068 the code is folded to:
10069 SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1> */
10070
10071 static tree
10072 fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
10073 {
10074 tree type0, type1;
10075 enum tree_code code0, code1;
10076 tree cmp1, cmp2, cmp_type = NULL_TREE;
10077
10078 type0 = TREE_TYPE (arg0);
10079 type1 = TREE_TYPE (arg1);
10080
10081 code0 = TREE_CODE (type0);
10082 code1 = TREE_CODE (type1);
10083
10084 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10085 /* Choose the wider of two real types. */
10086 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10087 ? type0 : type1;
10088 else if (code0 == REAL_TYPE
10089 && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
10090 cmp_type = type0;
10091 else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
10092 && code1 == REAL_TYPE)
10093 cmp_type = type1;
10094
10095 arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
10096 arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
10097
10098 cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
10099 cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
10100
10101 return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
10102 }
10103
10104 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
10105 arithmetics if it can never overflow, or into internal functions that
10106 return both result of arithmetics and overflowed boolean flag in
10107 a complex integer result, or some other check for overflow.
10108 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
10109 checking part of that. */
10110
10111 static tree
10112 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
10113 tree arg0, tree arg1, tree arg2)
10114 {
10115 enum internal_fn ifn = IFN_LAST;
10116 /* The code of the expression corresponding to the built-in. */
10117 enum tree_code opcode = ERROR_MARK;
10118 bool ovf_only = false;
10119
10120 switch (fcode)
10121 {
10122 case BUILT_IN_ADD_OVERFLOW_P:
10123 ovf_only = true;
10124 /* FALLTHRU */
10125 case BUILT_IN_ADD_OVERFLOW:
10126 case BUILT_IN_SADD_OVERFLOW:
10127 case BUILT_IN_SADDL_OVERFLOW:
10128 case BUILT_IN_SADDLL_OVERFLOW:
10129 case BUILT_IN_UADD_OVERFLOW:
10130 case BUILT_IN_UADDL_OVERFLOW:
10131 case BUILT_IN_UADDLL_OVERFLOW:
10132 opcode = PLUS_EXPR;
10133 ifn = IFN_ADD_OVERFLOW;
10134 break;
10135 case BUILT_IN_SUB_OVERFLOW_P:
10136 ovf_only = true;
10137 /* FALLTHRU */
10138 case BUILT_IN_SUB_OVERFLOW:
10139 case BUILT_IN_SSUB_OVERFLOW:
10140 case BUILT_IN_SSUBL_OVERFLOW:
10141 case BUILT_IN_SSUBLL_OVERFLOW:
10142 case BUILT_IN_USUB_OVERFLOW:
10143 case BUILT_IN_USUBL_OVERFLOW:
10144 case BUILT_IN_USUBLL_OVERFLOW:
10145 opcode = MINUS_EXPR;
10146 ifn = IFN_SUB_OVERFLOW;
10147 break;
10148 case BUILT_IN_MUL_OVERFLOW_P:
10149 ovf_only = true;
10150 /* FALLTHRU */
10151 case BUILT_IN_MUL_OVERFLOW:
10152 case BUILT_IN_SMUL_OVERFLOW:
10153 case BUILT_IN_SMULL_OVERFLOW:
10154 case BUILT_IN_SMULLL_OVERFLOW:
10155 case BUILT_IN_UMUL_OVERFLOW:
10156 case BUILT_IN_UMULL_OVERFLOW:
10157 case BUILT_IN_UMULLL_OVERFLOW:
10158 opcode = MULT_EXPR;
10159 ifn = IFN_MUL_OVERFLOW;
10160 break;
10161 default:
10162 gcc_unreachable ();
10163 }
10164
10165 /* For the "generic" overloads, the first two arguments can have different
10166 types and the last argument determines the target type to use to check
10167 for overflow. The arguments of the other overloads all have the same
10168 type. */
10169 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
10170
10171 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
10172 arguments are constant, attempt to fold the built-in call into a constant
10173 expression indicating whether or not it detected an overflow. */
10174 if (ovf_only
10175 && TREE_CODE (arg0) == INTEGER_CST
10176 && TREE_CODE (arg1) == INTEGER_CST)
10177 /* Perform the computation in the target type and check for overflow. */
10178 return omit_one_operand_loc (loc, boolean_type_node,
10179 arith_overflowed_p (opcode, type, arg0, arg1)
10180 ? boolean_true_node : boolean_false_node,
10181 arg2);
10182
10183 tree intres, ovfres;
10184 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10185 {
10186 intres = fold_binary_loc (loc, opcode, type,
10187 fold_convert_loc (loc, type, arg0),
10188 fold_convert_loc (loc, type, arg1));
10189 if (TREE_OVERFLOW (intres))
10190 intres = drop_tree_overflow (intres);
10191 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
10192 ? boolean_true_node : boolean_false_node);
10193 }
10194 else
10195 {
10196 tree ctype = build_complex_type (type);
10197 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10198 arg0, arg1);
10199 tree tgt;
10200 if (ovf_only)
10201 {
10202 tgt = call;
10203 intres = NULL_TREE;
10204 }
10205 else
10206 {
10207 /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10208 as while the call itself is const, the REALPART_EXPR store is
10209 certainly not. And in any case, we want just one call,
10210 not multiple and trying to CSE them later. */
10211 TREE_SIDE_EFFECTS (call) = 1;
10212 tgt = save_expr (call);
10213 }
10214 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10215 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10216 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
10217 }
10218
10219 if (ovf_only)
10220 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
10221
10222 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
10223 tree store
10224 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
10225 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
10226 }
10227
10228 /* Fold __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g into corresponding
10229 internal function. */
10230
10231 static tree
10232 fold_builtin_bit_query (location_t loc, enum built_in_function fcode,
10233 tree arg0, tree arg1)
10234 {
10235 enum internal_fn ifn;
10236 enum built_in_function fcodei, fcodel, fcodell;
10237 tree arg0_type = TREE_TYPE (arg0);
10238 tree cast_type = NULL_TREE;
10239 int addend = 0;
10240
10241 switch (fcode)
10242 {
10243 case BUILT_IN_CLZG:
10244 if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10245 return NULL_TREE;
10246 ifn = IFN_CLZ;
10247 fcodei = BUILT_IN_CLZ;
10248 fcodel = BUILT_IN_CLZL;
10249 fcodell = BUILT_IN_CLZLL;
10250 break;
10251 case BUILT_IN_CTZG:
10252 if (arg1 && TREE_CODE (arg1) != INTEGER_CST)
10253 return NULL_TREE;
10254 ifn = IFN_CTZ;
10255 fcodei = BUILT_IN_CTZ;
10256 fcodel = BUILT_IN_CTZL;
10257 fcodell = BUILT_IN_CTZLL;
10258 break;
10259 case BUILT_IN_CLRSBG:
10260 ifn = IFN_CLRSB;
10261 fcodei = BUILT_IN_CLRSB;
10262 fcodel = BUILT_IN_CLRSBL;
10263 fcodell = BUILT_IN_CLRSBLL;
10264 break;
10265 case BUILT_IN_FFSG:
10266 ifn = IFN_FFS;
10267 fcodei = BUILT_IN_FFS;
10268 fcodel = BUILT_IN_FFSL;
10269 fcodell = BUILT_IN_FFSLL;
10270 break;
10271 case BUILT_IN_PARITYG:
10272 ifn = IFN_PARITY;
10273 fcodei = BUILT_IN_PARITY;
10274 fcodel = BUILT_IN_PARITYL;
10275 fcodell = BUILT_IN_PARITYLL;
10276 break;
10277 case BUILT_IN_POPCOUNTG:
10278 ifn = IFN_POPCOUNT;
10279 fcodei = BUILT_IN_POPCOUNT;
10280 fcodel = BUILT_IN_POPCOUNTL;
10281 fcodell = BUILT_IN_POPCOUNTLL;
10282 break;
10283 default:
10284 gcc_unreachable ();
10285 }
10286
10287 if (TYPE_PRECISION (arg0_type)
10288 <= TYPE_PRECISION (long_long_unsigned_type_node))
10289 {
10290 if (TYPE_PRECISION (arg0_type) <= TYPE_PRECISION (unsigned_type_node))
10291
10292 cast_type = (TYPE_UNSIGNED (arg0_type)
10293 ? unsigned_type_node : integer_type_node);
10294 else if (TYPE_PRECISION (arg0_type)
10295 <= TYPE_PRECISION (long_unsigned_type_node))
10296 {
10297 cast_type = (TYPE_UNSIGNED (arg0_type)
10298 ? long_unsigned_type_node : long_integer_type_node);
10299 fcodei = fcodel;
10300 }
10301 else
10302 {
10303 cast_type = (TYPE_UNSIGNED (arg0_type)
10304 ? long_long_unsigned_type_node
10305 : long_long_integer_type_node);
10306 fcodei = fcodell;
10307 }
10308 }
10309 else if (TYPE_PRECISION (arg0_type) <= MAX_FIXED_MODE_SIZE)
10310 {
10311 cast_type
10312 = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
10313 TYPE_UNSIGNED (arg0_type));
10314 gcc_assert (TYPE_PRECISION (cast_type)
10315 == 2 * TYPE_PRECISION (long_long_unsigned_type_node));
10316 fcodei = END_BUILTINS;
10317 }
10318 else
10319 fcodei = END_BUILTINS;
10320 if (cast_type)
10321 {
10322 switch (fcode)
10323 {
10324 case BUILT_IN_CLZG:
10325 case BUILT_IN_CLRSBG:
10326 addend = TYPE_PRECISION (arg0_type) - TYPE_PRECISION (cast_type);
10327 break;
10328 default:
10329 break;
10330 }
10331 arg0 = fold_convert (cast_type, arg0);
10332 arg0_type = cast_type;
10333 }
10334
10335 if (arg1)
10336 arg1 = fold_convert (integer_type_node, arg1);
10337
10338 tree arg2 = arg1;
10339 if (fcode == BUILT_IN_CLZG && addend)
10340 {
10341 if (arg1)
10342 arg0 = save_expr (arg0);
10343 arg2 = NULL_TREE;
10344 }
10345 tree call = NULL_TREE, tem;
10346 if (TYPE_PRECISION (arg0_type) == MAX_FIXED_MODE_SIZE
10347 && (TYPE_PRECISION (arg0_type)
10348 == 2 * TYPE_PRECISION (long_long_unsigned_type_node))
10349 /* If the target supports the optab, then don't do the expansion. */
10350 && !direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH))
10351 {
10352 /* __int128 expansions using up to 2 long long builtins. */
10353 arg0 = save_expr (arg0);
10354 tree type = (TYPE_UNSIGNED (arg0_type)
10355 ? long_long_unsigned_type_node
10356 : long_long_integer_type_node);
10357 tree hi = fold_build2 (RSHIFT_EXPR, arg0_type, arg0,
10358 build_int_cst (integer_type_node,
10359 MAX_FIXED_MODE_SIZE / 2));
10360 hi = fold_convert (type, hi);
10361 tree lo = fold_convert (type, arg0);
10362 switch (fcode)
10363 {
10364 case BUILT_IN_CLZG:
10365 call = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10366 call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10367 build_int_cst (integer_type_node,
10368 MAX_FIXED_MODE_SIZE / 2));
10369 if (arg2)
10370 call = fold_build3 (COND_EXPR, integer_type_node,
10371 fold_build2 (NE_EXPR, boolean_type_node,
10372 lo, build_zero_cst (type)),
10373 call, arg2);
10374 call = fold_build3 (COND_EXPR, integer_type_node,
10375 fold_build2 (NE_EXPR, boolean_type_node,
10376 hi, build_zero_cst (type)),
10377 fold_builtin_bit_query (loc, fcode, hi,
10378 NULL_TREE),
10379 call);
10380 break;
10381 case BUILT_IN_CTZG:
10382 call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10383 call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10384 build_int_cst (integer_type_node,
10385 MAX_FIXED_MODE_SIZE / 2));
10386 if (arg2)
10387 call = fold_build3 (COND_EXPR, integer_type_node,
10388 fold_build2 (NE_EXPR, boolean_type_node,
10389 hi, build_zero_cst (type)),
10390 call, arg2);
10391 call = fold_build3 (COND_EXPR, integer_type_node,
10392 fold_build2 (NE_EXPR, boolean_type_node,
10393 lo, build_zero_cst (type)),
10394 fold_builtin_bit_query (loc, fcode, lo,
10395 NULL_TREE),
10396 call);
10397 break;
10398 case BUILT_IN_CLRSBG:
10399 tem = fold_builtin_bit_query (loc, fcode, lo, NULL_TREE);
10400 tem = fold_build2 (PLUS_EXPR, integer_type_node, tem,
10401 build_int_cst (integer_type_node,
10402 MAX_FIXED_MODE_SIZE / 2));
10403 tem = fold_build3 (COND_EXPR, integer_type_node,
10404 fold_build2 (LT_EXPR, boolean_type_node,
10405 fold_build2 (BIT_XOR_EXPR, type,
10406 lo, hi),
10407 build_zero_cst (type)),
10408 build_int_cst (integer_type_node,
10409 MAX_FIXED_MODE_SIZE / 2 - 1),
10410 tem);
10411 call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10412 call = save_expr (call);
10413 call = fold_build3 (COND_EXPR, integer_type_node,
10414 fold_build2 (NE_EXPR, boolean_type_node,
10415 call,
10416 build_int_cst (integer_type_node,
10417 MAX_FIXED_MODE_SIZE
10418 / 2 - 1)),
10419 call, tem);
10420 break;
10421 case BUILT_IN_FFSG:
10422 call = fold_builtin_bit_query (loc, fcode, hi, NULL_TREE);
10423 call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10424 build_int_cst (integer_type_node,
10425 MAX_FIXED_MODE_SIZE / 2));
10426 call = fold_build3 (COND_EXPR, integer_type_node,
10427 fold_build2 (NE_EXPR, boolean_type_node,
10428 hi, build_zero_cst (type)),
10429 call, integer_zero_node);
10430 call = fold_build3 (COND_EXPR, integer_type_node,
10431 fold_build2 (NE_EXPR, boolean_type_node,
10432 lo, build_zero_cst (type)),
10433 fold_builtin_bit_query (loc, fcode, lo,
10434 NULL_TREE),
10435 call);
10436 break;
10437 case BUILT_IN_PARITYG:
10438 call = fold_builtin_bit_query (loc, fcode,
10439 fold_build2 (BIT_XOR_EXPR, type,
10440 lo, hi), NULL_TREE);
10441 break;
10442 case BUILT_IN_POPCOUNTG:
10443 call = fold_build2 (PLUS_EXPR, integer_type_node,
10444 fold_builtin_bit_query (loc, fcode, hi,
10445 NULL_TREE),
10446 fold_builtin_bit_query (loc, fcode, lo,
10447 NULL_TREE));
10448 break;
10449 default:
10450 gcc_unreachable ();
10451 }
10452 }
10453 else
10454 {
10455 /* Only keep second argument to IFN_CLZ/IFN_CTZ if it is the
10456 value defined at zero during GIMPLE, or for large/huge _BitInt
10457 (which are then lowered during bitint lowering). */
10458 if (arg2 && TREE_CODE (TREE_TYPE (arg0)) != BITINT_TYPE)
10459 {
10460 int val;
10461 if (fcode == BUILT_IN_CLZG)
10462 {
10463 if (CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10464 val) != 2
10465 || wi::to_widest (arg2) != val)
10466 arg2 = NULL_TREE;
10467 }
10468 else if (CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (arg0_type),
10469 val) != 2
10470 || wi::to_widest (arg2) != val)
10471 arg2 = NULL_TREE;
10472 if (!direct_internal_fn_supported_p (ifn, arg0_type,
10473 OPTIMIZE_FOR_BOTH))
10474 arg2 = NULL_TREE;
10475 if (arg2 == NULL_TREE)
10476 arg0 = save_expr (arg0);
10477 }
10478 if (fcodei == END_BUILTINS || arg2)
10479 call = build_call_expr_internal_loc (loc, ifn, integer_type_node,
10480 arg2 ? 2 : 1, arg0, arg2);
10481 else
10482 call = build_call_expr_loc (loc, builtin_decl_explicit (fcodei), 1,
10483 arg0);
10484 }
10485 if (addend)
10486 call = fold_build2 (PLUS_EXPR, integer_type_node, call,
10487 build_int_cst (integer_type_node, addend));
10488 if (arg1 && arg2 == NULL_TREE)
10489 call = fold_build3 (COND_EXPR, integer_type_node,
10490 fold_build2 (NE_EXPR, boolean_type_node,
10491 arg0, build_zero_cst (arg0_type)),
10492 call, arg1);
10493
10494 return call;
10495 }
10496
10497 /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
10498 that return both result of arithmetics and overflowed boolean
10499 flag in a complex integer result. */
10500
10501 static tree
10502 fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
10503 tree *args)
10504 {
10505 enum internal_fn ifn;
10506
10507 switch (fcode)
10508 {
10509 case BUILT_IN_ADDC:
10510 case BUILT_IN_ADDCL:
10511 case BUILT_IN_ADDCLL:
10512 ifn = IFN_ADD_OVERFLOW;
10513 break;
10514 case BUILT_IN_SUBC:
10515 case BUILT_IN_SUBCL:
10516 case BUILT_IN_SUBCLL:
10517 ifn = IFN_SUB_OVERFLOW;
10518 break;
10519 default:
10520 gcc_unreachable ();
10521 }
10522
10523 tree type = TREE_TYPE (args[0]);
10524 tree ctype = build_complex_type (type);
10525 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10526 args[0], args[1]);
10527 /* Force SAVE_EXPR even for calls which satisfy tree_invariant_p_1,
10528 as while the call itself is const, the REALPART_EXPR store is
10529 certainly not. And in any case, we want just one call,
10530 not multiple and trying to CSE them later. */
10531 TREE_SIDE_EFFECTS (call) = 1;
10532 tree tgt = save_expr (call);
10533 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10534 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10535 call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
10536 intres, args[2]);
10537 TREE_SIDE_EFFECTS (call) = 1;
10538 tgt = save_expr (call);
10539 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
10540 tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
10541 ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
10542 tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
10543 tree store
10544 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
10545 return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
10546 }
10547
10548 /* Fold a call to __builtin_FILE to a constant string. */
10549
10550 static inline tree
10551 fold_builtin_FILE (location_t loc)
10552 {
10553 if (const char *fname = LOCATION_FILE (loc))
10554 {
10555 /* The documentation says this builtin is equivalent to the preprocessor
10556 __FILE__ macro so it appears appropriate to use the same file prefix
10557 mappings. */
10558 fname = remap_macro_filename (fname);
10559 return build_string_literal (fname);
10560 }
10561
10562 return build_string_literal ("");
10563 }
10564
10565 /* Fold a call to __builtin_FUNCTION to a constant string. */
10566
10567 static inline tree
10568 fold_builtin_FUNCTION ()
10569 {
10570 const char *name = "";
10571
10572 if (current_function_decl)
10573 name = lang_hooks.decl_printable_name (current_function_decl, 0);
10574
10575 return build_string_literal (name);
10576 }
10577
10578 /* Fold a call to __builtin_LINE to an integer constant. */
10579
10580 static inline tree
10581 fold_builtin_LINE (location_t loc, tree type)
10582 {
10583 return build_int_cst (type, LOCATION_LINE (loc));
10584 }
10585
10586 /* Fold a call to built-in function FNDECL with 0 arguments.
10587 This function returns NULL_TREE if no simplification was possible. */
10588
10589 static tree
10590 fold_builtin_0 (location_t loc, tree fndecl)
10591 {
10592 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10593 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10594 switch (fcode)
10595 {
10596 case BUILT_IN_FILE:
10597 return fold_builtin_FILE (loc);
10598
10599 case BUILT_IN_FUNCTION:
10600 return fold_builtin_FUNCTION ();
10601
10602 case BUILT_IN_LINE:
10603 return fold_builtin_LINE (loc, type);
10604
10605 CASE_FLT_FN (BUILT_IN_INF):
10606 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
10607 case BUILT_IN_INFD32:
10608 case BUILT_IN_INFD64:
10609 case BUILT_IN_INFD128:
10610 case BUILT_IN_INFD64X:
10611 return fold_builtin_inf (loc, type, true);
10612
10613 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10614 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
10615 return fold_builtin_inf (loc, type, false);
10616
10617 case BUILT_IN_CLASSIFY_TYPE:
10618 return fold_builtin_classify_type (NULL_TREE);
10619
10620 case BUILT_IN_UNREACHABLE:
10621 /* Rewrite any explicit calls to __builtin_unreachable. */
10622 if (sanitize_flags_p (SANITIZE_UNREACHABLE))
10623 return build_builtin_unreachable (loc);
10624 break;
10625
10626 default:
10627 break;
10628 }
10629 return NULL_TREE;
10630 }
10631
10632 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10633 This function returns NULL_TREE if no simplification was possible. */
10634
10635 static tree
10636 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
10637 {
10638 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10639 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10640
10641 if (error_operand_p (arg0))
10642 return NULL_TREE;
10643
10644 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
10645 return ret;
10646
10647 switch (fcode)
10648 {
10649 case BUILT_IN_CONSTANT_P:
10650 {
10651 tree val = fold_builtin_constant_p (arg0);
10652
10653 /* Gimplification will pull the CALL_EXPR for the builtin out of
10654 an if condition. When not optimizing, we'll not CSE it back.
10655 To avoid link error types of regressions, return false now. */
10656 if (!val && !optimize)
10657 val = integer_zero_node;
10658
10659 return val;
10660 }
10661
10662 case BUILT_IN_CLASSIFY_TYPE:
10663 return fold_builtin_classify_type (arg0);
10664
10665 case BUILT_IN_STRLEN:
10666 return fold_builtin_strlen (loc, expr, type, arg0);
10667
10668 CASE_FLT_FN (BUILT_IN_FABS):
10669 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10670 case BUILT_IN_FABSD32:
10671 case BUILT_IN_FABSD64:
10672 case BUILT_IN_FABSD128:
10673 case BUILT_IN_FABSD64X:
10674 return fold_builtin_fabs (loc, arg0, type);
10675
10676 case BUILT_IN_ABS:
10677 case BUILT_IN_LABS:
10678 case BUILT_IN_LLABS:
10679 case BUILT_IN_IMAXABS:
10680 case BUILT_IN_UABS:
10681 case BUILT_IN_ULABS:
10682 case BUILT_IN_ULLABS:
10683 case BUILT_IN_UIMAXABS:
10684 return fold_builtin_abs (loc, arg0, type);
10685
10686 CASE_FLT_FN (BUILT_IN_CONJ):
10687 if (validate_arg (arg0, COMPLEX_TYPE)
10688 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10689 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10690 break;
10691
10692 CASE_FLT_FN (BUILT_IN_CREAL):
10693 if (validate_arg (arg0, COMPLEX_TYPE)
10694 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10695 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
10696 break;
10697
10698 CASE_FLT_FN (BUILT_IN_CIMAG):
10699 if (validate_arg (arg0, COMPLEX_TYPE)
10700 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10701 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10702 break;
10703
10704 CASE_FLT_FN (BUILT_IN_CARG):
10705 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
10706 return fold_builtin_carg (loc, arg0, type);
10707
10708 case BUILT_IN_ISASCII:
10709 return fold_builtin_isascii (loc, arg0);
10710
10711 case BUILT_IN_TOASCII:
10712 return fold_builtin_toascii (loc, arg0);
10713
10714 case BUILT_IN_ISDIGIT:
10715 return fold_builtin_isdigit (loc, arg0);
10716
10717 CASE_FLT_FN (BUILT_IN_FINITE):
10718 case BUILT_IN_FINITED32:
10719 case BUILT_IN_FINITED64:
10720 case BUILT_IN_FINITED128:
10721 case BUILT_IN_ISFINITE:
10722 {
10723 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10724 if (ret)
10725 return ret;
10726 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10727 }
10728
10729 CASE_FLT_FN (BUILT_IN_ISINF):
10730 case BUILT_IN_ISINFD32:
10731 case BUILT_IN_ISINFD64:
10732 case BUILT_IN_ISINFD128:
10733 {
10734 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10735 if (ret)
10736 return ret;
10737 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10738 }
10739
10740 case BUILT_IN_ISNORMAL:
10741 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10742
10743 case BUILT_IN_ISINF_SIGN:
10744 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10745
10746 CASE_FLT_FN (BUILT_IN_ISNAN):
10747 case BUILT_IN_ISNAND32:
10748 case BUILT_IN_ISNAND64:
10749 case BUILT_IN_ISNAND128:
10750 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10751
10752 case BUILT_IN_ISSIGNALING:
10753 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
10754
10755 case BUILT_IN_FREE:
10756 if (integer_zerop (arg0))
10757 return build_empty_stmt (loc);
10758 break;
10759
10760 case BUILT_IN_CLZG:
10761 case BUILT_IN_CTZG:
10762 case BUILT_IN_CLRSBG:
10763 case BUILT_IN_FFSG:
10764 case BUILT_IN_PARITYG:
10765 case BUILT_IN_POPCOUNTG:
10766 return fold_builtin_bit_query (loc, fcode, arg0, NULL_TREE);
10767
10768 default:
10769 break;
10770 }
10771
10772 return NULL_TREE;
10773
10774 }
10775
10776 /* Folds a call EXPR (which may be null) to built-in function FNDECL
10777 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
10778 if no simplification was possible. */
10779
10780 static tree
10781 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
10782 {
10783 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10784 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10785
10786 if (error_operand_p (arg0)
10787 || error_operand_p (arg1))
10788 return NULL_TREE;
10789
10790 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
10791 return ret;
10792
10793 switch (fcode)
10794 {
10795 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10796 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10797 if (validate_arg (arg0, REAL_TYPE)
10798 && validate_arg (arg1, POINTER_TYPE))
10799 return do_mpfr_lgamma_r (arg0, arg1, type);
10800 break;
10801
10802 CASE_FLT_FN (BUILT_IN_FREXP):
10803 return fold_builtin_frexp (loc, arg0, arg1, type);
10804
10805 CASE_FLT_FN (BUILT_IN_MODF):
10806 return fold_builtin_modf (loc, arg0, arg1, type);
10807
10808 case BUILT_IN_STRSPN:
10809 return fold_builtin_strspn (loc, expr, arg0, arg1, type);
10810
10811 case BUILT_IN_STRCSPN:
10812 return fold_builtin_strcspn (loc, expr, arg0, arg1, type);
10813
10814 case BUILT_IN_STRPBRK:
10815 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
10816
10817 case BUILT_IN_EXPECT:
10818 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
10819
10820 case BUILT_IN_ISGREATER:
10821 return fold_builtin_unordered_cmp (loc, fndecl,
10822 arg0, arg1, UNLE_EXPR, LE_EXPR);
10823 case BUILT_IN_ISGREATEREQUAL:
10824 return fold_builtin_unordered_cmp (loc, fndecl,
10825 arg0, arg1, UNLT_EXPR, LT_EXPR);
10826 case BUILT_IN_ISLESS:
10827 return fold_builtin_unordered_cmp (loc, fndecl,
10828 arg0, arg1, UNGE_EXPR, GE_EXPR);
10829 case BUILT_IN_ISLESSEQUAL:
10830 return fold_builtin_unordered_cmp (loc, fndecl,
10831 arg0, arg1, UNGT_EXPR, GT_EXPR);
10832 case BUILT_IN_ISLESSGREATER:
10833 return fold_builtin_unordered_cmp (loc, fndecl,
10834 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10835 case BUILT_IN_ISUNORDERED:
10836 return fold_builtin_unordered_cmp (loc, fndecl,
10837 arg0, arg1, UNORDERED_EXPR,
10838 NOP_EXPR);
10839
10840 case BUILT_IN_ISEQSIG:
10841 return fold_builtin_iseqsig (loc, arg0, arg1);
10842
10843 /* We do the folding for va_start in the expander. */
10844 case BUILT_IN_VA_START:
10845 break;
10846
10847 case BUILT_IN_OBJECT_SIZE:
10848 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
10849 return fold_builtin_object_size (arg0, arg1, fcode);
10850
10851 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10852 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10853
10854 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10855 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10856
10857 case BUILT_IN_CLZG:
10858 case BUILT_IN_CTZG:
10859 return fold_builtin_bit_query (loc, fcode, arg0, arg1);
10860
10861 default:
10862 break;
10863 }
10864 return NULL_TREE;
10865 }
10866
10867 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10868 and ARG2.
10869 This function returns NULL_TREE if no simplification was possible. */
10870
10871 static tree
10872 fold_builtin_3 (location_t loc, tree fndecl,
10873 tree arg0, tree arg1, tree arg2)
10874 {
10875 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10876 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10877
10878 if (error_operand_p (arg0)
10879 || error_operand_p (arg1)
10880 || error_operand_p (arg2))
10881 return NULL_TREE;
10882
10883 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
10884 arg0, arg1, arg2))
10885 return ret;
10886
10887 switch (fcode)
10888 {
10889
10890 CASE_FLT_FN (BUILT_IN_SINCOS):
10891 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10892
10893 CASE_FLT_FN (BUILT_IN_REMQUO):
10894 if (validate_arg (arg0, REAL_TYPE)
10895 && validate_arg (arg1, REAL_TYPE)
10896 && validate_arg (arg2, POINTER_TYPE))
10897 return do_mpfr_remquo (arg0, arg1, arg2);
10898 break;
10899
10900 case BUILT_IN_MEMCMP:
10901 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
10902
10903 case BUILT_IN_EXPECT:
10904 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
10905
10906 case BUILT_IN_EXPECT_WITH_PROBABILITY:
10907 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
10908
10909 case BUILT_IN_ADD_OVERFLOW:
10910 case BUILT_IN_SUB_OVERFLOW:
10911 case BUILT_IN_MUL_OVERFLOW:
10912 case BUILT_IN_ADD_OVERFLOW_P:
10913 case BUILT_IN_SUB_OVERFLOW_P:
10914 case BUILT_IN_MUL_OVERFLOW_P:
10915 case BUILT_IN_SADD_OVERFLOW:
10916 case BUILT_IN_SADDL_OVERFLOW:
10917 case BUILT_IN_SADDLL_OVERFLOW:
10918 case BUILT_IN_SSUB_OVERFLOW:
10919 case BUILT_IN_SSUBL_OVERFLOW:
10920 case BUILT_IN_SSUBLL_OVERFLOW:
10921 case BUILT_IN_SMUL_OVERFLOW:
10922 case BUILT_IN_SMULL_OVERFLOW:
10923 case BUILT_IN_SMULLL_OVERFLOW:
10924 case BUILT_IN_UADD_OVERFLOW:
10925 case BUILT_IN_UADDL_OVERFLOW:
10926 case BUILT_IN_UADDLL_OVERFLOW:
10927 case BUILT_IN_USUB_OVERFLOW:
10928 case BUILT_IN_USUBL_OVERFLOW:
10929 case BUILT_IN_USUBLL_OVERFLOW:
10930 case BUILT_IN_UMUL_OVERFLOW:
10931 case BUILT_IN_UMULL_OVERFLOW:
10932 case BUILT_IN_UMULLL_OVERFLOW:
10933 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10934
10935 default:
10936 break;
10937 }
10938 return NULL_TREE;
10939 }
10940
10941 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
10942 ARGS is an array of NARGS arguments. IGNORE is true if the result
10943 of the function call is ignored. This function returns NULL_TREE
10944 if no simplification was possible. */
10945
10946 static tree
10947 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
10948 int nargs, bool)
10949 {
10950 tree ret = NULL_TREE;
10951
10952 switch (nargs)
10953 {
10954 case 0:
10955 ret = fold_builtin_0 (loc, fndecl);
10956 break;
10957 case 1:
10958 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
10959 break;
10960 case 2:
10961 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
10962 break;
10963 case 3:
10964 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10965 break;
10966 default:
10967 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10968 break;
10969 }
10970 if (ret)
10971 {
10972 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10973 SET_EXPR_LOCATION (ret, loc);
10974 return ret;
10975 }
10976 return NULL_TREE;
10977 }
10978
10979 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10980 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10981 of arguments in ARGS to be omitted. OLDNARGS is the number of
10982 elements in ARGS. */
10983
10984 static tree
10985 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10986 int skip, tree fndecl, int n, va_list newargs)
10987 {
10988 int nargs = oldnargs - skip + n;
10989 tree *buffer;
10990
10991 if (n > 0)
10992 {
10993 int i, j;
10994
10995 buffer = XALLOCAVEC (tree, nargs);
10996 for (i = 0; i < n; i++)
10997 buffer[i] = va_arg (newargs, tree);
10998 for (j = skip; j < oldnargs; j++, i++)
10999 buffer[i] = args[j];
11000 }
11001 else
11002 buffer = args + skip;
11003
11004 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11005 }
11006
11007 /* Return true if FNDECL shouldn't be folded right now.
11008 If a built-in function has an inline attribute always_inline
11009 wrapper, defer folding it after always_inline functions have
11010 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11011 might not be performed. */
11012
11013 bool
11014 avoid_folding_inline_builtin (tree fndecl)
11015 {
11016 return (DECL_DECLARED_INLINE_P (fndecl)
11017 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11018 && cfun
11019 && !cfun->always_inline_functions_inlined
11020 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11021 }
11022
11023 /* A wrapper function for builtin folding that prevents warnings for
11024 "statement without effect" and the like, caused by removing the
11025 call node earlier than the warning is generated. */
11026
11027 tree
11028 fold_call_expr (location_t loc, tree exp, bool ignore)
11029 {
11030 tree ret = NULL_TREE;
11031 tree fndecl = get_callee_fndecl (exp);
11032 if (fndecl && fndecl_built_in_p (fndecl)
11033 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11034 yet. Defer folding until we see all the arguments
11035 (after inlining). */
11036 && !CALL_EXPR_VA_ARG_PACK (exp))
11037 {
11038 int nargs = call_expr_nargs (exp);
11039
11040 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11041 instead last argument is __builtin_va_arg_pack (). Defer folding
11042 even in that case, until arguments are finalized. */
11043 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11044 {
11045 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11046 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11047 return NULL_TREE;
11048 }
11049
11050 if (avoid_folding_inline_builtin (fndecl))
11051 return NULL_TREE;
11052
11053 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11054 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11055 CALL_EXPR_ARGP (exp), ignore);
11056 else
11057 {
11058 tree *args = CALL_EXPR_ARGP (exp);
11059 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
11060 if (ret)
11061 return ret;
11062 }
11063 }
11064 return NULL_TREE;
11065 }
11066
11067 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
11068 N arguments are passed in the array ARGARRAY. Return a folded
11069 expression or NULL_TREE if no simplification was possible. */
11070
11071 tree
11072 fold_builtin_call_array (location_t loc, tree,
11073 tree fn,
11074 int n,
11075 tree *argarray)
11076 {
11077 if (TREE_CODE (fn) != ADDR_EXPR)
11078 return NULL_TREE;
11079
11080 tree fndecl = TREE_OPERAND (fn, 0);
11081 if (TREE_CODE (fndecl) == FUNCTION_DECL
11082 && fndecl_built_in_p (fndecl))
11083 {
11084 /* If last argument is __builtin_va_arg_pack (), arguments to this
11085 function are not finalized yet. Defer folding until they are. */
11086 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11087 {
11088 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11089 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
11090 return NULL_TREE;
11091 }
11092 if (avoid_folding_inline_builtin (fndecl))
11093 return NULL_TREE;
11094 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11095 return targetm.fold_builtin (fndecl, n, argarray, false);
11096 else
11097 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
11098 }
11099
11100 return NULL_TREE;
11101 }
11102
11103 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11104 along with N new arguments specified as the "..." parameters. SKIP
11105 is the number of arguments in EXP to be omitted. This function is used
11106 to do varargs-to-varargs transformations. */
11107
11108 static tree
11109 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11110 {
11111 va_list ap;
11112 tree t;
11113
11114 va_start (ap, n);
11115 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11116 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11117 va_end (ap);
11118
11119 return t;
11120 }
11121
11122 /* Validate a single argument ARG against a tree code CODE representing
11123 a type. Return true when argument is valid. */
11124
11125 static bool
11126 validate_arg (const_tree arg, enum tree_code code)
11127 {
11128 if (!arg)
11129 return false;
11130 else if (code == POINTER_TYPE)
11131 return POINTER_TYPE_P (TREE_TYPE (arg));
11132 else if (code == INTEGER_TYPE)
11133 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11134 return code == TREE_CODE (TREE_TYPE (arg));
11135 }
11136
11137 /* This function validates the types of a function call argument list
11138 against a specified list of tree_codes. If the last specifier is a 0,
11139 that represents an ellipses, otherwise the last specifier must be a
11140 VOID_TYPE.
11141
11142 This is the GIMPLE version of validate_arglist. Eventually we want to
11143 completely convert builtins.cc to work from GIMPLEs and the tree based
11144 validate_arglist will then be removed. */
11145
11146 bool
11147 validate_gimple_arglist (const gcall *call, ...)
11148 {
11149 enum tree_code code;
11150 bool res = 0;
11151 va_list ap;
11152 const_tree arg;
11153 size_t i;
11154
11155 va_start (ap, call);
11156 i = 0;
11157
11158 do
11159 {
11160 code = (enum tree_code) va_arg (ap, int);
11161 switch (code)
11162 {
11163 case 0:
11164 /* This signifies an ellipses, any further arguments are all ok. */
11165 res = true;
11166 goto end;
11167 case VOID_TYPE:
11168 /* This signifies an endlink, if no arguments remain, return
11169 true, otherwise return false. */
11170 res = (i == gimple_call_num_args (call));
11171 goto end;
11172 default:
11173 /* If no parameters remain or the parameter's code does not
11174 match the specified code, return false. Otherwise continue
11175 checking any remaining arguments. */
11176 arg = gimple_call_arg (call, i++);
11177 if (!validate_arg (arg, code))
11178 goto end;
11179 break;
11180 }
11181 }
11182 while (1);
11183
11184 /* We need gotos here since we can only have one VA_CLOSE in a
11185 function. */
11186 end: ;
11187 va_end (ap);
11188
11189 return res;
11190 }
11191
11192 /* Default target-specific builtin expander that does nothing. */
11193
11194 rtx
11195 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11196 rtx target ATTRIBUTE_UNUSED,
11197 rtx subtarget ATTRIBUTE_UNUSED,
11198 machine_mode mode ATTRIBUTE_UNUSED,
11199 int ignore ATTRIBUTE_UNUSED)
11200 {
11201 return NULL_RTX;
11202 }
11203
11204 /* Returns true is EXP represents data that would potentially reside
11205 in a readonly section. */
11206
11207 bool
11208 readonly_data_expr (tree exp)
11209 {
11210 STRIP_NOPS (exp);
11211
11212 if (TREE_CODE (exp) != ADDR_EXPR)
11213 return false;
11214
11215 exp = get_base_address (TREE_OPERAND (exp, 0));
11216 if (!exp)
11217 return false;
11218
11219 /* Make sure we call decl_readonly_section only for trees it
11220 can handle (since it returns true for everything it doesn't
11221 understand). */
11222 if (TREE_CODE (exp) == STRING_CST
11223 || TREE_CODE (exp) == CONSTRUCTOR
11224 || (VAR_P (exp) && TREE_STATIC (exp)))
11225 return decl_readonly_section (exp, 0);
11226 else
11227 return false;
11228 }
11229
11230 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11231 to the call, and TYPE is its return type.
11232
11233 Return NULL_TREE if no simplification was possible, otherwise return the
11234 simplified form of the call as a tree.
11235
11236 The simplified form may be a constant or other expression which
11237 computes the same value, but in a more efficient manner (including
11238 calls to other builtin functions).
11239
11240 The call may contain arguments which need to be evaluated, but
11241 which are not useful to determine the result of the call. In
11242 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11243 COMPOUND_EXPR will be an argument which must be evaluated.
11244 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11245 COMPOUND_EXPR in the chain will contain the tree for the simplified
11246 form of the builtin function call. */
11247
11248 static tree
11249 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
11250 {
11251 if (!validate_arg (s1, POINTER_TYPE)
11252 || !validate_arg (s2, POINTER_TYPE))
11253 return NULL_TREE;
11254
11255 tree fn;
11256 const char *p1, *p2;
11257
11258 p2 = c_getstr (s2);
11259 if (p2 == NULL)
11260 return NULL_TREE;
11261
11262 p1 = c_getstr (s1);
11263 if (p1 != NULL)
11264 {
11265 const char *r = strpbrk (p1, p2);
11266 tree tem;
11267
11268 if (r == NULL)
11269 return build_int_cst (TREE_TYPE (s1), 0);
11270
11271 /* Return an offset into the constant string argument. */
11272 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11273 return fold_convert_loc (loc, type, tem);
11274 }
11275
11276 if (p2[0] == '\0')
11277 /* strpbrk(x, "") == NULL.
11278 Evaluate and ignore s1 in case it had side-effects. */
11279 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
11280
11281 if (p2[1] != '\0')
11282 return NULL_TREE; /* Really call strpbrk. */
11283
11284 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11285 if (!fn)
11286 return NULL_TREE;
11287
11288 /* New argument list transforming strpbrk(s1, s2) to
11289 strchr(s1, s2[0]). */
11290 return build_call_expr_loc (loc, fn, 2, s1,
11291 build_int_cst (integer_type_node, p2[0]));
11292 }
11293
11294 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11295 to the call.
11296
11297 Return NULL_TREE if no simplification was possible, otherwise return the
11298 simplified form of the call as a tree.
11299
11300 The simplified form may be a constant or other expression which
11301 computes the same value, but in a more efficient manner (including
11302 calls to other builtin functions).
11303
11304 The call may contain arguments which need to be evaluated, but
11305 which are not useful to determine the result of the call. In
11306 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11307 COMPOUND_EXPR will be an argument which must be evaluated.
11308 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11309 COMPOUND_EXPR in the chain will contain the tree for the simplified
11310 form of the builtin function call. */
11311
11312 static tree
11313 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11314 {
11315 if (!validate_arg (s1, POINTER_TYPE)
11316 || !validate_arg (s2, POINTER_TYPE))
11317 return NULL_TREE;
11318
11319 if (!check_nul_terminated_array (expr, s1)
11320 || !check_nul_terminated_array (expr, s2))
11321 return NULL_TREE;
11322
11323 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11324
11325 /* If either argument is "", return NULL_TREE. */
11326 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11327 /* Evaluate and ignore both arguments in case either one has
11328 side-effects. */
11329 return omit_two_operands_loc (loc, type, size_zero_node, s1, s2);
11330 return NULL_TREE;
11331 }
11332
11333 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11334 to the call.
11335
11336 Return NULL_TREE if no simplification was possible, otherwise return the
11337 simplified form of the call as a tree.
11338
11339 The simplified form may be a constant or other expression which
11340 computes the same value, but in a more efficient manner (including
11341 calls to other builtin functions).
11342
11343 The call may contain arguments which need to be evaluated, but
11344 which are not useful to determine the result of the call. In
11345 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11346 COMPOUND_EXPR will be an argument which must be evaluated.
11347 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11348 COMPOUND_EXPR in the chain will contain the tree for the simplified
11349 form of the builtin function call. */
11350
11351 static tree
11352 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2, tree type)
11353 {
11354 if (!validate_arg (s1, POINTER_TYPE)
11355 || !validate_arg (s2, POINTER_TYPE))
11356 return NULL_TREE;
11357
11358 if (!check_nul_terminated_array (expr, s1)
11359 || !check_nul_terminated_array (expr, s2))
11360 return NULL_TREE;
11361
11362 /* If the first argument is "", return NULL_TREE. */
11363 const char *p1 = c_getstr (s1);
11364 if (p1 && *p1 == '\0')
11365 {
11366 /* Evaluate and ignore argument s2 in case it has
11367 side-effects. */
11368 return omit_one_operand_loc (loc, type, size_zero_node, s2);
11369 }
11370
11371 /* If the second argument is "", return __builtin_strlen(s1). */
11372 const char *p2 = c_getstr (s2);
11373 if (p2 && *p2 == '\0')
11374 {
11375 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11376
11377 /* If the replacement _DECL isn't initialized, don't do the
11378 transformation. */
11379 if (!fn)
11380 return NULL_TREE;
11381
11382 return fold_convert_loc (loc, type,
11383 build_call_expr_loc (loc, fn, 1, s1));
11384 }
11385 return NULL_TREE;
11386 }
11387
11388 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11389 produced. False otherwise. This is done so that we don't output the error
11390 or warning twice or three times. */
11391
11392 bool
11393 fold_builtin_next_arg (tree exp, bool va_start_p)
11394 {
11395 tree fntype = TREE_TYPE (current_function_decl);
11396 int nargs = call_expr_nargs (exp);
11397 tree arg;
11398 /* There is good chance the current input_location points inside the
11399 definition of the va_start macro (perhaps on the token for
11400 builtin) in a system header, so warnings will not be emitted.
11401 Use the location in real source code. */
11402 location_t current_location =
11403 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11404 NULL);
11405
11406 if (!stdarg_p (fntype))
11407 {
11408 error ("%<va_start%> used in function with fixed arguments");
11409 return true;
11410 }
11411
11412 if (va_start_p)
11413 {
11414 if (va_start_p && (nargs != 2))
11415 {
11416 error ("wrong number of arguments to function %<va_start%>");
11417 return true;
11418 }
11419 arg = CALL_EXPR_ARG (exp, 1);
11420 }
11421 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11422 when we checked the arguments and if needed issued a warning. */
11423 else
11424 {
11425 if (nargs == 0)
11426 {
11427 /* Evidently an out of date version of <stdarg.h>; can't validate
11428 va_start's second argument, but can still work as intended. */
11429 warning_at (current_location,
11430 OPT_Wvarargs,
11431 "%<__builtin_next_arg%> called without an argument");
11432 return true;
11433 }
11434 else if (nargs > 1)
11435 {
11436 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11437 return true;
11438 }
11439 arg = CALL_EXPR_ARG (exp, 0);
11440 }
11441
11442 if (TREE_CODE (arg) == SSA_NAME
11443 && SSA_NAME_VAR (arg))
11444 arg = SSA_NAME_VAR (arg);
11445
11446 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11447 or __builtin_next_arg (0) the first time we see it, after checking
11448 the arguments and if needed issuing a warning. */
11449 if (!integer_zerop (arg))
11450 {
11451 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11452
11453 /* Strip off all nops for the sake of the comparison. This
11454 is not quite the same as STRIP_NOPS. It does more.
11455 We must also strip off INDIRECT_EXPR for C++ reference
11456 parameters. */
11457 while (CONVERT_EXPR_P (arg)
11458 || INDIRECT_REF_P (arg))
11459 arg = TREE_OPERAND (arg, 0);
11460 if (arg != last_parm)
11461 {
11462 /* FIXME: Sometimes with the tree optimizers we can get the
11463 not the last argument even though the user used the last
11464 argument. We just warn and set the arg to be the last
11465 argument so that we will get wrong-code because of
11466 it. */
11467 warning_at (current_location,
11468 OPT_Wvarargs,
11469 "second parameter of %<va_start%> not last named argument");
11470 }
11471
11472 /* Undefined by C99 7.15.1.4p4 (va_start):
11473 "If the parameter parmN is declared with the register storage
11474 class, with a function or array type, or with a type that is
11475 not compatible with the type that results after application of
11476 the default argument promotions, the behavior is undefined."
11477 */
11478 else if (DECL_REGISTER (arg))
11479 {
11480 warning_at (current_location,
11481 OPT_Wvarargs,
11482 "undefined behavior when second parameter of "
11483 "%<va_start%> is declared with %<register%> storage");
11484 }
11485
11486 /* We want to verify the second parameter just once before the tree
11487 optimizers are run and then avoid keeping it in the tree,
11488 as otherwise we could warn even for correct code like:
11489 void foo (int i, ...)
11490 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11491 if (va_start_p)
11492 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11493 else
11494 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11495 }
11496 return false;
11497 }
11498
11499
11500 /* Expand a call EXP to __builtin_object_size. */
11501
11502 static rtx
11503 expand_builtin_object_size (tree exp)
11504 {
11505 tree ost;
11506 int object_size_type;
11507 tree fndecl = get_callee_fndecl (exp);
11508
11509 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11510 {
11511 error ("first argument of %qD must be a pointer, second integer constant",
11512 fndecl);
11513 expand_builtin_trap ();
11514 return const0_rtx;
11515 }
11516
11517 ost = CALL_EXPR_ARG (exp, 1);
11518 STRIP_NOPS (ost);
11519
11520 if (TREE_CODE (ost) != INTEGER_CST
11521 || tree_int_cst_sgn (ost) < 0
11522 || compare_tree_int (ost, 3) > 0)
11523 {
11524 error ("last argument of %qD is not integer constant between 0 and 3",
11525 fndecl);
11526 expand_builtin_trap ();
11527 return const0_rtx;
11528 }
11529
11530 object_size_type = tree_to_shwi (ost);
11531
11532 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11533 }
11534
11535 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11536 FCODE is the BUILT_IN_* to use.
11537 Return NULL_RTX if we failed; the caller should emit a normal call,
11538 otherwise try to get the result in TARGET, if convenient (and in
11539 mode MODE if that's convenient). */
11540
11541 static rtx
11542 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11543 enum built_in_function fcode)
11544 {
11545 if (!validate_arglist (exp,
11546 POINTER_TYPE,
11547 fcode == BUILT_IN_MEMSET_CHK
11548 ? INTEGER_TYPE : POINTER_TYPE,
11549 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11550 return NULL_RTX;
11551
11552 tree dest = CALL_EXPR_ARG (exp, 0);
11553 tree src = CALL_EXPR_ARG (exp, 1);
11554 tree len = CALL_EXPR_ARG (exp, 2);
11555 tree size = CALL_EXPR_ARG (exp, 3);
11556
11557 /* FIXME: Set access mode to write only for memset et al. */
11558 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
11559 /*srcstr=*/NULL_TREE, size, access_read_write);
11560
11561 if (!tree_fits_uhwi_p (size))
11562 return NULL_RTX;
11563
11564 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11565 {
11566 /* Avoid transforming the checking call to an ordinary one when
11567 an overflow has been detected or when the call couldn't be
11568 validated because the size is not constant. */
11569 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
11570 return NULL_RTX;
11571
11572 tree fn = NULL_TREE;
11573 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11574 mem{cpy,pcpy,move,set} is available. */
11575 switch (fcode)
11576 {
11577 case BUILT_IN_MEMCPY_CHK:
11578 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11579 break;
11580 case BUILT_IN_MEMPCPY_CHK:
11581 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11582 break;
11583 case BUILT_IN_MEMMOVE_CHK:
11584 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11585 break;
11586 case BUILT_IN_MEMSET_CHK:
11587 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11588 break;
11589 default:
11590 break;
11591 }
11592
11593 if (! fn)
11594 return NULL_RTX;
11595
11596 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11597 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11598 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11599 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11600 }
11601 else if (fcode == BUILT_IN_MEMSET_CHK)
11602 return NULL_RTX;
11603 else
11604 {
11605 unsigned int dest_align = get_pointer_alignment (dest);
11606
11607 /* If DEST is not a pointer type, call the normal function. */
11608 if (dest_align == 0)
11609 return NULL_RTX;
11610
11611 /* If SRC and DEST are the same (and not volatile), do nothing. */
11612 if (operand_equal_p (src, dest, 0))
11613 {
11614 tree expr;
11615
11616 if (fcode != BUILT_IN_MEMPCPY_CHK)
11617 {
11618 /* Evaluate and ignore LEN in case it has side-effects. */
11619 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11620 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11621 }
11622
11623 expr = fold_build_pointer_plus (dest, len);
11624 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11625 }
11626
11627 /* __memmove_chk special case. */
11628 if (fcode == BUILT_IN_MEMMOVE_CHK)
11629 {
11630 unsigned int src_align = get_pointer_alignment (src);
11631
11632 if (src_align == 0)
11633 return NULL_RTX;
11634
11635 /* If src is categorized for a readonly section we can use
11636 normal __memcpy_chk. */
11637 if (readonly_data_expr (src))
11638 {
11639 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11640 if (!fn)
11641 return NULL_RTX;
11642 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11643 dest, src, len, size);
11644 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11645 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11646 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11647 }
11648 }
11649 return NULL_RTX;
11650 }
11651 }
11652
11653 /* Emit warning if a buffer overflow is detected at compile time. */
11654
11655 static void
11656 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11657 {
11658 /* The source string. */
11659 tree srcstr = NULL_TREE;
11660 /* The size of the destination object returned by __builtin_object_size. */
11661 tree objsize = NULL_TREE;
11662 /* The string that is being concatenated with (as in __strcat_chk)
11663 or null if it isn't. */
11664 tree catstr = NULL_TREE;
11665 /* The maximum length of the source sequence in a bounded operation
11666 (such as __strncat_chk) or null if the operation isn't bounded
11667 (such as __strcat_chk). */
11668 tree maxread = NULL_TREE;
11669 /* The exact size of the access (such as in __strncpy_chk). */
11670 tree size = NULL_TREE;
11671 /* The access by the function that's checked. Except for snprintf
11672 both writing and reading is checked. */
11673 access_mode mode = access_read_write;
11674
11675 switch (fcode)
11676 {
11677 case BUILT_IN_STRCPY_CHK:
11678 case BUILT_IN_STPCPY_CHK:
11679 srcstr = CALL_EXPR_ARG (exp, 1);
11680 objsize = CALL_EXPR_ARG (exp, 2);
11681 break;
11682
11683 case BUILT_IN_STRCAT_CHK:
11684 /* For __strcat_chk the warning will be emitted only if overflowing
11685 by at least strlen (dest) + 1 bytes. */
11686 catstr = CALL_EXPR_ARG (exp, 0);
11687 srcstr = CALL_EXPR_ARG (exp, 1);
11688 objsize = CALL_EXPR_ARG (exp, 2);
11689 break;
11690
11691 case BUILT_IN_STRNCAT_CHK:
11692 catstr = CALL_EXPR_ARG (exp, 0);
11693 srcstr = CALL_EXPR_ARG (exp, 1);
11694 maxread = CALL_EXPR_ARG (exp, 2);
11695 objsize = CALL_EXPR_ARG (exp, 3);
11696 break;
11697
11698 case BUILT_IN_STRNCPY_CHK:
11699 case BUILT_IN_STPNCPY_CHK:
11700 srcstr = CALL_EXPR_ARG (exp, 1);
11701 size = CALL_EXPR_ARG (exp, 2);
11702 objsize = CALL_EXPR_ARG (exp, 3);
11703 break;
11704
11705 case BUILT_IN_SNPRINTF_CHK:
11706 case BUILT_IN_VSNPRINTF_CHK:
11707 maxread = CALL_EXPR_ARG (exp, 1);
11708 objsize = CALL_EXPR_ARG (exp, 3);
11709 /* The only checked access the write to the destination. */
11710 mode = access_write_only;
11711 break;
11712 default:
11713 gcc_unreachable ();
11714 }
11715
11716 if (catstr && maxread)
11717 {
11718 /* Check __strncat_chk. There is no way to determine the length
11719 of the string to which the source string is being appended so
11720 just warn when the length of the source string is not known. */
11721 check_strncat_sizes (exp, objsize);
11722 return;
11723 }
11724
11725 check_access (exp, size, maxread, srcstr, objsize, mode);
11726 }
11727
11728 /* Emit warning if a buffer overflow is detected at compile time
11729 in __sprintf_chk/__vsprintf_chk calls. */
11730
11731 static void
11732 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11733 {
11734 tree size, len, fmt;
11735 const char *fmt_str;
11736 int nargs = call_expr_nargs (exp);
11737
11738 /* Verify the required arguments in the original call. */
11739
11740 if (nargs < 4)
11741 return;
11742 size = CALL_EXPR_ARG (exp, 2);
11743 fmt = CALL_EXPR_ARG (exp, 3);
11744
11745 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11746 return;
11747
11748 /* Check whether the format is a literal string constant. */
11749 fmt_str = c_getstr (fmt);
11750 if (fmt_str == NULL)
11751 return;
11752
11753 if (!init_target_chars ())
11754 return;
11755
11756 /* If the format doesn't contain % args or %%, we know its size. */
11757 if (strchr (fmt_str, target_percent) == 0)
11758 len = build_int_cstu (size_type_node, strlen (fmt_str));
11759 /* If the format is "%s" and first ... argument is a string literal,
11760 we know it too. */
11761 else if (fcode == BUILT_IN_SPRINTF_CHK
11762 && strcmp (fmt_str, target_percent_s) == 0)
11763 {
11764 tree arg;
11765
11766 if (nargs < 5)
11767 return;
11768 arg = CALL_EXPR_ARG (exp, 4);
11769 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11770 return;
11771
11772 len = c_strlen (arg, 1);
11773 if (!len || ! tree_fits_uhwi_p (len))
11774 return;
11775 }
11776 else
11777 return;
11778
11779 /* Add one for the terminating nul. */
11780 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
11781
11782 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
11783 access_write_only);
11784 }
11785
11786 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11787 if possible. */
11788
11789 static tree
11790 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
11791 {
11792 tree bytes;
11793 int object_size_type;
11794
11795 if (!validate_arg (ptr, POINTER_TYPE)
11796 || !validate_arg (ost, INTEGER_TYPE))
11797 return NULL_TREE;
11798
11799 STRIP_NOPS (ost);
11800
11801 if (TREE_CODE (ost) != INTEGER_CST
11802 || tree_int_cst_sgn (ost) < 0
11803 || compare_tree_int (ost, 3) > 0)
11804 return NULL_TREE;
11805
11806 object_size_type = tree_to_shwi (ost);
11807
11808 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11809 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11810 and (size_t) 0 for types 2 and 3. */
11811 if (TREE_SIDE_EFFECTS (ptr))
11812 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11813
11814 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
11815 object_size_type |= OST_DYNAMIC;
11816
11817 if (TREE_CODE (ptr) == ADDR_EXPR)
11818 {
11819 compute_builtin_object_size (ptr, object_size_type, &bytes);
11820 if ((object_size_type & OST_DYNAMIC)
11821 || int_fits_type_p (bytes, size_type_node))
11822 return fold_convert (size_type_node, bytes);
11823 }
11824 else if (TREE_CODE (ptr) == SSA_NAME)
11825 {
11826 /* If object size is not known yet, delay folding until
11827 later. Maybe subsequent passes will help determining
11828 it. */
11829 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
11830 && ((object_size_type & OST_DYNAMIC)
11831 || int_fits_type_p (bytes, size_type_node)))
11832 return fold_convert (size_type_node, bytes);
11833 }
11834
11835 return NULL_TREE;
11836 }
11837
11838 /* Builtins with folding operations that operate on "..." arguments
11839 need special handling; we need to store the arguments in a convenient
11840 data structure before attempting any folding. Fortunately there are
11841 only a few builtins that fall into this category. FNDECL is the
11842 function, EXP is the CALL_EXPR for the call. */
11843
11844 static tree
11845 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11846 {
11847 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11848 tree ret = NULL_TREE;
11849
11850 switch (fcode)
11851 {
11852 case BUILT_IN_FPCLASSIFY:
11853 ret = fold_builtin_fpclassify (loc, args, nargs);
11854 break;
11855
11856 case BUILT_IN_ADDC:
11857 case BUILT_IN_ADDCL:
11858 case BUILT_IN_ADDCLL:
11859 case BUILT_IN_SUBC:
11860 case BUILT_IN_SUBCL:
11861 case BUILT_IN_SUBCLL:
11862 return fold_builtin_addc_subc (loc, fcode, args);
11863
11864 default:
11865 break;
11866 }
11867 if (ret)
11868 {
11869 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11870 SET_EXPR_LOCATION (ret, loc);
11871 suppress_warning (ret);
11872 return ret;
11873 }
11874 return NULL_TREE;
11875 }
11876
11877 /* Initialize format string characters in the target charset. */
11878
11879 bool
11880 init_target_chars (void)
11881 {
11882 static bool init;
11883 if (!init)
11884 {
11885 target_newline = lang_hooks.to_target_charset ('\n');
11886 target_percent = lang_hooks.to_target_charset ('%');
11887 target_c = lang_hooks.to_target_charset ('c');
11888 target_s = lang_hooks.to_target_charset ('s');
11889 if (target_newline == 0 || target_percent == 0 || target_c == 0
11890 || target_s == 0)
11891 return false;
11892
11893 target_percent_c[0] = target_percent;
11894 target_percent_c[1] = target_c;
11895 target_percent_c[2] = '\0';
11896
11897 target_percent_s[0] = target_percent;
11898 target_percent_s[1] = target_s;
11899 target_percent_s[2] = '\0';
11900
11901 target_percent_s_newline[0] = target_percent;
11902 target_percent_s_newline[1] = target_s;
11903 target_percent_s_newline[2] = target_newline;
11904 target_percent_s_newline[3] = '\0';
11905
11906 init = true;
11907 }
11908 return true;
11909 }
11910
11911 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11912 and no overflow/underflow occurred. INEXACT is true if M was not
11913 exactly calculated. TYPE is the tree type for the result. This
11914 function assumes that you cleared the MPFR flags and then
11915 calculated M to see if anything subsequently set a flag prior to
11916 entering this function. Return NULL_TREE if any checks fail. */
11917
11918 static tree
11919 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11920 {
11921 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11922 overflow/underflow occurred. If -frounding-math, proceed iff the
11923 result of calling FUNC was exact. */
11924 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11925 && (!flag_rounding_math || !inexact))
11926 {
11927 REAL_VALUE_TYPE rr;
11928
11929 real_from_mpfr (&rr, m, type, MPFR_RNDN);
11930 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11931 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11932 but the mpfr_t is not, then we underflowed in the
11933 conversion. */
11934 if (real_isfinite (&rr)
11935 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11936 {
11937 REAL_VALUE_TYPE rmode;
11938
11939 real_convert (&rmode, TYPE_MODE (type), &rr);
11940 /* Proceed iff the specified mode can hold the value. */
11941 if (real_identical (&rmode, &rr))
11942 return build_real (type, rmode);
11943 }
11944 }
11945 return NULL_TREE;
11946 }
11947
11948 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11949 number and no overflow/underflow occurred. INEXACT is true if M
11950 was not exactly calculated. TYPE is the tree type for the result.
11951 This function assumes that you cleared the MPFR flags and then
11952 calculated M to see if anything subsequently set a flag prior to
11953 entering this function. Return NULL_TREE if any checks fail, if
11954 FORCE_CONVERT is true, then bypass the checks. */
11955
11956 static tree
11957 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11958 {
11959 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11960 overflow/underflow occurred. If -frounding-math, proceed iff the
11961 result of calling FUNC was exact. */
11962 if (force_convert
11963 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11964 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11965 && (!flag_rounding_math || !inexact)))
11966 {
11967 REAL_VALUE_TYPE re, im;
11968
11969 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
11970 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
11971 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11972 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11973 but the mpfr_t is not, then we underflowed in the
11974 conversion. */
11975 if (force_convert
11976 || (real_isfinite (&re) && real_isfinite (&im)
11977 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11978 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11979 {
11980 REAL_VALUE_TYPE re_mode, im_mode;
11981
11982 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11983 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11984 /* Proceed iff the specified mode can hold the value. */
11985 if (force_convert
11986 || (real_identical (&re_mode, &re)
11987 && real_identical (&im_mode, &im)))
11988 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11989 build_real (TREE_TYPE (type), im_mode));
11990 }
11991 }
11992 return NULL_TREE;
11993 }
11994
11995 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11996 the pointer *(ARG_QUO) and return the result. The type is taken
11997 from the type of ARG0 and is used for setting the precision of the
11998 calculation and results. */
11999
12000 static tree
12001 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12002 {
12003 tree const type = TREE_TYPE (arg0);
12004 tree result = NULL_TREE;
12005
12006 STRIP_NOPS (arg0);
12007 STRIP_NOPS (arg1);
12008
12009 /* To proceed, MPFR must exactly represent the target floating point
12010 format, which only happens when the target base equals two. */
12011 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12012 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12013 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12014 {
12015 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12016 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12017
12018 if (real_isfinite (ra0) && real_isfinite (ra1))
12019 {
12020 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12021 const int prec = fmt->p;
12022 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12023 tree result_rem;
12024 long integer_quo;
12025 mpfr_t m0, m1;
12026
12027 mpfr_inits2 (prec, m0, m1, NULL);
12028 mpfr_from_real (m0, ra0, MPFR_RNDN);
12029 mpfr_from_real (m1, ra1, MPFR_RNDN);
12030 mpfr_clear_flags ();
12031 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12032 /* Remquo is independent of the rounding mode, so pass
12033 inexact=0 to do_mpfr_ckconv(). */
12034 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12035 mpfr_clears (m0, m1, NULL);
12036 if (result_rem)
12037 {
12038 /* MPFR calculates quo in the host's long so it may
12039 return more bits in quo than the target int can hold
12040 if sizeof(host long) > sizeof(target int). This can
12041 happen even for native compilers in LP64 mode. In
12042 these cases, modulo the quo value with the largest
12043 number that the target int can hold while leaving one
12044 bit for the sign. */
12045 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12046 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12047
12048 /* Dereference the quo pointer argument. */
12049 arg_quo = build_fold_indirect_ref (arg_quo);
12050 /* Proceed iff a valid pointer type was passed in. */
12051 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12052 {
12053 /* Set the value. */
12054 tree result_quo
12055 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12056 build_int_cst (TREE_TYPE (arg_quo),
12057 integer_quo));
12058 TREE_SIDE_EFFECTS (result_quo) = 1;
12059 /* Combine the quo assignment with the rem. */
12060 result = fold_build2 (COMPOUND_EXPR, type,
12061 result_quo, result_rem);
12062 suppress_warning (result, OPT_Wunused_value);
12063 result = non_lvalue (result);
12064 }
12065 }
12066 }
12067 }
12068 return result;
12069 }
12070
12071 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12072 resulting value as a tree with type TYPE. The mpfr precision is
12073 set to the precision of TYPE. We assume that this mpfr function
12074 returns zero if the result could be calculated exactly within the
12075 requested precision. In addition, the integer pointer represented
12076 by ARG_SG will be dereferenced and set to the appropriate signgam
12077 (-1,1) value. */
12078
12079 static tree
12080 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12081 {
12082 tree result = NULL_TREE;
12083
12084 STRIP_NOPS (arg);
12085
12086 /* To proceed, MPFR must exactly represent the target floating point
12087 format, which only happens when the target base equals two. Also
12088 verify ARG is a constant and that ARG_SG is an int pointer. */
12089 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12090 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12091 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12092 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12093 {
12094 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12095
12096 /* In addition to NaN and Inf, the argument cannot be zero or a
12097 negative integer. */
12098 if (real_isfinite (ra)
12099 && ra->cl != rvc_zero
12100 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12101 {
12102 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12103 const int prec = fmt->p;
12104 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
12105 int inexact, sg;
12106 tree result_lg;
12107
12108 auto_mpfr m (prec);
12109 mpfr_from_real (m, ra, MPFR_RNDN);
12110 mpfr_clear_flags ();
12111 inexact = mpfr_lgamma (m, &sg, m, rnd);
12112 result_lg = do_mpfr_ckconv (m, type, inexact);
12113 if (result_lg)
12114 {
12115 tree result_sg;
12116
12117 /* Dereference the arg_sg pointer argument. */
12118 arg_sg = build_fold_indirect_ref (arg_sg);
12119 /* Assign the signgam value into *arg_sg. */
12120 result_sg = fold_build2 (MODIFY_EXPR,
12121 TREE_TYPE (arg_sg), arg_sg,
12122 build_int_cst (TREE_TYPE (arg_sg), sg));
12123 TREE_SIDE_EFFECTS (result_sg) = 1;
12124 /* Combine the signgam assignment with the lgamma result. */
12125 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12126 result_sg, result_lg));
12127 }
12128 }
12129 }
12130
12131 return result;
12132 }
12133
12134 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12135 mpc function FUNC on it and return the resulting value as a tree
12136 with type TYPE. The mpfr precision is set to the precision of
12137 TYPE. We assume that function FUNC returns zero if the result
12138 could be calculated exactly within the requested precision. If
12139 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12140 in the arguments and/or results. */
12141
12142 tree
12143 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12144 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12145 {
12146 tree result = NULL_TREE;
12147
12148 STRIP_NOPS (arg0);
12149 STRIP_NOPS (arg1);
12150
12151 /* To proceed, MPFR must exactly represent the target floating point
12152 format, which only happens when the target base equals two. */
12153 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12154 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
12155 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12156 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
12157 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12158 {
12159 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12160 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12161 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12162 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12163
12164 if (do_nonfinite
12165 || (real_isfinite (re0) && real_isfinite (im0)
12166 && real_isfinite (re1) && real_isfinite (im1)))
12167 {
12168 const struct real_format *const fmt =
12169 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12170 const int prec = fmt->p;
12171 const mpfr_rnd_t rnd = fmt->round_towards_zero
12172 ? MPFR_RNDZ : MPFR_RNDN;
12173 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12174 int inexact;
12175 mpc_t m0, m1;
12176
12177 mpc_init2 (m0, prec);
12178 mpc_init2 (m1, prec);
12179 mpfr_from_real (mpc_realref (m0), re0, rnd);
12180 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12181 mpfr_from_real (mpc_realref (m1), re1, rnd);
12182 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12183 mpfr_clear_flags ();
12184 inexact = func (m0, m0, m1, crnd);
12185 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12186 mpc_clear (m0);
12187 mpc_clear (m1);
12188 }
12189 }
12190
12191 return result;
12192 }
12193
12194 /* A wrapper function for builtin folding that prevents warnings for
12195 "statement without effect" and the like, caused by removing the
12196 call node earlier than the warning is generated. */
12197
12198 tree
12199 fold_call_stmt (gcall *stmt, bool ignore)
12200 {
12201 tree ret = NULL_TREE;
12202 tree fndecl = gimple_call_fndecl (stmt);
12203 location_t loc = gimple_location (stmt);
12204 if (fndecl && fndecl_built_in_p (fndecl)
12205 && !gimple_call_va_arg_pack_p (stmt))
12206 {
12207 int nargs = gimple_call_num_args (stmt);
12208 tree *args = (nargs > 0
12209 ? gimple_call_arg_ptr (stmt, 0)
12210 : &error_mark_node);
12211
12212 if (avoid_folding_inline_builtin (fndecl))
12213 return NULL_TREE;
12214 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12215 {
12216 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12217 }
12218 else
12219 {
12220 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
12221 if (ret)
12222 {
12223 /* Propagate location information from original call to
12224 expansion of builtin. Otherwise things like
12225 maybe_emit_chk_warning, that operate on the expansion
12226 of a builtin, will use the wrong location information. */
12227 if (gimple_has_location (stmt))
12228 {
12229 tree realret = ret;
12230 if (TREE_CODE (ret) == NOP_EXPR)
12231 realret = TREE_OPERAND (ret, 0);
12232 if (CAN_HAVE_LOCATION_P (realret)
12233 && !EXPR_HAS_LOCATION (realret))
12234 SET_EXPR_LOCATION (realret, loc);
12235 return realret;
12236 }
12237 return ret;
12238 }
12239 }
12240 }
12241 return NULL_TREE;
12242 }
12243
12244 /* Look up the function in builtin_decl that corresponds to DECL
12245 and set ASMSPEC as its user assembler name. DECL must be a
12246 function decl that declares a builtin. */
12247
12248 void
12249 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12250 {
12251 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
12252 && asmspec != 0);
12253
12254 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12255 set_user_assembler_name (builtin, asmspec);
12256
12257 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
12258 && INT_TYPE_SIZE < BITS_PER_WORD)
12259 {
12260 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
12261 set_user_assembler_libfunc ("ffs", asmspec);
12262 set_optab_libfunc (ffs_optab, mode, "ffs");
12263 }
12264 }
12265
12266 /* Return true if DECL is a builtin that expands to a constant or similarly
12267 simple code. */
12268 bool
12269 is_simple_builtin (tree decl)
12270 {
12271 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
12272 switch (DECL_FUNCTION_CODE (decl))
12273 {
12274 /* Builtins that expand to constants. */
12275 case BUILT_IN_CONSTANT_P:
12276 case BUILT_IN_EXPECT:
12277 case BUILT_IN_OBJECT_SIZE:
12278 case BUILT_IN_UNREACHABLE:
12279 /* Simple register moves or loads from stack. */
12280 case BUILT_IN_ASSUME_ALIGNED:
12281 case BUILT_IN_RETURN_ADDRESS:
12282 case BUILT_IN_EXTRACT_RETURN_ADDR:
12283 case BUILT_IN_FROB_RETURN_ADDR:
12284 case BUILT_IN_RETURN:
12285 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12286 case BUILT_IN_FRAME_ADDRESS:
12287 case BUILT_IN_VA_END:
12288 case BUILT_IN_STACK_SAVE:
12289 case BUILT_IN_STACK_RESTORE:
12290 case BUILT_IN_DWARF_CFA:
12291 /* Exception state returns or moves registers around. */
12292 case BUILT_IN_EH_FILTER:
12293 case BUILT_IN_EH_POINTER:
12294 case BUILT_IN_EH_COPY_VALUES:
12295 return true;
12296
12297 default:
12298 return false;
12299 }
12300
12301 return false;
12302 }
12303
12304 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12305 most probably expanded inline into reasonably simple code. This is a
12306 superset of is_simple_builtin. */
12307 bool
12308 is_inexpensive_builtin (tree decl)
12309 {
12310 if (!decl)
12311 return false;
12312 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12313 return true;
12314 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12315 switch (DECL_FUNCTION_CODE (decl))
12316 {
12317 case BUILT_IN_ABS:
12318 CASE_BUILT_IN_ALLOCA:
12319 case BUILT_IN_BSWAP16:
12320 case BUILT_IN_BSWAP32:
12321 case BUILT_IN_BSWAP64:
12322 case BUILT_IN_BSWAP128:
12323 case BUILT_IN_CLZ:
12324 case BUILT_IN_CLZIMAX:
12325 case BUILT_IN_CLZL:
12326 case BUILT_IN_CLZLL:
12327 case BUILT_IN_CTZ:
12328 case BUILT_IN_CTZIMAX:
12329 case BUILT_IN_CTZL:
12330 case BUILT_IN_CTZLL:
12331 case BUILT_IN_FFS:
12332 case BUILT_IN_FFSIMAX:
12333 case BUILT_IN_FFSL:
12334 case BUILT_IN_FFSLL:
12335 case BUILT_IN_IMAXABS:
12336 case BUILT_IN_FINITE:
12337 case BUILT_IN_FINITEF:
12338 case BUILT_IN_FINITEL:
12339 case BUILT_IN_FINITED32:
12340 case BUILT_IN_FINITED64:
12341 case BUILT_IN_FINITED128:
12342 case BUILT_IN_FPCLASSIFY:
12343 case BUILT_IN_ISFINITE:
12344 case BUILT_IN_ISINF_SIGN:
12345 case BUILT_IN_ISINF:
12346 case BUILT_IN_ISINFF:
12347 case BUILT_IN_ISINFL:
12348 case BUILT_IN_ISINFD32:
12349 case BUILT_IN_ISINFD64:
12350 case BUILT_IN_ISINFD128:
12351 case BUILT_IN_ISNAN:
12352 case BUILT_IN_ISNANF:
12353 case BUILT_IN_ISNANL:
12354 case BUILT_IN_ISNAND32:
12355 case BUILT_IN_ISNAND64:
12356 case BUILT_IN_ISNAND128:
12357 case BUILT_IN_ISNORMAL:
12358 case BUILT_IN_ISGREATER:
12359 case BUILT_IN_ISGREATEREQUAL:
12360 case BUILT_IN_ISLESS:
12361 case BUILT_IN_ISLESSEQUAL:
12362 case BUILT_IN_ISLESSGREATER:
12363 case BUILT_IN_ISUNORDERED:
12364 case BUILT_IN_ISEQSIG:
12365 case BUILT_IN_VA_ARG_PACK:
12366 case BUILT_IN_VA_ARG_PACK_LEN:
12367 case BUILT_IN_VA_COPY:
12368 case BUILT_IN_TRAP:
12369 case BUILT_IN_UNREACHABLE_TRAP:
12370 case BUILT_IN_SAVEREGS:
12371 case BUILT_IN_POPCOUNTL:
12372 case BUILT_IN_POPCOUNTLL:
12373 case BUILT_IN_POPCOUNTIMAX:
12374 case BUILT_IN_POPCOUNT:
12375 case BUILT_IN_PARITYL:
12376 case BUILT_IN_PARITYLL:
12377 case BUILT_IN_PARITYIMAX:
12378 case BUILT_IN_PARITY:
12379 case BUILT_IN_LABS:
12380 case BUILT_IN_LLABS:
12381 case BUILT_IN_PREFETCH:
12382 case BUILT_IN_ACC_ON_DEVICE:
12383 return true;
12384
12385 default:
12386 return is_simple_builtin (decl);
12387 }
12388
12389 return false;
12390 }
12391
12392 /* Return true if T is a constant and the value cast to a target char
12393 can be represented by a host char.
12394 Store the casted char constant in *P if so. */
12395
12396 bool
12397 target_char_cst_p (tree t, char *p)
12398 {
12399 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
12400 return false;
12401
12402 *p = (char)tree_to_uhwi (t);
12403 return true;
12404 }
12405
12406 /* Return true if the builtin DECL is implemented in a standard library.
12407 Otherwise return false which doesn't guarantee it is not (thus the list
12408 of handled builtins below may be incomplete). */
12409
12410 bool
12411 builtin_with_linkage_p (tree decl)
12412 {
12413 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12414 switch (DECL_FUNCTION_CODE (decl))
12415 {
12416 CASE_FLT_FN (BUILT_IN_ACOS):
12417 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
12418 CASE_FLT_FN (BUILT_IN_ACOSH):
12419 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
12420 CASE_FLT_FN (BUILT_IN_ASIN):
12421 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
12422 CASE_FLT_FN (BUILT_IN_ASINH):
12423 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
12424 CASE_FLT_FN (BUILT_IN_ATAN):
12425 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
12426 CASE_FLT_FN (BUILT_IN_ATANH):
12427 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
12428 CASE_FLT_FN (BUILT_IN_ATAN2):
12429 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
12430 CASE_FLT_FN (BUILT_IN_CBRT):
12431 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
12432 CASE_FLT_FN (BUILT_IN_CEIL):
12433 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
12434 CASE_FLT_FN (BUILT_IN_COPYSIGN):
12435 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
12436 CASE_FLT_FN (BUILT_IN_COS):
12437 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
12438 CASE_FLT_FN (BUILT_IN_COSH):
12439 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
12440 CASE_FLT_FN (BUILT_IN_ERF):
12441 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
12442 CASE_FLT_FN (BUILT_IN_ERFC):
12443 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
12444 CASE_FLT_FN (BUILT_IN_EXP):
12445 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
12446 CASE_FLT_FN (BUILT_IN_EXP2):
12447 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
12448 CASE_FLT_FN (BUILT_IN_EXPM1):
12449 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
12450 CASE_FLT_FN (BUILT_IN_FABS):
12451 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
12452 CASE_FLT_FN (BUILT_IN_FDIM):
12453 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
12454 CASE_FLT_FN (BUILT_IN_FLOOR):
12455 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
12456 CASE_FLT_FN (BUILT_IN_FMA):
12457 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
12458 CASE_FLT_FN (BUILT_IN_FMAX):
12459 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
12460 CASE_FLT_FN (BUILT_IN_FMIN):
12461 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
12462 CASE_FLT_FN (BUILT_IN_FMOD):
12463 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
12464 CASE_FLT_FN (BUILT_IN_FREXP):
12465 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
12466 CASE_FLT_FN (BUILT_IN_HYPOT):
12467 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
12468 CASE_FLT_FN (BUILT_IN_ILOGB):
12469 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
12470 CASE_FLT_FN (BUILT_IN_LDEXP):
12471 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
12472 CASE_FLT_FN (BUILT_IN_LGAMMA):
12473 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
12474 CASE_FLT_FN (BUILT_IN_LLRINT):
12475 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
12476 CASE_FLT_FN (BUILT_IN_LLROUND):
12477 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
12478 CASE_FLT_FN (BUILT_IN_LOG):
12479 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
12480 CASE_FLT_FN (BUILT_IN_LOG10):
12481 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
12482 CASE_FLT_FN (BUILT_IN_LOG1P):
12483 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
12484 CASE_FLT_FN (BUILT_IN_LOG2):
12485 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
12486 CASE_FLT_FN (BUILT_IN_LOGB):
12487 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
12488 CASE_FLT_FN (BUILT_IN_LRINT):
12489 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
12490 CASE_FLT_FN (BUILT_IN_LROUND):
12491 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
12492 CASE_FLT_FN (BUILT_IN_MODF):
12493 CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
12494 CASE_FLT_FN (BUILT_IN_NAN):
12495 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
12496 CASE_FLT_FN (BUILT_IN_NEARBYINT):
12497 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
12498 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
12499 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
12500 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
12501 CASE_FLT_FN (BUILT_IN_POW):
12502 CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
12503 CASE_FLT_FN (BUILT_IN_REMAINDER):
12504 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
12505 CASE_FLT_FN (BUILT_IN_REMQUO):
12506 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
12507 CASE_FLT_FN (BUILT_IN_RINT):
12508 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
12509 CASE_FLT_FN (BUILT_IN_ROUND):
12510 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
12511 CASE_FLT_FN (BUILT_IN_SCALBLN):
12512 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
12513 CASE_FLT_FN (BUILT_IN_SCALBN):
12514 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
12515 CASE_FLT_FN (BUILT_IN_SIN):
12516 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
12517 CASE_FLT_FN (BUILT_IN_SINH):
12518 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
12519 CASE_FLT_FN (BUILT_IN_SINCOS):
12520 CASE_FLT_FN (BUILT_IN_SQRT):
12521 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
12522 CASE_FLT_FN (BUILT_IN_TAN):
12523 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
12524 CASE_FLT_FN (BUILT_IN_TANH):
12525 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
12526 CASE_FLT_FN (BUILT_IN_TGAMMA):
12527 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
12528 CASE_FLT_FN (BUILT_IN_TRUNC):
12529 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
12530 return true;
12531
12532 case BUILT_IN_STPCPY:
12533 case BUILT_IN_STPNCPY:
12534 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
12535 by libiberty's stpcpy.c for MinGW targets so we need to return true
12536 in order to be able to build libiberty in LTO mode for them. */
12537 return true;
12538
12539 default:
12540 break;
12541 }
12542 return false;
12543 }
12544
12545 /* Return true if OFFRNG is bounded to a subrange of offset values
12546 valid for the largest possible object. */
12547
12548 bool
12549 access_ref::offset_bounded () const
12550 {
12551 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
12552 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
12553 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
12554 }
12555
12556 /* If CALLEE has known side effects, fill in INFO and return true.
12557 See tree-ssa-structalias.cc:find_func_aliases
12558 for the list of builtins we might need to handle here. */
12559
12560 attr_fnspec
12561 builtin_fnspec (tree callee)
12562 {
12563 built_in_function code = DECL_FUNCTION_CODE (callee);
12564
12565 switch (code)
12566 {
12567 /* All the following functions read memory pointed to by
12568 their second argument and write memory pointed to by first
12569 argument.
12570 strcat/strncat additionally reads memory pointed to by the first
12571 argument. */
12572 case BUILT_IN_STRCAT:
12573 case BUILT_IN_STRCAT_CHK:
12574 return "1cW 1 ";
12575 case BUILT_IN_STRNCAT:
12576 case BUILT_IN_STRNCAT_CHK:
12577 return "1cW 13";
12578 case BUILT_IN_STRCPY:
12579 case BUILT_IN_STRCPY_CHK:
12580 return "1cO 1 ";
12581 case BUILT_IN_STPCPY:
12582 case BUILT_IN_STPCPY_CHK:
12583 return ".cO 1 ";
12584 case BUILT_IN_STRNCPY:
12585 case BUILT_IN_MEMCPY:
12586 case BUILT_IN_MEMMOVE:
12587 case BUILT_IN_TM_MEMCPY:
12588 case BUILT_IN_TM_MEMMOVE:
12589 case BUILT_IN_STRNCPY_CHK:
12590 case BUILT_IN_MEMCPY_CHK:
12591 case BUILT_IN_MEMMOVE_CHK:
12592 return "1cO313";
12593 case BUILT_IN_MEMPCPY:
12594 case BUILT_IN_MEMPCPY_CHK:
12595 return ".cO313";
12596 case BUILT_IN_STPNCPY:
12597 case BUILT_IN_STPNCPY_CHK:
12598 return ".cO313";
12599 case BUILT_IN_BCOPY:
12600 return ".c23O3";
12601 case BUILT_IN_BZERO:
12602 return ".cO2";
12603 case BUILT_IN_MEMCMP:
12604 case BUILT_IN_MEMCMP_EQ:
12605 case BUILT_IN_BCMP:
12606 case BUILT_IN_STRNCMP:
12607 case BUILT_IN_STRNCMP_EQ:
12608 case BUILT_IN_STRNCASECMP:
12609 return ".cR3R3";
12610
12611 /* The following functions read memory pointed to by their
12612 first argument. */
12613 CASE_BUILT_IN_TM_LOAD (1):
12614 CASE_BUILT_IN_TM_LOAD (2):
12615 CASE_BUILT_IN_TM_LOAD (4):
12616 CASE_BUILT_IN_TM_LOAD (8):
12617 CASE_BUILT_IN_TM_LOAD (FLOAT):
12618 CASE_BUILT_IN_TM_LOAD (DOUBLE):
12619 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
12620 CASE_BUILT_IN_TM_LOAD (M64):
12621 CASE_BUILT_IN_TM_LOAD (M128):
12622 CASE_BUILT_IN_TM_LOAD (M256):
12623 case BUILT_IN_TM_LOG:
12624 case BUILT_IN_TM_LOG_1:
12625 case BUILT_IN_TM_LOG_2:
12626 case BUILT_IN_TM_LOG_4:
12627 case BUILT_IN_TM_LOG_8:
12628 case BUILT_IN_TM_LOG_FLOAT:
12629 case BUILT_IN_TM_LOG_DOUBLE:
12630 case BUILT_IN_TM_LOG_LDOUBLE:
12631 case BUILT_IN_TM_LOG_M64:
12632 case BUILT_IN_TM_LOG_M128:
12633 case BUILT_IN_TM_LOG_M256:
12634 return ".cR ";
12635
12636 case BUILT_IN_INDEX:
12637 case BUILT_IN_RINDEX:
12638 case BUILT_IN_STRCHR:
12639 case BUILT_IN_STRLEN:
12640 case BUILT_IN_STRRCHR:
12641 return ".cR ";
12642 case BUILT_IN_STRNLEN:
12643 return ".cR2";
12644
12645 /* These read memory pointed to by the first argument.
12646 Allocating memory does not have any side-effects apart from
12647 being the definition point for the pointer.
12648 Unix98 specifies that errno is set on allocation failure. */
12649 case BUILT_IN_STRDUP:
12650 return "mCR ";
12651 case BUILT_IN_STRNDUP:
12652 return "mCR2";
12653 /* Allocating memory does not have any side-effects apart from
12654 being the definition point for the pointer. */
12655 case BUILT_IN_MALLOC:
12656 case BUILT_IN_ALIGNED_ALLOC:
12657 case BUILT_IN_CALLOC:
12658 case BUILT_IN_GOMP_ALLOC:
12659 return "mC";
12660 CASE_BUILT_IN_ALLOCA:
12661 return "mc";
12662 /* These read memory pointed to by the first argument with size
12663 in the third argument. */
12664 case BUILT_IN_MEMCHR:
12665 return ".cR3";
12666 /* These read memory pointed to by the first and second arguments. */
12667 case BUILT_IN_STRSTR:
12668 case BUILT_IN_STRPBRK:
12669 case BUILT_IN_STRCASECMP:
12670 case BUILT_IN_STRCSPN:
12671 case BUILT_IN_STRSPN:
12672 case BUILT_IN_STRCMP:
12673 case BUILT_IN_STRCMP_EQ:
12674 return ".cR R ";
12675 /* Freeing memory kills the pointed-to memory. More importantly
12676 the call has to serve as a barrier for moving loads and stores
12677 across it. */
12678 case BUILT_IN_STACK_RESTORE:
12679 case BUILT_IN_FREE:
12680 case BUILT_IN_GOMP_FREE:
12681 return ".co ";
12682 case BUILT_IN_VA_END:
12683 return ".cO ";
12684 /* Realloc serves both as allocation point and deallocation point. */
12685 case BUILT_IN_REALLOC:
12686 case BUILT_IN_GOMP_REALLOC:
12687 return ".Cw ";
12688 case BUILT_IN_GAMMA_R:
12689 case BUILT_IN_GAMMAF_R:
12690 case BUILT_IN_GAMMAL_R:
12691 case BUILT_IN_LGAMMA_R:
12692 case BUILT_IN_LGAMMAF_R:
12693 case BUILT_IN_LGAMMAL_R:
12694 return ".C. Ot";
12695 case BUILT_IN_FREXP:
12696 case BUILT_IN_FREXPF:
12697 case BUILT_IN_FREXPL:
12698 case BUILT_IN_MODF:
12699 case BUILT_IN_MODFF:
12700 case BUILT_IN_MODFL:
12701 return ".c. Ot";
12702 case BUILT_IN_REMQUO:
12703 case BUILT_IN_REMQUOF:
12704 case BUILT_IN_REMQUOL:
12705 return ".c. . Ot";
12706 case BUILT_IN_SINCOS:
12707 case BUILT_IN_SINCOSF:
12708 case BUILT_IN_SINCOSL:
12709 return ".c. OtOt";
12710 case BUILT_IN_MEMSET:
12711 case BUILT_IN_MEMSET_CHK:
12712 case BUILT_IN_TM_MEMSET:
12713 return "1cO3";
12714 CASE_BUILT_IN_TM_STORE (1):
12715 CASE_BUILT_IN_TM_STORE (2):
12716 CASE_BUILT_IN_TM_STORE (4):
12717 CASE_BUILT_IN_TM_STORE (8):
12718 CASE_BUILT_IN_TM_STORE (FLOAT):
12719 CASE_BUILT_IN_TM_STORE (DOUBLE):
12720 CASE_BUILT_IN_TM_STORE (LDOUBLE):
12721 CASE_BUILT_IN_TM_STORE (M64):
12722 CASE_BUILT_IN_TM_STORE (M128):
12723 CASE_BUILT_IN_TM_STORE (M256):
12724 return ".cO ";
12725 case BUILT_IN_STACK_SAVE:
12726 case BUILT_IN_RETURN:
12727 case BUILT_IN_EH_POINTER:
12728 case BUILT_IN_EH_FILTER:
12729 case BUILT_IN_UNWIND_RESUME:
12730 case BUILT_IN_CXA_END_CLEANUP:
12731 case BUILT_IN_EH_COPY_VALUES:
12732 case BUILT_IN_FRAME_ADDRESS:
12733 case BUILT_IN_APPLY_ARGS:
12734 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
12735 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
12736 case BUILT_IN_PREFETCH:
12737 case BUILT_IN_DWARF_CFA:
12738 case BUILT_IN_RETURN_ADDRESS:
12739 return ".c";
12740 case BUILT_IN_ASSUME_ALIGNED:
12741 case BUILT_IN_EXPECT:
12742 case BUILT_IN_EXPECT_WITH_PROBABILITY:
12743 return "1cX ";
12744 /* But posix_memalign stores a pointer into the memory pointed to
12745 by its first argument. */
12746 case BUILT_IN_POSIX_MEMALIGN:
12747 return ".cOt";
12748 case BUILT_IN_OMP_GET_MAPPED_PTR:
12749 return ". R ";
12750
12751 default:
12752 return "";
12753 }
12754 }