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