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