]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/builtins.c
2004-09-23 Kelley Cook <kcook@gcc.gnu.org>
[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,
75abb4b6 3 2000, 2001, 2002, 2003, 2004 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
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, 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"
53800dbe 50
51#define CALLED_AS_BUILT_IN(NODE) \
52 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
53
726e2588 54#ifndef PAD_VARARGS_DOWN
55#define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56#endif
57
ab7943b9 58/* Define the names of the builtin function types and codes. */
8934cb0c 59const char *const built_in_class_names[4]
ab7943b9 60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
61
18e43155 62#define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X,
8934cb0c 63const char *const built_in_names[(int) END_BUILTINS] =
4e9d90c7 64{
65#include "builtins.def"
66};
67#undef DEF_BUILTIN
ab7943b9 68
df94cd3b 69/* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
d2d4bdde 71tree built_in_decls[(int) END_BUILTINS];
0a68165a 72/* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
4ee9c684 74 required to implement the function call in all cases). */
0a68165a 75tree implicit_built_in_decls[(int) END_BUILTINS];
df94cd3b 76
aecda0d6 77static int get_pointer_alignment (tree, unsigned int);
aecda0d6 78static const char *c_getstr (tree);
79static rtx c_readstr (const char *, enum machine_mode);
80static int target_char_cast (tree, char *);
81static rtx get_memory_rtx (tree);
19bf118a 82static tree build_string_literal (int, const char *);
aecda0d6 83static int apply_args_size (void);
84static int apply_result_size (void);
d8c9779c 85#if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
aecda0d6 86static rtx result_vector (int, rtx);
d8c9779c 87#endif
aecda0d6 88static rtx expand_builtin_setjmp (tree, rtx);
843d08a9 89static void expand_builtin_update_setjmp_buf (rtx);
aecda0d6 90static void expand_builtin_prefetch (tree);
91static rtx expand_builtin_apply_args (void);
92static rtx expand_builtin_apply_args_1 (void);
93static rtx expand_builtin_apply (rtx, rtx, rtx);
94static void expand_builtin_return (rtx);
95static enum type_class type_to_class (tree);
96static rtx expand_builtin_classify_type (tree);
97static void expand_errno_check (tree, rtx);
98static rtx expand_builtin_mathfn (tree, rtx, rtx);
99static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
6b43bae4 100static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
aecda0d6 101static rtx expand_builtin_args_info (tree);
102static rtx expand_builtin_next_arg (tree);
103static rtx expand_builtin_va_start (tree);
104static rtx expand_builtin_va_end (tree);
105static rtx expand_builtin_va_copy (tree);
106static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110static rtx expand_builtin_strcat (tree, rtx, enum machine_mode);
111static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int);
116static rtx expand_builtin_memmove (tree, rtx, enum machine_mode);
117static rtx expand_builtin_bcopy (tree);
118static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
119static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125static rtx expand_builtin_bzero (tree);
126static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
128static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
129static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
130static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
131static rtx expand_builtin_alloca (tree, rtx);
132static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133static rtx expand_builtin_frame_address (tree, tree);
19bf118a 134static rtx expand_builtin_fputs (tree, rtx, bool);
135static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
6411575e 137static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
aecda0d6 138static tree stabilize_va_list (tree, int);
139static rtx expand_builtin_expect (tree, rtx);
140static tree fold_builtin_constant_p (tree);
141static tree fold_builtin_classify_type (tree);
e6e27594 142static tree fold_builtin_strlen (tree);
aecda0d6 143static tree fold_builtin_inf (tree, int);
144static tree fold_builtin_nan (tree, tree, int);
145static int validate_arglist (tree, ...);
277f8dd2 146static bool integer_valued_real_p (tree);
aecda0d6 147static tree fold_trunc_transparent_mathfn (tree);
148static bool readonly_data_expr (tree);
149static rtx expand_builtin_fabs (tree, rtx, rtx);
27f261ef 150static rtx expand_builtin_signbit (tree, rtx);
7d99bd73 151static tree fold_builtin_cabs (tree, tree);
e6e27594 152static tree fold_builtin_sqrt (tree, tree);
153static tree fold_builtin_cbrt (tree, tree);
154static tree fold_builtin_pow (tree, tree, tree);
155static tree fold_builtin_sin (tree);
156static tree fold_builtin_cos (tree, tree, tree);
157static tree fold_builtin_tan (tree);
ea67c510 158static tree fold_builtin_atan (tree, tree);
277f8dd2 159static tree fold_builtin_trunc (tree);
160static tree fold_builtin_floor (tree);
161static tree fold_builtin_ceil (tree);
89ab3887 162static tree fold_builtin_round (tree);
70fb4c07 163static tree fold_builtin_bitop (tree);
9c8a1629 164static tree fold_builtin_memcpy (tree);
165static tree fold_builtin_mempcpy (tree);
166static tree fold_builtin_memmove (tree);
e739f726 167static tree fold_builtin_strchr (tree, bool);
9c8a1629 168static tree fold_builtin_memcmp (tree);
169static tree fold_builtin_strcmp (tree);
170static tree fold_builtin_strncmp (tree);
27f261ef 171static tree fold_builtin_signbit (tree);
467214fd 172static tree fold_builtin_copysign (tree, tree);
173static tree fold_builtin_isascii (tree);
174static tree fold_builtin_toascii (tree);
175static tree fold_builtin_isdigit (tree);
d1aade50 176static tree fold_builtin_fabs (tree, tree);
177static tree fold_builtin_abs (tree, tree);
859f903a 178static tree fold_builtin_unordered_cmp (tree, enum tree_code, enum tree_code);
f0613857 179static tree fold_builtin_1 (tree, bool);
98b40778 180
4ee9c684 181static tree simplify_builtin_strpbrk (tree);
182static tree simplify_builtin_strstr (tree);
183static tree simplify_builtin_strchr (tree);
184static tree simplify_builtin_strrchr (tree);
185static tree simplify_builtin_strcat (tree);
186static tree simplify_builtin_strncat (tree);
187static tree simplify_builtin_strspn (tree);
188static tree simplify_builtin_strcspn (tree);
189static void simplify_builtin_next_arg (tree);
190static void simplify_builtin_va_start (tree);
191static tree simplify_builtin_sprintf (tree, int);
192
193
53800dbe 194/* Return the alignment in bits of EXP, a pointer valued expression.
195 But don't return more than MAX_ALIGN no matter what.
196 The alignment returned is, by default, the alignment of the thing that
27d0c333 197 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
53800dbe 198
199 Otherwise, look at the expression to see if we can do better, i.e., if the
200 expression is actually pointing at an object whose alignment is tighter. */
201
202static int
aecda0d6 203get_pointer_alignment (tree exp, unsigned int max_align)
53800dbe 204{
27d0c333 205 unsigned int align, inner;
53800dbe 206
207 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
208 return 0;
209
210 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
211 align = MIN (align, max_align);
212
213 while (1)
214 {
215 switch (TREE_CODE (exp))
216 {
217 case NOP_EXPR:
218 case CONVERT_EXPR:
219 case NON_LVALUE_EXPR:
220 exp = TREE_OPERAND (exp, 0);
221 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
222 return align;
325d1c45 223
53800dbe 224 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
225 align = MIN (inner, max_align);
226 break;
227
228 case PLUS_EXPR:
229 /* If sum of pointer + int, restrict our maximum alignment to that
230 imposed by the integer. If not, we can't do any better than
231 ALIGN. */
325d1c45 232 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
53800dbe 233 return align;
234
0994d2ed 235 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
236 & (max_align / BITS_PER_UNIT - 1))
53800dbe 237 != 0)
238 max_align >>= 1;
239
240 exp = TREE_OPERAND (exp, 0);
241 break;
242
243 case ADDR_EXPR:
244 /* See what we are pointing at and look at its alignment. */
245 exp = TREE_OPERAND (exp, 0);
246 if (TREE_CODE (exp) == FUNCTION_DECL)
247 align = FUNCTION_BOUNDARY;
9308e976 248 else if (DECL_P (exp))
53800dbe 249 align = DECL_ALIGN (exp);
250#ifdef CONSTANT_ALIGNMENT
ce45a448 251 else if (CONSTANT_CLASS_P (exp))
53800dbe 252 align = CONSTANT_ALIGNMENT (exp, align);
253#endif
254 return MIN (align, max_align);
255
256 default:
257 return align;
258 }
259 }
260}
261
262/* Compute the length of a C string. TREE_STRING_LENGTH is not the right
263 way, because it could contain a zero byte in the middle.
264 TREE_STRING_LENGTH is the size of the character array, not the string.
265
4172d65e 266 ONLY_VALUE should be nonzero if the result is not going to be emitted
c09841f6 267 into the instruction stream and zero if it is going to be expanded.
4172d65e 268 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
681fab1e 269 is returned, otherwise NULL, since
270 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
271 evaluate the side-effects.
272
902de8ed 273 The value returned is of type `ssizetype'.
274
53800dbe 275 Unfortunately, string_constant can't access the values of const char
276 arrays with initializers, so neither can we do so here. */
277
4ee9c684 278tree
681fab1e 279c_strlen (tree src, int only_value)
53800dbe 280{
281 tree offset_node;
27d0c333 282 HOST_WIDE_INT offset;
283 int max;
44acf429 284 const char *ptr;
53800dbe 285
681fab1e 286 STRIP_NOPS (src);
287 if (TREE_CODE (src) == COND_EXPR
288 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
289 {
290 tree len1, len2;
291
292 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
293 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
0862b7e9 294 if (tree_int_cst_equal (len1, len2))
681fab1e 295 return len1;
296 }
297
298 if (TREE_CODE (src) == COMPOUND_EXPR
299 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
300 return c_strlen (TREE_OPERAND (src, 1), only_value);
301
53800dbe 302 src = string_constant (src, &offset_node);
303 if (src == 0)
304 return 0;
902de8ed 305
83d79705 306 max = TREE_STRING_LENGTH (src) - 1;
53800dbe 307 ptr = TREE_STRING_POINTER (src);
902de8ed 308
53800dbe 309 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
310 {
311 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
312 compute the offset to the following null if we don't know where to
313 start searching for it. */
314 int i;
902de8ed 315
53800dbe 316 for (i = 0; i < max; i++)
317 if (ptr[i] == 0)
318 return 0;
902de8ed 319
53800dbe 320 /* We don't know the starting offset, but we do know that the string
321 has no internal zero bytes. We can assume that the offset falls
322 within the bounds of the string; otherwise, the programmer deserves
323 what he gets. Subtract the offset from the length of the string,
902de8ed 324 and return that. This would perhaps not be valid if we were dealing
325 with named arrays in addition to literal string constants. */
326
327 return size_diffop (size_int (max), offset_node);
53800dbe 328 }
329
330 /* We have a known offset into the string. Start searching there for
27d0c333 331 a null character if we can represent it as a single HOST_WIDE_INT. */
dabc4084 332 if (offset_node == 0)
53800dbe 333 offset = 0;
dabc4084 334 else if (! host_integerp (offset_node, 0))
335 offset = -1;
53800dbe 336 else
27d0c333 337 offset = tree_low_cst (offset_node, 0);
902de8ed 338
53800dbe 339 /* If the offset is known to be out of bounds, warn, and call strlen at
340 runtime. */
341 if (offset < 0 || offset > max)
342 {
343 warning ("offset outside bounds of constant string");
344 return 0;
345 }
902de8ed 346
53800dbe 347 /* Use strlen to search for the first zero byte. Since any strings
348 constructed with build_string will have nulls appended, we win even
349 if we get handed something like (char[4])"abcd".
350
351 Since OFFSET is our starting index into the string, no further
352 calculation is needed. */
902de8ed 353 return ssize_int (strlen (ptr + offset));
53800dbe 354}
355
83d79705 356/* Return a char pointer for a C string if it is a string constant
357 or sum of string constant and integer constant. */
358
359static const char *
aecda0d6 360c_getstr (tree src)
83d79705 361{
362 tree offset_node;
83d79705 363
364 src = string_constant (src, &offset_node);
365 if (src == 0)
366 return 0;
367
8c85fcb7 368 if (offset_node == 0)
369 return TREE_STRING_POINTER (src);
370 else if (!host_integerp (offset_node, 1)
371 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
83d79705 372 return 0;
83d79705 373
8c85fcb7 374 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
83d79705 375}
376
8c85fcb7 377/* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
378 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
ecc318ff 379
6840589f 380static rtx
aecda0d6 381c_readstr (const char *str, enum machine_mode mode)
6840589f 382{
383 HOST_WIDE_INT c[2];
384 HOST_WIDE_INT ch;
385 unsigned int i, j;
386
64db345d 387 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7d3f6cc7 388
6840589f 389 c[0] = 0;
390 c[1] = 0;
391 ch = 1;
392 for (i = 0; i < GET_MODE_SIZE (mode); i++)
393 {
394 j = i;
395 if (WORDS_BIG_ENDIAN)
396 j = GET_MODE_SIZE (mode) - i - 1;
397 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
398 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
399 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
400 j *= BITS_PER_UNIT;
64db345d 401 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
7d3f6cc7 402
6840589f 403 if (ch)
404 ch = (unsigned char) str[i];
405 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
406 }
407 return immed_double_const (c[0], c[1], mode);
408}
409
ecc318ff 410/* Cast a target constant CST to target CHAR and if that value fits into
411 host char type, return zero and put that value into variable pointed by
412 P. */
413
414static int
aecda0d6 415target_char_cast (tree cst, char *p)
ecc318ff 416{
417 unsigned HOST_WIDE_INT val, hostval;
418
27d0c333 419 if (!host_integerp (cst, 1)
ecc318ff 420 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
421 return 1;
422
27d0c333 423 val = tree_low_cst (cst, 1);
ecc318ff 424 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
425 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
426
427 hostval = val;
428 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
429 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
430
431 if (val != hostval)
432 return 1;
433
434 *p = hostval;
435 return 0;
436}
437
4ee9c684 438/* Similar to save_expr, but assumes that arbitrary code is not executed
439 in between the multiple evaluations. In particular, we assume that a
440 non-addressable local variable will not be modified. */
441
442static tree
443builtin_save_expr (tree exp)
444{
445 if (TREE_ADDRESSABLE (exp) == 0
446 && (TREE_CODE (exp) == PARM_DECL
447 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
448 return exp;
449
450 return save_expr (exp);
451}
452
53800dbe 453/* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
454 times to get the address of either a higher stack frame, or a return
455 address located within it (depending on FNDECL_CODE). */
902de8ed 456
53800dbe 457rtx
aecda0d6 458expand_builtin_return_addr (enum built_in_function fndecl_code, int count,
459 rtx tem)
53800dbe 460{
461 int i;
462
463 /* Some machines need special handling before we can access
464 arbitrary frames. For example, on the sparc, we must first flush
465 all register windows to the stack. */
466#ifdef SETUP_FRAME_ADDRESSES
467 if (count > 0)
468 SETUP_FRAME_ADDRESSES ();
469#endif
470
471 /* On the sparc, the return address is not in the frame, it is in a
472 register. There is no way to access it off of the current frame
473 pointer, but it can be accessed off the previous frame pointer by
474 reading the value from the register window save area. */
475#ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
476 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
477 count--;
478#endif
479
480 /* Scan back COUNT frames to the specified frame. */
481 for (i = 0; i < count; i++)
482 {
483 /* Assume the dynamic chain pointer is in the word that the
484 frame address points to, unless otherwise specified. */
485#ifdef DYNAMIC_CHAIN_ADDRESS
486 tem = DYNAMIC_CHAIN_ADDRESS (tem);
487#endif
488 tem = memory_address (Pmode, tem);
83fc1478 489 tem = gen_rtx_MEM (Pmode, tem);
ab6ab77e 490 set_mem_alias_set (tem, get_frame_alias_set ());
83fc1478 491 tem = copy_to_reg (tem);
53800dbe 492 }
493
494 /* For __builtin_frame_address, return what we've got. */
495 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
496 return tem;
497
498 /* For __builtin_return_address, Get the return address from that
499 frame. */
500#ifdef RETURN_ADDR_RTX
501 tem = RETURN_ADDR_RTX (count, tem);
502#else
503 tem = memory_address (Pmode,
504 plus_constant (tem, GET_MODE_SIZE (Pmode)));
505 tem = gen_rtx_MEM (Pmode, tem);
ab6ab77e 506 set_mem_alias_set (tem, get_frame_alias_set ());
53800dbe 507#endif
508 return tem;
509}
510
f7c44134 511/* Alias set used for setjmp buffer. */
512static HOST_WIDE_INT setjmp_alias_set = -1;
513
6b7f6858 514/* Construct the leading half of a __builtin_setjmp call. Control will
515 return to RECEIVER_LABEL. This is used directly by sjlj exception
516 handling code. */
53800dbe 517
6b7f6858 518void
aecda0d6 519expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
53800dbe 520{
53800dbe 521 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
53800dbe 522 rtx stack_save;
f7c44134 523 rtx mem;
53800dbe 524
f7c44134 525 if (setjmp_alias_set == -1)
526 setjmp_alias_set = new_alias_set ();
527
85d654dd 528 buf_addr = convert_memory_address (Pmode, buf_addr);
53800dbe 529
37ae8504 530 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
53800dbe 531
6b7f6858 532 /* We store the frame pointer and the address of receiver_label in
533 the buffer and use the rest of it for the stack save area, which
534 is machine-dependent. */
53800dbe 535
f7c44134 536 mem = gen_rtx_MEM (Pmode, buf_addr);
ab6ab77e 537 set_mem_alias_set (mem, setjmp_alias_set);
e3e026e8 538 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
f7c44134 539
540 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
ab6ab77e 541 set_mem_alias_set (mem, setjmp_alias_set);
f7c44134 542
543 emit_move_insn (validize_mem (mem),
6b7f6858 544 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
53800dbe 545
546 stack_save = gen_rtx_MEM (sa_mode,
547 plus_constant (buf_addr,
548 2 * GET_MODE_SIZE (Pmode)));
ab6ab77e 549 set_mem_alias_set (stack_save, setjmp_alias_set);
53800dbe 550 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
551
552 /* If there is further processing to do, do it. */
553#ifdef HAVE_builtin_setjmp_setup
554 if (HAVE_builtin_setjmp_setup)
555 emit_insn (gen_builtin_setjmp_setup (buf_addr));
556#endif
557
6b7f6858 558 /* Tell optimize_save_area_alloca that extra work is going to
559 need to go on during alloca. */
3b0fa6b6 560 current_function_calls_setjmp = 1;
80ab81b9 561
562 /* Set this so all the registers get saved in our frame; we need to be
2c0e001b 563 able to copy the saved values for any registers from frames we unwind. */
80ab81b9 564 current_function_has_nonlocal_label = 1;
6b7f6858 565}
53800dbe 566
6b7f6858 567/* Construct the trailing part of a __builtin_setjmp call.
568 This is used directly by sjlj exception handling code. */
569
570void
aecda0d6 571expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
6b7f6858 572{
53800dbe 573 /* Clobber the FP when we get here, so we have to make sure it's
574 marked as used by this function. */
575 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
576
577 /* Mark the static chain as clobbered here so life information
578 doesn't get messed up for it. */
579 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
580
581 /* Now put in the code to restore the frame pointer, and argument
491e04ef 582 pointer, if needed. */
53800dbe 583#ifdef HAVE_nonlocal_goto
584 if (! HAVE_nonlocal_goto)
585#endif
586 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
587
588#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
589 if (fixed_regs[ARG_POINTER_REGNUM])
590 {
591#ifdef ELIMINABLE_REGS
592 size_t i;
e99c3a1d 593 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
53800dbe 594
3098b2d3 595 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
53800dbe 596 if (elim_regs[i].from == ARG_POINTER_REGNUM
597 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
598 break;
599
3098b2d3 600 if (i == ARRAY_SIZE (elim_regs))
53800dbe 601#endif
602 {
603 /* Now restore our arg pointer from the address at which it
05927e40 604 was saved in our stack frame. */
53800dbe 605 emit_move_insn (virtual_incoming_args_rtx,
05927e40 606 copy_to_reg (get_arg_pointer_save_area (cfun)));
53800dbe 607 }
608 }
609#endif
610
611#ifdef HAVE_builtin_setjmp_receiver
612 if (HAVE_builtin_setjmp_receiver)
6b7f6858 613 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
53800dbe 614 else
615#endif
616#ifdef HAVE_nonlocal_goto_receiver
617 if (HAVE_nonlocal_goto_receiver)
618 emit_insn (gen_nonlocal_goto_receiver ());
619 else
620#endif
6b7f6858 621 { /* Nothing */ }
57f6bb94 622
623 /* @@@ This is a kludge. Not all machine descriptions define a blockage
624 insn, but we must not allow the code we just generated to be reordered
625 by scheduling. Specifically, the update of the frame pointer must
626 happen immediately, not later. So emit an ASM_INPUT to act as blockage
627 insn. */
628 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
6b7f6858 629}
53800dbe 630
6b7f6858 631/* __builtin_setjmp is passed a pointer to an array of five words (not
632 all will be used on all machines). It operates similarly to the C
633 library function of the same name, but is more efficient. Much of
634 the code below (and for longjmp) is copied from the handling of
635 non-local gotos.
636
637 NOTE: This is intended for use by GNAT and the exception handling
638 scheme in the compiler and will only work in the method used by
639 them. */
640
641static rtx
aecda0d6 642expand_builtin_setjmp (tree arglist, rtx target)
6b7f6858 643{
644 rtx buf_addr, next_lab, cont_lab;
645
0eb671f7 646 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6b7f6858 647 return NULL_RTX;
648
8ad4c111 649 if (target == 0 || !REG_P (target)
6b7f6858 650 || REGNO (target) < FIRST_PSEUDO_REGISTER)
651 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
652
653 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
654
655 next_lab = gen_label_rtx ();
656 cont_lab = gen_label_rtx ();
657
658 expand_builtin_setjmp_setup (buf_addr, next_lab);
659
101a5a9d 660 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
661 ensure that pending stack adjustments are flushed. */
6b7f6858 662 emit_move_insn (target, const0_rtx);
101a5a9d 663 emit_jump (cont_lab);
664
6b7f6858 665 emit_label (next_lab);
666
667 expand_builtin_setjmp_receiver (next_lab);
668
669 /* Set TARGET to one. */
670 emit_move_insn (target, const1_rtx);
671 emit_label (cont_lab);
672
673 /* Tell flow about the strange goings on. Putting `next_lab' on
674 `nonlocal_goto_handler_labels' to indicates that function
675 calls may traverse the arc back to this label. */
676
677 current_function_has_nonlocal_label = 1;
678 nonlocal_goto_handler_labels
679 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
53800dbe 680
681 return target;
682}
683
684/* __builtin_longjmp is passed a pointer to an array of five words (not
685 all will be used on all machines). It operates similarly to the C
686 library function of the same name, but is more efficient. Much of
687 the code below is copied from the handling of non-local gotos.
688
689 NOTE: This is intended for use by GNAT and the exception handling
690 scheme in the compiler and will only work in the method used by
691 them. */
692
693void
aecda0d6 694expand_builtin_longjmp (rtx buf_addr, rtx value)
53800dbe 695{
4712c7d6 696 rtx fp, lab, stack, insn, last;
53800dbe 697 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
698
f7c44134 699 if (setjmp_alias_set == -1)
700 setjmp_alias_set = new_alias_set ();
701
85d654dd 702 buf_addr = convert_memory_address (Pmode, buf_addr);
479e4d5e 703
53800dbe 704 buf_addr = force_reg (Pmode, buf_addr);
705
706 /* We used to store value in static_chain_rtx, but that fails if pointers
707 are smaller than integers. We instead require that the user must pass
708 a second argument of 1, because that is what builtin_setjmp will
709 return. This also makes EH slightly more efficient, since we are no
710 longer copying around a value that we don't care about. */
64db345d 711 gcc_assert (value == const1_rtx);
53800dbe 712
3b0fa6b6 713 current_function_calls_longjmp = 1;
714
4712c7d6 715 last = get_last_insn ();
53800dbe 716#ifdef HAVE_builtin_longjmp
717 if (HAVE_builtin_longjmp)
718 emit_insn (gen_builtin_longjmp (buf_addr));
719 else
720#endif
721 {
722 fp = gen_rtx_MEM (Pmode, buf_addr);
723 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
724 GET_MODE_SIZE (Pmode)));
725
726 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
727 2 * GET_MODE_SIZE (Pmode)));
ab6ab77e 728 set_mem_alias_set (fp, setjmp_alias_set);
729 set_mem_alias_set (lab, setjmp_alias_set);
730 set_mem_alias_set (stack, setjmp_alias_set);
53800dbe 731
732 /* Pick up FP, label, and SP from the block and jump. This code is
733 from expand_goto in stmt.c; see there for detailed comments. */
734#if HAVE_nonlocal_goto
735 if (HAVE_nonlocal_goto)
736 /* We have to pass a value to the nonlocal_goto pattern that will
737 get copied into the static_chain pointer, but it does not matter
738 what that value is, because builtin_setjmp does not use it. */
28d202a8 739 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
53800dbe 740 else
741#endif
742 {
743 lab = copy_to_reg (lab);
744
2a871ad1 745 emit_insn (gen_rtx_CLOBBER (VOIDmode,
746 gen_rtx_MEM (BLKmode,
747 gen_rtx_SCRATCH (VOIDmode))));
748 emit_insn (gen_rtx_CLOBBER (VOIDmode,
749 gen_rtx_MEM (BLKmode,
750 hard_frame_pointer_rtx)));
751
53800dbe 752 emit_move_insn (hard_frame_pointer_rtx, fp);
753 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
754
755 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
756 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
757 emit_indirect_jump (lab);
758 }
759 }
615166bb 760
761 /* Search backwards and mark the jump insn as a non-local goto.
762 Note that this precludes the use of __builtin_longjmp to a
763 __builtin_setjmp target in the same function. However, we've
764 already cautioned the user that these functions are for
765 internal exception handling use only. */
449c0509 766 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
767 {
64db345d 768 gcc_assert (insn != last);
7d3f6cc7 769
6d7dc5b9 770 if (JUMP_P (insn))
449c0509 771 {
772 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
773 REG_NOTES (insn));
774 break;
775 }
6d7dc5b9 776 else if (CALL_P (insn))
9342ee68 777 break;
449c0509 778 }
53800dbe 779}
780
4ee9c684 781/* Expand a call to __builtin_nonlocal_goto. We're passed the target label
782 and the address of the save area. */
783
784static rtx
785expand_builtin_nonlocal_goto (tree arglist)
786{
787 tree t_label, t_save_area;
788 rtx r_label, r_save_area, r_fp, r_sp, insn;
789
790 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
791 return NULL_RTX;
792
793 t_label = TREE_VALUE (arglist);
794 arglist = TREE_CHAIN (arglist);
795 t_save_area = TREE_VALUE (arglist);
796
797 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
3dce56cc 798 r_label = convert_memory_address (Pmode, r_label);
4ee9c684 799 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
3dce56cc 800 r_save_area = convert_memory_address (Pmode, r_save_area);
4ee9c684 801 r_fp = gen_rtx_MEM (Pmode, r_save_area);
802 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
803 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
804
805 current_function_has_nonlocal_goto = 1;
806
807#if HAVE_nonlocal_goto
808 /* ??? We no longer need to pass the static chain value, afaik. */
809 if (HAVE_nonlocal_goto)
810 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
811 else
812#endif
813 {
814 r_label = copy_to_reg (r_label);
815
816 emit_insn (gen_rtx_CLOBBER (VOIDmode,
817 gen_rtx_MEM (BLKmode,
818 gen_rtx_SCRATCH (VOIDmode))));
819
820 emit_insn (gen_rtx_CLOBBER (VOIDmode,
821 gen_rtx_MEM (BLKmode,
822 hard_frame_pointer_rtx)));
491e04ef 823
4ee9c684 824 /* Restore frame pointer for containing function.
825 This sets the actual hard register used for the frame pointer
826 to the location of the function's incoming static chain info.
827 The non-local goto handler will then adjust it to contain the
828 proper value and reload the argument pointer, if needed. */
829 emit_move_insn (hard_frame_pointer_rtx, r_fp);
830 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
491e04ef 831
4ee9c684 832 /* USE of hard_frame_pointer_rtx added for consistency;
833 not clear if really needed. */
834 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
835 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
836 emit_indirect_jump (r_label);
837 }
491e04ef 838
4ee9c684 839 /* Search backwards to the jump insn and mark it as a
840 non-local goto. */
841 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
842 {
6d7dc5b9 843 if (JUMP_P (insn))
4ee9c684 844 {
845 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
846 const0_rtx, REG_NOTES (insn));
847 break;
848 }
6d7dc5b9 849 else if (CALL_P (insn))
4ee9c684 850 break;
851 }
852
853 return const0_rtx;
854}
855
843d08a9 856/* __builtin_update_setjmp_buf is passed a pointer to an array of five words
857 (not all will be used on all machines) that was passed to __builtin_setjmp.
858 It updates the stack pointer in that block to correspond to the current
859 stack pointer. */
860
861static void
862expand_builtin_update_setjmp_buf (rtx buf_addr)
863{
864 enum machine_mode sa_mode = Pmode;
865 rtx stack_save;
866
867
868#ifdef HAVE_save_stack_nonlocal
869 if (HAVE_save_stack_nonlocal)
870 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
871#endif
872#ifdef STACK_SAVEAREA_MODE
873 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
874#endif
875
876 stack_save
877 = gen_rtx_MEM (sa_mode,
878 memory_address
879 (sa_mode,
880 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
881
882#ifdef HAVE_setjmp
883 if (HAVE_setjmp)
884 emit_insn (gen_setjmp ());
885#endif
886
887 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
888}
889
5e3608d8 890/* Expand a call to __builtin_prefetch. For a target that does not support
891 data prefetch, evaluate the memory address argument in case it has side
892 effects. */
893
894static void
aecda0d6 895expand_builtin_prefetch (tree arglist)
5e3608d8 896{
897 tree arg0, arg1, arg2;
898 rtx op0, op1, op2;
899
26a5cadb 900 if (!validate_arglist (arglist, POINTER_TYPE, 0))
901 return;
902
5e3608d8 903 arg0 = TREE_VALUE (arglist);
26a5cadb 904 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
905 zero (read) and argument 2 (locality) defaults to 3 (high degree of
906 locality). */
907 if (TREE_CHAIN (arglist))
908 {
909 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
910 if (TREE_CHAIN (TREE_CHAIN (arglist)))
9342ee68 911 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
26a5cadb 912 else
7016c612 913 arg2 = build_int_cst (NULL_TREE, 3);
26a5cadb 914 }
915 else
916 {
917 arg1 = integer_zero_node;
7016c612 918 arg2 = build_int_cst (NULL_TREE, 3);
26a5cadb 919 }
5e3608d8 920
921 /* Argument 0 is an address. */
922 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
923
924 /* Argument 1 (read/write flag) must be a compile-time constant int. */
925 if (TREE_CODE (arg1) != INTEGER_CST)
926 {
eb586f2c 927 error ("second arg to %<__builtin_prefetch%> must be a constant");
9342ee68 928 arg1 = integer_zero_node;
5e3608d8 929 }
bf8e3599 930 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5e3608d8 931 /* Argument 1 must be either zero or one. */
932 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
933 {
934 warning ("invalid second arg to __builtin_prefetch; using zero");
935 op1 = const0_rtx;
936 }
937
938 /* Argument 2 (locality) must be a compile-time constant int. */
939 if (TREE_CODE (arg2) != INTEGER_CST)
940 {
eb586f2c 941 error ("third arg to %<__builtin_prefetch%> must be a constant");
5e3608d8 942 arg2 = integer_zero_node;
943 }
bf8e3599 944 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5e3608d8 945 /* Argument 2 must be 0, 1, 2, or 3. */
946 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
947 {
948 warning ("invalid third arg to __builtin_prefetch; using zero");
949 op2 = const0_rtx;
950 }
951
952#ifdef HAVE_prefetch
953 if (HAVE_prefetch)
954 {
f0ce3b1f 955 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
4cd21437 956 (op0,
f0ce3b1f 957 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
19bf118a 958 || (GET_MODE (op0) != Pmode))
9342ee68 959 {
85d654dd 960 op0 = convert_memory_address (Pmode, op0);
9342ee68 961 op0 = force_reg (Pmode, op0);
962 }
5e3608d8 963 emit_insn (gen_prefetch (op0, op1, op2));
964 }
5e3608d8 965#endif
0a534ba7 966
f0ce3b1f 967 /* Don't do anything with direct references to volatile memory, but
968 generate code to handle other side effects. */
e16ceb8e 969 if (!MEM_P (op0) && side_effects_p (op0))
f0ce3b1f 970 emit_insn (op0);
5e3608d8 971}
972
f7c44134 973/* Get a MEM rtx for expression EXP which is the address of an operand
008c057d 974 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
f7c44134 975
53800dbe 976static rtx
aecda0d6 977get_memory_rtx (tree exp)
53800dbe 978{
726ec87c 979 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM);
980 rtx mem;
f7c44134 981
85d654dd 982 addr = convert_memory_address (Pmode, addr);
726ec87c 983
984 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
2a631e19 985
f7c44134 986 /* Get an expression we can use to find the attributes to assign to MEM.
987 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
988 we can. First remove any nops. */
989 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
f0ce3b1f 990 || TREE_CODE (exp) == NON_LVALUE_EXPR)
f7c44134 991 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
992 exp = TREE_OPERAND (exp, 0);
993
994 if (TREE_CODE (exp) == ADDR_EXPR)
eec8e941 995 {
996 exp = TREE_OPERAND (exp, 0);
997 set_mem_attributes (mem, exp, 0);
998 }
f7c44134 999 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
eec8e941 1000 {
1001 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1002 /* memcpy, memset and other builtin stringops can alias with anything. */
1003 set_mem_alias_set (mem, 0);
1004 }
53800dbe 1005
53800dbe 1006 return mem;
1007}
1008\f
1009/* Built-in functions to perform an untyped call and return. */
1010
1011/* For each register that may be used for calling a function, this
1012 gives a mode used to copy the register's value. VOIDmode indicates
1013 the register is not used for calling a function. If the machine
1014 has register windows, this gives only the outbound registers.
1015 INCOMING_REGNO gives the corresponding inbound register. */
1016static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1017
1018/* For each register that may be used for returning values, this gives
1019 a mode used to copy the register's value. VOIDmode indicates the
1020 register is not used for returning values. If the machine has
1021 register windows, this gives only the outbound registers.
1022 INCOMING_REGNO gives the corresponding inbound register. */
1023static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1024
1025/* For each register that may be used for calling a function, this
1026 gives the offset of that register into the block returned by
1027 __builtin_apply_args. 0 indicates that the register is not
1028 used for calling a function. */
1029static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1030
53800dbe 1031/* Return the size required for the block returned by __builtin_apply_args,
1032 and initialize apply_args_mode. */
1033
1034static int
aecda0d6 1035apply_args_size (void)
53800dbe 1036{
1037 static int size = -1;
58e9ce8f 1038 int align;
1039 unsigned int regno;
53800dbe 1040 enum machine_mode mode;
1041
1042 /* The values computed by this function never change. */
1043 if (size < 0)
1044 {
1045 /* The first value is the incoming arg-pointer. */
1046 size = GET_MODE_SIZE (Pmode);
1047
1048 /* The second value is the structure value address unless this is
1049 passed as an "invisible" first argument. */
6812c89e 1050 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
53800dbe 1051 size += GET_MODE_SIZE (Pmode);
1052
1053 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1054 if (FUNCTION_ARG_REGNO_P (regno))
1055 {
0862b7e9 1056 mode = reg_raw_mode[regno];
1057
64db345d 1058 gcc_assert (mode != VOIDmode);
53800dbe 1059
1060 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1061 if (size % align != 0)
1062 size = CEIL (size, align) * align;
1063 apply_args_reg_offset[regno] = size;
1064 size += GET_MODE_SIZE (mode);
1065 apply_args_mode[regno] = mode;
1066 }
1067 else
1068 {
1069 apply_args_mode[regno] = VOIDmode;
1070 apply_args_reg_offset[regno] = 0;
1071 }
1072 }
1073 return size;
1074}
1075
1076/* Return the size required for the block returned by __builtin_apply,
1077 and initialize apply_result_mode. */
1078
1079static int
aecda0d6 1080apply_result_size (void)
53800dbe 1081{
1082 static int size = -1;
1083 int align, regno;
1084 enum machine_mode mode;
1085
1086 /* The values computed by this function never change. */
1087 if (size < 0)
1088 {
1089 size = 0;
1090
1091 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1092 if (FUNCTION_VALUE_REGNO_P (regno))
1093 {
0862b7e9 1094 mode = reg_raw_mode[regno];
1095
64db345d 1096 gcc_assert (mode != VOIDmode);
53800dbe 1097
1098 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1099 if (size % align != 0)
1100 size = CEIL (size, align) * align;
1101 size += GET_MODE_SIZE (mode);
1102 apply_result_mode[regno] = mode;
1103 }
1104 else
1105 apply_result_mode[regno] = VOIDmode;
1106
1107 /* Allow targets that use untyped_call and untyped_return to override
1108 the size so that machine-specific information can be stored here. */
1109#ifdef APPLY_RESULT_SIZE
1110 size = APPLY_RESULT_SIZE;
1111#endif
1112 }
1113 return size;
1114}
1115
1116#if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1117/* Create a vector describing the result block RESULT. If SAVEP is true,
1118 the result block is used to save the values; otherwise it is used to
1119 restore the values. */
1120
1121static rtx
aecda0d6 1122result_vector (int savep, rtx result)
53800dbe 1123{
1124 int regno, size, align, nelts;
1125 enum machine_mode mode;
1126 rtx reg, mem;
f0af5a88 1127 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
bf8e3599 1128
53800dbe 1129 size = nelts = 0;
1130 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1131 if ((mode = apply_result_mode[regno]) != VOIDmode)
1132 {
1133 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1134 if (size % align != 0)
1135 size = CEIL (size, align) * align;
1136 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
e513d163 1137 mem = adjust_address (result, mode, size);
53800dbe 1138 savevec[nelts++] = (savep
1139 ? gen_rtx_SET (VOIDmode, mem, reg)
1140 : gen_rtx_SET (VOIDmode, reg, mem));
1141 size += GET_MODE_SIZE (mode);
1142 }
1143 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1144}
1145#endif /* HAVE_untyped_call or HAVE_untyped_return */
1146
1147/* Save the state required to perform an untyped call with the same
1148 arguments as were passed to the current function. */
1149
1150static rtx
aecda0d6 1151expand_builtin_apply_args_1 (void)
53800dbe 1152{
1c7e61a7 1153 rtx registers, tem;
53800dbe 1154 int size, align, regno;
1155 enum machine_mode mode;
6812c89e 1156 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
53800dbe 1157
1158 /* Create a block where the arg-pointer, structure value address,
1159 and argument registers can be saved. */
1160 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1161
1162 /* Walk past the arg-pointer and structure value address. */
1163 size = GET_MODE_SIZE (Pmode);
6812c89e 1164 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
53800dbe 1165 size += GET_MODE_SIZE (Pmode);
1166
1167 /* Save each register used in calling a function to the block. */
1168 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1169 if ((mode = apply_args_mode[regno]) != VOIDmode)
1170 {
53800dbe 1171 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1172 if (size % align != 0)
1173 size = CEIL (size, align) * align;
1174
1175 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1176
e513d163 1177 emit_move_insn (adjust_address (registers, mode, size), tem);
53800dbe 1178 size += GET_MODE_SIZE (mode);
1179 }
1180
1181 /* Save the arg pointer to the block. */
1c7e61a7 1182 tem = copy_to_reg (virtual_incoming_args_rtx);
f083a92b 1183#ifdef STACK_GROWS_DOWNWARD
1c7e61a7 1184 /* We need the pointer as the caller actually passed them to us, not
9d4b544c 1185 as we might have pretended they were passed. Make sure it's a valid
1186 operand, as emit_move_insn isn't expected to handle a PLUS. */
1187 tem
1188 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1189 NULL_RTX);
1c7e61a7 1190#endif
1191 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
0862b7e9 1192
53800dbe 1193 size = GET_MODE_SIZE (Pmode);
1194
1195 /* Save the structure value address unless this is passed as an
1196 "invisible" first argument. */
45550790 1197 if (struct_incoming_value)
53800dbe 1198 {
e513d163 1199 emit_move_insn (adjust_address (registers, Pmode, size),
45550790 1200 copy_to_reg (struct_incoming_value));
53800dbe 1201 size += GET_MODE_SIZE (Pmode);
1202 }
1203
1204 /* Return the address of the block. */
1205 return copy_addr_to_reg (XEXP (registers, 0));
1206}
1207
1208/* __builtin_apply_args returns block of memory allocated on
1209 the stack into which is stored the arg pointer, structure
1210 value address, static chain, and all the registers that might
1211 possibly be used in performing a function call. The code is
1212 moved to the start of the function so the incoming values are
1213 saved. */
27d0c333 1214
53800dbe 1215static rtx
aecda0d6 1216expand_builtin_apply_args (void)
53800dbe 1217{
1218 /* Don't do __builtin_apply_args more than once in a function.
1219 Save the result of the first call and reuse it. */
1220 if (apply_args_value != 0)
1221 return apply_args_value;
1222 {
1223 /* When this function is called, it means that registers must be
1224 saved on entry to this function. So we migrate the
1225 call to the first insn of this function. */
1226 rtx temp;
1227 rtx seq;
1228
1229 start_sequence ();
1230 temp = expand_builtin_apply_args_1 ();
1231 seq = get_insns ();
1232 end_sequence ();
1233
1234 apply_args_value = temp;
1235
31d3e01c 1236 /* Put the insns after the NOTE that starts the function.
1237 If this is inside a start_sequence, make the outer-level insn
53800dbe 1238 chain current, so the code is placed at the start of the
1239 function. */
1240 push_topmost_sequence ();
0ec80471 1241 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
53800dbe 1242 pop_topmost_sequence ();
1243 return temp;
1244 }
1245}
1246
1247/* Perform an untyped call and save the state required to perform an
1248 untyped return of whatever value was returned by the given function. */
1249
1250static rtx
aecda0d6 1251expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
53800dbe 1252{
1253 int size, align, regno;
1254 enum machine_mode mode;
2a631e19 1255 rtx incoming_args, result, reg, dest, src, call_insn;
53800dbe 1256 rtx old_stack_level = 0;
1257 rtx call_fusage = 0;
6812c89e 1258 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
53800dbe 1259
85d654dd 1260 arguments = convert_memory_address (Pmode, arguments);
726ec87c 1261
53800dbe 1262 /* Create a block where the return registers can be saved. */
1263 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1264
53800dbe 1265 /* Fetch the arg pointer from the ARGUMENTS block. */
1266 incoming_args = gen_reg_rtx (Pmode);
726ec87c 1267 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
53800dbe 1268#ifndef STACK_GROWS_DOWNWARD
ad99e708 1269 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1270 incoming_args, 0, OPTAB_LIB_WIDEN);
53800dbe 1271#endif
1272
04a46d40 1273 /* Push a new argument block and copy the arguments. Do not allow
1274 the (potential) memcpy call below to interfere with our stack
1275 manipulations. */
53800dbe 1276 do_pending_stack_adjust ();
04a46d40 1277 NO_DEFER_POP;
53800dbe 1278
2358393e 1279 /* Save the stack with nonlocal if available. */
53800dbe 1280#ifdef HAVE_save_stack_nonlocal
1281 if (HAVE_save_stack_nonlocal)
1282 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1283 else
1284#endif
1285 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1286
59647703 1287 /* Allocate a block of memory onto the stack and copy the memory
1288 arguments to the outgoing arguments address. */
1289 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1290 dest = virtual_outgoing_args_rtx;
1291#ifndef STACK_GROWS_DOWNWARD
1292 if (GET_CODE (argsize) == CONST_INT)
1293 dest = plus_constant (dest, -INTVAL (argsize));
1294 else
1295 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1296#endif
2a631e19 1297 dest = gen_rtx_MEM (BLKmode, dest);
1298 set_mem_align (dest, PARM_BOUNDARY);
1299 src = gen_rtx_MEM (BLKmode, incoming_args);
1300 set_mem_align (src, PARM_BOUNDARY);
0378dbdc 1301 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
53800dbe 1302
1303 /* Refer to the argument block. */
1304 apply_args_size ();
1305 arguments = gen_rtx_MEM (BLKmode, arguments);
2a631e19 1306 set_mem_align (arguments, PARM_BOUNDARY);
53800dbe 1307
1308 /* Walk past the arg-pointer and structure value address. */
1309 size = GET_MODE_SIZE (Pmode);
45550790 1310 if (struct_value)
53800dbe 1311 size += GET_MODE_SIZE (Pmode);
1312
1313 /* Restore each of the registers previously saved. Make USE insns
1314 for each of these registers for use in making the call. */
1315 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1316 if ((mode = apply_args_mode[regno]) != VOIDmode)
1317 {
1318 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1319 if (size % align != 0)
1320 size = CEIL (size, align) * align;
1321 reg = gen_rtx_REG (mode, regno);
e513d163 1322 emit_move_insn (reg, adjust_address (arguments, mode, size));
53800dbe 1323 use_reg (&call_fusage, reg);
1324 size += GET_MODE_SIZE (mode);
1325 }
1326
1327 /* Restore the structure value address unless this is passed as an
1328 "invisible" first argument. */
1329 size = GET_MODE_SIZE (Pmode);
45550790 1330 if (struct_value)
53800dbe 1331 {
1332 rtx value = gen_reg_rtx (Pmode);
e513d163 1333 emit_move_insn (value, adjust_address (arguments, Pmode, size));
45550790 1334 emit_move_insn (struct_value, value);
8ad4c111 1335 if (REG_P (struct_value))
45550790 1336 use_reg (&call_fusage, struct_value);
53800dbe 1337 size += GET_MODE_SIZE (Pmode);
1338 }
1339
1340 /* All arguments and registers used for the call are set up by now! */
4ee9c684 1341 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
53800dbe 1342
1343 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1344 and we don't want to load it into a register as an optimization,
1345 because prepare_call_address already did it if it should be done. */
1346 if (GET_CODE (function) != SYMBOL_REF)
1347 function = memory_address (FUNCTION_MODE, function);
1348
1349 /* Generate the actual call instruction and save the return value. */
1350#ifdef HAVE_untyped_call
1351 if (HAVE_untyped_call)
1352 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1353 result, result_vector (1, result)));
1354 else
1355#endif
1356#ifdef HAVE_call_value
1357 if (HAVE_call_value)
1358 {
1359 rtx valreg = 0;
1360
1361 /* Locate the unique return register. It is not possible to
1362 express a call that sets more than one return register using
1363 call_value; use untyped_call for that. In fact, untyped_call
1364 only needs to save the return registers in the given block. */
1365 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1366 if ((mode = apply_result_mode[regno]) != VOIDmode)
1367 {
64db345d 1368 gcc_assert (!valreg); /* HAVE_untyped_call required. */
7d3f6cc7 1369
53800dbe 1370 valreg = gen_rtx_REG (mode, regno);
1371 }
1372
2ed6c343 1373 emit_call_insn (GEN_CALL_VALUE (valreg,
53800dbe 1374 gen_rtx_MEM (FUNCTION_MODE, function),
1375 const0_rtx, NULL_RTX, const0_rtx));
1376
e513d163 1377 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
53800dbe 1378 }
1379 else
1380#endif
64db345d 1381 gcc_unreachable ();
53800dbe 1382
d5f9786f 1383 /* Find the CALL insn we just emitted, and attach the register usage
1384 information. */
1385 call_insn = last_call_insn ();
1386 add_function_usage_to (call_insn, call_fusage);
53800dbe 1387
1388 /* Restore the stack. */
1389#ifdef HAVE_save_stack_nonlocal
1390 if (HAVE_save_stack_nonlocal)
1391 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1392 else
1393#endif
1394 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1395
04a46d40 1396 OK_DEFER_POP;
1397
53800dbe 1398 /* Return the address of the result block. */
85d654dd 1399 result = copy_addr_to_reg (XEXP (result, 0));
1400 return convert_memory_address (ptr_mode, result);
53800dbe 1401}
1402
1403/* Perform an untyped return. */
1404
1405static void
aecda0d6 1406expand_builtin_return (rtx result)
53800dbe 1407{
1408 int size, align, regno;
1409 enum machine_mode mode;
1410 rtx reg;
1411 rtx call_fusage = 0;
1412
85d654dd 1413 result = convert_memory_address (Pmode, result);
726ec87c 1414
53800dbe 1415 apply_result_size ();
1416 result = gen_rtx_MEM (BLKmode, result);
1417
1418#ifdef HAVE_untyped_return
1419 if (HAVE_untyped_return)
1420 {
1421 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1422 emit_barrier ();
1423 return;
1424 }
1425#endif
1426
1427 /* Restore the return value and note that each value is used. */
1428 size = 0;
1429 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1430 if ((mode = apply_result_mode[regno]) != VOIDmode)
1431 {
1432 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1433 if (size % align != 0)
1434 size = CEIL (size, align) * align;
1435 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
e513d163 1436 emit_move_insn (reg, adjust_address (result, mode, size));
53800dbe 1437
1438 push_to_sequence (call_fusage);
1439 emit_insn (gen_rtx_USE (VOIDmode, reg));
1440 call_fusage = get_insns ();
1441 end_sequence ();
1442 size += GET_MODE_SIZE (mode);
1443 }
1444
1445 /* Put the USE insns before the return. */
31d3e01c 1446 emit_insn (call_fusage);
53800dbe 1447
1448 /* Return whatever values was restored by jumping directly to the end
1449 of the function. */
62380d2d 1450 expand_naked_return ();
53800dbe 1451}
1452
539a3a92 1453/* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
27d0c333 1454
539a3a92 1455static enum type_class
aecda0d6 1456type_to_class (tree type)
539a3a92 1457{
1458 switch (TREE_CODE (type))
1459 {
1460 case VOID_TYPE: return void_type_class;
1461 case INTEGER_TYPE: return integer_type_class;
1462 case CHAR_TYPE: return char_type_class;
1463 case ENUMERAL_TYPE: return enumeral_type_class;
1464 case BOOLEAN_TYPE: return boolean_type_class;
1465 case POINTER_TYPE: return pointer_type_class;
1466 case REFERENCE_TYPE: return reference_type_class;
1467 case OFFSET_TYPE: return offset_type_class;
1468 case REAL_TYPE: return real_type_class;
1469 case COMPLEX_TYPE: return complex_type_class;
1470 case FUNCTION_TYPE: return function_type_class;
1471 case METHOD_TYPE: return method_type_class;
1472 case RECORD_TYPE: return record_type_class;
1473 case UNION_TYPE:
1474 case QUAL_UNION_TYPE: return union_type_class;
1475 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1476 ? string_type_class : array_type_class);
1477 case SET_TYPE: return set_type_class;
1478 case FILE_TYPE: return file_type_class;
1479 case LANG_TYPE: return lang_type_class;
1480 default: return no_type_class;
1481 }
1482}
bf8e3599 1483
53800dbe 1484/* Expand a call to __builtin_classify_type with arguments found in
1485 ARGLIST. */
27d0c333 1486
53800dbe 1487static rtx
aecda0d6 1488expand_builtin_classify_type (tree arglist)
53800dbe 1489{
1490 if (arglist != 0)
539a3a92 1491 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
53800dbe 1492 return GEN_INT (no_type_class);
1493}
1494
07976da7 1495/* This helper macro, meant to be used in mathfn_built_in below,
1496 determines which among a set of three builtin math functions is
1497 appropriate for a given type mode. The `F' and `L' cases are
1498 automatically generated from the `double' case. */
1499#define CASE_MATHFN(BUILT_IN_MATHFN) \
1500 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1501 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1502 fcodel = BUILT_IN_MATHFN##L ; break;
1503
1504/* Return mathematic function equivalent to FN but operating directly
1505 on TYPE, if available. If we can't do the conversion, return zero. */
0a68165a 1506tree
aecda0d6 1507mathfn_built_in (tree type, enum built_in_function fn)
0a68165a 1508{
07976da7 1509 enum built_in_function fcode, fcodef, fcodel;
1510
1511 switch (fn)
1512 {
746114e8 1513 CASE_MATHFN (BUILT_IN_ACOS)
1514 CASE_MATHFN (BUILT_IN_ACOSH)
1515 CASE_MATHFN (BUILT_IN_ASIN)
1516 CASE_MATHFN (BUILT_IN_ASINH)
07976da7 1517 CASE_MATHFN (BUILT_IN_ATAN)
746114e8 1518 CASE_MATHFN (BUILT_IN_ATAN2)
1519 CASE_MATHFN (BUILT_IN_ATANH)
1520 CASE_MATHFN (BUILT_IN_CBRT)
07976da7 1521 CASE_MATHFN (BUILT_IN_CEIL)
746114e8 1522 CASE_MATHFN (BUILT_IN_COPYSIGN)
07976da7 1523 CASE_MATHFN (BUILT_IN_COS)
746114e8 1524 CASE_MATHFN (BUILT_IN_COSH)
1525 CASE_MATHFN (BUILT_IN_DREM)
1526 CASE_MATHFN (BUILT_IN_ERF)
1527 CASE_MATHFN (BUILT_IN_ERFC)
07976da7 1528 CASE_MATHFN (BUILT_IN_EXP)
746114e8 1529 CASE_MATHFN (BUILT_IN_EXP10)
1530 CASE_MATHFN (BUILT_IN_EXP2)
1531 CASE_MATHFN (BUILT_IN_EXPM1)
1532 CASE_MATHFN (BUILT_IN_FABS)
1533 CASE_MATHFN (BUILT_IN_FDIM)
07976da7 1534 CASE_MATHFN (BUILT_IN_FLOOR)
746114e8 1535 CASE_MATHFN (BUILT_IN_FMA)
1536 CASE_MATHFN (BUILT_IN_FMAX)
1537 CASE_MATHFN (BUILT_IN_FMIN)
1538 CASE_MATHFN (BUILT_IN_FMOD)
1539 CASE_MATHFN (BUILT_IN_FREXP)
1540 CASE_MATHFN (BUILT_IN_GAMMA)
1541 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1542 CASE_MATHFN (BUILT_IN_HYPOT)
1543 CASE_MATHFN (BUILT_IN_ILOGB)
1544 CASE_MATHFN (BUILT_IN_INF)
1545 CASE_MATHFN (BUILT_IN_J0)
1546 CASE_MATHFN (BUILT_IN_J1)
1547 CASE_MATHFN (BUILT_IN_JN)
1548 CASE_MATHFN (BUILT_IN_LDEXP)
1549 CASE_MATHFN (BUILT_IN_LGAMMA)
1550 CASE_MATHFN (BUILT_IN_LLRINT)
1551 CASE_MATHFN (BUILT_IN_LLROUND)
07976da7 1552 CASE_MATHFN (BUILT_IN_LOG)
746114e8 1553 CASE_MATHFN (BUILT_IN_LOG10)
1554 CASE_MATHFN (BUILT_IN_LOG1P)
1555 CASE_MATHFN (BUILT_IN_LOG2)
1556 CASE_MATHFN (BUILT_IN_LOGB)
1557 CASE_MATHFN (BUILT_IN_LRINT)
1558 CASE_MATHFN (BUILT_IN_LROUND)
1559 CASE_MATHFN (BUILT_IN_MODF)
1560 CASE_MATHFN (BUILT_IN_NAN)
1561 CASE_MATHFN (BUILT_IN_NANS)
07976da7 1562 CASE_MATHFN (BUILT_IN_NEARBYINT)
746114e8 1563 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1564 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1565 CASE_MATHFN (BUILT_IN_POW)
1566 CASE_MATHFN (BUILT_IN_POW10)
1567 CASE_MATHFN (BUILT_IN_REMAINDER)
1568 CASE_MATHFN (BUILT_IN_REMQUO)
1569 CASE_MATHFN (BUILT_IN_RINT)
07976da7 1570 CASE_MATHFN (BUILT_IN_ROUND)
746114e8 1571 CASE_MATHFN (BUILT_IN_SCALB)
1572 CASE_MATHFN (BUILT_IN_SCALBLN)
1573 CASE_MATHFN (BUILT_IN_SCALBN)
1574 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
07976da7 1575 CASE_MATHFN (BUILT_IN_SIN)
746114e8 1576 CASE_MATHFN (BUILT_IN_SINCOS)
1577 CASE_MATHFN (BUILT_IN_SINH)
07976da7 1578 CASE_MATHFN (BUILT_IN_SQRT)
1579 CASE_MATHFN (BUILT_IN_TAN)
746114e8 1580 CASE_MATHFN (BUILT_IN_TANH)
1581 CASE_MATHFN (BUILT_IN_TGAMMA)
07976da7 1582 CASE_MATHFN (BUILT_IN_TRUNC)
746114e8 1583 CASE_MATHFN (BUILT_IN_Y0)
1584 CASE_MATHFN (BUILT_IN_Y1)
1585 CASE_MATHFN (BUILT_IN_YN)
07976da7 1586
0a68165a 1587 default:
07976da7 1588 return 0;
0a68165a 1589 }
07976da7 1590
96b9f485 1591 if (TYPE_MAIN_VARIANT (type) == double_type_node)
07976da7 1592 return implicit_built_in_decls[fcode];
96b9f485 1593 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
07976da7 1594 return implicit_built_in_decls[fcodef];
96b9f485 1595 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
07976da7 1596 return implicit_built_in_decls[fcodel];
1597 else
1598 return 0;
0a68165a 1599}
1600
0fd605a5 1601/* If errno must be maintained, expand the RTL to check if the result,
1602 TARGET, of a built-in function call, EXP, is NaN, and if so set
1603 errno to EDOM. */
1604
1605static void
aecda0d6 1606expand_errno_check (tree exp, rtx target)
0fd605a5 1607{
7f05340e 1608 rtx lab = gen_label_rtx ();
0fd605a5 1609
7f05340e 1610 /* Test the result; if it is NaN, set errno=EDOM because
1611 the argument was not in the domain. */
1612 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1613 0, lab);
0fd605a5 1614
1615#ifdef TARGET_EDOM
7f05340e 1616 /* If this built-in doesn't throw an exception, set errno directly. */
1617 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1618 {
0fd605a5 1619#ifdef GEN_ERRNO_RTX
7f05340e 1620 rtx errno_rtx = GEN_ERRNO_RTX;
0fd605a5 1621#else
7f05340e 1622 rtx errno_rtx
0fd605a5 1623 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1624#endif
7f05340e 1625 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
0fd605a5 1626 emit_label (lab);
7f05340e 1627 return;
0fd605a5 1628 }
7f05340e 1629#endif
1630
1631 /* We can't set errno=EDOM directly; let the library call do it.
1632 Pop the arguments right away in case the call gets deleted. */
1633 NO_DEFER_POP;
1634 expand_call (exp, target, 0);
1635 OK_DEFER_POP;
1636 emit_label (lab);
0fd605a5 1637}
1638
1639
6b43bae4 1640/* Expand a call to one of the builtin math functions (sqrt, exp, or log).
53800dbe 1641 Return 0 if a normal call should be emitted rather than expanding the
1642 function in-line. EXP is the expression that is a call to the builtin
1643 function; if convenient, the result should be placed in TARGET.
1644 SUBTARGET may be used as the target for computing one of EXP's operands. */
27d0c333 1645
53800dbe 1646static rtx
aecda0d6 1647expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
53800dbe 1648{
bf8e3599 1649 optab builtin_optab;
bd421108 1650 rtx op0, insns, before_call;
c6e6ecb1 1651 tree fndecl = get_callee_fndecl (exp);
53800dbe 1652 tree arglist = TREE_OPERAND (exp, 1);
7f05340e 1653 enum machine_mode mode;
528ee710 1654 bool errno_set = false;
68e6cb9d 1655 tree arg, narg;
53800dbe 1656
0eb671f7 1657 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
53800dbe 1658 return 0;
1659
7f05340e 1660 arg = TREE_VALUE (arglist);
53800dbe 1661
1662 switch (DECL_FUNCTION_CODE (fndecl))
1663 {
75357f66 1664 case BUILT_IN_SQRT:
d2d4bdde 1665 case BUILT_IN_SQRTF:
1666 case BUILT_IN_SQRTL:
7f05340e 1667 errno_set = ! tree_expr_nonnegative_p (arg);
1668 builtin_optab = sqrt_optab;
1669 break;
42721db0 1670 case BUILT_IN_EXP:
1671 case BUILT_IN_EXPF:
1672 case BUILT_IN_EXPL:
528ee710 1673 errno_set = true; builtin_optab = exp_optab; break;
750ef9f5 1674 case BUILT_IN_EXP10:
1675 case BUILT_IN_EXP10F:
1676 case BUILT_IN_EXP10L:
9d9ccca1 1677 case BUILT_IN_POW10:
1678 case BUILT_IN_POW10F:
1679 case BUILT_IN_POW10L:
750ef9f5 1680 errno_set = true; builtin_optab = exp10_optab; break;
1681 case BUILT_IN_EXP2:
1682 case BUILT_IN_EXP2F:
1683 case BUILT_IN_EXP2L:
1684 errno_set = true; builtin_optab = exp2_optab; break;
a6b4eed2 1685 case BUILT_IN_EXPM1:
1686 case BUILT_IN_EXPM1F:
1687 case BUILT_IN_EXPM1L:
1688 errno_set = true; builtin_optab = expm1_optab; break;
4efbc641 1689 case BUILT_IN_LOGB:
1690 case BUILT_IN_LOGBF:
1691 case BUILT_IN_LOGBL:
1692 errno_set = true; builtin_optab = logb_optab; break;
1693 case BUILT_IN_ILOGB:
1694 case BUILT_IN_ILOGBF:
1695 case BUILT_IN_ILOGBL:
1696 errno_set = true; builtin_optab = ilogb_optab; break;
42721db0 1697 case BUILT_IN_LOG:
1698 case BUILT_IN_LOGF:
1699 case BUILT_IN_LOGL:
528ee710 1700 errno_set = true; builtin_optab = log_optab; break;
d3cd9bde 1701 case BUILT_IN_LOG10:
1702 case BUILT_IN_LOG10F:
1703 case BUILT_IN_LOG10L:
1704 errno_set = true; builtin_optab = log10_optab; break;
1705 case BUILT_IN_LOG2:
1706 case BUILT_IN_LOG2F:
1707 case BUILT_IN_LOG2L:
1708 errno_set = true; builtin_optab = log2_optab; break;
f474cd93 1709 case BUILT_IN_LOG1P:
1710 case BUILT_IN_LOG1PF:
1711 case BUILT_IN_LOG1PL:
1712 errno_set = true; builtin_optab = log1p_optab; break;
8de2f465 1713 case BUILT_IN_ASIN:
1714 case BUILT_IN_ASINF:
1715 case BUILT_IN_ASINL:
1716 builtin_optab = asin_optab; break;
1717 case BUILT_IN_ACOS:
1718 case BUILT_IN_ACOSF:
1719 case BUILT_IN_ACOSL:
1720 builtin_optab = acos_optab; break;
528ee710 1721 case BUILT_IN_TAN:
1722 case BUILT_IN_TANF:
1723 case BUILT_IN_TANL:
1724 builtin_optab = tan_optab; break;
1725 case BUILT_IN_ATAN:
1726 case BUILT_IN_ATANF:
1727 case BUILT_IN_ATANL:
1728 builtin_optab = atan_optab; break;
805e22b2 1729 case BUILT_IN_FLOOR:
1730 case BUILT_IN_FLOORF:
1731 case BUILT_IN_FLOORL:
528ee710 1732 builtin_optab = floor_optab; break;
805e22b2 1733 case BUILT_IN_CEIL:
1734 case BUILT_IN_CEILF:
1735 case BUILT_IN_CEILL:
528ee710 1736 builtin_optab = ceil_optab; break;
805e22b2 1737 case BUILT_IN_TRUNC:
1738 case BUILT_IN_TRUNCF:
1739 case BUILT_IN_TRUNCL:
a7cc195f 1740 builtin_optab = btrunc_optab; break;
805e22b2 1741 case BUILT_IN_ROUND:
1742 case BUILT_IN_ROUNDF:
1743 case BUILT_IN_ROUNDL:
528ee710 1744 builtin_optab = round_optab; break;
805e22b2 1745 case BUILT_IN_NEARBYINT:
1746 case BUILT_IN_NEARBYINTF:
1747 case BUILT_IN_NEARBYINTL:
528ee710 1748 builtin_optab = nearbyint_optab; break;
aef94a0f 1749 case BUILT_IN_RINT:
1750 case BUILT_IN_RINTF:
1751 case BUILT_IN_RINTL:
1752 builtin_optab = rint_optab; break;
42721db0 1753 default:
64db345d 1754 gcc_unreachable ();
53800dbe 1755 }
1756
7f05340e 1757 /* Make a suitable register to place result in. */
1758 mode = TYPE_MODE (TREE_TYPE (exp));
fc4eef90 1759
7f05340e 1760 if (! flag_errno_math || ! HONOR_NANS (mode))
1761 errno_set = false;
1762
bd421108 1763 /* Before working hard, check whether the instruction is available. */
1764 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
68e6cb9d 1765 {
bd421108 1766 target = gen_reg_rtx (mode);
7f05340e 1767
bd421108 1768 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1769 need to expand the argument again. This way, we will not perform
1770 side-effects more the once. */
4ee9c684 1771 narg = builtin_save_expr (arg);
bd421108 1772 if (narg != arg)
1773 {
1774 arglist = build_tree_list (NULL_TREE, arg);
1775 exp = build_function_call_expr (fndecl, arglist);
1776 }
7f05340e 1777
bd421108 1778 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
7f05340e 1779
bd421108 1780 start_sequence ();
53800dbe 1781
bd421108 1782 /* Compute into TARGET.
1783 Set TARGET to wherever the result comes back. */
1784 target = expand_unop (mode, builtin_optab, op0, target, 0);
1785
1786 if (target != 0)
1787 {
1788 if (errno_set)
1789 expand_errno_check (exp, target);
1790
1791 /* Output the entire sequence. */
1792 insns = get_insns ();
1793 end_sequence ();
1794 emit_insn (insns);
1795 return target;
1796 }
1797
1798 /* If we were unable to expand via the builtin, stop the sequence
1799 (without outputting the insns) and call to the library function
1800 with the stabilized argument list. */
53800dbe 1801 end_sequence ();
53800dbe 1802 }
1803
bd421108 1804 before_call = get_last_insn ();
1805
1806 target = expand_call (exp, target, target == const0_rtx);
53800dbe 1807
bd421108 1808 /* If this is a sqrt operation and we don't care about errno, try to
1809 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1810 This allows the semantics of the libcall to be visible to the RTL
1811 optimizers. */
1812 if (builtin_optab == sqrt_optab && !errno_set)
1813 {
1814 /* Search backwards through the insns emitted by expand_call looking
1815 for the instruction with the REG_RETVAL note. */
1816 rtx last = get_last_insn ();
1817 while (last != before_call)
1818 {
1819 if (find_reg_note (last, REG_RETVAL, NULL))
1820 {
1821 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1822 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1823 two elements, i.e. symbol_ref(sqrt) and the operand. */
1824 if (note
1825 && GET_CODE (note) == EXPR_LIST
1826 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1827 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1828 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1829 {
1830 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1831 /* Check operand is a register with expected mode. */
1832 if (operand
8ad4c111 1833 && REG_P (operand)
bd421108 1834 && GET_MODE (operand) == mode)
1835 {
1836 /* Replace the REG_EQUAL note with a SQRT rtx. */
1837 rtx equiv = gen_rtx_SQRT (mode, operand);
1838 set_unique_reg_note (last, REG_EQUAL, equiv);
1839 }
1840 }
1841 break;
1842 }
1843 last = PREV_INSN (last);
1844 }
1845 }
0fd605a5 1846
1847 return target;
1848}
1849
1850/* Expand a call to the builtin binary math functions (pow and atan2).
1851 Return 0 if a normal call should be emitted rather than expanding the
1852 function in-line. EXP is the expression that is a call to the builtin
1853 function; if convenient, the result should be placed in TARGET.
1854 SUBTARGET may be used as the target for computing one of EXP's
1855 operands. */
1856
1857static rtx
aecda0d6 1858expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
0fd605a5 1859{
1860 optab builtin_optab;
1861 rtx op0, op1, insns;
c6e6ecb1 1862 tree fndecl = get_callee_fndecl (exp);
0fd605a5 1863 tree arglist = TREE_OPERAND (exp, 1);
68e6cb9d 1864 tree arg0, arg1, temp, narg;
7f05340e 1865 enum machine_mode mode;
0fd605a5 1866 bool errno_set = true;
1867 bool stable = true;
1868
1869 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
1870 return 0;
1871
1872 arg0 = TREE_VALUE (arglist);
1873 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1874
0fd605a5 1875 switch (DECL_FUNCTION_CODE (fndecl))
1876 {
1877 case BUILT_IN_POW:
1878 case BUILT_IN_POWF:
1879 case BUILT_IN_POWL:
1880 builtin_optab = pow_optab; break;
1881 case BUILT_IN_ATAN2:
1882 case BUILT_IN_ATAN2F:
1883 case BUILT_IN_ATAN2L:
1884 builtin_optab = atan2_optab; break;
80ed5c06 1885 case BUILT_IN_FMOD:
1886 case BUILT_IN_FMODF:
1887 case BUILT_IN_FMODL:
1888 builtin_optab = fmod_optab; break;
1889 case BUILT_IN_DREM:
1890 case BUILT_IN_DREMF:
1891 case BUILT_IN_DREML:
1892 builtin_optab = drem_optab; break;
0fd605a5 1893 default:
64db345d 1894 gcc_unreachable ();
0fd605a5 1895 }
1896
7f05340e 1897 /* Make a suitable register to place result in. */
1898 mode = TYPE_MODE (TREE_TYPE (exp));
fc4eef90 1899
1900 /* Before working hard, check whether the instruction is available. */
1901 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1902 return 0;
1903
7f05340e 1904 target = gen_reg_rtx (mode);
1905
1906 if (! flag_errno_math || ! HONOR_NANS (mode))
1907 errno_set = false;
1908
4ee9c684 1909 /* Always stabilize the argument list. */
1910 narg = builtin_save_expr (arg1);
68e6cb9d 1911 if (narg != arg1)
7f05340e 1912 {
68e6cb9d 1913 temp = build_tree_list (NULL_TREE, narg);
1914 stable = false;
1915 }
1916 else
1917 temp = TREE_CHAIN (arglist);
7f05340e 1918
4ee9c684 1919 narg = builtin_save_expr (arg0);
68e6cb9d 1920 if (narg != arg0)
1921 {
1922 arglist = tree_cons (NULL_TREE, narg, temp);
1923 stable = false;
7f05340e 1924 }
68e6cb9d 1925 else if (! stable)
1926 arglist = tree_cons (NULL_TREE, arg0, temp);
1927
1928 if (! stable)
1929 exp = build_function_call_expr (fndecl, arglist);
7f05340e 1930
1931 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1932 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1933
7f05340e 1934 start_sequence ();
1935
0fd605a5 1936 /* Compute into TARGET.
1937 Set TARGET to wherever the result comes back. */
7f05340e 1938 target = expand_binop (mode, builtin_optab, op0, op1,
0fd605a5 1939 target, 0, OPTAB_DIRECT);
53800dbe 1940
68e6cb9d 1941 /* If we were unable to expand via the builtin, stop the sequence
1942 (without outputting the insns) and call to the library function
1943 with the stabilized argument list. */
0fd605a5 1944 if (target == 0)
1945 {
1946 end_sequence ();
68e6cb9d 1947 return expand_call (exp, target, target == const0_rtx);
53800dbe 1948 }
1949
a4356fb9 1950 if (errno_set)
1951 expand_errno_check (exp, target);
0fd605a5 1952
53800dbe 1953 /* Output the entire sequence. */
1954 insns = get_insns ();
1955 end_sequence ();
31d3e01c 1956 emit_insn (insns);
bf8e3599 1957
53800dbe 1958 return target;
1959}
1960
6b43bae4 1961/* Expand a call to the builtin sin and cos math functions.
1962 Return 0 if a normal call should be emitted rather than expanding the
1963 function in-line. EXP is the expression that is a call to the builtin
1964 function; if convenient, the result should be placed in TARGET.
1965 SUBTARGET may be used as the target for computing one of EXP's
1966 operands. */
1967
1968static rtx
1969expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1970{
1971 optab builtin_optab;
1972 rtx op0, insns, before_call;
1973 tree fndecl = get_callee_fndecl (exp);
1974 tree arglist = TREE_OPERAND (exp, 1);
1975 enum machine_mode mode;
1976 bool errno_set = false;
1977 tree arg, narg;
1978
1979 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1980 return 0;
1981
1982 arg = TREE_VALUE (arglist);
1983
1984 switch (DECL_FUNCTION_CODE (fndecl))
1985 {
1986 case BUILT_IN_SIN:
1987 case BUILT_IN_SINF:
1988 case BUILT_IN_SINL:
1989 case BUILT_IN_COS:
1990 case BUILT_IN_COSF:
1991 case BUILT_IN_COSL:
1992 builtin_optab = sincos_optab; break;
1993 default:
64db345d 1994 gcc_unreachable ();
6b43bae4 1995 }
1996
1997 /* Make a suitable register to place result in. */
1998 mode = TYPE_MODE (TREE_TYPE (exp));
1999
2000 if (! flag_errno_math || ! HONOR_NANS (mode))
2001 errno_set = false;
2002
2003 /* Check if sincos insn is available, otherwise fallback
0bed3869 2004 to sin or cos insn. */
6b43bae4 2005 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2006 switch (DECL_FUNCTION_CODE (fndecl))
2007 {
2008 case BUILT_IN_SIN:
2009 case BUILT_IN_SINF:
2010 case BUILT_IN_SINL:
2011 builtin_optab = sin_optab; break;
2012 case BUILT_IN_COS:
2013 case BUILT_IN_COSF:
2014 case BUILT_IN_COSL:
2015 builtin_optab = cos_optab; break;
2016 default:
64db345d 2017 gcc_unreachable ();
6b43bae4 2018 }
2019 }
2020
2021 /* Before working hard, check whether the instruction is available. */
2022 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2023 {
2024 target = gen_reg_rtx (mode);
2025
2026 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2027 need to expand the argument again. This way, we will not perform
2028 side-effects more the once. */
2029 narg = save_expr (arg);
2030 if (narg != arg)
2031 {
2032 arglist = build_tree_list (NULL_TREE, arg);
2033 exp = build_function_call_expr (fndecl, arglist);
2034 }
2035
2036 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2037
6b43bae4 2038 start_sequence ();
2039
2040 /* Compute into TARGET.
2041 Set TARGET to wherever the result comes back. */
2042 if (builtin_optab == sincos_optab)
2043 {
64db345d 2044 int result;
7d3f6cc7 2045
6b43bae4 2046 switch (DECL_FUNCTION_CODE (fndecl))
2047 {
2048 case BUILT_IN_SIN:
2049 case BUILT_IN_SINF:
2050 case BUILT_IN_SINL:
64db345d 2051 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
6b43bae4 2052 break;
2053 case BUILT_IN_COS:
2054 case BUILT_IN_COSF:
2055 case BUILT_IN_COSL:
64db345d 2056 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
6b43bae4 2057 break;
2058 default:
64db345d 2059 gcc_unreachable ();
6b43bae4 2060 }
64db345d 2061 gcc_assert (result);
6b43bae4 2062 }
2063 else
2064 {
2065 target = expand_unop (mode, builtin_optab, op0, target, 0);
2066 }
2067
2068 if (target != 0)
2069 {
2070 if (errno_set)
2071 expand_errno_check (exp, target);
2072
2073 /* Output the entire sequence. */
2074 insns = get_insns ();
2075 end_sequence ();
2076 emit_insn (insns);
2077 return target;
2078 }
2079
2080 /* If we were unable to expand via the builtin, stop the sequence
2081 (without outputting the insns) and call to the library function
2082 with the stabilized argument list. */
2083 end_sequence ();
2084 }
2085
2086 before_call = get_last_insn ();
2087
2088 target = expand_call (exp, target, target == const0_rtx);
2089
2090 return target;
2091}
2092
f1b844c6 2093/* To evaluate powi(x,n), the floating point value x raised to the
2094 constant integer exponent n, we use a hybrid algorithm that
2095 combines the "window method" with look-up tables. For an
2096 introduction to exponentiation algorithms and "addition chains",
2097 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2098 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2099 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2100 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2101
2102/* Provide a default value for POWI_MAX_MULTS, the maximum number of
2103 multiplications to inline before calling the system library's pow
2104 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2105 so this default never requires calling pow, powf or powl. */
0862b7e9 2106
f1b844c6 2107#ifndef POWI_MAX_MULTS
2108#define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2109#endif
2110
2111/* The size of the "optimal power tree" lookup table. All
2112 exponents less than this value are simply looked up in the
2113 powi_table below. This threshold is also used to size the
2114 cache of pseudo registers that hold intermediate results. */
2115#define POWI_TABLE_SIZE 256
2116
2117/* The size, in bits of the window, used in the "window method"
2118 exponentiation algorithm. This is equivalent to a radix of
2119 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2120#define POWI_WINDOW_SIZE 3
2121
2122/* The following table is an efficient representation of an
2123 "optimal power tree". For each value, i, the corresponding
2124 value, j, in the table states than an optimal evaluation
2125 sequence for calculating pow(x,i) can be found by evaluating
2126 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2127 100 integers is given in Knuth's "Seminumerical algorithms". */
2128
2129static const unsigned char powi_table[POWI_TABLE_SIZE] =
2130 {
2131 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2132 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2133 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2134 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2135 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2136 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2137 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2138 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2139 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2140 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2141 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2142 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2143 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2144 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2145 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2146 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2147 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2148 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2149 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2150 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2151 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2152 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2153 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2154 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2155 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2156 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2157 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2158 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2159 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2160 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2161 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2162 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2163 };
2164
2165
2166/* Return the number of multiplications required to calculate
2167 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2168 subroutine of powi_cost. CACHE is an array indicating
2169 which exponents have already been calculated. */
2170
2171static int
2172powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2173{
2174 /* If we've already calculated this exponent, then this evaluation
2175 doesn't require any additional multiplications. */
2176 if (cache[n])
2177 return 0;
2178
2179 cache[n] = true;
2180 return powi_lookup_cost (n - powi_table[n], cache)
2181 + powi_lookup_cost (powi_table[n], cache) + 1;
2182}
2183
2184/* Return the number of multiplications required to calculate
2185 powi(x,n) for an arbitrary x, given the exponent N. This
2186 function needs to be kept in sync with expand_powi below. */
2187
2188static int
2189powi_cost (HOST_WIDE_INT n)
2190{
2191 bool cache[POWI_TABLE_SIZE];
2192 unsigned HOST_WIDE_INT digit;
2193 unsigned HOST_WIDE_INT val;
2194 int result;
2195
2196 if (n == 0)
2197 return 0;
2198
2199 /* Ignore the reciprocal when calculating the cost. */
2200 val = (n < 0) ? -n : n;
2201
2202 /* Initialize the exponent cache. */
2203 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2204 cache[1] = true;
2205
2206 result = 0;
2207
2208 while (val >= POWI_TABLE_SIZE)
2209 {
2210 if (val & 1)
2211 {
2212 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2213 result += powi_lookup_cost (digit, cache)
2214 + POWI_WINDOW_SIZE + 1;
2215 val >>= POWI_WINDOW_SIZE;
2216 }
2217 else
2218 {
2219 val >>= 1;
2220 result++;
2221 }
2222 }
0862b7e9 2223
6881dbab 2224 return result + powi_lookup_cost (val, cache);
f1b844c6 2225}
2226
2227/* Recursive subroutine of expand_powi. This function takes the array,
2228 CACHE, of already calculated exponents and an exponent N and returns
2229 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2230
2231static rtx
2232expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2233{
2234 unsigned HOST_WIDE_INT digit;
2235 rtx target, result;
2236 rtx op0, op1;
2237
2238 if (n < POWI_TABLE_SIZE)
2239 {
2240 if (cache[n])
2241 return cache[n];
2242
2243 target = gen_reg_rtx (mode);
2244 cache[n] = target;
2245
2246 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2247 op1 = expand_powi_1 (mode, powi_table[n], cache);
2248 }
2249 else if (n & 1)
2250 {
2251 target = gen_reg_rtx (mode);
2252 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2253 op0 = expand_powi_1 (mode, n - digit, cache);
2254 op1 = expand_powi_1 (mode, digit, cache);
2255 }
2256 else
2257 {
2258 target = gen_reg_rtx (mode);
2259 op0 = expand_powi_1 (mode, n >> 1, cache);
2260 op1 = op0;
2261 }
2262
2263 result = expand_mult (mode, op0, op1, target, 0);
2264 if (result != target)
2265 emit_move_insn (target, result);
2266 return target;
2267}
2268
2269/* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2270 floating point operand in mode MODE, and N is the exponent. This
2271 function needs to be kept in sync with powi_cost above. */
0862b7e9 2272
f1b844c6 2273static rtx
2274expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2275{
2276 unsigned HOST_WIDE_INT val;
2277 rtx cache[POWI_TABLE_SIZE];
2278 rtx result;
2279
2280 if (n == 0)
2281 return CONST1_RTX (mode);
2282
2283 val = (n < 0) ? -n : n;
2284
19bf118a 2285 memset (cache, 0, sizeof (cache));
f1b844c6 2286 cache[1] = x;
2287
2288 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2289
2290 /* If the original exponent was negative, reciprocate the result. */
2291 if (n < 0)
2292 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2293 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2294
2295 return result;
2296}
2297
2298/* Expand a call to the pow built-in mathematical function. Return 0 if
2299 a normal call should be emitted rather than expanding the function
2300 in-line. EXP is the expression that is a call to the builtin
2301 function; if convenient, the result should be placed in TARGET. */
2302
2303static rtx
2304expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2305{
2306 tree arglist = TREE_OPERAND (exp, 1);
2307 tree arg0, arg1;
2308
2309 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2310 return 0;
2311
2312 arg0 = TREE_VALUE (arglist);
2313 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2314
8c5cac78 2315 if (TREE_CODE (arg1) == REAL_CST
f1b844c6 2316 && ! TREE_CONSTANT_OVERFLOW (arg1))
2317 {
2318 REAL_VALUE_TYPE cint;
2319 REAL_VALUE_TYPE c;
2320 HOST_WIDE_INT n;
2321
2322 c = TREE_REAL_CST (arg1);
2323 n = real_to_integer (&c);
2324 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8c5cac78 2325 if (real_identical (&c, &cint))
f1b844c6 2326 {
8c5cac78 2327 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2328 Otherwise, check the number of multiplications required.
2329 Note that pow never sets errno for an integer exponent. */
2330 if ((n >= -1 && n <= 2)
2331 || (flag_unsafe_math_optimizations
2332 && ! optimize_size
2333 && powi_cost (n) <= POWI_MAX_MULTS))
2334 {
2335 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2336 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2337 op = force_reg (mode, op);
2338 return expand_powi (op, mode, n);
2339 }
f1b844c6 2340 }
2341 }
b613c2a6 2342
2343 if (! flag_unsafe_math_optimizations)
2344 return NULL_RTX;
2345 return expand_builtin_mathfn_2 (exp, target, subtarget);
f1b844c6 2346}
2347
53800dbe 2348/* Expand expression EXP which is a call to the strlen builtin. Return 0
2349 if we failed the caller should emit a normal call, otherwise
aed0bd19 2350 try to get the result in TARGET, if convenient. */
f7c44134 2351
53800dbe 2352static rtx
aecda0d6 2353expand_builtin_strlen (tree arglist, rtx target,
2354 enum machine_mode target_mode)
53800dbe 2355{
0eb671f7 2356 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
53800dbe 2357 return 0;
2358 else
2359 {
911c0150 2360 rtx pat;
6248e345 2361 tree len, src = TREE_VALUE (arglist);
911c0150 2362 rtx result, src_reg, char_rtx, before_strlen;
80cd7a5e 2363 enum machine_mode insn_mode = target_mode, char_mode;
ef2c4a29 2364 enum insn_code icode = CODE_FOR_nothing;
6248e345 2365 int align;
2366
2367 /* If the length can be computed at compile-time, return it. */
681fab1e 2368 len = c_strlen (src, 0);
6248e345 2369 if (len)
80cd7a5e 2370 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
6248e345 2371
681fab1e 2372 /* If the length can be computed at compile-time and is constant
2373 integer, but there are side-effects in src, evaluate
2374 src for side-effects, then return len.
2375 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2376 can be optimized into: i++; x = 3; */
2377 len = c_strlen (src, 1);
2378 if (len && TREE_CODE (len) == INTEGER_CST)
2379 {
2380 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2381 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2382 }
2383
6248e345 2384 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
53800dbe 2385
53800dbe 2386 /* If SRC is not a pointer type, don't do this operation inline. */
2387 if (align == 0)
2388 return 0;
2389
911c0150 2390 /* Bail out if we can't compute strlen in the right mode. */
53800dbe 2391 while (insn_mode != VOIDmode)
2392 {
2393 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2394 if (icode != CODE_FOR_nothing)
c28ae87f 2395 break;
53800dbe 2396
2397 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2398 }
2399 if (insn_mode == VOIDmode)
2400 return 0;
2401
2402 /* Make a place to write the result of the instruction. */
2403 result = target;
2404 if (! (result != 0
8ad4c111 2405 && REG_P (result)
53800dbe 2406 && GET_MODE (result) == insn_mode
2407 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2408 result = gen_reg_rtx (insn_mode);
2409
911c0150 2410 /* Make a place to hold the source address. We will not expand
2411 the actual source until we are sure that the expansion will
2412 not fail -- there are trees that cannot be expanded twice. */
2413 src_reg = gen_reg_rtx (Pmode);
53800dbe 2414
911c0150 2415 /* Mark the beginning of the strlen sequence so we can emit the
2416 source operand later. */
f0ce3b1f 2417 before_strlen = get_last_insn ();
53800dbe 2418
53800dbe 2419 char_rtx = const0_rtx;
f7c44134 2420 char_mode = insn_data[(int) icode].operand[2].mode;
2421 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2422 char_mode))
53800dbe 2423 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2424
911c0150 2425 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2426 char_rtx, GEN_INT (align));
2427 if (! pat)
2428 return 0;
2429 emit_insn (pat);
2430
2431 /* Now that we are assured of success, expand the source. */
2432 start_sequence ();
bf8e3599 2433 pat = memory_address (BLKmode,
f7c44134 2434 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
911c0150 2435 if (pat != src_reg)
2436 emit_move_insn (src_reg, pat);
31d3e01c 2437 pat = get_insns ();
911c0150 2438 end_sequence ();
bceb0d1f 2439
2440 if (before_strlen)
2441 emit_insn_after (pat, before_strlen);
2442 else
2443 emit_insn_before (pat, get_insns ());
53800dbe 2444
2445 /* Return the value in the proper mode for this function. */
80cd7a5e 2446 if (GET_MODE (result) == target_mode)
911c0150 2447 target = result;
53800dbe 2448 else if (target != 0)
911c0150 2449 convert_move (target, result, 0);
53800dbe 2450 else
80cd7a5e 2451 target = convert_to_mode (target_mode, result, 0);
911c0150 2452
2453 return target;
53800dbe 2454 }
2455}
2456
17f5ea87 2457/* Expand a call to the strstr builtin. Return 0 if we failed the
2458 caller should emit a normal call, otherwise try to get the result
2459 in TARGET, if convenient (and in mode MODE if that's convenient). */
2460
2461static rtx
aecda0d6 2462expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode)
17f5ea87 2463{
8a06f2d4 2464 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
17f5ea87 2465 return 0;
2466 else
2467 {
2468 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
05abc81b 2469 tree fn, tmp;
83d79705 2470 const char *p1, *p2;
17f5ea87 2471
83d79705 2472 p2 = c_getstr (s2);
2473 if (p2 == NULL)
17f5ea87 2474 return 0;
2475
83d79705 2476 p1 = c_getstr (s1);
2477 if (p1 != NULL)
2478 {
2479 const char *r = strstr (p1, p2);
17f5ea87 2480
83d79705 2481 if (r == NULL)
2482 return const0_rtx;
17f5ea87 2483
83d79705 2484 /* Return an offset into the constant string argument. */
05abc81b 2485 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2486 fold_convert (TREE_TYPE (s1),
2487 ssize_int (r - p1))));
2488 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
17f5ea87 2489 }
83d79705 2490
2491 if (p2[0] == '\0')
2492 return expand_expr (s1, target, mode, EXPAND_NORMAL);
2493
2494 if (p2[1] != '\0')
2495 return 0;
2496
0a68165a 2497 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
83d79705 2498 if (!fn)
2499 return 0;
2500
2501 /* New argument list transforming strstr(s1, s2) to
2502 strchr(s1, s2[0]). */
7c446c95 2503 arglist = build_tree_list (NULL_TREE,
7016c612 2504 build_int_cst (NULL_TREE, p2[0]));
83d79705 2505 arglist = tree_cons (NULL_TREE, s1, arglist);
7e15618b 2506 return expand_expr (build_function_call_expr (fn, arglist),
2507 target, mode, EXPAND_NORMAL);
17f5ea87 2508 }
2509}
2510
83d79705 2511/* Expand a call to the strchr builtin. Return 0 if we failed the
46f3a74a 2512 caller should emit a normal call, otherwise try to get the result
2513 in TARGET, if convenient (and in mode MODE if that's convenient). */
2514
2515static rtx
aecda0d6 2516expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode)
46f3a74a 2517{
8a06f2d4 2518 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
46f3a74a 2519 return 0;
2520 else
2521 {
2522 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
83d79705 2523 const char *p1;
46f3a74a 2524
83d79705 2525 if (TREE_CODE (s2) != INTEGER_CST)
2526 return 0;
2527
2528 p1 = c_getstr (s1);
2529 if (p1 != NULL)
2530 {
ecc318ff 2531 char c;
2532 const char *r;
05abc81b 2533 tree tmp;
ecc318ff 2534
2535 if (target_char_cast (s2, &c))
2536 return 0;
2537
2538 r = strchr (p1, c);
83d79705 2539
2540 if (r == NULL)
46f3a74a 2541 return const0_rtx;
83d79705 2542
2543 /* Return an offset into the constant string argument. */
05abc81b 2544 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2545 fold_convert (TREE_TYPE (s1),
2546 ssize_int (r - p1))));
2547 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
46f3a74a 2548 }
2549
83d79705 2550 /* FIXME: Should use here strchrM optab so that ports can optimize
2551 this. */
2552 return 0;
2553 }
2554}
2555
2556/* Expand a call to the strrchr builtin. Return 0 if we failed the
2557 caller should emit a normal call, otherwise try to get the result
2558 in TARGET, if convenient (and in mode MODE if that's convenient). */
2559
2560static rtx
aecda0d6 2561expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode)
83d79705 2562{
8a06f2d4 2563 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
83d79705 2564 return 0;
2565 else
2566 {
2567 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
05abc81b 2568 tree fn, tmp;
83d79705 2569 const char *p1;
2570
2571 if (TREE_CODE (s2) != INTEGER_CST)
46f3a74a 2572 return 0;
2573
83d79705 2574 p1 = c_getstr (s1);
2575 if (p1 != NULL)
2576 {
ecc318ff 2577 char c;
2578 const char *r;
2579
2580 if (target_char_cast (s2, &c))
2581 return 0;
2582
2583 r = strrchr (p1, c);
83d79705 2584
2585 if (r == NULL)
46f3a74a 2586 return const0_rtx;
46f3a74a 2587
83d79705 2588 /* Return an offset into the constant string argument. */
05abc81b 2589 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2590 fold_convert (TREE_TYPE (s1),
2591 ssize_int (r - p1))));
2592 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
83d79705 2593 }
46f3a74a 2594
83d79705 2595 if (! integer_zerop (s2))
2596 return 0;
2597
0a68165a 2598 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
83d79705 2599 if (!fn)
2600 return 0;
2601
2602 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
7e15618b 2603 return expand_expr (build_function_call_expr (fn, arglist),
2604 target, mode, EXPAND_NORMAL);
83d79705 2605 }
2606}
2607
2608/* Expand a call to the strpbrk builtin. Return 0 if we failed the
2609 caller should emit a normal call, otherwise try to get the result
2610 in TARGET, if convenient (and in mode MODE if that's convenient). */
2611
2612static rtx
aecda0d6 2613expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode)
83d79705 2614{
8a06f2d4 2615 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
83d79705 2616 return 0;
2617 else
2618 {
2619 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
05abc81b 2620 tree fn, tmp;
83d79705 2621 const char *p1, *p2;
2622
2623 p2 = c_getstr (s2);
2624 if (p2 == NULL)
2625 return 0;
2626
2627 p1 = c_getstr (s1);
2628 if (p1 != NULL)
2629 {
2630 const char *r = strpbrk (p1, p2);
2631
2632 if (r == NULL)
2633 return const0_rtx;
2634
2635 /* Return an offset into the constant string argument. */
05abc81b 2636 tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (s1), s1,
2637 fold_convert (TREE_TYPE (s1),
2638 ssize_int (r - p1))));
2639 return expand_expr (tmp, target, mode, EXPAND_NORMAL);
46f3a74a 2640 }
83d79705 2641
2642 if (p2[0] == '\0')
2643 {
2644 /* strpbrk(x, "") == NULL.
2645 Evaluate and ignore the arguments in case they had
2646 side-effects. */
2647 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
2648 return const0_rtx;
2649 }
2650
2651 if (p2[1] != '\0')
2652 return 0; /* Really call strpbrk. */
2653
0a68165a 2654 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
83d79705 2655 if (!fn)
2656 return 0;
2657
2658 /* New argument list transforming strpbrk(s1, s2) to
2659 strchr(s1, s2[0]). */
7c446c95 2660 arglist = build_tree_list (NULL_TREE,
7016c612 2661 build_int_cst (NULL_TREE, p2[0]));
83d79705 2662 arglist = tree_cons (NULL_TREE, s1, arglist);
7e15618b 2663 return expand_expr (build_function_call_expr (fn, arglist),
2664 target, mode, EXPAND_NORMAL);
46f3a74a 2665 }
2666}
2667
6840589f 2668/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2669 bytes from constant string DATA + OFFSET and return it as target
2670 constant. */
2671
2672static rtx
aecda0d6 2673builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2674 enum machine_mode mode)
6840589f 2675{
2676 const char *str = (const char *) data;
2677
64db345d 2678 gcc_assert (offset >= 0
2679 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2680 <= strlen (str) + 1));
6840589f 2681
2682 return c_readstr (str + offset, mode);
2683}
2684
6f428e8b 2685/* Expand a call to the memcpy builtin, with arguments in ARGLIST.
3b824fa6 2686 Return 0 if we failed, the caller should emit a normal call,
2687 otherwise try to get the result in TARGET, if convenient (and in
9fe0e1b8 2688 mode MODE if that's convenient). */
53800dbe 2689static rtx
aecda0d6 2690expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode)
53800dbe 2691{
0eb671f7 2692 if (!validate_arglist (arglist,
2693 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 2694 return 0;
2695 else
2696 {
2697 tree dest = TREE_VALUE (arglist);
2698 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2699 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6840589f 2700 const char *src_str;
27d0c333 2701 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2702 unsigned int dest_align
2703 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
53800dbe 2704 rtx dest_mem, src_mem, dest_addr, len_rtx;
2705
6f428e8b 2706 /* If DEST is not a pointer type, call the normal function. */
2707 if (dest_align == 0)
9342ee68 2708 return 0;
6f428e8b 2709
2710 /* If the LEN parameter is zero, return DEST. */
9c8a1629 2711 if (integer_zerop (len))
9342ee68 2712 {
2713 /* Evaluate and ignore SRC in case it has side-effects. */
2714 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2715 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2716 }
6f428e8b 2717
9c8a1629 2718 /* If SRC and DEST are the same (and not volatile), return DEST. */
2719 if (operand_equal_p (src, dest, 0))
2720 {
2721 /* Evaluate and ignore LEN in case it has side-effects. */
2722 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2723 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2724 }
2725
6f428e8b 2726 /* If either SRC is not a pointer type, don't do this
2727 operation in-line. */
2728 if (src_align == 0)
9342ee68 2729 return 0;
53800dbe 2730
2731 dest_mem = get_memory_rtx (dest);
2a631e19 2732 set_mem_align (dest_mem, dest_align);
53800dbe 2733 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
6840589f 2734 src_str = c_getstr (src);
2735
2736 /* If SRC is a string constant and block move would be done
2737 by pieces, we can avoid loading the string from memory
2738 and only stored the computed constants. */
2739 if (src_str
6840589f 2740 && GET_CODE (len_rtx) == CONST_INT
2741 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2742 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
b9a7cc69 2743 (void *) src_str, dest_align))
6840589f 2744 {
9fe0e1b8 2745 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2746 builtin_memcpy_read_str,
b9a7cc69 2747 (void *) src_str, dest_align, 0);
e5716f7e 2748 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 2749 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 2750 return dest_mem;
6840589f 2751 }
2752
2753 src_mem = get_memory_rtx (src);
2a631e19 2754 set_mem_align (src_mem, src_align);
53800dbe 2755
53800dbe 2756 /* Copy word part most expediently. */
0378dbdc 2757 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2758 BLOCK_OP_NORMAL);
53800dbe 2759
2760 if (dest_addr == 0)
e5716f7e 2761 {
2762 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 2763 dest_addr = convert_memory_address (ptr_mode, dest_addr);
e5716f7e 2764 }
9fe0e1b8 2765 return dest_addr;
53800dbe 2766 }
2767}
2768
647661c6 2769/* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2770 Return 0 if we failed the caller should emit a normal call,
2771 otherwise try to get the result in TARGET, if convenient (and in
9fe0e1b8 2772 mode MODE if that's convenient). If ENDP is 0 return the
2773 destination pointer, if ENDP is 1 return the end pointer ala
2774 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2775 stpcpy. */
647661c6 2776
2777static rtx
aecda0d6 2778expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
2779 int endp)
647661c6 2780{
2781 if (!validate_arglist (arglist,
2782 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2783 return 0;
9fe0e1b8 2784 /* If return value is ignored, transform mempcpy into memcpy. */
2785 else if (target == const0_rtx)
2786 {
2787 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2788
2789 if (!fn)
2790 return 0;
2791
2792 return expand_expr (build_function_call_expr (fn, arglist),
2793 target, mode, EXPAND_NORMAL);
2794 }
647661c6 2795 else
2796 {
9fe0e1b8 2797 tree dest = TREE_VALUE (arglist);
2798 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2799 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2800 const char *src_str;
2801 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2802 unsigned int dest_align
2803 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2804 rtx dest_mem, src_mem, len_rtx;
2805
6217c238 2806 /* If DEST is not a pointer type, call the normal function. */
2807 if (dest_align == 0)
9fe0e1b8 2808 return 0;
2809
6217c238 2810 /* If SRC and DEST are the same (and not volatile), do nothing. */
2811 if (operand_equal_p (src, dest, 0))
2812 {
2813 tree expr;
2814
2815 if (endp == 0)
2816 {
2817 /* Evaluate and ignore LEN in case it has side-effects. */
2818 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2819 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2820 }
2821
2822 if (endp == 2)
05abc81b 2823 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
2824 integer_one_node));
d7aeca92 2825 len = fold_convert (TREE_TYPE (dest), len);
05abc81b 2826 expr = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
6217c238 2827 return expand_expr (expr, target, mode, EXPAND_NORMAL);
2828 }
2829
2830 /* If LEN is not constant, call the normal function. */
2831 if (! host_integerp (len, 1))
2832 return 0;
0862b7e9 2833
9fe0e1b8 2834 /* If the LEN parameter is zero, return DEST. */
2835 if (tree_low_cst (len, 1) == 0)
647661c6 2836 {
9fe0e1b8 2837 /* Evaluate and ignore SRC in case it has side-effects. */
2838 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2839 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2840 }
647661c6 2841
9fe0e1b8 2842 /* If either SRC is not a pointer type, don't do this
2843 operation in-line. */
2844 if (src_align == 0)
2845 return 0;
647661c6 2846
9fe0e1b8 2847 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2848 src_str = c_getstr (src);
647661c6 2849
9fe0e1b8 2850 /* If SRC is a string constant and block move would be done
2851 by pieces, we can avoid loading the string from memory
2852 and only stored the computed constants. */
2853 if (src_str
2854 && GET_CODE (len_rtx) == CONST_INT
2855 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2856 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
b9a7cc69 2857 (void *) src_str, dest_align))
9fe0e1b8 2858 {
2859 dest_mem = get_memory_rtx (dest);
2860 set_mem_align (dest_mem, dest_align);
2861 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2862 builtin_memcpy_read_str,
b9a7cc69 2863 (void *) src_str, dest_align, endp);
9fe0e1b8 2864 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 2865 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 2866 return dest_mem;
647661c6 2867 }
2868
9fe0e1b8 2869 if (GET_CODE (len_rtx) == CONST_INT
2870 && can_move_by_pieces (INTVAL (len_rtx),
2871 MIN (dest_align, src_align)))
2872 {
2873 dest_mem = get_memory_rtx (dest);
2874 set_mem_align (dest_mem, dest_align);
2875 src_mem = get_memory_rtx (src);
2876 set_mem_align (src_mem, src_align);
2877 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2878 MIN (dest_align, src_align), endp);
2879 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 2880 dest_mem = convert_memory_address (ptr_mode, dest_mem);
9fe0e1b8 2881 return dest_mem;
2882 }
2883
2884 return 0;
647661c6 2885 }
2886}
2887
c4950093 2888/* Expand expression EXP, which is a call to the memmove builtin. Return 0
2889 if we failed the caller should emit a normal call. */
2890
2891static rtx
aecda0d6 2892expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode)
c4950093 2893{
2894 if (!validate_arglist (arglist,
2895 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2896 return 0;
2897 else
2898 {
2899 tree dest = TREE_VALUE (arglist);
2900 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2901 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2902
2903 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2904 unsigned int dest_align
2905 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2906
2907 /* If DEST is not a pointer type, call the normal function. */
2908 if (dest_align == 0)
2909 return 0;
2910
2911 /* If the LEN parameter is zero, return DEST. */
9c8a1629 2912 if (integer_zerop (len))
c4950093 2913 {
2914 /* Evaluate and ignore SRC in case it has side-effects. */
2915 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2916 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2917 }
2918
6217c238 2919 /* If SRC and DEST are the same (and not volatile), return DEST. */
2920 if (operand_equal_p (src, dest, 0))
2921 {
2922 /* Evaluate and ignore LEN in case it has side-effects. */
2923 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
2924 return expand_expr (dest, target, mode, EXPAND_NORMAL);
2925 }
2926
c4950093 2927 /* If either SRC is not a pointer type, don't do this
2928 operation in-line. */
2929 if (src_align == 0)
2930 return 0;
2931
01537105 2932 /* If src is categorized for a readonly section we can use
2933 normal memcpy. */
2934 if (readonly_data_expr (src))
2935 {
2936 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2937 if (!fn)
2938 return 0;
2939 return expand_expr (build_function_call_expr (fn, arglist),
2940 target, mode, EXPAND_NORMAL);
2941 }
c4950093 2942
9a2f233a 2943 /* If length is 1 and we can expand memcpy call inline,
2944 it is ok to use memcpy as well. */
2945 if (integer_onep (len))
2946 {
2947 rtx ret = expand_builtin_mempcpy (arglist, target, mode,
2948 /*endp=*/0);
2949 if (ret)
2950 return ret;
2951 }
2952
c4950093 2953 /* Otherwise, call the normal function. */
2954 return 0;
2955 }
2956}
2957
2958/* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2959 if we failed the caller should emit a normal call. */
2960
2961static rtx
aecda0d6 2962expand_builtin_bcopy (tree arglist)
c4950093 2963{
2964 tree src, dest, size, newarglist;
2965
2966 if (!validate_arglist (arglist,
2967 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2968 return NULL_RTX;
2969
2970 src = TREE_VALUE (arglist);
2971 dest = TREE_VALUE (TREE_CHAIN (arglist));
2972 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2973
2974 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2975 memmove(ptr y, ptr x, size_t z). This is done this way
2976 so that if it isn't expanded inline, we fallback to
2977 calling bcopy instead of memmove. */
2978
d7aeca92 2979 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
c4950093 2980 newarglist = tree_cons (NULL_TREE, src, newarglist);
2981 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2982
2983 return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode);
2984}
2985
727c62dd 2986#ifndef HAVE_movstr
2987# define HAVE_movstr 0
2988# define CODE_FOR_movstr CODE_FOR_nothing
2989#endif
2990
2991/* Expand into a movstr instruction, if one is available. Return 0 if
2992 we failed, the caller should emit a normal call, otherwise try to
2993 get the result in TARGET, if convenient. If ENDP is 0 return the
2994 destination pointer, if ENDP is 1 return the end pointer ala
2995 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2996 stpcpy. */
2997
2998static rtx
2999expand_movstr (tree dest, tree src, rtx target, int endp)
3000{
3001 rtx end;
3002 rtx dest_mem;
3003 rtx src_mem;
3004 rtx insn;
3005 const struct insn_data * data;
3006
3007 if (!HAVE_movstr)
3008 return 0;
3009
3010 dest_mem = get_memory_rtx (dest);
3011 src_mem = get_memory_rtx (src);
3012 if (!endp)
3013 {
3014 target = force_reg (Pmode, XEXP (dest_mem, 0));
3015 dest_mem = replace_equiv_address (dest_mem, target);
3016 end = gen_reg_rtx (Pmode);
3017 }
3018 else
3019 {
3020 if (target == 0 || target == const0_rtx)
3021 {
3022 end = gen_reg_rtx (Pmode);
3023 if (target == 0)
3024 target = end;
3025 }
3026 else
3027 end = target;
3028 }
3029
3030 data = insn_data + CODE_FOR_movstr;
3031
3032 if (data->operand[0].mode != VOIDmode)
3033 end = gen_lowpart (data->operand[0].mode, end);
3034
3035 insn = data->genfun (end, dest_mem, src_mem);
3036
64db345d 3037 gcc_assert (insn);
727c62dd 3038
3039 emit_insn (insn);
3040
3041 /* movstr is supposed to set end to the address of the NUL
3042 terminator. If the caller requested a mempcpy-like return value,
3043 adjust it. */
3044 if (endp == 1 && target != const0_rtx)
3045 emit_move_insn (target, plus_constant (gen_lowpart (GET_MODE (target),
3046 end), 1));
3047
3048 return target;
3049}
3050
53800dbe 3051/* Expand expression EXP, which is a call to the strcpy builtin. Return 0
6f428e8b 3052 if we failed the caller should emit a normal call, otherwise try to get
3053 the result in TARGET, if convenient (and in mode MODE if that's
3054 convenient). */
902de8ed 3055
53800dbe 3056static rtx
aecda0d6 3057expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode)
53800dbe 3058{
6411575e 3059 tree fn, len, src, dst;
53800dbe 3060
0eb671f7 3061 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
53800dbe 3062 return 0;
53800dbe 3063
6217c238 3064 src = TREE_VALUE (TREE_CHAIN (arglist));
3065 dst = TREE_VALUE (arglist);
3066
3067 /* If SRC and DST are equal (and not volatile), return DST. */
3068 if (operand_equal_p (src, dst, 0))
3069 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3070
681fab1e 3071 len = c_strlen (src, 1);
5c23237b 3072 if (len == 0 || TREE_SIDE_EFFECTS (len))
727c62dd 3073 return expand_movstr (TREE_VALUE (arglist),
3074 TREE_VALUE (TREE_CHAIN (arglist)),
3075 target, /*endp=*/0);
3076
3077 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3078 if (!fn)
6f428e8b 3079 return 0;
902de8ed 3080
6f428e8b 3081 len = size_binop (PLUS_EXPR, len, ssize_int (1));
6411575e 3082 arglist = build_tree_list (NULL_TREE, len);
3083 arglist = tree_cons (NULL_TREE, src, arglist);
3084 arglist = tree_cons (NULL_TREE, dst, arglist);
6f428e8b 3085 return expand_expr (build_function_call_expr (fn, arglist),
9342ee68 3086 target, mode, EXPAND_NORMAL);
53800dbe 3087}
3088
3b824fa6 3089/* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3090 Return 0 if we failed the caller should emit a normal call,
3091 otherwise try to get the result in TARGET, if convenient (and in
3092 mode MODE if that's convenient). */
3093
3094static rtx
aecda0d6 3095expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode)
3b824fa6 3096{
727c62dd 3097 /* If return value is ignored, transform stpcpy into strcpy. */
3098 if (target == const0_rtx)
978836e5 3099 {
3100 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3101 if (!fn)
3102 return 0;
3103
3104 return expand_expr (build_function_call_expr (fn, arglist),
3105 target, mode, EXPAND_NORMAL);
3106 }
727c62dd 3107
3b824fa6 3108 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3109 return 0;
3110 else
3111 {
7d705727 3112 tree dst, src, len, lenp1;
727c62dd 3113 tree narglist;
3114 rtx ret;
647661c6 3115
9fe0e1b8 3116 /* Ensure we get an actual string whose length can be evaluated at
c04062e9 3117 compile-time, not an expression containing a string. This is
3118 because the latter will potentially produce pessimized code
3119 when used to produce the return value. */
3120 src = TREE_VALUE (TREE_CHAIN (arglist));
681fab1e 3121 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
727c62dd 3122 return expand_movstr (TREE_VALUE (arglist),
3123 TREE_VALUE (TREE_CHAIN (arglist)),
3124 target, /*endp=*/2);
3b824fa6 3125
6411575e 3126 dst = TREE_VALUE (arglist);
7d705727 3127 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3128 narglist = build_tree_list (NULL_TREE, lenp1);
727c62dd 3129 narglist = tree_cons (NULL_TREE, src, narglist);
3130 narglist = tree_cons (NULL_TREE, dst, narglist);
3131 ret = expand_builtin_mempcpy (narglist, target, mode, /*endp=*/2);
3132
3133 if (ret)
3134 return ret;
3135
3136 if (TREE_CODE (len) == INTEGER_CST)
3137 {
3138 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3139
3140 if (GET_CODE (len_rtx) == CONST_INT)
3141 {
3142 ret = expand_builtin_strcpy (arglist, target, mode);
3143
3144 if (ret)
3145 {
3146 if (! target)
7ac87324 3147 {
3148 if (mode != VOIDmode)
3149 target = gen_reg_rtx (mode);
3150 else
3151 target = gen_reg_rtx (GET_MODE (ret));
3152 }
727c62dd 3153 if (GET_MODE (target) != GET_MODE (ret))
3154 ret = gen_lowpart (GET_MODE (target), ret);
3155
3156 ret = emit_move_insn (target,
3157 plus_constant (ret,
3158 INTVAL (len_rtx)));
64db345d 3159 gcc_assert (ret);
727c62dd 3160
3161 return target;
3162 }
3163 }
3164 }
3165
3166 return expand_movstr (TREE_VALUE (arglist),
3167 TREE_VALUE (TREE_CHAIN (arglist)),
3168 target, /*endp=*/2);
3b824fa6 3169 }
3170}
3171
6840589f 3172/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3173 bytes from constant string DATA + OFFSET and return it as target
3174 constant. */
3175
3176static rtx
aecda0d6 3177builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3178 enum machine_mode mode)
6840589f 3179{
3180 const char *str = (const char *) data;
3181
3182 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3183 return const0_rtx;
3184
3185 return c_readstr (str + offset, mode);
3186}
3187
ed09096d 3188/* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3189 if we failed the caller should emit a normal call. */
3190
3191static rtx
aecda0d6 3192expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode)
ed09096d 3193{
0eb671f7 3194 if (!validate_arglist (arglist,
3195 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
ed09096d 3196 return 0;
3197 else
3198 {
681fab1e 3199 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
ed09096d 3200 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6f428e8b 3201 tree fn;
ed09096d 3202
3203 /* We must be passed a constant len parameter. */
3204 if (TREE_CODE (len) != INTEGER_CST)
3205 return 0;
3206
3207 /* If the len parameter is zero, return the dst parameter. */
df5c389e 3208 if (integer_zerop (len))
9342ee68 3209 {
3210 /* Evaluate and ignore the src argument in case it has
3211 side-effects. */
ed09096d 3212 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
3213 VOIDmode, EXPAND_NORMAL);
bf8e3599 3214 /* Return the dst parameter. */
ed09096d 3215 return expand_expr (TREE_VALUE (arglist), target, mode,
3216 EXPAND_NORMAL);
3217 }
6840589f 3218
ed09096d 3219 /* Now, we must be passed a constant src ptr parameter. */
6840589f 3220 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
ed09096d 3221 return 0;
3222
3223 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3224
3225 /* We're required to pad with trailing zeros if the requested
6840589f 3226 len is greater than strlen(s2)+1. In that case try to
3227 use store_by_pieces, if it fails, punt. */
ed09096d 3228 if (tree_int_cst_lt (slen, len))
6840589f 3229 {
3230 tree dest = TREE_VALUE (arglist);
27d0c333 3231 unsigned int dest_align
3232 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
6840589f 3233 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3234 rtx dest_mem;
3235
27d0c333 3236 if (!p || dest_align == 0 || !host_integerp (len, 1)
3237 || !can_store_by_pieces (tree_low_cst (len, 1),
6840589f 3238 builtin_strncpy_read_str,
b9a7cc69 3239 (void *) p, dest_align))
6840589f 3240 return 0;
3241
3242 dest_mem = get_memory_rtx (dest);
27d0c333 3243 store_by_pieces (dest_mem, tree_low_cst (len, 1),
6840589f 3244 builtin_strncpy_read_str,
b9a7cc69 3245 (void *) p, dest_align, 0);
e5716f7e 3246 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3247 dest_mem = convert_memory_address (ptr_mode, dest_mem);
e5716f7e 3248 return dest_mem;
6840589f 3249 }
bf8e3599 3250
ed09096d 3251 /* OK transform into builtin memcpy. */
0a68165a 3252 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
6f428e8b 3253 if (!fn)
9342ee68 3254 return 0;
6f428e8b 3255 return expand_expr (build_function_call_expr (fn, arglist),
9342ee68 3256 target, mode, EXPAND_NORMAL);
ed09096d 3257 }
3258}
3259
ecc318ff 3260/* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3261 bytes from constant string DATA + OFFSET and return it as target
3262 constant. */
3263
3264static rtx
aecda0d6 3265builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3266 enum machine_mode mode)
ecc318ff 3267{
3268 const char *c = (const char *) data;
3269 char *p = alloca (GET_MODE_SIZE (mode));
3270
3271 memset (p, *c, GET_MODE_SIZE (mode));
3272
3273 return c_readstr (p, mode);
3274}
3275
a7ec6974 3276/* Callback routine for store_by_pieces. Return the RTL of a register
3277 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3278 char value given in the RTL register data. For example, if mode is
3279 4 bytes wide, return the RTL for 0x01010101*data. */
3280
3281static rtx
aecda0d6 3282builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3283 enum machine_mode mode)
a7ec6974 3284{
3285 rtx target, coeff;
3286 size_t size;
3287 char *p;
3288
3289 size = GET_MODE_SIZE (mode);
f0ce3b1f 3290 if (size == 1)
3291 return (rtx) data;
a7ec6974 3292
3293 p = alloca (size);
3294 memset (p, 1, size);
3295 coeff = c_readstr (p, mode);
3296
f0ce3b1f 3297 target = convert_to_mode (mode, (rtx) data, 1);
a7ec6974 3298 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3299 return force_reg (mode, target);
3300}
3301
53800dbe 3302/* Expand expression EXP, which is a call to the memset builtin. Return 0
6f428e8b 3303 if we failed the caller should emit a normal call, otherwise try to get
3304 the result in TARGET, if convenient (and in mode MODE if that's
3305 convenient). */
902de8ed 3306
53800dbe 3307static rtx
aecda0d6 3308expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
53800dbe 3309{
0eb671f7 3310 if (!validate_arglist (arglist,
3311 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 3312 return 0;
3313 else
3314 {
3315 tree dest = TREE_VALUE (arglist);
3316 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3317 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
ecc318ff 3318 char c;
53800dbe 3319
27d0c333 3320 unsigned int dest_align
3321 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
53800dbe 3322 rtx dest_mem, dest_addr, len_rtx;
3323
bf8e3599 3324 /* If DEST is not a pointer type, don't do this
53800dbe 3325 operation in-line. */
3326 if (dest_align == 0)
3327 return 0;
3328
6f428e8b 3329 /* If the LEN parameter is zero, return DEST. */
9c8a1629 3330 if (integer_zerop (len))
9342ee68 3331 {
3332 /* Evaluate and ignore VAL in case it has side-effects. */
3333 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3334 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3335 }
6f428e8b 3336
ecc318ff 3337 if (TREE_CODE (val) != INTEGER_CST)
9342ee68 3338 {
3339 rtx val_rtx;
a7ec6974 3340
9342ee68 3341 if (!host_integerp (len, 1))
3342 return 0;
a7ec6974 3343
9342ee68 3344 if (optimize_size && tree_low_cst (len, 1) > 1)
3345 return 0;
a7ec6974 3346
9342ee68 3347 /* Assume that we can memset by pieces if we can store the
3348 * the coefficients by pieces (in the required modes).
3349 * We can't pass builtin_memset_gen_str as that emits RTL. */
3350 c = 1;
a7ec6974 3351 if (!can_store_by_pieces (tree_low_cst (len, 1),
3352 builtin_memset_read_str,
b9a7cc69 3353 &c, dest_align))
a7ec6974 3354 return 0;
3355
9342ee68 3356 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3357 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3358 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3359 val_rtx);
a7ec6974 3360 dest_mem = get_memory_rtx (dest);
3361 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3362 builtin_memset_gen_str,
b9a7cc69 3363 val_rtx, dest_align, 0);
e5716f7e 3364 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3365 dest_mem = convert_memory_address (ptr_mode, dest_mem);
e5716f7e 3366 return dest_mem;
9342ee68 3367 }
53800dbe 3368
ecc318ff 3369 if (target_char_cast (val, &c))
53800dbe 3370 return 0;
3371
ecc318ff 3372 if (c)
3373 {
27d0c333 3374 if (!host_integerp (len, 1))
ecc318ff 3375 return 0;
8a06f2d4 3376 if (!can_store_by_pieces (tree_low_cst (len, 1),
b9a7cc69 3377 builtin_memset_read_str, &c,
8a06f2d4 3378 dest_align))
ecc318ff 3379 return 0;
3380
3381 dest_mem = get_memory_rtx (dest);
27d0c333 3382 store_by_pieces (dest_mem, tree_low_cst (len, 1),
ecc318ff 3383 builtin_memset_read_str,
b9a7cc69 3384 &c, dest_align, 0);
e5716f7e 3385 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3386 dest_mem = convert_memory_address (ptr_mode, dest_mem);
e5716f7e 3387 return dest_mem;
ecc318ff 3388 }
3389
53800dbe 3390 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
53800dbe 3391
3392 dest_mem = get_memory_rtx (dest);
2a631e19 3393 set_mem_align (dest_mem, dest_align);
2a631e19 3394 dest_addr = clear_storage (dest_mem, len_rtx);
53800dbe 3395
3396 if (dest_addr == 0)
e5716f7e 3397 {
3398 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
85d654dd 3399 dest_addr = convert_memory_address (ptr_mode, dest_addr);
e5716f7e 3400 }
53800dbe 3401
3402 return dest_addr;
3403 }
3404}
3405
ffc83088 3406/* Expand expression EXP, which is a call to the bzero builtin. Return 0
3407 if we failed the caller should emit a normal call. */
27d0c333 3408
ffc83088 3409static rtx
aecda0d6 3410expand_builtin_bzero (tree arglist)
ffc83088 3411{
7369e7ba 3412 tree dest, size, newarglist;
ffc83088 3413
0eb671f7 3414 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7369e7ba 3415 return NULL_RTX;
ffc83088 3416
0eb671f7 3417 dest = TREE_VALUE (arglist);
3418 size = TREE_VALUE (TREE_CHAIN (arglist));
bf8e3599 3419
7369e7ba 3420 /* New argument list transforming bzero(ptr x, int y) to
6f428e8b 3421 memset(ptr x, int 0, size_t y). This is done this way
3422 so that if it isn't expanded inline, we fallback to
3423 calling bzero instead of memset. */
bf8e3599 3424
d7aeca92 3425 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
7369e7ba 3426 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3427 newarglist = tree_cons (NULL_TREE, dest, newarglist);
ffc83088 3428
80cd7a5e 3429 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
ffc83088 3430}
3431
7a3f89b5 3432/* Expand expression EXP, which is a call to the memcmp built-in function.
53800dbe 3433 ARGLIST is the argument list for this call. Return 0 if we failed and the
3434 caller should emit a normal call, otherwise try to get the result in
6f428e8b 3435 TARGET, if convenient (and in mode MODE, if that's convenient). */
27d0c333 3436
53800dbe 3437static rtx
aecda0d6 3438expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3439 enum machine_mode mode)
53800dbe 3440{
6f428e8b 3441 tree arg1, arg2, len;
d470d782 3442 const char *p1, *p2;
6f428e8b 3443
0eb671f7 3444 if (!validate_arglist (arglist,
9342ee68 3445 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 3446 return 0;
53800dbe 3447
6f428e8b 3448 arg1 = TREE_VALUE (arglist);
3449 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3450 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3451
3452 /* If the len parameter is zero, return zero. */
9c8a1629 3453 if (integer_zerop (len))
6f428e8b 3454 {
3455 /* Evaluate and ignore arg1 and arg2 in case they have
3456 side-effects. */
3457 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3458 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3459 return const0_rtx;
3460 }
3461
6217c238 3462 /* If both arguments are equal (and not volatile), return zero. */
3463 if (operand_equal_p (arg1, arg2, 0))
3464 {
3465 /* Evaluate and ignore len in case it has side-effects. */
3466 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3467 return const0_rtx;
3468 }
3469
d470d782 3470 p1 = c_getstr (arg1);
3471 p2 = c_getstr (arg2);
3472
3473 /* If all arguments are constant, and the value of len is not greater
3474 than the lengths of arg1 and arg2, evaluate at compile-time. */
3475 if (host_integerp (len, 1) && p1 && p2
df5c389e 3476 && compare_tree_int (len, strlen (p1) + 1) <= 0
3477 && compare_tree_int (len, strlen (p2) + 1) <= 0)
d470d782 3478 {
3479 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
df5c389e 3480
d470d782 3481 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3482 }
3483
6f428e8b 3484 /* If len parameter is one, return an expression corresponding to
3485 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9c8a1629 3486 if (integer_onep (len))
6f428e8b 3487 {
3488 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3489 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3490 tree ind1 =
3491 fold (build1 (CONVERT_EXPR, integer_type_node,
9342ee68 3492 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3493 fold_convert (cst_uchar_ptr_node, arg1))));
6f428e8b 3494 tree ind2 =
3495 fold (build1 (CONVERT_EXPR, integer_type_node,
9342ee68 3496 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3497 fold_convert (cst_uchar_ptr_node, arg2))));
05abc81b 3498 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
6f428e8b 3499 return expand_expr (result, target, mode, EXPAND_NORMAL);
3500 }
3501
b428c0a5 3502#if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
53800dbe 3503 {
0cd832f0 3504 rtx arg1_rtx, arg2_rtx, arg3_rtx;
53800dbe 3505 rtx result;
0cd832f0 3506 rtx insn;
53800dbe 3507
3508 int arg1_align
3509 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3510 int arg2_align
3511 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
b428c0a5 3512 enum machine_mode insn_mode;
3513
3514#ifdef HAVE_cmpmemsi
3515 if (HAVE_cmpmemsi)
3516 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3517 else
3518#endif
3519#ifdef HAVE_cmpstrsi
3520 if (HAVE_cmpstrsi)
3521 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3522 else
3523#endif
0862b7e9 3524 return 0;
53800dbe 3525
3526 /* If we don't have POINTER_TYPE, call the function. */
3527 if (arg1_align == 0 || arg2_align == 0)
3528 return 0;
3529
3530 /* Make a place to write the result of the instruction. */
3531 result = target;
3532 if (! (result != 0
8ad4c111 3533 && REG_P (result) && GET_MODE (result) == insn_mode
53800dbe 3534 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3535 result = gen_reg_rtx (insn_mode);
3536
0cd832f0 3537 arg1_rtx = get_memory_rtx (arg1);
3538 arg2_rtx = get_memory_rtx (arg2);
3539 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
b428c0a5 3540#ifdef HAVE_cmpmemsi
3541 if (HAVE_cmpmemsi)
3542 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3543 GEN_INT (MIN (arg1_align, arg2_align)));
0cd832f0 3544 else
b428c0a5 3545#endif
3546#ifdef HAVE_cmpstrsi
3547 if (HAVE_cmpstrsi)
0cd832f0 3548 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3549 GEN_INT (MIN (arg1_align, arg2_align)));
b428c0a5 3550 else
3551#endif
64db345d 3552 gcc_unreachable ();
0cd832f0 3553
3554 if (insn)
3555 emit_insn (insn);
3556 else
2c5d421b 3557 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
0cd832f0 3558 TYPE_MODE (integer_type_node), 3,
3559 XEXP (arg1_rtx, 0), Pmode,
3560 XEXP (arg2_rtx, 0), Pmode,
3561 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
78a8ed03 3562 TYPE_UNSIGNED (sizetype)),
0cd832f0 3563 TYPE_MODE (sizetype));
53800dbe 3564
3565 /* Return the value in the proper mode for this function. */
3566 mode = TYPE_MODE (TREE_TYPE (exp));
3567 if (GET_MODE (result) == mode)
3568 return result;
3569 else if (target != 0)
3570 {
3571 convert_move (target, result, 0);
3572 return target;
3573 }
3574 else
3575 return convert_to_mode (mode, result, 0);
3576 }
83d79705 3577#endif
53800dbe 3578
6f428e8b 3579 return 0;
3580}
3581
53800dbe 3582/* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3583 if we failed the caller should emit a normal call, otherwise try to get
3584 the result in TARGET, if convenient. */
902de8ed 3585
53800dbe 3586static rtx
aecda0d6 3587expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
53800dbe 3588{
3589 tree arglist = TREE_OPERAND (exp, 1);
7a3f89b5 3590 tree arg1, arg2;
83d79705 3591 const char *p1, *p2;
53800dbe 3592
0eb671f7 3593 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
53800dbe 3594 return 0;
902de8ed 3595
83d79705 3596 arg1 = TREE_VALUE (arglist);
3597 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3598
6217c238 3599 /* If both arguments are equal (and not volatile), return zero. */
3600 if (operand_equal_p (arg1, arg2, 0))
3601 return const0_rtx;
3602
83d79705 3603 p1 = c_getstr (arg1);
3604 p2 = c_getstr (arg2);
3605
3606 if (p1 && p2)
3607 {
ef6c187e 3608 const int i = strcmp (p1, p2);
3609 return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx));
83d79705 3610 }
3611
ef6c187e 3612 /* If either arg is "", return an expression corresponding to
3613 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
3614 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
3615 {
3616 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3617 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3618 tree ind1 =
3619 fold (build1 (CONVERT_EXPR, integer_type_node,
3620 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3621 fold_convert (cst_uchar_ptr_node, arg1))));
ef6c187e 3622 tree ind2 =
3623 fold (build1 (CONVERT_EXPR, integer_type_node,
3624 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3625 fold_convert (cst_uchar_ptr_node, arg2))));
05abc81b 3626 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
ef6c187e 3627 return expand_expr (result, target, mode, EXPAND_NORMAL);
3628 }
bf8e3599 3629
7a3f89b5 3630#ifdef HAVE_cmpstrsi
3631 if (HAVE_cmpstrsi)
3632 {
3633 tree len, len1, len2;
3634 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3635 rtx result, insn;
3f8aefe2 3636 tree fndecl;
902de8ed 3637
7a3f89b5 3638 int arg1_align
3639 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3640 int arg2_align
3641 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3642 enum machine_mode insn_mode
3643 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
902de8ed 3644
681fab1e 3645 len1 = c_strlen (arg1, 1);
3646 len2 = c_strlen (arg2, 1);
7a3f89b5 3647
3648 if (len1)
3649 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3650 if (len2)
3651 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3652
3653 /* If we don't have a constant length for the first, use the length
3654 of the second, if we know it. We don't require a constant for
3655 this case; some cost analysis could be done if both are available
3656 but neither is constant. For now, assume they're equally cheap,
3657 unless one has side effects. If both strings have constant lengths,
3658 use the smaller. */
3659
3660 if (!len1)
3661 len = len2;
3662 else if (!len2)
3663 len = len1;
3664 else if (TREE_SIDE_EFFECTS (len1))
3665 len = len2;
3666 else if (TREE_SIDE_EFFECTS (len2))
3667 len = len1;
3668 else if (TREE_CODE (len1) != INTEGER_CST)
3669 len = len2;
3670 else if (TREE_CODE (len2) != INTEGER_CST)
3671 len = len1;
3672 else if (tree_int_cst_lt (len1, len2))
3673 len = len1;
3674 else
3675 len = len2;
53800dbe 3676
7a3f89b5 3677 /* If both arguments have side effects, we cannot optimize. */
3678 if (!len || TREE_SIDE_EFFECTS (len))
3679 return 0;
53800dbe 3680
7a3f89b5 3681 /* If we don't have POINTER_TYPE, call the function. */
3682 if (arg1_align == 0 || arg2_align == 0)
3683 return 0;
902de8ed 3684
7a3f89b5 3685 /* Make a place to write the result of the instruction. */
3686 result = target;
3687 if (! (result != 0
8ad4c111 3688 && REG_P (result) && GET_MODE (result) == insn_mode
7a3f89b5 3689 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3690 result = gen_reg_rtx (insn_mode);
53800dbe 3691
3f8aefe2 3692 /* Stabilize the arguments in case gen_cmpstrsi fails. */
4ee9c684 3693 arg1 = builtin_save_expr (arg1);
3694 arg2 = builtin_save_expr (arg2);
3f8aefe2 3695
7a3f89b5 3696 arg1_rtx = get_memory_rtx (arg1);
3697 arg2_rtx = get_memory_rtx (arg2);
3698 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3699 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3700 GEN_INT (MIN (arg1_align, arg2_align)));
3f8aefe2 3701 if (insn)
3702 {
3703 emit_insn (insn);
3704
3705 /* Return the value in the proper mode for this function. */
3706 mode = TYPE_MODE (TREE_TYPE (exp));
3707 if (GET_MODE (result) == mode)
3708 return result;
3709 if (target == 0)
3710 return convert_to_mode (mode, result, 0);
3711 convert_move (target, result, 0);
3712 return target;
3713 }
902de8ed 3714
3f8aefe2 3715 /* Expand the library call ourselves using a stabilized argument
3716 list to avoid re-evaluating the function's arguments twice. */
3717 arglist = build_tree_list (NULL_TREE, arg2);
3718 arglist = tree_cons (NULL_TREE, arg1, arglist);
3719 fndecl = get_callee_fndecl (exp);
3720 exp = build_function_call_expr (fndecl, arglist);
3721 return expand_call (exp, target, target == const0_rtx);
7a3f89b5 3722 }
3723#endif
3724 return 0;
83d79705 3725}
53800dbe 3726
ed09096d 3727/* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3728 if we failed the caller should emit a normal call, otherwise try to get
3729 the result in TARGET, if convenient. */
27d0c333 3730
ed09096d 3731static rtx
aecda0d6 3732expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
ed09096d 3733{
3734 tree arglist = TREE_OPERAND (exp, 1);
3735 tree arg1, arg2, arg3;
3736 const char *p1, *p2;
3737
0eb671f7 3738 if (!validate_arglist (arglist,
3739 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
ed09096d 3740 return 0;
3741
3742 arg1 = TREE_VALUE (arglist);
3743 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3744 arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3745
ed09096d 3746 /* If the len parameter is zero, return zero. */
9c8a1629 3747 if (integer_zerop (arg3))
9342ee68 3748 {
3749 /* Evaluate and ignore arg1 and arg2 in case they have
3750 side-effects. */
3751 expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
3752 expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
3753 return const0_rtx;
3754 }
ed09096d 3755
6217c238 3756 /* If arg1 and arg2 are equal (and not volatile), return zero. */
3757 if (operand_equal_p (arg1, arg2, 0))
3758 {
3759 /* Evaluate and ignore arg3 in case it has side-effects. */
3760 expand_expr (arg3, const0_rtx, VOIDmode, EXPAND_NORMAL);
3761 return const0_rtx;
3762 }
3763
ed09096d 3764 p1 = c_getstr (arg1);
3765 p2 = c_getstr (arg2);
3766
3767 /* If all arguments are constant, evaluate at compile-time. */
6e34e617 3768 if (host_integerp (arg3, 1) && p1 && p2)
9342ee68 3769 {
3770 const int r = strncmp (p1, p2, tree_low_cst (arg3, 1));
3771 return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
3772 }
ed09096d 3773
ef6c187e 3774 /* If len == 1 or (either string parameter is "" and (len >= 1)),
6e34e617 3775 return (*(const u_char*)arg1 - *(const u_char*)arg2). */
3776 if (host_integerp (arg3, 1)
3777 && (tree_low_cst (arg3, 1) == 1
3778 || (tree_low_cst (arg3, 1) > 1
3779 && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')))))
ef6c187e 3780 {
3781 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
3782 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
3783 tree ind1 =
3784 fold (build1 (CONVERT_EXPR, integer_type_node,
3785 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3786 fold_convert (cst_uchar_ptr_node, arg1))));
ef6c187e 3787 tree ind2 =
3788 fold (build1 (CONVERT_EXPR, integer_type_node,
3789 build1 (INDIRECT_REF, cst_uchar_node,
d7aeca92 3790 fold_convert (cst_uchar_ptr_node, arg2))));
05abc81b 3791 tree result = fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
ef6c187e 3792 return expand_expr (result, target, mode, EXPAND_NORMAL);
3793 }
ed09096d 3794
6e34e617 3795 /* If c_strlen can determine an expression for one of the string
7a3f89b5 3796 lengths, and it doesn't have side effects, then emit cmpstrsi
3797 using length MIN(strlen(string)+1, arg3). */
3798#ifdef HAVE_cmpstrsi
3799 if (HAVE_cmpstrsi)
3800 {
3801 tree len, len1, len2;
3802 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3803 rtx result, insn;
3f8aefe2 3804 tree fndecl;
6f428e8b 3805
7a3f89b5 3806 int arg1_align
3807 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3808 int arg2_align
3809 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3810 enum machine_mode insn_mode
3811 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
bf8e3599 3812
681fab1e 3813 len1 = c_strlen (arg1, 1);
3814 len2 = c_strlen (arg2, 1);
7a3f89b5 3815
3816 if (len1)
3817 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3818 if (len2)
3819 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3820
3821 /* If we don't have a constant length for the first, use the length
3822 of the second, if we know it. We don't require a constant for
3823 this case; some cost analysis could be done if both are available
3824 but neither is constant. For now, assume they're equally cheap,
3825 unless one has side effects. If both strings have constant lengths,
3826 use the smaller. */
3827
3828 if (!len1)
3829 len = len2;
3830 else if (!len2)
3831 len = len1;
3832 else if (TREE_SIDE_EFFECTS (len1))
3833 len = len2;
3834 else if (TREE_SIDE_EFFECTS (len2))
3835 len = len1;
3836 else if (TREE_CODE (len1) != INTEGER_CST)
3837 len = len2;
3838 else if (TREE_CODE (len2) != INTEGER_CST)
3839 len = len1;
3840 else if (tree_int_cst_lt (len1, len2))
3841 len = len1;
3842 else
3843 len = len2;
6e34e617 3844
7a3f89b5 3845 /* If both arguments have side effects, we cannot optimize. */
3846 if (!len || TREE_SIDE_EFFECTS (len))
3847 return 0;
bf8e3599 3848
7a3f89b5 3849 /* The actual new length parameter is MIN(len,arg3). */
5c9198bd 3850 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3851 fold_convert (TREE_TYPE (len), arg3)));
7a3f89b5 3852
3853 /* If we don't have POINTER_TYPE, call the function. */
3854 if (arg1_align == 0 || arg2_align == 0)
3855 return 0;
3856
3857 /* Make a place to write the result of the instruction. */
3858 result = target;
3859 if (! (result != 0
8ad4c111 3860 && REG_P (result) && GET_MODE (result) == insn_mode
7a3f89b5 3861 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3862 result = gen_reg_rtx (insn_mode);
3863
3f8aefe2 3864 /* Stabilize the arguments in case gen_cmpstrsi fails. */
4ee9c684 3865 arg1 = builtin_save_expr (arg1);
3866 arg2 = builtin_save_expr (arg2);
3867 len = builtin_save_expr (len);
3f8aefe2 3868
7a3f89b5 3869 arg1_rtx = get_memory_rtx (arg1);
3870 arg2_rtx = get_memory_rtx (arg2);
3871 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3872 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3873 GEN_INT (MIN (arg1_align, arg2_align)));
3f8aefe2 3874 if (insn)
3875 {
3876 emit_insn (insn);
3877
3878 /* Return the value in the proper mode for this function. */
3879 mode = TYPE_MODE (TREE_TYPE (exp));
3880 if (GET_MODE (result) == mode)
3881 return result;
3882 if (target == 0)
3883 return convert_to_mode (mode, result, 0);
3884 convert_move (target, result, 0);
3885 return target;
3886 }
7a3f89b5 3887
3f8aefe2 3888 /* Expand the library call ourselves using a stabilized argument
3889 list to avoid re-evaluating the function's arguments twice. */
3890 arglist = build_tree_list (NULL_TREE, len);
3891 arglist = tree_cons (NULL_TREE, arg2, arglist);
3892 arglist = tree_cons (NULL_TREE, arg1, arglist);
3893 fndecl = get_callee_fndecl (exp);
3894 exp = build_function_call_expr (fndecl, arglist);
3895 return expand_call (exp, target, target == const0_rtx);
7a3f89b5 3896 }
3897#endif
3898 return 0;
ed09096d 3899}
3900
49f0327b 3901/* Expand expression EXP, which is a call to the strcat builtin.
3902 Return 0 if we failed the caller should emit a normal call,
3903 otherwise try to get the result in TARGET, if convenient. */
27d0c333 3904
49f0327b 3905static rtx
aecda0d6 3906expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
49f0327b 3907{
0eb671f7 3908 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
49f0327b 3909 return 0;
3910 else
3911 {
3912 tree dst = TREE_VALUE (arglist),
3913 src = TREE_VALUE (TREE_CHAIN (arglist));
3914 const char *p = c_getstr (src);
3915
ca71f89b 3916 if (p)
3917 {
3918 /* If the string length is zero, return the dst parameter. */
3919 if (*p == '\0')
3920 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3921 else if (!optimize_size)
3922 {
3923 /* Otherwise if !optimize_size, see if we can store by
3924 pieces into (dst + strlen(dst)). */
3925 tree newdst, arglist,
3926 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
0862b7e9 3927
ca71f89b 3928 /* This is the length argument. */
3929 arglist = build_tree_list (NULL_TREE,
3930 fold (size_binop (PLUS_EXPR,
3931 c_strlen (src, 0),
3932 ssize_int (1))));
3933 /* Prepend src argument. */
3934 arglist = tree_cons (NULL_TREE, src, arglist);
0862b7e9 3935
ca71f89b 3936 /* We're going to use dst more than once. */
4ee9c684 3937 dst = builtin_save_expr (dst);
ca71f89b 3938
3939 /* Create strlen (dst). */
3940 newdst =
3941 fold (build_function_call_expr (strlen_fn,
3942 build_tree_list (NULL_TREE,
3943 dst)));
3944 /* Create (dst + strlen (dst)). */
05abc81b 3945 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
ca71f89b 3946
3947 /* Prepend the new dst argument. */
3948 arglist = tree_cons (NULL_TREE, newdst, arglist);
0862b7e9 3949
ca71f89b 3950 /* We don't want to get turned into a memcpy if the
3951 target is const0_rtx, i.e. when the return value
3952 isn't used. That would produce pessimized code so
3953 pass in a target of zero, it should never actually be
3954 used. If this was successful return the original
3955 dst, not the result of mempcpy. */
3956 if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0))
3957 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3958 else
3959 return 0;
3960 }
3961 }
49f0327b 3962
3963 return 0;
3964 }
3965}
3966
3967/* Expand expression EXP, which is a call to the strncat builtin.
3968 Return 0 if we failed the caller should emit a normal call,
3969 otherwise try to get the result in TARGET, if convenient. */
27d0c333 3970
49f0327b 3971static rtx
aecda0d6 3972expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
49f0327b 3973{
0eb671f7 3974 if (!validate_arglist (arglist,
3975 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
49f0327b 3976 return 0;
3977 else
3978 {
3979 tree dst = TREE_VALUE (arglist),
3980 src = TREE_VALUE (TREE_CHAIN (arglist)),
3981 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3982 const char *p = c_getstr (src);
3983
3984 /* If the requested length is zero, or the src parameter string
3985 length is zero, return the dst parameter. */
b527e67e 3986 if (integer_zerop (len) || (p && *p == '\0'))
9342ee68 3987 {
49f0327b 3988 /* Evaluate and ignore the src and len parameters in case
3989 they have side-effects. */
3990 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3991 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
3992 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3993 }
3994
3995 /* If the requested len is greater than or equal to the string
3996 length, call strcat. */
3997 if (TREE_CODE (len) == INTEGER_CST && p
3998 && compare_tree_int (len, strlen (p)) >= 0)
9342ee68 3999 {
df5c389e 4000 tree newarglist
4001 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
0a68165a 4002 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
bf8e3599 4003
49f0327b 4004 /* If the replacement _DECL isn't initialized, don't do the
2c0e001b 4005 transformation. */
49f0327b 4006 if (!fn)
4007 return 0;
4008
7e15618b 4009 return expand_expr (build_function_call_expr (fn, newarglist),
4010 target, mode, EXPAND_NORMAL);
49f0327b 4011 }
4012 return 0;
4013 }
4014}
4015
4016/* Expand expression EXP, which is a call to the strspn builtin.
4017 Return 0 if we failed the caller should emit a normal call,
4018 otherwise try to get the result in TARGET, if convenient. */
27d0c333 4019
49f0327b 4020static rtx
aecda0d6 4021expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
49f0327b 4022{
0eb671f7 4023 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
49f0327b 4024 return 0;
4025 else
4026 {
4027 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
4028 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
bf8e3599 4029
49f0327b 4030 /* If both arguments are constants, evaluate at compile-time. */
4031 if (p1 && p2)
9342ee68 4032 {
49f0327b 4033 const size_t r = strspn (p1, p2);
4034 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
4035 }
bf8e3599 4036
fb1f44c5 4037 /* If either argument is "", return 0. */
4038 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9342ee68 4039 {
fb1f44c5 4040 /* Evaluate and ignore both arguments in case either one has
49f0327b 4041 side-effects. */
4042 expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL);
fb1f44c5 4043 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
49f0327b 4044 return const0_rtx;
4045 }
4046 return 0;
4047 }
4048}
4049
4050/* Expand expression EXP, which is a call to the strcspn builtin.
4051 Return 0 if we failed the caller should emit a normal call,
4052 otherwise try to get the result in TARGET, if convenient. */
27d0c333 4053
49f0327b 4054static rtx
aecda0d6 4055expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
49f0327b 4056{
0eb671f7 4057 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
49f0327b 4058 return 0;
4059 else
4060 {
4061 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
4062 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
bf8e3599 4063
49f0327b 4064 /* If both arguments are constants, evaluate at compile-time. */
4065 if (p1 && p2)
9342ee68 4066 {
49f0327b 4067 const size_t r = strcspn (p1, p2);
4068 return expand_expr (size_int (r), target, mode, EXPAND_NORMAL);
4069 }
bf8e3599 4070
fb1f44c5 4071 /* If the first argument is "", return 0. */
4072 if (p1 && *p1 == '\0')
9342ee68 4073 {
fb1f44c5 4074 /* Evaluate and ignore argument s2 in case it has
4075 side-effects. */
4076 expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL);
4077 return const0_rtx;
4078 }
4079
49f0327b 4080 /* If the second argument is "", return __builtin_strlen(s1). */
4081 if (p2 && *p2 == '\0')
9342ee68 4082 {
7e15618b 4083 tree newarglist = build_tree_list (NULL_TREE, s1),
0a68165a 4084 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
bf8e3599 4085
49f0327b 4086 /* If the replacement _DECL isn't initialized, don't do the
2c0e001b 4087 transformation. */
49f0327b 4088 if (!fn)
4089 return 0;
4090
7e15618b 4091 return expand_expr (build_function_call_expr (fn, newarglist),
4092 target, mode, EXPAND_NORMAL);
49f0327b 4093 }
4094 return 0;
4095 }
4096}
4097
a66c9326 4098/* Expand a call to __builtin_saveregs, generating the result in TARGET,
4099 if that's convenient. */
902de8ed 4100
a66c9326 4101rtx
aecda0d6 4102expand_builtin_saveregs (void)
53800dbe 4103{
a66c9326 4104 rtx val, seq;
53800dbe 4105
4106 /* Don't do __builtin_saveregs more than once in a function.
4107 Save the result of the first call and reuse it. */
4108 if (saveregs_value != 0)
4109 return saveregs_value;
53800dbe 4110
a66c9326 4111 /* When this function is called, it means that registers must be
4112 saved on entry to this function. So we migrate the call to the
4113 first insn of this function. */
4114
4115 start_sequence ();
53800dbe 4116
a66c9326 4117 /* Do whatever the machine needs done in this case. */
45550790 4118 val = targetm.calls.expand_builtin_saveregs ();
53800dbe 4119
a66c9326 4120 seq = get_insns ();
4121 end_sequence ();
53800dbe 4122
a66c9326 4123 saveregs_value = val;
53800dbe 4124
31d3e01c 4125 /* Put the insns after the NOTE that starts the function. If this
4126 is inside a start_sequence, make the outer-level insn chain current, so
a66c9326 4127 the code is placed at the start of the function. */
4128 push_topmost_sequence ();
0ec80471 4129 emit_insn_after (seq, entry_of_function ());
a66c9326 4130 pop_topmost_sequence ();
4131
4132 return val;
53800dbe 4133}
4134
4135/* __builtin_args_info (N) returns word N of the arg space info
4136 for the current function. The number and meanings of words
4137 is controlled by the definition of CUMULATIVE_ARGS. */
f7c44134 4138
53800dbe 4139static rtx
aecda0d6 4140expand_builtin_args_info (tree arglist)
53800dbe 4141{
53800dbe 4142 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4143 int *word_ptr = (int *) &current_function_args_info;
53800dbe 4144
64db345d 4145 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
53800dbe 4146
4147 if (arglist != 0)
4148 {
27d0c333 4149 if (!host_integerp (TREE_VALUE (arglist), 0))
eb586f2c 4150 error ("argument of %<__builtin_args_info%> must be constant");
53800dbe 4151 else
4152 {
27d0c333 4153 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
53800dbe 4154
27d0c333 4155 if (wordnum < 0 || wordnum >= nwords)
eb586f2c 4156 error ("argument of %<__builtin_args_info%> out of range");
53800dbe 4157 else
4158 return GEN_INT (word_ptr[wordnum]);
4159 }
4160 }
4161 else
eb586f2c 4162 error ("missing argument in %<__builtin_args_info%>");
53800dbe 4163
4164 return const0_rtx;
53800dbe 4165}
4166
a66c9326 4167/* Expand ARGLIST, from a call to __builtin_next_arg. */
27d0c333 4168
53800dbe 4169static rtx
aecda0d6 4170expand_builtin_next_arg (tree arglist)
53800dbe 4171{
53800dbe 4172 tree fntype = TREE_TYPE (current_function_decl);
4173
7ccc713a 4174 if (TYPE_ARG_TYPES (fntype) == 0
4175 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4176 == void_type_node))
53800dbe 4177 {
eb586f2c 4178 error ("%<va_start%> used in function with fixed args");
53800dbe 4179 return const0_rtx;
4180 }
4181
4182 if (arglist)
4183 {
4184 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
4185 tree arg = TREE_VALUE (arglist);
4186
4187 /* Strip off all nops for the sake of the comparison. This
bf8e3599 4188 is not quite the same as STRIP_NOPS. It does more.
53800dbe 4189 We must also strip off INDIRECT_EXPR for C++ reference
4190 parameters. */
4191 while (TREE_CODE (arg) == NOP_EXPR
4192 || TREE_CODE (arg) == CONVERT_EXPR
4193 || TREE_CODE (arg) == NON_LVALUE_EXPR
4194 || TREE_CODE (arg) == INDIRECT_REF)
4195 arg = TREE_OPERAND (arg, 0);
4196 if (arg != last_parm)
eb586f2c 4197 warning ("second parameter of %<va_start%> not last named argument");
53800dbe 4198 }
7ccc713a 4199 else
53800dbe 4200 /* Evidently an out of date version of <stdarg.h>; can't validate
4201 va_start's second argument, but can still work as intended. */
eb586f2c 4202 warning ("%<__builtin_next_arg%> called without an argument");
53800dbe 4203
4204 return expand_binop (Pmode, add_optab,
4205 current_function_internal_arg_pointer,
4206 current_function_arg_offset_rtx,
4207 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4208}
4209
a66c9326 4210/* Make it easier for the backends by protecting the valist argument
4211 from multiple evaluations. */
4212
4213static tree
aecda0d6 4214stabilize_va_list (tree valist, int needs_lvalue)
a66c9326 4215{
11a61dea 4216 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
a66c9326 4217 {
2d47cc32 4218 if (TREE_SIDE_EFFECTS (valist))
4219 valist = save_expr (valist);
11a61dea 4220
2d47cc32 4221 /* For this case, the backends will be expecting a pointer to
4222 TREE_TYPE (va_list_type_node), but it's possible we've
4223 actually been given an array (an actual va_list_type_node).
4224 So fix it. */
4225 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8a15c04a 4226 {
bf8e3599 4227 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
fcdd3ab3 4228 valist = build_fold_addr_expr_with_type (valist, p1);
8a15c04a 4229 }
a66c9326 4230 }
11a61dea 4231 else
a66c9326 4232 {
2d47cc32 4233 tree pt;
11a61dea 4234
2d47cc32 4235 if (! needs_lvalue)
4236 {
11a61dea 4237 if (! TREE_SIDE_EFFECTS (valist))
4238 return valist;
bf8e3599 4239
11a61dea 4240 pt = build_pointer_type (va_list_type_node);
2d47cc32 4241 valist = fold (build1 (ADDR_EXPR, pt, valist));
a66c9326 4242 TREE_SIDE_EFFECTS (valist) = 1;
a66c9326 4243 }
2d47cc32 4244
11a61dea 4245 if (TREE_SIDE_EFFECTS (valist))
2d47cc32 4246 valist = save_expr (valist);
fcdd3ab3 4247 valist = build_fold_indirect_ref (valist);
a66c9326 4248 }
4249
4250 return valist;
4251}
4252
2e15d750 4253/* The "standard" definition of va_list is void*. */
4254
4255tree
4256std_build_builtin_va_list (void)
4257{
4258 return ptr_type_node;
4259}
4260
a66c9326 4261/* The "standard" implementation of va_start: just assign `nextarg' to
4262 the variable. */
27d0c333 4263
a66c9326 4264void
aecda0d6 4265std_expand_builtin_va_start (tree valist, rtx nextarg)
a66c9326 4266{
4267 tree t;
4268
05abc81b 4269 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4270 make_tree (ptr_type_node, nextarg));
a66c9326 4271 TREE_SIDE_EFFECTS (t) = 1;
4272
4273 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4274}
4275
7ccc713a 4276/* Expand ARGLIST, from a call to __builtin_va_start. */
27d0c333 4277
a66c9326 4278static rtx
aecda0d6 4279expand_builtin_va_start (tree arglist)
a66c9326 4280{
4281 rtx nextarg;
7ccc713a 4282 tree chain, valist;
a66c9326 4283
7ccc713a 4284 chain = TREE_CHAIN (arglist);
a66c9326 4285
4286 if (TREE_CHAIN (chain))
eb586f2c 4287 error ("too many arguments to function %<va_start%>");
a66c9326 4288
7ccc713a 4289 nextarg = expand_builtin_next_arg (chain);
a66c9326 4290 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4291
4292#ifdef EXPAND_BUILTIN_VA_START
7df226a2 4293 EXPAND_BUILTIN_VA_START (valist, nextarg);
a66c9326 4294#else
7df226a2 4295 std_expand_builtin_va_start (valist, nextarg);
a66c9326 4296#endif
4297
4298 return const0_rtx;
4299}
4300
a66c9326 4301/* The "standard" implementation of va_arg: read the value from the
4302 current (padded) address and increment by the (padded) size. */
f7c44134 4303
e0eca1fa 4304tree
4305std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
fcdd3ab3 4306{
52cc2eeb 4307 tree addr, t, type_size, rounded_size, valist_tmp;
b8e8e485 4308 unsigned HOST_WIDE_INT align, boundary;
bef380a4 4309 bool indirect;
52cc2eeb 4310
4311#ifdef ARGS_GROW_DOWNWARD
4312 /* All of the alignment and movement below is for args-grow-up machines.
4313 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4314 implement their own specialized gimplify_va_arg_expr routines. */
64db345d 4315 gcc_unreachable ();
52cc2eeb 4316#endif
fcdd3ab3 4317
bef380a4 4318 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4319 if (indirect)
4320 type = build_pointer_type (type);
4321
52cc2eeb 4322 align = PARM_BOUNDARY / BITS_PER_UNIT;
bcff3604 4323 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
fcdd3ab3 4324
52cc2eeb 4325 /* Hoist the valist value into a temporary for the moment. */
ecdeeb37 4326 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4327
fcdd3ab3 4328 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4329 requires greater alignment, we must perform dynamic alignment. */
bcff3604 4330 if (boundary > align)
fcdd3ab3 4331 {
bcff3604 4332 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
ecdeeb37 4333 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
52cc2eeb 4334 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4335 gimplify_and_add (t, pre_p);
4336
bcff3604 4337 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
52cc2eeb 4338 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4339 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
ecdeeb37 4340 gimplify_and_add (t, pre_p);
fcdd3ab3 4341 }
4342
bcff3604 4343 /* Compute the rounded size of the type. */
52cc2eeb 4344 type_size = size_in_bytes (type);
4345 rounded_size = round_up (type_size, align);
4346
fcdd3ab3 4347 /* Reduce rounded_size so it's sharable with the postqueue. */
4348 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4349
4350 /* Get AP. */
ecdeeb37 4351 addr = valist_tmp;
52cc2eeb 4352 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
fcdd3ab3 4353 {
4354 /* Small args are padded downward. */
52cc2eeb 4355 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4356 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4357 size_binop (MINUS_EXPR, rounded_size, type_size)));
4358 t = fold_convert (TREE_TYPE (addr), t);
b8e8e485 4359 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
fcdd3ab3 4360 }
4361
fcdd3ab3 4362 /* Compute new value for AP. */
52cc2eeb 4363 t = fold_convert (TREE_TYPE (valist), rounded_size);
4364 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4365 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
ecdeeb37 4366 gimplify_and_add (t, pre_p);
e0eca1fa 4367
4368 addr = fold_convert (build_pointer_type (type), addr);
fcdd3ab3 4369
bef380a4 4370 if (indirect)
f2462d6c 4371 addr = build_va_arg_indirect_ref (addr);
4372
4373 return build_va_arg_indirect_ref (addr);
4374}
a0930a69 4375
f2462d6c 4376/* Build an indirect-ref expression over the given TREE, which represents a
4377 piece of a va_arg() expansion. */
4378tree
4379build_va_arg_indirect_ref (tree addr)
4380{
a0930a69 4381 addr = build_fold_indirect_ref (addr);
f2462d6c 4382
a0930a69 4383 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4384 mf_mark (addr);
bef380a4 4385
a0930a69 4386 return addr;
433006a9 4387}
4388
fcdd3ab3 4389/* Return a dummy expression of type TYPE in order to keep going after an
4390 error. */
4391
4392static tree
4393dummy_object (tree type)
4394{
4395 tree t = convert (build_pointer_type (type), null_pointer_node);
4396 return build1 (INDIRECT_REF, type, t);
4397}
4398
2799a2b7 4399/* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4400 builtin function, but a very special sort of operator. */
fcdd3ab3 4401
4402enum gimplify_status
4403gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4404{
4405 tree promoted_type, want_va_type, have_va_type;
4406 tree valist = TREE_OPERAND (*expr_p, 0);
4407 tree type = TREE_TYPE (*expr_p);
4408 tree t;
4409
4410 /* Verify that valist is of the proper type. */
fcdd3ab3 4411 want_va_type = va_list_type_node;
4412 have_va_type = TREE_TYPE (valist);
f43dda35 4413
4414 if (have_va_type == error_mark_node)
4415 return GS_ERROR;
4416
fcdd3ab3 4417 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4418 {
4419 /* If va_list is an array type, the argument may have decayed
4420 to a pointer type, e.g. by being passed to another function.
4421 In that case, unwrap both types so that we can compare the
4422 underlying records. */
4423 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4424 || TREE_CODE (have_va_type) == POINTER_TYPE)
4425 {
4426 want_va_type = TREE_TYPE (want_va_type);
4427 have_va_type = TREE_TYPE (have_va_type);
4428 }
4429 }
4430
4431 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4432 {
eb586f2c 4433 error ("first argument to %<va_arg%> not of type %<va_list%>");
e0eca1fa 4434 return GS_ERROR;
fcdd3ab3 4435 }
4436
4437 /* Generate a diagnostic for requesting data of a type that cannot
4438 be passed through `...' due to type promotion at the call site. */
4439 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4440 != type)
4441 {
4442 static bool gave_help;
4443
4444 /* Unfortunately, this is merely undefined, rather than a constraint
4445 violation, so we cannot make this an error. If this call is never
4446 executed, the program is still strictly conforming. */
eb586f2c 4447 warning ("%qT is promoted to %qT when passed through %<...%>",
fcdd3ab3 4448 type, promoted_type);
4449 if (! gave_help)
4450 {
4451 gave_help = true;
eb586f2c 4452 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
fcdd3ab3 4453 promoted_type, type);
4454 }
4455
4456 /* We can, however, treat "undefined" any way we please.
4457 Call abort to encourage the user to fix the program. */
4458 inform ("if this code is reached, the program will abort");
4459 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4460 NULL);
4461 append_to_statement_list (t, pre_p);
4462
4463 /* This is dead code, but go ahead and finish so that the
4464 mode of the result comes out right. */
4465 *expr_p = dummy_object (type);
4466 return GS_ALL_DONE;
4467 }
4468 else
4469 {
4470 /* Make it easier for the backends by protecting the valist argument
4471 from multiple evaluations. */
e0eca1fa 4472 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4473 {
4474 /* For this case, the backends will be expecting a pointer to
4475 TREE_TYPE (va_list_type_node), but it's possible we've
4476 actually been given an array (an actual va_list_type_node).
4477 So fix it. */
4478 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4479 {
4480 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4481 valist = build_fold_addr_expr_with_type (valist, p1);
4482 }
4483 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4484 }
4485 else
4486 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
fcdd3ab3 4487
17a6380d 4488 if (!targetm.gimplify_va_arg_expr)
fcdd3ab3 4489 /* Once most targets are converted this should abort. */
4490 return GS_ALL_DONE;
4491
17a6380d 4492 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
fcdd3ab3 4493 return GS_OK;
4494 }
4495}
4496
a66c9326 4497/* Expand ARGLIST, from a call to __builtin_va_end. */
f7c44134 4498
a66c9326 4499static rtx
aecda0d6 4500expand_builtin_va_end (tree arglist)
a66c9326 4501{
8a15c04a 4502 tree valist = TREE_VALUE (arglist);
4503
8a15c04a 4504 /* Evaluate for side effects, if needed. I hate macros that don't
4505 do that. */
4506 if (TREE_SIDE_EFFECTS (valist))
4507 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
a66c9326 4508
4509 return const0_rtx;
4510}
4511
bf8e3599 4512/* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
a66c9326 4513 builtin rather than just as an assignment in stdarg.h because of the
4514 nastiness of array-type va_list types. */
f7c44134 4515
a66c9326 4516static rtx
aecda0d6 4517expand_builtin_va_copy (tree arglist)
a66c9326 4518{
4519 tree dst, src, t;
4520
4521 dst = TREE_VALUE (arglist);
4522 src = TREE_VALUE (TREE_CHAIN (arglist));
4523
4524 dst = stabilize_va_list (dst, 1);
4525 src = stabilize_va_list (src, 0);
4526
4527 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4528 {
05abc81b 4529 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
a66c9326 4530 TREE_SIDE_EFFECTS (t) = 1;
4531 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4532 }
4533 else
4534 {
11a61dea 4535 rtx dstb, srcb, size;
4536
4537 /* Evaluate to pointers. */
4538 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4539 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4540 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4541 VOIDmode, EXPAND_NORMAL);
4542
85d654dd 4543 dstb = convert_memory_address (Pmode, dstb);
4544 srcb = convert_memory_address (Pmode, srcb);
726ec87c 4545
11a61dea 4546 /* "Dereference" to BLKmode memories. */
4547 dstb = gen_rtx_MEM (BLKmode, dstb);
ab6ab77e 4548 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
2a631e19 4549 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
11a61dea 4550 srcb = gen_rtx_MEM (BLKmode, srcb);
ab6ab77e 4551 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
2a631e19 4552 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
11a61dea 4553
4554 /* Copy. */
0378dbdc 4555 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
a66c9326 4556 }
4557
4558 return const0_rtx;
4559}
4560
53800dbe 4561/* Expand a call to one of the builtin functions __builtin_frame_address or
4562 __builtin_return_address. */
27d0c333 4563
53800dbe 4564static rtx
aecda0d6 4565expand_builtin_frame_address (tree fndecl, tree arglist)
53800dbe 4566{
53800dbe 4567 /* The argument must be a nonnegative integer constant.
4568 It counts the number of frames to scan up the stack.
4569 The value is the return address saved in that frame. */
4570 if (arglist == 0)
4571 /* Warning about missing arg was already issued. */
4572 return const0_rtx;
27d0c333 4573 else if (! host_integerp (TREE_VALUE (arglist), 1))
53800dbe 4574 {
4575 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
eb586f2c 4576 error ("invalid arg to %<__builtin_frame_address%>");
53800dbe 4577 else
eb586f2c 4578 error ("invalid arg to %<__builtin_return_address%>");
53800dbe 4579 return const0_rtx;
4580 }
4581 else
4582 {
27d0c333 4583 rtx tem
4584 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4585 tree_low_cst (TREE_VALUE (arglist), 1),
4586 hard_frame_pointer_rtx);
53800dbe 4587
4588 /* Some ports cannot access arbitrary stack frames. */
4589 if (tem == NULL)
4590 {
4591 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
eb586f2c 4592 warning ("unsupported arg to %<__builtin_frame_address%>");
53800dbe 4593 else
eb586f2c 4594 warning ("unsupported arg to %<__builtin_return_address%>");
53800dbe 4595 return const0_rtx;
4596 }
4597
4598 /* For __builtin_frame_address, return what we've got. */
4599 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4600 return tem;
4601
8ad4c111 4602 if (!REG_P (tem)
53800dbe 4603 && ! CONSTANT_P (tem))
4604 tem = copy_to_mode_reg (Pmode, tem);
4605 return tem;
4606 }
4607}
4608
4609/* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4610 we failed and the caller should emit a normal call, otherwise try to get
4611 the result in TARGET, if convenient. */
15c6cf6b 4612
53800dbe 4613static rtx
aecda0d6 4614expand_builtin_alloca (tree arglist, rtx target)
53800dbe 4615{
4616 rtx op0;
15c6cf6b 4617 rtx result;
53800dbe 4618
4ee9c684 4619 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4620 should always expand to function calls. These can be intercepted
4621 in libmudflap. */
4622 if (flag_mudflap)
4623 return 0;
4624
0eb671f7 4625 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
53800dbe 4626 return 0;
4627
4628 /* Compute the argument. */
4629 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4630
4631 /* Allocate the desired space. */
15c6cf6b 4632 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
85d654dd 4633 result = convert_memory_address (ptr_mode, result);
15c6cf6b 4634
4635 return result;
53800dbe 4636}
4637
6a08d0ab 4638/* Expand a call to a unary builtin. The arguments are in ARGLIST.
53800dbe 4639 Return 0 if a normal call should be emitted rather than expanding the
4640 function in-line. If convenient, the result should be placed in TARGET.
4641 SUBTARGET may be used as the target for computing one of EXP's operands. */
15c6cf6b 4642
53800dbe 4643static rtx
aecda0d6 4644expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4645 rtx subtarget, optab op_optab)
53800dbe 4646{
4647 rtx op0;
0eb671f7 4648 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
53800dbe 4649 return 0;
4650
4651 /* Compute the argument. */
4652 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
6a08d0ab 4653 /* Compute op, into TARGET if possible.
53800dbe 4654 Set TARGET to wherever the result comes back. */
4655 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
6a08d0ab 4656 op_optab, op0, target, 1);
64db345d 4657 gcc_assert (target);
7d3f6cc7 4658
efb070c8 4659 return convert_to_mode (target_mode, target, 0);
53800dbe 4660}
89cfe6e5 4661
df94cd3b 4662/* If the string passed to fputs is a constant and is one character
2c0e001b 4663 long, we attempt to transform this call into __builtin_fputc(). */
15c6cf6b 4664
df94cd3b 4665static rtx
19bf118a 4666expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
df94cd3b 4667{
c013a46e 4668 tree len, fn;
0a68165a 4669 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4670 : implicit_built_in_decls[BUILT_IN_FPUTC];
4671 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
4672 : implicit_built_in_decls[BUILT_IN_FWRITE];
df94cd3b 4673
4674 /* If the return value is used, or the replacement _DECL isn't
2c0e001b 4675 initialized, don't do the transformation. */
19bf118a 4676 if (target != const0_rtx || !fn_fputc || !fn_fwrite)
df94cd3b 4677 return 0;
4678
2c0e001b 4679 /* Verify the arguments in the original call. */
8a06f2d4 4680 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
df94cd3b 4681 return 0;
4682
ce1b14f4 4683 /* Get the length of the string passed to fputs. If the length
4684 can't be determined, punt. */
681fab1e 4685 if (!(len = c_strlen (TREE_VALUE (arglist), 1))
6840589f 4686 || TREE_CODE (len) != INTEGER_CST)
df94cd3b 4687 return 0;
4688
ce1b14f4 4689 switch (compare_tree_int (len, 1))
4690 {
4691 case -1: /* length is 0, delete the call entirely . */
6d42b7e4 4692 {
4693 /* Evaluate and ignore the argument in case it has
4694 side-effects. */
4695 expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
4696 VOIDmode, EXPAND_NORMAL);
4697 return const0_rtx;
4698 }
ce1b14f4 4699 case 0: /* length is 1, call fputc. */
4700 {
83d79705 4701 const char *p = c_getstr (TREE_VALUE (arglist));
df94cd3b 4702
83d79705 4703 if (p != NULL)
bf8e3599 4704 {
83d79705 4705 /* New argument list transforming fputs(string, stream) to
4706 fputc(string[0], stream). */
7c446c95 4707 arglist = build_tree_list (NULL_TREE,
4708 TREE_VALUE (TREE_CHAIN (arglist)));
4709 arglist = tree_cons (NULL_TREE,
7016c612 4710 build_int_cst (NULL_TREE, p[0]),
7c446c95 4711 arglist);
83d79705 4712 fn = fn_fputc;
4713 break;
4714 }
ce1b14f4 4715 }
d632b59a 4716 /* Fall through. */
ce1b14f4 4717 case 1: /* length is greater than 1, call fwrite. */
4718 {
4ec25652 4719 tree string_arg;
bf8e3599 4720
6473f3f4 4721 /* If optimizing for size keep fputs. */
4ec25652 4722 if (optimize_size)
4723 return 0;
4724 string_arg = TREE_VALUE (arglist);
ce1b14f4 4725 /* New argument list transforming fputs(string, stream) to
4726 fwrite(string, 1, len, stream). */
4727 arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
4728 arglist = tree_cons (NULL_TREE, len, arglist);
38d76e41 4729 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
ce1b14f4 4730 arglist = tree_cons (NULL_TREE, string_arg, arglist);
4731 fn = fn_fwrite;
4732 break;
4733 }
4734 default:
64db345d 4735 gcc_unreachable ();
ce1b14f4 4736 }
bf8e3599 4737
7e15618b 4738 return expand_expr (build_function_call_expr (fn, arglist),
19bf118a 4739 const0_rtx, VOIDmode, EXPAND_NORMAL);
3311f67b 4740}
4741
689df48e 4742/* Expand a call to __builtin_expect. We return our argument and emit a
4743 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4744 a non-jump context. */
89cfe6e5 4745
4746static rtx
aecda0d6 4747expand_builtin_expect (tree arglist, rtx target)
89cfe6e5 4748{
4749 tree exp, c;
4750 rtx note, rtx_c;
4751
4752 if (arglist == NULL_TREE
4753 || TREE_CHAIN (arglist) == NULL_TREE)
4754 return const0_rtx;
4755 exp = TREE_VALUE (arglist);
4756 c = TREE_VALUE (TREE_CHAIN (arglist));
4757
4758 if (TREE_CODE (c) != INTEGER_CST)
4759 {
eb586f2c 4760 error ("second arg to %<__builtin_expect%> must be a constant");
89cfe6e5 4761 c = integer_zero_node;
4762 }
4763
4764 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4765
4766 /* Don't bother with expected value notes for integral constants. */
b28bedce 4767 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
89cfe6e5 4768 {
4769 /* We do need to force this into a register so that we can be
4770 moderately sure to be able to correctly interpret the branch
4771 condition later. */
4772 target = force_reg (GET_MODE (target), target);
bf8e3599 4773
89cfe6e5 4774 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4775
31b97e8f 4776 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
89cfe6e5 4777 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4778 }
4779
4780 return target;
4781}
689df48e 4782
4783/* Like expand_builtin_expect, except do this in a jump context. This is
4784 called from do_jump if the conditional is a __builtin_expect. Return either
31d3e01c 4785 a list of insns to emit the jump or NULL if we cannot optimize
689df48e 4786 __builtin_expect. We need to optimize this at jump time so that machines
4787 like the PowerPC don't turn the test into a SCC operation, and then jump
4788 based on the test being 0/1. */
4789
4790rtx
aecda0d6 4791expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
689df48e 4792{
4793 tree arglist = TREE_OPERAND (exp, 1);
4794 tree arg0 = TREE_VALUE (arglist);
4795 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4796 rtx ret = NULL_RTX;
4797
4798 /* Only handle __builtin_expect (test, 0) and
4799 __builtin_expect (test, 1). */
4800 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
27d0c333 4801 && (integer_zerop (arg1) || integer_onep (arg1)))
689df48e 4802 {
6372a134 4803 rtx insn, drop_through_label, temp;
1dc55962 4804
689df48e 4805 /* Expand the jump insns. */
4806 start_sequence ();
4807 do_jump (arg0, if_false_label, if_true_label);
31d3e01c 4808 ret = get_insns ();
15c85053 4809
4810 drop_through_label = get_last_insn ();
6d7dc5b9 4811 if (drop_through_label && NOTE_P (drop_through_label))
15c85053 4812 drop_through_label = prev_nonnote_insn (drop_through_label);
6d7dc5b9 4813 if (drop_through_label && !LABEL_P (drop_through_label))
15c85053 4814 drop_through_label = NULL_RTX;
689df48e 4815 end_sequence ();
4816
15c85053 4817 if (! if_true_label)
4818 if_true_label = drop_through_label;
4819 if (! if_false_label)
4820 if_false_label = drop_through_label;
55a723b1 4821
6c04b2f1 4822 /* Go through and add the expect's to each of the conditional jumps. */
31d3e01c 4823 insn = ret;
4824 while (insn != NULL_RTX)
689df48e 4825 {
31d3e01c 4826 rtx next = NEXT_INSN (insn);
689df48e 4827
6d7dc5b9 4828 if (JUMP_P (insn) && any_condjump_p (insn))
689df48e 4829 {
7caedf36 4830 rtx ifelse = SET_SRC (pc_set (insn));
6c04b2f1 4831 rtx then_dest = XEXP (ifelse, 1);
4832 rtx else_dest = XEXP (ifelse, 2);
4833 int taken = -1;
4834
4835 /* First check if we recognize any of the labels. */
4836 if (GET_CODE (then_dest) == LABEL_REF
b7b16e57 4837 && XEXP (then_dest, 0) == if_true_label)
6c04b2f1 4838 taken = 1;
4839 else if (GET_CODE (then_dest) == LABEL_REF
b7b16e57 4840 && XEXP (then_dest, 0) == if_false_label)
6c04b2f1 4841 taken = 0;
4842 else if (GET_CODE (else_dest) == LABEL_REF
b7b16e57 4843 && XEXP (else_dest, 0) == if_false_label)
6c04b2f1 4844 taken = 1;
4845 else if (GET_CODE (else_dest) == LABEL_REF
b7b16e57 4846 && XEXP (else_dest, 0) == if_true_label)
6c04b2f1 4847 taken = 0;
4848 /* Otherwise check where we drop through. */
4849 else if (else_dest == pc_rtx)
689df48e 4850 {
6d7dc5b9 4851 if (next && NOTE_P (next))
6c04b2f1 4852 next = next_nonnote_insn (next);
4853
6d7dc5b9 4854 if (next && JUMP_P (next)
6c04b2f1 4855 && any_uncondjump_p (next))
6372a134 4856 temp = XEXP (SET_SRC (pc_set (next)), 0);
4857 else
4858 temp = next;
6c04b2f1 4859
6372a134 4860 /* TEMP is either a CODE_LABEL, NULL_RTX or something
6c04b2f1 4861 else that can't possibly match either target label. */
6372a134 4862 if (temp == if_false_label)
6c04b2f1 4863 taken = 1;
6372a134 4864 else if (temp == if_true_label)
6c04b2f1 4865 taken = 0;
689df48e 4866 }
6c04b2f1 4867 else if (then_dest == pc_rtx)
689df48e 4868 {
6d7dc5b9 4869 if (next && NOTE_P (next))
6c04b2f1 4870 next = next_nonnote_insn (next);
4871
6d7dc5b9 4872 if (next && JUMP_P (next)
6c04b2f1 4873 && any_uncondjump_p (next))
6372a134 4874 temp = XEXP (SET_SRC (pc_set (next)), 0);
4875 else
4876 temp = next;
6c04b2f1 4877
6372a134 4878 if (temp == if_false_label)
6c04b2f1 4879 taken = 0;
6372a134 4880 else if (temp == if_true_label)
6c04b2f1 4881 taken = 1;
689df48e 4882 }
6c04b2f1 4883
4884 if (taken != -1)
689df48e 4885 {
6c04b2f1 4886 /* If the test is expected to fail, reverse the
4887 probabilities. */
4888 if (integer_zerop (arg1))
4889 taken = 1 - taken;
4890 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
689df48e 4891 }
689df48e 4892 }
31d3e01c 4893
31d3e01c 4894 insn = next;
689df48e 4895 }
689df48e 4896 }
4897
4898 return ret;
4899}
a0ef1725 4900
4901void
aecda0d6 4902expand_builtin_trap (void)
a0ef1725 4903{
4904#ifdef HAVE_trap
4905 if (HAVE_trap)
4906 emit_insn (gen_trap ());
4907 else
4908#endif
4909 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4910 emit_barrier ();
4911}
78a74442 4912
4913/* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4914 Return 0 if a normal call should be emitted rather than expanding
4915 the function inline. If convenient, the result should be placed
4916 in TARGET. SUBTARGET may be used as the target for computing
4917 the operand. */
4918
4919static rtx
aecda0d6 4920expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
78a74442 4921{
4922 enum machine_mode mode;
4923 tree arg;
4924 rtx op0;
4925
4926 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4927 return 0;
4928
4929 arg = TREE_VALUE (arglist);
4930 mode = TYPE_MODE (TREE_TYPE (arg));
4931 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4932 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4933}
4934
19bf118a 4935/* Create a new constant string literal and return a char* pointer to it.
4936 The STRING_CST value is the LEN characters at STR. */
4937static tree
4938build_string_literal (int len, const char *str)
4939{
4940 tree t, elem, index, type;
4941
4942 t = build_string (len, str);
4943 elem = build_type_variant (char_type_node, 1, 0);
7016c612 4944 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
19bf118a 4945 type = build_array_type (elem, index);
4946 TREE_TYPE (t) = type;
4947 TREE_CONSTANT (t) = 1;
4ee9c684 4948 TREE_INVARIANT (t) = 1;
19bf118a 4949 TREE_READONLY (t) = 1;
4950 TREE_STATIC (t) = 1;
4951
4952 type = build_pointer_type (type);
4953 t = build1 (ADDR_EXPR, type, t);
4954
4955 type = build_pointer_type (elem);
4956 t = build1 (NOP_EXPR, type, t);
4957 return t;
4958}
4959
4960/* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4961 Return 0 if a normal call should be emitted rather than transforming
4962 the function inline. If convenient, the result should be placed in
0862b7e9 4963 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
19bf118a 4964 call. */
4965static rtx
4966expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4967 bool unlocked)
4968{
4969 tree fn_putchar = unlocked
4970 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4971 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4972 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4973 : implicit_built_in_decls[BUILT_IN_PUTS];
4974 const char *fmt_str;
4975 tree fn, fmt, arg;
4976
4977 /* If the return value is used, don't do the transformation. */
4978 if (target != const0_rtx)
4979 return 0;
4980
4981 /* Verify the required arguments in the original call. */
4982 if (! arglist)
4983 return 0;
4984 fmt = TREE_VALUE (arglist);
4985 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
4986 return 0;
4987 arglist = TREE_CHAIN (arglist);
4988
4989 /* Check whether the format is a literal string constant. */
4990 fmt_str = c_getstr (fmt);
4991 if (fmt_str == NULL)
4992 return 0;
4993
4994 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4995 if (strcmp (fmt_str, "%s\n") == 0)
4996 {
4997 if (! arglist
4998 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
4999 || TREE_CHAIN (arglist))
5000 return 0;
5001 fn = fn_puts;
5002 }
5003 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5004 else if (strcmp (fmt_str, "%c") == 0)
5005 {
5006 if (! arglist
5007 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5008 || TREE_CHAIN (arglist))
5009 return 0;
5010 fn = fn_putchar;
5011 }
5012 else
5013 {
5014 /* We can't handle anything else with % args or %% ... yet. */
5015 if (strchr (fmt_str, '%'))
5016 return 0;
5017
5018 if (arglist)
5019 return 0;
5020
5021 /* If the format specifier was "", printf does nothing. */
5022 if (fmt_str[0] == '\0')
5023 return const0_rtx;
5024 /* If the format specifier has length of 1, call putchar. */
5025 if (fmt_str[1] == '\0')
5026 {
5027 /* Given printf("c"), (where c is any one character,)
5028 convert "c"[0] to an int and pass that to the replacement
5029 function. */
7016c612 5030 arg = build_int_cst (NULL_TREE, fmt_str[0]);
19bf118a 5031 arglist = build_tree_list (NULL_TREE, arg);
5032 fn = fn_putchar;
5033 }
5034 else
5035 {
5036 /* If the format specifier was "string\n", call puts("string"). */
5037 size_t len = strlen (fmt_str);
5038 if (fmt_str[len - 1] == '\n')
5039 {
91c82c20 5040 /* Create a NUL-terminated string that's one char shorter
19bf118a 5041 than the original, stripping off the trailing '\n'. */
4acc30e5 5042 char *newstr = alloca (len);
19bf118a 5043 memcpy (newstr, fmt_str, len - 1);
5044 newstr[len - 1] = 0;
5045
5046 arg = build_string_literal (len, newstr);
5047 arglist = build_tree_list (NULL_TREE, arg);
5048 fn = fn_puts;
5049 }
5050 else
5051 /* We'd like to arrange to call fputs(string,stdout) here,
5052 but we need stdout and don't have a way to get it yet. */
5053 return 0;
5054 }
5055 }
5056
5057 if (!fn)
5058 return 0;
5059 return expand_expr (build_function_call_expr (fn, arglist),
5060 target, mode, EXPAND_NORMAL);
5061}
5062
5063/* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
5064 Return 0 if a normal call should be emitted rather than transforming
5065 the function inline. If convenient, the result should be placed in
0862b7e9 5066 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
19bf118a 5067 call. */
5068static rtx
5069expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
5070 bool unlocked)
5071{
5072 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5073 : implicit_built_in_decls[BUILT_IN_FPUTC];
5074 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5075 : implicit_built_in_decls[BUILT_IN_FPUTS];
5076 const char *fmt_str;
5077 tree fn, fmt, fp, arg;
5078
5079 /* If the return value is used, don't do the transformation. */
5080 if (target != const0_rtx)
5081 return 0;
5082
5083 /* Verify the required arguments in the original call. */
5084 if (! arglist)
5085 return 0;
5086 fp = TREE_VALUE (arglist);
5087 if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE)
5088 return 0;
5089 arglist = TREE_CHAIN (arglist);
5090 if (! arglist)
5091 return 0;
5092 fmt = TREE_VALUE (arglist);
5093 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
5094 return 0;
5095 arglist = TREE_CHAIN (arglist);
5096
5097 /* Check whether the format is a literal string constant. */
5098 fmt_str = c_getstr (fmt);
5099 if (fmt_str == NULL)
5100 return 0;
5101
5102 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5103 if (strcmp (fmt_str, "%s") == 0)
5104 {
5105 if (! arglist
5106 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
5107 || TREE_CHAIN (arglist))
5108 return 0;
5109 arg = TREE_VALUE (arglist);
5110 arglist = build_tree_list (NULL_TREE, fp);
5111 arglist = tree_cons (NULL_TREE, arg, arglist);
5112 fn = fn_fputs;
5113 }
5114 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5115 else if (strcmp (fmt_str, "%c") == 0)
5116 {
5117 if (! arglist
5118 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5119 || TREE_CHAIN (arglist))
5120 return 0;
5121 arg = TREE_VALUE (arglist);
5122 arglist = build_tree_list (NULL_TREE, fp);
5123 arglist = tree_cons (NULL_TREE, arg, arglist);
5124 fn = fn_fputc;
5125 }
5126 else
5127 {
5128 /* We can't handle anything else with % args or %% ... yet. */
5129 if (strchr (fmt_str, '%'))
5130 return 0;
5131
5132 if (arglist)
5133 return 0;
5134
5135 /* If the format specifier was "", fprintf does nothing. */
5136 if (fmt_str[0] == '\0')
5137 {
5138 /* Evaluate and ignore FILE* argument for side-effects. */
5139 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5140 return const0_rtx;
5141 }
5142
5143 /* When "string" doesn't contain %, replace all cases of
5144 fprintf(stream,string) with fputs(string,stream). The fputs
5145 builtin will take care of special cases like length == 1. */
5146 arglist = build_tree_list (NULL_TREE, fp);
5147 arglist = tree_cons (NULL_TREE, fmt, arglist);
5148 fn = fn_fputs;
5149 }
5150
5151 if (!fn)
5152 return 0;
5153 return expand_expr (build_function_call_expr (fn, arglist),
5154 target, mode, EXPAND_NORMAL);
5155}
5156
6411575e 5157/* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5158 a normal call should be emitted rather than expanding the function
5159 inline. If convenient, the result should be placed in TARGET with
5160 mode MODE. */
5161
5162static rtx
5163expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5164{
a7a723f0 5165 tree orig_arglist, dest, fmt;
5166 const char *fmt_str;
6411575e 5167
5168 orig_arglist = arglist;
5169
5170 /* Verify the required arguments in the original call. */
5171 if (! arglist)
5172 return 0;
5173 dest = TREE_VALUE (arglist);
5174 if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE)
5175 return 0;
5176 arglist = TREE_CHAIN (arglist);
5177 if (! arglist)
5178 return 0;
5179 fmt = TREE_VALUE (arglist);
19bf118a 5180 if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE)
6411575e 5181 return 0;
5182 arglist = TREE_CHAIN (arglist);
5183
5184 /* Check whether the format is a literal string constant. */
a7a723f0 5185 fmt_str = c_getstr (fmt);
5186 if (fmt_str == NULL)
6411575e 5187 return 0;
5188
5189 /* If the format doesn't contain % args or %%, use strcpy. */
a7a723f0 5190 if (strchr (fmt_str, '%') == 0)
6411575e 5191 {
5192 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5193 tree exp;
5194
a7a723f0 5195 if (arglist || ! fn)
6411575e 5196 return 0;
5197 expand_expr (build_function_call_expr (fn, orig_arglist),
5198 const0_rtx, VOIDmode, EXPAND_NORMAL);
5199 if (target == const0_rtx)
5200 return const0_rtx;
7016c612 5201 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
6411575e 5202 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5203 }
a7a723f0 5204 /* If the format is "%s", use strcpy if the result isn't used. */
5205 else if (strcmp (fmt_str, "%s") == 0)
6411575e 5206 {
a7a723f0 5207 tree fn, arg, len;
5208 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
6411575e 5209
a7a723f0 5210 if (! fn)
6411575e 5211 return 0;
5212
5213 if (! arglist || TREE_CHAIN (arglist))
5214 return 0;
5215 arg = TREE_VALUE (arglist);
5216 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
5217 return 0;
5218
5219 if (target != const0_rtx)
5220 {
681fab1e 5221 len = c_strlen (arg, 1);
a7a723f0 5222 if (! len || TREE_CODE (len) != INTEGER_CST)
6411575e 5223 return 0;
6411575e 5224 }
5225 else
a7a723f0 5226 len = NULL_TREE;
6411575e 5227
5228 arglist = build_tree_list (NULL_TREE, arg);
5229 arglist = tree_cons (NULL_TREE, dest, arglist);
a7a723f0 5230 expand_expr (build_function_call_expr (fn, arglist),
6411575e 5231 const0_rtx, VOIDmode, EXPAND_NORMAL);
5232
5233 if (target == const0_rtx)
5234 return const0_rtx;
a7a723f0 5235 return expand_expr (len, target, mode, EXPAND_NORMAL);
6411575e 5236 }
5237
5238 return 0;
5239}
27f261ef 5240
4ee9c684 5241/* Expand a call to either the entry or exit function profiler. */
5242
5243static rtx
5244expand_builtin_profile_func (bool exitp)
5245{
5246 rtx this, which;
5247
5248 this = DECL_RTL (current_function_decl);
64db345d 5249 gcc_assert (MEM_P (this));
5250 this = XEXP (this, 0);
4ee9c684 5251
5252 if (exitp)
5253 which = profile_function_exit_libfunc;
5254 else
5255 which = profile_function_entry_libfunc;
5256
5257 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5258 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5259 0, hard_frame_pointer_rtx),
5260 Pmode);
5261
5262 return const0_rtx;
5263}
5264
5265/* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5266
5267static rtx
5268round_trampoline_addr (rtx tramp)
5269{
5270 rtx temp, addend, mask;
5271
5272 /* If we don't need too much alignment, we'll have been guaranteed
5273 proper alignment by get_trampoline_type. */
5274 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5275 return tramp;
5276
5277 /* Round address up to desired boundary. */
5278 temp = gen_reg_rtx (Pmode);
5279 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5280 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5281
5282 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5283 temp, 0, OPTAB_LIB_WIDEN);
5284 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5285 temp, 0, OPTAB_LIB_WIDEN);
5286
5287 return tramp;
5288}
5289
5290static rtx
5291expand_builtin_init_trampoline (tree arglist)
5292{
5293 tree t_tramp, t_func, t_chain;
5294 rtx r_tramp, r_func, r_chain;
5295#ifdef TRAMPOLINE_TEMPLATE
5296 rtx blktramp;
5297#endif
5298
5299 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5300 POINTER_TYPE, VOID_TYPE))
5301 return NULL_RTX;
5302
5303 t_tramp = TREE_VALUE (arglist);
5304 arglist = TREE_CHAIN (arglist);
5305 t_func = TREE_VALUE (arglist);
5306 arglist = TREE_CHAIN (arglist);
5307 t_chain = TREE_VALUE (arglist);
5308
5309 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
5310 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
5311 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
5312
5313 /* Generate insns to initialize the trampoline. */
5314 r_tramp = round_trampoline_addr (r_tramp);
5315#ifdef TRAMPOLINE_TEMPLATE
5316 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5317 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5318 emit_block_move (blktramp, assemble_trampoline_template (),
5319 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5320#endif
5321 trampolines_created = 1;
5322 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5323
5324 return const0_rtx;
5325}
5326
5327static rtx
5328expand_builtin_adjust_trampoline (tree arglist)
5329{
5330 rtx tramp;
5331
5332 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5333 return NULL_RTX;
5334
5335 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5336 tramp = round_trampoline_addr (tramp);
5337#ifdef TRAMPOLINE_ADJUST_ADDRESS
5338 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5339#endif
5340
5341 return tramp;
5342}
5343
27f261ef 5344/* Expand a call to the built-in signbit, signbitf or signbitl function.
5345 Return NULL_RTX if a normal call should be emitted rather than expanding
5346 the function in-line. EXP is the expression that is a call to the builtin
5347 function; if convenient, the result should be placed in TARGET. */
5348
5349static rtx
5350expand_builtin_signbit (tree exp, rtx target)
5351{
5352 const struct real_format *fmt;
5353 enum machine_mode fmode, imode, rmode;
5354 HOST_WIDE_INT hi, lo;
5355 tree arg, arglist;
5356 int bitpos;
5357 rtx temp;
5358
5359 arglist = TREE_OPERAND (exp, 1);
5360 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5361 return 0;
5362
5363 arg = TREE_VALUE (arglist);
5364 fmode = TYPE_MODE (TREE_TYPE (arg));
5365 rmode = TYPE_MODE (TREE_TYPE (exp));
5366 fmt = REAL_MODE_FORMAT (fmode);
5367
5368 /* For floating point formats without a sign bit, implement signbit
5369 as "ARG < 0.0". */
5370 if (fmt->signbit < 0)
5371 {
5372 /* But we can't do this if the format supports signed zero. */
5373 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5374 return 0;
5375
05abc81b 5376 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
5377 build_real (TREE_TYPE (arg), dconst0)));
27f261ef 5378 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5379 }
5380
5381 imode = int_mode_for_mode (fmode);
5382 if (imode == BLKmode)
5383 return 0;
5384
5385 bitpos = fmt->signbit;
5386 /* Handle targets with different FP word orders. */
5387 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5388 {
5389 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
5390 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
5391 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
5392 }
5393
5394 /* If the sign bit is not in the lowpart and the floating point format
5395 is wider than an integer, check that is twice the size of an integer
5396 so that we can use gen_highpart below. */
5397 if (bitpos >= GET_MODE_BITSIZE (rmode)
5398 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5399 return 0;
5400
5401 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5402 temp = gen_lowpart (imode, temp);
5403
24fd4260 5404 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
27f261ef 5405 {
ec23d012 5406 if (BYTES_BIG_ENDIAN)
24fd4260 5407 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5408 temp = copy_to_mode_reg (imode, temp);
5409 temp = extract_bit_field (temp, 1, bitpos, 1,
1445ea5b 5410 NULL_RTX, rmode, rmode);
24fd4260 5411 }
5412 else
5413 {
5414 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5415 temp = gen_lowpart (rmode, temp);
5416 if (bitpos < HOST_BITS_PER_WIDE_INT)
27f261ef 5417 {
24fd4260 5418 hi = 0;
5419 lo = (HOST_WIDE_INT) 1 << bitpos;
27f261ef 5420 }
5421 else
24fd4260 5422 {
5423 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5424 lo = 0;
5425 }
27f261ef 5426
24fd4260 5427 temp = force_reg (rmode, temp);
5428 temp = expand_binop (rmode, and_optab, temp,
5429 immed_double_const (lo, hi, rmode),
5430 target, 1, OPTAB_LIB_WIDEN);
27f261ef 5431 }
27f261ef 5432 return temp;
5433}
73673831 5434
5435/* Expand fork or exec calls. TARGET is the desired target of the
5436 call. ARGLIST is the list of arguments of the call. FN is the
5437 identificator of the actual function. IGNORE is nonzero if the
5438 value is to be ignored. */
5439
5440static rtx
5441expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5442{
5443 tree id, decl;
5444 tree call;
5445
5446 /* If we are not profiling, just call the function. */
5447 if (!profile_arc_flag)
5448 return NULL_RTX;
5449
5450 /* Otherwise call the wrapper. This should be equivalent for the rest of
5451 compiler, so the code does not diverge, and the wrapper may run the
9c9bad97 5452 code necessary for keeping the profiling sane. */
73673831 5453
5454 switch (DECL_FUNCTION_CODE (fn))
5455 {
5456 case BUILT_IN_FORK:
5457 id = get_identifier ("__gcov_fork");
5458 break;
5459
5460 case BUILT_IN_EXECL:
5461 id = get_identifier ("__gcov_execl");
5462 break;
5463
5464 case BUILT_IN_EXECV:
5465 id = get_identifier ("__gcov_execv");
5466 break;
5467
5468 case BUILT_IN_EXECLP:
5469 id = get_identifier ("__gcov_execlp");
5470 break;
5471
5472 case BUILT_IN_EXECLE:
5473 id = get_identifier ("__gcov_execle");
5474 break;
5475
5476 case BUILT_IN_EXECVP:
5477 id = get_identifier ("__gcov_execvp");
5478 break;
5479
5480 case BUILT_IN_EXECVE:
5481 id = get_identifier ("__gcov_execve");
5482 break;
5483
5484 default:
64db345d 5485 gcc_unreachable ();
73673831 5486 }
5487
5488 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5489 DECL_EXTERNAL (decl) = 1;
5490 TREE_PUBLIC (decl) = 1;
5491 DECL_ARTIFICIAL (decl) = 1;
5492 TREE_NOTHROW (decl) = 1;
5493 call = build_function_call_expr (decl, arglist);
5494
5495 return expand_call (call, target, ignore);
5496}
53800dbe 5497\f
5498/* Expand an expression EXP that calls a built-in function,
5499 with result going to TARGET if that's convenient
5500 (and in mode MODE if that's convenient).
5501 SUBTARGET may be used as the target for computing one of EXP's operands.
5502 IGNORE is nonzero if the value is to be ignored. */
5503
5504rtx
aecda0d6 5505expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5506 int ignore)
53800dbe 5507{
c6e6ecb1 5508 tree fndecl = get_callee_fndecl (exp);
53800dbe 5509 tree arglist = TREE_OPERAND (exp, 1);
5510 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
efb070c8 5511 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
53800dbe 5512
8305149e 5513 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
883b2e73 5514 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
bf8e3599 5515
53800dbe 5516 /* When not optimizing, generate calls to library functions for a certain
5517 set of builtins. */
cd9ff771 5518 if (!optimize
5519 && !CALLED_AS_BUILT_IN (fndecl)
5520 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5521 && fcode != BUILT_IN_ALLOCA)
5522 return expand_call (exp, target, ignore);
53800dbe 5523
8d6d7930 5524 /* The built-in function expanders test for target == const0_rtx
5525 to determine whether the function's result will be ignored. */
5526 if (ignore)
5527 target = const0_rtx;
5528
5529 /* If the result of a pure or const built-in function is ignored, and
5530 none of its arguments are volatile, we can avoid expanding the
5531 built-in call and just evaluate the arguments for side-effects. */
5532 if (target == const0_rtx
5533 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5534 {
5535 bool volatilep = false;
5536 tree arg;
5537
5538 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5539 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5540 {
5541 volatilep = true;
5542 break;
5543 }
5544
5545 if (! volatilep)
5546 {
5547 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5548 expand_expr (TREE_VALUE (arg), const0_rtx,
5549 VOIDmode, EXPAND_NORMAL);
5550 return const0_rtx;
5551 }
5552 }
5553
53800dbe 5554 switch (fcode)
5555 {
53800dbe 5556 case BUILT_IN_FABS:
d2d4bdde 5557 case BUILT_IN_FABSF:
5558 case BUILT_IN_FABSL:
78a74442 5559 target = expand_builtin_fabs (arglist, target, subtarget);
5560 if (target)
5561 return target;
5562 break;
5563
7d3f6cc7 5564 /* Just do a normal library call if we were unable to fold
5565 the values. */
78a74442 5566 case BUILT_IN_CABS:
5567 case BUILT_IN_CABSF:
5568 case BUILT_IN_CABSL:
78a74442 5569 break;
53800dbe 5570
42721db0 5571 case BUILT_IN_EXP:
5572 case BUILT_IN_EXPF:
5573 case BUILT_IN_EXPL:
750ef9f5 5574 case BUILT_IN_EXP10:
5575 case BUILT_IN_EXP10F:
5576 case BUILT_IN_EXP10L:
9d9ccca1 5577 case BUILT_IN_POW10:
5578 case BUILT_IN_POW10F:
5579 case BUILT_IN_POW10L:
750ef9f5 5580 case BUILT_IN_EXP2:
5581 case BUILT_IN_EXP2F:
5582 case BUILT_IN_EXP2L:
a6b4eed2 5583 case BUILT_IN_EXPM1:
5584 case BUILT_IN_EXPM1F:
5585 case BUILT_IN_EXPM1L:
4efbc641 5586 case BUILT_IN_LOGB:
5587 case BUILT_IN_LOGBF:
5588 case BUILT_IN_LOGBL:
5589 case BUILT_IN_ILOGB:
5590 case BUILT_IN_ILOGBF:
5591 case BUILT_IN_ILOGBL:
42721db0 5592 case BUILT_IN_LOG:
5593 case BUILT_IN_LOGF:
5594 case BUILT_IN_LOGL:
d3cd9bde 5595 case BUILT_IN_LOG10:
5596 case BUILT_IN_LOG10F:
5597 case BUILT_IN_LOG10L:
5598 case BUILT_IN_LOG2:
5599 case BUILT_IN_LOG2F:
5600 case BUILT_IN_LOG2L:
f474cd93 5601 case BUILT_IN_LOG1P:
5602 case BUILT_IN_LOG1PF:
5603 case BUILT_IN_LOG1PL:
528ee710 5604 case BUILT_IN_TAN:
5605 case BUILT_IN_TANF:
5606 case BUILT_IN_TANL:
8de2f465 5607 case BUILT_IN_ASIN:
5608 case BUILT_IN_ASINF:
5609 case BUILT_IN_ASINL:
5610 case BUILT_IN_ACOS:
5611 case BUILT_IN_ACOSF:
5612 case BUILT_IN_ACOSL:
528ee710 5613 case BUILT_IN_ATAN:
5614 case BUILT_IN_ATANF:
5615 case BUILT_IN_ATANL:
7f3be425 5616 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5617 because of possible accuracy problems. */
5618 if (! flag_unsafe_math_optimizations)
53800dbe 5619 break;
75357f66 5620 case BUILT_IN_SQRT:
d2d4bdde 5621 case BUILT_IN_SQRTF:
5622 case BUILT_IN_SQRTL:
805e22b2 5623 case BUILT_IN_FLOOR:
5624 case BUILT_IN_FLOORF:
5625 case BUILT_IN_FLOORL:
5626 case BUILT_IN_CEIL:
5627 case BUILT_IN_CEILF:
5628 case BUILT_IN_CEILL:
5629 case BUILT_IN_TRUNC:
5630 case BUILT_IN_TRUNCF:
5631 case BUILT_IN_TRUNCL:
5632 case BUILT_IN_ROUND:
5633 case BUILT_IN_ROUNDF:
5634 case BUILT_IN_ROUNDL:
5635 case BUILT_IN_NEARBYINT:
5636 case BUILT_IN_NEARBYINTF:
5637 case BUILT_IN_NEARBYINTL:
aef94a0f 5638 case BUILT_IN_RINT:
5639 case BUILT_IN_RINTF:
5640 case BUILT_IN_RINTL:
53800dbe 5641 target = expand_builtin_mathfn (exp, target, subtarget);
5642 if (target)
5643 return target;
5644 break;
5645
0fd605a5 5646 case BUILT_IN_POW:
5647 case BUILT_IN_POWF:
5648 case BUILT_IN_POWL:
f1b844c6 5649 target = expand_builtin_pow (exp, target, subtarget);
5650 if (target)
5651 return target;
5652 break;
5653
0fd605a5 5654 case BUILT_IN_ATAN2:
5655 case BUILT_IN_ATAN2F:
5656 case BUILT_IN_ATAN2L:
80ed5c06 5657 case BUILT_IN_FMOD:
5658 case BUILT_IN_FMODF:
5659 case BUILT_IN_FMODL:
5660 case BUILT_IN_DREM:
5661 case BUILT_IN_DREMF:
5662 case BUILT_IN_DREML:
0fd605a5 5663 if (! flag_unsafe_math_optimizations)
5664 break;
5665 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5666 if (target)
5667 return target;
5668 break;
5669
6b43bae4 5670 case BUILT_IN_SIN:
5671 case BUILT_IN_SINF:
5672 case BUILT_IN_SINL:
5673 case BUILT_IN_COS:
5674 case BUILT_IN_COSF:
5675 case BUILT_IN_COSL:
5676 if (! flag_unsafe_math_optimizations)
5677 break;
5678 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5679 if (target)
5680 return target;
5681 break;
5682
53800dbe 5683 case BUILT_IN_APPLY_ARGS:
5684 return expand_builtin_apply_args ();
5685
5686 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5687 FUNCTION with a copy of the parameters described by
5688 ARGUMENTS, and ARGSIZE. It returns a block of memory
5689 allocated on the stack into which is stored all the registers
5690 that might possibly be used for returning the result of a
5691 function. ARGUMENTS is the value returned by
5692 __builtin_apply_args. ARGSIZE is the number of bytes of
5693 arguments that must be copied. ??? How should this value be
5694 computed? We'll also need a safe worst case value for varargs
5695 functions. */
5696 case BUILT_IN_APPLY:
0eb671f7 5697 if (!validate_arglist (arglist, POINTER_TYPE,
5698 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5699 && !validate_arglist (arglist, REFERENCE_TYPE,
5700 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 5701 return const0_rtx;
5702 else
5703 {
5704 int i;
5705 tree t;
5706 rtx ops[3];
5707
5708 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5709 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5710
5711 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5712 }
5713
5714 /* __builtin_return (RESULT) causes the function to return the
5715 value described by RESULT. RESULT is address of the block of
5716 memory returned by __builtin_apply. */
5717 case BUILT_IN_RETURN:
0eb671f7 5718 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
53800dbe 5719 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5720 NULL_RTX, VOIDmode, 0));
5721 return const0_rtx;
5722
5723 case BUILT_IN_SAVEREGS:
a66c9326 5724 return expand_builtin_saveregs ();
53800dbe 5725
5726 case BUILT_IN_ARGS_INFO:
80cd7a5e 5727 return expand_builtin_args_info (arglist);
53800dbe 5728
5729 /* Return the address of the first anonymous stack arg. */
5730 case BUILT_IN_NEXT_ARG:
4ee9c684 5731 simplify_builtin_next_arg (arglist);
a66c9326 5732 return expand_builtin_next_arg (arglist);
53800dbe 5733
5734 case BUILT_IN_CLASSIFY_TYPE:
5735 return expand_builtin_classify_type (arglist);
5736
5737 case BUILT_IN_CONSTANT_P:
4ee9c684 5738 return const0_rtx;
53800dbe 5739
5740 case BUILT_IN_FRAME_ADDRESS:
5741 case BUILT_IN_RETURN_ADDRESS:
80cd7a5e 5742 return expand_builtin_frame_address (fndecl, arglist);
53800dbe 5743
5744 /* Returns the address of the area where the structure is returned.
5745 0 otherwise. */
5746 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5747 if (arglist != 0
9342ee68 5748 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
e16ceb8e 5749 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
9342ee68 5750 return const0_rtx;
53800dbe 5751 else
9342ee68 5752 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
53800dbe 5753
5754 case BUILT_IN_ALLOCA:
5755 target = expand_builtin_alloca (arglist, target);
5756 if (target)
5757 return target;
5758 break;
5759
4ee9c684 5760 case BUILT_IN_STACK_SAVE:
5761 return expand_stack_save ();
5762
5763 case BUILT_IN_STACK_RESTORE:
5764 expand_stack_restore (TREE_VALUE (arglist));
5765 return const0_rtx;
5766
53800dbe 5767 case BUILT_IN_FFS:
6a08d0ab 5768 case BUILT_IN_FFSL:
5769 case BUILT_IN_FFSLL:
5053259b 5770 case BUILT_IN_FFSIMAX:
efb070c8 5771 target = expand_builtin_unop (target_mode, arglist, target,
5772 subtarget, ffs_optab);
6a08d0ab 5773 if (target)
5774 return target;
5775 break;
5776
5777 case BUILT_IN_CLZ:
5778 case BUILT_IN_CLZL:
5779 case BUILT_IN_CLZLL:
5053259b 5780 case BUILT_IN_CLZIMAX:
efb070c8 5781 target = expand_builtin_unop (target_mode, arglist, target,
5782 subtarget, clz_optab);
6a08d0ab 5783 if (target)
5784 return target;
5785 break;
5786
5787 case BUILT_IN_CTZ:
5788 case BUILT_IN_CTZL:
5789 case BUILT_IN_CTZLL:
5053259b 5790 case BUILT_IN_CTZIMAX:
efb070c8 5791 target = expand_builtin_unop (target_mode, arglist, target,
5792 subtarget, ctz_optab);
6a08d0ab 5793 if (target)
5794 return target;
5795 break;
5796
5797 case BUILT_IN_POPCOUNT:
5798 case BUILT_IN_POPCOUNTL:
5799 case BUILT_IN_POPCOUNTLL:
5053259b 5800 case BUILT_IN_POPCOUNTIMAX:
efb070c8 5801 target = expand_builtin_unop (target_mode, arglist, target,
5802 subtarget, popcount_optab);
6a08d0ab 5803 if (target)
5804 return target;
5805 break;
5806
5807 case BUILT_IN_PARITY:
5808 case BUILT_IN_PARITYL:
5809 case BUILT_IN_PARITYLL:
5053259b 5810 case BUILT_IN_PARITYIMAX:
efb070c8 5811 target = expand_builtin_unop (target_mode, arglist, target,
5812 subtarget, parity_optab);
53800dbe 5813 if (target)
5814 return target;
5815 break;
5816
5817 case BUILT_IN_STRLEN:
80cd7a5e 5818 target = expand_builtin_strlen (arglist, target, target_mode);
53800dbe 5819 if (target)
5820 return target;
5821 break;
5822
5823 case BUILT_IN_STRCPY:
80cd7a5e 5824 target = expand_builtin_strcpy (arglist, target, mode);
53800dbe 5825 if (target)
5826 return target;
5827 break;
bf8e3599 5828
ed09096d 5829 case BUILT_IN_STRNCPY:
5830 target = expand_builtin_strncpy (arglist, target, mode);
5831 if (target)
5832 return target;
5833 break;
bf8e3599 5834
3b824fa6 5835 case BUILT_IN_STPCPY:
5836 target = expand_builtin_stpcpy (arglist, target, mode);
5837 if (target)
5838 return target;
5839 break;
5840
49f0327b 5841 case BUILT_IN_STRCAT:
5842 target = expand_builtin_strcat (arglist, target, mode);
5843 if (target)
5844 return target;
5845 break;
bf8e3599 5846
49f0327b 5847 case BUILT_IN_STRNCAT:
5848 target = expand_builtin_strncat (arglist, target, mode);
5849 if (target)
5850 return target;
5851 break;
bf8e3599 5852
49f0327b 5853 case BUILT_IN_STRSPN:
5854 target = expand_builtin_strspn (arglist, target, mode);
5855 if (target)
5856 return target;
5857 break;
bf8e3599 5858
49f0327b 5859 case BUILT_IN_STRCSPN:
5860 target = expand_builtin_strcspn (arglist, target, mode);
5861 if (target)
5862 return target;
5863 break;
bf8e3599 5864
17f5ea87 5865 case BUILT_IN_STRSTR:
5866 target = expand_builtin_strstr (arglist, target, mode);
5867 if (target)
5868 return target;
5869 break;
bf8e3599 5870
46f3a74a 5871 case BUILT_IN_STRPBRK:
5872 target = expand_builtin_strpbrk (arglist, target, mode);
5873 if (target)
5874 return target;
5875 break;
bf8e3599 5876
398aae36 5877 case BUILT_IN_INDEX:
83d79705 5878 case BUILT_IN_STRCHR:
5879 target = expand_builtin_strchr (arglist, target, mode);
5880 if (target)
5881 return target;
5882 break;
5883
398aae36 5884 case BUILT_IN_RINDEX:
83d79705 5885 case BUILT_IN_STRRCHR:
5886 target = expand_builtin_strrchr (arglist, target, mode);
5887 if (target)
5888 return target;
5889 break;
5890
53800dbe 5891 case BUILT_IN_MEMCPY:
9fe0e1b8 5892 target = expand_builtin_memcpy (arglist, target, mode);
3b824fa6 5893 if (target)
5894 return target;
5895 break;
5896
5897 case BUILT_IN_MEMPCPY:
9fe0e1b8 5898 target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
53800dbe 5899 if (target)
5900 return target;
5901 break;
5902
c4950093 5903 case BUILT_IN_MEMMOVE:
5904 target = expand_builtin_memmove (arglist, target, mode);
5905 if (target)
5906 return target;
5907 break;
5908
5909 case BUILT_IN_BCOPY:
5910 target = expand_builtin_bcopy (arglist);
5911 if (target)
5912 return target;
5913 break;
5914
53800dbe 5915 case BUILT_IN_MEMSET:
80cd7a5e 5916 target = expand_builtin_memset (arglist, target, mode);
53800dbe 5917 if (target)
5918 return target;
5919 break;
5920
ffc83088 5921 case BUILT_IN_BZERO:
80cd7a5e 5922 target = expand_builtin_bzero (arglist);
ffc83088 5923 if (target)
5924 return target;
5925 break;
5926
53800dbe 5927 case BUILT_IN_STRCMP:
83d79705 5928 target = expand_builtin_strcmp (exp, target, mode);
53800dbe 5929 if (target)
5930 return target;
5931 break;
5932
ed09096d 5933 case BUILT_IN_STRNCMP:
5934 target = expand_builtin_strncmp (exp, target, mode);
5935 if (target)
5936 return target;
5937 break;
5938
071f1696 5939 case BUILT_IN_BCMP:
53800dbe 5940 case BUILT_IN_MEMCMP:
6f428e8b 5941 target = expand_builtin_memcmp (exp, arglist, target, mode);
53800dbe 5942 if (target)
5943 return target;
5944 break;
53800dbe 5945
5946 case BUILT_IN_SETJMP:
6b7f6858 5947 target = expand_builtin_setjmp (arglist, target);
5948 if (target)
5949 return target;
5950 break;
53800dbe 5951
5952 /* __builtin_longjmp is passed a pointer to an array of five words.
5953 It's similar to the C library longjmp function but works with
5954 __builtin_setjmp above. */
5955 case BUILT_IN_LONGJMP:
0eb671f7 5956 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
53800dbe 5957 break;
5958 else
5959 {
5960 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5961 VOIDmode, 0);
5962 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5963 NULL_RTX, VOIDmode, 0);
5964
5965 if (value != const1_rtx)
5966 {
5967 error ("__builtin_longjmp second argument must be 1");
5968 return const0_rtx;
5969 }
5970
5971 expand_builtin_longjmp (buf_addr, value);
5972 return const0_rtx;
5973 }
5974
4ee9c684 5975 case BUILT_IN_NONLOCAL_GOTO:
5976 target = expand_builtin_nonlocal_goto (arglist);
5977 if (target)
5978 return target;
5979 break;
5980
843d08a9 5981 /* This updates the setjmp buffer that is its argument with the value
5982 of the current stack pointer. */
5983 case BUILT_IN_UPDATE_SETJMP_BUF:
5984 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5985 {
5986 rtx buf_addr
5987 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5988
5989 expand_builtin_update_setjmp_buf (buf_addr);
5990 return const0_rtx;
5991 }
5992 break;
5993
53800dbe 5994 case BUILT_IN_TRAP:
a0ef1725 5995 expand_builtin_trap ();
53800dbe 5996 return const0_rtx;
5997
19bf118a 5998 case BUILT_IN_PRINTF:
5999 target = expand_builtin_printf (arglist, target, mode, false);
6000 if (target)
6001 return target;
6002 break;
6003
6004 case BUILT_IN_PRINTF_UNLOCKED:
6005 target = expand_builtin_printf (arglist, target, mode, true);
6006 if (target)
6007 return target;
6008 break;
6009
df94cd3b 6010 case BUILT_IN_FPUTS:
19bf118a 6011 target = expand_builtin_fputs (arglist, target, false);
c013a46e 6012 if (target)
6013 return target;
6014 break;
6015 case BUILT_IN_FPUTS_UNLOCKED:
19bf118a 6016 target = expand_builtin_fputs (arglist, target, true);
6017 if (target)
6018 return target;
6019 break;
6020
6021 case BUILT_IN_FPRINTF:
6022 target = expand_builtin_fprintf (arglist, target, mode, false);
6023 if (target)
6024 return target;
6025 break;
6026
6027 case BUILT_IN_FPRINTF_UNLOCKED:
6028 target = expand_builtin_fprintf (arglist, target, mode, true);
df94cd3b 6029 if (target)
6030 return target;
6031 break;
bf8e3599 6032
6411575e 6033 case BUILT_IN_SPRINTF:
6034 target = expand_builtin_sprintf (arglist, target, mode);
6035 if (target)
6036 return target;
6037 break;
6038
27f261ef 6039 case BUILT_IN_SIGNBIT:
6040 case BUILT_IN_SIGNBITF:
6041 case BUILT_IN_SIGNBITL:
6042 target = expand_builtin_signbit (exp, target);
6043 if (target)
6044 return target;
6045 break;
6046
53800dbe 6047 /* Various hooks for the DWARF 2 __throw routine. */
6048 case BUILT_IN_UNWIND_INIT:
6049 expand_builtin_unwind_init ();
6050 return const0_rtx;
6051 case BUILT_IN_DWARF_CFA:
6052 return virtual_cfa_rtx;
6053#ifdef DWARF2_UNWIND_INFO
f8f023a5 6054 case BUILT_IN_DWARF_SP_COLUMN:
6055 return expand_builtin_dwarf_sp_column ();
695e919b 6056 case BUILT_IN_INIT_DWARF_REG_SIZES:
6057 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6058 return const0_rtx;
53800dbe 6059#endif
6060 case BUILT_IN_FROB_RETURN_ADDR:
6061 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6062 case BUILT_IN_EXTRACT_RETURN_ADDR:
6063 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6064 case BUILT_IN_EH_RETURN:
6065 expand_builtin_eh_return (TREE_VALUE (arglist),
df4b504c 6066 TREE_VALUE (TREE_CHAIN (arglist)));
53800dbe 6067 return const0_rtx;
df4b504c 6068#ifdef EH_RETURN_DATA_REGNO
6069 case BUILT_IN_EH_RETURN_DATA_REGNO:
6070 return expand_builtin_eh_return_data_regno (arglist);
6071#endif
26093bf4 6072 case BUILT_IN_EXTEND_POINTER:
6073 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6074
7ccc713a 6075 case BUILT_IN_VA_START:
a66c9326 6076 case BUILT_IN_STDARG_START:
7ccc713a 6077 return expand_builtin_va_start (arglist);
a66c9326 6078 case BUILT_IN_VA_END:
6079 return expand_builtin_va_end (arglist);
6080 case BUILT_IN_VA_COPY:
6081 return expand_builtin_va_copy (arglist);
89cfe6e5 6082 case BUILT_IN_EXPECT:
6083 return expand_builtin_expect (arglist, target);
5e3608d8 6084 case BUILT_IN_PREFETCH:
6085 expand_builtin_prefetch (arglist);
6086 return const0_rtx;
6087
4ee9c684 6088 case BUILT_IN_PROFILE_FUNC_ENTER:
6089 return expand_builtin_profile_func (false);
6090 case BUILT_IN_PROFILE_FUNC_EXIT:
6091 return expand_builtin_profile_func (true);
6092
6093 case BUILT_IN_INIT_TRAMPOLINE:
6094 return expand_builtin_init_trampoline (arglist);
6095 case BUILT_IN_ADJUST_TRAMPOLINE:
6096 return expand_builtin_adjust_trampoline (arglist);
6097
73673831 6098 case BUILT_IN_FORK:
6099 case BUILT_IN_EXECL:
6100 case BUILT_IN_EXECV:
6101 case BUILT_IN_EXECLP:
6102 case BUILT_IN_EXECLE:
6103 case BUILT_IN_EXECVP:
6104 case BUILT_IN_EXECVE:
6105 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6106 if (target)
6107 return target;
6108 break;
53800dbe 6109
92482ee0 6110 default: /* just do library call, if unknown builtin */
146c1b4f 6111 break;
53800dbe 6112 }
6113
6114 /* The switch statement above can drop through to cause the function
6115 to be called normally. */
6116 return expand_call (exp, target, ignore);
6117}
650e4c94 6118
805e22b2 6119/* Determine whether a tree node represents a call to a built-in
52203a9d 6120 function. If the tree T is a call to a built-in function with
6121 the right number of arguments of the appropriate types, return
6122 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6123 Otherwise the return value is END_BUILTINS. */
aecda0d6 6124
805e22b2 6125enum built_in_function
aecda0d6 6126builtin_mathfn_code (tree t)
805e22b2 6127{
52203a9d 6128 tree fndecl, arglist, parmlist;
6129 tree argtype, parmtype;
805e22b2 6130
6131 if (TREE_CODE (t) != CALL_EXPR
6132 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6133 return END_BUILTINS;
6134
c6e6ecb1 6135 fndecl = get_callee_fndecl (t);
6136 if (fndecl == NULL_TREE
52203a9d 6137 || TREE_CODE (fndecl) != FUNCTION_DECL
805e22b2 6138 || ! DECL_BUILT_IN (fndecl)
6139 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6140 return END_BUILTINS;
6141
6142 arglist = TREE_OPERAND (t, 1);
52203a9d 6143 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6144 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
e9f80ff5 6145 {
52203a9d 6146 /* If a function doesn't take a variable number of arguments,
6147 the last element in the list will have type `void'. */
6148 parmtype = TREE_VALUE (parmlist);
6149 if (VOID_TYPE_P (parmtype))
6150 {
6151 if (arglist)
6152 return END_BUILTINS;
6153 return DECL_FUNCTION_CODE (fndecl);
6154 }
6155
6156 if (! arglist)
e9f80ff5 6157 return END_BUILTINS;
e9f80ff5 6158
52203a9d 6159 argtype = TREE_TYPE (TREE_VALUE (arglist));
6160
6161 if (SCALAR_FLOAT_TYPE_P (parmtype))
6162 {
6163 if (! SCALAR_FLOAT_TYPE_P (argtype))
6164 return END_BUILTINS;
6165 }
6166 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6167 {
6168 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6169 return END_BUILTINS;
6170 }
6171 else if (POINTER_TYPE_P (parmtype))
6172 {
6173 if (! POINTER_TYPE_P (argtype))
6174 return END_BUILTINS;
6175 }
6176 else if (INTEGRAL_TYPE_P (parmtype))
6177 {
6178 if (! INTEGRAL_TYPE_P (argtype))
6179 return END_BUILTINS;
6180 }
6181 else
e9f80ff5 6182 return END_BUILTINS;
52203a9d 6183
6184 arglist = TREE_CHAIN (arglist);
e9f80ff5 6185 }
6186
52203a9d 6187 /* Variable-length argument list. */
805e22b2 6188 return DECL_FUNCTION_CODE (fndecl);
6189}
6190
650e4c94 6191/* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6192 constant. ARGLIST is the argument list of the call. */
6193
6194static tree
aecda0d6 6195fold_builtin_constant_p (tree arglist)
650e4c94 6196{
6197 if (arglist == 0)
6198 return 0;
6199
6200 arglist = TREE_VALUE (arglist);
6201
6202 /* We return 1 for a numeric type that's known to be a constant
6203 value at compile-time or for an aggregate type that's a
6204 literal constant. */
6205 STRIP_NOPS (arglist);
6206
6207 /* If we know this is a constant, emit the constant of one. */
ce45a448 6208 if (CONSTANT_CLASS_P (arglist)
650e4c94 6209 || (TREE_CODE (arglist) == CONSTRUCTOR
6210 && TREE_CONSTANT (arglist))
6211 || (TREE_CODE (arglist) == ADDR_EXPR
6212 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
6213 return integer_one_node;
6214
1fb4300c 6215 /* If this expression has side effects, show we don't know it to be a
6216 constant. Likewise if it's a pointer or aggregate type since in
6217 those case we only want literals, since those are only optimized
f97c71a1 6218 when generating RTL, not later.
6219 And finally, if we are compiling an initializer, not code, we
6220 need to return a definite result now; there's not going to be any
6221 more optimization done. */
1fb4300c 6222 if (TREE_SIDE_EFFECTS (arglist)
650e4c94 6223 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
f97c71a1 6224 || POINTER_TYPE_P (TREE_TYPE (arglist))
6225 || cfun == 0)
650e4c94 6226 return integer_zero_node;
6227
6228 return 0;
6229}
6230
4ee9c684 6231/* Fold a call to __builtin_expect, if we expect that a comparison against
6232 the argument will fold to a constant. In practice, this means a true
6233 constant or the address of a non-weak symbol. ARGLIST is the argument
6234 list of the call. */
6235
6236static tree
6237fold_builtin_expect (tree arglist)
6238{
6239 tree arg, inner;
6240
6241 if (arglist == 0)
6242 return 0;
6243
6244 arg = TREE_VALUE (arglist);
6245
6246 /* If the argument isn't invariant, then there's nothing we can do. */
6247 if (!TREE_INVARIANT (arg))
6248 return 0;
6249
6250 /* If we're looking at an address of a weak decl, then do not fold. */
6251 inner = arg;
6252 STRIP_NOPS (inner);
6253 if (TREE_CODE (inner) == ADDR_EXPR)
6254 {
6255 do
6256 {
6257 inner = TREE_OPERAND (inner, 0);
6258 }
6259 while (TREE_CODE (inner) == COMPONENT_REF
6260 || TREE_CODE (inner) == ARRAY_REF);
6261 if (DECL_P (inner) && DECL_WEAK (inner))
6262 return 0;
6263 }
6264
6265 /* Otherwise, ARG already has the proper type for the return value. */
6266 return arg;
6267}
6268
539a3a92 6269/* Fold a call to __builtin_classify_type. */
27d0c333 6270
539a3a92 6271static tree
aecda0d6 6272fold_builtin_classify_type (tree arglist)
539a3a92 6273{
6274 if (arglist == 0)
7016c612 6275 return build_int_cst (NULL_TREE, no_type_class);
539a3a92 6276
7c446c95 6277 return build_int_cst (NULL_TREE,
7016c612 6278 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
539a3a92 6279}
6280
e6e27594 6281/* Fold a call to __builtin_strlen. */
6282
6283static tree
6284fold_builtin_strlen (tree arglist)
6285{
6286 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6287 return NULL_TREE;
6288 else
6289 {
6290 tree len = c_strlen (TREE_VALUE (arglist), 0);
6291
6292 if (len)
6293 {
6294 /* Convert from the internal "sizetype" type to "size_t". */
6295 if (size_type_node)
6296 len = fold_convert (size_type_node, len);
6297 return len;
6298 }
6299
6300 return NULL_TREE;
6301 }
6302}
6303
92c43e3c 6304/* Fold a call to __builtin_inf or __builtin_huge_val. */
6305
6306static tree
aecda0d6 6307fold_builtin_inf (tree type, int warn)
92c43e3c 6308{
aa870c1b 6309 REAL_VALUE_TYPE real;
6310
92c43e3c 6311 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6312 warning ("target format does not support infinity");
6313
aa870c1b 6314 real_inf (&real);
6315 return build_real (type, real);
92c43e3c 6316}
6317
b0db7939 6318/* Fold a call to __builtin_nan or __builtin_nans. */
6319
6320static tree
aecda0d6 6321fold_builtin_nan (tree arglist, tree type, int quiet)
b0db7939 6322{
6323 REAL_VALUE_TYPE real;
6324 const char *str;
6325
6326 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6327 return 0;
6328 str = c_getstr (TREE_VALUE (arglist));
6329 if (!str)
6330 return 0;
6331
6332 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6333 return 0;
6334
6335 return build_real (type, real);
6336}
6337
277f8dd2 6338/* Return true if the floating point expression T has an integer value.
6339 We also allow +Inf, -Inf and NaN to be considered integer values. */
6340
6341static bool
6342integer_valued_real_p (tree t)
6343{
6344 switch (TREE_CODE (t))
6345 {
6346 case FLOAT_EXPR:
6347 return true;
6348
6349 case ABS_EXPR:
6350 case SAVE_EXPR:
6351 case NON_LVALUE_EXPR:
6352 return integer_valued_real_p (TREE_OPERAND (t, 0));
6353
6354 case COMPOUND_EXPR:
6355 case MODIFY_EXPR:
6356 case BIND_EXPR:
6357 return integer_valued_real_p (TREE_OPERAND (t, 1));
6358
6359 case PLUS_EXPR:
6360 case MINUS_EXPR:
6361 case MULT_EXPR:
6362 case MIN_EXPR:
6363 case MAX_EXPR:
6364 return integer_valued_real_p (TREE_OPERAND (t, 0))
6365 && integer_valued_real_p (TREE_OPERAND (t, 1));
6366
6367 case COND_EXPR:
6368 return integer_valued_real_p (TREE_OPERAND (t, 1))
6369 && integer_valued_real_p (TREE_OPERAND (t, 2));
6370
6371 case REAL_CST:
6372 if (! TREE_CONSTANT_OVERFLOW (t))
6373 {
6374 REAL_VALUE_TYPE c, cint;
6375
6376 c = TREE_REAL_CST (t);
6377 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6378 return real_identical (&c, &cint);
6379 }
6380
6381 case NOP_EXPR:
6382 {
6383 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6384 if (TREE_CODE (type) == INTEGER_TYPE)
6385 return true;
6386 if (TREE_CODE (type) == REAL_TYPE)
6387 return integer_valued_real_p (TREE_OPERAND (t, 0));
6388 break;
6389 }
6390
6391 case CALL_EXPR:
6392 switch (builtin_mathfn_code (t))
6393 {
6394 case BUILT_IN_CEIL:
6395 case BUILT_IN_CEILF:
6396 case BUILT_IN_CEILL:
6397 case BUILT_IN_FLOOR:
6398 case BUILT_IN_FLOORF:
6399 case BUILT_IN_FLOORL:
6400 case BUILT_IN_NEARBYINT:
6401 case BUILT_IN_NEARBYINTF:
6402 case BUILT_IN_NEARBYINTL:
5bc8fd11 6403 case BUILT_IN_RINT:
6404 case BUILT_IN_RINTF:
6405 case BUILT_IN_RINTL:
277f8dd2 6406 case BUILT_IN_ROUND:
6407 case BUILT_IN_ROUNDF:
6408 case BUILT_IN_ROUNDL:
6409 case BUILT_IN_TRUNC:
6410 case BUILT_IN_TRUNCF:
6411 case BUILT_IN_TRUNCL:
6412 return true;
6413
6414 default:
6415 break;
6416 }
6417 break;
6418
6419 default:
6420 break;
6421 }
6422 return false;
6423}
6424
6425/* EXP is assumed to be builtin call where truncation can be propagated
6528f4f4 6426 across (for instance floor((double)f) == (double)floorf (f).
6427 Do the transformation. */
277f8dd2 6428
6528f4f4 6429static tree
aecda0d6 6430fold_trunc_transparent_mathfn (tree exp)
6528f4f4 6431{
c6e6ecb1 6432 tree fndecl = get_callee_fndecl (exp);
6528f4f4 6433 tree arglist = TREE_OPERAND (exp, 1);
6434 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
277f8dd2 6435 tree arg;
6436
6437 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6438 return 0;
6528f4f4 6439
277f8dd2 6440 arg = TREE_VALUE (arglist);
6441 /* Integer rounding functions are idempotent. */
6442 if (fcode == builtin_mathfn_code (arg))
6443 return arg;
6444
6445 /* If argument is already integer valued, and we don't need to worry
6446 about setting errno, there's no need to perform rounding. */
6447 if (! flag_errno_math && integer_valued_real_p (arg))
6448 return arg;
6449
6450 if (optimize)
6528f4f4 6451 {
277f8dd2 6452 tree arg0 = strip_float_extensions (arg);
6528f4f4 6453 tree ftype = TREE_TYPE (exp);
6454 tree newtype = TREE_TYPE (arg0);
6455 tree decl;
6456
6457 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6458 && (decl = mathfn_built_in (newtype, fcode)))
6459 {
6460 arglist =
d7aeca92 6461 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6462 return fold_convert (ftype,
6463 build_function_call_expr (decl, arglist));
6528f4f4 6464 }
6465 }
6466 return 0;
6467}
6468
9ed65c7f 6469/* EXP is assumed to be builtin call which can narrow the FP type of
6470 the argument, for instance lround((double)f) -> lroundf (f). */
6471
6472static tree
6473fold_fixed_mathfn (tree exp)
6474{
6475 tree fndecl = get_callee_fndecl (exp);
6476 tree arglist = TREE_OPERAND (exp, 1);
6477 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6478 tree arg;
6479
6480 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6481 return 0;
6482
6483 arg = TREE_VALUE (arglist);
6484
6485 /* If argument is already integer valued, and we don't need to worry
6486 about setting errno, there's no need to perform rounding. */
6487 if (! flag_errno_math && integer_valued_real_p (arg))
6488 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6489
6490 if (optimize)
6491 {
6492 tree ftype = TREE_TYPE (arg);
6493 tree arg0 = strip_float_extensions (arg);
6494 tree newtype = TREE_TYPE (arg0);
6495 tree decl;
6496
6497 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6498 && (decl = mathfn_built_in (newtype, fcode)))
6499 {
6500 arglist =
6501 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6502 return build_function_call_expr (decl, arglist);
6503 }
6504 }
6505 return 0;
6506}
6507
7d99bd73 6508/* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6509 is the argument list and TYPE is the return type. Return
6510 NULL_TREE if no if no simplification can be made. */
c63f4ad3 6511
6512static tree
7d99bd73 6513fold_builtin_cabs (tree arglist, tree type)
c63f4ad3 6514{
6515 tree arg;
6516
6517 if (!arglist || TREE_CHAIN (arglist))
6518 return NULL_TREE;
6519
6520 arg = TREE_VALUE (arglist);
6521 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6522 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6523 return NULL_TREE;
6524
6525 /* Evaluate cabs of a constant at compile-time. */
6526 if (flag_unsafe_math_optimizations
6527 && TREE_CODE (arg) == COMPLEX_CST
6528 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6529 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6530 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6531 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6532 {
6533 REAL_VALUE_TYPE r, i;
6534
6535 r = TREE_REAL_CST (TREE_REALPART (arg));
6536 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6537
6538 real_arithmetic (&r, MULT_EXPR, &r, &r);
6539 real_arithmetic (&i, MULT_EXPR, &i, &i);
6540 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6541 if (real_sqrt (&r, TYPE_MODE (type), &r)
6542 || ! flag_trapping_math)
6543 return build_real (type, r);
6544 }
6545
6546 /* If either part is zero, cabs is fabs of the other. */
6547 if (TREE_CODE (arg) == COMPLEX_EXPR
6548 && real_zerop (TREE_OPERAND (arg, 0)))
6549 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6550 if (TREE_CODE (arg) == COMPLEX_EXPR
6551 && real_zerop (TREE_OPERAND (arg, 1)))
6552 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6553
7d3f6cc7 6554 /* Don't do this when optimizing for size. */
6555 if (flag_unsafe_math_optimizations
6556 && optimize && !optimize_size)
c63f4ad3 6557 {
0da0dbfa 6558 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
c63f4ad3 6559
6560 if (sqrtfn != NULL_TREE)
6561 {
6562 tree rpart, ipart, result, arglist;
6563
4ee9c684 6564 arg = builtin_save_expr (arg);
29a6518e 6565
c63f4ad3 6566 rpart = fold (build1 (REALPART_EXPR, type, arg));
6567 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6568
4ee9c684 6569 rpart = builtin_save_expr (rpart);
6570 ipart = builtin_save_expr (ipart);
c63f4ad3 6571
05abc81b 6572 result = fold (build2 (PLUS_EXPR, type,
6573 fold (build2 (MULT_EXPR, type,
6574 rpart, rpart)),
6575 fold (build2 (MULT_EXPR, type,
6576 ipart, ipart))));
c63f4ad3 6577
6578 arglist = build_tree_list (NULL_TREE, result);
6579 return build_function_call_expr (sqrtfn, arglist);
6580 }
6581 }
6582
6583 return NULL_TREE;
6584}
6585
e6e27594 6586/* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6587 NULL_TREE if no simplification can be made. */
6588
6589static tree
6590fold_builtin_sqrt (tree arglist, tree type)
6591{
6592
6593 enum built_in_function fcode;
6594 tree arg = TREE_VALUE (arglist);
6595
6596 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6597 return NULL_TREE;
6598
6599 /* Optimize sqrt of constant value. */
6600 if (TREE_CODE (arg) == REAL_CST
6601 && ! TREE_CONSTANT_OVERFLOW (arg))
6602 {
6603 REAL_VALUE_TYPE r, x;
6604
6605 x = TREE_REAL_CST (arg);
6606 if (real_sqrt (&r, TYPE_MODE (type), &x)
6607 || (!flag_trapping_math && !flag_errno_math))
6608 return build_real (type, r);
6609 }
6610
6611 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6612 fcode = builtin_mathfn_code (arg);
6613 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6614 {
6615 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6616 arg = fold (build2 (MULT_EXPR, type,
6617 TREE_VALUE (TREE_OPERAND (arg, 1)),
6618 build_real (type, dconsthalf)));
6619 arglist = build_tree_list (NULL_TREE, arg);
6620 return build_function_call_expr (expfn, arglist);
6621 }
6622
6623 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6624 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6625 {
6626 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6627
6628 if (powfn)
6629 {
6630 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6631 tree tree_root;
6632 /* The inner root was either sqrt or cbrt. */
6633 REAL_VALUE_TYPE dconstroot =
6634 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6635
6636 /* Adjust for the outer root. */
6637 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6638 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6639 tree_root = build_real (type, dconstroot);
6640 arglist = tree_cons (NULL_TREE, arg0,
6641 build_tree_list (NULL_TREE, tree_root));
6642 return build_function_call_expr (powfn, arglist);
6643 }
6644 }
6645
6646 /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */
6647 if (flag_unsafe_math_optimizations
6648 && (fcode == BUILT_IN_POW
6649 || fcode == BUILT_IN_POWF
6650 || fcode == BUILT_IN_POWL))
6651 {
6652 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6653 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6654 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6655 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
6656 build_real (type, dconsthalf)));
6657 arglist = tree_cons (NULL_TREE, arg0,
6658 build_tree_list (NULL_TREE, narg1));
6659 return build_function_call_expr (powfn, arglist);
6660 }
6661
6662 return NULL_TREE;
6663}
6664
6665/* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6666 NULL_TREE if no simplification can be made. */
6667static tree
6668fold_builtin_cbrt (tree arglist, tree type)
6669{
6670 tree arg = TREE_VALUE (arglist);
6671 const enum built_in_function fcode = builtin_mathfn_code (arg);
6672
6673 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6674 return NULL_TREE;
6675
6676 /* Optimize cbrt of constant value. */
6677 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6678 return arg;
6679
6680 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6681 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6682 {
6683 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6684 const REAL_VALUE_TYPE third_trunc =
6685 real_value_truncate (TYPE_MODE (type), dconstthird);
6686 arg = fold (build2 (MULT_EXPR, type,
6687 TREE_VALUE (TREE_OPERAND (arg, 1)),
6688 build_real (type, third_trunc)));
6689 arglist = build_tree_list (NULL_TREE, arg);
6690 return build_function_call_expr (expfn, arglist);
6691 }
6692
6693 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6694 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6695 x is negative pow will error but cbrt won't. */
6696 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6697 {
6698 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6699
6700 if (powfn)
6701 {
6702 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6703 tree tree_root;
6704 REAL_VALUE_TYPE dconstroot = dconstthird;
6705
6706 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6707 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6708 tree_root = build_real (type, dconstroot);
6709 arglist = tree_cons (NULL_TREE, arg0,
6710 build_tree_list (NULL_TREE, tree_root));
6711 return build_function_call_expr (powfn, arglist);
6712 }
6713
6714 }
6715 return NULL_TREE;
6716}
6717
6718/* Fold function call to builtin sin, sinf, or sinl. Return
6719 NULL_TREE if no simplification can be made. */
6720static tree
6721fold_builtin_sin (tree arglist)
6722{
6723 tree arg = TREE_VALUE (arglist);
6724
6725 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6726 return NULL_TREE;
6727
6728 /* Optimize sin (0.0) = 0.0. */
6729 if (real_zerop (arg))
6730 return arg;
6731
6732 return NULL_TREE;
6733}
6734
6735/* Fold function call to builtin cos, cosf, or cosl. Return
6736 NULL_TREE if no simplification can be made. */
6737static tree
6738fold_builtin_cos (tree arglist, tree type, tree fndecl)
6739{
6740 tree arg = TREE_VALUE (arglist);
6741
6742 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6743 return NULL_TREE;
6744
6745 /* Optimize cos (0.0) = 1.0. */
6746 if (real_zerop (arg))
6747 return build_real (type, dconst1);
6748
6749 /* Optimize cos(-x) into cos (x). */
6750 if (TREE_CODE (arg) == NEGATE_EXPR)
6751 {
6752 tree args = build_tree_list (NULL_TREE,
6753 TREE_OPERAND (arg, 0));
6754 return build_function_call_expr (fndecl, args);
6755 }
6756
6757 return NULL_TREE;
6758}
6759
6760/* Fold function call to builtin tan, tanf, or tanl. Return
6761 NULL_TREE if no simplification can be made. */
6762static tree
6763fold_builtin_tan (tree arglist)
6764{
6765 enum built_in_function fcode;
6766 tree arg = TREE_VALUE (arglist);
6767
6768 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6769 return NULL_TREE;
6770
6771 /* Optimize tan(0.0) = 0.0. */
6772 if (real_zerop (arg))
6773 return arg;
6774
6775 /* Optimize tan(atan(x)) = x. */
6776 fcode = builtin_mathfn_code (arg);
6777 if (flag_unsafe_math_optimizations
6778 && (fcode == BUILT_IN_ATAN
6779 || fcode == BUILT_IN_ATANF
6780 || fcode == BUILT_IN_ATANL))
6781 return TREE_VALUE (TREE_OPERAND (arg, 1));
6782
6783 return NULL_TREE;
6784}
6785
6786/* Fold function call to builtin atan, atanf, or atanl. Return
6787 NULL_TREE if no simplification can be made. */
6788
6789static tree
6790fold_builtin_atan (tree arglist, tree type)
6791{
6792
6793 tree arg = TREE_VALUE (arglist);
6794
6795 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6796 return NULL_TREE;
6797
6798 /* Optimize atan(0.0) = 0.0. */
6799 if (real_zerop (arg))
6800 return arg;
6801
6802 /* Optimize atan(1.0) = pi/4. */
6803 if (real_onep (arg))
6804 {
6805 REAL_VALUE_TYPE cst;
6806
6807 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6808 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6809 return build_real (type, cst);
6810 }
6811
6812 return NULL_TREE;
6813}
6814
277f8dd2 6815/* Fold function call to builtin trunc, truncf or truncl. Return
6816 NULL_TREE if no simplification can be made. */
6817
6818static tree
6819fold_builtin_trunc (tree exp)
6820{
6821 tree arglist = TREE_OPERAND (exp, 1);
6822 tree arg;
6823
6824 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6825 return 0;
6826
6827 /* Optimize trunc of constant value. */
6828 arg = TREE_VALUE (arglist);
6829 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6830 {
6831 REAL_VALUE_TYPE r, x;
6832 tree type = TREE_TYPE (exp);
6833
6834 x = TREE_REAL_CST (arg);
6835 real_trunc (&r, TYPE_MODE (type), &x);
6836 return build_real (type, r);
6837 }
6838
6839 return fold_trunc_transparent_mathfn (exp);
6840}
6841
6842/* Fold function call to builtin floor, floorf or floorl. Return
6843 NULL_TREE if no simplification can be made. */
6844
6845static tree
6846fold_builtin_floor (tree exp)
6847{
6848 tree arglist = TREE_OPERAND (exp, 1);
6849 tree arg;
6850
6851 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6852 return 0;
6853
6854 /* Optimize floor of constant value. */
6855 arg = TREE_VALUE (arglist);
6856 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6857 {
6858 REAL_VALUE_TYPE x;
6859
6860 x = TREE_REAL_CST (arg);
6861 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6862 {
6863 tree type = TREE_TYPE (exp);
6864 REAL_VALUE_TYPE r;
6865
6866 real_floor (&r, TYPE_MODE (type), &x);
6867 return build_real (type, r);
6868 }
6869 }
6870
6871 return fold_trunc_transparent_mathfn (exp);
6872}
6873
6874/* Fold function call to builtin ceil, ceilf or ceill. Return
6875 NULL_TREE if no simplification can be made. */
6876
6877static tree
6878fold_builtin_ceil (tree exp)
6879{
6880 tree arglist = TREE_OPERAND (exp, 1);
6881 tree arg;
6882
6883 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6884 return 0;
6885
6886 /* Optimize ceil of constant value. */
6887 arg = TREE_VALUE (arglist);
6888 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6889 {
6890 REAL_VALUE_TYPE x;
6891
6892 x = TREE_REAL_CST (arg);
6893 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6894 {
6895 tree type = TREE_TYPE (exp);
6896 REAL_VALUE_TYPE r;
6897
6898 real_ceil (&r, TYPE_MODE (type), &x);
6899 return build_real (type, r);
6900 }
6901 }
6902
6903 return fold_trunc_transparent_mathfn (exp);
6904}
6905
89ab3887 6906/* Fold function call to builtin round, roundf or roundl. Return
6907 NULL_TREE if no simplification can be made. */
6908
6909static tree
6910fold_builtin_round (tree exp)
6911{
6912 tree arglist = TREE_OPERAND (exp, 1);
6913 tree arg;
6914
6915 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6916 return 0;
6917
34f17811 6918 /* Optimize round of constant value. */
89ab3887 6919 arg = TREE_VALUE (arglist);
6920 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6921 {
6922 REAL_VALUE_TYPE x;
6923
6924 x = TREE_REAL_CST (arg);
6925 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6926 {
6927 tree type = TREE_TYPE (exp);
6928 REAL_VALUE_TYPE r;
6929
6930 real_round (&r, TYPE_MODE (type), &x);
6931 return build_real (type, r);
6932 }
6933 }
6934
6935 return fold_trunc_transparent_mathfn (exp);
6936}
6937
34f17811 6938/* Fold function call to builtin lround, lroundf or lroundl (or the
6939 corresponding long long versions). Return NULL_TREE if no
6940 simplification can be made. */
6941
6942static tree
6943fold_builtin_lround (tree exp)
6944{
6945 tree arglist = TREE_OPERAND (exp, 1);
6946 tree arg;
6947
6948 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6949 return 0;
6950
6951 /* Optimize lround of constant value. */
6952 arg = TREE_VALUE (arglist);
6953 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6954 {
6955 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6956
6957 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6958 {
6959 tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6960 HOST_WIDE_INT hi, lo;
6961 REAL_VALUE_TYPE r;
6962
6963 real_round (&r, TYPE_MODE (ftype), &x);
6964 REAL_VALUE_TO_INT (&lo, &hi, r);
7016c612 6965 result = build_int_cst_wide (NULL_TREE, lo, hi);
34f17811 6966 if (int_fits_type_p (result, itype))
6967 return fold_convert (itype, result);
6968 }
6969 }
6970
9ed65c7f 6971 return fold_fixed_mathfn (exp);
34f17811 6972}
6973
70fb4c07 6974/* Fold function call to builtin ffs, clz, ctz, popcount and parity
6975 and their long and long long variants (i.e. ffsl and ffsll).
6976 Return NULL_TREE if no simplification can be made. */
6977
6978static tree
6979fold_builtin_bitop (tree exp)
6980{
6981 tree fndecl = get_callee_fndecl (exp);
6982 tree arglist = TREE_OPERAND (exp, 1);
6983 tree arg;
6984
6985 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6986 return NULL_TREE;
6987
6988 /* Optimize for constant argument. */
6989 arg = TREE_VALUE (arglist);
6990 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6991 {
6992 HOST_WIDE_INT hi, width, result;
6993 unsigned HOST_WIDE_INT lo;
7c446c95 6994 tree type;
70fb4c07 6995
6996 type = TREE_TYPE (arg);
6997 width = TYPE_PRECISION (type);
6998 lo = TREE_INT_CST_LOW (arg);
6999
7000 /* Clear all the bits that are beyond the type's precision. */
7001 if (width > HOST_BITS_PER_WIDE_INT)
7002 {
7003 hi = TREE_INT_CST_HIGH (arg);
7004 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7005 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7006 }
7007 else
7008 {
7009 hi = 0;
7010 if (width < HOST_BITS_PER_WIDE_INT)
7011 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7012 }
7013
7014 switch (DECL_FUNCTION_CODE (fndecl))
7015 {
7016 case BUILT_IN_FFS:
7017 case BUILT_IN_FFSL:
7018 case BUILT_IN_FFSLL:
7019 if (lo != 0)
7020 result = exact_log2 (lo & -lo) + 1;
7021 else if (hi != 0)
7022 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7023 else
7024 result = 0;
7025 break;
7026
7027 case BUILT_IN_CLZ:
7028 case BUILT_IN_CLZL:
7029 case BUILT_IN_CLZLL:
7030 if (hi != 0)
7031 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7032 else if (lo != 0)
7033 result = width - floor_log2 (lo) - 1;
7034 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7035 result = width;
7036 break;
7037
7038 case BUILT_IN_CTZ:
7039 case BUILT_IN_CTZL:
7040 case BUILT_IN_CTZLL:
7041 if (lo != 0)
7042 result = exact_log2 (lo & -lo);
7043 else if (hi != 0)
7044 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7045 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7046 result = width;
7047 break;
7048
7049 case BUILT_IN_POPCOUNT:
7050 case BUILT_IN_POPCOUNTL:
7051 case BUILT_IN_POPCOUNTLL:
7052 result = 0;
7053 while (lo)
7054 result++, lo &= lo - 1;
7055 while (hi)
7056 result++, hi &= hi - 1;
7057 break;
7058
7059 case BUILT_IN_PARITY:
7060 case BUILT_IN_PARITYL:
7061 case BUILT_IN_PARITYLL:
7062 result = 0;
7063 while (lo)
7064 result++, lo &= lo - 1;
7065 while (hi)
7066 result++, hi &= hi - 1;
7067 result &= 1;
7068 break;
7069
7070 default:
64db345d 7071 gcc_unreachable ();
70fb4c07 7072 }
7073
7016c612 7074 return build_int_cst (TREE_TYPE (exp), result);
70fb4c07 7075 }
7076
7077 return NULL_TREE;
7078}
7079
8918c507 7080/* Return true if EXPR is the real constant contained in VALUE. */
7081
7082static bool
7083real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7084{
7085 STRIP_NOPS (expr);
7086
7087 return ((TREE_CODE (expr) == REAL_CST
7088 && ! TREE_CONSTANT_OVERFLOW (expr)
7089 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7090 || (TREE_CODE (expr) == COMPLEX_CST
7091 && real_dconstp (TREE_REALPART (expr), value)
7092 && real_zerop (TREE_IMAGPART (expr))));
7093}
7094
7095/* A subroutine of fold_builtin to fold the various logarithmic
ca273d4a 7096 functions. EXP is the CALL_EXPR of a call to a builtin logN
7097 function. VALUE is the base of the logN function. */
8918c507 7098
7099static tree
7100fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
7101{
7102 tree arglist = TREE_OPERAND (exp, 1);
7103
7104 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7105 {
7106 tree fndecl = get_callee_fndecl (exp);
7107 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7108 tree arg = TREE_VALUE (arglist);
7109 const enum built_in_function fcode = builtin_mathfn_code (arg);
0862b7e9 7110
ca273d4a 7111 /* Optimize logN(1.0) = 0.0. */
8918c507 7112 if (real_onep (arg))
7113 return build_real (type, dconst0);
7114
1f5cb383 7115 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
7116 exactly, then only do this if flag_unsafe_math_optimizations. */
7117 if (exact_real_truncate (TYPE_MODE (type), value)
7118 || flag_unsafe_math_optimizations)
7119 {
7120 const REAL_VALUE_TYPE value_truncate =
7121 real_value_truncate (TYPE_MODE (type), *value);
7122 if (real_dconstp (arg, &value_truncate))
7123 return build_real (type, dconst1);
7124 }
0862b7e9 7125
8918c507 7126 /* Special case, optimize logN(expN(x)) = x. */
7127 if (flag_unsafe_math_optimizations
7128 && ((value == &dconste
7129 && (fcode == BUILT_IN_EXP
7130 || fcode == BUILT_IN_EXPF
7131 || fcode == BUILT_IN_EXPL))
7132 || (value == &dconst2
7133 && (fcode == BUILT_IN_EXP2
7134 || fcode == BUILT_IN_EXP2F
7135 || fcode == BUILT_IN_EXP2L))
852da3c3 7136 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
d7aeca92 7137 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8918c507 7138
ca273d4a 7139 /* Optimize logN(func()) for various exponential functions. We
8918c507 7140 want to determine the value "x" and the power "exponent" in
7141 order to transform logN(x**exponent) into exponent*logN(x). */
7142 if (flag_unsafe_math_optimizations)
7143 {
7144 tree exponent = 0, x = 0;
0862b7e9 7145
8918c507 7146 switch (fcode)
7147 {
7148 case BUILT_IN_EXP:
7149 case BUILT_IN_EXPF:
7150 case BUILT_IN_EXPL:
7151 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8918c507 7152 x = build_real (type,
7153 real_value_truncate (TYPE_MODE (type), dconste));
7154 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7155 break;
7156 case BUILT_IN_EXP2:
7157 case BUILT_IN_EXP2F:
7158 case BUILT_IN_EXP2L:
7159 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7160 x = build_real (type, dconst2);
7161 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7162 break;
7163 case BUILT_IN_EXP10:
7164 case BUILT_IN_EXP10F:
7165 case BUILT_IN_EXP10L:
7166 case BUILT_IN_POW10:
7167 case BUILT_IN_POW10F:
7168 case BUILT_IN_POW10L:
7169 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7170 x = build_real (type, dconst10);
7171 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7172 break;
7173 case BUILT_IN_SQRT:
7174 case BUILT_IN_SQRTF:
7175 case BUILT_IN_SQRTL:
7176 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7177 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7178 exponent = build_real (type, dconsthalf);
7179 break;
7180 case BUILT_IN_CBRT:
7181 case BUILT_IN_CBRTF:
7182 case BUILT_IN_CBRTL:
7183 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7184 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7185 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7186 dconstthird));
7187 break;
7188 case BUILT_IN_POW:
7189 case BUILT_IN_POWF:
7190 case BUILT_IN_POWL:
7191 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7192 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7193 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7194 break;
7195 default:
7196 break;
7197 }
7198
7199 /* Now perform the optimization. */
7200 if (x && exponent)
7201 {
7202 tree logfn;
7203 arglist = build_tree_list (NULL_TREE, x);
7204 logfn = build_function_call_expr (fndecl, arglist);
05abc81b 7205 return fold (build2 (MULT_EXPR, type, exponent, logfn));
8918c507 7206 }
7207 }
7208 }
7209
7210 return 0;
7211}
0862b7e9 7212
e6e27594 7213/* Fold a builtin function call to pow, powf, or powl. Return
7214 NULL_TREE if no simplification can be made. */
7215static tree
7216fold_builtin_pow (tree fndecl, tree arglist, tree type)
7217{
7218 enum built_in_function fcode;
7219 tree arg0 = TREE_VALUE (arglist);
7220 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7221
7222 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7223 return NULL_TREE;
7224
7225 /* Optimize pow(1.0,y) = 1.0. */
7226 if (real_onep (arg0))
7227 return omit_one_operand (type, build_real (type, dconst1), arg1);
7228
7229 if (TREE_CODE (arg1) == REAL_CST
7230 && ! TREE_CONSTANT_OVERFLOW (arg1))
7231 {
7232 REAL_VALUE_TYPE c;
7233 c = TREE_REAL_CST (arg1);
7234
7235 /* Optimize pow(x,0.0) = 1.0. */
7236 if (REAL_VALUES_EQUAL (c, dconst0))
7237 return omit_one_operand (type, build_real (type, dconst1),
7238 arg0);
7239
7240 /* Optimize pow(x,1.0) = x. */
7241 if (REAL_VALUES_EQUAL (c, dconst1))
7242 return arg0;
7243
7244 /* Optimize pow(x,-1.0) = 1.0/x. */
7245 if (REAL_VALUES_EQUAL (c, dconstm1))
7246 return fold (build2 (RDIV_EXPR, type,
7247 build_real (type, dconst1), arg0));
7248
7249 /* Optimize pow(x,0.5) = sqrt(x). */
7250 if (flag_unsafe_math_optimizations
7251 && REAL_VALUES_EQUAL (c, dconsthalf))
7252 {
7253 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7254
7255 if (sqrtfn != NULL_TREE)
7256 {
7257 tree arglist = build_tree_list (NULL_TREE, arg0);
7258 return build_function_call_expr (sqrtfn, arglist);
7259 }
7260 }
7261
7262 /* Attempt to evaluate pow at compile-time. */
7263 if (TREE_CODE (arg0) == REAL_CST
7264 && ! TREE_CONSTANT_OVERFLOW (arg0))
7265 {
7266 REAL_VALUE_TYPE cint;
7267 HOST_WIDE_INT n;
7268
7269 n = real_to_integer (&c);
7270 real_from_integer (&cint, VOIDmode, n,
7271 n < 0 ? -1 : 0, 0);
7272 if (real_identical (&c, &cint))
7273 {
7274 REAL_VALUE_TYPE x;
7275 bool inexact;
7276
7277 x = TREE_REAL_CST (arg0);
7278 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7279 if (flag_unsafe_math_optimizations || !inexact)
7280 return build_real (type, x);
7281 }
7282 }
7283 }
7284
7285 /* Optimize pow(expN(x),y) = expN(x*y). */
7286 fcode = builtin_mathfn_code (arg0);
7287 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7288 {
7289 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7290 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7291 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
7292 arglist = build_tree_list (NULL_TREE, arg);
7293 return build_function_call_expr (expfn, arglist);
7294 }
7295
7296 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7297 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
7298 {
7299 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7300 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7301 build_real (type, dconsthalf)));
7302
7303 arglist = tree_cons (NULL_TREE, narg0,
7304 build_tree_list (NULL_TREE, narg1));
7305 return build_function_call_expr (fndecl, arglist);
7306 }
7307
7308 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7309 if (flag_unsafe_math_optimizations
7310 && (fcode == BUILT_IN_POW
7311 || fcode == BUILT_IN_POWF
7312 || fcode == BUILT_IN_POWL))
7313 {
7314 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7315 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7316 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7317 arglist = tree_cons (NULL_TREE, arg00,
7318 build_tree_list (NULL_TREE, narg1));
7319 return build_function_call_expr (fndecl, arglist);
7320 }
7321 return NULL_TREE;
7322}
7323
8918c507 7324/* A subroutine of fold_builtin to fold the various exponent
7325 functions. EXP is the CALL_EXPR of a call to a builtin function.
7326 VALUE is the value which will be raised to a power. */
7327
7328static tree
7329fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
7330{
7331 tree arglist = TREE_OPERAND (exp, 1);
7332
7333 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7334 {
7335 tree fndecl = get_callee_fndecl (exp);
7336 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7337 tree arg = TREE_VALUE (arglist);
7338
7339 /* Optimize exp*(0.0) = 1.0. */
7340 if (real_zerop (arg))
7341 return build_real (type, dconst1);
7342
7343 /* Optimize expN(1.0) = N. */
7344 if (real_onep (arg))
7345 {
7346 REAL_VALUE_TYPE cst;
7347
7348 real_convert (&cst, TYPE_MODE (type), value);
7349 return build_real (type, cst);
7350 }
7351
7352 /* Attempt to evaluate expN(integer) at compile-time. */
7353 if (flag_unsafe_math_optimizations
7354 && TREE_CODE (arg) == REAL_CST
7355 && ! TREE_CONSTANT_OVERFLOW (arg))
7356 {
7357 REAL_VALUE_TYPE cint;
7358 REAL_VALUE_TYPE c;
7359 HOST_WIDE_INT n;
7360
7361 c = TREE_REAL_CST (arg);
7362 n = real_to_integer (&c);
7363 real_from_integer (&cint, VOIDmode, n,
7364 n < 0 ? -1 : 0, 0);
7365 if (real_identical (&c, &cint))
7366 {
7367 REAL_VALUE_TYPE x;
7368
7369 real_powi (&x, TYPE_MODE (type), value, n);
7370 return build_real (type, x);
7371 }
7372 }
7373
7374 /* Optimize expN(logN(x)) = x. */
7375 if (flag_unsafe_math_optimizations)
7376 {
7377 const enum built_in_function fcode = builtin_mathfn_code (arg);
7378
7379 if ((value == &dconste
7380 && (fcode == BUILT_IN_LOG
7381 || fcode == BUILT_IN_LOGF
7382 || fcode == BUILT_IN_LOGL))
7383 || (value == &dconst2
7384 && (fcode == BUILT_IN_LOG2
7385 || fcode == BUILT_IN_LOG2F
7386 || fcode == BUILT_IN_LOG2L))
7387 || (value == &dconst10
7388 && (fcode == BUILT_IN_LOG10
7389 || fcode == BUILT_IN_LOG10F
7390 || fcode == BUILT_IN_LOG10L)))
d7aeca92 7391 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8918c507 7392 }
7393 }
7394
7395 return 0;
7396}
7397
9c8a1629 7398/* Fold function call to builtin memcpy. Return
7399 NULL_TREE if no simplification can be made. */
7400
7401static tree
7402fold_builtin_memcpy (tree exp)
7403{
7404 tree arglist = TREE_OPERAND (exp, 1);
7405 tree dest, src, len;
7406
7407 if (!validate_arglist (arglist,
7408 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7409 return 0;
7410
7411 dest = TREE_VALUE (arglist);
7412 src = TREE_VALUE (TREE_CHAIN (arglist));
7413 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7414
7415 /* If the LEN parameter is zero, return DEST. */
7416 if (integer_zerop (len))
7417 return omit_one_operand (TREE_TYPE (exp), dest, src);
7418
7419 /* If SRC and DEST are the same (and not volatile), return DEST. */
7420 if (operand_equal_p (src, dest, 0))
7421 return omit_one_operand (TREE_TYPE (exp), dest, len);
7422
7423 return 0;
7424}
7425
7426/* Fold function call to builtin mempcpy. Return
7427 NULL_TREE if no simplification can be made. */
7428
7429static tree
7430fold_builtin_mempcpy (tree exp)
7431{
7432 tree arglist = TREE_OPERAND (exp, 1);
7433 tree dest, src, len;
7434
7435 if (!validate_arglist (arglist,
7436 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7437 return 0;
7438
7439 dest = TREE_VALUE (arglist);
7440 src = TREE_VALUE (TREE_CHAIN (arglist));
7441 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7442
7443 /* If the LEN parameter is zero, return DEST. */
7444 if (integer_zerop (len))
7445 return omit_one_operand (TREE_TYPE (exp), dest, src);
7446
7447 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7448 if (operand_equal_p (src, dest, 0))
7449 {
d7aeca92 7450 tree temp = fold_convert (TREE_TYPE (dest), len);
05abc81b 7451 temp = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp));
d7aeca92 7452 return fold_convert (TREE_TYPE (exp), temp);
9c8a1629 7453 }
7454
7455 return 0;
7456}
7457
7458/* Fold function call to builtin memmove. Return
7459 NULL_TREE if no simplification can be made. */
7460
7461static tree
7462fold_builtin_memmove (tree exp)
7463{
7464 tree arglist = TREE_OPERAND (exp, 1);
7465 tree dest, src, len;
7466
7467 if (!validate_arglist (arglist,
7468 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7469 return 0;
7470
7471 dest = TREE_VALUE (arglist);
7472 src = TREE_VALUE (TREE_CHAIN (arglist));
7473 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7474
7475 /* If the LEN parameter is zero, return DEST. */
7476 if (integer_zerop (len))
7477 return omit_one_operand (TREE_TYPE (exp), dest, src);
7478
7479 /* If SRC and DEST are the same (and not volatile), return DEST. */
7480 if (operand_equal_p (src, dest, 0))
7481 return omit_one_operand (TREE_TYPE (exp), dest, len);
7482
7483 return 0;
7484}
7485
f0613857 7486/* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7487 the length of the string to be copied. Return NULL_TREE if no
7488 simplification can be made. */
9c8a1629 7489
f0613857 7490tree
7491fold_builtin_strcpy (tree exp, tree len)
9c8a1629 7492{
7493 tree arglist = TREE_OPERAND (exp, 1);
f0613857 7494 tree dest, src, fn;
9c8a1629 7495
7496 if (!validate_arglist (arglist,
7497 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7498 return 0;
7499
7500 dest = TREE_VALUE (arglist);
7501 src = TREE_VALUE (TREE_CHAIN (arglist));
7502
7503 /* If SRC and DEST are the same (and not volatile), return DEST. */
7504 if (operand_equal_p (src, dest, 0))
d7aeca92 7505 return fold_convert (TREE_TYPE (exp), dest);
9c8a1629 7506
f0613857 7507 if (optimize_size)
7508 return 0;
7509
7510 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7511 if (!fn)
7512 return 0;
7513
7514 if (!len)
7515 {
7516 len = c_strlen (src, 1);
7517 if (! len || TREE_SIDE_EFFECTS (len))
7518 return 0;
7519 }
7520
7521 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7522 arglist = build_tree_list (NULL_TREE, len);
7523 arglist = tree_cons (NULL_TREE, src, arglist);
7524 arglist = tree_cons (NULL_TREE, dest, arglist);
7525 return fold_convert (TREE_TYPE (exp),
7526 build_function_call_expr (fn, arglist));
9c8a1629 7527}
7528
f0613857 7529/* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7530 the length of the source string. Return NULL_TREE if no simplification
7531 can be made. */
9c8a1629 7532
f0613857 7533tree
7534fold_builtin_strncpy (tree exp, tree slen)
9c8a1629 7535{
7536 tree arglist = TREE_OPERAND (exp, 1);
f0613857 7537 tree dest, src, len, fn;
9c8a1629 7538
7539 if (!validate_arglist (arglist,
7540 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7541 return 0;
7542
7543 dest = TREE_VALUE (arglist);
7544 src = TREE_VALUE (TREE_CHAIN (arglist));
7545 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7546
7547 /* If the LEN parameter is zero, return DEST. */
7548 if (integer_zerop (len))
7549 return omit_one_operand (TREE_TYPE (exp), dest, src);
7550
19226313 7551 /* We can't compare slen with len as constants below if len is not a
7552 constant. */
7553 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7554 return 0;
7555
f0613857 7556 if (!slen)
7557 slen = c_strlen (src, 1);
7558
7559 /* Now, we must be passed a constant src ptr parameter. */
7560 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7561 return 0;
7562
7563 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7564
7565 /* We do not support simplification of this case, though we do
7566 support it when expanding trees into RTL. */
7567 /* FIXME: generate a call to __builtin_memset. */
7568 if (tree_int_cst_lt (slen, len))
7569 return 0;
7570
7571 /* OK transform into builtin memcpy. */
7572 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7573 if (!fn)
7574 return 0;
7575 return fold_convert (TREE_TYPE (exp),
7576 build_function_call_expr (fn, arglist));
9c8a1629 7577}
7578
57f5fba9 7579/* Fold function call to builtin strchr and strrchr.
7580 Return NULL_TREE if no simplification can be made. */
7581
7582static tree
7583fold_builtin_strchr (tree exp, bool actually_strrchr)
7584{
7585 tree arglist = TREE_OPERAND (exp, 1);
7586 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7587 return 0;
7588 else
7589 {
7590 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
7591 const char *p1;
7592
7593 if (TREE_CODE (s2) != INTEGER_CST)
7594 return 0;
7595
7596 p1 = c_getstr (s1);
7597 if (p1 != NULL)
7598 {
7599 char c;
7600 const char *r;
7601
7602 if (target_char_cast (s2, &c))
7603 return 0;
7604
7605 r = actually_strrchr ? strrchr (p1, c) : strchr (p1, c);
7606
7607 if (r == NULL)
779b4c41 7608 return build_int_cst (TREE_TYPE (s1), 0);
57f5fba9 7609
7610 /* Return an offset into the constant string argument. */
7611 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
779b4c41 7612 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
57f5fba9 7613 }
7614
43c77eaa 7615 if (actually_strrchr)
7616 {
7617 tree fn;
7618
7619 if (!integer_zerop (s2))
7620 return 0;
7621
7622 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
7623 if (!fn)
7624 return 0;
7625
7626 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
7627 return build_function_call_expr (fn, arglist);
7628 }
7629
57f5fba9 7630 return 0;
7631 }
7632}
7633
9c8a1629 7634/* Fold function call to builtin memcmp. Return
7635 NULL_TREE if no simplification can be made. */
7636
7637static tree
c4fef134 7638fold_builtin_memcmp (tree arglist)
9c8a1629 7639{
9c8a1629 7640 tree arg1, arg2, len;
c4fef134 7641 const char *p1, *p2;
9c8a1629 7642
7643 if (!validate_arglist (arglist,
7644 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7645 return 0;
7646
7647 arg1 = TREE_VALUE (arglist);
7648 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7649 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7650
7651 /* If the LEN parameter is zero, return zero. */
7652 if (integer_zerop (len))
c4fef134 7653 return omit_two_operands (integer_type_node, integer_zero_node,
7654 arg1, arg2);
9c8a1629 7655
7656 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7657 if (operand_equal_p (arg1, arg2, 0))
c4fef134 7658 return omit_one_operand (integer_type_node, integer_zero_node, len);
7659
7660 p1 = c_getstr (arg1);
7661 p2 = c_getstr (arg2);
7662
7663 /* If all arguments are constant, and the value of len is not greater
7664 than the lengths of arg1 and arg2, evaluate at compile-time. */
7665 if (host_integerp (len, 1) && p1 && p2
7666 && compare_tree_int (len, strlen (p1) + 1) <= 0
7667 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7668 {
7669 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7670
7671 if (r > 0)
7672 return integer_one_node;
7673 else if (r < 0)
7674 return integer_minus_one_node;
7675 else
7676 return integer_zero_node;
7677 }
7678
7679 /* If len parameter is one, return an expression corresponding to
7680 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7681 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7682 {
7683 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7684 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7685 tree ind1 = fold_convert (integer_type_node,
7686 build1 (INDIRECT_REF, cst_uchar_node,
7687 fold_convert (cst_uchar_ptr_node,
7688 arg1)));
7689 tree ind2 = fold_convert (integer_type_node,
7690 build1 (INDIRECT_REF, cst_uchar_node,
7691 fold_convert (cst_uchar_ptr_node,
7692 arg2)));
7693 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7694 }
9c8a1629 7695
7696 return 0;
7697}
7698
7699/* Fold function call to builtin strcmp. Return
7700 NULL_TREE if no simplification can be made. */
7701
7702static tree
c4fef134 7703fold_builtin_strcmp (tree arglist)
9c8a1629 7704{
9c8a1629 7705 tree arg1, arg2;
7706 const char *p1, *p2;
7707
c4fef134 7708 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9c8a1629 7709 return 0;
7710
7711 arg1 = TREE_VALUE (arglist);
7712 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7713
7714 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7715 if (operand_equal_p (arg1, arg2, 0))
c4fef134 7716 return integer_zero_node;
9c8a1629 7717
7718 p1 = c_getstr (arg1);
7719 p2 = c_getstr (arg2);
7720
7721 if (p1 && p2)
7722 {
9c8a1629 7723 const int i = strcmp (p1, p2);
7724 if (i < 0)
c4fef134 7725 return integer_minus_one_node;
9c8a1629 7726 else if (i > 0)
c4fef134 7727 return integer_one_node;
9c8a1629 7728 else
c4fef134 7729 return integer_zero_node;
7730 }
7731
7732 /* If the second arg is "", return *(const unsigned char*)arg1. */
7733 if (p2 && *p2 == '\0')
7734 {
7735 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7736 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7737 return fold_convert (integer_type_node,
7738 build1 (INDIRECT_REF, cst_uchar_node,
7739 fold_convert (cst_uchar_ptr_node,
7740 arg1)));
7741 }
7742
7743 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7744 if (p1 && *p1 == '\0')
7745 {
7746 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7747 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7748 tree temp = fold_convert (integer_type_node,
7749 build1 (INDIRECT_REF, cst_uchar_node,
7750 fold_convert (cst_uchar_ptr_node,
7751 arg2)));
7752 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
9c8a1629 7753 }
7754
7755 return 0;
7756}
7757
7758/* Fold function call to builtin strncmp. Return
7759 NULL_TREE if no simplification can be made. */
7760
7761static tree
c4fef134 7762fold_builtin_strncmp (tree arglist)
9c8a1629 7763{
9c8a1629 7764 tree arg1, arg2, len;
7765 const char *p1, *p2;
7766
7767 if (!validate_arglist (arglist,
7768 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7769 return 0;
7770
7771 arg1 = TREE_VALUE (arglist);
7772 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7773 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7774
7775 /* If the LEN parameter is zero, return zero. */
7776 if (integer_zerop (len))
c4fef134 7777 return omit_two_operands (integer_type_node, integer_zero_node,
7778 arg1, arg2);
9c8a1629 7779
7780 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7781 if (operand_equal_p (arg1, arg2, 0))
c4fef134 7782 return omit_one_operand (integer_type_node, integer_zero_node, len);
9c8a1629 7783
7784 p1 = c_getstr (arg1);
7785 p2 = c_getstr (arg2);
7786
7787 if (host_integerp (len, 1) && p1 && p2)
7788 {
9c8a1629 7789 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
c4fef134 7790 if (i > 0)
7791 return integer_one_node;
7792 else if (i < 0)
7793 return integer_minus_one_node;
9c8a1629 7794 else
c4fef134 7795 return integer_zero_node;
7796 }
7797
7798 /* If the second arg is "", and the length is greater than zero,
7799 return *(const unsigned char*)arg1. */
7800 if (p2 && *p2 == '\0'
7801 && TREE_CODE (len) == INTEGER_CST
7802 && tree_int_cst_sgn (len) == 1)
7803 {
7804 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7805 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7806 return fold_convert (integer_type_node,
7807 build1 (INDIRECT_REF, cst_uchar_node,
7808 fold_convert (cst_uchar_ptr_node,
7809 arg1)));
7810 }
7811
7812 /* If the first arg is "", and the length is greater than zero,
7813 return -*(const unsigned char*)arg2. */
7814 if (p1 && *p1 == '\0'
7815 && TREE_CODE (len) == INTEGER_CST
7816 && tree_int_cst_sgn (len) == 1)
7817 {
7818 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7819 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7820 tree temp = fold_convert (integer_type_node,
7821 build1 (INDIRECT_REF, cst_uchar_node,
7822 fold_convert (cst_uchar_ptr_node,
7823 arg2)));
7824 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7825 }
7826
7827 /* If len parameter is one, return an expression corresponding to
7828 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7829 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7830 {
7831 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7832 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7833 tree ind1 = fold_convert (integer_type_node,
7834 build1 (INDIRECT_REF, cst_uchar_node,
7835 fold_convert (cst_uchar_ptr_node,
7836 arg1)));
7837 tree ind2 = fold_convert (integer_type_node,
7838 build1 (INDIRECT_REF, cst_uchar_node,
7839 fold_convert (cst_uchar_ptr_node,
7840 arg2)));
7841 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
9c8a1629 7842 }
7843
7844 return 0;
7845}
7846
27f261ef 7847/* Fold function call to builtin signbit, signbitf or signbitl. Return
7848 NULL_TREE if no simplification can be made. */
7849
7850static tree
7851fold_builtin_signbit (tree exp)
7852{
7853 tree arglist = TREE_OPERAND (exp, 1);
7854 tree arg, temp;
7855
7856 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7857 return NULL_TREE;
7858
7859 arg = TREE_VALUE (arglist);
7860
7861 /* If ARG is a compile-time constant, determine the result. */
7862 if (TREE_CODE (arg) == REAL_CST
7863 && !TREE_CONSTANT_OVERFLOW (arg))
7864 {
7865 REAL_VALUE_TYPE c;
7866
7867 c = TREE_REAL_CST (arg);
7868 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
d7aeca92 7869 return fold_convert (TREE_TYPE (exp), temp);
27f261ef 7870 }
7871
7872 /* If ARG is non-negative, the result is always zero. */
7873 if (tree_expr_nonnegative_p (arg))
7874 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7875
7876 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7877 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
05abc81b 7878 return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7879 build_real (TREE_TYPE (arg), dconst0)));
27f261ef 7880
7881 return NULL_TREE;
7882}
7883
467214fd 7884/* Fold function call to builtin copysign, copysignf or copysignl.
7885 Return NULL_TREE if no simplification can be made. */
7886
7887static tree
7888fold_builtin_copysign (tree arglist, tree type)
7889{
7890 tree arg1, arg2;
7891
7892 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7893 return NULL_TREE;
7894
7895 arg1 = TREE_VALUE (arglist);
7896 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7897
7898 /* copysign(X,X) is X. */
7899 if (operand_equal_p (arg1, arg2, 0))
7900 return fold_convert (type, arg1);
7901
7902 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7903 if (TREE_CODE (arg1) == REAL_CST
7904 && TREE_CODE (arg2) == REAL_CST
7905 && !TREE_CONSTANT_OVERFLOW (arg1)
7906 && !TREE_CONSTANT_OVERFLOW (arg2))
7907 {
7908 REAL_VALUE_TYPE c1, c2;
7909
7910 c1 = TREE_REAL_CST (arg1);
7911 c2 = TREE_REAL_CST (arg2);
7912 real_copysign (&c1, &c2);
7913 return build_real (type, c1);
7914 c1.sign = c2.sign;
7915 }
7916
7917 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7918 Remember to evaluate Y for side-effects. */
7919 if (tree_expr_nonnegative_p (arg2))
7920 return omit_one_operand (type,
7921 fold (build1 (ABS_EXPR, type, arg1)),
7922 arg2);
7923
7924 return NULL_TREE;
7925}
7926
d49367d4 7927/* Fold a call to builtin isascii. */
7928
7929static tree
7930fold_builtin_isascii (tree arglist)
7931{
7932 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7933 return 0;
7934 else
7935 {
7936 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7937 tree arg = TREE_VALUE (arglist);
491e04ef 7938
7c446c95 7939 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7940 build_int_cst (NULL_TREE,
7016c612 7941 ~ (unsigned HOST_WIDE_INT) 0x7f));
05abc81b 7942 arg = fold (build2 (EQ_EXPR, integer_type_node,
7c446c95 7943 arg, integer_zero_node));
491e04ef 7944
4ee9c684 7945 if (in_gimple_form && !TREE_CONSTANT (arg))
7946 return NULL_TREE;
7947 else
7948 return arg;
d49367d4 7949 }
7950}
7951
7952/* Fold a call to builtin toascii. */
7953
7954static tree
7955fold_builtin_toascii (tree arglist)
7956{
7957 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7958 return 0;
7959 else
7960 {
7961 /* Transform toascii(c) -> (c & 0x7f). */
7962 tree arg = TREE_VALUE (arglist);
491e04ef 7963
05abc81b 7964 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7016c612 7965 build_int_cst (NULL_TREE, 0x7f)));
d49367d4 7966 }
7967}
7968
df1cf42e 7969/* Fold a call to builtin isdigit. */
7970
7971static tree
7972fold_builtin_isdigit (tree arglist)
7973{
7974 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7975 return 0;
7976 else
7977 {
7978 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7979 /* According to the C standard, isdigit is unaffected by locale. */
7980 tree arg = TREE_VALUE (arglist);
d7aeca92 7981 arg = fold_convert (unsigned_type_node, arg);
05abc81b 7982 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7016c612 7983 build_int_cst (unsigned_type_node, TARGET_DIGIT0));
05abc81b 7984 arg = build2 (LE_EXPR, integer_type_node, arg,
7016c612 7985 build_int_cst (unsigned_type_node, 9));
4ee9c684 7986 arg = fold (arg);
7987 if (in_gimple_form && !TREE_CONSTANT (arg))
7988 return NULL_TREE;
7989 else
7990 return arg;
df1cf42e 7991 }
7992}
27f261ef 7993
d1aade50 7994/* Fold a call to fabs, fabsf or fabsl. */
7995
7996static tree
7997fold_builtin_fabs (tree arglist, tree type)
7998{
7999 tree arg;
8000
8001 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8002 return 0;
8003
8004 arg = TREE_VALUE (arglist);
8005 if (TREE_CODE (arg) == REAL_CST)
8006 return fold_abs_const (arg, type);
8007 return fold (build1 (ABS_EXPR, type, arg));
8008}
8009
8010/* Fold a call to abs, labs, llabs or imaxabs. */
8011
8012static tree
8013fold_builtin_abs (tree arglist, tree type)
8014{
8015 tree arg;
8016
8017 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8018 return 0;
8019
8020 arg = TREE_VALUE (arglist);
8021 if (TREE_CODE (arg) == INTEGER_CST)
8022 return fold_abs_const (arg, type);
8023 return fold (build1 (ABS_EXPR, type, arg));
8024}
8025
726069ba 8026/* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8027 EXP is the CALL_EXPR for the call. */
8028
8029static tree
8030fold_builtin_classify (tree exp, int builtin_index)
8031{
8032 tree fndecl = get_callee_fndecl (exp);
8033 tree arglist = TREE_OPERAND (exp, 1);
8034 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8035 tree arg;
8036 REAL_VALUE_TYPE r;
8037
8038 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8039 {
8040 /* Check that we have exactly one argument. */
8041 if (arglist == 0)
8042 {
eb586f2c 8043 error ("too few arguments to function %qs",
726069ba 8044 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8045 return error_mark_node;
8046 }
8047 else if (TREE_CHAIN (arglist) != 0)
8048 {
eb586f2c 8049 error ("too many arguments to function %qs",
726069ba 8050 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8051 return error_mark_node;
8052 }
8053 else
8054 {
eb586f2c 8055 error ("non-floating-point argument to function %qs",
726069ba 8056 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8057 return error_mark_node;
8058 }
8059 }
8060
8061 arg = TREE_VALUE (arglist);
8062 switch (builtin_index)
8063 {
8064 case BUILT_IN_ISINF:
8065 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8066 return omit_one_operand (type, integer_zero_node, arg);
8067
8068 if (TREE_CODE (arg) == REAL_CST)
8069 {
8070 r = TREE_REAL_CST (arg);
8071 if (real_isinf (&r))
8072 return real_compare (GT_EXPR, &r, &dconst0)
8073 ? integer_one_node : integer_minus_one_node;
8074 else
8075 return integer_zero_node;
8076 }
8077
8078 return NULL_TREE;
8079
f23e5f4e 8080 case BUILT_IN_FINITE:
726069ba 8081 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
8082 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8083 return omit_one_operand (type, integer_zero_node, arg);
8084
8085 if (TREE_CODE (arg) == REAL_CST)
8086 {
8087 r = TREE_REAL_CST (arg);
8088 return real_isinf (&r) || real_isnan (&r)
8089 ? integer_zero_node : integer_one_node;
8090 }
8091
8092 return NULL_TREE;
8093
8094 case BUILT_IN_ISNAN:
8095 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
8096 return omit_one_operand (type, integer_zero_node, arg);
8097
8098 if (TREE_CODE (arg) == REAL_CST)
8099 {
8100 r = TREE_REAL_CST (arg);
8101 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8102 }
8103
8104 arg = builtin_save_expr (arg);
8105 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
8106
8107 default:
64db345d 8108 gcc_unreachable ();
726069ba 8109 }
8110}
8111
9bc9f15f 8112/* Fold a call to an unordered comparison function such as
726069ba 8113 __builtin_isgreater(). EXP is the CALL_EXPR for the call.
8114 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8115 the opposite of the desired result. UNORDERED_CODE is used
8116 for modes that can hold NaNs and ORDERED_CODE is used for
8117 the rest. */
9bc9f15f 8118
8119static tree
859f903a 8120fold_builtin_unordered_cmp (tree exp,
9bc9f15f 8121 enum tree_code unordered_code,
8122 enum tree_code ordered_code)
8123{
859f903a 8124 tree fndecl = get_callee_fndecl (exp);
8125 tree arglist = TREE_OPERAND (exp, 1);
8126 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9bc9f15f 8127 enum tree_code code;
8128 tree arg0, arg1;
8129
8130 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
859f903a 8131 {
8132 enum tree_code code0, code1;
8133 tree type0, type1;
8134 tree cmp_type = 0;
9bc9f15f 8135
859f903a 8136 /* Check that we have exactly two arguments. */
8137 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8138 {
eb586f2c 8139 error ("too few arguments to function %qs",
859f903a 8140 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8141 return error_mark_node;
8142 }
8143 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8144 {
eb586f2c 8145 error ("too many arguments to function %qs",
859f903a 8146 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8147 return error_mark_node;
8148 }
8149
8150 arg0 = TREE_VALUE (arglist);
8151 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8152
8153 type0 = TREE_TYPE (arg0);
8154 type1 = TREE_TYPE (arg1);
8155
8156 code0 = TREE_CODE (type0);
8157 code1 = TREE_CODE (type1);
8158
8159 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8160 /* Choose the wider of two real types. */
8161 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8162 ? type0 : type1;
8163 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8164 cmp_type = type0;
8165 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8166 cmp_type = type1;
8167 else
8168 {
eb586f2c 8169 error ("non-floating-point argument to function %qs",
859f903a 8170 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8171 return error_mark_node;
8172 }
8173
8174 arg0 = fold_convert (cmp_type, arg0);
8175 arg1 = fold_convert (cmp_type, arg1);
8176 }
8177 else
8178 {
8179 arg0 = TREE_VALUE (arglist);
8180 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8181 }
8182
8183 if (unordered_code == UNORDERED_EXPR)
8184 {
8185 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8186 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8187 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
8188 }
9bc9f15f 8189
8190 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8191 : ordered_code;
8192 return fold (build1 (TRUTH_NOT_EXPR, type,
8193 fold (build2 (code, type, arg0, arg1))));
8194}
8195
f0613857 8196/* Used by constant folding to simplify calls to builtin functions. EXP is
8197 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8198 result of the function call is ignored. This function returns NULL_TREE
8199 if no simplification was possible. */
650e4c94 8200
4ee9c684 8201static tree
f0613857 8202fold_builtin_1 (tree exp, bool ignore)
650e4c94 8203{
c6e6ecb1 8204 tree fndecl = get_callee_fndecl (exp);
650e4c94 8205 tree arglist = TREE_OPERAND (exp, 1);
e9f80ff5 8206 tree type = TREE_TYPE (TREE_TYPE (fndecl));
650e4c94 8207
8208 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
b0160cb4 8209 return targetm.fold_builtin (exp, ignore);
650e4c94 8210
2643011a 8211 switch (DECL_FUNCTION_CODE (fndecl))
650e4c94 8212 {
8213 case BUILT_IN_CONSTANT_P:
8214 return fold_builtin_constant_p (arglist);
8215
4ee9c684 8216 case BUILT_IN_EXPECT:
8217 return fold_builtin_expect (arglist);
8218
539a3a92 8219 case BUILT_IN_CLASSIFY_TYPE:
8220 return fold_builtin_classify_type (arglist);
8221
650e4c94 8222 case BUILT_IN_STRLEN:
e6e27594 8223 return fold_builtin_strlen (arglist);
650e4c94 8224
c63f4ad3 8225 case BUILT_IN_FABS:
8226 case BUILT_IN_FABSF:
8227 case BUILT_IN_FABSL:
d1aade50 8228 return fold_builtin_fabs (arglist, type);
8229
8230 case BUILT_IN_ABS:
8231 case BUILT_IN_LABS:
8232 case BUILT_IN_LLABS:
8233 case BUILT_IN_IMAXABS:
8234 return fold_builtin_abs (arglist, type);
c63f4ad3 8235
36d3581d 8236 case BUILT_IN_CONJ:
8237 case BUILT_IN_CONJF:
8238 case BUILT_IN_CONJL:
8239 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8240 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
8241 break;
8242
8243 case BUILT_IN_CREAL:
8244 case BUILT_IN_CREALF:
8245 case BUILT_IN_CREALL:
8246 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8247 return non_lvalue (fold (build1 (REALPART_EXPR, type,
8248 TREE_VALUE (arglist))));
8249 break;
8250
8251 case BUILT_IN_CIMAG:
8252 case BUILT_IN_CIMAGF:
8253 case BUILT_IN_CIMAGL:
8254 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8255 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8256 TREE_VALUE (arglist))));
8257 break;
8258
c63f4ad3 8259 case BUILT_IN_CABS:
8260 case BUILT_IN_CABSF:
8261 case BUILT_IN_CABSL:
7d99bd73 8262 return fold_builtin_cabs (arglist, type);
c63f4ad3 8263
805e22b2 8264 case BUILT_IN_SQRT:
8265 case BUILT_IN_SQRTF:
8266 case BUILT_IN_SQRTL:
e6e27594 8267 return fold_builtin_sqrt (arglist, type);
805e22b2 8268
3bc5c41b 8269 case BUILT_IN_CBRT:
8270 case BUILT_IN_CBRTF:
8271 case BUILT_IN_CBRTL:
e6e27594 8272 return fold_builtin_cbrt (arglist, type);
3bc5c41b 8273
77e89269 8274 case BUILT_IN_SIN:
8275 case BUILT_IN_SINF:
8276 case BUILT_IN_SINL:
e6e27594 8277 return fold_builtin_sin (arglist);
77e89269 8278
8279 case BUILT_IN_COS:
8280 case BUILT_IN_COSF:
8281 case BUILT_IN_COSL:
e6e27594 8282 return fold_builtin_cos (arglist, type, fndecl);
77e89269 8283
805e22b2 8284 case BUILT_IN_EXP:
8285 case BUILT_IN_EXPF:
8286 case BUILT_IN_EXPL:
8918c507 8287 return fold_builtin_exponent (exp, &dconste);
467214fd 8288
8918c507 8289 case BUILT_IN_EXP2:
8290 case BUILT_IN_EXP2F:
8291 case BUILT_IN_EXP2L:
8292 return fold_builtin_exponent (exp, &dconst2);
467214fd 8293
8918c507 8294 case BUILT_IN_EXP10:
8295 case BUILT_IN_EXP10F:
8296 case BUILT_IN_EXP10L:
8297 case BUILT_IN_POW10:
8298 case BUILT_IN_POW10F:
8299 case BUILT_IN_POW10L:
8300 return fold_builtin_exponent (exp, &dconst10);
467214fd 8301
805e22b2 8302 case BUILT_IN_LOG:
8303 case BUILT_IN_LOGF:
8304 case BUILT_IN_LOGL:
8918c507 8305 return fold_builtin_logarithm (exp, &dconste);
467214fd 8306
8918c507 8307 case BUILT_IN_LOG2:
8308 case BUILT_IN_LOG2F:
8309 case BUILT_IN_LOG2L:
8310 return fold_builtin_logarithm (exp, &dconst2);
467214fd 8311
8918c507 8312 case BUILT_IN_LOG10:
8313 case BUILT_IN_LOG10F:
8314 case BUILT_IN_LOG10L:
8315 return fold_builtin_logarithm (exp, &dconst10);
805e22b2 8316
98b40778 8317 case BUILT_IN_TAN:
8318 case BUILT_IN_TANF:
8319 case BUILT_IN_TANL:
e6e27594 8320 return fold_builtin_tan (arglist);
98b40778 8321
8322 case BUILT_IN_ATAN:
8323 case BUILT_IN_ATANF:
8324 case BUILT_IN_ATANL:
e6e27594 8325 return fold_builtin_atan (arglist, type);
98b40778 8326
e9f80ff5 8327 case BUILT_IN_POW:
8328 case BUILT_IN_POWF:
8329 case BUILT_IN_POWL:
e6e27594 8330 return fold_builtin_pow (fndecl, arglist, type);
e9f80ff5 8331
92c43e3c 8332 case BUILT_IN_INF:
8333 case BUILT_IN_INFF:
8334 case BUILT_IN_INFL:
e9f80ff5 8335 return fold_builtin_inf (type, true);
92c43e3c 8336
8337 case BUILT_IN_HUGE_VAL:
8338 case BUILT_IN_HUGE_VALF:
8339 case BUILT_IN_HUGE_VALL:
e9f80ff5 8340 return fold_builtin_inf (type, false);
92c43e3c 8341
b0db7939 8342 case BUILT_IN_NAN:
8343 case BUILT_IN_NANF:
8344 case BUILT_IN_NANL:
e9f80ff5 8345 return fold_builtin_nan (arglist, type, true);
b0db7939 8346
8347 case BUILT_IN_NANS:
8348 case BUILT_IN_NANSF:
8349 case BUILT_IN_NANSL:
e9f80ff5 8350 return fold_builtin_nan (arglist, type, false);
b0db7939 8351
6528f4f4 8352 case BUILT_IN_FLOOR:
8353 case BUILT_IN_FLOORF:
8354 case BUILT_IN_FLOORL:
277f8dd2 8355 return fold_builtin_floor (exp);
8356
6528f4f4 8357 case BUILT_IN_CEIL:
8358 case BUILT_IN_CEILF:
8359 case BUILT_IN_CEILL:
277f8dd2 8360 return fold_builtin_ceil (exp);
8361
6528f4f4 8362 case BUILT_IN_TRUNC:
8363 case BUILT_IN_TRUNCF:
8364 case BUILT_IN_TRUNCL:
277f8dd2 8365 return fold_builtin_trunc (exp);
8366
6528f4f4 8367 case BUILT_IN_ROUND:
8368 case BUILT_IN_ROUNDF:
8369 case BUILT_IN_ROUNDL:
89ab3887 8370 return fold_builtin_round (exp);
8371
6528f4f4 8372 case BUILT_IN_NEARBYINT:
8373 case BUILT_IN_NEARBYINTF:
8374 case BUILT_IN_NEARBYINTL:
5bc8fd11 8375 case BUILT_IN_RINT:
8376 case BUILT_IN_RINTF:
8377 case BUILT_IN_RINTL:
6528f4f4 8378 return fold_trunc_transparent_mathfn (exp);
8379
34f17811 8380 case BUILT_IN_LROUND:
8381 case BUILT_IN_LROUNDF:
8382 case BUILT_IN_LROUNDL:
8383 case BUILT_IN_LLROUND:
8384 case BUILT_IN_LLROUNDF:
8385 case BUILT_IN_LLROUNDL:
8386 return fold_builtin_lround (exp);
8387
9ed65c7f 8388 case BUILT_IN_LRINT:
8389 case BUILT_IN_LRINTF:
8390 case BUILT_IN_LRINTL:
8391 case BUILT_IN_LLRINT:
8392 case BUILT_IN_LLRINTF:
8393 case BUILT_IN_LLRINTL:
8394 return fold_fixed_mathfn (exp);
8395
70fb4c07 8396 case BUILT_IN_FFS:
8397 case BUILT_IN_FFSL:
8398 case BUILT_IN_FFSLL:
8399 case BUILT_IN_CLZ:
8400 case BUILT_IN_CLZL:
8401 case BUILT_IN_CLZLL:
8402 case BUILT_IN_CTZ:
8403 case BUILT_IN_CTZL:
8404 case BUILT_IN_CTZLL:
8405 case BUILT_IN_POPCOUNT:
8406 case BUILT_IN_POPCOUNTL:
8407 case BUILT_IN_POPCOUNTLL:
8408 case BUILT_IN_PARITY:
8409 case BUILT_IN_PARITYL:
8410 case BUILT_IN_PARITYLL:
8411 return fold_builtin_bitop (exp);
8412
9c8a1629 8413 case BUILT_IN_MEMCPY:
8414 return fold_builtin_memcpy (exp);
8415
8416 case BUILT_IN_MEMPCPY:
8417 return fold_builtin_mempcpy (exp);
8418
8419 case BUILT_IN_MEMMOVE:
8420 return fold_builtin_memmove (exp);
8421
8422 case BUILT_IN_STRCPY:
f0613857 8423 return fold_builtin_strcpy (exp, NULL_TREE);
9c8a1629 8424
8425 case BUILT_IN_STRNCPY:
f0613857 8426 return fold_builtin_strncpy (exp, NULL_TREE);
9c8a1629 8427
de3b9a39 8428 case BUILT_IN_INDEX:
57f5fba9 8429 case BUILT_IN_STRCHR:
8430 return fold_builtin_strchr (exp, false);
8431
de3b9a39 8432 case BUILT_IN_RINDEX:
57f5fba9 8433 case BUILT_IN_STRRCHR:
8434 return fold_builtin_strchr (exp, true);
8435
9c8a1629 8436 case BUILT_IN_MEMCMP:
c4fef134 8437 return fold_builtin_memcmp (arglist);
9c8a1629 8438
8439 case BUILT_IN_STRCMP:
c4fef134 8440 return fold_builtin_strcmp (arglist);
9c8a1629 8441
8442 case BUILT_IN_STRNCMP:
c4fef134 8443 return fold_builtin_strncmp (arglist);
9c8a1629 8444
27f261ef 8445 case BUILT_IN_SIGNBIT:
8446 case BUILT_IN_SIGNBITF:
8447 case BUILT_IN_SIGNBITL:
8448 return fold_builtin_signbit (exp);
8449
d49367d4 8450 case BUILT_IN_ISASCII:
8451 return fold_builtin_isascii (arglist);
8452
8453 case BUILT_IN_TOASCII:
8454 return fold_builtin_toascii (arglist);
8455
df1cf42e 8456 case BUILT_IN_ISDIGIT:
8457 return fold_builtin_isdigit (arglist);
8458
467214fd 8459 case BUILT_IN_COPYSIGN:
8460 case BUILT_IN_COPYSIGNF:
8461 case BUILT_IN_COPYSIGNL:
8462 return fold_builtin_copysign (arglist, type);
8463
726069ba 8464 case BUILT_IN_FINITE:
8465 case BUILT_IN_FINITEF:
8466 case BUILT_IN_FINITEL:
8467 return fold_builtin_classify (exp, BUILT_IN_FINITE);
8468
8469 case BUILT_IN_ISINF:
8470 case BUILT_IN_ISINFF:
8471 case BUILT_IN_ISINFL:
8472 return fold_builtin_classify (exp, BUILT_IN_ISINF);
8473
8474 case BUILT_IN_ISNAN:
8475 case BUILT_IN_ISNANF:
8476 case BUILT_IN_ISNANL:
8477 return fold_builtin_classify (exp, BUILT_IN_ISNAN);
8478
9bc9f15f 8479 case BUILT_IN_ISGREATER:
859f903a 8480 return fold_builtin_unordered_cmp (exp, UNLE_EXPR, LE_EXPR);
9bc9f15f 8481 case BUILT_IN_ISGREATEREQUAL:
859f903a 8482 return fold_builtin_unordered_cmp (exp, UNLT_EXPR, LT_EXPR);
9bc9f15f 8483 case BUILT_IN_ISLESS:
859f903a 8484 return fold_builtin_unordered_cmp (exp, UNGE_EXPR, GE_EXPR);
9bc9f15f 8485 case BUILT_IN_ISLESSEQUAL:
859f903a 8486 return fold_builtin_unordered_cmp (exp, UNGT_EXPR, GT_EXPR);
9bc9f15f 8487 case BUILT_IN_ISLESSGREATER:
859f903a 8488 return fold_builtin_unordered_cmp (exp, UNEQ_EXPR, EQ_EXPR);
9bc9f15f 8489 case BUILT_IN_ISUNORDERED:
859f903a 8490 return fold_builtin_unordered_cmp (exp, UNORDERED_EXPR, NOP_EXPR);
9bc9f15f 8491
f0613857 8492 case BUILT_IN_FPUTS:
8493 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8494
8495 case BUILT_IN_FPUTS_UNLOCKED:
8496 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8497
650e4c94 8498 default:
8499 break;
8500 }
8501
8502 return 0;
8503}
7e15618b 8504
4ee9c684 8505/* A wrapper function for builtin folding that prevents warnings for
491e04ef 8506 "statement without effect" and the like, caused by removing the
4ee9c684 8507 call node earlier than the warning is generated. */
8508
8509tree
f0613857 8510fold_builtin (tree exp, bool ignore)
4ee9c684 8511{
f0613857 8512 exp = fold_builtin_1 (exp, ignore);
4ee9c684 8513 if (exp)
8514 {
8515 /* ??? Don't clobber shared nodes such as integer_zero_node. */
ce45a448 8516 if (CONSTANT_CLASS_P (exp))
4ee9c684 8517 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8518 TREE_NO_WARNING (exp) = 1;
8519 }
8520 return exp;
8521}
8522
805e22b2 8523/* Conveniently construct a function call expression. */
8524
8525tree
aecda0d6 8526build_function_call_expr (tree fn, tree arglist)
7e15618b 8527{
8528 tree call_expr;
8529
8530 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
05abc81b 8531 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8532 call_expr, arglist, NULL_TREE);
7e15618b 8533 return fold (call_expr);
8534}
0eb671f7 8535
8536/* This function validates the types of a function call argument list
8537 represented as a tree chain of parameters against a specified list
8538 of tree_codes. If the last specifier is a 0, that represents an
8539 ellipses, otherwise the last specifier must be a VOID_TYPE. */
27d0c333 8540
0eb671f7 8541static int
ee582a61 8542validate_arglist (tree arglist, ...)
0eb671f7 8543{
0eb671f7 8544 enum tree_code code;
0903457a 8545 int res = 0;
ee582a61 8546 va_list ap;
aecda0d6 8547
ee582a61 8548 va_start (ap, arglist);
0eb671f7 8549
f0ce3b1f 8550 do
0eb671f7 8551 {
f0ce3b1f 8552 code = va_arg (ap, enum tree_code);
8553 switch (code)
8554 {
8555 case 0:
8556 /* This signifies an ellipses, any further arguments are all ok. */
8557 res = 1;
8558 goto end;
8559 case VOID_TYPE:
8560 /* This signifies an endlink, if no arguments remain, return
8561 true, otherwise return false. */
8562 res = arglist == 0;
8563 goto end;
8564 default:
8565 /* If no parameters remain or the parameter's code does not
8566 match the specified code, return false. Otherwise continue
8567 checking any remaining arguments. */
8568 if (arglist == 0
8569 || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8570 goto end;
8571 break;
8572 }
8573 arglist = TREE_CHAIN (arglist);
0eb671f7 8574 }
f0ce3b1f 8575 while (1);
0903457a 8576
8577 /* We need gotos here since we can only have one VA_CLOSE in a
8578 function. */
8579 end: ;
ee582a61 8580 va_end (ap);
0903457a 8581
8582 return res;
0eb671f7 8583}
fc2a2dcb 8584
fc2a2dcb 8585/* Default target-specific builtin expander that does nothing. */
8586
8587rtx
aecda0d6 8588default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8589 rtx target ATTRIBUTE_UNUSED,
8590 rtx subtarget ATTRIBUTE_UNUSED,
8591 enum machine_mode mode ATTRIBUTE_UNUSED,
8592 int ignore ATTRIBUTE_UNUSED)
fc2a2dcb 8593{
8594 return NULL_RTX;
8595}
c7926a82 8596
01537105 8597/* Returns true is EXP represents data that would potentially reside
8598 in a readonly section. */
8599
8600static bool
8601readonly_data_expr (tree exp)
8602{
8603 STRIP_NOPS (exp);
8604
9ff0637e 8605 if (TREE_CODE (exp) != ADDR_EXPR)
8606 return false;
8607
8608 exp = get_base_address (TREE_OPERAND (exp, 0));
8609 if (!exp)
8610 return false;
8611
8612 /* Make sure we call decl_readonly_section only for trees it
8613 can handle (since it returns true for everything it doesn't
8614 understand). */
491e04ef 8615 if (TREE_CODE (exp) == STRING_CST
9ff0637e 8616 || TREE_CODE (exp) == CONSTRUCTOR
8617 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8618 return decl_readonly_section (exp, 0);
01537105 8619 else
8620 return false;
8621}
4ee9c684 8622
8623/* Front-end to the simplify_builtin_XXX routines.
8624
8625 EXP is a call to a builtin function. If possible try to simplify
8626 that into a constant, expression or call to a more efficient
8627 builtin function.
8628
8629 If IGNORE is nonzero, then the result of this builtin function
8630 call is ignored.
8631
8632 If simplification is possible, return the simplified tree, otherwise
8633 return NULL_TREE. */
8634
8635tree
8636simplify_builtin (tree exp, int ignore)
8637{
8638 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8639 tree arglist = TREE_OPERAND (exp, 1);
8640 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8641 tree val;
8642
8643 switch (fcode)
8644 {
8645 case BUILT_IN_FPUTS:
f0613857 8646 val = fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
4ee9c684 8647 break;
8648 case BUILT_IN_FPUTS_UNLOCKED:
f0613857 8649 val = fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
4ee9c684 8650 break;
8651 case BUILT_IN_STRSTR:
8652 val = simplify_builtin_strstr (arglist);
8653 break;
8654 case BUILT_IN_STRCAT:
8655 val = simplify_builtin_strcat (arglist);
8656 break;
8657 case BUILT_IN_STRNCAT:
8658 val = simplify_builtin_strncat (arglist);
8659 break;
8660 case BUILT_IN_STRSPN:
8661 val = simplify_builtin_strspn (arglist);
8662 break;
8663 case BUILT_IN_STRCSPN:
8664 val = simplify_builtin_strcspn (arglist);
8665 break;
8666 case BUILT_IN_STRCHR:
8667 case BUILT_IN_INDEX:
8668 val = simplify_builtin_strchr (arglist);
8669 break;
8670 case BUILT_IN_STRRCHR:
8671 case BUILT_IN_RINDEX:
8672 val = simplify_builtin_strrchr (arglist);
8673 break;
8674 case BUILT_IN_STRCPY:
f0613857 8675 val = fold_builtin_strcpy (exp, NULL_TREE);
4ee9c684 8676 break;
8677 case BUILT_IN_STRNCPY:
f0613857 8678 val = fold_builtin_strncpy (exp, NULL_TREE);
4ee9c684 8679 break;
8680 case BUILT_IN_STRCMP:
c4fef134 8681 val = fold_builtin_strcmp (arglist);
4ee9c684 8682 break;
8683 case BUILT_IN_STRNCMP:
c4fef134 8684 val = fold_builtin_strncmp (arglist);
4ee9c684 8685 break;
8686 case BUILT_IN_STRPBRK:
8687 val = simplify_builtin_strpbrk (arglist);
8688 break;
8689 case BUILT_IN_BCMP:
8690 case BUILT_IN_MEMCMP:
c4fef134 8691 val = fold_builtin_memcmp (arglist);
4ee9c684 8692 break;
8693 case BUILT_IN_VA_START:
8694 simplify_builtin_va_start (arglist);
8695 val = NULL_TREE;
8696 break;
8697 case BUILT_IN_SPRINTF:
8698 val = simplify_builtin_sprintf (arglist, ignore);
8699 break;
8700 case BUILT_IN_CONSTANT_P:
8701 val = fold_builtin_constant_p (arglist);
8702 /* Gimplification will pull the CALL_EXPR for the builtin out of
8703 an if condition. When not optimizing, we'll not CSE it back.
8704 To avoid link error types of regressions, return false now. */
8705 if (!val && !optimize)
8706 val = integer_zero_node;
8707 break;
8708 default:
8709 val = NULL_TREE;
8710 break;
8711 }
8712
8713 if (val)
c0c67e38 8714 val = fold_convert (TREE_TYPE (exp), val);
4ee9c684 8715 return val;
8716}
8717
8718/* Simplify a call to the strstr builtin.
8719
8720 Return 0 if no simplification was possible, otherwise return the
8721 simplified form of the call as a tree.
8722
8723 The simplified form may be a constant or other expression which
8724 computes the same value, but in a more efficient manner (including
8725 calls to other builtin functions).
8726
8727 The call may contain arguments which need to be evaluated, but
8728 which are not useful to determine the result of the call. In
8729 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8730 COMPOUND_EXPR will be an argument which must be evaluated.
8731 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8732 COMPOUND_EXPR in the chain will contain the tree for the simplified
8733 form of the builtin function call. */
8734
8735static tree
8736simplify_builtin_strstr (tree arglist)
8737{
8738 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8739 return 0;
8740 else
8741 {
8742 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8743 tree fn;
8744 const char *p1, *p2;
8745
8746 p2 = c_getstr (s2);
8747 if (p2 == NULL)
8748 return 0;
8749
8750 p1 = c_getstr (s1);
8751 if (p1 != NULL)
8752 {
8753 const char *r = strstr (p1, p2);
8754
4ee9c684 8755 if (r == NULL)
779b4c41 8756 return build_int_cst (TREE_TYPE (s1), 0);
c0c67e38 8757
8758 /* Return an offset into the constant string argument. */
8759 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
779b4c41 8760 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
4ee9c684 8761 }
8762
8763 if (p2[0] == '\0')
8764 return s1;
8765
8766 if (p2[1] != '\0')
8767 return 0;
8768
8769 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8770 if (!fn)
8771 return 0;
8772
8773 /* New argument list transforming strstr(s1, s2) to
8774 strchr(s1, s2[0]). */
7c446c95 8775 arglist = build_tree_list (NULL_TREE,
7016c612 8776 build_int_cst (NULL_TREE, p2[0]));
4ee9c684 8777 arglist = tree_cons (NULL_TREE, s1, arglist);
8778 return build_function_call_expr (fn, arglist);
8779 }
8780}
8781
8782/* Simplify a call to the strstr builtin.
8783
8784 Return 0 if no simplification was possible, otherwise return the
8785 simplified form of the call as a tree.
8786
8787 The simplified form may be a constant or other expression which
8788 computes the same value, but in a more efficient manner (including
8789 calls to other builtin functions).
8790
8791 The call may contain arguments which need to be evaluated, but
8792 which are not useful to determine the result of the call. In
8793 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8794 COMPOUND_EXPR will be an argument which must be evaluated.
8795 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8796 COMPOUND_EXPR in the chain will contain the tree for the simplified
8797 form of the builtin function call. */
8798
8799static tree
8800simplify_builtin_strchr (tree arglist)
8801{
8802 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8803 return 0;
8804 else
8805 {
8806 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8807 const char *p1;
8808
8809 if (TREE_CODE (s2) != INTEGER_CST)
8810 return 0;
8811
8812 p1 = c_getstr (s1);
8813 if (p1 != NULL)
8814 {
8815 char c;
8816 const char *r;
8817
8818 if (target_char_cast (s2, &c))
8819 return 0;
8820
8821 r = strchr (p1, c);
8822
8823 if (r == NULL)
779b4c41 8824 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 8825
8826 /* Return an offset into the constant string argument. */
05abc81b 8827 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
779b4c41 8828 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
4ee9c684 8829 }
8830
8831 /* FIXME: Should use here strchrM optab so that ports can optimize
8832 this. */
8833 return 0;
8834 }
8835}
8836
8837/* Simplify a call to the strrchr builtin.
8838
8839 Return 0 if no simplification was possible, otherwise return the
8840 simplified form of the call as a tree.
8841
8842 The simplified form may be a constant or other expression which
8843 computes the same value, but in a more efficient manner (including
8844 calls to other builtin functions).
8845
8846 The call may contain arguments which need to be evaluated, but
8847 which are not useful to determine the result of the call. In
8848 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8849 COMPOUND_EXPR will be an argument which must be evaluated.
8850 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8851 COMPOUND_EXPR in the chain will contain the tree for the simplified
8852 form of the builtin function call. */
8853
8854static tree
8855simplify_builtin_strrchr (tree arglist)
8856{
8857 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8858 return 0;
8859 else
8860 {
8861 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8862 tree fn;
8863 const char *p1;
8864
8865 if (TREE_CODE (s2) != INTEGER_CST)
8866 return 0;
8867
8868 p1 = c_getstr (s1);
8869 if (p1 != NULL)
8870 {
8871 char c;
8872 const char *r;
8873
8874 if (target_char_cast (s2, &c))
8875 return 0;
8876
8877 r = strrchr (p1, c);
8878
8879 if (r == NULL)
779b4c41 8880 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 8881
8882 /* Return an offset into the constant string argument. */
05abc81b 8883 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
779b4c41 8884 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
4ee9c684 8885 }
8886
8887 if (! integer_zerop (s2))
8888 return 0;
8889
8890 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8891 if (!fn)
8892 return 0;
8893
8894 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8895 return build_function_call_expr (fn, arglist);
8896 }
8897}
8898
8899/* Simplify a call to the strpbrk builtin.
8900
8901 Return 0 if no simplification was possible, otherwise return the
8902 simplified form of the call as a tree.
8903
8904 The simplified form may be a constant or other expression which
8905 computes the same value, but in a more efficient manner (including
8906 calls to other builtin functions).
8907
8908 The call may contain arguments which need to be evaluated, but
8909 which are not useful to determine the result of the call. In
8910 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8911 COMPOUND_EXPR will be an argument which must be evaluated.
8912 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8913 COMPOUND_EXPR in the chain will contain the tree for the simplified
8914 form of the builtin function call. */
8915
8916static tree
8917simplify_builtin_strpbrk (tree arglist)
8918{
8919 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8920 return 0;
8921 else
8922 {
8923 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8924 tree fn;
8925 const char *p1, *p2;
8926
8927 p2 = c_getstr (s2);
8928 if (p2 == NULL)
8929 return 0;
8930
8931 p1 = c_getstr (s1);
8932 if (p1 != NULL)
8933 {
8934 const char *r = strpbrk (p1, p2);
8935
8936 if (r == NULL)
779b4c41 8937 return build_int_cst (TREE_TYPE (s1), 0);
4ee9c684 8938
8939 /* Return an offset into the constant string argument. */
05abc81b 8940 return fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
779b4c41 8941 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
4ee9c684 8942 }
8943
8944 if (p2[0] == '\0')
05abc81b 8945 /* strpbrk(x, "") == NULL.
8946 Evaluate and ignore s1 in case it had side-effects. */
8947 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
4ee9c684 8948
8949 if (p2[1] != '\0')
8950 return 0; /* Really call strpbrk. */
8951
8952 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8953 if (!fn)
8954 return 0;
8955
8956 /* New argument list transforming strpbrk(s1, s2) to
8957 strchr(s1, s2[0]). */
7c446c95 8958 arglist = build_tree_list (NULL_TREE,
7016c612 8959 build_int_cst (NULL_TREE, p2[0]));
4ee9c684 8960 arglist = tree_cons (NULL_TREE, s1, arglist);
8961 return build_function_call_expr (fn, arglist);
8962 }
8963}
8964
4ee9c684 8965/* Simplify a call to the strcat builtin.
8966
8967 Return 0 if no simplification was possible, otherwise return the
8968 simplified form of the call as a tree.
8969
8970 The simplified form may be a constant or other expression which
8971 computes the same value, but in a more efficient manner (including
8972 calls to other builtin functions).
8973
8974 The call may contain arguments which need to be evaluated, but
8975 which are not useful to determine the result of the call. In
8976 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8977 COMPOUND_EXPR will be an argument which must be evaluated.
8978 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8979 COMPOUND_EXPR in the chain will contain the tree for the simplified
8980 form of the builtin function call. */
8981
8982static tree
8983simplify_builtin_strcat (tree arglist)
8984{
8985 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8986 return 0;
8987 else
8988 {
8989 tree dst = TREE_VALUE (arglist),
8990 src = TREE_VALUE (TREE_CHAIN (arglist));
8991 const char *p = c_getstr (src);
8992
8993 /* If the string length is zero, return the dst parameter. */
8994 if (p && *p == '\0')
8995 return dst;
8996
8997 return 0;
8998 }
8999}
9000
9001/* Simplify a call to the strncat builtin.
9002
9003 Return 0 if no simplification was possible, otherwise return the
9004 simplified form of the call as a tree.
9005
9006 The simplified form may be a constant or other expression which
9007 computes the same value, but in a more efficient manner (including
9008 calls to other builtin functions).
9009
9010 The call may contain arguments which need to be evaluated, but
9011 which are not useful to determine the result of the call. In
9012 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9013 COMPOUND_EXPR will be an argument which must be evaluated.
9014 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9015 COMPOUND_EXPR in the chain will contain the tree for the simplified
9016 form of the builtin function call. */
9017
9018static tree
9019simplify_builtin_strncat (tree arglist)
9020{
9021 if (!validate_arglist (arglist,
9022 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9023 return 0;
9024 else
9025 {
9026 tree dst = TREE_VALUE (arglist);
9027 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9028 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9029 const char *p = c_getstr (src);
9030
9031 /* If the requested length is zero, or the src parameter string
9032 length is zero, return the dst parameter. */
9033 if (integer_zerop (len) || (p && *p == '\0'))
9bc9f15f 9034 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
4ee9c684 9035
9036 /* If the requested len is greater than or equal to the string
9037 length, call strcat. */
9038 if (TREE_CODE (len) == INTEGER_CST && p
9039 && compare_tree_int (len, strlen (p)) >= 0)
9040 {
9041 tree newarglist
9042 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9043 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9044
9045 /* If the replacement _DECL isn't initialized, don't do the
9046 transformation. */
9047 if (!fn)
9048 return 0;
9049
9050 return build_function_call_expr (fn, newarglist);
9051 }
9052 return 0;
9053 }
9054}
9055
9056/* Simplify a call to the strspn builtin.
9057
9058 Return 0 if no simplification was possible, otherwise return the
9059 simplified form of the call as a tree.
9060
9061 The simplified form may be a constant or other expression which
9062 computes the same value, but in a more efficient manner (including
9063 calls to other builtin functions).
9064
9065 The call may contain arguments which need to be evaluated, but
9066 which are not useful to determine the result of the call. In
9067 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9068 COMPOUND_EXPR will be an argument which must be evaluated.
9069 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9070 COMPOUND_EXPR in the chain will contain the tree for the simplified
9071 form of the builtin function call. */
9072
9073static tree
9074simplify_builtin_strspn (tree arglist)
9075{
9076 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9077 return 0;
9078 else
9079 {
9080 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9081 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9082
9083 /* If both arguments are constants, evaluate at compile-time. */
9084 if (p1 && p2)
9085 {
9086 const size_t r = strspn (p1, p2);
9087 return size_int (r);
9088 }
9089
9090 /* If either argument is "", return 0. */
9091 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9bc9f15f 9092 /* Evaluate and ignore both arguments in case either one has
9093 side-effects. */
9094 return omit_two_operands (integer_type_node, integer_zero_node,
9095 s1, s2);
4ee9c684 9096 return 0;
9097 }
9098}
9099
9100/* Simplify a call to the strcspn builtin.
9101
9102 Return 0 if no simplification was possible, otherwise return the
9103 simplified form of the call as a tree.
9104
9105 The simplified form may be a constant or other expression which
9106 computes the same value, but in a more efficient manner (including
9107 calls to other builtin functions).
9108
9109 The call may contain arguments which need to be evaluated, but
9110 which are not useful to determine the result of the call. In
9111 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9112 COMPOUND_EXPR will be an argument which must be evaluated.
9113 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9114 COMPOUND_EXPR in the chain will contain the tree for the simplified
9115 form of the builtin function call. */
9116
9117static tree
9118simplify_builtin_strcspn (tree arglist)
9119{
9120 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9121 return 0;
9122 else
9123 {
9124 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9125 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9126
9127 /* If both arguments are constants, evaluate at compile-time. */
9128 if (p1 && p2)
9129 {
9130 const size_t r = strcspn (p1, p2);
9131 return size_int (r);
9132 }
9133
9134 /* If the first argument is "", return 0. */
9135 if (p1 && *p1 == '\0')
9136 {
9137 /* Evaluate and ignore argument s2 in case it has
9138 side-effects. */
05abc81b 9139 return omit_one_operand (integer_type_node,
9140 integer_zero_node, s2);
4ee9c684 9141 }
9142
9143 /* If the second argument is "", return __builtin_strlen(s1). */
9144 if (p2 && *p2 == '\0')
9145 {
9146 tree newarglist = build_tree_list (NULL_TREE, s1),
9147 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
9148
9149 /* If the replacement _DECL isn't initialized, don't do the
9150 transformation. */
9151 if (!fn)
9152 return 0;
9153
9154 return build_function_call_expr (fn, newarglist);
9155 }
9156 return 0;
9157 }
9158}
9159
f0613857 9160/* Fold a call to the fputs builtin. IGNORE is true if the value returned
9161 by the builtin will be ignored. UNLOCKED is true is true if this
9162 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9163 the known length of the string. Return NULL_TREE if no simplification
9164 was possible. */
4ee9c684 9165
9166tree
f0613857 9167fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
4ee9c684 9168{
f0613857 9169 tree fn;
4ee9c684 9170 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9171 : implicit_built_in_decls[BUILT_IN_FPUTC];
9172 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9173 : implicit_built_in_decls[BUILT_IN_FWRITE];
9174
9175 /* If the return value is used, or the replacement _DECL isn't
9176 initialized, don't do the transformation. */
9177 if (!ignore || !fn_fputc || !fn_fwrite)
9178 return 0;
9179
9180 /* Verify the arguments in the original call. */
9181 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9182 return 0;
9183
f0613857 9184 if (! len)
9185 len = c_strlen (TREE_VALUE (arglist), 0);
4ee9c684 9186
9187 /* Get the length of the string passed to fputs. If the length
9188 can't be determined, punt. */
9189 if (!len
9190 || TREE_CODE (len) != INTEGER_CST)
9191 return 0;
9192
9193 switch (compare_tree_int (len, 1))
9194 {
9195 case -1: /* length is 0, delete the call entirely . */
05abc81b 9196 return omit_one_operand (integer_type_node, integer_zero_node,
9197 TREE_VALUE (TREE_CHAIN (arglist)));
9198
4ee9c684 9199 case 0: /* length is 1, call fputc. */
9200 {
9201 const char *p = c_getstr (TREE_VALUE (arglist));
9202
9203 if (p != NULL)
9204 {
9205 /* New argument list transforming fputs(string, stream) to
9206 fputc(string[0], stream). */
7c446c95 9207 arglist = build_tree_list (NULL_TREE,
9208 TREE_VALUE (TREE_CHAIN (arglist)));
9209 arglist = tree_cons (NULL_TREE,
7016c612 9210 build_int_cst (NULL_TREE, p[0]),
7c446c95 9211 arglist);
4ee9c684 9212 fn = fn_fputc;
9213 break;
9214 }
9215 }
9216 /* FALLTHROUGH */
9217 case 1: /* length is greater than 1, call fwrite. */
9218 {
9219 tree string_arg;
9220
0bed3869 9221 /* If optimizing for size keep fputs. */
4ee9c684 9222 if (optimize_size)
9223 return 0;
9224 string_arg = TREE_VALUE (arglist);
9225 /* New argument list transforming fputs(string, stream) to
9226 fwrite(string, 1, len, stream). */
f0613857 9227 arglist = build_tree_list (NULL_TREE,
9228 TREE_VALUE (TREE_CHAIN (arglist)));
4ee9c684 9229 arglist = tree_cons (NULL_TREE, len, arglist);
9230 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9231 arglist = tree_cons (NULL_TREE, string_arg, arglist);
9232 fn = fn_fwrite;
9233 break;
9234 }
9235 default:
64db345d 9236 gcc_unreachable ();
4ee9c684 9237 }
9238
db97ad41 9239 /* These optimizations are only performed when the result is ignored,
9240 hence there's no need to cast the result to integer_type_node. */
9241 return build_function_call_expr (fn, arglist);
4ee9c684 9242}
9243
9244static void
9245simplify_builtin_va_start (tree arglist)
9246{
9247 tree chain = TREE_CHAIN (arglist);
9248
9249 if (TREE_CHAIN (chain))
eb586f2c 9250 error ("too many arguments to function %<va_start%>");
4ee9c684 9251
9252 simplify_builtin_next_arg (chain);
9253}
9254
9255static void
9256simplify_builtin_next_arg (tree arglist)
9257{
9258 tree fntype = TREE_TYPE (current_function_decl);
9259
9260 if (TYPE_ARG_TYPES (fntype) == 0
9261 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9262 == void_type_node))
eb586f2c 9263 error ("%<va_start%> used in function with fixed args");
4ee9c684 9264 else if (arglist)
9265 {
9266 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9267 tree arg = TREE_VALUE (arglist);
9268
9269 /* Strip off all nops for the sake of the comparison. This
9270 is not quite the same as STRIP_NOPS. It does more.
9271 We must also strip off INDIRECT_EXPR for C++ reference
9272 parameters. */
9273 while (TREE_CODE (arg) == NOP_EXPR
9274 || TREE_CODE (arg) == CONVERT_EXPR
9275 || TREE_CODE (arg) == NON_LVALUE_EXPR
9276 || TREE_CODE (arg) == INDIRECT_REF)
9277 arg = TREE_OPERAND (arg, 0);
9278 if (arg != last_parm)
eb586f2c 9279 warning ("second parameter of %<va_start%> not last named argument");
4ee9c684 9280 TREE_VALUE (arglist) = arg;
9281 }
9282 else
9283 /* Evidently an out of date version of <stdarg.h>; can't validate
9284 va_start's second argument, but can still work as intended. */
eb586f2c 9285 warning ("%<__builtin_next_arg%> called without an argument");
4ee9c684 9286}
9287
9288
9289/* Simplify a call to the sprintf builtin.
9290
9291 Return 0 if no simplification was possible, otherwise return the
9292 simplified form of the call as a tree. If IGNORED is true, it means that
9293 the caller does not use the returned value of the function. */
9294
9295static tree
9296simplify_builtin_sprintf (tree arglist, int ignored)
9297{
9298 tree call, retval, dest, fmt;
9299 const char *fmt_str = NULL;
9300
9301 /* Verify the required arguments in the original call. We deal with two
9302 types of sprintf() calls: 'sprintf (str, fmt)' and
9303 'sprintf (dest, "%s", orig)'. */
9304 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9305 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9306 VOID_TYPE))
9307 return NULL_TREE;
9308
9309 /* Get the destination string and the format specifier. */
9310 dest = TREE_VALUE (arglist);
9311 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9312
9313 /* Check whether the format is a literal string constant. */
9314 fmt_str = c_getstr (fmt);
9315 if (fmt_str == NULL)
9316 return NULL_TREE;
9317
9318 call = NULL_TREE;
9319 retval = NULL_TREE;
9320
9321 /* If the format doesn't contain % args or %%, use strcpy. */
9322 if (strchr (fmt_str, '%') == NULL)
9323 {
9324 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9325
9326 if (!fn)
9327 return NULL_TREE;
9328
9329 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9330 'format' is known to contain no % formats. */
9331 arglist = build_tree_list (NULL_TREE, fmt);
9332 arglist = tree_cons (NULL_TREE, dest, arglist);
9333 call = build_function_call_expr (fn, arglist);
9334 if (!ignored)
7016c612 9335 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
4ee9c684 9336 }
9337
9338 /* If the format is "%s", use strcpy if the result isn't used. */
9339 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9340 {
9341 tree fn, orig;
9342 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9343
9344 if (!fn)
9345 return NULL_TREE;
9346
9347 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9348 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9349 arglist = build_tree_list (NULL_TREE, orig);
9350 arglist = tree_cons (NULL_TREE, dest, arglist);
9351 if (!ignored)
9352 {
9353 retval = c_strlen (orig, 1);
9354 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9355 return NULL_TREE;
9356 }
9357 call = build_function_call_expr (fn, arglist);
9358 }
9359
9360 if (call && retval)
9361 {
9362 retval = convert
9363 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9364 retval);
05abc81b 9365 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
4ee9c684 9366 }
9367 else
9368 return call;
9369}