re PR target/30519 (Configuration mismatch -- extra parts)
[official-gcc.git] / gcc / builtins.c
blobbf57c7bf55e0e96e523cb897d2e3fdafccb82c5d
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "machmode.h"
28 #include "real.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "tree-gimple.h"
32 #include "flags.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "except.h"
36 #include "function.h"
37 #include "insn-config.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "typeclass.h"
44 #include "toplev.h"
45 #include "predict.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "langhooks.h"
49 #include "basic-block.h"
50 #include "tree-mudflap.h"
51 #include "tree-flow.h"
53 #ifndef PAD_VARARGS_DOWN
54 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
55 #endif
57 /* Define the names of the builtin function types and codes. */
58 const char *const built_in_class_names[4]
59 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
61 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
62 const char * built_in_names[(int) END_BUILTINS] =
64 #include "builtins.def"
66 #undef DEF_BUILTIN
68 /* Setup an array of _DECL trees, make sure each element is
69 initialized to NULL_TREE. */
70 tree built_in_decls[(int) END_BUILTINS];
71 /* Declarations used when constructing the builtin implicitly in the compiler.
72 It may be NULL_TREE when this is invalid (for instance runtime is not
73 required to implement the function call in all cases). */
74 tree implicit_built_in_decls[(int) END_BUILTINS];
76 static const char *c_getstr (tree);
77 static rtx c_readstr (const char *, enum machine_mode);
78 static int target_char_cast (tree, char *);
79 static rtx get_memory_rtx (tree, tree);
80 static int apply_args_size (void);
81 static int apply_result_size (void);
82 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
83 static rtx result_vector (int, rtx);
84 #endif
85 static void expand_builtin_update_setjmp_buf (rtx);
86 static void expand_builtin_prefetch (tree);
87 static rtx expand_builtin_apply_args (void);
88 static rtx expand_builtin_apply_args_1 (void);
89 static rtx expand_builtin_apply (rtx, rtx, rtx);
90 static void expand_builtin_return (rtx);
91 static enum type_class type_to_class (tree);
92 static rtx expand_builtin_classify_type (tree);
93 static void expand_errno_check (tree, rtx);
94 static rtx expand_builtin_mathfn (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
97 static rtx expand_builtin_sincos (tree);
98 static rtx expand_builtin_cexpi (tree, rtx, rtx);
99 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
100 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
101 static rtx expand_builtin_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree);
118 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
124 static rtx expand_builtin_bzero (tree);
125 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_alloca (tree, rtx);
131 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
132 static rtx expand_builtin_frame_address (tree, tree);
133 static rtx expand_builtin_fputs (tree, rtx, bool);
134 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
137 static tree stabilize_va_list (tree, int);
138 static rtx expand_builtin_expect (tree, rtx);
139 static tree fold_builtin_constant_p (tree);
140 static tree fold_builtin_classify_type (tree);
141 static tree fold_builtin_strlen (tree);
142 static tree fold_builtin_inf (tree, int);
143 static tree fold_builtin_nan (tree, tree, int);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_cosh (tree, tree, tree);
155 static tree fold_builtin_tan (tree, tree);
156 static tree fold_builtin_trunc (tree, tree);
157 static tree fold_builtin_floor (tree, tree);
158 static tree fold_builtin_ceil (tree, tree);
159 static tree fold_builtin_round (tree, tree);
160 static tree fold_builtin_int_roundingfn (tree, tree);
161 static tree fold_builtin_bitop (tree, tree);
162 static tree fold_builtin_memory_op (tree, tree, bool, int);
163 static tree fold_builtin_strchr (tree, tree);
164 static tree fold_builtin_memcmp (tree);
165 static tree fold_builtin_strcmp (tree);
166 static tree fold_builtin_strncmp (tree);
167 static tree fold_builtin_signbit (tree, tree);
168 static tree fold_builtin_copysign (tree, tree, tree);
169 static tree fold_builtin_isascii (tree);
170 static tree fold_builtin_toascii (tree);
171 static tree fold_builtin_isdigit (tree);
172 static tree fold_builtin_fabs (tree, tree);
173 static tree fold_builtin_abs (tree, tree);
174 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
175 enum tree_code);
176 static tree fold_builtin_1 (tree, tree, bool);
178 static tree fold_builtin_strpbrk (tree, tree);
179 static tree fold_builtin_strstr (tree, tree);
180 static tree fold_builtin_strrchr (tree, tree);
181 static tree fold_builtin_strcat (tree);
182 static tree fold_builtin_strncat (tree);
183 static tree fold_builtin_strspn (tree);
184 static tree fold_builtin_strcspn (tree);
185 static tree fold_builtin_sprintf (tree, int);
187 static rtx expand_builtin_object_size (tree);
188 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
189 enum built_in_function);
190 static void maybe_emit_chk_warning (tree, enum built_in_function);
191 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
192 static tree fold_builtin_object_size (tree);
193 static tree fold_builtin_strcat_chk (tree, tree);
194 static tree fold_builtin_strncat_chk (tree, tree);
195 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
196 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
197 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
198 static bool init_target_chars (void);
200 static unsigned HOST_WIDE_INT target_newline;
201 static unsigned HOST_WIDE_INT target_percent;
202 static unsigned HOST_WIDE_INT target_c;
203 static unsigned HOST_WIDE_INT target_s;
204 static char target_percent_c[3];
205 static char target_percent_s[3];
206 static char target_percent_s_newline[4];
207 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
208 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
209 static tree do_mpfr_arg2 (tree, tree, tree,
210 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
211 static tree do_mpfr_arg3 (tree, tree, tree, tree,
212 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
213 static tree do_mpfr_sincos (tree, tree, tree);
215 /* Return true if NODE should be considered for inline expansion regardless
216 of the optimization level. This means whenever a function is invoked with
217 its "internal" name, which normally contains the prefix "__builtin". */
219 static bool called_as_built_in (tree node)
221 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
222 if (strncmp (name, "__builtin_", 10) == 0)
223 return true;
224 if (strncmp (name, "__sync_", 7) == 0)
225 return true;
226 return false;
229 /* Return the alignment in bits of EXP, a pointer valued expression.
230 But don't return more than MAX_ALIGN no matter what.
231 The alignment returned is, by default, the alignment of the thing that
232 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
234 Otherwise, look at the expression to see if we can do better, i.e., if the
235 expression is actually pointing at an object whose alignment is tighter. */
238 get_pointer_alignment (tree exp, unsigned int max_align)
240 unsigned int align, inner;
242 /* We rely on TER to compute accurate alignment information. */
243 if (!(optimize && flag_tree_ter))
244 return 0;
246 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
247 return 0;
249 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
250 align = MIN (align, max_align);
252 while (1)
254 switch (TREE_CODE (exp))
256 case NOP_EXPR:
257 case CONVERT_EXPR:
258 case NON_LVALUE_EXPR:
259 exp = TREE_OPERAND (exp, 0);
260 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
261 return align;
263 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
264 align = MIN (inner, max_align);
265 break;
267 case PLUS_EXPR:
268 /* If sum of pointer + int, restrict our maximum alignment to that
269 imposed by the integer. If not, we can't do any better than
270 ALIGN. */
271 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
272 return align;
274 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
275 & (max_align / BITS_PER_UNIT - 1))
276 != 0)
277 max_align >>= 1;
279 exp = TREE_OPERAND (exp, 0);
280 break;
282 case ADDR_EXPR:
283 /* See what we are pointing at and look at its alignment. */
284 exp = TREE_OPERAND (exp, 0);
285 inner = max_align;
286 if (handled_component_p (exp))
288 HOST_WIDE_INT bitsize, bitpos;
289 tree offset;
290 enum machine_mode mode;
291 int unsignedp, volatilep;
293 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
294 &mode, &unsignedp, &volatilep, true);
295 if (bitpos)
296 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
297 if (offset && TREE_CODE (offset) == PLUS_EXPR
298 && host_integerp (TREE_OPERAND (offset, 1), 1))
300 /* Any overflow in calculating offset_bits won't change
301 the alignment. */
302 unsigned offset_bits
303 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
304 * BITS_PER_UNIT);
306 if (offset_bits)
307 inner = MIN (inner, (offset_bits & -offset_bits));
308 offset = TREE_OPERAND (offset, 0);
310 if (offset && TREE_CODE (offset) == MULT_EXPR
311 && host_integerp (TREE_OPERAND (offset, 1), 1))
313 /* Any overflow in calculating offset_factor won't change
314 the alignment. */
315 unsigned offset_factor
316 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
317 * BITS_PER_UNIT);
319 if (offset_factor)
320 inner = MIN (inner, (offset_factor & -offset_factor));
322 else if (offset)
323 inner = MIN (inner, BITS_PER_UNIT);
325 if (TREE_CODE (exp) == FUNCTION_DECL)
326 align = FUNCTION_BOUNDARY;
327 else if (DECL_P (exp))
328 align = MIN (inner, DECL_ALIGN (exp));
329 #ifdef CONSTANT_ALIGNMENT
330 else if (CONSTANT_CLASS_P (exp))
331 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
332 #endif
333 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
334 || TREE_CODE (exp) == INDIRECT_REF)
335 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
336 else
337 align = MIN (align, inner);
338 return MIN (align, max_align);
340 default:
341 return align;
346 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
347 way, because it could contain a zero byte in the middle.
348 TREE_STRING_LENGTH is the size of the character array, not the string.
350 ONLY_VALUE should be nonzero if the result is not going to be emitted
351 into the instruction stream and zero if it is going to be expanded.
352 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
353 is returned, otherwise NULL, since
354 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
355 evaluate the side-effects.
357 The value returned is of type `ssizetype'.
359 Unfortunately, string_constant can't access the values of const char
360 arrays with initializers, so neither can we do so here. */
362 tree
363 c_strlen (tree src, int only_value)
365 tree offset_node;
366 HOST_WIDE_INT offset;
367 int max;
368 const char *ptr;
370 STRIP_NOPS (src);
371 if (TREE_CODE (src) == COND_EXPR
372 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
374 tree len1, len2;
376 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
377 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
378 if (tree_int_cst_equal (len1, len2))
379 return len1;
382 if (TREE_CODE (src) == COMPOUND_EXPR
383 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
384 return c_strlen (TREE_OPERAND (src, 1), only_value);
386 src = string_constant (src, &offset_node);
387 if (src == 0)
388 return 0;
390 max = TREE_STRING_LENGTH (src) - 1;
391 ptr = TREE_STRING_POINTER (src);
393 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
395 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
396 compute the offset to the following null if we don't know where to
397 start searching for it. */
398 int i;
400 for (i = 0; i < max; i++)
401 if (ptr[i] == 0)
402 return 0;
404 /* We don't know the starting offset, but we do know that the string
405 has no internal zero bytes. We can assume that the offset falls
406 within the bounds of the string; otherwise, the programmer deserves
407 what he gets. Subtract the offset from the length of the string,
408 and return that. This would perhaps not be valid if we were dealing
409 with named arrays in addition to literal string constants. */
411 return size_diffop (size_int (max), offset_node);
414 /* We have a known offset into the string. Start searching there for
415 a null character if we can represent it as a single HOST_WIDE_INT. */
416 if (offset_node == 0)
417 offset = 0;
418 else if (! host_integerp (offset_node, 0))
419 offset = -1;
420 else
421 offset = tree_low_cst (offset_node, 0);
423 /* If the offset is known to be out of bounds, warn, and call strlen at
424 runtime. */
425 if (offset < 0 || offset > max)
427 warning (0, "offset outside bounds of constant string");
428 return 0;
431 /* Use strlen to search for the first zero byte. Since any strings
432 constructed with build_string will have nulls appended, we win even
433 if we get handed something like (char[4])"abcd".
435 Since OFFSET is our starting index into the string, no further
436 calculation is needed. */
437 return ssize_int (strlen (ptr + offset));
440 /* Return a char pointer for a C string if it is a string constant
441 or sum of string constant and integer constant. */
443 static const char *
444 c_getstr (tree src)
446 tree offset_node;
448 src = string_constant (src, &offset_node);
449 if (src == 0)
450 return 0;
452 if (offset_node == 0)
453 return TREE_STRING_POINTER (src);
454 else if (!host_integerp (offset_node, 1)
455 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
456 return 0;
458 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
461 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
462 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
464 static rtx
465 c_readstr (const char *str, enum machine_mode mode)
467 HOST_WIDE_INT c[2];
468 HOST_WIDE_INT ch;
469 unsigned int i, j;
471 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
473 c[0] = 0;
474 c[1] = 0;
475 ch = 1;
476 for (i = 0; i < GET_MODE_SIZE (mode); i++)
478 j = i;
479 if (WORDS_BIG_ENDIAN)
480 j = GET_MODE_SIZE (mode) - i - 1;
481 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
482 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
483 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
484 j *= BITS_PER_UNIT;
485 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
487 if (ch)
488 ch = (unsigned char) str[i];
489 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
491 return immed_double_const (c[0], c[1], mode);
494 /* Cast a target constant CST to target CHAR and if that value fits into
495 host char type, return zero and put that value into variable pointed to by
496 P. */
498 static int
499 target_char_cast (tree cst, char *p)
501 unsigned HOST_WIDE_INT val, hostval;
503 if (!host_integerp (cst, 1)
504 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
505 return 1;
507 val = tree_low_cst (cst, 1);
508 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
509 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
511 hostval = val;
512 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
513 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
515 if (val != hostval)
516 return 1;
518 *p = hostval;
519 return 0;
522 /* Similar to save_expr, but assumes that arbitrary code is not executed
523 in between the multiple evaluations. In particular, we assume that a
524 non-addressable local variable will not be modified. */
526 static tree
527 builtin_save_expr (tree exp)
529 if (TREE_ADDRESSABLE (exp) == 0
530 && (TREE_CODE (exp) == PARM_DECL
531 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
532 return exp;
534 return save_expr (exp);
537 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
538 times to get the address of either a higher stack frame, or a return
539 address located within it (depending on FNDECL_CODE). */
541 static rtx
542 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
544 int i;
546 #ifdef INITIAL_FRAME_ADDRESS_RTX
547 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
548 #else
549 rtx tem;
551 /* For a zero count with __builtin_return_address, we don't care what
552 frame address we return, because target-specific definitions will
553 override us. Therefore frame pointer elimination is OK, and using
554 the soft frame pointer is OK.
556 For a nonzero count, or a zero count with __builtin_frame_address,
557 we require a stable offset from the current frame pointer to the
558 previous one, so we must use the hard frame pointer, and
559 we must disable frame pointer elimination. */
560 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
561 tem = frame_pointer_rtx;
562 else
564 tem = hard_frame_pointer_rtx;
566 /* Tell reload not to eliminate the frame pointer. */
567 current_function_accesses_prior_frames = 1;
569 #endif
571 /* Some machines need special handling before we can access
572 arbitrary frames. For example, on the SPARC, we must first flush
573 all register windows to the stack. */
574 #ifdef SETUP_FRAME_ADDRESSES
575 if (count > 0)
576 SETUP_FRAME_ADDRESSES ();
577 #endif
579 /* On the SPARC, the return address is not in the frame, it is in a
580 register. There is no way to access it off of the current frame
581 pointer, but it can be accessed off the previous frame pointer by
582 reading the value from the register window save area. */
583 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
584 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
585 count--;
586 #endif
588 /* Scan back COUNT frames to the specified frame. */
589 for (i = 0; i < count; i++)
591 /* Assume the dynamic chain pointer is in the word that the
592 frame address points to, unless otherwise specified. */
593 #ifdef DYNAMIC_CHAIN_ADDRESS
594 tem = DYNAMIC_CHAIN_ADDRESS (tem);
595 #endif
596 tem = memory_address (Pmode, tem);
597 tem = gen_frame_mem (Pmode, tem);
598 tem = copy_to_reg (tem);
601 /* For __builtin_frame_address, return what we've got. But, on
602 the SPARC for example, we may have to add a bias. */
603 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
604 #ifdef FRAME_ADDR_RTX
605 return FRAME_ADDR_RTX (tem);
606 #else
607 return tem;
608 #endif
610 /* For __builtin_return_address, get the return address from that frame. */
611 #ifdef RETURN_ADDR_RTX
612 tem = RETURN_ADDR_RTX (count, tem);
613 #else
614 tem = memory_address (Pmode,
615 plus_constant (tem, GET_MODE_SIZE (Pmode)));
616 tem = gen_frame_mem (Pmode, tem);
617 #endif
618 return tem;
621 /* Alias set used for setjmp buffer. */
622 static HOST_WIDE_INT setjmp_alias_set = -1;
624 /* Construct the leading half of a __builtin_setjmp call. Control will
625 return to RECEIVER_LABEL. This is also called directly by the SJLJ
626 exception handling code. */
628 void
629 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
631 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
632 rtx stack_save;
633 rtx mem;
635 if (setjmp_alias_set == -1)
636 setjmp_alias_set = new_alias_set ();
638 buf_addr = convert_memory_address (Pmode, buf_addr);
640 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
642 /* We store the frame pointer and the address of receiver_label in
643 the buffer and use the rest of it for the stack save area, which
644 is machine-dependent. */
646 mem = gen_rtx_MEM (Pmode, buf_addr);
647 set_mem_alias_set (mem, setjmp_alias_set);
648 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
650 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
651 set_mem_alias_set (mem, setjmp_alias_set);
653 emit_move_insn (validize_mem (mem),
654 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
656 stack_save = gen_rtx_MEM (sa_mode,
657 plus_constant (buf_addr,
658 2 * GET_MODE_SIZE (Pmode)));
659 set_mem_alias_set (stack_save, setjmp_alias_set);
660 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
662 /* If there is further processing to do, do it. */
663 #ifdef HAVE_builtin_setjmp_setup
664 if (HAVE_builtin_setjmp_setup)
665 emit_insn (gen_builtin_setjmp_setup (buf_addr));
666 #endif
668 /* Tell optimize_save_area_alloca that extra work is going to
669 need to go on during alloca. */
670 current_function_calls_setjmp = 1;
672 /* Set this so all the registers get saved in our frame; we need to be
673 able to copy the saved values for any registers from frames we unwind. */
674 current_function_has_nonlocal_label = 1;
677 /* Construct the trailing part of a __builtin_setjmp call. This is
678 also called directly by the SJLJ exception handling code. */
680 void
681 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
683 /* Clobber the FP when we get here, so we have to make sure it's
684 marked as used by this function. */
685 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
687 /* Mark the static chain as clobbered here so life information
688 doesn't get messed up for it. */
689 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
691 /* Now put in the code to restore the frame pointer, and argument
692 pointer, if needed. */
693 #ifdef HAVE_nonlocal_goto
694 if (! HAVE_nonlocal_goto)
695 #endif
697 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
698 /* This might change the hard frame pointer in ways that aren't
699 apparent to early optimization passes, so force a clobber. */
700 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
703 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
704 if (fixed_regs[ARG_POINTER_REGNUM])
706 #ifdef ELIMINABLE_REGS
707 size_t i;
708 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
710 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
711 if (elim_regs[i].from == ARG_POINTER_REGNUM
712 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
713 break;
715 if (i == ARRAY_SIZE (elim_regs))
716 #endif
718 /* Now restore our arg pointer from the address at which it
719 was saved in our stack frame. */
720 emit_move_insn (virtual_incoming_args_rtx,
721 copy_to_reg (get_arg_pointer_save_area (cfun)));
724 #endif
726 #ifdef HAVE_builtin_setjmp_receiver
727 if (HAVE_builtin_setjmp_receiver)
728 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
729 else
730 #endif
731 #ifdef HAVE_nonlocal_goto_receiver
732 if (HAVE_nonlocal_goto_receiver)
733 emit_insn (gen_nonlocal_goto_receiver ());
734 else
735 #endif
736 { /* Nothing */ }
738 /* @@@ This is a kludge. Not all machine descriptions define a blockage
739 insn, but we must not allow the code we just generated to be reordered
740 by scheduling. Specifically, the update of the frame pointer must
741 happen immediately, not later. So emit an ASM_INPUT to act as blockage
742 insn. */
743 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
746 /* __builtin_longjmp is passed a pointer to an array of five words (not
747 all will be used on all machines). It operates similarly to the C
748 library function of the same name, but is more efficient. Much of
749 the code below is copied from the handling of non-local gotos. */
751 static void
752 expand_builtin_longjmp (rtx buf_addr, rtx value)
754 rtx fp, lab, stack, insn, last;
755 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
757 if (setjmp_alias_set == -1)
758 setjmp_alias_set = new_alias_set ();
760 buf_addr = convert_memory_address (Pmode, buf_addr);
762 buf_addr = force_reg (Pmode, buf_addr);
764 /* We used to store value in static_chain_rtx, but that fails if pointers
765 are smaller than integers. We instead require that the user must pass
766 a second argument of 1, because that is what builtin_setjmp will
767 return. This also makes EH slightly more efficient, since we are no
768 longer copying around a value that we don't care about. */
769 gcc_assert (value == const1_rtx);
771 last = get_last_insn ();
772 #ifdef HAVE_builtin_longjmp
773 if (HAVE_builtin_longjmp)
774 emit_insn (gen_builtin_longjmp (buf_addr));
775 else
776 #endif
778 fp = gen_rtx_MEM (Pmode, buf_addr);
779 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
780 GET_MODE_SIZE (Pmode)));
782 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
783 2 * GET_MODE_SIZE (Pmode)));
784 set_mem_alias_set (fp, setjmp_alias_set);
785 set_mem_alias_set (lab, setjmp_alias_set);
786 set_mem_alias_set (stack, setjmp_alias_set);
788 /* Pick up FP, label, and SP from the block and jump. This code is
789 from expand_goto in stmt.c; see there for detailed comments. */
790 #ifdef HAVE_nonlocal_goto
791 if (HAVE_nonlocal_goto)
792 /* We have to pass a value to the nonlocal_goto pattern that will
793 get copied into the static_chain pointer, but it does not matter
794 what that value is, because builtin_setjmp does not use it. */
795 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
796 else
797 #endif
799 lab = copy_to_reg (lab);
801 emit_insn (gen_rtx_CLOBBER (VOIDmode,
802 gen_rtx_MEM (BLKmode,
803 gen_rtx_SCRATCH (VOIDmode))));
804 emit_insn (gen_rtx_CLOBBER (VOIDmode,
805 gen_rtx_MEM (BLKmode,
806 hard_frame_pointer_rtx)));
808 emit_move_insn (hard_frame_pointer_rtx, fp);
809 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
811 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
812 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
813 emit_indirect_jump (lab);
817 /* Search backwards and mark the jump insn as a non-local goto.
818 Note that this precludes the use of __builtin_longjmp to a
819 __builtin_setjmp target in the same function. However, we've
820 already cautioned the user that these functions are for
821 internal exception handling use only. */
822 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
824 gcc_assert (insn != last);
826 if (JUMP_P (insn))
828 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
829 REG_NOTES (insn));
830 break;
832 else if (CALL_P (insn))
833 break;
837 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
838 and the address of the save area. */
840 static rtx
841 expand_builtin_nonlocal_goto (tree arglist)
843 tree t_label, t_save_area;
844 rtx r_label, r_save_area, r_fp, r_sp, insn;
846 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
847 return NULL_RTX;
849 t_label = TREE_VALUE (arglist);
850 arglist = TREE_CHAIN (arglist);
851 t_save_area = TREE_VALUE (arglist);
853 r_label = expand_normal (t_label);
854 r_label = convert_memory_address (Pmode, r_label);
855 r_save_area = expand_normal (t_save_area);
856 r_save_area = convert_memory_address (Pmode, r_save_area);
857 r_fp = gen_rtx_MEM (Pmode, r_save_area);
858 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
859 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
861 current_function_has_nonlocal_goto = 1;
863 #ifdef HAVE_nonlocal_goto
864 /* ??? We no longer need to pass the static chain value, afaik. */
865 if (HAVE_nonlocal_goto)
866 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
867 else
868 #endif
870 r_label = copy_to_reg (r_label);
872 emit_insn (gen_rtx_CLOBBER (VOIDmode,
873 gen_rtx_MEM (BLKmode,
874 gen_rtx_SCRATCH (VOIDmode))));
876 emit_insn (gen_rtx_CLOBBER (VOIDmode,
877 gen_rtx_MEM (BLKmode,
878 hard_frame_pointer_rtx)));
880 /* Restore frame pointer for containing function.
881 This sets the actual hard register used for the frame pointer
882 to the location of the function's incoming static chain info.
883 The non-local goto handler will then adjust it to contain the
884 proper value and reload the argument pointer, if needed. */
885 emit_move_insn (hard_frame_pointer_rtx, r_fp);
886 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
888 /* USE of hard_frame_pointer_rtx added for consistency;
889 not clear if really needed. */
890 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
891 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
892 emit_indirect_jump (r_label);
895 /* Search backwards to the jump insn and mark it as a
896 non-local goto. */
897 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
899 if (JUMP_P (insn))
901 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
902 const0_rtx, REG_NOTES (insn));
903 break;
905 else if (CALL_P (insn))
906 break;
909 return const0_rtx;
912 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
913 (not all will be used on all machines) that was passed to __builtin_setjmp.
914 It updates the stack pointer in that block to correspond to the current
915 stack pointer. */
917 static void
918 expand_builtin_update_setjmp_buf (rtx buf_addr)
920 enum machine_mode sa_mode = Pmode;
921 rtx stack_save;
924 #ifdef HAVE_save_stack_nonlocal
925 if (HAVE_save_stack_nonlocal)
926 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
927 #endif
928 #ifdef STACK_SAVEAREA_MODE
929 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
930 #endif
932 stack_save
933 = gen_rtx_MEM (sa_mode,
934 memory_address
935 (sa_mode,
936 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
938 #ifdef HAVE_setjmp
939 if (HAVE_setjmp)
940 emit_insn (gen_setjmp ());
941 #endif
943 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
946 /* Expand a call to __builtin_prefetch. For a target that does not support
947 data prefetch, evaluate the memory address argument in case it has side
948 effects. */
950 static void
951 expand_builtin_prefetch (tree arglist)
953 tree arg0, arg1, arg2;
954 rtx op0, op1, op2;
956 if (!validate_arglist (arglist, POINTER_TYPE, 0))
957 return;
959 arg0 = TREE_VALUE (arglist);
960 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
961 zero (read) and argument 2 (locality) defaults to 3 (high degree of
962 locality). */
963 if (TREE_CHAIN (arglist))
965 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
966 if (TREE_CHAIN (TREE_CHAIN (arglist)))
967 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
968 else
969 arg2 = build_int_cst (NULL_TREE, 3);
971 else
973 arg1 = integer_zero_node;
974 arg2 = build_int_cst (NULL_TREE, 3);
977 /* Argument 0 is an address. */
978 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
980 /* Argument 1 (read/write flag) must be a compile-time constant int. */
981 if (TREE_CODE (arg1) != INTEGER_CST)
983 error ("second argument to %<__builtin_prefetch%> must be a constant");
984 arg1 = integer_zero_node;
986 op1 = expand_normal (arg1);
987 /* Argument 1 must be either zero or one. */
988 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
990 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
991 " using zero");
992 op1 = const0_rtx;
995 /* Argument 2 (locality) must be a compile-time constant int. */
996 if (TREE_CODE (arg2) != INTEGER_CST)
998 error ("third argument to %<__builtin_prefetch%> must be a constant");
999 arg2 = integer_zero_node;
1001 op2 = expand_normal (arg2);
1002 /* Argument 2 must be 0, 1, 2, or 3. */
1003 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1005 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1006 op2 = const0_rtx;
1009 #ifdef HAVE_prefetch
1010 if (HAVE_prefetch)
1012 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1013 (op0,
1014 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1015 || (GET_MODE (op0) != Pmode))
1017 op0 = convert_memory_address (Pmode, op0);
1018 op0 = force_reg (Pmode, op0);
1020 emit_insn (gen_prefetch (op0, op1, op2));
1022 #endif
1024 /* Don't do anything with direct references to volatile memory, but
1025 generate code to handle other side effects. */
1026 if (!MEM_P (op0) && side_effects_p (op0))
1027 emit_insn (op0);
1030 /* Get a MEM rtx for expression EXP which is the address of an operand
1031 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1032 the maximum length of the block of memory that might be accessed or
1033 NULL if unknown. */
1035 static rtx
1036 get_memory_rtx (tree exp, tree len)
1038 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1039 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1041 /* Get an expression we can use to find the attributes to assign to MEM.
1042 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1043 we can. First remove any nops. */
1044 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1045 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1046 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1047 exp = TREE_OPERAND (exp, 0);
1049 if (TREE_CODE (exp) == ADDR_EXPR)
1050 exp = TREE_OPERAND (exp, 0);
1051 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1052 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1053 else
1054 exp = NULL;
1056 /* Honor attributes derived from exp, except for the alias set
1057 (as builtin stringops may alias with anything) and the size
1058 (as stringops may access multiple array elements). */
1059 if (exp)
1061 set_mem_attributes (mem, exp, 0);
1063 /* Allow the string and memory builtins to overflow from one
1064 field into another, see http://gcc.gnu.org/PR23561.
1065 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1066 memory accessed by the string or memory builtin will fit
1067 within the field. */
1068 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1070 tree mem_expr = MEM_EXPR (mem);
1071 HOST_WIDE_INT offset = -1, length = -1;
1072 tree inner = exp;
1074 while (TREE_CODE (inner) == ARRAY_REF
1075 || TREE_CODE (inner) == NOP_EXPR
1076 || TREE_CODE (inner) == CONVERT_EXPR
1077 || TREE_CODE (inner) == NON_LVALUE_EXPR
1078 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1079 || TREE_CODE (inner) == SAVE_EXPR)
1080 inner = TREE_OPERAND (inner, 0);
1082 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1084 if (MEM_OFFSET (mem)
1085 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1086 offset = INTVAL (MEM_OFFSET (mem));
1088 if (offset >= 0 && len && host_integerp (len, 0))
1089 length = tree_low_cst (len, 0);
1091 while (TREE_CODE (inner) == COMPONENT_REF)
1093 tree field = TREE_OPERAND (inner, 1);
1094 gcc_assert (! DECL_BIT_FIELD (field));
1095 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1096 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1098 if (length >= 0
1099 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1100 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1102 HOST_WIDE_INT size
1103 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1104 /* If we can prove the memory starting at XEXP (mem, 0)
1105 and ending at XEXP (mem, 0) + LENGTH will fit into
1106 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1107 if (offset <= size
1108 && length <= size
1109 && offset + length <= size)
1110 break;
1113 if (offset >= 0
1114 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1115 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1116 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1117 / BITS_PER_UNIT;
1118 else
1120 offset = -1;
1121 length = -1;
1124 mem_expr = TREE_OPERAND (mem_expr, 0);
1125 inner = TREE_OPERAND (inner, 0);
1128 if (mem_expr == NULL)
1129 offset = -1;
1130 if (mem_expr != MEM_EXPR (mem))
1132 set_mem_expr (mem, mem_expr);
1133 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1136 set_mem_alias_set (mem, 0);
1137 set_mem_size (mem, NULL_RTX);
1140 return mem;
1143 /* Built-in functions to perform an untyped call and return. */
1145 /* For each register that may be used for calling a function, this
1146 gives a mode used to copy the register's value. VOIDmode indicates
1147 the register is not used for calling a function. If the machine
1148 has register windows, this gives only the outbound registers.
1149 INCOMING_REGNO gives the corresponding inbound register. */
1150 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1152 /* For each register that may be used for returning values, this gives
1153 a mode used to copy the register's value. VOIDmode indicates the
1154 register is not used for returning values. If the machine has
1155 register windows, this gives only the outbound registers.
1156 INCOMING_REGNO gives the corresponding inbound register. */
1157 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1159 /* For each register that may be used for calling a function, this
1160 gives the offset of that register into the block returned by
1161 __builtin_apply_args. 0 indicates that the register is not
1162 used for calling a function. */
1163 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1165 /* Return the size required for the block returned by __builtin_apply_args,
1166 and initialize apply_args_mode. */
1168 static int
1169 apply_args_size (void)
1171 static int size = -1;
1172 int align;
1173 unsigned int regno;
1174 enum machine_mode mode;
1176 /* The values computed by this function never change. */
1177 if (size < 0)
1179 /* The first value is the incoming arg-pointer. */
1180 size = GET_MODE_SIZE (Pmode);
1182 /* The second value is the structure value address unless this is
1183 passed as an "invisible" first argument. */
1184 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1185 size += GET_MODE_SIZE (Pmode);
1187 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1188 if (FUNCTION_ARG_REGNO_P (regno))
1190 mode = reg_raw_mode[regno];
1192 gcc_assert (mode != VOIDmode);
1194 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1195 if (size % align != 0)
1196 size = CEIL (size, align) * align;
1197 apply_args_reg_offset[regno] = size;
1198 size += GET_MODE_SIZE (mode);
1199 apply_args_mode[regno] = mode;
1201 else
1203 apply_args_mode[regno] = VOIDmode;
1204 apply_args_reg_offset[regno] = 0;
1207 return size;
1210 /* Return the size required for the block returned by __builtin_apply,
1211 and initialize apply_result_mode. */
1213 static int
1214 apply_result_size (void)
1216 static int size = -1;
1217 int align, regno;
1218 enum machine_mode mode;
1220 /* The values computed by this function never change. */
1221 if (size < 0)
1223 size = 0;
1225 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1226 if (FUNCTION_VALUE_REGNO_P (regno))
1228 mode = reg_raw_mode[regno];
1230 gcc_assert (mode != VOIDmode);
1232 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1233 if (size % align != 0)
1234 size = CEIL (size, align) * align;
1235 size += GET_MODE_SIZE (mode);
1236 apply_result_mode[regno] = mode;
1238 else
1239 apply_result_mode[regno] = VOIDmode;
1241 /* Allow targets that use untyped_call and untyped_return to override
1242 the size so that machine-specific information can be stored here. */
1243 #ifdef APPLY_RESULT_SIZE
1244 size = APPLY_RESULT_SIZE;
1245 #endif
1247 return size;
1250 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1251 /* Create a vector describing the result block RESULT. If SAVEP is true,
1252 the result block is used to save the values; otherwise it is used to
1253 restore the values. */
1255 static rtx
1256 result_vector (int savep, rtx result)
1258 int regno, size, align, nelts;
1259 enum machine_mode mode;
1260 rtx reg, mem;
1261 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1263 size = nelts = 0;
1264 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1265 if ((mode = apply_result_mode[regno]) != VOIDmode)
1267 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1268 if (size % align != 0)
1269 size = CEIL (size, align) * align;
1270 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1271 mem = adjust_address (result, mode, size);
1272 savevec[nelts++] = (savep
1273 ? gen_rtx_SET (VOIDmode, mem, reg)
1274 : gen_rtx_SET (VOIDmode, reg, mem));
1275 size += GET_MODE_SIZE (mode);
1277 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1279 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1281 /* Save the state required to perform an untyped call with the same
1282 arguments as were passed to the current function. */
1284 static rtx
1285 expand_builtin_apply_args_1 (void)
1287 rtx registers, tem;
1288 int size, align, regno;
1289 enum machine_mode mode;
1290 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1292 /* Create a block where the arg-pointer, structure value address,
1293 and argument registers can be saved. */
1294 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1296 /* Walk past the arg-pointer and structure value address. */
1297 size = GET_MODE_SIZE (Pmode);
1298 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1299 size += GET_MODE_SIZE (Pmode);
1301 /* Save each register used in calling a function to the block. */
1302 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1303 if ((mode = apply_args_mode[regno]) != VOIDmode)
1305 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1306 if (size % align != 0)
1307 size = CEIL (size, align) * align;
1309 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1311 emit_move_insn (adjust_address (registers, mode, size), tem);
1312 size += GET_MODE_SIZE (mode);
1315 /* Save the arg pointer to the block. */
1316 tem = copy_to_reg (virtual_incoming_args_rtx);
1317 #ifdef STACK_GROWS_DOWNWARD
1318 /* We need the pointer as the caller actually passed them to us, not
1319 as we might have pretended they were passed. Make sure it's a valid
1320 operand, as emit_move_insn isn't expected to handle a PLUS. */
1322 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1323 NULL_RTX);
1324 #endif
1325 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1327 size = GET_MODE_SIZE (Pmode);
1329 /* Save the structure value address unless this is passed as an
1330 "invisible" first argument. */
1331 if (struct_incoming_value)
1333 emit_move_insn (adjust_address (registers, Pmode, size),
1334 copy_to_reg (struct_incoming_value));
1335 size += GET_MODE_SIZE (Pmode);
1338 /* Return the address of the block. */
1339 return copy_addr_to_reg (XEXP (registers, 0));
1342 /* __builtin_apply_args returns block of memory allocated on
1343 the stack into which is stored the arg pointer, structure
1344 value address, static chain, and all the registers that might
1345 possibly be used in performing a function call. The code is
1346 moved to the start of the function so the incoming values are
1347 saved. */
1349 static rtx
1350 expand_builtin_apply_args (void)
1352 /* Don't do __builtin_apply_args more than once in a function.
1353 Save the result of the first call and reuse it. */
1354 if (apply_args_value != 0)
1355 return apply_args_value;
1357 /* When this function is called, it means that registers must be
1358 saved on entry to this function. So we migrate the
1359 call to the first insn of this function. */
1360 rtx temp;
1361 rtx seq;
1363 start_sequence ();
1364 temp = expand_builtin_apply_args_1 ();
1365 seq = get_insns ();
1366 end_sequence ();
1368 apply_args_value = temp;
1370 /* Put the insns after the NOTE that starts the function.
1371 If this is inside a start_sequence, make the outer-level insn
1372 chain current, so the code is placed at the start of the
1373 function. */
1374 push_topmost_sequence ();
1375 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1376 pop_topmost_sequence ();
1377 return temp;
1381 /* Perform an untyped call and save the state required to perform an
1382 untyped return of whatever value was returned by the given function. */
1384 static rtx
1385 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1387 int size, align, regno;
1388 enum machine_mode mode;
1389 rtx incoming_args, result, reg, dest, src, call_insn;
1390 rtx old_stack_level = 0;
1391 rtx call_fusage = 0;
1392 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1394 arguments = convert_memory_address (Pmode, arguments);
1396 /* Create a block where the return registers can be saved. */
1397 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1399 /* Fetch the arg pointer from the ARGUMENTS block. */
1400 incoming_args = gen_reg_rtx (Pmode);
1401 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1402 #ifndef STACK_GROWS_DOWNWARD
1403 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1404 incoming_args, 0, OPTAB_LIB_WIDEN);
1405 #endif
1407 /* Push a new argument block and copy the arguments. Do not allow
1408 the (potential) memcpy call below to interfere with our stack
1409 manipulations. */
1410 do_pending_stack_adjust ();
1411 NO_DEFER_POP;
1413 /* Save the stack with nonlocal if available. */
1414 #ifdef HAVE_save_stack_nonlocal
1415 if (HAVE_save_stack_nonlocal)
1416 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1417 else
1418 #endif
1419 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1421 /* Allocate a block of memory onto the stack and copy the memory
1422 arguments to the outgoing arguments address. */
1423 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1424 dest = virtual_outgoing_args_rtx;
1425 #ifndef STACK_GROWS_DOWNWARD
1426 if (GET_CODE (argsize) == CONST_INT)
1427 dest = plus_constant (dest, -INTVAL (argsize));
1428 else
1429 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1430 #endif
1431 dest = gen_rtx_MEM (BLKmode, dest);
1432 set_mem_align (dest, PARM_BOUNDARY);
1433 src = gen_rtx_MEM (BLKmode, incoming_args);
1434 set_mem_align (src, PARM_BOUNDARY);
1435 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1437 /* Refer to the argument block. */
1438 apply_args_size ();
1439 arguments = gen_rtx_MEM (BLKmode, arguments);
1440 set_mem_align (arguments, PARM_BOUNDARY);
1442 /* Walk past the arg-pointer and structure value address. */
1443 size = GET_MODE_SIZE (Pmode);
1444 if (struct_value)
1445 size += GET_MODE_SIZE (Pmode);
1447 /* Restore each of the registers previously saved. Make USE insns
1448 for each of these registers for use in making the call. */
1449 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1450 if ((mode = apply_args_mode[regno]) != VOIDmode)
1452 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1453 if (size % align != 0)
1454 size = CEIL (size, align) * align;
1455 reg = gen_rtx_REG (mode, regno);
1456 emit_move_insn (reg, adjust_address (arguments, mode, size));
1457 use_reg (&call_fusage, reg);
1458 size += GET_MODE_SIZE (mode);
1461 /* Restore the structure value address unless this is passed as an
1462 "invisible" first argument. */
1463 size = GET_MODE_SIZE (Pmode);
1464 if (struct_value)
1466 rtx value = gen_reg_rtx (Pmode);
1467 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1468 emit_move_insn (struct_value, value);
1469 if (REG_P (struct_value))
1470 use_reg (&call_fusage, struct_value);
1471 size += GET_MODE_SIZE (Pmode);
1474 /* All arguments and registers used for the call are set up by now! */
1475 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1477 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1478 and we don't want to load it into a register as an optimization,
1479 because prepare_call_address already did it if it should be done. */
1480 if (GET_CODE (function) != SYMBOL_REF)
1481 function = memory_address (FUNCTION_MODE, function);
1483 /* Generate the actual call instruction and save the return value. */
1484 #ifdef HAVE_untyped_call
1485 if (HAVE_untyped_call)
1486 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1487 result, result_vector (1, result)));
1488 else
1489 #endif
1490 #ifdef HAVE_call_value
1491 if (HAVE_call_value)
1493 rtx valreg = 0;
1495 /* Locate the unique return register. It is not possible to
1496 express a call that sets more than one return register using
1497 call_value; use untyped_call for that. In fact, untyped_call
1498 only needs to save the return registers in the given block. */
1499 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1500 if ((mode = apply_result_mode[regno]) != VOIDmode)
1502 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1504 valreg = gen_rtx_REG (mode, regno);
1507 emit_call_insn (GEN_CALL_VALUE (valreg,
1508 gen_rtx_MEM (FUNCTION_MODE, function),
1509 const0_rtx, NULL_RTX, const0_rtx));
1511 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1513 else
1514 #endif
1515 gcc_unreachable ();
1517 /* Find the CALL insn we just emitted, and attach the register usage
1518 information. */
1519 call_insn = last_call_insn ();
1520 add_function_usage_to (call_insn, call_fusage);
1522 /* Restore the stack. */
1523 #ifdef HAVE_save_stack_nonlocal
1524 if (HAVE_save_stack_nonlocal)
1525 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1526 else
1527 #endif
1528 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1530 OK_DEFER_POP;
1532 /* Return the address of the result block. */
1533 result = copy_addr_to_reg (XEXP (result, 0));
1534 return convert_memory_address (ptr_mode, result);
1537 /* Perform an untyped return. */
1539 static void
1540 expand_builtin_return (rtx result)
1542 int size, align, regno;
1543 enum machine_mode mode;
1544 rtx reg;
1545 rtx call_fusage = 0;
1547 result = convert_memory_address (Pmode, result);
1549 apply_result_size ();
1550 result = gen_rtx_MEM (BLKmode, result);
1552 #ifdef HAVE_untyped_return
1553 if (HAVE_untyped_return)
1555 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1556 emit_barrier ();
1557 return;
1559 #endif
1561 /* Restore the return value and note that each value is used. */
1562 size = 0;
1563 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1564 if ((mode = apply_result_mode[regno]) != VOIDmode)
1566 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1567 if (size % align != 0)
1568 size = CEIL (size, align) * align;
1569 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1570 emit_move_insn (reg, adjust_address (result, mode, size));
1572 push_to_sequence (call_fusage);
1573 emit_insn (gen_rtx_USE (VOIDmode, reg));
1574 call_fusage = get_insns ();
1575 end_sequence ();
1576 size += GET_MODE_SIZE (mode);
1579 /* Put the USE insns before the return. */
1580 emit_insn (call_fusage);
1582 /* Return whatever values was restored by jumping directly to the end
1583 of the function. */
1584 expand_naked_return ();
1587 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1589 static enum type_class
1590 type_to_class (tree type)
1592 switch (TREE_CODE (type))
1594 case VOID_TYPE: return void_type_class;
1595 case INTEGER_TYPE: return integer_type_class;
1596 case ENUMERAL_TYPE: return enumeral_type_class;
1597 case BOOLEAN_TYPE: return boolean_type_class;
1598 case POINTER_TYPE: return pointer_type_class;
1599 case REFERENCE_TYPE: return reference_type_class;
1600 case OFFSET_TYPE: return offset_type_class;
1601 case REAL_TYPE: return real_type_class;
1602 case COMPLEX_TYPE: return complex_type_class;
1603 case FUNCTION_TYPE: return function_type_class;
1604 case METHOD_TYPE: return method_type_class;
1605 case RECORD_TYPE: return record_type_class;
1606 case UNION_TYPE:
1607 case QUAL_UNION_TYPE: return union_type_class;
1608 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1609 ? string_type_class : array_type_class);
1610 case LANG_TYPE: return lang_type_class;
1611 default: return no_type_class;
1615 /* Expand a call to __builtin_classify_type with arguments found in
1616 ARGLIST. */
1618 static rtx
1619 expand_builtin_classify_type (tree arglist)
1621 if (arglist != 0)
1622 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1623 return GEN_INT (no_type_class);
1626 /* This helper macro, meant to be used in mathfn_built_in below,
1627 determines which among a set of three builtin math functions is
1628 appropriate for a given type mode. The `F' and `L' cases are
1629 automatically generated from the `double' case. */
1630 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1631 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1632 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1633 fcodel = BUILT_IN_MATHFN##L ; break;
1635 /* Return mathematic function equivalent to FN but operating directly
1636 on TYPE, if available. If we can't do the conversion, return zero. */
1637 tree
1638 mathfn_built_in (tree type, enum built_in_function fn)
1640 enum built_in_function fcode, fcodef, fcodel;
1642 switch (fn)
1644 CASE_MATHFN (BUILT_IN_ACOS)
1645 CASE_MATHFN (BUILT_IN_ACOSH)
1646 CASE_MATHFN (BUILT_IN_ASIN)
1647 CASE_MATHFN (BUILT_IN_ASINH)
1648 CASE_MATHFN (BUILT_IN_ATAN)
1649 CASE_MATHFN (BUILT_IN_ATAN2)
1650 CASE_MATHFN (BUILT_IN_ATANH)
1651 CASE_MATHFN (BUILT_IN_CBRT)
1652 CASE_MATHFN (BUILT_IN_CEIL)
1653 CASE_MATHFN (BUILT_IN_CEXPI)
1654 CASE_MATHFN (BUILT_IN_COPYSIGN)
1655 CASE_MATHFN (BUILT_IN_COS)
1656 CASE_MATHFN (BUILT_IN_COSH)
1657 CASE_MATHFN (BUILT_IN_DREM)
1658 CASE_MATHFN (BUILT_IN_ERF)
1659 CASE_MATHFN (BUILT_IN_ERFC)
1660 CASE_MATHFN (BUILT_IN_EXP)
1661 CASE_MATHFN (BUILT_IN_EXP10)
1662 CASE_MATHFN (BUILT_IN_EXP2)
1663 CASE_MATHFN (BUILT_IN_EXPM1)
1664 CASE_MATHFN (BUILT_IN_FABS)
1665 CASE_MATHFN (BUILT_IN_FDIM)
1666 CASE_MATHFN (BUILT_IN_FLOOR)
1667 CASE_MATHFN (BUILT_IN_FMA)
1668 CASE_MATHFN (BUILT_IN_FMAX)
1669 CASE_MATHFN (BUILT_IN_FMIN)
1670 CASE_MATHFN (BUILT_IN_FMOD)
1671 CASE_MATHFN (BUILT_IN_FREXP)
1672 CASE_MATHFN (BUILT_IN_GAMMA)
1673 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1674 CASE_MATHFN (BUILT_IN_HYPOT)
1675 CASE_MATHFN (BUILT_IN_ILOGB)
1676 CASE_MATHFN (BUILT_IN_INF)
1677 CASE_MATHFN (BUILT_IN_J0)
1678 CASE_MATHFN (BUILT_IN_J1)
1679 CASE_MATHFN (BUILT_IN_JN)
1680 CASE_MATHFN (BUILT_IN_LCEIL)
1681 CASE_MATHFN (BUILT_IN_LDEXP)
1682 CASE_MATHFN (BUILT_IN_LFLOOR)
1683 CASE_MATHFN (BUILT_IN_LGAMMA)
1684 CASE_MATHFN (BUILT_IN_LLCEIL)
1685 CASE_MATHFN (BUILT_IN_LLFLOOR)
1686 CASE_MATHFN (BUILT_IN_LLRINT)
1687 CASE_MATHFN (BUILT_IN_LLROUND)
1688 CASE_MATHFN (BUILT_IN_LOG)
1689 CASE_MATHFN (BUILT_IN_LOG10)
1690 CASE_MATHFN (BUILT_IN_LOG1P)
1691 CASE_MATHFN (BUILT_IN_LOG2)
1692 CASE_MATHFN (BUILT_IN_LOGB)
1693 CASE_MATHFN (BUILT_IN_LRINT)
1694 CASE_MATHFN (BUILT_IN_LROUND)
1695 CASE_MATHFN (BUILT_IN_MODF)
1696 CASE_MATHFN (BUILT_IN_NAN)
1697 CASE_MATHFN (BUILT_IN_NANS)
1698 CASE_MATHFN (BUILT_IN_NEARBYINT)
1699 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1700 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1701 CASE_MATHFN (BUILT_IN_POW)
1702 CASE_MATHFN (BUILT_IN_POWI)
1703 CASE_MATHFN (BUILT_IN_POW10)
1704 CASE_MATHFN (BUILT_IN_REMAINDER)
1705 CASE_MATHFN (BUILT_IN_REMQUO)
1706 CASE_MATHFN (BUILT_IN_RINT)
1707 CASE_MATHFN (BUILT_IN_ROUND)
1708 CASE_MATHFN (BUILT_IN_SCALB)
1709 CASE_MATHFN (BUILT_IN_SCALBLN)
1710 CASE_MATHFN (BUILT_IN_SCALBN)
1711 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1712 CASE_MATHFN (BUILT_IN_SIN)
1713 CASE_MATHFN (BUILT_IN_SINCOS)
1714 CASE_MATHFN (BUILT_IN_SINH)
1715 CASE_MATHFN (BUILT_IN_SQRT)
1716 CASE_MATHFN (BUILT_IN_TAN)
1717 CASE_MATHFN (BUILT_IN_TANH)
1718 CASE_MATHFN (BUILT_IN_TGAMMA)
1719 CASE_MATHFN (BUILT_IN_TRUNC)
1720 CASE_MATHFN (BUILT_IN_Y0)
1721 CASE_MATHFN (BUILT_IN_Y1)
1722 CASE_MATHFN (BUILT_IN_YN)
1724 default:
1725 return 0;
1728 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1729 return implicit_built_in_decls[fcode];
1730 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1731 return implicit_built_in_decls[fcodef];
1732 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1733 return implicit_built_in_decls[fcodel];
1734 else
1735 return 0;
1738 /* If errno must be maintained, expand the RTL to check if the result,
1739 TARGET, of a built-in function call, EXP, is NaN, and if so set
1740 errno to EDOM. */
1742 static void
1743 expand_errno_check (tree exp, rtx target)
1745 rtx lab = gen_label_rtx ();
1747 /* Test the result; if it is NaN, set errno=EDOM because
1748 the argument was not in the domain. */
1749 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1750 0, lab);
1752 #ifdef TARGET_EDOM
1753 /* If this built-in doesn't throw an exception, set errno directly. */
1754 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1756 #ifdef GEN_ERRNO_RTX
1757 rtx errno_rtx = GEN_ERRNO_RTX;
1758 #else
1759 rtx errno_rtx
1760 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1761 #endif
1762 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1763 emit_label (lab);
1764 return;
1766 #endif
1768 /* We can't set errno=EDOM directly; let the library call do it.
1769 Pop the arguments right away in case the call gets deleted. */
1770 NO_DEFER_POP;
1771 expand_call (exp, target, 0);
1772 OK_DEFER_POP;
1773 emit_label (lab);
1777 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1778 Return 0 if a normal call should be emitted rather than expanding the
1779 function in-line. EXP is the expression that is a call to the builtin
1780 function; if convenient, the result should be placed in TARGET.
1781 SUBTARGET may be used as the target for computing one of EXP's operands. */
1783 static rtx
1784 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1786 optab builtin_optab;
1787 rtx op0, insns, before_call;
1788 tree fndecl = get_callee_fndecl (exp);
1789 tree arglist = TREE_OPERAND (exp, 1);
1790 enum machine_mode mode;
1791 bool errno_set = false;
1792 tree arg, narg;
1794 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1795 return 0;
1797 arg = TREE_VALUE (arglist);
1799 switch (DECL_FUNCTION_CODE (fndecl))
1801 CASE_FLT_FN (BUILT_IN_SQRT):
1802 errno_set = ! tree_expr_nonnegative_p (arg);
1803 builtin_optab = sqrt_optab;
1804 break;
1805 CASE_FLT_FN (BUILT_IN_EXP):
1806 errno_set = true; builtin_optab = exp_optab; break;
1807 CASE_FLT_FN (BUILT_IN_EXP10):
1808 CASE_FLT_FN (BUILT_IN_POW10):
1809 errno_set = true; builtin_optab = exp10_optab; break;
1810 CASE_FLT_FN (BUILT_IN_EXP2):
1811 errno_set = true; builtin_optab = exp2_optab; break;
1812 CASE_FLT_FN (BUILT_IN_EXPM1):
1813 errno_set = true; builtin_optab = expm1_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOGB):
1815 errno_set = true; builtin_optab = logb_optab; break;
1816 CASE_FLT_FN (BUILT_IN_ILOGB):
1817 errno_set = true; builtin_optab = ilogb_optab; break;
1818 CASE_FLT_FN (BUILT_IN_LOG):
1819 errno_set = true; builtin_optab = log_optab; break;
1820 CASE_FLT_FN (BUILT_IN_LOG10):
1821 errno_set = true; builtin_optab = log10_optab; break;
1822 CASE_FLT_FN (BUILT_IN_LOG2):
1823 errno_set = true; builtin_optab = log2_optab; break;
1824 CASE_FLT_FN (BUILT_IN_LOG1P):
1825 errno_set = true; builtin_optab = log1p_optab; break;
1826 CASE_FLT_FN (BUILT_IN_ASIN):
1827 builtin_optab = asin_optab; break;
1828 CASE_FLT_FN (BUILT_IN_ACOS):
1829 builtin_optab = acos_optab; break;
1830 CASE_FLT_FN (BUILT_IN_TAN):
1831 builtin_optab = tan_optab; break;
1832 CASE_FLT_FN (BUILT_IN_ATAN):
1833 builtin_optab = atan_optab; break;
1834 CASE_FLT_FN (BUILT_IN_FLOOR):
1835 builtin_optab = floor_optab; break;
1836 CASE_FLT_FN (BUILT_IN_CEIL):
1837 builtin_optab = ceil_optab; break;
1838 CASE_FLT_FN (BUILT_IN_TRUNC):
1839 builtin_optab = btrunc_optab; break;
1840 CASE_FLT_FN (BUILT_IN_ROUND):
1841 builtin_optab = round_optab; break;
1842 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1843 builtin_optab = nearbyint_optab;
1844 if (flag_trapping_math)
1845 break;
1846 /* Else fallthrough and expand as rint. */
1847 CASE_FLT_FN (BUILT_IN_RINT):
1848 builtin_optab = rint_optab; break;
1849 default:
1850 gcc_unreachable ();
1853 /* Make a suitable register to place result in. */
1854 mode = TYPE_MODE (TREE_TYPE (exp));
1856 if (! flag_errno_math || ! HONOR_NANS (mode))
1857 errno_set = false;
1859 /* Before working hard, check whether the instruction is available. */
1860 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1862 target = gen_reg_rtx (mode);
1864 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1865 need to expand the argument again. This way, we will not perform
1866 side-effects more the once. */
1867 narg = builtin_save_expr (arg);
1868 if (narg != arg)
1870 arg = narg;
1871 arglist = build_tree_list (NULL_TREE, arg);
1872 exp = build_function_call_expr (fndecl, arglist);
1875 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1877 start_sequence ();
1879 /* Compute into TARGET.
1880 Set TARGET to wherever the result comes back. */
1881 target = expand_unop (mode, builtin_optab, op0, target, 0);
1883 if (target != 0)
1885 if (errno_set)
1886 expand_errno_check (exp, target);
1888 /* Output the entire sequence. */
1889 insns = get_insns ();
1890 end_sequence ();
1891 emit_insn (insns);
1892 return target;
1895 /* If we were unable to expand via the builtin, stop the sequence
1896 (without outputting the insns) and call to the library function
1897 with the stabilized argument list. */
1898 end_sequence ();
1901 before_call = get_last_insn ();
1903 target = expand_call (exp, target, target == const0_rtx);
1905 /* If this is a sqrt operation and we don't care about errno, try to
1906 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1907 This allows the semantics of the libcall to be visible to the RTL
1908 optimizers. */
1909 if (builtin_optab == sqrt_optab && !errno_set)
1911 /* Search backwards through the insns emitted by expand_call looking
1912 for the instruction with the REG_RETVAL note. */
1913 rtx last = get_last_insn ();
1914 while (last != before_call)
1916 if (find_reg_note (last, REG_RETVAL, NULL))
1918 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1919 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1920 two elements, i.e. symbol_ref(sqrt) and the operand. */
1921 if (note
1922 && GET_CODE (note) == EXPR_LIST
1923 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1924 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1925 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1927 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1928 /* Check operand is a register with expected mode. */
1929 if (operand
1930 && REG_P (operand)
1931 && GET_MODE (operand) == mode)
1933 /* Replace the REG_EQUAL note with a SQRT rtx. */
1934 rtx equiv = gen_rtx_SQRT (mode, operand);
1935 set_unique_reg_note (last, REG_EQUAL, equiv);
1938 break;
1940 last = PREV_INSN (last);
1944 return target;
1947 /* Expand a call to the builtin binary math functions (pow and atan2).
1948 Return 0 if a normal call should be emitted rather than expanding the
1949 function in-line. EXP is the expression that is a call to the builtin
1950 function; if convenient, the result should be placed in TARGET.
1951 SUBTARGET may be used as the target for computing one of EXP's
1952 operands. */
1954 static rtx
1955 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1957 optab builtin_optab;
1958 rtx op0, op1, insns;
1959 int op1_type = REAL_TYPE;
1960 tree fndecl = get_callee_fndecl (exp);
1961 tree arglist = TREE_OPERAND (exp, 1);
1962 tree arg0, arg1, temp, narg;
1963 enum machine_mode mode;
1964 bool errno_set = true;
1965 bool stable = true;
1967 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1968 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1969 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1970 op1_type = INTEGER_TYPE;
1972 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1973 return 0;
1975 arg0 = TREE_VALUE (arglist);
1976 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1978 switch (DECL_FUNCTION_CODE (fndecl))
1980 CASE_FLT_FN (BUILT_IN_POW):
1981 builtin_optab = pow_optab; break;
1982 CASE_FLT_FN (BUILT_IN_ATAN2):
1983 builtin_optab = atan2_optab; break;
1984 CASE_FLT_FN (BUILT_IN_LDEXP):
1985 builtin_optab = ldexp_optab; break;
1986 CASE_FLT_FN (BUILT_IN_FMOD):
1987 builtin_optab = fmod_optab; break;
1988 CASE_FLT_FN (BUILT_IN_REMAINDER):
1989 CASE_FLT_FN (BUILT_IN_DREM):
1990 builtin_optab = remainder_optab; break;
1991 default:
1992 gcc_unreachable ();
1995 /* Make a suitable register to place result in. */
1996 mode = TYPE_MODE (TREE_TYPE (exp));
1998 /* Before working hard, check whether the instruction is available. */
1999 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2000 return 0;
2002 target = gen_reg_rtx (mode);
2004 if (! flag_errno_math || ! HONOR_NANS (mode))
2005 errno_set = false;
2007 /* Always stabilize the argument list. */
2008 narg = builtin_save_expr (arg1);
2009 if (narg != arg1)
2011 arg1 = narg;
2012 temp = build_tree_list (NULL_TREE, narg);
2013 stable = false;
2015 else
2016 temp = TREE_CHAIN (arglist);
2018 narg = builtin_save_expr (arg0);
2019 if (narg != arg0)
2021 arg0 = narg;
2022 arglist = tree_cons (NULL_TREE, narg, temp);
2023 stable = false;
2025 else if (! stable)
2026 arglist = tree_cons (NULL_TREE, arg0, temp);
2028 if (! stable)
2029 exp = build_function_call_expr (fndecl, arglist);
2031 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2032 op1 = expand_normal (arg1);
2034 start_sequence ();
2036 /* Compute into TARGET.
2037 Set TARGET to wherever the result comes back. */
2038 target = expand_binop (mode, builtin_optab, op0, op1,
2039 target, 0, OPTAB_DIRECT);
2041 /* If we were unable to expand via the builtin, stop the sequence
2042 (without outputting the insns) and call to the library function
2043 with the stabilized argument list. */
2044 if (target == 0)
2046 end_sequence ();
2047 return expand_call (exp, target, target == const0_rtx);
2050 if (errno_set)
2051 expand_errno_check (exp, target);
2053 /* Output the entire sequence. */
2054 insns = get_insns ();
2055 end_sequence ();
2056 emit_insn (insns);
2058 return target;
2061 /* Expand a call to the builtin sin and cos math functions.
2062 Return 0 if a normal call should be emitted rather than expanding the
2063 function in-line. EXP is the expression that is a call to the builtin
2064 function; if convenient, the result should be placed in TARGET.
2065 SUBTARGET may be used as the target for computing one of EXP's
2066 operands. */
2068 static rtx
2069 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2071 optab builtin_optab;
2072 rtx op0, insns;
2073 tree fndecl = get_callee_fndecl (exp);
2074 tree arglist = TREE_OPERAND (exp, 1);
2075 enum machine_mode mode;
2076 tree arg, narg;
2078 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2079 return 0;
2081 arg = TREE_VALUE (arglist);
2083 switch (DECL_FUNCTION_CODE (fndecl))
2085 CASE_FLT_FN (BUILT_IN_SIN):
2086 CASE_FLT_FN (BUILT_IN_COS):
2087 builtin_optab = sincos_optab; break;
2088 default:
2089 gcc_unreachable ();
2092 /* Make a suitable register to place result in. */
2093 mode = TYPE_MODE (TREE_TYPE (exp));
2095 /* Check if sincos insn is available, otherwise fallback
2096 to sin or cos insn. */
2097 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2098 switch (DECL_FUNCTION_CODE (fndecl))
2100 CASE_FLT_FN (BUILT_IN_SIN):
2101 builtin_optab = sin_optab; break;
2102 CASE_FLT_FN (BUILT_IN_COS):
2103 builtin_optab = cos_optab; break;
2104 default:
2105 gcc_unreachable ();
2108 /* Before working hard, check whether the instruction is available. */
2109 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2111 target = gen_reg_rtx (mode);
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 narg = save_expr (arg);
2117 if (narg != arg)
2119 arg = narg;
2120 arglist = build_tree_list (NULL_TREE, arg);
2121 exp = build_function_call_expr (fndecl, arglist);
2124 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2126 start_sequence ();
2128 /* Compute into TARGET.
2129 Set TARGET to wherever the result comes back. */
2130 if (builtin_optab == sincos_optab)
2132 int result;
2134 switch (DECL_FUNCTION_CODE (fndecl))
2136 CASE_FLT_FN (BUILT_IN_SIN):
2137 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2138 break;
2139 CASE_FLT_FN (BUILT_IN_COS):
2140 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2141 break;
2142 default:
2143 gcc_unreachable ();
2145 gcc_assert (result);
2147 else
2149 target = expand_unop (mode, builtin_optab, op0, target, 0);
2152 if (target != 0)
2154 /* Output the entire sequence. */
2155 insns = get_insns ();
2156 end_sequence ();
2157 emit_insn (insns);
2158 return target;
2161 /* If we were unable to expand via the builtin, stop the sequence
2162 (without outputting the insns) and call to the library function
2163 with the stabilized argument list. */
2164 end_sequence ();
2167 target = expand_call (exp, target, target == const0_rtx);
2169 return target;
2172 /* Expand a call to the builtin sincos math function.
2173 Return 0 if a normal call should be emitted rather than expanding the
2174 function in-line. EXP is the expression that is a call to the builtin
2175 function. */
2177 static rtx
2178 expand_builtin_sincos (tree exp)
2180 rtx op0, op1, op2, target1, target2;
2181 tree arglist = TREE_OPERAND (exp, 1);
2182 enum machine_mode mode;
2183 tree arg, sinp, cosp;
2184 int result;
2186 if (!validate_arglist (arglist, REAL_TYPE,
2187 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2188 return 0;
2190 arg = TREE_VALUE (arglist);
2191 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2192 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2194 /* Make a suitable register to place result in. */
2195 mode = TYPE_MODE (TREE_TYPE (arg));
2197 /* Check if sincos insn is available, otherwise emit the call. */
2198 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2199 return NULL_RTX;
2201 target1 = gen_reg_rtx (mode);
2202 target2 = gen_reg_rtx (mode);
2204 op0 = expand_normal (arg);
2205 op1 = expand_normal (build_fold_indirect_ref (sinp));
2206 op2 = expand_normal (build_fold_indirect_ref (cosp));
2208 /* Compute into target1 and target2.
2209 Set TARGET to wherever the result comes back. */
2210 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2211 gcc_assert (result);
2213 /* Move target1 and target2 to the memory locations indicated
2214 by op1 and op2. */
2215 emit_move_insn (op1, target1);
2216 emit_move_insn (op2, target2);
2218 return const0_rtx;
2221 /* Expand a call to the internal cexpi builtin to the sincos math function.
2222 EXP is the expression that is a call to the builtin function; if convenient,
2223 the result should be placed in TARGET. SUBTARGET may be used as the target
2224 for computing one of EXP's operands. */
2226 static rtx
2227 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2229 tree fndecl = get_callee_fndecl (exp);
2230 tree arglist = TREE_OPERAND (exp, 1);
2231 enum machine_mode mode;
2232 tree arg, type;
2233 rtx op0, op1, op2;
2235 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2236 return 0;
2238 arg = TREE_VALUE (arglist);
2239 type = TREE_TYPE (arg);
2240 mode = TYPE_MODE (TREE_TYPE (arg));
2242 /* Try expanding via a sincos optab, fall back to emitting a libcall
2243 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2244 is only generated from sincos, cexp or if we have either of them. */
2245 if (sincos_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2247 op1 = gen_reg_rtx (mode);
2248 op2 = gen_reg_rtx (mode);
2250 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2252 /* Compute into op1 and op2. */
2253 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2255 else if (TARGET_HAS_SINCOS)
2257 tree call, narglist, fn = NULL_TREE;
2258 tree top1, top2;
2259 rtx op1a, op2a;
2261 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2262 fn = built_in_decls[BUILT_IN_SINCOSF];
2263 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2264 fn = built_in_decls[BUILT_IN_SINCOS];
2265 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2266 fn = built_in_decls[BUILT_IN_SINCOSL];
2268 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2269 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2270 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2271 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2272 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2273 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2275 narglist = build_tree_list (NULL_TREE, top2);
2276 narglist = tree_cons (NULL_TREE, top1, narglist);
2277 narglist = tree_cons (NULL_TREE, arg, narglist);
2279 /* Make sure not to fold the sincos call again. */
2280 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2281 expand_normal (build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
2282 call, narglist, NULL_TREE));
2284 else
2286 tree call, fn, narg;
2287 tree ctype = build_complex_type (type);
2289 /* We can expand via the C99 cexp function. */
2290 gcc_assert (TARGET_C99_FUNCTIONS);
2292 fn = mathfn_built_in (ctype, BUILT_IN_CEXP);
2293 narg = fold_build2 (COMPLEX_EXPR, ctype,
2294 build_real (type, dconst0), arg);
2296 /* Make sure not to fold the cexp call again. */
2297 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2298 return expand_expr (build3 (CALL_EXPR, ctype, call,
2299 build_tree_list (NULL_TREE, narg),
2300 NULL_TREE), target, VOIDmode, 0);
2303 /* Now build the proper return type. */
2304 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2305 make_tree (TREE_TYPE (arg), op2),
2306 make_tree (TREE_TYPE (arg), op1)),
2307 target, VOIDmode, 0);
2310 /* Expand a call to one of the builtin rounding functions gcc defines
2311 as an extension (lfloor and lceil). As these are gcc extensions we
2312 do not need to worry about setting errno to EDOM.
2313 If expanding via optab fails, lower expression to (int)(floor(x)).
2314 EXP is the expression that is a call to the builtin function;
2315 if convenient, the result should be placed in TARGET. SUBTARGET may
2316 be used as the target for computing one of EXP's operands. */
2318 static rtx
2319 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2321 convert_optab builtin_optab;
2322 rtx op0, insns, tmp;
2323 tree fndecl = get_callee_fndecl (exp);
2324 tree arglist = TREE_OPERAND (exp, 1);
2325 enum built_in_function fallback_fn;
2326 tree fallback_fndecl;
2327 enum machine_mode mode;
2328 tree arg, narg;
2330 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2331 gcc_unreachable ();
2333 arg = TREE_VALUE (arglist);
2335 switch (DECL_FUNCTION_CODE (fndecl))
2337 CASE_FLT_FN (BUILT_IN_LCEIL):
2338 CASE_FLT_FN (BUILT_IN_LLCEIL):
2339 builtin_optab = lceil_optab;
2340 fallback_fn = BUILT_IN_CEIL;
2341 break;
2343 CASE_FLT_FN (BUILT_IN_LFLOOR):
2344 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2345 builtin_optab = lfloor_optab;
2346 fallback_fn = BUILT_IN_FLOOR;
2347 break;
2349 default:
2350 gcc_unreachable ();
2353 /* Make a suitable register to place result in. */
2354 mode = TYPE_MODE (TREE_TYPE (exp));
2356 target = gen_reg_rtx (mode);
2358 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2359 need to expand the argument again. This way, we will not perform
2360 side-effects more the once. */
2361 narg = builtin_save_expr (arg);
2362 if (narg != arg)
2364 arg = narg;
2365 arglist = build_tree_list (NULL_TREE, arg);
2366 exp = build_function_call_expr (fndecl, arglist);
2369 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2371 start_sequence ();
2373 /* Compute into TARGET. */
2374 if (expand_sfix_optab (target, op0, builtin_optab))
2376 /* Output the entire sequence. */
2377 insns = get_insns ();
2378 end_sequence ();
2379 emit_insn (insns);
2380 return target;
2383 /* If we were unable to expand via the builtin, stop the sequence
2384 (without outputting the insns). */
2385 end_sequence ();
2387 /* Fall back to floating point rounding optab. */
2388 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2389 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2390 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2391 gcc_assert (fallback_fndecl != NULL_TREE);
2392 exp = build_function_call_expr (fallback_fndecl, arglist);
2394 tmp = expand_normal (exp);
2396 /* Truncate the result of floating point optab to integer
2397 via expand_fix (). */
2398 target = gen_reg_rtx (mode);
2399 expand_fix (target, tmp, 0);
2401 return target;
2404 /* Expand a call to one of the builtin math functions doing integer
2405 conversion (lrint).
2406 Return 0 if a normal call should be emitted rather than expanding the
2407 function in-line. EXP is the expression that is a call to the builtin
2408 function; if convenient, the result should be placed in TARGET.
2409 SUBTARGET may be used as the target for computing one of EXP's operands. */
2411 static rtx
2412 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2414 convert_optab builtin_optab;
2415 rtx op0, insns;
2416 tree fndecl = get_callee_fndecl (exp);
2417 tree arglist = TREE_OPERAND (exp, 1);
2418 enum machine_mode mode;
2419 tree arg, narg;
2421 /* There's no easy way to detect the case we need to set EDOM. */
2422 if (flag_errno_math)
2423 return NULL_RTX;
2425 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2426 return NULL_RTX;
2428 arg = TREE_VALUE (arglist);
2430 switch (DECL_FUNCTION_CODE (fndecl))
2432 CASE_FLT_FN (BUILT_IN_LRINT):
2433 CASE_FLT_FN (BUILT_IN_LLRINT):
2434 builtin_optab = lrint_optab; break;
2435 CASE_FLT_FN (BUILT_IN_LROUND):
2436 CASE_FLT_FN (BUILT_IN_LLROUND):
2437 builtin_optab = lround_optab; break;
2438 default:
2439 gcc_unreachable ();
2442 /* Make a suitable register to place result in. */
2443 mode = TYPE_MODE (TREE_TYPE (exp));
2445 target = gen_reg_rtx (mode);
2447 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2448 need to expand the argument again. This way, we will not perform
2449 side-effects more the once. */
2450 narg = builtin_save_expr (arg);
2451 if (narg != arg)
2453 arg = narg;
2454 arglist = build_tree_list (NULL_TREE, arg);
2455 exp = build_function_call_expr (fndecl, arglist);
2458 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2460 start_sequence ();
2462 if (expand_sfix_optab (target, op0, builtin_optab))
2464 /* Output the entire sequence. */
2465 insns = get_insns ();
2466 end_sequence ();
2467 emit_insn (insns);
2468 return target;
2471 /* If we were unable to expand via the builtin, stop the sequence
2472 (without outputting the insns) and call to the library function
2473 with the stabilized argument list. */
2474 end_sequence ();
2476 target = expand_call (exp, target, target == const0_rtx);
2478 return target;
2481 /* To evaluate powi(x,n), the floating point value x raised to the
2482 constant integer exponent n, we use a hybrid algorithm that
2483 combines the "window method" with look-up tables. For an
2484 introduction to exponentiation algorithms and "addition chains",
2485 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2486 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2487 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2488 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2490 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2491 multiplications to inline before calling the system library's pow
2492 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2493 so this default never requires calling pow, powf or powl. */
2495 #ifndef POWI_MAX_MULTS
2496 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2497 #endif
2499 /* The size of the "optimal power tree" lookup table. All
2500 exponents less than this value are simply looked up in the
2501 powi_table below. This threshold is also used to size the
2502 cache of pseudo registers that hold intermediate results. */
2503 #define POWI_TABLE_SIZE 256
2505 /* The size, in bits of the window, used in the "window method"
2506 exponentiation algorithm. This is equivalent to a radix of
2507 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2508 #define POWI_WINDOW_SIZE 3
2510 /* The following table is an efficient representation of an
2511 "optimal power tree". For each value, i, the corresponding
2512 value, j, in the table states than an optimal evaluation
2513 sequence for calculating pow(x,i) can be found by evaluating
2514 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2515 100 integers is given in Knuth's "Seminumerical algorithms". */
2517 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2519 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2520 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2521 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2522 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2523 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2524 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2525 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2526 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2527 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2528 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2529 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2530 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2531 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2532 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2533 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2534 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2535 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2536 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2537 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2538 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2539 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2540 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2541 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2542 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2543 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2544 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2545 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2546 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2547 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2548 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2549 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2550 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2554 /* Return the number of multiplications required to calculate
2555 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2556 subroutine of powi_cost. CACHE is an array indicating
2557 which exponents have already been calculated. */
2559 static int
2560 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2562 /* If we've already calculated this exponent, then this evaluation
2563 doesn't require any additional multiplications. */
2564 if (cache[n])
2565 return 0;
2567 cache[n] = true;
2568 return powi_lookup_cost (n - powi_table[n], cache)
2569 + powi_lookup_cost (powi_table[n], cache) + 1;
2572 /* Return the number of multiplications required to calculate
2573 powi(x,n) for an arbitrary x, given the exponent N. This
2574 function needs to be kept in sync with expand_powi below. */
2576 static int
2577 powi_cost (HOST_WIDE_INT n)
2579 bool cache[POWI_TABLE_SIZE];
2580 unsigned HOST_WIDE_INT digit;
2581 unsigned HOST_WIDE_INT val;
2582 int result;
2584 if (n == 0)
2585 return 0;
2587 /* Ignore the reciprocal when calculating the cost. */
2588 val = (n < 0) ? -n : n;
2590 /* Initialize the exponent cache. */
2591 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2592 cache[1] = true;
2594 result = 0;
2596 while (val >= POWI_TABLE_SIZE)
2598 if (val & 1)
2600 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2601 result += powi_lookup_cost (digit, cache)
2602 + POWI_WINDOW_SIZE + 1;
2603 val >>= POWI_WINDOW_SIZE;
2605 else
2607 val >>= 1;
2608 result++;
2612 return result + powi_lookup_cost (val, cache);
2615 /* Recursive subroutine of expand_powi. This function takes the array,
2616 CACHE, of already calculated exponents and an exponent N and returns
2617 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2619 static rtx
2620 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2622 unsigned HOST_WIDE_INT digit;
2623 rtx target, result;
2624 rtx op0, op1;
2626 if (n < POWI_TABLE_SIZE)
2628 if (cache[n])
2629 return cache[n];
2631 target = gen_reg_rtx (mode);
2632 cache[n] = target;
2634 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2635 op1 = expand_powi_1 (mode, powi_table[n], cache);
2637 else if (n & 1)
2639 target = gen_reg_rtx (mode);
2640 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2641 op0 = expand_powi_1 (mode, n - digit, cache);
2642 op1 = expand_powi_1 (mode, digit, cache);
2644 else
2646 target = gen_reg_rtx (mode);
2647 op0 = expand_powi_1 (mode, n >> 1, cache);
2648 op1 = op0;
2651 result = expand_mult (mode, op0, op1, target, 0);
2652 if (result != target)
2653 emit_move_insn (target, result);
2654 return target;
2657 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2658 floating point operand in mode MODE, and N is the exponent. This
2659 function needs to be kept in sync with powi_cost above. */
2661 static rtx
2662 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2664 unsigned HOST_WIDE_INT val;
2665 rtx cache[POWI_TABLE_SIZE];
2666 rtx result;
2668 if (n == 0)
2669 return CONST1_RTX (mode);
2671 val = (n < 0) ? -n : n;
2673 memset (cache, 0, sizeof (cache));
2674 cache[1] = x;
2676 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2678 /* If the original exponent was negative, reciprocate the result. */
2679 if (n < 0)
2680 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2681 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2683 return result;
2686 /* Expand a call to the pow built-in mathematical function. Return 0 if
2687 a normal call should be emitted rather than expanding the function
2688 in-line. EXP is the expression that is a call to the builtin
2689 function; if convenient, the result should be placed in TARGET. */
2691 static rtx
2692 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2694 tree arg0, arg1, fn, narg0, narglist;
2695 tree arglist = TREE_OPERAND (exp, 1);
2696 tree type = TREE_TYPE (exp);
2697 REAL_VALUE_TYPE cint, c, c2;
2698 HOST_WIDE_INT n;
2699 rtx op, op2;
2700 enum machine_mode mode = TYPE_MODE (type);
2702 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2703 return 0;
2705 arg0 = TREE_VALUE (arglist);
2706 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2708 if (TREE_CODE (arg1) != REAL_CST
2709 || TREE_OVERFLOW (arg1))
2710 return expand_builtin_mathfn_2 (exp, target, subtarget);
2712 /* Handle constant exponents. */
2714 /* For integer valued exponents we can expand to an optimal multiplication
2715 sequence using expand_powi. */
2716 c = TREE_REAL_CST (arg1);
2717 n = real_to_integer (&c);
2718 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2719 if (real_identical (&c, &cint)
2720 && ((n >= -1 && n <= 2)
2721 || (flag_unsafe_math_optimizations
2722 && !optimize_size
2723 && powi_cost (n) <= POWI_MAX_MULTS)))
2725 op = expand_expr (arg0, subtarget, VOIDmode, 0);
2726 if (n != 1)
2728 op = force_reg (mode, op);
2729 op = expand_powi (op, mode, n);
2731 return op;
2734 narg0 = builtin_save_expr (arg0);
2735 narglist = build_tree_list (NULL_TREE, narg0);
2737 /* If the exponent is not integer valued, check if it is half of an integer.
2738 In this case we can expand to sqrt (x) * x**(n/2). */
2739 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2740 if (fn != NULL_TREE)
2742 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2743 n = real_to_integer (&c2);
2744 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2745 if (real_identical (&c2, &cint)
2746 && ((flag_unsafe_math_optimizations
2747 && !optimize_size
2748 && powi_cost (n/2) <= POWI_MAX_MULTS)
2749 || n == 1))
2751 tree call_expr = build_function_call_expr (fn, narglist);
2752 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2753 if (n != 1)
2755 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2756 op2 = force_reg (mode, op2);
2757 op2 = expand_powi (op2, mode, abs (n / 2));
2758 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2759 0, OPTAB_LIB_WIDEN);
2760 /* If the original exponent was negative, reciprocate the
2761 result. */
2762 if (n < 0)
2763 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2764 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2766 return op;
2770 /* Try if the exponent is a third of an integer. In this case
2771 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2772 different from pow (x, 1./3.) due to rounding and behavior
2773 with negative x we need to constrain this transformation to
2774 unsafe math and positive x or finite math. */
2775 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2776 if (fn != NULL_TREE
2777 && flag_unsafe_math_optimizations
2778 && (tree_expr_nonnegative_p (arg0)
2779 || !HONOR_NANS (mode)))
2781 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2782 real_round (&c2, mode, &c2);
2783 n = real_to_integer (&c2);
2784 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2785 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2786 real_convert (&c2, mode, &c2);
2787 if (real_identical (&c2, &c)
2788 && ((!optimize_size
2789 && powi_cost (n/3) <= POWI_MAX_MULTS)
2790 || n == 1))
2792 tree call_expr = build_function_call_expr (fn, narglist);
2793 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2794 if (abs (n) % 3 == 2)
2795 op = expand_simple_binop (mode, MULT, op, op, op,
2796 0, OPTAB_LIB_WIDEN);
2797 if (n != 1)
2799 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2800 op2 = force_reg (mode, op2);
2801 op2 = expand_powi (op2, mode, abs (n / 3));
2802 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2803 0, OPTAB_LIB_WIDEN);
2804 /* If the original exponent was negative, reciprocate the
2805 result. */
2806 if (n < 0)
2807 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2808 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2810 return op;
2814 /* Fall back to optab expansion. */
2815 return expand_builtin_mathfn_2 (exp, target, subtarget);
2818 /* Expand a call to the powi built-in mathematical function. Return 0 if
2819 a normal call should be emitted rather than expanding the function
2820 in-line. EXP is the expression that is a call to the builtin
2821 function; if convenient, the result should be placed in TARGET. */
2823 static rtx
2824 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2826 tree arglist = TREE_OPERAND (exp, 1);
2827 tree arg0, arg1;
2828 rtx op0, op1;
2829 enum machine_mode mode;
2830 enum machine_mode mode2;
2832 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2833 return 0;
2835 arg0 = TREE_VALUE (arglist);
2836 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2837 mode = TYPE_MODE (TREE_TYPE (exp));
2839 /* Handle constant power. */
2841 if (TREE_CODE (arg1) == INTEGER_CST
2842 && !TREE_OVERFLOW (arg1))
2844 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2846 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2847 Otherwise, check the number of multiplications required. */
2848 if ((TREE_INT_CST_HIGH (arg1) == 0
2849 || TREE_INT_CST_HIGH (arg1) == -1)
2850 && ((n >= -1 && n <= 2)
2851 || (! optimize_size
2852 && powi_cost (n) <= POWI_MAX_MULTS)))
2854 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2855 op0 = force_reg (mode, op0);
2856 return expand_powi (op0, mode, n);
2860 /* Emit a libcall to libgcc. */
2862 /* Mode of the 2nd argument must match that of an int. */
2863 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2865 if (target == NULL_RTX)
2866 target = gen_reg_rtx (mode);
2868 op0 = expand_expr (arg0, subtarget, mode, 0);
2869 if (GET_MODE (op0) != mode)
2870 op0 = convert_to_mode (mode, op0, 0);
2871 op1 = expand_expr (arg1, 0, mode2, 0);
2872 if (GET_MODE (op1) != mode2)
2873 op1 = convert_to_mode (mode2, op1, 0);
2875 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2876 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2877 op0, mode, op1, mode2);
2879 return target;
2882 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2883 if we failed the caller should emit a normal call, otherwise
2884 try to get the result in TARGET, if convenient. */
2886 static rtx
2887 expand_builtin_strlen (tree arglist, rtx target,
2888 enum machine_mode target_mode)
2890 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2891 return 0;
2892 else
2894 rtx pat;
2895 tree len, src = TREE_VALUE (arglist);
2896 rtx result, src_reg, char_rtx, before_strlen;
2897 enum machine_mode insn_mode = target_mode, char_mode;
2898 enum insn_code icode = CODE_FOR_nothing;
2899 int align;
2901 /* If the length can be computed at compile-time, return it. */
2902 len = c_strlen (src, 0);
2903 if (len)
2904 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2906 /* If the length can be computed at compile-time and is constant
2907 integer, but there are side-effects in src, evaluate
2908 src for side-effects, then return len.
2909 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2910 can be optimized into: i++; x = 3; */
2911 len = c_strlen (src, 1);
2912 if (len && TREE_CODE (len) == INTEGER_CST)
2914 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2915 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2918 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2920 /* If SRC is not a pointer type, don't do this operation inline. */
2921 if (align == 0)
2922 return 0;
2924 /* Bail out if we can't compute strlen in the right mode. */
2925 while (insn_mode != VOIDmode)
2927 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2928 if (icode != CODE_FOR_nothing)
2929 break;
2931 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2933 if (insn_mode == VOIDmode)
2934 return 0;
2936 /* Make a place to write the result of the instruction. */
2937 result = target;
2938 if (! (result != 0
2939 && REG_P (result)
2940 && GET_MODE (result) == insn_mode
2941 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2942 result = gen_reg_rtx (insn_mode);
2944 /* Make a place to hold the source address. We will not expand
2945 the actual source until we are sure that the expansion will
2946 not fail -- there are trees that cannot be expanded twice. */
2947 src_reg = gen_reg_rtx (Pmode);
2949 /* Mark the beginning of the strlen sequence so we can emit the
2950 source operand later. */
2951 before_strlen = get_last_insn ();
2953 char_rtx = const0_rtx;
2954 char_mode = insn_data[(int) icode].operand[2].mode;
2955 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2956 char_mode))
2957 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2959 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2960 char_rtx, GEN_INT (align));
2961 if (! pat)
2962 return 0;
2963 emit_insn (pat);
2965 /* Now that we are assured of success, expand the source. */
2966 start_sequence ();
2967 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2968 if (pat != src_reg)
2969 emit_move_insn (src_reg, pat);
2970 pat = get_insns ();
2971 end_sequence ();
2973 if (before_strlen)
2974 emit_insn_after (pat, before_strlen);
2975 else
2976 emit_insn_before (pat, get_insns ());
2978 /* Return the value in the proper mode for this function. */
2979 if (GET_MODE (result) == target_mode)
2980 target = result;
2981 else if (target != 0)
2982 convert_move (target, result, 0);
2983 else
2984 target = convert_to_mode (target_mode, result, 0);
2986 return target;
2990 /* Expand a call to the strstr builtin. Return 0 if we failed the
2991 caller should emit a normal call, otherwise try to get the result
2992 in TARGET, if convenient (and in mode MODE if that's convenient). */
2994 static rtx
2995 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2997 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2999 tree result = fold_builtin_strstr (arglist, type);
3000 if (result)
3001 return expand_expr (result, target, mode, EXPAND_NORMAL);
3003 return 0;
3006 /* Expand a call to the strchr builtin. Return 0 if we failed the
3007 caller should emit a normal call, otherwise try to get the result
3008 in TARGET, if convenient (and in mode MODE if that's convenient). */
3010 static rtx
3011 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3013 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3015 tree result = fold_builtin_strchr (arglist, type);
3016 if (result)
3017 return expand_expr (result, target, mode, EXPAND_NORMAL);
3019 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3021 return 0;
3024 /* Expand a call to the strrchr builtin. Return 0 if we failed the
3025 caller should emit a normal call, otherwise try to get the result
3026 in TARGET, if convenient (and in mode MODE if that's convenient). */
3028 static rtx
3029 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3031 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3033 tree result = fold_builtin_strrchr (arglist, type);
3034 if (result)
3035 return expand_expr (result, target, mode, EXPAND_NORMAL);
3037 return 0;
3040 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
3041 caller should emit a normal call, otherwise try to get the result
3042 in TARGET, if convenient (and in mode MODE if that's convenient). */
3044 static rtx
3045 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
3047 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3049 tree result = fold_builtin_strpbrk (arglist, type);
3050 if (result)
3051 return expand_expr (result, target, mode, EXPAND_NORMAL);
3053 return 0;
3056 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3057 bytes from constant string DATA + OFFSET and return it as target
3058 constant. */
3060 static rtx
3061 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3062 enum machine_mode mode)
3064 const char *str = (const char *) data;
3066 gcc_assert (offset >= 0
3067 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3068 <= strlen (str) + 1));
3070 return c_readstr (str + offset, mode);
3073 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
3074 Return 0 if we failed, the caller should emit a normal call,
3075 otherwise try to get the result in TARGET, if convenient (and in
3076 mode MODE if that's convenient). */
3077 static rtx
3078 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3080 tree fndecl = get_callee_fndecl (exp);
3081 tree arglist = TREE_OPERAND (exp, 1);
3082 if (!validate_arglist (arglist,
3083 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3084 return 0;
3085 else
3087 tree dest = TREE_VALUE (arglist);
3088 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3089 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3090 const char *src_str;
3091 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3092 unsigned int dest_align
3093 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3094 rtx dest_mem, src_mem, dest_addr, len_rtx;
3095 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
3096 false, /*endp=*/0);
3098 if (result)
3100 while (TREE_CODE (result) == COMPOUND_EXPR)
3102 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3103 EXPAND_NORMAL);
3104 result = TREE_OPERAND (result, 1);
3106 return expand_expr (result, target, mode, EXPAND_NORMAL);
3109 /* If DEST is not a pointer type, call the normal function. */
3110 if (dest_align == 0)
3111 return 0;
3113 /* If either SRC is not a pointer type, don't do this
3114 operation in-line. */
3115 if (src_align == 0)
3116 return 0;
3118 dest_mem = get_memory_rtx (dest, len);
3119 set_mem_align (dest_mem, dest_align);
3120 len_rtx = expand_normal (len);
3121 src_str = c_getstr (src);
3123 /* If SRC is a string constant and block move would be done
3124 by pieces, we can avoid loading the string from memory
3125 and only stored the computed constants. */
3126 if (src_str
3127 && GET_CODE (len_rtx) == CONST_INT
3128 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3129 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3130 (void *) src_str, dest_align))
3132 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3133 builtin_memcpy_read_str,
3134 (void *) src_str, dest_align, 0);
3135 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3136 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3137 return dest_mem;
3140 src_mem = get_memory_rtx (src, len);
3141 set_mem_align (src_mem, src_align);
3143 /* Copy word part most expediently. */
3144 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
3145 CALL_EXPR_TAILCALL (exp)
3146 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3148 if (dest_addr == 0)
3150 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3151 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3153 return dest_addr;
3157 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
3158 Return 0 if we failed; the caller should emit a normal call,
3159 otherwise try to get the result in TARGET, if convenient (and in
3160 mode MODE if that's convenient). If ENDP is 0 return the
3161 destination pointer, if ENDP is 1 return the end pointer ala
3162 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3163 stpcpy. */
3165 static rtx
3166 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
3167 int endp)
3169 if (!validate_arglist (arglist,
3170 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3171 return 0;
3172 /* If return value is ignored, transform mempcpy into memcpy. */
3173 else if (target == const0_rtx)
3175 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3177 if (!fn)
3178 return 0;
3180 return expand_expr (build_function_call_expr (fn, arglist),
3181 target, mode, EXPAND_NORMAL);
3183 else
3185 tree dest = TREE_VALUE (arglist);
3186 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3187 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3188 const char *src_str;
3189 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3190 unsigned int dest_align
3191 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3192 rtx dest_mem, src_mem, len_rtx;
3193 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3195 if (result)
3197 while (TREE_CODE (result) == COMPOUND_EXPR)
3199 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3200 EXPAND_NORMAL);
3201 result = TREE_OPERAND (result, 1);
3203 return expand_expr (result, target, mode, EXPAND_NORMAL);
3206 /* If either SRC or DEST is not a pointer type, don't do this
3207 operation in-line. */
3208 if (dest_align == 0 || src_align == 0)
3209 return 0;
3211 /* If LEN is not constant, call the normal function. */
3212 if (! host_integerp (len, 1))
3213 return 0;
3215 len_rtx = expand_normal (len);
3216 src_str = c_getstr (src);
3218 /* If SRC is a string constant and block move would be done
3219 by pieces, we can avoid loading the string from memory
3220 and only stored the computed constants. */
3221 if (src_str
3222 && GET_CODE (len_rtx) == CONST_INT
3223 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3224 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3225 (void *) src_str, dest_align))
3227 dest_mem = get_memory_rtx (dest, len);
3228 set_mem_align (dest_mem, dest_align);
3229 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3230 builtin_memcpy_read_str,
3231 (void *) src_str, dest_align, endp);
3232 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3233 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3234 return dest_mem;
3237 if (GET_CODE (len_rtx) == CONST_INT
3238 && can_move_by_pieces (INTVAL (len_rtx),
3239 MIN (dest_align, src_align)))
3241 dest_mem = get_memory_rtx (dest, len);
3242 set_mem_align (dest_mem, dest_align);
3243 src_mem = get_memory_rtx (src, len);
3244 set_mem_align (src_mem, src_align);
3245 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3246 MIN (dest_align, src_align), endp);
3247 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3248 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3249 return dest_mem;
3252 return 0;
3256 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3257 if we failed; the caller should emit a normal call. */
3259 static rtx
3260 expand_builtin_memmove (tree arglist, tree type, rtx target,
3261 enum machine_mode mode)
3263 if (!validate_arglist (arglist,
3264 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3265 return 0;
3266 else
3268 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3270 if (result)
3272 while (TREE_CODE (result) == COMPOUND_EXPR)
3274 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3275 EXPAND_NORMAL);
3276 result = TREE_OPERAND (result, 1);
3278 return expand_expr (result, target, mode, EXPAND_NORMAL);
3281 /* Otherwise, call the normal function. */
3282 return 0;
3286 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3287 if we failed the caller should emit a normal call. */
3289 static rtx
3290 expand_builtin_bcopy (tree exp)
3292 tree arglist = TREE_OPERAND (exp, 1);
3293 tree type = TREE_TYPE (exp);
3294 tree src, dest, size, newarglist;
3296 if (!validate_arglist (arglist,
3297 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3298 return NULL_RTX;
3300 src = TREE_VALUE (arglist);
3301 dest = TREE_VALUE (TREE_CHAIN (arglist));
3302 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3304 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3305 memmove(ptr y, ptr x, size_t z). This is done this way
3306 so that if it isn't expanded inline, we fallback to
3307 calling bcopy instead of memmove. */
3309 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3310 newarglist = tree_cons (NULL_TREE, src, newarglist);
3311 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3313 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3316 #ifndef HAVE_movstr
3317 # define HAVE_movstr 0
3318 # define CODE_FOR_movstr CODE_FOR_nothing
3319 #endif
3321 /* Expand into a movstr instruction, if one is available. Return 0 if
3322 we failed, the caller should emit a normal call, otherwise try to
3323 get the result in TARGET, if convenient. If ENDP is 0 return the
3324 destination pointer, if ENDP is 1 return the end pointer ala
3325 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3326 stpcpy. */
3328 static rtx
3329 expand_movstr (tree dest, tree src, rtx target, int endp)
3331 rtx end;
3332 rtx dest_mem;
3333 rtx src_mem;
3334 rtx insn;
3335 const struct insn_data * data;
3337 if (!HAVE_movstr)
3338 return 0;
3340 dest_mem = get_memory_rtx (dest, NULL);
3341 src_mem = get_memory_rtx (src, NULL);
3342 if (!endp)
3344 target = force_reg (Pmode, XEXP (dest_mem, 0));
3345 dest_mem = replace_equiv_address (dest_mem, target);
3346 end = gen_reg_rtx (Pmode);
3348 else
3350 if (target == 0 || target == const0_rtx)
3352 end = gen_reg_rtx (Pmode);
3353 if (target == 0)
3354 target = end;
3356 else
3357 end = target;
3360 data = insn_data + CODE_FOR_movstr;
3362 if (data->operand[0].mode != VOIDmode)
3363 end = gen_lowpart (data->operand[0].mode, end);
3365 insn = data->genfun (end, dest_mem, src_mem);
3367 gcc_assert (insn);
3369 emit_insn (insn);
3371 /* movstr is supposed to set end to the address of the NUL
3372 terminator. If the caller requested a mempcpy-like return value,
3373 adjust it. */
3374 if (endp == 1 && target != const0_rtx)
3376 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3377 emit_move_insn (target, force_operand (tem, NULL_RTX));
3380 return target;
3383 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3384 if we failed the caller should emit a normal call, otherwise try to get
3385 the result in TARGET, if convenient (and in mode MODE if that's
3386 convenient). */
3388 static rtx
3389 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3391 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3393 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3394 if (result)
3396 while (TREE_CODE (result) == COMPOUND_EXPR)
3398 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3399 EXPAND_NORMAL);
3400 result = TREE_OPERAND (result, 1);
3402 return expand_expr (result, target, mode, EXPAND_NORMAL);
3405 return expand_movstr (TREE_VALUE (arglist),
3406 TREE_VALUE (TREE_CHAIN (arglist)),
3407 target, /*endp=*/0);
3409 return 0;
3412 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3413 Return 0 if we failed the caller should emit a normal call,
3414 otherwise try to get the result in TARGET, if convenient (and in
3415 mode MODE if that's convenient). */
3417 static rtx
3418 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3420 tree arglist = TREE_OPERAND (exp, 1);
3421 /* If return value is ignored, transform stpcpy into strcpy. */
3422 if (target == const0_rtx)
3424 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3425 if (!fn)
3426 return 0;
3428 return expand_expr (build_function_call_expr (fn, arglist),
3429 target, mode, EXPAND_NORMAL);
3432 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3433 return 0;
3434 else
3436 tree dst, src, len, lenp1;
3437 tree narglist;
3438 rtx ret;
3440 /* Ensure we get an actual string whose length can be evaluated at
3441 compile-time, not an expression containing a string. This is
3442 because the latter will potentially produce pessimized code
3443 when used to produce the return value. */
3444 src = TREE_VALUE (TREE_CHAIN (arglist));
3445 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3446 return expand_movstr (TREE_VALUE (arglist),
3447 TREE_VALUE (TREE_CHAIN (arglist)),
3448 target, /*endp=*/2);
3450 dst = TREE_VALUE (arglist);
3451 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3452 narglist = build_tree_list (NULL_TREE, lenp1);
3453 narglist = tree_cons (NULL_TREE, src, narglist);
3454 narglist = tree_cons (NULL_TREE, dst, narglist);
3455 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3456 target, mode, /*endp=*/2);
3458 if (ret)
3459 return ret;
3461 if (TREE_CODE (len) == INTEGER_CST)
3463 rtx len_rtx = expand_normal (len);
3465 if (GET_CODE (len_rtx) == CONST_INT)
3467 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3468 arglist, target, mode);
3470 if (ret)
3472 if (! target)
3474 if (mode != VOIDmode)
3475 target = gen_reg_rtx (mode);
3476 else
3477 target = gen_reg_rtx (GET_MODE (ret));
3479 if (GET_MODE (target) != GET_MODE (ret))
3480 ret = gen_lowpart (GET_MODE (target), ret);
3482 ret = plus_constant (ret, INTVAL (len_rtx));
3483 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3484 gcc_assert (ret);
3486 return target;
3491 return expand_movstr (TREE_VALUE (arglist),
3492 TREE_VALUE (TREE_CHAIN (arglist)),
3493 target, /*endp=*/2);
3497 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3498 bytes from constant string DATA + OFFSET and return it as target
3499 constant. */
3501 static rtx
3502 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3503 enum machine_mode mode)
3505 const char *str = (const char *) data;
3507 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3508 return const0_rtx;
3510 return c_readstr (str + offset, mode);
3513 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3514 if we failed the caller should emit a normal call. */
3516 static rtx
3517 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3519 tree fndecl = get_callee_fndecl (exp);
3520 tree arglist = TREE_OPERAND (exp, 1);
3521 if (validate_arglist (arglist,
3522 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3524 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3525 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3526 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3528 if (result)
3530 while (TREE_CODE (result) == COMPOUND_EXPR)
3532 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3533 EXPAND_NORMAL);
3534 result = TREE_OPERAND (result, 1);
3536 return expand_expr (result, target, mode, EXPAND_NORMAL);
3539 /* We must be passed a constant len and src parameter. */
3540 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3541 return 0;
3543 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3545 /* We're required to pad with trailing zeros if the requested
3546 len is greater than strlen(s2)+1. In that case try to
3547 use store_by_pieces, if it fails, punt. */
3548 if (tree_int_cst_lt (slen, len))
3550 tree dest = TREE_VALUE (arglist);
3551 unsigned int dest_align
3552 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3553 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3554 rtx dest_mem;
3556 if (!p || dest_align == 0 || !host_integerp (len, 1)
3557 || !can_store_by_pieces (tree_low_cst (len, 1),
3558 builtin_strncpy_read_str,
3559 (void *) p, dest_align))
3560 return 0;
3562 dest_mem = get_memory_rtx (dest, len);
3563 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3564 builtin_strncpy_read_str,
3565 (void *) p, dest_align, 0);
3566 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3567 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3568 return dest_mem;
3571 return 0;
3574 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3575 bytes from constant string DATA + OFFSET and return it as target
3576 constant. */
3579 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3580 enum machine_mode mode)
3582 const char *c = (const char *) data;
3583 char *p = alloca (GET_MODE_SIZE (mode));
3585 memset (p, *c, GET_MODE_SIZE (mode));
3587 return c_readstr (p, mode);
3590 /* Callback routine for store_by_pieces. Return the RTL of a register
3591 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3592 char value given in the RTL register data. For example, if mode is
3593 4 bytes wide, return the RTL for 0x01010101*data. */
3595 static rtx
3596 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3597 enum machine_mode mode)
3599 rtx target, coeff;
3600 size_t size;
3601 char *p;
3603 size = GET_MODE_SIZE (mode);
3604 if (size == 1)
3605 return (rtx) data;
3607 p = alloca (size);
3608 memset (p, 1, size);
3609 coeff = c_readstr (p, mode);
3611 target = convert_to_mode (mode, (rtx) data, 1);
3612 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3613 return force_reg (mode, target);
3616 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3617 if we failed the caller should emit a normal call, otherwise try to get
3618 the result in TARGET, if convenient (and in mode MODE if that's
3619 convenient). */
3621 static rtx
3622 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3623 tree orig_exp)
3625 if (!validate_arglist (arglist,
3626 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3627 return 0;
3628 else
3630 tree dest = TREE_VALUE (arglist);
3631 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3632 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3633 tree fndecl, fn;
3634 enum built_in_function fcode;
3635 char c;
3636 unsigned int dest_align;
3637 rtx dest_mem, dest_addr, len_rtx;
3639 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3641 /* If DEST is not a pointer type, don't do this
3642 operation in-line. */
3643 if (dest_align == 0)
3644 return 0;
3646 /* If the LEN parameter is zero, return DEST. */
3647 if (integer_zerop (len))
3649 /* Evaluate and ignore VAL in case it has side-effects. */
3650 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3651 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3654 /* Stabilize the arguments in case we fail. */
3655 dest = builtin_save_expr (dest);
3656 val = builtin_save_expr (val);
3657 len = builtin_save_expr (len);
3659 len_rtx = expand_normal (len);
3660 dest_mem = get_memory_rtx (dest, len);
3662 if (TREE_CODE (val) != INTEGER_CST)
3664 rtx val_rtx;
3666 val_rtx = expand_normal (val);
3667 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3668 val_rtx, 0);
3670 /* Assume that we can memset by pieces if we can store the
3671 * the coefficients by pieces (in the required modes).
3672 * We can't pass builtin_memset_gen_str as that emits RTL. */
3673 c = 1;
3674 if (host_integerp (len, 1)
3675 && !(optimize_size && tree_low_cst (len, 1) > 1)
3676 && can_store_by_pieces (tree_low_cst (len, 1),
3677 builtin_memset_read_str, &c, dest_align))
3679 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3680 val_rtx);
3681 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3682 builtin_memset_gen_str, val_rtx, dest_align, 0);
3684 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3685 dest_align))
3686 goto do_libcall;
3688 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3689 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3690 return dest_mem;
3693 if (target_char_cast (val, &c))
3694 goto do_libcall;
3696 if (c)
3698 if (host_integerp (len, 1)
3699 && !(optimize_size && tree_low_cst (len, 1) > 1)
3700 && can_store_by_pieces (tree_low_cst (len, 1),
3701 builtin_memset_read_str, &c, dest_align))
3702 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3703 builtin_memset_read_str, &c, dest_align, 0);
3704 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3705 dest_align))
3706 goto do_libcall;
3708 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3709 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3710 return dest_mem;
3713 set_mem_align (dest_mem, dest_align);
3714 dest_addr = clear_storage (dest_mem, len_rtx,
3715 CALL_EXPR_TAILCALL (orig_exp)
3716 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3718 if (dest_addr == 0)
3720 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3721 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3724 return dest_addr;
3726 do_libcall:
3727 fndecl = get_callee_fndecl (orig_exp);
3728 fcode = DECL_FUNCTION_CODE (fndecl);
3729 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3730 arglist = build_tree_list (NULL_TREE, len);
3731 if (fcode == BUILT_IN_MEMSET)
3732 arglist = tree_cons (NULL_TREE, val, arglist);
3733 arglist = tree_cons (NULL_TREE, dest, arglist);
3734 fn = build_function_call_expr (fndecl, arglist);
3735 if (TREE_CODE (fn) == CALL_EXPR)
3736 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3737 return expand_call (fn, target, target == const0_rtx);
3741 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3742 if we failed the caller should emit a normal call. */
3744 static rtx
3745 expand_builtin_bzero (tree exp)
3747 tree arglist = TREE_OPERAND (exp, 1);
3748 tree dest, size, newarglist;
3750 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3751 return NULL_RTX;
3753 dest = TREE_VALUE (arglist);
3754 size = TREE_VALUE (TREE_CHAIN (arglist));
3756 /* New argument list transforming bzero(ptr x, int y) to
3757 memset(ptr x, int 0, size_t y). This is done this way
3758 so that if it isn't expanded inline, we fallback to
3759 calling bzero instead of memset. */
3761 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3762 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3763 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3765 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3768 /* Expand expression EXP, which is a call to the memcmp built-in function.
3769 ARGLIST is the argument list for this call. Return 0 if we failed and the
3770 caller should emit a normal call, otherwise try to get the result in
3771 TARGET, if convenient (and in mode MODE, if that's convenient). */
3773 static rtx
3774 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3775 enum machine_mode mode)
3777 if (!validate_arglist (arglist,
3778 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3779 return 0;
3780 else
3782 tree result = fold_builtin_memcmp (arglist);
3783 if (result)
3784 return expand_expr (result, target, mode, EXPAND_NORMAL);
3787 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3789 tree arg1 = TREE_VALUE (arglist);
3790 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3791 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3792 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3793 rtx result;
3794 rtx insn;
3796 int arg1_align
3797 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3798 int arg2_align
3799 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3800 enum machine_mode insn_mode;
3802 #ifdef HAVE_cmpmemsi
3803 if (HAVE_cmpmemsi)
3804 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3805 else
3806 #endif
3807 #ifdef HAVE_cmpstrnsi
3808 if (HAVE_cmpstrnsi)
3809 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3810 else
3811 #endif
3812 return 0;
3814 /* If we don't have POINTER_TYPE, call the function. */
3815 if (arg1_align == 0 || arg2_align == 0)
3816 return 0;
3818 /* Make a place to write the result of the instruction. */
3819 result = target;
3820 if (! (result != 0
3821 && REG_P (result) && GET_MODE (result) == insn_mode
3822 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3823 result = gen_reg_rtx (insn_mode);
3825 arg1_rtx = get_memory_rtx (arg1, len);
3826 arg2_rtx = get_memory_rtx (arg2, len);
3827 arg3_rtx = expand_normal (len);
3829 /* Set MEM_SIZE as appropriate. */
3830 if (GET_CODE (arg3_rtx) == CONST_INT)
3832 set_mem_size (arg1_rtx, arg3_rtx);
3833 set_mem_size (arg2_rtx, arg3_rtx);
3836 #ifdef HAVE_cmpmemsi
3837 if (HAVE_cmpmemsi)
3838 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3839 GEN_INT (MIN (arg1_align, arg2_align)));
3840 else
3841 #endif
3842 #ifdef HAVE_cmpstrnsi
3843 if (HAVE_cmpstrnsi)
3844 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3845 GEN_INT (MIN (arg1_align, arg2_align)));
3846 else
3847 #endif
3848 gcc_unreachable ();
3850 if (insn)
3851 emit_insn (insn);
3852 else
3853 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3854 TYPE_MODE (integer_type_node), 3,
3855 XEXP (arg1_rtx, 0), Pmode,
3856 XEXP (arg2_rtx, 0), Pmode,
3857 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3858 TYPE_UNSIGNED (sizetype)),
3859 TYPE_MODE (sizetype));
3861 /* Return the value in the proper mode for this function. */
3862 mode = TYPE_MODE (TREE_TYPE (exp));
3863 if (GET_MODE (result) == mode)
3864 return result;
3865 else if (target != 0)
3867 convert_move (target, result, 0);
3868 return target;
3870 else
3871 return convert_to_mode (mode, result, 0);
3873 #endif
3875 return 0;
3878 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3879 if we failed the caller should emit a normal call, otherwise try to get
3880 the result in TARGET, if convenient. */
3882 static rtx
3883 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3885 tree arglist = TREE_OPERAND (exp, 1);
3887 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3888 return 0;
3889 else
3891 tree result = fold_builtin_strcmp (arglist);
3892 if (result)
3893 return expand_expr (result, target, mode, EXPAND_NORMAL);
3896 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3897 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3898 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3900 rtx arg1_rtx, arg2_rtx;
3901 rtx result, insn = NULL_RTX;
3902 tree fndecl, fn;
3904 tree arg1 = TREE_VALUE (arglist);
3905 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3906 int arg1_align
3907 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3908 int arg2_align
3909 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3911 /* If we don't have POINTER_TYPE, call the function. */
3912 if (arg1_align == 0 || arg2_align == 0)
3913 return 0;
3915 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3916 arg1 = builtin_save_expr (arg1);
3917 arg2 = builtin_save_expr (arg2);
3919 arg1_rtx = get_memory_rtx (arg1, NULL);
3920 arg2_rtx = get_memory_rtx (arg2, NULL);
3922 #ifdef HAVE_cmpstrsi
3923 /* Try to call cmpstrsi. */
3924 if (HAVE_cmpstrsi)
3926 enum machine_mode insn_mode
3927 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3929 /* Make a place to write the result of the instruction. */
3930 result = target;
3931 if (! (result != 0
3932 && REG_P (result) && GET_MODE (result) == insn_mode
3933 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3934 result = gen_reg_rtx (insn_mode);
3936 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3937 GEN_INT (MIN (arg1_align, arg2_align)));
3939 #endif
3940 #ifdef HAVE_cmpstrnsi
3941 /* Try to determine at least one length and call cmpstrnsi. */
3942 if (!insn && HAVE_cmpstrnsi)
3944 tree len;
3945 rtx arg3_rtx;
3947 enum machine_mode insn_mode
3948 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3949 tree len1 = c_strlen (arg1, 1);
3950 tree len2 = c_strlen (arg2, 1);
3952 if (len1)
3953 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3954 if (len2)
3955 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3957 /* If we don't have a constant length for the first, use the length
3958 of the second, if we know it. We don't require a constant for
3959 this case; some cost analysis could be done if both are available
3960 but neither is constant. For now, assume they're equally cheap,
3961 unless one has side effects. If both strings have constant lengths,
3962 use the smaller. */
3964 if (!len1)
3965 len = len2;
3966 else if (!len2)
3967 len = len1;
3968 else if (TREE_SIDE_EFFECTS (len1))
3969 len = len2;
3970 else if (TREE_SIDE_EFFECTS (len2))
3971 len = len1;
3972 else if (TREE_CODE (len1) != INTEGER_CST)
3973 len = len2;
3974 else if (TREE_CODE (len2) != INTEGER_CST)
3975 len = len1;
3976 else if (tree_int_cst_lt (len1, len2))
3977 len = len1;
3978 else
3979 len = len2;
3981 /* If both arguments have side effects, we cannot optimize. */
3982 if (!len || TREE_SIDE_EFFECTS (len))
3983 goto do_libcall;
3985 arg3_rtx = expand_normal (len);
3987 /* Make a place to write the result of the instruction. */
3988 result = target;
3989 if (! (result != 0
3990 && REG_P (result) && GET_MODE (result) == insn_mode
3991 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3992 result = gen_reg_rtx (insn_mode);
3994 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3995 GEN_INT (MIN (arg1_align, arg2_align)));
3997 #endif
3999 if (insn)
4001 emit_insn (insn);
4003 /* Return the value in the proper mode for this function. */
4004 mode = TYPE_MODE (TREE_TYPE (exp));
4005 if (GET_MODE (result) == mode)
4006 return result;
4007 if (target == 0)
4008 return convert_to_mode (mode, result, 0);
4009 convert_move (target, result, 0);
4010 return target;
4013 /* Expand the library call ourselves using a stabilized argument
4014 list to avoid re-evaluating the function's arguments twice. */
4015 #ifdef HAVE_cmpstrnsi
4016 do_libcall:
4017 #endif
4018 arglist = build_tree_list (NULL_TREE, arg2);
4019 arglist = tree_cons (NULL_TREE, arg1, arglist);
4020 fndecl = get_callee_fndecl (exp);
4021 fn = build_function_call_expr (fndecl, arglist);
4022 if (TREE_CODE (fn) == CALL_EXPR)
4023 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4024 return expand_call (fn, target, target == const0_rtx);
4026 #endif
4027 return 0;
4030 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
4031 if we failed the caller should emit a normal call, otherwise try to get
4032 the result in TARGET, if convenient. */
4034 static rtx
4035 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4037 tree arglist = TREE_OPERAND (exp, 1);
4039 if (!validate_arglist (arglist,
4040 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4041 return 0;
4042 else
4044 tree result = fold_builtin_strncmp (arglist);
4045 if (result)
4046 return expand_expr (result, target, mode, EXPAND_NORMAL);
4049 /* If c_strlen can determine an expression for one of the string
4050 lengths, and it doesn't have side effects, then emit cmpstrnsi
4051 using length MIN(strlen(string)+1, arg3). */
4052 #ifdef HAVE_cmpstrnsi
4053 if (HAVE_cmpstrnsi)
4055 tree arg1 = TREE_VALUE (arglist);
4056 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
4057 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4058 tree len, len1, len2;
4059 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4060 rtx result, insn;
4061 tree fndecl, fn;
4063 int arg1_align
4064 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4065 int arg2_align
4066 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4067 enum machine_mode insn_mode
4068 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4070 len1 = c_strlen (arg1, 1);
4071 len2 = c_strlen (arg2, 1);
4073 if (len1)
4074 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4075 if (len2)
4076 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4078 /* If we don't have a constant length for the first, use the length
4079 of the second, if we know it. We don't require a constant for
4080 this case; some cost analysis could be done if both are available
4081 but neither is constant. For now, assume they're equally cheap,
4082 unless one has side effects. If both strings have constant lengths,
4083 use the smaller. */
4085 if (!len1)
4086 len = len2;
4087 else if (!len2)
4088 len = len1;
4089 else if (TREE_SIDE_EFFECTS (len1))
4090 len = len2;
4091 else if (TREE_SIDE_EFFECTS (len2))
4092 len = len1;
4093 else if (TREE_CODE (len1) != INTEGER_CST)
4094 len = len2;
4095 else if (TREE_CODE (len2) != INTEGER_CST)
4096 len = len1;
4097 else if (tree_int_cst_lt (len1, len2))
4098 len = len1;
4099 else
4100 len = len2;
4102 /* If both arguments have side effects, we cannot optimize. */
4103 if (!len || TREE_SIDE_EFFECTS (len))
4104 return 0;
4106 /* The actual new length parameter is MIN(len,arg3). */
4107 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4108 fold_convert (TREE_TYPE (len), arg3));
4110 /* If we don't have POINTER_TYPE, call the function. */
4111 if (arg1_align == 0 || arg2_align == 0)
4112 return 0;
4114 /* Make a place to write the result of the instruction. */
4115 result = target;
4116 if (! (result != 0
4117 && REG_P (result) && GET_MODE (result) == insn_mode
4118 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4119 result = gen_reg_rtx (insn_mode);
4121 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4122 arg1 = builtin_save_expr (arg1);
4123 arg2 = builtin_save_expr (arg2);
4124 len = builtin_save_expr (len);
4126 arg1_rtx = get_memory_rtx (arg1, len);
4127 arg2_rtx = get_memory_rtx (arg2, len);
4128 arg3_rtx = expand_normal (len);
4129 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4130 GEN_INT (MIN (arg1_align, arg2_align)));
4131 if (insn)
4133 emit_insn (insn);
4135 /* Return the value in the proper mode for this function. */
4136 mode = TYPE_MODE (TREE_TYPE (exp));
4137 if (GET_MODE (result) == mode)
4138 return result;
4139 if (target == 0)
4140 return convert_to_mode (mode, result, 0);
4141 convert_move (target, result, 0);
4142 return target;
4145 /* Expand the library call ourselves using a stabilized argument
4146 list to avoid re-evaluating the function's arguments twice. */
4147 arglist = build_tree_list (NULL_TREE, len);
4148 arglist = tree_cons (NULL_TREE, arg2, arglist);
4149 arglist = tree_cons (NULL_TREE, arg1, arglist);
4150 fndecl = get_callee_fndecl (exp);
4151 fn = build_function_call_expr (fndecl, arglist);
4152 if (TREE_CODE (fn) == CALL_EXPR)
4153 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4154 return expand_call (fn, target, target == const0_rtx);
4156 #endif
4157 return 0;
4160 /* Expand expression EXP, which is a call to the strcat builtin.
4161 Return 0 if we failed the caller should emit a normal call,
4162 otherwise try to get the result in TARGET, if convenient. */
4164 static rtx
4165 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
4167 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4168 return 0;
4169 else
4171 tree dst = TREE_VALUE (arglist),
4172 src = TREE_VALUE (TREE_CHAIN (arglist));
4173 const char *p = c_getstr (src);
4175 /* If the string length is zero, return the dst parameter. */
4176 if (p && *p == '\0')
4177 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4179 if (!optimize_size)
4181 /* See if we can store by pieces into (dst + strlen(dst)). */
4182 tree newsrc, newdst,
4183 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4184 rtx insns;
4186 /* Stabilize the argument list. */
4187 newsrc = builtin_save_expr (src);
4188 if (newsrc != src)
4189 arglist = build_tree_list (NULL_TREE, newsrc);
4190 else
4191 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4193 dst = builtin_save_expr (dst);
4195 start_sequence ();
4197 /* Create strlen (dst). */
4198 newdst =
4199 build_function_call_expr (strlen_fn,
4200 build_tree_list (NULL_TREE, dst));
4201 /* Create (dst + (cast) strlen (dst)). */
4202 newdst = fold_convert (TREE_TYPE (dst), newdst);
4203 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4205 newdst = builtin_save_expr (newdst);
4206 arglist = tree_cons (NULL_TREE, newdst, arglist);
4208 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4210 end_sequence (); /* Stop sequence. */
4211 return 0;
4214 /* Output the entire sequence. */
4215 insns = get_insns ();
4216 end_sequence ();
4217 emit_insn (insns);
4219 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4222 return 0;
4226 /* Expand expression EXP, which is a call to the strncat builtin.
4227 Return 0 if we failed the caller should emit a normal call,
4228 otherwise try to get the result in TARGET, if convenient. */
4230 static rtx
4231 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4233 if (validate_arglist (arglist,
4234 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4236 tree result = fold_builtin_strncat (arglist);
4237 if (result)
4238 return expand_expr (result, target, mode, EXPAND_NORMAL);
4240 return 0;
4243 /* Expand expression EXP, which is a call to the strspn builtin.
4244 Return 0 if we failed the caller should emit a normal call,
4245 otherwise try to get the result in TARGET, if convenient. */
4247 static rtx
4248 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4250 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4252 tree result = fold_builtin_strspn (arglist);
4253 if (result)
4254 return expand_expr (result, target, mode, EXPAND_NORMAL);
4256 return 0;
4259 /* Expand expression EXP, which is a call to the strcspn builtin.
4260 Return 0 if we failed the caller should emit a normal call,
4261 otherwise try to get the result in TARGET, if convenient. */
4263 static rtx
4264 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4266 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4268 tree result = fold_builtin_strcspn (arglist);
4269 if (result)
4270 return expand_expr (result, target, mode, EXPAND_NORMAL);
4272 return 0;
4275 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4276 if that's convenient. */
4279 expand_builtin_saveregs (void)
4281 rtx val, seq;
4283 /* Don't do __builtin_saveregs more than once in a function.
4284 Save the result of the first call and reuse it. */
4285 if (saveregs_value != 0)
4286 return saveregs_value;
4288 /* When this function is called, it means that registers must be
4289 saved on entry to this function. So we migrate the call to the
4290 first insn of this function. */
4292 start_sequence ();
4294 /* Do whatever the machine needs done in this case. */
4295 val = targetm.calls.expand_builtin_saveregs ();
4297 seq = get_insns ();
4298 end_sequence ();
4300 saveregs_value = val;
4302 /* Put the insns after the NOTE that starts the function. If this
4303 is inside a start_sequence, make the outer-level insn chain current, so
4304 the code is placed at the start of the function. */
4305 push_topmost_sequence ();
4306 emit_insn_after (seq, entry_of_function ());
4307 pop_topmost_sequence ();
4309 return val;
4312 /* __builtin_args_info (N) returns word N of the arg space info
4313 for the current function. The number and meanings of words
4314 is controlled by the definition of CUMULATIVE_ARGS. */
4316 static rtx
4317 expand_builtin_args_info (tree arglist)
4319 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4320 int *word_ptr = (int *) &current_function_args_info;
4322 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4324 if (arglist != 0)
4326 if (!host_integerp (TREE_VALUE (arglist), 0))
4327 error ("argument of %<__builtin_args_info%> must be constant");
4328 else
4330 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4332 if (wordnum < 0 || wordnum >= nwords)
4333 error ("argument of %<__builtin_args_info%> out of range");
4334 else
4335 return GEN_INT (word_ptr[wordnum]);
4338 else
4339 error ("missing argument in %<__builtin_args_info%>");
4341 return const0_rtx;
4344 /* Expand a call to __builtin_next_arg. */
4346 static rtx
4347 expand_builtin_next_arg (void)
4349 /* Checking arguments is already done in fold_builtin_next_arg
4350 that must be called before this function. */
4351 return expand_binop (Pmode, add_optab,
4352 current_function_internal_arg_pointer,
4353 current_function_arg_offset_rtx,
4354 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4357 /* Make it easier for the backends by protecting the valist argument
4358 from multiple evaluations. */
4360 static tree
4361 stabilize_va_list (tree valist, int needs_lvalue)
4363 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4365 if (TREE_SIDE_EFFECTS (valist))
4366 valist = save_expr (valist);
4368 /* For this case, the backends will be expecting a pointer to
4369 TREE_TYPE (va_list_type_node), but it's possible we've
4370 actually been given an array (an actual va_list_type_node).
4371 So fix it. */
4372 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4374 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4375 valist = build_fold_addr_expr_with_type (valist, p1);
4378 else
4380 tree pt;
4382 if (! needs_lvalue)
4384 if (! TREE_SIDE_EFFECTS (valist))
4385 return valist;
4387 pt = build_pointer_type (va_list_type_node);
4388 valist = fold_build1 (ADDR_EXPR, pt, valist);
4389 TREE_SIDE_EFFECTS (valist) = 1;
4392 if (TREE_SIDE_EFFECTS (valist))
4393 valist = save_expr (valist);
4394 valist = build_fold_indirect_ref (valist);
4397 return valist;
4400 /* The "standard" definition of va_list is void*. */
4402 tree
4403 std_build_builtin_va_list (void)
4405 return ptr_type_node;
4408 /* The "standard" implementation of va_start: just assign `nextarg' to
4409 the variable. */
4411 void
4412 std_expand_builtin_va_start (tree valist, rtx nextarg)
4414 tree t;
4416 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist,
4417 make_tree (ptr_type_node, nextarg));
4418 TREE_SIDE_EFFECTS (t) = 1;
4420 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4423 /* Expand ARGLIST, from a call to __builtin_va_start. */
4425 static rtx
4426 expand_builtin_va_start (tree arglist)
4428 rtx nextarg;
4429 tree chain, valist;
4431 chain = TREE_CHAIN (arglist);
4433 if (!chain)
4435 error ("too few arguments to function %<va_start%>");
4436 return const0_rtx;
4439 if (fold_builtin_next_arg (chain))
4440 return const0_rtx;
4442 nextarg = expand_builtin_next_arg ();
4443 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4445 #ifdef EXPAND_BUILTIN_VA_START
4446 EXPAND_BUILTIN_VA_START (valist, nextarg);
4447 #else
4448 std_expand_builtin_va_start (valist, nextarg);
4449 #endif
4451 return const0_rtx;
4454 /* The "standard" implementation of va_arg: read the value from the
4455 current (padded) address and increment by the (padded) size. */
4457 tree
4458 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4460 tree addr, t, type_size, rounded_size, valist_tmp;
4461 unsigned HOST_WIDE_INT align, boundary;
4462 bool indirect;
4464 #ifdef ARGS_GROW_DOWNWARD
4465 /* All of the alignment and movement below is for args-grow-up machines.
4466 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4467 implement their own specialized gimplify_va_arg_expr routines. */
4468 gcc_unreachable ();
4469 #endif
4471 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4472 if (indirect)
4473 type = build_pointer_type (type);
4475 align = PARM_BOUNDARY / BITS_PER_UNIT;
4476 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4478 /* Hoist the valist value into a temporary for the moment. */
4479 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4481 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4482 requires greater alignment, we must perform dynamic alignment. */
4483 if (boundary > align
4484 && !integer_zerop (TYPE_SIZE (type)))
4486 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4487 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4488 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4489 gimplify_and_add (t, pre_p);
4491 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4492 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4493 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4494 gimplify_and_add (t, pre_p);
4496 else
4497 boundary = align;
4499 /* If the actual alignment is less than the alignment of the type,
4500 adjust the type accordingly so that we don't assume strict alignment
4501 when deferencing the pointer. */
4502 boundary *= BITS_PER_UNIT;
4503 if (boundary < TYPE_ALIGN (type))
4505 type = build_variant_type_copy (type);
4506 TYPE_ALIGN (type) = boundary;
4509 /* Compute the rounded size of the type. */
4510 type_size = size_in_bytes (type);
4511 rounded_size = round_up (type_size, align);
4513 /* Reduce rounded_size so it's sharable with the postqueue. */
4514 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4516 /* Get AP. */
4517 addr = valist_tmp;
4518 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4520 /* Small args are padded downward. */
4521 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4522 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4523 size_binop (MINUS_EXPR, rounded_size, type_size));
4524 t = fold_convert (TREE_TYPE (addr), t);
4525 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4528 /* Compute new value for AP. */
4529 t = fold_convert (TREE_TYPE (valist), rounded_size);
4530 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4531 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist, t);
4532 gimplify_and_add (t, pre_p);
4534 addr = fold_convert (build_pointer_type (type), addr);
4536 if (indirect)
4537 addr = build_va_arg_indirect_ref (addr);
4539 return build_va_arg_indirect_ref (addr);
4542 /* Build an indirect-ref expression over the given TREE, which represents a
4543 piece of a va_arg() expansion. */
4544 tree
4545 build_va_arg_indirect_ref (tree addr)
4547 addr = build_fold_indirect_ref (addr);
4549 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4550 mf_mark (addr);
4552 return addr;
4555 /* Return a dummy expression of type TYPE in order to keep going after an
4556 error. */
4558 static tree
4559 dummy_object (tree type)
4561 tree t = build_int_cst (build_pointer_type (type), 0);
4562 return build1 (INDIRECT_REF, type, t);
4565 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4566 builtin function, but a very special sort of operator. */
4568 enum gimplify_status
4569 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4571 tree promoted_type, want_va_type, have_va_type;
4572 tree valist = TREE_OPERAND (*expr_p, 0);
4573 tree type = TREE_TYPE (*expr_p);
4574 tree t;
4576 /* Verify that valist is of the proper type. */
4577 want_va_type = va_list_type_node;
4578 have_va_type = TREE_TYPE (valist);
4580 if (have_va_type == error_mark_node)
4581 return GS_ERROR;
4583 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4585 /* If va_list is an array type, the argument may have decayed
4586 to a pointer type, e.g. by being passed to another function.
4587 In that case, unwrap both types so that we can compare the
4588 underlying records. */
4589 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4590 || POINTER_TYPE_P (have_va_type))
4592 want_va_type = TREE_TYPE (want_va_type);
4593 have_va_type = TREE_TYPE (have_va_type);
4597 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4599 error ("first argument to %<va_arg%> not of type %<va_list%>");
4600 return GS_ERROR;
4603 /* Generate a diagnostic for requesting data of a type that cannot
4604 be passed through `...' due to type promotion at the call site. */
4605 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4606 != type)
4608 static bool gave_help;
4610 /* Unfortunately, this is merely undefined, rather than a constraint
4611 violation, so we cannot make this an error. If this call is never
4612 executed, the program is still strictly conforming. */
4613 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4614 type, promoted_type);
4615 if (! gave_help)
4617 gave_help = true;
4618 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4619 promoted_type, type);
4622 /* We can, however, treat "undefined" any way we please.
4623 Call abort to encourage the user to fix the program. */
4624 inform ("if this code is reached, the program will abort");
4625 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4626 NULL);
4627 append_to_statement_list (t, pre_p);
4629 /* This is dead code, but go ahead and finish so that the
4630 mode of the result comes out right. */
4631 *expr_p = dummy_object (type);
4632 return GS_ALL_DONE;
4634 else
4636 /* Make it easier for the backends by protecting the valist argument
4637 from multiple evaluations. */
4638 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4640 /* For this case, the backends will be expecting a pointer to
4641 TREE_TYPE (va_list_type_node), but it's possible we've
4642 actually been given an array (an actual va_list_type_node).
4643 So fix it. */
4644 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4646 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4647 valist = build_fold_addr_expr_with_type (valist, p1);
4649 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4651 else
4652 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4654 if (!targetm.gimplify_va_arg_expr)
4655 /* FIXME:Once most targets are converted we should merely
4656 assert this is non-null. */
4657 return GS_ALL_DONE;
4659 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4660 return GS_OK;
4664 /* Expand ARGLIST, from a call to __builtin_va_end. */
4666 static rtx
4667 expand_builtin_va_end (tree arglist)
4669 tree valist = TREE_VALUE (arglist);
4671 /* Evaluate for side effects, if needed. I hate macros that don't
4672 do that. */
4673 if (TREE_SIDE_EFFECTS (valist))
4674 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4676 return const0_rtx;
4679 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4680 builtin rather than just as an assignment in stdarg.h because of the
4681 nastiness of array-type va_list types. */
4683 static rtx
4684 expand_builtin_va_copy (tree arglist)
4686 tree dst, src, t;
4688 dst = TREE_VALUE (arglist);
4689 src = TREE_VALUE (TREE_CHAIN (arglist));
4691 dst = stabilize_va_list (dst, 1);
4692 src = stabilize_va_list (src, 0);
4694 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4696 t = build2 (GIMPLE_MODIFY_STMT, va_list_type_node, dst, src);
4697 TREE_SIDE_EFFECTS (t) = 1;
4698 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4700 else
4702 rtx dstb, srcb, size;
4704 /* Evaluate to pointers. */
4705 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4706 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4707 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4708 VOIDmode, EXPAND_NORMAL);
4710 dstb = convert_memory_address (Pmode, dstb);
4711 srcb = convert_memory_address (Pmode, srcb);
4713 /* "Dereference" to BLKmode memories. */
4714 dstb = gen_rtx_MEM (BLKmode, dstb);
4715 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4716 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4717 srcb = gen_rtx_MEM (BLKmode, srcb);
4718 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4719 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4721 /* Copy. */
4722 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4725 return const0_rtx;
4728 /* Expand a call to one of the builtin functions __builtin_frame_address or
4729 __builtin_return_address. */
4731 static rtx
4732 expand_builtin_frame_address (tree fndecl, tree arglist)
4734 /* The argument must be a nonnegative integer constant.
4735 It counts the number of frames to scan up the stack.
4736 The value is the return address saved in that frame. */
4737 if (arglist == 0)
4738 /* Warning about missing arg was already issued. */
4739 return const0_rtx;
4740 else if (! host_integerp (TREE_VALUE (arglist), 1))
4742 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4743 error ("invalid argument to %<__builtin_frame_address%>");
4744 else
4745 error ("invalid argument to %<__builtin_return_address%>");
4746 return const0_rtx;
4748 else
4750 rtx tem
4751 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4752 tree_low_cst (TREE_VALUE (arglist), 1));
4754 /* Some ports cannot access arbitrary stack frames. */
4755 if (tem == NULL)
4757 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4758 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4759 else
4760 warning (0, "unsupported argument to %<__builtin_return_address%>");
4761 return const0_rtx;
4764 /* For __builtin_frame_address, return what we've got. */
4765 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4766 return tem;
4768 if (!REG_P (tem)
4769 && ! CONSTANT_P (tem))
4770 tem = copy_to_mode_reg (Pmode, tem);
4771 return tem;
4775 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4776 we failed and the caller should emit a normal call, otherwise try to get
4777 the result in TARGET, if convenient. */
4779 static rtx
4780 expand_builtin_alloca (tree arglist, rtx target)
4782 rtx op0;
4783 rtx result;
4785 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4786 should always expand to function calls. These can be intercepted
4787 in libmudflap. */
4788 if (flag_mudflap)
4789 return 0;
4791 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4792 return 0;
4794 /* Compute the argument. */
4795 op0 = expand_normal (TREE_VALUE (arglist));
4797 /* Allocate the desired space. */
4798 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4799 result = convert_memory_address (ptr_mode, result);
4801 return result;
4804 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4805 is the mode to expand with. */
4807 static rtx
4808 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4810 enum machine_mode mode;
4811 tree arg;
4812 rtx op0;
4814 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4815 return 0;
4817 arg = TREE_VALUE (arglist);
4818 mode = TYPE_MODE (TREE_TYPE (arg));
4819 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4821 target = expand_unop (mode, bswap_optab, op0, target, 1);
4823 gcc_assert (target);
4825 return convert_to_mode (mode, target, 0);
4828 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4829 Return 0 if a normal call should be emitted rather than expanding the
4830 function in-line. If convenient, the result should be placed in TARGET.
4831 SUBTARGET may be used as the target for computing one of EXP's operands. */
4833 static rtx
4834 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4835 rtx subtarget, optab op_optab)
4837 rtx op0;
4838 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4839 return 0;
4841 /* Compute the argument. */
4842 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4843 /* Compute op, into TARGET if possible.
4844 Set TARGET to wherever the result comes back. */
4845 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4846 op_optab, op0, target, 1);
4847 gcc_assert (target);
4849 return convert_to_mode (target_mode, target, 0);
4852 /* If the string passed to fputs is a constant and is one character
4853 long, we attempt to transform this call into __builtin_fputc(). */
4855 static rtx
4856 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4858 /* Verify the arguments in the original call. */
4859 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4861 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4862 unlocked, NULL_TREE);
4863 if (result)
4864 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4866 return 0;
4869 /* Expand a call to __builtin_expect. We just return our argument
4870 as the builtin_expect semantic should've been already executed by
4871 tree branch prediction pass. */
4873 static rtx
4874 expand_builtin_expect (tree arglist, rtx target)
4876 tree exp, c;
4878 if (arglist == NULL_TREE
4879 || TREE_CHAIN (arglist) == NULL_TREE)
4880 return const0_rtx;
4881 exp = TREE_VALUE (arglist);
4882 c = TREE_VALUE (TREE_CHAIN (arglist));
4884 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4885 /* When guessing was done, the hints should be already stripped away. */
4886 gcc_assert (!flag_guess_branch_prob);
4887 return target;
4890 void
4891 expand_builtin_trap (void)
4893 #ifdef HAVE_trap
4894 if (HAVE_trap)
4895 emit_insn (gen_trap ());
4896 else
4897 #endif
4898 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4899 emit_barrier ();
4902 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4903 Return 0 if a normal call should be emitted rather than expanding
4904 the function inline. If convenient, the result should be placed
4905 in TARGET. SUBTARGET may be used as the target for computing
4906 the operand. */
4908 static rtx
4909 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4911 enum machine_mode mode;
4912 tree arg;
4913 rtx op0;
4915 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4916 return 0;
4918 arg = TREE_VALUE (arglist);
4919 mode = TYPE_MODE (TREE_TYPE (arg));
4920 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4921 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4924 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4925 Return NULL is a normal call should be emitted rather than expanding the
4926 function inline. If convenient, the result should be placed in TARGET.
4927 SUBTARGET may be used as the target for computing the operand. */
4929 static rtx
4930 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4932 rtx op0, op1;
4933 tree arg;
4935 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4936 return 0;
4938 arg = TREE_VALUE (arglist);
4939 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4941 arg = TREE_VALUE (TREE_CHAIN (arglist));
4942 op1 = expand_normal (arg);
4944 return expand_copysign (op0, op1, target);
4947 /* Create a new constant string literal and return a char* pointer to it.
4948 The STRING_CST value is the LEN characters at STR. */
4949 tree
4950 build_string_literal (int len, const char *str)
4952 tree t, elem, index, type;
4954 t = build_string (len, str);
4955 elem = build_type_variant (char_type_node, 1, 0);
4956 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4957 type = build_array_type (elem, index);
4958 TREE_TYPE (t) = type;
4959 TREE_CONSTANT (t) = 1;
4960 TREE_INVARIANT (t) = 1;
4961 TREE_READONLY (t) = 1;
4962 TREE_STATIC (t) = 1;
4964 type = build_pointer_type (type);
4965 t = build1 (ADDR_EXPR, type, t);
4967 type = build_pointer_type (elem);
4968 t = build1 (NOP_EXPR, type, t);
4969 return t;
4972 /* Expand EXP, a call to printf or printf_unlocked.
4973 Return 0 if a normal call should be emitted rather than transforming
4974 the function inline. If convenient, the result should be placed in
4975 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4976 call. */
4977 static rtx
4978 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4979 bool unlocked)
4981 tree arglist = TREE_OPERAND (exp, 1);
4982 /* If we're using an unlocked function, assume the other unlocked
4983 functions exist explicitly. */
4984 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4985 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4986 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4987 : implicit_built_in_decls[BUILT_IN_PUTS];
4988 const char *fmt_str;
4989 tree fn, fmt, arg;
4991 /* If the return value is used, don't do the transformation. */
4992 if (target != const0_rtx)
4993 return 0;
4995 /* Verify the required arguments in the original call. */
4996 if (! arglist)
4997 return 0;
4998 fmt = TREE_VALUE (arglist);
4999 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5000 return 0;
5001 arglist = TREE_CHAIN (arglist);
5003 /* Check whether the format is a literal string constant. */
5004 fmt_str = c_getstr (fmt);
5005 if (fmt_str == NULL)
5006 return 0;
5008 if (!init_target_chars())
5009 return 0;
5011 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5012 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5014 if (! arglist
5015 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5016 || TREE_CHAIN (arglist))
5017 return 0;
5018 fn = fn_puts;
5020 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5021 else if (strcmp (fmt_str, target_percent_c) == 0)
5023 if (! arglist
5024 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5025 || TREE_CHAIN (arglist))
5026 return 0;
5027 fn = fn_putchar;
5029 else
5031 /* We can't handle anything else with % args or %% ... yet. */
5032 if (strchr (fmt_str, target_percent))
5033 return 0;
5035 if (arglist)
5036 return 0;
5038 /* If the format specifier was "", printf does nothing. */
5039 if (fmt_str[0] == '\0')
5040 return const0_rtx;
5041 /* If the format specifier has length of 1, call putchar. */
5042 if (fmt_str[1] == '\0')
5044 /* Given printf("c"), (where c is any one character,)
5045 convert "c"[0] to an int and pass that to the replacement
5046 function. */
5047 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5048 arglist = build_tree_list (NULL_TREE, arg);
5049 fn = fn_putchar;
5051 else
5053 /* If the format specifier was "string\n", call puts("string"). */
5054 size_t len = strlen (fmt_str);
5055 if ((unsigned char)fmt_str[len - 1] == target_newline)
5057 /* Create a NUL-terminated string that's one char shorter
5058 than the original, stripping off the trailing '\n'. */
5059 char *newstr = alloca (len);
5060 memcpy (newstr, fmt_str, len - 1);
5061 newstr[len - 1] = 0;
5063 arg = build_string_literal (len, newstr);
5064 arglist = build_tree_list (NULL_TREE, arg);
5065 fn = fn_puts;
5067 else
5068 /* We'd like to arrange to call fputs(string,stdout) here,
5069 but we need stdout and don't have a way to get it yet. */
5070 return 0;
5074 if (!fn)
5075 return 0;
5076 fn = build_function_call_expr (fn, arglist);
5077 if (TREE_CODE (fn) == CALL_EXPR)
5078 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5079 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5082 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5083 Return 0 if a normal call should be emitted rather than transforming
5084 the function inline. If convenient, the result should be placed in
5085 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5086 call. */
5087 static rtx
5088 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5089 bool unlocked)
5091 tree arglist = TREE_OPERAND (exp, 1);
5092 /* If we're using an unlocked function, assume the other unlocked
5093 functions exist explicitly. */
5094 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5095 : implicit_built_in_decls[BUILT_IN_FPUTC];
5096 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5097 : implicit_built_in_decls[BUILT_IN_FPUTS];
5098 const char *fmt_str;
5099 tree fn, fmt, fp, arg;
5101 /* If the return value is used, don't do the transformation. */
5102 if (target != const0_rtx)
5103 return 0;
5105 /* Verify the required arguments in the original call. */
5106 if (! arglist)
5107 return 0;
5108 fp = TREE_VALUE (arglist);
5109 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5110 return 0;
5111 arglist = TREE_CHAIN (arglist);
5112 if (! arglist)
5113 return 0;
5114 fmt = TREE_VALUE (arglist);
5115 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5116 return 0;
5117 arglist = TREE_CHAIN (arglist);
5119 /* Check whether the format is a literal string constant. */
5120 fmt_str = c_getstr (fmt);
5121 if (fmt_str == NULL)
5122 return 0;
5124 if (!init_target_chars())
5125 return 0;
5127 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5128 if (strcmp (fmt_str, target_percent_s) == 0)
5130 if (! arglist
5131 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5132 || TREE_CHAIN (arglist))
5133 return 0;
5134 arg = TREE_VALUE (arglist);
5135 arglist = build_tree_list (NULL_TREE, fp);
5136 arglist = tree_cons (NULL_TREE, arg, arglist);
5137 fn = fn_fputs;
5139 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5140 else if (strcmp (fmt_str, target_percent_c) == 0)
5142 if (! arglist
5143 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5144 || TREE_CHAIN (arglist))
5145 return 0;
5146 arg = TREE_VALUE (arglist);
5147 arglist = build_tree_list (NULL_TREE, fp);
5148 arglist = tree_cons (NULL_TREE, arg, arglist);
5149 fn = fn_fputc;
5151 else
5153 /* We can't handle anything else with % args or %% ... yet. */
5154 if (strchr (fmt_str, target_percent))
5155 return 0;
5157 if (arglist)
5158 return 0;
5160 /* If the format specifier was "", fprintf does nothing. */
5161 if (fmt_str[0] == '\0')
5163 /* Evaluate and ignore FILE* argument for side-effects. */
5164 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5165 return const0_rtx;
5168 /* When "string" doesn't contain %, replace all cases of
5169 fprintf(stream,string) with fputs(string,stream). The fputs
5170 builtin will take care of special cases like length == 1. */
5171 arglist = build_tree_list (NULL_TREE, fp);
5172 arglist = tree_cons (NULL_TREE, fmt, arglist);
5173 fn = fn_fputs;
5176 if (!fn)
5177 return 0;
5178 fn = build_function_call_expr (fn, arglist);
5179 if (TREE_CODE (fn) == CALL_EXPR)
5180 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5181 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5184 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5185 a normal call should be emitted rather than expanding the function
5186 inline. If convenient, the result should be placed in TARGET with
5187 mode MODE. */
5189 static rtx
5190 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5192 tree orig_arglist, dest, fmt;
5193 const char *fmt_str;
5195 orig_arglist = arglist;
5197 /* Verify the required arguments in the original call. */
5198 if (! arglist)
5199 return 0;
5200 dest = TREE_VALUE (arglist);
5201 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5202 return 0;
5203 arglist = TREE_CHAIN (arglist);
5204 if (! arglist)
5205 return 0;
5206 fmt = TREE_VALUE (arglist);
5207 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5208 return 0;
5209 arglist = TREE_CHAIN (arglist);
5211 /* Check whether the format is a literal string constant. */
5212 fmt_str = c_getstr (fmt);
5213 if (fmt_str == NULL)
5214 return 0;
5216 if (!init_target_chars())
5217 return 0;
5219 /* If the format doesn't contain % args or %%, use strcpy. */
5220 if (strchr (fmt_str, target_percent) == 0)
5222 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5223 tree exp;
5225 if (arglist || ! fn)
5226 return 0;
5227 expand_expr (build_function_call_expr (fn, orig_arglist),
5228 const0_rtx, VOIDmode, EXPAND_NORMAL);
5229 if (target == const0_rtx)
5230 return const0_rtx;
5231 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5232 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5234 /* If the format is "%s", use strcpy if the result isn't used. */
5235 else if (strcmp (fmt_str, target_percent_s) == 0)
5237 tree fn, arg, len;
5238 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5240 if (! fn)
5241 return 0;
5243 if (! arglist || TREE_CHAIN (arglist))
5244 return 0;
5245 arg = TREE_VALUE (arglist);
5246 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5247 return 0;
5249 if (target != const0_rtx)
5251 len = c_strlen (arg, 1);
5252 if (! len || TREE_CODE (len) != INTEGER_CST)
5253 return 0;
5255 else
5256 len = NULL_TREE;
5258 arglist = build_tree_list (NULL_TREE, arg);
5259 arglist = tree_cons (NULL_TREE, dest, arglist);
5260 expand_expr (build_function_call_expr (fn, arglist),
5261 const0_rtx, VOIDmode, EXPAND_NORMAL);
5263 if (target == const0_rtx)
5264 return const0_rtx;
5265 return expand_expr (len, target, mode, EXPAND_NORMAL);
5268 return 0;
5271 /* Expand a call to either the entry or exit function profiler. */
5273 static rtx
5274 expand_builtin_profile_func (bool exitp)
5276 rtx this, which;
5278 this = DECL_RTL (current_function_decl);
5279 gcc_assert (MEM_P (this));
5280 this = XEXP (this, 0);
5282 if (exitp)
5283 which = profile_function_exit_libfunc;
5284 else
5285 which = profile_function_entry_libfunc;
5287 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5288 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5290 Pmode);
5292 return const0_rtx;
5295 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5297 static rtx
5298 round_trampoline_addr (rtx tramp)
5300 rtx temp, addend, mask;
5302 /* If we don't need too much alignment, we'll have been guaranteed
5303 proper alignment by get_trampoline_type. */
5304 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5305 return tramp;
5307 /* Round address up to desired boundary. */
5308 temp = gen_reg_rtx (Pmode);
5309 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5310 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5312 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5313 temp, 0, OPTAB_LIB_WIDEN);
5314 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5315 temp, 0, OPTAB_LIB_WIDEN);
5317 return tramp;
5320 static rtx
5321 expand_builtin_init_trampoline (tree arglist)
5323 tree t_tramp, t_func, t_chain;
5324 rtx r_tramp, r_func, r_chain;
5325 #ifdef TRAMPOLINE_TEMPLATE
5326 rtx blktramp;
5327 #endif
5329 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5330 POINTER_TYPE, VOID_TYPE))
5331 return NULL_RTX;
5333 t_tramp = TREE_VALUE (arglist);
5334 arglist = TREE_CHAIN (arglist);
5335 t_func = TREE_VALUE (arglist);
5336 arglist = TREE_CHAIN (arglist);
5337 t_chain = TREE_VALUE (arglist);
5339 r_tramp = expand_normal (t_tramp);
5340 r_func = expand_normal (t_func);
5341 r_chain = expand_normal (t_chain);
5343 /* Generate insns to initialize the trampoline. */
5344 r_tramp = round_trampoline_addr (r_tramp);
5345 #ifdef TRAMPOLINE_TEMPLATE
5346 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5347 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5348 emit_block_move (blktramp, assemble_trampoline_template (),
5349 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5350 #endif
5351 trampolines_created = 1;
5352 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5354 return const0_rtx;
5357 static rtx
5358 expand_builtin_adjust_trampoline (tree arglist)
5360 rtx tramp;
5362 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5363 return NULL_RTX;
5365 tramp = expand_normal (TREE_VALUE (arglist));
5366 tramp = round_trampoline_addr (tramp);
5367 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5368 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5369 #endif
5371 return tramp;
5374 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5375 Return NULL_RTX if a normal call should be emitted rather than expanding
5376 the function in-line. EXP is the expression that is a call to the builtin
5377 function; if convenient, the result should be placed in TARGET. */
5379 static rtx
5380 expand_builtin_signbit (tree exp, rtx target)
5382 const struct real_format *fmt;
5383 enum machine_mode fmode, imode, rmode;
5384 HOST_WIDE_INT hi, lo;
5385 tree arg, arglist;
5386 int word, bitpos;
5387 rtx temp;
5389 arglist = TREE_OPERAND (exp, 1);
5390 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5391 return 0;
5393 arg = TREE_VALUE (arglist);
5394 fmode = TYPE_MODE (TREE_TYPE (arg));
5395 rmode = TYPE_MODE (TREE_TYPE (exp));
5396 fmt = REAL_MODE_FORMAT (fmode);
5398 /* For floating point formats without a sign bit, implement signbit
5399 as "ARG < 0.0". */
5400 bitpos = fmt->signbit_ro;
5401 if (bitpos < 0)
5403 /* But we can't do this if the format supports signed zero. */
5404 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5405 return 0;
5407 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5408 build_real (TREE_TYPE (arg), dconst0));
5409 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5412 temp = expand_normal (arg);
5413 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5415 imode = int_mode_for_mode (fmode);
5416 if (imode == BLKmode)
5417 return 0;
5418 temp = gen_lowpart (imode, temp);
5420 else
5422 imode = word_mode;
5423 /* Handle targets with different FP word orders. */
5424 if (FLOAT_WORDS_BIG_ENDIAN)
5425 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5426 else
5427 word = bitpos / BITS_PER_WORD;
5428 temp = operand_subword_force (temp, word, fmode);
5429 bitpos = bitpos % BITS_PER_WORD;
5432 /* Force the intermediate word_mode (or narrower) result into a
5433 register. This avoids attempting to create paradoxical SUBREGs
5434 of floating point modes below. */
5435 temp = force_reg (imode, temp);
5437 /* If the bitpos is within the "result mode" lowpart, the operation
5438 can be implement with a single bitwise AND. Otherwise, we need
5439 a right shift and an AND. */
5441 if (bitpos < GET_MODE_BITSIZE (rmode))
5443 if (bitpos < HOST_BITS_PER_WIDE_INT)
5445 hi = 0;
5446 lo = (HOST_WIDE_INT) 1 << bitpos;
5448 else
5450 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5451 lo = 0;
5454 if (imode != rmode)
5455 temp = gen_lowpart (rmode, temp);
5456 temp = expand_binop (rmode, and_optab, temp,
5457 immed_double_const (lo, hi, rmode),
5458 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5460 else
5462 /* Perform a logical right shift to place the signbit in the least
5463 significant bit, then truncate the result to the desired mode
5464 and mask just this bit. */
5465 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5466 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5467 temp = gen_lowpart (rmode, temp);
5468 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5469 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5472 return temp;
5475 /* Expand fork or exec calls. TARGET is the desired target of the
5476 call. ARGLIST is the list of arguments of the call. FN is the
5477 identificator of the actual function. IGNORE is nonzero if the
5478 value is to be ignored. */
5480 static rtx
5481 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5483 tree id, decl;
5484 tree call;
5486 /* If we are not profiling, just call the function. */
5487 if (!profile_arc_flag)
5488 return NULL_RTX;
5490 /* Otherwise call the wrapper. This should be equivalent for the rest of
5491 compiler, so the code does not diverge, and the wrapper may run the
5492 code necessary for keeping the profiling sane. */
5494 switch (DECL_FUNCTION_CODE (fn))
5496 case BUILT_IN_FORK:
5497 id = get_identifier ("__gcov_fork");
5498 break;
5500 case BUILT_IN_EXECL:
5501 id = get_identifier ("__gcov_execl");
5502 break;
5504 case BUILT_IN_EXECV:
5505 id = get_identifier ("__gcov_execv");
5506 break;
5508 case BUILT_IN_EXECLP:
5509 id = get_identifier ("__gcov_execlp");
5510 break;
5512 case BUILT_IN_EXECLE:
5513 id = get_identifier ("__gcov_execle");
5514 break;
5516 case BUILT_IN_EXECVP:
5517 id = get_identifier ("__gcov_execvp");
5518 break;
5520 case BUILT_IN_EXECVE:
5521 id = get_identifier ("__gcov_execve");
5522 break;
5524 default:
5525 gcc_unreachable ();
5528 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5529 DECL_EXTERNAL (decl) = 1;
5530 TREE_PUBLIC (decl) = 1;
5531 DECL_ARTIFICIAL (decl) = 1;
5532 TREE_NOTHROW (decl) = 1;
5533 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5534 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5535 call = build_function_call_expr (decl, arglist);
5537 return expand_call (call, target, ignore);
5541 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5542 the pointer in these functions is void*, the tree optimizers may remove
5543 casts. The mode computed in expand_builtin isn't reliable either, due
5544 to __sync_bool_compare_and_swap.
5546 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5547 group of builtins. This gives us log2 of the mode size. */
5549 static inline enum machine_mode
5550 get_builtin_sync_mode (int fcode_diff)
5552 /* The size is not negotiable, so ask not to get BLKmode in return
5553 if the target indicates that a smaller size would be better. */
5554 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5557 /* Expand the memory expression LOC and return the appropriate memory operand
5558 for the builtin_sync operations. */
5560 static rtx
5561 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5563 rtx addr, mem;
5565 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5567 /* Note that we explicitly do not want any alias information for this
5568 memory, so that we kill all other live memories. Otherwise we don't
5569 satisfy the full barrier semantics of the intrinsic. */
5570 mem = validize_mem (gen_rtx_MEM (mode, addr));
5572 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5573 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5574 MEM_VOLATILE_P (mem) = 1;
5576 return mem;
5579 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5580 ARGLIST is the operands list to the function. CODE is the rtx code
5581 that corresponds to the arithmetic or logical operation from the name;
5582 an exception here is that NOT actually means NAND. TARGET is an optional
5583 place for us to store the results; AFTER is true if this is the
5584 fetch_and_xxx form. IGNORE is true if we don't actually care about
5585 the result of the operation at all. */
5587 static rtx
5588 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5589 enum rtx_code code, bool after,
5590 rtx target, bool ignore)
5592 rtx val, mem;
5594 /* Expand the operands. */
5595 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5597 arglist = TREE_CHAIN (arglist);
5598 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5599 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5600 val = convert_to_mode (mode, val, 1);
5602 if (ignore)
5603 return expand_sync_operation (mem, val, code);
5604 else
5605 return expand_sync_fetch_operation (mem, val, code, after, target);
5608 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5609 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5610 true if this is the boolean form. TARGET is a place for us to store the
5611 results; this is NOT optional if IS_BOOL is true. */
5613 static rtx
5614 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5615 bool is_bool, rtx target)
5617 rtx old_val, new_val, mem;
5619 /* Expand the operands. */
5620 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5622 arglist = TREE_CHAIN (arglist);
5623 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5624 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5625 old_val = convert_to_mode (mode, old_val, 1);
5627 arglist = TREE_CHAIN (arglist);
5628 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5629 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5630 new_val = convert_to_mode (mode, new_val, 1);
5632 if (is_bool)
5633 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5634 else
5635 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5638 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5639 general form is actually an atomic exchange, and some targets only
5640 support a reduced form with the second argument being a constant 1.
5641 ARGLIST is the operands list to the function; TARGET is an optional
5642 place for us to store the results. */
5644 static rtx
5645 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5646 rtx target)
5648 rtx val, mem;
5650 /* Expand the operands. */
5651 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5653 arglist = TREE_CHAIN (arglist);
5654 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5655 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5656 val = convert_to_mode (mode, val, 1);
5658 return expand_sync_lock_test_and_set (mem, val, target);
5661 /* Expand the __sync_synchronize intrinsic. */
5663 static void
5664 expand_builtin_synchronize (void)
5666 tree x;
5668 #ifdef HAVE_memory_barrier
5669 if (HAVE_memory_barrier)
5671 emit_insn (gen_memory_barrier ());
5672 return;
5674 #endif
5676 /* If no explicit memory barrier instruction is available, create an
5677 empty asm stmt with a memory clobber. */
5678 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5679 tree_cons (NULL, build_string (6, "memory"), NULL));
5680 ASM_VOLATILE_P (x) = 1;
5681 expand_asm_expr (x);
5684 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5685 to the function. */
5687 static void
5688 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5690 enum insn_code icode;
5691 rtx mem, insn;
5692 rtx val = const0_rtx;
5694 /* Expand the operands. */
5695 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5697 /* If there is an explicit operation in the md file, use it. */
5698 icode = sync_lock_release[mode];
5699 if (icode != CODE_FOR_nothing)
5701 if (!insn_data[icode].operand[1].predicate (val, mode))
5702 val = force_reg (mode, val);
5704 insn = GEN_FCN (icode) (mem, val);
5705 if (insn)
5707 emit_insn (insn);
5708 return;
5712 /* Otherwise we can implement this operation by emitting a barrier
5713 followed by a store of zero. */
5714 expand_builtin_synchronize ();
5715 emit_move_insn (mem, val);
5718 /* Expand an expression EXP that calls a built-in function,
5719 with result going to TARGET if that's convenient
5720 (and in mode MODE if that's convenient).
5721 SUBTARGET may be used as the target for computing one of EXP's operands.
5722 IGNORE is nonzero if the value is to be ignored. */
5725 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5726 int ignore)
5728 tree fndecl = get_callee_fndecl (exp);
5729 tree arglist = TREE_OPERAND (exp, 1);
5730 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5731 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5733 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5734 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5736 /* When not optimizing, generate calls to library functions for a certain
5737 set of builtins. */
5738 if (!optimize
5739 && !called_as_built_in (fndecl)
5740 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5741 && fcode != BUILT_IN_ALLOCA)
5742 return expand_call (exp, target, ignore);
5744 /* The built-in function expanders test for target == const0_rtx
5745 to determine whether the function's result will be ignored. */
5746 if (ignore)
5747 target = const0_rtx;
5749 /* If the result of a pure or const built-in function is ignored, and
5750 none of its arguments are volatile, we can avoid expanding the
5751 built-in call and just evaluate the arguments for side-effects. */
5752 if (target == const0_rtx
5753 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5755 bool volatilep = false;
5756 tree arg;
5758 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5759 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5761 volatilep = true;
5762 break;
5765 if (! volatilep)
5767 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5768 expand_expr (TREE_VALUE (arg), const0_rtx,
5769 VOIDmode, EXPAND_NORMAL);
5770 return const0_rtx;
5774 switch (fcode)
5776 CASE_FLT_FN (BUILT_IN_FABS):
5777 target = expand_builtin_fabs (arglist, target, subtarget);
5778 if (target)
5779 return target;
5780 break;
5782 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5783 target = expand_builtin_copysign (arglist, target, subtarget);
5784 if (target)
5785 return target;
5786 break;
5788 /* Just do a normal library call if we were unable to fold
5789 the values. */
5790 CASE_FLT_FN (BUILT_IN_CABS):
5791 break;
5793 CASE_FLT_FN (BUILT_IN_EXP):
5794 CASE_FLT_FN (BUILT_IN_EXP10):
5795 CASE_FLT_FN (BUILT_IN_POW10):
5796 CASE_FLT_FN (BUILT_IN_EXP2):
5797 CASE_FLT_FN (BUILT_IN_EXPM1):
5798 CASE_FLT_FN (BUILT_IN_LOGB):
5799 CASE_FLT_FN (BUILT_IN_ILOGB):
5800 CASE_FLT_FN (BUILT_IN_LOG):
5801 CASE_FLT_FN (BUILT_IN_LOG10):
5802 CASE_FLT_FN (BUILT_IN_LOG2):
5803 CASE_FLT_FN (BUILT_IN_LOG1P):
5804 CASE_FLT_FN (BUILT_IN_TAN):
5805 CASE_FLT_FN (BUILT_IN_ASIN):
5806 CASE_FLT_FN (BUILT_IN_ACOS):
5807 CASE_FLT_FN (BUILT_IN_ATAN):
5808 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5809 because of possible accuracy problems. */
5810 if (! flag_unsafe_math_optimizations)
5811 break;
5812 CASE_FLT_FN (BUILT_IN_SQRT):
5813 CASE_FLT_FN (BUILT_IN_FLOOR):
5814 CASE_FLT_FN (BUILT_IN_CEIL):
5815 CASE_FLT_FN (BUILT_IN_TRUNC):
5816 CASE_FLT_FN (BUILT_IN_ROUND):
5817 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5818 CASE_FLT_FN (BUILT_IN_RINT):
5819 target = expand_builtin_mathfn (exp, target, subtarget);
5820 if (target)
5821 return target;
5822 break;
5824 CASE_FLT_FN (BUILT_IN_LCEIL):
5825 CASE_FLT_FN (BUILT_IN_LLCEIL):
5826 CASE_FLT_FN (BUILT_IN_LFLOOR):
5827 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5828 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5829 if (target)
5830 return target;
5831 break;
5833 CASE_FLT_FN (BUILT_IN_LRINT):
5834 CASE_FLT_FN (BUILT_IN_LLRINT):
5835 CASE_FLT_FN (BUILT_IN_LROUND):
5836 CASE_FLT_FN (BUILT_IN_LLROUND):
5837 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5838 if (target)
5839 return target;
5840 break;
5842 CASE_FLT_FN (BUILT_IN_POW):
5843 target = expand_builtin_pow (exp, target, subtarget);
5844 if (target)
5845 return target;
5846 break;
5848 CASE_FLT_FN (BUILT_IN_POWI):
5849 target = expand_builtin_powi (exp, target, subtarget);
5850 if (target)
5851 return target;
5852 break;
5854 CASE_FLT_FN (BUILT_IN_ATAN2):
5855 CASE_FLT_FN (BUILT_IN_LDEXP):
5856 if (! flag_unsafe_math_optimizations)
5857 break;
5859 CASE_FLT_FN (BUILT_IN_FMOD):
5860 CASE_FLT_FN (BUILT_IN_REMAINDER):
5861 CASE_FLT_FN (BUILT_IN_DREM):
5862 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5863 if (target)
5864 return target;
5865 break;
5867 CASE_FLT_FN (BUILT_IN_CEXPI):
5868 target = expand_builtin_cexpi (exp, target, subtarget);
5869 gcc_assert (target);
5870 return target;
5872 CASE_FLT_FN (BUILT_IN_SIN):
5873 CASE_FLT_FN (BUILT_IN_COS):
5874 if (! flag_unsafe_math_optimizations)
5875 break;
5876 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5877 if (target)
5878 return target;
5879 break;
5881 CASE_FLT_FN (BUILT_IN_SINCOS):
5882 if (! flag_unsafe_math_optimizations)
5883 break;
5884 target = expand_builtin_sincos (exp);
5885 if (target)
5886 return target;
5887 break;
5889 case BUILT_IN_APPLY_ARGS:
5890 return expand_builtin_apply_args ();
5892 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5893 FUNCTION with a copy of the parameters described by
5894 ARGUMENTS, and ARGSIZE. It returns a block of memory
5895 allocated on the stack into which is stored all the registers
5896 that might possibly be used for returning the result of a
5897 function. ARGUMENTS is the value returned by
5898 __builtin_apply_args. ARGSIZE is the number of bytes of
5899 arguments that must be copied. ??? How should this value be
5900 computed? We'll also need a safe worst case value for varargs
5901 functions. */
5902 case BUILT_IN_APPLY:
5903 if (!validate_arglist (arglist, POINTER_TYPE,
5904 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5905 && !validate_arglist (arglist, REFERENCE_TYPE,
5906 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5907 return const0_rtx;
5908 else
5910 int i;
5911 tree t;
5912 rtx ops[3];
5914 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5915 ops[i] = expand_normal (TREE_VALUE (t));
5917 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5920 /* __builtin_return (RESULT) causes the function to return the
5921 value described by RESULT. RESULT is address of the block of
5922 memory returned by __builtin_apply. */
5923 case BUILT_IN_RETURN:
5924 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5925 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5926 return const0_rtx;
5928 case BUILT_IN_SAVEREGS:
5929 return expand_builtin_saveregs ();
5931 case BUILT_IN_ARGS_INFO:
5932 return expand_builtin_args_info (arglist);
5934 /* Return the address of the first anonymous stack arg. */
5935 case BUILT_IN_NEXT_ARG:
5936 if (fold_builtin_next_arg (arglist))
5937 return const0_rtx;
5938 return expand_builtin_next_arg ();
5940 case BUILT_IN_CLASSIFY_TYPE:
5941 return expand_builtin_classify_type (arglist);
5943 case BUILT_IN_CONSTANT_P:
5944 return const0_rtx;
5946 case BUILT_IN_FRAME_ADDRESS:
5947 case BUILT_IN_RETURN_ADDRESS:
5948 return expand_builtin_frame_address (fndecl, arglist);
5950 /* Returns the address of the area where the structure is returned.
5951 0 otherwise. */
5952 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5953 if (arglist != 0
5954 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5955 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5956 return const0_rtx;
5957 else
5958 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5960 case BUILT_IN_ALLOCA:
5961 target = expand_builtin_alloca (arglist, target);
5962 if (target)
5963 return target;
5964 break;
5966 case BUILT_IN_STACK_SAVE:
5967 return expand_stack_save ();
5969 case BUILT_IN_STACK_RESTORE:
5970 expand_stack_restore (TREE_VALUE (arglist));
5971 return const0_rtx;
5973 case BUILT_IN_BSWAP32:
5974 case BUILT_IN_BSWAP64:
5975 target = expand_builtin_bswap (arglist, target, subtarget);
5977 if (target)
5978 return target;
5979 break;
5981 CASE_INT_FN (BUILT_IN_FFS):
5982 case BUILT_IN_FFSIMAX:
5983 target = expand_builtin_unop (target_mode, arglist, target,
5984 subtarget, ffs_optab);
5985 if (target)
5986 return target;
5987 break;
5989 CASE_INT_FN (BUILT_IN_CLZ):
5990 case BUILT_IN_CLZIMAX:
5991 target = expand_builtin_unop (target_mode, arglist, target,
5992 subtarget, clz_optab);
5993 if (target)
5994 return target;
5995 break;
5997 CASE_INT_FN (BUILT_IN_CTZ):
5998 case BUILT_IN_CTZIMAX:
5999 target = expand_builtin_unop (target_mode, arglist, target,
6000 subtarget, ctz_optab);
6001 if (target)
6002 return target;
6003 break;
6005 CASE_INT_FN (BUILT_IN_POPCOUNT):
6006 case BUILT_IN_POPCOUNTIMAX:
6007 target = expand_builtin_unop (target_mode, arglist, target,
6008 subtarget, popcount_optab);
6009 if (target)
6010 return target;
6011 break;
6013 CASE_INT_FN (BUILT_IN_PARITY):
6014 case BUILT_IN_PARITYIMAX:
6015 target = expand_builtin_unop (target_mode, arglist, target,
6016 subtarget, parity_optab);
6017 if (target)
6018 return target;
6019 break;
6021 case BUILT_IN_STRLEN:
6022 target = expand_builtin_strlen (arglist, target, target_mode);
6023 if (target)
6024 return target;
6025 break;
6027 case BUILT_IN_STRCPY:
6028 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
6029 if (target)
6030 return target;
6031 break;
6033 case BUILT_IN_STRNCPY:
6034 target = expand_builtin_strncpy (exp, target, mode);
6035 if (target)
6036 return target;
6037 break;
6039 case BUILT_IN_STPCPY:
6040 target = expand_builtin_stpcpy (exp, target, mode);
6041 if (target)
6042 return target;
6043 break;
6045 case BUILT_IN_STRCAT:
6046 target = expand_builtin_strcat (fndecl, arglist, target, mode);
6047 if (target)
6048 return target;
6049 break;
6051 case BUILT_IN_STRNCAT:
6052 target = expand_builtin_strncat (arglist, target, mode);
6053 if (target)
6054 return target;
6055 break;
6057 case BUILT_IN_STRSPN:
6058 target = expand_builtin_strspn (arglist, target, mode);
6059 if (target)
6060 return target;
6061 break;
6063 case BUILT_IN_STRCSPN:
6064 target = expand_builtin_strcspn (arglist, target, mode);
6065 if (target)
6066 return target;
6067 break;
6069 case BUILT_IN_STRSTR:
6070 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6071 if (target)
6072 return target;
6073 break;
6075 case BUILT_IN_STRPBRK:
6076 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6077 if (target)
6078 return target;
6079 break;
6081 case BUILT_IN_INDEX:
6082 case BUILT_IN_STRCHR:
6083 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6084 if (target)
6085 return target;
6086 break;
6088 case BUILT_IN_RINDEX:
6089 case BUILT_IN_STRRCHR:
6090 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6091 if (target)
6092 return target;
6093 break;
6095 case BUILT_IN_MEMCPY:
6096 target = expand_builtin_memcpy (exp, target, mode);
6097 if (target)
6098 return target;
6099 break;
6101 case BUILT_IN_MEMPCPY:
6102 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6103 if (target)
6104 return target;
6105 break;
6107 case BUILT_IN_MEMMOVE:
6108 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6109 mode);
6110 if (target)
6111 return target;
6112 break;
6114 case BUILT_IN_BCOPY:
6115 target = expand_builtin_bcopy (exp);
6116 if (target)
6117 return target;
6118 break;
6120 case BUILT_IN_MEMSET:
6121 target = expand_builtin_memset (arglist, target, mode, exp);
6122 if (target)
6123 return target;
6124 break;
6126 case BUILT_IN_BZERO:
6127 target = expand_builtin_bzero (exp);
6128 if (target)
6129 return target;
6130 break;
6132 case BUILT_IN_STRCMP:
6133 target = expand_builtin_strcmp (exp, target, mode);
6134 if (target)
6135 return target;
6136 break;
6138 case BUILT_IN_STRNCMP:
6139 target = expand_builtin_strncmp (exp, target, mode);
6140 if (target)
6141 return target;
6142 break;
6144 case BUILT_IN_BCMP:
6145 case BUILT_IN_MEMCMP:
6146 target = expand_builtin_memcmp (exp, arglist, target, mode);
6147 if (target)
6148 return target;
6149 break;
6151 case BUILT_IN_SETJMP:
6152 /* This should have been lowered to the builtins below. */
6153 gcc_unreachable ();
6155 case BUILT_IN_SETJMP_SETUP:
6156 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6157 and the receiver label. */
6158 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6160 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6161 VOIDmode, EXPAND_NORMAL);
6162 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6163 rtx label_r = label_rtx (label);
6165 /* This is copied from the handling of non-local gotos. */
6166 expand_builtin_setjmp_setup (buf_addr, label_r);
6167 nonlocal_goto_handler_labels
6168 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6169 nonlocal_goto_handler_labels);
6170 /* ??? Do not let expand_label treat us as such since we would
6171 not want to be both on the list of non-local labels and on
6172 the list of forced labels. */
6173 FORCED_LABEL (label) = 0;
6174 return const0_rtx;
6176 break;
6178 case BUILT_IN_SETJMP_DISPATCHER:
6179 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6180 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6182 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6183 rtx label_r = label_rtx (label);
6185 /* Remove the dispatcher label from the list of non-local labels
6186 since the receiver labels have been added to it above. */
6187 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6188 return const0_rtx;
6190 break;
6192 case BUILT_IN_SETJMP_RECEIVER:
6193 /* __builtin_setjmp_receiver is passed the receiver label. */
6194 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6196 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6197 rtx label_r = label_rtx (label);
6199 expand_builtin_setjmp_receiver (label_r);
6200 return const0_rtx;
6202 break;
6204 /* __builtin_longjmp is passed a pointer to an array of five words.
6205 It's similar to the C library longjmp function but works with
6206 __builtin_setjmp above. */
6207 case BUILT_IN_LONGJMP:
6208 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6210 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6211 VOIDmode, EXPAND_NORMAL);
6212 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6214 if (value != const1_rtx)
6216 error ("%<__builtin_longjmp%> second argument must be 1");
6217 return const0_rtx;
6220 expand_builtin_longjmp (buf_addr, value);
6221 return const0_rtx;
6223 break;
6225 case BUILT_IN_NONLOCAL_GOTO:
6226 target = expand_builtin_nonlocal_goto (arglist);
6227 if (target)
6228 return target;
6229 break;
6231 /* This updates the setjmp buffer that is its argument with the value
6232 of the current stack pointer. */
6233 case BUILT_IN_UPDATE_SETJMP_BUF:
6234 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6236 rtx buf_addr
6237 = expand_normal (TREE_VALUE (arglist));
6239 expand_builtin_update_setjmp_buf (buf_addr);
6240 return const0_rtx;
6242 break;
6244 case BUILT_IN_TRAP:
6245 expand_builtin_trap ();
6246 return const0_rtx;
6248 case BUILT_IN_PRINTF:
6249 target = expand_builtin_printf (exp, target, mode, false);
6250 if (target)
6251 return target;
6252 break;
6254 case BUILT_IN_PRINTF_UNLOCKED:
6255 target = expand_builtin_printf (exp, target, mode, true);
6256 if (target)
6257 return target;
6258 break;
6260 case BUILT_IN_FPUTS:
6261 target = expand_builtin_fputs (arglist, target, false);
6262 if (target)
6263 return target;
6264 break;
6265 case BUILT_IN_FPUTS_UNLOCKED:
6266 target = expand_builtin_fputs (arglist, target, true);
6267 if (target)
6268 return target;
6269 break;
6271 case BUILT_IN_FPRINTF:
6272 target = expand_builtin_fprintf (exp, target, mode, false);
6273 if (target)
6274 return target;
6275 break;
6277 case BUILT_IN_FPRINTF_UNLOCKED:
6278 target = expand_builtin_fprintf (exp, target, mode, true);
6279 if (target)
6280 return target;
6281 break;
6283 case BUILT_IN_SPRINTF:
6284 target = expand_builtin_sprintf (arglist, target, mode);
6285 if (target)
6286 return target;
6287 break;
6289 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6290 target = expand_builtin_signbit (exp, target);
6291 if (target)
6292 return target;
6293 break;
6295 /* Various hooks for the DWARF 2 __throw routine. */
6296 case BUILT_IN_UNWIND_INIT:
6297 expand_builtin_unwind_init ();
6298 return const0_rtx;
6299 case BUILT_IN_DWARF_CFA:
6300 return virtual_cfa_rtx;
6301 #ifdef DWARF2_UNWIND_INFO
6302 case BUILT_IN_DWARF_SP_COLUMN:
6303 return expand_builtin_dwarf_sp_column ();
6304 case BUILT_IN_INIT_DWARF_REG_SIZES:
6305 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6306 return const0_rtx;
6307 #endif
6308 case BUILT_IN_FROB_RETURN_ADDR:
6309 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6310 case BUILT_IN_EXTRACT_RETURN_ADDR:
6311 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6312 case BUILT_IN_EH_RETURN:
6313 expand_builtin_eh_return (TREE_VALUE (arglist),
6314 TREE_VALUE (TREE_CHAIN (arglist)));
6315 return const0_rtx;
6316 #ifdef EH_RETURN_DATA_REGNO
6317 case BUILT_IN_EH_RETURN_DATA_REGNO:
6318 return expand_builtin_eh_return_data_regno (arglist);
6319 #endif
6320 case BUILT_IN_EXTEND_POINTER:
6321 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6323 case BUILT_IN_VA_START:
6324 case BUILT_IN_STDARG_START:
6325 return expand_builtin_va_start (arglist);
6326 case BUILT_IN_VA_END:
6327 return expand_builtin_va_end (arglist);
6328 case BUILT_IN_VA_COPY:
6329 return expand_builtin_va_copy (arglist);
6330 case BUILT_IN_EXPECT:
6331 return expand_builtin_expect (arglist, target);
6332 case BUILT_IN_PREFETCH:
6333 expand_builtin_prefetch (arglist);
6334 return const0_rtx;
6336 case BUILT_IN_PROFILE_FUNC_ENTER:
6337 return expand_builtin_profile_func (false);
6338 case BUILT_IN_PROFILE_FUNC_EXIT:
6339 return expand_builtin_profile_func (true);
6341 case BUILT_IN_INIT_TRAMPOLINE:
6342 return expand_builtin_init_trampoline (arglist);
6343 case BUILT_IN_ADJUST_TRAMPOLINE:
6344 return expand_builtin_adjust_trampoline (arglist);
6346 case BUILT_IN_FORK:
6347 case BUILT_IN_EXECL:
6348 case BUILT_IN_EXECV:
6349 case BUILT_IN_EXECLP:
6350 case BUILT_IN_EXECLE:
6351 case BUILT_IN_EXECVP:
6352 case BUILT_IN_EXECVE:
6353 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6354 if (target)
6355 return target;
6356 break;
6358 case BUILT_IN_FETCH_AND_ADD_1:
6359 case BUILT_IN_FETCH_AND_ADD_2:
6360 case BUILT_IN_FETCH_AND_ADD_4:
6361 case BUILT_IN_FETCH_AND_ADD_8:
6362 case BUILT_IN_FETCH_AND_ADD_16:
6363 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6364 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6365 false, target, ignore);
6366 if (target)
6367 return target;
6368 break;
6370 case BUILT_IN_FETCH_AND_SUB_1:
6371 case BUILT_IN_FETCH_AND_SUB_2:
6372 case BUILT_IN_FETCH_AND_SUB_4:
6373 case BUILT_IN_FETCH_AND_SUB_8:
6374 case BUILT_IN_FETCH_AND_SUB_16:
6375 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6376 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6377 false, target, ignore);
6378 if (target)
6379 return target;
6380 break;
6382 case BUILT_IN_FETCH_AND_OR_1:
6383 case BUILT_IN_FETCH_AND_OR_2:
6384 case BUILT_IN_FETCH_AND_OR_4:
6385 case BUILT_IN_FETCH_AND_OR_8:
6386 case BUILT_IN_FETCH_AND_OR_16:
6387 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6388 target = expand_builtin_sync_operation (mode, arglist, IOR,
6389 false, target, ignore);
6390 if (target)
6391 return target;
6392 break;
6394 case BUILT_IN_FETCH_AND_AND_1:
6395 case BUILT_IN_FETCH_AND_AND_2:
6396 case BUILT_IN_FETCH_AND_AND_4:
6397 case BUILT_IN_FETCH_AND_AND_8:
6398 case BUILT_IN_FETCH_AND_AND_16:
6399 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6400 target = expand_builtin_sync_operation (mode, arglist, AND,
6401 false, target, ignore);
6402 if (target)
6403 return target;
6404 break;
6406 case BUILT_IN_FETCH_AND_XOR_1:
6407 case BUILT_IN_FETCH_AND_XOR_2:
6408 case BUILT_IN_FETCH_AND_XOR_4:
6409 case BUILT_IN_FETCH_AND_XOR_8:
6410 case BUILT_IN_FETCH_AND_XOR_16:
6411 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6412 target = expand_builtin_sync_operation (mode, arglist, XOR,
6413 false, target, ignore);
6414 if (target)
6415 return target;
6416 break;
6418 case BUILT_IN_FETCH_AND_NAND_1:
6419 case BUILT_IN_FETCH_AND_NAND_2:
6420 case BUILT_IN_FETCH_AND_NAND_4:
6421 case BUILT_IN_FETCH_AND_NAND_8:
6422 case BUILT_IN_FETCH_AND_NAND_16:
6423 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6424 target = expand_builtin_sync_operation (mode, arglist, NOT,
6425 false, target, ignore);
6426 if (target)
6427 return target;
6428 break;
6430 case BUILT_IN_ADD_AND_FETCH_1:
6431 case BUILT_IN_ADD_AND_FETCH_2:
6432 case BUILT_IN_ADD_AND_FETCH_4:
6433 case BUILT_IN_ADD_AND_FETCH_8:
6434 case BUILT_IN_ADD_AND_FETCH_16:
6435 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6436 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6437 true, target, ignore);
6438 if (target)
6439 return target;
6440 break;
6442 case BUILT_IN_SUB_AND_FETCH_1:
6443 case BUILT_IN_SUB_AND_FETCH_2:
6444 case BUILT_IN_SUB_AND_FETCH_4:
6445 case BUILT_IN_SUB_AND_FETCH_8:
6446 case BUILT_IN_SUB_AND_FETCH_16:
6447 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6448 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6449 true, target, ignore);
6450 if (target)
6451 return target;
6452 break;
6454 case BUILT_IN_OR_AND_FETCH_1:
6455 case BUILT_IN_OR_AND_FETCH_2:
6456 case BUILT_IN_OR_AND_FETCH_4:
6457 case BUILT_IN_OR_AND_FETCH_8:
6458 case BUILT_IN_OR_AND_FETCH_16:
6459 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6460 target = expand_builtin_sync_operation (mode, arglist, IOR,
6461 true, target, ignore);
6462 if (target)
6463 return target;
6464 break;
6466 case BUILT_IN_AND_AND_FETCH_1:
6467 case BUILT_IN_AND_AND_FETCH_2:
6468 case BUILT_IN_AND_AND_FETCH_4:
6469 case BUILT_IN_AND_AND_FETCH_8:
6470 case BUILT_IN_AND_AND_FETCH_16:
6471 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6472 target = expand_builtin_sync_operation (mode, arglist, AND,
6473 true, target, ignore);
6474 if (target)
6475 return target;
6476 break;
6478 case BUILT_IN_XOR_AND_FETCH_1:
6479 case BUILT_IN_XOR_AND_FETCH_2:
6480 case BUILT_IN_XOR_AND_FETCH_4:
6481 case BUILT_IN_XOR_AND_FETCH_8:
6482 case BUILT_IN_XOR_AND_FETCH_16:
6483 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6484 target = expand_builtin_sync_operation (mode, arglist, XOR,
6485 true, target, ignore);
6486 if (target)
6487 return target;
6488 break;
6490 case BUILT_IN_NAND_AND_FETCH_1:
6491 case BUILT_IN_NAND_AND_FETCH_2:
6492 case BUILT_IN_NAND_AND_FETCH_4:
6493 case BUILT_IN_NAND_AND_FETCH_8:
6494 case BUILT_IN_NAND_AND_FETCH_16:
6495 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6496 target = expand_builtin_sync_operation (mode, arglist, NOT,
6497 true, target, ignore);
6498 if (target)
6499 return target;
6500 break;
6502 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6503 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6504 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6505 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6506 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6507 if (mode == VOIDmode)
6508 mode = TYPE_MODE (boolean_type_node);
6509 if (!target || !register_operand (target, mode))
6510 target = gen_reg_rtx (mode);
6512 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6513 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6514 if (target)
6515 return target;
6516 break;
6518 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6519 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6520 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6521 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6522 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6523 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6524 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6525 if (target)
6526 return target;
6527 break;
6529 case BUILT_IN_LOCK_TEST_AND_SET_1:
6530 case BUILT_IN_LOCK_TEST_AND_SET_2:
6531 case BUILT_IN_LOCK_TEST_AND_SET_4:
6532 case BUILT_IN_LOCK_TEST_AND_SET_8:
6533 case BUILT_IN_LOCK_TEST_AND_SET_16:
6534 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6535 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6536 if (target)
6537 return target;
6538 break;
6540 case BUILT_IN_LOCK_RELEASE_1:
6541 case BUILT_IN_LOCK_RELEASE_2:
6542 case BUILT_IN_LOCK_RELEASE_4:
6543 case BUILT_IN_LOCK_RELEASE_8:
6544 case BUILT_IN_LOCK_RELEASE_16:
6545 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6546 expand_builtin_lock_release (mode, arglist);
6547 return const0_rtx;
6549 case BUILT_IN_SYNCHRONIZE:
6550 expand_builtin_synchronize ();
6551 return const0_rtx;
6553 case BUILT_IN_OBJECT_SIZE:
6554 return expand_builtin_object_size (exp);
6556 case BUILT_IN_MEMCPY_CHK:
6557 case BUILT_IN_MEMPCPY_CHK:
6558 case BUILT_IN_MEMMOVE_CHK:
6559 case BUILT_IN_MEMSET_CHK:
6560 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6561 if (target)
6562 return target;
6563 break;
6565 case BUILT_IN_STRCPY_CHK:
6566 case BUILT_IN_STPCPY_CHK:
6567 case BUILT_IN_STRNCPY_CHK:
6568 case BUILT_IN_STRCAT_CHK:
6569 case BUILT_IN_STRNCAT_CHK:
6570 case BUILT_IN_SNPRINTF_CHK:
6571 case BUILT_IN_VSNPRINTF_CHK:
6572 maybe_emit_chk_warning (exp, fcode);
6573 break;
6575 case BUILT_IN_SPRINTF_CHK:
6576 case BUILT_IN_VSPRINTF_CHK:
6577 maybe_emit_sprintf_chk_warning (exp, fcode);
6578 break;
6580 default: /* just do library call, if unknown builtin */
6581 break;
6584 /* The switch statement above can drop through to cause the function
6585 to be called normally. */
6586 return expand_call (exp, target, ignore);
6589 /* Determine whether a tree node represents a call to a built-in
6590 function. If the tree T is a call to a built-in function with
6591 the right number of arguments of the appropriate types, return
6592 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6593 Otherwise the return value is END_BUILTINS. */
6595 enum built_in_function
6596 builtin_mathfn_code (tree t)
6598 tree fndecl, arglist, parmlist;
6599 tree argtype, parmtype;
6601 if (TREE_CODE (t) != CALL_EXPR
6602 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6603 return END_BUILTINS;
6605 fndecl = get_callee_fndecl (t);
6606 if (fndecl == NULL_TREE
6607 || TREE_CODE (fndecl) != FUNCTION_DECL
6608 || ! DECL_BUILT_IN (fndecl)
6609 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6610 return END_BUILTINS;
6612 arglist = TREE_OPERAND (t, 1);
6613 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6614 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6616 /* If a function doesn't take a variable number of arguments,
6617 the last element in the list will have type `void'. */
6618 parmtype = TREE_VALUE (parmlist);
6619 if (VOID_TYPE_P (parmtype))
6621 if (arglist)
6622 return END_BUILTINS;
6623 return DECL_FUNCTION_CODE (fndecl);
6626 if (! arglist)
6627 return END_BUILTINS;
6629 argtype = TREE_TYPE (TREE_VALUE (arglist));
6631 if (SCALAR_FLOAT_TYPE_P (parmtype))
6633 if (! SCALAR_FLOAT_TYPE_P (argtype))
6634 return END_BUILTINS;
6636 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6638 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6639 return END_BUILTINS;
6641 else if (POINTER_TYPE_P (parmtype))
6643 if (! POINTER_TYPE_P (argtype))
6644 return END_BUILTINS;
6646 else if (INTEGRAL_TYPE_P (parmtype))
6648 if (! INTEGRAL_TYPE_P (argtype))
6649 return END_BUILTINS;
6651 else
6652 return END_BUILTINS;
6654 arglist = TREE_CHAIN (arglist);
6657 /* Variable-length argument list. */
6658 return DECL_FUNCTION_CODE (fndecl);
6661 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6662 constant. ARGLIST is the argument list of the call. */
6664 static tree
6665 fold_builtin_constant_p (tree arglist)
6667 if (arglist == 0)
6668 return 0;
6670 arglist = TREE_VALUE (arglist);
6672 /* We return 1 for a numeric type that's known to be a constant
6673 value at compile-time or for an aggregate type that's a
6674 literal constant. */
6675 STRIP_NOPS (arglist);
6677 /* If we know this is a constant, emit the constant of one. */
6678 if (CONSTANT_CLASS_P (arglist)
6679 || (TREE_CODE (arglist) == CONSTRUCTOR
6680 && TREE_CONSTANT (arglist)))
6681 return integer_one_node;
6682 if (TREE_CODE (arglist) == ADDR_EXPR)
6684 tree op = TREE_OPERAND (arglist, 0);
6685 if (TREE_CODE (op) == STRING_CST
6686 || (TREE_CODE (op) == ARRAY_REF
6687 && integer_zerop (TREE_OPERAND (op, 1))
6688 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6689 return integer_one_node;
6692 /* If this expression has side effects, show we don't know it to be a
6693 constant. Likewise if it's a pointer or aggregate type since in
6694 those case we only want literals, since those are only optimized
6695 when generating RTL, not later.
6696 And finally, if we are compiling an initializer, not code, we
6697 need to return a definite result now; there's not going to be any
6698 more optimization done. */
6699 if (TREE_SIDE_EFFECTS (arglist)
6700 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6701 || POINTER_TYPE_P (TREE_TYPE (arglist))
6702 || cfun == 0
6703 || folding_initializer)
6704 return integer_zero_node;
6706 return 0;
6709 /* Fold a call to __builtin_expect, if we expect that a comparison against
6710 the argument will fold to a constant. In practice, this means a true
6711 constant or the address of a non-weak symbol. ARGLIST is the argument
6712 list of the call. */
6714 static tree
6715 fold_builtin_expect (tree arglist)
6717 tree arg, inner;
6719 if (arglist == 0)
6720 return 0;
6722 arg = TREE_VALUE (arglist);
6724 /* If the argument isn't invariant, then there's nothing we can do. */
6725 if (!TREE_INVARIANT (arg))
6726 return 0;
6728 /* If we're looking at an address of a weak decl, then do not fold. */
6729 inner = arg;
6730 STRIP_NOPS (inner);
6731 if (TREE_CODE (inner) == ADDR_EXPR)
6735 inner = TREE_OPERAND (inner, 0);
6737 while (TREE_CODE (inner) == COMPONENT_REF
6738 || TREE_CODE (inner) == ARRAY_REF);
6739 if (DECL_P (inner) && DECL_WEAK (inner))
6740 return 0;
6743 /* Otherwise, ARG already has the proper type for the return value. */
6744 return arg;
6747 /* Fold a call to __builtin_classify_type. */
6749 static tree
6750 fold_builtin_classify_type (tree arglist)
6752 if (arglist == 0)
6753 return build_int_cst (NULL_TREE, no_type_class);
6755 return build_int_cst (NULL_TREE,
6756 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6759 /* Fold a call to __builtin_strlen. */
6761 static tree
6762 fold_builtin_strlen (tree arglist)
6764 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6765 return NULL_TREE;
6766 else
6768 tree len = c_strlen (TREE_VALUE (arglist), 0);
6770 if (len)
6772 /* Convert from the internal "sizetype" type to "size_t". */
6773 if (size_type_node)
6774 len = fold_convert (size_type_node, len);
6775 return len;
6778 return NULL_TREE;
6782 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6784 static tree
6785 fold_builtin_inf (tree type, int warn)
6787 REAL_VALUE_TYPE real;
6789 /* __builtin_inff is intended to be usable to define INFINITY on all
6790 targets. If an infinity is not available, INFINITY expands "to a
6791 positive constant of type float that overflows at translation
6792 time", footnote "In this case, using INFINITY will violate the
6793 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6794 Thus we pedwarn to ensure this constraint violation is
6795 diagnosed. */
6796 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6797 pedwarn ("target format does not support infinity");
6799 real_inf (&real);
6800 return build_real (type, real);
6803 /* Fold a call to __builtin_nan or __builtin_nans. */
6805 static tree
6806 fold_builtin_nan (tree arglist, tree type, int quiet)
6808 REAL_VALUE_TYPE real;
6809 const char *str;
6811 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6812 return 0;
6813 str = c_getstr (TREE_VALUE (arglist));
6814 if (!str)
6815 return 0;
6817 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6818 return 0;
6820 return build_real (type, real);
6823 /* Return true if the floating point expression T has an integer value.
6824 We also allow +Inf, -Inf and NaN to be considered integer values. */
6826 static bool
6827 integer_valued_real_p (tree t)
6829 switch (TREE_CODE (t))
6831 case FLOAT_EXPR:
6832 return true;
6834 case ABS_EXPR:
6835 case SAVE_EXPR:
6836 case NON_LVALUE_EXPR:
6837 return integer_valued_real_p (TREE_OPERAND (t, 0));
6839 case COMPOUND_EXPR:
6840 case GIMPLE_MODIFY_STMT:
6841 case BIND_EXPR:
6842 return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1));
6844 case PLUS_EXPR:
6845 case MINUS_EXPR:
6846 case MULT_EXPR:
6847 case MIN_EXPR:
6848 case MAX_EXPR:
6849 return integer_valued_real_p (TREE_OPERAND (t, 0))
6850 && integer_valued_real_p (TREE_OPERAND (t, 1));
6852 case COND_EXPR:
6853 return integer_valued_real_p (TREE_OPERAND (t, 1))
6854 && integer_valued_real_p (TREE_OPERAND (t, 2));
6856 case REAL_CST:
6857 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6859 case NOP_EXPR:
6861 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6862 if (TREE_CODE (type) == INTEGER_TYPE)
6863 return true;
6864 if (TREE_CODE (type) == REAL_TYPE)
6865 return integer_valued_real_p (TREE_OPERAND (t, 0));
6866 break;
6869 case CALL_EXPR:
6870 switch (builtin_mathfn_code (t))
6872 CASE_FLT_FN (BUILT_IN_CEIL):
6873 CASE_FLT_FN (BUILT_IN_FLOOR):
6874 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6875 CASE_FLT_FN (BUILT_IN_RINT):
6876 CASE_FLT_FN (BUILT_IN_ROUND):
6877 CASE_FLT_FN (BUILT_IN_TRUNC):
6878 return true;
6880 CASE_FLT_FN (BUILT_IN_FMIN):
6881 CASE_FLT_FN (BUILT_IN_FMAX):
6882 return integer_valued_real_p (TREE_VALUE (TREE_OPERAND (t, 1)))
6883 && integer_valued_real_p (TREE_VALUE (TREE_CHAIN (TREE_OPERAND (t, 1))));
6885 default:
6886 break;
6888 break;
6890 default:
6891 break;
6893 return false;
6896 /* EXP is assumed to be builtin call where truncation can be propagated
6897 across (for instance floor((double)f) == (double)floorf (f).
6898 Do the transformation. */
6900 static tree
6901 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6903 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6904 tree arg;
6906 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6907 return 0;
6909 arg = TREE_VALUE (arglist);
6910 /* Integer rounding functions are idempotent. */
6911 if (fcode == builtin_mathfn_code (arg))
6912 return arg;
6914 /* If argument is already integer valued, and we don't need to worry
6915 about setting errno, there's no need to perform rounding. */
6916 if (! flag_errno_math && integer_valued_real_p (arg))
6917 return arg;
6919 if (optimize)
6921 tree arg0 = strip_float_extensions (arg);
6922 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6923 tree newtype = TREE_TYPE (arg0);
6924 tree decl;
6926 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6927 && (decl = mathfn_built_in (newtype, fcode)))
6929 arglist =
6930 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6931 return fold_convert (ftype,
6932 build_function_call_expr (decl, arglist));
6935 return 0;
6938 /* EXP is assumed to be builtin call which can narrow the FP type of
6939 the argument, for instance lround((double)f) -> lroundf (f). */
6941 static tree
6942 fold_fixed_mathfn (tree fndecl, tree arglist)
6944 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6945 tree arg;
6947 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6948 return 0;
6950 arg = TREE_VALUE (arglist);
6952 /* If argument is already integer valued, and we don't need to worry
6953 about setting errno, there's no need to perform rounding. */
6954 if (! flag_errno_math && integer_valued_real_p (arg))
6955 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6957 if (optimize)
6959 tree ftype = TREE_TYPE (arg);
6960 tree arg0 = strip_float_extensions (arg);
6961 tree newtype = TREE_TYPE (arg0);
6962 tree decl;
6964 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6965 && (decl = mathfn_built_in (newtype, fcode)))
6967 arglist =
6968 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6969 return build_function_call_expr (decl, arglist);
6973 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6974 sizeof (long long) == sizeof (long). */
6975 if (TYPE_PRECISION (long_long_integer_type_node)
6976 == TYPE_PRECISION (long_integer_type_node))
6978 tree newfn = NULL_TREE;
6979 switch (fcode)
6981 CASE_FLT_FN (BUILT_IN_LLCEIL):
6982 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6983 break;
6985 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6986 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6987 break;
6989 CASE_FLT_FN (BUILT_IN_LLROUND):
6990 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6991 break;
6993 CASE_FLT_FN (BUILT_IN_LLRINT):
6994 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6995 break;
6997 default:
6998 break;
7001 if (newfn)
7003 tree newcall = build_function_call_expr (newfn, arglist);
7004 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7008 return 0;
7011 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
7012 is the argument list, TYPE is the return type and FNDECL is the
7013 original function DECL. Return NULL_TREE if no if no simplification
7014 can be made. */
7016 static tree
7017 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
7019 tree arg;
7021 if (!arglist || TREE_CHAIN (arglist))
7022 return NULL_TREE;
7024 arg = TREE_VALUE (arglist);
7025 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7026 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7027 return NULL_TREE;
7029 /* Evaluate cabs of a constant at compile-time. */
7030 if (flag_unsafe_math_optimizations
7031 && TREE_CODE (arg) == COMPLEX_CST
7032 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
7033 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
7034 && !TREE_OVERFLOW (TREE_REALPART (arg))
7035 && !TREE_OVERFLOW (TREE_IMAGPART (arg)))
7037 REAL_VALUE_TYPE r, i;
7039 r = TREE_REAL_CST (TREE_REALPART (arg));
7040 i = TREE_REAL_CST (TREE_IMAGPART (arg));
7042 real_arithmetic (&r, MULT_EXPR, &r, &r);
7043 real_arithmetic (&i, MULT_EXPR, &i, &i);
7044 real_arithmetic (&r, PLUS_EXPR, &r, &i);
7045 if (real_sqrt (&r, TYPE_MODE (type), &r)
7046 || ! flag_trapping_math)
7047 return build_real (type, r);
7050 /* If either part is zero, cabs is fabs of the other. */
7051 if (TREE_CODE (arg) == COMPLEX_EXPR
7052 && real_zerop (TREE_OPERAND (arg, 0)))
7053 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
7054 if (TREE_CODE (arg) == COMPLEX_EXPR
7055 && real_zerop (TREE_OPERAND (arg, 1)))
7056 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
7058 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7059 if (TREE_CODE (arg) == NEGATE_EXPR
7060 || TREE_CODE (arg) == CONJ_EXPR)
7062 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
7063 return build_function_call_expr (fndecl, arglist);
7066 /* Don't do this when optimizing for size. */
7067 if (flag_unsafe_math_optimizations
7068 && optimize && !optimize_size)
7070 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7072 if (sqrtfn != NULL_TREE)
7074 tree rpart, ipart, result, arglist;
7076 arg = builtin_save_expr (arg);
7078 rpart = fold_build1 (REALPART_EXPR, type, arg);
7079 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7081 rpart = builtin_save_expr (rpart);
7082 ipart = builtin_save_expr (ipart);
7084 result = fold_build2 (PLUS_EXPR, type,
7085 fold_build2 (MULT_EXPR, type,
7086 rpart, rpart),
7087 fold_build2 (MULT_EXPR, type,
7088 ipart, ipart));
7090 arglist = build_tree_list (NULL_TREE, result);
7091 return build_function_call_expr (sqrtfn, arglist);
7095 return NULL_TREE;
7098 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7099 NULL_TREE if no simplification can be made. */
7101 static tree
7102 fold_builtin_sqrt (tree arglist, tree type)
7105 enum built_in_function fcode;
7106 tree arg = TREE_VALUE (arglist);
7108 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7109 return NULL_TREE;
7111 /* Optimize sqrt of constant value. */
7112 if (TREE_CODE (arg) == REAL_CST
7113 && !TREE_OVERFLOW (arg))
7115 REAL_VALUE_TYPE r, x;
7117 x = TREE_REAL_CST (arg);
7118 if (real_sqrt (&r, TYPE_MODE (type), &x)
7119 || (!flag_trapping_math && !flag_errno_math))
7120 return build_real (type, r);
7123 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7124 fcode = builtin_mathfn_code (arg);
7125 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7127 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7128 arg = fold_build2 (MULT_EXPR, type,
7129 TREE_VALUE (TREE_OPERAND (arg, 1)),
7130 build_real (type, dconsthalf));
7131 arglist = build_tree_list (NULL_TREE, arg);
7132 return build_function_call_expr (expfn, arglist);
7135 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7136 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7138 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7140 if (powfn)
7142 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7143 tree tree_root;
7144 /* The inner root was either sqrt or cbrt. */
7145 REAL_VALUE_TYPE dconstroot =
7146 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7148 /* Adjust for the outer root. */
7149 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7150 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7151 tree_root = build_real (type, dconstroot);
7152 arglist = tree_cons (NULL_TREE, arg0,
7153 build_tree_list (NULL_TREE, tree_root));
7154 return build_function_call_expr (powfn, arglist);
7158 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7159 if (flag_unsafe_math_optimizations
7160 && (fcode == BUILT_IN_POW
7161 || fcode == BUILT_IN_POWF
7162 || fcode == BUILT_IN_POWL))
7164 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7165 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7166 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7167 tree narg1;
7168 if (!tree_expr_nonnegative_p (arg0))
7169 arg0 = build1 (ABS_EXPR, type, arg0);
7170 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7171 build_real (type, dconsthalf));
7172 arglist = tree_cons (NULL_TREE, arg0,
7173 build_tree_list (NULL_TREE, narg1));
7174 return build_function_call_expr (powfn, arglist);
7177 return NULL_TREE;
7180 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7181 NULL_TREE if no simplification can be made. */
7182 static tree
7183 fold_builtin_cbrt (tree arglist, tree type)
7185 tree arg = TREE_VALUE (arglist);
7186 const enum built_in_function fcode = builtin_mathfn_code (arg);
7187 tree res;
7189 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7190 return NULL_TREE;
7192 /* Calculate the result when the argument is a constant. */
7193 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7194 return res;
7196 if (flag_unsafe_math_optimizations)
7198 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7199 if (BUILTIN_EXPONENT_P (fcode))
7201 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7202 const REAL_VALUE_TYPE third_trunc =
7203 real_value_truncate (TYPE_MODE (type), dconstthird);
7204 arg = fold_build2 (MULT_EXPR, type,
7205 TREE_VALUE (TREE_OPERAND (arg, 1)),
7206 build_real (type, third_trunc));
7207 arglist = build_tree_list (NULL_TREE, arg);
7208 return build_function_call_expr (expfn, arglist);
7211 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7212 if (BUILTIN_SQRT_P (fcode))
7214 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7216 if (powfn)
7218 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7219 tree tree_root;
7220 REAL_VALUE_TYPE dconstroot = dconstthird;
7222 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7223 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7224 tree_root = build_real (type, dconstroot);
7225 arglist = tree_cons (NULL_TREE, arg0,
7226 build_tree_list (NULL_TREE, tree_root));
7227 return build_function_call_expr (powfn, arglist);
7231 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7232 if (BUILTIN_CBRT_P (fcode))
7234 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7235 if (tree_expr_nonnegative_p (arg0))
7237 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7239 if (powfn)
7241 tree tree_root;
7242 REAL_VALUE_TYPE dconstroot;
7244 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7245 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7246 tree_root = build_real (type, dconstroot);
7247 arglist = tree_cons (NULL_TREE, arg0,
7248 build_tree_list (NULL_TREE, tree_root));
7249 return build_function_call_expr (powfn, arglist);
7254 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7255 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7256 || fcode == BUILT_IN_POWL)
7258 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7259 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7260 if (tree_expr_nonnegative_p (arg00))
7262 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7263 const REAL_VALUE_TYPE dconstroot
7264 = real_value_truncate (TYPE_MODE (type), dconstthird);
7265 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7266 build_real (type, dconstroot));
7267 arglist = tree_cons (NULL_TREE, arg00,
7268 build_tree_list (NULL_TREE, narg01));
7269 return build_function_call_expr (powfn, arglist);
7273 return NULL_TREE;
7276 /* Fold function call to builtin cos, cosf, or cosl. Return
7277 NULL_TREE if no simplification can be made. */
7278 static tree
7279 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7281 tree arg = TREE_VALUE (arglist);
7282 tree res, narg;
7284 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7285 return NULL_TREE;
7287 /* Calculate the result when the argument is a constant. */
7288 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7289 return res;
7291 /* Optimize cos(-x) into cos (x). */
7292 if ((narg = fold_strip_sign_ops (arg)))
7293 return build_function_call_expr (fndecl,
7294 build_tree_list (NULL_TREE, narg));
7296 return NULL_TREE;
7299 /* Fold function call to builtin cosh, coshf, or coshl. Return
7300 NULL_TREE if no simplification can be made. */
7301 static tree
7302 fold_builtin_cosh (tree arglist, tree type, tree fndecl)
7304 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7306 tree arg = TREE_VALUE (arglist);
7307 tree res, narg;
7309 /* Calculate the result when the argument is a constant. */
7310 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7311 return res;
7313 /* Optimize cosh(-x) into cosh (x). */
7314 if ((narg = fold_strip_sign_ops (arg)))
7315 return build_function_call_expr (fndecl,
7316 build_tree_list (NULL_TREE, narg));
7319 return NULL_TREE;
7322 /* Fold function call to builtin tan, tanf, or tanl. Return
7323 NULL_TREE if no simplification can be made. */
7324 static tree
7325 fold_builtin_tan (tree arglist, tree type)
7327 enum built_in_function fcode;
7328 tree arg = TREE_VALUE (arglist);
7329 tree res;
7331 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7332 return NULL_TREE;
7334 /* Calculate the result when the argument is a constant. */
7335 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7336 return res;
7338 /* Optimize tan(atan(x)) = x. */
7339 fcode = builtin_mathfn_code (arg);
7340 if (flag_unsafe_math_optimizations
7341 && (fcode == BUILT_IN_ATAN
7342 || fcode == BUILT_IN_ATANF
7343 || fcode == BUILT_IN_ATANL))
7344 return TREE_VALUE (TREE_OPERAND (arg, 1));
7346 return NULL_TREE;
7349 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7350 NULL_TREE if no simplification can be made. */
7352 static tree
7353 fold_builtin_sincos (tree arglist)
7355 tree type, arg0, arg1, arg2;
7356 tree res, fn, call;
7358 if (!validate_arglist (arglist, REAL_TYPE, POINTER_TYPE,
7359 POINTER_TYPE, VOID_TYPE))
7360 return NULL_TREE;
7362 arg0 = TREE_VALUE (arglist);
7363 type = TREE_TYPE (arg0);
7364 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7365 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7367 /* Calculate the result when the argument is a constant. */
7368 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7369 return res;
7371 /* Canonicalize sincos to cexpi. */
7372 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7373 if (!fn)
7374 return NULL_TREE;
7376 call = build_function_call_expr (fn, build_tree_list (NULL_TREE, arg0));
7377 call = builtin_save_expr (call);
7379 return build2 (COMPOUND_EXPR, type,
7380 build2 (MODIFY_EXPR, void_type_node,
7381 build_fold_indirect_ref (arg1),
7382 build1 (IMAGPART_EXPR, type, call)),
7383 build2 (MODIFY_EXPR, void_type_node,
7384 build_fold_indirect_ref (arg2),
7385 build1 (REALPART_EXPR, type, call)));
7388 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7389 NULL_TREE if no simplification can be made. */
7391 static tree
7392 fold_builtin_cexp (tree arglist, tree type)
7394 tree arg0, rtype;
7395 tree realp, imagp, ifn;
7397 if (!validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7398 return NULL_TREE;
7400 arg0 = TREE_VALUE (arglist);
7401 rtype = TREE_TYPE (TREE_TYPE (arg0));
7403 /* In case we can figure out the real part of arg0 and it is constant zero
7404 fold to cexpi. */
7405 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7406 if (!ifn)
7407 return NULL_TREE;
7409 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
7410 && real_zerop (realp))
7412 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
7413 return build_function_call_expr (ifn, build_tree_list (NULL_TREE, narg));
7416 /* In case we can easily decompose real and imaginary parts split cexp
7417 to exp (r) * cexpi (i). */
7418 if (flag_unsafe_math_optimizations
7419 && realp)
7421 tree rfn, rcall, icall;
7423 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7424 if (!rfn)
7425 return NULL_TREE;
7427 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
7428 if (!imagp)
7429 return NULL_TREE;
7431 icall = build_function_call_expr (ifn,
7432 build_tree_list (NULL_TREE, imagp));
7433 icall = builtin_save_expr (icall);
7434 rcall = build_function_call_expr (rfn,
7435 build_tree_list (NULL_TREE, realp));
7436 rcall = builtin_save_expr (rcall);
7437 return build2 (COMPLEX_EXPR, type,
7438 build2 (MULT_EXPR, rtype,
7439 rcall,
7440 build1 (REALPART_EXPR, rtype, icall)),
7441 build2 (MULT_EXPR, rtype,
7442 rcall,
7443 build1 (IMAGPART_EXPR, rtype, icall)));
7446 return NULL_TREE;
7449 /* Fold function call to builtin trunc, truncf or truncl. Return
7450 NULL_TREE if no simplification can be made. */
7452 static tree
7453 fold_builtin_trunc (tree fndecl, tree arglist)
7455 tree arg;
7457 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7458 return 0;
7460 /* Optimize trunc of constant value. */
7461 arg = TREE_VALUE (arglist);
7462 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7464 REAL_VALUE_TYPE r, x;
7465 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7467 x = TREE_REAL_CST (arg);
7468 real_trunc (&r, TYPE_MODE (type), &x);
7469 return build_real (type, r);
7472 return fold_trunc_transparent_mathfn (fndecl, arglist);
7475 /* Fold function call to builtin floor, floorf or floorl. Return
7476 NULL_TREE if no simplification can be made. */
7478 static tree
7479 fold_builtin_floor (tree fndecl, tree arglist)
7481 tree arg;
7483 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7484 return 0;
7486 /* Optimize floor of constant value. */
7487 arg = TREE_VALUE (arglist);
7488 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7490 REAL_VALUE_TYPE x;
7492 x = TREE_REAL_CST (arg);
7493 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7495 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7496 REAL_VALUE_TYPE r;
7498 real_floor (&r, TYPE_MODE (type), &x);
7499 return build_real (type, r);
7503 /* Fold floor (x) where x is nonnegative to trunc (x). */
7504 if (tree_expr_nonnegative_p (arg))
7506 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7507 if (truncfn)
7508 return build_function_call_expr (truncfn, arglist);
7511 return fold_trunc_transparent_mathfn (fndecl, arglist);
7514 /* Fold function call to builtin ceil, ceilf or ceill. Return
7515 NULL_TREE if no simplification can be made. */
7517 static tree
7518 fold_builtin_ceil (tree fndecl, tree arglist)
7520 tree arg;
7522 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7523 return 0;
7525 /* Optimize ceil of constant value. */
7526 arg = TREE_VALUE (arglist);
7527 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7529 REAL_VALUE_TYPE x;
7531 x = TREE_REAL_CST (arg);
7532 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7534 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7535 REAL_VALUE_TYPE r;
7537 real_ceil (&r, TYPE_MODE (type), &x);
7538 return build_real (type, r);
7542 return fold_trunc_transparent_mathfn (fndecl, arglist);
7545 /* Fold function call to builtin round, roundf or roundl. Return
7546 NULL_TREE if no simplification can be made. */
7548 static tree
7549 fold_builtin_round (tree fndecl, tree arglist)
7551 tree arg;
7553 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7554 return 0;
7556 /* Optimize round of constant value. */
7557 arg = TREE_VALUE (arglist);
7558 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7560 REAL_VALUE_TYPE x;
7562 x = TREE_REAL_CST (arg);
7563 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7565 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7566 REAL_VALUE_TYPE r;
7568 real_round (&r, TYPE_MODE (type), &x);
7569 return build_real (type, r);
7573 return fold_trunc_transparent_mathfn (fndecl, arglist);
7576 /* Fold function call to builtin lround, lroundf or lroundl (or the
7577 corresponding long long versions) and other rounding functions.
7578 Return NULL_TREE if no simplification can be made. */
7580 static tree
7581 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7583 tree arg;
7585 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7586 return 0;
7588 /* Optimize lround of constant value. */
7589 arg = TREE_VALUE (arglist);
7590 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7592 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7594 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7596 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7597 tree ftype = TREE_TYPE (arg);
7598 unsigned HOST_WIDE_INT lo2;
7599 HOST_WIDE_INT hi, lo;
7600 REAL_VALUE_TYPE r;
7602 switch (DECL_FUNCTION_CODE (fndecl))
7604 CASE_FLT_FN (BUILT_IN_LFLOOR):
7605 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7606 real_floor (&r, TYPE_MODE (ftype), &x);
7607 break;
7609 CASE_FLT_FN (BUILT_IN_LCEIL):
7610 CASE_FLT_FN (BUILT_IN_LLCEIL):
7611 real_ceil (&r, TYPE_MODE (ftype), &x);
7612 break;
7614 CASE_FLT_FN (BUILT_IN_LROUND):
7615 CASE_FLT_FN (BUILT_IN_LLROUND):
7616 real_round (&r, TYPE_MODE (ftype), &x);
7617 break;
7619 default:
7620 gcc_unreachable ();
7623 REAL_VALUE_TO_INT (&lo, &hi, r);
7624 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
7625 return build_int_cst_wide (itype, lo2, hi);
7629 switch (DECL_FUNCTION_CODE (fndecl))
7631 CASE_FLT_FN (BUILT_IN_LFLOOR):
7632 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7633 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7634 if (tree_expr_nonnegative_p (arg))
7635 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7636 arg);
7637 break;
7638 default:;
7641 return fold_fixed_mathfn (fndecl, arglist);
7644 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7645 and their long and long long variants (i.e. ffsl and ffsll).
7646 Return NULL_TREE if no simplification can be made. */
7648 static tree
7649 fold_builtin_bitop (tree fndecl, tree arglist)
7651 tree arg;
7653 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7654 return NULL_TREE;
7656 /* Optimize for constant argument. */
7657 arg = TREE_VALUE (arglist);
7658 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7660 HOST_WIDE_INT hi, width, result;
7661 unsigned HOST_WIDE_INT lo;
7662 tree type;
7664 type = TREE_TYPE (arg);
7665 width = TYPE_PRECISION (type);
7666 lo = TREE_INT_CST_LOW (arg);
7668 /* Clear all the bits that are beyond the type's precision. */
7669 if (width > HOST_BITS_PER_WIDE_INT)
7671 hi = TREE_INT_CST_HIGH (arg);
7672 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7673 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7675 else
7677 hi = 0;
7678 if (width < HOST_BITS_PER_WIDE_INT)
7679 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7682 switch (DECL_FUNCTION_CODE (fndecl))
7684 CASE_INT_FN (BUILT_IN_FFS):
7685 if (lo != 0)
7686 result = exact_log2 (lo & -lo) + 1;
7687 else if (hi != 0)
7688 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7689 else
7690 result = 0;
7691 break;
7693 CASE_INT_FN (BUILT_IN_CLZ):
7694 if (hi != 0)
7695 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7696 else if (lo != 0)
7697 result = width - floor_log2 (lo) - 1;
7698 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7699 result = width;
7700 break;
7702 CASE_INT_FN (BUILT_IN_CTZ):
7703 if (lo != 0)
7704 result = exact_log2 (lo & -lo);
7705 else if (hi != 0)
7706 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7707 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7708 result = width;
7709 break;
7711 CASE_INT_FN (BUILT_IN_POPCOUNT):
7712 result = 0;
7713 while (lo)
7714 result++, lo &= lo - 1;
7715 while (hi)
7716 result++, hi &= hi - 1;
7717 break;
7719 CASE_INT_FN (BUILT_IN_PARITY):
7720 result = 0;
7721 while (lo)
7722 result++, lo &= lo - 1;
7723 while (hi)
7724 result++, hi &= hi - 1;
7725 result &= 1;
7726 break;
7728 default:
7729 gcc_unreachable ();
7732 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7735 return NULL_TREE;
7738 /* Fold function call to builtin_bswap and the long and long long
7739 variants. Return NULL_TREE if no simplification can be made. */
7740 static tree
7741 fold_builtin_bswap (tree fndecl, tree arglist)
7743 tree arg;
7745 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7746 return 0;
7748 /* Optimize constant value. */
7749 arg = TREE_VALUE (arglist);
7750 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7752 HOST_WIDE_INT hi, width, r_hi = 0;
7753 unsigned HOST_WIDE_INT lo, r_lo = 0;
7754 tree type;
7756 type = TREE_TYPE (arg);
7757 width = TYPE_PRECISION (type);
7758 lo = TREE_INT_CST_LOW (arg);
7759 hi = TREE_INT_CST_HIGH (arg);
7761 switch (DECL_FUNCTION_CODE (fndecl))
7763 case BUILT_IN_BSWAP32:
7764 case BUILT_IN_BSWAP64:
7766 int s;
7768 for (s = 0; s < width; s += 8)
7770 int d = width - s - 8;
7771 unsigned HOST_WIDE_INT byte;
7773 if (s < HOST_BITS_PER_WIDE_INT)
7774 byte = (lo >> s) & 0xff;
7775 else
7776 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7778 if (d < HOST_BITS_PER_WIDE_INT)
7779 r_lo |= byte << d;
7780 else
7781 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7785 break;
7787 default:
7788 gcc_unreachable ();
7791 if (width < HOST_BITS_PER_WIDE_INT)
7792 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7793 else
7794 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7797 return NULL_TREE;
7799 /* Return true if EXPR is the real constant contained in VALUE. */
7801 static bool
7802 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7804 STRIP_NOPS (expr);
7806 return ((TREE_CODE (expr) == REAL_CST
7807 && !TREE_OVERFLOW (expr)
7808 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7809 || (TREE_CODE (expr) == COMPLEX_CST
7810 && real_dconstp (TREE_REALPART (expr), value)
7811 && real_zerop (TREE_IMAGPART (expr))));
7814 /* A subroutine of fold_builtin to fold the various logarithmic
7815 functions. Return NULL_TREE if no simplification can me made.
7816 FUNC is the corresponding MPFR logarithm function. */
7818 static tree
7819 fold_builtin_logarithm (tree fndecl, tree arglist,
7820 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7822 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7824 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7825 tree arg = TREE_VALUE (arglist);
7826 tree res;
7827 const enum built_in_function fcode = builtin_mathfn_code (arg);
7829 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7830 instead we'll look for 'e' truncated to MODE. So only do
7831 this if flag_unsafe_math_optimizations is set. */
7832 if (flag_unsafe_math_optimizations && func == mpfr_log)
7834 const REAL_VALUE_TYPE e_truncated =
7835 real_value_truncate (TYPE_MODE (type), dconste);
7836 if (real_dconstp (arg, &e_truncated))
7837 return build_real (type, dconst1);
7840 /* Calculate the result when the argument is a constant. */
7841 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7842 return res;
7844 /* Special case, optimize logN(expN(x)) = x. */
7845 if (flag_unsafe_math_optimizations
7846 && ((func == mpfr_log
7847 && (fcode == BUILT_IN_EXP
7848 || fcode == BUILT_IN_EXPF
7849 || fcode == BUILT_IN_EXPL))
7850 || (func == mpfr_log2
7851 && (fcode == BUILT_IN_EXP2
7852 || fcode == BUILT_IN_EXP2F
7853 || fcode == BUILT_IN_EXP2L))
7854 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7855 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7857 /* Optimize logN(func()) for various exponential functions. We
7858 want to determine the value "x" and the power "exponent" in
7859 order to transform logN(x**exponent) into exponent*logN(x). */
7860 if (flag_unsafe_math_optimizations)
7862 tree exponent = 0, x = 0;
7864 switch (fcode)
7866 CASE_FLT_FN (BUILT_IN_EXP):
7867 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7868 x = build_real (type,
7869 real_value_truncate (TYPE_MODE (type), dconste));
7870 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7871 break;
7872 CASE_FLT_FN (BUILT_IN_EXP2):
7873 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7874 x = build_real (type, dconst2);
7875 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7876 break;
7877 CASE_FLT_FN (BUILT_IN_EXP10):
7878 CASE_FLT_FN (BUILT_IN_POW10):
7879 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7880 x = build_real (type, dconst10);
7881 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7882 break;
7883 CASE_FLT_FN (BUILT_IN_SQRT):
7884 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7885 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7886 exponent = build_real (type, dconsthalf);
7887 break;
7888 CASE_FLT_FN (BUILT_IN_CBRT):
7889 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7890 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7891 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7892 dconstthird));
7893 break;
7894 CASE_FLT_FN (BUILT_IN_POW):
7895 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7896 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7897 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7898 break;
7899 default:
7900 break;
7903 /* Now perform the optimization. */
7904 if (x && exponent)
7906 tree logfn;
7907 arglist = build_tree_list (NULL_TREE, x);
7908 logfn = build_function_call_expr (fndecl, arglist);
7909 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7914 return 0;
7917 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7918 NULL_TREE if no simplification can be made. */
7920 static tree
7921 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7923 tree arg0 = TREE_VALUE (arglist);
7924 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7925 tree res, narg0, narg1;
7927 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7928 return NULL_TREE;
7930 /* Calculate the result when the argument is a constant. */
7931 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7932 return res;
7934 /* If either argument to hypot has a negate or abs, strip that off.
7935 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7936 narg0 = fold_strip_sign_ops (arg0);
7937 narg1 = fold_strip_sign_ops (arg1);
7938 if (narg0 || narg1)
7940 tree narglist = tree_cons (NULL_TREE, narg0 ? narg0 : arg0,
7941 build_tree_list (NULL_TREE,
7942 narg1 ? narg1 : arg1));
7943 return build_function_call_expr (fndecl, narglist);
7946 /* If either argument is zero, hypot is fabs of the other. */
7947 if (real_zerop (arg0))
7948 return fold_build1 (ABS_EXPR, type, arg1);
7949 else if (real_zerop (arg1))
7950 return fold_build1 (ABS_EXPR, type, arg0);
7952 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7953 if (flag_unsafe_math_optimizations
7954 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7956 REAL_VALUE_TYPE sqrt2;
7958 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7959 return fold_build2 (MULT_EXPR, type,
7960 fold_build1 (ABS_EXPR, type, arg0),
7961 build_real (type, sqrt2));
7964 return NULL_TREE;
7968 /* Fold a builtin function call to pow, powf, or powl. Return
7969 NULL_TREE if no simplification can be made. */
7970 static tree
7971 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7973 tree arg0 = TREE_VALUE (arglist);
7974 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7975 tree res;
7977 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7978 return NULL_TREE;
7980 /* Calculate the result when the argument is a constant. */
7981 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7982 return res;
7984 /* Optimize pow(1.0,y) = 1.0. */
7985 if (real_onep (arg0))
7986 return omit_one_operand (type, build_real (type, dconst1), arg1);
7988 if (TREE_CODE (arg1) == REAL_CST
7989 && !TREE_OVERFLOW (arg1))
7991 REAL_VALUE_TYPE cint;
7992 REAL_VALUE_TYPE c;
7993 HOST_WIDE_INT n;
7995 c = TREE_REAL_CST (arg1);
7997 /* Optimize pow(x,0.0) = 1.0. */
7998 if (REAL_VALUES_EQUAL (c, dconst0))
7999 return omit_one_operand (type, build_real (type, dconst1),
8000 arg0);
8002 /* Optimize pow(x,1.0) = x. */
8003 if (REAL_VALUES_EQUAL (c, dconst1))
8004 return arg0;
8006 /* Optimize pow(x,-1.0) = 1.0/x. */
8007 if (REAL_VALUES_EQUAL (c, dconstm1))
8008 return fold_build2 (RDIV_EXPR, type,
8009 build_real (type, dconst1), arg0);
8011 /* Optimize pow(x,0.5) = sqrt(x). */
8012 if (flag_unsafe_math_optimizations
8013 && REAL_VALUES_EQUAL (c, dconsthalf))
8015 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8017 if (sqrtfn != NULL_TREE)
8019 tree arglist = build_tree_list (NULL_TREE, arg0);
8020 return build_function_call_expr (sqrtfn, arglist);
8024 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8025 if (flag_unsafe_math_optimizations)
8027 const REAL_VALUE_TYPE dconstroot
8028 = real_value_truncate (TYPE_MODE (type), dconstthird);
8030 if (REAL_VALUES_EQUAL (c, dconstroot))
8032 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8033 if (cbrtfn != NULL_TREE)
8035 tree arglist = build_tree_list (NULL_TREE, arg0);
8036 return build_function_call_expr (cbrtfn, arglist);
8041 /* Check for an integer exponent. */
8042 n = real_to_integer (&c);
8043 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8044 if (real_identical (&c, &cint))
8046 /* Attempt to evaluate pow at compile-time. */
8047 if (TREE_CODE (arg0) == REAL_CST
8048 && !TREE_OVERFLOW (arg0))
8050 REAL_VALUE_TYPE x;
8051 bool inexact;
8053 x = TREE_REAL_CST (arg0);
8054 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8055 if (flag_unsafe_math_optimizations || !inexact)
8056 return build_real (type, x);
8059 /* Strip sign ops from even integer powers. */
8060 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8062 tree narg0 = fold_strip_sign_ops (arg0);
8063 if (narg0)
8065 arglist = build_tree_list (NULL_TREE, arg1);
8066 arglist = tree_cons (NULL_TREE, narg0, arglist);
8067 return build_function_call_expr (fndecl, arglist);
8073 if (flag_unsafe_math_optimizations)
8075 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8077 /* Optimize pow(expN(x),y) = expN(x*y). */
8078 if (BUILTIN_EXPONENT_P (fcode))
8080 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8081 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8082 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8083 arglist = build_tree_list (NULL_TREE, arg);
8084 return build_function_call_expr (expfn, arglist);
8087 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8088 if (BUILTIN_SQRT_P (fcode))
8090 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8091 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8092 build_real (type, dconsthalf));
8094 arglist = tree_cons (NULL_TREE, narg0,
8095 build_tree_list (NULL_TREE, narg1));
8096 return build_function_call_expr (fndecl, arglist);
8099 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8100 if (BUILTIN_CBRT_P (fcode))
8102 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8103 if (tree_expr_nonnegative_p (arg))
8105 const REAL_VALUE_TYPE dconstroot
8106 = real_value_truncate (TYPE_MODE (type), dconstthird);
8107 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8108 build_real (type, dconstroot));
8109 arglist = tree_cons (NULL_TREE, arg,
8110 build_tree_list (NULL_TREE, narg1));
8111 return build_function_call_expr (fndecl, arglist);
8115 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8116 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
8117 || fcode == BUILT_IN_POWL)
8119 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8120 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8121 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8122 arglist = tree_cons (NULL_TREE, arg00,
8123 build_tree_list (NULL_TREE, narg1));
8124 return build_function_call_expr (fndecl, arglist);
8128 return NULL_TREE;
8131 /* Fold a builtin function call to powi, powif, or powil. Return
8132 NULL_TREE if no simplification can be made. */
8133 static tree
8134 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
8136 tree arg0 = TREE_VALUE (arglist);
8137 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8139 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
8140 return NULL_TREE;
8142 /* Optimize pow(1.0,y) = 1.0. */
8143 if (real_onep (arg0))
8144 return omit_one_operand (type, build_real (type, dconst1), arg1);
8146 if (host_integerp (arg1, 0))
8148 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8150 /* Evaluate powi at compile-time. */
8151 if (TREE_CODE (arg0) == REAL_CST
8152 && !TREE_OVERFLOW (arg0))
8154 REAL_VALUE_TYPE x;
8155 x = TREE_REAL_CST (arg0);
8156 real_powi (&x, TYPE_MODE (type), &x, c);
8157 return build_real (type, x);
8160 /* Optimize pow(x,0) = 1.0. */
8161 if (c == 0)
8162 return omit_one_operand (type, build_real (type, dconst1),
8163 arg0);
8165 /* Optimize pow(x,1) = x. */
8166 if (c == 1)
8167 return arg0;
8169 /* Optimize pow(x,-1) = 1.0/x. */
8170 if (c == -1)
8171 return fold_build2 (RDIV_EXPR, type,
8172 build_real (type, dconst1), arg0);
8175 return NULL_TREE;
8178 /* A subroutine of fold_builtin to fold the various exponent
8179 functions. Return NULL_TREE if no simplification can me made.
8180 FUNC is the corresponding MPFR exponent function. */
8182 static tree
8183 fold_builtin_exponent (tree fndecl, tree arglist,
8184 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8186 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8188 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8189 tree arg = TREE_VALUE (arglist);
8190 tree res;
8192 /* Calculate the result when the argument is a constant. */
8193 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8194 return res;
8196 /* Optimize expN(logN(x)) = x. */
8197 if (flag_unsafe_math_optimizations)
8199 const enum built_in_function fcode = builtin_mathfn_code (arg);
8201 if ((func == mpfr_exp
8202 && (fcode == BUILT_IN_LOG
8203 || fcode == BUILT_IN_LOGF
8204 || fcode == BUILT_IN_LOGL))
8205 || (func == mpfr_exp2
8206 && (fcode == BUILT_IN_LOG2
8207 || fcode == BUILT_IN_LOG2F
8208 || fcode == BUILT_IN_LOG2L))
8209 || (func == mpfr_exp10
8210 && (fcode == BUILT_IN_LOG10
8211 || fcode == BUILT_IN_LOG10F
8212 || fcode == BUILT_IN_LOG10L)))
8213 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8217 return 0;
8220 /* Return true if VAR is a VAR_DECL or a component thereof. */
8222 static bool
8223 var_decl_component_p (tree var)
8225 tree inner = var;
8226 while (handled_component_p (inner))
8227 inner = TREE_OPERAND (inner, 0);
8228 return SSA_VAR_P (inner);
8231 /* Fold function call to builtin memset. Return
8232 NULL_TREE if no simplification can be made. */
8234 static tree
8235 fold_builtin_memset (tree arglist, tree type, bool ignore)
8237 tree dest, c, len, var, ret;
8238 unsigned HOST_WIDE_INT length, cval;
8240 if (!validate_arglist (arglist,
8241 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8242 return 0;
8244 dest = TREE_VALUE (arglist);
8245 c = TREE_VALUE (TREE_CHAIN (arglist));
8246 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8248 if (! host_integerp (len, 1))
8249 return 0;
8251 /* If the LEN parameter is zero, return DEST. */
8252 if (integer_zerop (len))
8253 return omit_one_operand (type, dest, c);
8255 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8256 return 0;
8258 var = dest;
8259 STRIP_NOPS (var);
8260 if (TREE_CODE (var) != ADDR_EXPR)
8261 return 0;
8263 var = TREE_OPERAND (var, 0);
8264 if (TREE_THIS_VOLATILE (var))
8265 return 0;
8267 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8268 && !POINTER_TYPE_P (TREE_TYPE (var)))
8269 return 0;
8271 if (! var_decl_component_p (var))
8272 return 0;
8274 length = tree_low_cst (len, 1);
8275 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8276 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8277 < (int) length)
8278 return 0;
8280 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8281 return 0;
8283 if (integer_zerop (c))
8284 cval = 0;
8285 else
8287 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8288 return 0;
8290 cval = tree_low_cst (c, 1);
8291 cval &= 0xff;
8292 cval |= cval << 8;
8293 cval |= cval << 16;
8294 cval |= (cval << 31) << 1;
8297 ret = build_int_cst_type (TREE_TYPE (var), cval);
8298 ret = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, ret);
8299 if (ignore)
8300 return ret;
8302 return omit_one_operand (type, dest, ret);
8305 /* Fold function call to builtin memset. Return
8306 NULL_TREE if no simplification can be made. */
8308 static tree
8309 fold_builtin_bzero (tree arglist, bool ignore)
8311 tree dest, size, newarglist;
8313 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8314 return 0;
8316 if (!ignore)
8317 return 0;
8319 dest = TREE_VALUE (arglist);
8320 size = TREE_VALUE (TREE_CHAIN (arglist));
8322 /* New argument list transforming bzero(ptr x, int y) to
8323 memset(ptr x, int 0, size_t y). This is done this way
8324 so that if it isn't expanded inline, we fallback to
8325 calling bzero instead of memset. */
8327 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8328 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8329 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8330 return fold_builtin_memset (newarglist, void_type_node, ignore);
8333 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8334 NULL_TREE if no simplification can be made.
8335 If ENDP is 0, return DEST (like memcpy).
8336 If ENDP is 1, return DEST+LEN (like mempcpy).
8337 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8338 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8339 (memmove). */
8341 static tree
8342 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8344 tree dest, src, len, destvar, srcvar, expr;
8346 if (! validate_arglist (arglist,
8347 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8348 return 0;
8350 dest = TREE_VALUE (arglist);
8351 src = TREE_VALUE (TREE_CHAIN (arglist));
8352 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8354 /* If the LEN parameter is zero, return DEST. */
8355 if (integer_zerop (len))
8356 return omit_one_operand (type, dest, src);
8358 /* If SRC and DEST are the same (and not volatile), return
8359 DEST{,+LEN,+LEN-1}. */
8360 if (operand_equal_p (src, dest, 0))
8361 expr = len;
8362 else
8364 tree srctype, desttype;
8365 if (endp == 3)
8367 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8368 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8370 /* Both DEST and SRC must be pointer types.
8371 ??? This is what old code did. Is the testing for pointer types
8372 really mandatory?
8374 If either SRC is readonly or length is 1, we can use memcpy. */
8375 if (dest_align && src_align
8376 && (readonly_data_expr (src)
8377 || (host_integerp (len, 1)
8378 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8379 tree_low_cst (len, 1)))))
8381 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8382 if (!fn)
8383 return 0;
8384 return build_function_call_expr (fn, arglist);
8386 return 0;
8389 if (!host_integerp (len, 0))
8390 return 0;
8391 /* FIXME:
8392 This logic lose for arguments like (type *)malloc (sizeof (type)),
8393 since we strip the casts of up to VOID return value from malloc.
8394 Perhaps we ought to inherit type from non-VOID argument here? */
8395 STRIP_NOPS (src);
8396 STRIP_NOPS (dest);
8397 srctype = TREE_TYPE (TREE_TYPE (src));
8398 desttype = TREE_TYPE (TREE_TYPE (dest));
8399 if (!srctype || !desttype
8400 || !TYPE_SIZE_UNIT (srctype)
8401 || !TYPE_SIZE_UNIT (desttype)
8402 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8403 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8404 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
8405 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8406 return 0;
8408 if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT)
8409 < (int) TYPE_ALIGN (desttype)
8410 || (get_pointer_alignment (src, BIGGEST_ALIGNMENT)
8411 < (int) TYPE_ALIGN (srctype)))
8412 return 0;
8414 if (!ignore)
8415 dest = builtin_save_expr (dest);
8417 srcvar = build_fold_indirect_ref (src);
8418 if (TREE_THIS_VOLATILE (srcvar))
8419 return 0;
8420 if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8421 return 0;
8422 /* With memcpy, it is possible to bypass aliasing rules, so without
8423 this check i. e. execute/20060930-2.c would be misoptimized, because
8424 it use conflicting alias set to hold argument for the memcpy call.
8425 This check is probably unnecesary with -fno-strict-aliasing.
8426 Similarly for destvar. See also PR29286. */
8427 if (!var_decl_component_p (srcvar)
8428 /* Accept: memcpy (*char_var, "test", 1); that simplify
8429 to char_var='t'; */
8430 || is_gimple_min_invariant (srcvar)
8431 || readonly_data_expr (src))
8432 return 0;
8434 destvar = build_fold_indirect_ref (dest);
8435 if (TREE_THIS_VOLATILE (destvar))
8436 return 0;
8437 if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8438 return 0;
8439 if (!var_decl_component_p (destvar))
8440 return 0;
8442 if (srctype == desttype
8443 || (gimple_in_ssa_p (cfun)
8444 && tree_ssa_useless_type_conversion_1 (desttype, srctype)))
8445 expr = srcvar;
8446 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8447 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8448 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8449 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8450 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8451 else
8452 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8453 expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (destvar), destvar, expr);
8456 if (ignore)
8457 return expr;
8459 if (endp == 0 || endp == 3)
8460 return omit_one_operand (type, dest, expr);
8462 if (expr == len)
8463 expr = 0;
8465 if (endp == 2)
8466 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8467 ssize_int (1));
8469 len = fold_convert (TREE_TYPE (dest), len);
8470 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8471 dest = fold_convert (type, dest);
8472 if (expr)
8473 dest = omit_one_operand (type, dest, expr);
8474 return dest;
8477 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8478 simplification can be made. */
8480 static tree
8481 fold_builtin_bcopy (tree arglist, bool ignore)
8483 tree src, dest, size, newarglist;
8485 if (!validate_arglist (arglist,
8486 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8487 return 0;
8489 if (! ignore)
8490 return 0;
8492 src = TREE_VALUE (arglist);
8493 dest = TREE_VALUE (TREE_CHAIN (arglist));
8494 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8496 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8497 memmove(ptr y, ptr x, size_t z). This is done this way
8498 so that if it isn't expanded inline, we fallback to
8499 calling bcopy instead of memmove. */
8501 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8502 newarglist = tree_cons (NULL_TREE, src, newarglist);
8503 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8505 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8508 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8509 the length of the string to be copied. Return NULL_TREE if no
8510 simplification can be made. */
8512 tree
8513 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8515 tree dest, src, fn;
8517 if (!validate_arglist (arglist,
8518 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8519 return 0;
8521 dest = TREE_VALUE (arglist);
8522 src = TREE_VALUE (TREE_CHAIN (arglist));
8524 /* If SRC and DEST are the same (and not volatile), return DEST. */
8525 if (operand_equal_p (src, dest, 0))
8526 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8528 if (optimize_size)
8529 return 0;
8531 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8532 if (!fn)
8533 return 0;
8535 if (!len)
8537 len = c_strlen (src, 1);
8538 if (! len || TREE_SIDE_EFFECTS (len))
8539 return 0;
8542 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8543 arglist = build_tree_list (NULL_TREE, len);
8544 arglist = tree_cons (NULL_TREE, src, arglist);
8545 arglist = tree_cons (NULL_TREE, dest, arglist);
8546 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8547 build_function_call_expr (fn, arglist));
8550 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8551 the length of the source string. Return NULL_TREE if no simplification
8552 can be made. */
8554 tree
8555 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8557 tree dest, src, len, fn;
8559 if (!validate_arglist (arglist,
8560 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8561 return 0;
8563 dest = TREE_VALUE (arglist);
8564 src = TREE_VALUE (TREE_CHAIN (arglist));
8565 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8567 /* If the LEN parameter is zero, return DEST. */
8568 if (integer_zerop (len))
8569 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8571 /* We can't compare slen with len as constants below if len is not a
8572 constant. */
8573 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8574 return 0;
8576 if (!slen)
8577 slen = c_strlen (src, 1);
8579 /* Now, we must be passed a constant src ptr parameter. */
8580 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8581 return 0;
8583 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8585 /* We do not support simplification of this case, though we do
8586 support it when expanding trees into RTL. */
8587 /* FIXME: generate a call to __builtin_memset. */
8588 if (tree_int_cst_lt (slen, len))
8589 return 0;
8591 /* OK transform into builtin memcpy. */
8592 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8593 if (!fn)
8594 return 0;
8595 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8596 build_function_call_expr (fn, arglist));
8599 /* Fold function call to builtin memcmp. Return
8600 NULL_TREE if no simplification can be made. */
8602 static tree
8603 fold_builtin_memcmp (tree arglist)
8605 tree arg1, arg2, len;
8606 const char *p1, *p2;
8608 if (!validate_arglist (arglist,
8609 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8610 return 0;
8612 arg1 = TREE_VALUE (arglist);
8613 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8614 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8616 /* If the LEN parameter is zero, return zero. */
8617 if (integer_zerop (len))
8618 return omit_two_operands (integer_type_node, integer_zero_node,
8619 arg1, arg2);
8621 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8622 if (operand_equal_p (arg1, arg2, 0))
8623 return omit_one_operand (integer_type_node, integer_zero_node, len);
8625 p1 = c_getstr (arg1);
8626 p2 = c_getstr (arg2);
8628 /* If all arguments are constant, and the value of len is not greater
8629 than the lengths of arg1 and arg2, evaluate at compile-time. */
8630 if (host_integerp (len, 1) && p1 && p2
8631 && compare_tree_int (len, strlen (p1) + 1) <= 0
8632 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8634 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8636 if (r > 0)
8637 return integer_one_node;
8638 else if (r < 0)
8639 return integer_minus_one_node;
8640 else
8641 return integer_zero_node;
8644 /* If len parameter is one, return an expression corresponding to
8645 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8646 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8648 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8649 tree cst_uchar_ptr_node
8650 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8652 tree ind1 = fold_convert (integer_type_node,
8653 build1 (INDIRECT_REF, cst_uchar_node,
8654 fold_convert (cst_uchar_ptr_node,
8655 arg1)));
8656 tree ind2 = fold_convert (integer_type_node,
8657 build1 (INDIRECT_REF, cst_uchar_node,
8658 fold_convert (cst_uchar_ptr_node,
8659 arg2)));
8660 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8663 return 0;
8666 /* Fold function call to builtin strcmp. Return
8667 NULL_TREE if no simplification can be made. */
8669 static tree
8670 fold_builtin_strcmp (tree arglist)
8672 tree arg1, arg2;
8673 const char *p1, *p2;
8675 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8676 return 0;
8678 arg1 = TREE_VALUE (arglist);
8679 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8681 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8682 if (operand_equal_p (arg1, arg2, 0))
8683 return integer_zero_node;
8685 p1 = c_getstr (arg1);
8686 p2 = c_getstr (arg2);
8688 if (p1 && p2)
8690 const int i = strcmp (p1, p2);
8691 if (i < 0)
8692 return integer_minus_one_node;
8693 else if (i > 0)
8694 return integer_one_node;
8695 else
8696 return integer_zero_node;
8699 /* If the second arg is "", return *(const unsigned char*)arg1. */
8700 if (p2 && *p2 == '\0')
8702 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8703 tree cst_uchar_ptr_node
8704 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8706 return fold_convert (integer_type_node,
8707 build1 (INDIRECT_REF, cst_uchar_node,
8708 fold_convert (cst_uchar_ptr_node,
8709 arg1)));
8712 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8713 if (p1 && *p1 == '\0')
8715 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8716 tree cst_uchar_ptr_node
8717 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8719 tree temp = fold_convert (integer_type_node,
8720 build1 (INDIRECT_REF, cst_uchar_node,
8721 fold_convert (cst_uchar_ptr_node,
8722 arg2)));
8723 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8726 return 0;
8729 /* Fold function call to builtin strncmp. Return
8730 NULL_TREE if no simplification can be made. */
8732 static tree
8733 fold_builtin_strncmp (tree arglist)
8735 tree arg1, arg2, len;
8736 const char *p1, *p2;
8738 if (!validate_arglist (arglist,
8739 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8740 return 0;
8742 arg1 = TREE_VALUE (arglist);
8743 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8744 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8746 /* If the LEN parameter is zero, return zero. */
8747 if (integer_zerop (len))
8748 return omit_two_operands (integer_type_node, integer_zero_node,
8749 arg1, arg2);
8751 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8752 if (operand_equal_p (arg1, arg2, 0))
8753 return omit_one_operand (integer_type_node, integer_zero_node, len);
8755 p1 = c_getstr (arg1);
8756 p2 = c_getstr (arg2);
8758 if (host_integerp (len, 1) && p1 && p2)
8760 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8761 if (i > 0)
8762 return integer_one_node;
8763 else if (i < 0)
8764 return integer_minus_one_node;
8765 else
8766 return integer_zero_node;
8769 /* If the second arg is "", and the length is greater than zero,
8770 return *(const unsigned char*)arg1. */
8771 if (p2 && *p2 == '\0'
8772 && TREE_CODE (len) == INTEGER_CST
8773 && tree_int_cst_sgn (len) == 1)
8775 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8776 tree cst_uchar_ptr_node
8777 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8779 return fold_convert (integer_type_node,
8780 build1 (INDIRECT_REF, cst_uchar_node,
8781 fold_convert (cst_uchar_ptr_node,
8782 arg1)));
8785 /* If the first arg is "", and the length is greater than zero,
8786 return -*(const unsigned char*)arg2. */
8787 if (p1 && *p1 == '\0'
8788 && TREE_CODE (len) == INTEGER_CST
8789 && tree_int_cst_sgn (len) == 1)
8791 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8792 tree cst_uchar_ptr_node
8793 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8795 tree temp = fold_convert (integer_type_node,
8796 build1 (INDIRECT_REF, cst_uchar_node,
8797 fold_convert (cst_uchar_ptr_node,
8798 arg2)));
8799 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8802 /* If len parameter is one, return an expression corresponding to
8803 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8804 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8806 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8807 tree cst_uchar_ptr_node
8808 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8810 tree ind1 = fold_convert (integer_type_node,
8811 build1 (INDIRECT_REF, cst_uchar_node,
8812 fold_convert (cst_uchar_ptr_node,
8813 arg1)));
8814 tree ind2 = fold_convert (integer_type_node,
8815 build1 (INDIRECT_REF, cst_uchar_node,
8816 fold_convert (cst_uchar_ptr_node,
8817 arg2)));
8818 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8821 return 0;
8824 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8825 NULL_TREE if no simplification can be made. */
8827 static tree
8828 fold_builtin_signbit (tree fndecl, tree arglist)
8830 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8831 tree arg, temp;
8833 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8834 return NULL_TREE;
8836 arg = TREE_VALUE (arglist);
8838 /* If ARG is a compile-time constant, determine the result. */
8839 if (TREE_CODE (arg) == REAL_CST
8840 && !TREE_OVERFLOW (arg))
8842 REAL_VALUE_TYPE c;
8844 c = TREE_REAL_CST (arg);
8845 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8846 return fold_convert (type, temp);
8849 /* If ARG is non-negative, the result is always zero. */
8850 if (tree_expr_nonnegative_p (arg))
8851 return omit_one_operand (type, integer_zero_node, arg);
8853 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8854 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8855 return fold_build2 (LT_EXPR, type, arg,
8856 build_real (TREE_TYPE (arg), dconst0));
8858 return NULL_TREE;
8861 /* Fold function call to builtin copysign, copysignf or copysignl.
8862 Return NULL_TREE if no simplification can be made. */
8864 static tree
8865 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8867 tree arg1, arg2, tem;
8869 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8870 return NULL_TREE;
8872 arg1 = TREE_VALUE (arglist);
8873 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8875 /* copysign(X,X) is X. */
8876 if (operand_equal_p (arg1, arg2, 0))
8877 return fold_convert (type, arg1);
8879 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8880 if (TREE_CODE (arg1) == REAL_CST
8881 && TREE_CODE (arg2) == REAL_CST
8882 && !TREE_OVERFLOW (arg1)
8883 && !TREE_OVERFLOW (arg2))
8885 REAL_VALUE_TYPE c1, c2;
8887 c1 = TREE_REAL_CST (arg1);
8888 c2 = TREE_REAL_CST (arg2);
8889 /* c1.sign := c2.sign. */
8890 real_copysign (&c1, &c2);
8891 return build_real (type, c1);
8894 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8895 Remember to evaluate Y for side-effects. */
8896 if (tree_expr_nonnegative_p (arg2))
8897 return omit_one_operand (type,
8898 fold_build1 (ABS_EXPR, type, arg1),
8899 arg2);
8901 /* Strip sign changing operations for the first argument. */
8902 tem = fold_strip_sign_ops (arg1);
8903 if (tem)
8905 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8906 return build_function_call_expr (fndecl, arglist);
8909 return NULL_TREE;
8912 /* Fold a call to builtin isascii. */
8914 static tree
8915 fold_builtin_isascii (tree arglist)
8917 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8918 return 0;
8919 else
8921 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8922 tree arg = TREE_VALUE (arglist);
8924 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8925 build_int_cst (NULL_TREE,
8926 ~ (unsigned HOST_WIDE_INT) 0x7f));
8927 return fold_build2 (EQ_EXPR, integer_type_node,
8928 arg, integer_zero_node);
8932 /* Fold a call to builtin toascii. */
8934 static tree
8935 fold_builtin_toascii (tree arglist)
8937 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8938 return 0;
8939 else
8941 /* Transform toascii(c) -> (c & 0x7f). */
8942 tree arg = TREE_VALUE (arglist);
8944 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8945 build_int_cst (NULL_TREE, 0x7f));
8949 /* Fold a call to builtin isdigit. */
8951 static tree
8952 fold_builtin_isdigit (tree arglist)
8954 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8955 return 0;
8956 else
8958 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8959 /* According to the C standard, isdigit is unaffected by locale.
8960 However, it definitely is affected by the target character set. */
8961 tree arg;
8962 unsigned HOST_WIDE_INT target_digit0
8963 = lang_hooks.to_target_charset ('0');
8965 if (target_digit0 == 0)
8966 return NULL_TREE;
8968 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8969 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8970 build_int_cst (unsigned_type_node, target_digit0));
8971 return fold_build2 (LE_EXPR, integer_type_node, arg,
8972 build_int_cst (unsigned_type_node, 9));
8976 /* Fold a call to fabs, fabsf or fabsl. */
8978 static tree
8979 fold_builtin_fabs (tree arglist, tree type)
8981 tree arg;
8983 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8984 return 0;
8986 arg = TREE_VALUE (arglist);
8987 arg = fold_convert (type, arg);
8988 if (TREE_CODE (arg) == REAL_CST)
8989 return fold_abs_const (arg, type);
8990 return fold_build1 (ABS_EXPR, type, arg);
8993 /* Fold a call to abs, labs, llabs or imaxabs. */
8995 static tree
8996 fold_builtin_abs (tree arglist, tree type)
8998 tree arg;
9000 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
9001 return 0;
9003 arg = TREE_VALUE (arglist);
9004 arg = fold_convert (type, arg);
9005 if (TREE_CODE (arg) == INTEGER_CST)
9006 return fold_abs_const (arg, type);
9007 return fold_build1 (ABS_EXPR, type, arg);
9010 /* Fold a call to builtin fmin or fmax. */
9012 static tree
9013 fold_builtin_fmin_fmax (tree arglist, tree type, bool max)
9015 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9017 tree arg0 = TREE_VALUE (arglist);
9018 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9019 /* Calculate the result when the argument is a constant. */
9020 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9022 if (res)
9023 return res;
9025 /* If either argument is NaN, return the other one. Avoid the
9026 transformation if we get (and honor) a signalling NaN. Using
9027 omit_one_operand() ensures we create a non-lvalue. */
9028 if (TREE_CODE (arg0) == REAL_CST
9029 && real_isnan (&TREE_REAL_CST (arg0))
9030 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9031 || ! TREE_REAL_CST (arg0).signalling))
9032 return omit_one_operand (type, arg1, arg0);
9033 if (TREE_CODE (arg1) == REAL_CST
9034 && real_isnan (&TREE_REAL_CST (arg1))
9035 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9036 || ! TREE_REAL_CST (arg1).signalling))
9037 return omit_one_operand (type, arg0, arg1);
9039 /* Transform fmin/fmax(x,x) -> x. */
9040 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9041 return omit_one_operand (type, arg0, arg1);
9043 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9044 functions to return the numeric arg if the other one is NaN.
9045 These tree codes don't honor that, so only transform if
9046 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9047 handled, so we don't have to worry about it either. */
9048 if (flag_finite_math_only)
9049 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9050 fold_convert (type, arg0),
9051 fold_convert (type, arg1));
9053 return NULL_TREE;
9056 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9057 EXP is the CALL_EXPR for the call. */
9059 static tree
9060 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
9062 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9063 tree arg;
9064 REAL_VALUE_TYPE r;
9066 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9068 /* Check that we have exactly one argument. */
9069 if (arglist == 0)
9071 error ("too few arguments to function %qs",
9072 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9073 return error_mark_node;
9075 else if (TREE_CHAIN (arglist) != 0)
9077 error ("too many arguments to function %qs",
9078 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9079 return error_mark_node;
9081 else
9083 error ("non-floating-point argument to function %qs",
9084 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9085 return error_mark_node;
9089 arg = TREE_VALUE (arglist);
9090 switch (builtin_index)
9092 case BUILT_IN_ISINF:
9093 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9094 return omit_one_operand (type, integer_zero_node, arg);
9096 if (TREE_CODE (arg) == REAL_CST)
9098 r = TREE_REAL_CST (arg);
9099 if (real_isinf (&r))
9100 return real_compare (GT_EXPR, &r, &dconst0)
9101 ? integer_one_node : integer_minus_one_node;
9102 else
9103 return integer_zero_node;
9106 return NULL_TREE;
9108 case BUILT_IN_FINITE:
9109 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9110 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9111 return omit_one_operand (type, integer_one_node, arg);
9113 if (TREE_CODE (arg) == REAL_CST)
9115 r = TREE_REAL_CST (arg);
9116 return real_isinf (&r) || real_isnan (&r)
9117 ? integer_zero_node : integer_one_node;
9120 return NULL_TREE;
9122 case BUILT_IN_ISNAN:
9123 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9124 return omit_one_operand (type, integer_zero_node, arg);
9126 if (TREE_CODE (arg) == REAL_CST)
9128 r = TREE_REAL_CST (arg);
9129 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9132 arg = builtin_save_expr (arg);
9133 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
9135 default:
9136 gcc_unreachable ();
9140 /* Fold a call to an unordered comparison function such as
9141 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9142 being called and ARGLIST is the argument list for the call.
9143 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9144 the opposite of the desired result. UNORDERED_CODE is used
9145 for modes that can hold NaNs and ORDERED_CODE is used for
9146 the rest. */
9148 static tree
9149 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
9150 enum tree_code unordered_code,
9151 enum tree_code ordered_code)
9153 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9154 enum tree_code code;
9155 tree arg0, arg1;
9156 tree type0, type1;
9157 enum tree_code code0, code1;
9158 tree cmp_type = NULL_TREE;
9160 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9162 /* Check that we have exactly two arguments. */
9163 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
9165 error ("too few arguments to function %qs",
9166 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9167 return error_mark_node;
9169 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
9171 error ("too many arguments to function %qs",
9172 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9173 return error_mark_node;
9177 arg0 = TREE_VALUE (arglist);
9178 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9180 type0 = TREE_TYPE (arg0);
9181 type1 = TREE_TYPE (arg1);
9183 code0 = TREE_CODE (type0);
9184 code1 = TREE_CODE (type1);
9186 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9187 /* Choose the wider of two real types. */
9188 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9189 ? type0 : type1;
9190 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9191 cmp_type = type0;
9192 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9193 cmp_type = type1;
9194 else
9196 error ("non-floating-point argument to function %qs",
9197 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9198 return error_mark_node;
9201 arg0 = fold_convert (cmp_type, arg0);
9202 arg1 = fold_convert (cmp_type, arg1);
9204 if (unordered_code == UNORDERED_EXPR)
9206 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9207 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9208 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9211 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9212 : ordered_code;
9213 return fold_build1 (TRUTH_NOT_EXPR, type,
9214 fold_build2 (code, type, arg0, arg1));
9217 /* Used by constant folding to simplify calls to builtin functions. EXP is
9218 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9219 result of the function call is ignored. This function returns NULL_TREE
9220 if no simplification was possible. */
9222 static tree
9223 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9225 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9226 enum built_in_function fcode;
9228 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9229 return targetm.fold_builtin (fndecl, arglist, ignore);
9231 fcode = DECL_FUNCTION_CODE (fndecl);
9232 switch (fcode)
9234 case BUILT_IN_FPUTS:
9235 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9237 case BUILT_IN_FPUTS_UNLOCKED:
9238 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9240 case BUILT_IN_STRSTR:
9241 return fold_builtin_strstr (arglist, type);
9243 case BUILT_IN_STRCAT:
9244 return fold_builtin_strcat (arglist);
9246 case BUILT_IN_STRNCAT:
9247 return fold_builtin_strncat (arglist);
9249 case BUILT_IN_STRSPN:
9250 return fold_builtin_strspn (arglist);
9252 case BUILT_IN_STRCSPN:
9253 return fold_builtin_strcspn (arglist);
9255 case BUILT_IN_STRCHR:
9256 case BUILT_IN_INDEX:
9257 return fold_builtin_strchr (arglist, type);
9259 case BUILT_IN_STRRCHR:
9260 case BUILT_IN_RINDEX:
9261 return fold_builtin_strrchr (arglist, type);
9263 case BUILT_IN_STRCPY:
9264 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9266 case BUILT_IN_STRNCPY:
9267 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9269 case BUILT_IN_STRCMP:
9270 return fold_builtin_strcmp (arglist);
9272 case BUILT_IN_STRNCMP:
9273 return fold_builtin_strncmp (arglist);
9275 case BUILT_IN_STRPBRK:
9276 return fold_builtin_strpbrk (arglist, type);
9278 case BUILT_IN_BCMP:
9279 case BUILT_IN_MEMCMP:
9280 return fold_builtin_memcmp (arglist);
9282 case BUILT_IN_SPRINTF:
9283 return fold_builtin_sprintf (arglist, ignore);
9285 case BUILT_IN_CONSTANT_P:
9287 tree val;
9289 val = fold_builtin_constant_p (arglist);
9290 /* Gimplification will pull the CALL_EXPR for the builtin out of
9291 an if condition. When not optimizing, we'll not CSE it back.
9292 To avoid link error types of regressions, return false now. */
9293 if (!val && !optimize)
9294 val = integer_zero_node;
9296 return val;
9299 case BUILT_IN_EXPECT:
9300 return fold_builtin_expect (arglist);
9302 case BUILT_IN_CLASSIFY_TYPE:
9303 return fold_builtin_classify_type (arglist);
9305 case BUILT_IN_STRLEN:
9306 return fold_builtin_strlen (arglist);
9308 CASE_FLT_FN (BUILT_IN_FABS):
9309 return fold_builtin_fabs (arglist, type);
9311 case BUILT_IN_ABS:
9312 case BUILT_IN_LABS:
9313 case BUILT_IN_LLABS:
9314 case BUILT_IN_IMAXABS:
9315 return fold_builtin_abs (arglist, type);
9317 CASE_FLT_FN (BUILT_IN_CONJ):
9318 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9319 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9320 break;
9322 CASE_FLT_FN (BUILT_IN_CREAL):
9323 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9324 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9325 TREE_VALUE (arglist)));
9326 break;
9328 CASE_FLT_FN (BUILT_IN_CIMAG):
9329 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9330 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9331 TREE_VALUE (arglist)));
9332 break;
9334 CASE_FLT_FN (BUILT_IN_CABS):
9335 return fold_builtin_cabs (arglist, type, fndecl);
9337 CASE_FLT_FN (BUILT_IN_SQRT):
9338 return fold_builtin_sqrt (arglist, type);
9340 CASE_FLT_FN (BUILT_IN_CBRT):
9341 return fold_builtin_cbrt (arglist, type);
9343 CASE_FLT_FN (BUILT_IN_ASIN):
9344 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9345 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9346 &dconstm1, &dconst1, true);
9347 break;
9349 CASE_FLT_FN (BUILT_IN_ACOS):
9350 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9351 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9352 &dconstm1, &dconst1, true);
9353 break;
9355 CASE_FLT_FN (BUILT_IN_ATAN):
9356 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9357 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9358 NULL, NULL, 0);
9359 break;
9361 CASE_FLT_FN (BUILT_IN_ASINH):
9362 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9363 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9364 NULL, NULL, 0);
9365 break;
9367 CASE_FLT_FN (BUILT_IN_ACOSH):
9368 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9369 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9370 &dconst1, NULL, true);
9371 break;
9373 CASE_FLT_FN (BUILT_IN_ATANH):
9374 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9375 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9376 &dconstm1, &dconst1, false);
9377 break;
9379 CASE_FLT_FN (BUILT_IN_SIN):
9380 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9381 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9382 NULL, NULL, 0);
9383 break;
9385 CASE_FLT_FN (BUILT_IN_COS):
9386 return fold_builtin_cos (arglist, type, fndecl);
9388 CASE_FLT_FN (BUILT_IN_TAN):
9389 return fold_builtin_tan (arglist, type);
9391 CASE_FLT_FN (BUILT_IN_SINCOS):
9392 return fold_builtin_sincos (arglist);
9394 CASE_FLT_FN (BUILT_IN_CEXP):
9395 return fold_builtin_cexp (arglist, type);
9397 CASE_FLT_FN (BUILT_IN_CEXPI):
9398 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9399 return do_mpfr_sincos (TREE_VALUE (arglist), NULL_TREE, NULL_TREE);
9401 CASE_FLT_FN (BUILT_IN_SINH):
9402 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9403 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9404 NULL, NULL, 0);
9405 break;
9407 CASE_FLT_FN (BUILT_IN_COSH):
9408 return fold_builtin_cosh (arglist, type, fndecl);
9410 CASE_FLT_FN (BUILT_IN_TANH):
9411 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9412 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9413 NULL, NULL, 0);
9414 break;
9416 CASE_FLT_FN (BUILT_IN_ERF):
9417 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9418 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9419 NULL, NULL, 0);
9420 break;
9422 CASE_FLT_FN (BUILT_IN_ERFC):
9423 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9424 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9425 NULL, NULL, 0);
9426 break;
9428 CASE_FLT_FN (BUILT_IN_TGAMMA):
9429 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9430 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9431 NULL, NULL, 0);
9432 break;
9434 CASE_FLT_FN (BUILT_IN_EXP):
9435 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9437 CASE_FLT_FN (BUILT_IN_EXP2):
9438 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9440 CASE_FLT_FN (BUILT_IN_EXP10):
9441 CASE_FLT_FN (BUILT_IN_POW10):
9442 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9444 CASE_FLT_FN (BUILT_IN_EXPM1):
9445 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9446 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9447 NULL, NULL, 0);
9448 break;
9450 CASE_FLT_FN (BUILT_IN_LOG):
9451 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9453 CASE_FLT_FN (BUILT_IN_LOG2):
9454 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9456 CASE_FLT_FN (BUILT_IN_LOG10):
9457 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9459 CASE_FLT_FN (BUILT_IN_LOG1P):
9460 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9461 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9462 &dconstm1, NULL, false);
9463 break;
9465 CASE_FLT_FN (BUILT_IN_ATAN2):
9466 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9467 return do_mpfr_arg2 (TREE_VALUE (arglist),
9468 TREE_VALUE (TREE_CHAIN (arglist)),
9469 type, mpfr_atan2);
9470 break;
9472 CASE_FLT_FN (BUILT_IN_FDIM):
9473 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9474 return do_mpfr_arg2 (TREE_VALUE (arglist),
9475 TREE_VALUE (TREE_CHAIN (arglist)),
9476 type, mpfr_dim);
9477 break;
9479 CASE_FLT_FN (BUILT_IN_FMA):
9480 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9481 return do_mpfr_arg3 (TREE_VALUE (arglist),
9482 TREE_VALUE (TREE_CHAIN (arglist)),
9483 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
9484 type, mpfr_fma);
9485 break;
9487 CASE_FLT_FN (BUILT_IN_FMIN):
9488 return fold_builtin_fmin_fmax (arglist, type, /*max=*/false);
9490 CASE_FLT_FN (BUILT_IN_FMAX):
9491 return fold_builtin_fmin_fmax (arglist, type, /*max=*/true);
9493 CASE_FLT_FN (BUILT_IN_HYPOT):
9494 return fold_builtin_hypot (fndecl, arglist, type);
9496 CASE_FLT_FN (BUILT_IN_POW):
9497 return fold_builtin_pow (fndecl, arglist, type);
9499 CASE_FLT_FN (BUILT_IN_POWI):
9500 return fold_builtin_powi (fndecl, arglist, type);
9502 CASE_FLT_FN (BUILT_IN_INF):
9503 case BUILT_IN_INFD32:
9504 case BUILT_IN_INFD64:
9505 case BUILT_IN_INFD128:
9506 return fold_builtin_inf (type, true);
9508 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9509 return fold_builtin_inf (type, false);
9511 CASE_FLT_FN (BUILT_IN_NAN):
9512 case BUILT_IN_NAND32:
9513 case BUILT_IN_NAND64:
9514 case BUILT_IN_NAND128:
9515 return fold_builtin_nan (arglist, type, true);
9517 CASE_FLT_FN (BUILT_IN_NANS):
9518 return fold_builtin_nan (arglist, type, false);
9520 CASE_FLT_FN (BUILT_IN_FLOOR):
9521 return fold_builtin_floor (fndecl, arglist);
9523 CASE_FLT_FN (BUILT_IN_CEIL):
9524 return fold_builtin_ceil (fndecl, arglist);
9526 CASE_FLT_FN (BUILT_IN_TRUNC):
9527 return fold_builtin_trunc (fndecl, arglist);
9529 CASE_FLT_FN (BUILT_IN_ROUND):
9530 return fold_builtin_round (fndecl, arglist);
9532 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9533 CASE_FLT_FN (BUILT_IN_RINT):
9534 return fold_trunc_transparent_mathfn (fndecl, arglist);
9536 CASE_FLT_FN (BUILT_IN_LCEIL):
9537 CASE_FLT_FN (BUILT_IN_LLCEIL):
9538 CASE_FLT_FN (BUILT_IN_LFLOOR):
9539 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9540 CASE_FLT_FN (BUILT_IN_LROUND):
9541 CASE_FLT_FN (BUILT_IN_LLROUND):
9542 return fold_builtin_int_roundingfn (fndecl, arglist);
9544 CASE_FLT_FN (BUILT_IN_LRINT):
9545 CASE_FLT_FN (BUILT_IN_LLRINT):
9546 return fold_fixed_mathfn (fndecl, arglist);
9548 case BUILT_IN_BSWAP32:
9549 case BUILT_IN_BSWAP64:
9550 return fold_builtin_bswap (fndecl, arglist);
9552 CASE_INT_FN (BUILT_IN_FFS):
9553 CASE_INT_FN (BUILT_IN_CLZ):
9554 CASE_INT_FN (BUILT_IN_CTZ):
9555 CASE_INT_FN (BUILT_IN_POPCOUNT):
9556 CASE_INT_FN (BUILT_IN_PARITY):
9557 return fold_builtin_bitop (fndecl, arglist);
9559 case BUILT_IN_MEMSET:
9560 return fold_builtin_memset (arglist, type, ignore);
9562 case BUILT_IN_MEMCPY:
9563 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9565 case BUILT_IN_MEMPCPY:
9566 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9568 case BUILT_IN_MEMMOVE:
9569 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9571 case BUILT_IN_BZERO:
9572 return fold_builtin_bzero (arglist, ignore);
9574 case BUILT_IN_BCOPY:
9575 return fold_builtin_bcopy (arglist, ignore);
9577 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9578 return fold_builtin_signbit (fndecl, arglist);
9580 case BUILT_IN_ISASCII:
9581 return fold_builtin_isascii (arglist);
9583 case BUILT_IN_TOASCII:
9584 return fold_builtin_toascii (arglist);
9586 case BUILT_IN_ISDIGIT:
9587 return fold_builtin_isdigit (arglist);
9589 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9590 return fold_builtin_copysign (fndecl, arglist, type);
9592 CASE_FLT_FN (BUILT_IN_FINITE):
9593 case BUILT_IN_FINITED32:
9594 case BUILT_IN_FINITED64:
9595 case BUILT_IN_FINITED128:
9596 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9598 CASE_FLT_FN (BUILT_IN_ISINF):
9599 case BUILT_IN_ISINFD32:
9600 case BUILT_IN_ISINFD64:
9601 case BUILT_IN_ISINFD128:
9602 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9604 CASE_FLT_FN (BUILT_IN_ISNAN):
9605 case BUILT_IN_ISNAND32:
9606 case BUILT_IN_ISNAND64:
9607 case BUILT_IN_ISNAND128:
9608 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9610 case BUILT_IN_ISGREATER:
9611 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9612 case BUILT_IN_ISGREATEREQUAL:
9613 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9614 case BUILT_IN_ISLESS:
9615 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9616 case BUILT_IN_ISLESSEQUAL:
9617 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9618 case BUILT_IN_ISLESSGREATER:
9619 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9620 case BUILT_IN_ISUNORDERED:
9621 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9622 NOP_EXPR);
9624 /* We do the folding for va_start in the expander. */
9625 case BUILT_IN_VA_START:
9626 break;
9628 case BUILT_IN_OBJECT_SIZE:
9629 return fold_builtin_object_size (arglist);
9630 case BUILT_IN_MEMCPY_CHK:
9631 case BUILT_IN_MEMPCPY_CHK:
9632 case BUILT_IN_MEMMOVE_CHK:
9633 case BUILT_IN_MEMSET_CHK:
9634 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9635 DECL_FUNCTION_CODE (fndecl));
9636 case BUILT_IN_STRCPY_CHK:
9637 case BUILT_IN_STPCPY_CHK:
9638 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9639 DECL_FUNCTION_CODE (fndecl));
9640 case BUILT_IN_STRNCPY_CHK:
9641 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9642 case BUILT_IN_STRCAT_CHK:
9643 return fold_builtin_strcat_chk (fndecl, arglist);
9644 case BUILT_IN_STRNCAT_CHK:
9645 return fold_builtin_strncat_chk (fndecl, arglist);
9646 case BUILT_IN_SPRINTF_CHK:
9647 case BUILT_IN_VSPRINTF_CHK:
9648 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9649 case BUILT_IN_SNPRINTF_CHK:
9650 case BUILT_IN_VSNPRINTF_CHK:
9651 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9652 DECL_FUNCTION_CODE (fndecl));
9654 case BUILT_IN_PRINTF:
9655 case BUILT_IN_PRINTF_UNLOCKED:
9656 case BUILT_IN_VPRINTF:
9657 case BUILT_IN_PRINTF_CHK:
9658 case BUILT_IN_VPRINTF_CHK:
9659 return fold_builtin_printf (fndecl, arglist, ignore,
9660 DECL_FUNCTION_CODE (fndecl));
9662 case BUILT_IN_FPRINTF:
9663 case BUILT_IN_FPRINTF_UNLOCKED:
9664 case BUILT_IN_VFPRINTF:
9665 case BUILT_IN_FPRINTF_CHK:
9666 case BUILT_IN_VFPRINTF_CHK:
9667 return fold_builtin_fprintf (fndecl, arglist, ignore,
9668 DECL_FUNCTION_CODE (fndecl));
9670 default:
9671 break;
9674 return 0;
9677 /* A wrapper function for builtin folding that prevents warnings for
9678 "statement without effect" and the like, caused by removing the
9679 call node earlier than the warning is generated. */
9681 tree
9682 fold_builtin (tree fndecl, tree arglist, bool ignore)
9684 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9685 if (exp && !ignore)
9687 exp = build1 (NOP_EXPR, GENERIC_TREE_TYPE (exp), exp);
9688 TREE_NO_WARNING (exp) = 1;
9691 return exp;
9694 /* Conveniently construct a function call expression. */
9696 tree
9697 build_function_call_expr (tree fn, tree arglist)
9699 tree call_expr;
9701 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9702 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9703 call_expr, arglist, NULL_TREE);
9706 /* This function validates the types of a function call argument list
9707 represented as a tree chain of parameters against a specified list
9708 of tree_codes. If the last specifier is a 0, that represents an
9709 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9712 validate_arglist (tree arglist, ...)
9714 enum tree_code code;
9715 int res = 0;
9716 va_list ap;
9718 va_start (ap, arglist);
9722 code = va_arg (ap, enum tree_code);
9723 switch (code)
9725 case 0:
9726 /* This signifies an ellipses, any further arguments are all ok. */
9727 res = 1;
9728 goto end;
9729 case VOID_TYPE:
9730 /* This signifies an endlink, if no arguments remain, return
9731 true, otherwise return false. */
9732 res = arglist == 0;
9733 goto end;
9734 default:
9735 /* If no parameters remain or the parameter's code does not
9736 match the specified code, return false. Otherwise continue
9737 checking any remaining arguments. */
9738 if (arglist == 0)
9739 goto end;
9740 if (code == POINTER_TYPE)
9742 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9743 goto end;
9745 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9746 goto end;
9747 break;
9749 arglist = TREE_CHAIN (arglist);
9751 while (1);
9753 /* We need gotos here since we can only have one VA_CLOSE in a
9754 function. */
9755 end: ;
9756 va_end (ap);
9758 return res;
9761 /* Default target-specific builtin expander that does nothing. */
9764 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9765 rtx target ATTRIBUTE_UNUSED,
9766 rtx subtarget ATTRIBUTE_UNUSED,
9767 enum machine_mode mode ATTRIBUTE_UNUSED,
9768 int ignore ATTRIBUTE_UNUSED)
9770 return NULL_RTX;
9773 /* Returns true is EXP represents data that would potentially reside
9774 in a readonly section. */
9776 static bool
9777 readonly_data_expr (tree exp)
9779 STRIP_NOPS (exp);
9781 if (TREE_CODE (exp) != ADDR_EXPR)
9782 return false;
9784 exp = get_base_address (TREE_OPERAND (exp, 0));
9785 if (!exp)
9786 return false;
9788 /* Make sure we call decl_readonly_section only for trees it
9789 can handle (since it returns true for everything it doesn't
9790 understand). */
9791 if (TREE_CODE (exp) == STRING_CST
9792 || TREE_CODE (exp) == CONSTRUCTOR
9793 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9794 return decl_readonly_section (exp, 0);
9795 else
9796 return false;
9799 /* Simplify a call to the strstr builtin.
9801 Return 0 if no simplification was possible, otherwise return the
9802 simplified form of the call as a tree.
9804 The simplified form may be a constant or other expression which
9805 computes the same value, but in a more efficient manner (including
9806 calls to other builtin functions).
9808 The call may contain arguments which need to be evaluated, but
9809 which are not useful to determine the result of the call. In
9810 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9811 COMPOUND_EXPR will be an argument which must be evaluated.
9812 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9813 COMPOUND_EXPR in the chain will contain the tree for the simplified
9814 form of the builtin function call. */
9816 static tree
9817 fold_builtin_strstr (tree arglist, tree type)
9819 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9820 return 0;
9821 else
9823 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9824 tree fn;
9825 const char *p1, *p2;
9827 p2 = c_getstr (s2);
9828 if (p2 == NULL)
9829 return 0;
9831 p1 = c_getstr (s1);
9832 if (p1 != NULL)
9834 const char *r = strstr (p1, p2);
9835 tree tem;
9837 if (r == NULL)
9838 return build_int_cst (TREE_TYPE (s1), 0);
9840 /* Return an offset into the constant string argument. */
9841 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9842 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9843 return fold_convert (type, tem);
9846 /* The argument is const char *, and the result is char *, so we need
9847 a type conversion here to avoid a warning. */
9848 if (p2[0] == '\0')
9849 return fold_convert (type, s1);
9851 if (p2[1] != '\0')
9852 return 0;
9854 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9855 if (!fn)
9856 return 0;
9858 /* New argument list transforming strstr(s1, s2) to
9859 strchr(s1, s2[0]). */
9860 arglist = build_tree_list (NULL_TREE,
9861 build_int_cst (NULL_TREE, p2[0]));
9862 arglist = tree_cons (NULL_TREE, s1, arglist);
9863 return build_function_call_expr (fn, arglist);
9867 /* Simplify a call to the strchr builtin.
9869 Return 0 if no simplification was possible, otherwise return the
9870 simplified form of the call as a tree.
9872 The simplified form may be a constant or other expression which
9873 computes the same value, but in a more efficient manner (including
9874 calls to other builtin functions).
9876 The call may contain arguments which need to be evaluated, but
9877 which are not useful to determine the result of the call. In
9878 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9879 COMPOUND_EXPR will be an argument which must be evaluated.
9880 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9881 COMPOUND_EXPR in the chain will contain the tree for the simplified
9882 form of the builtin function call. */
9884 static tree
9885 fold_builtin_strchr (tree arglist, tree type)
9887 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9888 return 0;
9889 else
9891 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9892 const char *p1;
9894 if (TREE_CODE (s2) != INTEGER_CST)
9895 return 0;
9897 p1 = c_getstr (s1);
9898 if (p1 != NULL)
9900 char c;
9901 const char *r;
9902 tree tem;
9904 if (target_char_cast (s2, &c))
9905 return 0;
9907 r = strchr (p1, c);
9909 if (r == NULL)
9910 return build_int_cst (TREE_TYPE (s1), 0);
9912 /* Return an offset into the constant string argument. */
9913 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9914 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9915 return fold_convert (type, tem);
9917 return 0;
9921 /* Simplify a call to the strrchr builtin.
9923 Return 0 if no simplification was possible, otherwise return the
9924 simplified form of the call as a tree.
9926 The simplified form may be a constant or other expression which
9927 computes the same value, but in a more efficient manner (including
9928 calls to other builtin functions).
9930 The call may contain arguments which need to be evaluated, but
9931 which are not useful to determine the result of the call. In
9932 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9933 COMPOUND_EXPR will be an argument which must be evaluated.
9934 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9935 COMPOUND_EXPR in the chain will contain the tree for the simplified
9936 form of the builtin function call. */
9938 static tree
9939 fold_builtin_strrchr (tree arglist, tree type)
9941 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9942 return 0;
9943 else
9945 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9946 tree fn;
9947 const char *p1;
9949 if (TREE_CODE (s2) != INTEGER_CST)
9950 return 0;
9952 p1 = c_getstr (s1);
9953 if (p1 != NULL)
9955 char c;
9956 const char *r;
9957 tree tem;
9959 if (target_char_cast (s2, &c))
9960 return 0;
9962 r = strrchr (p1, c);
9964 if (r == NULL)
9965 return build_int_cst (TREE_TYPE (s1), 0);
9967 /* Return an offset into the constant string argument. */
9968 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9969 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9970 return fold_convert (type, tem);
9973 if (! integer_zerop (s2))
9974 return 0;
9976 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9977 if (!fn)
9978 return 0;
9980 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9981 return build_function_call_expr (fn, arglist);
9985 /* Simplify a call to the strpbrk builtin.
9987 Return 0 if no simplification was possible, otherwise return the
9988 simplified form of the call as a tree.
9990 The simplified form may be a constant or other expression which
9991 computes the same value, but in a more efficient manner (including
9992 calls to other builtin functions).
9994 The call may contain arguments which need to be evaluated, but
9995 which are not useful to determine the result of the call. In
9996 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9997 COMPOUND_EXPR will be an argument which must be evaluated.
9998 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9999 COMPOUND_EXPR in the chain will contain the tree for the simplified
10000 form of the builtin function call. */
10002 static tree
10003 fold_builtin_strpbrk (tree arglist, tree type)
10005 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10006 return 0;
10007 else
10009 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10010 tree fn;
10011 const char *p1, *p2;
10013 p2 = c_getstr (s2);
10014 if (p2 == NULL)
10015 return 0;
10017 p1 = c_getstr (s1);
10018 if (p1 != NULL)
10020 const char *r = strpbrk (p1, p2);
10021 tree tem;
10023 if (r == NULL)
10024 return build_int_cst (TREE_TYPE (s1), 0);
10026 /* Return an offset into the constant string argument. */
10027 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
10028 s1, build_int_cst (TREE_TYPE (s1), r - p1));
10029 return fold_convert (type, tem);
10032 if (p2[0] == '\0')
10033 /* strpbrk(x, "") == NULL.
10034 Evaluate and ignore s1 in case it had side-effects. */
10035 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
10037 if (p2[1] != '\0')
10038 return 0; /* Really call strpbrk. */
10040 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10041 if (!fn)
10042 return 0;
10044 /* New argument list transforming strpbrk(s1, s2) to
10045 strchr(s1, s2[0]). */
10046 arglist = build_tree_list (NULL_TREE,
10047 build_int_cst (NULL_TREE, p2[0]));
10048 arglist = tree_cons (NULL_TREE, s1, arglist);
10049 return build_function_call_expr (fn, arglist);
10053 /* Simplify a call to the strcat builtin.
10055 Return 0 if no simplification was possible, otherwise return the
10056 simplified form of the call as a tree.
10058 The simplified form may be a constant or other expression which
10059 computes the same value, but in a more efficient manner (including
10060 calls to other builtin functions).
10062 The call may contain arguments which need to be evaluated, but
10063 which are not useful to determine the result of the call. In
10064 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10065 COMPOUND_EXPR will be an argument which must be evaluated.
10066 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10067 COMPOUND_EXPR in the chain will contain the tree for the simplified
10068 form of the builtin function call. */
10070 static tree
10071 fold_builtin_strcat (tree arglist)
10073 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10074 return 0;
10075 else
10077 tree dst = TREE_VALUE (arglist),
10078 src = TREE_VALUE (TREE_CHAIN (arglist));
10079 const char *p = c_getstr (src);
10081 /* If the string length is zero, return the dst parameter. */
10082 if (p && *p == '\0')
10083 return dst;
10085 return 0;
10089 /* Simplify a call to the strncat builtin.
10091 Return 0 if no simplification was possible, otherwise return the
10092 simplified form of the call as a tree.
10094 The simplified form may be a constant or other expression which
10095 computes the same value, but in a more efficient manner (including
10096 calls to other builtin functions).
10098 The call may contain arguments which need to be evaluated, but
10099 which are not useful to determine the result of the call. In
10100 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10101 COMPOUND_EXPR will be an argument which must be evaluated.
10102 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10103 COMPOUND_EXPR in the chain will contain the tree for the simplified
10104 form of the builtin function call. */
10106 static tree
10107 fold_builtin_strncat (tree arglist)
10109 if (!validate_arglist (arglist,
10110 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10111 return 0;
10112 else
10114 tree dst = TREE_VALUE (arglist);
10115 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10116 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10117 const char *p = c_getstr (src);
10119 /* If the requested length is zero, or the src parameter string
10120 length is zero, return the dst parameter. */
10121 if (integer_zerop (len) || (p && *p == '\0'))
10122 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
10124 /* If the requested len is greater than or equal to the string
10125 length, call strcat. */
10126 if (TREE_CODE (len) == INTEGER_CST && p
10127 && compare_tree_int (len, strlen (p)) >= 0)
10129 tree newarglist
10130 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
10131 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
10133 /* If the replacement _DECL isn't initialized, don't do the
10134 transformation. */
10135 if (!fn)
10136 return 0;
10138 return build_function_call_expr (fn, newarglist);
10140 return 0;
10144 /* Simplify a call to the strspn builtin.
10146 Return 0 if no simplification was possible, otherwise return the
10147 simplified form of the call as a tree.
10149 The simplified form may be a constant or other expression which
10150 computes the same value, but in a more efficient manner (including
10151 calls to other builtin functions).
10153 The call may contain arguments which need to be evaluated, but
10154 which are not useful to determine the result of the call. In
10155 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10156 COMPOUND_EXPR will be an argument which must be evaluated.
10157 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10158 COMPOUND_EXPR in the chain will contain the tree for the simplified
10159 form of the builtin function call. */
10161 static tree
10162 fold_builtin_strspn (tree arglist)
10164 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10165 return 0;
10166 else
10168 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10169 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10171 /* If both arguments are constants, evaluate at compile-time. */
10172 if (p1 && p2)
10174 const size_t r = strspn (p1, p2);
10175 return size_int (r);
10178 /* If either argument is "", return 0. */
10179 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10180 /* Evaluate and ignore both arguments in case either one has
10181 side-effects. */
10182 return omit_two_operands (integer_type_node, integer_zero_node,
10183 s1, s2);
10184 return 0;
10188 /* Simplify a call to the strcspn builtin.
10190 Return 0 if no simplification was possible, otherwise return the
10191 simplified form of the call as a tree.
10193 The simplified form may be a constant or other expression which
10194 computes the same value, but in a more efficient manner (including
10195 calls to other builtin functions).
10197 The call may contain arguments which need to be evaluated, but
10198 which are not useful to determine the result of the call. In
10199 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10200 COMPOUND_EXPR will be an argument which must be evaluated.
10201 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10202 COMPOUND_EXPR in the chain will contain the tree for the simplified
10203 form of the builtin function call. */
10205 static tree
10206 fold_builtin_strcspn (tree arglist)
10208 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10209 return 0;
10210 else
10212 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10213 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10215 /* If both arguments are constants, evaluate at compile-time. */
10216 if (p1 && p2)
10218 const size_t r = strcspn (p1, p2);
10219 return size_int (r);
10222 /* If the first argument is "", return 0. */
10223 if (p1 && *p1 == '\0')
10225 /* Evaluate and ignore argument s2 in case it has
10226 side-effects. */
10227 return omit_one_operand (integer_type_node,
10228 integer_zero_node, s2);
10231 /* If the second argument is "", return __builtin_strlen(s1). */
10232 if (p2 && *p2 == '\0')
10234 tree newarglist = build_tree_list (NULL_TREE, s1),
10235 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10237 /* If the replacement _DECL isn't initialized, don't do the
10238 transformation. */
10239 if (!fn)
10240 return 0;
10242 return build_function_call_expr (fn, newarglist);
10244 return 0;
10248 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10249 by the builtin will be ignored. UNLOCKED is true is true if this
10250 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10251 the known length of the string. Return NULL_TREE if no simplification
10252 was possible. */
10254 tree
10255 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10257 tree fn;
10258 /* If we're using an unlocked function, assume the other unlocked
10259 functions exist explicitly. */
10260 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10261 : implicit_built_in_decls[BUILT_IN_FPUTC];
10262 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10263 : implicit_built_in_decls[BUILT_IN_FWRITE];
10265 /* If the return value is used, don't do the transformation. */
10266 if (!ignore)
10267 return 0;
10269 /* Verify the arguments in the original call. */
10270 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10271 return 0;
10273 if (! len)
10274 len = c_strlen (TREE_VALUE (arglist), 0);
10276 /* Get the length of the string passed to fputs. If the length
10277 can't be determined, punt. */
10278 if (!len
10279 || TREE_CODE (len) != INTEGER_CST)
10280 return 0;
10282 switch (compare_tree_int (len, 1))
10284 case -1: /* length is 0, delete the call entirely . */
10285 return omit_one_operand (integer_type_node, integer_zero_node,
10286 TREE_VALUE (TREE_CHAIN (arglist)));
10288 case 0: /* length is 1, call fputc. */
10290 const char *p = c_getstr (TREE_VALUE (arglist));
10292 if (p != NULL)
10294 /* New argument list transforming fputs(string, stream) to
10295 fputc(string[0], stream). */
10296 arglist = build_tree_list (NULL_TREE,
10297 TREE_VALUE (TREE_CHAIN (arglist)));
10298 arglist = tree_cons (NULL_TREE,
10299 build_int_cst (NULL_TREE, p[0]),
10300 arglist);
10301 fn = fn_fputc;
10302 break;
10305 /* FALLTHROUGH */
10306 case 1: /* length is greater than 1, call fwrite. */
10308 tree string_arg;
10310 /* If optimizing for size keep fputs. */
10311 if (optimize_size)
10312 return 0;
10313 string_arg = TREE_VALUE (arglist);
10314 /* New argument list transforming fputs(string, stream) to
10315 fwrite(string, 1, len, stream). */
10316 arglist = build_tree_list (NULL_TREE,
10317 TREE_VALUE (TREE_CHAIN (arglist)));
10318 arglist = tree_cons (NULL_TREE, len, arglist);
10319 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10320 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10321 fn = fn_fwrite;
10322 break;
10324 default:
10325 gcc_unreachable ();
10328 /* If the replacement _DECL isn't initialized, don't do the
10329 transformation. */
10330 if (!fn)
10331 return 0;
10333 /* These optimizations are only performed when the result is ignored,
10334 hence there's no need to cast the result to integer_type_node. */
10335 return build_function_call_expr (fn, arglist);
10338 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10339 produced. False otherwise. This is done so that we don't output the error
10340 or warning twice or three times. */
10341 bool
10342 fold_builtin_next_arg (tree arglist)
10344 tree fntype = TREE_TYPE (current_function_decl);
10346 if (TYPE_ARG_TYPES (fntype) == 0
10347 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10348 == void_type_node))
10350 error ("%<va_start%> used in function with fixed args");
10351 return true;
10353 else if (!arglist)
10355 /* Evidently an out of date version of <stdarg.h>; can't validate
10356 va_start's second argument, but can still work as intended. */
10357 warning (0, "%<__builtin_next_arg%> called without an argument");
10358 return true;
10360 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10361 when we checked the arguments and if needed issued a warning. */
10362 else if (!TREE_CHAIN (arglist)
10363 || !integer_zerop (TREE_VALUE (arglist))
10364 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10365 || TREE_CHAIN (TREE_CHAIN (arglist)))
10367 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10368 tree arg = TREE_VALUE (arglist);
10370 if (TREE_CHAIN (arglist))
10372 error ("%<va_start%> used with too many arguments");
10373 return true;
10376 /* Strip off all nops for the sake of the comparison. This
10377 is not quite the same as STRIP_NOPS. It does more.
10378 We must also strip off INDIRECT_EXPR for C++ reference
10379 parameters. */
10380 while (TREE_CODE (arg) == NOP_EXPR
10381 || TREE_CODE (arg) == CONVERT_EXPR
10382 || TREE_CODE (arg) == NON_LVALUE_EXPR
10383 || TREE_CODE (arg) == INDIRECT_REF)
10384 arg = TREE_OPERAND (arg, 0);
10385 if (arg != last_parm)
10387 /* FIXME: Sometimes with the tree optimizers we can get the
10388 not the last argument even though the user used the last
10389 argument. We just warn and set the arg to be the last
10390 argument so that we will get wrong-code because of
10391 it. */
10392 warning (0, "second parameter of %<va_start%> not last named argument");
10394 /* We want to verify the second parameter just once before the tree
10395 optimizers are run and then avoid keeping it in the tree,
10396 as otherwise we could warn even for correct code like:
10397 void foo (int i, ...)
10398 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10399 TREE_VALUE (arglist) = integer_zero_node;
10400 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10402 return false;
10406 /* Simplify a call to the sprintf builtin.
10408 Return 0 if no simplification was possible, otherwise return the
10409 simplified form of the call as a tree. If IGNORED is true, it means that
10410 the caller does not use the returned value of the function. */
10412 static tree
10413 fold_builtin_sprintf (tree arglist, int ignored)
10415 tree call, retval, dest, fmt;
10416 const char *fmt_str = NULL;
10418 /* Verify the required arguments in the original call. We deal with two
10419 types of sprintf() calls: 'sprintf (str, fmt)' and
10420 'sprintf (dest, "%s", orig)'. */
10421 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10422 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10423 VOID_TYPE))
10424 return NULL_TREE;
10426 /* Get the destination string and the format specifier. */
10427 dest = TREE_VALUE (arglist);
10428 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10430 /* Check whether the format is a literal string constant. */
10431 fmt_str = c_getstr (fmt);
10432 if (fmt_str == NULL)
10433 return NULL_TREE;
10435 call = NULL_TREE;
10436 retval = NULL_TREE;
10438 if (!init_target_chars())
10439 return 0;
10441 /* If the format doesn't contain % args or %%, use strcpy. */
10442 if (strchr (fmt_str, target_percent) == NULL)
10444 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10446 if (!fn)
10447 return NULL_TREE;
10449 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10450 'format' is known to contain no % formats. */
10451 arglist = build_tree_list (NULL_TREE, fmt);
10452 arglist = tree_cons (NULL_TREE, dest, arglist);
10453 call = build_function_call_expr (fn, arglist);
10454 if (!ignored)
10455 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10458 /* If the format is "%s", use strcpy if the result isn't used. */
10459 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10461 tree fn, orig;
10462 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10464 if (!fn)
10465 return NULL_TREE;
10467 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10468 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10469 arglist = build_tree_list (NULL_TREE, orig);
10470 arglist = tree_cons (NULL_TREE, dest, arglist);
10471 if (!ignored)
10473 retval = c_strlen (orig, 1);
10474 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10475 return NULL_TREE;
10477 call = build_function_call_expr (fn, arglist);
10480 if (call && retval)
10482 retval = fold_convert
10483 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10484 retval);
10485 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10487 else
10488 return call;
10491 /* Expand a call to __builtin_object_size. */
10494 expand_builtin_object_size (tree exp)
10496 tree ost;
10497 int object_size_type;
10498 tree fndecl = get_callee_fndecl (exp);
10499 tree arglist = TREE_OPERAND (exp, 1);
10500 location_t locus = EXPR_LOCATION (exp);
10502 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10504 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10505 &locus, fndecl);
10506 expand_builtin_trap ();
10507 return const0_rtx;
10510 ost = TREE_VALUE (TREE_CHAIN (arglist));
10511 STRIP_NOPS (ost);
10513 if (TREE_CODE (ost) != INTEGER_CST
10514 || tree_int_cst_sgn (ost) < 0
10515 || compare_tree_int (ost, 3) > 0)
10517 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10518 &locus, fndecl);
10519 expand_builtin_trap ();
10520 return const0_rtx;
10523 object_size_type = tree_low_cst (ost, 0);
10525 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10528 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10529 FCODE is the BUILT_IN_* to use.
10530 Return 0 if we failed; the caller should emit a normal call,
10531 otherwise try to get the result in TARGET, if convenient (and in
10532 mode MODE if that's convenient). */
10534 static rtx
10535 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10536 enum built_in_function fcode)
10538 tree arglist = TREE_OPERAND (exp, 1);
10539 tree dest, src, len, size;
10541 if (!validate_arglist (arglist,
10542 POINTER_TYPE,
10543 fcode == BUILT_IN_MEMSET_CHK
10544 ? INTEGER_TYPE : POINTER_TYPE,
10545 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10546 return 0;
10548 dest = TREE_VALUE (arglist);
10549 src = TREE_VALUE (TREE_CHAIN (arglist));
10550 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10551 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10553 if (! host_integerp (size, 1))
10554 return 0;
10556 if (host_integerp (len, 1) || integer_all_onesp (size))
10558 tree fn;
10560 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10562 location_t locus = EXPR_LOCATION (exp);
10563 warning (0, "%Hcall to %D will always overflow destination buffer",
10564 &locus, get_callee_fndecl (exp));
10565 return 0;
10568 arglist = build_tree_list (NULL_TREE, len);
10569 arglist = tree_cons (NULL_TREE, src, arglist);
10570 arglist = tree_cons (NULL_TREE, dest, arglist);
10572 fn = NULL_TREE;
10573 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10574 mem{cpy,pcpy,move,set} is available. */
10575 switch (fcode)
10577 case BUILT_IN_MEMCPY_CHK:
10578 fn = built_in_decls[BUILT_IN_MEMCPY];
10579 break;
10580 case BUILT_IN_MEMPCPY_CHK:
10581 fn = built_in_decls[BUILT_IN_MEMPCPY];
10582 break;
10583 case BUILT_IN_MEMMOVE_CHK:
10584 fn = built_in_decls[BUILT_IN_MEMMOVE];
10585 break;
10586 case BUILT_IN_MEMSET_CHK:
10587 fn = built_in_decls[BUILT_IN_MEMSET];
10588 break;
10589 default:
10590 break;
10593 if (! fn)
10594 return 0;
10596 fn = build_function_call_expr (fn, arglist);
10597 if (TREE_CODE (fn) == CALL_EXPR)
10598 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10599 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10601 else if (fcode == BUILT_IN_MEMSET_CHK)
10602 return 0;
10603 else
10605 unsigned int dest_align
10606 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10608 /* If DEST is not a pointer type, call the normal function. */
10609 if (dest_align == 0)
10610 return 0;
10612 /* If SRC and DEST are the same (and not volatile), do nothing. */
10613 if (operand_equal_p (src, dest, 0))
10615 tree expr;
10617 if (fcode != BUILT_IN_MEMPCPY_CHK)
10619 /* Evaluate and ignore LEN in case it has side-effects. */
10620 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10621 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10624 len = fold_convert (TREE_TYPE (dest), len);
10625 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10626 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10629 /* __memmove_chk special case. */
10630 if (fcode == BUILT_IN_MEMMOVE_CHK)
10632 unsigned int src_align
10633 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10635 if (src_align == 0)
10636 return 0;
10638 /* If src is categorized for a readonly section we can use
10639 normal __memcpy_chk. */
10640 if (readonly_data_expr (src))
10642 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10643 if (!fn)
10644 return 0;
10645 fn = build_function_call_expr (fn, arglist);
10646 if (TREE_CODE (fn) == CALL_EXPR)
10647 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10648 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10651 return 0;
10655 /* Emit warning if a buffer overflow is detected at compile time. */
10657 static void
10658 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10660 int arg_mask, is_strlen = 0;
10661 tree arglist = TREE_OPERAND (exp, 1), a;
10662 tree len, size;
10663 location_t locus;
10665 switch (fcode)
10667 case BUILT_IN_STRCPY_CHK:
10668 case BUILT_IN_STPCPY_CHK:
10669 /* For __strcat_chk the warning will be emitted only if overflowing
10670 by at least strlen (dest) + 1 bytes. */
10671 case BUILT_IN_STRCAT_CHK:
10672 arg_mask = 6;
10673 is_strlen = 1;
10674 break;
10675 case BUILT_IN_STRNCAT_CHK:
10676 /* For __strncat_chk the warning will be emitted only if overflowing
10677 by at least strlen (dest) + 1 bytes. */
10678 arg_mask = 12;
10679 break;
10680 case BUILT_IN_STRNCPY_CHK:
10681 arg_mask = 12;
10682 break;
10683 case BUILT_IN_SNPRINTF_CHK:
10684 case BUILT_IN_VSNPRINTF_CHK:
10685 arg_mask = 10;
10686 break;
10687 default:
10688 gcc_unreachable ();
10691 len = NULL_TREE;
10692 size = NULL_TREE;
10693 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10694 if (arg_mask & 1)
10696 if (len)
10697 size = a;
10698 else
10699 len = a;
10702 if (!len || !size)
10703 return;
10705 len = TREE_VALUE (len);
10706 size = TREE_VALUE (size);
10708 if (! host_integerp (size, 1) || integer_all_onesp (size))
10709 return;
10711 if (is_strlen)
10713 len = c_strlen (len, 1);
10714 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10715 return;
10717 else if (fcode == BUILT_IN_STRNCAT_CHK)
10719 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10720 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10721 return;
10722 src = c_strlen (src, 1);
10723 if (! src || ! host_integerp (src, 1))
10725 locus = EXPR_LOCATION (exp);
10726 warning (0, "%Hcall to %D might overflow destination buffer",
10727 &locus, get_callee_fndecl (exp));
10728 return;
10730 else if (tree_int_cst_lt (src, size))
10731 return;
10733 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10734 return;
10736 locus = EXPR_LOCATION (exp);
10737 warning (0, "%Hcall to %D will always overflow destination buffer",
10738 &locus, get_callee_fndecl (exp));
10741 /* Emit warning if a buffer overflow is detected at compile time
10742 in __sprintf_chk/__vsprintf_chk calls. */
10744 static void
10745 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10747 tree arglist = TREE_OPERAND (exp, 1);
10748 tree dest, size, len, fmt, flag;
10749 const char *fmt_str;
10751 /* Verify the required arguments in the original call. */
10752 if (! arglist)
10753 return;
10754 dest = TREE_VALUE (arglist);
10755 arglist = TREE_CHAIN (arglist);
10756 if (! arglist)
10757 return;
10758 flag = TREE_VALUE (arglist);
10759 arglist = TREE_CHAIN (arglist);
10760 if (! arglist)
10761 return;
10762 size = TREE_VALUE (arglist);
10763 arglist = TREE_CHAIN (arglist);
10764 if (! arglist)
10765 return;
10766 fmt = TREE_VALUE (arglist);
10767 arglist = TREE_CHAIN (arglist);
10769 if (! host_integerp (size, 1) || integer_all_onesp (size))
10770 return;
10772 /* Check whether the format is a literal string constant. */
10773 fmt_str = c_getstr (fmt);
10774 if (fmt_str == NULL)
10775 return;
10777 if (!init_target_chars())
10778 return;
10780 /* If the format doesn't contain % args or %%, we know its size. */
10781 if (strchr (fmt_str, target_percent) == 0)
10782 len = build_int_cstu (size_type_node, strlen (fmt_str));
10783 /* If the format is "%s" and first ... argument is a string literal,
10784 we know it too. */
10785 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10787 tree arg;
10789 if (! arglist)
10790 return;
10791 arg = TREE_VALUE (arglist);
10792 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10793 return;
10795 len = c_strlen (arg, 1);
10796 if (!len || ! host_integerp (len, 1))
10797 return;
10799 else
10800 return;
10802 if (! tree_int_cst_lt (len, size))
10804 location_t locus = EXPR_LOCATION (exp);
10805 warning (0, "%Hcall to %D will always overflow destination buffer",
10806 &locus, get_callee_fndecl (exp));
10810 /* Fold a call to __builtin_object_size, if possible. */
10812 tree
10813 fold_builtin_object_size (tree arglist)
10815 tree ptr, ost, ret = 0;
10816 int object_size_type;
10818 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10819 return 0;
10821 ptr = TREE_VALUE (arglist);
10822 ost = TREE_VALUE (TREE_CHAIN (arglist));
10823 STRIP_NOPS (ost);
10825 if (TREE_CODE (ost) != INTEGER_CST
10826 || tree_int_cst_sgn (ost) < 0
10827 || compare_tree_int (ost, 3) > 0)
10828 return 0;
10830 object_size_type = tree_low_cst (ost, 0);
10832 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10833 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10834 and (size_t) 0 for types 2 and 3. */
10835 if (TREE_SIDE_EFFECTS (ptr))
10836 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10838 if (TREE_CODE (ptr) == ADDR_EXPR)
10839 ret = build_int_cstu (size_type_node,
10840 compute_builtin_object_size (ptr, object_size_type));
10842 else if (TREE_CODE (ptr) == SSA_NAME)
10844 unsigned HOST_WIDE_INT bytes;
10846 /* If object size is not known yet, delay folding until
10847 later. Maybe subsequent passes will help determining
10848 it. */
10849 bytes = compute_builtin_object_size (ptr, object_size_type);
10850 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10851 ? -1 : 0))
10852 ret = build_int_cstu (size_type_node, bytes);
10855 if (ret)
10857 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
10858 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
10859 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
10860 ret = NULL_TREE;
10863 return ret;
10866 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10867 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10868 code of the builtin. If MAXLEN is not NULL, it is maximum length
10869 passed as third argument. */
10871 tree
10872 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10873 enum built_in_function fcode)
10875 tree dest, src, len, size, fn;
10877 if (!validate_arglist (arglist,
10878 POINTER_TYPE,
10879 fcode == BUILT_IN_MEMSET_CHK
10880 ? INTEGER_TYPE : POINTER_TYPE,
10881 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10882 return 0;
10884 dest = TREE_VALUE (arglist);
10885 /* Actually val for __memset_chk, but it doesn't matter. */
10886 src = TREE_VALUE (TREE_CHAIN (arglist));
10887 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10888 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10890 /* If SRC and DEST are the same (and not volatile), return DEST
10891 (resp. DEST+LEN for __mempcpy_chk). */
10892 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10894 if (fcode != BUILT_IN_MEMPCPY_CHK)
10895 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10896 else
10898 tree temp = fold_convert (TREE_TYPE (dest), len);
10899 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10900 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10904 if (! host_integerp (size, 1))
10905 return 0;
10907 if (! integer_all_onesp (size))
10909 if (! host_integerp (len, 1))
10911 /* If LEN is not constant, try MAXLEN too.
10912 For MAXLEN only allow optimizing into non-_ocs function
10913 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10914 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10916 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10918 /* (void) __mempcpy_chk () can be optimized into
10919 (void) __memcpy_chk (). */
10920 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10921 if (!fn)
10922 return 0;
10924 return build_function_call_expr (fn, arglist);
10926 return 0;
10929 else
10930 maxlen = len;
10932 if (tree_int_cst_lt (size, maxlen))
10933 return 0;
10936 arglist = build_tree_list (NULL_TREE, len);
10937 arglist = tree_cons (NULL_TREE, src, arglist);
10938 arglist = tree_cons (NULL_TREE, dest, arglist);
10940 fn = NULL_TREE;
10941 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10942 mem{cpy,pcpy,move,set} is available. */
10943 switch (fcode)
10945 case BUILT_IN_MEMCPY_CHK:
10946 fn = built_in_decls[BUILT_IN_MEMCPY];
10947 break;
10948 case BUILT_IN_MEMPCPY_CHK:
10949 fn = built_in_decls[BUILT_IN_MEMPCPY];
10950 break;
10951 case BUILT_IN_MEMMOVE_CHK:
10952 fn = built_in_decls[BUILT_IN_MEMMOVE];
10953 break;
10954 case BUILT_IN_MEMSET_CHK:
10955 fn = built_in_decls[BUILT_IN_MEMSET];
10956 break;
10957 default:
10958 break;
10961 if (!fn)
10962 return 0;
10964 return build_function_call_expr (fn, arglist);
10967 /* Fold a call to the __st[rp]cpy_chk builtin.
10968 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10969 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10970 strings passed as second argument. */
10972 tree
10973 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10974 enum built_in_function fcode)
10976 tree dest, src, size, len, fn;
10978 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10979 VOID_TYPE))
10980 return 0;
10982 dest = TREE_VALUE (arglist);
10983 src = TREE_VALUE (TREE_CHAIN (arglist));
10984 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10986 /* If SRC and DEST are the same (and not volatile), return DEST. */
10987 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10988 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10990 if (! host_integerp (size, 1))
10991 return 0;
10993 if (! integer_all_onesp (size))
10995 len = c_strlen (src, 1);
10996 if (! len || ! host_integerp (len, 1))
10998 /* If LEN is not constant, try MAXLEN too.
10999 For MAXLEN only allow optimizing into non-_ocs function
11000 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11001 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11003 if (fcode == BUILT_IN_STPCPY_CHK)
11005 if (! ignore)
11006 return 0;
11008 /* If return value of __stpcpy_chk is ignored,
11009 optimize into __strcpy_chk. */
11010 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
11011 if (!fn)
11012 return 0;
11014 return build_function_call_expr (fn, arglist);
11017 if (! len || TREE_SIDE_EFFECTS (len))
11018 return 0;
11020 /* If c_strlen returned something, but not a constant,
11021 transform __strcpy_chk into __memcpy_chk. */
11022 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11023 if (!fn)
11024 return 0;
11026 len = size_binop (PLUS_EXPR, len, ssize_int (1));
11027 arglist = build_tree_list (NULL_TREE, size);
11028 arglist = tree_cons (NULL_TREE, len, arglist);
11029 arglist = tree_cons (NULL_TREE, src, arglist);
11030 arglist = tree_cons (NULL_TREE, dest, arglist);
11031 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
11032 build_function_call_expr (fn, arglist));
11035 else
11036 maxlen = len;
11038 if (! tree_int_cst_lt (maxlen, size))
11039 return 0;
11042 arglist = build_tree_list (NULL_TREE, src);
11043 arglist = tree_cons (NULL_TREE, dest, arglist);
11045 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
11046 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
11047 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
11048 if (!fn)
11049 return 0;
11051 return build_function_call_expr (fn, arglist);
11054 /* Fold a call to the __strncpy_chk builtin.
11055 If MAXLEN is not NULL, it is maximum length passed as third argument. */
11057 tree
11058 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
11060 tree dest, src, size, len, fn;
11062 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11063 INTEGER_TYPE, VOID_TYPE))
11064 return 0;
11066 dest = TREE_VALUE (arglist);
11067 src = TREE_VALUE (TREE_CHAIN (arglist));
11068 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11069 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11071 if (! host_integerp (size, 1))
11072 return 0;
11074 if (! integer_all_onesp (size))
11076 if (! host_integerp (len, 1))
11078 /* If LEN is not constant, try MAXLEN too.
11079 For MAXLEN only allow optimizing into non-_ocs function
11080 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11081 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11082 return 0;
11084 else
11085 maxlen = len;
11087 if (tree_int_cst_lt (size, maxlen))
11088 return 0;
11091 arglist = build_tree_list (NULL_TREE, len);
11092 arglist = tree_cons (NULL_TREE, src, arglist);
11093 arglist = tree_cons (NULL_TREE, dest, arglist);
11095 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
11096 fn = built_in_decls[BUILT_IN_STRNCPY];
11097 if (!fn)
11098 return 0;
11100 return build_function_call_expr (fn, arglist);
11103 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
11105 static tree
11106 fold_builtin_strcat_chk (tree fndecl, tree arglist)
11108 tree dest, src, size, fn;
11109 const char *p;
11111 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11112 VOID_TYPE))
11113 return 0;
11115 dest = TREE_VALUE (arglist);
11116 src = TREE_VALUE (TREE_CHAIN (arglist));
11117 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11119 p = c_getstr (src);
11120 /* If the SRC parameter is "", return DEST. */
11121 if (p && *p == '\0')
11122 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11124 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
11125 return 0;
11127 arglist = build_tree_list (NULL_TREE, src);
11128 arglist = tree_cons (NULL_TREE, dest, arglist);
11130 /* If __builtin_strcat_chk is used, assume strcat is available. */
11131 fn = built_in_decls[BUILT_IN_STRCAT];
11132 if (!fn)
11133 return 0;
11135 return build_function_call_expr (fn, arglist);
11138 /* Fold a call to the __strncat_chk builtin EXP. */
11140 static tree
11141 fold_builtin_strncat_chk (tree fndecl, tree arglist)
11143 tree dest, src, size, len, fn;
11144 const char *p;
11146 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11147 INTEGER_TYPE, VOID_TYPE))
11148 return 0;
11150 dest = TREE_VALUE (arglist);
11151 src = TREE_VALUE (TREE_CHAIN (arglist));
11152 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11153 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11155 p = c_getstr (src);
11156 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
11157 if (p && *p == '\0')
11158 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
11159 else if (integer_zerop (len))
11160 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11162 if (! host_integerp (size, 1))
11163 return 0;
11165 if (! integer_all_onesp (size))
11167 tree src_len = c_strlen (src, 1);
11168 if (src_len
11169 && host_integerp (src_len, 1)
11170 && host_integerp (len, 1)
11171 && ! tree_int_cst_lt (len, src_len))
11173 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
11174 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
11175 if (!fn)
11176 return 0;
11178 arglist = build_tree_list (NULL_TREE, size);
11179 arglist = tree_cons (NULL_TREE, src, arglist);
11180 arglist = tree_cons (NULL_TREE, dest, arglist);
11181 return build_function_call_expr (fn, arglist);
11183 return 0;
11186 arglist = build_tree_list (NULL_TREE, len);
11187 arglist = tree_cons (NULL_TREE, src, arglist);
11188 arglist = tree_cons (NULL_TREE, dest, arglist);
11190 /* If __builtin_strncat_chk is used, assume strncat is available. */
11191 fn = built_in_decls[BUILT_IN_STRNCAT];
11192 if (!fn)
11193 return 0;
11195 return build_function_call_expr (fn, arglist);
11198 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
11199 a normal call should be emitted rather than expanding the function
11200 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
11202 static tree
11203 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
11205 tree dest, size, len, fn, fmt, flag;
11206 const char *fmt_str;
11208 /* Verify the required arguments in the original call. */
11209 if (! arglist)
11210 return 0;
11211 dest = TREE_VALUE (arglist);
11212 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11213 return 0;
11214 arglist = TREE_CHAIN (arglist);
11215 if (! arglist)
11216 return 0;
11217 flag = TREE_VALUE (arglist);
11218 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
11219 return 0;
11220 arglist = TREE_CHAIN (arglist);
11221 if (! arglist)
11222 return 0;
11223 size = TREE_VALUE (arglist);
11224 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11225 return 0;
11226 arglist = TREE_CHAIN (arglist);
11227 if (! arglist)
11228 return 0;
11229 fmt = TREE_VALUE (arglist);
11230 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11231 return 0;
11232 arglist = TREE_CHAIN (arglist);
11234 if (! host_integerp (size, 1))
11235 return 0;
11237 len = NULL_TREE;
11239 if (!init_target_chars())
11240 return 0;
11242 /* Check whether the format is a literal string constant. */
11243 fmt_str = c_getstr (fmt);
11244 if (fmt_str != NULL)
11246 /* If the format doesn't contain % args or %%, we know the size. */
11247 if (strchr (fmt_str, target_percent) == 0)
11249 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11250 len = build_int_cstu (size_type_node, strlen (fmt_str));
11252 /* If the format is "%s" and first ... argument is a string literal,
11253 we know the size too. */
11254 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11256 tree arg;
11258 if (arglist && !TREE_CHAIN (arglist))
11260 arg = TREE_VALUE (arglist);
11261 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11263 len = c_strlen (arg, 1);
11264 if (! len || ! host_integerp (len, 1))
11265 len = NULL_TREE;
11271 if (! integer_all_onesp (size))
11273 if (! len || ! tree_int_cst_lt (len, size))
11274 return 0;
11277 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11278 or if format doesn't contain % chars or is "%s". */
11279 if (! integer_zerop (flag))
11281 if (fmt_str == NULL)
11282 return 0;
11283 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11284 return 0;
11287 arglist = tree_cons (NULL_TREE, fmt, arglist);
11288 arglist = tree_cons (NULL_TREE, dest, arglist);
11290 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11291 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11292 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11293 if (!fn)
11294 return 0;
11296 return build_function_call_expr (fn, arglist);
11299 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11300 a normal call should be emitted rather than expanding the function
11301 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11302 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11303 passed as second argument. */
11305 tree
11306 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11307 enum built_in_function fcode)
11309 tree dest, size, len, fn, fmt, flag;
11310 const char *fmt_str;
11312 /* Verify the required arguments in the original call. */
11313 if (! arglist)
11314 return 0;
11315 dest = TREE_VALUE (arglist);
11316 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11317 return 0;
11318 arglist = TREE_CHAIN (arglist);
11319 if (! arglist)
11320 return 0;
11321 len = TREE_VALUE (arglist);
11322 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11323 return 0;
11324 arglist = TREE_CHAIN (arglist);
11325 if (! arglist)
11326 return 0;
11327 flag = TREE_VALUE (arglist);
11328 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11329 return 0;
11330 arglist = TREE_CHAIN (arglist);
11331 if (! arglist)
11332 return 0;
11333 size = TREE_VALUE (arglist);
11334 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11335 return 0;
11336 arglist = TREE_CHAIN (arglist);
11337 if (! arglist)
11338 return 0;
11339 fmt = TREE_VALUE (arglist);
11340 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11341 return 0;
11342 arglist = TREE_CHAIN (arglist);
11344 if (! host_integerp (size, 1))
11345 return 0;
11347 if (! integer_all_onesp (size))
11349 if (! host_integerp (len, 1))
11351 /* If LEN is not constant, try MAXLEN too.
11352 For MAXLEN only allow optimizing into non-_ocs function
11353 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11354 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11355 return 0;
11357 else
11358 maxlen = len;
11360 if (tree_int_cst_lt (size, maxlen))
11361 return 0;
11364 if (!init_target_chars())
11365 return 0;
11367 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11368 or if format doesn't contain % chars or is "%s". */
11369 if (! integer_zerop (flag))
11371 fmt_str = c_getstr (fmt);
11372 if (fmt_str == NULL)
11373 return 0;
11374 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11375 return 0;
11378 arglist = tree_cons (NULL_TREE, fmt, arglist);
11379 arglist = tree_cons (NULL_TREE, len, arglist);
11380 arglist = tree_cons (NULL_TREE, dest, arglist);
11382 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11383 available. */
11384 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11385 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11386 if (!fn)
11387 return 0;
11389 return build_function_call_expr (fn, arglist);
11392 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11394 Return 0 if no simplification was possible, otherwise return the
11395 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11396 code of the function to be simplified. */
11398 static tree
11399 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11400 enum built_in_function fcode)
11402 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11403 const char *fmt_str = NULL;
11405 /* If the return value is used, don't do the transformation. */
11406 if (! ignore)
11407 return 0;
11409 /* Verify the required arguments in the original call. */
11410 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11412 tree flag;
11414 if (! arglist)
11415 return 0;
11416 flag = TREE_VALUE (arglist);
11417 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11418 || TREE_SIDE_EFFECTS (flag))
11419 return 0;
11420 arglist = TREE_CHAIN (arglist);
11423 if (! arglist)
11424 return 0;
11425 fmt = TREE_VALUE (arglist);
11426 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11427 return 0;
11428 arglist = TREE_CHAIN (arglist);
11430 /* Check whether the format is a literal string constant. */
11431 fmt_str = c_getstr (fmt);
11432 if (fmt_str == NULL)
11433 return NULL_TREE;
11435 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11437 /* If we're using an unlocked function, assume the other
11438 unlocked functions exist explicitly. */
11439 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11440 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11442 else
11444 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11445 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11448 if (!init_target_chars())
11449 return 0;
11451 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11453 const char *str;
11455 if (strcmp (fmt_str, target_percent_s) == 0)
11457 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11458 return 0;
11460 if (! arglist
11461 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11462 || TREE_CHAIN (arglist))
11463 return 0;
11465 str = c_getstr (TREE_VALUE (arglist));
11466 if (str == NULL)
11467 return 0;
11469 else
11471 /* The format specifier doesn't contain any '%' characters. */
11472 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11473 && arglist)
11474 return 0;
11475 str = fmt_str;
11478 /* If the string was "", printf does nothing. */
11479 if (str[0] == '\0')
11480 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11482 /* If the string has length of 1, call putchar. */
11483 if (str[1] == '\0')
11485 /* Given printf("c"), (where c is any one character,)
11486 convert "c"[0] to an int and pass that to the replacement
11487 function. */
11488 arg = build_int_cst (NULL_TREE, str[0]);
11489 arglist = build_tree_list (NULL_TREE, arg);
11490 fn = fn_putchar;
11492 else
11494 /* If the string was "string\n", call puts("string"). */
11495 size_t len = strlen (str);
11496 if ((unsigned char)str[len - 1] == target_newline)
11498 /* Create a NUL-terminated string that's one char shorter
11499 than the original, stripping off the trailing '\n'. */
11500 char *newstr = alloca (len);
11501 memcpy (newstr, str, len - 1);
11502 newstr[len - 1] = 0;
11504 arg = build_string_literal (len, newstr);
11505 arglist = build_tree_list (NULL_TREE, arg);
11506 fn = fn_puts;
11508 else
11509 /* We'd like to arrange to call fputs(string,stdout) here,
11510 but we need stdout and don't have a way to get it yet. */
11511 return 0;
11515 /* The other optimizations can be done only on the non-va_list variants. */
11516 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11517 return 0;
11519 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11520 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11522 if (! arglist
11523 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11524 || TREE_CHAIN (arglist))
11525 return 0;
11526 fn = fn_puts;
11529 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11530 else if (strcmp (fmt_str, target_percent_c) == 0)
11532 if (! arglist
11533 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11534 || TREE_CHAIN (arglist))
11535 return 0;
11536 fn = fn_putchar;
11539 if (!fn)
11540 return 0;
11542 call = build_function_call_expr (fn, arglist);
11543 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11546 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11548 Return 0 if no simplification was possible, otherwise return the
11549 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11550 code of the function to be simplified. */
11552 static tree
11553 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11554 enum built_in_function fcode)
11556 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11557 const char *fmt_str = NULL;
11559 /* If the return value is used, don't do the transformation. */
11560 if (! ignore)
11561 return 0;
11563 /* Verify the required arguments in the original call. */
11564 if (! arglist)
11565 return 0;
11566 fp = TREE_VALUE (arglist);
11567 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11568 return 0;
11569 arglist = TREE_CHAIN (arglist);
11571 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11573 tree flag;
11575 if (! arglist)
11576 return 0;
11577 flag = TREE_VALUE (arglist);
11578 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11579 || TREE_SIDE_EFFECTS (flag))
11580 return 0;
11581 arglist = TREE_CHAIN (arglist);
11584 if (! arglist)
11585 return 0;
11586 fmt = TREE_VALUE (arglist);
11587 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11588 return 0;
11589 arglist = TREE_CHAIN (arglist);
11591 /* Check whether the format is a literal string constant. */
11592 fmt_str = c_getstr (fmt);
11593 if (fmt_str == NULL)
11594 return NULL_TREE;
11596 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11598 /* If we're using an unlocked function, assume the other
11599 unlocked functions exist explicitly. */
11600 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11601 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11603 else
11605 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11606 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11609 if (!init_target_chars())
11610 return 0;
11612 /* If the format doesn't contain % args or %%, use strcpy. */
11613 if (strchr (fmt_str, target_percent) == NULL)
11615 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11616 && arglist)
11617 return 0;
11619 /* If the format specifier was "", fprintf does nothing. */
11620 if (fmt_str[0] == '\0')
11622 /* If FP has side-effects, just wait until gimplification is
11623 done. */
11624 if (TREE_SIDE_EFFECTS (fp))
11625 return 0;
11627 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11630 /* When "string" doesn't contain %, replace all cases of
11631 fprintf (fp, string) with fputs (string, fp). The fputs
11632 builtin will take care of special cases like length == 1. */
11633 arglist = build_tree_list (NULL_TREE, fp);
11634 arglist = tree_cons (NULL_TREE, fmt, arglist);
11635 fn = fn_fputs;
11638 /* The other optimizations can be done only on the non-va_list variants. */
11639 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11640 return 0;
11642 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11643 else if (strcmp (fmt_str, target_percent_s) == 0)
11645 if (! arglist
11646 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11647 || TREE_CHAIN (arglist))
11648 return 0;
11649 arg = TREE_VALUE (arglist);
11650 arglist = build_tree_list (NULL_TREE, fp);
11651 arglist = tree_cons (NULL_TREE, arg, arglist);
11652 fn = fn_fputs;
11655 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11656 else if (strcmp (fmt_str, target_percent_c) == 0)
11658 if (! arglist
11659 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11660 || TREE_CHAIN (arglist))
11661 return 0;
11662 arg = TREE_VALUE (arglist);
11663 arglist = build_tree_list (NULL_TREE, fp);
11664 arglist = tree_cons (NULL_TREE, arg, arglist);
11665 fn = fn_fputc;
11668 if (!fn)
11669 return 0;
11671 call = build_function_call_expr (fn, arglist);
11672 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11675 /* Initialize format string characters in the target charset. */
11677 static bool
11678 init_target_chars (void)
11680 static bool init;
11681 if (!init)
11683 target_newline = lang_hooks.to_target_charset ('\n');
11684 target_percent = lang_hooks.to_target_charset ('%');
11685 target_c = lang_hooks.to_target_charset ('c');
11686 target_s = lang_hooks.to_target_charset ('s');
11687 if (target_newline == 0 || target_percent == 0 || target_c == 0
11688 || target_s == 0)
11689 return false;
11691 target_percent_c[0] = target_percent;
11692 target_percent_c[1] = target_c;
11693 target_percent_c[2] = '\0';
11695 target_percent_s[0] = target_percent;
11696 target_percent_s[1] = target_s;
11697 target_percent_s[2] = '\0';
11699 target_percent_s_newline[0] = target_percent;
11700 target_percent_s_newline[1] = target_s;
11701 target_percent_s_newline[2] = target_newline;
11702 target_percent_s_newline[3] = '\0';
11704 init = true;
11706 return true;
11709 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11710 and no overflow/underflow occurred. INEXACT is true if M was not
11711 exactly calculated. TYPE is the tree type for the result. This
11712 function assumes that you cleared the MPFR flags and then
11713 calculated M to see if anything subsequently set a flag prior to
11714 entering this function. Return NULL_TREE if any checks fail. */
11716 static tree
11717 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11719 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11720 overflow/underflow occurred. If -frounding-math, proceed iff the
11721 result of calling FUNC was exact. */
11722 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11723 && (!flag_rounding_math || !inexact))
11725 REAL_VALUE_TYPE rr;
11727 real_from_mpfr (&rr, m);
11728 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11729 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11730 but the mpft_t is not, then we underflowed in the
11731 conversion. */
11732 if (!real_isnan (&rr) && !real_isinf (&rr)
11733 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11735 REAL_VALUE_TYPE rmode;
11737 real_convert (&rmode, TYPE_MODE (type), &rr);
11738 /* Proceed iff the specified mode can hold the value. */
11739 if (real_identical (&rmode, &rr))
11740 return build_real (type, rmode);
11743 return NULL_TREE;
11746 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11747 FUNC on it and return the resulting value as a tree with type TYPE.
11748 If MIN and/or MAX are not NULL, then the supplied ARG must be
11749 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11750 acceptable values, otherwise they are not. The mpfr precision is
11751 set to the precision of TYPE. We assume that function FUNC returns
11752 zero if the result could be calculated exactly within the requested
11753 precision. */
11755 static tree
11756 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11757 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11758 bool inclusive)
11760 tree result = NULL_TREE;
11762 STRIP_NOPS (arg);
11764 /* To proceed, MPFR must exactly represent the target floating point
11765 format, which only happens when the target base equals two. */
11766 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11767 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11769 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11771 if (!real_isnan (ra) && !real_isinf (ra)
11772 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11773 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11775 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11776 int inexact;
11777 mpfr_t m;
11779 mpfr_init2 (m, prec);
11780 mpfr_from_real (m, ra);
11781 mpfr_clear_flags();
11782 inexact = func (m, m, GMP_RNDN);
11783 result = do_mpfr_ckconv (m, type, inexact);
11784 mpfr_clear (m);
11788 return result;
11791 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11792 FUNC on it and return the resulting value as a tree with type TYPE.
11793 The mpfr precision is set to the precision of TYPE. We assume that
11794 function FUNC returns zero if the result could be calculated
11795 exactly within the requested precision. */
11797 static tree
11798 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11799 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11801 tree result = NULL_TREE;
11803 STRIP_NOPS (arg1);
11804 STRIP_NOPS (arg2);
11806 /* To proceed, MPFR must exactly represent the target floating point
11807 format, which only happens when the target base equals two. */
11808 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11809 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11810 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11812 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11813 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11815 if (!real_isnan (ra1) && !real_isinf (ra1)
11816 && !real_isnan (ra2) && !real_isinf (ra2))
11818 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11819 int inexact;
11820 mpfr_t m1, m2;
11822 mpfr_inits2 (prec, m1, m2, NULL);
11823 mpfr_from_real (m1, ra1);
11824 mpfr_from_real (m2, ra2);
11825 mpfr_clear_flags();
11826 inexact = func (m1, m1, m2, GMP_RNDN);
11827 result = do_mpfr_ckconv (m1, type, inexact);
11828 mpfr_clears (m1, m2, NULL);
11832 return result;
11835 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11836 FUNC on it and return the resulting value as a tree with type TYPE.
11837 The mpfr precision is set to the precision of TYPE. We assume that
11838 function FUNC returns zero if the result could be calculated
11839 exactly within the requested precision. */
11841 static tree
11842 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11843 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11845 tree result = NULL_TREE;
11847 STRIP_NOPS (arg1);
11848 STRIP_NOPS (arg2);
11849 STRIP_NOPS (arg3);
11851 /* To proceed, MPFR must exactly represent the target floating point
11852 format, which only happens when the target base equals two. */
11853 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11854 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11855 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11856 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11858 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11859 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11860 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11862 if (!real_isnan (ra1) && !real_isinf (ra1)
11863 && !real_isnan (ra2) && !real_isinf (ra2)
11864 && !real_isnan (ra3) && !real_isinf (ra3))
11866 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11867 int inexact;
11868 mpfr_t m1, m2, m3;
11870 mpfr_inits2 (prec, m1, m2, m3, NULL);
11871 mpfr_from_real (m1, ra1);
11872 mpfr_from_real (m2, ra2);
11873 mpfr_from_real (m3, ra3);
11874 mpfr_clear_flags();
11875 inexact = func (m1, m1, m2, m3, GMP_RNDN);
11876 result = do_mpfr_ckconv (m1, type, inexact);
11877 mpfr_clears (m1, m2, m3, NULL);
11881 return result;
11884 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11885 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11886 If ARG_SINP and ARG_COSP are NULL then the result is returned
11887 as a complex value.
11888 The type is taken from the type of ARG and is used for setting the
11889 precision of the calculation and results. */
11891 static tree
11892 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11894 tree const type = TREE_TYPE (arg);
11895 tree result = NULL_TREE;
11897 STRIP_NOPS (arg);
11899 /* To proceed, MPFR must exactly represent the target floating point
11900 format, which only happens when the target base equals two. */
11901 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11902 && TREE_CODE (arg) == REAL_CST
11903 && !TREE_OVERFLOW (arg))
11905 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11907 if (!real_isnan (ra) && !real_isinf (ra))
11909 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11910 tree result_s, result_c;
11911 int inexact;
11912 mpfr_t m, ms, mc;
11914 mpfr_inits2 (prec, m, ms, mc, NULL);
11915 mpfr_from_real (m, ra);
11916 mpfr_clear_flags();
11917 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
11918 result_s = do_mpfr_ckconv (ms, type, inexact);
11919 result_c = do_mpfr_ckconv (mc, type, inexact);
11920 mpfr_clears (m, ms, mc, NULL);
11921 if (result_s && result_c)
11923 /* If we are to return in a complex value do so. */
11924 if (!arg_sinp && !arg_cosp)
11925 return build_complex (build_complex_type (type),
11926 result_c, result_s);
11928 /* Dereference the sin/cos pointer arguments. */
11929 arg_sinp = build_fold_indirect_ref (arg_sinp);
11930 arg_cosp = build_fold_indirect_ref (arg_cosp);
11931 /* Proceed if valid pointer type were passed in. */
11932 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11933 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11935 /* Set the values. */
11936 result_s = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_sinp,
11937 result_s);
11938 TREE_SIDE_EFFECTS (result_s) = 1;
11939 result_c = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_cosp,
11940 result_c);
11941 TREE_SIDE_EFFECTS (result_c) = 1;
11942 /* Combine the assignments into a compound expr. */
11943 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11944 result_s, result_c));
11949 return result;