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