]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/builtins.c
gcc/
[thirdparty/gcc.git] / gcc / builtins.c
CommitLineData
53800dbe 1/* Expand builtin functions.
61f1dccc 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
c307f106 3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
53800dbe 5
f12b58b3 6This file is part of GCC.
53800dbe 7
f12b58b3 8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
f12b58b3 11version.
53800dbe 12
f12b58b3 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
53800dbe 17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
53800dbe 21
22#include "config.h"
23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
53800dbe 26#include "machmode.h"
27#include "rtl.h"
28#include "tree.h"
dae0b5cb 29#include "realmpfr.h"
75a70cf9 30#include "gimple.h"
53800dbe 31#include "flags.h"
32#include "regs.h"
33#include "hard-reg-set.h"
34#include "except.h"
35#include "function.h"
53800dbe 36#include "insn-config.h"
37#include "expr.h"
d8fc4d0b 38#include "optabs.h"
39#include "libfuncs.h"
53800dbe 40#include "recog.h"
41#include "output.h"
42#include "typeclass.h"
689df48e 43#include "predict.h"
1dd6c958 44#include "tm_p.h"
fc2a2dcb 45#include "target.h"
63c62881 46#include "langhooks.h"
0ec80471 47#include "basic-block.h"
fd62c19a 48#include "tree-mudflap.h"
5a84fdd6 49#include "tree-flow.h"
162719b3 50#include "value-prof.h"
852f689e 51#include "diagnostic-core.h"
3b9c3a16 52#include "builtins.h"
53800dbe 53
5383fb56 54
726e2588 55#ifndef PAD_VARARGS_DOWN
56#define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57#endif
239d491a 58static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
726e2588 59
3b9c3a16 60struct target_builtins default_target_builtins;
61#if SWITCHABLE_TARGET
62struct target_builtins *this_target_builtins = &default_target_builtins;
63#endif
64
ab7943b9 65/* Define the names of the builtin function types and codes. */
8934cb0c 66const char *const built_in_class_names[4]
ab7943b9 67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
68
9cfddb70 69#define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
0dfc45b5 70const char * built_in_names[(int) END_BUILTINS] =
4e9d90c7 71{
72#include "builtins.def"
73};
74#undef DEF_BUILTIN
ab7943b9 75
df94cd3b 76/* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
b9a16870 78builtin_info_type builtin_info;
df94cd3b 79
aecda0d6 80static const char *c_getstr (tree);
81static rtx c_readstr (const char *, enum machine_mode);
82static int target_char_cast (tree, char *);
d8ae1baa 83static rtx get_memory_rtx (tree, tree);
aecda0d6 84static int apply_args_size (void);
85static int apply_result_size (void);
d8c9779c 86#if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
aecda0d6 87static rtx result_vector (int, rtx);
d8c9779c 88#endif
843d08a9 89static void expand_builtin_update_setjmp_buf (rtx);
aecda0d6 90static void expand_builtin_prefetch (tree);
91static rtx expand_builtin_apply_args (void);
92static rtx expand_builtin_apply_args_1 (void);
93static rtx expand_builtin_apply (rtx, rtx, rtx);
94static void expand_builtin_return (rtx);
95static enum type_class type_to_class (tree);
96static rtx expand_builtin_classify_type (tree);
97static void expand_errno_check (tree, rtx);
98static rtx expand_builtin_mathfn (tree, rtx, rtx);
99static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
6b43bae4 100static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
7e0713b1 101static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
f97eea22 102static rtx expand_builtin_interclass_mathfn (tree, rtx);
c3147c1a 103static rtx expand_builtin_sincos (tree);
f97eea22 104static rtx expand_builtin_cexpi (tree, rtx);
ff1b14e4 105static rtx expand_builtin_int_roundingfn (tree, rtx);
106static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
79012a9d 107static rtx expand_builtin_next_arg (void);
aecda0d6 108static rtx expand_builtin_va_start (tree);
109static rtx expand_builtin_va_end (tree);
110static rtx expand_builtin_va_copy (tree);
c2f47e15 111static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
a65c4d64 112static rtx expand_builtin_strcmp (tree, rtx);
aecda0d6 113static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
a65c4d64 115static rtx expand_builtin_memcpy (tree, rtx);
c2f47e15 116static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
48e1416a 117static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
c2f47e15 118 enum machine_mode, int);
a65c4d64 119static rtx expand_builtin_strcpy (tree, rtx);
120static rtx expand_builtin_strcpy_args (tree, tree, rtx);
aecda0d6 121static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
a65c4d64 122static rtx expand_builtin_strncpy (tree, rtx);
aecda0d6 123static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
c2f47e15 124static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
aecda0d6 126static rtx expand_builtin_bzero (tree);
127static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
5be42b39 128static rtx expand_builtin_alloca (tree, bool);
aecda0d6 129static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130static rtx expand_builtin_frame_address (tree, tree);
389dd41b 131static tree stabilize_va_list_loc (location_t, tree, int);
aecda0d6 132static rtx expand_builtin_expect (tree, rtx);
133static tree fold_builtin_constant_p (tree);
389dd41b 134static tree fold_builtin_expect (location_t, tree, tree);
aecda0d6 135static tree fold_builtin_classify_type (tree);
c7cbde74 136static tree fold_builtin_strlen (location_t, tree, tree);
389dd41b 137static tree fold_builtin_inf (location_t, tree, int);
aecda0d6 138static tree fold_builtin_nan (tree, tree, int);
389dd41b 139static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
b7bf20db 140static bool validate_arg (const_tree, enum tree_code code);
277f8dd2 141static bool integer_valued_real_p (tree);
389dd41b 142static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
aecda0d6 143static bool readonly_data_expr (tree);
144static rtx expand_builtin_fabs (tree, rtx, rtx);
27f261ef 145static rtx expand_builtin_signbit (tree, rtx);
389dd41b 146static tree fold_builtin_sqrt (location_t, tree, tree);
147static tree fold_builtin_cbrt (location_t, tree, tree);
148static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150static tree fold_builtin_cos (location_t, tree, tree, tree);
151static tree fold_builtin_cosh (location_t, tree, tree, tree);
bffb7645 152static tree fold_builtin_tan (tree, tree);
389dd41b 153static tree fold_builtin_trunc (location_t, tree, tree);
154static tree fold_builtin_floor (location_t, tree, tree);
155static tree fold_builtin_ceil (location_t, tree, tree);
156static tree fold_builtin_round (location_t, tree, tree);
157static tree fold_builtin_int_roundingfn (location_t, tree, tree);
10b9666f 158static tree fold_builtin_bitop (tree, tree);
389dd41b 159static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160static tree fold_builtin_strchr (location_t, tree, tree, tree);
161static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163static tree fold_builtin_strcmp (location_t, tree, tree);
164static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165static tree fold_builtin_signbit (location_t, tree, tree);
166static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167static tree fold_builtin_isascii (location_t, tree);
168static tree fold_builtin_toascii (location_t, tree);
169static tree fold_builtin_isdigit (location_t, tree);
170static tree fold_builtin_fabs (location_t, tree, tree);
171static tree fold_builtin_abs (location_t, tree, tree);
172static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
d5019fe8 173 enum tree_code);
389dd41b 174static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175static tree fold_builtin_0 (location_t, tree, bool);
176static tree fold_builtin_1 (location_t, tree, tree, bool);
177static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180static tree fold_builtin_varargs (location_t, tree, tree, bool);
181
182static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183static tree fold_builtin_strstr (location_t, tree, tree, tree);
184static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185static tree fold_builtin_strcat (location_t, tree, tree);
186static tree fold_builtin_strncat (location_t, tree, tree, tree);
187static tree fold_builtin_strspn (location_t, tree, tree);
188static tree fold_builtin_strcspn (location_t, tree, tree);
189static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
750d7b16 190static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
4ee9c684 191
0a39fd54 192static rtx expand_builtin_object_size (tree);
193static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194 enum built_in_function);
195static void maybe_emit_chk_warning (tree, enum built_in_function);
196static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
2c281b15 197static void maybe_emit_free_warning (tree);
c2f47e15 198static tree fold_builtin_object_size (tree, tree);
389dd41b 199static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
c2f47e15 204 enum built_in_function);
99eabcc1 205static bool init_target_chars (void);
206
207static unsigned HOST_WIDE_INT target_newline;
208static unsigned HOST_WIDE_INT target_percent;
209static unsigned HOST_WIDE_INT target_c;
210static unsigned HOST_WIDE_INT target_s;
211static char target_percent_c[3];
212static char target_percent_s[3];
213static char target_percent_s_newline[4];
728bac60 214static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
f0c477f2 216static tree do_mpfr_arg2 (tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
9917422b 218static tree do_mpfr_arg3 (tree, tree, tree, tree,
219 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
d92f994c 220static tree do_mpfr_sincos (tree, tree, tree);
65dd1378 221static tree do_mpfr_bessel_n (tree, tree, tree,
222 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, bool);
e5407ca6 224static tree do_mpfr_remquo (tree, tree, tree);
e84da7c1 225static tree do_mpfr_lgamma_r (tree, tree, tree);
1cd6e20d 226static void expand_builtin_sync_synchronize (void);
0a39fd54 227
7bfefa9d 228/* Return true if NAME starts with __builtin_ or __sync_. */
229
b29139ad 230static bool
1c47b3e8 231is_builtin_name (const char *name)
b6a5fc45 232{
b6a5fc45 233 if (strncmp (name, "__builtin_", 10) == 0)
234 return true;
235 if (strncmp (name, "__sync_", 7) == 0)
236 return true;
1cd6e20d 237 if (strncmp (name, "__atomic_", 9) == 0)
238 return true;
b6a5fc45 239 return false;
240}
4ee9c684 241
7bfefa9d 242
243/* Return true if DECL is a function symbol representing a built-in. */
244
245bool
246is_builtin_fn (tree decl)
247{
248 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
249}
250
251
1c47b3e8 252/* Return true if NODE should be considered for inline expansion regardless
253 of the optimization level. This means whenever a function is invoked with
254 its "internal" name, which normally contains the prefix "__builtin". */
255
256static bool
257called_as_built_in (tree node)
258{
259 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260 we want the name used to call the function, not the name it
261 will have. */
262 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
263 return is_builtin_name (name);
264}
265
0d8f7716 266/* Compute values M and N such that M divides (address of EXP - N) and
267 such that N < M. Store N in *BITPOSP and return M.
268
269 Note that the address (and thus the alignment) computed here is based
270 on the address to which a symbol resolves, whereas DECL_ALIGN is based
271 on the address at which an object is actually located. These two
272 addresses are not always the same. For example, on ARM targets,
273 the address &foo of a Thumb function foo() has the lowest bit set,
274 whereas foo() itself starts on an even address. */
698537d1 275
98ab9e8f 276unsigned int
0c883ef3 277get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
698537d1 278{
98ab9e8f 279 HOST_WIDE_INT bitsize, bitpos;
280 tree offset;
281 enum machine_mode mode;
282 int unsignedp, volatilep;
283 unsigned int align, inner;
698537d1 284
98ab9e8f 285 /* Get the innermost object and the constant (bitpos) and possibly
286 variable (offset) offset of the access. */
287 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
288 &mode, &unsignedp, &volatilep, true);
289
290 /* Extract alignment information from the innermost object and
291 possibly adjust bitpos and offset. */
0ea95334 292 if (TREE_CODE (exp) == CONST_DECL)
293 exp = DECL_INITIAL (exp);
e064ace6 294 if (DECL_P (exp)
295 && TREE_CODE (exp) != LABEL_DECL)
0d8f7716 296 {
297 if (TREE_CODE (exp) == FUNCTION_DECL)
298 {
299 /* Function addresses can encode extra information besides their
300 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
301 allows the low bit to be used as a virtual bit, we know
302 that the address itself must be 2-byte aligned. */
303 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
304 align = 2 * BITS_PER_UNIT;
305 else
306 align = BITS_PER_UNIT;
307 }
308 else
309 align = DECL_ALIGN (exp);
310 }
698537d1 311 else if (CONSTANT_CLASS_P (exp))
98ab9e8f 312 {
313 align = TYPE_ALIGN (TREE_TYPE (exp));
314#ifdef CONSTANT_ALIGNMENT
315 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
698537d1 316#endif
98ab9e8f 317 }
318 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
319 align = TYPE_ALIGN (TREE_TYPE (exp));
320 else if (TREE_CODE (exp) == INDIRECT_REF)
321 align = TYPE_ALIGN (TREE_TYPE (exp));
98ab9e8f 322 else if (TREE_CODE (exp) == MEM_REF)
323 {
324 tree addr = TREE_OPERAND (exp, 0);
153c3b50 325 struct ptr_info_def *pi;
98ab9e8f 326 if (TREE_CODE (addr) == BIT_AND_EXPR
327 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
328 {
329 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
330 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
331 align *= BITS_PER_UNIT;
332 addr = TREE_OPERAND (addr, 0);
333 }
334 else
335 align = BITS_PER_UNIT;
153c3b50 336 if (TREE_CODE (addr) == SSA_NAME
337 && (pi = SSA_NAME_PTR_INFO (addr)))
338 {
339 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
340 align = MAX (pi->align * BITS_PER_UNIT, align);
341 }
342 else if (TREE_CODE (addr) == ADDR_EXPR)
957d0361 343 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
98ab9e8f 344 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
345 }
28daba6f 346 else if (TREE_CODE (exp) == TARGET_MEM_REF)
153c3b50 347 {
348 struct ptr_info_def *pi;
349 tree addr = TMR_BASE (exp);
350 if (TREE_CODE (addr) == BIT_AND_EXPR
351 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
352 {
353 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
354 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
355 align *= BITS_PER_UNIT;
356 addr = TREE_OPERAND (addr, 0);
357 }
358 else
359 align = BITS_PER_UNIT;
360 if (TREE_CODE (addr) == SSA_NAME
361 && (pi = SSA_NAME_PTR_INFO (addr)))
362 {
363 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
364 align = MAX (pi->align * BITS_PER_UNIT, align);
365 }
366 else if (TREE_CODE (addr) == ADDR_EXPR)
957d0361 367 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
153c3b50 368 if (TMR_OFFSET (exp))
369 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
370 if (TMR_INDEX (exp) && TMR_STEP (exp))
371 {
372 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
373 align = MIN (align, (step & -step) * BITS_PER_UNIT);
374 }
375 else if (TMR_INDEX (exp))
376 align = BITS_PER_UNIT;
28daba6f 377 if (TMR_INDEX2 (exp))
e077c79b 378 align = BITS_PER_UNIT;
98ab9e8f 379 }
698537d1 380 else
98ab9e8f 381 align = BITS_PER_UNIT;
382
383 /* If there is a non-constant offset part extract the maximum
384 alignment that can prevail. */
0c883ef3 385 inner = ~0U;
98ab9e8f 386 while (offset)
387 {
388 tree next_offset;
389
390 if (TREE_CODE (offset) == PLUS_EXPR)
391 {
392 next_offset = TREE_OPERAND (offset, 0);
393 offset = TREE_OPERAND (offset, 1);
394 }
395 else
396 next_offset = NULL;
397 if (host_integerp (offset, 1))
398 {
399 /* Any overflow in calculating offset_bits won't change
400 the alignment. */
401 unsigned offset_bits
402 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
403
404 if (offset_bits)
405 inner = MIN (inner, (offset_bits & -offset_bits));
406 }
407 else if (TREE_CODE (offset) == MULT_EXPR
408 && host_integerp (TREE_OPERAND (offset, 1), 1))
409 {
410 /* Any overflow in calculating offset_factor won't change
411 the alignment. */
412 unsigned offset_factor
413 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
414 * BITS_PER_UNIT);
415
416 if (offset_factor)
417 inner = MIN (inner, (offset_factor & -offset_factor));
418 }
419 else
420 {
421 inner = MIN (inner, BITS_PER_UNIT);
422 break;
423 }
424 offset = next_offset;
425 }
426
427 /* Alignment is innermost object alignment adjusted by the constant
428 and non-constant offset parts. */
429 align = MIN (align, inner);
430 bitpos = bitpos & (align - 1);
431
0c883ef3 432 *bitposp = bitpos;
433 return align;
434}
435
957d0361 436/* Return the alignment in bits of EXP, an object. */
0c883ef3 437
438unsigned int
957d0361 439get_object_alignment (tree exp)
0c883ef3 440{
441 unsigned HOST_WIDE_INT bitpos = 0;
442 unsigned int align;
443
444 align = get_object_alignment_1 (exp, &bitpos);
445
98ab9e8f 446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
448
449 if (bitpos != 0)
450 align = (bitpos & -bitpos);
451
957d0361 452 return align;
698537d1 453}
454
e3694d1d 455/* Return the alignment of object EXP, also considering its type when we do
456 not know of explicit misalignment. Only handle MEM_REF and TARGET_MEM_REF.
457
458 ??? Note that, in the general case, the type of an expression is not kept
459 consistent with misalignment information by the front-end, for example when
460 taking the address of a member of a packed structure. However, in most of
461 the cases, expressions have the alignment of their type so we optimistically
462 fall back to this alignment when we cannot compute a misalignment. */
463
464unsigned int
465get_object_or_type_alignment (tree exp)
466{
467 unsigned HOST_WIDE_INT misalign;
468 unsigned int align = get_object_alignment_1 (exp, &misalign);
469
470 gcc_assert (TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == TARGET_MEM_REF);
471
472 if (misalign != 0)
473 align = (misalign & -misalign);
474 else
475 align = MAX (TYPE_ALIGN (TREE_TYPE (exp)), align);
476
477 return align;
478}
479
69fbc3aa 480/* For a pointer valued expression EXP compute values M and N such that
481 M divides (EXP - N) and such that N < M. Store N in *BITPOSP and return M.
53800dbe 482
69fbc3aa 483 If EXP is not a pointer, 0 is returned. */
53800dbe 484
153c3b50 485unsigned int
69fbc3aa 486get_pointer_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
53800dbe 487{
153c3b50 488 STRIP_NOPS (exp);
535e2026 489
153c3b50 490 if (TREE_CODE (exp) == ADDR_EXPR)
69fbc3aa 491 return get_object_alignment_1 (TREE_OPERAND (exp, 0), bitposp);
153c3b50 492 else if (TREE_CODE (exp) == SSA_NAME
493 && POINTER_TYPE_P (TREE_TYPE (exp)))
53800dbe 494 {
153c3b50 495 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
153c3b50 496 if (!pi)
69fbc3aa 497 {
498 *bitposp = 0;
499 return BITS_PER_UNIT;
500 }
501 *bitposp = pi->misalign * BITS_PER_UNIT;
502 return pi->align * BITS_PER_UNIT;
53800dbe 503 }
153c3b50 504
69fbc3aa 505 *bitposp = 0;
153c3b50 506 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
53800dbe 507}
508
69fbc3aa 509/* Return the alignment in bits of EXP, a pointer valued expression.
510 The alignment returned is, by default, the alignment of the thing that
511 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
512
513 Otherwise, look at the expression to see if we can do better, i.e., if the
514 expression is actually pointing at an object whose alignment is tighter. */
515
516unsigned int
517get_pointer_alignment (tree exp)
518{
519 unsigned HOST_WIDE_INT bitpos = 0;
520 unsigned int align;
521
522 align = get_pointer_alignment_1 (exp, &bitpos);
523
524 /* align and bitpos now specify known low bits of the pointer.
525 ptr & (align - 1) == bitpos. */
526
527 if (bitpos != 0)
528 align = (bitpos & -bitpos);
529
530 return align;
531}
532
53800dbe 533/* Compute the length of a C string. TREE_STRING_LENGTH is not the right
534 way, because it could contain a zero byte in the middle.
535 TREE_STRING_LENGTH is the size of the character array, not the string.
536
4172d65e 537 ONLY_VALUE should be nonzero if the result is not going to be emitted
c09841f6 538 into the instruction stream and zero if it is going to be expanded.
4172d65e 539 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
681fab1e 540 is returned, otherwise NULL, since
541 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
542 evaluate the side-effects.
543
902de8ed 544 The value returned is of type `ssizetype'.
545
53800dbe 546 Unfortunately, string_constant can't access the values of const char
547 arrays with initializers, so neither can we do so here. */
548
4ee9c684 549tree
681fab1e 550c_strlen (tree src, int only_value)
53800dbe 551{
552 tree offset_node;
27d0c333 553 HOST_WIDE_INT offset;
554 int max;
44acf429 555 const char *ptr;
da136652 556 location_t loc;
53800dbe 557
681fab1e 558 STRIP_NOPS (src);
559 if (TREE_CODE (src) == COND_EXPR
560 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
561 {
562 tree len1, len2;
563
564 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
565 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
0862b7e9 566 if (tree_int_cst_equal (len1, len2))
681fab1e 567 return len1;
568 }
569
570 if (TREE_CODE (src) == COMPOUND_EXPR
571 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
572 return c_strlen (TREE_OPERAND (src, 1), only_value);
573
8637f1db 574 loc = EXPR_LOC_OR_HERE (src);
da136652 575
53800dbe 576 src = string_constant (src, &offset_node);
577 if (src == 0)
c2f47e15 578 return NULL_TREE;
902de8ed 579
83d79705 580 max = TREE_STRING_LENGTH (src) - 1;
53800dbe 581 ptr = TREE_STRING_POINTER (src);
902de8ed 582
53800dbe 583 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
584 {
585 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
586 compute the offset to the following null if we don't know where to
587 start searching for it. */
588 int i;
902de8ed 589
53800dbe 590 for (i = 0; i < max; i++)
591 if (ptr[i] == 0)
c2f47e15 592 return NULL_TREE;
902de8ed 593
53800dbe 594 /* We don't know the starting offset, but we do know that the string
595 has no internal zero bytes. We can assume that the offset falls
596 within the bounds of the string; otherwise, the programmer deserves
597 what he gets. Subtract the offset from the length of the string,
902de8ed 598 and return that. This would perhaps not be valid if we were dealing
599 with named arrays in addition to literal string constants. */
600
da136652 601 return size_diffop_loc (loc, size_int (max), offset_node);
53800dbe 602 }
603
604 /* We have a known offset into the string. Start searching there for
27d0c333 605 a null character if we can represent it as a single HOST_WIDE_INT. */
dabc4084 606 if (offset_node == 0)
53800dbe 607 offset = 0;
dabc4084 608 else if (! host_integerp (offset_node, 0))
609 offset = -1;
53800dbe 610 else
27d0c333 611 offset = tree_low_cst (offset_node, 0);
902de8ed 612
1f63a7d6 613 /* If the offset is known to be out of bounds, warn, and call strlen at
614 runtime. */
53800dbe 615 if (offset < 0 || offset > max)
616 {
1f63a7d6 617 /* Suppress multiple warnings for propagated constant strings. */
618 if (! TREE_NO_WARNING (src))
619 {
da136652 620 warning_at (loc, 0, "offset outside bounds of constant string");
1f63a7d6 621 TREE_NO_WARNING (src) = 1;
622 }
c2f47e15 623 return NULL_TREE;
53800dbe 624 }
902de8ed 625
53800dbe 626 /* Use strlen to search for the first zero byte. Since any strings
627 constructed with build_string will have nulls appended, we win even
628 if we get handed something like (char[4])"abcd".
629
630 Since OFFSET is our starting index into the string, no further
631 calculation is needed. */
902de8ed 632 return ssize_int (strlen (ptr + offset));
53800dbe 633}
634
83d79705 635/* Return a char pointer for a C string if it is a string constant
636 or sum of string constant and integer constant. */
637
638static const char *
aecda0d6 639c_getstr (tree src)
83d79705 640{
641 tree offset_node;
83d79705 642
643 src = string_constant (src, &offset_node);
644 if (src == 0)
645 return 0;
646
8c85fcb7 647 if (offset_node == 0)
648 return TREE_STRING_POINTER (src);
649 else if (!host_integerp (offset_node, 1)
650 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
83d79705 651 return 0;
83d79705 652
8c85fcb7 653 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
83d79705 654}
655
8c85fcb7 656/* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
657 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
ecc318ff 658
6840589f 659static rtx
aecda0d6 660c_readstr (const char *str, enum machine_mode mode)
6840589f 661{
662 HOST_WIDE_INT c[2];
663 HOST_WIDE_INT ch;
664 unsigned int i, j;
665
64db345d 666 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7d3f6cc7 667
6840589f 668 c[0] = 0;
669 c[1] = 0;
670 ch = 1;
671 for (i = 0; i < GET_MODE_SIZE (mode); i++)
672 {
673 j = i;
674 if (WORDS_BIG_ENDIAN)
675 j = GET_MODE_SIZE (mode) - i - 1;
676 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
ad8f8e52 677 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
6840589f 678 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
679 j *= BITS_PER_UNIT;
e212f99a 680 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
7d3f6cc7 681
6840589f 682 if (ch)
683 ch = (unsigned char) str[i];
684 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
685 }
686 return immed_double_const (c[0], c[1], mode);
687}
688
ecc318ff 689/* Cast a target constant CST to target CHAR and if that value fits into
5206b159 690 host char type, return zero and put that value into variable pointed to by
ecc318ff 691 P. */
692
693static int
aecda0d6 694target_char_cast (tree cst, char *p)
ecc318ff 695{
696 unsigned HOST_WIDE_INT val, hostval;
697
c19686c5 698 if (TREE_CODE (cst) != INTEGER_CST
ecc318ff 699 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
700 return 1;
701
c19686c5 702 val = TREE_INT_CST_LOW (cst);
ecc318ff 703 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
704 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
705
706 hostval = val;
707 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
708 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
709
710 if (val != hostval)
711 return 1;
712
713 *p = hostval;
714 return 0;
715}
716
4ee9c684 717/* Similar to save_expr, but assumes that arbitrary code is not executed
718 in between the multiple evaluations. In particular, we assume that a
719 non-addressable local variable will not be modified. */
720
721static tree
722builtin_save_expr (tree exp)
723{
f6c35aa4 724 if (TREE_CODE (exp) == SSA_NAME
725 || (TREE_ADDRESSABLE (exp) == 0
726 && (TREE_CODE (exp) == PARM_DECL
727 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
4ee9c684 728 return exp;
729
730 return save_expr (exp);
731}
732
53800dbe 733/* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
734 times to get the address of either a higher stack frame, or a return
735 address located within it (depending on FNDECL_CODE). */
902de8ed 736
c626df3d 737static rtx
869d0ef0 738expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
53800dbe 739{
740 int i;
741
869d0ef0 742#ifdef INITIAL_FRAME_ADDRESS_RTX
743 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
744#else
e3e15c50 745 rtx tem;
746
1b74fde7 747 /* For a zero count with __builtin_return_address, we don't care what
748 frame address we return, because target-specific definitions will
749 override us. Therefore frame pointer elimination is OK, and using
750 the soft frame pointer is OK.
751
fa7637bd 752 For a nonzero count, or a zero count with __builtin_frame_address,
1b74fde7 753 we require a stable offset from the current frame pointer to the
754 previous one, so we must use the hard frame pointer, and
e3e15c50 755 we must disable frame pointer elimination. */
1b74fde7 756 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
e3e15c50 757 tem = frame_pointer_rtx;
a0c938f0 758 else
e3e15c50 759 {
760 tem = hard_frame_pointer_rtx;
761
762 /* Tell reload not to eliminate the frame pointer. */
18d50ae6 763 crtl->accesses_prior_frames = 1;
e3e15c50 764 }
869d0ef0 765#endif
766
53800dbe 767 /* Some machines need special handling before we can access
3a69c60c 768 arbitrary frames. For example, on the SPARC, we must first flush
53800dbe 769 all register windows to the stack. */
770#ifdef SETUP_FRAME_ADDRESSES
771 if (count > 0)
772 SETUP_FRAME_ADDRESSES ();
773#endif
774
3a69c60c 775 /* On the SPARC, the return address is not in the frame, it is in a
53800dbe 776 register. There is no way to access it off of the current frame
777 pointer, but it can be accessed off the previous frame pointer by
778 reading the value from the register window save area. */
779#ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
780 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
781 count--;
782#endif
783
784 /* Scan back COUNT frames to the specified frame. */
785 for (i = 0; i < count; i++)
786 {
787 /* Assume the dynamic chain pointer is in the word that the
788 frame address points to, unless otherwise specified. */
789#ifdef DYNAMIC_CHAIN_ADDRESS
790 tem = DYNAMIC_CHAIN_ADDRESS (tem);
791#endif
792 tem = memory_address (Pmode, tem);
00060fc2 793 tem = gen_frame_mem (Pmode, tem);
83fc1478 794 tem = copy_to_reg (tem);
53800dbe 795 }
796
3a69c60c 797 /* For __builtin_frame_address, return what we've got. But, on
798 the SPARC for example, we may have to add a bias. */
53800dbe 799 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
3a69c60c 800#ifdef FRAME_ADDR_RTX
801 return FRAME_ADDR_RTX (tem);
802#else
53800dbe 803 return tem;
3a69c60c 804#endif
53800dbe 805
3a69c60c 806 /* For __builtin_return_address, get the return address from that frame. */
53800dbe 807#ifdef RETURN_ADDR_RTX
808 tem = RETURN_ADDR_RTX (count, tem);
809#else
810 tem = memory_address (Pmode,
811 plus_constant (tem, GET_MODE_SIZE (Pmode)));
00060fc2 812 tem = gen_frame_mem (Pmode, tem);
53800dbe 813#endif
814 return tem;
815}
816
f7c44134 817/* Alias set used for setjmp buffer. */
32c2fdea 818static alias_set_type setjmp_alias_set = -1;
f7c44134 819
6b7f6858 820/* Construct the leading half of a __builtin_setjmp call. Control will
2c8a1497 821 return to RECEIVER_LABEL. This is also called directly by the SJLJ
822 exception handling code. */
53800dbe 823
6b7f6858 824void
aecda0d6 825expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
53800dbe 826{
53800dbe 827 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
53800dbe 828 rtx stack_save;
f7c44134 829 rtx mem;
53800dbe 830
f7c44134 831 if (setjmp_alias_set == -1)
832 setjmp_alias_set = new_alias_set ();
833
85d654dd 834 buf_addr = convert_memory_address (Pmode, buf_addr);
53800dbe 835
37ae8504 836 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
53800dbe 837
6b7f6858 838 /* We store the frame pointer and the address of receiver_label in
839 the buffer and use the rest of it for the stack save area, which
840 is machine-dependent. */
53800dbe 841
f7c44134 842 mem = gen_rtx_MEM (Pmode, buf_addr);
ab6ab77e 843 set_mem_alias_set (mem, setjmp_alias_set);
e3e026e8 844 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
f7c44134 845
846 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
ab6ab77e 847 set_mem_alias_set (mem, setjmp_alias_set);
f7c44134 848
849 emit_move_insn (validize_mem (mem),
6b7f6858 850 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
53800dbe 851
852 stack_save = gen_rtx_MEM (sa_mode,
853 plus_constant (buf_addr,
854 2 * GET_MODE_SIZE (Pmode)));
ab6ab77e 855 set_mem_alias_set (stack_save, setjmp_alias_set);
e9c97615 856 emit_stack_save (SAVE_NONLOCAL, &stack_save);
53800dbe 857
858 /* If there is further processing to do, do it. */
859#ifdef HAVE_builtin_setjmp_setup
860 if (HAVE_builtin_setjmp_setup)
861 emit_insn (gen_builtin_setjmp_setup (buf_addr));
862#endif
863
29f09705 864 /* We have a nonlocal label. */
18d50ae6 865 cfun->has_nonlocal_label = 1;
6b7f6858 866}
53800dbe 867
2c8a1497 868/* Construct the trailing part of a __builtin_setjmp call. This is
869 also called directly by the SJLJ exception handling code. */
6b7f6858 870
871void
aecda0d6 872expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
6b7f6858 873{
82c7907c 874 rtx chain;
875
53800dbe 876 /* Clobber the FP when we get here, so we have to make sure it's
877 marked as used by this function. */
18b42941 878 emit_use (hard_frame_pointer_rtx);
53800dbe 879
880 /* Mark the static chain as clobbered here so life information
881 doesn't get messed up for it. */
82c7907c 882 chain = targetm.calls.static_chain (current_function_decl, true);
883 if (chain && REG_P (chain))
884 emit_clobber (chain);
53800dbe 885
886 /* Now put in the code to restore the frame pointer, and argument
491e04ef 887 pointer, if needed. */
53800dbe 888#ifdef HAVE_nonlocal_goto
889 if (! HAVE_nonlocal_goto)
890#endif
5a1c3f40 891 {
892 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
893 /* This might change the hard frame pointer in ways that aren't
894 apparent to early optimization passes, so force a clobber. */
18b42941 895 emit_clobber (hard_frame_pointer_rtx);
5a1c3f40 896 }
53800dbe 897
5ae82d58 898#if !HARD_FRAME_POINTER_IS_ARG_POINTER
53800dbe 899 if (fixed_regs[ARG_POINTER_REGNUM])
900 {
901#ifdef ELIMINABLE_REGS
902 size_t i;
e99c3a1d 903 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
53800dbe 904
3098b2d3 905 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
53800dbe 906 if (elim_regs[i].from == ARG_POINTER_REGNUM
907 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
908 break;
909
3098b2d3 910 if (i == ARRAY_SIZE (elim_regs))
53800dbe 911#endif
912 {
913 /* Now restore our arg pointer from the address at which it
05927e40 914 was saved in our stack frame. */
27a7a23a 915 emit_move_insn (crtl->args.internal_arg_pointer,
b079a207 916 copy_to_reg (get_arg_pointer_save_area ()));
53800dbe 917 }
918 }
919#endif
920
921#ifdef HAVE_builtin_setjmp_receiver
922 if (HAVE_builtin_setjmp_receiver)
6b7f6858 923 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
53800dbe 924 else
925#endif
926#ifdef HAVE_nonlocal_goto_receiver
927 if (HAVE_nonlocal_goto_receiver)
928 emit_insn (gen_nonlocal_goto_receiver ());
929 else
930#endif
6b7f6858 931 { /* Nothing */ }
57f6bb94 932
3072d30e 933 /* We must not allow the code we just generated to be reordered by
934 scheduling. Specifically, the update of the frame pointer must
935 happen immediately, not later. */
936 emit_insn (gen_blockage ());
6b7f6858 937}
53800dbe 938
53800dbe 939/* __builtin_longjmp is passed a pointer to an array of five words (not
940 all will be used on all machines). It operates similarly to the C
941 library function of the same name, but is more efficient. Much of
2c8a1497 942 the code below is copied from the handling of non-local gotos. */
53800dbe 943
c626df3d 944static void
aecda0d6 945expand_builtin_longjmp (rtx buf_addr, rtx value)
53800dbe 946{
4712c7d6 947 rtx fp, lab, stack, insn, last;
53800dbe 948 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
949
48e1416a 950 /* DRAP is needed for stack realign if longjmp is expanded to current
27a7a23a 951 function */
952 if (SUPPORTS_STACK_ALIGNMENT)
953 crtl->need_drap = true;
954
f7c44134 955 if (setjmp_alias_set == -1)
956 setjmp_alias_set = new_alias_set ();
957
85d654dd 958 buf_addr = convert_memory_address (Pmode, buf_addr);
479e4d5e 959
53800dbe 960 buf_addr = force_reg (Pmode, buf_addr);
961
82c7907c 962 /* We require that the user must pass a second argument of 1, because
963 that is what builtin_setjmp will return. */
64db345d 964 gcc_assert (value == const1_rtx);
53800dbe 965
4712c7d6 966 last = get_last_insn ();
53800dbe 967#ifdef HAVE_builtin_longjmp
968 if (HAVE_builtin_longjmp)
969 emit_insn (gen_builtin_longjmp (buf_addr));
970 else
971#endif
972 {
973 fp = gen_rtx_MEM (Pmode, buf_addr);
974 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
975 GET_MODE_SIZE (Pmode)));
976
977 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
978 2 * GET_MODE_SIZE (Pmode)));
ab6ab77e 979 set_mem_alias_set (fp, setjmp_alias_set);
980 set_mem_alias_set (lab, setjmp_alias_set);
981 set_mem_alias_set (stack, setjmp_alias_set);
53800dbe 982
983 /* Pick up FP, label, and SP from the block and jump. This code is
984 from expand_goto in stmt.c; see there for detailed comments. */
03fd9d2c 985#ifdef HAVE_nonlocal_goto
53800dbe 986 if (HAVE_nonlocal_goto)
987 /* We have to pass a value to the nonlocal_goto pattern that will
988 get copied into the static_chain pointer, but it does not matter
989 what that value is, because builtin_setjmp does not use it. */
28d202a8 990 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
53800dbe 991 else
992#endif
993 {
994 lab = copy_to_reg (lab);
995
18b42941 996 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
997 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
2a871ad1 998
53800dbe 999 emit_move_insn (hard_frame_pointer_rtx, fp);
e9c97615 1000 emit_stack_restore (SAVE_NONLOCAL, stack);
53800dbe 1001
18b42941 1002 emit_use (hard_frame_pointer_rtx);
1003 emit_use (stack_pointer_rtx);
53800dbe 1004 emit_indirect_jump (lab);
1005 }
1006 }
615166bb 1007
1008 /* Search backwards and mark the jump insn as a non-local goto.
1009 Note that this precludes the use of __builtin_longjmp to a
1010 __builtin_setjmp target in the same function. However, we've
1011 already cautioned the user that these functions are for
1012 internal exception handling use only. */
449c0509 1013 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1014 {
64db345d 1015 gcc_assert (insn != last);
7d3f6cc7 1016
6d7dc5b9 1017 if (JUMP_P (insn))
449c0509 1018 {
a1ddb869 1019 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
449c0509 1020 break;
1021 }
6d7dc5b9 1022 else if (CALL_P (insn))
9342ee68 1023 break;
449c0509 1024 }
53800dbe 1025}
1026
4ee9c684 1027/* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1028 and the address of the save area. */
1029
1030static rtx
c2f47e15 1031expand_builtin_nonlocal_goto (tree exp)
4ee9c684 1032{
1033 tree t_label, t_save_area;
1034 rtx r_label, r_save_area, r_fp, r_sp, insn;
1035
c2f47e15 1036 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4ee9c684 1037 return NULL_RTX;
1038
c2f47e15 1039 t_label = CALL_EXPR_ARG (exp, 0);
1040 t_save_area = CALL_EXPR_ARG (exp, 1);
4ee9c684 1041
8ec3c5c2 1042 r_label = expand_normal (t_label);
3dce56cc 1043 r_label = convert_memory_address (Pmode, r_label);
8ec3c5c2 1044 r_save_area = expand_normal (t_save_area);
3dce56cc 1045 r_save_area = convert_memory_address (Pmode, r_save_area);
d1ff492e 1046 /* Copy the address of the save location to a register just in case it was
1047 based on the frame pointer. */
51adbc8a 1048 r_save_area = copy_to_reg (r_save_area);
4ee9c684 1049 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1050 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1051 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1052
18d50ae6 1053 crtl->has_nonlocal_goto = 1;
4ee9c684 1054
03fd9d2c 1055#ifdef HAVE_nonlocal_goto
4ee9c684 1056 /* ??? We no longer need to pass the static chain value, afaik. */
1057 if (HAVE_nonlocal_goto)
1058 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1059 else
1060#endif
1061 {
1062 r_label = copy_to_reg (r_label);
1063
18b42941 1064 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1065 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
491e04ef 1066
d1ff492e 1067 /* Restore frame pointer for containing function. */
4ee9c684 1068 emit_move_insn (hard_frame_pointer_rtx, r_fp);
e9c97615 1069 emit_stack_restore (SAVE_NONLOCAL, r_sp);
491e04ef 1070
4ee9c684 1071 /* USE of hard_frame_pointer_rtx added for consistency;
1072 not clear if really needed. */
18b42941 1073 emit_use (hard_frame_pointer_rtx);
1074 emit_use (stack_pointer_rtx);
ad0d0af8 1075
1076 /* If the architecture is using a GP register, we must
1077 conservatively assume that the target function makes use of it.
1078 The prologue of functions with nonlocal gotos must therefore
1079 initialize the GP register to the appropriate value, and we
1080 must then make sure that this value is live at the point
1081 of the jump. (Note that this doesn't necessarily apply
1082 to targets with a nonlocal_goto pattern; they are free
1083 to implement it in their own way. Note also that this is
1084 a no-op if the GP register is a global invariant.) */
1085 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1086 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
18b42941 1087 emit_use (pic_offset_table_rtx);
ad0d0af8 1088
4ee9c684 1089 emit_indirect_jump (r_label);
1090 }
491e04ef 1091
4ee9c684 1092 /* Search backwards to the jump insn and mark it as a
1093 non-local goto. */
1094 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1095 {
6d7dc5b9 1096 if (JUMP_P (insn))
4ee9c684 1097 {
a1ddb869 1098 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
4ee9c684 1099 break;
1100 }
6d7dc5b9 1101 else if (CALL_P (insn))
4ee9c684 1102 break;
1103 }
1104
1105 return const0_rtx;
1106}
1107
843d08a9 1108/* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1109 (not all will be used on all machines) that was passed to __builtin_setjmp.
1110 It updates the stack pointer in that block to correspond to the current
1111 stack pointer. */
1112
1113static void
1114expand_builtin_update_setjmp_buf (rtx buf_addr)
1115{
d1ff492e 1116 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1117 rtx stack_save
843d08a9 1118 = gen_rtx_MEM (sa_mode,
1119 memory_address
1120 (sa_mode,
1121 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1122
e9c97615 1123 emit_stack_save (SAVE_NONLOCAL, &stack_save);
843d08a9 1124}
1125
5e3608d8 1126/* Expand a call to __builtin_prefetch. For a target that does not support
1127 data prefetch, evaluate the memory address argument in case it has side
1128 effects. */
1129
1130static void
c2f47e15 1131expand_builtin_prefetch (tree exp)
5e3608d8 1132{
1133 tree arg0, arg1, arg2;
c2f47e15 1134 int nargs;
5e3608d8 1135 rtx op0, op1, op2;
1136
c2f47e15 1137 if (!validate_arglist (exp, POINTER_TYPE, 0))
26a5cadb 1138 return;
1139
c2f47e15 1140 arg0 = CALL_EXPR_ARG (exp, 0);
1141
26a5cadb 1142 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1143 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1144 locality). */
c2f47e15 1145 nargs = call_expr_nargs (exp);
1146 if (nargs > 1)
1147 arg1 = CALL_EXPR_ARG (exp, 1);
26a5cadb 1148 else
c2f47e15 1149 arg1 = integer_zero_node;
1150 if (nargs > 2)
1151 arg2 = CALL_EXPR_ARG (exp, 2);
1152 else
2512209b 1153 arg2 = integer_three_node;
5e3608d8 1154
1155 /* Argument 0 is an address. */
1156 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1157
1158 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1159 if (TREE_CODE (arg1) != INTEGER_CST)
1160 {
07e3a3d2 1161 error ("second argument to %<__builtin_prefetch%> must be a constant");
9342ee68 1162 arg1 = integer_zero_node;
5e3608d8 1163 }
8ec3c5c2 1164 op1 = expand_normal (arg1);
5e3608d8 1165 /* Argument 1 must be either zero or one. */
1166 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1167 {
c3ceba8e 1168 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
07e3a3d2 1169 " using zero");
5e3608d8 1170 op1 = const0_rtx;
1171 }
1172
1173 /* Argument 2 (locality) must be a compile-time constant int. */
1174 if (TREE_CODE (arg2) != INTEGER_CST)
1175 {
07e3a3d2 1176 error ("third argument to %<__builtin_prefetch%> must be a constant");
5e3608d8 1177 arg2 = integer_zero_node;
1178 }
8ec3c5c2 1179 op2 = expand_normal (arg2);
5e3608d8 1180 /* Argument 2 must be 0, 1, 2, or 3. */
1181 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1182 {
c3ceba8e 1183 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
5e3608d8 1184 op2 = const0_rtx;
1185 }
1186
1187#ifdef HAVE_prefetch
1188 if (HAVE_prefetch)
1189 {
8786db1e 1190 struct expand_operand ops[3];
1191
1192 create_address_operand (&ops[0], op0);
1193 create_integer_operand (&ops[1], INTVAL (op1));
1194 create_integer_operand (&ops[2], INTVAL (op2));
1195 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1196 return;
5e3608d8 1197 }
5e3608d8 1198#endif
0a534ba7 1199
f0ce3b1f 1200 /* Don't do anything with direct references to volatile memory, but
1201 generate code to handle other side effects. */
e16ceb8e 1202 if (!MEM_P (op0) && side_effects_p (op0))
f0ce3b1f 1203 emit_insn (op0);
5e3608d8 1204}
1205
f7c44134 1206/* Get a MEM rtx for expression EXP which is the address of an operand
d8ae1baa 1207 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1208 the maximum length of the block of memory that might be accessed or
1209 NULL if unknown. */
f7c44134 1210
53800dbe 1211static rtx
d8ae1baa 1212get_memory_rtx (tree exp, tree len)
53800dbe 1213{
ad0a178f 1214 tree orig_exp = exp;
1215 rtx addr, mem;
1216 HOST_WIDE_INT off;
1217
1218 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1219 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1220 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1221 exp = TREE_OPERAND (exp, 0);
1222
1223 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1224 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
2a631e19 1225
f7c44134 1226 /* Get an expression we can use to find the attributes to assign to MEM.
1227 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1228 we can. First remove any nops. */
72dd6141 1229 while (CONVERT_EXPR_P (exp)
f7c44134 1230 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1231 exp = TREE_OPERAND (exp, 0);
1232
ad0a178f 1233 off = 0;
1234 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1235 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1236 && host_integerp (TREE_OPERAND (exp, 1), 0)
1237 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1238 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1239 else if (TREE_CODE (exp) == ADDR_EXPR)
a1a25d19 1240 exp = TREE_OPERAND (exp, 0);
f7c44134 1241 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
a1a25d19 1242 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1243 else
1244 exp = NULL;
1245
1246 /* Honor attributes derived from exp, except for the alias set
1247 (as builtin stringops may alias with anything) and the size
1248 (as stringops may access multiple array elements). */
1249 if (exp)
eec8e941 1250 {
a1a25d19 1251 set_mem_attributes (mem, exp, 0);
d8ae1baa 1252
ad0a178f 1253 if (off)
1254 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1255
d8ae1baa 1256 /* Allow the string and memory builtins to overflow from one
1257 field into another, see http://gcc.gnu.org/PR23561.
1258 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1259 memory accessed by the string or memory builtin will fit
1260 within the field. */
1261 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1262 {
1263 tree mem_expr = MEM_EXPR (mem);
1264 HOST_WIDE_INT offset = -1, length = -1;
1265 tree inner = exp;
1266
1267 while (TREE_CODE (inner) == ARRAY_REF
72dd6141 1268 || CONVERT_EXPR_P (inner)
d8ae1baa 1269 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1270 || TREE_CODE (inner) == SAVE_EXPR)
1271 inner = TREE_OPERAND (inner, 0);
1272
1273 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1274
da443c27 1275 if (MEM_OFFSET_KNOWN_P (mem))
1276 offset = MEM_OFFSET (mem);
d8ae1baa 1277
1278 if (offset >= 0 && len && host_integerp (len, 0))
1279 length = tree_low_cst (len, 0);
1280
1281 while (TREE_CODE (inner) == COMPONENT_REF)
1282 {
1283 tree field = TREE_OPERAND (inner, 1);
d8ae1baa 1284 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1285 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1286
6933ebec 1287 /* Bitfields are generally not byte-addressable. */
1288 gcc_assert (!DECL_BIT_FIELD (field)
1289 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1290 % BITS_PER_UNIT) == 0
1291 && host_integerp (DECL_SIZE (field), 0)
1292 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1293 % BITS_PER_UNIT) == 0));
1294
b2f0b2a3 1295 /* If we can prove that the memory starting at XEXP (mem, 0) and
1296 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1297 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1298 fields without DECL_SIZE_UNIT like flexible array members. */
d8ae1baa 1299 if (length >= 0
b2f0b2a3 1300 && DECL_SIZE_UNIT (field)
6933ebec 1301 && host_integerp (DECL_SIZE_UNIT (field), 0))
d8ae1baa 1302 {
1303 HOST_WIDE_INT size
6933ebec 1304 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
d8ae1baa 1305 if (offset <= size
1306 && length <= size
1307 && offset + length <= size)
1308 break;
1309 }
1310
1311 if (offset >= 0
1312 && host_integerp (DECL_FIELD_OFFSET (field), 0))
6933ebec 1313 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
d8ae1baa 1314 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1315 / BITS_PER_UNIT;
1316 else
1317 {
1318 offset = -1;
1319 length = -1;
1320 }
1321
1322 mem_expr = TREE_OPERAND (mem_expr, 0);
1323 inner = TREE_OPERAND (inner, 0);
d8ae1baa 1324 }
1325
1326 if (mem_expr == NULL)
1327 offset = -1;
1328 if (mem_expr != MEM_EXPR (mem))
1329 {
1330 set_mem_expr (mem, mem_expr);
da443c27 1331 if (offset >= 0)
1332 set_mem_offset (mem, offset);
1333 else
1334 clear_mem_offset (mem);
d8ae1baa 1335 }
1336 }
eec8e941 1337 set_mem_alias_set (mem, 0);
5b2a69fa 1338 clear_mem_size (mem);
eec8e941 1339 }
53800dbe 1340
53800dbe 1341 return mem;
1342}
1343\f
1344/* Built-in functions to perform an untyped call and return. */
1345
3b9c3a16 1346#define apply_args_mode \
1347 (this_target_builtins->x_apply_args_mode)
1348#define apply_result_mode \
1349 (this_target_builtins->x_apply_result_mode)
53800dbe 1350
53800dbe 1351/* Return the size required for the block returned by __builtin_apply_args,
1352 and initialize apply_args_mode. */
1353
1354static int
aecda0d6 1355apply_args_size (void)
53800dbe 1356{
1357 static int size = -1;
58e9ce8f 1358 int align;
1359 unsigned int regno;
53800dbe 1360 enum machine_mode mode;
1361
1362 /* The values computed by this function never change. */
1363 if (size < 0)
1364 {
1365 /* The first value is the incoming arg-pointer. */
1366 size = GET_MODE_SIZE (Pmode);
1367
1368 /* The second value is the structure value address unless this is
1369 passed as an "invisible" first argument. */
6812c89e 1370 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
53800dbe 1371 size += GET_MODE_SIZE (Pmode);
1372
1373 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1374 if (FUNCTION_ARG_REGNO_P (regno))
1375 {
4bac51c9 1376 mode = targetm.calls.get_raw_arg_mode (regno);
0862b7e9 1377
64db345d 1378 gcc_assert (mode != VOIDmode);
53800dbe 1379
1380 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1381 if (size % align != 0)
1382 size = CEIL (size, align) * align;
53800dbe 1383 size += GET_MODE_SIZE (mode);
1384 apply_args_mode[regno] = mode;
1385 }
1386 else
1387 {
1388 apply_args_mode[regno] = VOIDmode;
53800dbe 1389 }
1390 }
1391 return size;
1392}
1393
1394/* Return the size required for the block returned by __builtin_apply,
1395 and initialize apply_result_mode. */
1396
1397static int
aecda0d6 1398apply_result_size (void)
53800dbe 1399{
1400 static int size = -1;
1401 int align, regno;
1402 enum machine_mode mode;
1403
1404 /* The values computed by this function never change. */
1405 if (size < 0)
1406 {
1407 size = 0;
1408
1409 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
e1ce1485 1410 if (targetm.calls.function_value_regno_p (regno))
53800dbe 1411 {
4bac51c9 1412 mode = targetm.calls.get_raw_result_mode (regno);
0862b7e9 1413
64db345d 1414 gcc_assert (mode != VOIDmode);
53800dbe 1415
1416 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1417 if (size % align != 0)
1418 size = CEIL (size, align) * align;
1419 size += GET_MODE_SIZE (mode);
1420 apply_result_mode[regno] = mode;
1421 }
1422 else
1423 apply_result_mode[regno] = VOIDmode;
1424
1425 /* Allow targets that use untyped_call and untyped_return to override
1426 the size so that machine-specific information can be stored here. */
1427#ifdef APPLY_RESULT_SIZE
1428 size = APPLY_RESULT_SIZE;
1429#endif
1430 }
1431 return size;
1432}
1433
1434#if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1435/* Create a vector describing the result block RESULT. If SAVEP is true,
1436 the result block is used to save the values; otherwise it is used to
1437 restore the values. */
1438
1439static rtx
aecda0d6 1440result_vector (int savep, rtx result)
53800dbe 1441{
1442 int regno, size, align, nelts;
1443 enum machine_mode mode;
1444 rtx reg, mem;
364c0c59 1445 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
bf8e3599 1446
53800dbe 1447 size = nelts = 0;
1448 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1449 if ((mode = apply_result_mode[regno]) != VOIDmode)
1450 {
1451 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1452 if (size % align != 0)
1453 size = CEIL (size, align) * align;
1454 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
e513d163 1455 mem = adjust_address (result, mode, size);
53800dbe 1456 savevec[nelts++] = (savep
1457 ? gen_rtx_SET (VOIDmode, mem, reg)
1458 : gen_rtx_SET (VOIDmode, reg, mem));
1459 size += GET_MODE_SIZE (mode);
1460 }
1461 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1462}
1463#endif /* HAVE_untyped_call or HAVE_untyped_return */
1464
1465/* Save the state required to perform an untyped call with the same
1466 arguments as were passed to the current function. */
1467
1468static rtx
aecda0d6 1469expand_builtin_apply_args_1 (void)
53800dbe 1470{
1c7e61a7 1471 rtx registers, tem;
53800dbe 1472 int size, align, regno;
1473 enum machine_mode mode;
6812c89e 1474 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
53800dbe 1475
1476 /* Create a block where the arg-pointer, structure value address,
1477 and argument registers can be saved. */
1478 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1479
1480 /* Walk past the arg-pointer and structure value address. */
1481 size = GET_MODE_SIZE (Pmode);
6812c89e 1482 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
53800dbe 1483 size += GET_MODE_SIZE (Pmode);
1484
1485 /* Save each register used in calling a function to the block. */
1486 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1487 if ((mode = apply_args_mode[regno]) != VOIDmode)
1488 {
53800dbe 1489 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1490 if (size % align != 0)
1491 size = CEIL (size, align) * align;
1492
1493 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1494
e513d163 1495 emit_move_insn (adjust_address (registers, mode, size), tem);
53800dbe 1496 size += GET_MODE_SIZE (mode);
1497 }
1498
1499 /* Save the arg pointer to the block. */
27a7a23a 1500 tem = copy_to_reg (crtl->args.internal_arg_pointer);
f083a92b 1501#ifdef STACK_GROWS_DOWNWARD
1c7e61a7 1502 /* We need the pointer as the caller actually passed them to us, not
9d4b544c 1503 as we might have pretended they were passed. Make sure it's a valid
1504 operand, as emit_move_insn isn't expected to handle a PLUS. */
1505 tem
abe32cce 1506 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
9d4b544c 1507 NULL_RTX);
1c7e61a7 1508#endif
1509 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
0862b7e9 1510
53800dbe 1511 size = GET_MODE_SIZE (Pmode);
1512
1513 /* Save the structure value address unless this is passed as an
1514 "invisible" first argument. */
45550790 1515 if (struct_incoming_value)
53800dbe 1516 {
e513d163 1517 emit_move_insn (adjust_address (registers, Pmode, size),
45550790 1518 copy_to_reg (struct_incoming_value));
53800dbe 1519 size += GET_MODE_SIZE (Pmode);
1520 }
1521
1522 /* Return the address of the block. */
1523 return copy_addr_to_reg (XEXP (registers, 0));
1524}
1525
1526/* __builtin_apply_args returns block of memory allocated on
1527 the stack into which is stored the arg pointer, structure
1528 value address, static chain, and all the registers that might
1529 possibly be used in performing a function call. The code is
1530 moved to the start of the function so the incoming values are
1531 saved. */
27d0c333 1532
53800dbe 1533static rtx
aecda0d6 1534expand_builtin_apply_args (void)
53800dbe 1535{
1536 /* Don't do __builtin_apply_args more than once in a function.
1537 Save the result of the first call and reuse it. */
1538 if (apply_args_value != 0)
1539 return apply_args_value;
1540 {
1541 /* When this function is called, it means that registers must be
1542 saved on entry to this function. So we migrate the
1543 call to the first insn of this function. */
1544 rtx temp;
1545 rtx seq;
1546
1547 start_sequence ();
1548 temp = expand_builtin_apply_args_1 ();
1549 seq = get_insns ();
1550 end_sequence ();
1551
1552 apply_args_value = temp;
1553
31d3e01c 1554 /* Put the insns after the NOTE that starts the function.
1555 If this is inside a start_sequence, make the outer-level insn
53800dbe 1556 chain current, so the code is placed at the start of the
0ef1a651 1557 function. If internal_arg_pointer is a non-virtual pseudo,
1558 it needs to be placed after the function that initializes
1559 that pseudo. */
53800dbe 1560 push_topmost_sequence ();
0ef1a651 1561 if (REG_P (crtl->args.internal_arg_pointer)
1562 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1563 emit_insn_before (seq, parm_birth_insn);
1564 else
1565 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
53800dbe 1566 pop_topmost_sequence ();
1567 return temp;
1568 }
1569}
1570
1571/* Perform an untyped call and save the state required to perform an
1572 untyped return of whatever value was returned by the given function. */
1573
1574static rtx
aecda0d6 1575expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
53800dbe 1576{
1577 int size, align, regno;
1578 enum machine_mode mode;
2a631e19 1579 rtx incoming_args, result, reg, dest, src, call_insn;
53800dbe 1580 rtx old_stack_level = 0;
1581 rtx call_fusage = 0;
6812c89e 1582 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
53800dbe 1583
85d654dd 1584 arguments = convert_memory_address (Pmode, arguments);
726ec87c 1585
53800dbe 1586 /* Create a block where the return registers can be saved. */
1587 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1588
53800dbe 1589 /* Fetch the arg pointer from the ARGUMENTS block. */
1590 incoming_args = gen_reg_rtx (Pmode);
726ec87c 1591 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
53800dbe 1592#ifndef STACK_GROWS_DOWNWARD
ad99e708 1593 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1594 incoming_args, 0, OPTAB_LIB_WIDEN);
53800dbe 1595#endif
1596
04a46d40 1597 /* Push a new argument block and copy the arguments. Do not allow
1598 the (potential) memcpy call below to interfere with our stack
1599 manipulations. */
53800dbe 1600 do_pending_stack_adjust ();
04a46d40 1601 NO_DEFER_POP;
53800dbe 1602
2358393e 1603 /* Save the stack with nonlocal if available. */
53800dbe 1604#ifdef HAVE_save_stack_nonlocal
1605 if (HAVE_save_stack_nonlocal)
e9c97615 1606 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
53800dbe 1607 else
1608#endif
e9c97615 1609 emit_stack_save (SAVE_BLOCK, &old_stack_level);
53800dbe 1610
59647703 1611 /* Allocate a block of memory onto the stack and copy the memory
990495a7 1612 arguments to the outgoing arguments address. We can pass TRUE
1613 as the 4th argument because we just saved the stack pointer
1614 and will restore it right after the call. */
5be42b39 1615 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
27a7a23a 1616
1617 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1618 may have already set current_function_calls_alloca to true.
1619 current_function_calls_alloca won't be set if argsize is zero,
1620 so we have to guarantee need_drap is true here. */
1621 if (SUPPORTS_STACK_ALIGNMENT)
1622 crtl->need_drap = true;
1623
59647703 1624 dest = virtual_outgoing_args_rtx;
1625#ifndef STACK_GROWS_DOWNWARD
971ba038 1626 if (CONST_INT_P (argsize))
59647703 1627 dest = plus_constant (dest, -INTVAL (argsize));
1628 else
1629 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1630#endif
2a631e19 1631 dest = gen_rtx_MEM (BLKmode, dest);
1632 set_mem_align (dest, PARM_BOUNDARY);
1633 src = gen_rtx_MEM (BLKmode, incoming_args);
1634 set_mem_align (src, PARM_BOUNDARY);
0378dbdc 1635 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
53800dbe 1636
1637 /* Refer to the argument block. */
1638 apply_args_size ();
1639 arguments = gen_rtx_MEM (BLKmode, arguments);
2a631e19 1640 set_mem_align (arguments, PARM_BOUNDARY);
53800dbe 1641
1642 /* Walk past the arg-pointer and structure value address. */
1643 size = GET_MODE_SIZE (Pmode);
45550790 1644 if (struct_value)
53800dbe 1645 size += GET_MODE_SIZE (Pmode);
1646
1647 /* Restore each of the registers previously saved. Make USE insns
1648 for each of these registers for use in making the call. */
1649 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1650 if ((mode = apply_args_mode[regno]) != VOIDmode)
1651 {
1652 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1653 if (size % align != 0)
1654 size = CEIL (size, align) * align;
1655 reg = gen_rtx_REG (mode, regno);
e513d163 1656 emit_move_insn (reg, adjust_address (arguments, mode, size));
53800dbe 1657 use_reg (&call_fusage, reg);
1658 size += GET_MODE_SIZE (mode);
1659 }
1660
1661 /* Restore the structure value address unless this is passed as an
1662 "invisible" first argument. */
1663 size = GET_MODE_SIZE (Pmode);
45550790 1664 if (struct_value)
53800dbe 1665 {
1666 rtx value = gen_reg_rtx (Pmode);
e513d163 1667 emit_move_insn (value, adjust_address (arguments, Pmode, size));
45550790 1668 emit_move_insn (struct_value, value);
8ad4c111 1669 if (REG_P (struct_value))
45550790 1670 use_reg (&call_fusage, struct_value);
53800dbe 1671 size += GET_MODE_SIZE (Pmode);
1672 }
1673
1674 /* All arguments and registers used for the call are set up by now! */
82c7907c 1675 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
53800dbe 1676
1677 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1678 and we don't want to load it into a register as an optimization,
1679 because prepare_call_address already did it if it should be done. */
1680 if (GET_CODE (function) != SYMBOL_REF)
1681 function = memory_address (FUNCTION_MODE, function);
1682
1683 /* Generate the actual call instruction and save the return value. */
1684#ifdef HAVE_untyped_call
1685 if (HAVE_untyped_call)
1686 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1687 result, result_vector (1, result)));
1688 else
1689#endif
1690#ifdef HAVE_call_value
1691 if (HAVE_call_value)
1692 {
1693 rtx valreg = 0;
1694
1695 /* Locate the unique return register. It is not possible to
1696 express a call that sets more than one return register using
1697 call_value; use untyped_call for that. In fact, untyped_call
1698 only needs to save the return registers in the given block. */
1699 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1700 if ((mode = apply_result_mode[regno]) != VOIDmode)
1701 {
64db345d 1702 gcc_assert (!valreg); /* HAVE_untyped_call required. */
7d3f6cc7 1703
53800dbe 1704 valreg = gen_rtx_REG (mode, regno);
1705 }
1706
2ed6c343 1707 emit_call_insn (GEN_CALL_VALUE (valreg,
53800dbe 1708 gen_rtx_MEM (FUNCTION_MODE, function),
1709 const0_rtx, NULL_RTX, const0_rtx));
1710
e513d163 1711 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
53800dbe 1712 }
1713 else
1714#endif
64db345d 1715 gcc_unreachable ();
53800dbe 1716
d5f9786f 1717 /* Find the CALL insn we just emitted, and attach the register usage
1718 information. */
1719 call_insn = last_call_insn ();
1720 add_function_usage_to (call_insn, call_fusage);
53800dbe 1721
1722 /* Restore the stack. */
1723#ifdef HAVE_save_stack_nonlocal
1724 if (HAVE_save_stack_nonlocal)
e9c97615 1725 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
53800dbe 1726 else
1727#endif
e9c97615 1728 emit_stack_restore (SAVE_BLOCK, old_stack_level);
4bb7c660 1729 fixup_args_size_notes (call_insn, get_last_insn(), 0);
53800dbe 1730
04a46d40 1731 OK_DEFER_POP;
1732
53800dbe 1733 /* Return the address of the result block. */
85d654dd 1734 result = copy_addr_to_reg (XEXP (result, 0));
1735 return convert_memory_address (ptr_mode, result);
53800dbe 1736}
1737
1738/* Perform an untyped return. */
1739
1740static void
aecda0d6 1741expand_builtin_return (rtx result)
53800dbe 1742{
1743 int size, align, regno;
1744 enum machine_mode mode;
1745 rtx reg;
1746 rtx call_fusage = 0;
1747
85d654dd 1748 result = convert_memory_address (Pmode, result);
726ec87c 1749
53800dbe 1750 apply_result_size ();
1751 result = gen_rtx_MEM (BLKmode, result);
1752
1753#ifdef HAVE_untyped_return
1754 if (HAVE_untyped_return)
1755 {
1756 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1757 emit_barrier ();
1758 return;
1759 }
1760#endif
1761
1762 /* Restore the return value and note that each value is used. */
1763 size = 0;
1764 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1765 if ((mode = apply_result_mode[regno]) != VOIDmode)
1766 {
1767 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1768 if (size % align != 0)
1769 size = CEIL (size, align) * align;
1770 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
e513d163 1771 emit_move_insn (reg, adjust_address (result, mode, size));
53800dbe 1772
1773 push_to_sequence (call_fusage);
18b42941 1774 emit_use (reg);
53800dbe 1775 call_fusage = get_insns ();
1776 end_sequence ();
1777 size += GET_MODE_SIZE (mode);
1778 }
1779
1780 /* Put the USE insns before the return. */
31d3e01c 1781 emit_insn (call_fusage);
53800dbe 1782
1783 /* Return whatever values was restored by jumping directly to the end
1784 of the function. */
62380d2d 1785 expand_naked_return ();
53800dbe 1786}
1787
539a3a92 1788/* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
27d0c333 1789
539a3a92 1790static enum type_class
aecda0d6 1791type_to_class (tree type)
539a3a92 1792{
1793 switch (TREE_CODE (type))
1794 {
1795 case VOID_TYPE: return void_type_class;
1796 case INTEGER_TYPE: return integer_type_class;
539a3a92 1797 case ENUMERAL_TYPE: return enumeral_type_class;
1798 case BOOLEAN_TYPE: return boolean_type_class;
1799 case POINTER_TYPE: return pointer_type_class;
1800 case REFERENCE_TYPE: return reference_type_class;
1801 case OFFSET_TYPE: return offset_type_class;
1802 case REAL_TYPE: return real_type_class;
1803 case COMPLEX_TYPE: return complex_type_class;
1804 case FUNCTION_TYPE: return function_type_class;
1805 case METHOD_TYPE: return method_type_class;
1806 case RECORD_TYPE: return record_type_class;
1807 case UNION_TYPE:
1808 case QUAL_UNION_TYPE: return union_type_class;
1809 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1810 ? string_type_class : array_type_class);
539a3a92 1811 case LANG_TYPE: return lang_type_class;
1812 default: return no_type_class;
1813 }
1814}
bf8e3599 1815
c2f47e15 1816/* Expand a call EXP to __builtin_classify_type. */
27d0c333 1817
53800dbe 1818static rtx
c2f47e15 1819expand_builtin_classify_type (tree exp)
53800dbe 1820{
c2f47e15 1821 if (call_expr_nargs (exp))
1822 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
53800dbe 1823 return GEN_INT (no_type_class);
1824}
1825
07976da7 1826/* This helper macro, meant to be used in mathfn_built_in below,
1827 determines which among a set of three builtin math functions is
1828 appropriate for a given type mode. The `F' and `L' cases are
1829 automatically generated from the `double' case. */
1830#define CASE_MATHFN(BUILT_IN_MATHFN) \
1831 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1832 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1833 fcodel = BUILT_IN_MATHFN##L ; break;
cd2656b0 1834/* Similar to above, but appends _R after any F/L suffix. */
1835#define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1836 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1837 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1838 fcodel = BUILT_IN_MATHFN##L_R ; break;
07976da7 1839
b9a16870 1840/* Return mathematic function equivalent to FN but operating directly on TYPE,
1841 if available. If IMPLICIT is true use the implicit builtin declaration,
1842 otherwise use the explicit declaration. If we can't do the conversion,
1843 return zero. */
c319d56a 1844
1845static tree
b9a16870 1846mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
0a68165a 1847{
b9a16870 1848 enum built_in_function fcode, fcodef, fcodel, fcode2;
07976da7 1849
1850 switch (fn)
1851 {
746114e8 1852 CASE_MATHFN (BUILT_IN_ACOS)
1853 CASE_MATHFN (BUILT_IN_ACOSH)
1854 CASE_MATHFN (BUILT_IN_ASIN)
1855 CASE_MATHFN (BUILT_IN_ASINH)
07976da7 1856 CASE_MATHFN (BUILT_IN_ATAN)
746114e8 1857 CASE_MATHFN (BUILT_IN_ATAN2)
1858 CASE_MATHFN (BUILT_IN_ATANH)
1859 CASE_MATHFN (BUILT_IN_CBRT)
07976da7 1860 CASE_MATHFN (BUILT_IN_CEIL)
d735c391 1861 CASE_MATHFN (BUILT_IN_CEXPI)
746114e8 1862 CASE_MATHFN (BUILT_IN_COPYSIGN)
07976da7 1863 CASE_MATHFN (BUILT_IN_COS)
746114e8 1864 CASE_MATHFN (BUILT_IN_COSH)
1865 CASE_MATHFN (BUILT_IN_DREM)
1866 CASE_MATHFN (BUILT_IN_ERF)
1867 CASE_MATHFN (BUILT_IN_ERFC)
07976da7 1868 CASE_MATHFN (BUILT_IN_EXP)
746114e8 1869 CASE_MATHFN (BUILT_IN_EXP10)
1870 CASE_MATHFN (BUILT_IN_EXP2)
1871 CASE_MATHFN (BUILT_IN_EXPM1)
1872 CASE_MATHFN (BUILT_IN_FABS)
1873 CASE_MATHFN (BUILT_IN_FDIM)
07976da7 1874 CASE_MATHFN (BUILT_IN_FLOOR)
746114e8 1875 CASE_MATHFN (BUILT_IN_FMA)
1876 CASE_MATHFN (BUILT_IN_FMAX)
1877 CASE_MATHFN (BUILT_IN_FMIN)
1878 CASE_MATHFN (BUILT_IN_FMOD)
1879 CASE_MATHFN (BUILT_IN_FREXP)
1880 CASE_MATHFN (BUILT_IN_GAMMA)
cd2656b0 1881 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
746114e8 1882 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1883 CASE_MATHFN (BUILT_IN_HYPOT)
1884 CASE_MATHFN (BUILT_IN_ILOGB)
80ff6494 1885 CASE_MATHFN (BUILT_IN_ICEIL)
1886 CASE_MATHFN (BUILT_IN_IFLOOR)
746114e8 1887 CASE_MATHFN (BUILT_IN_INF)
80ff6494 1888 CASE_MATHFN (BUILT_IN_IRINT)
1889 CASE_MATHFN (BUILT_IN_IROUND)
69b779ea 1890 CASE_MATHFN (BUILT_IN_ISINF)
746114e8 1891 CASE_MATHFN (BUILT_IN_J0)
1892 CASE_MATHFN (BUILT_IN_J1)
1893 CASE_MATHFN (BUILT_IN_JN)
ac148751 1894 CASE_MATHFN (BUILT_IN_LCEIL)
746114e8 1895 CASE_MATHFN (BUILT_IN_LDEXP)
ad52b9b7 1896 CASE_MATHFN (BUILT_IN_LFLOOR)
746114e8 1897 CASE_MATHFN (BUILT_IN_LGAMMA)
cd2656b0 1898 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
ac148751 1899 CASE_MATHFN (BUILT_IN_LLCEIL)
ad52b9b7 1900 CASE_MATHFN (BUILT_IN_LLFLOOR)
746114e8 1901 CASE_MATHFN (BUILT_IN_LLRINT)
1902 CASE_MATHFN (BUILT_IN_LLROUND)
07976da7 1903 CASE_MATHFN (BUILT_IN_LOG)
746114e8 1904 CASE_MATHFN (BUILT_IN_LOG10)
1905 CASE_MATHFN (BUILT_IN_LOG1P)
1906 CASE_MATHFN (BUILT_IN_LOG2)
1907 CASE_MATHFN (BUILT_IN_LOGB)
1908 CASE_MATHFN (BUILT_IN_LRINT)
1909 CASE_MATHFN (BUILT_IN_LROUND)
1910 CASE_MATHFN (BUILT_IN_MODF)
1911 CASE_MATHFN (BUILT_IN_NAN)
1912 CASE_MATHFN (BUILT_IN_NANS)
07976da7 1913 CASE_MATHFN (BUILT_IN_NEARBYINT)
746114e8 1914 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1915 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1916 CASE_MATHFN (BUILT_IN_POW)
757c219d 1917 CASE_MATHFN (BUILT_IN_POWI)
746114e8 1918 CASE_MATHFN (BUILT_IN_POW10)
1919 CASE_MATHFN (BUILT_IN_REMAINDER)
1920 CASE_MATHFN (BUILT_IN_REMQUO)
1921 CASE_MATHFN (BUILT_IN_RINT)
07976da7 1922 CASE_MATHFN (BUILT_IN_ROUND)
746114e8 1923 CASE_MATHFN (BUILT_IN_SCALB)
1924 CASE_MATHFN (BUILT_IN_SCALBLN)
1925 CASE_MATHFN (BUILT_IN_SCALBN)
c319d56a 1926 CASE_MATHFN (BUILT_IN_SIGNBIT)
746114e8 1927 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
07976da7 1928 CASE_MATHFN (BUILT_IN_SIN)
746114e8 1929 CASE_MATHFN (BUILT_IN_SINCOS)
1930 CASE_MATHFN (BUILT_IN_SINH)
07976da7 1931 CASE_MATHFN (BUILT_IN_SQRT)
1932 CASE_MATHFN (BUILT_IN_TAN)
746114e8 1933 CASE_MATHFN (BUILT_IN_TANH)
1934 CASE_MATHFN (BUILT_IN_TGAMMA)
07976da7 1935 CASE_MATHFN (BUILT_IN_TRUNC)
746114e8 1936 CASE_MATHFN (BUILT_IN_Y0)
1937 CASE_MATHFN (BUILT_IN_Y1)
1938 CASE_MATHFN (BUILT_IN_YN)
07976da7 1939
0a68165a 1940 default:
c2f47e15 1941 return NULL_TREE;
0a68165a 1942 }
07976da7 1943
96b9f485 1944 if (TYPE_MAIN_VARIANT (type) == double_type_node)
b9a16870 1945 fcode2 = fcode;
96b9f485 1946 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
b9a16870 1947 fcode2 = fcodef;
96b9f485 1948 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
b9a16870 1949 fcode2 = fcodel;
07976da7 1950 else
c2f47e15 1951 return NULL_TREE;
b9a16870 1952
1953 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1954 return NULL_TREE;
1955
1956 return builtin_decl_explicit (fcode2);
0a68165a 1957}
1958
c319d56a 1959/* Like mathfn_built_in_1(), but always use the implicit array. */
1960
1961tree
1962mathfn_built_in (tree type, enum built_in_function fn)
1963{
1964 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1965}
1966
0fd605a5 1967/* If errno must be maintained, expand the RTL to check if the result,
1968 TARGET, of a built-in function call, EXP, is NaN, and if so set
1969 errno to EDOM. */
1970
1971static void
aecda0d6 1972expand_errno_check (tree exp, rtx target)
0fd605a5 1973{
7f05340e 1974 rtx lab = gen_label_rtx ();
0fd605a5 1975
7f05340e 1976 /* Test the result; if it is NaN, set errno=EDOM because
1977 the argument was not in the domain. */
3fcf767f 1978 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
79ab74cc 1979 NULL_RTX, NULL_RTX, lab,
1980 /* The jump is very likely. */
1981 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
0fd605a5 1982
1983#ifdef TARGET_EDOM
7f05340e 1984 /* If this built-in doesn't throw an exception, set errno directly. */
c2f47e15 1985 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
7f05340e 1986 {
0fd605a5 1987#ifdef GEN_ERRNO_RTX
7f05340e 1988 rtx errno_rtx = GEN_ERRNO_RTX;
0fd605a5 1989#else
7f05340e 1990 rtx errno_rtx
0fd605a5 1991 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1992#endif
7f05340e 1993 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
0fd605a5 1994 emit_label (lab);
7f05340e 1995 return;
0fd605a5 1996 }
7f05340e 1997#endif
1998
08491912 1999 /* Make sure the library call isn't expanded as a tail call. */
2000 CALL_EXPR_TAILCALL (exp) = 0;
2001
7f05340e 2002 /* We can't set errno=EDOM directly; let the library call do it.
2003 Pop the arguments right away in case the call gets deleted. */
2004 NO_DEFER_POP;
2005 expand_call (exp, target, 0);
2006 OK_DEFER_POP;
2007 emit_label (lab);
0fd605a5 2008}
2009
6b43bae4 2010/* Expand a call to one of the builtin math functions (sqrt, exp, or log).
c2f47e15 2011 Return NULL_RTX if a normal call should be emitted rather than expanding
2012 the function in-line. EXP is the expression that is a call to the builtin
53800dbe 2013 function; if convenient, the result should be placed in TARGET.
2014 SUBTARGET may be used as the target for computing one of EXP's operands. */
27d0c333 2015
53800dbe 2016static rtx
aecda0d6 2017expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
53800dbe 2018{
bf8e3599 2019 optab builtin_optab;
1e4adcfc 2020 rtx op0, insns;
c6e6ecb1 2021 tree fndecl = get_callee_fndecl (exp);
7f05340e 2022 enum machine_mode mode;
528ee710 2023 bool errno_set = false;
abfea505 2024 tree arg;
53800dbe 2025
c2f47e15 2026 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2027 return NULL_RTX;
53800dbe 2028
c2f47e15 2029 arg = CALL_EXPR_ARG (exp, 0);
53800dbe 2030
2031 switch (DECL_FUNCTION_CODE (fndecl))
2032 {
4f35b1fc 2033 CASE_FLT_FN (BUILT_IN_SQRT):
7f05340e 2034 errno_set = ! tree_expr_nonnegative_p (arg);
2035 builtin_optab = sqrt_optab;
2036 break;
4f35b1fc 2037 CASE_FLT_FN (BUILT_IN_EXP):
528ee710 2038 errno_set = true; builtin_optab = exp_optab; break;
4f35b1fc 2039 CASE_FLT_FN (BUILT_IN_EXP10):
2040 CASE_FLT_FN (BUILT_IN_POW10):
750ef9f5 2041 errno_set = true; builtin_optab = exp10_optab; break;
4f35b1fc 2042 CASE_FLT_FN (BUILT_IN_EXP2):
750ef9f5 2043 errno_set = true; builtin_optab = exp2_optab; break;
4f35b1fc 2044 CASE_FLT_FN (BUILT_IN_EXPM1):
a6b4eed2 2045 errno_set = true; builtin_optab = expm1_optab; break;
4f35b1fc 2046 CASE_FLT_FN (BUILT_IN_LOGB):
4efbc641 2047 errno_set = true; builtin_optab = logb_optab; break;
4f35b1fc 2048 CASE_FLT_FN (BUILT_IN_LOG):
528ee710 2049 errno_set = true; builtin_optab = log_optab; break;
4f35b1fc 2050 CASE_FLT_FN (BUILT_IN_LOG10):
d3cd9bde 2051 errno_set = true; builtin_optab = log10_optab; break;
4f35b1fc 2052 CASE_FLT_FN (BUILT_IN_LOG2):
d3cd9bde 2053 errno_set = true; builtin_optab = log2_optab; break;
4f35b1fc 2054 CASE_FLT_FN (BUILT_IN_LOG1P):
f474cd93 2055 errno_set = true; builtin_optab = log1p_optab; break;
4f35b1fc 2056 CASE_FLT_FN (BUILT_IN_ASIN):
8de2f465 2057 builtin_optab = asin_optab; break;
4f35b1fc 2058 CASE_FLT_FN (BUILT_IN_ACOS):
8de2f465 2059 builtin_optab = acos_optab; break;
4f35b1fc 2060 CASE_FLT_FN (BUILT_IN_TAN):
528ee710 2061 builtin_optab = tan_optab; break;
4f35b1fc 2062 CASE_FLT_FN (BUILT_IN_ATAN):
528ee710 2063 builtin_optab = atan_optab; break;
4f35b1fc 2064 CASE_FLT_FN (BUILT_IN_FLOOR):
528ee710 2065 builtin_optab = floor_optab; break;
4f35b1fc 2066 CASE_FLT_FN (BUILT_IN_CEIL):
528ee710 2067 builtin_optab = ceil_optab; break;
4f35b1fc 2068 CASE_FLT_FN (BUILT_IN_TRUNC):
a7cc195f 2069 builtin_optab = btrunc_optab; break;
4f35b1fc 2070 CASE_FLT_FN (BUILT_IN_ROUND):
528ee710 2071 builtin_optab = round_optab; break;
4f35b1fc 2072 CASE_FLT_FN (BUILT_IN_NEARBYINT):
0ddf4ad9 2073 builtin_optab = nearbyint_optab;
2074 if (flag_trapping_math)
2075 break;
2076 /* Else fallthrough and expand as rint. */
4f35b1fc 2077 CASE_FLT_FN (BUILT_IN_RINT):
aef94a0f 2078 builtin_optab = rint_optab; break;
b3154a1f 2079 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2080 builtin_optab = significand_optab; break;
42721db0 2081 default:
64db345d 2082 gcc_unreachable ();
53800dbe 2083 }
2084
7f05340e 2085 /* Make a suitable register to place result in. */
2086 mode = TYPE_MODE (TREE_TYPE (exp));
fc4eef90 2087
7f05340e 2088 if (! flag_errno_math || ! HONOR_NANS (mode))
2089 errno_set = false;
2090
bd421108 2091 /* Before working hard, check whether the instruction is available. */
f2aca212 2092 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2093 && (!errno_set || !optimize_insn_for_size_p ()))
68e6cb9d 2094 {
bd421108 2095 target = gen_reg_rtx (mode);
7f05340e 2096
bd421108 2097 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2098 need to expand the argument again. This way, we will not perform
2099 side-effects more the once. */
abfea505 2100 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
7f05340e 2101
1db6d067 2102 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
7f05340e 2103
bd421108 2104 start_sequence ();
53800dbe 2105
bd421108 2106 /* Compute into TARGET.
2107 Set TARGET to wherever the result comes back. */
2108 target = expand_unop (mode, builtin_optab, op0, target, 0);
2109
2110 if (target != 0)
2111 {
2112 if (errno_set)
2113 expand_errno_check (exp, target);
2114
2115 /* Output the entire sequence. */
2116 insns = get_insns ();
2117 end_sequence ();
2118 emit_insn (insns);
2119 return target;
2120 }
2121
2122 /* If we were unable to expand via the builtin, stop the sequence
2123 (without outputting the insns) and call to the library function
2124 with the stabilized argument list. */
53800dbe 2125 end_sequence ();
53800dbe 2126 }
2127
1e5b92fa 2128 return expand_call (exp, target, target == const0_rtx);
0fd605a5 2129}
2130
2131/* Expand a call to the builtin binary math functions (pow and atan2).
c2f47e15 2132 Return NULL_RTX if a normal call should be emitted rather than expanding the
0fd605a5 2133 function in-line. EXP is the expression that is a call to the builtin
2134 function; if convenient, the result should be placed in TARGET.
2135 SUBTARGET may be used as the target for computing one of EXP's
2136 operands. */
2137
2138static rtx
aecda0d6 2139expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
0fd605a5 2140{
2141 optab builtin_optab;
2142 rtx op0, op1, insns;
4737caf2 2143 int op1_type = REAL_TYPE;
c6e6ecb1 2144 tree fndecl = get_callee_fndecl (exp);
abfea505 2145 tree arg0, arg1;
7f05340e 2146 enum machine_mode mode;
0fd605a5 2147 bool errno_set = true;
0fd605a5 2148
73a954a1 2149 switch (DECL_FUNCTION_CODE (fndecl))
2150 {
2151 CASE_FLT_FN (BUILT_IN_SCALBN):
2152 CASE_FLT_FN (BUILT_IN_SCALBLN):
2153 CASE_FLT_FN (BUILT_IN_LDEXP):
2154 op1_type = INTEGER_TYPE;
2155 default:
2156 break;
2157 }
4737caf2 2158
c2f47e15 2159 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2160 return NULL_RTX;
0fd605a5 2161
c2f47e15 2162 arg0 = CALL_EXPR_ARG (exp, 0);
2163 arg1 = CALL_EXPR_ARG (exp, 1);
0fd605a5 2164
0fd605a5 2165 switch (DECL_FUNCTION_CODE (fndecl))
2166 {
4f35b1fc 2167 CASE_FLT_FN (BUILT_IN_POW):
0fd605a5 2168 builtin_optab = pow_optab; break;
4f35b1fc 2169 CASE_FLT_FN (BUILT_IN_ATAN2):
0fd605a5 2170 builtin_optab = atan2_optab; break;
73a954a1 2171 CASE_FLT_FN (BUILT_IN_SCALB):
2172 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2173 return 0;
2174 builtin_optab = scalb_optab; break;
2175 CASE_FLT_FN (BUILT_IN_SCALBN):
2176 CASE_FLT_FN (BUILT_IN_SCALBLN):
2177 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2178 return 0;
2179 /* Fall through... */
4f35b1fc 2180 CASE_FLT_FN (BUILT_IN_LDEXP):
4737caf2 2181 builtin_optab = ldexp_optab; break;
4f35b1fc 2182 CASE_FLT_FN (BUILT_IN_FMOD):
80ed5c06 2183 builtin_optab = fmod_optab; break;
ef722005 2184 CASE_FLT_FN (BUILT_IN_REMAINDER):
4f35b1fc 2185 CASE_FLT_FN (BUILT_IN_DREM):
ef722005 2186 builtin_optab = remainder_optab; break;
0fd605a5 2187 default:
64db345d 2188 gcc_unreachable ();
0fd605a5 2189 }
2190
7f05340e 2191 /* Make a suitable register to place result in. */
2192 mode = TYPE_MODE (TREE_TYPE (exp));
fc4eef90 2193
2194 /* Before working hard, check whether the instruction is available. */
d6bf3b14 2195 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
c2f47e15 2196 return NULL_RTX;
fc4eef90 2197
7f05340e 2198 target = gen_reg_rtx (mode);
2199
2200 if (! flag_errno_math || ! HONOR_NANS (mode))
2201 errno_set = false;
2202
f2aca212 2203 if (errno_set && optimize_insn_for_size_p ())
2204 return 0;
2205
4ee9c684 2206 /* Always stabilize the argument list. */
abfea505 2207 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2208 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
7f05340e 2209
8ec3c5c2 2210 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2211 op1 = expand_normal (arg1);
7f05340e 2212
7f05340e 2213 start_sequence ();
2214
0fd605a5 2215 /* Compute into TARGET.
2216 Set TARGET to wherever the result comes back. */
7f05340e 2217 target = expand_binop (mode, builtin_optab, op0, op1,
0fd605a5 2218 target, 0, OPTAB_DIRECT);
53800dbe 2219
68e6cb9d 2220 /* If we were unable to expand via the builtin, stop the sequence
2221 (without outputting the insns) and call to the library function
2222 with the stabilized argument list. */
0fd605a5 2223 if (target == 0)
2224 {
2225 end_sequence ();
68e6cb9d 2226 return expand_call (exp, target, target == const0_rtx);
53800dbe 2227 }
2228
a4356fb9 2229 if (errno_set)
2230 expand_errno_check (exp, target);
0fd605a5 2231
53800dbe 2232 /* Output the entire sequence. */
2233 insns = get_insns ();
2234 end_sequence ();
31d3e01c 2235 emit_insn (insns);
bf8e3599 2236
53800dbe 2237 return target;
2238}
2239
7e0713b1 2240/* Expand a call to the builtin trinary math functions (fma).
2241 Return NULL_RTX if a normal call should be emitted rather than expanding the
2242 function in-line. EXP is the expression that is a call to the builtin
2243 function; if convenient, the result should be placed in TARGET.
2244 SUBTARGET may be used as the target for computing one of EXP's
2245 operands. */
2246
2247static rtx
2248expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2249{
2250 optab builtin_optab;
2251 rtx op0, op1, op2, insns;
2252 tree fndecl = get_callee_fndecl (exp);
2253 tree arg0, arg1, arg2;
2254 enum machine_mode mode;
2255
2256 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2257 return NULL_RTX;
2258
2259 arg0 = CALL_EXPR_ARG (exp, 0);
2260 arg1 = CALL_EXPR_ARG (exp, 1);
2261 arg2 = CALL_EXPR_ARG (exp, 2);
2262
2263 switch (DECL_FUNCTION_CODE (fndecl))
2264 {
2265 CASE_FLT_FN (BUILT_IN_FMA):
2266 builtin_optab = fma_optab; break;
2267 default:
2268 gcc_unreachable ();
2269 }
2270
2271 /* Make a suitable register to place result in. */
2272 mode = TYPE_MODE (TREE_TYPE (exp));
2273
2274 /* Before working hard, check whether the instruction is available. */
2275 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2276 return NULL_RTX;
2277
2278 target = gen_reg_rtx (mode);
2279
2280 /* Always stabilize the argument list. */
2281 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2282 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2283 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2284
2285 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2286 op1 = expand_normal (arg1);
2287 op2 = expand_normal (arg2);
2288
2289 start_sequence ();
2290
2291 /* Compute into TARGET.
2292 Set TARGET to wherever the result comes back. */
2293 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2294 target, 0);
2295
2296 /* If we were unable to expand via the builtin, stop the sequence
2297 (without outputting the insns) and call to the library function
2298 with the stabilized argument list. */
2299 if (target == 0)
2300 {
2301 end_sequence ();
2302 return expand_call (exp, target, target == const0_rtx);
2303 }
2304
2305 /* Output the entire sequence. */
2306 insns = get_insns ();
2307 end_sequence ();
2308 emit_insn (insns);
2309
2310 return target;
2311}
2312
6b43bae4 2313/* Expand a call to the builtin sin and cos math functions.
c2f47e15 2314 Return NULL_RTX if a normal call should be emitted rather than expanding the
6b43bae4 2315 function in-line. EXP is the expression that is a call to the builtin
2316 function; if convenient, the result should be placed in TARGET.
2317 SUBTARGET may be used as the target for computing one of EXP's
2318 operands. */
2319
2320static rtx
2321expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2322{
2323 optab builtin_optab;
7511b819 2324 rtx op0, insns;
6b43bae4 2325 tree fndecl = get_callee_fndecl (exp);
6b43bae4 2326 enum machine_mode mode;
abfea505 2327 tree arg;
6b43bae4 2328
c2f47e15 2329 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2330 return NULL_RTX;
6b43bae4 2331
c2f47e15 2332 arg = CALL_EXPR_ARG (exp, 0);
6b43bae4 2333
2334 switch (DECL_FUNCTION_CODE (fndecl))
2335 {
4f35b1fc 2336 CASE_FLT_FN (BUILT_IN_SIN):
2337 CASE_FLT_FN (BUILT_IN_COS):
6b43bae4 2338 builtin_optab = sincos_optab; break;
2339 default:
64db345d 2340 gcc_unreachable ();
6b43bae4 2341 }
2342
2343 /* Make a suitable register to place result in. */
2344 mode = TYPE_MODE (TREE_TYPE (exp));
2345
6b43bae4 2346 /* Check if sincos insn is available, otherwise fallback
0bed3869 2347 to sin or cos insn. */
d6bf3b14 2348 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
6b43bae4 2349 switch (DECL_FUNCTION_CODE (fndecl))
2350 {
4f35b1fc 2351 CASE_FLT_FN (BUILT_IN_SIN):
6b43bae4 2352 builtin_optab = sin_optab; break;
4f35b1fc 2353 CASE_FLT_FN (BUILT_IN_COS):
6b43bae4 2354 builtin_optab = cos_optab; break;
2355 default:
64db345d 2356 gcc_unreachable ();
6b43bae4 2357 }
6b43bae4 2358
2359 /* Before working hard, check whether the instruction is available. */
d6bf3b14 2360 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
6b43bae4 2361 {
2362 target = gen_reg_rtx (mode);
2363
2364 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2365 need to expand the argument again. This way, we will not perform
2366 side-effects more the once. */
abfea505 2367 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
6b43bae4 2368
1db6d067 2369 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
6b43bae4 2370
6b43bae4 2371 start_sequence ();
2372
2373 /* Compute into TARGET.
2374 Set TARGET to wherever the result comes back. */
2375 if (builtin_optab == sincos_optab)
2376 {
64db345d 2377 int result;
7d3f6cc7 2378
6b43bae4 2379 switch (DECL_FUNCTION_CODE (fndecl))
2380 {
4f35b1fc 2381 CASE_FLT_FN (BUILT_IN_SIN):
64db345d 2382 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
6b43bae4 2383 break;
4f35b1fc 2384 CASE_FLT_FN (BUILT_IN_COS):
64db345d 2385 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
6b43bae4 2386 break;
2387 default:
64db345d 2388 gcc_unreachable ();
6b43bae4 2389 }
64db345d 2390 gcc_assert (result);
6b43bae4 2391 }
2392 else
2393 {
2394 target = expand_unop (mode, builtin_optab, op0, target, 0);
2395 }
2396
2397 if (target != 0)
2398 {
6b43bae4 2399 /* Output the entire sequence. */
2400 insns = get_insns ();
2401 end_sequence ();
2402 emit_insn (insns);
2403 return target;
2404 }
2405
2406 /* If we were unable to expand via the builtin, stop the sequence
2407 (without outputting the insns) and call to the library function
2408 with the stabilized argument list. */
2409 end_sequence ();
2410 }
2411
6b43bae4 2412 target = expand_call (exp, target, target == const0_rtx);
2413
2414 return target;
2415}
2416
a65c4d64 2417/* Given an interclass math builtin decl FNDECL and it's argument ARG
2418 return an RTL instruction code that implements the functionality.
2419 If that isn't possible or available return CODE_FOR_nothing. */
a67a90e5 2420
a65c4d64 2421static enum insn_code
2422interclass_mathfn_icode (tree arg, tree fndecl)
a67a90e5 2423{
a65c4d64 2424 bool errno_set = false;
cde061c1 2425 optab builtin_optab = 0;
a67a90e5 2426 enum machine_mode mode;
a67a90e5 2427
2428 switch (DECL_FUNCTION_CODE (fndecl))
2429 {
2430 CASE_FLT_FN (BUILT_IN_ILOGB):
2431 errno_set = true; builtin_optab = ilogb_optab; break;
69b779ea 2432 CASE_FLT_FN (BUILT_IN_ISINF):
2433 builtin_optab = isinf_optab; break;
8a1a9cb7 2434 case BUILT_IN_ISNORMAL:
cde061c1 2435 case BUILT_IN_ISFINITE:
2436 CASE_FLT_FN (BUILT_IN_FINITE):
a65c4d64 2437 case BUILT_IN_FINITED32:
2438 case BUILT_IN_FINITED64:
2439 case BUILT_IN_FINITED128:
2440 case BUILT_IN_ISINFD32:
2441 case BUILT_IN_ISINFD64:
2442 case BUILT_IN_ISINFD128:
cde061c1 2443 /* These builtins have no optabs (yet). */
2444 break;
a67a90e5 2445 default:
2446 gcc_unreachable ();
2447 }
2448
2449 /* There's no easy way to detect the case we need to set EDOM. */
2450 if (flag_errno_math && errno_set)
a65c4d64 2451 return CODE_FOR_nothing;
a67a90e5 2452
2453 /* Optab mode depends on the mode of the input argument. */
2454 mode = TYPE_MODE (TREE_TYPE (arg));
2455
cde061c1 2456 if (builtin_optab)
d6bf3b14 2457 return optab_handler (builtin_optab, mode);
a65c4d64 2458 return CODE_FOR_nothing;
2459}
2460
2461/* Expand a call to one of the builtin math functions that operate on
2462 floating point argument and output an integer result (ilogb, isinf,
2463 isnan, etc).
2464 Return 0 if a normal call should be emitted rather than expanding the
2465 function in-line. EXP is the expression that is a call to the builtin
f97eea22 2466 function; if convenient, the result should be placed in TARGET. */
a65c4d64 2467
2468static rtx
f97eea22 2469expand_builtin_interclass_mathfn (tree exp, rtx target)
a65c4d64 2470{
2471 enum insn_code icode = CODE_FOR_nothing;
2472 rtx op0;
2473 tree fndecl = get_callee_fndecl (exp);
2474 enum machine_mode mode;
2475 tree arg;
2476
2477 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2478 return NULL_RTX;
2479
2480 arg = CALL_EXPR_ARG (exp, 0);
2481 icode = interclass_mathfn_icode (arg, fndecl);
2482 mode = TYPE_MODE (TREE_TYPE (arg));
2483
a67a90e5 2484 if (icode != CODE_FOR_nothing)
2485 {
8786db1e 2486 struct expand_operand ops[1];
4e2a2fb4 2487 rtx last = get_last_insn ();
2488 tree orig_arg = arg;
a67a90e5 2489
2490 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2491 need to expand the argument again. This way, we will not perform
2492 side-effects more the once. */
abfea505 2493 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
a67a90e5 2494
f97eea22 2495 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
a67a90e5 2496
2497 if (mode != GET_MODE (op0))
2498 op0 = convert_to_mode (mode, op0, 0);
2499
8786db1e 2500 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2501 if (maybe_legitimize_operands (icode, 0, 1, ops)
2502 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2503 return ops[0].value;
2504
4e2a2fb4 2505 delete_insns_since (last);
2506 CALL_EXPR_ARG (exp, 0) = orig_arg;
a67a90e5 2507 }
2508
a65c4d64 2509 return NULL_RTX;
a67a90e5 2510}
2511
c3147c1a 2512/* Expand a call to the builtin sincos math function.
c2f47e15 2513 Return NULL_RTX if a normal call should be emitted rather than expanding the
c3147c1a 2514 function in-line. EXP is the expression that is a call to the builtin
2515 function. */
2516
2517static rtx
2518expand_builtin_sincos (tree exp)
2519{
2520 rtx op0, op1, op2, target1, target2;
c3147c1a 2521 enum machine_mode mode;
2522 tree arg, sinp, cosp;
2523 int result;
389dd41b 2524 location_t loc = EXPR_LOCATION (exp);
be5575b2 2525 tree alias_type, alias_off;
c3147c1a 2526
c2f47e15 2527 if (!validate_arglist (exp, REAL_TYPE,
2528 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2529 return NULL_RTX;
c3147c1a 2530
c2f47e15 2531 arg = CALL_EXPR_ARG (exp, 0);
2532 sinp = CALL_EXPR_ARG (exp, 1);
2533 cosp = CALL_EXPR_ARG (exp, 2);
c3147c1a 2534
2535 /* Make a suitable register to place result in. */
2536 mode = TYPE_MODE (TREE_TYPE (arg));
2537
2538 /* Check if sincos insn is available, otherwise emit the call. */
d6bf3b14 2539 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
c3147c1a 2540 return NULL_RTX;
2541
2542 target1 = gen_reg_rtx (mode);
2543 target2 = gen_reg_rtx (mode);
2544
8ec3c5c2 2545 op0 = expand_normal (arg);
be5575b2 2546 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2547 alias_off = build_int_cst (alias_type, 0);
2548 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2549 sinp, alias_off));
2550 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2551 cosp, alias_off));
c3147c1a 2552
2553 /* Compute into target1 and target2.
2554 Set TARGET to wherever the result comes back. */
2555 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2556 gcc_assert (result);
2557
2558 /* Move target1 and target2 to the memory locations indicated
2559 by op1 and op2. */
2560 emit_move_insn (op1, target1);
2561 emit_move_insn (op2, target2);
2562
2563 return const0_rtx;
2564}
2565
d735c391 2566/* Expand a call to the internal cexpi builtin to the sincos math function.
2567 EXP is the expression that is a call to the builtin function; if convenient,
f97eea22 2568 the result should be placed in TARGET. */
d735c391 2569
2570static rtx
f97eea22 2571expand_builtin_cexpi (tree exp, rtx target)
d735c391 2572{
2573 tree fndecl = get_callee_fndecl (exp);
d735c391 2574 tree arg, type;
c2f47e15 2575 enum machine_mode mode;
d735c391 2576 rtx op0, op1, op2;
389dd41b 2577 location_t loc = EXPR_LOCATION (exp);
d735c391 2578
c2f47e15 2579 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2580 return NULL_RTX;
d735c391 2581
c2f47e15 2582 arg = CALL_EXPR_ARG (exp, 0);
d735c391 2583 type = TREE_TYPE (arg);
2584 mode = TYPE_MODE (TREE_TYPE (arg));
2585
2586 /* Try expanding via a sincos optab, fall back to emitting a libcall
18b8d8ae 2587 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2588 is only generated from sincos, cexp or if we have either of them. */
d6bf3b14 2589 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
d735c391 2590 {
2591 op1 = gen_reg_rtx (mode);
2592 op2 = gen_reg_rtx (mode);
2593
f97eea22 2594 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
d735c391 2595
2596 /* Compute into op1 and op2. */
2597 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2598 }
18b8d8ae 2599 else if (TARGET_HAS_SINCOS)
d735c391 2600 {
c2f47e15 2601 tree call, fn = NULL_TREE;
d735c391 2602 tree top1, top2;
2603 rtx op1a, op2a;
2604
2605 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
b9a16870 2606 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
d735c391 2607 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
b9a16870 2608 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
d735c391 2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
b9a16870 2610 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
c2f47e15 2611 else
2612 gcc_unreachable ();
48e1416a 2613
d735c391 2614 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2615 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
99182918 2616 op1a = copy_addr_to_reg (XEXP (op1, 0));
2617 op2a = copy_addr_to_reg (XEXP (op2, 0));
d735c391 2618 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2619 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2620
d735c391 2621 /* Make sure not to fold the sincos call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
c2f47e15 2623 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2624 call, 3, arg, top1, top2));
d735c391 2625 }
18b8d8ae 2626 else
2627 {
0ecbc158 2628 tree call, fn = NULL_TREE, narg;
18b8d8ae 2629 tree ctype = build_complex_type (type);
2630
0ecbc158 2631 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
b9a16870 2632 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
0ecbc158 2633 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
b9a16870 2634 fn = builtin_decl_explicit (BUILT_IN_CEXP);
0ecbc158 2635 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
b9a16870 2636 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
c2f47e15 2637 else
2638 gcc_unreachable ();
fc0dfa6e 2639
2640 /* If we don't have a decl for cexp create one. This is the
2641 friendliest fallback if the user calls __builtin_cexpi
2642 without full target C99 function support. */
2643 if (fn == NULL_TREE)
2644 {
2645 tree fntype;
2646 const char *name = NULL;
2647
2648 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2649 name = "cexpf";
2650 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2651 name = "cexp";
2652 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2653 name = "cexpl";
2654
2655 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2656 fn = build_fn_decl (name, fntype);
2657 }
2658
389dd41b 2659 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
18b8d8ae 2660 build_real (type, dconst0), arg);
2661
2662 /* Make sure not to fold the cexp call again. */
2663 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
48e1416a 2664 return expand_expr (build_call_nary (ctype, call, 1, narg),
1db6d067 2665 target, VOIDmode, EXPAND_NORMAL);
18b8d8ae 2666 }
d735c391 2667
2668 /* Now build the proper return type. */
2669 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2670 make_tree (TREE_TYPE (arg), op2),
2671 make_tree (TREE_TYPE (arg), op1)),
1db6d067 2672 target, VOIDmode, EXPAND_NORMAL);
d735c391 2673}
2674
a65c4d64 2675/* Conveniently construct a function call expression. FNDECL names the
2676 function to be called, N is the number of arguments, and the "..."
2677 parameters are the argument expressions. Unlike build_call_exr
2678 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2679
2680static tree
2681build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2682{
2683 va_list ap;
2684 tree fntype = TREE_TYPE (fndecl);
2685 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2686
2687 va_start (ap, n);
2688 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2689 va_end (ap);
2690 SET_EXPR_LOCATION (fn, loc);
2691 return fn;
2692}
a65c4d64 2693
7d3afc77 2694/* Expand a call to one of the builtin rounding functions gcc defines
2695 as an extension (lfloor and lceil). As these are gcc extensions we
2696 do not need to worry about setting errno to EDOM.
ad52b9b7 2697 If expanding via optab fails, lower expression to (int)(floor(x)).
2698 EXP is the expression that is a call to the builtin function;
ff1b14e4 2699 if convenient, the result should be placed in TARGET. */
ad52b9b7 2700
2701static rtx
ff1b14e4 2702expand_builtin_int_roundingfn (tree exp, rtx target)
ad52b9b7 2703{
9c42dd28 2704 convert_optab builtin_optab;
ad52b9b7 2705 rtx op0, insns, tmp;
2706 tree fndecl = get_callee_fndecl (exp);
ad52b9b7 2707 enum built_in_function fallback_fn;
2708 tree fallback_fndecl;
2709 enum machine_mode mode;
4de0924f 2710 tree arg;
ad52b9b7 2711
c2f47e15 2712 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
ad52b9b7 2713 gcc_unreachable ();
2714
c2f47e15 2715 arg = CALL_EXPR_ARG (exp, 0);
ad52b9b7 2716
2717 switch (DECL_FUNCTION_CODE (fndecl))
2718 {
80ff6494 2719 CASE_FLT_FN (BUILT_IN_ICEIL):
4f35b1fc 2720 CASE_FLT_FN (BUILT_IN_LCEIL):
2721 CASE_FLT_FN (BUILT_IN_LLCEIL):
ac148751 2722 builtin_optab = lceil_optab;
2723 fallback_fn = BUILT_IN_CEIL;
2724 break;
2725
80ff6494 2726 CASE_FLT_FN (BUILT_IN_IFLOOR):
4f35b1fc 2727 CASE_FLT_FN (BUILT_IN_LFLOOR):
2728 CASE_FLT_FN (BUILT_IN_LLFLOOR):
ad52b9b7 2729 builtin_optab = lfloor_optab;
2730 fallback_fn = BUILT_IN_FLOOR;
2731 break;
2732
2733 default:
2734 gcc_unreachable ();
2735 }
2736
2737 /* Make a suitable register to place result in. */
2738 mode = TYPE_MODE (TREE_TYPE (exp));
2739
9c42dd28 2740 target = gen_reg_rtx (mode);
ad52b9b7 2741
9c42dd28 2742 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2743 need to expand the argument again. This way, we will not perform
2744 side-effects more the once. */
abfea505 2745 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
ad52b9b7 2746
ff1b14e4 2747 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
ad52b9b7 2748
9c42dd28 2749 start_sequence ();
ad52b9b7 2750
9c42dd28 2751 /* Compute into TARGET. */
2752 if (expand_sfix_optab (target, op0, builtin_optab))
2753 {
2754 /* Output the entire sequence. */
2755 insns = get_insns ();
ad52b9b7 2756 end_sequence ();
9c42dd28 2757 emit_insn (insns);
2758 return target;
ad52b9b7 2759 }
2760
9c42dd28 2761 /* If we were unable to expand via the builtin, stop the sequence
2762 (without outputting the insns). */
2763 end_sequence ();
2764
ad52b9b7 2765 /* Fall back to floating point rounding optab. */
2766 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
fc0dfa6e 2767
2768 /* For non-C99 targets we may end up without a fallback fndecl here
2769 if the user called __builtin_lfloor directly. In this case emit
2770 a call to the floor/ceil variants nevertheless. This should result
2771 in the best user experience for not full C99 targets. */
2772 if (fallback_fndecl == NULL_TREE)
2773 {
2774 tree fntype;
2775 const char *name = NULL;
2776
2777 switch (DECL_FUNCTION_CODE (fndecl))
2778 {
80ff6494 2779 case BUILT_IN_ICEIL:
fc0dfa6e 2780 case BUILT_IN_LCEIL:
2781 case BUILT_IN_LLCEIL:
2782 name = "ceil";
2783 break;
80ff6494 2784 case BUILT_IN_ICEILF:
fc0dfa6e 2785 case BUILT_IN_LCEILF:
2786 case BUILT_IN_LLCEILF:
2787 name = "ceilf";
2788 break;
80ff6494 2789 case BUILT_IN_ICEILL:
fc0dfa6e 2790 case BUILT_IN_LCEILL:
2791 case BUILT_IN_LLCEILL:
2792 name = "ceill";
2793 break;
80ff6494 2794 case BUILT_IN_IFLOOR:
fc0dfa6e 2795 case BUILT_IN_LFLOOR:
2796 case BUILT_IN_LLFLOOR:
2797 name = "floor";
2798 break;
80ff6494 2799 case BUILT_IN_IFLOORF:
fc0dfa6e 2800 case BUILT_IN_LFLOORF:
2801 case BUILT_IN_LLFLOORF:
2802 name = "floorf";
2803 break;
80ff6494 2804 case BUILT_IN_IFLOORL:
fc0dfa6e 2805 case BUILT_IN_LFLOORL:
2806 case BUILT_IN_LLFLOORL:
2807 name = "floorl";
2808 break;
2809 default:
2810 gcc_unreachable ();
2811 }
2812
2813 fntype = build_function_type_list (TREE_TYPE (arg),
2814 TREE_TYPE (arg), NULL_TREE);
2815 fallback_fndecl = build_fn_decl (name, fntype);
2816 }
2817
0568e9c1 2818 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
ad52b9b7 2819
d4c690af 2820 tmp = expand_normal (exp);
ad52b9b7 2821
2822 /* Truncate the result of floating point optab to integer
2823 via expand_fix (). */
2824 target = gen_reg_rtx (mode);
2825 expand_fix (target, tmp, 0);
2826
2827 return target;
2828}
2829
7d3afc77 2830/* Expand a call to one of the builtin math functions doing integer
2831 conversion (lrint).
2832 Return 0 if a normal call should be emitted rather than expanding the
2833 function in-line. EXP is the expression that is a call to the builtin
ff1b14e4 2834 function; if convenient, the result should be placed in TARGET. */
7d3afc77 2835
2836static rtx
ff1b14e4 2837expand_builtin_int_roundingfn_2 (tree exp, rtx target)
7d3afc77 2838{
5f51ee59 2839 convert_optab builtin_optab;
7d3afc77 2840 rtx op0, insns;
2841 tree fndecl = get_callee_fndecl (exp);
4de0924f 2842 tree arg;
c2f47e15 2843 enum machine_mode mode;
e951f9a4 2844 enum built_in_function fallback_fn = BUILT_IN_NONE;
7d3afc77 2845
c2f47e15 2846 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2847 gcc_unreachable ();
48e1416a 2848
c2f47e15 2849 arg = CALL_EXPR_ARG (exp, 0);
7d3afc77 2850
2851 switch (DECL_FUNCTION_CODE (fndecl))
2852 {
80ff6494 2853 CASE_FLT_FN (BUILT_IN_IRINT):
e951f9a4 2854 fallback_fn = BUILT_IN_LRINT;
2855 /* FALLTHRU */
7d3afc77 2856 CASE_FLT_FN (BUILT_IN_LRINT):
2857 CASE_FLT_FN (BUILT_IN_LLRINT):
e951f9a4 2858 builtin_optab = lrint_optab;
2859 break;
80ff6494 2860
2861 CASE_FLT_FN (BUILT_IN_IROUND):
e951f9a4 2862 fallback_fn = BUILT_IN_LROUND;
2863 /* FALLTHRU */
ef2f1a10 2864 CASE_FLT_FN (BUILT_IN_LROUND):
2865 CASE_FLT_FN (BUILT_IN_LLROUND):
e951f9a4 2866 builtin_optab = lround_optab;
2867 break;
80ff6494 2868
7d3afc77 2869 default:
2870 gcc_unreachable ();
2871 }
2872
e951f9a4 2873 /* There's no easy way to detect the case we need to set EDOM. */
2874 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2875 return NULL_RTX;
2876
7d3afc77 2877 /* Make a suitable register to place result in. */
2878 mode = TYPE_MODE (TREE_TYPE (exp));
2879
e951f9a4 2880 /* There's no easy way to detect the case we need to set EDOM. */
2881 if (!flag_errno_math)
2882 {
2883 target = gen_reg_rtx (mode);
7d3afc77 2884
e951f9a4 2885 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2886 need to expand the argument again. This way, we will not perform
2887 side-effects more the once. */
2888 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
7d3afc77 2889
e951f9a4 2890 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
7d3afc77 2891
e951f9a4 2892 start_sequence ();
7d3afc77 2893
e951f9a4 2894 if (expand_sfix_optab (target, op0, builtin_optab))
2895 {
2896 /* Output the entire sequence. */
2897 insns = get_insns ();
2898 end_sequence ();
2899 emit_insn (insns);
2900 return target;
2901 }
2902
2903 /* If we were unable to expand via the builtin, stop the sequence
2904 (without outputting the insns) and call to the library function
2905 with the stabilized argument list. */
7d3afc77 2906 end_sequence ();
2907 }
2908
e951f9a4 2909 if (fallback_fn != BUILT_IN_NONE)
2910 {
2911 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2912 targets, (int) round (x) should never be transformed into
2913 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2914 a call to lround in the hope that the target provides at least some
2915 C99 functions. This should result in the best user experience for
2916 not full C99 targets. */
2917 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2918 fallback_fn, 0);
2919
2920 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2921 fallback_fndecl, 1, arg);
2922
2923 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2924 return convert_to_mode (mode, target, 0);
2925 }
5f51ee59 2926
7d3afc77 2927 target = expand_call (exp, target, target == const0_rtx);
2928
2929 return target;
2930}
2931
c2f47e15 2932/* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
757c219d 2933 a normal call should be emitted rather than expanding the function
2934 in-line. EXP is the expression that is a call to the builtin
2935 function; if convenient, the result should be placed in TARGET. */
2936
2937static rtx
f97eea22 2938expand_builtin_powi (tree exp, rtx target)
757c219d 2939{
757c219d 2940 tree arg0, arg1;
2941 rtx op0, op1;
2942 enum machine_mode mode;
d0405f40 2943 enum machine_mode mode2;
757c219d 2944
c2f47e15 2945 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2946 return NULL_RTX;
757c219d 2947
c2f47e15 2948 arg0 = CALL_EXPR_ARG (exp, 0);
2949 arg1 = CALL_EXPR_ARG (exp, 1);
757c219d 2950 mode = TYPE_MODE (TREE_TYPE (exp));
2951
757c219d 2952 /* Emit a libcall to libgcc. */
2953
c2f47e15 2954 /* Mode of the 2nd argument must match that of an int. */
d0405f40 2955 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2956
757c219d 2957 if (target == NULL_RTX)
2958 target = gen_reg_rtx (mode);
2959
f97eea22 2960 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
757c219d 2961 if (GET_MODE (op0) != mode)
2962 op0 = convert_to_mode (mode, op0, 0);
1db6d067 2963 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
d0405f40 2964 if (GET_MODE (op1) != mode2)
2965 op1 = convert_to_mode (mode2, op1, 0);
757c219d 2966
f36b9f69 2967 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2dd6f9ed 2968 target, LCT_CONST, mode, 2,
d0405f40 2969 op0, mode, op1, mode2);
757c219d 2970
2971 return target;
2972}
2973
48e1416a 2974/* Expand expression EXP which is a call to the strlen builtin. Return
c2f47e15 2975 NULL_RTX if we failed the caller should emit a normal call, otherwise
aed0bd19 2976 try to get the result in TARGET, if convenient. */
f7c44134 2977
53800dbe 2978static rtx
c2f47e15 2979expand_builtin_strlen (tree exp, rtx target,
aecda0d6 2980 enum machine_mode target_mode)
53800dbe 2981{
c2f47e15 2982 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2983 return NULL_RTX;
53800dbe 2984 else
2985 {
8786db1e 2986 struct expand_operand ops[4];
911c0150 2987 rtx pat;
c2f47e15 2988 tree len;
2989 tree src = CALL_EXPR_ARG (exp, 0);
8786db1e 2990 rtx src_reg, before_strlen;
2991 enum machine_mode insn_mode = target_mode;
ef2c4a29 2992 enum insn_code icode = CODE_FOR_nothing;
153c3b50 2993 unsigned int align;
6248e345 2994
2995 /* If the length can be computed at compile-time, return it. */
681fab1e 2996 len = c_strlen (src, 0);
6248e345 2997 if (len)
80cd7a5e 2998 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
6248e345 2999
681fab1e 3000 /* If the length can be computed at compile-time and is constant
3001 integer, but there are side-effects in src, evaluate
3002 src for side-effects, then return len.
3003 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3004 can be optimized into: i++; x = 3; */
3005 len = c_strlen (src, 1);
3006 if (len && TREE_CODE (len) == INTEGER_CST)
3007 {
3008 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3009 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3010 }
3011
957d0361 3012 align = get_pointer_alignment (src) / BITS_PER_UNIT;
53800dbe 3013
53800dbe 3014 /* If SRC is not a pointer type, don't do this operation inline. */
3015 if (align == 0)
c2f47e15 3016 return NULL_RTX;
53800dbe 3017
911c0150 3018 /* Bail out if we can't compute strlen in the right mode. */
53800dbe 3019 while (insn_mode != VOIDmode)
3020 {
d6bf3b14 3021 icode = optab_handler (strlen_optab, insn_mode);
53800dbe 3022 if (icode != CODE_FOR_nothing)
c28ae87f 3023 break;
53800dbe 3024
3025 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3026 }
3027 if (insn_mode == VOIDmode)
c2f47e15 3028 return NULL_RTX;
53800dbe 3029
911c0150 3030 /* Make a place to hold the source address. We will not expand
3031 the actual source until we are sure that the expansion will
3032 not fail -- there are trees that cannot be expanded twice. */
3033 src_reg = gen_reg_rtx (Pmode);
53800dbe 3034
911c0150 3035 /* Mark the beginning of the strlen sequence so we can emit the
3036 source operand later. */
f0ce3b1f 3037 before_strlen = get_last_insn ();
53800dbe 3038
8786db1e 3039 create_output_operand (&ops[0], target, insn_mode);
3040 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3041 create_integer_operand (&ops[2], 0);
3042 create_integer_operand (&ops[3], align);
3043 if (!maybe_expand_insn (icode, 4, ops))
c2f47e15 3044 return NULL_RTX;
911c0150 3045
3046 /* Now that we are assured of success, expand the source. */
3047 start_sequence ();
499eee58 3048 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
911c0150 3049 if (pat != src_reg)
499eee58 3050 {
3051#ifdef POINTERS_EXTEND_UNSIGNED
3052 if (GET_MODE (pat) != Pmode)
3053 pat = convert_to_mode (Pmode, pat,
3054 POINTERS_EXTEND_UNSIGNED);
3055#endif
3056 emit_move_insn (src_reg, pat);
3057 }
31d3e01c 3058 pat = get_insns ();
911c0150 3059 end_sequence ();
bceb0d1f 3060
3061 if (before_strlen)
3062 emit_insn_after (pat, before_strlen);
3063 else
3064 emit_insn_before (pat, get_insns ());
53800dbe 3065
3066 /* Return the value in the proper mode for this function. */
8786db1e 3067 if (GET_MODE (ops[0].value) == target_mode)
3068 target = ops[0].value;
53800dbe 3069 else if (target != 0)
8786db1e 3070 convert_move (target, ops[0].value, 0);
53800dbe 3071 else
8786db1e 3072 target = convert_to_mode (target_mode, ops[0].value, 0);
911c0150 3073
3074 return target;
53800dbe 3075 }
3076}
3077
6840589f 3078/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3079 bytes from constant string DATA + OFFSET and return it as target
3080 constant. */
3081
3082static rtx
aecda0d6 3083builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3084 enum machine_mode mode)
6840589f 3085{
3086 const char *str = (const char *) data;
3087
64db345d 3088 gcc_assert (offset >= 0
3089 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3090 <= strlen (str) + 1));
6840589f 3091
3092 return c_readstr (str + offset, mode);
3093}
3094
c2f47e15 3095/* Expand a call EXP to the memcpy builtin.
3096 Return NULL_RTX if we failed, the caller should emit a normal call,
3b824fa6 3097 otherwise try to get the result in TARGET, if convenient (and in
9fe0e1b8 3098 mode MODE if that's convenient). */
c2f47e15 3099
53800dbe 3100static rtx
a65c4d64 3101expand_builtin_memcpy (tree exp, rtx target)
53800dbe 3102{
c2f47e15 3103 if (!validate_arglist (exp,
3104 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3105 return NULL_RTX;
53800dbe 3106 else
3107 {
c2f47e15 3108 tree dest = CALL_EXPR_ARG (exp, 0);
3109 tree src = CALL_EXPR_ARG (exp, 1);
3110 tree len = CALL_EXPR_ARG (exp, 2);
6840589f 3111 const char *src_str;
957d0361 3112 unsigned int src_align = get_pointer_alignment (src);
3113 unsigned int dest_align = get_pointer_alignment (dest);
53800dbe 3114 rtx dest_mem, src_mem, dest_addr, len_rtx;
162719b3 3115 HOST_WIDE_INT expected_size = -1;
3116 unsigned int expected_align = 0;
5a0de151 3117
6f428e8b 3118 /* If DEST is not a pointer type, call the normal function. */
3119 if (dest_align == 0)
c2f47e15 3120 return NULL_RTX;
6f428e8b 3121
6f428e8b 3122 /* If either SRC is not a pointer type, don't do this
a0c938f0 3123 operation in-line. */
6f428e8b 3124 if (src_align == 0)
c2f47e15 3125 return NULL_RTX;
48e1416a 3126
8cee8dc0 3127 if (currently_expanding_gimple_stmt)
3128 stringop_block_profile (currently_expanding_gimple_stmt,
3129 &expected_align, &expected_size);
75a70cf9 3130
162719b3 3131 if (expected_align < dest_align)
3132 expected_align = dest_align;
d8ae1baa 3133 dest_mem = get_memory_rtx (dest, len);
2a631e19 3134 set_mem_align (dest_mem, dest_align);
8ec3c5c2 3135 len_rtx = expand_normal (len);
6840589f 3136 src_str = c_getstr (src);
3137
3138 /* If SRC is a string constant and block move would be done
3139 by pieces, we can avoid loading the string from memory
3140 and only stored the computed constants. */
3141 if (src_str
971ba038 3142 && CONST_INT_P (len_rtx)
6840589f 3143 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3144 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
364c0c59 3145 CONST_CAST (char *, src_str),
3146 dest_align, false))
6840589f 3147 {
9fe0e1b8 3148 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3149 builtin_memcpy_read_str,
364c0c59 3150 CONST_CAST (char *, src_str),
3151 dest_align, false, 0);
a65c4d64 3152 dest_mem = force_operand (XEXP (dest_mem, 0), target);
85d654dd 3153 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 3154 return dest_mem;
6840589f 3155 }
3156
d8ae1baa 3157 src_mem = get_memory_rtx (src, len);
2a631e19 3158 set_mem_align (src_mem, src_align);
53800dbe 3159
53800dbe 3160 /* Copy word part most expediently. */
162719b3 3161 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3162 CALL_EXPR_TAILCALL (exp)
3163 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3164 expected_align, expected_size);
53800dbe 3165
3166 if (dest_addr == 0)
e5716f7e 3167 {
a65c4d64 3168 dest_addr = force_operand (XEXP (dest_mem, 0), target);
85d654dd 3169 dest_addr = convert_memory_address (ptr_mode, dest_addr);
e5716f7e 3170 }
9fe0e1b8 3171 return dest_addr;
53800dbe 3172 }
3173}
3174
c2f47e15 3175/* Expand a call EXP to the mempcpy builtin.
3176 Return NULL_RTX if we failed; the caller should emit a normal call,
647661c6 3177 otherwise try to get the result in TARGET, if convenient (and in
9fe0e1b8 3178 mode MODE if that's convenient). If ENDP is 0 return the
3179 destination pointer, if ENDP is 1 return the end pointer ala
3180 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3181 stpcpy. */
647661c6 3182
3183static rtx
c8b17b2e 3184expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
647661c6 3185{
c2f47e15 3186 if (!validate_arglist (exp,
3187 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3188 return NULL_RTX;
3189 else
3190 {
3191 tree dest = CALL_EXPR_ARG (exp, 0);
3192 tree src = CALL_EXPR_ARG (exp, 1);
3193 tree len = CALL_EXPR_ARG (exp, 2);
3194 return expand_builtin_mempcpy_args (dest, src, len,
c2f47e15 3195 target, mode, /*endp=*/ 1);
3196 }
3197}
3198
3199/* Helper function to do the actual work for expand_builtin_mempcpy. The
3200 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3201 so that this can also be called without constructing an actual CALL_EXPR.
a65c4d64 3202 The other arguments and return value are the same as for
3203 expand_builtin_mempcpy. */
c2f47e15 3204
3205static rtx
a65c4d64 3206expand_builtin_mempcpy_args (tree dest, tree src, tree len,
c2f47e15 3207 rtx target, enum machine_mode mode, int endp)
3208{
3209 /* If return value is ignored, transform mempcpy into memcpy. */
b9a16870 3210 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
9fe0e1b8 3211 {
b9a16870 3212 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
0568e9c1 3213 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3214 dest, src, len);
c8b17b2e 3215 return expand_expr (result, target, mode, EXPAND_NORMAL);
9fe0e1b8 3216 }
647661c6 3217 else
3218 {
9fe0e1b8 3219 const char *src_str;
957d0361 3220 unsigned int src_align = get_pointer_alignment (src);
3221 unsigned int dest_align = get_pointer_alignment (dest);
9fe0e1b8 3222 rtx dest_mem, src_mem, len_rtx;
a0c938f0 3223
7da1412b 3224 /* If either SRC or DEST is not a pointer type, don't do this
a0c938f0 3225 operation in-line. */
7da1412b 3226 if (dest_align == 0 || src_align == 0)
c2f47e15 3227 return NULL_RTX;
9fe0e1b8 3228
6217c238 3229 /* If LEN is not constant, call the normal function. */
3230 if (! host_integerp (len, 1))
c2f47e15 3231 return NULL_RTX;
0862b7e9 3232
8ec3c5c2 3233 len_rtx = expand_normal (len);
9fe0e1b8 3234 src_str = c_getstr (src);
647661c6 3235
9fe0e1b8 3236 /* If SRC is a string constant and block move would be done
3237 by pieces, we can avoid loading the string from memory
3238 and only stored the computed constants. */
3239 if (src_str
971ba038 3240 && CONST_INT_P (len_rtx)
9fe0e1b8 3241 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3242 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
364c0c59 3243 CONST_CAST (char *, src_str),
3244 dest_align, false))
9fe0e1b8 3245 {
d8ae1baa 3246 dest_mem = get_memory_rtx (dest, len);
9fe0e1b8 3247 set_mem_align (dest_mem, dest_align);
3248 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3249 builtin_memcpy_read_str,
364c0c59 3250 CONST_CAST (char *, src_str),
3251 dest_align, false, endp);
9fe0e1b8 3252 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3253 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 3254 return dest_mem;
647661c6 3255 }
3256
971ba038 3257 if (CONST_INT_P (len_rtx)
9fe0e1b8 3258 && can_move_by_pieces (INTVAL (len_rtx),
3259 MIN (dest_align, src_align)))
3260 {
d8ae1baa 3261 dest_mem = get_memory_rtx (dest, len);
9fe0e1b8 3262 set_mem_align (dest_mem, dest_align);
d8ae1baa 3263 src_mem = get_memory_rtx (src, len);
9fe0e1b8 3264 set_mem_align (src_mem, src_align);
3265 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3266 MIN (dest_align, src_align), endp);
3267 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3268 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 3269 return dest_mem;
3270 }
3271
c2f47e15 3272 return NULL_RTX;
647661c6 3273 }
3274}
3275
727c62dd 3276#ifndef HAVE_movstr
3277# define HAVE_movstr 0
3278# define CODE_FOR_movstr CODE_FOR_nothing
3279#endif
3280
c2f47e15 3281/* Expand into a movstr instruction, if one is available. Return NULL_RTX if
727c62dd 3282 we failed, the caller should emit a normal call, otherwise try to
3283 get the result in TARGET, if convenient. If ENDP is 0 return the
3284 destination pointer, if ENDP is 1 return the end pointer ala
3285 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3286 stpcpy. */
3287
3288static rtx
3289expand_movstr (tree dest, tree src, rtx target, int endp)
3290{
8786db1e 3291 struct expand_operand ops[3];
727c62dd 3292 rtx dest_mem;
3293 rtx src_mem;
727c62dd 3294
3295 if (!HAVE_movstr)
c2f47e15 3296 return NULL_RTX;
727c62dd 3297
d8ae1baa 3298 dest_mem = get_memory_rtx (dest, NULL);
3299 src_mem = get_memory_rtx (src, NULL);
727c62dd 3300 if (!endp)
3301 {
3302 target = force_reg (Pmode, XEXP (dest_mem, 0));
3303 dest_mem = replace_equiv_address (dest_mem, target);
727c62dd 3304 }
3305
8786db1e 3306 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3307 create_fixed_operand (&ops[1], dest_mem);
3308 create_fixed_operand (&ops[2], src_mem);
3309 expand_insn (CODE_FOR_movstr, 3, ops);
727c62dd 3310
8786db1e 3311 if (endp && target != const0_rtx)
c5aba89c 3312 {
8786db1e 3313 target = ops[0].value;
3314 /* movstr is supposed to set end to the address of the NUL
3315 terminator. If the caller requested a mempcpy-like return value,
3316 adjust it. */
3317 if (endp == 1)
3318 {
45c0b118 3319 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
8786db1e 3320 emit_move_insn (target, force_operand (tem, NULL_RTX));
3321 }
c5aba89c 3322 }
727c62dd 3323 return target;
3324}
3325
48e1416a 3326/* Expand expression EXP, which is a call to the strcpy builtin. Return
3327 NULL_RTX if we failed the caller should emit a normal call, otherwise
c2f47e15 3328 try to get the result in TARGET, if convenient (and in mode MODE if that's
6f428e8b 3329 convenient). */
902de8ed 3330
53800dbe 3331static rtx
a65c4d64 3332expand_builtin_strcpy (tree exp, rtx target)
53800dbe 3333{
c2f47e15 3334 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3335 {
3336 tree dest = CALL_EXPR_ARG (exp, 0);
3337 tree src = CALL_EXPR_ARG (exp, 1);
a65c4d64 3338 return expand_builtin_strcpy_args (dest, src, target);
c2f47e15 3339 }
3340 return NULL_RTX;
3341}
3342
3343/* Helper function to do the actual work for expand_builtin_strcpy. The
3344 arguments to the builtin_strcpy call DEST and SRC are broken out
3345 so that this can also be called without constructing an actual CALL_EXPR.
3346 The other arguments and return value are the same as for
3347 expand_builtin_strcpy. */
3348
3349static rtx
a65c4d64 3350expand_builtin_strcpy_args (tree dest, tree src, rtx target)
c2f47e15 3351{
c2f47e15 3352 return expand_movstr (dest, src, target, /*endp=*/0);
53800dbe 3353}
3354
c2f47e15 3355/* Expand a call EXP to the stpcpy builtin.
3356 Return NULL_RTX if we failed the caller should emit a normal call,
3b824fa6 3357 otherwise try to get the result in TARGET, if convenient (and in
3358 mode MODE if that's convenient). */
3359
3360static rtx
dc369150 3361expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3b824fa6 3362{
c2f47e15 3363 tree dst, src;
389dd41b 3364 location_t loc = EXPR_LOCATION (exp);
c2f47e15 3365
3366 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3367 return NULL_RTX;
3368
3369 dst = CALL_EXPR_ARG (exp, 0);
3370 src = CALL_EXPR_ARG (exp, 1);
3371
727c62dd 3372 /* If return value is ignored, transform stpcpy into strcpy. */
b9a16870 3373 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
978836e5 3374 {
b9a16870 3375 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
0568e9c1 3376 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
c8b17b2e 3377 return expand_expr (result, target, mode, EXPAND_NORMAL);
978836e5 3378 }
3b824fa6 3379 else
3380 {
c2f47e15 3381 tree len, lenp1;
727c62dd 3382 rtx ret;
647661c6 3383
9fe0e1b8 3384 /* Ensure we get an actual string whose length can be evaluated at
a0c938f0 3385 compile-time, not an expression containing a string. This is
3386 because the latter will potentially produce pessimized code
3387 when used to produce the return value. */
681fab1e 3388 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
c2f47e15 3389 return expand_movstr (dst, src, target, /*endp=*/2);
3b824fa6 3390
389dd41b 3391 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
a65c4d64 3392 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
c2f47e15 3393 target, mode, /*endp=*/2);
727c62dd 3394
3395 if (ret)
3396 return ret;
3397
3398 if (TREE_CODE (len) == INTEGER_CST)
3399 {
8ec3c5c2 3400 rtx len_rtx = expand_normal (len);
727c62dd 3401
971ba038 3402 if (CONST_INT_P (len_rtx))
727c62dd 3403 {
a65c4d64 3404 ret = expand_builtin_strcpy_args (dst, src, target);
727c62dd 3405
3406 if (ret)
3407 {
3408 if (! target)
7ac87324 3409 {
3410 if (mode != VOIDmode)
3411 target = gen_reg_rtx (mode);
3412 else
3413 target = gen_reg_rtx (GET_MODE (ret));
3414 }
727c62dd 3415 if (GET_MODE (target) != GET_MODE (ret))
3416 ret = gen_lowpart (GET_MODE (target), ret);
3417
c5aba89c 3418 ret = plus_constant (ret, INTVAL (len_rtx));
3419 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
64db345d 3420 gcc_assert (ret);
727c62dd 3421
3422 return target;
3423 }
3424 }
3425 }
3426
c2f47e15 3427 return expand_movstr (dst, src, target, /*endp=*/2);
3b824fa6 3428 }
3429}
3430
6840589f 3431/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3432 bytes from constant string DATA + OFFSET and return it as target
3433 constant. */
3434
09879952 3435rtx
aecda0d6 3436builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3437 enum machine_mode mode)
6840589f 3438{
3439 const char *str = (const char *) data;
3440
3441 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3442 return const0_rtx;
3443
3444 return c_readstr (str + offset, mode);
3445}
3446
48e1416a 3447/* Expand expression EXP, which is a call to the strncpy builtin. Return
c2f47e15 3448 NULL_RTX if we failed the caller should emit a normal call. */
ed09096d 3449
3450static rtx
a65c4d64 3451expand_builtin_strncpy (tree exp, rtx target)
ed09096d 3452{
389dd41b 3453 location_t loc = EXPR_LOCATION (exp);
c2f47e15 3454
3455 if (validate_arglist (exp,
3456 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
ed09096d 3457 {
c2f47e15 3458 tree dest = CALL_EXPR_ARG (exp, 0);
3459 tree src = CALL_EXPR_ARG (exp, 1);
3460 tree len = CALL_EXPR_ARG (exp, 2);
3461 tree slen = c_strlen (src, 1);
6840589f 3462
8ff6a5cd 3463 /* We must be passed a constant len and src parameter. */
3464 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
c2f47e15 3465 return NULL_RTX;
ed09096d 3466
389dd41b 3467 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
ed09096d 3468
3469 /* We're required to pad with trailing zeros if the requested
a0c938f0 3470 len is greater than strlen(s2)+1. In that case try to
6840589f 3471 use store_by_pieces, if it fails, punt. */
ed09096d 3472 if (tree_int_cst_lt (slen, len))
6840589f 3473 {
957d0361 3474 unsigned int dest_align = get_pointer_alignment (dest);
c2f47e15 3475 const char *p = c_getstr (src);
6840589f 3476 rtx dest_mem;
3477
27d0c333 3478 if (!p || dest_align == 0 || !host_integerp (len, 1)
3479 || !can_store_by_pieces (tree_low_cst (len, 1),
6840589f 3480 builtin_strncpy_read_str,
364c0c59 3481 CONST_CAST (char *, p),
3482 dest_align, false))
c2f47e15 3483 return NULL_RTX;
6840589f 3484
d8ae1baa 3485 dest_mem = get_memory_rtx (dest, len);
27d0c333 3486 store_by_pieces (dest_mem, tree_low_cst (len, 1),
6840589f 3487 builtin_strncpy_read_str,
364c0c59 3488 CONST_CAST (char *, p), dest_align, false, 0);
a65c4d64 3489 dest_mem = force_operand (XEXP (dest_mem, 0), target);
85d654dd 3490 dest_mem = convert_memory_address (ptr_mode, dest_mem);
e5716f7e 3491 return dest_mem;
6840589f 3492 }
ed09096d 3493 }
c2f47e15 3494 return NULL_RTX;
ed09096d 3495}
3496
ecc318ff 3497/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3498 bytes from constant string DATA + OFFSET and return it as target
3499 constant. */
3500
f656b751 3501rtx
aecda0d6 3502builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3503 enum machine_mode mode)
ecc318ff 3504{
3505 const char *c = (const char *) data;
364c0c59 3506 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
ecc318ff 3507
3508 memset (p, *c, GET_MODE_SIZE (mode));
3509
3510 return c_readstr (p, mode);
3511}
3512
a7ec6974 3513/* Callback routine for store_by_pieces. Return the RTL of a register
3514 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3515 char value given in the RTL register data. For example, if mode is
3516 4 bytes wide, return the RTL for 0x01010101*data. */
3517
3518static rtx
aecda0d6 3519builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3520 enum machine_mode mode)
a7ec6974 3521{
3522 rtx target, coeff;
3523 size_t size;
3524 char *p;
3525
3526 size = GET_MODE_SIZE (mode);
f0ce3b1f 3527 if (size == 1)
3528 return (rtx) data;
a7ec6974 3529
364c0c59 3530 p = XALLOCAVEC (char, size);
a7ec6974 3531 memset (p, 1, size);
3532 coeff = c_readstr (p, mode);
3533
f0ce3b1f 3534 target = convert_to_mode (mode, (rtx) data, 1);
a7ec6974 3535 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3536 return force_reg (mode, target);
3537}
3538
48e1416a 3539/* Expand expression EXP, which is a call to the memset builtin. Return
3540 NULL_RTX if we failed the caller should emit a normal call, otherwise
c2f47e15 3541 try to get the result in TARGET, if convenient (and in mode MODE if that's
6f428e8b 3542 convenient). */
902de8ed 3543
53800dbe 3544static rtx
c2f47e15 3545expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
53800dbe 3546{
c2f47e15 3547 if (!validate_arglist (exp,
3548 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3549 return NULL_RTX;
53800dbe 3550 else
3551 {
c2f47e15 3552 tree dest = CALL_EXPR_ARG (exp, 0);
3553 tree val = CALL_EXPR_ARG (exp, 1);
3554 tree len = CALL_EXPR_ARG (exp, 2);
3555 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3556 }
3557}
53800dbe 3558
c2f47e15 3559/* Helper function to do the actual work for expand_builtin_memset. The
3560 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3561 so that this can also be called without constructing an actual CALL_EXPR.
3562 The other arguments and return value are the same as for
3563 expand_builtin_memset. */
6b961939 3564
c2f47e15 3565static rtx
3566expand_builtin_memset_args (tree dest, tree val, tree len,
3567 rtx target, enum machine_mode mode, tree orig_exp)
3568{
3569 tree fndecl, fn;
3570 enum built_in_function fcode;
03a5dda9 3571 enum machine_mode val_mode;
c2f47e15 3572 char c;
3573 unsigned int dest_align;
3574 rtx dest_mem, dest_addr, len_rtx;
3575 HOST_WIDE_INT expected_size = -1;
3576 unsigned int expected_align = 0;
53800dbe 3577
957d0361 3578 dest_align = get_pointer_alignment (dest);
162719b3 3579
c2f47e15 3580 /* If DEST is not a pointer type, don't do this operation in-line. */
3581 if (dest_align == 0)
3582 return NULL_RTX;
6f428e8b 3583
8cee8dc0 3584 if (currently_expanding_gimple_stmt)
3585 stringop_block_profile (currently_expanding_gimple_stmt,
3586 &expected_align, &expected_size);
75a70cf9 3587
c2f47e15 3588 if (expected_align < dest_align)
3589 expected_align = dest_align;
6b961939 3590
c2f47e15 3591 /* If the LEN parameter is zero, return DEST. */
3592 if (integer_zerop (len))
3593 {
3594 /* Evaluate and ignore VAL in case it has side-effects. */
3595 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3596 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3597 }
7a3e5564 3598
c2f47e15 3599 /* Stabilize the arguments in case we fail. */
3600 dest = builtin_save_expr (dest);
3601 val = builtin_save_expr (val);
3602 len = builtin_save_expr (len);
a7ec6974 3603
c2f47e15 3604 len_rtx = expand_normal (len);
3605 dest_mem = get_memory_rtx (dest, len);
03a5dda9 3606 val_mode = TYPE_MODE (unsigned_char_type_node);
a7ec6974 3607
c2f47e15 3608 if (TREE_CODE (val) != INTEGER_CST)
3609 {
3610 rtx val_rtx;
a7ec6974 3611
c2f47e15 3612 val_rtx = expand_normal (val);
03a5dda9 3613 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
53800dbe 3614
c2f47e15 3615 /* Assume that we can memset by pieces if we can store
3616 * the coefficients by pieces (in the required modes).
3617 * We can't pass builtin_memset_gen_str as that emits RTL. */
3618 c = 1;
3619 if (host_integerp (len, 1)
c2f47e15 3620 && can_store_by_pieces (tree_low_cst (len, 1),
4b297e2e 3621 builtin_memset_read_str, &c, dest_align,
3622 true))
c2f47e15 3623 {
03a5dda9 3624 val_rtx = force_reg (val_mode, val_rtx);
c2f47e15 3625 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4b297e2e 3626 builtin_memset_gen_str, val_rtx, dest_align,
3627 true, 0);
c2f47e15 3628 }
3629 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3630 dest_align, expected_align,
3631 expected_size))
6b961939 3632 goto do_libcall;
48e1416a 3633
c2f47e15 3634 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3635 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3636 return dest_mem;
3637 }
53800dbe 3638
c2f47e15 3639 if (target_char_cast (val, &c))
3640 goto do_libcall;
ecc318ff 3641
c2f47e15 3642 if (c)
3643 {
3644 if (host_integerp (len, 1)
c2f47e15 3645 && can_store_by_pieces (tree_low_cst (len, 1),
4b297e2e 3646 builtin_memset_read_str, &c, dest_align,
3647 true))
c2f47e15 3648 store_by_pieces (dest_mem, tree_low_cst (len, 1),
4b297e2e 3649 builtin_memset_read_str, &c, dest_align, true, 0);
03a5dda9 3650 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3651 gen_int_mode (c, val_mode),
c2f47e15 3652 dest_align, expected_align,
3653 expected_size))
3654 goto do_libcall;
48e1416a 3655
c2f47e15 3656 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3657 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3658 return dest_mem;
3659 }
ecc318ff 3660
c2f47e15 3661 set_mem_align (dest_mem, dest_align);
3662 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3663 CALL_EXPR_TAILCALL (orig_exp)
3664 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3665 expected_align, expected_size);
53800dbe 3666
c2f47e15 3667 if (dest_addr == 0)
3668 {
3669 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3670 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3671 }
53800dbe 3672
c2f47e15 3673 return dest_addr;
6b961939 3674
c2f47e15 3675 do_libcall:
3676 fndecl = get_callee_fndecl (orig_exp);
3677 fcode = DECL_FUNCTION_CODE (fndecl);
3678 if (fcode == BUILT_IN_MEMSET)
0568e9c1 3679 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3680 dest, val, len);
c2f47e15 3681 else if (fcode == BUILT_IN_BZERO)
0568e9c1 3682 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3683 dest, len);
c2f47e15 3684 else
3685 gcc_unreachable ();
a65c4d64 3686 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3687 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
c2f47e15 3688 return expand_call (fn, target, target == const0_rtx);
53800dbe 3689}
3690
48e1416a 3691/* Expand expression EXP, which is a call to the bzero builtin. Return
c2f47e15 3692 NULL_RTX if we failed the caller should emit a normal call. */
27d0c333 3693
ffc83088 3694static rtx
0b25db21 3695expand_builtin_bzero (tree exp)
ffc83088 3696{
c2f47e15 3697 tree dest, size;
389dd41b 3698 location_t loc = EXPR_LOCATION (exp);
ffc83088 3699
c2f47e15 3700 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7369e7ba 3701 return NULL_RTX;
ffc83088 3702
c2f47e15 3703 dest = CALL_EXPR_ARG (exp, 0);
3704 size = CALL_EXPR_ARG (exp, 1);
bf8e3599 3705
7369e7ba 3706 /* New argument list transforming bzero(ptr x, int y) to
6f428e8b 3707 memset(ptr x, int 0, size_t y). This is done this way
3708 so that if it isn't expanded inline, we fallback to
3709 calling bzero instead of memset. */
bf8e3599 3710
c2f47e15 3711 return expand_builtin_memset_args (dest, integer_zero_node,
a0553bff 3712 fold_convert_loc (loc,
3713 size_type_node, size),
c2f47e15 3714 const0_rtx, VOIDmode, exp);
ffc83088 3715}
3716
7a3f89b5 3717/* Expand expression EXP, which is a call to the memcmp built-in function.
bd021c1c 3718 Return NULL_RTX if we failed and the caller should emit a normal call,
3719 otherwise try to get the result in TARGET, if convenient (and in mode
3720 MODE, if that's convenient). */
27d0c333 3721
53800dbe 3722static rtx
a65c4d64 3723expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3724 ATTRIBUTE_UNUSED enum machine_mode mode)
53800dbe 3725{
a65c4d64 3726 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
389dd41b 3727
c2f47e15 3728 if (!validate_arglist (exp,
3729 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3730 return NULL_RTX;
6f428e8b 3731
bd021c1c 3732 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3733 implementing memcmp because it will stop if it encounters two
3734 zero bytes. */
3735#if defined HAVE_cmpmemsi
53800dbe 3736 {
0cd832f0 3737 rtx arg1_rtx, arg2_rtx, arg3_rtx;
53800dbe 3738 rtx result;
0cd832f0 3739 rtx insn;
c2f47e15 3740 tree arg1 = CALL_EXPR_ARG (exp, 0);
3741 tree arg2 = CALL_EXPR_ARG (exp, 1);
3742 tree len = CALL_EXPR_ARG (exp, 2);
53800dbe 3743
957d0361 3744 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3745 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
b428c0a5 3746 enum machine_mode insn_mode;
3747
b428c0a5 3748 if (HAVE_cmpmemsi)
3749 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3750 else
c2f47e15 3751 return NULL_RTX;
53800dbe 3752
3753 /* If we don't have POINTER_TYPE, call the function. */
3754 if (arg1_align == 0 || arg2_align == 0)
c2f47e15 3755 return NULL_RTX;
53800dbe 3756
3757 /* Make a place to write the result of the instruction. */
3758 result = target;
3759 if (! (result != 0
8ad4c111 3760 && REG_P (result) && GET_MODE (result) == insn_mode
53800dbe 3761 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3762 result = gen_reg_rtx (insn_mode);
3763
d8ae1baa 3764 arg1_rtx = get_memory_rtx (arg1, len);
3765 arg2_rtx = get_memory_rtx (arg2, len);
389dd41b 3766 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
83f88f8e 3767
3768 /* Set MEM_SIZE as appropriate. */
971ba038 3769 if (CONST_INT_P (arg3_rtx))
83f88f8e 3770 {
5b2a69fa 3771 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3772 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
83f88f8e 3773 }
3774
b428c0a5 3775 if (HAVE_cmpmemsi)
3776 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3777 GEN_INT (MIN (arg1_align, arg2_align)));
0cd832f0 3778 else
64db345d 3779 gcc_unreachable ();
0cd832f0 3780
3781 if (insn)
3782 emit_insn (insn);
3783 else
2dd6f9ed 3784 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
0cd832f0 3785 TYPE_MODE (integer_type_node), 3,
3786 XEXP (arg1_rtx, 0), Pmode,
3787 XEXP (arg2_rtx, 0), Pmode,
3788 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
78a8ed03 3789 TYPE_UNSIGNED (sizetype)),
0cd832f0 3790 TYPE_MODE (sizetype));
53800dbe 3791
3792 /* Return the value in the proper mode for this function. */
3793 mode = TYPE_MODE (TREE_TYPE (exp));
3794 if (GET_MODE (result) == mode)
3795 return result;
3796 else if (target != 0)
3797 {
3798 convert_move (target, result, 0);
3799 return target;
3800 }
3801 else
3802 return convert_to_mode (mode, result, 0);
3803 }
bd021c1c 3804#endif /* HAVE_cmpmemsi. */
53800dbe 3805
c2f47e15 3806 return NULL_RTX;
6f428e8b 3807}
3808
c2f47e15 3809/* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
53800dbe 3810 if we failed the caller should emit a normal call, otherwise try to get
3811 the result in TARGET, if convenient. */
902de8ed 3812
53800dbe 3813static rtx
a65c4d64 3814expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
53800dbe 3815{
c2f47e15 3816 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3817 return NULL_RTX;
bf8e3599 3818
6ac5504b 3819#if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
6b531606 3820 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3821 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
6ac5504b 3822 {
3823 rtx arg1_rtx, arg2_rtx;
3824 rtx result, insn = NULL_RTX;
3825 tree fndecl, fn;
c2f47e15 3826 tree arg1 = CALL_EXPR_ARG (exp, 0);
3827 tree arg2 = CALL_EXPR_ARG (exp, 1);
a0c938f0 3828
957d0361 3829 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3830 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
6ac5504b 3831
3832 /* If we don't have POINTER_TYPE, call the function. */
3833 if (arg1_align == 0 || arg2_align == 0)
c2f47e15 3834 return NULL_RTX;
7a3f89b5 3835
6ac5504b 3836 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3837 arg1 = builtin_save_expr (arg1);
3838 arg2 = builtin_save_expr (arg2);
7a3f89b5 3839
d8ae1baa 3840 arg1_rtx = get_memory_rtx (arg1, NULL);
3841 arg2_rtx = get_memory_rtx (arg2, NULL);
53800dbe 3842
6ac5504b 3843#ifdef HAVE_cmpstrsi
3844 /* Try to call cmpstrsi. */
3845 if (HAVE_cmpstrsi)
3846 {
a0c938f0 3847 enum machine_mode insn_mode
6ac5504b 3848 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3849
3850 /* Make a place to write the result of the instruction. */
3851 result = target;
3852 if (! (result != 0
3853 && REG_P (result) && GET_MODE (result) == insn_mode
3854 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3855 result = gen_reg_rtx (insn_mode);
3856
3857 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3858 GEN_INT (MIN (arg1_align, arg2_align)));
3859 }
3860#endif
03fd9d2c 3861#ifdef HAVE_cmpstrnsi
6ac5504b 3862 /* Try to determine at least one length and call cmpstrnsi. */
a0c938f0 3863 if (!insn && HAVE_cmpstrnsi)
6ac5504b 3864 {
3865 tree len;
3866 rtx arg3_rtx;
3867
a0c938f0 3868 enum machine_mode insn_mode
6ac5504b 3869 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3870 tree len1 = c_strlen (arg1, 1);
3871 tree len2 = c_strlen (arg2, 1);
3872
3873 if (len1)
3874 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3875 if (len2)
3876 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3877
3878 /* If we don't have a constant length for the first, use the length
3879 of the second, if we know it. We don't require a constant for
3880 this case; some cost analysis could be done if both are available
3881 but neither is constant. For now, assume they're equally cheap,
3882 unless one has side effects. If both strings have constant lengths,
3883 use the smaller. */
3884
3885 if (!len1)
3886 len = len2;
3887 else if (!len2)
3888 len = len1;
3889 else if (TREE_SIDE_EFFECTS (len1))
3890 len = len2;
3891 else if (TREE_SIDE_EFFECTS (len2))
3892 len = len1;
3893 else if (TREE_CODE (len1) != INTEGER_CST)
3894 len = len2;
3895 else if (TREE_CODE (len2) != INTEGER_CST)
3896 len = len1;
3897 else if (tree_int_cst_lt (len1, len2))
3898 len = len1;
3899 else
3900 len = len2;
3901
3902 /* If both arguments have side effects, we cannot optimize. */
3903 if (!len || TREE_SIDE_EFFECTS (len))
6b961939 3904 goto do_libcall;
53800dbe 3905
8ec3c5c2 3906 arg3_rtx = expand_normal (len);
902de8ed 3907
6ac5504b 3908 /* Make a place to write the result of the instruction. */
3909 result = target;
3910 if (! (result != 0
3911 && REG_P (result) && GET_MODE (result) == insn_mode
3912 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3913 result = gen_reg_rtx (insn_mode);
53800dbe 3914
6ac5504b 3915 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3916 GEN_INT (MIN (arg1_align, arg2_align)));
3917 }
3918#endif
3f8aefe2 3919
6ac5504b 3920 if (insn)
3921 {
a65c4d64 3922 enum machine_mode mode;
6ac5504b 3923 emit_insn (insn);
3f8aefe2 3924
6ac5504b 3925 /* Return the value in the proper mode for this function. */
3926 mode = TYPE_MODE (TREE_TYPE (exp));
3927 if (GET_MODE (result) == mode)
3928 return result;
3929 if (target == 0)
3930 return convert_to_mode (mode, result, 0);
3931 convert_move (target, result, 0);
3932 return target;
3933 }
902de8ed 3934
6ac5504b 3935 /* Expand the library call ourselves using a stabilized argument
3936 list to avoid re-evaluating the function's arguments twice. */
2694880e 3937#ifdef HAVE_cmpstrnsi
6b961939 3938 do_libcall:
2694880e 3939#endif
6ac5504b 3940 fndecl = get_callee_fndecl (exp);
0568e9c1 3941 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
a65c4d64 3942 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3943 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
6ac5504b 3944 return expand_call (fn, target, target == const0_rtx);
3945 }
7a3f89b5 3946#endif
c2f47e15 3947 return NULL_RTX;
83d79705 3948}
53800dbe 3949
48e1416a 3950/* Expand expression EXP, which is a call to the strncmp builtin. Return
c2f47e15 3951 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
ed09096d 3952 the result in TARGET, if convenient. */
27d0c333 3953
ed09096d 3954static rtx
a65c4d64 3955expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3956 ATTRIBUTE_UNUSED enum machine_mode mode)
ed09096d 3957{
a65c4d64 3958 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
389dd41b 3959
c2f47e15 3960 if (!validate_arglist (exp,
3961 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3962 return NULL_RTX;
ed09096d 3963
6e34e617 3964 /* If c_strlen can determine an expression for one of the string
6ac5504b 3965 lengths, and it doesn't have side effects, then emit cmpstrnsi
7a3f89b5 3966 using length MIN(strlen(string)+1, arg3). */
6ac5504b 3967#ifdef HAVE_cmpstrnsi
3968 if (HAVE_cmpstrnsi)
7a3f89b5 3969 {
3970 tree len, len1, len2;
3971 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3972 rtx result, insn;
0b25db21 3973 tree fndecl, fn;
c2f47e15 3974 tree arg1 = CALL_EXPR_ARG (exp, 0);
3975 tree arg2 = CALL_EXPR_ARG (exp, 1);
3976 tree arg3 = CALL_EXPR_ARG (exp, 2);
6f428e8b 3977
957d0361 3978 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3979 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
7a3f89b5 3980 enum machine_mode insn_mode
6ac5504b 3981 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
bf8e3599 3982
681fab1e 3983 len1 = c_strlen (arg1, 1);
3984 len2 = c_strlen (arg2, 1);
7a3f89b5 3985
3986 if (len1)
389dd41b 3987 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
7a3f89b5 3988 if (len2)
389dd41b 3989 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
7a3f89b5 3990
3991 /* If we don't have a constant length for the first, use the length
3992 of the second, if we know it. We don't require a constant for
3993 this case; some cost analysis could be done if both are available
3994 but neither is constant. For now, assume they're equally cheap,
3995 unless one has side effects. If both strings have constant lengths,
3996 use the smaller. */
3997
3998 if (!len1)
3999 len = len2;
4000 else if (!len2)
4001 len = len1;
4002 else if (TREE_SIDE_EFFECTS (len1))
4003 len = len2;
4004 else if (TREE_SIDE_EFFECTS (len2))
4005 len = len1;
4006 else if (TREE_CODE (len1) != INTEGER_CST)
4007 len = len2;
4008 else if (TREE_CODE (len2) != INTEGER_CST)
4009 len = len1;
4010 else if (tree_int_cst_lt (len1, len2))
4011 len = len1;
4012 else
4013 len = len2;
6e34e617 4014
7a3f89b5 4015 /* If both arguments have side effects, we cannot optimize. */
4016 if (!len || TREE_SIDE_EFFECTS (len))
c2f47e15 4017 return NULL_RTX;
bf8e3599 4018
7a3f89b5 4019 /* The actual new length parameter is MIN(len,arg3). */
389dd41b 4020 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4021 fold_convert_loc (loc, TREE_TYPE (len), arg3));
7a3f89b5 4022
4023 /* If we don't have POINTER_TYPE, call the function. */
4024 if (arg1_align == 0 || arg2_align == 0)
c2f47e15 4025 return NULL_RTX;
7a3f89b5 4026
4027 /* Make a place to write the result of the instruction. */
4028 result = target;
4029 if (! (result != 0
8ad4c111 4030 && REG_P (result) && GET_MODE (result) == insn_mode
7a3f89b5 4031 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4032 result = gen_reg_rtx (insn_mode);
4033
a65c4d64 4034 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4035 arg1 = builtin_save_expr (arg1);
4036 arg2 = builtin_save_expr (arg2);
4037 len = builtin_save_expr (len);
27d0c333 4038
a65c4d64 4039 arg1_rtx = get_memory_rtx (arg1, len);
4040 arg2_rtx = get_memory_rtx (arg2, len);
4041 arg3_rtx = expand_normal (len);
4042 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4043 GEN_INT (MIN (arg1_align, arg2_align)));
4044 if (insn)
4045 {
4046 emit_insn (insn);
49f0327b 4047
a65c4d64 4048 /* Return the value in the proper mode for this function. */
4049 mode = TYPE_MODE (TREE_TYPE (exp));
4050 if (GET_MODE (result) == mode)
4051 return result;
4052 if (target == 0)
4053 return convert_to_mode (mode, result, 0);
4054 convert_move (target, result, 0);
4055 return target;
4056 }
27d0c333 4057
a65c4d64 4058 /* Expand the library call ourselves using a stabilized argument
4059 list to avoid re-evaluating the function's arguments twice. */
4060 fndecl = get_callee_fndecl (exp);
0568e9c1 4061 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4062 arg1, arg2, len);
a65c4d64 4063 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4064 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4065 return expand_call (fn, target, target == const0_rtx);
4066 }
4067#endif
c2f47e15 4068 return NULL_RTX;
49f0327b 4069}
4070
a66c9326 4071/* Expand a call to __builtin_saveregs, generating the result in TARGET,
4072 if that's convenient. */
902de8ed 4073
a66c9326 4074rtx
aecda0d6 4075expand_builtin_saveregs (void)
53800dbe 4076{
a66c9326 4077 rtx val, seq;
53800dbe 4078
4079 /* Don't do __builtin_saveregs more than once in a function.
4080 Save the result of the first call and reuse it. */
4081 if (saveregs_value != 0)
4082 return saveregs_value;
53800dbe 4083
a66c9326 4084 /* When this function is called, it means that registers must be
4085 saved on entry to this function. So we migrate the call to the
4086 first insn of this function. */
4087
4088 start_sequence ();
53800dbe 4089
a66c9326 4090 /* Do whatever the machine needs done in this case. */
45550790 4091 val = targetm.calls.expand_builtin_saveregs ();
53800dbe 4092
a66c9326 4093 seq = get_insns ();
4094 end_sequence ();
53800dbe 4095
a66c9326 4096 saveregs_value = val;
53800dbe 4097
31d3e01c 4098 /* Put the insns after the NOTE that starts the function. If this
4099 is inside a start_sequence, make the outer-level insn chain current, so
a66c9326 4100 the code is placed at the start of the function. */
4101 push_topmost_sequence ();
0ec80471 4102 emit_insn_after (seq, entry_of_function ());
a66c9326 4103 pop_topmost_sequence ();
4104
4105 return val;
53800dbe 4106}
4107
79012a9d 4108/* Expand a call to __builtin_next_arg. */
27d0c333 4109
53800dbe 4110static rtx
79012a9d 4111expand_builtin_next_arg (void)
53800dbe 4112{
79012a9d 4113 /* Checking arguments is already done in fold_builtin_next_arg
4114 that must be called before this function. */
940ddc5c 4115 return expand_binop (ptr_mode, add_optab,
abe32cce 4116 crtl->args.internal_arg_pointer,
4117 crtl->args.arg_offset_rtx,
53800dbe 4118 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4119}
4120
a66c9326 4121/* Make it easier for the backends by protecting the valist argument
4122 from multiple evaluations. */
4123
4124static tree
389dd41b 4125stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
a66c9326 4126{
5f57a8b1 4127 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4128
182cf5a9 4129 /* The current way of determining the type of valist is completely
4130 bogus. We should have the information on the va builtin instead. */
4131 if (!vatype)
4132 vatype = targetm.fn_abi_va_list (cfun->decl);
5f57a8b1 4133
4134 if (TREE_CODE (vatype) == ARRAY_TYPE)
a66c9326 4135 {
2d47cc32 4136 if (TREE_SIDE_EFFECTS (valist))
4137 valist = save_expr (valist);
11a61dea 4138
2d47cc32 4139 /* For this case, the backends will be expecting a pointer to
5f57a8b1 4140 vatype, but it's possible we've actually been given an array
4141 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
2d47cc32 4142 So fix it. */
4143 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8a15c04a 4144 {
5f57a8b1 4145 tree p1 = build_pointer_type (TREE_TYPE (vatype));
389dd41b 4146 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
8a15c04a 4147 }
a66c9326 4148 }
11a61dea 4149 else
a66c9326 4150 {
182cf5a9 4151 tree pt = build_pointer_type (vatype);
11a61dea 4152
2d47cc32 4153 if (! needs_lvalue)
4154 {
11a61dea 4155 if (! TREE_SIDE_EFFECTS (valist))
4156 return valist;
bf8e3599 4157
389dd41b 4158 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
a66c9326 4159 TREE_SIDE_EFFECTS (valist) = 1;
a66c9326 4160 }
2d47cc32 4161
11a61dea 4162 if (TREE_SIDE_EFFECTS (valist))
2d47cc32 4163 valist = save_expr (valist);
182cf5a9 4164 valist = fold_build2_loc (loc, MEM_REF,
4165 vatype, valist, build_int_cst (pt, 0));
a66c9326 4166 }
4167
4168 return valist;
4169}
4170
2e15d750 4171/* The "standard" definition of va_list is void*. */
4172
4173tree
4174std_build_builtin_va_list (void)
4175{
4176 return ptr_type_node;
4177}
4178
5f57a8b1 4179/* The "standard" abi va_list is va_list_type_node. */
4180
4181tree
4182std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4183{
4184 return va_list_type_node;
4185}
4186
4187/* The "standard" type of va_list is va_list_type_node. */
4188
4189tree
4190std_canonical_va_list_type (tree type)
4191{
4192 tree wtype, htype;
4193
4194 if (INDIRECT_REF_P (type))
4195 type = TREE_TYPE (type);
4196 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4197 type = TREE_TYPE (type);
5f57a8b1 4198 wtype = va_list_type_node;
4199 htype = type;
7b36f9ab 4200 /* Treat structure va_list types. */
4201 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4202 htype = TREE_TYPE (htype);
4203 else if (TREE_CODE (wtype) == ARRAY_TYPE)
5f57a8b1 4204 {
4205 /* If va_list is an array type, the argument may have decayed
4206 to a pointer type, e.g. by being passed to another function.
4207 In that case, unwrap both types so that we can compare the
4208 underlying records. */
4209 if (TREE_CODE (htype) == ARRAY_TYPE
4210 || POINTER_TYPE_P (htype))
4211 {
4212 wtype = TREE_TYPE (wtype);
4213 htype = TREE_TYPE (htype);
4214 }
4215 }
4216 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4217 return va_list_type_node;
4218
4219 return NULL_TREE;
4220}
4221
a66c9326 4222/* The "standard" implementation of va_start: just assign `nextarg' to
4223 the variable. */
27d0c333 4224
a66c9326 4225void
aecda0d6 4226std_expand_builtin_va_start (tree valist, rtx nextarg)
a66c9326 4227{
f03c17bc 4228 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4229 convert_move (va_r, nextarg, 0);
a66c9326 4230}
4231
c2f47e15 4232/* Expand EXP, a call to __builtin_va_start. */
27d0c333 4233
a66c9326 4234static rtx
c2f47e15 4235expand_builtin_va_start (tree exp)
a66c9326 4236{
4237 rtx nextarg;
c2f47e15 4238 tree valist;
389dd41b 4239 location_t loc = EXPR_LOCATION (exp);
a66c9326 4240
c2f47e15 4241 if (call_expr_nargs (exp) < 2)
cb166087 4242 {
389dd41b 4243 error_at (loc, "too few arguments to function %<va_start%>");
cb166087 4244 return const0_rtx;
4245 }
a66c9326 4246
c2f47e15 4247 if (fold_builtin_next_arg (exp, true))
79012a9d 4248 return const0_rtx;
7c2f0500 4249
79012a9d 4250 nextarg = expand_builtin_next_arg ();
389dd41b 4251 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
a66c9326 4252
8a58ed0a 4253 if (targetm.expand_builtin_va_start)
4254 targetm.expand_builtin_va_start (valist, nextarg);
4255 else
4256 std_expand_builtin_va_start (valist, nextarg);
a66c9326 4257
4258 return const0_rtx;
4259}
4260
a66c9326 4261/* The "standard" implementation of va_arg: read the value from the
4262 current (padded) address and increment by the (padded) size. */
f7c44134 4263
e0eca1fa 4264tree
75a70cf9 4265std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4266 gimple_seq *post_p)
fcdd3ab3 4267{
52cc2eeb 4268 tree addr, t, type_size, rounded_size, valist_tmp;
b8e8e485 4269 unsigned HOST_WIDE_INT align, boundary;
bef380a4 4270 bool indirect;
52cc2eeb 4271
4272#ifdef ARGS_GROW_DOWNWARD
4273 /* All of the alignment and movement below is for args-grow-up machines.
4274 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4275 implement their own specialized gimplify_va_arg_expr routines. */
64db345d 4276 gcc_unreachable ();
52cc2eeb 4277#endif
fcdd3ab3 4278
bef380a4 4279 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4280 if (indirect)
4281 type = build_pointer_type (type);
4282
52cc2eeb 4283 align = PARM_BOUNDARY / BITS_PER_UNIT;
bd99ba64 4284 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
c8b779eb 4285
4286 /* When we align parameter on stack for caller, if the parameter
befa808c 4287 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4288 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
c8b779eb 4289 here with caller. */
befa808c 4290 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4291 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
c8b779eb 4292
4293 boundary /= BITS_PER_UNIT;
fcdd3ab3 4294
52cc2eeb 4295 /* Hoist the valist value into a temporary for the moment. */
ecdeeb37 4296 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4297
fcdd3ab3 4298 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4299 requires greater alignment, we must perform dynamic alignment. */
85c1f587 4300 if (boundary > align
4301 && !integer_zerop (TYPE_SIZE (type)))
fcdd3ab3 4302 {
41076ef6 4303 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
2cc66f2a 4304 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
52cc2eeb 4305 gimplify_and_add (t, pre_p);
4306
41076ef6 4307 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
a0553bff 4308 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4309 valist_tmp,
4310 build_int_cst (TREE_TYPE (valist), -boundary)));
ecdeeb37 4311 gimplify_and_add (t, pre_p);
fcdd3ab3 4312 }
c5dc0c32 4313 else
4314 boundary = align;
4315
4316 /* If the actual alignment is less than the alignment of the type,
4317 adjust the type accordingly so that we don't assume strict alignment
f0b5f617 4318 when dereferencing the pointer. */
c5dc0c32 4319 boundary *= BITS_PER_UNIT;
4320 if (boundary < TYPE_ALIGN (type))
4321 {
4322 type = build_variant_type_copy (type);
4323 TYPE_ALIGN (type) = boundary;
4324 }
fcdd3ab3 4325
bcff3604 4326 /* Compute the rounded size of the type. */
52cc2eeb 4327 type_size = size_in_bytes (type);
4328 rounded_size = round_up (type_size, align);
4329
fcdd3ab3 4330 /* Reduce rounded_size so it's sharable with the postqueue. */
4331 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4332
4333 /* Get AP. */
ecdeeb37 4334 addr = valist_tmp;
52cc2eeb 4335 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
fcdd3ab3 4336 {
4337 /* Small args are padded downward. */
389dd41b 4338 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4339 rounded_size, size_int (align));
49d00087 4340 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4341 size_binop (MINUS_EXPR, rounded_size, type_size));
2cc66f2a 4342 addr = fold_build_pointer_plus (addr, t);
fcdd3ab3 4343 }
4344
fcdd3ab3 4345 /* Compute new value for AP. */
2cc66f2a 4346 t = fold_build_pointer_plus (valist_tmp, rounded_size);
41076ef6 4347 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
ecdeeb37 4348 gimplify_and_add (t, pre_p);
e0eca1fa 4349
4350 addr = fold_convert (build_pointer_type (type), addr);
fcdd3ab3 4351
bef380a4 4352 if (indirect)
f2462d6c 4353 addr = build_va_arg_indirect_ref (addr);
4354
4355 return build_va_arg_indirect_ref (addr);
4356}
a0930a69 4357
f2462d6c 4358/* Build an indirect-ref expression over the given TREE, which represents a
4359 piece of a va_arg() expansion. */
4360tree
4361build_va_arg_indirect_ref (tree addr)
4362{
5c9d7b33 4363 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
f2462d6c 4364
a0930a69 4365 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4366 mf_mark (addr);
bef380a4 4367
a0930a69 4368 return addr;
433006a9 4369}
4370
fcdd3ab3 4371/* Return a dummy expression of type TYPE in order to keep going after an
4372 error. */
4373
4374static tree
4375dummy_object (tree type)
4376{
535664e3 4377 tree t = build_int_cst (build_pointer_type (type), 0);
377bc54c 4378 return build2 (MEM_REF, type, t, t);
fcdd3ab3 4379}
4380
2799a2b7 4381/* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4382 builtin function, but a very special sort of operator. */
fcdd3ab3 4383
4384enum gimplify_status
75a70cf9 4385gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
fcdd3ab3 4386{
5f57a8b1 4387 tree promoted_type, have_va_type;
fcdd3ab3 4388 tree valist = TREE_OPERAND (*expr_p, 0);
4389 tree type = TREE_TYPE (*expr_p);
4390 tree t;
389dd41b 4391 location_t loc = EXPR_LOCATION (*expr_p);
fcdd3ab3 4392
4393 /* Verify that valist is of the proper type. */
fcdd3ab3 4394 have_va_type = TREE_TYPE (valist);
f43dda35 4395 if (have_va_type == error_mark_node)
4396 return GS_ERROR;
5f57a8b1 4397 have_va_type = targetm.canonical_va_list_type (have_va_type);
f43dda35 4398
5f57a8b1 4399 if (have_va_type == NULL_TREE)
fcdd3ab3 4400 {
e60a6f7b 4401 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
e0eca1fa 4402 return GS_ERROR;
fcdd3ab3 4403 }
4404
4405 /* Generate a diagnostic for requesting data of a type that cannot
4406 be passed through `...' due to type promotion at the call site. */
5f57a8b1 4407 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
fcdd3ab3 4408 != type)
4409 {
4410 static bool gave_help;
a52d5726 4411 bool warned;
fcdd3ab3 4412
4413 /* Unfortunately, this is merely undefined, rather than a constraint
4414 violation, so we cannot make this an error. If this call is never
4415 executed, the program is still strictly conforming. */
e60a6f7b 4416 warned = warning_at (loc, 0,
4417 "%qT is promoted to %qT when passed through %<...%>",
4418 type, promoted_type);
a52d5726 4419 if (!gave_help && warned)
fcdd3ab3 4420 {
4421 gave_help = true;
e60a6f7b 4422 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4423 promoted_type, type);
fcdd3ab3 4424 }
4425
4426 /* We can, however, treat "undefined" any way we please.
4427 Call abort to encourage the user to fix the program. */
a52d5726 4428 if (warned)
e60a6f7b 4429 inform (loc, "if this code is reached, the program will abort");
ad0e7a06 4430 /* Before the abort, allow the evaluation of the va_list
4431 expression to exit or longjmp. */
4432 gimplify_and_add (valist, pre_p);
389dd41b 4433 t = build_call_expr_loc (loc,
b9a16870 4434 builtin_decl_implicit (BUILT_IN_TRAP), 0);
75a70cf9 4435 gimplify_and_add (t, pre_p);
fcdd3ab3 4436
4437 /* This is dead code, but go ahead and finish so that the
4438 mode of the result comes out right. */
4439 *expr_p = dummy_object (type);
4440 return GS_ALL_DONE;
4441 }
4442 else
4443 {
4444 /* Make it easier for the backends by protecting the valist argument
a0c938f0 4445 from multiple evaluations. */
5f57a8b1 4446 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
e0eca1fa 4447 {
4448 /* For this case, the backends will be expecting a pointer to
5f57a8b1 4449 TREE_TYPE (abi), but it's possible we've
4450 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
e0eca1fa 4451 So fix it. */
4452 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4453 {
5f57a8b1 4454 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
389dd41b 4455 valist = fold_convert_loc (loc, p1,
4456 build_fold_addr_expr_loc (loc, valist));
e0eca1fa 4457 }
75a70cf9 4458
e0eca1fa 4459 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4460 }
4461 else
4462 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
fcdd3ab3 4463
17a6380d 4464 if (!targetm.gimplify_va_arg_expr)
75a70cf9 4465 /* FIXME: Once most targets are converted we should merely
89f18f73 4466 assert this is non-null. */
fcdd3ab3 4467 return GS_ALL_DONE;
4468
17a6380d 4469 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
fcdd3ab3 4470 return GS_OK;
4471 }
4472}
4473
c2f47e15 4474/* Expand EXP, a call to __builtin_va_end. */
f7c44134 4475
a66c9326 4476static rtx
c2f47e15 4477expand_builtin_va_end (tree exp)
a66c9326 4478{
c2f47e15 4479 tree valist = CALL_EXPR_ARG (exp, 0);
8a15c04a 4480
8a15c04a 4481 /* Evaluate for side effects, if needed. I hate macros that don't
4482 do that. */
4483 if (TREE_SIDE_EFFECTS (valist))
4484 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
a66c9326 4485
4486 return const0_rtx;
4487}
4488
c2f47e15 4489/* Expand EXP, a call to __builtin_va_copy. We do this as a
a66c9326 4490 builtin rather than just as an assignment in stdarg.h because of the
4491 nastiness of array-type va_list types. */
f7c44134 4492
a66c9326 4493static rtx
c2f47e15 4494expand_builtin_va_copy (tree exp)
a66c9326 4495{
4496 tree dst, src, t;
389dd41b 4497 location_t loc = EXPR_LOCATION (exp);
a66c9326 4498
c2f47e15 4499 dst = CALL_EXPR_ARG (exp, 0);
4500 src = CALL_EXPR_ARG (exp, 1);
a66c9326 4501
389dd41b 4502 dst = stabilize_va_list_loc (loc, dst, 1);
4503 src = stabilize_va_list_loc (loc, src, 0);
a66c9326 4504
5f57a8b1 4505 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4506
4507 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
a66c9326 4508 {
5f57a8b1 4509 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
a66c9326 4510 TREE_SIDE_EFFECTS (t) = 1;
4511 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4512 }
4513 else
4514 {
11a61dea 4515 rtx dstb, srcb, size;
4516
4517 /* Evaluate to pointers. */
4518 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4519 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5f57a8b1 4520 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4521 NULL_RTX, VOIDmode, EXPAND_NORMAL);
11a61dea 4522
85d654dd 4523 dstb = convert_memory_address (Pmode, dstb);
4524 srcb = convert_memory_address (Pmode, srcb);
726ec87c 4525
11a61dea 4526 /* "Dereference" to BLKmode memories. */
4527 dstb = gen_rtx_MEM (BLKmode, dstb);
ab6ab77e 4528 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5f57a8b1 4529 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
11a61dea 4530 srcb = gen_rtx_MEM (BLKmode, srcb);
ab6ab77e 4531 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5f57a8b1 4532 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
11a61dea 4533
4534 /* Copy. */
0378dbdc 4535 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
a66c9326 4536 }
4537
4538 return const0_rtx;
4539}
4540
53800dbe 4541/* Expand a call to one of the builtin functions __builtin_frame_address or
4542 __builtin_return_address. */
27d0c333 4543
53800dbe 4544static rtx
c2f47e15 4545expand_builtin_frame_address (tree fndecl, tree exp)
53800dbe 4546{
53800dbe 4547 /* The argument must be a nonnegative integer constant.
4548 It counts the number of frames to scan up the stack.
4549 The value is the return address saved in that frame. */
c2f47e15 4550 if (call_expr_nargs (exp) == 0)
53800dbe 4551 /* Warning about missing arg was already issued. */
4552 return const0_rtx;
c2f47e15 4553 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
53800dbe 4554 {
4555 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
07e3a3d2 4556 error ("invalid argument to %<__builtin_frame_address%>");
53800dbe 4557 else
07e3a3d2 4558 error ("invalid argument to %<__builtin_return_address%>");
53800dbe 4559 return const0_rtx;
4560 }
4561 else
4562 {
27d0c333 4563 rtx tem
4564 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
c2f47e15 4565 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
53800dbe 4566
4567 /* Some ports cannot access arbitrary stack frames. */
4568 if (tem == NULL)
4569 {
4570 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
c3ceba8e 4571 warning (0, "unsupported argument to %<__builtin_frame_address%>");
53800dbe 4572 else
c3ceba8e 4573 warning (0, "unsupported argument to %<__builtin_return_address%>");
53800dbe 4574 return const0_rtx;
4575 }
4576
4577 /* For __builtin_frame_address, return what we've got. */
4578 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4579 return tem;
4580
8ad4c111 4581 if (!REG_P (tem)
53800dbe 4582 && ! CONSTANT_P (tem))
99182918 4583 tem = copy_addr_to_reg (tem);
53800dbe 4584 return tem;
4585 }
4586}
4587
990495a7 4588/* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5be42b39 4589 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4590 is the same as for allocate_dynamic_stack_space. */
15c6cf6b 4591
53800dbe 4592static rtx
5be42b39 4593expand_builtin_alloca (tree exp, bool cannot_accumulate)
53800dbe 4594{
4595 rtx op0;
15c6cf6b 4596 rtx result;
581bf1c2 4597 bool valid_arglist;
4598 unsigned int align;
4599 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4600 == BUILT_IN_ALLOCA_WITH_ALIGN);
53800dbe 4601
f883da84 4602 /* Emit normal call if we use mudflap. */
4603 if (flag_mudflap)
c2f47e15 4604 return NULL_RTX;
4ee9c684 4605
581bf1c2 4606 valid_arglist
4607 = (alloca_with_align
4608 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4609 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4610
4611 if (!valid_arglist)
c2f47e15 4612 return NULL_RTX;
53800dbe 4613
4614 /* Compute the argument. */
c2f47e15 4615 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
53800dbe 4616
581bf1c2 4617 /* Compute the alignment. */
4618 align = (alloca_with_align
4619 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4620 : BIGGEST_ALIGNMENT);
4621
53800dbe 4622 /* Allocate the desired space. */
581bf1c2 4623 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
85d654dd 4624 result = convert_memory_address (ptr_mode, result);
15c6cf6b 4625
4626 return result;
53800dbe 4627}
4628
74bdbe96 4629/* Expand a call to bswap builtin in EXP.
4630 Return NULL_RTX if a normal call should be emitted rather than expanding the
4631 function in-line. If convenient, the result should be placed in TARGET.
4632 SUBTARGET may be used as the target for computing one of EXP's operands. */
42791117 4633
4634static rtx
74bdbe96 4635expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4636 rtx subtarget)
42791117 4637{
42791117 4638 tree arg;
4639 rtx op0;
4640
c2f47e15 4641 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4642 return NULL_RTX;
42791117 4643
c2f47e15 4644 arg = CALL_EXPR_ARG (exp, 0);
74bdbe96 4645 op0 = expand_expr (arg,
4646 subtarget && GET_MODE (subtarget) == target_mode
4647 ? subtarget : NULL_RTX,
4648 target_mode, EXPAND_NORMAL);
4649 if (GET_MODE (op0) != target_mode)
4650 op0 = convert_to_mode (target_mode, op0, 1);
42791117 4651
74bdbe96 4652 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
42791117 4653
4654 gcc_assert (target);
4655
74bdbe96 4656 return convert_to_mode (target_mode, target, 1);
42791117 4657}
4658
c2f47e15 4659/* Expand a call to a unary builtin in EXP.
4660 Return NULL_RTX if a normal call should be emitted rather than expanding the
53800dbe 4661 function in-line. If convenient, the result should be placed in TARGET.
4662 SUBTARGET may be used as the target for computing one of EXP's operands. */
15c6cf6b 4663
53800dbe 4664static rtx
c2f47e15 4665expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
aecda0d6 4666 rtx subtarget, optab op_optab)
53800dbe 4667{
4668 rtx op0;
c2f47e15 4669
4670 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4671 return NULL_RTX;
53800dbe 4672
4673 /* Compute the argument. */
f97eea22 4674 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4675 (subtarget
4676 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4677 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
1db6d067 4678 VOIDmode, EXPAND_NORMAL);
6a08d0ab 4679 /* Compute op, into TARGET if possible.
53800dbe 4680 Set TARGET to wherever the result comes back. */
c2f47e15 4681 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
6aaa1f9e 4682 op_optab, op0, target, op_optab != clrsb_optab);
64db345d 4683 gcc_assert (target);
7d3f6cc7 4684
efb070c8 4685 return convert_to_mode (target_mode, target, 0);
53800dbe 4686}
89cfe6e5 4687
48e1416a 4688/* Expand a call to __builtin_expect. We just return our argument
5a74f77e 4689 as the builtin_expect semantic should've been already executed by
4690 tree branch prediction pass. */
89cfe6e5 4691
4692static rtx
c2f47e15 4693expand_builtin_expect (tree exp, rtx target)
89cfe6e5 4694{
1e4adcfc 4695 tree arg;
89cfe6e5 4696
c2f47e15 4697 if (call_expr_nargs (exp) < 2)
89cfe6e5 4698 return const0_rtx;
c2f47e15 4699 arg = CALL_EXPR_ARG (exp, 0);
89cfe6e5 4700
c2f47e15 4701 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5a74f77e 4702 /* When guessing was done, the hints should be already stripped away. */
07311427 4703 gcc_assert (!flag_guess_branch_prob
852f689e 4704 || optimize == 0 || seen_error ());
89cfe6e5 4705 return target;
4706}
689df48e 4707
fca0886c 4708/* Expand a call to __builtin_assume_aligned. We just return our first
4709 argument as the builtin_assume_aligned semantic should've been already
4710 executed by CCP. */
4711
4712static rtx
4713expand_builtin_assume_aligned (tree exp, rtx target)
4714{
4715 if (call_expr_nargs (exp) < 2)
4716 return const0_rtx;
4717 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4718 EXPAND_NORMAL);
4719 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4720 && (call_expr_nargs (exp) < 3
4721 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4722 return target;
4723}
4724
c22de3f0 4725void
aecda0d6 4726expand_builtin_trap (void)
a0ef1725 4727{
4728#ifdef HAVE_trap
4729 if (HAVE_trap)
4730 emit_insn (gen_trap ());
4731 else
4732#endif
4733 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4734 emit_barrier ();
4735}
78a74442 4736
d2b48f0c 4737/* Expand a call to __builtin_unreachable. We do nothing except emit
4738 a barrier saying that control flow will not pass here.
4739
4740 It is the responsibility of the program being compiled to ensure
4741 that control flow does never reach __builtin_unreachable. */
4742static void
4743expand_builtin_unreachable (void)
4744{
4745 emit_barrier ();
4746}
4747
c2f47e15 4748/* Expand EXP, a call to fabs, fabsf or fabsl.
4749 Return NULL_RTX if a normal call should be emitted rather than expanding
78a74442 4750 the function inline. If convenient, the result should be placed
4751 in TARGET. SUBTARGET may be used as the target for computing
4752 the operand. */
4753
4754static rtx
c2f47e15 4755expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
78a74442 4756{
4757 enum machine_mode mode;
4758 tree arg;
4759 rtx op0;
4760
c2f47e15 4761 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4762 return NULL_RTX;
78a74442 4763
c2f47e15 4764 arg = CALL_EXPR_ARG (exp, 0);
c7f617c2 4765 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
78a74442 4766 mode = TYPE_MODE (TREE_TYPE (arg));
1db6d067 4767 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
78a74442 4768 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4769}
4770
c2f47e15 4771/* Expand EXP, a call to copysign, copysignf, or copysignl.
270436f3 4772 Return NULL is a normal call should be emitted rather than expanding the
4773 function inline. If convenient, the result should be placed in TARGET.
4774 SUBTARGET may be used as the target for computing the operand. */
4775
4776static rtx
c2f47e15 4777expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
270436f3 4778{
4779 rtx op0, op1;
4780 tree arg;
4781
c2f47e15 4782 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4783 return NULL_RTX;
270436f3 4784
c2f47e15 4785 arg = CALL_EXPR_ARG (exp, 0);
8ec3c5c2 4786 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
270436f3 4787
c2f47e15 4788 arg = CALL_EXPR_ARG (exp, 1);
8ec3c5c2 4789 op1 = expand_normal (arg);
270436f3 4790
4791 return expand_copysign (op0, op1, target);
4792}
4793
19bf118a 4794/* Create a new constant string literal and return a char* pointer to it.
4795 The STRING_CST value is the LEN characters at STR. */
1e8e9920 4796tree
19bf118a 4797build_string_literal (int len, const char *str)
4798{
4799 tree t, elem, index, type;
4800
4801 t = build_string (len, str);
4802 elem = build_type_variant (char_type_node, 1, 0);
40238f64 4803 index = build_index_type (size_int (len - 1));
19bf118a 4804 type = build_array_type (elem, index);
4805 TREE_TYPE (t) = type;
4806 TREE_CONSTANT (t) = 1;
4807 TREE_READONLY (t) = 1;
4808 TREE_STATIC (t) = 1;
4809
19bf118a 4810 type = build_pointer_type (elem);
40238f64 4811 t = build1 (ADDR_EXPR, type,
4812 build4 (ARRAY_REF, elem,
4813 t, integer_zero_node, NULL_TREE, NULL_TREE));
19bf118a 4814 return t;
4815}
4816
ac8fb6db 4817/* Expand a call to __builtin___clear_cache. */
4818
4819static rtx
4820expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4821{
4822#ifndef HAVE_clear_cache
4823#ifdef CLEAR_INSN_CACHE
4824 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4825 does something. Just do the default expansion to a call to
4826 __clear_cache(). */
4827 return NULL_RTX;
4828#else
4829 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4830 does nothing. There is no need to call it. Do nothing. */
4831 return const0_rtx;
4832#endif /* CLEAR_INSN_CACHE */
4833#else
4834 /* We have a "clear_cache" insn, and it will handle everything. */
4835 tree begin, end;
4836 rtx begin_rtx, end_rtx;
ac8fb6db 4837
4838 /* We must not expand to a library call. If we did, any
4839 fallback library function in libgcc that might contain a call to
4840 __builtin___clear_cache() would recurse infinitely. */
4841 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4842 {
4843 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4844 return const0_rtx;
4845 }
4846
4847 if (HAVE_clear_cache)
4848 {
8786db1e 4849 struct expand_operand ops[2];
ac8fb6db 4850
4851 begin = CALL_EXPR_ARG (exp, 0);
4852 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
ac8fb6db 4853
4854 end = CALL_EXPR_ARG (exp, 1);
4855 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
ac8fb6db 4856
8786db1e 4857 create_address_operand (&ops[0], begin_rtx);
4858 create_address_operand (&ops[1], end_rtx);
4859 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4860 return const0_rtx;
ac8fb6db 4861 }
4862 return const0_rtx;
4863#endif /* HAVE_clear_cache */
4864}
4865
4ee9c684 4866/* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4867
4868static rtx
4869round_trampoline_addr (rtx tramp)
4870{
4871 rtx temp, addend, mask;
4872
4873 /* If we don't need too much alignment, we'll have been guaranteed
4874 proper alignment by get_trampoline_type. */
4875 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4876 return tramp;
4877
4878 /* Round address up to desired boundary. */
4879 temp = gen_reg_rtx (Pmode);
4880 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4881 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4882
4883 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4884 temp, 0, OPTAB_LIB_WIDEN);
4885 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4886 temp, 0, OPTAB_LIB_WIDEN);
4887
4888 return tramp;
4889}
4890
4891static rtx
c307f106 4892expand_builtin_init_trampoline (tree exp, bool onstack)
4ee9c684 4893{
4894 tree t_tramp, t_func, t_chain;
82c7907c 4895 rtx m_tramp, r_tramp, r_chain, tmp;
4ee9c684 4896
c2f47e15 4897 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4ee9c684 4898 POINTER_TYPE, VOID_TYPE))
4899 return NULL_RTX;
4900
c2f47e15 4901 t_tramp = CALL_EXPR_ARG (exp, 0);
4902 t_func = CALL_EXPR_ARG (exp, 1);
4903 t_chain = CALL_EXPR_ARG (exp, 2);
4ee9c684 4904
8ec3c5c2 4905 r_tramp = expand_normal (t_tramp);
82c7907c 4906 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4907 MEM_NOTRAP_P (m_tramp) = 1;
4908
c307f106 4909 /* If ONSTACK, the TRAMP argument should be the address of a field
4910 within the local function's FRAME decl. Either way, let's see if
4911 we can fill in the MEM_ATTRs for this memory. */
82c7907c 4912 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4913 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4914 true, 0);
4915
c307f106 4916 /* Creator of a heap trampoline is responsible for making sure the
4917 address is aligned to at least STACK_BOUNDARY. Normally malloc
4918 will ensure this anyhow. */
82c7907c 4919 tmp = round_trampoline_addr (r_tramp);
4920 if (tmp != r_tramp)
4921 {
4922 m_tramp = change_address (m_tramp, BLKmode, tmp);
4923 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5b2a69fa 4924 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
82c7907c 4925 }
4926
4927 /* The FUNC argument should be the address of the nested function.
4928 Extract the actual function decl to pass to the hook. */
4929 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4930 t_func = TREE_OPERAND (t_func, 0);
4931 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4932
8ec3c5c2 4933 r_chain = expand_normal (t_chain);
4ee9c684 4934
4935 /* Generate insns to initialize the trampoline. */
82c7907c 4936 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4ee9c684 4937
c307f106 4938 if (onstack)
4939 {
4940 trampolines_created = 1;
8bc8a8f4 4941
c307f106 4942 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4943 "trampoline generated for nested function %qD", t_func);
4944 }
8bc8a8f4 4945
4ee9c684 4946 return const0_rtx;
4947}
4948
4949static rtx
c2f47e15 4950expand_builtin_adjust_trampoline (tree exp)
4ee9c684 4951{
4952 rtx tramp;
4953
c2f47e15 4954 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4ee9c684 4955 return NULL_RTX;
4956
c2f47e15 4957 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4ee9c684 4958 tramp = round_trampoline_addr (tramp);
82c7907c 4959 if (targetm.calls.trampoline_adjust_address)
4960 tramp = targetm.calls.trampoline_adjust_address (tramp);
4ee9c684 4961
4962 return tramp;
4963}
4964
93f564d6 4965/* Expand the call EXP to the built-in signbit, signbitf or signbitl
4966 function. The function first checks whether the back end provides
4967 an insn to implement signbit for the respective mode. If not, it
4968 checks whether the floating point format of the value is such that
4969 the sign bit can be extracted. If that is not the case, the
4970 function returns NULL_RTX to indicate that a normal call should be
4971 emitted rather than expanding the function in-line. EXP is the
4972 expression that is a call to the builtin function; if convenient,
4973 the result should be placed in TARGET. */
27f261ef 4974static rtx
4975expand_builtin_signbit (tree exp, rtx target)
4976{
4977 const struct real_format *fmt;
4978 enum machine_mode fmode, imode, rmode;
c2f47e15 4979 tree arg;
ca4f1f5b 4980 int word, bitpos;
27eda240 4981 enum insn_code icode;
27f261ef 4982 rtx temp;
389dd41b 4983 location_t loc = EXPR_LOCATION (exp);
27f261ef 4984
c2f47e15 4985 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4986 return NULL_RTX;
27f261ef 4987
c2f47e15 4988 arg = CALL_EXPR_ARG (exp, 0);
27f261ef 4989 fmode = TYPE_MODE (TREE_TYPE (arg));
4990 rmode = TYPE_MODE (TREE_TYPE (exp));
4991 fmt = REAL_MODE_FORMAT (fmode);
4992
93f564d6 4993 arg = builtin_save_expr (arg);
4994
4995 /* Expand the argument yielding a RTX expression. */
4996 temp = expand_normal (arg);
4997
4998 /* Check if the back end provides an insn that handles signbit for the
4999 argument's mode. */
d6bf3b14 5000 icode = optab_handler (signbit_optab, fmode);
27eda240 5001 if (icode != CODE_FOR_nothing)
93f564d6 5002 {
4e2a2fb4 5003 rtx last = get_last_insn ();
93f564d6 5004 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4e2a2fb4 5005 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5006 return target;
5007 delete_insns_since (last);
93f564d6 5008 }
5009
27f261ef 5010 /* For floating point formats without a sign bit, implement signbit
5011 as "ARG < 0.0". */
8d564692 5012 bitpos = fmt->signbit_ro;
ca4f1f5b 5013 if (bitpos < 0)
27f261ef 5014 {
5015 /* But we can't do this if the format supports signed zero. */
5016 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
c2f47e15 5017 return NULL_RTX;
27f261ef 5018
389dd41b 5019 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
49d00087 5020 build_real (TREE_TYPE (arg), dconst0));
27f261ef 5021 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5022 }
5023
ca4f1f5b 5024 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
27f261ef 5025 {
ca4f1f5b 5026 imode = int_mode_for_mode (fmode);
5027 if (imode == BLKmode)
c2f47e15 5028 return NULL_RTX;
ca4f1f5b 5029 temp = gen_lowpart (imode, temp);
24fd4260 5030 }
5031 else
5032 {
ca4f1f5b 5033 imode = word_mode;
5034 /* Handle targets with different FP word orders. */
5035 if (FLOAT_WORDS_BIG_ENDIAN)
a0c938f0 5036 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
ca4f1f5b 5037 else
a0c938f0 5038 word = bitpos / BITS_PER_WORD;
ca4f1f5b 5039 temp = operand_subword_force (temp, word, fmode);
5040 bitpos = bitpos % BITS_PER_WORD;
5041 }
5042
44b0f1d0 5043 /* Force the intermediate word_mode (or narrower) result into a
5044 register. This avoids attempting to create paradoxical SUBREGs
5045 of floating point modes below. */
5046 temp = force_reg (imode, temp);
5047
ca4f1f5b 5048 /* If the bitpos is within the "result mode" lowpart, the operation
5049 can be implement with a single bitwise AND. Otherwise, we need
5050 a right shift and an AND. */
5051
5052 if (bitpos < GET_MODE_BITSIZE (rmode))
5053 {
3e052aec 5054 double_int mask = double_int_setbit (double_int_zero, bitpos);
27f261ef 5055
4a46f016 5056 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
ca4f1f5b 5057 temp = gen_lowpart (rmode, temp);
24fd4260 5058 temp = expand_binop (rmode, and_optab, temp,
3e052aec 5059 immed_double_int_const (mask, rmode),
ca4f1f5b 5060 NULL_RTX, 1, OPTAB_LIB_WIDEN);
27f261ef 5061 }
ca4f1f5b 5062 else
5063 {
5064 /* Perform a logical right shift to place the signbit in the least
a0c938f0 5065 significant bit, then truncate the result to the desired mode
ca4f1f5b 5066 and mask just this bit. */
f5ff0b21 5067 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
ca4f1f5b 5068 temp = gen_lowpart (rmode, temp);
5069 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5070 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5071 }
5072
27f261ef 5073 return temp;
5074}
73673831 5075
5076/* Expand fork or exec calls. TARGET is the desired target of the
c2f47e15 5077 call. EXP is the call. FN is the
73673831 5078 identificator of the actual function. IGNORE is nonzero if the
5079 value is to be ignored. */
5080
5081static rtx
c2f47e15 5082expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
73673831 5083{
5084 tree id, decl;
5085 tree call;
5086
5087 /* If we are not profiling, just call the function. */
5088 if (!profile_arc_flag)
5089 return NULL_RTX;
5090
5091 /* Otherwise call the wrapper. This should be equivalent for the rest of
5092 compiler, so the code does not diverge, and the wrapper may run the
9c9bad97 5093 code necessary for keeping the profiling sane. */
73673831 5094
5095 switch (DECL_FUNCTION_CODE (fn))
5096 {
5097 case BUILT_IN_FORK:
5098 id = get_identifier ("__gcov_fork");
5099 break;
5100
5101 case BUILT_IN_EXECL:
5102 id = get_identifier ("__gcov_execl");
5103 break;
5104
5105 case BUILT_IN_EXECV:
5106 id = get_identifier ("__gcov_execv");
5107 break;
5108
5109 case BUILT_IN_EXECLP:
5110 id = get_identifier ("__gcov_execlp");
5111 break;
5112
5113 case BUILT_IN_EXECLE:
5114 id = get_identifier ("__gcov_execle");
5115 break;
5116
5117 case BUILT_IN_EXECVP:
5118 id = get_identifier ("__gcov_execvp");
5119 break;
5120
5121 case BUILT_IN_EXECVE:
5122 id = get_identifier ("__gcov_execve");
5123 break;
5124
5125 default:
64db345d 5126 gcc_unreachable ();
73673831 5127 }
5128
e60a6f7b 5129 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5130 FUNCTION_DECL, id, TREE_TYPE (fn));
73673831 5131 DECL_EXTERNAL (decl) = 1;
5132 TREE_PUBLIC (decl) = 1;
5133 DECL_ARTIFICIAL (decl) = 1;
5134 TREE_NOTHROW (decl) = 1;
e82d310b 5135 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5136 DECL_VISIBILITY_SPECIFIED (decl) = 1;
389dd41b 5137 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
73673831 5138 return expand_call (call, target, ignore);
c2f47e15 5139 }
48e1416a 5140
b6a5fc45 5141
5142\f
3e272de8 5143/* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5144 the pointer in these functions is void*, the tree optimizers may remove
5145 casts. The mode computed in expand_builtin isn't reliable either, due
5146 to __sync_bool_compare_and_swap.
5147
5148 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5149 group of builtins. This gives us log2 of the mode size. */
5150
5151static inline enum machine_mode
5152get_builtin_sync_mode (int fcode_diff)
5153{
ad3a13b5 5154 /* The size is not negotiable, so ask not to get BLKmode in return
5155 if the target indicates that a smaller size would be better. */
5156 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
3e272de8 5157}
5158
041e0215 5159/* Expand the memory expression LOC and return the appropriate memory operand
5160 for the builtin_sync operations. */
5161
5162static rtx
5163get_builtin_sync_mem (tree loc, enum machine_mode mode)
5164{
5165 rtx addr, mem;
5166
7f4d56ad 5167 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5168 addr = convert_memory_address (Pmode, addr);
041e0215 5169
5170 /* Note that we explicitly do not want any alias information for this
5171 memory, so that we kill all other live memories. Otherwise we don't
5172 satisfy the full barrier semantics of the intrinsic. */
5173 mem = validize_mem (gen_rtx_MEM (mode, addr));
5174
153c3b50 5175 /* The alignment needs to be at least according to that of the mode. */
5176 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
957d0361 5177 get_pointer_alignment (loc)));
c94cfd1c 5178 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
041e0215 5179 MEM_VOLATILE_P (mem) = 1;
5180
5181 return mem;
5182}
5183
1cd6e20d 5184/* Make sure an argument is in the right mode.
5185 EXP is the tree argument.
5186 MODE is the mode it should be in. */
5187
5188static rtx
5189expand_expr_force_mode (tree exp, enum machine_mode mode)
5190{
5191 rtx val;
5192 enum machine_mode old_mode;
5193
5194 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5195 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5196 of CONST_INTs, where we know the old_mode only from the call argument. */
5197
5198 old_mode = GET_MODE (val);
5199 if (old_mode == VOIDmode)
5200 old_mode = TYPE_MODE (TREE_TYPE (exp));
5201 val = convert_modes (mode, old_mode, val, 1);
5202 return val;
5203}
5204
5205
b6a5fc45 5206/* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
c2f47e15 5207 EXP is the CALL_EXPR. CODE is the rtx code
b6a5fc45 5208 that corresponds to the arithmetic or logical operation from the name;
5209 an exception here is that NOT actually means NAND. TARGET is an optional
5210 place for us to store the results; AFTER is true if this is the
1cd6e20d 5211 fetch_and_xxx form. */
b6a5fc45 5212
5213static rtx
c2f47e15 5214expand_builtin_sync_operation (enum machine_mode mode, tree exp,
3e272de8 5215 enum rtx_code code, bool after,
1cd6e20d 5216 rtx target)
b6a5fc45 5217{
041e0215 5218 rtx val, mem;
e60a6f7b 5219 location_t loc = EXPR_LOCATION (exp);
b6a5fc45 5220
cf73e559 5221 if (code == NOT && warn_sync_nand)
5222 {
5223 tree fndecl = get_callee_fndecl (exp);
5224 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5225
5226 static bool warned_f_a_n, warned_n_a_f;
5227
5228 switch (fcode)
5229 {
2797f13a 5230 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5231 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5232 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5233 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5234 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
cf73e559 5235 if (warned_f_a_n)
5236 break;
5237
b9a16870 5238 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
e60a6f7b 5239 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
cf73e559 5240 warned_f_a_n = true;
5241 break;
5242
2797f13a 5243 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5244 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5245 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5246 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5247 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
cf73e559 5248 if (warned_n_a_f)
5249 break;
5250
b9a16870 5251 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
e60a6f7b 5252 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
cf73e559 5253 warned_n_a_f = true;
5254 break;
5255
5256 default:
5257 gcc_unreachable ();
5258 }
5259 }
5260
b6a5fc45 5261 /* Expand the operands. */
c2f47e15 5262 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
1cd6e20d 5263 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
b6a5fc45 5264
1cd6e20d 5265 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5266 after);
b6a5fc45 5267}
5268
5269/* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
c2f47e15 5270 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
b6a5fc45 5271 true if this is the boolean form. TARGET is a place for us to store the
5272 results; this is NOT optional if IS_BOOL is true. */
5273
5274static rtx
c2f47e15 5275expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
3e272de8 5276 bool is_bool, rtx target)
b6a5fc45 5277{
041e0215 5278 rtx old_val, new_val, mem;
ba885f6a 5279 rtx *pbool, *poval;
b6a5fc45 5280
5281 /* Expand the operands. */
c2f47e15 5282 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
1cd6e20d 5283 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5284 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
b6a5fc45 5285
ba885f6a 5286 pbool = poval = NULL;
5287 if (target != const0_rtx)
5288 {
5289 if (is_bool)
5290 pbool = &target;
5291 else
5292 poval = &target;
5293 }
5294 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5295 false, MEMMODEL_SEQ_CST,
5296 MEMMODEL_SEQ_CST))
1cd6e20d 5297 return NULL_RTX;
c2f47e15 5298
1cd6e20d 5299 return target;
b6a5fc45 5300}
5301
5302/* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5303 general form is actually an atomic exchange, and some targets only
5304 support a reduced form with the second argument being a constant 1.
48e1416a 5305 EXP is the CALL_EXPR; TARGET is an optional place for us to store
c2f47e15 5306 the results. */
b6a5fc45 5307
5308static rtx
2797f13a 5309expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
1cd6e20d 5310 rtx target)
b6a5fc45 5311{
041e0215 5312 rtx val, mem;
b6a5fc45 5313
5314 /* Expand the operands. */
c2f47e15 5315 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
1cd6e20d 5316 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5317
7821cde1 5318 return expand_sync_lock_test_and_set (target, mem, val);
1cd6e20d 5319}
5320
5321/* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5322
5323static void
5324expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5325{
5326 rtx mem;
5327
5328 /* Expand the operands. */
5329 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5330
8808bf16 5331 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
1cd6e20d 5332}
5333
5334/* Given an integer representing an ``enum memmodel'', verify its
5335 correctness and return the memory model enum. */
5336
5337static enum memmodel
5338get_memmodel (tree exp)
5339{
5340 rtx op;
5341
5342 /* If the parameter is not a constant, it's a run time value so we'll just
5343 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5344 if (TREE_CODE (exp) != INTEGER_CST)
5345 return MEMMODEL_SEQ_CST;
5346
5347 op = expand_normal (exp);
5348 if (INTVAL (op) < 0 || INTVAL (op) >= MEMMODEL_LAST)
5349 {
5350 warning (OPT_Winvalid_memory_model,
5351 "invalid memory model argument to builtin");
5352 return MEMMODEL_SEQ_CST;
5353 }
5354 return (enum memmodel) INTVAL (op);
5355}
5356
5357/* Expand the __atomic_exchange intrinsic:
5358 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5359 EXP is the CALL_EXPR.
5360 TARGET is an optional place for us to store the results. */
5361
5362static rtx
5363expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5364{
5365 rtx val, mem;
5366 enum memmodel model;
5367
5368 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5369 if (model == MEMMODEL_CONSUME)
5370 {
5371 error ("invalid memory model for %<__atomic_exchange%>");
5372 return NULL_RTX;
5373 }
5374
5375 if (!flag_inline_atomics)
5376 return NULL_RTX;
5377
5378 /* Expand the operands. */
5379 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5380 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5381
7821cde1 5382 return expand_atomic_exchange (target, mem, val, model);
1cd6e20d 5383}
5384
5385/* Expand the __atomic_compare_exchange intrinsic:
5386 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5387 TYPE desired, BOOL weak,
5388 enum memmodel success,
5389 enum memmodel failure)
5390 EXP is the CALL_EXPR.
5391 TARGET is an optional place for us to store the results. */
5392
5393static rtx
5394expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5395 rtx target)
5396{
5397 rtx expect, desired, mem, oldval;
5398 enum memmodel success, failure;
5399 tree weak;
5400 bool is_weak;
5401
5402 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5403 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5404
5405 if (failure == MEMMODEL_RELEASE || failure == MEMMODEL_ACQ_REL)
5406 {
5407 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5408 return NULL_RTX;
5409 }
5410
5411 if (failure > success)
5412 {
5413 error ("failure memory model cannot be stronger than success "
5414 "memory model for %<__atomic_compare_exchange%>");
5415 return NULL_RTX;
5416 }
5417
5418 if (!flag_inline_atomics)
5419 return NULL_RTX;
5420
5421 /* Expand the operands. */
5422 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5423
5424 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5425 expect = convert_memory_address (Pmode, expect);
5426 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5427
5428 weak = CALL_EXPR_ARG (exp, 3);
5429 is_weak = false;
5430 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5431 is_weak = true;
5432
5433 oldval = copy_to_reg (gen_rtx_MEM (mode, expect));
5434
ba885f6a 5435 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5436 &oldval, mem, oldval, desired,
5437 is_weak, success, failure))
1cd6e20d 5438 return NULL_RTX;
5439
5440 emit_move_insn (gen_rtx_MEM (mode, expect), oldval);
5441 return target;
5442}
5443
5444/* Expand the __atomic_load intrinsic:
5445 TYPE __atomic_load (TYPE *object, enum memmodel)
5446 EXP is the CALL_EXPR.
5447 TARGET is an optional place for us to store the results. */
5448
5449static rtx
5450expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5451{
5452 rtx mem;
5453 enum memmodel model;
5454
5455 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5456 if (model == MEMMODEL_RELEASE
5457 || model == MEMMODEL_ACQ_REL)
5458 {
5459 error ("invalid memory model for %<__atomic_load%>");
5460 return NULL_RTX;
5461 }
5462
5463 if (!flag_inline_atomics)
5464 return NULL_RTX;
5465
5466 /* Expand the operand. */
5467 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5468
5469 return expand_atomic_load (target, mem, model);
5470}
5471
5472
5473/* Expand the __atomic_store intrinsic:
5474 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5475 EXP is the CALL_EXPR.
5476 TARGET is an optional place for us to store the results. */
5477
5478static rtx
5479expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5480{
5481 rtx mem, val;
5482 enum memmodel model;
5483
5484 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5485 if (model != MEMMODEL_RELAXED
5486 && model != MEMMODEL_SEQ_CST
5487 && model != MEMMODEL_RELEASE)
5488 {
5489 error ("invalid memory model for %<__atomic_store%>");
5490 return NULL_RTX;
5491 }
5492
5493 if (!flag_inline_atomics)
5494 return NULL_RTX;
5495
5496 /* Expand the operands. */
5497 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5498 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5499
8808bf16 5500 return expand_atomic_store (mem, val, model, false);
1cd6e20d 5501}
5502
5503/* Expand the __atomic_fetch_XXX intrinsic:
5504 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5505 EXP is the CALL_EXPR.
5506 TARGET is an optional place for us to store the results.
5507 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5508 FETCH_AFTER is true if returning the result of the operation.
5509 FETCH_AFTER is false if returning the value before the operation.
5510 IGNORE is true if the result is not used.
5511 EXT_CALL is the correct builtin for an external call if this cannot be
5512 resolved to an instruction sequence. */
5513
5514static rtx
5515expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5516 enum rtx_code code, bool fetch_after,
5517 bool ignore, enum built_in_function ext_call)
5518{
5519 rtx val, mem, ret;
5520 enum memmodel model;
5521 tree fndecl;
5522 tree addr;
5523
5524 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5525
5526 /* Expand the operands. */
5527 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5528 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5529
5530 /* Only try generating instructions if inlining is turned on. */
5531 if (flag_inline_atomics)
5532 {
5533 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5534 if (ret)
5535 return ret;
5536 }
5537
5538 /* Return if a different routine isn't needed for the library call. */
5539 if (ext_call == BUILT_IN_NONE)
5540 return NULL_RTX;
5541
5542 /* Change the call to the specified function. */
5543 fndecl = get_callee_fndecl (exp);
5544 addr = CALL_EXPR_FN (exp);
5545 STRIP_NOPS (addr);
5546
5547 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5548 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5549
5550 /* Expand the call here so we can emit trailing code. */
5551 ret = expand_call (exp, target, ignore);
5552
5553 /* Replace the original function just in case it matters. */
5554 TREE_OPERAND (addr, 0) = fndecl;
5555
5556 /* Then issue the arithmetic correction to return the right result. */
5557 if (!ignore)
c449f851 5558 {
5559 if (code == NOT)
5560 {
5561 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5562 OPTAB_LIB_WIDEN);
5563 ret = expand_simple_unop (mode, NOT, ret, target, true);
5564 }
5565 else
5566 ret = expand_simple_binop (mode, code, ret, val, target, true,
5567 OPTAB_LIB_WIDEN);
5568 }
1cd6e20d 5569 return ret;
5570}
5571
10b744a3 5572
7821cde1 5573#ifndef HAVE_atomic_clear
5574# define HAVE_atomic_clear 0
5575# define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5576#endif
5577
10b744a3 5578/* Expand an atomic clear operation.
5579 void _atomic_clear (BOOL *obj, enum memmodel)
5580 EXP is the call expression. */
5581
5582static rtx
5583expand_builtin_atomic_clear (tree exp)
5584{
5585 enum machine_mode mode;
5586 rtx mem, ret;
5587 enum memmodel model;
5588
5589 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5590 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5591 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5592
5593 if (model == MEMMODEL_ACQUIRE || model == MEMMODEL_ACQ_REL)
5594 {
5595 error ("invalid memory model for %<__atomic_store%>");
5596 return const0_rtx;
5597 }
5598
7821cde1 5599 if (HAVE_atomic_clear)
5600 {
5601 emit_insn (gen_atomic_clear (mem, model));
5602 return const0_rtx;
5603 }
5604
10b744a3 5605 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5606 Failing that, a store is issued by __atomic_store. The only way this can
5607 fail is if the bool type is larger than a word size. Unlikely, but
5608 handle it anyway for completeness. Assume a single threaded model since
5609 there is no atomic support in this case, and no barriers are required. */
5610 ret = expand_atomic_store (mem, const0_rtx, model, true);
5611 if (!ret)
5612 emit_move_insn (mem, const0_rtx);
5613 return const0_rtx;
5614}
5615
5616/* Expand an atomic test_and_set operation.
5617 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5618 EXP is the call expression. */
5619
5620static rtx
7821cde1 5621expand_builtin_atomic_test_and_set (tree exp, rtx target)
10b744a3 5622{
7821cde1 5623 rtx mem;
10b744a3 5624 enum memmodel model;
5625 enum machine_mode mode;
5626
5627 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5628 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5629 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5630
7821cde1 5631 return expand_atomic_test_and_set (target, mem, model);
10b744a3 5632}
5633
5634
1cd6e20d 5635/* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5636 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5637
5638static tree
5639fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5640{
5641 int size;
5642 enum machine_mode mode;
5643 unsigned int mode_align, type_align;
5644
5645 if (TREE_CODE (arg0) != INTEGER_CST)
5646 return NULL_TREE;
b6a5fc45 5647
1cd6e20d 5648 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5649 mode = mode_for_size (size, MODE_INT, 0);
5650 mode_align = GET_MODE_ALIGNMENT (mode);
5651
5652 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5653 type_align = mode_align;
5654 else
5655 {
5656 tree ttype = TREE_TYPE (arg1);
5657
5658 /* This function is usually invoked and folded immediately by the front
5659 end before anything else has a chance to look at it. The pointer
5660 parameter at this point is usually cast to a void *, so check for that
5661 and look past the cast. */
5662 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5663 && VOID_TYPE_P (TREE_TYPE (ttype)))
5664 arg1 = TREE_OPERAND (arg1, 0);
5665
5666 ttype = TREE_TYPE (arg1);
5667 gcc_assert (POINTER_TYPE_P (ttype));
5668
5669 /* Get the underlying type of the object. */
5670 ttype = TREE_TYPE (ttype);
5671 type_align = TYPE_ALIGN (ttype);
5672 }
5673
5674 /* If the object has smaller alignment, the the lock free routines cannot
5675 be used. */
5676 if (type_align < mode_align)
06308d2a 5677 return boolean_false_node;
1cd6e20d 5678
5679 /* Check if a compare_and_swap pattern exists for the mode which represents
5680 the required size. The pattern is not allowed to fail, so the existence
5681 of the pattern indicates support is present. */
29139cdc 5682 if (can_compare_and_swap_p (mode, true))
06308d2a 5683 return boolean_true_node;
1cd6e20d 5684 else
06308d2a 5685 return boolean_false_node;
1cd6e20d 5686}
5687
5688/* Return true if the parameters to call EXP represent an object which will
5689 always generate lock free instructions. The first argument represents the
5690 size of the object, and the second parameter is a pointer to the object
5691 itself. If NULL is passed for the object, then the result is based on
5692 typical alignment for an object of the specified size. Otherwise return
5693 false. */
5694
5695static rtx
5696expand_builtin_atomic_always_lock_free (tree exp)
5697{
5698 tree size;
5699 tree arg0 = CALL_EXPR_ARG (exp, 0);
5700 tree arg1 = CALL_EXPR_ARG (exp, 1);
5701
5702 if (TREE_CODE (arg0) != INTEGER_CST)
5703 {
5704 error ("non-constant argument 1 to __atomic_always_lock_free");
5705 return const0_rtx;
5706 }
5707
5708 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
06308d2a 5709 if (size == boolean_true_node)
1cd6e20d 5710 return const1_rtx;
5711 return const0_rtx;
5712}
5713
5714/* Return a one or zero if it can be determined that object ARG1 of size ARG
5715 is lock free on this architecture. */
5716
5717static tree
5718fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5719{
5720 if (!flag_inline_atomics)
5721 return NULL_TREE;
5722
5723 /* If it isn't always lock free, don't generate a result. */
06308d2a 5724 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5725 return boolean_true_node;
1cd6e20d 5726
5727 return NULL_TREE;
5728}
5729
5730/* Return true if the parameters to call EXP represent an object which will
5731 always generate lock free instructions. The first argument represents the
5732 size of the object, and the second parameter is a pointer to the object
5733 itself. If NULL is passed for the object, then the result is based on
5734 typical alignment for an object of the specified size. Otherwise return
5735 NULL*/
5736
5737static rtx
5738expand_builtin_atomic_is_lock_free (tree exp)
5739{
5740 tree size;
5741 tree arg0 = CALL_EXPR_ARG (exp, 0);
5742 tree arg1 = CALL_EXPR_ARG (exp, 1);
5743
5744 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5745 {
5746 error ("non-integer argument 1 to __atomic_is_lock_free");
5747 return NULL_RTX;
5748 }
5749
5750 if (!flag_inline_atomics)
5751 return NULL_RTX;
5752
5753 /* If the value is known at compile time, return the RTX for it. */
5754 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
06308d2a 5755 if (size == boolean_true_node)
1cd6e20d 5756 return const1_rtx;
5757
5758 return NULL_RTX;
5759}
5760
1cd6e20d 5761/* Expand the __atomic_thread_fence intrinsic:
5762 void __atomic_thread_fence (enum memmodel)
5763 EXP is the CALL_EXPR. */
5764
5765static void
5766expand_builtin_atomic_thread_fence (tree exp)
5767{
fe54c06b 5768 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5769 expand_mem_thread_fence (model);
1cd6e20d 5770}
5771
5772/* Expand the __atomic_signal_fence intrinsic:
5773 void __atomic_signal_fence (enum memmodel)
5774 EXP is the CALL_EXPR. */
5775
5776static void
5777expand_builtin_atomic_signal_fence (tree exp)
5778{
fe54c06b 5779 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5780 expand_mem_signal_fence (model);
b6a5fc45 5781}
5782
5783/* Expand the __sync_synchronize intrinsic. */
5784
5785static void
2797f13a 5786expand_builtin_sync_synchronize (void)
b6a5fc45 5787{
fe54c06b 5788 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
b6a5fc45 5789}
5790
53800dbe 5791\f
5792/* Expand an expression EXP that calls a built-in function,
5793 with result going to TARGET if that's convenient
5794 (and in mode MODE if that's convenient).
5795 SUBTARGET may be used as the target for computing one of EXP's operands.
5796 IGNORE is nonzero if the value is to be ignored. */
5797
5798rtx
aecda0d6 5799expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5800 int ignore)
53800dbe 5801{
c6e6ecb1 5802 tree fndecl = get_callee_fndecl (exp);
53800dbe 5803 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
efb070c8 5804 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
67fa4078 5805 int flags;
53800dbe 5806
8305149e 5807 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
883b2e73 5808 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
bf8e3599 5809
53800dbe 5810 /* When not optimizing, generate calls to library functions for a certain
5811 set of builtins. */
cd9ff771 5812 if (!optimize
b6a5fc45 5813 && !called_as_built_in (fndecl)
2c281b15 5814 && fcode != BUILT_IN_ALLOCA
581bf1c2 5815 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
2c281b15 5816 && fcode != BUILT_IN_FREE)
cd9ff771 5817 return expand_call (exp, target, ignore);
53800dbe 5818
8d6d7930 5819 /* The built-in function expanders test for target == const0_rtx
5820 to determine whether the function's result will be ignored. */
5821 if (ignore)
5822 target = const0_rtx;
5823
5824 /* If the result of a pure or const built-in function is ignored, and
5825 none of its arguments are volatile, we can avoid expanding the
5826 built-in call and just evaluate the arguments for side-effects. */
5827 if (target == const0_rtx
67fa4078 5828 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5829 && !(flags & ECF_LOOPING_CONST_OR_PURE))
8d6d7930 5830 {
5831 bool volatilep = false;
5832 tree arg;
c2f47e15 5833 call_expr_arg_iterator iter;
8d6d7930 5834
c2f47e15 5835 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5836 if (TREE_THIS_VOLATILE (arg))
8d6d7930 5837 {
5838 volatilep = true;
5839 break;
5840 }
5841
5842 if (! volatilep)
5843 {
c2f47e15 5844 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5845 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
8d6d7930 5846 return const0_rtx;
5847 }
5848 }
5849
53800dbe 5850 switch (fcode)
5851 {
4f35b1fc 5852 CASE_FLT_FN (BUILT_IN_FABS):
c2f47e15 5853 target = expand_builtin_fabs (exp, target, subtarget);
78a74442 5854 if (target)
a0c938f0 5855 return target;
78a74442 5856 break;
5857
4f35b1fc 5858 CASE_FLT_FN (BUILT_IN_COPYSIGN):
c2f47e15 5859 target = expand_builtin_copysign (exp, target, subtarget);
270436f3 5860 if (target)
5861 return target;
5862 break;
5863
7d3f6cc7 5864 /* Just do a normal library call if we were unable to fold
5865 the values. */
4f35b1fc 5866 CASE_FLT_FN (BUILT_IN_CABS):
78a74442 5867 break;
53800dbe 5868
4f35b1fc 5869 CASE_FLT_FN (BUILT_IN_EXP):
5870 CASE_FLT_FN (BUILT_IN_EXP10):
5871 CASE_FLT_FN (BUILT_IN_POW10):
5872 CASE_FLT_FN (BUILT_IN_EXP2):
5873 CASE_FLT_FN (BUILT_IN_EXPM1):
5874 CASE_FLT_FN (BUILT_IN_LOGB):
4f35b1fc 5875 CASE_FLT_FN (BUILT_IN_LOG):
5876 CASE_FLT_FN (BUILT_IN_LOG10):
5877 CASE_FLT_FN (BUILT_IN_LOG2):
5878 CASE_FLT_FN (BUILT_IN_LOG1P):
5879 CASE_FLT_FN (BUILT_IN_TAN):
5880 CASE_FLT_FN (BUILT_IN_ASIN):
5881 CASE_FLT_FN (BUILT_IN_ACOS):
5882 CASE_FLT_FN (BUILT_IN_ATAN):
b3154a1f 5883 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
7f3be425 5884 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5885 because of possible accuracy problems. */
5886 if (! flag_unsafe_math_optimizations)
53800dbe 5887 break;
4f35b1fc 5888 CASE_FLT_FN (BUILT_IN_SQRT):
5889 CASE_FLT_FN (BUILT_IN_FLOOR):
5890 CASE_FLT_FN (BUILT_IN_CEIL):
5891 CASE_FLT_FN (BUILT_IN_TRUNC):
5892 CASE_FLT_FN (BUILT_IN_ROUND):
5893 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5894 CASE_FLT_FN (BUILT_IN_RINT):
53800dbe 5895 target = expand_builtin_mathfn (exp, target, subtarget);
5896 if (target)
5897 return target;
5898 break;
5899
7e0713b1 5900 CASE_FLT_FN (BUILT_IN_FMA):
5901 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5902 if (target)
5903 return target;
5904 break;
5905
a67a90e5 5906 CASE_FLT_FN (BUILT_IN_ILOGB):
5907 if (! flag_unsafe_math_optimizations)
5908 break;
69b779ea 5909 CASE_FLT_FN (BUILT_IN_ISINF):
cde061c1 5910 CASE_FLT_FN (BUILT_IN_FINITE):
5911 case BUILT_IN_ISFINITE:
8a1a9cb7 5912 case BUILT_IN_ISNORMAL:
f97eea22 5913 target = expand_builtin_interclass_mathfn (exp, target);
a67a90e5 5914 if (target)
5915 return target;
5916 break;
5917
80ff6494 5918 CASE_FLT_FN (BUILT_IN_ICEIL):
4f35b1fc 5919 CASE_FLT_FN (BUILT_IN_LCEIL):
5920 CASE_FLT_FN (BUILT_IN_LLCEIL):
5921 CASE_FLT_FN (BUILT_IN_LFLOOR):
80ff6494 5922 CASE_FLT_FN (BUILT_IN_IFLOOR):
4f35b1fc 5923 CASE_FLT_FN (BUILT_IN_LLFLOOR):
ff1b14e4 5924 target = expand_builtin_int_roundingfn (exp, target);
ad52b9b7 5925 if (target)
5926 return target;
5927 break;
5928
80ff6494 5929 CASE_FLT_FN (BUILT_IN_IRINT):
7d3afc77 5930 CASE_FLT_FN (BUILT_IN_LRINT):
5931 CASE_FLT_FN (BUILT_IN_LLRINT):
80ff6494 5932 CASE_FLT_FN (BUILT_IN_IROUND):
ef2f1a10 5933 CASE_FLT_FN (BUILT_IN_LROUND):
5934 CASE_FLT_FN (BUILT_IN_LLROUND):
ff1b14e4 5935 target = expand_builtin_int_roundingfn_2 (exp, target);
7d3afc77 5936 if (target)
5937 return target;
5938 break;
5939
4f35b1fc 5940 CASE_FLT_FN (BUILT_IN_POWI):
f97eea22 5941 target = expand_builtin_powi (exp, target);
757c219d 5942 if (target)
5943 return target;
5944 break;
5945
4f35b1fc 5946 CASE_FLT_FN (BUILT_IN_ATAN2):
5947 CASE_FLT_FN (BUILT_IN_LDEXP):
73a954a1 5948 CASE_FLT_FN (BUILT_IN_SCALB):
5949 CASE_FLT_FN (BUILT_IN_SCALBN):
5950 CASE_FLT_FN (BUILT_IN_SCALBLN):
0fd605a5 5951 if (! flag_unsafe_math_optimizations)
5952 break;
ef722005 5953
5954 CASE_FLT_FN (BUILT_IN_FMOD):
5955 CASE_FLT_FN (BUILT_IN_REMAINDER):
5956 CASE_FLT_FN (BUILT_IN_DREM):
0810ff17 5957 CASE_FLT_FN (BUILT_IN_POW):
0fd605a5 5958 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5959 if (target)
5960 return target;
5961 break;
5962
d735c391 5963 CASE_FLT_FN (BUILT_IN_CEXPI):
f97eea22 5964 target = expand_builtin_cexpi (exp, target);
d735c391 5965 gcc_assert (target);
5966 return target;
5967
4f35b1fc 5968 CASE_FLT_FN (BUILT_IN_SIN):
5969 CASE_FLT_FN (BUILT_IN_COS):
6b43bae4 5970 if (! flag_unsafe_math_optimizations)
5971 break;
5972 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5973 if (target)
5974 return target;
5975 break;
5976
c3147c1a 5977 CASE_FLT_FN (BUILT_IN_SINCOS):
5978 if (! flag_unsafe_math_optimizations)
5979 break;
5980 target = expand_builtin_sincos (exp);
5981 if (target)
5982 return target;
5983 break;
5984
53800dbe 5985 case BUILT_IN_APPLY_ARGS:
5986 return expand_builtin_apply_args ();
5987
5988 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5989 FUNCTION with a copy of the parameters described by
5990 ARGUMENTS, and ARGSIZE. It returns a block of memory
5991 allocated on the stack into which is stored all the registers
5992 that might possibly be used for returning the result of a
5993 function. ARGUMENTS is the value returned by
5994 __builtin_apply_args. ARGSIZE is the number of bytes of
5995 arguments that must be copied. ??? How should this value be
5996 computed? We'll also need a safe worst case value for varargs
5997 functions. */
5998 case BUILT_IN_APPLY:
c2f47e15 5999 if (!validate_arglist (exp, POINTER_TYPE,
0eb671f7 6000 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
c2f47e15 6001 && !validate_arglist (exp, REFERENCE_TYPE,
0eb671f7 6002 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 6003 return const0_rtx;
6004 else
6005 {
53800dbe 6006 rtx ops[3];
6007
c2f47e15 6008 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6009 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6010 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
53800dbe 6011
6012 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6013 }
6014
6015 /* __builtin_return (RESULT) causes the function to return the
6016 value described by RESULT. RESULT is address of the block of
6017 memory returned by __builtin_apply. */
6018 case BUILT_IN_RETURN:
c2f47e15 6019 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6020 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
53800dbe 6021 return const0_rtx;
6022
6023 case BUILT_IN_SAVEREGS:
a66c9326 6024 return expand_builtin_saveregs ();
53800dbe 6025
48dc2227 6026 case BUILT_IN_VA_ARG_PACK:
6027 /* All valid uses of __builtin_va_arg_pack () are removed during
6028 inlining. */
b8c23db3 6029 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
48dc2227 6030 return const0_rtx;
6031
4e1d7ea4 6032 case BUILT_IN_VA_ARG_PACK_LEN:
6033 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6034 inlining. */
b8c23db3 6035 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
4e1d7ea4 6036 return const0_rtx;
6037
53800dbe 6038 /* Return the address of the first anonymous stack arg. */
6039 case BUILT_IN_NEXT_ARG:
c2f47e15 6040 if (fold_builtin_next_arg (exp, false))
a0c938f0 6041 return const0_rtx;
79012a9d 6042 return expand_builtin_next_arg ();
53800dbe 6043
ac8fb6db 6044 case BUILT_IN_CLEAR_CACHE:
6045 target = expand_builtin___clear_cache (exp);
6046 if (target)
6047 return target;
6048 break;
6049
53800dbe 6050 case BUILT_IN_CLASSIFY_TYPE:
c2f47e15 6051 return expand_builtin_classify_type (exp);
53800dbe 6052
6053 case BUILT_IN_CONSTANT_P:
4ee9c684 6054 return const0_rtx;
53800dbe 6055
6056 case BUILT_IN_FRAME_ADDRESS:
6057 case BUILT_IN_RETURN_ADDRESS:
c2f47e15 6058 return expand_builtin_frame_address (fndecl, exp);
53800dbe 6059
6060 /* Returns the address of the area where the structure is returned.
6061 0 otherwise. */
6062 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
c2f47e15 6063 if (call_expr_nargs (exp) != 0
9342ee68 6064 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
e16ceb8e 6065 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
9342ee68 6066 return const0_rtx;
53800dbe 6067 else
9342ee68 6068 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
53800dbe 6069
6070 case BUILT_IN_ALLOCA:
581bf1c2 6071 case BUILT_IN_ALLOCA_WITH_ALIGN:
990495a7 6072 /* If the allocation stems from the declaration of a variable-sized
6073 object, it cannot accumulate. */
a882d754 6074 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
53800dbe 6075 if (target)
6076 return target;
6077 break;
6078
4ee9c684 6079 case BUILT_IN_STACK_SAVE:
6080 return expand_stack_save ();
6081
6082 case BUILT_IN_STACK_RESTORE:
c2f47e15 6083 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
4ee9c684 6084 return const0_rtx;
6085
74bdbe96 6086 case BUILT_IN_BSWAP16:
42791117 6087 case BUILT_IN_BSWAP32:
6088 case BUILT_IN_BSWAP64:
74bdbe96 6089 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
42791117 6090 if (target)
6091 return target;
6092 break;
6093
4f35b1fc 6094 CASE_INT_FN (BUILT_IN_FFS):
5053259b 6095 case BUILT_IN_FFSIMAX:
c2f47e15 6096 target = expand_builtin_unop (target_mode, exp, target,
efb070c8 6097 subtarget, ffs_optab);
6a08d0ab 6098 if (target)
6099 return target;
6100 break;
6101
4f35b1fc 6102 CASE_INT_FN (BUILT_IN_CLZ):
5053259b 6103 case BUILT_IN_CLZIMAX:
c2f47e15 6104 target = expand_builtin_unop (target_mode, exp, target,
efb070c8 6105 subtarget, clz_optab);
6a08d0ab 6106 if (target)
6107 return target;
6108 break;
6109
4f35b1fc 6110 CASE_INT_FN (BUILT_IN_CTZ):
5053259b 6111 case BUILT_IN_CTZIMAX:
c2f47e15 6112 target = expand_builtin_unop (target_mode, exp, target,
efb070c8 6113 subtarget, ctz_optab);
6a08d0ab 6114 if (target)
6115 return target;
6116 break;
6117
d8492bd3 6118 CASE_INT_FN (BUILT_IN_CLRSB):
6119 case BUILT_IN_CLRSBIMAX:
6120 target = expand_builtin_unop (target_mode, exp, target,
6121 subtarget, clrsb_optab);
6122 if (target)
6123 return target;
6124 break;
6125
4f35b1fc 6126 CASE_INT_FN (BUILT_IN_POPCOUNT):
5053259b 6127 case BUILT_IN_POPCOUNTIMAX:
c2f47e15 6128 target = expand_builtin_unop (target_mode, exp, target,
efb070c8 6129 subtarget, popcount_optab);
6a08d0ab 6130 if (target)
6131 return target;
6132 break;
6133
4f35b1fc 6134 CASE_INT_FN (BUILT_IN_PARITY):
5053259b 6135 case BUILT_IN_PARITYIMAX:
c2f47e15 6136 target = expand_builtin_unop (target_mode, exp, target,
efb070c8 6137 subtarget, parity_optab);
53800dbe 6138 if (target)
6139 return target;
6140 break;
6141
6142 case BUILT_IN_STRLEN:
c2f47e15 6143 target = expand_builtin_strlen (exp, target, target_mode);
53800dbe 6144 if (target)
6145 return target;
6146 break;
6147
6148 case BUILT_IN_STRCPY:
a65c4d64 6149 target = expand_builtin_strcpy (exp, target);
53800dbe 6150 if (target)
6151 return target;
6152 break;
bf8e3599 6153
ed09096d 6154 case BUILT_IN_STRNCPY:
a65c4d64 6155 target = expand_builtin_strncpy (exp, target);
ed09096d 6156 if (target)
6157 return target;
6158 break;
bf8e3599 6159
3b824fa6 6160 case BUILT_IN_STPCPY:
dc369150 6161 target = expand_builtin_stpcpy (exp, target, mode);
3b824fa6 6162 if (target)
6163 return target;
6164 break;
6165
53800dbe 6166 case BUILT_IN_MEMCPY:
a65c4d64 6167 target = expand_builtin_memcpy (exp, target);
3b824fa6 6168 if (target)
6169 return target;
6170 break;
6171
6172 case BUILT_IN_MEMPCPY:
c2f47e15 6173 target = expand_builtin_mempcpy (exp, target, mode);
53800dbe 6174 if (target)
6175 return target;
6176 break;
6177
6178 case BUILT_IN_MEMSET:
c2f47e15 6179 target = expand_builtin_memset (exp, target, mode);
53800dbe 6180 if (target)
6181 return target;
6182 break;
6183
ffc83088 6184 case BUILT_IN_BZERO:
0b25db21 6185 target = expand_builtin_bzero (exp);
ffc83088 6186 if (target)
6187 return target;
6188 break;
6189
53800dbe 6190 case BUILT_IN_STRCMP:
a65c4d64 6191 target = expand_builtin_strcmp (exp, target);
53800dbe 6192 if (target)
6193 return target;
6194 break;
6195
ed09096d 6196 case BUILT_IN_STRNCMP:
6197 target = expand_builtin_strncmp (exp, target, mode);
6198 if (target)
6199 return target;
6200 break;
6201
071f1696 6202 case BUILT_IN_BCMP:
53800dbe 6203 case BUILT_IN_MEMCMP:
c2f47e15 6204 target = expand_builtin_memcmp (exp, target, mode);
53800dbe 6205 if (target)
6206 return target;
6207 break;
53800dbe 6208
6209 case BUILT_IN_SETJMP:
2c8a1497 6210 /* This should have been lowered to the builtins below. */
6211 gcc_unreachable ();
6212
6213 case BUILT_IN_SETJMP_SETUP:
6214 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6215 and the receiver label. */
c2f47e15 6216 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2c8a1497 6217 {
c2f47e15 6218 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
2c8a1497 6219 VOIDmode, EXPAND_NORMAL);
c2f47e15 6220 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
2c8a1497 6221 rtx label_r = label_rtx (label);
6222
6223 /* This is copied from the handling of non-local gotos. */
6224 expand_builtin_setjmp_setup (buf_addr, label_r);
6225 nonlocal_goto_handler_labels
6226 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6227 nonlocal_goto_handler_labels);
6228 /* ??? Do not let expand_label treat us as such since we would
6229 not want to be both on the list of non-local labels and on
6230 the list of forced labels. */
6231 FORCED_LABEL (label) = 0;
6232 return const0_rtx;
6233 }
6234 break;
6235
6236 case BUILT_IN_SETJMP_DISPATCHER:
6237 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
c2f47e15 6238 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2c8a1497 6239 {
c2f47e15 6240 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
2c8a1497 6241 rtx label_r = label_rtx (label);
6242
6243 /* Remove the dispatcher label from the list of non-local labels
6244 since the receiver labels have been added to it above. */
6245 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6246 return const0_rtx;
6247 }
6248 break;
6249
6250 case BUILT_IN_SETJMP_RECEIVER:
6251 /* __builtin_setjmp_receiver is passed the receiver label. */
c2f47e15 6252 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2c8a1497 6253 {
c2f47e15 6254 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
2c8a1497 6255 rtx label_r = label_rtx (label);
6256
6257 expand_builtin_setjmp_receiver (label_r);
6258 return const0_rtx;
6259 }
6b7f6858 6260 break;
53800dbe 6261
6262 /* __builtin_longjmp is passed a pointer to an array of five words.
6263 It's similar to the C library longjmp function but works with
6264 __builtin_setjmp above. */
6265 case BUILT_IN_LONGJMP:
c2f47e15 6266 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 6267 {
c2f47e15 6268 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
8ec3c5c2 6269 VOIDmode, EXPAND_NORMAL);
c2f47e15 6270 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
53800dbe 6271
6272 if (value != const1_rtx)
6273 {
1e5fcbe2 6274 error ("%<__builtin_longjmp%> second argument must be 1");
53800dbe 6275 return const0_rtx;
6276 }
6277
6278 expand_builtin_longjmp (buf_addr, value);
6279 return const0_rtx;
6280 }
2c8a1497 6281 break;
53800dbe 6282
4ee9c684 6283 case BUILT_IN_NONLOCAL_GOTO:
c2f47e15 6284 target = expand_builtin_nonlocal_goto (exp);
4ee9c684 6285 if (target)
6286 return target;
6287 break;
6288
843d08a9 6289 /* This updates the setjmp buffer that is its argument with the value
6290 of the current stack pointer. */
6291 case BUILT_IN_UPDATE_SETJMP_BUF:
c2f47e15 6292 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
843d08a9 6293 {
6294 rtx buf_addr
c2f47e15 6295 = expand_normal (CALL_EXPR_ARG (exp, 0));
843d08a9 6296
6297 expand_builtin_update_setjmp_buf (buf_addr);
6298 return const0_rtx;
6299 }
6300 break;
6301
53800dbe 6302 case BUILT_IN_TRAP:
a0ef1725 6303 expand_builtin_trap ();
53800dbe 6304 return const0_rtx;
6305
d2b48f0c 6306 case BUILT_IN_UNREACHABLE:
6307 expand_builtin_unreachable ();
6308 return const0_rtx;
6309
4f35b1fc 6310 CASE_FLT_FN (BUILT_IN_SIGNBIT):
004e23c4 6311 case BUILT_IN_SIGNBITD32:
6312 case BUILT_IN_SIGNBITD64:
6313 case BUILT_IN_SIGNBITD128:
27f261ef 6314 target = expand_builtin_signbit (exp, target);
6315 if (target)
6316 return target;
6317 break;
6318
53800dbe 6319 /* Various hooks for the DWARF 2 __throw routine. */
6320 case BUILT_IN_UNWIND_INIT:
6321 expand_builtin_unwind_init ();
6322 return const0_rtx;
6323 case BUILT_IN_DWARF_CFA:
6324 return virtual_cfa_rtx;
6325#ifdef DWARF2_UNWIND_INFO
f8f023a5 6326 case BUILT_IN_DWARF_SP_COLUMN:
6327 return expand_builtin_dwarf_sp_column ();
695e919b 6328 case BUILT_IN_INIT_DWARF_REG_SIZES:
c2f47e15 6329 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
695e919b 6330 return const0_rtx;
53800dbe 6331#endif
6332 case BUILT_IN_FROB_RETURN_ADDR:
c2f47e15 6333 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
53800dbe 6334 case BUILT_IN_EXTRACT_RETURN_ADDR:
c2f47e15 6335 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
53800dbe 6336 case BUILT_IN_EH_RETURN:
c2f47e15 6337 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6338 CALL_EXPR_ARG (exp, 1));
53800dbe 6339 return const0_rtx;
df4b504c 6340#ifdef EH_RETURN_DATA_REGNO
6341 case BUILT_IN_EH_RETURN_DATA_REGNO:
c2f47e15 6342 return expand_builtin_eh_return_data_regno (exp);
df4b504c 6343#endif
26093bf4 6344 case BUILT_IN_EXTEND_POINTER:
c2f47e15 6345 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
e38def9c 6346 case BUILT_IN_EH_POINTER:
6347 return expand_builtin_eh_pointer (exp);
6348 case BUILT_IN_EH_FILTER:
6349 return expand_builtin_eh_filter (exp);
6350 case BUILT_IN_EH_COPY_VALUES:
6351 return expand_builtin_eh_copy_values (exp);
26093bf4 6352
7ccc713a 6353 case BUILT_IN_VA_START:
c2f47e15 6354 return expand_builtin_va_start (exp);
a66c9326 6355 case BUILT_IN_VA_END:
c2f47e15 6356 return expand_builtin_va_end (exp);
a66c9326 6357 case BUILT_IN_VA_COPY:
c2f47e15 6358 return expand_builtin_va_copy (exp);
89cfe6e5 6359 case BUILT_IN_EXPECT:
c2f47e15 6360 return expand_builtin_expect (exp, target);
fca0886c 6361 case BUILT_IN_ASSUME_ALIGNED:
6362 return expand_builtin_assume_aligned (exp, target);
5e3608d8 6363 case BUILT_IN_PREFETCH:
c2f47e15 6364 expand_builtin_prefetch (exp);
5e3608d8 6365 return const0_rtx;
6366
4ee9c684 6367 case BUILT_IN_INIT_TRAMPOLINE:
c307f106 6368 return expand_builtin_init_trampoline (exp, true);
6369 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6370 return expand_builtin_init_trampoline (exp, false);
4ee9c684 6371 case BUILT_IN_ADJUST_TRAMPOLINE:
c2f47e15 6372 return expand_builtin_adjust_trampoline (exp);
4ee9c684 6373
73673831 6374 case BUILT_IN_FORK:
6375 case BUILT_IN_EXECL:
6376 case BUILT_IN_EXECV:
6377 case BUILT_IN_EXECLP:
6378 case BUILT_IN_EXECLE:
6379 case BUILT_IN_EXECVP:
6380 case BUILT_IN_EXECVE:
c2f47e15 6381 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
73673831 6382 if (target)
6383 return target;
6384 break;
53800dbe 6385
2797f13a 6386 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6387 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6388 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6389 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6390 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6391 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
1cd6e20d 6392 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
b6a5fc45 6393 if (target)
6394 return target;
6395 break;
6396
2797f13a 6397 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6398 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6399 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6400 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6401 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6402 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
1cd6e20d 6403 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
b6a5fc45 6404 if (target)
6405 return target;
6406 break;
6407
2797f13a 6408 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6409 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6410 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6411 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6412 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6413 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
1cd6e20d 6414 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
b6a5fc45 6415 if (target)
6416 return target;
6417 break;
6418
2797f13a 6419 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6420 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6421 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6422 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6423 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6424 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
1cd6e20d 6425 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
b6a5fc45 6426 if (target)
6427 return target;
6428 break;
6429
2797f13a 6430 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6431 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6432 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6433 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6434 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6435 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
1cd6e20d 6436 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
b6a5fc45 6437 if (target)
6438 return target;
6439 break;
6440
2797f13a 6441 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6442 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6443 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6444 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6445 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6446 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
1cd6e20d 6447 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
b6a5fc45 6448 if (target)
6449 return target;
6450 break;
6451
2797f13a 6452 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6453 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6454 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6455 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6456 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6457 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
1cd6e20d 6458 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
b6a5fc45 6459 if (target)
6460 return target;
6461 break;
6462
2797f13a 6463 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6464 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6465 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6466 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6467 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6468 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
1cd6e20d 6469 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
b6a5fc45 6470 if (target)
6471 return target;
6472 break;
6473
2797f13a 6474 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6475 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6476 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6477 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6478 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6479 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
1cd6e20d 6480 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
b6a5fc45 6481 if (target)
6482 return target;
6483 break;
6484
2797f13a 6485 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6486 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6487 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6488 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6489 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6490 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
1cd6e20d 6491 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
b6a5fc45 6492 if (target)
6493 return target;
6494 break;
6495
2797f13a 6496 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6497 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6498 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6499 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6500 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6501 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
1cd6e20d 6502 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
b6a5fc45 6503 if (target)
6504 return target;
6505 break;
6506
2797f13a 6507 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6508 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6509 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6510 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6511 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6512 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
1cd6e20d 6513 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
b6a5fc45 6514 if (target)
6515 return target;
6516 break;
6517
2797f13a 6518 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6519 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6520 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6521 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6522 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
a601d32a 6523 if (mode == VOIDmode)
6524 mode = TYPE_MODE (boolean_type_node);
b6a5fc45 6525 if (!target || !register_operand (target, mode))
6526 target = gen_reg_rtx (mode);
3e272de8 6527
2797f13a 6528 mode = get_builtin_sync_mode
6529 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
c2f47e15 6530 target = expand_builtin_compare_and_swap (mode, exp, true, target);
b6a5fc45 6531 if (target)
6532 return target;
6533 break;
6534
2797f13a 6535 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6536 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6537 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6538 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6539 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6540 mode = get_builtin_sync_mode
6541 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
c2f47e15 6542 target = expand_builtin_compare_and_swap (mode, exp, false, target);
b6a5fc45 6543 if (target)
6544 return target;
6545 break;
6546
2797f13a 6547 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6548 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6549 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6550 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6551 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6552 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6553 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
b6a5fc45 6554 if (target)
6555 return target;
6556 break;
6557
2797f13a 6558 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6559 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6560 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6561 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6562 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6563 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6564 expand_builtin_sync_lock_release (mode, exp);
b6a5fc45 6565 return const0_rtx;
6566
2797f13a 6567 case BUILT_IN_SYNC_SYNCHRONIZE:
6568 expand_builtin_sync_synchronize ();
b6a5fc45 6569 return const0_rtx;
6570
1cd6e20d 6571 case BUILT_IN_ATOMIC_EXCHANGE_1:
6572 case BUILT_IN_ATOMIC_EXCHANGE_2:
6573 case BUILT_IN_ATOMIC_EXCHANGE_4:
6574 case BUILT_IN_ATOMIC_EXCHANGE_8:
6575 case BUILT_IN_ATOMIC_EXCHANGE_16:
6576 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6577 target = expand_builtin_atomic_exchange (mode, exp, target);
6578 if (target)
6579 return target;
6580 break;
6581
6582 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6583 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6584 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6585 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6586 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2c201ad1 6587 {
6588 unsigned int nargs, z;
6589 VEC(tree,gc) *vec;
6590
6591 mode =
6592 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6593 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6594 if (target)
6595 return target;
6596
6597 /* If this is turned into an external library call, the weak parameter
6598 must be dropped to match the expected parameter list. */
6599 nargs = call_expr_nargs (exp);
6600 vec = VEC_alloc (tree, gc, nargs - 1);
6601 for (z = 0; z < 3; z++)
6602 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6603 /* Skip the boolean weak parameter. */
6604 for (z = 4; z < 6; z++)
6605 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6606 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6607 break;
6608 }
1cd6e20d 6609
6610 case BUILT_IN_ATOMIC_LOAD_1:
6611 case BUILT_IN_ATOMIC_LOAD_2:
6612 case BUILT_IN_ATOMIC_LOAD_4:
6613 case BUILT_IN_ATOMIC_LOAD_8:
6614 case BUILT_IN_ATOMIC_LOAD_16:
6615 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6616 target = expand_builtin_atomic_load (mode, exp, target);
6617 if (target)
6618 return target;
6619 break;
6620
6621 case BUILT_IN_ATOMIC_STORE_1:
6622 case BUILT_IN_ATOMIC_STORE_2:
6623 case BUILT_IN_ATOMIC_STORE_4:
6624 case BUILT_IN_ATOMIC_STORE_8:
6625 case BUILT_IN_ATOMIC_STORE_16:
6626 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6627 target = expand_builtin_atomic_store (mode, exp);
6628 if (target)
6629 return const0_rtx;
6630 break;
6631
6632 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6633 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6634 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6635 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6636 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6637 {
6638 enum built_in_function lib;
6639 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6640 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6641 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6642 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6643 ignore, lib);
6644 if (target)
6645 return target;
6646 break;
6647 }
6648 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6649 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6650 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6651 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6652 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6653 {
6654 enum built_in_function lib;
6655 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6656 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6657 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6658 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6659 ignore, lib);
6660 if (target)
6661 return target;
6662 break;
6663 }
6664 case BUILT_IN_ATOMIC_AND_FETCH_1:
6665 case BUILT_IN_ATOMIC_AND_FETCH_2:
6666 case BUILT_IN_ATOMIC_AND_FETCH_4:
6667 case BUILT_IN_ATOMIC_AND_FETCH_8:
6668 case BUILT_IN_ATOMIC_AND_FETCH_16:
6669 {
6670 enum built_in_function lib;
6671 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6672 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6673 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6674 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6675 ignore, lib);
6676 if (target)
6677 return target;
6678 break;
6679 }
6680 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6681 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6682 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6683 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6684 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6685 {
6686 enum built_in_function lib;
6687 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6688 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6689 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6690 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6691 ignore, lib);
6692 if (target)
6693 return target;
6694 break;
6695 }
6696 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6697 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6698 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6699 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6700 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6701 {
6702 enum built_in_function lib;
6703 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6704 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6705 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6706 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6707 ignore, lib);
6708 if (target)
6709 return target;
6710 break;
6711 }
6712 case BUILT_IN_ATOMIC_OR_FETCH_1:
6713 case BUILT_IN_ATOMIC_OR_FETCH_2:
6714 case BUILT_IN_ATOMIC_OR_FETCH_4:
6715 case BUILT_IN_ATOMIC_OR_FETCH_8:
6716 case BUILT_IN_ATOMIC_OR_FETCH_16:
6717 {
6718 enum built_in_function lib;
6719 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6720 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6721 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6722 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6723 ignore, lib);
6724 if (target)
6725 return target;
6726 break;
6727 }
6728 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6729 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6730 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6731 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6732 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6733 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6734 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6735 ignore, BUILT_IN_NONE);
6736 if (target)
6737 return target;
6738 break;
6739
6740 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6741 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6742 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6743 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6744 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6745 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6746 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6747 ignore, BUILT_IN_NONE);
6748 if (target)
6749 return target;
6750 break;
6751
6752 case BUILT_IN_ATOMIC_FETCH_AND_1:
6753 case BUILT_IN_ATOMIC_FETCH_AND_2:
6754 case BUILT_IN_ATOMIC_FETCH_AND_4:
6755 case BUILT_IN_ATOMIC_FETCH_AND_8:
6756 case BUILT_IN_ATOMIC_FETCH_AND_16:
6757 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6758 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6759 ignore, BUILT_IN_NONE);
6760 if (target)
6761 return target;
6762 break;
6763
6764 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6765 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6766 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6767 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6768 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6769 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6770 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6771 ignore, BUILT_IN_NONE);
6772 if (target)
6773 return target;
6774 break;
6775
6776 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6777 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6778 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6779 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6780 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6781 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6782 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6783 ignore, BUILT_IN_NONE);
6784 if (target)
6785 return target;
6786 break;
6787
6788 case BUILT_IN_ATOMIC_FETCH_OR_1:
6789 case BUILT_IN_ATOMIC_FETCH_OR_2:
6790 case BUILT_IN_ATOMIC_FETCH_OR_4:
6791 case BUILT_IN_ATOMIC_FETCH_OR_8:
6792 case BUILT_IN_ATOMIC_FETCH_OR_16:
6793 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6794 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6795 ignore, BUILT_IN_NONE);
6796 if (target)
6797 return target;
6798 break;
10b744a3 6799
6800 case BUILT_IN_ATOMIC_TEST_AND_SET:
7821cde1 6801 return expand_builtin_atomic_test_and_set (exp, target);
10b744a3 6802
6803 case BUILT_IN_ATOMIC_CLEAR:
6804 return expand_builtin_atomic_clear (exp);
1cd6e20d 6805
6806 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6807 return expand_builtin_atomic_always_lock_free (exp);
6808
6809 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6810 target = expand_builtin_atomic_is_lock_free (exp);
6811 if (target)
6812 return target;
6813 break;
6814
6815 case BUILT_IN_ATOMIC_THREAD_FENCE:
6816 expand_builtin_atomic_thread_fence (exp);
6817 return const0_rtx;
6818
6819 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6820 expand_builtin_atomic_signal_fence (exp);
6821 return const0_rtx;
6822
0a39fd54 6823 case BUILT_IN_OBJECT_SIZE:
6824 return expand_builtin_object_size (exp);
6825
6826 case BUILT_IN_MEMCPY_CHK:
6827 case BUILT_IN_MEMPCPY_CHK:
6828 case BUILT_IN_MEMMOVE_CHK:
6829 case BUILT_IN_MEMSET_CHK:
6830 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6831 if (target)
6832 return target;
6833 break;
6834
6835 case BUILT_IN_STRCPY_CHK:
6836 case BUILT_IN_STPCPY_CHK:
6837 case BUILT_IN_STRNCPY_CHK:
1063acde 6838 case BUILT_IN_STPNCPY_CHK:
0a39fd54 6839 case BUILT_IN_STRCAT_CHK:
b356dfef 6840 case BUILT_IN_STRNCAT_CHK:
0a39fd54 6841 case BUILT_IN_SNPRINTF_CHK:
6842 case BUILT_IN_VSNPRINTF_CHK:
6843 maybe_emit_chk_warning (exp, fcode);
6844 break;
6845
6846 case BUILT_IN_SPRINTF_CHK:
6847 case BUILT_IN_VSPRINTF_CHK:
6848 maybe_emit_sprintf_chk_warning (exp, fcode);
6849 break;
6850
2c281b15 6851 case BUILT_IN_FREE:
f74ea1c2 6852 if (warn_free_nonheap_object)
6853 maybe_emit_free_warning (exp);
2c281b15 6854 break;
6855
92482ee0 6856 default: /* just do library call, if unknown builtin */
146c1b4f 6857 break;
53800dbe 6858 }
6859
6860 /* The switch statement above can drop through to cause the function
6861 to be called normally. */
6862 return expand_call (exp, target, ignore);
6863}
650e4c94 6864
805e22b2 6865/* Determine whether a tree node represents a call to a built-in
52203a9d 6866 function. If the tree T is a call to a built-in function with
6867 the right number of arguments of the appropriate types, return
6868 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6869 Otherwise the return value is END_BUILTINS. */
aecda0d6 6870
805e22b2 6871enum built_in_function
b7bf20db 6872builtin_mathfn_code (const_tree t)
805e22b2 6873{
b7bf20db 6874 const_tree fndecl, arg, parmlist;
6875 const_tree argtype, parmtype;
6876 const_call_expr_arg_iterator iter;
805e22b2 6877
6878 if (TREE_CODE (t) != CALL_EXPR
c2f47e15 6879 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
805e22b2 6880 return END_BUILTINS;
6881
c6e6ecb1 6882 fndecl = get_callee_fndecl (t);
6883 if (fndecl == NULL_TREE
52203a9d 6884 || TREE_CODE (fndecl) != FUNCTION_DECL
805e22b2 6885 || ! DECL_BUILT_IN (fndecl)
6886 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6887 return END_BUILTINS;
6888
52203a9d 6889 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
b7bf20db 6890 init_const_call_expr_arg_iterator (t, &iter);
52203a9d 6891 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
e9f80ff5 6892 {
52203a9d 6893 /* If a function doesn't take a variable number of arguments,
6894 the last element in the list will have type `void'. */
6895 parmtype = TREE_VALUE (parmlist);
6896 if (VOID_TYPE_P (parmtype))
6897 {
b7bf20db 6898 if (more_const_call_expr_args_p (&iter))
52203a9d 6899 return END_BUILTINS;
6900 return DECL_FUNCTION_CODE (fndecl);
6901 }
6902
b7bf20db 6903 if (! more_const_call_expr_args_p (&iter))
e9f80ff5 6904 return END_BUILTINS;
48e1416a 6905
b7bf20db 6906 arg = next_const_call_expr_arg (&iter);
c2f47e15 6907 argtype = TREE_TYPE (arg);
52203a9d 6908
6909 if (SCALAR_FLOAT_TYPE_P (parmtype))
6910 {
6911 if (! SCALAR_FLOAT_TYPE_P (argtype))
6912 return END_BUILTINS;
6913 }
6914 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6915 {
6916 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6917 return END_BUILTINS;
6918 }
6919 else if (POINTER_TYPE_P (parmtype))
6920 {
6921 if (! POINTER_TYPE_P (argtype))
6922 return END_BUILTINS;
6923 }
6924 else if (INTEGRAL_TYPE_P (parmtype))
6925 {
6926 if (! INTEGRAL_TYPE_P (argtype))
6927 return END_BUILTINS;
6928 }
6929 else
e9f80ff5 6930 return END_BUILTINS;
e9f80ff5 6931 }
6932
52203a9d 6933 /* Variable-length argument list. */
805e22b2 6934 return DECL_FUNCTION_CODE (fndecl);
6935}
6936
c2f47e15 6937/* Fold a call to __builtin_constant_p, if we know its argument ARG will
6938 evaluate to a constant. */
650e4c94 6939
6940static tree
c2f47e15 6941fold_builtin_constant_p (tree arg)
650e4c94 6942{
650e4c94 6943 /* We return 1 for a numeric type that's known to be a constant
6944 value at compile-time or for an aggregate type that's a
6945 literal constant. */
c2f47e15 6946 STRIP_NOPS (arg);
650e4c94 6947
6948 /* If we know this is a constant, emit the constant of one. */
c2f47e15 6949 if (CONSTANT_CLASS_P (arg)
6950 || (TREE_CODE (arg) == CONSTRUCTOR
6951 && TREE_CONSTANT (arg)))
650e4c94 6952 return integer_one_node;
c2f47e15 6953 if (TREE_CODE (arg) == ADDR_EXPR)
adcfa3a3 6954 {
c2f47e15 6955 tree op = TREE_OPERAND (arg, 0);
adcfa3a3 6956 if (TREE_CODE (op) == STRING_CST
6957 || (TREE_CODE (op) == ARRAY_REF
6958 && integer_zerop (TREE_OPERAND (op, 1))
6959 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6960 return integer_one_node;
6961 }
650e4c94 6962
1fb4300c 6963 /* If this expression has side effects, show we don't know it to be a
6964 constant. Likewise if it's a pointer or aggregate type since in
6965 those case we only want literals, since those are only optimized
f97c71a1 6966 when generating RTL, not later.
6967 And finally, if we are compiling an initializer, not code, we
6968 need to return a definite result now; there's not going to be any
6969 more optimization done. */
c2f47e15 6970 if (TREE_SIDE_EFFECTS (arg)
6971 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6972 || POINTER_TYPE_P (TREE_TYPE (arg))
47be647d 6973 || cfun == 0
6974 || folding_initializer)
650e4c94 6975 return integer_zero_node;
6976
c2f47e15 6977 return NULL_TREE;
650e4c94 6978}
6979
76f5a783 6980/* Create builtin_expect with PRED and EXPECTED as its arguments and
6981 return it as a truthvalue. */
4ee9c684 6982
6983static tree
389dd41b 6984build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
4ee9c684 6985{
76f5a783 6986 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
4ee9c684 6987
b9a16870 6988 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
76f5a783 6989 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6990 ret_type = TREE_TYPE (TREE_TYPE (fn));
6991 pred_type = TREE_VALUE (arg_types);
6992 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6993
389dd41b 6994 pred = fold_convert_loc (loc, pred_type, pred);
6995 expected = fold_convert_loc (loc, expected_type, expected);
6996 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
76f5a783 6997
6998 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6999 build_int_cst (ret_type, 0));
7000}
7001
7002/* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7003 NULL_TREE if no simplification is possible. */
7004
7005static tree
389dd41b 7006fold_builtin_expect (location_t loc, tree arg0, tree arg1)
76f5a783 7007{
083bada9 7008 tree inner, fndecl, inner_arg0;
76f5a783 7009 enum tree_code code;
7010
083bada9 7011 /* Distribute the expected value over short-circuiting operators.
7012 See through the cast from truthvalue_type_node to long. */
7013 inner_arg0 = arg0;
7014 while (TREE_CODE (inner_arg0) == NOP_EXPR
7015 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7016 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7017 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7018
76f5a783 7019 /* If this is a builtin_expect within a builtin_expect keep the
7020 inner one. See through a comparison against a constant. It
7021 might have been added to create a thruthvalue. */
083bada9 7022 inner = inner_arg0;
7023
76f5a783 7024 if (COMPARISON_CLASS_P (inner)
7025 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7026 inner = TREE_OPERAND (inner, 0);
7027
7028 if (TREE_CODE (inner) == CALL_EXPR
7029 && (fndecl = get_callee_fndecl (inner))
7030 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7031 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7032 return arg0;
7033
083bada9 7034 inner = inner_arg0;
76f5a783 7035 code = TREE_CODE (inner);
7036 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7037 {
7038 tree op0 = TREE_OPERAND (inner, 0);
7039 tree op1 = TREE_OPERAND (inner, 1);
7040
389dd41b 7041 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7042 op1 = build_builtin_expect_predicate (loc, op1, arg1);
76f5a783 7043 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7044
389dd41b 7045 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
76f5a783 7046 }
7047
7048 /* If the argument isn't invariant then there's nothing else we can do. */
083bada9 7049 if (!TREE_CONSTANT (inner_arg0))
c2f47e15 7050 return NULL_TREE;
4ee9c684 7051
76f5a783 7052 /* If we expect that a comparison against the argument will fold to
7053 a constant return the constant. In practice, this means a true
7054 constant or the address of a non-weak symbol. */
083bada9 7055 inner = inner_arg0;
4ee9c684 7056 STRIP_NOPS (inner);
7057 if (TREE_CODE (inner) == ADDR_EXPR)
7058 {
7059 do
7060 {
7061 inner = TREE_OPERAND (inner, 0);
7062 }
7063 while (TREE_CODE (inner) == COMPONENT_REF
7064 || TREE_CODE (inner) == ARRAY_REF);
062b4460 7065 if ((TREE_CODE (inner) == VAR_DECL
7066 || TREE_CODE (inner) == FUNCTION_DECL)
7067 && DECL_WEAK (inner))
c2f47e15 7068 return NULL_TREE;
4ee9c684 7069 }
7070
76f5a783 7071 /* Otherwise, ARG0 already has the proper type for the return value. */
7072 return arg0;
4ee9c684 7073}
7074
c2f47e15 7075/* Fold a call to __builtin_classify_type with argument ARG. */
27d0c333 7076
539a3a92 7077static tree
c2f47e15 7078fold_builtin_classify_type (tree arg)
539a3a92 7079{
c2f47e15 7080 if (arg == 0)
7002a1c8 7081 return build_int_cst (integer_type_node, no_type_class);
539a3a92 7082
7002a1c8 7083 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
539a3a92 7084}
7085
c2f47e15 7086/* Fold a call to __builtin_strlen with argument ARG. */
e6e27594 7087
7088static tree
c7cbde74 7089fold_builtin_strlen (location_t loc, tree type, tree arg)
e6e27594 7090{
c2f47e15 7091 if (!validate_arg (arg, POINTER_TYPE))
e6e27594 7092 return NULL_TREE;
7093 else
7094 {
c2f47e15 7095 tree len = c_strlen (arg, 0);
e6e27594 7096
7097 if (len)
c7cbde74 7098 return fold_convert_loc (loc, type, len);
e6e27594 7099
7100 return NULL_TREE;
7101 }
7102}
7103
92c43e3c 7104/* Fold a call to __builtin_inf or __builtin_huge_val. */
7105
7106static tree
389dd41b 7107fold_builtin_inf (location_t loc, tree type, int warn)
92c43e3c 7108{
aa870c1b 7109 REAL_VALUE_TYPE real;
7110
40f4dbd5 7111 /* __builtin_inff is intended to be usable to define INFINITY on all
7112 targets. If an infinity is not available, INFINITY expands "to a
7113 positive constant of type float that overflows at translation
7114 time", footnote "In this case, using INFINITY will violate the
7115 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7116 Thus we pedwarn to ensure this constraint violation is
7117 diagnosed. */
92c43e3c 7118 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
389dd41b 7119 pedwarn (loc, 0, "target format does not support infinity");
92c43e3c 7120
aa870c1b 7121 real_inf (&real);
7122 return build_real (type, real);
92c43e3c 7123}
7124
c2f47e15 7125/* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
b0db7939 7126
7127static tree
c2f47e15 7128fold_builtin_nan (tree arg, tree type, int quiet)
b0db7939 7129{
7130 REAL_VALUE_TYPE real;
7131 const char *str;
7132
c2f47e15 7133 if (!validate_arg (arg, POINTER_TYPE))
7134 return NULL_TREE;
7135 str = c_getstr (arg);
b0db7939 7136 if (!str)
c2f47e15 7137 return NULL_TREE;
b0db7939 7138
7139 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
c2f47e15 7140 return NULL_TREE;
b0db7939 7141
7142 return build_real (type, real);
7143}
7144
277f8dd2 7145/* Return true if the floating point expression T has an integer value.
7146 We also allow +Inf, -Inf and NaN to be considered integer values. */
7147
7148static bool
7149integer_valued_real_p (tree t)
7150{
7151 switch (TREE_CODE (t))
7152 {
7153 case FLOAT_EXPR:
7154 return true;
7155
7156 case ABS_EXPR:
7157 case SAVE_EXPR:
277f8dd2 7158 return integer_valued_real_p (TREE_OPERAND (t, 0));
7159
7160 case COMPOUND_EXPR:
41076ef6 7161 case MODIFY_EXPR:
277f8dd2 7162 case BIND_EXPR:
75a70cf9 7163 return integer_valued_real_p (TREE_OPERAND (t, 1));
277f8dd2 7164
7165 case PLUS_EXPR:
7166 case MINUS_EXPR:
7167 case MULT_EXPR:
7168 case MIN_EXPR:
7169 case MAX_EXPR:
7170 return integer_valued_real_p (TREE_OPERAND (t, 0))
7171 && integer_valued_real_p (TREE_OPERAND (t, 1));
7172
7173 case COND_EXPR:
7174 return integer_valued_real_p (TREE_OPERAND (t, 1))
7175 && integer_valued_real_p (TREE_OPERAND (t, 2));
7176
7177 case REAL_CST:
0570334c 7178 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
277f8dd2 7179
7180 case NOP_EXPR:
7181 {
7182 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7183 if (TREE_CODE (type) == INTEGER_TYPE)
7184 return true;
7185 if (TREE_CODE (type) == REAL_TYPE)
7186 return integer_valued_real_p (TREE_OPERAND (t, 0));
7187 break;
7188 }
7189
7190 case CALL_EXPR:
7191 switch (builtin_mathfn_code (t))
7192 {
4f35b1fc 7193 CASE_FLT_FN (BUILT_IN_CEIL):
7194 CASE_FLT_FN (BUILT_IN_FLOOR):
7195 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7196 CASE_FLT_FN (BUILT_IN_RINT):
7197 CASE_FLT_FN (BUILT_IN_ROUND):
7198 CASE_FLT_FN (BUILT_IN_TRUNC):
277f8dd2 7199 return true;
7200
d4a43a03 7201 CASE_FLT_FN (BUILT_IN_FMIN):
7202 CASE_FLT_FN (BUILT_IN_FMAX):
c2f47e15 7203 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7204 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
d4a43a03 7205
277f8dd2 7206 default:
7207 break;
7208 }
7209 break;
7210
7211 default:
7212 break;
7213 }
7214 return false;
7215}
7216
c2f47e15 7217/* FNDECL is assumed to be a builtin where truncation can be propagated
6528f4f4 7218 across (for instance floor((double)f) == (double)floorf (f).
c2f47e15 7219 Do the transformation for a call with argument ARG. */
277f8dd2 7220
6528f4f4 7221static tree
389dd41b 7222fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6528f4f4 7223{
6528f4f4 7224 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
277f8dd2 7225
c2f47e15 7226 if (!validate_arg (arg, REAL_TYPE))
7227 return NULL_TREE;
6528f4f4 7228
277f8dd2 7229 /* Integer rounding functions are idempotent. */
7230 if (fcode == builtin_mathfn_code (arg))
7231 return arg;
7232
7233 /* If argument is already integer valued, and we don't need to worry
7234 about setting errno, there's no need to perform rounding. */
7235 if (! flag_errno_math && integer_valued_real_p (arg))
7236 return arg;
7237
7238 if (optimize)
6528f4f4 7239 {
277f8dd2 7240 tree arg0 = strip_float_extensions (arg);
2426241c 7241 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6528f4f4 7242 tree newtype = TREE_TYPE (arg0);
7243 tree decl;
7244
7245 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7246 && (decl = mathfn_built_in (newtype, fcode)))
389dd41b 7247 return fold_convert_loc (loc, ftype,
7248 build_call_expr_loc (loc, decl, 1,
7249 fold_convert_loc (loc,
7250 newtype,
7251 arg0)));
6528f4f4 7252 }
c2f47e15 7253 return NULL_TREE;
6528f4f4 7254}
7255
c2f47e15 7256/* FNDECL is assumed to be builtin which can narrow the FP type of
7257 the argument, for instance lround((double)f) -> lroundf (f).
7258 Do the transformation for a call with argument ARG. */
9ed65c7f 7259
7260static tree
389dd41b 7261fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
9ed65c7f 7262{
9ed65c7f 7263 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9ed65c7f 7264
c2f47e15 7265 if (!validate_arg (arg, REAL_TYPE))
7266 return NULL_TREE;
9ed65c7f 7267
7268 /* If argument is already integer valued, and we don't need to worry
7269 about setting errno, there's no need to perform rounding. */
7270 if (! flag_errno_math && integer_valued_real_p (arg))
389dd41b 7271 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7272 TREE_TYPE (TREE_TYPE (fndecl)), arg);
9ed65c7f 7273
7274 if (optimize)
7275 {
7276 tree ftype = TREE_TYPE (arg);
7277 tree arg0 = strip_float_extensions (arg);
7278 tree newtype = TREE_TYPE (arg0);
7279 tree decl;
7280
7281 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7282 && (decl = mathfn_built_in (newtype, fcode)))
389dd41b 7283 return build_call_expr_loc (loc, decl, 1,
7284 fold_convert_loc (loc, newtype, arg0));
9ed65c7f 7285 }
73a0da56 7286
80ff6494 7287 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7288 sizeof (int) == sizeof (long). */
7289 if (TYPE_PRECISION (integer_type_node)
7290 == TYPE_PRECISION (long_integer_type_node))
7291 {
7292 tree newfn = NULL_TREE;
7293 switch (fcode)
7294 {
7295 CASE_FLT_FN (BUILT_IN_ICEIL):
7296 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7297 break;
7298
7299 CASE_FLT_FN (BUILT_IN_IFLOOR):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7301 break;
7302
7303 CASE_FLT_FN (BUILT_IN_IROUND):
7304 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7305 break;
7306
7307 CASE_FLT_FN (BUILT_IN_IRINT):
7308 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7309 break;
7310
7311 default:
7312 break;
7313 }
7314
7315 if (newfn)
7316 {
7317 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7318 return fold_convert_loc (loc,
7319 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7320 }
7321 }
7322
73a0da56 7323 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7324 sizeof (long long) == sizeof (long). */
7325 if (TYPE_PRECISION (long_long_integer_type_node)
7326 == TYPE_PRECISION (long_integer_type_node))
7327 {
7328 tree newfn = NULL_TREE;
7329 switch (fcode)
7330 {
7331 CASE_FLT_FN (BUILT_IN_LLCEIL):
7332 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7333 break;
7334
7335 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7336 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7337 break;
7338
7339 CASE_FLT_FN (BUILT_IN_LLROUND):
7340 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7341 break;
7342
7343 CASE_FLT_FN (BUILT_IN_LLRINT):
7344 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7345 break;
7346
7347 default:
7348 break;
7349 }
7350
7351 if (newfn)
7352 {
389dd41b 7353 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7354 return fold_convert_loc (loc,
7355 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
73a0da56 7356 }
7357 }
7358
c2f47e15 7359 return NULL_TREE;
9ed65c7f 7360}
7361
c2f47e15 7362/* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7363 return type. Return NULL_TREE if no simplification can be made. */
c63f4ad3 7364
7365static tree
389dd41b 7366fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
c63f4ad3 7367{
c2f47e15 7368 tree res;
c63f4ad3 7369
b0ce8887 7370 if (!validate_arg (arg, COMPLEX_TYPE)
c63f4ad3 7371 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7372 return NULL_TREE;
7373
b4725390 7374 /* Calculate the result when the argument is a constant. */
7375 if (TREE_CODE (arg) == COMPLEX_CST
7376 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7377 type, mpfr_hypot)))
7378 return res;
48e1416a 7379
1af0d139 7380 if (TREE_CODE (arg) == COMPLEX_EXPR)
7381 {
7382 tree real = TREE_OPERAND (arg, 0);
7383 tree imag = TREE_OPERAND (arg, 1);
48e1416a 7384
1af0d139 7385 /* If either part is zero, cabs is fabs of the other. */
7386 if (real_zerop (real))
389dd41b 7387 return fold_build1_loc (loc, ABS_EXPR, type, imag);
1af0d139 7388 if (real_zerop (imag))
389dd41b 7389 return fold_build1_loc (loc, ABS_EXPR, type, real);
1af0d139 7390
7391 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7392 if (flag_unsafe_math_optimizations
7393 && operand_equal_p (real, imag, OEP_PURE_SAME))
7394 {
2e7ca27b 7395 const REAL_VALUE_TYPE sqrt2_trunc
7910b2fb 7396 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
1af0d139 7397 STRIP_NOPS (real);
389dd41b 7398 return fold_build2_loc (loc, MULT_EXPR, type,
7399 fold_build1_loc (loc, ABS_EXPR, type, real),
2e7ca27b 7400 build_real (type, sqrt2_trunc));
1af0d139 7401 }
7402 }
c63f4ad3 7403
749891b2 7404 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7405 if (TREE_CODE (arg) == NEGATE_EXPR
7406 || TREE_CODE (arg) == CONJ_EXPR)
389dd41b 7407 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
749891b2 7408
7d3f6cc7 7409 /* Don't do this when optimizing for size. */
7410 if (flag_unsafe_math_optimizations
0bfd8d5c 7411 && optimize && optimize_function_for_speed_p (cfun))
c63f4ad3 7412 {
0da0dbfa 7413 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
c63f4ad3 7414
7415 if (sqrtfn != NULL_TREE)
7416 {
c2f47e15 7417 tree rpart, ipart, result;
c63f4ad3 7418
4ee9c684 7419 arg = builtin_save_expr (arg);
29a6518e 7420
389dd41b 7421 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7422 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
c63f4ad3 7423
4ee9c684 7424 rpart = builtin_save_expr (rpart);
7425 ipart = builtin_save_expr (ipart);
c63f4ad3 7426
389dd41b 7427 result = fold_build2_loc (loc, PLUS_EXPR, type,
7428 fold_build2_loc (loc, MULT_EXPR, type,
49d00087 7429 rpart, rpart),
389dd41b 7430 fold_build2_loc (loc, MULT_EXPR, type,
49d00087 7431 ipart, ipart));
c63f4ad3 7432
389dd41b 7433 return build_call_expr_loc (loc, sqrtfn, 1, result);
c63f4ad3 7434 }
7435 }
7436
7437 return NULL_TREE;
7438}
7439
c2373fdb 7440/* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7441 complex tree type of the result. If NEG is true, the imaginary
7442 zero is negative. */
7443
7444static tree
7445build_complex_cproj (tree type, bool neg)
7446{
7447 REAL_VALUE_TYPE rinf, rzero = dconst0;
7448
7449 real_inf (&rinf);
7450 rzero.sign = neg;
7451 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7452 build_real (TREE_TYPE (type), rzero));
7453}
7454
7455/* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7456 return type. Return NULL_TREE if no simplification can be made. */
7457
7458static tree
7459fold_builtin_cproj (location_t loc, tree arg, tree type)
7460{
7461 if (!validate_arg (arg, COMPLEX_TYPE)
7462 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7463 return NULL_TREE;
7464
7465 /* If there are no infinities, return arg. */
7466 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7467 return non_lvalue_loc (loc, arg);
7468
7469 /* Calculate the result when the argument is a constant. */
7470 if (TREE_CODE (arg) == COMPLEX_CST)
7471 {
7472 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7473 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7474
7475 if (real_isinf (real) || real_isinf (imag))
7476 return build_complex_cproj (type, imag->sign);
7477 else
7478 return arg;
7479 }
b4c7e601 7480 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7481 {
7482 tree real = TREE_OPERAND (arg, 0);
7483 tree imag = TREE_OPERAND (arg, 1);
7484
7485 STRIP_NOPS (real);
7486 STRIP_NOPS (imag);
7487
7488 /* If the real part is inf and the imag part is known to be
7489 nonnegative, return (inf + 0i). Remember side-effects are
7490 possible in the imag part. */
7491 if (TREE_CODE (real) == REAL_CST
7492 && real_isinf (TREE_REAL_CST_PTR (real))
7493 && tree_expr_nonnegative_p (imag))
7494 return omit_one_operand_loc (loc, type,
7495 build_complex_cproj (type, false),
7496 arg);
7497
7498 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7499 Remember side-effects are possible in the real part. */
7500 if (TREE_CODE (imag) == REAL_CST
7501 && real_isinf (TREE_REAL_CST_PTR (imag)))
7502 return
7503 omit_one_operand_loc (loc, type,
7504 build_complex_cproj (type, TREE_REAL_CST_PTR
7505 (imag)->sign), arg);
7506 }
c2373fdb 7507
7508 return NULL_TREE;
7509}
7510
c2f47e15 7511/* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7512 Return NULL_TREE if no simplification can be made. */
e6e27594 7513
7514static tree
389dd41b 7515fold_builtin_sqrt (location_t loc, tree arg, tree type)
e6e27594 7516{
7517
7518 enum built_in_function fcode;
b4e8ab0c 7519 tree res;
c2f47e15 7520
7521 if (!validate_arg (arg, REAL_TYPE))
e6e27594 7522 return NULL_TREE;
7523
b4e8ab0c 7524 /* Calculate the result when the argument is a constant. */
7525 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7526 return res;
48e1416a 7527
e6e27594 7528 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7529 fcode = builtin_mathfn_code (arg);
7530 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7531 {
c2f47e15 7532 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
389dd41b 7533 arg = fold_build2_loc (loc, MULT_EXPR, type,
c2f47e15 7534 CALL_EXPR_ARG (arg, 0),
49d00087 7535 build_real (type, dconsthalf));
389dd41b 7536 return build_call_expr_loc (loc, expfn, 1, arg);
e6e27594 7537 }
7538
7539 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7540 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7541 {
7542 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7543
7544 if (powfn)
7545 {
c2f47e15 7546 tree arg0 = CALL_EXPR_ARG (arg, 0);
e6e27594 7547 tree tree_root;
7548 /* The inner root was either sqrt or cbrt. */
57510da6 7549 /* This was a conditional expression but it triggered a bug
18381619 7550 in Sun C 5.5. */
ce6cd837 7551 REAL_VALUE_TYPE dconstroot;
7552 if (BUILTIN_SQRT_P (fcode))
7553 dconstroot = dconsthalf;
7554 else
7555 dconstroot = dconst_third ();
e6e27594 7556
7557 /* Adjust for the outer root. */
7558 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7559 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7560 tree_root = build_real (type, dconstroot);
389dd41b 7561 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
e6e27594 7562 }
7563 }
7564
bc33117f 7565 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
e6e27594 7566 if (flag_unsafe_math_optimizations
7567 && (fcode == BUILT_IN_POW
7568 || fcode == BUILT_IN_POWF
7569 || fcode == BUILT_IN_POWL))
7570 {
c2f47e15 7571 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7572 tree arg0 = CALL_EXPR_ARG (arg, 0);
7573 tree arg1 = CALL_EXPR_ARG (arg, 1);
bc33117f 7574 tree narg1;
7575 if (!tree_expr_nonnegative_p (arg0))
7576 arg0 = build1 (ABS_EXPR, type, arg0);
389dd41b 7577 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
49d00087 7578 build_real (type, dconsthalf));
389dd41b 7579 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
e6e27594 7580 }
7581
7582 return NULL_TREE;
7583}
7584
c2f47e15 7585/* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7586 Return NULL_TREE if no simplification can be made. */
7587
e6e27594 7588static tree
389dd41b 7589fold_builtin_cbrt (location_t loc, tree arg, tree type)
e6e27594 7590{
e6e27594 7591 const enum built_in_function fcode = builtin_mathfn_code (arg);
29f4cd78 7592 tree res;
e6e27594 7593
c2f47e15 7594 if (!validate_arg (arg, REAL_TYPE))
e6e27594 7595 return NULL_TREE;
7596
29f4cd78 7597 /* Calculate the result when the argument is a constant. */
7598 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7599 return res;
e6e27594 7600
cdfeb715 7601 if (flag_unsafe_math_optimizations)
e6e27594 7602 {
cdfeb715 7603 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7604 if (BUILTIN_EXPONENT_P (fcode))
a0c938f0 7605 {
c2f47e15 7606 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
cdfeb715 7607 const REAL_VALUE_TYPE third_trunc =
7910b2fb 7608 real_value_truncate (TYPE_MODE (type), dconst_third ());
389dd41b 7609 arg = fold_build2_loc (loc, MULT_EXPR, type,
c2f47e15 7610 CALL_EXPR_ARG (arg, 0),
49d00087 7611 build_real (type, third_trunc));
389dd41b 7612 return build_call_expr_loc (loc, expfn, 1, arg);
cdfeb715 7613 }
e6e27594 7614
cdfeb715 7615 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7616 if (BUILTIN_SQRT_P (fcode))
a0c938f0 7617 {
cdfeb715 7618 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
e6e27594 7619
cdfeb715 7620 if (powfn)
7621 {
c2f47e15 7622 tree arg0 = CALL_EXPR_ARG (arg, 0);
cdfeb715 7623 tree tree_root;
7910b2fb 7624 REAL_VALUE_TYPE dconstroot = dconst_third ();
cdfeb715 7625
7626 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7627 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7628 tree_root = build_real (type, dconstroot);
389dd41b 7629 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
cdfeb715 7630 }
e6e27594 7631 }
7632
cdfeb715 7633 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7634 if (BUILTIN_CBRT_P (fcode))
a0c938f0 7635 {
c2f47e15 7636 tree arg0 = CALL_EXPR_ARG (arg, 0);
cdfeb715 7637 if (tree_expr_nonnegative_p (arg0))
7638 {
7639 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7640
7641 if (powfn)
a0c938f0 7642 {
cdfeb715 7643 tree tree_root;
7644 REAL_VALUE_TYPE dconstroot;
a0c938f0 7645
3fa759a9 7646 real_arithmetic (&dconstroot, MULT_EXPR,
7910b2fb 7647 dconst_third_ptr (), dconst_third_ptr ());
cdfeb715 7648 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7649 tree_root = build_real (type, dconstroot);
389dd41b 7650 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
cdfeb715 7651 }
7652 }
7653 }
a0c938f0 7654
cdfeb715 7655 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
48e1416a 7656 if (fcode == BUILT_IN_POW
c2f47e15 7657 || fcode == BUILT_IN_POWF
cdfeb715 7658 || fcode == BUILT_IN_POWL)
a0c938f0 7659 {
c2f47e15 7660 tree arg00 = CALL_EXPR_ARG (arg, 0);
7661 tree arg01 = CALL_EXPR_ARG (arg, 1);
cdfeb715 7662 if (tree_expr_nonnegative_p (arg00))
7663 {
c2f47e15 7664 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
cdfeb715 7665 const REAL_VALUE_TYPE dconstroot
7910b2fb 7666 = real_value_truncate (TYPE_MODE (type), dconst_third ());
389dd41b 7667 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
49d00087 7668 build_real (type, dconstroot));
389dd41b 7669 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
cdfeb715 7670 }
7671 }
e6e27594 7672 }
7673 return NULL_TREE;
7674}
7675
c2f47e15 7676/* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7677 TYPE is the type of the return value. Return NULL_TREE if no
7678 simplification can be made. */
7679
e6e27594 7680static tree
389dd41b 7681fold_builtin_cos (location_t loc,
7682 tree arg, tree type, tree fndecl)
e6e27594 7683{
e6ab33d8 7684 tree res, narg;
e6e27594 7685
c2f47e15 7686 if (!validate_arg (arg, REAL_TYPE))
e6e27594 7687 return NULL_TREE;
7688
bffb7645 7689 /* Calculate the result when the argument is a constant. */
728bac60 7690 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
bffb7645 7691 return res;
48e1416a 7692
e6e27594 7693 /* Optimize cos(-x) into cos (x). */
e6ab33d8 7694 if ((narg = fold_strip_sign_ops (arg)))
389dd41b 7695 return build_call_expr_loc (loc, fndecl, 1, narg);
e6e27594 7696
7697 return NULL_TREE;
7698}
7699
c2f47e15 7700/* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7701 Return NULL_TREE if no simplification can be made. */
7702
cacdc1af 7703static tree
389dd41b 7704fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
cacdc1af 7705{
c2f47e15 7706 if (validate_arg (arg, REAL_TYPE))
cacdc1af 7707 {
cacdc1af 7708 tree res, narg;
7709
7710 /* Calculate the result when the argument is a constant. */
7711 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7712 return res;
48e1416a 7713
cacdc1af 7714 /* Optimize cosh(-x) into cosh (x). */
7715 if ((narg = fold_strip_sign_ops (arg)))
389dd41b 7716 return build_call_expr_loc (loc, fndecl, 1, narg);
cacdc1af 7717 }
48e1416a 7718
cacdc1af 7719 return NULL_TREE;
7720}
7721
239d491a 7722/* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7723 argument ARG. TYPE is the type of the return value. Return
7724 NULL_TREE if no simplification can be made. */
7725
7726static tree
965d0f29 7727fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7728 bool hyper)
239d491a 7729{
7730 if (validate_arg (arg, COMPLEX_TYPE)
7731 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7732 {
7733 tree tmp;
7734
239d491a 7735 /* Calculate the result when the argument is a constant. */
7736 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7737 return tmp;
48e1416a 7738
239d491a 7739 /* Optimize fn(-x) into fn(x). */
7740 if ((tmp = fold_strip_sign_ops (arg)))
389dd41b 7741 return build_call_expr_loc (loc, fndecl, 1, tmp);
239d491a 7742 }
7743
7744 return NULL_TREE;
7745}
7746
c2f47e15 7747/* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7748 Return NULL_TREE if no simplification can be made. */
7749
e6e27594 7750static tree
c2f47e15 7751fold_builtin_tan (tree arg, tree type)
e6e27594 7752{
7753 enum built_in_function fcode;
29f4cd78 7754 tree res;
e6e27594 7755
c2f47e15 7756 if (!validate_arg (arg, REAL_TYPE))
e6e27594 7757 return NULL_TREE;
7758
bffb7645 7759 /* Calculate the result when the argument is a constant. */
728bac60 7760 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
bffb7645 7761 return res;
48e1416a 7762
e6e27594 7763 /* Optimize tan(atan(x)) = x. */
7764 fcode = builtin_mathfn_code (arg);
7765 if (flag_unsafe_math_optimizations
7766 && (fcode == BUILT_IN_ATAN
7767 || fcode == BUILT_IN_ATANF
7768 || fcode == BUILT_IN_ATANL))
c2f47e15 7769 return CALL_EXPR_ARG (arg, 0);
e6e27594 7770
7771 return NULL_TREE;
7772}
7773
d735c391 7774/* Fold function call to builtin sincos, sincosf, or sincosl. Return
7775 NULL_TREE if no simplification can be made. */
7776
7777static tree
389dd41b 7778fold_builtin_sincos (location_t loc,
7779 tree arg0, tree arg1, tree arg2)
d735c391 7780{
c2f47e15 7781 tree type;
d735c391 7782 tree res, fn, call;
7783
c2f47e15 7784 if (!validate_arg (arg0, REAL_TYPE)
7785 || !validate_arg (arg1, POINTER_TYPE)
7786 || !validate_arg (arg2, POINTER_TYPE))
d735c391 7787 return NULL_TREE;
7788
d735c391 7789 type = TREE_TYPE (arg0);
d735c391 7790
7791 /* Calculate the result when the argument is a constant. */
7792 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7793 return res;
7794
7795 /* Canonicalize sincos to cexpi. */
2a6b4c77 7796 if (!TARGET_C99_FUNCTIONS)
7797 return NULL_TREE;
d735c391 7798 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7799 if (!fn)
7800 return NULL_TREE;
7801
389dd41b 7802 call = build_call_expr_loc (loc, fn, 1, arg0);
d735c391 7803 call = builtin_save_expr (call);
7804
a75b1c71 7805 return build2 (COMPOUND_EXPR, void_type_node,
d735c391 7806 build2 (MODIFY_EXPR, void_type_node,
389dd41b 7807 build_fold_indirect_ref_loc (loc, arg1),
d735c391 7808 build1 (IMAGPART_EXPR, type, call)),
7809 build2 (MODIFY_EXPR, void_type_node,
389dd41b 7810 build_fold_indirect_ref_loc (loc, arg2),
d735c391 7811 build1 (REALPART_EXPR, type, call)));
7812}
7813
c5bb2c4b 7814/* Fold function call to builtin cexp, cexpf, or cexpl. Return
7815 NULL_TREE if no simplification can be made. */
7816
7817static tree
389dd41b 7818fold_builtin_cexp (location_t loc, tree arg0, tree type)
c5bb2c4b 7819{
c2f47e15 7820 tree rtype;
c5bb2c4b 7821 tree realp, imagp, ifn;
239d491a 7822 tree res;
c5bb2c4b 7823
239d491a 7824 if (!validate_arg (arg0, COMPLEX_TYPE)
b0ce8887 7825 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
c5bb2c4b 7826 return NULL_TREE;
7827
239d491a 7828 /* Calculate the result when the argument is a constant. */
7829 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7830 return res;
48e1416a 7831
c5bb2c4b 7832 rtype = TREE_TYPE (TREE_TYPE (arg0));
7833
7834 /* In case we can figure out the real part of arg0 and it is constant zero
7835 fold to cexpi. */
2a6b4c77 7836 if (!TARGET_C99_FUNCTIONS)
7837 return NULL_TREE;
c5bb2c4b 7838 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7839 if (!ifn)
7840 return NULL_TREE;
7841
389dd41b 7842 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
c5bb2c4b 7843 && real_zerop (realp))
7844 {
389dd41b 7845 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7846 return build_call_expr_loc (loc, ifn, 1, narg);
c5bb2c4b 7847 }
7848
7849 /* In case we can easily decompose real and imaginary parts split cexp
7850 to exp (r) * cexpi (i). */
7851 if (flag_unsafe_math_optimizations
7852 && realp)
7853 {
7854 tree rfn, rcall, icall;
7855
7856 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7857 if (!rfn)
7858 return NULL_TREE;
7859
389dd41b 7860 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
c5bb2c4b 7861 if (!imagp)
7862 return NULL_TREE;
7863
389dd41b 7864 icall = build_call_expr_loc (loc, ifn, 1, imagp);
c5bb2c4b 7865 icall = builtin_save_expr (icall);
389dd41b 7866 rcall = build_call_expr_loc (loc, rfn, 1, realp);
c5bb2c4b 7867 rcall = builtin_save_expr (rcall);
389dd41b 7868 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7869 fold_build2_loc (loc, MULT_EXPR, rtype,
71bf42bb 7870 rcall,
389dd41b 7871 fold_build1_loc (loc, REALPART_EXPR,
7872 rtype, icall)),
7873 fold_build2_loc (loc, MULT_EXPR, rtype,
71bf42bb 7874 rcall,
389dd41b 7875 fold_build1_loc (loc, IMAGPART_EXPR,
7876 rtype, icall)));
c5bb2c4b 7877 }
7878
7879 return NULL_TREE;
7880}
7881
c2f47e15 7882/* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7883 Return NULL_TREE if no simplification can be made. */
277f8dd2 7884
7885static tree
389dd41b 7886fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
277f8dd2 7887{
c2f47e15 7888 if (!validate_arg (arg, REAL_TYPE))
7889 return NULL_TREE;
277f8dd2 7890
7891 /* Optimize trunc of constant value. */
f96bd2bf 7892 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
277f8dd2 7893 {
7894 REAL_VALUE_TYPE r, x;
2426241c 7895 tree type = TREE_TYPE (TREE_TYPE (fndecl));
277f8dd2 7896
7897 x = TREE_REAL_CST (arg);
7898 real_trunc (&r, TYPE_MODE (type), &x);
7899 return build_real (type, r);
7900 }
7901
389dd41b 7902 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
277f8dd2 7903}
7904
c2f47e15 7905/* Fold function call to builtin floor, floorf or floorl with argument ARG.
7906 Return NULL_TREE if no simplification can be made. */
277f8dd2 7907
7908static tree
389dd41b 7909fold_builtin_floor (location_t loc, tree fndecl, tree arg)
277f8dd2 7910{
c2f47e15 7911 if (!validate_arg (arg, REAL_TYPE))
7912 return NULL_TREE;
277f8dd2 7913
7914 /* Optimize floor of constant value. */
f96bd2bf 7915 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
277f8dd2 7916 {
7917 REAL_VALUE_TYPE x;
7918
7919 x = TREE_REAL_CST (arg);
7920 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7921 {
2426241c 7922 tree type = TREE_TYPE (TREE_TYPE (fndecl));
277f8dd2 7923 REAL_VALUE_TYPE r;
7924
7925 real_floor (&r, TYPE_MODE (type), &x);
7926 return build_real (type, r);
7927 }
7928 }
7929
acc2b92e 7930 /* Fold floor (x) where x is nonnegative to trunc (x). */
7931 if (tree_expr_nonnegative_p (arg))
30fe8286 7932 {
7933 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7934 if (truncfn)
389dd41b 7935 return build_call_expr_loc (loc, truncfn, 1, arg);
30fe8286 7936 }
acc2b92e 7937
389dd41b 7938 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
277f8dd2 7939}
7940
c2f47e15 7941/* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7942 Return NULL_TREE if no simplification can be made. */
277f8dd2 7943
7944static tree
389dd41b 7945fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
277f8dd2 7946{
c2f47e15 7947 if (!validate_arg (arg, REAL_TYPE))
7948 return NULL_TREE;
277f8dd2 7949
7950 /* Optimize ceil of constant value. */
f96bd2bf 7951 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
277f8dd2 7952 {
7953 REAL_VALUE_TYPE x;
7954
7955 x = TREE_REAL_CST (arg);
7956 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7957 {
2426241c 7958 tree type = TREE_TYPE (TREE_TYPE (fndecl));
277f8dd2 7959 REAL_VALUE_TYPE r;
7960
7961 real_ceil (&r, TYPE_MODE (type), &x);
7962 return build_real (type, r);
7963 }
7964 }
7965
389dd41b 7966 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
277f8dd2 7967}
7968
c2f47e15 7969/* Fold function call to builtin round, roundf or roundl with argument ARG.
7970 Return NULL_TREE if no simplification can be made. */
89ab3887 7971
7972static tree
389dd41b 7973fold_builtin_round (location_t loc, tree fndecl, tree arg)
89ab3887 7974{
c2f47e15 7975 if (!validate_arg (arg, REAL_TYPE))
7976 return NULL_TREE;
89ab3887 7977
34f17811 7978 /* Optimize round of constant value. */
f96bd2bf 7979 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
89ab3887 7980 {
7981 REAL_VALUE_TYPE x;
7982
7983 x = TREE_REAL_CST (arg);
7984 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7985 {
2426241c 7986 tree type = TREE_TYPE (TREE_TYPE (fndecl));
89ab3887 7987 REAL_VALUE_TYPE r;
7988
7989 real_round (&r, TYPE_MODE (type), &x);
7990 return build_real (type, r);
7991 }
7992 }
7993
389dd41b 7994 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
89ab3887 7995}
7996
34f17811 7997/* Fold function call to builtin lround, lroundf or lroundl (or the
c2f47e15 7998 corresponding long long versions) and other rounding functions. ARG
7999 is the argument to the call. Return NULL_TREE if no simplification
8000 can be made. */
34f17811 8001
8002static tree
389dd41b 8003fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
34f17811 8004{
c2f47e15 8005 if (!validate_arg (arg, REAL_TYPE))
8006 return NULL_TREE;
34f17811 8007
8008 /* Optimize lround of constant value. */
f96bd2bf 8009 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
34f17811 8010 {
8011 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8012
776a7bab 8013 if (real_isfinite (&x))
34f17811 8014 {
2426241c 8015 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
ca9b061d 8016 tree ftype = TREE_TYPE (arg);
a6caa15f 8017 double_int val;
34f17811 8018 REAL_VALUE_TYPE r;
8019
ad52b9b7 8020 switch (DECL_FUNCTION_CODE (fndecl))
8021 {
80ff6494 8022 CASE_FLT_FN (BUILT_IN_IFLOOR):
4f35b1fc 8023 CASE_FLT_FN (BUILT_IN_LFLOOR):
8024 CASE_FLT_FN (BUILT_IN_LLFLOOR):
ad52b9b7 8025 real_floor (&r, TYPE_MODE (ftype), &x);
8026 break;
8027
80ff6494 8028 CASE_FLT_FN (BUILT_IN_ICEIL):
4f35b1fc 8029 CASE_FLT_FN (BUILT_IN_LCEIL):
8030 CASE_FLT_FN (BUILT_IN_LLCEIL):
ac148751 8031 real_ceil (&r, TYPE_MODE (ftype), &x);
8032 break;
8033
80ff6494 8034 CASE_FLT_FN (BUILT_IN_IROUND):
4f35b1fc 8035 CASE_FLT_FN (BUILT_IN_LROUND):
8036 CASE_FLT_FN (BUILT_IN_LLROUND):
ad52b9b7 8037 real_round (&r, TYPE_MODE (ftype), &x);
8038 break;
8039
8040 default:
8041 gcc_unreachable ();
8042 }
8043
a6caa15f 8044 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8045 if (double_int_fits_to_tree_p (itype, val))
8046 return double_int_to_tree (itype, val);
34f17811 8047 }
8048 }
8049
acc2b92e 8050 switch (DECL_FUNCTION_CODE (fndecl))
8051 {
8052 CASE_FLT_FN (BUILT_IN_LFLOOR):
8053 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8054 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8055 if (tree_expr_nonnegative_p (arg))
389dd41b 8056 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8057 TREE_TYPE (TREE_TYPE (fndecl)), arg);
acc2b92e 8058 break;
8059 default:;
8060 }
8061
389dd41b 8062 return fold_fixed_mathfn (loc, fndecl, arg);
34f17811 8063}
8064
70fb4c07 8065/* Fold function call to builtin ffs, clz, ctz, popcount and parity
c2f47e15 8066 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8067 the argument to the call. Return NULL_TREE if no simplification can
8068 be made. */
70fb4c07 8069
8070static tree
c2f47e15 8071fold_builtin_bitop (tree fndecl, tree arg)
70fb4c07 8072{
c2f47e15 8073 if (!validate_arg (arg, INTEGER_TYPE))
70fb4c07 8074 return NULL_TREE;
8075
8076 /* Optimize for constant argument. */
f96bd2bf 8077 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
70fb4c07 8078 {
8079 HOST_WIDE_INT hi, width, result;
8080 unsigned HOST_WIDE_INT lo;
7c446c95 8081 tree type;
70fb4c07 8082
8083 type = TREE_TYPE (arg);
8084 width = TYPE_PRECISION (type);
8085 lo = TREE_INT_CST_LOW (arg);
8086
8087 /* Clear all the bits that are beyond the type's precision. */
8088 if (width > HOST_BITS_PER_WIDE_INT)
8089 {
8090 hi = TREE_INT_CST_HIGH (arg);
8091 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6aaa1f9e 8092 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8093 << (width - HOST_BITS_PER_WIDE_INT));
70fb4c07 8094 }
8095 else
8096 {
8097 hi = 0;
8098 if (width < HOST_BITS_PER_WIDE_INT)
8099 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8100 }
8101
8102 switch (DECL_FUNCTION_CODE (fndecl))
8103 {
4f35b1fc 8104 CASE_INT_FN (BUILT_IN_FFS):
70fb4c07 8105 if (lo != 0)
7e8d812e 8106 result = ffs_hwi (lo);
70fb4c07 8107 else if (hi != 0)
7e8d812e 8108 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
70fb4c07 8109 else
8110 result = 0;
8111 break;
8112
4f35b1fc 8113 CASE_INT_FN (BUILT_IN_CLZ):
70fb4c07 8114 if (hi != 0)
8115 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8116 else if (lo != 0)
8117 result = width - floor_log2 (lo) - 1;
8118 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8119 result = width;
8120 break;
8121
4f35b1fc 8122 CASE_INT_FN (BUILT_IN_CTZ):
70fb4c07 8123 if (lo != 0)
7e8d812e 8124 result = ctz_hwi (lo);
70fb4c07 8125 else if (hi != 0)
7e8d812e 8126 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
70fb4c07 8127 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8128 result = width;
8129 break;
8130
6aaa1f9e 8131 CASE_INT_FN (BUILT_IN_CLRSB):
8132 if (width > HOST_BITS_PER_WIDE_INT
8133 && (hi & ((unsigned HOST_WIDE_INT) 1
8134 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8135 {
8136 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8137 << (width - HOST_BITS_PER_WIDE_INT - 1));
8138 lo = ~lo;
8139 }
8140 else if (width <= HOST_BITS_PER_WIDE_INT
8141 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8142 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8143 if (hi != 0)
8144 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8145 else if (lo != 0)
8146 result = width - floor_log2 (lo) - 2;
8147 else
8148 result = width - 1;
8149 break;
8150
4f35b1fc 8151 CASE_INT_FN (BUILT_IN_POPCOUNT):
70fb4c07 8152 result = 0;
8153 while (lo)
8154 result++, lo &= lo - 1;
8155 while (hi)
7e8d812e 8156 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
70fb4c07 8157 break;
8158
4f35b1fc 8159 CASE_INT_FN (BUILT_IN_PARITY):
70fb4c07 8160 result = 0;
8161 while (lo)
8162 result++, lo &= lo - 1;
8163 while (hi)
7e8d812e 8164 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
70fb4c07 8165 result &= 1;
8166 break;
8167
8168 default:
64db345d 8169 gcc_unreachable ();
70fb4c07 8170 }
8171
2426241c 8172 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
70fb4c07 8173 }
8174
8175 return NULL_TREE;
8176}
8177
74bdbe96 8178/* Fold function call to builtin_bswap and the short, long and long long
42791117 8179 variants. Return NULL_TREE if no simplification can be made. */
8180static tree
c2f47e15 8181fold_builtin_bswap (tree fndecl, tree arg)
42791117 8182{
c2f47e15 8183 if (! validate_arg (arg, INTEGER_TYPE))
8184 return NULL_TREE;
42791117 8185
8186 /* Optimize constant value. */
f96bd2bf 8187 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
42791117 8188 {
8189 HOST_WIDE_INT hi, width, r_hi = 0;
8190 unsigned HOST_WIDE_INT lo, r_lo = 0;
74bdbe96 8191 tree type = TREE_TYPE (TREE_TYPE (fndecl));
42791117 8192
42791117 8193 width = TYPE_PRECISION (type);
8194 lo = TREE_INT_CST_LOW (arg);
8195 hi = TREE_INT_CST_HIGH (arg);
8196
8197 switch (DECL_FUNCTION_CODE (fndecl))
8198 {
74bdbe96 8199 case BUILT_IN_BSWAP16:
42791117 8200 case BUILT_IN_BSWAP32:
8201 case BUILT_IN_BSWAP64:
8202 {
8203 int s;
8204
8205 for (s = 0; s < width; s += 8)
8206 {
8207 int d = width - s - 8;
8208 unsigned HOST_WIDE_INT byte;
8209
8210 if (s < HOST_BITS_PER_WIDE_INT)
8211 byte = (lo >> s) & 0xff;
8212 else
8213 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8214
8215 if (d < HOST_BITS_PER_WIDE_INT)
8216 r_lo |= byte << d;
8217 else
8218 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8219 }
8220 }
8221
8222 break;
8223
8224 default:
8225 gcc_unreachable ();
8226 }
8227
8228 if (width < HOST_BITS_PER_WIDE_INT)
74bdbe96 8229 return build_int_cst (type, r_lo);
42791117 8230 else
74bdbe96 8231 return build_int_cst_wide (type, r_lo, r_hi);
42791117 8232 }
8233
8234 return NULL_TREE;
8235}
c2f47e15 8236
8918c507 8237/* A subroutine of fold_builtin to fold the various logarithmic
29f4cd78 8238 functions. Return NULL_TREE if no simplification can me made.
8239 FUNC is the corresponding MPFR logarithm function. */
8918c507 8240
8241static tree
389dd41b 8242fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
29f4cd78 8243 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8918c507 8244{
c2f47e15 8245 if (validate_arg (arg, REAL_TYPE))
8918c507 8246 {
8918c507 8247 tree type = TREE_TYPE (TREE_TYPE (fndecl));
29f4cd78 8248 tree res;
8918c507 8249 const enum built_in_function fcode = builtin_mathfn_code (arg);
0862b7e9 8250
29f4cd78 8251 /* Calculate the result when the argument is a constant. */
8252 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8253 return res;
8254
8918c507 8255 /* Special case, optimize logN(expN(x)) = x. */
8256 if (flag_unsafe_math_optimizations
29f4cd78 8257 && ((func == mpfr_log
8918c507 8258 && (fcode == BUILT_IN_EXP
8259 || fcode == BUILT_IN_EXPF
8260 || fcode == BUILT_IN_EXPL))
29f4cd78 8261 || (func == mpfr_log2
8918c507 8262 && (fcode == BUILT_IN_EXP2
8263 || fcode == BUILT_IN_EXP2F
8264 || fcode == BUILT_IN_EXP2L))
29f4cd78 8265 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
389dd41b 8266 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8918c507 8267
ca273d4a 8268 /* Optimize logN(func()) for various exponential functions. We
a0c938f0 8269 want to determine the value "x" and the power "exponent" in
8270 order to transform logN(x**exponent) into exponent*logN(x). */
8918c507 8271 if (flag_unsafe_math_optimizations)
a0c938f0 8272 {
8918c507 8273 tree exponent = 0, x = 0;
0862b7e9 8274
8918c507 8275 switch (fcode)
8276 {
4f35b1fc 8277 CASE_FLT_FN (BUILT_IN_EXP):
8918c507 8278 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
48e1416a 8279 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7910b2fb 8280 dconst_e ()));
c2f47e15 8281 exponent = CALL_EXPR_ARG (arg, 0);
8918c507 8282 break;
4f35b1fc 8283 CASE_FLT_FN (BUILT_IN_EXP2):
8918c507 8284 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8285 x = build_real (type, dconst2);
c2f47e15 8286 exponent = CALL_EXPR_ARG (arg, 0);
8918c507 8287 break;
4f35b1fc 8288 CASE_FLT_FN (BUILT_IN_EXP10):
8289 CASE_FLT_FN (BUILT_IN_POW10):
8918c507 8290 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
3fa759a9 8291 {
8292 REAL_VALUE_TYPE dconst10;
8293 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8294 x = build_real (type, dconst10);
8295 }
c2f47e15 8296 exponent = CALL_EXPR_ARG (arg, 0);
8918c507 8297 break;
4f35b1fc 8298 CASE_FLT_FN (BUILT_IN_SQRT):
8918c507 8299 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
c2f47e15 8300 x = CALL_EXPR_ARG (arg, 0);
8918c507 8301 exponent = build_real (type, dconsthalf);
8302 break;
4f35b1fc 8303 CASE_FLT_FN (BUILT_IN_CBRT):
8918c507 8304 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
c2f47e15 8305 x = CALL_EXPR_ARG (arg, 0);
8918c507 8306 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7910b2fb 8307 dconst_third ()));
8918c507 8308 break;
4f35b1fc 8309 CASE_FLT_FN (BUILT_IN_POW):
8918c507 8310 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
c2f47e15 8311 x = CALL_EXPR_ARG (arg, 0);
8312 exponent = CALL_EXPR_ARG (arg, 1);
8918c507 8313 break;
8314 default:
8315 break;
8316 }
8317
8318 /* Now perform the optimization. */
8319 if (x && exponent)
8320 {
389dd41b 8321 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8322 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8918c507 8323 }
8324 }
8325 }
8326
c2f47e15 8327 return NULL_TREE;
8918c507 8328}
0862b7e9 8329
f0c477f2 8330/* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8331 NULL_TREE if no simplification can be made. */
8332
8333static tree
389dd41b 8334fold_builtin_hypot (location_t loc, tree fndecl,
8335 tree arg0, tree arg1, tree type)
f0c477f2 8336{
e6ab33d8 8337 tree res, narg0, narg1;
f0c477f2 8338
c2f47e15 8339 if (!validate_arg (arg0, REAL_TYPE)
8340 || !validate_arg (arg1, REAL_TYPE))
f0c477f2 8341 return NULL_TREE;
8342
8343 /* Calculate the result when the argument is a constant. */
8344 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8345 return res;
48e1416a 8346
6c95f21c 8347 /* If either argument to hypot has a negate or abs, strip that off.
8348 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
e6ab33d8 8349 narg0 = fold_strip_sign_ops (arg0);
8350 narg1 = fold_strip_sign_ops (arg1);
8351 if (narg0 || narg1)
8352 {
48e1416a 8353 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
c2f47e15 8354 narg1 ? narg1 : arg1);
6c95f21c 8355 }
48e1416a 8356
f0c477f2 8357 /* If either argument is zero, hypot is fabs of the other. */
8358 if (real_zerop (arg0))
389dd41b 8359 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
f0c477f2 8360 else if (real_zerop (arg1))
389dd41b 8361 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
48e1416a 8362
6c95f21c 8363 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8364 if (flag_unsafe_math_optimizations
8365 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
f0c477f2 8366 {
2e7ca27b 8367 const REAL_VALUE_TYPE sqrt2_trunc
7910b2fb 8368 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
389dd41b 8369 return fold_build2_loc (loc, MULT_EXPR, type,
8370 fold_build1_loc (loc, ABS_EXPR, type, arg0),
2e7ca27b 8371 build_real (type, sqrt2_trunc));
f0c477f2 8372 }
8373
f0c477f2 8374 return NULL_TREE;
8375}
8376
8377
e6e27594 8378/* Fold a builtin function call to pow, powf, or powl. Return
8379 NULL_TREE if no simplification can be made. */
8380static tree
389dd41b 8381fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
e6e27594 8382{
f0c477f2 8383 tree res;
e6e27594 8384
c2f47e15 8385 if (!validate_arg (arg0, REAL_TYPE)
8386 || !validate_arg (arg1, REAL_TYPE))
e6e27594 8387 return NULL_TREE;
8388
f0c477f2 8389 /* Calculate the result when the argument is a constant. */
8390 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8391 return res;
8392
e6e27594 8393 /* Optimize pow(1.0,y) = 1.0. */
8394 if (real_onep (arg0))
389dd41b 8395 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
e6e27594 8396
8397 if (TREE_CODE (arg1) == REAL_CST
f96bd2bf 8398 && !TREE_OVERFLOW (arg1))
e6e27594 8399 {
198d9bbe 8400 REAL_VALUE_TYPE cint;
e6e27594 8401 REAL_VALUE_TYPE c;
198d9bbe 8402 HOST_WIDE_INT n;
8403
e6e27594 8404 c = TREE_REAL_CST (arg1);
8405
8406 /* Optimize pow(x,0.0) = 1.0. */
8407 if (REAL_VALUES_EQUAL (c, dconst0))
389dd41b 8408 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
e6e27594 8409 arg0);
8410
8411 /* Optimize pow(x,1.0) = x. */
8412 if (REAL_VALUES_EQUAL (c, dconst1))
8413 return arg0;
8414
8415 /* Optimize pow(x,-1.0) = 1.0/x. */
8416 if (REAL_VALUES_EQUAL (c, dconstm1))
389dd41b 8417 return fold_build2_loc (loc, RDIV_EXPR, type,
49d00087 8418 build_real (type, dconst1), arg0);
e6e27594 8419
8420 /* Optimize pow(x,0.5) = sqrt(x). */
8421 if (flag_unsafe_math_optimizations
8422 && REAL_VALUES_EQUAL (c, dconsthalf))
8423 {
8424 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8425
8426 if (sqrtfn != NULL_TREE)
389dd41b 8427 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
e6e27594 8428 }
8429
feb5b3eb 8430 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8431 if (flag_unsafe_math_optimizations)
8432 {
8433 const REAL_VALUE_TYPE dconstroot
7910b2fb 8434 = real_value_truncate (TYPE_MODE (type), dconst_third ());
feb5b3eb 8435
8436 if (REAL_VALUES_EQUAL (c, dconstroot))
8437 {
8438 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8439 if (cbrtfn != NULL_TREE)
389dd41b 8440 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
feb5b3eb 8441 }
8442 }
8443
198d9bbe 8444 /* Check for an integer exponent. */
8445 n = real_to_integer (&c);
8446 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8447 if (real_identical (&c, &cint))
e6e27594 8448 {
a2b30b48 8449 /* Attempt to evaluate pow at compile-time, unless this should
8450 raise an exception. */
198d9bbe 8451 if (TREE_CODE (arg0) == REAL_CST
a2b30b48 8452 && !TREE_OVERFLOW (arg0)
8453 && (n > 0
8454 || (!flag_trapping_math && !flag_errno_math)
8455 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
e6e27594 8456 {
8457 REAL_VALUE_TYPE x;
8458 bool inexact;
8459
8460 x = TREE_REAL_CST (arg0);
8461 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8462 if (flag_unsafe_math_optimizations || !inexact)
8463 return build_real (type, x);
8464 }
198d9bbe 8465
8466 /* Strip sign ops from even integer powers. */
8467 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8468 {
8469 tree narg0 = fold_strip_sign_ops (arg0);
8470 if (narg0)
389dd41b 8471 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
198d9bbe 8472 }
e6e27594 8473 }
8474 }
8475
cdfeb715 8476 if (flag_unsafe_math_optimizations)
e6e27594 8477 {
cdfeb715 8478 const enum built_in_function fcode = builtin_mathfn_code (arg0);
e6e27594 8479
cdfeb715 8480 /* Optimize pow(expN(x),y) = expN(x*y). */
8481 if (BUILTIN_EXPONENT_P (fcode))
a0c938f0 8482 {
c2f47e15 8483 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8484 tree arg = CALL_EXPR_ARG (arg0, 0);
389dd41b 8485 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8486 return build_call_expr_loc (loc, expfn, 1, arg);
cdfeb715 8487 }
e6e27594 8488
cdfeb715 8489 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8490 if (BUILTIN_SQRT_P (fcode))
a0c938f0 8491 {
c2f47e15 8492 tree narg0 = CALL_EXPR_ARG (arg0, 0);
389dd41b 8493 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
49d00087 8494 build_real (type, dconsthalf));
389dd41b 8495 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
cdfeb715 8496 }
8497
8498 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8499 if (BUILTIN_CBRT_P (fcode))
a0c938f0 8500 {
c2f47e15 8501 tree arg = CALL_EXPR_ARG (arg0, 0);
cdfeb715 8502 if (tree_expr_nonnegative_p (arg))
8503 {
8504 const REAL_VALUE_TYPE dconstroot
7910b2fb 8505 = real_value_truncate (TYPE_MODE (type), dconst_third ());
389dd41b 8506 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
49d00087 8507 build_real (type, dconstroot));
389dd41b 8508 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
cdfeb715 8509 }
8510 }
a0c938f0 8511
49e436b5 8512 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
c2f47e15 8513 if (fcode == BUILT_IN_POW
8514 || fcode == BUILT_IN_POWF
8515 || fcode == BUILT_IN_POWL)
a0c938f0 8516 {
c2f47e15 8517 tree arg00 = CALL_EXPR_ARG (arg0, 0);
49e436b5 8518 if (tree_expr_nonnegative_p (arg00))
8519 {
8520 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8521 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8522 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8523 }
cdfeb715 8524 }
e6e27594 8525 }
cdfeb715 8526
e6e27594 8527 return NULL_TREE;
8528}
8529
c2f47e15 8530/* Fold a builtin function call to powi, powif, or powil with argument ARG.
8531 Return NULL_TREE if no simplification can be made. */
b4d0c20c 8532static tree
389dd41b 8533fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
c2f47e15 8534 tree arg0, tree arg1, tree type)
b4d0c20c 8535{
c2f47e15 8536 if (!validate_arg (arg0, REAL_TYPE)
8537 || !validate_arg (arg1, INTEGER_TYPE))
b4d0c20c 8538 return NULL_TREE;
8539
8540 /* Optimize pow(1.0,y) = 1.0. */
8541 if (real_onep (arg0))
389dd41b 8542 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
b4d0c20c 8543
8544 if (host_integerp (arg1, 0))
8545 {
8546 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8547
8548 /* Evaluate powi at compile-time. */
8549 if (TREE_CODE (arg0) == REAL_CST
f96bd2bf 8550 && !TREE_OVERFLOW (arg0))
b4d0c20c 8551 {
8552 REAL_VALUE_TYPE x;
8553 x = TREE_REAL_CST (arg0);
8554 real_powi (&x, TYPE_MODE (type), &x, c);
8555 return build_real (type, x);
8556 }
8557
8558 /* Optimize pow(x,0) = 1.0. */
8559 if (c == 0)
389dd41b 8560 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
b4d0c20c 8561 arg0);
8562
8563 /* Optimize pow(x,1) = x. */
8564 if (c == 1)
8565 return arg0;
8566
8567 /* Optimize pow(x,-1) = 1.0/x. */
8568 if (c == -1)
389dd41b 8569 return fold_build2_loc (loc, RDIV_EXPR, type,
49d00087 8570 build_real (type, dconst1), arg0);
b4d0c20c 8571 }
8572
8573 return NULL_TREE;
8574}
8575
8918c507 8576/* A subroutine of fold_builtin to fold the various exponent
c2f47e15 8577 functions. Return NULL_TREE if no simplification can be made.
debf9994 8578 FUNC is the corresponding MPFR exponent function. */
8918c507 8579
8580static tree
389dd41b 8581fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
debf9994 8582 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8918c507 8583{
c2f47e15 8584 if (validate_arg (arg, REAL_TYPE))
8918c507 8585 {
8918c507 8586 tree type = TREE_TYPE (TREE_TYPE (fndecl));
29f4cd78 8587 tree res;
48e1416a 8588
debf9994 8589 /* Calculate the result when the argument is a constant. */
728bac60 8590 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
debf9994 8591 return res;
8918c507 8592
8593 /* Optimize expN(logN(x)) = x. */
8594 if (flag_unsafe_math_optimizations)
a0c938f0 8595 {
8918c507 8596 const enum built_in_function fcode = builtin_mathfn_code (arg);
8597
debf9994 8598 if ((func == mpfr_exp
8918c507 8599 && (fcode == BUILT_IN_LOG
8600 || fcode == BUILT_IN_LOGF
8601 || fcode == BUILT_IN_LOGL))
debf9994 8602 || (func == mpfr_exp2
8918c507 8603 && (fcode == BUILT_IN_LOG2
8604 || fcode == BUILT_IN_LOG2F
8605 || fcode == BUILT_IN_LOG2L))
debf9994 8606 || (func == mpfr_exp10
8918c507 8607 && (fcode == BUILT_IN_LOG10
8608 || fcode == BUILT_IN_LOG10F
8609 || fcode == BUILT_IN_LOG10L)))
389dd41b 8610 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8918c507 8611 }
8612 }
8613
c2f47e15 8614 return NULL_TREE;
8918c507 8615}
8616
e973ffcc 8617/* Return true if VAR is a VAR_DECL or a component thereof. */
8618
8619static bool
8620var_decl_component_p (tree var)
8621{
8622 tree inner = var;
8623 while (handled_component_p (inner))
8624 inner = TREE_OPERAND (inner, 0);
8625 return SSA_VAR_P (inner);
8626}
8627
4f46f2b9 8628/* Fold function call to builtin memset. Return
9c8a1629 8629 NULL_TREE if no simplification can be made. */
8630
8631static tree
389dd41b 8632fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8633 tree type, bool ignore)
9c8a1629 8634{
45bb3afb 8635 tree var, ret, etype;
4f46f2b9 8636 unsigned HOST_WIDE_INT length, cval;
9c8a1629 8637
c2f47e15 8638 if (! validate_arg (dest, POINTER_TYPE)
8639 || ! validate_arg (c, INTEGER_TYPE)
8640 || ! validate_arg (len, INTEGER_TYPE))
8641 return NULL_TREE;
9c8a1629 8642
4f46f2b9 8643 if (! host_integerp (len, 1))
c2f47e15 8644 return NULL_TREE;
4f46f2b9 8645
9c8a1629 8646 /* If the LEN parameter is zero, return DEST. */
8647 if (integer_zerop (len))
389dd41b 8648 return omit_one_operand_loc (loc, type, dest, c);
9c8a1629 8649
d5d2f74a 8650 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
c2f47e15 8651 return NULL_TREE;
9c8a1629 8652
4f46f2b9 8653 var = dest;
8654 STRIP_NOPS (var);
8655 if (TREE_CODE (var) != ADDR_EXPR)
c2f47e15 8656 return NULL_TREE;
4f46f2b9 8657
8658 var = TREE_OPERAND (var, 0);
8659 if (TREE_THIS_VOLATILE (var))
c2f47e15 8660 return NULL_TREE;
4f46f2b9 8661
45bb3afb 8662 etype = TREE_TYPE (var);
8663 if (TREE_CODE (etype) == ARRAY_TYPE)
8664 etype = TREE_TYPE (etype);
8665
8666 if (!INTEGRAL_TYPE_P (etype)
8667 && !POINTER_TYPE_P (etype))
c2f47e15 8668 return NULL_TREE;
4f46f2b9 8669
e973ffcc 8670 if (! var_decl_component_p (var))
c2f47e15 8671 return NULL_TREE;
e973ffcc 8672
4f46f2b9 8673 length = tree_low_cst (len, 1);
45bb3afb 8674 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
957d0361 8675 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
c2f47e15 8676 return NULL_TREE;
4f46f2b9 8677
8678 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
c2f47e15 8679 return NULL_TREE;
4f46f2b9 8680
8681 if (integer_zerop (c))
8682 cval = 0;
8683 else
8684 {
8685 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
c2f47e15 8686 return NULL_TREE;
4f46f2b9 8687
d5d2f74a 8688 cval = TREE_INT_CST_LOW (c);
4f46f2b9 8689 cval &= 0xff;
8690 cval |= cval << 8;
8691 cval |= cval << 16;
8692 cval |= (cval << 31) << 1;
8693 }
8694
45bb3afb 8695 ret = build_int_cst_type (etype, cval);
389dd41b 8696 var = build_fold_indirect_ref_loc (loc,
8697 fold_convert_loc (loc,
8698 build_pointer_type (etype),
8699 dest));
45bb3afb 8700 ret = build2 (MODIFY_EXPR, etype, var, ret);
4f46f2b9 8701 if (ignore)
8702 return ret;
8703
389dd41b 8704 return omit_one_operand_loc (loc, type, dest, ret);
9c8a1629 8705}
8706
4f46f2b9 8707/* Fold function call to builtin memset. Return
9c8a1629 8708 NULL_TREE if no simplification can be made. */
8709
8710static tree
389dd41b 8711fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
9c8a1629 8712{
c2f47e15 8713 if (! validate_arg (dest, POINTER_TYPE)
8714 || ! validate_arg (size, INTEGER_TYPE))
8715 return NULL_TREE;
9c8a1629 8716
4f46f2b9 8717 if (!ignore)
c2f47e15 8718 return NULL_TREE;
a0c938f0 8719
4f46f2b9 8720 /* New argument list transforming bzero(ptr x, int y) to
8721 memset(ptr x, int 0, size_t y). This is done this way
8722 so that if it isn't expanded inline, we fallback to
8723 calling bzero instead of memset. */
8724
389dd41b 8725 return fold_builtin_memset (loc, dest, integer_zero_node,
a0553bff 8726 fold_convert_loc (loc, size_type_node, size),
c2f47e15 8727 void_type_node, ignore);
9c8a1629 8728}
8729
4f46f2b9 8730/* Fold function call to builtin mem{{,p}cpy,move}. Return
8731 NULL_TREE if no simplification can be made.
8732 If ENDP is 0, return DEST (like memcpy).
8733 If ENDP is 1, return DEST+LEN (like mempcpy).
8734 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8735 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8736 (memmove). */
9c8a1629 8737
8738static tree
389dd41b 8739fold_builtin_memory_op (location_t loc, tree dest, tree src,
8740 tree len, tree type, bool ignore, int endp)
9c8a1629 8741{
c2f47e15 8742 tree destvar, srcvar, expr;
9c8a1629 8743
c2f47e15 8744 if (! validate_arg (dest, POINTER_TYPE)
8745 || ! validate_arg (src, POINTER_TYPE)
8746 || ! validate_arg (len, INTEGER_TYPE))
8747 return NULL_TREE;
9c8a1629 8748
8749 /* If the LEN parameter is zero, return DEST. */
8750 if (integer_zerop (len))
389dd41b 8751 return omit_one_operand_loc (loc, type, dest, src);
9c8a1629 8752
4f46f2b9 8753 /* If SRC and DEST are the same (and not volatile), return
8754 DEST{,+LEN,+LEN-1}. */
9c8a1629 8755 if (operand_equal_p (src, dest, 0))
4f46f2b9 8756 expr = len;
8757 else
8758 {
5a84fdd6 8759 tree srctype, desttype;
153c3b50 8760 unsigned int src_align, dest_align;
182cf5a9 8761 tree off0;
5383fb56 8762
3b1757a2 8763 if (endp == 3)
8764 {
957d0361 8765 src_align = get_pointer_alignment (src);
8766 dest_align = get_pointer_alignment (dest);
5a84fdd6 8767
48e1416a 8768 /* Both DEST and SRC must be pointer types.
3b1757a2 8769 ??? This is what old code did. Is the testing for pointer types
8770 really mandatory?
8771
8772 If either SRC is readonly or length is 1, we can use memcpy. */
3f95c690 8773 if (!dest_align || !src_align)
8774 return NULL_TREE;
8775 if (readonly_data_expr (src)
8776 || (host_integerp (len, 1)
8777 && (MIN (src_align, dest_align) / BITS_PER_UNIT
12c655b1 8778 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
3b1757a2 8779 {
b9a16870 8780 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3b1757a2 8781 if (!fn)
c2f47e15 8782 return NULL_TREE;
389dd41b 8783 return build_call_expr_loc (loc, fn, 3, dest, src, len);
3b1757a2 8784 }
3f95c690 8785
8786 /* If *src and *dest can't overlap, optimize into memcpy as well. */
182cf5a9 8787 if (TREE_CODE (src) == ADDR_EXPR
8788 && TREE_CODE (dest) == ADDR_EXPR)
3f95c690 8789 {
f85fb819 8790 tree src_base, dest_base, fn;
8791 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8792 HOST_WIDE_INT size = -1;
8793 HOST_WIDE_INT maxsize = -1;
8794
182cf5a9 8795 srcvar = TREE_OPERAND (src, 0);
8796 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8797 &size, &maxsize);
8798 destvar = TREE_OPERAND (dest, 0);
8799 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8800 &size, &maxsize);
f85fb819 8801 if (host_integerp (len, 1))
182cf5a9 8802 maxsize = tree_low_cst (len, 1);
f85fb819 8803 else
8804 maxsize = -1;
182cf5a9 8805 src_offset /= BITS_PER_UNIT;
8806 dest_offset /= BITS_PER_UNIT;
f85fb819 8807 if (SSA_VAR_P (src_base)
8808 && SSA_VAR_P (dest_base))
8809 {
8810 if (operand_equal_p (src_base, dest_base, 0)
8811 && ranges_overlap_p (src_offset, maxsize,
8812 dest_offset, maxsize))
8813 return NULL_TREE;
8814 }
182cf5a9 8815 else if (TREE_CODE (src_base) == MEM_REF
8816 && TREE_CODE (dest_base) == MEM_REF)
f85fb819 8817 {
182cf5a9 8818 double_int off;
f85fb819 8819 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
182cf5a9 8820 TREE_OPERAND (dest_base, 0), 0))
8821 return NULL_TREE;
8822 off = double_int_add (mem_ref_offset (src_base),
8823 shwi_to_double_int (src_offset));
8824 if (!double_int_fits_in_shwi_p (off))
8825 return NULL_TREE;
8826 src_offset = off.low;
8827 off = double_int_add (mem_ref_offset (dest_base),
8828 shwi_to_double_int (dest_offset));
8829 if (!double_int_fits_in_shwi_p (off))
8830 return NULL_TREE;
8831 dest_offset = off.low;
8832 if (ranges_overlap_p (src_offset, maxsize,
8833 dest_offset, maxsize))
f85fb819 8834 return NULL_TREE;
8835 }
8836 else
8837 return NULL_TREE;
8838
b9a16870 8839 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3f95c690 8840 if (!fn)
8841 return NULL_TREE;
389dd41b 8842 return build_call_expr_loc (loc, fn, 3, dest, src, len);
3f95c690 8843 }
c4222b63 8844
8845 /* If the destination and source do not alias optimize into
8846 memcpy as well. */
8847 if ((is_gimple_min_invariant (dest)
8848 || TREE_CODE (dest) == SSA_NAME)
8849 && (is_gimple_min_invariant (src)
8850 || TREE_CODE (src) == SSA_NAME))
8851 {
8852 ao_ref destr, srcr;
8853 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8854 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8855 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8856 {
8857 tree fn;
b9a16870 8858 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
c4222b63 8859 if (!fn)
8860 return NULL_TREE;
8861 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8862 }
8863 }
8864
c2f47e15 8865 return NULL_TREE;
3b1757a2 8866 }
4f46f2b9 8867
5a84fdd6 8868 if (!host_integerp (len, 0))
c2f47e15 8869 return NULL_TREE;
5a84fdd6 8870 /* FIXME:
8871 This logic lose for arguments like (type *)malloc (sizeof (type)),
8872 since we strip the casts of up to VOID return value from malloc.
8873 Perhaps we ought to inherit type from non-VOID argument here? */
8874 STRIP_NOPS (src);
8875 STRIP_NOPS (dest);
e07c7898 8876 if (!POINTER_TYPE_P (TREE_TYPE (src))
8877 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8878 return NULL_TREE;
45bb3afb 8879 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8880 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8881 {
8882 tree tem = TREE_OPERAND (src, 0);
8883 STRIP_NOPS (tem);
8884 if (tem != TREE_OPERAND (src, 0))
8885 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8886 }
8887 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8888 {
8889 tree tem = TREE_OPERAND (dest, 0);
8890 STRIP_NOPS (tem);
8891 if (tem != TREE_OPERAND (dest, 0))
8892 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8893 }
5a84fdd6 8894 srctype = TREE_TYPE (TREE_TYPE (src));
e07c7898 8895 if (TREE_CODE (srctype) == ARRAY_TYPE
97d4c6a6 8896 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
45bb3afb 8897 {
8898 srctype = TREE_TYPE (srctype);
8899 STRIP_NOPS (src);
8900 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8901 }
5a84fdd6 8902 desttype = TREE_TYPE (TREE_TYPE (dest));
e07c7898 8903 if (TREE_CODE (desttype) == ARRAY_TYPE
97d4c6a6 8904 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
45bb3afb 8905 {
8906 desttype = TREE_TYPE (desttype);
8907 STRIP_NOPS (dest);
8908 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8909 }
e07c7898 8910 if (TREE_ADDRESSABLE (srctype)
8911 || TREE_ADDRESSABLE (desttype))
c2f47e15 8912 return NULL_TREE;
4f46f2b9 8913
957d0361 8914 src_align = get_pointer_alignment (src);
8915 dest_align = get_pointer_alignment (dest);
153c3b50 8916 if (dest_align < TYPE_ALIGN (desttype)
8917 || src_align < TYPE_ALIGN (srctype))
c2f47e15 8918 return NULL_TREE;
e973ffcc 8919
5a84fdd6 8920 if (!ignore)
8921 dest = builtin_save_expr (dest);
4f46f2b9 8922
182cf5a9 8923 /* Build accesses at offset zero with a ref-all character type. */
8924 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8925 ptr_mode, true), 0);
4f46f2b9 8926
182cf5a9 8927 destvar = dest;
8928 STRIP_NOPS (destvar);
8929 if (TREE_CODE (destvar) == ADDR_EXPR
8930 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8931 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8932 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8933 else
8934 destvar = NULL_TREE;
8935
8936 srcvar = src;
8937 STRIP_NOPS (srcvar);
8938 if (TREE_CODE (srcvar) == ADDR_EXPR
8939 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
5b99ff01 8940 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8941 {
8942 if (!destvar
8943 || src_align >= TYPE_ALIGN (desttype))
8944 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8945 srcvar, off0);
8946 else if (!STRICT_ALIGNMENT)
8947 {
8948 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8949 src_align);
8950 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8951 }
8952 else
8953 srcvar = NULL_TREE;
8954 }
182cf5a9 8955 else
8956 srcvar = NULL_TREE;
5383fb56 8957
8958 if (srcvar == NULL_TREE && destvar == NULL_TREE)
c2f47e15 8959 return NULL_TREE;
4f46f2b9 8960
5383fb56 8961 if (srcvar == NULL_TREE)
8962 {
182cf5a9 8963 STRIP_NOPS (src);
5b99ff01 8964 if (src_align >= TYPE_ALIGN (desttype))
8965 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8966 else
8967 {
8968 if (STRICT_ALIGNMENT)
8969 return NULL_TREE;
8970 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8971 src_align);
8972 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8973 }
5383fb56 8974 }
8975 else if (destvar == NULL_TREE)
8976 {
182cf5a9 8977 STRIP_NOPS (dest);
5b99ff01 8978 if (dest_align >= TYPE_ALIGN (srctype))
8979 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8980 else
8981 {
8982 if (STRICT_ALIGNMENT)
8983 return NULL_TREE;
8984 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8985 dest_align);
8986 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8987 }
5383fb56 8988 }
8989
182cf5a9 8990 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
4f46f2b9 8991 }
8992
8993 if (ignore)
8994 return expr;
8995
8996 if (endp == 0 || endp == 3)
389dd41b 8997 return omit_one_operand_loc (loc, type, dest, expr);
4f46f2b9 8998
8999 if (expr == len)
c2f47e15 9000 expr = NULL_TREE;
4f46f2b9 9001
9002 if (endp == 2)
389dd41b 9003 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
4f46f2b9 9004 ssize_int (1));
9005
2cc66f2a 9006 dest = fold_build_pointer_plus_loc (loc, dest, len);
389dd41b 9007 dest = fold_convert_loc (loc, type, dest);
4f46f2b9 9008 if (expr)
389dd41b 9009 dest = omit_one_operand_loc (loc, type, dest, expr);
4f46f2b9 9010 return dest;
9011}
9012
c2f47e15 9013/* Fold function call to builtin strcpy with arguments DEST and SRC.
9014 If LEN is not NULL, it represents the length of the string to be
9015 copied. Return NULL_TREE if no simplification can be made. */
9c8a1629 9016
f0613857 9017tree
389dd41b 9018fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9c8a1629 9019{
c2f47e15 9020 tree fn;
9c8a1629 9021
c2f47e15 9022 if (!validate_arg (dest, POINTER_TYPE)
9023 || !validate_arg (src, POINTER_TYPE))
9024 return NULL_TREE;
9c8a1629 9025
9026 /* If SRC and DEST are the same (and not volatile), return DEST. */
9027 if (operand_equal_p (src, dest, 0))
389dd41b 9028 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9c8a1629 9029
0bfd8d5c 9030 if (optimize_function_for_size_p (cfun))
c2f47e15 9031 return NULL_TREE;
f0613857 9032
b9a16870 9033 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
f0613857 9034 if (!fn)
c2f47e15 9035 return NULL_TREE;
f0613857 9036
9037 if (!len)
9038 {
9039 len = c_strlen (src, 1);
9040 if (! len || TREE_SIDE_EFFECTS (len))
c2f47e15 9041 return NULL_TREE;
f0613857 9042 }
9043
bd6dcc04 9044 len = fold_convert_loc (loc, size_type_node, len);
9045 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
389dd41b 9046 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9047 build_call_expr_loc (loc, fn, 3, dest, src, len));
9c8a1629 9048}
9049
a65c4d64 9050/* Fold function call to builtin stpcpy with arguments DEST and SRC.
9051 Return NULL_TREE if no simplification can be made. */
9052
9053static tree
9054fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9055{
9056 tree fn, len, lenp1, call, type;
9057
9058 if (!validate_arg (dest, POINTER_TYPE)
9059 || !validate_arg (src, POINTER_TYPE))
9060 return NULL_TREE;
9061
9062 len = c_strlen (src, 1);
9063 if (!len
9064 || TREE_CODE (len) != INTEGER_CST)
9065 return NULL_TREE;
9066
9067 if (optimize_function_for_size_p (cfun)
9068 /* If length is zero it's small enough. */
9069 && !integer_zerop (len))
9070 return NULL_TREE;
9071
b9a16870 9072 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
a65c4d64 9073 if (!fn)
9074 return NULL_TREE;
9075
bd6dcc04 9076 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9077 fold_convert_loc (loc, size_type_node, len),
9078 build_int_cst (size_type_node, 1));
a65c4d64 9079 /* We use dest twice in building our expression. Save it from
9080 multiple expansions. */
9081 dest = builtin_save_expr (dest);
9082 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9083
9084 type = TREE_TYPE (TREE_TYPE (fndecl));
2cc66f2a 9085 dest = fold_build_pointer_plus_loc (loc, dest, len);
a65c4d64 9086 dest = fold_convert_loc (loc, type, dest);
9087 dest = omit_one_operand_loc (loc, type, dest, call);
9088 return dest;
9089}
9090
c2f47e15 9091/* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9092 If SLEN is not NULL, it represents the length of the source string.
9093 Return NULL_TREE if no simplification can be made. */
9c8a1629 9094
f0613857 9095tree
389dd41b 9096fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9097 tree src, tree len, tree slen)
9c8a1629 9098{
c2f47e15 9099 tree fn;
9c8a1629 9100
c2f47e15 9101 if (!validate_arg (dest, POINTER_TYPE)
9102 || !validate_arg (src, POINTER_TYPE)
9103 || !validate_arg (len, INTEGER_TYPE))
9104 return NULL_TREE;
9c8a1629 9105
9106 /* If the LEN parameter is zero, return DEST. */
9107 if (integer_zerop (len))
389dd41b 9108 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9c8a1629 9109
19226313 9110 /* We can't compare slen with len as constants below if len is not a
9111 constant. */
9112 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
c2f47e15 9113 return NULL_TREE;
19226313 9114
f0613857 9115 if (!slen)
9116 slen = c_strlen (src, 1);
9117
9118 /* Now, we must be passed a constant src ptr parameter. */
9119 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
c2f47e15 9120 return NULL_TREE;
f0613857 9121
389dd41b 9122 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
f0613857 9123
9124 /* We do not support simplification of this case, though we do
9125 support it when expanding trees into RTL. */
9126 /* FIXME: generate a call to __builtin_memset. */
9127 if (tree_int_cst_lt (slen, len))
c2f47e15 9128 return NULL_TREE;
f0613857 9129
9130 /* OK transform into builtin memcpy. */
b9a16870 9131 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
f0613857 9132 if (!fn)
c2f47e15 9133 return NULL_TREE;
bd6dcc04 9134
9135 len = fold_convert_loc (loc, size_type_node, len);
389dd41b 9136 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9137 build_call_expr_loc (loc, fn, 3, dest, src, len));
9c8a1629 9138}
9139
7959b13b 9140/* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9141 arguments to the call, and TYPE is its return type.
9142 Return NULL_TREE if no simplification can be made. */
9143
9144static tree
389dd41b 9145fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
7959b13b 9146{
9147 if (!validate_arg (arg1, POINTER_TYPE)
9148 || !validate_arg (arg2, INTEGER_TYPE)
9149 || !validate_arg (len, INTEGER_TYPE))
9150 return NULL_TREE;
9151 else
9152 {
9153 const char *p1;
9154
9155 if (TREE_CODE (arg2) != INTEGER_CST
9156 || !host_integerp (len, 1))
9157 return NULL_TREE;
9158
9159 p1 = c_getstr (arg1);
9160 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9161 {
9162 char c;
9163 const char *r;
9164 tree tem;
9165
9166 if (target_char_cast (arg2, &c))
9167 return NULL_TREE;
9168
609cab98 9169 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
7959b13b 9170
9171 if (r == NULL)
9172 return build_int_cst (TREE_TYPE (arg1), 0);
9173
2cc66f2a 9174 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
389dd41b 9175 return fold_convert_loc (loc, type, tem);
7959b13b 9176 }
9177 return NULL_TREE;
9178 }
9179}
9180
c2f47e15 9181/* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9182 Return NULL_TREE if no simplification can be made. */
9c8a1629 9183
9184static tree
389dd41b 9185fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9c8a1629 9186{
c4fef134 9187 const char *p1, *p2;
9c8a1629 9188
c2f47e15 9189 if (!validate_arg (arg1, POINTER_TYPE)
9190 || !validate_arg (arg2, POINTER_TYPE)
9191 || !validate_arg (len, INTEGER_TYPE))
9192 return NULL_TREE;
9c8a1629 9193
9194 /* If the LEN parameter is zero, return zero. */
9195 if (integer_zerop (len))
389dd41b 9196 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
c4fef134 9197 arg1, arg2);
9c8a1629 9198
9199 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9200 if (operand_equal_p (arg1, arg2, 0))
389dd41b 9201 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
c4fef134 9202
9203 p1 = c_getstr (arg1);
9204 p2 = c_getstr (arg2);
9205
9206 /* If all arguments are constant, and the value of len is not greater
9207 than the lengths of arg1 and arg2, evaluate at compile-time. */
9208 if (host_integerp (len, 1) && p1 && p2
9209 && compare_tree_int (len, strlen (p1) + 1) <= 0
9210 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9211 {
9212 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9213
9214 if (r > 0)
9215 return integer_one_node;
9216 else if (r < 0)
9217 return integer_minus_one_node;
9218 else
9219 return integer_zero_node;
9220 }
9221
9222 /* If len parameter is one, return an expression corresponding to
9223 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9224 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9225 {
9226 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9227 tree cst_uchar_ptr_node
9228 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9229
389dd41b 9230 tree ind1
9231 = fold_convert_loc (loc, integer_type_node,
9232 build1 (INDIRECT_REF, cst_uchar_node,
9233 fold_convert_loc (loc,
9234 cst_uchar_ptr_node,
c4fef134 9235 arg1)));
389dd41b 9236 tree ind2
9237 = fold_convert_loc (loc, integer_type_node,
9238 build1 (INDIRECT_REF, cst_uchar_node,
9239 fold_convert_loc (loc,
9240 cst_uchar_ptr_node,
c4fef134 9241 arg2)));
389dd41b 9242 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
c4fef134 9243 }
9c8a1629 9244
c2f47e15 9245 return NULL_TREE;
9c8a1629 9246}
9247
c2f47e15 9248/* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9249 Return NULL_TREE if no simplification can be made. */
9c8a1629 9250
9251static tree
389dd41b 9252fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9c8a1629 9253{
9c8a1629 9254 const char *p1, *p2;
9255
c2f47e15 9256 if (!validate_arg (arg1, POINTER_TYPE)
9257 || !validate_arg (arg2, POINTER_TYPE))
9258 return NULL_TREE;
9c8a1629 9259
9260 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9261 if (operand_equal_p (arg1, arg2, 0))
c4fef134 9262 return integer_zero_node;
9c8a1629 9263
9264 p1 = c_getstr (arg1);
9265 p2 = c_getstr (arg2);
9266
9267 if (p1 && p2)
9268 {
9c8a1629 9269 const int i = strcmp (p1, p2);
9270 if (i < 0)
c4fef134 9271 return integer_minus_one_node;
9c8a1629 9272 else if (i > 0)
c4fef134 9273 return integer_one_node;
9c8a1629 9274 else
c4fef134 9275 return integer_zero_node;
9276 }
9277
9278 /* If the second arg is "", return *(const unsigned char*)arg1. */
9279 if (p2 && *p2 == '\0')
9280 {
9281 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9282 tree cst_uchar_ptr_node
9283 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9284
389dd41b 9285 return fold_convert_loc (loc, integer_type_node,
9286 build1 (INDIRECT_REF, cst_uchar_node,
9287 fold_convert_loc (loc,
9288 cst_uchar_ptr_node,
9289 arg1)));
c4fef134 9290 }
9291
9292 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9293 if (p1 && *p1 == '\0')
9294 {
9295 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9296 tree cst_uchar_ptr_node
9297 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9298
389dd41b 9299 tree temp
9300 = fold_convert_loc (loc, integer_type_node,
9301 build1 (INDIRECT_REF, cst_uchar_node,
9302 fold_convert_loc (loc,
9303 cst_uchar_ptr_node,
c4fef134 9304 arg2)));
389dd41b 9305 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9c8a1629 9306 }
9307
c2f47e15 9308 return NULL_TREE;
9c8a1629 9309}
9310
c2f47e15 9311/* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9312 Return NULL_TREE if no simplification can be made. */
9c8a1629 9313
9314static tree
389dd41b 9315fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9c8a1629 9316{
9c8a1629 9317 const char *p1, *p2;
9318
c2f47e15 9319 if (!validate_arg (arg1, POINTER_TYPE)
9320 || !validate_arg (arg2, POINTER_TYPE)
9321 || !validate_arg (len, INTEGER_TYPE))
9322 return NULL_TREE;
9c8a1629 9323
9324 /* If the LEN parameter is zero, return zero. */
9325 if (integer_zerop (len))
389dd41b 9326 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
c4fef134 9327 arg1, arg2);
9c8a1629 9328
9329 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9330 if (operand_equal_p (arg1, arg2, 0))
389dd41b 9331 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9c8a1629 9332
9333 p1 = c_getstr (arg1);
9334 p2 = c_getstr (arg2);
9335
9336 if (host_integerp (len, 1) && p1 && p2)
9337 {
9c8a1629 9338 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
c4fef134 9339 if (i > 0)
9340 return integer_one_node;
9341 else if (i < 0)
9342 return integer_minus_one_node;
9c8a1629 9343 else
c4fef134 9344 return integer_zero_node;
9345 }
9346
9347 /* If the second arg is "", and the length is greater than zero,
9348 return *(const unsigned char*)arg1. */
9349 if (p2 && *p2 == '\0'
9350 && TREE_CODE (len) == INTEGER_CST
9351 && tree_int_cst_sgn (len) == 1)
9352 {
9353 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9354 tree cst_uchar_ptr_node
9355 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9356
389dd41b 9357 return fold_convert_loc (loc, integer_type_node,
9358 build1 (INDIRECT_REF, cst_uchar_node,
9359 fold_convert_loc (loc,
9360 cst_uchar_ptr_node,
9361 arg1)));
c4fef134 9362 }
9363
9364 /* If the first arg is "", and the length is greater than zero,
9365 return -*(const unsigned char*)arg2. */
9366 if (p1 && *p1 == '\0'
9367 && TREE_CODE (len) == INTEGER_CST
9368 && tree_int_cst_sgn (len) == 1)
9369 {
9370 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9371 tree cst_uchar_ptr_node
9372 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9373
389dd41b 9374 tree temp = fold_convert_loc (loc, integer_type_node,
9375 build1 (INDIRECT_REF, cst_uchar_node,
9376 fold_convert_loc (loc,
9377 cst_uchar_ptr_node,
9378 arg2)));
9379 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
c4fef134 9380 }
9381
9382 /* If len parameter is one, return an expression corresponding to
9383 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9384 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9385 {
9386 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
4f1b71c0 9387 tree cst_uchar_ptr_node
9388 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9389
389dd41b 9390 tree ind1 = fold_convert_loc (loc, integer_type_node,
9391 build1 (INDIRECT_REF, cst_uchar_node,
9392 fold_convert_loc (loc,
9393 cst_uchar_ptr_node,
9394 arg1)));
9395 tree ind2 = fold_convert_loc (loc, integer_type_node,
9396 build1 (INDIRECT_REF, cst_uchar_node,
9397 fold_convert_loc (loc,
9398 cst_uchar_ptr_node,
9399 arg2)));
9400 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9c8a1629 9401 }
9402
c2f47e15 9403 return NULL_TREE;
9c8a1629 9404}
9405
c2f47e15 9406/* Fold function call to builtin signbit, signbitf or signbitl with argument
9407 ARG. Return NULL_TREE if no simplification can be made. */
27f261ef 9408
9409static tree
389dd41b 9410fold_builtin_signbit (location_t loc, tree arg, tree type)
27f261ef 9411{
c2f47e15 9412 if (!validate_arg (arg, REAL_TYPE))
27f261ef 9413 return NULL_TREE;
9414
27f261ef 9415 /* If ARG is a compile-time constant, determine the result. */
9416 if (TREE_CODE (arg) == REAL_CST
f96bd2bf 9417 && !TREE_OVERFLOW (arg))
27f261ef 9418 {
9419 REAL_VALUE_TYPE c;
9420
9421 c = TREE_REAL_CST (arg);
385f3f36 9422 return (REAL_VALUE_NEGATIVE (c)
9423 ? build_one_cst (type)
9424 : build_zero_cst (type));
27f261ef 9425 }
9426
9427 /* If ARG is non-negative, the result is always zero. */
9428 if (tree_expr_nonnegative_p (arg))
389dd41b 9429 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
27f261ef 9430
9431 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9432 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
de67cbb8 9433 return fold_convert (type,
9434 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9435 build_real (TREE_TYPE (arg), dconst0)));
27f261ef 9436
9437 return NULL_TREE;
9438}
9439
c2f47e15 9440/* Fold function call to builtin copysign, copysignf or copysignl with
9441 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9442 be made. */
467214fd 9443
9444static tree
389dd41b 9445fold_builtin_copysign (location_t loc, tree fndecl,
9446 tree arg1, tree arg2, tree type)
467214fd 9447{
c2f47e15 9448 tree tem;
467214fd 9449
c2f47e15 9450 if (!validate_arg (arg1, REAL_TYPE)
9451 || !validate_arg (arg2, REAL_TYPE))
467214fd 9452 return NULL_TREE;
9453
467214fd 9454 /* copysign(X,X) is X. */
9455 if (operand_equal_p (arg1, arg2, 0))
389dd41b 9456 return fold_convert_loc (loc, type, arg1);
467214fd 9457
9458 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9459 if (TREE_CODE (arg1) == REAL_CST
9460 && TREE_CODE (arg2) == REAL_CST
f96bd2bf 9461 && !TREE_OVERFLOW (arg1)
9462 && !TREE_OVERFLOW (arg2))
467214fd 9463 {
9464 REAL_VALUE_TYPE c1, c2;
9465
9466 c1 = TREE_REAL_CST (arg1);
9467 c2 = TREE_REAL_CST (arg2);
749680e2 9468 /* c1.sign := c2.sign. */
467214fd 9469 real_copysign (&c1, &c2);
9470 return build_real (type, c1);
467214fd 9471 }
9472
9473 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9474 Remember to evaluate Y for side-effects. */
9475 if (tree_expr_nonnegative_p (arg2))
389dd41b 9476 return omit_one_operand_loc (loc, type,
9477 fold_build1_loc (loc, ABS_EXPR, type, arg1),
467214fd 9478 arg2);
9479
198d9bbe 9480 /* Strip sign changing operations for the first argument. */
9481 tem = fold_strip_sign_ops (arg1);
9482 if (tem)
389dd41b 9483 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
198d9bbe 9484
467214fd 9485 return NULL_TREE;
9486}
9487
c2f47e15 9488/* Fold a call to builtin isascii with argument ARG. */
d49367d4 9489
9490static tree
389dd41b 9491fold_builtin_isascii (location_t loc, tree arg)
d49367d4 9492{
c2f47e15 9493 if (!validate_arg (arg, INTEGER_TYPE))
9494 return NULL_TREE;
d49367d4 9495 else
9496 {
9497 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
c90b5d40 9498 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
7002a1c8 9499 build_int_cst (integer_type_node,
c90b5d40 9500 ~ (unsigned HOST_WIDE_INT) 0x7f));
389dd41b 9501 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
7002a1c8 9502 arg, integer_zero_node);
d49367d4 9503 }
9504}
9505
c2f47e15 9506/* Fold a call to builtin toascii with argument ARG. */
d49367d4 9507
9508static tree
389dd41b 9509fold_builtin_toascii (location_t loc, tree arg)
d49367d4 9510{
c2f47e15 9511 if (!validate_arg (arg, INTEGER_TYPE))
9512 return NULL_TREE;
48e1416a 9513
c2f47e15 9514 /* Transform toascii(c) -> (c & 0x7f). */
389dd41b 9515 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
7002a1c8 9516 build_int_cst (integer_type_node, 0x7f));
d49367d4 9517}
9518
c2f47e15 9519/* Fold a call to builtin isdigit with argument ARG. */
df1cf42e 9520
9521static tree
389dd41b 9522fold_builtin_isdigit (location_t loc, tree arg)
df1cf42e 9523{
c2f47e15 9524 if (!validate_arg (arg, INTEGER_TYPE))
9525 return NULL_TREE;
df1cf42e 9526 else
9527 {
9528 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
624d37a6 9529 /* According to the C standard, isdigit is unaffected by locale.
9530 However, it definitely is affected by the target character set. */
624d37a6 9531 unsigned HOST_WIDE_INT target_digit0
9532 = lang_hooks.to_target_charset ('0');
9533
9534 if (target_digit0 == 0)
9535 return NULL_TREE;
9536
389dd41b 9537 arg = fold_convert_loc (loc, unsigned_type_node, arg);
c90b5d40 9538 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9539 build_int_cst (unsigned_type_node, target_digit0));
389dd41b 9540 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
f2532264 9541 build_int_cst (unsigned_type_node, 9));
df1cf42e 9542 }
9543}
27f261ef 9544
c2f47e15 9545/* Fold a call to fabs, fabsf or fabsl with argument ARG. */
d1aade50 9546
9547static tree
389dd41b 9548fold_builtin_fabs (location_t loc, tree arg, tree type)
d1aade50 9549{
c2f47e15 9550 if (!validate_arg (arg, REAL_TYPE))
9551 return NULL_TREE;
d1aade50 9552
389dd41b 9553 arg = fold_convert_loc (loc, type, arg);
d1aade50 9554 if (TREE_CODE (arg) == REAL_CST)
9555 return fold_abs_const (arg, type);
389dd41b 9556 return fold_build1_loc (loc, ABS_EXPR, type, arg);
d1aade50 9557}
9558
c2f47e15 9559/* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
d1aade50 9560
9561static tree
389dd41b 9562fold_builtin_abs (location_t loc, tree arg, tree type)
d1aade50 9563{
c2f47e15 9564 if (!validate_arg (arg, INTEGER_TYPE))
9565 return NULL_TREE;
d1aade50 9566
389dd41b 9567 arg = fold_convert_loc (loc, type, arg);
d1aade50 9568 if (TREE_CODE (arg) == INTEGER_CST)
9569 return fold_abs_const (arg, type);
389dd41b 9570 return fold_build1_loc (loc, ABS_EXPR, type, arg);
d1aade50 9571}
9572
b9be572e 9573/* Fold a fma operation with arguments ARG[012]. */
9574
9575tree
9576fold_fma (location_t loc ATTRIBUTE_UNUSED,
9577 tree type, tree arg0, tree arg1, tree arg2)
9578{
9579 if (TREE_CODE (arg0) == REAL_CST
9580 && TREE_CODE (arg1) == REAL_CST
9581 && TREE_CODE (arg2) == REAL_CST)
9582 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9583
9584 return NULL_TREE;
9585}
9586
9587/* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9588
9589static tree
9590fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9591{
9592 if (validate_arg (arg0, REAL_TYPE)
9593 && validate_arg(arg1, REAL_TYPE)
9594 && validate_arg(arg2, REAL_TYPE))
9595 {
9596 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9597 if (tem)
9598 return tem;
9599
9600 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9601 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9602 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9603 }
9604 return NULL_TREE;
9605}
9606
d4a43a03 9607/* Fold a call to builtin fmin or fmax. */
9608
9609static tree
389dd41b 9610fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9611 tree type, bool max)
d4a43a03 9612{
c2f47e15 9613 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
d4a43a03 9614 {
d4a43a03 9615 /* Calculate the result when the argument is a constant. */
9616 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9617
9618 if (res)
9619 return res;
9620
61fe3555 9621 /* If either argument is NaN, return the other one. Avoid the
9622 transformation if we get (and honor) a signalling NaN. Using
9623 omit_one_operand() ensures we create a non-lvalue. */
9624 if (TREE_CODE (arg0) == REAL_CST
9625 && real_isnan (&TREE_REAL_CST (arg0))
9626 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9627 || ! TREE_REAL_CST (arg0).signalling))
389dd41b 9628 return omit_one_operand_loc (loc, type, arg1, arg0);
61fe3555 9629 if (TREE_CODE (arg1) == REAL_CST
9630 && real_isnan (&TREE_REAL_CST (arg1))
9631 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9632 || ! TREE_REAL_CST (arg1).signalling))
389dd41b 9633 return omit_one_operand_loc (loc, type, arg0, arg1);
61fe3555 9634
d4a43a03 9635 /* Transform fmin/fmax(x,x) -> x. */
9636 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
389dd41b 9637 return omit_one_operand_loc (loc, type, arg0, arg1);
48e1416a 9638
d4a43a03 9639 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9640 functions to return the numeric arg if the other one is NaN.
9641 These tree codes don't honor that, so only transform if
9642 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9643 handled, so we don't have to worry about it either. */
9644 if (flag_finite_math_only)
389dd41b 9645 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9646 fold_convert_loc (loc, type, arg0),
9647 fold_convert_loc (loc, type, arg1));
d4a43a03 9648 }
9649 return NULL_TREE;
9650}
9651
abe4dcf6 9652/* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9653
9654static tree
389dd41b 9655fold_builtin_carg (location_t loc, tree arg, tree type)
abe4dcf6 9656{
239d491a 9657 if (validate_arg (arg, COMPLEX_TYPE)
9658 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
abe4dcf6 9659 {
9660 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
48e1416a 9661
abe4dcf6 9662 if (atan2_fn)
9663 {
c2f47e15 9664 tree new_arg = builtin_save_expr (arg);
389dd41b 9665 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9666 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9667 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
abe4dcf6 9668 }
9669 }
48e1416a 9670
abe4dcf6 9671 return NULL_TREE;
9672}
9673
cb2b9385 9674/* Fold a call to builtin logb/ilogb. */
9675
9676static tree
389dd41b 9677fold_builtin_logb (location_t loc, tree arg, tree rettype)
cb2b9385 9678{
9679 if (! validate_arg (arg, REAL_TYPE))
9680 return NULL_TREE;
48e1416a 9681
cb2b9385 9682 STRIP_NOPS (arg);
48e1416a 9683
cb2b9385 9684 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9685 {
9686 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
48e1416a 9687
cb2b9385 9688 switch (value->cl)
9689 {
9690 case rvc_nan:
9691 case rvc_inf:
9692 /* If arg is Inf or NaN and we're logb, return it. */
9693 if (TREE_CODE (rettype) == REAL_TYPE)
389dd41b 9694 return fold_convert_loc (loc, rettype, arg);
cb2b9385 9695 /* Fall through... */
9696 case rvc_zero:
9697 /* Zero may set errno and/or raise an exception for logb, also
9698 for ilogb we don't know FP_ILOGB0. */
9699 return NULL_TREE;
9700 case rvc_normal:
9701 /* For normal numbers, proceed iff radix == 2. In GCC,
9702 normalized significands are in the range [0.5, 1.0). We
9703 want the exponent as if they were [1.0, 2.0) so get the
9704 exponent and subtract 1. */
9705 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
389dd41b 9706 return fold_convert_loc (loc, rettype,
7002a1c8 9707 build_int_cst (integer_type_node,
389dd41b 9708 REAL_EXP (value)-1));
cb2b9385 9709 break;
9710 }
9711 }
48e1416a 9712
cb2b9385 9713 return NULL_TREE;
9714}
9715
9716/* Fold a call to builtin significand, if radix == 2. */
9717
9718static tree
389dd41b 9719fold_builtin_significand (location_t loc, tree arg, tree rettype)
cb2b9385 9720{
9721 if (! validate_arg (arg, REAL_TYPE))
9722 return NULL_TREE;
48e1416a 9723
cb2b9385 9724 STRIP_NOPS (arg);
48e1416a 9725
cb2b9385 9726 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9727 {
9728 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
48e1416a 9729
cb2b9385 9730 switch (value->cl)
9731 {
9732 case rvc_zero:
9733 case rvc_nan:
9734 case rvc_inf:
9735 /* If arg is +-0, +-Inf or +-NaN, then return it. */
389dd41b 9736 return fold_convert_loc (loc, rettype, arg);
cb2b9385 9737 case rvc_normal:
9738 /* For normal numbers, proceed iff radix == 2. */
9739 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9740 {
9741 REAL_VALUE_TYPE result = *value;
9742 /* In GCC, normalized significands are in the range [0.5,
9743 1.0). We want them to be [1.0, 2.0) so set the
9744 exponent to 1. */
9745 SET_REAL_EXP (&result, 1);
9746 return build_real (rettype, result);
9747 }
9748 break;
9749 }
9750 }
48e1416a 9751
cb2b9385 9752 return NULL_TREE;
9753}
9754
3838b9ae 9755/* Fold a call to builtin frexp, we can assume the base is 2. */
9756
9757static tree
389dd41b 9758fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
3838b9ae 9759{
9760 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9761 return NULL_TREE;
48e1416a 9762
3838b9ae 9763 STRIP_NOPS (arg0);
48e1416a 9764
3838b9ae 9765 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9766 return NULL_TREE;
48e1416a 9767
389dd41b 9768 arg1 = build_fold_indirect_ref_loc (loc, arg1);
3838b9ae 9769
9770 /* Proceed if a valid pointer type was passed in. */
9771 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9772 {
9773 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9774 tree frac, exp;
48e1416a 9775
3838b9ae 9776 switch (value->cl)
9777 {
9778 case rvc_zero:
9779 /* For +-0, return (*exp = 0, +-0). */
9780 exp = integer_zero_node;
9781 frac = arg0;
9782 break;
9783 case rvc_nan:
9784 case rvc_inf:
9785 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
389dd41b 9786 return omit_one_operand_loc (loc, rettype, arg0, arg1);
3838b9ae 9787 case rvc_normal:
9788 {
9789 /* Since the frexp function always expects base 2, and in
9790 GCC normalized significands are already in the range
9791 [0.5, 1.0), we have exactly what frexp wants. */
9792 REAL_VALUE_TYPE frac_rvt = *value;
9793 SET_REAL_EXP (&frac_rvt, 0);
9794 frac = build_real (rettype, frac_rvt);
7002a1c8 9795 exp = build_int_cst (integer_type_node, REAL_EXP (value));
3838b9ae 9796 }
9797 break;
9798 default:
9799 gcc_unreachable ();
9800 }
48e1416a 9801
3838b9ae 9802 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
389dd41b 9803 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
3838b9ae 9804 TREE_SIDE_EFFECTS (arg1) = 1;
389dd41b 9805 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
3838b9ae 9806 }
9807
9808 return NULL_TREE;
9809}
9810
7587301b 9811/* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9812 then we can assume the base is two. If it's false, then we have to
9813 check the mode of the TYPE parameter in certain cases. */
9814
9815static tree
389dd41b 9816fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9817 tree type, bool ldexp)
7587301b 9818{
9819 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9820 {
9821 STRIP_NOPS (arg0);
9822 STRIP_NOPS (arg1);
9823
9824 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9825 if (real_zerop (arg0) || integer_zerop (arg1)
9826 || (TREE_CODE (arg0) == REAL_CST
776a7bab 9827 && !real_isfinite (&TREE_REAL_CST (arg0))))
389dd41b 9828 return omit_one_operand_loc (loc, type, arg0, arg1);
48e1416a 9829
7587301b 9830 /* If both arguments are constant, then try to evaluate it. */
9831 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9832 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9833 && host_integerp (arg1, 0))
9834 {
9835 /* Bound the maximum adjustment to twice the range of the
9836 mode's valid exponents. Use abs to ensure the range is
9837 positive as a sanity check. */
48e1416a 9838 const long max_exp_adj = 2 *
7587301b 9839 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9840 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9841
9842 /* Get the user-requested adjustment. */
9843 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
48e1416a 9844
7587301b 9845 /* The requested adjustment must be inside this range. This
9846 is a preliminary cap to avoid things like overflow, we
9847 may still fail to compute the result for other reasons. */
9848 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9849 {
9850 REAL_VALUE_TYPE initial_result;
48e1416a 9851
7587301b 9852 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9853
9854 /* Ensure we didn't overflow. */
9855 if (! real_isinf (&initial_result))
9856 {
9857 const REAL_VALUE_TYPE trunc_result
9858 = real_value_truncate (TYPE_MODE (type), initial_result);
48e1416a 9859
7587301b 9860 /* Only proceed if the target mode can hold the
9861 resulting value. */
9862 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9863 return build_real (type, trunc_result);
9864 }
9865 }
9866 }
9867 }
9868
9869 return NULL_TREE;
9870}
9871
ebf8b4f5 9872/* Fold a call to builtin modf. */
9873
9874static tree
389dd41b 9875fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
ebf8b4f5 9876{
9877 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9878 return NULL_TREE;
48e1416a 9879
ebf8b4f5 9880 STRIP_NOPS (arg0);
48e1416a 9881
ebf8b4f5 9882 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9883 return NULL_TREE;
48e1416a 9884
389dd41b 9885 arg1 = build_fold_indirect_ref_loc (loc, arg1);
ebf8b4f5 9886
9887 /* Proceed if a valid pointer type was passed in. */
9888 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9889 {
9890 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9891 REAL_VALUE_TYPE trunc, frac;
9892
9893 switch (value->cl)
9894 {
9895 case rvc_nan:
9896 case rvc_zero:
9897 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9898 trunc = frac = *value;
9899 break;
9900 case rvc_inf:
9901 /* For +-Inf, return (*arg1 = arg0, +-0). */
9902 frac = dconst0;
9903 frac.sign = value->sign;
9904 trunc = *value;
9905 break;
9906 case rvc_normal:
9907 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9908 real_trunc (&trunc, VOIDmode, value);
9909 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9910 /* If the original number was negative and already
9911 integral, then the fractional part is -0.0. */
9912 if (value->sign && frac.cl == rvc_zero)
9913 frac.sign = value->sign;
9914 break;
9915 }
48e1416a 9916
ebf8b4f5 9917 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
389dd41b 9918 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
ebf8b4f5 9919 build_real (rettype, trunc));
9920 TREE_SIDE_EFFECTS (arg1) = 1;
389dd41b 9921 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
ebf8b4f5 9922 build_real (rettype, frac));
9923 }
48e1416a 9924
ebf8b4f5 9925 return NULL_TREE;
9926}
9927
a65c4d64 9928/* Given a location LOC, an interclass builtin function decl FNDECL
9929 and its single argument ARG, return an folded expression computing
9930 the same, or NULL_TREE if we either couldn't or didn't want to fold
9931 (the latter happen if there's an RTL instruction available). */
9932
9933static tree
9934fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9935{
9936 enum machine_mode mode;
9937
9938 if (!validate_arg (arg, REAL_TYPE))
9939 return NULL_TREE;
9940
9941 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9942 return NULL_TREE;
9943
9944 mode = TYPE_MODE (TREE_TYPE (arg));
9945
9946 /* If there is no optab, try generic code. */
9947 switch (DECL_FUNCTION_CODE (fndecl))
9948 {
9949 tree result;
9950
9951 CASE_FLT_FN (BUILT_IN_ISINF):
9952 {
9953 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
b9a16870 9954 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
a65c4d64 9955 tree const type = TREE_TYPE (arg);
9956 REAL_VALUE_TYPE r;
9957 char buf[128];
9958
9959 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9960 real_from_string (&r, buf);
9961 result = build_call_expr (isgr_fn, 2,
9962 fold_build1_loc (loc, ABS_EXPR, type, arg),
9963 build_real (type, r));
9964 return result;
9965 }
9966 CASE_FLT_FN (BUILT_IN_FINITE):
9967 case BUILT_IN_ISFINITE:
9968 {
9969 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
b9a16870 9970 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
a65c4d64 9971 tree const type = TREE_TYPE (arg);
9972 REAL_VALUE_TYPE r;
9973 char buf[128];
9974
9975 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9976 real_from_string (&r, buf);
9977 result = build_call_expr (isle_fn, 2,
9978 fold_build1_loc (loc, ABS_EXPR, type, arg),
9979 build_real (type, r));
9980 /*result = fold_build2_loc (loc, UNGT_EXPR,
9981 TREE_TYPE (TREE_TYPE (fndecl)),
9982 fold_build1_loc (loc, ABS_EXPR, type, arg),
9983 build_real (type, r));
9984 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9985 TREE_TYPE (TREE_TYPE (fndecl)),
9986 result);*/
9987 return result;
9988 }
9989 case BUILT_IN_ISNORMAL:
9990 {
9991 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9992 islessequal(fabs(x),DBL_MAX). */
b9a16870 9993 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9994 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
a65c4d64 9995 tree const type = TREE_TYPE (arg);
9996 REAL_VALUE_TYPE rmax, rmin;
9997 char buf[128];
9998
9999 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10000 real_from_string (&rmax, buf);
10001 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10002 real_from_string (&rmin, buf);
10003 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10004 result = build_call_expr (isle_fn, 2, arg,
10005 build_real (type, rmax));
10006 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10007 build_call_expr (isge_fn, 2, arg,
10008 build_real (type, rmin)));
10009 return result;
10010 }
10011 default:
10012 break;
10013 }
10014
10015 return NULL_TREE;
10016}
10017
726069ba 10018/* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
c2f47e15 10019 ARG is the argument for the call. */
726069ba 10020
10021static tree
389dd41b 10022fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
726069ba 10023{
726069ba 10024 tree type = TREE_TYPE (TREE_TYPE (fndecl));
726069ba 10025 REAL_VALUE_TYPE r;
10026
c2f47e15 10027 if (!validate_arg (arg, REAL_TYPE))
d43cee80 10028 return NULL_TREE;
726069ba 10029
726069ba 10030 switch (builtin_index)
10031 {
10032 case BUILT_IN_ISINF:
b70bfd00 10033 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
389dd41b 10034 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
726069ba 10035
10036 if (TREE_CODE (arg) == REAL_CST)
10037 {
10038 r = TREE_REAL_CST (arg);
10039 if (real_isinf (&r))
10040 return real_compare (GT_EXPR, &r, &dconst0)
10041 ? integer_one_node : integer_minus_one_node;
10042 else
10043 return integer_zero_node;
10044 }
10045
10046 return NULL_TREE;
10047
c319d56a 10048 case BUILT_IN_ISINF_SIGN:
10049 {
10050 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10051 /* In a boolean context, GCC will fold the inner COND_EXPR to
10052 1. So e.g. "if (isinf_sign(x))" would be folded to just
10053 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10054 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
b9a16870 10055 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
c319d56a 10056 tree tmp = NULL_TREE;
10057
10058 arg = builtin_save_expr (arg);
10059
10060 if (signbit_fn && isinf_fn)
10061 {
389dd41b 10062 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10063 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
c319d56a 10064
389dd41b 10065 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
c319d56a 10066 signbit_call, integer_zero_node);
389dd41b 10067 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
c319d56a 10068 isinf_call, integer_zero_node);
48e1416a 10069
389dd41b 10070 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
c319d56a 10071 integer_minus_one_node, integer_one_node);
389dd41b 10072 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10073 isinf_call, tmp,
c319d56a 10074 integer_zero_node);
10075 }
10076
10077 return tmp;
10078 }
10079
cde061c1 10080 case BUILT_IN_ISFINITE:
b70bfd00 10081 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10082 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
389dd41b 10083 return omit_one_operand_loc (loc, type, integer_one_node, arg);
726069ba 10084
10085 if (TREE_CODE (arg) == REAL_CST)
10086 {
10087 r = TREE_REAL_CST (arg);
776a7bab 10088 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
726069ba 10089 }
10090
10091 return NULL_TREE;
10092
10093 case BUILT_IN_ISNAN:
b70bfd00 10094 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
389dd41b 10095 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
726069ba 10096
10097 if (TREE_CODE (arg) == REAL_CST)
10098 {
10099 r = TREE_REAL_CST (arg);
10100 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10101 }
10102
10103 arg = builtin_save_expr (arg);
389dd41b 10104 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
726069ba 10105
10106 default:
64db345d 10107 gcc_unreachable ();
726069ba 10108 }
10109}
10110
19fbe3a4 10111/* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10112 This builtin will generate code to return the appropriate floating
10113 point classification depending on the value of the floating point
10114 number passed in. The possible return values must be supplied as
921b27c0 10115 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
19fbe3a4 10116 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10117 one floating point argument which is "type generic". */
10118
10119static tree
389dd41b 10120fold_builtin_fpclassify (location_t loc, tree exp)
19fbe3a4 10121{
921b27c0 10122 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10123 arg, type, res, tmp;
19fbe3a4 10124 enum machine_mode mode;
10125 REAL_VALUE_TYPE r;
10126 char buf[128];
48e1416a 10127
19fbe3a4 10128 /* Verify the required arguments in the original call. */
10129 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10130 INTEGER_TYPE, INTEGER_TYPE,
10131 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10132 return NULL_TREE;
48e1416a 10133
19fbe3a4 10134 fp_nan = CALL_EXPR_ARG (exp, 0);
921b27c0 10135 fp_infinite = CALL_EXPR_ARG (exp, 1);
19fbe3a4 10136 fp_normal = CALL_EXPR_ARG (exp, 2);
10137 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10138 fp_zero = CALL_EXPR_ARG (exp, 4);
10139 arg = CALL_EXPR_ARG (exp, 5);
10140 type = TREE_TYPE (arg);
10141 mode = TYPE_MODE (type);
389dd41b 10142 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
19fbe3a4 10143
48e1416a 10144 /* fpclassify(x) ->
19fbe3a4 10145 isnan(x) ? FP_NAN :
921b27c0 10146 (fabs(x) == Inf ? FP_INFINITE :
19fbe3a4 10147 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10148 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
48e1416a 10149
389dd41b 10150 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
19fbe3a4 10151 build_real (type, dconst0));
389dd41b 10152 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10153 tmp, fp_zero, fp_subnormal);
19fbe3a4 10154
10155 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10156 real_from_string (&r, buf);
389dd41b 10157 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10158 arg, build_real (type, r));
10159 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
48e1416a 10160
19fbe3a4 10161 if (HONOR_INFINITIES (mode))
10162 {
10163 real_inf (&r);
389dd41b 10164 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
19fbe3a4 10165 build_real (type, r));
389dd41b 10166 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10167 fp_infinite, res);
19fbe3a4 10168 }
10169
10170 if (HONOR_NANS (mode))
10171 {
389dd41b 10172 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10173 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
19fbe3a4 10174 }
48e1416a 10175
19fbe3a4 10176 return res;
10177}
10178
9bc9f15f 10179/* Fold a call to an unordered comparison function such as
d5019fe8 10180 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
c2f47e15 10181 being called and ARG0 and ARG1 are the arguments for the call.
726069ba 10182 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10183 the opposite of the desired result. UNORDERED_CODE is used
10184 for modes that can hold NaNs and ORDERED_CODE is used for
10185 the rest. */
9bc9f15f 10186
10187static tree
389dd41b 10188fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9bc9f15f 10189 enum tree_code unordered_code,
10190 enum tree_code ordered_code)
10191{
859f903a 10192 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9bc9f15f 10193 enum tree_code code;
6978db0d 10194 tree type0, type1;
10195 enum tree_code code0, code1;
10196 tree cmp_type = NULL_TREE;
9bc9f15f 10197
6978db0d 10198 type0 = TREE_TYPE (arg0);
10199 type1 = TREE_TYPE (arg1);
a0c938f0 10200
6978db0d 10201 code0 = TREE_CODE (type0);
10202 code1 = TREE_CODE (type1);
a0c938f0 10203
6978db0d 10204 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10205 /* Choose the wider of two real types. */
10206 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10207 ? type0 : type1;
10208 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10209 cmp_type = type0;
10210 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10211 cmp_type = type1;
a0c938f0 10212
389dd41b 10213 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10214 arg1 = fold_convert_loc (loc, cmp_type, arg1);
859f903a 10215
10216 if (unordered_code == UNORDERED_EXPR)
10217 {
b70bfd00 10218 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
389dd41b 10219 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10220 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
859f903a 10221 }
9bc9f15f 10222
b70bfd00 10223 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10224 : ordered_code;
389dd41b 10225 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10226 fold_build2_loc (loc, code, type, arg0, arg1));
9bc9f15f 10227}
10228
c2f47e15 10229/* Fold a call to built-in function FNDECL with 0 arguments.
10230 IGNORE is true if the result of the function call is ignored. This
10231 function returns NULL_TREE if no simplification was possible. */
650e4c94 10232
4ee9c684 10233static tree
389dd41b 10234fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
650e4c94 10235{
e9f80ff5 10236 tree type = TREE_TYPE (TREE_TYPE (fndecl));
c2f47e15 10237 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
189b3398 10238 switch (fcode)
650e4c94 10239 {
c2f47e15 10240 CASE_FLT_FN (BUILT_IN_INF):
10241 case BUILT_IN_INFD32:
10242 case BUILT_IN_INFD64:
10243 case BUILT_IN_INFD128:
389dd41b 10244 return fold_builtin_inf (loc, type, true);
7c2f0500 10245
c2f47e15 10246 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
389dd41b 10247 return fold_builtin_inf (loc, type, false);
7c2f0500 10248
c2f47e15 10249 case BUILT_IN_CLASSIFY_TYPE:
10250 return fold_builtin_classify_type (NULL_TREE);
7c2f0500 10251
c2f47e15 10252 default:
10253 break;
10254 }
10255 return NULL_TREE;
10256}
7c2f0500 10257
c2f47e15 10258/* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10259 IGNORE is true if the result of the function call is ignored. This
10260 function returns NULL_TREE if no simplification was possible. */
7c2f0500 10261
c2f47e15 10262static tree
389dd41b 10263fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
c2f47e15 10264{
10265 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10266 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10267 switch (fcode)
10268 {
650e4c94 10269 case BUILT_IN_CONSTANT_P:
7c2f0500 10270 {
c2f47e15 10271 tree val = fold_builtin_constant_p (arg0);
7c2f0500 10272
7c2f0500 10273 /* Gimplification will pull the CALL_EXPR for the builtin out of
10274 an if condition. When not optimizing, we'll not CSE it back.
10275 To avoid link error types of regressions, return false now. */
10276 if (!val && !optimize)
10277 val = integer_zero_node;
10278
10279 return val;
10280 }
650e4c94 10281
539a3a92 10282 case BUILT_IN_CLASSIFY_TYPE:
c2f47e15 10283 return fold_builtin_classify_type (arg0);
539a3a92 10284
650e4c94 10285 case BUILT_IN_STRLEN:
c7cbde74 10286 return fold_builtin_strlen (loc, type, arg0);
650e4c94 10287
4f35b1fc 10288 CASE_FLT_FN (BUILT_IN_FABS):
389dd41b 10289 return fold_builtin_fabs (loc, arg0, type);
d1aade50 10290
10291 case BUILT_IN_ABS:
10292 case BUILT_IN_LABS:
10293 case BUILT_IN_LLABS:
10294 case BUILT_IN_IMAXABS:
389dd41b 10295 return fold_builtin_abs (loc, arg0, type);
c63f4ad3 10296
4f35b1fc 10297 CASE_FLT_FN (BUILT_IN_CONJ):
239d491a 10298 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10299 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
389dd41b 10300 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
c2f47e15 10301 break;
36d3581d 10302
4f35b1fc 10303 CASE_FLT_FN (BUILT_IN_CREAL):
239d491a 10304 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10305 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
389dd41b 10306 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
c2f47e15 10307 break;
36d3581d 10308
4f35b1fc 10309 CASE_FLT_FN (BUILT_IN_CIMAG):
b0ce8887 10310 if (validate_arg (arg0, COMPLEX_TYPE)
10311 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
389dd41b 10312 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
c2f47e15 10313 break;
36d3581d 10314
503733d5 10315 CASE_FLT_FN (BUILT_IN_CCOS):
389dd41b 10316 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
48e1416a 10317
503733d5 10318 CASE_FLT_FN (BUILT_IN_CCOSH):
389dd41b 10319 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
48e1416a 10320
c2373fdb 10321 CASE_FLT_FN (BUILT_IN_CPROJ):
10322 return fold_builtin_cproj(loc, arg0, type);
10323
239d491a 10324 CASE_FLT_FN (BUILT_IN_CSIN):
10325 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10326 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10327 return do_mpc_arg1 (arg0, type, mpc_sin);
c2f47e15 10328 break;
48e1416a 10329
239d491a 10330 CASE_FLT_FN (BUILT_IN_CSINH):
10331 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10332 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10333 return do_mpc_arg1 (arg0, type, mpc_sinh);
10334 break;
48e1416a 10335
239d491a 10336 CASE_FLT_FN (BUILT_IN_CTAN):
10337 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10338 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10339 return do_mpc_arg1 (arg0, type, mpc_tan);
10340 break;
48e1416a 10341
239d491a 10342 CASE_FLT_FN (BUILT_IN_CTANH):
10343 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10344 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10345 return do_mpc_arg1 (arg0, type, mpc_tanh);
10346 break;
48e1416a 10347
239d491a 10348 CASE_FLT_FN (BUILT_IN_CLOG):
10349 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10350 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10351 return do_mpc_arg1 (arg0, type, mpc_log);
10352 break;
48e1416a 10353
239d491a 10354 CASE_FLT_FN (BUILT_IN_CSQRT):
10355 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10356 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
239d491a 10357 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10358 break;
48e1416a 10359
0e7e6e7f 10360 CASE_FLT_FN (BUILT_IN_CASIN):
10361 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10362 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10363 return do_mpc_arg1 (arg0, type, mpc_asin);
10364 break;
48e1416a 10365
0e7e6e7f 10366 CASE_FLT_FN (BUILT_IN_CACOS):
10367 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10368 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10369 return do_mpc_arg1 (arg0, type, mpc_acos);
10370 break;
48e1416a 10371
0e7e6e7f 10372 CASE_FLT_FN (BUILT_IN_CATAN):
10373 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10374 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10375 return do_mpc_arg1 (arg0, type, mpc_atan);
10376 break;
48e1416a 10377
0e7e6e7f 10378 CASE_FLT_FN (BUILT_IN_CASINH):
10379 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10380 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10381 return do_mpc_arg1 (arg0, type, mpc_asinh);
10382 break;
48e1416a 10383
0e7e6e7f 10384 CASE_FLT_FN (BUILT_IN_CACOSH):
10385 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10386 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10387 return do_mpc_arg1 (arg0, type, mpc_acosh);
10388 break;
48e1416a 10389
0e7e6e7f 10390 CASE_FLT_FN (BUILT_IN_CATANH):
10391 if (validate_arg (arg0, COMPLEX_TYPE)
48e1416a 10392 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
0e7e6e7f 10393 return do_mpc_arg1 (arg0, type, mpc_atanh);
10394 break;
48e1416a 10395
4f35b1fc 10396 CASE_FLT_FN (BUILT_IN_CABS):
389dd41b 10397 return fold_builtin_cabs (loc, arg0, type, fndecl);
c63f4ad3 10398
abe4dcf6 10399 CASE_FLT_FN (BUILT_IN_CARG):
389dd41b 10400 return fold_builtin_carg (loc, arg0, type);
abe4dcf6 10401
4f35b1fc 10402 CASE_FLT_FN (BUILT_IN_SQRT):
389dd41b 10403 return fold_builtin_sqrt (loc, arg0, type);
805e22b2 10404
4f35b1fc 10405 CASE_FLT_FN (BUILT_IN_CBRT):
389dd41b 10406 return fold_builtin_cbrt (loc, arg0, type);
3bc5c41b 10407
728bac60 10408 CASE_FLT_FN (BUILT_IN_ASIN):
c2f47e15 10409 if (validate_arg (arg0, REAL_TYPE))
10410 return do_mpfr_arg1 (arg0, type, mpfr_asin,
728bac60 10411 &dconstm1, &dconst1, true);
10412 break;
10413
10414 CASE_FLT_FN (BUILT_IN_ACOS):
c2f47e15 10415 if (validate_arg (arg0, REAL_TYPE))
10416 return do_mpfr_arg1 (arg0, type, mpfr_acos,
728bac60 10417 &dconstm1, &dconst1, true);
10418 break;
10419
10420 CASE_FLT_FN (BUILT_IN_ATAN):
c2f47e15 10421 if (validate_arg (arg0, REAL_TYPE))
10422 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
728bac60 10423 break;
10424
10425 CASE_FLT_FN (BUILT_IN_ASINH):
c2f47e15 10426 if (validate_arg (arg0, REAL_TYPE))
10427 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
728bac60 10428 break;
10429
10430 CASE_FLT_FN (BUILT_IN_ACOSH):
c2f47e15 10431 if (validate_arg (arg0, REAL_TYPE))
10432 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
728bac60 10433 &dconst1, NULL, true);
10434 break;
10435
10436 CASE_FLT_FN (BUILT_IN_ATANH):
c2f47e15 10437 if (validate_arg (arg0, REAL_TYPE))
10438 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
728bac60 10439 &dconstm1, &dconst1, false);
10440 break;
10441
4f35b1fc 10442 CASE_FLT_FN (BUILT_IN_SIN):
c2f47e15 10443 if (validate_arg (arg0, REAL_TYPE))
10444 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
728bac60 10445 break;
77e89269 10446
4f35b1fc 10447 CASE_FLT_FN (BUILT_IN_COS):
389dd41b 10448 return fold_builtin_cos (loc, arg0, type, fndecl);
77e89269 10449
728bac60 10450 CASE_FLT_FN (BUILT_IN_TAN):
c2f47e15 10451 return fold_builtin_tan (arg0, type);
d735c391 10452
c5bb2c4b 10453 CASE_FLT_FN (BUILT_IN_CEXP):
389dd41b 10454 return fold_builtin_cexp (loc, arg0, type);
c5bb2c4b 10455
d735c391 10456 CASE_FLT_FN (BUILT_IN_CEXPI):
c2f47e15 10457 if (validate_arg (arg0, REAL_TYPE))
10458 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10459 break;
d92f994c 10460
728bac60 10461 CASE_FLT_FN (BUILT_IN_SINH):
c2f47e15 10462 if (validate_arg (arg0, REAL_TYPE))
10463 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
728bac60 10464 break;
10465
10466 CASE_FLT_FN (BUILT_IN_COSH):
389dd41b 10467 return fold_builtin_cosh (loc, arg0, type, fndecl);
728bac60 10468
10469 CASE_FLT_FN (BUILT_IN_TANH):
c2f47e15 10470 if (validate_arg (arg0, REAL_TYPE))
10471 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
728bac60 10472 break;
10473
29f4cd78 10474 CASE_FLT_FN (BUILT_IN_ERF):
c2f47e15 10475 if (validate_arg (arg0, REAL_TYPE))
10476 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
29f4cd78 10477 break;
10478
10479 CASE_FLT_FN (BUILT_IN_ERFC):
c2f47e15 10480 if (validate_arg (arg0, REAL_TYPE))
10481 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
29f4cd78 10482 break;
10483
32dba52b 10484 CASE_FLT_FN (BUILT_IN_TGAMMA):
c2f47e15 10485 if (validate_arg (arg0, REAL_TYPE))
10486 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
32dba52b 10487 break;
48e1416a 10488
4f35b1fc 10489 CASE_FLT_FN (BUILT_IN_EXP):
389dd41b 10490 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
467214fd 10491
4f35b1fc 10492 CASE_FLT_FN (BUILT_IN_EXP2):
389dd41b 10493 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
467214fd 10494
4f35b1fc 10495 CASE_FLT_FN (BUILT_IN_EXP10):
10496 CASE_FLT_FN (BUILT_IN_POW10):
389dd41b 10497 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
467214fd 10498
29f4cd78 10499 CASE_FLT_FN (BUILT_IN_EXPM1):
c2f47e15 10500 if (validate_arg (arg0, REAL_TYPE))
10501 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
29f4cd78 10502 break;
48e1416a 10503
4f35b1fc 10504 CASE_FLT_FN (BUILT_IN_LOG):
389dd41b 10505 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
467214fd 10506
4f35b1fc 10507 CASE_FLT_FN (BUILT_IN_LOG2):
389dd41b 10508 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
467214fd 10509
4f35b1fc 10510 CASE_FLT_FN (BUILT_IN_LOG10):
389dd41b 10511 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
29f4cd78 10512
10513 CASE_FLT_FN (BUILT_IN_LOG1P):
c2f47e15 10514 if (validate_arg (arg0, REAL_TYPE))
10515 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
29f4cd78 10516 &dconstm1, NULL, false);
10517 break;
805e22b2 10518
65dd1378 10519 CASE_FLT_FN (BUILT_IN_J0):
10520 if (validate_arg (arg0, REAL_TYPE))
10521 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10522 NULL, NULL, 0);
10523 break;
10524
10525 CASE_FLT_FN (BUILT_IN_J1):
10526 if (validate_arg (arg0, REAL_TYPE))
10527 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10528 NULL, NULL, 0);
10529 break;
6ff9eeff 10530
10531 CASE_FLT_FN (BUILT_IN_Y0):
10532 if (validate_arg (arg0, REAL_TYPE))
10533 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10534 &dconst0, NULL, false);
10535 break;
10536
10537 CASE_FLT_FN (BUILT_IN_Y1):
10538 if (validate_arg (arg0, REAL_TYPE))
10539 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10540 &dconst0, NULL, false);
10541 break;
65dd1378 10542
4f35b1fc 10543 CASE_FLT_FN (BUILT_IN_NAN):
c4503c0a 10544 case BUILT_IN_NAND32:
10545 case BUILT_IN_NAND64:
10546 case BUILT_IN_NAND128:
c2f47e15 10547 return fold_builtin_nan (arg0, type, true);
b0db7939 10548
4f35b1fc 10549 CASE_FLT_FN (BUILT_IN_NANS):
c2f47e15 10550 return fold_builtin_nan (arg0, type, false);
b0db7939 10551
4f35b1fc 10552 CASE_FLT_FN (BUILT_IN_FLOOR):
389dd41b 10553 return fold_builtin_floor (loc, fndecl, arg0);
277f8dd2 10554
4f35b1fc 10555 CASE_FLT_FN (BUILT_IN_CEIL):
389dd41b 10556 return fold_builtin_ceil (loc, fndecl, arg0);
277f8dd2 10557
4f35b1fc 10558 CASE_FLT_FN (BUILT_IN_TRUNC):
389dd41b 10559 return fold_builtin_trunc (loc, fndecl, arg0);
277f8dd2 10560
4f35b1fc 10561 CASE_FLT_FN (BUILT_IN_ROUND):
389dd41b 10562 return fold_builtin_round (loc, fndecl, arg0);
89ab3887 10563
4f35b1fc 10564 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10565 CASE_FLT_FN (BUILT_IN_RINT):
389dd41b 10566 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
6528f4f4 10567
80ff6494 10568 CASE_FLT_FN (BUILT_IN_ICEIL):
4f35b1fc 10569 CASE_FLT_FN (BUILT_IN_LCEIL):
10570 CASE_FLT_FN (BUILT_IN_LLCEIL):
10571 CASE_FLT_FN (BUILT_IN_LFLOOR):
80ff6494 10572 CASE_FLT_FN (BUILT_IN_IFLOOR):
4f35b1fc 10573 CASE_FLT_FN (BUILT_IN_LLFLOOR):
80ff6494 10574 CASE_FLT_FN (BUILT_IN_IROUND):
a0c938f0 10575 CASE_FLT_FN (BUILT_IN_LROUND):
4f35b1fc 10576 CASE_FLT_FN (BUILT_IN_LLROUND):
389dd41b 10577 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
34f17811 10578
80ff6494 10579 CASE_FLT_FN (BUILT_IN_IRINT):
4f35b1fc 10580 CASE_FLT_FN (BUILT_IN_LRINT):
10581 CASE_FLT_FN (BUILT_IN_LLRINT):
389dd41b 10582 return fold_fixed_mathfn (loc, fndecl, arg0);
9ed65c7f 10583
74bdbe96 10584 case BUILT_IN_BSWAP16:
42791117 10585 case BUILT_IN_BSWAP32:
10586 case BUILT_IN_BSWAP64:
c2f47e15 10587 return fold_builtin_bswap (fndecl, arg0);
42791117 10588
4f35b1fc 10589 CASE_INT_FN (BUILT_IN_FFS):
10590 CASE_INT_FN (BUILT_IN_CLZ):
10591 CASE_INT_FN (BUILT_IN_CTZ):
6aaa1f9e 10592 CASE_INT_FN (BUILT_IN_CLRSB):
4f35b1fc 10593 CASE_INT_FN (BUILT_IN_POPCOUNT):
10594 CASE_INT_FN (BUILT_IN_PARITY):
c2f47e15 10595 return fold_builtin_bitop (fndecl, arg0);
9c8a1629 10596
4f35b1fc 10597 CASE_FLT_FN (BUILT_IN_SIGNBIT):
389dd41b 10598 return fold_builtin_signbit (loc, arg0, type);
27f261ef 10599
cb2b9385 10600 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
389dd41b 10601 return fold_builtin_significand (loc, arg0, type);
cb2b9385 10602
10603 CASE_FLT_FN (BUILT_IN_ILOGB):
10604 CASE_FLT_FN (BUILT_IN_LOGB):
389dd41b 10605 return fold_builtin_logb (loc, arg0, type);
cb2b9385 10606
d49367d4 10607 case BUILT_IN_ISASCII:
389dd41b 10608 return fold_builtin_isascii (loc, arg0);
d49367d4 10609
10610 case BUILT_IN_TOASCII:
389dd41b 10611 return fold_builtin_toascii (loc, arg0);
d49367d4 10612
df1cf42e 10613 case BUILT_IN_ISDIGIT:
389dd41b 10614 return fold_builtin_isdigit (loc, arg0);
467214fd 10615
4f35b1fc 10616 CASE_FLT_FN (BUILT_IN_FINITE):
c4503c0a 10617 case BUILT_IN_FINITED32:
10618 case BUILT_IN_FINITED64:
10619 case BUILT_IN_FINITED128:
cde061c1 10620 case BUILT_IN_ISFINITE:
a65c4d64 10621 {
10622 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10623 if (ret)
10624 return ret;
10625 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10626 }
726069ba 10627
4f35b1fc 10628 CASE_FLT_FN (BUILT_IN_ISINF):
c4503c0a 10629 case BUILT_IN_ISINFD32:
10630 case BUILT_IN_ISINFD64:
10631 case BUILT_IN_ISINFD128:
a65c4d64 10632 {
10633 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10634 if (ret)
10635 return ret;
10636 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10637 }
10638
10639 case BUILT_IN_ISNORMAL:
10640 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
726069ba 10641
c319d56a 10642 case BUILT_IN_ISINF_SIGN:
389dd41b 10643 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
c319d56a 10644
4f35b1fc 10645 CASE_FLT_FN (BUILT_IN_ISNAN):
c4503c0a 10646 case BUILT_IN_ISNAND32:
10647 case BUILT_IN_ISNAND64:
10648 case BUILT_IN_ISNAND128:
389dd41b 10649 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
c2f47e15 10650
10651 case BUILT_IN_PRINTF:
10652 case BUILT_IN_PRINTF_UNLOCKED:
10653 case BUILT_IN_VPRINTF:
389dd41b 10654 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
c2f47e15 10655
663870fc 10656 case BUILT_IN_FREE:
10657 if (integer_zerop (arg0))
10658 return build_empty_stmt (loc);
10659 break;
10660
c2f47e15 10661 default:
10662 break;
10663 }
10664
10665 return NULL_TREE;
10666
10667}
10668
10669/* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10670 IGNORE is true if the result of the function call is ignored. This
10671 function returns NULL_TREE if no simplification was possible. */
10672
10673static tree
389dd41b 10674fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
c2f47e15 10675{
10676 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10677 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10678
10679 switch (fcode)
10680 {
65dd1378 10681 CASE_FLT_FN (BUILT_IN_JN):
10682 if (validate_arg (arg0, INTEGER_TYPE)
10683 && validate_arg (arg1, REAL_TYPE))
10684 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10685 break;
6ff9eeff 10686
10687 CASE_FLT_FN (BUILT_IN_YN):
10688 if (validate_arg (arg0, INTEGER_TYPE)
10689 && validate_arg (arg1, REAL_TYPE))
10690 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10691 &dconst0, false);
10692 break;
e5407ca6 10693
10694 CASE_FLT_FN (BUILT_IN_DREM):
10695 CASE_FLT_FN (BUILT_IN_REMAINDER):
10696 if (validate_arg (arg0, REAL_TYPE)
10697 && validate_arg(arg1, REAL_TYPE))
10698 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10699 break;
e84da7c1 10700
10701 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10702 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10703 if (validate_arg (arg0, REAL_TYPE)
10704 && validate_arg(arg1, POINTER_TYPE))
10705 return do_mpfr_lgamma_r (arg0, arg1, type);
10706 break;
c2f47e15 10707
10708 CASE_FLT_FN (BUILT_IN_ATAN2):
10709 if (validate_arg (arg0, REAL_TYPE)
10710 && validate_arg(arg1, REAL_TYPE))
10711 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10712 break;
10713
10714 CASE_FLT_FN (BUILT_IN_FDIM):
10715 if (validate_arg (arg0, REAL_TYPE)
10716 && validate_arg(arg1, REAL_TYPE))
10717 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10718 break;
10719
10720 CASE_FLT_FN (BUILT_IN_HYPOT):
389dd41b 10721 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
c2f47e15 10722
c699fab8 10723 CASE_FLT_FN (BUILT_IN_CPOW):
10724 if (validate_arg (arg0, COMPLEX_TYPE)
10725 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10726 && validate_arg (arg1, COMPLEX_TYPE)
48e1416a 10727 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
652d9409 10728 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
c699fab8 10729 break;
c699fab8 10730
7587301b 10731 CASE_FLT_FN (BUILT_IN_LDEXP):
389dd41b 10732 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
7587301b 10733 CASE_FLT_FN (BUILT_IN_SCALBN):
10734 CASE_FLT_FN (BUILT_IN_SCALBLN):
389dd41b 10735 return fold_builtin_load_exponent (loc, arg0, arg1,
10736 type, /*ldexp=*/false);
7587301b 10737
3838b9ae 10738 CASE_FLT_FN (BUILT_IN_FREXP):
389dd41b 10739 return fold_builtin_frexp (loc, arg0, arg1, type);
3838b9ae 10740
ebf8b4f5 10741 CASE_FLT_FN (BUILT_IN_MODF):
389dd41b 10742 return fold_builtin_modf (loc, arg0, arg1, type);
ebf8b4f5 10743
c2f47e15 10744 case BUILT_IN_BZERO:
389dd41b 10745 return fold_builtin_bzero (loc, arg0, arg1, ignore);
c2f47e15 10746
10747 case BUILT_IN_FPUTS:
389dd41b 10748 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
c2f47e15 10749
10750 case BUILT_IN_FPUTS_UNLOCKED:
389dd41b 10751 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
c2f47e15 10752
10753 case BUILT_IN_STRSTR:
389dd41b 10754 return fold_builtin_strstr (loc, arg0, arg1, type);
c2f47e15 10755
10756 case BUILT_IN_STRCAT:
389dd41b 10757 return fold_builtin_strcat (loc, arg0, arg1);
c2f47e15 10758
10759 case BUILT_IN_STRSPN:
389dd41b 10760 return fold_builtin_strspn (loc, arg0, arg1);
c2f47e15 10761
10762 case BUILT_IN_STRCSPN:
389dd41b 10763 return fold_builtin_strcspn (loc, arg0, arg1);
c2f47e15 10764
10765 case BUILT_IN_STRCHR:
10766 case BUILT_IN_INDEX:
389dd41b 10767 return fold_builtin_strchr (loc, arg0, arg1, type);
c2f47e15 10768
10769 case BUILT_IN_STRRCHR:
10770 case BUILT_IN_RINDEX:
389dd41b 10771 return fold_builtin_strrchr (loc, arg0, arg1, type);
c2f47e15 10772
10773 case BUILT_IN_STRCPY:
389dd41b 10774 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
c2f47e15 10775
c8b17b2e 10776 case BUILT_IN_STPCPY:
10777 if (ignore)
10778 {
b9a16870 10779 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
c8b17b2e 10780 if (!fn)
10781 break;
10782
389dd41b 10783 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
c8b17b2e 10784 }
a65c4d64 10785 else
10786 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
c8b17b2e 10787 break;
10788
c2f47e15 10789 case BUILT_IN_STRCMP:
389dd41b 10790 return fold_builtin_strcmp (loc, arg0, arg1);
c2f47e15 10791
10792 case BUILT_IN_STRPBRK:
389dd41b 10793 return fold_builtin_strpbrk (loc, arg0, arg1, type);
c2f47e15 10794
10795 case BUILT_IN_EXPECT:
389dd41b 10796 return fold_builtin_expect (loc, arg0, arg1);
c2f47e15 10797
10798 CASE_FLT_FN (BUILT_IN_POW):
389dd41b 10799 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
c2f47e15 10800
10801 CASE_FLT_FN (BUILT_IN_POWI):
389dd41b 10802 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
c2f47e15 10803
10804 CASE_FLT_FN (BUILT_IN_COPYSIGN):
389dd41b 10805 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
c2f47e15 10806
10807 CASE_FLT_FN (BUILT_IN_FMIN):
389dd41b 10808 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
c2f47e15 10809
10810 CASE_FLT_FN (BUILT_IN_FMAX):
389dd41b 10811 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
726069ba 10812
9bc9f15f 10813 case BUILT_IN_ISGREATER:
389dd41b 10814 return fold_builtin_unordered_cmp (loc, fndecl,
10815 arg0, arg1, UNLE_EXPR, LE_EXPR);
9bc9f15f 10816 case BUILT_IN_ISGREATEREQUAL:
389dd41b 10817 return fold_builtin_unordered_cmp (loc, fndecl,
10818 arg0, arg1, UNLT_EXPR, LT_EXPR);
9bc9f15f 10819 case BUILT_IN_ISLESS:
389dd41b 10820 return fold_builtin_unordered_cmp (loc, fndecl,
10821 arg0, arg1, UNGE_EXPR, GE_EXPR);
9bc9f15f 10822 case BUILT_IN_ISLESSEQUAL:
389dd41b 10823 return fold_builtin_unordered_cmp (loc, fndecl,
10824 arg0, arg1, UNGT_EXPR, GT_EXPR);
9bc9f15f 10825 case BUILT_IN_ISLESSGREATER:
389dd41b 10826 return fold_builtin_unordered_cmp (loc, fndecl,
10827 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9bc9f15f 10828 case BUILT_IN_ISUNORDERED:
389dd41b 10829 return fold_builtin_unordered_cmp (loc, fndecl,
10830 arg0, arg1, UNORDERED_EXPR,
d5019fe8 10831 NOP_EXPR);
9bc9f15f 10832
7c2f0500 10833 /* We do the folding for va_start in the expander. */
10834 case BUILT_IN_VA_START:
10835 break;
f0613857 10836
c2f47e15 10837 case BUILT_IN_SPRINTF:
389dd41b 10838 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
c2f47e15 10839
0a39fd54 10840 case BUILT_IN_OBJECT_SIZE:
c2f47e15 10841 return fold_builtin_object_size (arg0, arg1);
0a39fd54 10842
10843 case BUILT_IN_PRINTF:
10844 case BUILT_IN_PRINTF_UNLOCKED:
10845 case BUILT_IN_VPRINTF:
389dd41b 10846 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
c2f47e15 10847
0a39fd54 10848 case BUILT_IN_PRINTF_CHK:
10849 case BUILT_IN_VPRINTF_CHK:
c2f47e15 10850 if (!validate_arg (arg0, INTEGER_TYPE)
10851 || TREE_SIDE_EFFECTS (arg0))
10852 return NULL_TREE;
10853 else
389dd41b 10854 return fold_builtin_printf (loc, fndecl,
10855 arg1, NULL_TREE, ignore, fcode);
c2f47e15 10856 break;
0a39fd54 10857
10858 case BUILT_IN_FPRINTF:
10859 case BUILT_IN_FPRINTF_UNLOCKED:
10860 case BUILT_IN_VFPRINTF:
389dd41b 10861 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
c2f47e15 10862 ignore, fcode);
10863
1cd6e20d 10864 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10865 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10866
10867 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10868 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10869
c2f47e15 10870 default:
10871 break;
10872 }
10873 return NULL_TREE;
10874}
10875
10876/* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10877 and ARG2. IGNORE is true if the result of the function call is ignored.
10878 This function returns NULL_TREE if no simplification was possible. */
10879
10880static tree
389dd41b 10881fold_builtin_3 (location_t loc, tree fndecl,
10882 tree arg0, tree arg1, tree arg2, bool ignore)
c2f47e15 10883{
10884 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10885 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10886 switch (fcode)
10887 {
10888
10889 CASE_FLT_FN (BUILT_IN_SINCOS):
389dd41b 10890 return fold_builtin_sincos (loc, arg0, arg1, arg2);
c2f47e15 10891
10892 CASE_FLT_FN (BUILT_IN_FMA):
b9be572e 10893 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
c2f47e15 10894 break;
10895
e5407ca6 10896 CASE_FLT_FN (BUILT_IN_REMQUO):
10897 if (validate_arg (arg0, REAL_TYPE)
10898 && validate_arg(arg1, REAL_TYPE)
10899 && validate_arg(arg2, POINTER_TYPE))
10900 return do_mpfr_remquo (arg0, arg1, arg2);
10901 break;
e5407ca6 10902
c2f47e15 10903 case BUILT_IN_MEMSET:
389dd41b 10904 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
c2f47e15 10905
10906 case BUILT_IN_BCOPY:
389dd41b 10907 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10908 void_type_node, true, /*endp=*/3);
c2f47e15 10909
10910 case BUILT_IN_MEMCPY:
389dd41b 10911 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10912 type, ignore, /*endp=*/0);
c2f47e15 10913
10914 case BUILT_IN_MEMPCPY:
389dd41b 10915 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10916 type, ignore, /*endp=*/1);
c2f47e15 10917
10918 case BUILT_IN_MEMMOVE:
389dd41b 10919 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10920 type, ignore, /*endp=*/3);
c2f47e15 10921
10922 case BUILT_IN_STRNCAT:
389dd41b 10923 return fold_builtin_strncat (loc, arg0, arg1, arg2);
c2f47e15 10924
10925 case BUILT_IN_STRNCPY:
389dd41b 10926 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
c2f47e15 10927
10928 case BUILT_IN_STRNCMP:
389dd41b 10929 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
c2f47e15 10930
7959b13b 10931 case BUILT_IN_MEMCHR:
389dd41b 10932 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
7959b13b 10933
c2f47e15 10934 case BUILT_IN_BCMP:
10935 case BUILT_IN_MEMCMP:
389dd41b 10936 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
c2f47e15 10937
10938 case BUILT_IN_SPRINTF:
389dd41b 10939 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
c2f47e15 10940
750d7b16 10941 case BUILT_IN_SNPRINTF:
10942 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10943
c2f47e15 10944 case BUILT_IN_STRCPY_CHK:
10945 case BUILT_IN_STPCPY_CHK:
389dd41b 10946 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
c2f47e15 10947 ignore, fcode);
10948
10949 case BUILT_IN_STRCAT_CHK:
389dd41b 10950 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
c2f47e15 10951
10952 case BUILT_IN_PRINTF_CHK:
10953 case BUILT_IN_VPRINTF_CHK:
10954 if (!validate_arg (arg0, INTEGER_TYPE)
10955 || TREE_SIDE_EFFECTS (arg0))
10956 return NULL_TREE;
10957 else
389dd41b 10958 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
c2f47e15 10959 break;
10960
10961 case BUILT_IN_FPRINTF:
10962 case BUILT_IN_FPRINTF_UNLOCKED:
10963 case BUILT_IN_VFPRINTF:
389dd41b 10964 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10965 ignore, fcode);
c2f47e15 10966
0a39fd54 10967 case BUILT_IN_FPRINTF_CHK:
10968 case BUILT_IN_VFPRINTF_CHK:
c2f47e15 10969 if (!validate_arg (arg1, INTEGER_TYPE)
10970 || TREE_SIDE_EFFECTS (arg1))
10971 return NULL_TREE;
10972 else
389dd41b 10973 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
c2f47e15 10974 ignore, fcode);
0a39fd54 10975
650e4c94 10976 default:
10977 break;
10978 }
c2f47e15 10979 return NULL_TREE;
10980}
650e4c94 10981
c2f47e15 10982/* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10983 ARG2, and ARG3. IGNORE is true if the result of the function call is
10984 ignored. This function returns NULL_TREE if no simplification was
10985 possible. */
48e1416a 10986
c2f47e15 10987static tree
389dd41b 10988fold_builtin_4 (location_t loc, tree fndecl,
10989 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
c2f47e15 10990{
10991 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10992
10993 switch (fcode)
10994 {
10995 case BUILT_IN_MEMCPY_CHK:
10996 case BUILT_IN_MEMPCPY_CHK:
10997 case BUILT_IN_MEMMOVE_CHK:
10998 case BUILT_IN_MEMSET_CHK:
389dd41b 10999 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
c2f47e15 11000 NULL_TREE, ignore,
11001 DECL_FUNCTION_CODE (fndecl));
11002
11003 case BUILT_IN_STRNCPY_CHK:
1063acde 11004 case BUILT_IN_STPNCPY_CHK:
11005 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11006 ignore, fcode);
c2f47e15 11007
11008 case BUILT_IN_STRNCAT_CHK:
389dd41b 11009 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
c2f47e15 11010
750d7b16 11011 case BUILT_IN_SNPRINTF:
11012 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11013
c2f47e15 11014 case BUILT_IN_FPRINTF_CHK:
11015 case BUILT_IN_VFPRINTF_CHK:
11016 if (!validate_arg (arg1, INTEGER_TYPE)
11017 || TREE_SIDE_EFFECTS (arg1))
11018 return NULL_TREE;
11019 else
389dd41b 11020 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
c2f47e15 11021 ignore, fcode);
11022 break;
11023
11024 default:
11025 break;
11026 }
11027 return NULL_TREE;
11028}
11029
11030/* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11031 arguments, where NARGS <= 4. IGNORE is true if the result of the
11032 function call is ignored. This function returns NULL_TREE if no
11033 simplification was possible. Note that this only folds builtins with
11034 fixed argument patterns. Foldings that do varargs-to-varargs
11035 transformations, or that match calls with more than 4 arguments,
11036 need to be handled with fold_builtin_varargs instead. */
48e1416a 11037
c2f47e15 11038#define MAX_ARGS_TO_FOLD_BUILTIN 4
48e1416a 11039
c2f47e15 11040static tree
389dd41b 11041fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
c2f47e15 11042{
11043 tree ret = NULL_TREE;
a7f5bb2d 11044
c2f47e15 11045 switch (nargs)
11046 {
11047 case 0:
389dd41b 11048 ret = fold_builtin_0 (loc, fndecl, ignore);
c2f47e15 11049 break;
11050 case 1:
389dd41b 11051 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
c2f47e15 11052 break;
11053 case 2:
389dd41b 11054 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
c2f47e15 11055 break;
11056 case 3:
389dd41b 11057 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
c2f47e15 11058 break;
11059 case 4:
389dd41b 11060 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
c2f47e15 11061 ignore);
11062 break;
11063 default:
11064 break;
11065 }
11066 if (ret)
11067 {
75a70cf9 11068 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
389dd41b 11069 SET_EXPR_LOCATION (ret, loc);
c2f47e15 11070 TREE_NO_WARNING (ret) = 1;
11071 return ret;
11072 }
11073 return NULL_TREE;
11074}
11075
11076/* Builtins with folding operations that operate on "..." arguments
11077 need special handling; we need to store the arguments in a convenient
11078 data structure before attempting any folding. Fortunately there are
11079 only a few builtins that fall into this category. FNDECL is the
11080 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11081 result of the function call is ignored. */
11082
11083static tree
389dd41b 11084fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11085 bool ignore ATTRIBUTE_UNUSED)
c2f47e15 11086{
11087 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11088 tree ret = NULL_TREE;
11089
11090 switch (fcode)
11091 {
11092 case BUILT_IN_SPRINTF_CHK:
11093 case BUILT_IN_VSPRINTF_CHK:
389dd41b 11094 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
c2f47e15 11095 break;
11096
11097 case BUILT_IN_SNPRINTF_CHK:
11098 case BUILT_IN_VSNPRINTF_CHK:
389dd41b 11099 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
19fbe3a4 11100 break;
11101
11102 case BUILT_IN_FPCLASSIFY:
389dd41b 11103 ret = fold_builtin_fpclassify (loc, exp);
19fbe3a4 11104 break;
c2f47e15 11105
11106 default:
11107 break;
11108 }
11109 if (ret)
11110 {
11111 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
389dd41b 11112 SET_EXPR_LOCATION (ret, loc);
c2f47e15 11113 TREE_NO_WARNING (ret) = 1;
11114 return ret;
11115 }
11116 return NULL_TREE;
650e4c94 11117}
7e15618b 11118
198622c0 11119/* Return true if FNDECL shouldn't be folded right now.
11120 If a built-in function has an inline attribute always_inline
11121 wrapper, defer folding it after always_inline functions have
11122 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11123 might not be performed. */
11124
51d2c51e 11125bool
198622c0 11126avoid_folding_inline_builtin (tree fndecl)
11127{
11128 return (DECL_DECLARED_INLINE_P (fndecl)
11129 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11130 && cfun
11131 && !cfun->always_inline_functions_inlined
11132 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11133}
11134
4ee9c684 11135/* A wrapper function for builtin folding that prevents warnings for
491e04ef 11136 "statement without effect" and the like, caused by removing the
4ee9c684 11137 call node earlier than the warning is generated. */
11138
11139tree
389dd41b 11140fold_call_expr (location_t loc, tree exp, bool ignore)
4ee9c684 11141{
c2f47e15 11142 tree ret = NULL_TREE;
11143 tree fndecl = get_callee_fndecl (exp);
11144 if (fndecl
11145 && TREE_CODE (fndecl) == FUNCTION_DECL
48dc2227 11146 && DECL_BUILT_IN (fndecl)
11147 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11148 yet. Defer folding until we see all the arguments
11149 (after inlining). */
11150 && !CALL_EXPR_VA_ARG_PACK (exp))
11151 {
11152 int nargs = call_expr_nargs (exp);
11153
11154 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11155 instead last argument is __builtin_va_arg_pack (). Defer folding
11156 even in that case, until arguments are finalized. */
11157 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11158 {
11159 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11160 if (fndecl2
11161 && TREE_CODE (fndecl2) == FUNCTION_DECL
11162 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11163 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11164 return NULL_TREE;
11165 }
11166
198622c0 11167 if (avoid_folding_inline_builtin (fndecl))
11168 return NULL_TREE;
11169
c2f47e15 11170 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
97d67146 11171 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11172 CALL_EXPR_ARGP (exp), ignore);
c2f47e15 11173 else
11174 {
c2f47e15 11175 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11176 {
11177 tree *args = CALL_EXPR_ARGP (exp);
389dd41b 11178 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
c2f47e15 11179 }
11180 if (!ret)
389dd41b 11181 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
c2f47e15 11182 if (ret)
389dd41b 11183 return ret;
c2f47e15 11184 }
4ee9c684 11185 }
c2f47e15 11186 return NULL_TREE;
11187}
48e1416a 11188
c2f47e15 11189/* Conveniently construct a function call expression. FNDECL names the
414c3a2c 11190 function to be called and N arguments are passed in the array
11191 ARGARRAY. */
48e1416a 11192
c2f47e15 11193tree
414c3a2c 11194build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
c2f47e15 11195{
11196 tree fntype = TREE_TYPE (fndecl);
11197 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
414c3a2c 11198
389dd41b 11199 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
c2f47e15 11200}
11201
414c3a2c 11202/* Conveniently construct a function call expression. FNDECL names the
11203 function to be called and the arguments are passed in the vector
11204 VEC. */
11205
11206tree
11207build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11208{
11209 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11210 VEC_address (tree, vec));
11211}
11212
11213
c2f47e15 11214/* Conveniently construct a function call expression. FNDECL names the
11215 function to be called, N is the number of arguments, and the "..."
11216 parameters are the argument expressions. */
48e1416a 11217
c2f47e15 11218tree
389dd41b 11219build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
c2f47e15 11220{
11221 va_list ap;
414c3a2c 11222 tree *argarray = XALLOCAVEC (tree, n);
d01f58f9 11223 int i;
c2f47e15 11224
11225 va_start (ap, n);
d01f58f9 11226 for (i = 0; i < n; i++)
11227 argarray[i] = va_arg (ap, tree);
c2f47e15 11228 va_end (ap);
414c3a2c 11229 return build_call_expr_loc_array (loc, fndecl, n, argarray);
4ee9c684 11230}
11231
0568e9c1 11232/* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11233 varargs macros aren't supported by all bootstrap compilers. */
11234
11235tree
11236build_call_expr (tree fndecl, int n, ...)
11237{
11238 va_list ap;
414c3a2c 11239 tree *argarray = XALLOCAVEC (tree, n);
0568e9c1 11240 int i;
11241
11242 va_start (ap, n);
11243 for (i = 0; i < n; i++)
11244 argarray[i] = va_arg (ap, tree);
11245 va_end (ap);
414c3a2c 11246 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
0568e9c1 11247}
11248
c2f47e15 11249/* Construct a CALL_EXPR with type TYPE with FN as the function expression.
d01f58f9 11250 N arguments are passed in the array ARGARRAY. */
805e22b2 11251
11252tree
389dd41b 11253fold_builtin_call_array (location_t loc, tree type,
d01f58f9 11254 tree fn,
11255 int n,
11256 tree *argarray)
7e15618b 11257{
c2f47e15 11258 tree ret = NULL_TREE;
c2f47e15 11259 tree exp;
11260
11261 if (TREE_CODE (fn) == ADDR_EXPR)
11262 {
11263 tree fndecl = TREE_OPERAND (fn, 0);
11264 if (TREE_CODE (fndecl) == FUNCTION_DECL
11265 && DECL_BUILT_IN (fndecl))
11266 {
48dc2227 11267 /* If last argument is __builtin_va_arg_pack (), arguments to this
11268 function are not finalized yet. Defer folding until they are. */
11269 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11270 {
11271 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11272 if (fndecl2
11273 && TREE_CODE (fndecl2) == FUNCTION_DECL
11274 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11275 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
389dd41b 11276 return build_call_array_loc (loc, type, fn, n, argarray);
48dc2227 11277 }
198622c0 11278 if (avoid_folding_inline_builtin (fndecl))
389dd41b 11279 return build_call_array_loc (loc, type, fn, n, argarray);
c2f47e15 11280 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11281 {
97d67146 11282 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11283 if (ret)
11284 return ret;
11285
389dd41b 11286 return build_call_array_loc (loc, type, fn, n, argarray);
c2f47e15 11287 }
11288 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11289 {
11290 /* First try the transformations that don't require consing up
11291 an exp. */
389dd41b 11292 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
c2f47e15 11293 if (ret)
11294 return ret;
11295 }
11296
11297 /* If we got this far, we need to build an exp. */
389dd41b 11298 exp = build_call_array_loc (loc, type, fn, n, argarray);
11299 ret = fold_builtin_varargs (loc, fndecl, exp, false);
c2f47e15 11300 return ret ? ret : exp;
11301 }
11302 }
11303
389dd41b 11304 return build_call_array_loc (loc, type, fn, n, argarray);
c2f47e15 11305}
11306
af1409ad 11307/* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11308 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11309 of arguments in ARGS to be omitted. OLDNARGS is the number of
11310 elements in ARGS. */
c2f47e15 11311
11312static tree
af1409ad 11313rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11314 int skip, tree fndecl, int n, va_list newargs)
c2f47e15 11315{
c2f47e15 11316 int nargs = oldnargs - skip + n;
c2f47e15 11317 tree *buffer;
11318
11319 if (n > 0)
11320 {
11321 int i, j;
7e15618b 11322
364c0c59 11323 buffer = XALLOCAVEC (tree, nargs);
c2f47e15 11324 for (i = 0; i < n; i++)
af1409ad 11325 buffer[i] = va_arg (newargs, tree);
c2f47e15 11326 for (j = skip; j < oldnargs; j++, i++)
af1409ad 11327 buffer[i] = args[j];
c2f47e15 11328 }
48e1416a 11329 else
af1409ad 11330 buffer = args + skip;
11331
11332 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11333}
11334
11335/* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11336 list ARGS along with N new arguments specified as the "..."
11337 parameters. SKIP is the number of arguments in ARGS to be omitted.
11338 OLDNARGS is the number of elements in ARGS. */
11339
11340static tree
11341rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11342 int skip, tree fndecl, int n, ...)
11343{
11344 va_list ap;
11345 tree t;
11346
11347 va_start (ap, n);
11348 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11349 va_end (ap);
11350
11351 return t;
11352}
11353
11354/* Construct a new CALL_EXPR using the tail of the argument list of EXP
11355 along with N new arguments specified as the "..." parameters. SKIP
11356 is the number of arguments in EXP to be omitted. This function is used
11357 to do varargs-to-varargs transformations. */
11358
11359static tree
11360rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11361{
11362 va_list ap;
11363 tree t;
11364
11365 va_start (ap, n);
11366 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11367 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11368 va_end (ap);
c2f47e15 11369
af1409ad 11370 return t;
c2f47e15 11371}
11372
11373/* Validate a single argument ARG against a tree code CODE representing
11374 a type. */
48e1416a 11375
c2f47e15 11376static bool
b7bf20db 11377validate_arg (const_tree arg, enum tree_code code)
c2f47e15 11378{
11379 if (!arg)
11380 return false;
11381 else if (code == POINTER_TYPE)
11382 return POINTER_TYPE_P (TREE_TYPE (arg));
c7f617c2 11383 else if (code == INTEGER_TYPE)
11384 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
c2f47e15 11385 return code == TREE_CODE (TREE_TYPE (arg));
7e15618b 11386}
0eb671f7 11387
75a70cf9 11388/* This function validates the types of a function call argument list
11389 against a specified list of tree_codes. If the last specifier is a 0,
11390 that represents an ellipses, otherwise the last specifier must be a
11391 VOID_TYPE.
11392
11393 This is the GIMPLE version of validate_arglist. Eventually we want to
11394 completely convert builtins.c to work from GIMPLEs and the tree based
11395 validate_arglist will then be removed. */
11396
11397bool
11398validate_gimple_arglist (const_gimple call, ...)
11399{
11400 enum tree_code code;
11401 bool res = 0;
11402 va_list ap;
11403 const_tree arg;
11404 size_t i;
11405
11406 va_start (ap, call);
11407 i = 0;
11408
11409 do
11410 {
d62e827b 11411 code = (enum tree_code) va_arg (ap, int);
75a70cf9 11412 switch (code)
11413 {
11414 case 0:
11415 /* This signifies an ellipses, any further arguments are all ok. */
11416 res = true;
11417 goto end;
11418 case VOID_TYPE:
11419 /* This signifies an endlink, if no arguments remain, return
11420 true, otherwise return false. */
11421 res = (i == gimple_call_num_args (call));
11422 goto end;
11423 default:
11424 /* If no parameters remain or the parameter's code does not
11425 match the specified code, return false. Otherwise continue
11426 checking any remaining arguments. */
11427 arg = gimple_call_arg (call, i++);
11428 if (!validate_arg (arg, code))
11429 goto end;
11430 break;
11431 }
11432 }
11433 while (1);
11434
11435 /* We need gotos here since we can only have one VA_CLOSE in a
11436 function. */
11437 end: ;
11438 va_end (ap);
11439
11440 return res;
11441}
11442
0eb671f7 11443/* This function validates the types of a function call argument list
c2f47e15 11444 against a specified list of tree_codes. If the last specifier is a 0,
11445 that represents an ellipses, otherwise the last specifier must be a
11446 VOID_TYPE. */
27d0c333 11447
c2f47e15 11448bool
b7bf20db 11449validate_arglist (const_tree callexpr, ...)
0eb671f7 11450{
0eb671f7 11451 enum tree_code code;
c2f47e15 11452 bool res = 0;
ee582a61 11453 va_list ap;
b7bf20db 11454 const_call_expr_arg_iterator iter;
11455 const_tree arg;
aecda0d6 11456
c2f47e15 11457 va_start (ap, callexpr);
b7bf20db 11458 init_const_call_expr_arg_iterator (callexpr, &iter);
0eb671f7 11459
f0ce3b1f 11460 do
0eb671f7 11461 {
d62e827b 11462 code = (enum tree_code) va_arg (ap, int);
f0ce3b1f 11463 switch (code)
11464 {
11465 case 0:
11466 /* This signifies an ellipses, any further arguments are all ok. */
c2f47e15 11467 res = true;
f0ce3b1f 11468 goto end;
11469 case VOID_TYPE:
11470 /* This signifies an endlink, if no arguments remain, return
11471 true, otherwise return false. */
b7bf20db 11472 res = !more_const_call_expr_args_p (&iter);
f0ce3b1f 11473 goto end;
11474 default:
11475 /* If no parameters remain or the parameter's code does not
11476 match the specified code, return false. Otherwise continue
11477 checking any remaining arguments. */
b7bf20db 11478 arg = next_const_call_expr_arg (&iter);
c2f47e15 11479 if (!validate_arg (arg, code))
f0ce3b1f 11480 goto end;
11481 break;
11482 }
0eb671f7 11483 }
f0ce3b1f 11484 while (1);
0903457a 11485
11486 /* We need gotos here since we can only have one VA_CLOSE in a
11487 function. */
11488 end: ;
ee582a61 11489 va_end (ap);
0903457a 11490
11491 return res;
0eb671f7 11492}
fc2a2dcb 11493
fc2a2dcb 11494/* Default target-specific builtin expander that does nothing. */
11495
11496rtx
aecda0d6 11497default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11498 rtx target ATTRIBUTE_UNUSED,
11499 rtx subtarget ATTRIBUTE_UNUSED,
11500 enum machine_mode mode ATTRIBUTE_UNUSED,
11501 int ignore ATTRIBUTE_UNUSED)
fc2a2dcb 11502{
11503 return NULL_RTX;
11504}
c7926a82 11505
01537105 11506/* Returns true is EXP represents data that would potentially reside
11507 in a readonly section. */
11508
11509static bool
11510readonly_data_expr (tree exp)
11511{
11512 STRIP_NOPS (exp);
11513
9ff0637e 11514 if (TREE_CODE (exp) != ADDR_EXPR)
11515 return false;
11516
11517 exp = get_base_address (TREE_OPERAND (exp, 0));
11518 if (!exp)
11519 return false;
11520
11521 /* Make sure we call decl_readonly_section only for trees it
11522 can handle (since it returns true for everything it doesn't
11523 understand). */
491e04ef 11524 if (TREE_CODE (exp) == STRING_CST
9ff0637e 11525 || TREE_CODE (exp) == CONSTRUCTOR
11526 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11527 return decl_readonly_section (exp, 0);
01537105 11528 else
11529 return false;
11530}
4ee9c684 11531
c2f47e15 11532/* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11533 to the call, and TYPE is its return type.
4ee9c684 11534
c2f47e15 11535 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11536 simplified form of the call as a tree.
11537
11538 The simplified form may be a constant or other expression which
11539 computes the same value, but in a more efficient manner (including
11540 calls to other builtin functions).
11541
11542 The call may contain arguments which need to be evaluated, but
11543 which are not useful to determine the result of the call. In
11544 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11545 COMPOUND_EXPR will be an argument which must be evaluated.
11546 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11547 COMPOUND_EXPR in the chain will contain the tree for the simplified
11548 form of the builtin function call. */
11549
11550static tree
389dd41b 11551fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
4ee9c684 11552{
c2f47e15 11553 if (!validate_arg (s1, POINTER_TYPE)
11554 || !validate_arg (s2, POINTER_TYPE))
11555 return NULL_TREE;
4ee9c684 11556 else
11557 {
4ee9c684 11558 tree fn;
11559 const char *p1, *p2;
11560
11561 p2 = c_getstr (s2);
11562 if (p2 == NULL)
c2f47e15 11563 return NULL_TREE;
4ee9c684 11564
11565 p1 = c_getstr (s1);
11566 if (p1 != NULL)
11567 {
11568 const char *r = strstr (p1, p2);
daa1d5f5 11569 tree tem;
4ee9c684 11570
4ee9c684 11571 if (r == NULL)
779b4c41 11572 return build_int_cst (TREE_TYPE (s1), 0);
c0c67e38 11573
11574 /* Return an offset into the constant string argument. */
2cc66f2a 11575 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
389dd41b 11576 return fold_convert_loc (loc, type, tem);
4ee9c684 11577 }
11578
7efa231c 11579 /* The argument is const char *, and the result is char *, so we need
11580 a type conversion here to avoid a warning. */
4ee9c684 11581 if (p2[0] == '\0')
389dd41b 11582 return fold_convert_loc (loc, type, s1);
4ee9c684 11583
11584 if (p2[1] != '\0')
c2f47e15 11585 return NULL_TREE;
4ee9c684 11586
b9a16870 11587 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
4ee9c684 11588 if (!fn)
c2f47e15 11589 return NULL_TREE;
4ee9c684 11590
11591 /* New argument list transforming strstr(s1, s2) to
11592 strchr(s1, s2[0]). */
7002a1c8 11593 return build_call_expr_loc (loc, fn, 2, s1,
11594 build_int_cst (integer_type_node, p2[0]));
4ee9c684 11595 }
11596}
11597
c2f47e15 11598/* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11599 the call, and TYPE is its return type.
4ee9c684 11600
c2f47e15 11601 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11602 simplified form of the call as a tree.
11603
11604 The simplified form may be a constant or other expression which
11605 computes the same value, but in a more efficient manner (including
11606 calls to other builtin functions).
11607
11608 The call may contain arguments which need to be evaluated, but
11609 which are not useful to determine the result of the call. In
11610 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11611 COMPOUND_EXPR will be an argument which must be evaluated.
11612 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11613 COMPOUND_EXPR in the chain will contain the tree for the simplified
11614 form of the builtin function call. */
11615
11616static tree
389dd41b 11617fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
4ee9c684 11618{
c2f47e15 11619 if (!validate_arg (s1, POINTER_TYPE)
11620 || !validate_arg (s2, INTEGER_TYPE))
11621 return NULL_TREE;
4ee9c684 11622 else
11623 {
4ee9c684 11624 const char *p1;
11625
11626 if (TREE_CODE (s2) != INTEGER_CST)
c2f47e15 11627 return NULL_TREE;
4ee9c684 11628
11629 p1 = c_getstr (s1);
11630 if (p1 != NULL)
11631 {
11632 char c;
11633 const char *r;
daa1d5f5 11634 tree tem;
4ee9c684 11635
11636 if (target_char_cast (s2, &c))
c2f47e15 11637 return NULL_TREE;
4ee9c684 11638
11639 r = strchr (p1, c);
11640
11641 if (r == NULL)
779b4c41 11642 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 11643
11644 /* Return an offset into the constant string argument. */
2cc66f2a 11645 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
389dd41b 11646 return fold_convert_loc (loc, type, tem);
4ee9c684 11647 }
c2f47e15 11648 return NULL_TREE;
4ee9c684 11649 }
11650}
11651
c2f47e15 11652/* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11653 the call, and TYPE is its return type.
4ee9c684 11654
c2f47e15 11655 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11656 simplified form of the call as a tree.
11657
11658 The simplified form may be a constant or other expression which
11659 computes the same value, but in a more efficient manner (including
11660 calls to other builtin functions).
11661
11662 The call may contain arguments which need to be evaluated, but
11663 which are not useful to determine the result of the call. In
11664 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11665 COMPOUND_EXPR will be an argument which must be evaluated.
11666 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11667 COMPOUND_EXPR in the chain will contain the tree for the simplified
11668 form of the builtin function call. */
11669
11670static tree
389dd41b 11671fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
4ee9c684 11672{
c2f47e15 11673 if (!validate_arg (s1, POINTER_TYPE)
11674 || !validate_arg (s2, INTEGER_TYPE))
11675 return NULL_TREE;
4ee9c684 11676 else
11677 {
4ee9c684 11678 tree fn;
11679 const char *p1;
11680
11681 if (TREE_CODE (s2) != INTEGER_CST)
c2f47e15 11682 return NULL_TREE;
4ee9c684 11683
11684 p1 = c_getstr (s1);
11685 if (p1 != NULL)
11686 {
11687 char c;
11688 const char *r;
daa1d5f5 11689 tree tem;
4ee9c684 11690
11691 if (target_char_cast (s2, &c))
c2f47e15 11692 return NULL_TREE;
4ee9c684 11693
11694 r = strrchr (p1, c);
11695
11696 if (r == NULL)
779b4c41 11697 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 11698
11699 /* Return an offset into the constant string argument. */
2cc66f2a 11700 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
389dd41b 11701 return fold_convert_loc (loc, type, tem);
4ee9c684 11702 }
11703
11704 if (! integer_zerop (s2))
c2f47e15 11705 return NULL_TREE;
4ee9c684 11706
b9a16870 11707 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
4ee9c684 11708 if (!fn)
c2f47e15 11709 return NULL_TREE;
4ee9c684 11710
11711 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
389dd41b 11712 return build_call_expr_loc (loc, fn, 2, s1, s2);
4ee9c684 11713 }
11714}
11715
c2f47e15 11716/* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11717 to the call, and TYPE is its return type.
4ee9c684 11718
c2f47e15 11719 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11720 simplified form of the call as a tree.
11721
11722 The simplified form may be a constant or other expression which
11723 computes the same value, but in a more efficient manner (including
11724 calls to other builtin functions).
11725
11726 The call may contain arguments which need to be evaluated, but
11727 which are not useful to determine the result of the call. In
11728 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11729 COMPOUND_EXPR will be an argument which must be evaluated.
11730 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11731 COMPOUND_EXPR in the chain will contain the tree for the simplified
11732 form of the builtin function call. */
11733
11734static tree
389dd41b 11735fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
4ee9c684 11736{
c2f47e15 11737 if (!validate_arg (s1, POINTER_TYPE)
11738 || !validate_arg (s2, POINTER_TYPE))
11739 return NULL_TREE;
4ee9c684 11740 else
11741 {
4ee9c684 11742 tree fn;
11743 const char *p1, *p2;
11744
11745 p2 = c_getstr (s2);
11746 if (p2 == NULL)
c2f47e15 11747 return NULL_TREE;
4ee9c684 11748
11749 p1 = c_getstr (s1);
11750 if (p1 != NULL)
11751 {
11752 const char *r = strpbrk (p1, p2);
daa1d5f5 11753 tree tem;
4ee9c684 11754
11755 if (r == NULL)
779b4c41 11756 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 11757
11758 /* Return an offset into the constant string argument. */
2cc66f2a 11759 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
389dd41b 11760 return fold_convert_loc (loc, type, tem);
4ee9c684 11761 }
11762
11763 if (p2[0] == '\0')
05abc81b 11764 /* strpbrk(x, "") == NULL.
11765 Evaluate and ignore s1 in case it had side-effects. */
389dd41b 11766 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
4ee9c684 11767
11768 if (p2[1] != '\0')
c2f47e15 11769 return NULL_TREE; /* Really call strpbrk. */
4ee9c684 11770
b9a16870 11771 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
4ee9c684 11772 if (!fn)
c2f47e15 11773 return NULL_TREE;
4ee9c684 11774
11775 /* New argument list transforming strpbrk(s1, s2) to
11776 strchr(s1, s2[0]). */
7002a1c8 11777 return build_call_expr_loc (loc, fn, 2, s1,
11778 build_int_cst (integer_type_node, p2[0]));
4ee9c684 11779 }
11780}
11781
c2f47e15 11782/* Simplify a call to the strcat builtin. DST and SRC are the arguments
11783 to the call.
4ee9c684 11784
c2f47e15 11785 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11786 simplified form of the call as a tree.
11787
11788 The simplified form may be a constant or other expression which
11789 computes the same value, but in a more efficient manner (including
11790 calls to other builtin functions).
11791
11792 The call may contain arguments which need to be evaluated, but
11793 which are not useful to determine the result of the call. In
11794 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11795 COMPOUND_EXPR will be an argument which must be evaluated.
11796 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11797 COMPOUND_EXPR in the chain will contain the tree for the simplified
11798 form of the builtin function call. */
11799
11800static tree
389dd41b 11801fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
4ee9c684 11802{
c2f47e15 11803 if (!validate_arg (dst, POINTER_TYPE)
11804 || !validate_arg (src, POINTER_TYPE))
11805 return NULL_TREE;
4ee9c684 11806 else
11807 {
4ee9c684 11808 const char *p = c_getstr (src);
11809
11810 /* If the string length is zero, return the dst parameter. */
11811 if (p && *p == '\0')
11812 return dst;
11813
a65c4d64 11814 if (optimize_insn_for_speed_p ())
11815 {
11816 /* See if we can store by pieces into (dst + strlen(dst)). */
11817 tree newdst, call;
b9a16870 11818 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11819 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
a65c4d64 11820
11821 if (!strlen_fn || !strcpy_fn)
11822 return NULL_TREE;
11823
11824 /* If we don't have a movstr we don't want to emit an strcpy
11825 call. We have to do that if the length of the source string
11826 isn't computable (in that case we can use memcpy probably
48e1416a 11827 later expanding to a sequence of mov instructions). If we
a65c4d64 11828 have movstr instructions we can emit strcpy calls. */
11829 if (!HAVE_movstr)
11830 {
11831 tree len = c_strlen (src, 1);
11832 if (! len || TREE_SIDE_EFFECTS (len))
11833 return NULL_TREE;
11834 }
11835
11836 /* Stabilize the argument list. */
11837 dst = builtin_save_expr (dst);
11838
11839 /* Create strlen (dst). */
11840 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11841 /* Create (dst p+ strlen (dst)). */
11842
2cc66f2a 11843 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
a65c4d64 11844 newdst = builtin_save_expr (newdst);
11845
11846 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11847 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11848 }
c2f47e15 11849 return NULL_TREE;
4ee9c684 11850 }
11851}
11852
c2f47e15 11853/* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11854 arguments to the call.
4ee9c684 11855
c2f47e15 11856 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11857 simplified form of the call as a tree.
11858
11859 The simplified form may be a constant or other expression which
11860 computes the same value, but in a more efficient manner (including
11861 calls to other builtin functions).
11862
11863 The call may contain arguments which need to be evaluated, but
11864 which are not useful to determine the result of the call. In
11865 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11866 COMPOUND_EXPR will be an argument which must be evaluated.
11867 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11868 COMPOUND_EXPR in the chain will contain the tree for the simplified
11869 form of the builtin function call. */
11870
11871static tree
389dd41b 11872fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
4ee9c684 11873{
c2f47e15 11874 if (!validate_arg (dst, POINTER_TYPE)
11875 || !validate_arg (src, POINTER_TYPE)
11876 || !validate_arg (len, INTEGER_TYPE))
11877 return NULL_TREE;
4ee9c684 11878 else
11879 {
4ee9c684 11880 const char *p = c_getstr (src);
11881
11882 /* If the requested length is zero, or the src parameter string
0a39fd54 11883 length is zero, return the dst parameter. */
4ee9c684 11884 if (integer_zerop (len) || (p && *p == '\0'))
389dd41b 11885 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
4ee9c684 11886
11887 /* If the requested len is greater than or equal to the string
a0c938f0 11888 length, call strcat. */
4ee9c684 11889 if (TREE_CODE (len) == INTEGER_CST && p
11890 && compare_tree_int (len, strlen (p)) >= 0)
11891 {
b9a16870 11892 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
4ee9c684 11893
11894 /* If the replacement _DECL isn't initialized, don't do the
11895 transformation. */
11896 if (!fn)
c2f47e15 11897 return NULL_TREE;
4ee9c684 11898
389dd41b 11899 return build_call_expr_loc (loc, fn, 2, dst, src);
4ee9c684 11900 }
c2f47e15 11901 return NULL_TREE;
4ee9c684 11902 }
11903}
11904
c2f47e15 11905/* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11906 to the call.
4ee9c684 11907
c2f47e15 11908 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11909 simplified form of the call as a tree.
11910
11911 The simplified form may be a constant or other expression which
11912 computes the same value, but in a more efficient manner (including
11913 calls to other builtin functions).
11914
11915 The call may contain arguments which need to be evaluated, but
11916 which are not useful to determine the result of the call. In
11917 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11918 COMPOUND_EXPR will be an argument which must be evaluated.
11919 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11920 COMPOUND_EXPR in the chain will contain the tree for the simplified
11921 form of the builtin function call. */
11922
11923static tree
389dd41b 11924fold_builtin_strspn (location_t loc, tree s1, tree s2)
4ee9c684 11925{
c2f47e15 11926 if (!validate_arg (s1, POINTER_TYPE)
11927 || !validate_arg (s2, POINTER_TYPE))
11928 return NULL_TREE;
4ee9c684 11929 else
11930 {
4ee9c684 11931 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11932
11933 /* If both arguments are constants, evaluate at compile-time. */
11934 if (p1 && p2)
11935 {
11936 const size_t r = strspn (p1, p2);
11937 return size_int (r);
11938 }
11939
c2f47e15 11940 /* If either argument is "", return NULL_TREE. */
4ee9c684 11941 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9bc9f15f 11942 /* Evaluate and ignore both arguments in case either one has
11943 side-effects. */
389dd41b 11944 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9bc9f15f 11945 s1, s2);
c2f47e15 11946 return NULL_TREE;
4ee9c684 11947 }
11948}
11949
c2f47e15 11950/* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11951 to the call.
4ee9c684 11952
c2f47e15 11953 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 11954 simplified form of the call as a tree.
11955
11956 The simplified form may be a constant or other expression which
11957 computes the same value, but in a more efficient manner (including
11958 calls to other builtin functions).
11959
11960 The call may contain arguments which need to be evaluated, but
11961 which are not useful to determine the result of the call. In
11962 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11963 COMPOUND_EXPR will be an argument which must be evaluated.
11964 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11965 COMPOUND_EXPR in the chain will contain the tree for the simplified
11966 form of the builtin function call. */
11967
11968static tree
389dd41b 11969fold_builtin_strcspn (location_t loc, tree s1, tree s2)
4ee9c684 11970{
c2f47e15 11971 if (!validate_arg (s1, POINTER_TYPE)
11972 || !validate_arg (s2, POINTER_TYPE))
11973 return NULL_TREE;
4ee9c684 11974 else
11975 {
4ee9c684 11976 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11977
11978 /* If both arguments are constants, evaluate at compile-time. */
11979 if (p1 && p2)
11980 {
11981 const size_t r = strcspn (p1, p2);
11982 return size_int (r);
11983 }
11984
c2f47e15 11985 /* If the first argument is "", return NULL_TREE. */
4ee9c684 11986 if (p1 && *p1 == '\0')
11987 {
11988 /* Evaluate and ignore argument s2 in case it has
11989 side-effects. */
389dd41b 11990 return omit_one_operand_loc (loc, size_type_node,
39761420 11991 size_zero_node, s2);
4ee9c684 11992 }
11993
11994 /* If the second argument is "", return __builtin_strlen(s1). */
11995 if (p2 && *p2 == '\0')
11996 {
b9a16870 11997 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
4ee9c684 11998
11999 /* If the replacement _DECL isn't initialized, don't do the
12000 transformation. */
12001 if (!fn)
c2f47e15 12002 return NULL_TREE;
4ee9c684 12003
389dd41b 12004 return build_call_expr_loc (loc, fn, 1, s1);
4ee9c684 12005 }
c2f47e15 12006 return NULL_TREE;
4ee9c684 12007 }
12008}
12009
c2f47e15 12010/* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
12011 to the call. IGNORE is true if the value returned
f0613857 12012 by the builtin will be ignored. UNLOCKED is true is true if this
12013 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
12014 the known length of the string. Return NULL_TREE if no simplification
12015 was possible. */
4ee9c684 12016
12017tree
389dd41b 12018fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12019 bool ignore, bool unlocked, tree len)
4ee9c684 12020{
6d77ed92 12021 /* If we're using an unlocked function, assume the other unlocked
12022 functions exist explicitly. */
b9a16870 12023 tree const fn_fputc = (unlocked
12024 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12025 : builtin_decl_implicit (BUILT_IN_FPUTC));
12026 tree const fn_fwrite = (unlocked
12027 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12028 : builtin_decl_implicit (BUILT_IN_FWRITE));
4ee9c684 12029
c403edd4 12030 /* If the return value is used, don't do the transformation. */
12031 if (!ignore)
c2f47e15 12032 return NULL_TREE;
4ee9c684 12033
12034 /* Verify the arguments in the original call. */
c2f47e15 12035 if (!validate_arg (arg0, POINTER_TYPE)
12036 || !validate_arg (arg1, POINTER_TYPE))
12037 return NULL_TREE;
4ee9c684 12038
f0613857 12039 if (! len)
c2f47e15 12040 len = c_strlen (arg0, 0);
4ee9c684 12041
12042 /* Get the length of the string passed to fputs. If the length
12043 can't be determined, punt. */
12044 if (!len
12045 || TREE_CODE (len) != INTEGER_CST)
c2f47e15 12046 return NULL_TREE;
4ee9c684 12047
12048 switch (compare_tree_int (len, 1))
12049 {
12050 case -1: /* length is 0, delete the call entirely . */
389dd41b 12051 return omit_one_operand_loc (loc, integer_type_node,
12052 integer_zero_node, arg1);;
05abc81b 12053
4ee9c684 12054 case 0: /* length is 1, call fputc. */
12055 {
c2f47e15 12056 const char *p = c_getstr (arg0);
4ee9c684 12057
12058 if (p != NULL)
12059 {
c2f47e15 12060 if (fn_fputc)
389dd41b 12061 return build_call_expr_loc (loc, fn_fputc, 2,
7002a1c8 12062 build_int_cst
12063 (integer_type_node, p[0]), arg1);
c2f47e15 12064 else
12065 return NULL_TREE;
4ee9c684 12066 }
12067 }
12068 /* FALLTHROUGH */
12069 case 1: /* length is greater than 1, call fwrite. */
12070 {
0bed3869 12071 /* If optimizing for size keep fputs. */
0bfd8d5c 12072 if (optimize_function_for_size_p (cfun))
c2f47e15 12073 return NULL_TREE;
4ee9c684 12074 /* New argument list transforming fputs(string, stream) to
12075 fwrite(string, 1, len, stream). */
c2f47e15 12076 if (fn_fwrite)
389dd41b 12077 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12078 size_one_node, len, arg1);
c2f47e15 12079 else
12080 return NULL_TREE;
4ee9c684 12081 }
12082 default:
64db345d 12083 gcc_unreachable ();
4ee9c684 12084 }
c2f47e15 12085 return NULL_TREE;
4ee9c684 12086}
12087
c2f47e15 12088/* Fold the next_arg or va_start call EXP. Returns true if there was an error
743b0c6a 12089 produced. False otherwise. This is done so that we don't output the error
12090 or warning twice or three times. */
75a70cf9 12091
743b0c6a 12092bool
c2f47e15 12093fold_builtin_next_arg (tree exp, bool va_start_p)
4ee9c684 12094{
12095 tree fntype = TREE_TYPE (current_function_decl);
c2f47e15 12096 int nargs = call_expr_nargs (exp);
12097 tree arg;
d98fd4a4 12098 /* There is good chance the current input_location points inside the
12099 definition of the va_start macro (perhaps on the token for
12100 builtin) in a system header, so warnings will not be emitted.
12101 Use the location in real source code. */
12102 source_location current_location =
12103 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12104 NULL);
4ee9c684 12105
257d99c3 12106 if (!stdarg_p (fntype))
743b0c6a 12107 {
12108 error ("%<va_start%> used in function with fixed args");
12109 return true;
12110 }
c2f47e15 12111
12112 if (va_start_p)
79012a9d 12113 {
c2f47e15 12114 if (va_start_p && (nargs != 2))
12115 {
12116 error ("wrong number of arguments to function %<va_start%>");
12117 return true;
12118 }
12119 arg = CALL_EXPR_ARG (exp, 1);
79012a9d 12120 }
12121 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12122 when we checked the arguments and if needed issued a warning. */
c2f47e15 12123 else
4ee9c684 12124 {
c2f47e15 12125 if (nargs == 0)
12126 {
12127 /* Evidently an out of date version of <stdarg.h>; can't validate
12128 va_start's second argument, but can still work as intended. */
d98fd4a4 12129 warning_at (current_location,
7edb1062 12130 OPT_Wvarargs,
12131 "%<__builtin_next_arg%> called without an argument");
c2f47e15 12132 return true;
12133 }
12134 else if (nargs > 1)
a0c938f0 12135 {
c2f47e15 12136 error ("wrong number of arguments to function %<__builtin_next_arg%>");
a0c938f0 12137 return true;
12138 }
c2f47e15 12139 arg = CALL_EXPR_ARG (exp, 0);
12140 }
12141
a8dd994c 12142 if (TREE_CODE (arg) == SSA_NAME)
12143 arg = SSA_NAME_VAR (arg);
12144
c2f47e15 12145 /* We destructively modify the call to be __builtin_va_start (ap, 0)
48e1416a 12146 or __builtin_next_arg (0) the first time we see it, after checking
c2f47e15 12147 the arguments and if needed issuing a warning. */
12148 if (!integer_zerop (arg))
12149 {
12150 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
79012a9d 12151
4ee9c684 12152 /* Strip off all nops for the sake of the comparison. This
12153 is not quite the same as STRIP_NOPS. It does more.
12154 We must also strip off INDIRECT_EXPR for C++ reference
12155 parameters. */
72dd6141 12156 while (CONVERT_EXPR_P (arg)
4ee9c684 12157 || TREE_CODE (arg) == INDIRECT_REF)
12158 arg = TREE_OPERAND (arg, 0);
12159 if (arg != last_parm)
a0c938f0 12160 {
b08cf617 12161 /* FIXME: Sometimes with the tree optimizers we can get the
12162 not the last argument even though the user used the last
12163 argument. We just warn and set the arg to be the last
12164 argument so that we will get wrong-code because of
12165 it. */
d98fd4a4 12166 warning_at (current_location,
7edb1062 12167 OPT_Wvarargs,
d98fd4a4 12168 "second parameter of %<va_start%> not last named argument");
743b0c6a 12169 }
24158ad7 12170
12171 /* Undefined by C99 7.15.1.4p4 (va_start):
12172 "If the parameter parmN is declared with the register storage
12173 class, with a function or array type, or with a type that is
12174 not compatible with the type that results after application of
12175 the default argument promotions, the behavior is undefined."
12176 */
12177 else if (DECL_REGISTER (arg))
d98fd4a4 12178 {
12179 warning_at (current_location,
7edb1062 12180 OPT_Wvarargs,
d98fd4a4 12181 "undefined behaviour when second parameter of "
12182 "%<va_start%> is declared with %<register%> storage");
12183 }
24158ad7 12184
79012a9d 12185 /* We want to verify the second parameter just once before the tree
a0c938f0 12186 optimizers are run and then avoid keeping it in the tree,
12187 as otherwise we could warn even for correct code like:
12188 void foo (int i, ...)
12189 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
c2f47e15 12190 if (va_start_p)
12191 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12192 else
12193 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
743b0c6a 12194 }
12195 return false;
4ee9c684 12196}
12197
12198
c2f47e15 12199/* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12200 ORIG may be null if this is a 2-argument call. We don't attempt to
12201 simplify calls with more than 3 arguments.
4ee9c684 12202
c2f47e15 12203 Return NULL_TREE if no simplification was possible, otherwise return the
4ee9c684 12204 simplified form of the call as a tree. If IGNORED is true, it means that
12205 the caller does not use the returned value of the function. */
12206
12207static tree
389dd41b 12208fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12209 tree orig, int ignored)
4ee9c684 12210{
c2f47e15 12211 tree call, retval;
4ee9c684 12212 const char *fmt_str = NULL;
12213
12214 /* Verify the required arguments in the original call. We deal with two
12215 types of sprintf() calls: 'sprintf (str, fmt)' and
12216 'sprintf (dest, "%s", orig)'. */
c2f47e15 12217 if (!validate_arg (dest, POINTER_TYPE)
12218 || !validate_arg (fmt, POINTER_TYPE))
12219 return NULL_TREE;
12220 if (orig && !validate_arg (orig, POINTER_TYPE))
4ee9c684 12221 return NULL_TREE;
4ee9c684 12222
12223 /* Check whether the format is a literal string constant. */
12224 fmt_str = c_getstr (fmt);
12225 if (fmt_str == NULL)
12226 return NULL_TREE;
12227
12228 call = NULL_TREE;
12229 retval = NULL_TREE;
12230
d4473c84 12231 if (!init_target_chars ())
c2f47e15 12232 return NULL_TREE;
99eabcc1 12233
4ee9c684 12234 /* If the format doesn't contain % args or %%, use strcpy. */
99eabcc1 12235 if (strchr (fmt_str, target_percent) == NULL)
4ee9c684 12236 {
b9a16870 12237 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
4ee9c684 12238
12239 if (!fn)
12240 return NULL_TREE;
12241
72dfb3f2 12242 /* Don't optimize sprintf (buf, "abc", ptr++). */
c2f47e15 12243 if (orig)
72dfb3f2 12244 return NULL_TREE;
12245
4ee9c684 12246 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12247 'format' is known to contain no % formats. */
389dd41b 12248 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
4ee9c684 12249 if (!ignored)
7002a1c8 12250 retval = build_int_cst (integer_type_node, strlen (fmt_str));
4ee9c684 12251 }
12252
12253 /* If the format is "%s", use strcpy if the result isn't used. */
99eabcc1 12254 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
4ee9c684 12255 {
c2f47e15 12256 tree fn;
b9a16870 12257 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
4ee9c684 12258
12259 if (!fn)
12260 return NULL_TREE;
12261
72dfb3f2 12262 /* Don't crash on sprintf (str1, "%s"). */
c2f47e15 12263 if (!orig)
72dfb3f2 12264 return NULL_TREE;
12265
4ee9c684 12266 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
4ee9c684 12267 if (!ignored)
12268 {
12269 retval = c_strlen (orig, 1);
12270 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12271 return NULL_TREE;
12272 }
389dd41b 12273 call = build_call_expr_loc (loc, fn, 2, dest, orig);
4ee9c684 12274 }
12275
12276 if (call && retval)
12277 {
389dd41b 12278 retval = fold_convert_loc
b9a16870 12279 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
4ee9c684 12280 retval);
05abc81b 12281 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
4ee9c684 12282 }
12283 else
12284 return call;
12285}
0a39fd54 12286
750d7b16 12287/* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12288 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12289 attempt to simplify calls with more than 4 arguments.
12290
12291 Return NULL_TREE if no simplification was possible, otherwise return the
12292 simplified form of the call as a tree. If IGNORED is true, it means that
12293 the caller does not use the returned value of the function. */
12294
12295static tree
12296fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12297 tree orig, int ignored)
12298{
12299 tree call, retval;
12300 const char *fmt_str = NULL;
12301 unsigned HOST_WIDE_INT destlen;
12302
12303 /* Verify the required arguments in the original call. We deal with two
12304 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12305 'snprintf (dest, cst, "%s", orig)'. */
12306 if (!validate_arg (dest, POINTER_TYPE)
12307 || !validate_arg (destsize, INTEGER_TYPE)
12308 || !validate_arg (fmt, POINTER_TYPE))
12309 return NULL_TREE;
12310 if (orig && !validate_arg (orig, POINTER_TYPE))
12311 return NULL_TREE;
12312
12313 if (!host_integerp (destsize, 1))
12314 return NULL_TREE;
12315
12316 /* Check whether the format is a literal string constant. */
12317 fmt_str = c_getstr (fmt);
12318 if (fmt_str == NULL)
12319 return NULL_TREE;
12320
12321 call = NULL_TREE;
12322 retval = NULL_TREE;
12323
12324 if (!init_target_chars ())
12325 return NULL_TREE;
12326
12327 destlen = tree_low_cst (destsize, 1);
12328
12329 /* If the format doesn't contain % args or %%, use strcpy. */
12330 if (strchr (fmt_str, target_percent) == NULL)
12331 {
b9a16870 12332 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
750d7b16 12333 size_t len = strlen (fmt_str);
12334
12335 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12336 if (orig)
12337 return NULL_TREE;
12338
12339 /* We could expand this as
12340 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12341 or to
12342 memcpy (str, fmt_with_nul_at_cstm1, cst);
12343 but in the former case that might increase code size
12344 and in the latter case grow .rodata section too much.
12345 So punt for now. */
12346 if (len >= destlen)
12347 return NULL_TREE;
12348
12349 if (!fn)
12350 return NULL_TREE;
12351
12352 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12353 'format' is known to contain no % formats and
12354 strlen (fmt) < cst. */
12355 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12356
12357 if (!ignored)
7002a1c8 12358 retval = build_int_cst (integer_type_node, strlen (fmt_str));
750d7b16 12359 }
12360
12361 /* If the format is "%s", use strcpy if the result isn't used. */
12362 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12363 {
b9a16870 12364 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
750d7b16 12365 unsigned HOST_WIDE_INT origlen;
12366
12367 /* Don't crash on snprintf (str1, cst, "%s"). */
12368 if (!orig)
12369 return NULL_TREE;
12370
12371 retval = c_strlen (orig, 1);
12372 if (!retval || !host_integerp (retval, 1))
12373 return NULL_TREE;
12374
12375 origlen = tree_low_cst (retval, 1);
12376 /* We could expand this as
12377 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12378 or to
12379 memcpy (str1, str2_with_nul_at_cstm1, cst);
12380 but in the former case that might increase code size
12381 and in the latter case grow .rodata section too much.
12382 So punt for now. */
12383 if (origlen >= destlen)
12384 return NULL_TREE;
12385
12386 /* Convert snprintf (str1, cst, "%s", str2) into
12387 strcpy (str1, str2) if strlen (str2) < cst. */
12388 if (!fn)
12389 return NULL_TREE;
12390
12391 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12392
12393 if (ignored)
12394 retval = NULL_TREE;
12395 }
12396
12397 if (call && retval)
12398 {
b9a16870 12399 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
1914d98e 12400 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
750d7b16 12401 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12402 }
12403 else
12404 return call;
12405}
12406
c2f47e15 12407/* Expand a call EXP to __builtin_object_size. */
0a39fd54 12408
12409rtx
12410expand_builtin_object_size (tree exp)
12411{
12412 tree ost;
12413 int object_size_type;
12414 tree fndecl = get_callee_fndecl (exp);
0a39fd54 12415
c2f47e15 12416 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
0a39fd54 12417 {
b8c23db3 12418 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12419 exp, fndecl);
0a39fd54 12420 expand_builtin_trap ();
12421 return const0_rtx;
12422 }
12423
c2f47e15 12424 ost = CALL_EXPR_ARG (exp, 1);
0a39fd54 12425 STRIP_NOPS (ost);
12426
12427 if (TREE_CODE (ost) != INTEGER_CST
12428 || tree_int_cst_sgn (ost) < 0
12429 || compare_tree_int (ost, 3) > 0)
12430 {
b8c23db3 12431 error ("%Klast argument of %D is not integer constant between 0 and 3",
12432 exp, fndecl);
0a39fd54 12433 expand_builtin_trap ();
12434 return const0_rtx;
12435 }
12436
12437 object_size_type = tree_low_cst (ost, 0);
12438
12439 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12440}
12441
12442/* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12443 FCODE is the BUILT_IN_* to use.
c2f47e15 12444 Return NULL_RTX if we failed; the caller should emit a normal call,
0a39fd54 12445 otherwise try to get the result in TARGET, if convenient (and in
12446 mode MODE if that's convenient). */
12447
12448static rtx
12449expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12450 enum built_in_function fcode)
12451{
0a39fd54 12452 tree dest, src, len, size;
12453
c2f47e15 12454 if (!validate_arglist (exp,
0a39fd54 12455 POINTER_TYPE,
12456 fcode == BUILT_IN_MEMSET_CHK
12457 ? INTEGER_TYPE : POINTER_TYPE,
12458 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
c2f47e15 12459 return NULL_RTX;
0a39fd54 12460
c2f47e15 12461 dest = CALL_EXPR_ARG (exp, 0);
12462 src = CALL_EXPR_ARG (exp, 1);
12463 len = CALL_EXPR_ARG (exp, 2);
12464 size = CALL_EXPR_ARG (exp, 3);
0a39fd54 12465
12466 if (! host_integerp (size, 1))
c2f47e15 12467 return NULL_RTX;
0a39fd54 12468
12469 if (host_integerp (len, 1) || integer_all_onesp (size))
12470 {
12471 tree fn;
12472
12473 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12474 {
b430e8d9 12475 warning_at (tree_nonartificial_location (exp),
12476 0, "%Kcall to %D will always overflow destination buffer",
12477 exp, get_callee_fndecl (exp));
c2f47e15 12478 return NULL_RTX;
0a39fd54 12479 }
12480
0a39fd54 12481 fn = NULL_TREE;
12482 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12483 mem{cpy,pcpy,move,set} is available. */
12484 switch (fcode)
12485 {
12486 case BUILT_IN_MEMCPY_CHK:
b9a16870 12487 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
0a39fd54 12488 break;
12489 case BUILT_IN_MEMPCPY_CHK:
b9a16870 12490 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
0a39fd54 12491 break;
12492 case BUILT_IN_MEMMOVE_CHK:
b9a16870 12493 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
0a39fd54 12494 break;
12495 case BUILT_IN_MEMSET_CHK:
b9a16870 12496 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
0a39fd54 12497 break;
12498 default:
12499 break;
12500 }
12501
12502 if (! fn)
c2f47e15 12503 return NULL_RTX;
0a39fd54 12504
0568e9c1 12505 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
a65c4d64 12506 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12507 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
0a39fd54 12508 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12509 }
12510 else if (fcode == BUILT_IN_MEMSET_CHK)
c2f47e15 12511 return NULL_RTX;
0a39fd54 12512 else
12513 {
957d0361 12514 unsigned int dest_align = get_pointer_alignment (dest);
0a39fd54 12515
12516 /* If DEST is not a pointer type, call the normal function. */
12517 if (dest_align == 0)
c2f47e15 12518 return NULL_RTX;
0a39fd54 12519
12520 /* If SRC and DEST are the same (and not volatile), do nothing. */
12521 if (operand_equal_p (src, dest, 0))
12522 {
12523 tree expr;
12524
12525 if (fcode != BUILT_IN_MEMPCPY_CHK)
12526 {
12527 /* Evaluate and ignore LEN in case it has side-effects. */
12528 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12529 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12530 }
12531
2cc66f2a 12532 expr = fold_build_pointer_plus (dest, len);
0a39fd54 12533 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12534 }
12535
12536 /* __memmove_chk special case. */
12537 if (fcode == BUILT_IN_MEMMOVE_CHK)
12538 {
957d0361 12539 unsigned int src_align = get_pointer_alignment (src);
0a39fd54 12540
12541 if (src_align == 0)
c2f47e15 12542 return NULL_RTX;
0a39fd54 12543
12544 /* If src is categorized for a readonly section we can use
12545 normal __memcpy_chk. */
12546 if (readonly_data_expr (src))
12547 {
b9a16870 12548 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
0a39fd54 12549 if (!fn)
c2f47e15 12550 return NULL_RTX;
0568e9c1 12551 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12552 dest, src, len, size);
a65c4d64 12553 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12554 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
0a39fd54 12555 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12556 }
12557 }
c2f47e15 12558 return NULL_RTX;
0a39fd54 12559 }
12560}
12561
12562/* Emit warning if a buffer overflow is detected at compile time. */
12563
12564static void
12565maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12566{
c2f47e15 12567 int is_strlen = 0;
0a39fd54 12568 tree len, size;
b430e8d9 12569 location_t loc = tree_nonartificial_location (exp);
0a39fd54 12570
12571 switch (fcode)
12572 {
12573 case BUILT_IN_STRCPY_CHK:
12574 case BUILT_IN_STPCPY_CHK:
12575 /* For __strcat_chk the warning will be emitted only if overflowing
12576 by at least strlen (dest) + 1 bytes. */
12577 case BUILT_IN_STRCAT_CHK:
c2f47e15 12578 len = CALL_EXPR_ARG (exp, 1);
12579 size = CALL_EXPR_ARG (exp, 2);
0a39fd54 12580 is_strlen = 1;
12581 break;
b356dfef 12582 case BUILT_IN_STRNCAT_CHK:
0a39fd54 12583 case BUILT_IN_STRNCPY_CHK:
1063acde 12584 case BUILT_IN_STPNCPY_CHK:
c2f47e15 12585 len = CALL_EXPR_ARG (exp, 2);
12586 size = CALL_EXPR_ARG (exp, 3);
0a39fd54 12587 break;
12588 case BUILT_IN_SNPRINTF_CHK:
12589 case BUILT_IN_VSNPRINTF_CHK:
c2f47e15 12590 len = CALL_EXPR_ARG (exp, 1);
12591 size = CALL_EXPR_ARG (exp, 3);
0a39fd54 12592 break;
12593 default:
12594 gcc_unreachable ();
12595 }
12596
0a39fd54 12597 if (!len || !size)
12598 return;
12599
0a39fd54 12600 if (! host_integerp (size, 1) || integer_all_onesp (size))
12601 return;
12602
12603 if (is_strlen)
12604 {
12605 len = c_strlen (len, 1);
12606 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12607 return;
12608 }
b356dfef 12609 else if (fcode == BUILT_IN_STRNCAT_CHK)
12610 {
c2f47e15 12611 tree src = CALL_EXPR_ARG (exp, 1);
b356dfef 12612 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12613 return;
12614 src = c_strlen (src, 1);
12615 if (! src || ! host_integerp (src, 1))
12616 {
b430e8d9 12617 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12618 exp, get_callee_fndecl (exp));
b356dfef 12619 return;
12620 }
12621 else if (tree_int_cst_lt (src, size))
12622 return;
12623 }
0a39fd54 12624 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12625 return;
12626
b430e8d9 12627 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12628 exp, get_callee_fndecl (exp));
0a39fd54 12629}
12630
12631/* Emit warning if a buffer overflow is detected at compile time
12632 in __sprintf_chk/__vsprintf_chk calls. */
12633
12634static void
12635maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12636{
1e4adcfc 12637 tree size, len, fmt;
0a39fd54 12638 const char *fmt_str;
c2f47e15 12639 int nargs = call_expr_nargs (exp);
0a39fd54 12640
12641 /* Verify the required arguments in the original call. */
48e1416a 12642
c2f47e15 12643 if (nargs < 4)
0a39fd54 12644 return;
c2f47e15 12645 size = CALL_EXPR_ARG (exp, 2);
12646 fmt = CALL_EXPR_ARG (exp, 3);
0a39fd54 12647
12648 if (! host_integerp (size, 1) || integer_all_onesp (size))
12649 return;
12650
12651 /* Check whether the format is a literal string constant. */
12652 fmt_str = c_getstr (fmt);
12653 if (fmt_str == NULL)
12654 return;
12655
d4473c84 12656 if (!init_target_chars ())
99eabcc1 12657 return;
12658
0a39fd54 12659 /* If the format doesn't contain % args or %%, we know its size. */
99eabcc1 12660 if (strchr (fmt_str, target_percent) == 0)
0a39fd54 12661 len = build_int_cstu (size_type_node, strlen (fmt_str));
12662 /* If the format is "%s" and first ... argument is a string literal,
12663 we know it too. */
c2f47e15 12664 else if (fcode == BUILT_IN_SPRINTF_CHK
12665 && strcmp (fmt_str, target_percent_s) == 0)
0a39fd54 12666 {
12667 tree arg;
12668
c2f47e15 12669 if (nargs < 5)
0a39fd54 12670 return;
c2f47e15 12671 arg = CALL_EXPR_ARG (exp, 4);
0a39fd54 12672 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12673 return;
12674
12675 len = c_strlen (arg, 1);
12676 if (!len || ! host_integerp (len, 1))
12677 return;
12678 }
12679 else
12680 return;
12681
12682 if (! tree_int_cst_lt (len, size))
b430e8d9 12683 warning_at (tree_nonartificial_location (exp),
12684 0, "%Kcall to %D will always overflow destination buffer",
12685 exp, get_callee_fndecl (exp));
0a39fd54 12686}
12687
2c281b15 12688/* Emit warning if a free is called with address of a variable. */
12689
12690static void
12691maybe_emit_free_warning (tree exp)
12692{
12693 tree arg = CALL_EXPR_ARG (exp, 0);
12694
12695 STRIP_NOPS (arg);
12696 if (TREE_CODE (arg) != ADDR_EXPR)
12697 return;
12698
12699 arg = get_base_address (TREE_OPERAND (arg, 0));
182cf5a9 12700 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
2c281b15 12701 return;
12702
12703 if (SSA_VAR_P (arg))
f74ea1c2 12704 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12705 "%Kattempt to free a non-heap object %qD", exp, arg);
2c281b15 12706 else
f74ea1c2 12707 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12708 "%Kattempt to free a non-heap object", exp);
2c281b15 12709}
12710
c2f47e15 12711/* Fold a call to __builtin_object_size with arguments PTR and OST,
12712 if possible. */
0a39fd54 12713
12714tree
c2f47e15 12715fold_builtin_object_size (tree ptr, tree ost)
0a39fd54 12716{
a6caa15f 12717 unsigned HOST_WIDE_INT bytes;
0a39fd54 12718 int object_size_type;
12719
c2f47e15 12720 if (!validate_arg (ptr, POINTER_TYPE)
12721 || !validate_arg (ost, INTEGER_TYPE))
12722 return NULL_TREE;
0a39fd54 12723
0a39fd54 12724 STRIP_NOPS (ost);
12725
12726 if (TREE_CODE (ost) != INTEGER_CST
12727 || tree_int_cst_sgn (ost) < 0
12728 || compare_tree_int (ost, 3) > 0)
c2f47e15 12729 return NULL_TREE;
0a39fd54 12730
12731 object_size_type = tree_low_cst (ost, 0);
12732
12733 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12734 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12735 and (size_t) 0 for types 2 and 3. */
12736 if (TREE_SIDE_EFFECTS (ptr))
697bbc3f 12737 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
0a39fd54 12738
12739 if (TREE_CODE (ptr) == ADDR_EXPR)
a6caa15f 12740 {
12741 bytes = compute_builtin_object_size (ptr, object_size_type);
12742 if (double_int_fits_to_tree_p (size_type_node,
12743 uhwi_to_double_int (bytes)))
12744 return build_int_cstu (size_type_node, bytes);
12745 }
0a39fd54 12746 else if (TREE_CODE (ptr) == SSA_NAME)
12747 {
0a39fd54 12748 /* If object size is not known yet, delay folding until
12749 later. Maybe subsequent passes will help determining
12750 it. */
12751 bytes = compute_builtin_object_size (ptr, object_size_type);
a6caa15f 12752 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12753 && double_int_fits_to_tree_p (size_type_node,
12754 uhwi_to_double_int (bytes)))
12755 return build_int_cstu (size_type_node, bytes);
0a39fd54 12756 }
12757
a6caa15f 12758 return NULL_TREE;
0a39fd54 12759}
12760
12761/* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
c2f47e15 12762 DEST, SRC, LEN, and SIZE are the arguments to the call.
0a39fd54 12763 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12764 code of the builtin. If MAXLEN is not NULL, it is maximum length
12765 passed as third argument. */
12766
12767tree
389dd41b 12768fold_builtin_memory_chk (location_t loc, tree fndecl,
c2f47e15 12769 tree dest, tree src, tree len, tree size,
12770 tree maxlen, bool ignore,
0a39fd54 12771 enum built_in_function fcode)
12772{
c2f47e15 12773 tree fn;
0a39fd54 12774
c2f47e15 12775 if (!validate_arg (dest, POINTER_TYPE)
12776 || !validate_arg (src,
12777 (fcode == BUILT_IN_MEMSET_CHK
12778 ? INTEGER_TYPE : POINTER_TYPE))
12779 || !validate_arg (len, INTEGER_TYPE)
12780 || !validate_arg (size, INTEGER_TYPE))
12781 return NULL_TREE;
0a39fd54 12782
12783 /* If SRC and DEST are the same (and not volatile), return DEST
12784 (resp. DEST+LEN for __mempcpy_chk). */
12785 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12786 {
12787 if (fcode != BUILT_IN_MEMPCPY_CHK)
389dd41b 12788 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12789 dest, len);
0a39fd54 12790 else
12791 {
2cc66f2a 12792 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
389dd41b 12793 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
0a39fd54 12794 }
12795 }
12796
12797 if (! host_integerp (size, 1))
c2f47e15 12798 return NULL_TREE;
0a39fd54 12799
12800 if (! integer_all_onesp (size))
12801 {
12802 if (! host_integerp (len, 1))
12803 {
12804 /* If LEN is not constant, try MAXLEN too.
12805 For MAXLEN only allow optimizing into non-_ocs function
12806 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12807 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12808 {
12809 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12810 {
12811 /* (void) __mempcpy_chk () can be optimized into
12812 (void) __memcpy_chk (). */
b9a16870 12813 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
0a39fd54 12814 if (!fn)
c2f47e15 12815 return NULL_TREE;
0a39fd54 12816
389dd41b 12817 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
0a39fd54 12818 }
c2f47e15 12819 return NULL_TREE;
0a39fd54 12820 }
0a39fd54 12821 }
ad89623c 12822 else
12823 maxlen = len;
0a39fd54 12824
ad89623c 12825 if (tree_int_cst_lt (size, maxlen))
c2f47e15 12826 return NULL_TREE;
0a39fd54 12827 }
12828
0a39fd54 12829 fn = NULL_TREE;
12830 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12831 mem{cpy,pcpy,move,set} is available. */
12832 switch (fcode)
12833 {
12834 case BUILT_IN_MEMCPY_CHK:
b9a16870 12835 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
0a39fd54 12836 break;
12837 case BUILT_IN_MEMPCPY_CHK:
b9a16870 12838 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
0a39fd54 12839 break;
12840 case BUILT_IN_MEMMOVE_CHK:
b9a16870 12841 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
0a39fd54 12842 break;
12843 case BUILT_IN_MEMSET_CHK:
b9a16870 12844 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
0a39fd54 12845 break;
12846 default:
12847 break;
12848 }
12849
12850 if (!fn)
c2f47e15 12851 return NULL_TREE;
0a39fd54 12852
389dd41b 12853 return build_call_expr_loc (loc, fn, 3, dest, src, len);
0a39fd54 12854}
12855
12856/* Fold a call to the __st[rp]cpy_chk builtin.
c2f47e15 12857 DEST, SRC, and SIZE are the arguments to the call.
12858 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
0a39fd54 12859 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12860 strings passed as second argument. */
12861
12862tree
389dd41b 12863fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12864 tree src, tree size,
c2f47e15 12865 tree maxlen, bool ignore,
0a39fd54 12866 enum built_in_function fcode)
12867{
c2f47e15 12868 tree len, fn;
0a39fd54 12869
c2f47e15 12870 if (!validate_arg (dest, POINTER_TYPE)
12871 || !validate_arg (src, POINTER_TYPE)
12872 || !validate_arg (size, INTEGER_TYPE))
12873 return NULL_TREE;
0a39fd54 12874
12875 /* If SRC and DEST are the same (and not volatile), return DEST. */
12876 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
389dd41b 12877 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
a0c938f0 12878
0a39fd54 12879 if (! host_integerp (size, 1))
c2f47e15 12880 return NULL_TREE;
0a39fd54 12881
12882 if (! integer_all_onesp (size))
12883 {
12884 len = c_strlen (src, 1);
12885 if (! len || ! host_integerp (len, 1))
12886 {
12887 /* If LEN is not constant, try MAXLEN too.
12888 For MAXLEN only allow optimizing into non-_ocs function
12889 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12890 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12891 {
12892 if (fcode == BUILT_IN_STPCPY_CHK)
12893 {
12894 if (! ignore)
c2f47e15 12895 return NULL_TREE;
0a39fd54 12896
12897 /* If return value of __stpcpy_chk is ignored,
12898 optimize into __strcpy_chk. */
b9a16870 12899 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
0a39fd54 12900 if (!fn)
c2f47e15 12901 return NULL_TREE;
0a39fd54 12902
389dd41b 12903 return build_call_expr_loc (loc, fn, 3, dest, src, size);
0a39fd54 12904 }
12905
12906 if (! len || TREE_SIDE_EFFECTS (len))
c2f47e15 12907 return NULL_TREE;
0a39fd54 12908
12909 /* If c_strlen returned something, but not a constant,
12910 transform __strcpy_chk into __memcpy_chk. */
b9a16870 12911 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
0a39fd54 12912 if (!fn)
c2f47e15 12913 return NULL_TREE;
0a39fd54 12914
bd6dcc04 12915 len = fold_convert_loc (loc, size_type_node, len);
12916 len = size_binop_loc (loc, PLUS_EXPR, len,
12917 build_int_cst (size_type_node, 1));
389dd41b 12918 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12919 build_call_expr_loc (loc, fn, 4,
12920 dest, src, len, size));
0a39fd54 12921 }
0a39fd54 12922 }
ad89623c 12923 else
12924 maxlen = len;
12925
12926 if (! tree_int_cst_lt (maxlen, size))
c2f47e15 12927 return NULL_TREE;
0a39fd54 12928 }
12929
0a39fd54 12930 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
b9a16870 12931 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12932 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
0a39fd54 12933 if (!fn)
c2f47e15 12934 return NULL_TREE;
0a39fd54 12935
389dd41b 12936 return build_call_expr_loc (loc, fn, 2, dest, src);
0a39fd54 12937}
12938
1063acde 12939/* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
c2f47e15 12940 are the arguments to the call. If MAXLEN is not NULL, it is maximum
1063acde 12941 length passed as third argument. IGNORE is true if return value can be
12942 ignored. FCODE is the BUILT_IN_* code of the builtin. */
0a39fd54 12943
12944tree
1063acde 12945fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12946 tree len, tree size, tree maxlen, bool ignore,
12947 enum built_in_function fcode)
0a39fd54 12948{
c2f47e15 12949 tree fn;
0a39fd54 12950
c2f47e15 12951 if (!validate_arg (dest, POINTER_TYPE)
12952 || !validate_arg (src, POINTER_TYPE)
12953 || !validate_arg (len, INTEGER_TYPE)
12954 || !validate_arg (size, INTEGER_TYPE))
12955 return NULL_TREE;
0a39fd54 12956
1063acde 12957 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12958 {
12959 /* If return value of __stpncpy_chk is ignored,
12960 optimize into __strncpy_chk. */
12961 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12962 if (fn)
12963 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12964 }
12965
0a39fd54 12966 if (! host_integerp (size, 1))
c2f47e15 12967 return NULL_TREE;
0a39fd54 12968
12969 if (! integer_all_onesp (size))
12970 {
12971 if (! host_integerp (len, 1))
12972 {
12973 /* If LEN is not constant, try MAXLEN too.
12974 For MAXLEN only allow optimizing into non-_ocs function
12975 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12976 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
c2f47e15 12977 return NULL_TREE;
0a39fd54 12978 }
ad89623c 12979 else
12980 maxlen = len;
0a39fd54 12981
ad89623c 12982 if (tree_int_cst_lt (size, maxlen))
c2f47e15 12983 return NULL_TREE;
0a39fd54 12984 }
12985
1063acde 12986 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12987 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12988 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
0a39fd54 12989 if (!fn)
c2f47e15 12990 return NULL_TREE;
0a39fd54 12991
389dd41b 12992 return build_call_expr_loc (loc, fn, 3, dest, src, len);
0a39fd54 12993}
12994
c2f47e15 12995/* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12996 are the arguments to the call. */
0a39fd54 12997
12998static tree
389dd41b 12999fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
13000 tree src, tree size)
0a39fd54 13001{
c2f47e15 13002 tree fn;
0a39fd54 13003 const char *p;
13004
c2f47e15 13005 if (!validate_arg (dest, POINTER_TYPE)
13006 || !validate_arg (src, POINTER_TYPE)
13007 || !validate_arg (size, INTEGER_TYPE))
13008 return NULL_TREE;
0a39fd54 13009
13010 p = c_getstr (src);
13011 /* If the SRC parameter is "", return DEST. */
13012 if (p && *p == '\0')
389dd41b 13013 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
0a39fd54 13014
13015 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
c2f47e15 13016 return NULL_TREE;
0a39fd54 13017
13018 /* If __builtin_strcat_chk is used, assume strcat is available. */
b9a16870 13019 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
0a39fd54 13020 if (!fn)
c2f47e15 13021 return NULL_TREE;
0a39fd54 13022
389dd41b 13023 return build_call_expr_loc (loc, fn, 2, dest, src);
0a39fd54 13024}
13025
c2f47e15 13026/* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13027 LEN, and SIZE. */
0a39fd54 13028
13029static tree
389dd41b 13030fold_builtin_strncat_chk (location_t loc, tree fndecl,
c2f47e15 13031 tree dest, tree src, tree len, tree size)
0a39fd54 13032{
c2f47e15 13033 tree fn;
0a39fd54 13034 const char *p;
13035
c2f47e15 13036 if (!validate_arg (dest, POINTER_TYPE)
13037 || !validate_arg (src, POINTER_TYPE)
13038 || !validate_arg (size, INTEGER_TYPE)
13039 || !validate_arg (size, INTEGER_TYPE))
13040 return NULL_TREE;
0a39fd54 13041
13042 p = c_getstr (src);
13043 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13044 if (p && *p == '\0')
389dd41b 13045 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
0a39fd54 13046 else if (integer_zerop (len))
389dd41b 13047 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
0a39fd54 13048
13049 if (! host_integerp (size, 1))
c2f47e15 13050 return NULL_TREE;
0a39fd54 13051
13052 if (! integer_all_onesp (size))
13053 {
13054 tree src_len = c_strlen (src, 1);
13055 if (src_len
13056 && host_integerp (src_len, 1)
13057 && host_integerp (len, 1)
13058 && ! tree_int_cst_lt (len, src_len))
13059 {
13060 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
b9a16870 13061 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
0a39fd54 13062 if (!fn)
c2f47e15 13063 return NULL_TREE;
0a39fd54 13064
389dd41b 13065 return build_call_expr_loc (loc, fn, 3, dest, src, size);
0a39fd54 13066 }
c2f47e15 13067 return NULL_TREE;
0a39fd54 13068 }
13069
0a39fd54 13070 /* If __builtin_strncat_chk is used, assume strncat is available. */
b9a16870 13071 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
0a39fd54 13072 if (!fn)
c2f47e15 13073 return NULL_TREE;
0a39fd54 13074
389dd41b 13075 return build_call_expr_loc (loc, fn, 3, dest, src, len);
0a39fd54 13076}
13077
af1409ad 13078/* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13079 Return NULL_TREE if a normal call should be emitted rather than
13080 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13081 or BUILT_IN_VSPRINTF_CHK. */
0a39fd54 13082
13083static tree
af1409ad 13084fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13085 enum built_in_function fcode)
0a39fd54 13086{
13087 tree dest, size, len, fn, fmt, flag;
13088 const char *fmt_str;
13089
13090 /* Verify the required arguments in the original call. */
c2f47e15 13091 if (nargs < 4)
13092 return NULL_TREE;
af1409ad 13093 dest = args[0];
c2f47e15 13094 if (!validate_arg (dest, POINTER_TYPE))
13095 return NULL_TREE;
af1409ad 13096 flag = args[1];
c2f47e15 13097 if (!validate_arg (flag, INTEGER_TYPE))
13098 return NULL_TREE;
af1409ad 13099 size = args[2];
c2f47e15 13100 if (!validate_arg (size, INTEGER_TYPE))
13101 return NULL_TREE;
af1409ad 13102 fmt = args[3];
c2f47e15 13103 if (!validate_arg (fmt, POINTER_TYPE))
13104 return NULL_TREE;
0a39fd54 13105
13106 if (! host_integerp (size, 1))
c2f47e15 13107 return NULL_TREE;
0a39fd54 13108
13109 len = NULL_TREE;
13110
d4473c84 13111 if (!init_target_chars ())
c2f47e15 13112 return NULL_TREE;
99eabcc1 13113
0a39fd54 13114 /* Check whether the format is a literal string constant. */
13115 fmt_str = c_getstr (fmt);
13116 if (fmt_str != NULL)
13117 {
13118 /* If the format doesn't contain % args or %%, we know the size. */
99eabcc1 13119 if (strchr (fmt_str, target_percent) == 0)
0a39fd54 13120 {
c2f47e15 13121 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
0a39fd54 13122 len = build_int_cstu (size_type_node, strlen (fmt_str));
13123 }
13124 /* If the format is "%s" and first ... argument is a string literal,
13125 we know the size too. */
c2f47e15 13126 else if (fcode == BUILT_IN_SPRINTF_CHK
13127 && strcmp (fmt_str, target_percent_s) == 0)
0a39fd54 13128 {
13129 tree arg;
13130
c2f47e15 13131 if (nargs == 5)
0a39fd54 13132 {
af1409ad 13133 arg = args[4];
c2f47e15 13134 if (validate_arg (arg, POINTER_TYPE))
0a39fd54 13135 {
13136 len = c_strlen (arg, 1);
13137 if (! len || ! host_integerp (len, 1))
13138 len = NULL_TREE;
13139 }
13140 }
13141 }
13142 }
13143
13144 if (! integer_all_onesp (size))
13145 {
13146 if (! len || ! tree_int_cst_lt (len, size))
c2f47e15 13147 return NULL_TREE;
0a39fd54 13148 }
13149
13150 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13151 or if format doesn't contain % chars or is "%s". */
13152 if (! integer_zerop (flag))
13153 {
13154 if (fmt_str == NULL)
c2f47e15 13155 return NULL_TREE;
13156 if (strchr (fmt_str, target_percent) != NULL
13157 && strcmp (fmt_str, target_percent_s))
13158 return NULL_TREE;
0a39fd54 13159 }
13160
0a39fd54 13161 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
b9a16870 13162 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13163 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
0a39fd54 13164 if (!fn)
c2f47e15 13165 return NULL_TREE;
0a39fd54 13166
af1409ad 13167 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
0a39fd54 13168}
13169
af1409ad 13170/* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
0a39fd54 13171 a normal call should be emitted rather than expanding the function
af1409ad 13172 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13173
13174static tree
13175fold_builtin_sprintf_chk (location_t loc, tree exp,
13176 enum built_in_function fcode)
13177{
13178 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13179 CALL_EXPR_ARGP (exp), fcode);
13180}
13181
13182/* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13183 NULL_TREE if a normal call should be emitted rather than expanding
13184 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
0a39fd54 13185 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13186 passed as second argument. */
13187
af1409ad 13188static tree
13189fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13190 tree maxlen, enum built_in_function fcode)
0a39fd54 13191{
13192 tree dest, size, len, fn, fmt, flag;
13193 const char *fmt_str;
13194
13195 /* Verify the required arguments in the original call. */
af1409ad 13196 if (nargs < 5)
c2f47e15 13197 return NULL_TREE;
af1409ad 13198 dest = args[0];
c2f47e15 13199 if (!validate_arg (dest, POINTER_TYPE))
13200 return NULL_TREE;
af1409ad 13201 len = args[1];
c2f47e15 13202 if (!validate_arg (len, INTEGER_TYPE))
13203 return NULL_TREE;
af1409ad 13204 flag = args[2];
c2f47e15 13205 if (!validate_arg (flag, INTEGER_TYPE))
13206 return NULL_TREE;
af1409ad 13207 size = args[3];
c2f47e15 13208 if (!validate_arg (size, INTEGER_TYPE))
13209 return NULL_TREE;
af1409ad 13210 fmt = args[4];
c2f47e15 13211 if (!validate_arg (fmt, POINTER_TYPE))
13212 return NULL_TREE;
0a39fd54 13213
13214 if (! host_integerp (size, 1))
c2f47e15 13215 return NULL_TREE;
0a39fd54 13216
13217 if (! integer_all_onesp (size))
13218 {
13219 if (! host_integerp (len, 1))
13220 {
13221 /* If LEN is not constant, try MAXLEN too.
13222 For MAXLEN only allow optimizing into non-_ocs function
13223 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13224 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
c2f47e15 13225 return NULL_TREE;
0a39fd54 13226 }
ad89623c 13227 else
13228 maxlen = len;
0a39fd54 13229
ad89623c 13230 if (tree_int_cst_lt (size, maxlen))
c2f47e15 13231 return NULL_TREE;
0a39fd54 13232 }
13233
d4473c84 13234 if (!init_target_chars ())
c2f47e15 13235 return NULL_TREE;
99eabcc1 13236
0a39fd54 13237 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13238 or if format doesn't contain % chars or is "%s". */
13239 if (! integer_zerop (flag))
13240 {
13241 fmt_str = c_getstr (fmt);
13242 if (fmt_str == NULL)
c2f47e15 13243 return NULL_TREE;
13244 if (strchr (fmt_str, target_percent) != NULL
13245 && strcmp (fmt_str, target_percent_s))
13246 return NULL_TREE;
0a39fd54 13247 }
13248
0a39fd54 13249 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13250 available. */
b9a16870 13251 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13252 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
0a39fd54 13253 if (!fn)
c2f47e15 13254 return NULL_TREE;
0a39fd54 13255
af1409ad 13256 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13257}
13258
13259/* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13260 a normal call should be emitted rather than expanding the function
13261 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13262 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13263 passed as second argument. */
13264
13265tree
13266fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13267 enum built_in_function fcode)
13268{
13269 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13270 CALL_EXPR_ARGP (exp), maxlen, fcode);
0a39fd54 13271}
13272
13273/* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
c2f47e15 13274 FMT and ARG are the arguments to the call; we don't fold cases with
13275 more than 2 arguments, and ARG may be null if this is a 1-argument case.
0a39fd54 13276
c2f47e15 13277 Return NULL_TREE if no simplification was possible, otherwise return the
0a39fd54 13278 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13279 code of the function to be simplified. */
13280
13281static tree
389dd41b 13282fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13283 tree arg, bool ignore,
0a39fd54 13284 enum built_in_function fcode)
13285{
c2f47e15 13286 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
0a39fd54 13287 const char *fmt_str = NULL;
13288
13289 /* If the return value is used, don't do the transformation. */
13290 if (! ignore)
c2f47e15 13291 return NULL_TREE;
0a39fd54 13292
13293 /* Verify the required arguments in the original call. */
c2f47e15 13294 if (!validate_arg (fmt, POINTER_TYPE))
13295 return NULL_TREE;
0a39fd54 13296
13297 /* Check whether the format is a literal string constant. */
13298 fmt_str = c_getstr (fmt);
13299 if (fmt_str == NULL)
13300 return NULL_TREE;
13301
13302 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13303 {
6d77ed92 13304 /* If we're using an unlocked function, assume the other
13305 unlocked functions exist explicitly. */
b9a16870 13306 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13307 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
0a39fd54 13308 }
13309 else
13310 {
b9a16870 13311 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13312 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
0a39fd54 13313 }
13314
d4473c84 13315 if (!init_target_chars ())
c2f47e15 13316 return NULL_TREE;
a0c938f0 13317
c2f47e15 13318 if (strcmp (fmt_str, target_percent_s) == 0
13319 || strchr (fmt_str, target_percent) == NULL)
0a39fd54 13320 {
13321 const char *str;
13322
99eabcc1 13323 if (strcmp (fmt_str, target_percent_s) == 0)
0a39fd54 13324 {
13325 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
c2f47e15 13326 return NULL_TREE;
0a39fd54 13327
c2f47e15 13328 if (!arg || !validate_arg (arg, POINTER_TYPE))
13329 return NULL_TREE;
0a39fd54 13330
c2f47e15 13331 str = c_getstr (arg);
0a39fd54 13332 if (str == NULL)
c2f47e15 13333 return NULL_TREE;
0a39fd54 13334 }
13335 else
13336 {
13337 /* The format specifier doesn't contain any '%' characters. */
13338 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
c2f47e15 13339 && arg)
13340 return NULL_TREE;
0a39fd54 13341 str = fmt_str;
13342 }
13343
13344 /* If the string was "", printf does nothing. */
13345 if (str[0] == '\0')
13346 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13347
13348 /* If the string has length of 1, call putchar. */
13349 if (str[1] == '\0')
13350 {
13351 /* Given printf("c"), (where c is any one character,)
13352 convert "c"[0] to an int and pass that to the replacement
13353 function. */
7002a1c8 13354 newarg = build_int_cst (integer_type_node, str[0]);
c2f47e15 13355 if (fn_putchar)
389dd41b 13356 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
0a39fd54 13357 }
13358 else
13359 {
13360 /* If the string was "string\n", call puts("string"). */
13361 size_t len = strlen (str);
663dbd5e 13362 if ((unsigned char)str[len - 1] == target_newline
13363 && (size_t) (int) len == len
13364 && (int) len > 0)
0a39fd54 13365 {
663dbd5e 13366 char *newstr;
13367 tree offset_node, string_cst;
13368
0a39fd54 13369 /* Create a NUL-terminated string that's one char shorter
13370 than the original, stripping off the trailing '\n'. */
663dbd5e 13371 newarg = build_string_literal (len, str);
13372 string_cst = string_constant (newarg, &offset_node);
13373 gcc_checking_assert (string_cst
13374 && (TREE_STRING_LENGTH (string_cst)
13375 == (int) len)
13376 && integer_zerop (offset_node)
13377 && (unsigned char)
13378 TREE_STRING_POINTER (string_cst)[len - 1]
13379 == target_newline);
13380 /* build_string_literal creates a new STRING_CST,
13381 modify it in place to avoid double copying. */
13382 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13383 newstr[len - 1] = '\0';
c2f47e15 13384 if (fn_puts)
389dd41b 13385 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
0a39fd54 13386 }
13387 else
13388 /* We'd like to arrange to call fputs(string,stdout) here,
13389 but we need stdout and don't have a way to get it yet. */
c2f47e15 13390 return NULL_TREE;
0a39fd54 13391 }
13392 }
13393
13394 /* The other optimizations can be done only on the non-va_list variants. */
13395 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
c2f47e15 13396 return NULL_TREE;
0a39fd54 13397
13398 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
99eabcc1 13399 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
0a39fd54 13400 {
c2f47e15 13401 if (!arg || !validate_arg (arg, POINTER_TYPE))
13402 return NULL_TREE;
13403 if (fn_puts)
389dd41b 13404 call = build_call_expr_loc (loc, fn_puts, 1, arg);
0a39fd54 13405 }
13406
13407 /* If the format specifier was "%c", call __builtin_putchar(arg). */
99eabcc1 13408 else if (strcmp (fmt_str, target_percent_c) == 0)
0a39fd54 13409 {
c2f47e15 13410 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13411 return NULL_TREE;
13412 if (fn_putchar)
389dd41b 13413 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
0a39fd54 13414 }
13415
c2f47e15 13416 if (!call)
13417 return NULL_TREE;
0a39fd54 13418
389dd41b 13419 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
0a39fd54 13420}
13421
13422/* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
c2f47e15 13423 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13424 more than 3 arguments, and ARG may be null in the 2-argument case.
0a39fd54 13425
c2f47e15 13426 Return NULL_TREE if no simplification was possible, otherwise return the
0a39fd54 13427 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13428 code of the function to be simplified. */
13429
13430static tree
389dd41b 13431fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13432 tree fmt, tree arg, bool ignore,
0a39fd54 13433 enum built_in_function fcode)
13434{
c2f47e15 13435 tree fn_fputc, fn_fputs, call = NULL_TREE;
0a39fd54 13436 const char *fmt_str = NULL;
13437
13438 /* If the return value is used, don't do the transformation. */
13439 if (! ignore)
c2f47e15 13440 return NULL_TREE;
0a39fd54 13441
13442 /* Verify the required arguments in the original call. */
c2f47e15 13443 if (!validate_arg (fp, POINTER_TYPE))
13444 return NULL_TREE;
13445 if (!validate_arg (fmt, POINTER_TYPE))
13446 return NULL_TREE;
0a39fd54 13447
13448 /* Check whether the format is a literal string constant. */
13449 fmt_str = c_getstr (fmt);
13450 if (fmt_str == NULL)
13451 return NULL_TREE;
13452
13453 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13454 {
6d77ed92 13455 /* If we're using an unlocked function, assume the other
13456 unlocked functions exist explicitly. */
b9a16870 13457 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13458 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
0a39fd54 13459 }
13460 else
13461 {
b9a16870 13462 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13463 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
0a39fd54 13464 }
13465
d4473c84 13466 if (!init_target_chars ())
c2f47e15 13467 return NULL_TREE;
a0c938f0 13468
0a39fd54 13469 /* If the format doesn't contain % args or %%, use strcpy. */
99eabcc1 13470 if (strchr (fmt_str, target_percent) == NULL)
0a39fd54 13471 {
13472 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
c2f47e15 13473 && arg)
13474 return NULL_TREE;
0a39fd54 13475
13476 /* If the format specifier was "", fprintf does nothing. */
13477 if (fmt_str[0] == '\0')
13478 {
13479 /* If FP has side-effects, just wait until gimplification is
13480 done. */
13481 if (TREE_SIDE_EFFECTS (fp))
c2f47e15 13482 return NULL_TREE;
0a39fd54 13483
13484 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13485 }
13486
13487 /* When "string" doesn't contain %, replace all cases of
13488 fprintf (fp, string) with fputs (string, fp). The fputs
13489 builtin will take care of special cases like length == 1. */
c2f47e15 13490 if (fn_fputs)
389dd41b 13491 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
0a39fd54 13492 }
13493
13494 /* The other optimizations can be done only on the non-va_list variants. */
13495 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
c2f47e15 13496 return NULL_TREE;
0a39fd54 13497
13498 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
99eabcc1 13499 else if (strcmp (fmt_str, target_percent_s) == 0)
0a39fd54 13500 {
c2f47e15 13501 if (!arg || !validate_arg (arg, POINTER_TYPE))
13502 return NULL_TREE;
13503 if (fn_fputs)
389dd41b 13504 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
0a39fd54 13505 }
13506
13507 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
99eabcc1 13508 else if (strcmp (fmt_str, target_percent_c) == 0)
0a39fd54 13509 {
c2f47e15 13510 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13511 return NULL_TREE;
13512 if (fn_fputc)
389dd41b 13513 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
0a39fd54 13514 }
13515
c2f47e15 13516 if (!call)
13517 return NULL_TREE;
389dd41b 13518 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
0a39fd54 13519}
99eabcc1 13520
13521/* Initialize format string characters in the target charset. */
13522
13523static bool
13524init_target_chars (void)
13525{
13526 static bool init;
13527 if (!init)
13528 {
13529 target_newline = lang_hooks.to_target_charset ('\n');
13530 target_percent = lang_hooks.to_target_charset ('%');
13531 target_c = lang_hooks.to_target_charset ('c');
13532 target_s = lang_hooks.to_target_charset ('s');
13533 if (target_newline == 0 || target_percent == 0 || target_c == 0
13534 || target_s == 0)
13535 return false;
13536
13537 target_percent_c[0] = target_percent;
13538 target_percent_c[1] = target_c;
13539 target_percent_c[2] = '\0';
13540
13541 target_percent_s[0] = target_percent;
13542 target_percent_s[1] = target_s;
13543 target_percent_s[2] = '\0';
13544
13545 target_percent_s_newline[0] = target_percent;
13546 target_percent_s_newline[1] = target_s;
13547 target_percent_s_newline[2] = target_newline;
13548 target_percent_s_newline[3] = '\0';
a0c938f0 13549
99eabcc1 13550 init = true;
13551 }
13552 return true;
13553}
bffb7645 13554
f0c477f2 13555/* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13556 and no overflow/underflow occurred. INEXACT is true if M was not
fa7637bd 13557 exactly calculated. TYPE is the tree type for the result. This
f0c477f2 13558 function assumes that you cleared the MPFR flags and then
13559 calculated M to see if anything subsequently set a flag prior to
13560 entering this function. Return NULL_TREE if any checks fail. */
13561
13562static tree
d4473c84 13563do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
f0c477f2 13564{
13565 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13566 overflow/underflow occurred. If -frounding-math, proceed iff the
13567 result of calling FUNC was exact. */
d4473c84 13568 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
f0c477f2 13569 && (!flag_rounding_math || !inexact))
13570 {
13571 REAL_VALUE_TYPE rr;
13572
66fa16e6 13573 real_from_mpfr (&rr, m, type, GMP_RNDN);
f0c477f2 13574 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13575 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13576 but the mpft_t is not, then we underflowed in the
13577 conversion. */
776a7bab 13578 if (real_isfinite (&rr)
f0c477f2 13579 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13580 {
13581 REAL_VALUE_TYPE rmode;
13582
13583 real_convert (&rmode, TYPE_MODE (type), &rr);
13584 /* Proceed iff the specified mode can hold the value. */
13585 if (real_identical (&rmode, &rr))
13586 return build_real (type, rmode);
13587 }
13588 }
13589 return NULL_TREE;
13590}
13591
239d491a 13592/* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13593 number and no overflow/underflow occurred. INEXACT is true if M
13594 was not exactly calculated. TYPE is the tree type for the result.
13595 This function assumes that you cleared the MPFR flags and then
13596 calculated M to see if anything subsequently set a flag prior to
652d9409 13597 entering this function. Return NULL_TREE if any checks fail, if
13598 FORCE_CONVERT is true, then bypass the checks. */
239d491a 13599
13600static tree
652d9409 13601do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
239d491a 13602{
13603 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13604 overflow/underflow occurred. If -frounding-math, proceed iff the
13605 result of calling FUNC was exact. */
652d9409 13606 if (force_convert
13607 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13608 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13609 && (!flag_rounding_math || !inexact)))
239d491a 13610 {
13611 REAL_VALUE_TYPE re, im;
13612
b0e7c4d4 13613 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13614 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
239d491a 13615 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13616 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13617 but the mpft_t is not, then we underflowed in the
13618 conversion. */
652d9409 13619 if (force_convert
13620 || (real_isfinite (&re) && real_isfinite (&im)
13621 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13622 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
239d491a 13623 {
13624 REAL_VALUE_TYPE re_mode, im_mode;
13625
13626 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13627 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13628 /* Proceed iff the specified mode can hold the value. */
652d9409 13629 if (force_convert
13630 || (real_identical (&re_mode, &re)
13631 && real_identical (&im_mode, &im)))
239d491a 13632 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13633 build_real (TREE_TYPE (type), im_mode));
13634 }
13635 }
13636 return NULL_TREE;
13637}
239d491a 13638
bffb7645 13639/* If argument ARG is a REAL_CST, call the one-argument mpfr function
13640 FUNC on it and return the resulting value as a tree with type TYPE.
728bac60 13641 If MIN and/or MAX are not NULL, then the supplied ARG must be
13642 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13643 acceptable values, otherwise they are not. The mpfr precision is
13644 set to the precision of TYPE. We assume that function FUNC returns
13645 zero if the result could be calculated exactly within the requested
13646 precision. */
bffb7645 13647
13648static tree
728bac60 13649do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13650 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13651 bool inclusive)
bffb7645 13652{
13653 tree result = NULL_TREE;
48e1416a 13654
bffb7645 13655 STRIP_NOPS (arg);
13656
bd7d6fa4 13657 /* To proceed, MPFR must exactly represent the target floating point
13658 format, which only happens when the target base equals two. */
13659 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
f96bd2bf 13660 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
bffb7645 13661 {
f0c477f2 13662 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
bffb7645 13663
776a7bab 13664 if (real_isfinite (ra)
f0c477f2 13665 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13666 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
bffb7645 13667 {
e2eb2b7f 13668 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13669 const int prec = fmt->p;
13670 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
debf9994 13671 int inexact;
bffb7645 13672 mpfr_t m;
13673
13674 mpfr_init2 (m, prec);
66fa16e6 13675 mpfr_from_real (m, ra, GMP_RNDN);
d4473c84 13676 mpfr_clear_flags ();
e2eb2b7f 13677 inexact = func (m, m, rnd);
f0c477f2 13678 result = do_mpfr_ckconv (m, type, inexact);
bffb7645 13679 mpfr_clear (m);
13680 }
13681 }
48e1416a 13682
bffb7645 13683 return result;
13684}
f0c477f2 13685
13686/* If argument ARG is a REAL_CST, call the two-argument mpfr function
13687 FUNC on it and return the resulting value as a tree with type TYPE.
13688 The mpfr precision is set to the precision of TYPE. We assume that
13689 function FUNC returns zero if the result could be calculated
13690 exactly within the requested precision. */
13691
13692static tree
13693do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13694 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13695{
13696 tree result = NULL_TREE;
48e1416a 13697
f0c477f2 13698 STRIP_NOPS (arg1);
13699 STRIP_NOPS (arg2);
13700
bd7d6fa4 13701 /* To proceed, MPFR must exactly represent the target floating point
13702 format, which only happens when the target base equals two. */
13703 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
f96bd2bf 13704 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13705 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
f0c477f2 13706 {
13707 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13708 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13709
776a7bab 13710 if (real_isfinite (ra1) && real_isfinite (ra2))
f0c477f2 13711 {
e2eb2b7f 13712 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13713 const int prec = fmt->p;
13714 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
f0c477f2 13715 int inexact;
13716 mpfr_t m1, m2;
13717
13718 mpfr_inits2 (prec, m1, m2, NULL);
66fa16e6 13719 mpfr_from_real (m1, ra1, GMP_RNDN);
13720 mpfr_from_real (m2, ra2, GMP_RNDN);
d4473c84 13721 mpfr_clear_flags ();
e2eb2b7f 13722 inexact = func (m1, m1, m2, rnd);
f0c477f2 13723 result = do_mpfr_ckconv (m1, type, inexact);
13724 mpfr_clears (m1, m2, NULL);
13725 }
13726 }
48e1416a 13727
f0c477f2 13728 return result;
13729}
d92f994c 13730
9917422b 13731/* If argument ARG is a REAL_CST, call the three-argument mpfr function
13732 FUNC on it and return the resulting value as a tree with type TYPE.
13733 The mpfr precision is set to the precision of TYPE. We assume that
13734 function FUNC returns zero if the result could be calculated
13735 exactly within the requested precision. */
13736
13737static tree
13738do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13739 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13740{
13741 tree result = NULL_TREE;
48e1416a 13742
9917422b 13743 STRIP_NOPS (arg1);
13744 STRIP_NOPS (arg2);
13745 STRIP_NOPS (arg3);
13746
bd7d6fa4 13747 /* To proceed, MPFR must exactly represent the target floating point
13748 format, which only happens when the target base equals two. */
13749 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
f96bd2bf 13750 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13751 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13752 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
9917422b 13753 {
13754 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13755 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13756 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13757
776a7bab 13758 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
9917422b 13759 {
e2eb2b7f 13760 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13761 const int prec = fmt->p;
13762 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
9917422b 13763 int inexact;
13764 mpfr_t m1, m2, m3;
13765
13766 mpfr_inits2 (prec, m1, m2, m3, NULL);
66fa16e6 13767 mpfr_from_real (m1, ra1, GMP_RNDN);
13768 mpfr_from_real (m2, ra2, GMP_RNDN);
13769 mpfr_from_real (m3, ra3, GMP_RNDN);
d4473c84 13770 mpfr_clear_flags ();
e2eb2b7f 13771 inexact = func (m1, m1, m2, m3, rnd);
9917422b 13772 result = do_mpfr_ckconv (m1, type, inexact);
13773 mpfr_clears (m1, m2, m3, NULL);
13774 }
13775 }
48e1416a 13776
9917422b 13777 return result;
13778}
13779
d92f994c 13780/* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13781 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
d735c391 13782 If ARG_SINP and ARG_COSP are NULL then the result is returned
13783 as a complex value.
d92f994c 13784 The type is taken from the type of ARG and is used for setting the
13785 precision of the calculation and results. */
13786
13787static tree
13788do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13789{
bd7d6fa4 13790 tree const type = TREE_TYPE (arg);
d92f994c 13791 tree result = NULL_TREE;
48e1416a 13792
d92f994c 13793 STRIP_NOPS (arg);
48e1416a 13794
bd7d6fa4 13795 /* To proceed, MPFR must exactly represent the target floating point
13796 format, which only happens when the target base equals two. */
13797 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
f96bd2bf 13798 && TREE_CODE (arg) == REAL_CST
13799 && !TREE_OVERFLOW (arg))
d92f994c 13800 {
13801 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13802
776a7bab 13803 if (real_isfinite (ra))
d92f994c 13804 {
e2eb2b7f 13805 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13806 const int prec = fmt->p;
13807 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
d92f994c 13808 tree result_s, result_c;
13809 int inexact;
13810 mpfr_t m, ms, mc;
13811
13812 mpfr_inits2 (prec, m, ms, mc, NULL);
66fa16e6 13813 mpfr_from_real (m, ra, GMP_RNDN);
d4473c84 13814 mpfr_clear_flags ();
e2eb2b7f 13815 inexact = mpfr_sin_cos (ms, mc, m, rnd);
d92f994c 13816 result_s = do_mpfr_ckconv (ms, type, inexact);
13817 result_c = do_mpfr_ckconv (mc, type, inexact);
13818 mpfr_clears (m, ms, mc, NULL);
13819 if (result_s && result_c)
13820 {
d735c391 13821 /* If we are to return in a complex value do so. */
13822 if (!arg_sinp && !arg_cosp)
13823 return build_complex (build_complex_type (type),
13824 result_c, result_s);
13825
d92f994c 13826 /* Dereference the sin/cos pointer arguments. */
13827 arg_sinp = build_fold_indirect_ref (arg_sinp);
13828 arg_cosp = build_fold_indirect_ref (arg_cosp);
13829 /* Proceed if valid pointer type were passed in. */
13830 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13831 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13832 {
13833 /* Set the values. */
41076ef6 13834 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
35cc02b5 13835 result_s);
d92f994c 13836 TREE_SIDE_EFFECTS (result_s) = 1;
41076ef6 13837 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
35cc02b5 13838 result_c);
d92f994c 13839 TREE_SIDE_EFFECTS (result_c) = 1;
13840 /* Combine the assignments into a compound expr. */
13841 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13842 result_s, result_c));
13843 }
13844 }
13845 }
13846 }
13847 return result;
13848}
65dd1378 13849
65dd1378 13850/* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13851 two-argument mpfr order N Bessel function FUNC on them and return
13852 the resulting value as a tree with type TYPE. The mpfr precision
13853 is set to the precision of TYPE. We assume that function FUNC
13854 returns zero if the result could be calculated exactly within the
13855 requested precision. */
13856static tree
13857do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13858 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13859 const REAL_VALUE_TYPE *min, bool inclusive)
13860{
13861 tree result = NULL_TREE;
13862
13863 STRIP_NOPS (arg1);
13864 STRIP_NOPS (arg2);
13865
13866 /* To proceed, MPFR must exactly represent the target floating point
13867 format, which only happens when the target base equals two. */
13868 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13869 && host_integerp (arg1, 0)
13870 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13871 {
13872 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13873 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13874
13875 if (n == (long)n
776a7bab 13876 && real_isfinite (ra)
65dd1378 13877 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13878 {
e2eb2b7f 13879 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13880 const int prec = fmt->p;
13881 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
65dd1378 13882 int inexact;
13883 mpfr_t m;
13884
13885 mpfr_init2 (m, prec);
13886 mpfr_from_real (m, ra, GMP_RNDN);
13887 mpfr_clear_flags ();
e2eb2b7f 13888 inexact = func (m, n, m, rnd);
65dd1378 13889 result = do_mpfr_ckconv (m, type, inexact);
13890 mpfr_clear (m);
13891 }
13892 }
48e1416a 13893
65dd1378 13894 return result;
13895}
e5407ca6 13896
13897/* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13898 the pointer *(ARG_QUO) and return the result. The type is taken
13899 from the type of ARG0 and is used for setting the precision of the
13900 calculation and results. */
13901
13902static tree
13903do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13904{
13905 tree const type = TREE_TYPE (arg0);
13906 tree result = NULL_TREE;
48e1416a 13907
e5407ca6 13908 STRIP_NOPS (arg0);
13909 STRIP_NOPS (arg1);
48e1416a 13910
e5407ca6 13911 /* To proceed, MPFR must exactly represent the target floating point
13912 format, which only happens when the target base equals two. */
13913 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13914 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13915 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13916 {
13917 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13918 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13919
776a7bab 13920 if (real_isfinite (ra0) && real_isfinite (ra1))
e5407ca6 13921 {
e2eb2b7f 13922 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13923 const int prec = fmt->p;
13924 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
e5407ca6 13925 tree result_rem;
13926 long integer_quo;
13927 mpfr_t m0, m1;
13928
13929 mpfr_inits2 (prec, m0, m1, NULL);
13930 mpfr_from_real (m0, ra0, GMP_RNDN);
13931 mpfr_from_real (m1, ra1, GMP_RNDN);
13932 mpfr_clear_flags ();
e2eb2b7f 13933 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
e5407ca6 13934 /* Remquo is independent of the rounding mode, so pass
13935 inexact=0 to do_mpfr_ckconv(). */
13936 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13937 mpfr_clears (m0, m1, NULL);
13938 if (result_rem)
13939 {
13940 /* MPFR calculates quo in the host's long so it may
13941 return more bits in quo than the target int can hold
13942 if sizeof(host long) > sizeof(target int). This can
13943 happen even for native compilers in LP64 mode. In
13944 these cases, modulo the quo value with the largest
13945 number that the target int can hold while leaving one
13946 bit for the sign. */
13947 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13948 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13949
13950 /* Dereference the quo pointer argument. */
13951 arg_quo = build_fold_indirect_ref (arg_quo);
13952 /* Proceed iff a valid pointer type was passed in. */
13953 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13954 {
13955 /* Set the value. */
7002a1c8 13956 tree result_quo
13957 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13958 build_int_cst (TREE_TYPE (arg_quo),
13959 integer_quo));
e5407ca6 13960 TREE_SIDE_EFFECTS (result_quo) = 1;
13961 /* Combine the quo assignment with the rem. */
13962 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13963 result_quo, result_rem));
13964 }
13965 }
13966 }
13967 }
13968 return result;
13969}
e84da7c1 13970
13971/* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13972 resulting value as a tree with type TYPE. The mpfr precision is
13973 set to the precision of TYPE. We assume that this mpfr function
13974 returns zero if the result could be calculated exactly within the
13975 requested precision. In addition, the integer pointer represented
13976 by ARG_SG will be dereferenced and set to the appropriate signgam
13977 (-1,1) value. */
13978
13979static tree
13980do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13981{
13982 tree result = NULL_TREE;
13983
13984 STRIP_NOPS (arg);
48e1416a 13985
e84da7c1 13986 /* To proceed, MPFR must exactly represent the target floating point
13987 format, which only happens when the target base equals two. Also
13988 verify ARG is a constant and that ARG_SG is an int pointer. */
13989 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13990 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13991 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13992 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13993 {
13994 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13995
13996 /* In addition to NaN and Inf, the argument cannot be zero or a
13997 negative integer. */
776a7bab 13998 if (real_isfinite (ra)
e84da7c1 13999 && ra->cl != rvc_zero
14000 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
14001 {
e2eb2b7f 14002 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14003 const int prec = fmt->p;
14004 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
e84da7c1 14005 int inexact, sg;
14006 mpfr_t m;
14007 tree result_lg;
14008
14009 mpfr_init2 (m, prec);
14010 mpfr_from_real (m, ra, GMP_RNDN);
14011 mpfr_clear_flags ();
e2eb2b7f 14012 inexact = mpfr_lgamma (m, &sg, m, rnd);
e84da7c1 14013 result_lg = do_mpfr_ckconv (m, type, inexact);
14014 mpfr_clear (m);
14015 if (result_lg)
14016 {
14017 tree result_sg;
14018
14019 /* Dereference the arg_sg pointer argument. */
14020 arg_sg = build_fold_indirect_ref (arg_sg);
14021 /* Assign the signgam value into *arg_sg. */
14022 result_sg = fold_build2 (MODIFY_EXPR,
14023 TREE_TYPE (arg_sg), arg_sg,
7002a1c8 14024 build_int_cst (TREE_TYPE (arg_sg), sg));
e84da7c1 14025 TREE_SIDE_EFFECTS (result_sg) = 1;
14026 /* Combine the signgam assignment with the lgamma result. */
14027 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14028 result_sg, result_lg));
14029 }
14030 }
14031 }
14032
14033 return result;
14034}
75a70cf9 14035
239d491a 14036/* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14037 function FUNC on it and return the resulting value as a tree with
14038 type TYPE. The mpfr precision is set to the precision of TYPE. We
14039 assume that function FUNC returns zero if the result could be
14040 calculated exactly within the requested precision. */
14041
14042static tree
14043do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14044{
14045 tree result = NULL_TREE;
48e1416a 14046
239d491a 14047 STRIP_NOPS (arg);
14048
14049 /* To proceed, MPFR must exactly represent the target floating point
14050 format, which only happens when the target base equals two. */
14051 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14052 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14053 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14054 {
14055 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14056 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14057
14058 if (real_isfinite (re) && real_isfinite (im))
14059 {
14060 const struct real_format *const fmt =
14061 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14062 const int prec = fmt->p;
14063 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
44d89feb 14064 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
239d491a 14065 int inexact;
14066 mpc_t m;
48e1416a 14067
239d491a 14068 mpc_init2 (m, prec);
c3cfad90 14069 mpfr_from_real (mpc_realref(m), re, rnd);
14070 mpfr_from_real (mpc_imagref(m), im, rnd);
239d491a 14071 mpfr_clear_flags ();
44d89feb 14072 inexact = func (m, m, crnd);
652d9409 14073 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
239d491a 14074 mpc_clear (m);
14075 }
14076 }
14077
14078 return result;
14079}
c699fab8 14080
14081/* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14082 mpc function FUNC on it and return the resulting value as a tree
14083 with type TYPE. The mpfr precision is set to the precision of
14084 TYPE. We assume that function FUNC returns zero if the result
652d9409 14085 could be calculated exactly within the requested precision. If
14086 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14087 in the arguments and/or results. */
c699fab8 14088
63e89698 14089tree
652d9409 14090do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
c699fab8 14091 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14092{
14093 tree result = NULL_TREE;
48e1416a 14094
c699fab8 14095 STRIP_NOPS (arg0);
14096 STRIP_NOPS (arg1);
14097
14098 /* To proceed, MPFR must exactly represent the target floating point
14099 format, which only happens when the target base equals two. */
14100 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14101 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14102 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14103 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14104 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14105 {
14106 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14107 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14108 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14109 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14110
652d9409 14111 if (do_nonfinite
14112 || (real_isfinite (re0) && real_isfinite (im0)
14113 && real_isfinite (re1) && real_isfinite (im1)))
c699fab8 14114 {
14115 const struct real_format *const fmt =
14116 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14117 const int prec = fmt->p;
14118 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14119 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14120 int inexact;
14121 mpc_t m0, m1;
48e1416a 14122
c699fab8 14123 mpc_init2 (m0, prec);
14124 mpc_init2 (m1, prec);
14125 mpfr_from_real (mpc_realref(m0), re0, rnd);
14126 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14127 mpfr_from_real (mpc_realref(m1), re1, rnd);
14128 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14129 mpfr_clear_flags ();
14130 inexact = func (m0, m0, m1, crnd);
652d9409 14131 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
c699fab8 14132 mpc_clear (m0);
14133 mpc_clear (m1);
14134 }
14135 }
14136
14137 return result;
14138}
239d491a 14139
75a70cf9 14140/* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14141 a normal call should be emitted rather than expanding the function
14142 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14143
14144static tree
14145gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14146{
75a70cf9 14147 int nargs = gimple_call_num_args (stmt);
14148
af1409ad 14149 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14150 (nargs > 0
14151 ? gimple_call_arg_ptr (stmt, 0)
14152 : &error_mark_node), fcode);
75a70cf9 14153}
14154
14155/* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14156 a normal call should be emitted rather than expanding the function
14157 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14158 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14159 passed as second argument. */
14160
14161tree
14162gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14163 enum built_in_function fcode)
14164{
af1409ad 14165 int nargs = gimple_call_num_args (stmt);
75a70cf9 14166
af1409ad 14167 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14168 (nargs > 0
14169 ? gimple_call_arg_ptr (stmt, 0)
14170 : &error_mark_node), maxlen, fcode);
75a70cf9 14171}
14172
14173/* Builtins with folding operations that operate on "..." arguments
14174 need special handling; we need to store the arguments in a convenient
14175 data structure before attempting any folding. Fortunately there are
14176 only a few builtins that fall into this category. FNDECL is the
14177 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14178 result of the function call is ignored. */
14179
14180static tree
389dd41b 14181gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14182 bool ignore ATTRIBUTE_UNUSED)
75a70cf9 14183{
14184 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14185 tree ret = NULL_TREE;
14186
14187 switch (fcode)
14188 {
14189 case BUILT_IN_SPRINTF_CHK:
14190 case BUILT_IN_VSPRINTF_CHK:
14191 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14192 break;
14193
14194 case BUILT_IN_SNPRINTF_CHK:
14195 case BUILT_IN_VSNPRINTF_CHK:
14196 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14197
14198 default:
14199 break;
14200 }
14201 if (ret)
14202 {
14203 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14204 TREE_NO_WARNING (ret) = 1;
14205 return ret;
14206 }
14207 return NULL_TREE;
14208}
14209
14210/* A wrapper function for builtin folding that prevents warnings for
14211 "statement without effect" and the like, caused by removing the
14212 call node earlier than the warning is generated. */
14213
14214tree
14215fold_call_stmt (gimple stmt, bool ignore)
14216{
14217 tree ret = NULL_TREE;
14218 tree fndecl = gimple_call_fndecl (stmt);
389dd41b 14219 location_t loc = gimple_location (stmt);
75a70cf9 14220 if (fndecl
14221 && TREE_CODE (fndecl) == FUNCTION_DECL
14222 && DECL_BUILT_IN (fndecl)
14223 && !gimple_call_va_arg_pack_p (stmt))
14224 {
14225 int nargs = gimple_call_num_args (stmt);
9845fb99 14226 tree *args = (nargs > 0
14227 ? gimple_call_arg_ptr (stmt, 0)
14228 : &error_mark_node);
75a70cf9 14229
198622c0 14230 if (avoid_folding_inline_builtin (fndecl))
14231 return NULL_TREE;
75a70cf9 14232 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14233 {
9845fb99 14234 return targetm.fold_builtin (fndecl, nargs, args, ignore);
75a70cf9 14235 }
14236 else
14237 {
14238 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
9845fb99 14239 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
75a70cf9 14240 if (!ret)
14241 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14242 if (ret)
14243 {
14244 /* Propagate location information from original call to
14245 expansion of builtin. Otherwise things like
14246 maybe_emit_chk_warning, that operate on the expansion
14247 of a builtin, will use the wrong location information. */
14248 if (gimple_has_location (stmt))
14249 {
14250 tree realret = ret;
14251 if (TREE_CODE (ret) == NOP_EXPR)
14252 realret = TREE_OPERAND (ret, 0);
14253 if (CAN_HAVE_LOCATION_P (realret)
14254 && !EXPR_HAS_LOCATION (realret))
389dd41b 14255 SET_EXPR_LOCATION (realret, loc);
75a70cf9 14256 return realret;
14257 }
14258 return ret;
14259 }
14260 }
14261 }
14262 return NULL_TREE;
14263}
7bfefa9d 14264
b9a16870 14265/* Look up the function in builtin_decl that corresponds to DECL
7bfefa9d 14266 and set ASMSPEC as its user assembler name. DECL must be a
14267 function decl that declares a builtin. */
14268
14269void
14270set_builtin_user_assembler_name (tree decl, const char *asmspec)
14271{
14272 tree builtin;
14273 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14274 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14275 && asmspec != 0);
14276
b9a16870 14277 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
4d8e0d6d 14278 set_user_assembler_name (builtin, asmspec);
7bfefa9d 14279 switch (DECL_FUNCTION_CODE (decl))
14280 {
14281 case BUILT_IN_MEMCPY:
14282 init_block_move_fn (asmspec);
14283 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14284 break;
14285 case BUILT_IN_MEMSET:
14286 init_block_clear_fn (asmspec);
14287 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14288 break;
14289 case BUILT_IN_MEMMOVE:
14290 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14291 break;
14292 case BUILT_IN_MEMCMP:
14293 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14294 break;
14295 case BUILT_IN_ABORT:
14296 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14297 break;
5a80a58b 14298 case BUILT_IN_FFS:
14299 if (INT_TYPE_SIZE < BITS_PER_WORD)
14300 {
14301 set_user_assembler_libfunc ("ffs", asmspec);
14302 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14303 MODE_INT, 0), "ffs");
14304 }
14305 break;
7bfefa9d 14306 default:
14307 break;
14308 }
14309}
a6b74a67 14310
14311/* Return true if DECL is a builtin that expands to a constant or similarly
14312 simple code. */
14313bool
14314is_simple_builtin (tree decl)
14315{
14316 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14317 switch (DECL_FUNCTION_CODE (decl))
14318 {
14319 /* Builtins that expand to constants. */
14320 case BUILT_IN_CONSTANT_P:
14321 case BUILT_IN_EXPECT:
14322 case BUILT_IN_OBJECT_SIZE:
14323 case BUILT_IN_UNREACHABLE:
14324 /* Simple register moves or loads from stack. */
fca0886c 14325 case BUILT_IN_ASSUME_ALIGNED:
a6b74a67 14326 case BUILT_IN_RETURN_ADDRESS:
14327 case BUILT_IN_EXTRACT_RETURN_ADDR:
14328 case BUILT_IN_FROB_RETURN_ADDR:
14329 case BUILT_IN_RETURN:
14330 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14331 case BUILT_IN_FRAME_ADDRESS:
14332 case BUILT_IN_VA_END:
14333 case BUILT_IN_STACK_SAVE:
14334 case BUILT_IN_STACK_RESTORE:
14335 /* Exception state returns or moves registers around. */
14336 case BUILT_IN_EH_FILTER:
14337 case BUILT_IN_EH_POINTER:
14338 case BUILT_IN_EH_COPY_VALUES:
14339 return true;
14340
14341 default:
14342 return false;
14343 }
14344
14345 return false;
14346}
14347
14348/* Return true if DECL is a builtin that is not expensive, i.e., they are
14349 most probably expanded inline into reasonably simple code. This is a
14350 superset of is_simple_builtin. */
14351bool
14352is_inexpensive_builtin (tree decl)
14353{
14354 if (!decl)
14355 return false;
14356 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14357 return true;
14358 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14359 switch (DECL_FUNCTION_CODE (decl))
14360 {
14361 case BUILT_IN_ABS:
14362 case BUILT_IN_ALLOCA:
581bf1c2 14363 case BUILT_IN_ALLOCA_WITH_ALIGN:
74bdbe96 14364 case BUILT_IN_BSWAP16:
a6b74a67 14365 case BUILT_IN_BSWAP32:
14366 case BUILT_IN_BSWAP64:
14367 case BUILT_IN_CLZ:
14368 case BUILT_IN_CLZIMAX:
14369 case BUILT_IN_CLZL:
14370 case BUILT_IN_CLZLL:
14371 case BUILT_IN_CTZ:
14372 case BUILT_IN_CTZIMAX:
14373 case BUILT_IN_CTZL:
14374 case BUILT_IN_CTZLL:
14375 case BUILT_IN_FFS:
14376 case BUILT_IN_FFSIMAX:
14377 case BUILT_IN_FFSL:
14378 case BUILT_IN_FFSLL:
14379 case BUILT_IN_IMAXABS:
14380 case BUILT_IN_FINITE:
14381 case BUILT_IN_FINITEF:
14382 case BUILT_IN_FINITEL:
14383 case BUILT_IN_FINITED32:
14384 case BUILT_IN_FINITED64:
14385 case BUILT_IN_FINITED128:
14386 case BUILT_IN_FPCLASSIFY:
14387 case BUILT_IN_ISFINITE:
14388 case BUILT_IN_ISINF_SIGN:
14389 case BUILT_IN_ISINF:
14390 case BUILT_IN_ISINFF:
14391 case BUILT_IN_ISINFL:
14392 case BUILT_IN_ISINFD32:
14393 case BUILT_IN_ISINFD64:
14394 case BUILT_IN_ISINFD128:
14395 case BUILT_IN_ISNAN:
14396 case BUILT_IN_ISNANF:
14397 case BUILT_IN_ISNANL:
14398 case BUILT_IN_ISNAND32:
14399 case BUILT_IN_ISNAND64:
14400 case BUILT_IN_ISNAND128:
14401 case BUILT_IN_ISNORMAL:
14402 case BUILT_IN_ISGREATER:
14403 case BUILT_IN_ISGREATEREQUAL:
14404 case BUILT_IN_ISLESS:
14405 case BUILT_IN_ISLESSEQUAL:
14406 case BUILT_IN_ISLESSGREATER:
14407 case BUILT_IN_ISUNORDERED:
14408 case BUILT_IN_VA_ARG_PACK:
14409 case BUILT_IN_VA_ARG_PACK_LEN:
14410 case BUILT_IN_VA_COPY:
14411 case BUILT_IN_TRAP:
14412 case BUILT_IN_SAVEREGS:
14413 case BUILT_IN_POPCOUNTL:
14414 case BUILT_IN_POPCOUNTLL:
14415 case BUILT_IN_POPCOUNTIMAX:
14416 case BUILT_IN_POPCOUNT:
14417 case BUILT_IN_PARITYL:
14418 case BUILT_IN_PARITYLL:
14419 case BUILT_IN_PARITYIMAX:
14420 case BUILT_IN_PARITY:
14421 case BUILT_IN_LABS:
14422 case BUILT_IN_LLABS:
14423 case BUILT_IN_PREFETCH:
14424 return true;
14425
14426 default:
14427 return is_simple_builtin (decl);
14428 }
14429
14430 return false;
14431}