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