Daily bump.
[official-gcc.git] / gcc / builtins.c
blob5e6ebf5d116c56da1d43ba213ef3ef7c1c577fda
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "real.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tree-gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "toplev.h"
44 #include "predict.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
50 #include "tree-flow.h"
51 #include "value-prof.h"
52 #include "diagnostic.h"
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 #endif
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
65 #include "builtins.def"
67 #undef DEF_BUILTIN
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
77 static const char *c_getstr (tree);
78 static rtx c_readstr (const char *, enum machine_mode);
79 static int target_char_cast (tree, char *);
80 static rtx get_memory_rtx (tree, tree);
81 static int apply_args_size (void);
82 static int apply_result_size (void);
83 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
84 static rtx result_vector (int, rtx);
85 #endif
86 static void expand_builtin_update_setjmp_buf (rtx);
87 static void expand_builtin_prefetch (tree);
88 static rtx expand_builtin_apply_args (void);
89 static rtx expand_builtin_apply_args_1 (void);
90 static rtx expand_builtin_apply (rtx, rtx, rtx);
91 static void expand_builtin_return (rtx);
92 static enum type_class type_to_class (tree);
93 static rtx expand_builtin_classify_type (tree);
94 static void expand_errno_check (tree, rtx);
95 static rtx expand_builtin_mathfn (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
97 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
98 static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_sincos (tree);
100 static rtx expand_builtin_cexpi (tree, rtx, rtx);
101 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
102 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
103 static rtx expand_builtin_args_info (tree);
104 static rtx expand_builtin_next_arg (void);
105 static rtx expand_builtin_va_start (tree);
106 static rtx expand_builtin_va_end (tree);
107 static rtx expand_builtin_va_copy (tree);
108 static rtx expand_builtin_memchr (tree, rtx, enum machine_mode);
109 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
112 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
113 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
114 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
118 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx,
120 enum machine_mode, int);
121 static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int);
122 static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx,
123 enum machine_mode, int);
124 static rtx expand_builtin_bcopy (tree, int);
125 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
129 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
130 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
132 static rtx expand_builtin_bzero (tree);
133 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_strstr (tree, rtx, enum machine_mode);
135 static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode);
136 static rtx expand_builtin_strchr (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode);
138 static rtx expand_builtin_alloca (tree, rtx);
139 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
140 static rtx expand_builtin_frame_address (tree, tree);
141 static rtx expand_builtin_fputs (tree, rtx, bool);
142 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
143 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
144 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
145 static tree stabilize_va_list (tree, int);
146 static rtx expand_builtin_expect (tree, rtx);
147 static tree fold_builtin_constant_p (tree);
148 static tree fold_builtin_expect (tree, tree);
149 static tree fold_builtin_classify_type (tree);
150 static tree fold_builtin_strlen (tree);
151 static tree fold_builtin_inf (tree, int);
152 static tree fold_builtin_nan (tree, tree, int);
153 static tree rewrite_call_expr (tree, int, tree, int, ...);
154 static bool validate_arg (const_tree, enum tree_code code);
155 static bool integer_valued_real_p (tree);
156 static tree fold_trunc_transparent_mathfn (tree, tree);
157 static bool readonly_data_expr (tree);
158 static rtx expand_builtin_fabs (tree, rtx, rtx);
159 static rtx expand_builtin_signbit (tree, rtx);
160 static tree fold_builtin_sqrt (tree, tree);
161 static tree fold_builtin_cbrt (tree, tree);
162 static tree fold_builtin_pow (tree, tree, tree, tree);
163 static tree fold_builtin_powi (tree, tree, tree, tree);
164 static tree fold_builtin_cos (tree, tree, tree);
165 static tree fold_builtin_cosh (tree, tree, tree);
166 static tree fold_builtin_tan (tree, tree);
167 static tree fold_builtin_trunc (tree, tree);
168 static tree fold_builtin_floor (tree, tree);
169 static tree fold_builtin_ceil (tree, tree);
170 static tree fold_builtin_round (tree, tree);
171 static tree fold_builtin_int_roundingfn (tree, tree);
172 static tree fold_builtin_bitop (tree, tree);
173 static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int);
174 static tree fold_builtin_strchr (tree, tree, tree);
175 static tree fold_builtin_memchr (tree, tree, tree, tree);
176 static tree fold_builtin_memcmp (tree, tree, tree);
177 static tree fold_builtin_strcmp (tree, tree);
178 static tree fold_builtin_strncmp (tree, tree, tree);
179 static tree fold_builtin_signbit (tree, tree);
180 static tree fold_builtin_copysign (tree, tree, tree, tree);
181 static tree fold_builtin_isascii (tree);
182 static tree fold_builtin_toascii (tree);
183 static tree fold_builtin_isdigit (tree);
184 static tree fold_builtin_fabs (tree, tree);
185 static tree fold_builtin_abs (tree, tree);
186 static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code,
187 enum tree_code);
188 static tree fold_builtin_n (tree, tree *, int, bool);
189 static tree fold_builtin_0 (tree, bool);
190 static tree fold_builtin_1 (tree, tree, bool);
191 static tree fold_builtin_2 (tree, tree, tree, bool);
192 static tree fold_builtin_3 (tree, tree, tree, tree, bool);
193 static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool);
194 static tree fold_builtin_varargs (tree, tree, bool);
196 static tree fold_builtin_strpbrk (tree, tree, tree);
197 static tree fold_builtin_strstr (tree, tree, tree);
198 static tree fold_builtin_strrchr (tree, tree, tree);
199 static tree fold_builtin_strcat (tree, tree);
200 static tree fold_builtin_strncat (tree, tree, tree);
201 static tree fold_builtin_strspn (tree, tree);
202 static tree fold_builtin_strcspn (tree, tree);
203 static tree fold_builtin_sprintf (tree, tree, tree, int);
205 static rtx expand_builtin_object_size (tree);
206 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
207 enum built_in_function);
208 static void maybe_emit_chk_warning (tree, enum built_in_function);
209 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
210 static tree fold_builtin_object_size (tree, tree);
211 static tree fold_builtin_strcat_chk (tree, tree, tree, tree);
212 static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree);
213 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
214 static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function);
215 static tree fold_builtin_fprintf (tree, tree, tree, tree, bool,
216 enum built_in_function);
217 static bool init_target_chars (void);
219 static unsigned HOST_WIDE_INT target_newline;
220 static unsigned HOST_WIDE_INT target_percent;
221 static unsigned HOST_WIDE_INT target_c;
222 static unsigned HOST_WIDE_INT target_s;
223 static char target_percent_c[3];
224 static char target_percent_s[3];
225 static char target_percent_s_newline[4];
226 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
227 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
228 static tree do_mpfr_arg2 (tree, tree, tree,
229 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
230 static tree do_mpfr_arg3 (tree, tree, tree, tree,
231 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
232 static tree do_mpfr_sincos (tree, tree, tree);
233 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
234 static tree do_mpfr_bessel_n (tree, tree, tree,
235 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
236 const REAL_VALUE_TYPE *, bool);
237 static tree do_mpfr_remquo (tree, tree, tree);
238 static tree do_mpfr_lgamma_r (tree, tree, tree);
239 #endif
241 /* Return true if NODE should be considered for inline expansion regardless
242 of the optimization level. This means whenever a function is invoked with
243 its "internal" name, which normally contains the prefix "__builtin". */
245 static bool called_as_built_in (tree node)
247 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
248 if (strncmp (name, "__builtin_", 10) == 0)
249 return true;
250 if (strncmp (name, "__sync_", 7) == 0)
251 return true;
252 return false;
255 /* Return the alignment in bits of EXP, a pointer valued expression.
256 But don't return more than MAX_ALIGN no matter what.
257 The alignment returned is, by default, the alignment of the thing that
258 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
260 Otherwise, look at the expression to see if we can do better, i.e., if the
261 expression is actually pointing at an object whose alignment is tighter. */
264 get_pointer_alignment (tree exp, unsigned int max_align)
266 unsigned int align, inner;
268 /* We rely on TER to compute accurate alignment information. */
269 if (!(optimize && flag_tree_ter))
270 return 0;
272 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
273 return 0;
275 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
276 align = MIN (align, max_align);
278 while (1)
280 switch (TREE_CODE (exp))
282 case NOP_EXPR:
283 case CONVERT_EXPR:
284 case NON_LVALUE_EXPR:
285 exp = TREE_OPERAND (exp, 0);
286 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
287 return align;
289 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
290 align = MIN (inner, max_align);
291 break;
293 case POINTER_PLUS_EXPR:
294 /* If sum of pointer + int, restrict our maximum alignment to that
295 imposed by the integer. If not, we can't do any better than
296 ALIGN. */
297 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
298 return align;
300 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
301 & (max_align / BITS_PER_UNIT - 1))
302 != 0)
303 max_align >>= 1;
305 exp = TREE_OPERAND (exp, 0);
306 break;
308 case ADDR_EXPR:
309 /* See what we are pointing at and look at its alignment. */
310 exp = TREE_OPERAND (exp, 0);
311 inner = max_align;
312 if (handled_component_p (exp))
314 HOST_WIDE_INT bitsize, bitpos;
315 tree offset;
316 enum machine_mode mode;
317 int unsignedp, volatilep;
319 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
320 &mode, &unsignedp, &volatilep, true);
321 if (bitpos)
322 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
323 if (offset && TREE_CODE (offset) == PLUS_EXPR
324 && host_integerp (TREE_OPERAND (offset, 1), 1))
326 /* Any overflow in calculating offset_bits won't change
327 the alignment. */
328 unsigned offset_bits
329 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
330 * BITS_PER_UNIT);
332 if (offset_bits)
333 inner = MIN (inner, (offset_bits & -offset_bits));
334 offset = TREE_OPERAND (offset, 0);
336 if (offset && TREE_CODE (offset) == MULT_EXPR
337 && host_integerp (TREE_OPERAND (offset, 1), 1))
339 /* Any overflow in calculating offset_factor won't change
340 the alignment. */
341 unsigned offset_factor
342 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
343 * BITS_PER_UNIT);
345 if (offset_factor)
346 inner = MIN (inner, (offset_factor & -offset_factor));
348 else if (offset)
349 inner = MIN (inner, BITS_PER_UNIT);
351 if (DECL_P (exp))
352 align = MIN (inner, DECL_ALIGN (exp));
353 #ifdef CONSTANT_ALIGNMENT
354 else if (CONSTANT_CLASS_P (exp))
355 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
356 #endif
357 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
358 || TREE_CODE (exp) == INDIRECT_REF)
359 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
360 else
361 align = MIN (align, inner);
362 return MIN (align, max_align);
364 default:
365 return align;
370 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
371 way, because it could contain a zero byte in the middle.
372 TREE_STRING_LENGTH is the size of the character array, not the string.
374 ONLY_VALUE should be nonzero if the result is not going to be emitted
375 into the instruction stream and zero if it is going to be expanded.
376 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
377 is returned, otherwise NULL, since
378 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
379 evaluate the side-effects.
381 The value returned is of type `ssizetype'.
383 Unfortunately, string_constant can't access the values of const char
384 arrays with initializers, so neither can we do so here. */
386 tree
387 c_strlen (tree src, int only_value)
389 tree offset_node;
390 HOST_WIDE_INT offset;
391 int max;
392 const char *ptr;
394 STRIP_NOPS (src);
395 if (TREE_CODE (src) == COND_EXPR
396 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
398 tree len1, len2;
400 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
401 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
402 if (tree_int_cst_equal (len1, len2))
403 return len1;
406 if (TREE_CODE (src) == COMPOUND_EXPR
407 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
408 return c_strlen (TREE_OPERAND (src, 1), only_value);
410 src = string_constant (src, &offset_node);
411 if (src == 0)
412 return NULL_TREE;
414 max = TREE_STRING_LENGTH (src) - 1;
415 ptr = TREE_STRING_POINTER (src);
417 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
419 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
420 compute the offset to the following null if we don't know where to
421 start searching for it. */
422 int i;
424 for (i = 0; i < max; i++)
425 if (ptr[i] == 0)
426 return NULL_TREE;
428 /* We don't know the starting offset, but we do know that the string
429 has no internal zero bytes. We can assume that the offset falls
430 within the bounds of the string; otherwise, the programmer deserves
431 what he gets. Subtract the offset from the length of the string,
432 and return that. This would perhaps not be valid if we were dealing
433 with named arrays in addition to literal string constants. */
435 return size_diffop (size_int (max), offset_node);
438 /* We have a known offset into the string. Start searching there for
439 a null character if we can represent it as a single HOST_WIDE_INT. */
440 if (offset_node == 0)
441 offset = 0;
442 else if (! host_integerp (offset_node, 0))
443 offset = -1;
444 else
445 offset = tree_low_cst (offset_node, 0);
447 /* If the offset is known to be out of bounds, warn, and call strlen at
448 runtime. */
449 if (offset < 0 || offset > max)
451 warning (0, "offset outside bounds of constant string");
452 return NULL_TREE;
455 /* Use strlen to search for the first zero byte. Since any strings
456 constructed with build_string will have nulls appended, we win even
457 if we get handed something like (char[4])"abcd".
459 Since OFFSET is our starting index into the string, no further
460 calculation is needed. */
461 return ssize_int (strlen (ptr + offset));
464 /* Return a char pointer for a C string if it is a string constant
465 or sum of string constant and integer constant. */
467 static const char *
468 c_getstr (tree src)
470 tree offset_node;
472 src = string_constant (src, &offset_node);
473 if (src == 0)
474 return 0;
476 if (offset_node == 0)
477 return TREE_STRING_POINTER (src);
478 else if (!host_integerp (offset_node, 1)
479 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
480 return 0;
482 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
485 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
486 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
488 static rtx
489 c_readstr (const char *str, enum machine_mode mode)
491 HOST_WIDE_INT c[2];
492 HOST_WIDE_INT ch;
493 unsigned int i, j;
495 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
497 c[0] = 0;
498 c[1] = 0;
499 ch = 1;
500 for (i = 0; i < GET_MODE_SIZE (mode); i++)
502 j = i;
503 if (WORDS_BIG_ENDIAN)
504 j = GET_MODE_SIZE (mode) - i - 1;
505 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
506 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
507 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
508 j *= BITS_PER_UNIT;
509 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
511 if (ch)
512 ch = (unsigned char) str[i];
513 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
515 return immed_double_const (c[0], c[1], mode);
518 /* Cast a target constant CST to target CHAR and if that value fits into
519 host char type, return zero and put that value into variable pointed to by
520 P. */
522 static int
523 target_char_cast (tree cst, char *p)
525 unsigned HOST_WIDE_INT val, hostval;
527 if (!host_integerp (cst, 1)
528 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
529 return 1;
531 val = tree_low_cst (cst, 1);
532 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
533 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
535 hostval = val;
536 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
537 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
539 if (val != hostval)
540 return 1;
542 *p = hostval;
543 return 0;
546 /* Similar to save_expr, but assumes that arbitrary code is not executed
547 in between the multiple evaluations. In particular, we assume that a
548 non-addressable local variable will not be modified. */
550 static tree
551 builtin_save_expr (tree exp)
553 if (TREE_ADDRESSABLE (exp) == 0
554 && (TREE_CODE (exp) == PARM_DECL
555 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
556 return exp;
558 return save_expr (exp);
561 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
562 times to get the address of either a higher stack frame, or a return
563 address located within it (depending on FNDECL_CODE). */
565 static rtx
566 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
568 int i;
570 #ifdef INITIAL_FRAME_ADDRESS_RTX
571 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
572 #else
573 rtx tem;
575 /* For a zero count with __builtin_return_address, we don't care what
576 frame address we return, because target-specific definitions will
577 override us. Therefore frame pointer elimination is OK, and using
578 the soft frame pointer is OK.
580 For a nonzero count, or a zero count with __builtin_frame_address,
581 we require a stable offset from the current frame pointer to the
582 previous one, so we must use the hard frame pointer, and
583 we must disable frame pointer elimination. */
584 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
585 tem = frame_pointer_rtx;
586 else
588 tem = hard_frame_pointer_rtx;
590 /* Tell reload not to eliminate the frame pointer. */
591 current_function_accesses_prior_frames = 1;
593 #endif
595 /* Some machines need special handling before we can access
596 arbitrary frames. For example, on the SPARC, we must first flush
597 all register windows to the stack. */
598 #ifdef SETUP_FRAME_ADDRESSES
599 if (count > 0)
600 SETUP_FRAME_ADDRESSES ();
601 #endif
603 /* On the SPARC, the return address is not in the frame, it is in a
604 register. There is no way to access it off of the current frame
605 pointer, but it can be accessed off the previous frame pointer by
606 reading the value from the register window save area. */
607 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
608 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
609 count--;
610 #endif
612 /* Scan back COUNT frames to the specified frame. */
613 for (i = 0; i < count; i++)
615 /* Assume the dynamic chain pointer is in the word that the
616 frame address points to, unless otherwise specified. */
617 #ifdef DYNAMIC_CHAIN_ADDRESS
618 tem = DYNAMIC_CHAIN_ADDRESS (tem);
619 #endif
620 tem = memory_address (Pmode, tem);
621 tem = gen_frame_mem (Pmode, tem);
622 tem = copy_to_reg (tem);
625 /* For __builtin_frame_address, return what we've got. But, on
626 the SPARC for example, we may have to add a bias. */
627 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
628 #ifdef FRAME_ADDR_RTX
629 return FRAME_ADDR_RTX (tem);
630 #else
631 return tem;
632 #endif
634 /* For __builtin_return_address, get the return address from that frame. */
635 #ifdef RETURN_ADDR_RTX
636 tem = RETURN_ADDR_RTX (count, tem);
637 #else
638 tem = memory_address (Pmode,
639 plus_constant (tem, GET_MODE_SIZE (Pmode)));
640 tem = gen_frame_mem (Pmode, tem);
641 #endif
642 return tem;
645 /* Alias set used for setjmp buffer. */
646 static alias_set_type setjmp_alias_set = -1;
648 /* Construct the leading half of a __builtin_setjmp call. Control will
649 return to RECEIVER_LABEL. This is also called directly by the SJLJ
650 exception handling code. */
652 void
653 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
655 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
656 rtx stack_save;
657 rtx mem;
659 if (setjmp_alias_set == -1)
660 setjmp_alias_set = new_alias_set ();
662 buf_addr = convert_memory_address (Pmode, buf_addr);
664 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
666 /* We store the frame pointer and the address of receiver_label in
667 the buffer and use the rest of it for the stack save area, which
668 is machine-dependent. */
670 mem = gen_rtx_MEM (Pmode, buf_addr);
671 set_mem_alias_set (mem, setjmp_alias_set);
672 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
674 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
675 set_mem_alias_set (mem, setjmp_alias_set);
677 emit_move_insn (validize_mem (mem),
678 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
680 stack_save = gen_rtx_MEM (sa_mode,
681 plus_constant (buf_addr,
682 2 * GET_MODE_SIZE (Pmode)));
683 set_mem_alias_set (stack_save, setjmp_alias_set);
684 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
686 /* If there is further processing to do, do it. */
687 #ifdef HAVE_builtin_setjmp_setup
688 if (HAVE_builtin_setjmp_setup)
689 emit_insn (gen_builtin_setjmp_setup (buf_addr));
690 #endif
692 /* Tell optimize_save_area_alloca that extra work is going to
693 need to go on during alloca. */
694 current_function_calls_setjmp = 1;
696 /* We have a nonlocal label. */
697 current_function_has_nonlocal_label = 1;
700 /* Construct the trailing part of a __builtin_setjmp call. This is
701 also called directly by the SJLJ exception handling code. */
703 void
704 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
706 /* Clobber the FP when we get here, so we have to make sure it's
707 marked as used by this function. */
708 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
710 /* Mark the static chain as clobbered here so life information
711 doesn't get messed up for it. */
712 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
714 /* Now put in the code to restore the frame pointer, and argument
715 pointer, if needed. */
716 #ifdef HAVE_nonlocal_goto
717 if (! HAVE_nonlocal_goto)
718 #endif
720 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
721 /* This might change the hard frame pointer in ways that aren't
722 apparent to early optimization passes, so force a clobber. */
723 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
726 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
727 if (fixed_regs[ARG_POINTER_REGNUM])
729 #ifdef ELIMINABLE_REGS
730 size_t i;
731 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
733 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
734 if (elim_regs[i].from == ARG_POINTER_REGNUM
735 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
736 break;
738 if (i == ARRAY_SIZE (elim_regs))
739 #endif
741 /* Now restore our arg pointer from the address at which it
742 was saved in our stack frame. */
743 emit_move_insn (virtual_incoming_args_rtx,
744 copy_to_reg (get_arg_pointer_save_area (cfun)));
747 #endif
749 #ifdef HAVE_builtin_setjmp_receiver
750 if (HAVE_builtin_setjmp_receiver)
751 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
752 else
753 #endif
754 #ifdef HAVE_nonlocal_goto_receiver
755 if (HAVE_nonlocal_goto_receiver)
756 emit_insn (gen_nonlocal_goto_receiver ());
757 else
758 #endif
759 { /* Nothing */ }
761 /* We must not allow the code we just generated to be reordered by
762 scheduling. Specifically, the update of the frame pointer must
763 happen immediately, not later. */
764 emit_insn (gen_blockage ());
767 /* __builtin_longjmp is passed a pointer to an array of five words (not
768 all will be used on all machines). It operates similarly to the C
769 library function of the same name, but is more efficient. Much of
770 the code below is copied from the handling of non-local gotos. */
772 static void
773 expand_builtin_longjmp (rtx buf_addr, rtx value)
775 rtx fp, lab, stack, insn, last;
776 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
778 if (setjmp_alias_set == -1)
779 setjmp_alias_set = new_alias_set ();
781 buf_addr = convert_memory_address (Pmode, buf_addr);
783 buf_addr = force_reg (Pmode, buf_addr);
785 /* We used to store value in static_chain_rtx, but that fails if pointers
786 are smaller than integers. We instead require that the user must pass
787 a second argument of 1, because that is what builtin_setjmp will
788 return. This also makes EH slightly more efficient, since we are no
789 longer copying around a value that we don't care about. */
790 gcc_assert (value == const1_rtx);
792 last = get_last_insn ();
793 #ifdef HAVE_builtin_longjmp
794 if (HAVE_builtin_longjmp)
795 emit_insn (gen_builtin_longjmp (buf_addr));
796 else
797 #endif
799 fp = gen_rtx_MEM (Pmode, buf_addr);
800 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
801 GET_MODE_SIZE (Pmode)));
803 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
804 2 * GET_MODE_SIZE (Pmode)));
805 set_mem_alias_set (fp, setjmp_alias_set);
806 set_mem_alias_set (lab, setjmp_alias_set);
807 set_mem_alias_set (stack, setjmp_alias_set);
809 /* Pick up FP, label, and SP from the block and jump. This code is
810 from expand_goto in stmt.c; see there for detailed comments. */
811 #ifdef HAVE_nonlocal_goto
812 if (HAVE_nonlocal_goto)
813 /* We have to pass a value to the nonlocal_goto pattern that will
814 get copied into the static_chain pointer, but it does not matter
815 what that value is, because builtin_setjmp does not use it. */
816 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
817 else
818 #endif
820 lab = copy_to_reg (lab);
822 emit_insn (gen_rtx_CLOBBER (VOIDmode,
823 gen_rtx_MEM (BLKmode,
824 gen_rtx_SCRATCH (VOIDmode))));
825 emit_insn (gen_rtx_CLOBBER (VOIDmode,
826 gen_rtx_MEM (BLKmode,
827 hard_frame_pointer_rtx)));
829 emit_move_insn (hard_frame_pointer_rtx, fp);
830 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
832 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
833 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
834 emit_indirect_jump (lab);
838 /* Search backwards and mark the jump insn as a non-local goto.
839 Note that this precludes the use of __builtin_longjmp to a
840 __builtin_setjmp target in the same function. However, we've
841 already cautioned the user that these functions are for
842 internal exception handling use only. */
843 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
845 gcc_assert (insn != last);
847 if (JUMP_P (insn))
849 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
850 REG_NOTES (insn));
851 break;
853 else if (CALL_P (insn))
854 break;
858 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
859 and the address of the save area. */
861 static rtx
862 expand_builtin_nonlocal_goto (tree exp)
864 tree t_label, t_save_area;
865 rtx r_label, r_save_area, r_fp, r_sp, insn;
867 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
868 return NULL_RTX;
870 t_label = CALL_EXPR_ARG (exp, 0);
871 t_save_area = CALL_EXPR_ARG (exp, 1);
873 r_label = expand_normal (t_label);
874 r_label = convert_memory_address (Pmode, r_label);
875 r_save_area = expand_normal (t_save_area);
876 r_save_area = convert_memory_address (Pmode, r_save_area);
877 r_fp = gen_rtx_MEM (Pmode, r_save_area);
878 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
879 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
881 current_function_has_nonlocal_goto = 1;
883 #ifdef HAVE_nonlocal_goto
884 /* ??? We no longer need to pass the static chain value, afaik. */
885 if (HAVE_nonlocal_goto)
886 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
887 else
888 #endif
890 r_label = copy_to_reg (r_label);
892 emit_insn (gen_rtx_CLOBBER (VOIDmode,
893 gen_rtx_MEM (BLKmode,
894 gen_rtx_SCRATCH (VOIDmode))));
896 emit_insn (gen_rtx_CLOBBER (VOIDmode,
897 gen_rtx_MEM (BLKmode,
898 hard_frame_pointer_rtx)));
900 /* Restore frame pointer for containing function.
901 This sets the actual hard register used for the frame pointer
902 to the location of the function's incoming static chain info.
903 The non-local goto handler will then adjust it to contain the
904 proper value and reload the argument pointer, if needed. */
905 emit_move_insn (hard_frame_pointer_rtx, r_fp);
906 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
908 /* USE of hard_frame_pointer_rtx added for consistency;
909 not clear if really needed. */
910 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
911 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
913 /* If the architecture is using a GP register, we must
914 conservatively assume that the target function makes use of it.
915 The prologue of functions with nonlocal gotos must therefore
916 initialize the GP register to the appropriate value, and we
917 must then make sure that this value is live at the point
918 of the jump. (Note that this doesn't necessarily apply
919 to targets with a nonlocal_goto pattern; they are free
920 to implement it in their own way. Note also that this is
921 a no-op if the GP register is a global invariant.) */
922 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
923 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
924 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
926 emit_indirect_jump (r_label);
929 /* Search backwards to the jump insn and mark it as a
930 non-local goto. */
931 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
933 if (JUMP_P (insn))
935 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
936 const0_rtx, REG_NOTES (insn));
937 break;
939 else if (CALL_P (insn))
940 break;
943 return const0_rtx;
946 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
947 (not all will be used on all machines) that was passed to __builtin_setjmp.
948 It updates the stack pointer in that block to correspond to the current
949 stack pointer. */
951 static void
952 expand_builtin_update_setjmp_buf (rtx buf_addr)
954 enum machine_mode sa_mode = Pmode;
955 rtx stack_save;
958 #ifdef HAVE_save_stack_nonlocal
959 if (HAVE_save_stack_nonlocal)
960 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
961 #endif
962 #ifdef STACK_SAVEAREA_MODE
963 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
964 #endif
966 stack_save
967 = gen_rtx_MEM (sa_mode,
968 memory_address
969 (sa_mode,
970 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
972 #ifdef HAVE_setjmp
973 if (HAVE_setjmp)
974 emit_insn (gen_setjmp ());
975 #endif
977 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
980 /* Expand a call to __builtin_prefetch. For a target that does not support
981 data prefetch, evaluate the memory address argument in case it has side
982 effects. */
984 static void
985 expand_builtin_prefetch (tree exp)
987 tree arg0, arg1, arg2;
988 int nargs;
989 rtx op0, op1, op2;
991 if (!validate_arglist (exp, POINTER_TYPE, 0))
992 return;
994 arg0 = CALL_EXPR_ARG (exp, 0);
996 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
997 zero (read) and argument 2 (locality) defaults to 3 (high degree of
998 locality). */
999 nargs = call_expr_nargs (exp);
1000 if (nargs > 1)
1001 arg1 = CALL_EXPR_ARG (exp, 1);
1002 else
1003 arg1 = integer_zero_node;
1004 if (nargs > 2)
1005 arg2 = CALL_EXPR_ARG (exp, 2);
1006 else
1007 arg2 = build_int_cst (NULL_TREE, 3);
1009 /* Argument 0 is an address. */
1010 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1012 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1013 if (TREE_CODE (arg1) != INTEGER_CST)
1015 error ("second argument to %<__builtin_prefetch%> must be a constant");
1016 arg1 = integer_zero_node;
1018 op1 = expand_normal (arg1);
1019 /* Argument 1 must be either zero or one. */
1020 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1022 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1023 " using zero");
1024 op1 = const0_rtx;
1027 /* Argument 2 (locality) must be a compile-time constant int. */
1028 if (TREE_CODE (arg2) != INTEGER_CST)
1030 error ("third argument to %<__builtin_prefetch%> must be a constant");
1031 arg2 = integer_zero_node;
1033 op2 = expand_normal (arg2);
1034 /* Argument 2 must be 0, 1, 2, or 3. */
1035 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1037 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1038 op2 = const0_rtx;
1041 #ifdef HAVE_prefetch
1042 if (HAVE_prefetch)
1044 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1045 (op0,
1046 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1047 || (GET_MODE (op0) != Pmode))
1049 op0 = convert_memory_address (Pmode, op0);
1050 op0 = force_reg (Pmode, op0);
1052 emit_insn (gen_prefetch (op0, op1, op2));
1054 #endif
1056 /* Don't do anything with direct references to volatile memory, but
1057 generate code to handle other side effects. */
1058 if (!MEM_P (op0) && side_effects_p (op0))
1059 emit_insn (op0);
1062 /* Get a MEM rtx for expression EXP which is the address of an operand
1063 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1064 the maximum length of the block of memory that might be accessed or
1065 NULL if unknown. */
1067 static rtx
1068 get_memory_rtx (tree exp, tree len)
1070 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1071 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1073 /* Get an expression we can use to find the attributes to assign to MEM.
1074 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1075 we can. First remove any nops. */
1076 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1077 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1078 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1079 exp = TREE_OPERAND (exp, 0);
1081 if (TREE_CODE (exp) == ADDR_EXPR)
1082 exp = TREE_OPERAND (exp, 0);
1083 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1084 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1085 else
1086 exp = NULL;
1088 /* Honor attributes derived from exp, except for the alias set
1089 (as builtin stringops may alias with anything) and the size
1090 (as stringops may access multiple array elements). */
1091 if (exp)
1093 set_mem_attributes (mem, exp, 0);
1095 /* Allow the string and memory builtins to overflow from one
1096 field into another, see http://gcc.gnu.org/PR23561.
1097 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1098 memory accessed by the string or memory builtin will fit
1099 within the field. */
1100 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1102 tree mem_expr = MEM_EXPR (mem);
1103 HOST_WIDE_INT offset = -1, length = -1;
1104 tree inner = exp;
1106 while (TREE_CODE (inner) == ARRAY_REF
1107 || TREE_CODE (inner) == NOP_EXPR
1108 || TREE_CODE (inner) == CONVERT_EXPR
1109 || TREE_CODE (inner) == NON_LVALUE_EXPR
1110 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1111 || TREE_CODE (inner) == SAVE_EXPR)
1112 inner = TREE_OPERAND (inner, 0);
1114 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1116 if (MEM_OFFSET (mem)
1117 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1118 offset = INTVAL (MEM_OFFSET (mem));
1120 if (offset >= 0 && len && host_integerp (len, 0))
1121 length = tree_low_cst (len, 0);
1123 while (TREE_CODE (inner) == COMPONENT_REF)
1125 tree field = TREE_OPERAND (inner, 1);
1126 gcc_assert (! DECL_BIT_FIELD (field));
1127 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1128 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1130 if (length >= 0
1131 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1132 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1134 HOST_WIDE_INT size
1135 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1136 /* If we can prove the memory starting at XEXP (mem, 0)
1137 and ending at XEXP (mem, 0) + LENGTH will fit into
1138 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1139 if (offset <= size
1140 && length <= size
1141 && offset + length <= size)
1142 break;
1145 if (offset >= 0
1146 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1147 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1148 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1149 / BITS_PER_UNIT;
1150 else
1152 offset = -1;
1153 length = -1;
1156 mem_expr = TREE_OPERAND (mem_expr, 0);
1157 inner = TREE_OPERAND (inner, 0);
1160 if (mem_expr == NULL)
1161 offset = -1;
1162 if (mem_expr != MEM_EXPR (mem))
1164 set_mem_expr (mem, mem_expr);
1165 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1168 set_mem_alias_set (mem, 0);
1169 set_mem_size (mem, NULL_RTX);
1172 return mem;
1175 /* Built-in functions to perform an untyped call and return. */
1177 /* For each register that may be used for calling a function, this
1178 gives a mode used to copy the register's value. VOIDmode indicates
1179 the register is not used for calling a function. If the machine
1180 has register windows, this gives only the outbound registers.
1181 INCOMING_REGNO gives the corresponding inbound register. */
1182 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1184 /* For each register that may be used for returning values, this gives
1185 a mode used to copy the register's value. VOIDmode indicates the
1186 register is not used for returning values. If the machine has
1187 register windows, this gives only the outbound registers.
1188 INCOMING_REGNO gives the corresponding inbound register. */
1189 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1191 /* For each register that may be used for calling a function, this
1192 gives the offset of that register into the block returned by
1193 __builtin_apply_args. 0 indicates that the register is not
1194 used for calling a function. */
1195 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1197 /* Return the size required for the block returned by __builtin_apply_args,
1198 and initialize apply_args_mode. */
1200 static int
1201 apply_args_size (void)
1203 static int size = -1;
1204 int align;
1205 unsigned int regno;
1206 enum machine_mode mode;
1208 /* The values computed by this function never change. */
1209 if (size < 0)
1211 /* The first value is the incoming arg-pointer. */
1212 size = GET_MODE_SIZE (Pmode);
1214 /* The second value is the structure value address unless this is
1215 passed as an "invisible" first argument. */
1216 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1217 size += GET_MODE_SIZE (Pmode);
1219 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1220 if (FUNCTION_ARG_REGNO_P (regno))
1222 mode = reg_raw_mode[regno];
1224 gcc_assert (mode != VOIDmode);
1226 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1227 if (size % align != 0)
1228 size = CEIL (size, align) * align;
1229 apply_args_reg_offset[regno] = size;
1230 size += GET_MODE_SIZE (mode);
1231 apply_args_mode[regno] = mode;
1233 else
1235 apply_args_mode[regno] = VOIDmode;
1236 apply_args_reg_offset[regno] = 0;
1239 return size;
1242 /* Return the size required for the block returned by __builtin_apply,
1243 and initialize apply_result_mode. */
1245 static int
1246 apply_result_size (void)
1248 static int size = -1;
1249 int align, regno;
1250 enum machine_mode mode;
1252 /* The values computed by this function never change. */
1253 if (size < 0)
1255 size = 0;
1257 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1258 if (FUNCTION_VALUE_REGNO_P (regno))
1260 mode = reg_raw_mode[regno];
1262 gcc_assert (mode != VOIDmode);
1264 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1265 if (size % align != 0)
1266 size = CEIL (size, align) * align;
1267 size += GET_MODE_SIZE (mode);
1268 apply_result_mode[regno] = mode;
1270 else
1271 apply_result_mode[regno] = VOIDmode;
1273 /* Allow targets that use untyped_call and untyped_return to override
1274 the size so that machine-specific information can be stored here. */
1275 #ifdef APPLY_RESULT_SIZE
1276 size = APPLY_RESULT_SIZE;
1277 #endif
1279 return size;
1282 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1283 /* Create a vector describing the result block RESULT. If SAVEP is true,
1284 the result block is used to save the values; otherwise it is used to
1285 restore the values. */
1287 static rtx
1288 result_vector (int savep, rtx result)
1290 int regno, size, align, nelts;
1291 enum machine_mode mode;
1292 rtx reg, mem;
1293 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1295 size = nelts = 0;
1296 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1297 if ((mode = apply_result_mode[regno]) != VOIDmode)
1299 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1300 if (size % align != 0)
1301 size = CEIL (size, align) * align;
1302 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1303 mem = adjust_address (result, mode, size);
1304 savevec[nelts++] = (savep
1305 ? gen_rtx_SET (VOIDmode, mem, reg)
1306 : gen_rtx_SET (VOIDmode, reg, mem));
1307 size += GET_MODE_SIZE (mode);
1309 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1311 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1313 /* Save the state required to perform an untyped call with the same
1314 arguments as were passed to the current function. */
1316 static rtx
1317 expand_builtin_apply_args_1 (void)
1319 rtx registers, tem;
1320 int size, align, regno;
1321 enum machine_mode mode;
1322 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1324 /* Create a block where the arg-pointer, structure value address,
1325 and argument registers can be saved. */
1326 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1328 /* Walk past the arg-pointer and structure value address. */
1329 size = GET_MODE_SIZE (Pmode);
1330 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1331 size += GET_MODE_SIZE (Pmode);
1333 /* Save each register used in calling a function to the block. */
1334 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1335 if ((mode = apply_args_mode[regno]) != VOIDmode)
1337 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1338 if (size % align != 0)
1339 size = CEIL (size, align) * align;
1341 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1343 emit_move_insn (adjust_address (registers, mode, size), tem);
1344 size += GET_MODE_SIZE (mode);
1347 /* Save the arg pointer to the block. */
1348 tem = copy_to_reg (virtual_incoming_args_rtx);
1349 #ifdef STACK_GROWS_DOWNWARD
1350 /* We need the pointer as the caller actually passed them to us, not
1351 as we might have pretended they were passed. Make sure it's a valid
1352 operand, as emit_move_insn isn't expected to handle a PLUS. */
1354 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1355 NULL_RTX);
1356 #endif
1357 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1359 size = GET_MODE_SIZE (Pmode);
1361 /* Save the structure value address unless this is passed as an
1362 "invisible" first argument. */
1363 if (struct_incoming_value)
1365 emit_move_insn (adjust_address (registers, Pmode, size),
1366 copy_to_reg (struct_incoming_value));
1367 size += GET_MODE_SIZE (Pmode);
1370 /* Return the address of the block. */
1371 return copy_addr_to_reg (XEXP (registers, 0));
1374 /* __builtin_apply_args returns block of memory allocated on
1375 the stack into which is stored the arg pointer, structure
1376 value address, static chain, and all the registers that might
1377 possibly be used in performing a function call. The code is
1378 moved to the start of the function so the incoming values are
1379 saved. */
1381 static rtx
1382 expand_builtin_apply_args (void)
1384 /* Don't do __builtin_apply_args more than once in a function.
1385 Save the result of the first call and reuse it. */
1386 if (apply_args_value != 0)
1387 return apply_args_value;
1389 /* When this function is called, it means that registers must be
1390 saved on entry to this function. So we migrate the
1391 call to the first insn of this function. */
1392 rtx temp;
1393 rtx seq;
1395 start_sequence ();
1396 temp = expand_builtin_apply_args_1 ();
1397 seq = get_insns ();
1398 end_sequence ();
1400 apply_args_value = temp;
1402 /* Put the insns after the NOTE that starts the function.
1403 If this is inside a start_sequence, make the outer-level insn
1404 chain current, so the code is placed at the start of the
1405 function. */
1406 push_topmost_sequence ();
1407 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1408 pop_topmost_sequence ();
1409 return temp;
1413 /* Perform an untyped call and save the state required to perform an
1414 untyped return of whatever value was returned by the given function. */
1416 static rtx
1417 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1419 int size, align, regno;
1420 enum machine_mode mode;
1421 rtx incoming_args, result, reg, dest, src, call_insn;
1422 rtx old_stack_level = 0;
1423 rtx call_fusage = 0;
1424 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1426 arguments = convert_memory_address (Pmode, arguments);
1428 /* Create a block where the return registers can be saved. */
1429 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1431 /* Fetch the arg pointer from the ARGUMENTS block. */
1432 incoming_args = gen_reg_rtx (Pmode);
1433 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1434 #ifndef STACK_GROWS_DOWNWARD
1435 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1436 incoming_args, 0, OPTAB_LIB_WIDEN);
1437 #endif
1439 /* Push a new argument block and copy the arguments. Do not allow
1440 the (potential) memcpy call below to interfere with our stack
1441 manipulations. */
1442 do_pending_stack_adjust ();
1443 NO_DEFER_POP;
1445 /* Save the stack with nonlocal if available. */
1446 #ifdef HAVE_save_stack_nonlocal
1447 if (HAVE_save_stack_nonlocal)
1448 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1449 else
1450 #endif
1451 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1453 /* Allocate a block of memory onto the stack and copy the memory
1454 arguments to the outgoing arguments address. */
1455 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1456 dest = virtual_outgoing_args_rtx;
1457 #ifndef STACK_GROWS_DOWNWARD
1458 if (GET_CODE (argsize) == CONST_INT)
1459 dest = plus_constant (dest, -INTVAL (argsize));
1460 else
1461 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1462 #endif
1463 dest = gen_rtx_MEM (BLKmode, dest);
1464 set_mem_align (dest, PARM_BOUNDARY);
1465 src = gen_rtx_MEM (BLKmode, incoming_args);
1466 set_mem_align (src, PARM_BOUNDARY);
1467 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1469 /* Refer to the argument block. */
1470 apply_args_size ();
1471 arguments = gen_rtx_MEM (BLKmode, arguments);
1472 set_mem_align (arguments, PARM_BOUNDARY);
1474 /* Walk past the arg-pointer and structure value address. */
1475 size = GET_MODE_SIZE (Pmode);
1476 if (struct_value)
1477 size += GET_MODE_SIZE (Pmode);
1479 /* Restore each of the registers previously saved. Make USE insns
1480 for each of these registers for use in making the call. */
1481 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1482 if ((mode = apply_args_mode[regno]) != VOIDmode)
1484 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1485 if (size % align != 0)
1486 size = CEIL (size, align) * align;
1487 reg = gen_rtx_REG (mode, regno);
1488 emit_move_insn (reg, adjust_address (arguments, mode, size));
1489 use_reg (&call_fusage, reg);
1490 size += GET_MODE_SIZE (mode);
1493 /* Restore the structure value address unless this is passed as an
1494 "invisible" first argument. */
1495 size = GET_MODE_SIZE (Pmode);
1496 if (struct_value)
1498 rtx value = gen_reg_rtx (Pmode);
1499 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1500 emit_move_insn (struct_value, value);
1501 if (REG_P (struct_value))
1502 use_reg (&call_fusage, struct_value);
1503 size += GET_MODE_SIZE (Pmode);
1506 /* All arguments and registers used for the call are set up by now! */
1507 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1509 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1510 and we don't want to load it into a register as an optimization,
1511 because prepare_call_address already did it if it should be done. */
1512 if (GET_CODE (function) != SYMBOL_REF)
1513 function = memory_address (FUNCTION_MODE, function);
1515 /* Generate the actual call instruction and save the return value. */
1516 #ifdef HAVE_untyped_call
1517 if (HAVE_untyped_call)
1518 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1519 result, result_vector (1, result)));
1520 else
1521 #endif
1522 #ifdef HAVE_call_value
1523 if (HAVE_call_value)
1525 rtx valreg = 0;
1527 /* Locate the unique return register. It is not possible to
1528 express a call that sets more than one return register using
1529 call_value; use untyped_call for that. In fact, untyped_call
1530 only needs to save the return registers in the given block. */
1531 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1532 if ((mode = apply_result_mode[regno]) != VOIDmode)
1534 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1536 valreg = gen_rtx_REG (mode, regno);
1539 emit_call_insn (GEN_CALL_VALUE (valreg,
1540 gen_rtx_MEM (FUNCTION_MODE, function),
1541 const0_rtx, NULL_RTX, const0_rtx));
1543 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1545 else
1546 #endif
1547 gcc_unreachable ();
1549 /* Find the CALL insn we just emitted, and attach the register usage
1550 information. */
1551 call_insn = last_call_insn ();
1552 add_function_usage_to (call_insn, call_fusage);
1554 /* Restore the stack. */
1555 #ifdef HAVE_save_stack_nonlocal
1556 if (HAVE_save_stack_nonlocal)
1557 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1558 else
1559 #endif
1560 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1562 OK_DEFER_POP;
1564 /* Return the address of the result block. */
1565 result = copy_addr_to_reg (XEXP (result, 0));
1566 return convert_memory_address (ptr_mode, result);
1569 /* Perform an untyped return. */
1571 static void
1572 expand_builtin_return (rtx result)
1574 int size, align, regno;
1575 enum machine_mode mode;
1576 rtx reg;
1577 rtx call_fusage = 0;
1579 result = convert_memory_address (Pmode, result);
1581 apply_result_size ();
1582 result = gen_rtx_MEM (BLKmode, result);
1584 #ifdef HAVE_untyped_return
1585 if (HAVE_untyped_return)
1587 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1588 emit_barrier ();
1589 return;
1591 #endif
1593 /* Restore the return value and note that each value is used. */
1594 size = 0;
1595 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1596 if ((mode = apply_result_mode[regno]) != VOIDmode)
1598 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1599 if (size % align != 0)
1600 size = CEIL (size, align) * align;
1601 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1602 emit_move_insn (reg, adjust_address (result, mode, size));
1604 push_to_sequence (call_fusage);
1605 emit_insn (gen_rtx_USE (VOIDmode, reg));
1606 call_fusage = get_insns ();
1607 end_sequence ();
1608 size += GET_MODE_SIZE (mode);
1611 /* Put the USE insns before the return. */
1612 emit_insn (call_fusage);
1614 /* Return whatever values was restored by jumping directly to the end
1615 of the function. */
1616 expand_naked_return ();
1619 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1621 static enum type_class
1622 type_to_class (tree type)
1624 switch (TREE_CODE (type))
1626 case VOID_TYPE: return void_type_class;
1627 case INTEGER_TYPE: return integer_type_class;
1628 case ENUMERAL_TYPE: return enumeral_type_class;
1629 case BOOLEAN_TYPE: return boolean_type_class;
1630 case POINTER_TYPE: return pointer_type_class;
1631 case REFERENCE_TYPE: return reference_type_class;
1632 case OFFSET_TYPE: return offset_type_class;
1633 case REAL_TYPE: return real_type_class;
1634 case COMPLEX_TYPE: return complex_type_class;
1635 case FUNCTION_TYPE: return function_type_class;
1636 case METHOD_TYPE: return method_type_class;
1637 case RECORD_TYPE: return record_type_class;
1638 case UNION_TYPE:
1639 case QUAL_UNION_TYPE: return union_type_class;
1640 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1641 ? string_type_class : array_type_class);
1642 case LANG_TYPE: return lang_type_class;
1643 default: return no_type_class;
1647 /* Expand a call EXP to __builtin_classify_type. */
1649 static rtx
1650 expand_builtin_classify_type (tree exp)
1652 if (call_expr_nargs (exp))
1653 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1654 return GEN_INT (no_type_class);
1657 /* This helper macro, meant to be used in mathfn_built_in below,
1658 determines which among a set of three builtin math functions is
1659 appropriate for a given type mode. The `F' and `L' cases are
1660 automatically generated from the `double' case. */
1661 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1662 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1663 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1664 fcodel = BUILT_IN_MATHFN##L ; break;
1665 /* Similar to above, but appends _R after any F/L suffix. */
1666 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1667 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1668 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1669 fcodel = BUILT_IN_MATHFN##L_R ; break;
1671 /* Return mathematic function equivalent to FN but operating directly
1672 on TYPE, if available. If we can't do the conversion, return zero. */
1673 tree
1674 mathfn_built_in (tree type, enum built_in_function fn)
1676 enum built_in_function fcode, fcodef, fcodel;
1678 switch (fn)
1680 CASE_MATHFN (BUILT_IN_ACOS)
1681 CASE_MATHFN (BUILT_IN_ACOSH)
1682 CASE_MATHFN (BUILT_IN_ASIN)
1683 CASE_MATHFN (BUILT_IN_ASINH)
1684 CASE_MATHFN (BUILT_IN_ATAN)
1685 CASE_MATHFN (BUILT_IN_ATAN2)
1686 CASE_MATHFN (BUILT_IN_ATANH)
1687 CASE_MATHFN (BUILT_IN_CBRT)
1688 CASE_MATHFN (BUILT_IN_CEIL)
1689 CASE_MATHFN (BUILT_IN_CEXPI)
1690 CASE_MATHFN (BUILT_IN_COPYSIGN)
1691 CASE_MATHFN (BUILT_IN_COS)
1692 CASE_MATHFN (BUILT_IN_COSH)
1693 CASE_MATHFN (BUILT_IN_DREM)
1694 CASE_MATHFN (BUILT_IN_ERF)
1695 CASE_MATHFN (BUILT_IN_ERFC)
1696 CASE_MATHFN (BUILT_IN_EXP)
1697 CASE_MATHFN (BUILT_IN_EXP10)
1698 CASE_MATHFN (BUILT_IN_EXP2)
1699 CASE_MATHFN (BUILT_IN_EXPM1)
1700 CASE_MATHFN (BUILT_IN_FABS)
1701 CASE_MATHFN (BUILT_IN_FDIM)
1702 CASE_MATHFN (BUILT_IN_FLOOR)
1703 CASE_MATHFN (BUILT_IN_FMA)
1704 CASE_MATHFN (BUILT_IN_FMAX)
1705 CASE_MATHFN (BUILT_IN_FMIN)
1706 CASE_MATHFN (BUILT_IN_FMOD)
1707 CASE_MATHFN (BUILT_IN_FREXP)
1708 CASE_MATHFN (BUILT_IN_GAMMA)
1709 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1710 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1711 CASE_MATHFN (BUILT_IN_HYPOT)
1712 CASE_MATHFN (BUILT_IN_ILOGB)
1713 CASE_MATHFN (BUILT_IN_INF)
1714 CASE_MATHFN (BUILT_IN_ISINF)
1715 CASE_MATHFN (BUILT_IN_J0)
1716 CASE_MATHFN (BUILT_IN_J1)
1717 CASE_MATHFN (BUILT_IN_JN)
1718 CASE_MATHFN (BUILT_IN_LCEIL)
1719 CASE_MATHFN (BUILT_IN_LDEXP)
1720 CASE_MATHFN (BUILT_IN_LFLOOR)
1721 CASE_MATHFN (BUILT_IN_LGAMMA)
1722 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1723 CASE_MATHFN (BUILT_IN_LLCEIL)
1724 CASE_MATHFN (BUILT_IN_LLFLOOR)
1725 CASE_MATHFN (BUILT_IN_LLRINT)
1726 CASE_MATHFN (BUILT_IN_LLROUND)
1727 CASE_MATHFN (BUILT_IN_LOG)
1728 CASE_MATHFN (BUILT_IN_LOG10)
1729 CASE_MATHFN (BUILT_IN_LOG1P)
1730 CASE_MATHFN (BUILT_IN_LOG2)
1731 CASE_MATHFN (BUILT_IN_LOGB)
1732 CASE_MATHFN (BUILT_IN_LRINT)
1733 CASE_MATHFN (BUILT_IN_LROUND)
1734 CASE_MATHFN (BUILT_IN_MODF)
1735 CASE_MATHFN (BUILT_IN_NAN)
1736 CASE_MATHFN (BUILT_IN_NANS)
1737 CASE_MATHFN (BUILT_IN_NEARBYINT)
1738 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1739 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1740 CASE_MATHFN (BUILT_IN_POW)
1741 CASE_MATHFN (BUILT_IN_POWI)
1742 CASE_MATHFN (BUILT_IN_POW10)
1743 CASE_MATHFN (BUILT_IN_REMAINDER)
1744 CASE_MATHFN (BUILT_IN_REMQUO)
1745 CASE_MATHFN (BUILT_IN_RINT)
1746 CASE_MATHFN (BUILT_IN_ROUND)
1747 CASE_MATHFN (BUILT_IN_SCALB)
1748 CASE_MATHFN (BUILT_IN_SCALBLN)
1749 CASE_MATHFN (BUILT_IN_SCALBN)
1750 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1751 CASE_MATHFN (BUILT_IN_SIN)
1752 CASE_MATHFN (BUILT_IN_SINCOS)
1753 CASE_MATHFN (BUILT_IN_SINH)
1754 CASE_MATHFN (BUILT_IN_SQRT)
1755 CASE_MATHFN (BUILT_IN_TAN)
1756 CASE_MATHFN (BUILT_IN_TANH)
1757 CASE_MATHFN (BUILT_IN_TGAMMA)
1758 CASE_MATHFN (BUILT_IN_TRUNC)
1759 CASE_MATHFN (BUILT_IN_Y0)
1760 CASE_MATHFN (BUILT_IN_Y1)
1761 CASE_MATHFN (BUILT_IN_YN)
1763 default:
1764 return NULL_TREE;
1767 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1768 return implicit_built_in_decls[fcode];
1769 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1770 return implicit_built_in_decls[fcodef];
1771 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1772 return implicit_built_in_decls[fcodel];
1773 else
1774 return NULL_TREE;
1777 /* If errno must be maintained, expand the RTL to check if the result,
1778 TARGET, of a built-in function call, EXP, is NaN, and if so set
1779 errno to EDOM. */
1781 static void
1782 expand_errno_check (tree exp, rtx target)
1784 rtx lab = gen_label_rtx ();
1786 /* Test the result; if it is NaN, set errno=EDOM because
1787 the argument was not in the domain. */
1788 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1789 0, lab);
1791 #ifdef TARGET_EDOM
1792 /* If this built-in doesn't throw an exception, set errno directly. */
1793 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1795 #ifdef GEN_ERRNO_RTX
1796 rtx errno_rtx = GEN_ERRNO_RTX;
1797 #else
1798 rtx errno_rtx
1799 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1800 #endif
1801 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1802 emit_label (lab);
1803 return;
1805 #endif
1807 /* We can't set errno=EDOM directly; let the library call do it.
1808 Pop the arguments right away in case the call gets deleted. */
1809 NO_DEFER_POP;
1810 expand_call (exp, target, 0);
1811 OK_DEFER_POP;
1812 emit_label (lab);
1815 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1816 Return NULL_RTX if a normal call should be emitted rather than expanding
1817 the function in-line. EXP is the expression that is a call to the builtin
1818 function; if convenient, the result should be placed in TARGET.
1819 SUBTARGET may be used as the target for computing one of EXP's operands. */
1821 static rtx
1822 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1824 optab builtin_optab;
1825 rtx op0, insns, before_call;
1826 tree fndecl = get_callee_fndecl (exp);
1827 enum machine_mode mode;
1828 bool errno_set = false;
1829 tree arg;
1831 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1832 return NULL_RTX;
1834 arg = CALL_EXPR_ARG (exp, 0);
1836 switch (DECL_FUNCTION_CODE (fndecl))
1838 CASE_FLT_FN (BUILT_IN_SQRT):
1839 errno_set = ! tree_expr_nonnegative_p (arg);
1840 builtin_optab = sqrt_optab;
1841 break;
1842 CASE_FLT_FN (BUILT_IN_EXP):
1843 errno_set = true; builtin_optab = exp_optab; break;
1844 CASE_FLT_FN (BUILT_IN_EXP10):
1845 CASE_FLT_FN (BUILT_IN_POW10):
1846 errno_set = true; builtin_optab = exp10_optab; break;
1847 CASE_FLT_FN (BUILT_IN_EXP2):
1848 errno_set = true; builtin_optab = exp2_optab; break;
1849 CASE_FLT_FN (BUILT_IN_EXPM1):
1850 errno_set = true; builtin_optab = expm1_optab; break;
1851 CASE_FLT_FN (BUILT_IN_LOGB):
1852 errno_set = true; builtin_optab = logb_optab; break;
1853 CASE_FLT_FN (BUILT_IN_LOG):
1854 errno_set = true; builtin_optab = log_optab; break;
1855 CASE_FLT_FN (BUILT_IN_LOG10):
1856 errno_set = true; builtin_optab = log10_optab; break;
1857 CASE_FLT_FN (BUILT_IN_LOG2):
1858 errno_set = true; builtin_optab = log2_optab; break;
1859 CASE_FLT_FN (BUILT_IN_LOG1P):
1860 errno_set = true; builtin_optab = log1p_optab; break;
1861 CASE_FLT_FN (BUILT_IN_ASIN):
1862 builtin_optab = asin_optab; break;
1863 CASE_FLT_FN (BUILT_IN_ACOS):
1864 builtin_optab = acos_optab; break;
1865 CASE_FLT_FN (BUILT_IN_TAN):
1866 builtin_optab = tan_optab; break;
1867 CASE_FLT_FN (BUILT_IN_ATAN):
1868 builtin_optab = atan_optab; break;
1869 CASE_FLT_FN (BUILT_IN_FLOOR):
1870 builtin_optab = floor_optab; break;
1871 CASE_FLT_FN (BUILT_IN_CEIL):
1872 builtin_optab = ceil_optab; break;
1873 CASE_FLT_FN (BUILT_IN_TRUNC):
1874 builtin_optab = btrunc_optab; break;
1875 CASE_FLT_FN (BUILT_IN_ROUND):
1876 builtin_optab = round_optab; break;
1877 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1878 builtin_optab = nearbyint_optab;
1879 if (flag_trapping_math)
1880 break;
1881 /* Else fallthrough and expand as rint. */
1882 CASE_FLT_FN (BUILT_IN_RINT):
1883 builtin_optab = rint_optab; break;
1884 default:
1885 gcc_unreachable ();
1888 /* Make a suitable register to place result in. */
1889 mode = TYPE_MODE (TREE_TYPE (exp));
1891 if (! flag_errno_math || ! HONOR_NANS (mode))
1892 errno_set = false;
1894 /* Before working hard, check whether the instruction is available. */
1895 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
1897 target = gen_reg_rtx (mode);
1899 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1900 need to expand the argument again. This way, we will not perform
1901 side-effects more the once. */
1902 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
1904 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
1906 start_sequence ();
1908 /* Compute into TARGET.
1909 Set TARGET to wherever the result comes back. */
1910 target = expand_unop (mode, builtin_optab, op0, target, 0);
1912 if (target != 0)
1914 if (errno_set)
1915 expand_errno_check (exp, target);
1917 /* Output the entire sequence. */
1918 insns = get_insns ();
1919 end_sequence ();
1920 emit_insn (insns);
1921 return target;
1924 /* If we were unable to expand via the builtin, stop the sequence
1925 (without outputting the insns) and call to the library function
1926 with the stabilized argument list. */
1927 end_sequence ();
1930 before_call = get_last_insn ();
1932 target = expand_call (exp, target, target == const0_rtx);
1934 /* If this is a sqrt operation and we don't care about errno, try to
1935 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1936 This allows the semantics of the libcall to be visible to the RTL
1937 optimizers. */
1938 if (builtin_optab == sqrt_optab && !errno_set)
1940 /* Search backwards through the insns emitted by expand_call looking
1941 for the instruction with the REG_RETVAL note. */
1942 rtx last = get_last_insn ();
1943 while (last != before_call)
1945 if (find_reg_note (last, REG_RETVAL, NULL))
1947 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1948 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1949 two elements, i.e. symbol_ref(sqrt) and the operand. */
1950 if (note
1951 && GET_CODE (note) == EXPR_LIST
1952 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1953 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1954 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1956 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1957 /* Check operand is a register with expected mode. */
1958 if (operand
1959 && REG_P (operand)
1960 && GET_MODE (operand) == mode)
1962 /* Replace the REG_EQUAL note with a SQRT rtx. */
1963 rtx equiv = gen_rtx_SQRT (mode, operand);
1964 set_unique_reg_note (last, REG_EQUAL, equiv);
1967 break;
1969 last = PREV_INSN (last);
1973 return target;
1976 /* Expand a call to the builtin binary math functions (pow and atan2).
1977 Return NULL_RTX if a normal call should be emitted rather than expanding the
1978 function in-line. EXP is the expression that is a call to the builtin
1979 function; if convenient, the result should be placed in TARGET.
1980 SUBTARGET may be used as the target for computing one of EXP's
1981 operands. */
1983 static rtx
1984 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1986 optab builtin_optab;
1987 rtx op0, op1, insns;
1988 int op1_type = REAL_TYPE;
1989 tree fndecl = get_callee_fndecl (exp);
1990 tree arg0, arg1;
1991 enum machine_mode mode;
1992 bool errno_set = true;
1994 switch (DECL_FUNCTION_CODE (fndecl))
1996 CASE_FLT_FN (BUILT_IN_SCALBN):
1997 CASE_FLT_FN (BUILT_IN_SCALBLN):
1998 CASE_FLT_FN (BUILT_IN_LDEXP):
1999 op1_type = INTEGER_TYPE;
2000 default:
2001 break;
2004 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2005 return NULL_RTX;
2007 arg0 = CALL_EXPR_ARG (exp, 0);
2008 arg1 = CALL_EXPR_ARG (exp, 1);
2010 switch (DECL_FUNCTION_CODE (fndecl))
2012 CASE_FLT_FN (BUILT_IN_POW):
2013 builtin_optab = pow_optab; break;
2014 CASE_FLT_FN (BUILT_IN_ATAN2):
2015 builtin_optab = atan2_optab; break;
2016 CASE_FLT_FN (BUILT_IN_SCALB):
2017 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2018 return 0;
2019 builtin_optab = scalb_optab; break;
2020 CASE_FLT_FN (BUILT_IN_SCALBN):
2021 CASE_FLT_FN (BUILT_IN_SCALBLN):
2022 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2023 return 0;
2024 /* Fall through... */
2025 CASE_FLT_FN (BUILT_IN_LDEXP):
2026 builtin_optab = ldexp_optab; break;
2027 CASE_FLT_FN (BUILT_IN_FMOD):
2028 builtin_optab = fmod_optab; break;
2029 CASE_FLT_FN (BUILT_IN_REMAINDER):
2030 CASE_FLT_FN (BUILT_IN_DREM):
2031 builtin_optab = remainder_optab; break;
2032 default:
2033 gcc_unreachable ();
2036 /* Make a suitable register to place result in. */
2037 mode = TYPE_MODE (TREE_TYPE (exp));
2039 /* Before working hard, check whether the instruction is available. */
2040 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2041 return NULL_RTX;
2043 target = gen_reg_rtx (mode);
2045 if (! flag_errno_math || ! HONOR_NANS (mode))
2046 errno_set = false;
2048 /* Always stabilize the argument list. */
2049 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2050 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2052 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2053 op1 = expand_normal (arg1);
2055 start_sequence ();
2057 /* Compute into TARGET.
2058 Set TARGET to wherever the result comes back. */
2059 target = expand_binop (mode, builtin_optab, op0, op1,
2060 target, 0, OPTAB_DIRECT);
2062 /* If we were unable to expand via the builtin, stop the sequence
2063 (without outputting the insns) and call to the library function
2064 with the stabilized argument list. */
2065 if (target == 0)
2067 end_sequence ();
2068 return expand_call (exp, target, target == const0_rtx);
2071 if (errno_set)
2072 expand_errno_check (exp, target);
2074 /* Output the entire sequence. */
2075 insns = get_insns ();
2076 end_sequence ();
2077 emit_insn (insns);
2079 return target;
2082 /* Expand a call to the builtin sin and cos math functions.
2083 Return NULL_RTX if a normal call should be emitted rather than expanding the
2084 function in-line. EXP is the expression that is a call to the builtin
2085 function; if convenient, the result should be placed in TARGET.
2086 SUBTARGET may be used as the target for computing one of EXP's
2087 operands. */
2089 static rtx
2090 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2092 optab builtin_optab;
2093 rtx op0, insns;
2094 tree fndecl = get_callee_fndecl (exp);
2095 enum machine_mode mode;
2096 tree arg;
2098 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2099 return NULL_RTX;
2101 arg = CALL_EXPR_ARG (exp, 0);
2103 switch (DECL_FUNCTION_CODE (fndecl))
2105 CASE_FLT_FN (BUILT_IN_SIN):
2106 CASE_FLT_FN (BUILT_IN_COS):
2107 builtin_optab = sincos_optab; break;
2108 default:
2109 gcc_unreachable ();
2112 /* Make a suitable register to place result in. */
2113 mode = TYPE_MODE (TREE_TYPE (exp));
2115 /* Check if sincos insn is available, otherwise fallback
2116 to sin or cos insn. */
2117 if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing)
2118 switch (DECL_FUNCTION_CODE (fndecl))
2120 CASE_FLT_FN (BUILT_IN_SIN):
2121 builtin_optab = sin_optab; break;
2122 CASE_FLT_FN (BUILT_IN_COS):
2123 builtin_optab = cos_optab; break;
2124 default:
2125 gcc_unreachable ();
2128 /* Before working hard, check whether the instruction is available. */
2129 if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing)
2131 target = gen_reg_rtx (mode);
2133 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2134 need to expand the argument again. This way, we will not perform
2135 side-effects more the once. */
2136 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2138 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2140 start_sequence ();
2142 /* Compute into TARGET.
2143 Set TARGET to wherever the result comes back. */
2144 if (builtin_optab == sincos_optab)
2146 int result;
2148 switch (DECL_FUNCTION_CODE (fndecl))
2150 CASE_FLT_FN (BUILT_IN_SIN):
2151 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2152 break;
2153 CASE_FLT_FN (BUILT_IN_COS):
2154 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2155 break;
2156 default:
2157 gcc_unreachable ();
2159 gcc_assert (result);
2161 else
2163 target = expand_unop (mode, builtin_optab, op0, target, 0);
2166 if (target != 0)
2168 /* Output the entire sequence. */
2169 insns = get_insns ();
2170 end_sequence ();
2171 emit_insn (insns);
2172 return target;
2175 /* If we were unable to expand via the builtin, stop the sequence
2176 (without outputting the insns) and call to the library function
2177 with the stabilized argument list. */
2178 end_sequence ();
2181 target = expand_call (exp, target, target == const0_rtx);
2183 return target;
2186 /* Expand a call to one of the builtin math functions that operate on
2187 floating point argument and output an integer result (ilogb, isinf,
2188 isnan, etc).
2189 Return 0 if a normal call should be emitted rather than expanding the
2190 function in-line. EXP is the expression that is a call to the builtin
2191 function; if convenient, the result should be placed in TARGET.
2192 SUBTARGET may be used as the target for computing one of EXP's operands. */
2194 static rtx
2195 expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget)
2197 optab builtin_optab = 0;
2198 enum insn_code icode = CODE_FOR_nothing;
2199 rtx op0;
2200 tree fndecl = get_callee_fndecl (exp);
2201 enum machine_mode mode;
2202 bool errno_set = false;
2203 tree arg;
2205 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2206 return NULL_RTX;
2208 arg = CALL_EXPR_ARG (exp, 0);
2210 switch (DECL_FUNCTION_CODE (fndecl))
2212 CASE_FLT_FN (BUILT_IN_ILOGB):
2213 errno_set = true; builtin_optab = ilogb_optab; break;
2214 CASE_FLT_FN (BUILT_IN_ISINF):
2215 builtin_optab = isinf_optab; break;
2216 case BUILT_IN_ISNORMAL:
2217 case BUILT_IN_ISFINITE:
2218 CASE_FLT_FN (BUILT_IN_FINITE):
2219 /* These builtins have no optabs (yet). */
2220 break;
2221 default:
2222 gcc_unreachable ();
2225 /* There's no easy way to detect the case we need to set EDOM. */
2226 if (flag_errno_math && errno_set)
2227 return NULL_RTX;
2229 /* Optab mode depends on the mode of the input argument. */
2230 mode = TYPE_MODE (TREE_TYPE (arg));
2232 if (builtin_optab)
2233 icode = optab_handler (builtin_optab, mode)->insn_code;
2235 /* Before working hard, check whether the instruction is available. */
2236 if (icode != CODE_FOR_nothing)
2238 /* Make a suitable register to place result in. */
2239 if (!target
2240 || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2241 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2243 gcc_assert (insn_data[icode].operand[0].predicate
2244 (target, GET_MODE (target)));
2246 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2247 need to expand the argument again. This way, we will not perform
2248 side-effects more the once. */
2249 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2251 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2253 if (mode != GET_MODE (op0))
2254 op0 = convert_to_mode (mode, op0, 0);
2256 /* Compute into TARGET.
2257 Set TARGET to wherever the result comes back. */
2258 emit_unop_insn (icode, target, op0, UNKNOWN);
2259 return target;
2262 /* If there is no optab, try generic code. */
2263 switch (DECL_FUNCTION_CODE (fndecl))
2265 tree result;
2267 CASE_FLT_FN (BUILT_IN_ISINF):
2269 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
2270 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
2271 tree const type = TREE_TYPE (arg);
2272 REAL_VALUE_TYPE r;
2273 char buf[128];
2275 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2276 real_from_string (&r, buf);
2277 result = build_call_expr (isgr_fn, 2,
2278 fold_build1 (ABS_EXPR, type, arg),
2279 build_real (type, r));
2280 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2282 CASE_FLT_FN (BUILT_IN_FINITE):
2283 case BUILT_IN_ISFINITE:
2285 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
2286 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2287 tree const type = TREE_TYPE (arg);
2288 REAL_VALUE_TYPE r;
2289 char buf[128];
2291 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2292 real_from_string (&r, buf);
2293 result = build_call_expr (isle_fn, 2,
2294 fold_build1 (ABS_EXPR, type, arg),
2295 build_real (type, r));
2296 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2298 case BUILT_IN_ISNORMAL:
2300 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
2301 islessequal(fabs(x),DBL_MAX). */
2302 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
2303 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
2304 tree const type = TREE_TYPE (arg);
2305 REAL_VALUE_TYPE rmax, rmin;
2306 char buf[128];
2308 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
2309 real_from_string (&rmax, buf);
2310 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
2311 real_from_string (&rmin, buf);
2312 arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
2313 result = build_call_expr (isle_fn, 2, arg,
2314 build_real (type, rmax));
2315 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
2316 build_call_expr (isge_fn, 2, arg,
2317 build_real (type, rmin)));
2318 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
2320 default:
2321 break;
2324 target = expand_call (exp, target, target == const0_rtx);
2326 return target;
2329 /* Expand a call to the builtin sincos math function.
2330 Return NULL_RTX if a normal call should be emitted rather than expanding the
2331 function in-line. EXP is the expression that is a call to the builtin
2332 function. */
2334 static rtx
2335 expand_builtin_sincos (tree exp)
2337 rtx op0, op1, op2, target1, target2;
2338 enum machine_mode mode;
2339 tree arg, sinp, cosp;
2340 int result;
2342 if (!validate_arglist (exp, REAL_TYPE,
2343 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2344 return NULL_RTX;
2346 arg = CALL_EXPR_ARG (exp, 0);
2347 sinp = CALL_EXPR_ARG (exp, 1);
2348 cosp = CALL_EXPR_ARG (exp, 2);
2350 /* Make a suitable register to place result in. */
2351 mode = TYPE_MODE (TREE_TYPE (arg));
2353 /* Check if sincos insn is available, otherwise emit the call. */
2354 if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing)
2355 return NULL_RTX;
2357 target1 = gen_reg_rtx (mode);
2358 target2 = gen_reg_rtx (mode);
2360 op0 = expand_normal (arg);
2361 op1 = expand_normal (build_fold_indirect_ref (sinp));
2362 op2 = expand_normal (build_fold_indirect_ref (cosp));
2364 /* Compute into target1 and target2.
2365 Set TARGET to wherever the result comes back. */
2366 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2367 gcc_assert (result);
2369 /* Move target1 and target2 to the memory locations indicated
2370 by op1 and op2. */
2371 emit_move_insn (op1, target1);
2372 emit_move_insn (op2, target2);
2374 return const0_rtx;
2377 /* Expand a call to the internal cexpi builtin to the sincos math function.
2378 EXP is the expression that is a call to the builtin function; if convenient,
2379 the result should be placed in TARGET. SUBTARGET may be used as the target
2380 for computing one of EXP's operands. */
2382 static rtx
2383 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2385 tree fndecl = get_callee_fndecl (exp);
2386 tree arg, type;
2387 enum machine_mode mode;
2388 rtx op0, op1, op2;
2390 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2391 return NULL_RTX;
2393 arg = CALL_EXPR_ARG (exp, 0);
2394 type = TREE_TYPE (arg);
2395 mode = TYPE_MODE (TREE_TYPE (arg));
2397 /* Try expanding via a sincos optab, fall back to emitting a libcall
2398 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2399 is only generated from sincos, cexp or if we have either of them. */
2400 if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing)
2402 op1 = gen_reg_rtx (mode);
2403 op2 = gen_reg_rtx (mode);
2405 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2407 /* Compute into op1 and op2. */
2408 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2410 else if (TARGET_HAS_SINCOS)
2412 tree call, fn = NULL_TREE;
2413 tree top1, top2;
2414 rtx op1a, op2a;
2416 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2417 fn = built_in_decls[BUILT_IN_SINCOSF];
2418 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2419 fn = built_in_decls[BUILT_IN_SINCOS];
2420 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2421 fn = built_in_decls[BUILT_IN_SINCOSL];
2422 else
2423 gcc_unreachable ();
2425 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2426 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2427 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2428 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2429 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2430 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2432 /* Make sure not to fold the sincos call again. */
2433 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2434 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2435 call, 3, arg, top1, top2));
2437 else
2439 tree call, fn = NULL_TREE, narg;
2440 tree ctype = build_complex_type (type);
2442 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2443 fn = built_in_decls[BUILT_IN_CEXPF];
2444 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2445 fn = built_in_decls[BUILT_IN_CEXP];
2446 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2447 fn = built_in_decls[BUILT_IN_CEXPL];
2448 else
2449 gcc_unreachable ();
2451 /* If we don't have a decl for cexp create one. This is the
2452 friendliest fallback if the user calls __builtin_cexpi
2453 without full target C99 function support. */
2454 if (fn == NULL_TREE)
2456 tree fntype;
2457 const char *name = NULL;
2459 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2460 name = "cexpf";
2461 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2462 name = "cexp";
2463 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2464 name = "cexpl";
2466 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2467 fn = build_fn_decl (name, fntype);
2470 narg = fold_build2 (COMPLEX_EXPR, ctype,
2471 build_real (type, dconst0), arg);
2473 /* Make sure not to fold the cexp call again. */
2474 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2475 return expand_expr (build_call_nary (ctype, call, 1, narg),
2476 target, VOIDmode, EXPAND_NORMAL);
2479 /* Now build the proper return type. */
2480 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2481 make_tree (TREE_TYPE (arg), op2),
2482 make_tree (TREE_TYPE (arg), op1)),
2483 target, VOIDmode, EXPAND_NORMAL);
2486 /* Expand a call to one of the builtin rounding functions gcc defines
2487 as an extension (lfloor and lceil). As these are gcc extensions we
2488 do not need to worry about setting errno to EDOM.
2489 If expanding via optab fails, lower expression to (int)(floor(x)).
2490 EXP is the expression that is a call to the builtin function;
2491 if convenient, the result should be placed in TARGET. SUBTARGET may
2492 be used as the target for computing one of EXP's operands. */
2494 static rtx
2495 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2497 convert_optab builtin_optab;
2498 rtx op0, insns, tmp;
2499 tree fndecl = get_callee_fndecl (exp);
2500 enum built_in_function fallback_fn;
2501 tree fallback_fndecl;
2502 enum machine_mode mode;
2503 tree arg;
2505 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2506 gcc_unreachable ();
2508 arg = CALL_EXPR_ARG (exp, 0);
2510 switch (DECL_FUNCTION_CODE (fndecl))
2512 CASE_FLT_FN (BUILT_IN_LCEIL):
2513 CASE_FLT_FN (BUILT_IN_LLCEIL):
2514 builtin_optab = lceil_optab;
2515 fallback_fn = BUILT_IN_CEIL;
2516 break;
2518 CASE_FLT_FN (BUILT_IN_LFLOOR):
2519 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2520 builtin_optab = lfloor_optab;
2521 fallback_fn = BUILT_IN_FLOOR;
2522 break;
2524 default:
2525 gcc_unreachable ();
2528 /* Make a suitable register to place result in. */
2529 mode = TYPE_MODE (TREE_TYPE (exp));
2531 target = gen_reg_rtx (mode);
2533 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2534 need to expand the argument again. This way, we will not perform
2535 side-effects more the once. */
2536 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2538 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2540 start_sequence ();
2542 /* Compute into TARGET. */
2543 if (expand_sfix_optab (target, op0, builtin_optab))
2545 /* Output the entire sequence. */
2546 insns = get_insns ();
2547 end_sequence ();
2548 emit_insn (insns);
2549 return target;
2552 /* If we were unable to expand via the builtin, stop the sequence
2553 (without outputting the insns). */
2554 end_sequence ();
2556 /* Fall back to floating point rounding optab. */
2557 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2559 /* For non-C99 targets we may end up without a fallback fndecl here
2560 if the user called __builtin_lfloor directly. In this case emit
2561 a call to the floor/ceil variants nevertheless. This should result
2562 in the best user experience for not full C99 targets. */
2563 if (fallback_fndecl == NULL_TREE)
2565 tree fntype;
2566 const char *name = NULL;
2568 switch (DECL_FUNCTION_CODE (fndecl))
2570 case BUILT_IN_LCEIL:
2571 case BUILT_IN_LLCEIL:
2572 name = "ceil";
2573 break;
2574 case BUILT_IN_LCEILF:
2575 case BUILT_IN_LLCEILF:
2576 name = "ceilf";
2577 break;
2578 case BUILT_IN_LCEILL:
2579 case BUILT_IN_LLCEILL:
2580 name = "ceill";
2581 break;
2582 case BUILT_IN_LFLOOR:
2583 case BUILT_IN_LLFLOOR:
2584 name = "floor";
2585 break;
2586 case BUILT_IN_LFLOORF:
2587 case BUILT_IN_LLFLOORF:
2588 name = "floorf";
2589 break;
2590 case BUILT_IN_LFLOORL:
2591 case BUILT_IN_LLFLOORL:
2592 name = "floorl";
2593 break;
2594 default:
2595 gcc_unreachable ();
2598 fntype = build_function_type_list (TREE_TYPE (arg),
2599 TREE_TYPE (arg), NULL_TREE);
2600 fallback_fndecl = build_fn_decl (name, fntype);
2603 exp = build_call_expr (fallback_fndecl, 1, arg);
2605 tmp = expand_normal (exp);
2607 /* Truncate the result of floating point optab to integer
2608 via expand_fix (). */
2609 target = gen_reg_rtx (mode);
2610 expand_fix (target, tmp, 0);
2612 return target;
2615 /* Expand a call to one of the builtin math functions doing integer
2616 conversion (lrint).
2617 Return 0 if a normal call should be emitted rather than expanding the
2618 function in-line. EXP is the expression that is a call to the builtin
2619 function; if convenient, the result should be placed in TARGET.
2620 SUBTARGET may be used as the target for computing one of EXP's operands. */
2622 static rtx
2623 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2625 convert_optab builtin_optab;
2626 rtx op0, insns;
2627 tree fndecl = get_callee_fndecl (exp);
2628 tree arg;
2629 enum machine_mode mode;
2631 /* There's no easy way to detect the case we need to set EDOM. */
2632 if (flag_errno_math)
2633 return NULL_RTX;
2635 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2636 gcc_unreachable ();
2638 arg = CALL_EXPR_ARG (exp, 0);
2640 switch (DECL_FUNCTION_CODE (fndecl))
2642 CASE_FLT_FN (BUILT_IN_LRINT):
2643 CASE_FLT_FN (BUILT_IN_LLRINT):
2644 builtin_optab = lrint_optab; break;
2645 CASE_FLT_FN (BUILT_IN_LROUND):
2646 CASE_FLT_FN (BUILT_IN_LLROUND):
2647 builtin_optab = lround_optab; break;
2648 default:
2649 gcc_unreachable ();
2652 /* Make a suitable register to place result in. */
2653 mode = TYPE_MODE (TREE_TYPE (exp));
2655 target = gen_reg_rtx (mode);
2657 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2658 need to expand the argument again. This way, we will not perform
2659 side-effects more the once. */
2660 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2662 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2664 start_sequence ();
2666 if (expand_sfix_optab (target, op0, builtin_optab))
2668 /* Output the entire sequence. */
2669 insns = get_insns ();
2670 end_sequence ();
2671 emit_insn (insns);
2672 return target;
2675 /* If we were unable to expand via the builtin, stop the sequence
2676 (without outputting the insns) and call to the library function
2677 with the stabilized argument list. */
2678 end_sequence ();
2680 target = expand_call (exp, target, target == const0_rtx);
2682 return target;
2685 /* To evaluate powi(x,n), the floating point value x raised to the
2686 constant integer exponent n, we use a hybrid algorithm that
2687 combines the "window method" with look-up tables. For an
2688 introduction to exponentiation algorithms and "addition chains",
2689 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2690 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2691 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2692 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2694 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2695 multiplications to inline before calling the system library's pow
2696 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2697 so this default never requires calling pow, powf or powl. */
2699 #ifndef POWI_MAX_MULTS
2700 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2701 #endif
2703 /* The size of the "optimal power tree" lookup table. All
2704 exponents less than this value are simply looked up in the
2705 powi_table below. This threshold is also used to size the
2706 cache of pseudo registers that hold intermediate results. */
2707 #define POWI_TABLE_SIZE 256
2709 /* The size, in bits of the window, used in the "window method"
2710 exponentiation algorithm. This is equivalent to a radix of
2711 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2712 #define POWI_WINDOW_SIZE 3
2714 /* The following table is an efficient representation of an
2715 "optimal power tree". For each value, i, the corresponding
2716 value, j, in the table states than an optimal evaluation
2717 sequence for calculating pow(x,i) can be found by evaluating
2718 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2719 100 integers is given in Knuth's "Seminumerical algorithms". */
2721 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2723 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2724 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2725 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2726 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2727 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2728 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2729 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2730 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2731 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2732 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2733 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2734 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2735 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2736 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2737 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2738 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2739 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2740 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2741 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2742 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2743 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2744 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2745 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2746 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2747 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2748 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2749 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2750 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2751 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2752 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2753 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2754 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2758 /* Return the number of multiplications required to calculate
2759 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2760 subroutine of powi_cost. CACHE is an array indicating
2761 which exponents have already been calculated. */
2763 static int
2764 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2766 /* If we've already calculated this exponent, then this evaluation
2767 doesn't require any additional multiplications. */
2768 if (cache[n])
2769 return 0;
2771 cache[n] = true;
2772 return powi_lookup_cost (n - powi_table[n], cache)
2773 + powi_lookup_cost (powi_table[n], cache) + 1;
2776 /* Return the number of multiplications required to calculate
2777 powi(x,n) for an arbitrary x, given the exponent N. This
2778 function needs to be kept in sync with expand_powi below. */
2780 static int
2781 powi_cost (HOST_WIDE_INT n)
2783 bool cache[POWI_TABLE_SIZE];
2784 unsigned HOST_WIDE_INT digit;
2785 unsigned HOST_WIDE_INT val;
2786 int result;
2788 if (n == 0)
2789 return 0;
2791 /* Ignore the reciprocal when calculating the cost. */
2792 val = (n < 0) ? -n : n;
2794 /* Initialize the exponent cache. */
2795 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2796 cache[1] = true;
2798 result = 0;
2800 while (val >= POWI_TABLE_SIZE)
2802 if (val & 1)
2804 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2805 result += powi_lookup_cost (digit, cache)
2806 + POWI_WINDOW_SIZE + 1;
2807 val >>= POWI_WINDOW_SIZE;
2809 else
2811 val >>= 1;
2812 result++;
2816 return result + powi_lookup_cost (val, cache);
2819 /* Recursive subroutine of expand_powi. This function takes the array,
2820 CACHE, of already calculated exponents and an exponent N and returns
2821 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2823 static rtx
2824 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2826 unsigned HOST_WIDE_INT digit;
2827 rtx target, result;
2828 rtx op0, op1;
2830 if (n < POWI_TABLE_SIZE)
2832 if (cache[n])
2833 return cache[n];
2835 target = gen_reg_rtx (mode);
2836 cache[n] = target;
2838 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2839 op1 = expand_powi_1 (mode, powi_table[n], cache);
2841 else if (n & 1)
2843 target = gen_reg_rtx (mode);
2844 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2845 op0 = expand_powi_1 (mode, n - digit, cache);
2846 op1 = expand_powi_1 (mode, digit, cache);
2848 else
2850 target = gen_reg_rtx (mode);
2851 op0 = expand_powi_1 (mode, n >> 1, cache);
2852 op1 = op0;
2855 result = expand_mult (mode, op0, op1, target, 0);
2856 if (result != target)
2857 emit_move_insn (target, result);
2858 return target;
2861 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2862 floating point operand in mode MODE, and N is the exponent. This
2863 function needs to be kept in sync with powi_cost above. */
2865 static rtx
2866 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2868 unsigned HOST_WIDE_INT val;
2869 rtx cache[POWI_TABLE_SIZE];
2870 rtx result;
2872 if (n == 0)
2873 return CONST1_RTX (mode);
2875 val = (n < 0) ? -n : n;
2877 memset (cache, 0, sizeof (cache));
2878 cache[1] = x;
2880 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2882 /* If the original exponent was negative, reciprocate the result. */
2883 if (n < 0)
2884 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2885 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2887 return result;
2890 /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if
2891 a normal call should be emitted rather than expanding the function
2892 in-line. EXP is the expression that is a call to the builtin
2893 function; if convenient, the result should be placed in TARGET. */
2895 static rtx
2896 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2898 tree arg0, arg1;
2899 tree fn, narg0;
2900 tree type = TREE_TYPE (exp);
2901 REAL_VALUE_TYPE cint, c, c2;
2902 HOST_WIDE_INT n;
2903 rtx op, op2;
2904 enum machine_mode mode = TYPE_MODE (type);
2906 if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2907 return NULL_RTX;
2909 arg0 = CALL_EXPR_ARG (exp, 0);
2910 arg1 = CALL_EXPR_ARG (exp, 1);
2912 if (TREE_CODE (arg1) != REAL_CST
2913 || TREE_OVERFLOW (arg1))
2914 return expand_builtin_mathfn_2 (exp, target, subtarget);
2916 /* Handle constant exponents. */
2918 /* For integer valued exponents we can expand to an optimal multiplication
2919 sequence using expand_powi. */
2920 c = TREE_REAL_CST (arg1);
2921 n = real_to_integer (&c);
2922 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2923 if (real_identical (&c, &cint)
2924 && ((n >= -1 && n <= 2)
2925 || (flag_unsafe_math_optimizations
2926 && !optimize_size
2927 && powi_cost (n) <= POWI_MAX_MULTS)))
2929 op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2930 if (n != 1)
2932 op = force_reg (mode, op);
2933 op = expand_powi (op, mode, n);
2935 return op;
2938 narg0 = builtin_save_expr (arg0);
2940 /* If the exponent is not integer valued, check if it is half of an integer.
2941 In this case we can expand to sqrt (x) * x**(n/2). */
2942 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2943 if (fn != NULL_TREE)
2945 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2946 n = real_to_integer (&c2);
2947 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2948 if (real_identical (&c2, &cint)
2949 && ((flag_unsafe_math_optimizations
2950 && !optimize_size
2951 && powi_cost (n/2) <= POWI_MAX_MULTS)
2952 || n == 1))
2954 tree call_expr = build_call_expr (fn, 1, narg0);
2955 /* Use expand_expr in case the newly built call expression
2956 was folded to a non-call. */
2957 op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL);
2958 if (n != 1)
2960 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
2961 op2 = force_reg (mode, op2);
2962 op2 = expand_powi (op2, mode, abs (n / 2));
2963 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2964 0, OPTAB_LIB_WIDEN);
2965 /* If the original exponent was negative, reciprocate the
2966 result. */
2967 if (n < 0)
2968 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2969 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2971 return op;
2975 /* Try if the exponent is a third of an integer. In this case
2976 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2977 different from pow (x, 1./3.) due to rounding and behavior
2978 with negative x we need to constrain this transformation to
2979 unsafe math and positive x or finite math. */
2980 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2981 if (fn != NULL_TREE
2982 && flag_unsafe_math_optimizations
2983 && (tree_expr_nonnegative_p (arg0)
2984 || !HONOR_NANS (mode)))
2986 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2987 real_round (&c2, mode, &c2);
2988 n = real_to_integer (&c2);
2989 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2990 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2991 real_convert (&c2, mode, &c2);
2992 if (real_identical (&c2, &c)
2993 && ((!optimize_size
2994 && powi_cost (n/3) <= POWI_MAX_MULTS)
2995 || n == 1))
2997 tree call_expr = build_call_expr (fn, 1,narg0);
2998 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2999 if (abs (n) % 3 == 2)
3000 op = expand_simple_binop (mode, MULT, op, op, op,
3001 0, OPTAB_LIB_WIDEN);
3002 if (n != 1)
3004 op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL);
3005 op2 = force_reg (mode, op2);
3006 op2 = expand_powi (op2, mode, abs (n / 3));
3007 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
3008 0, OPTAB_LIB_WIDEN);
3009 /* If the original exponent was negative, reciprocate the
3010 result. */
3011 if (n < 0)
3012 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
3013 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
3015 return op;
3019 /* Fall back to optab expansion. */
3020 return expand_builtin_mathfn_2 (exp, target, subtarget);
3023 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3024 a normal call should be emitted rather than expanding the function
3025 in-line. EXP is the expression that is a call to the builtin
3026 function; if convenient, the result should be placed in TARGET. */
3028 static rtx
3029 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
3031 tree arg0, arg1;
3032 rtx op0, op1;
3033 enum machine_mode mode;
3034 enum machine_mode mode2;
3036 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3037 return NULL_RTX;
3039 arg0 = CALL_EXPR_ARG (exp, 0);
3040 arg1 = CALL_EXPR_ARG (exp, 1);
3041 mode = TYPE_MODE (TREE_TYPE (exp));
3043 /* Handle constant power. */
3045 if (TREE_CODE (arg1) == INTEGER_CST
3046 && !TREE_OVERFLOW (arg1))
3048 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
3050 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
3051 Otherwise, check the number of multiplications required. */
3052 if ((TREE_INT_CST_HIGH (arg1) == 0
3053 || TREE_INT_CST_HIGH (arg1) == -1)
3054 && ((n >= -1 && n <= 2)
3055 || (! optimize_size
3056 && powi_cost (n) <= POWI_MAX_MULTS)))
3058 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
3059 op0 = force_reg (mode, op0);
3060 return expand_powi (op0, mode, n);
3064 /* Emit a libcall to libgcc. */
3066 /* Mode of the 2nd argument must match that of an int. */
3067 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
3069 if (target == NULL_RTX)
3070 target = gen_reg_rtx (mode);
3072 op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL);
3073 if (GET_MODE (op0) != mode)
3074 op0 = convert_to_mode (mode, op0, 0);
3075 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3076 if (GET_MODE (op1) != mode2)
3077 op1 = convert_to_mode (mode2, op1, 0);
3079 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3080 target, LCT_CONST_MAKE_BLOCK, mode, 2,
3081 op0, mode, op1, mode2);
3083 return target;
3086 /* Expand expression EXP which is a call to the strlen builtin. Return
3087 NULL_RTX if we failed the caller should emit a normal call, otherwise
3088 try to get the result in TARGET, if convenient. */
3090 static rtx
3091 expand_builtin_strlen (tree exp, rtx target,
3092 enum machine_mode target_mode)
3094 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3095 return NULL_RTX;
3096 else
3098 rtx pat;
3099 tree len;
3100 tree src = CALL_EXPR_ARG (exp, 0);
3101 rtx result, src_reg, char_rtx, before_strlen;
3102 enum machine_mode insn_mode = target_mode, char_mode;
3103 enum insn_code icode = CODE_FOR_nothing;
3104 int align;
3106 /* If the length can be computed at compile-time, return it. */
3107 len = c_strlen (src, 0);
3108 if (len)
3109 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3111 /* If the length can be computed at compile-time and is constant
3112 integer, but there are side-effects in src, evaluate
3113 src for side-effects, then return len.
3114 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3115 can be optimized into: i++; x = 3; */
3116 len = c_strlen (src, 1);
3117 if (len && TREE_CODE (len) == INTEGER_CST)
3119 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3120 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3123 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3125 /* If SRC is not a pointer type, don't do this operation inline. */
3126 if (align == 0)
3127 return NULL_RTX;
3129 /* Bail out if we can't compute strlen in the right mode. */
3130 while (insn_mode != VOIDmode)
3132 icode = optab_handler (strlen_optab, insn_mode)->insn_code;
3133 if (icode != CODE_FOR_nothing)
3134 break;
3136 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3138 if (insn_mode == VOIDmode)
3139 return NULL_RTX;
3141 /* Make a place to write the result of the instruction. */
3142 result = target;
3143 if (! (result != 0
3144 && REG_P (result)
3145 && GET_MODE (result) == insn_mode
3146 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3147 result = gen_reg_rtx (insn_mode);
3149 /* Make a place to hold the source address. We will not expand
3150 the actual source until we are sure that the expansion will
3151 not fail -- there are trees that cannot be expanded twice. */
3152 src_reg = gen_reg_rtx (Pmode);
3154 /* Mark the beginning of the strlen sequence so we can emit the
3155 source operand later. */
3156 before_strlen = get_last_insn ();
3158 char_rtx = const0_rtx;
3159 char_mode = insn_data[(int) icode].operand[2].mode;
3160 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
3161 char_mode))
3162 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
3164 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
3165 char_rtx, GEN_INT (align));
3166 if (! pat)
3167 return NULL_RTX;
3168 emit_insn (pat);
3170 /* Now that we are assured of success, expand the source. */
3171 start_sequence ();
3172 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
3173 if (pat != src_reg)
3174 emit_move_insn (src_reg, pat);
3175 pat = get_insns ();
3176 end_sequence ();
3178 if (before_strlen)
3179 emit_insn_after (pat, before_strlen);
3180 else
3181 emit_insn_before (pat, get_insns ());
3183 /* Return the value in the proper mode for this function. */
3184 if (GET_MODE (result) == target_mode)
3185 target = result;
3186 else if (target != 0)
3187 convert_move (target, result, 0);
3188 else
3189 target = convert_to_mode (target_mode, result, 0);
3191 return target;
3195 /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the
3196 caller should emit a normal call, otherwise try to get the result
3197 in TARGET, if convenient (and in mode MODE if that's convenient). */
3199 static rtx
3200 expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode)
3202 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3204 tree type = TREE_TYPE (exp);
3205 tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0),
3206 CALL_EXPR_ARG (exp, 1), type);
3207 if (result)
3208 return expand_expr (result, target, mode, EXPAND_NORMAL);
3210 return NULL_RTX;
3213 /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the
3214 caller should emit a normal call, otherwise try to get the result
3215 in TARGET, if convenient (and in mode MODE if that's convenient). */
3217 static rtx
3218 expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode)
3220 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3222 tree type = TREE_TYPE (exp);
3223 tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0),
3224 CALL_EXPR_ARG (exp, 1), type);
3225 if (result)
3226 return expand_expr (result, target, mode, EXPAND_NORMAL);
3228 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3230 return NULL_RTX;
3233 /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the
3234 caller should emit a normal call, otherwise try to get the result
3235 in TARGET, if convenient (and in mode MODE if that's convenient). */
3237 static rtx
3238 expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode)
3240 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3242 tree type = TREE_TYPE (exp);
3243 tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0),
3244 CALL_EXPR_ARG (exp, 1), type);
3245 if (result)
3246 return expand_expr (result, target, mode, EXPAND_NORMAL);
3248 return NULL_RTX;
3251 /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the
3252 caller should emit a normal call, otherwise try to get the result
3253 in TARGET, if convenient (and in mode MODE if that's convenient). */
3255 static rtx
3256 expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode)
3258 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3260 tree type = TREE_TYPE (exp);
3261 tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0),
3262 CALL_EXPR_ARG (exp, 1), type);
3263 if (result)
3264 return expand_expr (result, target, mode, EXPAND_NORMAL);
3266 return NULL_RTX;
3269 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3270 bytes from constant string DATA + OFFSET and return it as target
3271 constant. */
3273 static rtx
3274 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3275 enum machine_mode mode)
3277 const char *str = (const char *) data;
3279 gcc_assert (offset >= 0
3280 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3281 <= strlen (str) + 1));
3283 return c_readstr (str + offset, mode);
3286 /* Expand a call EXP to the memcpy builtin.
3287 Return NULL_RTX if we failed, the caller should emit a normal call,
3288 otherwise try to get the result in TARGET, if convenient (and in
3289 mode MODE if that's convenient). */
3291 static rtx
3292 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3294 tree fndecl = get_callee_fndecl (exp);
3296 if (!validate_arglist (exp,
3297 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3298 return NULL_RTX;
3299 else
3301 tree dest = CALL_EXPR_ARG (exp, 0);
3302 tree src = CALL_EXPR_ARG (exp, 1);
3303 tree len = CALL_EXPR_ARG (exp, 2);
3304 const char *src_str;
3305 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3306 unsigned int dest_align
3307 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3308 rtx dest_mem, src_mem, dest_addr, len_rtx;
3309 tree result = fold_builtin_memory_op (dest, src, len,
3310 TREE_TYPE (TREE_TYPE (fndecl)),
3311 false, /*endp=*/0);
3312 HOST_WIDE_INT expected_size = -1;
3313 unsigned int expected_align = 0;
3315 if (result)
3317 while (TREE_CODE (result) == COMPOUND_EXPR)
3319 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3320 EXPAND_NORMAL);
3321 result = TREE_OPERAND (result, 1);
3323 return expand_expr (result, target, mode, EXPAND_NORMAL);
3326 /* If DEST is not a pointer type, call the normal function. */
3327 if (dest_align == 0)
3328 return NULL_RTX;
3330 /* If either SRC is not a pointer type, don't do this
3331 operation in-line. */
3332 if (src_align == 0)
3333 return NULL_RTX;
3335 stringop_block_profile (exp, &expected_align, &expected_size);
3336 if (expected_align < dest_align)
3337 expected_align = dest_align;
3338 dest_mem = get_memory_rtx (dest, len);
3339 set_mem_align (dest_mem, dest_align);
3340 len_rtx = expand_normal (len);
3341 src_str = c_getstr (src);
3343 /* If SRC is a string constant and block move would be done
3344 by pieces, we can avoid loading the string from memory
3345 and only stored the computed constants. */
3346 if (src_str
3347 && GET_CODE (len_rtx) == CONST_INT
3348 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3349 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3350 (void *) src_str, dest_align, false))
3352 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3353 builtin_memcpy_read_str,
3354 (void *) src_str, dest_align, false, 0);
3355 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3356 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3357 return dest_mem;
3360 src_mem = get_memory_rtx (src, len);
3361 set_mem_align (src_mem, src_align);
3363 /* Copy word part most expediently. */
3364 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3365 CALL_EXPR_TAILCALL (exp)
3366 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3367 expected_align, expected_size);
3369 if (dest_addr == 0)
3371 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3372 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3374 return dest_addr;
3378 /* Expand a call EXP to the mempcpy builtin.
3379 Return NULL_RTX if we failed; the caller should emit a normal call,
3380 otherwise try to get the result in TARGET, if convenient (and in
3381 mode MODE if that's convenient). If ENDP is 0 return the
3382 destination pointer, if ENDP is 1 return the end pointer ala
3383 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3384 stpcpy. */
3386 static rtx
3387 expand_builtin_mempcpy(tree exp, rtx target, enum machine_mode mode)
3389 if (!validate_arglist (exp,
3390 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3391 return NULL_RTX;
3392 else
3394 tree dest = CALL_EXPR_ARG (exp, 0);
3395 tree src = CALL_EXPR_ARG (exp, 1);
3396 tree len = CALL_EXPR_ARG (exp, 2);
3397 return expand_builtin_mempcpy_args (dest, src, len,
3398 TREE_TYPE (exp),
3399 target, mode, /*endp=*/ 1);
3403 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3404 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3405 so that this can also be called without constructing an actual CALL_EXPR.
3406 TYPE is the return type of the call. The other arguments and return value
3407 are the same as for expand_builtin_mempcpy. */
3409 static rtx
3410 expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
3411 rtx target, enum machine_mode mode, int endp)
3413 /* If return value is ignored, transform mempcpy into memcpy. */
3414 if (target == const0_rtx)
3416 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3418 if (!fn)
3419 return NULL_RTX;
3421 return expand_expr (build_call_expr (fn, 3, dest, src, len),
3422 target, mode, EXPAND_NORMAL);
3424 else
3426 const char *src_str;
3427 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3428 unsigned int dest_align
3429 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3430 rtx dest_mem, src_mem, len_rtx;
3431 tree result = fold_builtin_memory_op (dest, src, len, type, false, endp);
3433 if (result)
3435 while (TREE_CODE (result) == COMPOUND_EXPR)
3437 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3438 EXPAND_NORMAL);
3439 result = TREE_OPERAND (result, 1);
3441 return expand_expr (result, target, mode, EXPAND_NORMAL);
3444 /* If either SRC or DEST is not a pointer type, don't do this
3445 operation in-line. */
3446 if (dest_align == 0 || src_align == 0)
3447 return NULL_RTX;
3449 /* If LEN is not constant, call the normal function. */
3450 if (! host_integerp (len, 1))
3451 return NULL_RTX;
3453 len_rtx = expand_normal (len);
3454 src_str = c_getstr (src);
3456 /* If SRC is a string constant and block move would be done
3457 by pieces, we can avoid loading the string from memory
3458 and only stored the computed constants. */
3459 if (src_str
3460 && GET_CODE (len_rtx) == CONST_INT
3461 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3462 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3463 (void *) src_str, dest_align, false))
3465 dest_mem = get_memory_rtx (dest, len);
3466 set_mem_align (dest_mem, dest_align);
3467 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3468 builtin_memcpy_read_str,
3469 (void *) src_str, dest_align,
3470 false, endp);
3471 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3472 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3473 return dest_mem;
3476 if (GET_CODE (len_rtx) == CONST_INT
3477 && can_move_by_pieces (INTVAL (len_rtx),
3478 MIN (dest_align, src_align)))
3480 dest_mem = get_memory_rtx (dest, len);
3481 set_mem_align (dest_mem, dest_align);
3482 src_mem = get_memory_rtx (src, len);
3483 set_mem_align (src_mem, src_align);
3484 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3485 MIN (dest_align, src_align), endp);
3486 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3487 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3488 return dest_mem;
3491 return NULL_RTX;
3495 /* Expand expression EXP, which is a call to the memmove builtin. Return
3496 NULL_RTX if we failed; the caller should emit a normal call. */
3498 static rtx
3499 expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore)
3501 if (!validate_arglist (exp,
3502 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3503 return NULL_RTX;
3504 else
3506 tree dest = CALL_EXPR_ARG (exp, 0);
3507 tree src = CALL_EXPR_ARG (exp, 1);
3508 tree len = CALL_EXPR_ARG (exp, 2);
3509 return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp),
3510 target, mode, ignore);
3514 /* Helper function to do the actual work for expand_builtin_memmove. The
3515 arguments to the builtin_memmove call DEST, SRC, and LEN are broken out
3516 so that this can also be called without constructing an actual CALL_EXPR.
3517 TYPE is the return type of the call. The other arguments and return value
3518 are the same as for expand_builtin_memmove. */
3520 static rtx
3521 expand_builtin_memmove_args (tree dest, tree src, tree len,
3522 tree type, rtx target, enum machine_mode mode,
3523 int ignore)
3525 tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3);
3527 if (result)
3529 STRIP_TYPE_NOPS (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 /* Otherwise, call the normal function. */
3540 return NULL_RTX;
3543 /* Expand expression EXP, which is a call to the bcopy builtin. Return
3544 NULL_RTX if we failed the caller should emit a normal call. */
3546 static rtx
3547 expand_builtin_bcopy (tree exp, int ignore)
3549 tree type = TREE_TYPE (exp);
3550 tree src, dest, size;
3552 if (!validate_arglist (exp,
3553 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3554 return NULL_RTX;
3556 src = CALL_EXPR_ARG (exp, 0);
3557 dest = CALL_EXPR_ARG (exp, 1);
3558 size = CALL_EXPR_ARG (exp, 2);
3560 /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z).
3561 This is done this way so that if it isn't expanded inline, we fall
3562 back to calling bcopy instead of memmove. */
3563 return expand_builtin_memmove_args (dest, src,
3564 fold_convert (sizetype, size),
3565 type, const0_rtx, VOIDmode,
3566 ignore);
3569 #ifndef HAVE_movstr
3570 # define HAVE_movstr 0
3571 # define CODE_FOR_movstr CODE_FOR_nothing
3572 #endif
3574 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3575 we failed, the caller should emit a normal call, otherwise try to
3576 get the result in TARGET, if convenient. If ENDP is 0 return the
3577 destination pointer, if ENDP is 1 return the end pointer ala
3578 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3579 stpcpy. */
3581 static rtx
3582 expand_movstr (tree dest, tree src, rtx target, int endp)
3584 rtx end;
3585 rtx dest_mem;
3586 rtx src_mem;
3587 rtx insn;
3588 const struct insn_data * data;
3590 if (!HAVE_movstr)
3591 return NULL_RTX;
3593 dest_mem = get_memory_rtx (dest, NULL);
3594 src_mem = get_memory_rtx (src, NULL);
3595 if (!endp)
3597 target = force_reg (Pmode, XEXP (dest_mem, 0));
3598 dest_mem = replace_equiv_address (dest_mem, target);
3599 end = gen_reg_rtx (Pmode);
3601 else
3603 if (target == 0 || target == const0_rtx)
3605 end = gen_reg_rtx (Pmode);
3606 if (target == 0)
3607 target = end;
3609 else
3610 end = target;
3613 data = insn_data + CODE_FOR_movstr;
3615 if (data->operand[0].mode != VOIDmode)
3616 end = gen_lowpart (data->operand[0].mode, end);
3618 insn = data->genfun (end, dest_mem, src_mem);
3620 gcc_assert (insn);
3622 emit_insn (insn);
3624 /* movstr is supposed to set end to the address of the NUL
3625 terminator. If the caller requested a mempcpy-like return value,
3626 adjust it. */
3627 if (endp == 1 && target != const0_rtx)
3629 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3630 emit_move_insn (target, force_operand (tem, NULL_RTX));
3633 return target;
3636 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3637 NULL_RTX if we failed the caller should emit a normal call, otherwise
3638 try to get the result in TARGET, if convenient (and in mode MODE if that's
3639 convenient). */
3641 static rtx
3642 expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode)
3644 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3646 tree dest = CALL_EXPR_ARG (exp, 0);
3647 tree src = CALL_EXPR_ARG (exp, 1);
3648 return expand_builtin_strcpy_args (fndecl, dest, src, target, mode);
3650 return NULL_RTX;
3653 /* Helper function to do the actual work for expand_builtin_strcpy. The
3654 arguments to the builtin_strcpy call DEST and SRC are broken out
3655 so that this can also be called without constructing an actual CALL_EXPR.
3656 The other arguments and return value are the same as for
3657 expand_builtin_strcpy. */
3659 static rtx
3660 expand_builtin_strcpy_args (tree fndecl, tree dest, tree src,
3661 rtx target, enum machine_mode mode)
3663 tree result = fold_builtin_strcpy (fndecl, dest, src, 0);
3664 if (result)
3665 return expand_expr (result, target, mode, EXPAND_NORMAL);
3666 return expand_movstr (dest, src, target, /*endp=*/0);
3670 /* Expand a call EXP to the stpcpy builtin.
3671 Return NULL_RTX if we failed the caller should emit a normal call,
3672 otherwise try to get the result in TARGET, if convenient (and in
3673 mode MODE if that's convenient). */
3675 static rtx
3676 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3678 tree dst, src;
3680 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3681 return NULL_RTX;
3683 dst = CALL_EXPR_ARG (exp, 0);
3684 src = CALL_EXPR_ARG (exp, 1);
3686 /* If return value is ignored, transform stpcpy into strcpy. */
3687 if (target == const0_rtx)
3689 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3690 if (!fn)
3691 return NULL_RTX;
3693 return expand_expr (build_call_expr (fn, 2, dst, src),
3694 target, mode, EXPAND_NORMAL);
3696 else
3698 tree len, lenp1;
3699 rtx ret;
3701 /* Ensure we get an actual string whose length can be evaluated at
3702 compile-time, not an expression containing a string. This is
3703 because the latter will potentially produce pessimized code
3704 when used to produce the return value. */
3705 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3706 return expand_movstr (dst, src, target, /*endp=*/2);
3708 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3709 ret = expand_builtin_mempcpy_args (dst, src, lenp1, TREE_TYPE (exp),
3710 target, mode, /*endp=*/2);
3712 if (ret)
3713 return ret;
3715 if (TREE_CODE (len) == INTEGER_CST)
3717 rtx len_rtx = expand_normal (len);
3719 if (GET_CODE (len_rtx) == CONST_INT)
3721 ret = expand_builtin_strcpy_args (get_callee_fndecl (exp),
3722 dst, src, target, mode);
3724 if (ret)
3726 if (! target)
3728 if (mode != VOIDmode)
3729 target = gen_reg_rtx (mode);
3730 else
3731 target = gen_reg_rtx (GET_MODE (ret));
3733 if (GET_MODE (target) != GET_MODE (ret))
3734 ret = gen_lowpart (GET_MODE (target), ret);
3736 ret = plus_constant (ret, INTVAL (len_rtx));
3737 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3738 gcc_assert (ret);
3740 return target;
3745 return expand_movstr (dst, src, target, /*endp=*/2);
3749 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3750 bytes from constant string DATA + OFFSET and return it as target
3751 constant. */
3754 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3755 enum machine_mode mode)
3757 const char *str = (const char *) data;
3759 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3760 return const0_rtx;
3762 return c_readstr (str + offset, mode);
3765 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3766 NULL_RTX if we failed the caller should emit a normal call. */
3768 static rtx
3769 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3771 tree fndecl = get_callee_fndecl (exp);
3773 if (validate_arglist (exp,
3774 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3776 tree dest = CALL_EXPR_ARG (exp, 0);
3777 tree src = CALL_EXPR_ARG (exp, 1);
3778 tree len = CALL_EXPR_ARG (exp, 2);
3779 tree slen = c_strlen (src, 1);
3780 tree result = fold_builtin_strncpy (fndecl, dest, src, len, slen);
3782 if (result)
3784 while (TREE_CODE (result) == COMPOUND_EXPR)
3786 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3787 EXPAND_NORMAL);
3788 result = TREE_OPERAND (result, 1);
3790 return expand_expr (result, target, mode, EXPAND_NORMAL);
3793 /* We must be passed a constant len and src parameter. */
3794 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3795 return NULL_RTX;
3797 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3799 /* We're required to pad with trailing zeros if the requested
3800 len is greater than strlen(s2)+1. In that case try to
3801 use store_by_pieces, if it fails, punt. */
3802 if (tree_int_cst_lt (slen, len))
3804 unsigned int dest_align
3805 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3806 const char *p = c_getstr (src);
3807 rtx dest_mem;
3809 if (!p || dest_align == 0 || !host_integerp (len, 1)
3810 || !can_store_by_pieces (tree_low_cst (len, 1),
3811 builtin_strncpy_read_str,
3812 (void *) p, dest_align, false))
3813 return NULL_RTX;
3815 dest_mem = get_memory_rtx (dest, len);
3816 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3817 builtin_strncpy_read_str,
3818 (void *) p, dest_align, false, 0);
3819 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3820 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3821 return dest_mem;
3824 return NULL_RTX;
3827 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3828 bytes from constant string DATA + OFFSET and return it as target
3829 constant. */
3832 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3833 enum machine_mode mode)
3835 const char *c = (const char *) data;
3836 char *p = alloca (GET_MODE_SIZE (mode));
3838 memset (p, *c, GET_MODE_SIZE (mode));
3840 return c_readstr (p, mode);
3843 /* Callback routine for store_by_pieces. Return the RTL of a register
3844 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3845 char value given in the RTL register data. For example, if mode is
3846 4 bytes wide, return the RTL for 0x01010101*data. */
3848 static rtx
3849 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3850 enum machine_mode mode)
3852 rtx target, coeff;
3853 size_t size;
3854 char *p;
3856 size = GET_MODE_SIZE (mode);
3857 if (size == 1)
3858 return (rtx) data;
3860 p = alloca (size);
3861 memset (p, 1, size);
3862 coeff = c_readstr (p, mode);
3864 target = convert_to_mode (mode, (rtx) data, 1);
3865 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3866 return force_reg (mode, target);
3869 /* Expand expression EXP, which is a call to the memset builtin. Return
3870 NULL_RTX if we failed the caller should emit a normal call, otherwise
3871 try to get the result in TARGET, if convenient (and in mode MODE if that's
3872 convenient). */
3874 static rtx
3875 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3877 if (!validate_arglist (exp,
3878 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3879 return NULL_RTX;
3880 else
3882 tree dest = CALL_EXPR_ARG (exp, 0);
3883 tree val = CALL_EXPR_ARG (exp, 1);
3884 tree len = CALL_EXPR_ARG (exp, 2);
3885 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3889 /* Helper function to do the actual work for expand_builtin_memset. The
3890 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3891 so that this can also be called without constructing an actual CALL_EXPR.
3892 The other arguments and return value are the same as for
3893 expand_builtin_memset. */
3895 static rtx
3896 expand_builtin_memset_args (tree dest, tree val, tree len,
3897 rtx target, enum machine_mode mode, tree orig_exp)
3899 tree fndecl, fn;
3900 enum built_in_function fcode;
3901 char c;
3902 unsigned int dest_align;
3903 rtx dest_mem, dest_addr, len_rtx;
3904 HOST_WIDE_INT expected_size = -1;
3905 unsigned int expected_align = 0;
3907 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3909 /* If DEST is not a pointer type, don't do this operation in-line. */
3910 if (dest_align == 0)
3911 return NULL_RTX;
3913 stringop_block_profile (orig_exp, &expected_align, &expected_size);
3914 if (expected_align < dest_align)
3915 expected_align = dest_align;
3917 /* If the LEN parameter is zero, return DEST. */
3918 if (integer_zerop (len))
3920 /* Evaluate and ignore VAL in case it has side-effects. */
3921 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3922 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3925 /* Stabilize the arguments in case we fail. */
3926 dest = builtin_save_expr (dest);
3927 val = builtin_save_expr (val);
3928 len = builtin_save_expr (len);
3930 len_rtx = expand_normal (len);
3931 dest_mem = get_memory_rtx (dest, len);
3933 if (TREE_CODE (val) != INTEGER_CST)
3935 rtx val_rtx;
3937 val_rtx = expand_normal (val);
3938 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3939 val_rtx, 0);
3941 /* Assume that we can memset by pieces if we can store
3942 * the coefficients by pieces (in the required modes).
3943 * We can't pass builtin_memset_gen_str as that emits RTL. */
3944 c = 1;
3945 if (host_integerp (len, 1)
3946 && can_store_by_pieces (tree_low_cst (len, 1),
3947 builtin_memset_read_str, &c, dest_align,
3948 true))
3950 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3951 val_rtx);
3952 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3953 builtin_memset_gen_str, val_rtx, dest_align,
3954 true, 0);
3956 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3957 dest_align, expected_align,
3958 expected_size))
3959 goto do_libcall;
3961 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3962 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3963 return dest_mem;
3966 if (target_char_cast (val, &c))
3967 goto do_libcall;
3969 if (c)
3971 if (host_integerp (len, 1)
3972 && can_store_by_pieces (tree_low_cst (len, 1),
3973 builtin_memset_read_str, &c, dest_align,
3974 true))
3975 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3976 builtin_memset_read_str, &c, dest_align, true, 0);
3977 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3978 dest_align, expected_align,
3979 expected_size))
3980 goto do_libcall;
3982 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3983 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3984 return dest_mem;
3987 set_mem_align (dest_mem, dest_align);
3988 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3989 CALL_EXPR_TAILCALL (orig_exp)
3990 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3991 expected_align, expected_size);
3993 if (dest_addr == 0)
3995 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3996 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3999 return dest_addr;
4001 do_libcall:
4002 fndecl = get_callee_fndecl (orig_exp);
4003 fcode = DECL_FUNCTION_CODE (fndecl);
4004 if (fcode == BUILT_IN_MEMSET)
4005 fn = build_call_expr (fndecl, 3, dest, val, len);
4006 else if (fcode == BUILT_IN_BZERO)
4007 fn = build_call_expr (fndecl, 2, dest, len);
4008 else
4009 gcc_unreachable ();
4010 if (TREE_CODE (fn) == CALL_EXPR)
4011 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4012 return expand_call (fn, target, target == const0_rtx);
4015 /* Expand expression EXP, which is a call to the bzero builtin. Return
4016 NULL_RTX if we failed the caller should emit a normal call. */
4018 static rtx
4019 expand_builtin_bzero (tree exp)
4021 tree dest, size;
4023 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4024 return NULL_RTX;
4026 dest = CALL_EXPR_ARG (exp, 0);
4027 size = CALL_EXPR_ARG (exp, 1);
4029 /* New argument list transforming bzero(ptr x, int y) to
4030 memset(ptr x, int 0, size_t y). This is done this way
4031 so that if it isn't expanded inline, we fallback to
4032 calling bzero instead of memset. */
4034 return expand_builtin_memset_args (dest, integer_zero_node,
4035 fold_convert (sizetype, size),
4036 const0_rtx, VOIDmode, exp);
4039 /* Expand a call to the memchr builtin. Return NULL_RTX if we failed the
4040 caller should emit a normal call, otherwise try to get the result
4041 in TARGET, if convenient (and in mode MODE if that's convenient). */
4043 static rtx
4044 expand_builtin_memchr (tree exp, rtx target, enum machine_mode mode)
4046 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE,
4047 INTEGER_TYPE, VOID_TYPE))
4049 tree type = TREE_TYPE (exp);
4050 tree result = fold_builtin_memchr (CALL_EXPR_ARG (exp, 0),
4051 CALL_EXPR_ARG (exp, 1),
4052 CALL_EXPR_ARG (exp, 2), type);
4053 if (result)
4054 return expand_expr (result, target, mode, EXPAND_NORMAL);
4056 return NULL_RTX;
4059 /* Expand expression EXP, which is a call to the memcmp built-in function.
4060 Return NULL_RTX if we failed and the
4061 caller should emit a normal call, otherwise try to get the result in
4062 TARGET, if convenient (and in mode MODE, if that's convenient). */
4064 static rtx
4065 expand_builtin_memcmp (tree exp, rtx target, enum machine_mode mode)
4067 if (!validate_arglist (exp,
4068 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4069 return NULL_RTX;
4070 else
4072 tree result = fold_builtin_memcmp (CALL_EXPR_ARG (exp, 0),
4073 CALL_EXPR_ARG (exp, 1),
4074 CALL_EXPR_ARG (exp, 2));
4075 if (result)
4076 return expand_expr (result, target, mode, EXPAND_NORMAL);
4079 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
4081 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4082 rtx result;
4083 rtx insn;
4084 tree arg1 = CALL_EXPR_ARG (exp, 0);
4085 tree arg2 = CALL_EXPR_ARG (exp, 1);
4086 tree len = CALL_EXPR_ARG (exp, 2);
4088 int arg1_align
4089 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4090 int arg2_align
4091 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4092 enum machine_mode insn_mode;
4094 #ifdef HAVE_cmpmemsi
4095 if (HAVE_cmpmemsi)
4096 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
4097 else
4098 #endif
4099 #ifdef HAVE_cmpstrnsi
4100 if (HAVE_cmpstrnsi)
4101 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4102 else
4103 #endif
4104 return NULL_RTX;
4106 /* If we don't have POINTER_TYPE, call the function. */
4107 if (arg1_align == 0 || arg2_align == 0)
4108 return NULL_RTX;
4110 /* Make a place to write the result of the instruction. */
4111 result = target;
4112 if (! (result != 0
4113 && REG_P (result) && GET_MODE (result) == insn_mode
4114 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4115 result = gen_reg_rtx (insn_mode);
4117 arg1_rtx = get_memory_rtx (arg1, len);
4118 arg2_rtx = get_memory_rtx (arg2, len);
4119 arg3_rtx = expand_normal (len);
4121 /* Set MEM_SIZE as appropriate. */
4122 if (GET_CODE (arg3_rtx) == CONST_INT)
4124 set_mem_size (arg1_rtx, arg3_rtx);
4125 set_mem_size (arg2_rtx, arg3_rtx);
4128 #ifdef HAVE_cmpmemsi
4129 if (HAVE_cmpmemsi)
4130 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4131 GEN_INT (MIN (arg1_align, arg2_align)));
4132 else
4133 #endif
4134 #ifdef HAVE_cmpstrnsi
4135 if (HAVE_cmpstrnsi)
4136 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4137 GEN_INT (MIN (arg1_align, arg2_align)));
4138 else
4139 #endif
4140 gcc_unreachable ();
4142 if (insn)
4143 emit_insn (insn);
4144 else
4145 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
4146 TYPE_MODE (integer_type_node), 3,
4147 XEXP (arg1_rtx, 0), Pmode,
4148 XEXP (arg2_rtx, 0), Pmode,
4149 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4150 TYPE_UNSIGNED (sizetype)),
4151 TYPE_MODE (sizetype));
4153 /* Return the value in the proper mode for this function. */
4154 mode = TYPE_MODE (TREE_TYPE (exp));
4155 if (GET_MODE (result) == mode)
4156 return result;
4157 else if (target != 0)
4159 convert_move (target, result, 0);
4160 return target;
4162 else
4163 return convert_to_mode (mode, result, 0);
4165 #endif
4167 return NULL_RTX;
4170 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4171 if we failed the caller should emit a normal call, otherwise try to get
4172 the result in TARGET, if convenient. */
4174 static rtx
4175 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
4177 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4178 return NULL_RTX;
4179 else
4181 tree result = fold_builtin_strcmp (CALL_EXPR_ARG (exp, 0),
4182 CALL_EXPR_ARG (exp, 1));
4183 if (result)
4184 return expand_expr (result, target, mode, EXPAND_NORMAL);
4187 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4188 if (cmpstr_optab[SImode] != CODE_FOR_nothing
4189 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
4191 rtx arg1_rtx, arg2_rtx;
4192 rtx result, insn = NULL_RTX;
4193 tree fndecl, fn;
4194 tree arg1 = CALL_EXPR_ARG (exp, 0);
4195 tree arg2 = CALL_EXPR_ARG (exp, 1);
4197 int arg1_align
4198 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4199 int arg2_align
4200 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4202 /* If we don't have POINTER_TYPE, call the function. */
4203 if (arg1_align == 0 || arg2_align == 0)
4204 return NULL_RTX;
4206 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4207 arg1 = builtin_save_expr (arg1);
4208 arg2 = builtin_save_expr (arg2);
4210 arg1_rtx = get_memory_rtx (arg1, NULL);
4211 arg2_rtx = get_memory_rtx (arg2, NULL);
4213 #ifdef HAVE_cmpstrsi
4214 /* Try to call cmpstrsi. */
4215 if (HAVE_cmpstrsi)
4217 enum machine_mode insn_mode
4218 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4220 /* Make a place to write the result of the instruction. */
4221 result = target;
4222 if (! (result != 0
4223 && REG_P (result) && GET_MODE (result) == insn_mode
4224 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4225 result = gen_reg_rtx (insn_mode);
4227 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4228 GEN_INT (MIN (arg1_align, arg2_align)));
4230 #endif
4231 #ifdef HAVE_cmpstrnsi
4232 /* Try to determine at least one length and call cmpstrnsi. */
4233 if (!insn && HAVE_cmpstrnsi)
4235 tree len;
4236 rtx arg3_rtx;
4238 enum machine_mode insn_mode
4239 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4240 tree len1 = c_strlen (arg1, 1);
4241 tree len2 = c_strlen (arg2, 1);
4243 if (len1)
4244 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4245 if (len2)
4246 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4248 /* If we don't have a constant length for the first, use the length
4249 of the second, if we know it. We don't require a constant for
4250 this case; some cost analysis could be done if both are available
4251 but neither is constant. For now, assume they're equally cheap,
4252 unless one has side effects. If both strings have constant lengths,
4253 use the smaller. */
4255 if (!len1)
4256 len = len2;
4257 else if (!len2)
4258 len = len1;
4259 else if (TREE_SIDE_EFFECTS (len1))
4260 len = len2;
4261 else if (TREE_SIDE_EFFECTS (len2))
4262 len = len1;
4263 else if (TREE_CODE (len1) != INTEGER_CST)
4264 len = len2;
4265 else if (TREE_CODE (len2) != INTEGER_CST)
4266 len = len1;
4267 else if (tree_int_cst_lt (len1, len2))
4268 len = len1;
4269 else
4270 len = len2;
4272 /* If both arguments have side effects, we cannot optimize. */
4273 if (!len || TREE_SIDE_EFFECTS (len))
4274 goto do_libcall;
4276 arg3_rtx = expand_normal (len);
4278 /* Make a place to write the result of the instruction. */
4279 result = target;
4280 if (! (result != 0
4281 && REG_P (result) && GET_MODE (result) == insn_mode
4282 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4283 result = gen_reg_rtx (insn_mode);
4285 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4286 GEN_INT (MIN (arg1_align, arg2_align)));
4288 #endif
4290 if (insn)
4292 emit_insn (insn);
4294 /* Return the value in the proper mode for this function. */
4295 mode = TYPE_MODE (TREE_TYPE (exp));
4296 if (GET_MODE (result) == mode)
4297 return result;
4298 if (target == 0)
4299 return convert_to_mode (mode, result, 0);
4300 convert_move (target, result, 0);
4301 return target;
4304 /* Expand the library call ourselves using a stabilized argument
4305 list to avoid re-evaluating the function's arguments twice. */
4306 #ifdef HAVE_cmpstrnsi
4307 do_libcall:
4308 #endif
4309 fndecl = get_callee_fndecl (exp);
4310 fn = build_call_expr (fndecl, 2, arg1, arg2);
4311 if (TREE_CODE (fn) == CALL_EXPR)
4312 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4313 return expand_call (fn, target, target == const0_rtx);
4315 #endif
4316 return NULL_RTX;
4319 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4320 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4321 the result in TARGET, if convenient. */
4323 static rtx
4324 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4326 if (!validate_arglist (exp,
4327 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4328 return NULL_RTX;
4329 else
4331 tree result = fold_builtin_strncmp (CALL_EXPR_ARG (exp, 0),
4332 CALL_EXPR_ARG (exp, 1),
4333 CALL_EXPR_ARG (exp, 2));
4334 if (result)
4335 return expand_expr (result, target, mode, EXPAND_NORMAL);
4338 /* If c_strlen can determine an expression for one of the string
4339 lengths, and it doesn't have side effects, then emit cmpstrnsi
4340 using length MIN(strlen(string)+1, arg3). */
4341 #ifdef HAVE_cmpstrnsi
4342 if (HAVE_cmpstrnsi)
4344 tree len, len1, len2;
4345 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4346 rtx result, insn;
4347 tree fndecl, fn;
4348 tree arg1 = CALL_EXPR_ARG (exp, 0);
4349 tree arg2 = CALL_EXPR_ARG (exp, 1);
4350 tree arg3 = CALL_EXPR_ARG (exp, 2);
4352 int arg1_align
4353 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4354 int arg2_align
4355 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4356 enum machine_mode insn_mode
4357 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4359 len1 = c_strlen (arg1, 1);
4360 len2 = c_strlen (arg2, 1);
4362 if (len1)
4363 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4364 if (len2)
4365 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4367 /* If we don't have a constant length for the first, use the length
4368 of the second, if we know it. We don't require a constant for
4369 this case; some cost analysis could be done if both are available
4370 but neither is constant. For now, assume they're equally cheap,
4371 unless one has side effects. If both strings have constant lengths,
4372 use the smaller. */
4374 if (!len1)
4375 len = len2;
4376 else if (!len2)
4377 len = len1;
4378 else if (TREE_SIDE_EFFECTS (len1))
4379 len = len2;
4380 else if (TREE_SIDE_EFFECTS (len2))
4381 len = len1;
4382 else if (TREE_CODE (len1) != INTEGER_CST)
4383 len = len2;
4384 else if (TREE_CODE (len2) != INTEGER_CST)
4385 len = len1;
4386 else if (tree_int_cst_lt (len1, len2))
4387 len = len1;
4388 else
4389 len = len2;
4391 /* If both arguments have side effects, we cannot optimize. */
4392 if (!len || TREE_SIDE_EFFECTS (len))
4393 return NULL_RTX;
4395 /* The actual new length parameter is MIN(len,arg3). */
4396 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4397 fold_convert (TREE_TYPE (len), arg3));
4399 /* If we don't have POINTER_TYPE, call the function. */
4400 if (arg1_align == 0 || arg2_align == 0)
4401 return NULL_RTX;
4403 /* Make a place to write the result of the instruction. */
4404 result = target;
4405 if (! (result != 0
4406 && REG_P (result) && GET_MODE (result) == insn_mode
4407 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4408 result = gen_reg_rtx (insn_mode);
4410 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4411 arg1 = builtin_save_expr (arg1);
4412 arg2 = builtin_save_expr (arg2);
4413 len = builtin_save_expr (len);
4415 arg1_rtx = get_memory_rtx (arg1, len);
4416 arg2_rtx = get_memory_rtx (arg2, len);
4417 arg3_rtx = expand_normal (len);
4418 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4419 GEN_INT (MIN (arg1_align, arg2_align)));
4420 if (insn)
4422 emit_insn (insn);
4424 /* Return the value in the proper mode for this function. */
4425 mode = TYPE_MODE (TREE_TYPE (exp));
4426 if (GET_MODE (result) == mode)
4427 return result;
4428 if (target == 0)
4429 return convert_to_mode (mode, result, 0);
4430 convert_move (target, result, 0);
4431 return target;
4434 /* Expand the library call ourselves using a stabilized argument
4435 list to avoid re-evaluating the function's arguments twice. */
4436 fndecl = get_callee_fndecl (exp);
4437 fn = build_call_expr (fndecl, 3, arg1, arg2, len);
4438 if (TREE_CODE (fn) == CALL_EXPR)
4439 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4440 return expand_call (fn, target, target == const0_rtx);
4442 #endif
4443 return NULL_RTX;
4446 /* Expand expression EXP, which is a call to the strcat builtin.
4447 Return NULL_RTX if we failed the caller should emit a normal call,
4448 otherwise try to get the result in TARGET, if convenient. */
4450 static rtx
4451 expand_builtin_strcat (tree fndecl, tree exp, rtx target, enum machine_mode mode)
4453 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4454 return NULL_RTX;
4455 else
4457 tree dst = CALL_EXPR_ARG (exp, 0);
4458 tree src = CALL_EXPR_ARG (exp, 1);
4459 const char *p = c_getstr (src);
4461 /* If the string length is zero, return the dst parameter. */
4462 if (p && *p == '\0')
4463 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4465 if (!optimize_size)
4467 /* See if we can store by pieces into (dst + strlen(dst)). */
4468 tree newsrc, newdst,
4469 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4470 rtx insns;
4472 /* Stabilize the argument list. */
4473 newsrc = builtin_save_expr (src);
4474 dst = builtin_save_expr (dst);
4476 start_sequence ();
4478 /* Create strlen (dst). */
4479 newdst = build_call_expr (strlen_fn, 1, dst);
4480 /* Create (dst p+ strlen (dst)). */
4482 newdst = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4483 newdst = builtin_save_expr (newdst);
4485 if (!expand_builtin_strcpy_args (fndecl, newdst, newsrc, target, mode))
4487 end_sequence (); /* Stop sequence. */
4488 return NULL_RTX;
4491 /* Output the entire sequence. */
4492 insns = get_insns ();
4493 end_sequence ();
4494 emit_insn (insns);
4496 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4499 return NULL_RTX;
4503 /* Expand expression EXP, which is a call to the strncat builtin.
4504 Return NULL_RTX if we failed the caller should emit a normal call,
4505 otherwise try to get the result in TARGET, if convenient. */
4507 static rtx
4508 expand_builtin_strncat (tree exp, rtx target, enum machine_mode mode)
4510 if (validate_arglist (exp,
4511 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4513 tree result = fold_builtin_strncat (CALL_EXPR_ARG (exp, 0),
4514 CALL_EXPR_ARG (exp, 1),
4515 CALL_EXPR_ARG (exp, 2));
4516 if (result)
4517 return expand_expr (result, target, mode, EXPAND_NORMAL);
4519 return NULL_RTX;
4522 /* Expand expression EXP, which is a call to the strspn builtin.
4523 Return NULL_RTX if we failed the caller should emit a normal call,
4524 otherwise try to get the result in TARGET, if convenient. */
4526 static rtx
4527 expand_builtin_strspn (tree exp, rtx target, enum machine_mode mode)
4529 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4531 tree result = fold_builtin_strspn (CALL_EXPR_ARG (exp, 0),
4532 CALL_EXPR_ARG (exp, 1));
4533 if (result)
4534 return expand_expr (result, target, mode, EXPAND_NORMAL);
4536 return NULL_RTX;
4539 /* Expand expression EXP, which is a call to the strcspn builtin.
4540 Return NULL_RTX if we failed the caller should emit a normal call,
4541 otherwise try to get the result in TARGET, if convenient. */
4543 static rtx
4544 expand_builtin_strcspn (tree exp, rtx target, enum machine_mode mode)
4546 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4548 tree result = fold_builtin_strcspn (CALL_EXPR_ARG (exp, 0),
4549 CALL_EXPR_ARG (exp, 1));
4550 if (result)
4551 return expand_expr (result, target, mode, EXPAND_NORMAL);
4553 return NULL_RTX;
4556 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4557 if that's convenient. */
4560 expand_builtin_saveregs (void)
4562 rtx val, seq;
4564 /* Don't do __builtin_saveregs more than once in a function.
4565 Save the result of the first call and reuse it. */
4566 if (saveregs_value != 0)
4567 return saveregs_value;
4569 /* When this function is called, it means that registers must be
4570 saved on entry to this function. So we migrate the call to the
4571 first insn of this function. */
4573 start_sequence ();
4575 /* Do whatever the machine needs done in this case. */
4576 val = targetm.calls.expand_builtin_saveregs ();
4578 seq = get_insns ();
4579 end_sequence ();
4581 saveregs_value = val;
4583 /* Put the insns after the NOTE that starts the function. If this
4584 is inside a start_sequence, make the outer-level insn chain current, so
4585 the code is placed at the start of the function. */
4586 push_topmost_sequence ();
4587 emit_insn_after (seq, entry_of_function ());
4588 pop_topmost_sequence ();
4590 return val;
4593 /* __builtin_args_info (N) returns word N of the arg space info
4594 for the current function. The number and meanings of words
4595 is controlled by the definition of CUMULATIVE_ARGS. */
4597 static rtx
4598 expand_builtin_args_info (tree exp)
4600 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4601 int *word_ptr = (int *) &current_function_args_info;
4603 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4605 if (call_expr_nargs (exp) != 0)
4607 if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0))
4608 error ("argument of %<__builtin_args_info%> must be constant");
4609 else
4611 HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0);
4613 if (wordnum < 0 || wordnum >= nwords)
4614 error ("argument of %<__builtin_args_info%> out of range");
4615 else
4616 return GEN_INT (word_ptr[wordnum]);
4619 else
4620 error ("missing argument in %<__builtin_args_info%>");
4622 return const0_rtx;
4625 /* Expand a call to __builtin_next_arg. */
4627 static rtx
4628 expand_builtin_next_arg (void)
4630 /* Checking arguments is already done in fold_builtin_next_arg
4631 that must be called before this function. */
4632 return expand_binop (ptr_mode, add_optab,
4633 current_function_internal_arg_pointer,
4634 current_function_arg_offset_rtx,
4635 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4638 /* Make it easier for the backends by protecting the valist argument
4639 from multiple evaluations. */
4641 static tree
4642 stabilize_va_list (tree valist, int needs_lvalue)
4644 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4646 if (TREE_SIDE_EFFECTS (valist))
4647 valist = save_expr (valist);
4649 /* For this case, the backends will be expecting a pointer to
4650 TREE_TYPE (va_list_type_node), but it's possible we've
4651 actually been given an array (an actual va_list_type_node).
4652 So fix it. */
4653 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4655 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4656 valist = build_fold_addr_expr_with_type (valist, p1);
4659 else
4661 tree pt;
4663 if (! needs_lvalue)
4665 if (! TREE_SIDE_EFFECTS (valist))
4666 return valist;
4668 pt = build_pointer_type (va_list_type_node);
4669 valist = fold_build1 (ADDR_EXPR, pt, valist);
4670 TREE_SIDE_EFFECTS (valist) = 1;
4673 if (TREE_SIDE_EFFECTS (valist))
4674 valist = save_expr (valist);
4675 valist = build_fold_indirect_ref (valist);
4678 return valist;
4681 /* The "standard" definition of va_list is void*. */
4683 tree
4684 std_build_builtin_va_list (void)
4686 return ptr_type_node;
4689 /* The "standard" implementation of va_start: just assign `nextarg' to
4690 the variable. */
4692 void
4693 std_expand_builtin_va_start (tree valist, rtx nextarg)
4695 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4696 convert_move (va_r, nextarg, 0);
4699 /* Expand EXP, a call to __builtin_va_start. */
4701 static rtx
4702 expand_builtin_va_start (tree exp)
4704 rtx nextarg;
4705 tree valist;
4707 if (call_expr_nargs (exp) < 2)
4709 error ("too few arguments to function %<va_start%>");
4710 return const0_rtx;
4713 if (fold_builtin_next_arg (exp, true))
4714 return const0_rtx;
4716 nextarg = expand_builtin_next_arg ();
4717 valist = stabilize_va_list (CALL_EXPR_ARG (exp, 0), 1);
4719 if (targetm.expand_builtin_va_start)
4720 targetm.expand_builtin_va_start (valist, nextarg);
4721 else
4722 std_expand_builtin_va_start (valist, nextarg);
4724 return const0_rtx;
4727 /* The "standard" implementation of va_arg: read the value from the
4728 current (padded) address and increment by the (padded) size. */
4730 tree
4731 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4733 tree addr, t, type_size, rounded_size, valist_tmp;
4734 unsigned HOST_WIDE_INT align, boundary;
4735 bool indirect;
4737 #ifdef ARGS_GROW_DOWNWARD
4738 /* All of the alignment and movement below is for args-grow-up machines.
4739 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4740 implement their own specialized gimplify_va_arg_expr routines. */
4741 gcc_unreachable ();
4742 #endif
4744 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4745 if (indirect)
4746 type = build_pointer_type (type);
4748 align = PARM_BOUNDARY / BITS_PER_UNIT;
4749 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4751 /* Hoist the valist value into a temporary for the moment. */
4752 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4754 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4755 requires greater alignment, we must perform dynamic alignment. */
4756 if (boundary > align
4757 && !integer_zerop (TYPE_SIZE (type)))
4759 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4760 fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist),
4761 valist_tmp, size_int (boundary - 1)));
4762 gimplify_and_add (t, pre_p);
4764 t = fold_convert (sizetype, valist_tmp);
4765 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4766 fold_convert (TREE_TYPE (valist),
4767 fold_build2 (BIT_AND_EXPR, sizetype, t,
4768 size_int (-boundary))));
4769 gimplify_and_add (t, pre_p);
4771 else
4772 boundary = align;
4774 /* If the actual alignment is less than the alignment of the type,
4775 adjust the type accordingly so that we don't assume strict alignment
4776 when deferencing the pointer. */
4777 boundary *= BITS_PER_UNIT;
4778 if (boundary < TYPE_ALIGN (type))
4780 type = build_variant_type_copy (type);
4781 TYPE_ALIGN (type) = boundary;
4784 /* Compute the rounded size of the type. */
4785 type_size = size_in_bytes (type);
4786 rounded_size = round_up (type_size, align);
4788 /* Reduce rounded_size so it's sharable with the postqueue. */
4789 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4791 /* Get AP. */
4792 addr = valist_tmp;
4793 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4795 /* Small args are padded downward. */
4796 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4797 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4798 size_binop (MINUS_EXPR, rounded_size, type_size));
4799 addr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr, t);
4802 /* Compute new value for AP. */
4803 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size);
4804 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4805 gimplify_and_add (t, pre_p);
4807 addr = fold_convert (build_pointer_type (type), addr);
4809 if (indirect)
4810 addr = build_va_arg_indirect_ref (addr);
4812 return build_va_arg_indirect_ref (addr);
4815 /* Build an indirect-ref expression over the given TREE, which represents a
4816 piece of a va_arg() expansion. */
4817 tree
4818 build_va_arg_indirect_ref (tree addr)
4820 addr = build_fold_indirect_ref (addr);
4822 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4823 mf_mark (addr);
4825 return addr;
4828 /* Return a dummy expression of type TYPE in order to keep going after an
4829 error. */
4831 static tree
4832 dummy_object (tree type)
4834 tree t = build_int_cst (build_pointer_type (type), 0);
4835 return build1 (INDIRECT_REF, type, t);
4838 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4839 builtin function, but a very special sort of operator. */
4841 enum gimplify_status
4842 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4844 tree promoted_type, want_va_type, have_va_type;
4845 tree valist = TREE_OPERAND (*expr_p, 0);
4846 tree type = TREE_TYPE (*expr_p);
4847 tree t;
4849 /* Verify that valist is of the proper type. */
4850 want_va_type = va_list_type_node;
4851 have_va_type = TREE_TYPE (valist);
4853 if (have_va_type == error_mark_node)
4854 return GS_ERROR;
4856 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4858 /* If va_list is an array type, the argument may have decayed
4859 to a pointer type, e.g. by being passed to another function.
4860 In that case, unwrap both types so that we can compare the
4861 underlying records. */
4862 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4863 || POINTER_TYPE_P (have_va_type))
4865 want_va_type = TREE_TYPE (want_va_type);
4866 have_va_type = TREE_TYPE (have_va_type);
4870 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4872 error ("first argument to %<va_arg%> not of type %<va_list%>");
4873 return GS_ERROR;
4876 /* Generate a diagnostic for requesting data of a type that cannot
4877 be passed through `...' due to type promotion at the call site. */
4878 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4879 != type)
4881 static bool gave_help;
4883 /* Unfortunately, this is merely undefined, rather than a constraint
4884 violation, so we cannot make this an error. If this call is never
4885 executed, the program is still strictly conforming. */
4886 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4887 type, promoted_type);
4888 if (! gave_help)
4890 gave_help = true;
4891 inform ("(so you should pass %qT not %qT to %<va_arg%>)",
4892 promoted_type, type);
4895 /* We can, however, treat "undefined" any way we please.
4896 Call abort to encourage the user to fix the program. */
4897 inform ("if this code is reached, the program will abort");
4898 t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0);
4899 append_to_statement_list (t, pre_p);
4901 /* This is dead code, but go ahead and finish so that the
4902 mode of the result comes out right. */
4903 *expr_p = dummy_object (type);
4904 return GS_ALL_DONE;
4906 else
4908 /* Make it easier for the backends by protecting the valist argument
4909 from multiple evaluations. */
4910 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4912 /* For this case, the backends will be expecting a pointer to
4913 TREE_TYPE (va_list_type_node), but it's possible we've
4914 actually been given an array (an actual va_list_type_node).
4915 So fix it. */
4916 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4918 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4919 valist = build_fold_addr_expr_with_type (valist, p1);
4921 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4923 else
4924 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4926 if (!targetm.gimplify_va_arg_expr)
4927 /* FIXME:Once most targets are converted we should merely
4928 assert this is non-null. */
4929 return GS_ALL_DONE;
4931 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4932 return GS_OK;
4936 /* Expand EXP, a call to __builtin_va_end. */
4938 static rtx
4939 expand_builtin_va_end (tree exp)
4941 tree valist = CALL_EXPR_ARG (exp, 0);
4943 /* Evaluate for side effects, if needed. I hate macros that don't
4944 do that. */
4945 if (TREE_SIDE_EFFECTS (valist))
4946 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4948 return const0_rtx;
4951 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4952 builtin rather than just as an assignment in stdarg.h because of the
4953 nastiness of array-type va_list types. */
4955 static rtx
4956 expand_builtin_va_copy (tree exp)
4958 tree dst, src, t;
4960 dst = CALL_EXPR_ARG (exp, 0);
4961 src = CALL_EXPR_ARG (exp, 1);
4963 dst = stabilize_va_list (dst, 1);
4964 src = stabilize_va_list (src, 0);
4966 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4968 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4969 TREE_SIDE_EFFECTS (t) = 1;
4970 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4972 else
4974 rtx dstb, srcb, size;
4976 /* Evaluate to pointers. */
4977 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4978 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4979 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4980 VOIDmode, EXPAND_NORMAL);
4982 dstb = convert_memory_address (Pmode, dstb);
4983 srcb = convert_memory_address (Pmode, srcb);
4985 /* "Dereference" to BLKmode memories. */
4986 dstb = gen_rtx_MEM (BLKmode, dstb);
4987 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4988 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4989 srcb = gen_rtx_MEM (BLKmode, srcb);
4990 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4991 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4993 /* Copy. */
4994 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4997 return const0_rtx;
5000 /* Expand a call to one of the builtin functions __builtin_frame_address or
5001 __builtin_return_address. */
5003 static rtx
5004 expand_builtin_frame_address (tree fndecl, tree exp)
5006 /* The argument must be a nonnegative integer constant.
5007 It counts the number of frames to scan up the stack.
5008 The value is the return address saved in that frame. */
5009 if (call_expr_nargs (exp) == 0)
5010 /* Warning about missing arg was already issued. */
5011 return const0_rtx;
5012 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
5014 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5015 error ("invalid argument to %<__builtin_frame_address%>");
5016 else
5017 error ("invalid argument to %<__builtin_return_address%>");
5018 return const0_rtx;
5020 else
5022 rtx tem
5023 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
5024 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
5026 /* Some ports cannot access arbitrary stack frames. */
5027 if (tem == NULL)
5029 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5030 warning (0, "unsupported argument to %<__builtin_frame_address%>");
5031 else
5032 warning (0, "unsupported argument to %<__builtin_return_address%>");
5033 return const0_rtx;
5036 /* For __builtin_frame_address, return what we've got. */
5037 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5038 return tem;
5040 if (!REG_P (tem)
5041 && ! CONSTANT_P (tem))
5042 tem = copy_to_mode_reg (Pmode, tem);
5043 return tem;
5047 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if
5048 we failed and the caller should emit a normal call, otherwise try to get
5049 the result in TARGET, if convenient. */
5051 static rtx
5052 expand_builtin_alloca (tree exp, rtx target)
5054 rtx op0;
5055 rtx result;
5057 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
5058 should always expand to function calls. These can be intercepted
5059 in libmudflap. */
5060 if (flag_mudflap)
5061 return NULL_RTX;
5063 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5064 return NULL_RTX;
5066 /* Compute the argument. */
5067 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5069 /* Allocate the desired space. */
5070 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
5071 result = convert_memory_address (ptr_mode, result);
5073 return result;
5076 /* Expand a call to a bswap builtin with argument ARG0. MODE
5077 is the mode to expand with. */
5079 static rtx
5080 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
5082 enum machine_mode mode;
5083 tree arg;
5084 rtx op0;
5086 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5087 return NULL_RTX;
5089 arg = CALL_EXPR_ARG (exp, 0);
5090 mode = TYPE_MODE (TREE_TYPE (arg));
5091 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5093 target = expand_unop (mode, bswap_optab, op0, target, 1);
5095 gcc_assert (target);
5097 return convert_to_mode (mode, target, 0);
5100 /* Expand a call to a unary builtin in EXP.
5101 Return NULL_RTX if a normal call should be emitted rather than expanding the
5102 function in-line. If convenient, the result should be placed in TARGET.
5103 SUBTARGET may be used as the target for computing one of EXP's operands. */
5105 static rtx
5106 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
5107 rtx subtarget, optab op_optab)
5109 rtx op0;
5111 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5112 return NULL_RTX;
5114 /* Compute the argument. */
5115 op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5116 VOIDmode, EXPAND_NORMAL);
5117 /* Compute op, into TARGET if possible.
5118 Set TARGET to wherever the result comes back. */
5119 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5120 op_optab, op0, target, 1);
5121 gcc_assert (target);
5123 return convert_to_mode (target_mode, target, 0);
5126 /* If the string passed to fputs is a constant and is one character
5127 long, we attempt to transform this call into __builtin_fputc(). */
5129 static rtx
5130 expand_builtin_fputs (tree exp, rtx target, bool unlocked)
5132 /* Verify the arguments in the original call. */
5133 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5135 tree result = fold_builtin_fputs (CALL_EXPR_ARG (exp, 0),
5136 CALL_EXPR_ARG (exp, 1),
5137 (target == const0_rtx),
5138 unlocked, NULL_TREE);
5139 if (result)
5140 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
5142 return NULL_RTX;
5145 /* Expand a call to __builtin_expect. We just return our argument
5146 as the builtin_expect semantic should've been already executed by
5147 tree branch prediction pass. */
5149 static rtx
5150 expand_builtin_expect (tree exp, rtx target)
5152 tree arg, c;
5154 if (call_expr_nargs (exp) < 2)
5155 return const0_rtx;
5156 arg = CALL_EXPR_ARG (exp, 0);
5157 c = CALL_EXPR_ARG (exp, 1);
5159 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5160 /* When guessing was done, the hints should be already stripped away. */
5161 gcc_assert (!flag_guess_branch_prob
5162 || optimize == 0 || errorcount || sorrycount);
5163 return target;
5166 void
5167 expand_builtin_trap (void)
5169 #ifdef HAVE_trap
5170 if (HAVE_trap)
5171 emit_insn (gen_trap ());
5172 else
5173 #endif
5174 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
5175 emit_barrier ();
5178 /* Expand EXP, a call to fabs, fabsf or fabsl.
5179 Return NULL_RTX if a normal call should be emitted rather than expanding
5180 the function inline. If convenient, the result should be placed
5181 in TARGET. SUBTARGET may be used as the target for computing
5182 the operand. */
5184 static rtx
5185 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5187 enum machine_mode mode;
5188 tree arg;
5189 rtx op0;
5191 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5192 return NULL_RTX;
5194 arg = CALL_EXPR_ARG (exp, 0);
5195 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5196 mode = TYPE_MODE (TREE_TYPE (arg));
5197 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5198 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5201 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5202 Return NULL is a normal call should be emitted rather than expanding the
5203 function inline. If convenient, the result should be placed in TARGET.
5204 SUBTARGET may be used as the target for computing the operand. */
5206 static rtx
5207 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5209 rtx op0, op1;
5210 tree arg;
5212 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5213 return NULL_RTX;
5215 arg = CALL_EXPR_ARG (exp, 0);
5216 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5218 arg = CALL_EXPR_ARG (exp, 1);
5219 op1 = expand_normal (arg);
5221 return expand_copysign (op0, op1, target);
5224 /* Create a new constant string literal and return a char* pointer to it.
5225 The STRING_CST value is the LEN characters at STR. */
5226 tree
5227 build_string_literal (int len, const char *str)
5229 tree t, elem, index, type;
5231 t = build_string (len, str);
5232 elem = build_type_variant (char_type_node, 1, 0);
5233 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
5234 type = build_array_type (elem, index);
5235 TREE_TYPE (t) = type;
5236 TREE_CONSTANT (t) = 1;
5237 TREE_INVARIANT (t) = 1;
5238 TREE_READONLY (t) = 1;
5239 TREE_STATIC (t) = 1;
5241 type = build_pointer_type (type);
5242 t = build1 (ADDR_EXPR, type, t);
5244 type = build_pointer_type (elem);
5245 t = build1 (NOP_EXPR, type, t);
5246 return t;
5249 /* Expand EXP, a call to printf or printf_unlocked.
5250 Return NULL_RTX if a normal call should be emitted rather than transforming
5251 the function inline. If convenient, the result should be placed in
5252 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
5253 call. */
5254 static rtx
5255 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
5256 bool unlocked)
5258 /* If we're using an unlocked function, assume the other unlocked
5259 functions exist explicitly. */
5260 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
5261 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
5262 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
5263 : implicit_built_in_decls[BUILT_IN_PUTS];
5264 const char *fmt_str;
5265 tree fn = 0;
5266 tree fmt, arg;
5267 int nargs = call_expr_nargs (exp);
5269 /* If the return value is used, don't do the transformation. */
5270 if (target != const0_rtx)
5271 return NULL_RTX;
5273 /* Verify the required arguments in the original call. */
5274 if (nargs == 0)
5275 return NULL_RTX;
5276 fmt = CALL_EXPR_ARG (exp, 0);
5277 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5278 return NULL_RTX;
5280 /* Check whether the format is a literal string constant. */
5281 fmt_str = c_getstr (fmt);
5282 if (fmt_str == NULL)
5283 return NULL_RTX;
5285 if (!init_target_chars ())
5286 return NULL_RTX;
5288 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5289 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5291 if ((nargs != 2)
5292 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1))))
5293 return NULL_RTX;
5294 if (fn_puts)
5295 fn = build_call_expr (fn_puts, 1, CALL_EXPR_ARG (exp, 1));
5297 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5298 else if (strcmp (fmt_str, target_percent_c) == 0)
5300 if ((nargs != 2)
5301 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE)
5302 return NULL_RTX;
5303 if (fn_putchar)
5304 fn = build_call_expr (fn_putchar, 1, CALL_EXPR_ARG (exp, 1));
5306 else
5308 /* We can't handle anything else with % args or %% ... yet. */
5309 if (strchr (fmt_str, target_percent))
5310 return NULL_RTX;
5312 if (nargs > 1)
5313 return NULL_RTX;
5315 /* If the format specifier was "", printf does nothing. */
5316 if (fmt_str[0] == '\0')
5317 return const0_rtx;
5318 /* If the format specifier has length of 1, call putchar. */
5319 if (fmt_str[1] == '\0')
5321 /* Given printf("c"), (where c is any one character,)
5322 convert "c"[0] to an int and pass that to the replacement
5323 function. */
5324 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5325 if (fn_putchar)
5326 fn = build_call_expr (fn_putchar, 1, arg);
5328 else
5330 /* If the format specifier was "string\n", call puts("string"). */
5331 size_t len = strlen (fmt_str);
5332 if ((unsigned char)fmt_str[len - 1] == target_newline)
5334 /* Create a NUL-terminated string that's one char shorter
5335 than the original, stripping off the trailing '\n'. */
5336 char *newstr = alloca (len);
5337 memcpy (newstr, fmt_str, len - 1);
5338 newstr[len - 1] = 0;
5339 arg = build_string_literal (len, newstr);
5340 if (fn_puts)
5341 fn = build_call_expr (fn_puts, 1, arg);
5343 else
5344 /* We'd like to arrange to call fputs(string,stdout) here,
5345 but we need stdout and don't have a way to get it yet. */
5346 return NULL_RTX;
5350 if (!fn)
5351 return NULL_RTX;
5352 if (TREE_CODE (fn) == CALL_EXPR)
5353 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5354 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5357 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5358 Return NULL_RTX if a normal call should be emitted rather than transforming
5359 the function inline. If convenient, the result should be placed in
5360 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5361 call. */
5362 static rtx
5363 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5364 bool unlocked)
5366 /* If we're using an unlocked function, assume the other unlocked
5367 functions exist explicitly. */
5368 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5369 : implicit_built_in_decls[BUILT_IN_FPUTC];
5370 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5371 : implicit_built_in_decls[BUILT_IN_FPUTS];
5372 const char *fmt_str;
5373 tree fn = 0;
5374 tree fmt, fp, arg;
5375 int nargs = call_expr_nargs (exp);
5377 /* If the return value is used, don't do the transformation. */
5378 if (target != const0_rtx)
5379 return NULL_RTX;
5381 /* Verify the required arguments in the original call. */
5382 if (nargs < 2)
5383 return NULL_RTX;
5384 fp = CALL_EXPR_ARG (exp, 0);
5385 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5386 return NULL_RTX;
5387 fmt = CALL_EXPR_ARG (exp, 1);
5388 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5389 return NULL_RTX;
5391 /* Check whether the format is a literal string constant. */
5392 fmt_str = c_getstr (fmt);
5393 if (fmt_str == NULL)
5394 return NULL_RTX;
5396 if (!init_target_chars ())
5397 return NULL_RTX;
5399 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5400 if (strcmp (fmt_str, target_percent_s) == 0)
5402 if ((nargs != 3)
5403 || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2))))
5404 return NULL_RTX;
5405 arg = CALL_EXPR_ARG (exp, 2);
5406 if (fn_fputs)
5407 fn = build_call_expr (fn_fputs, 2, arg, fp);
5409 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5410 else if (strcmp (fmt_str, target_percent_c) == 0)
5412 if ((nargs != 3)
5413 || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE)
5414 return NULL_RTX;
5415 arg = CALL_EXPR_ARG (exp, 2);
5416 if (fn_fputc)
5417 fn = build_call_expr (fn_fputc, 2, arg, fp);
5419 else
5421 /* We can't handle anything else with % args or %% ... yet. */
5422 if (strchr (fmt_str, target_percent))
5423 return NULL_RTX;
5425 if (nargs > 2)
5426 return NULL_RTX;
5428 /* If the format specifier was "", fprintf does nothing. */
5429 if (fmt_str[0] == '\0')
5431 /* Evaluate and ignore FILE* argument for side-effects. */
5432 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5433 return const0_rtx;
5436 /* When "string" doesn't contain %, replace all cases of
5437 fprintf(stream,string) with fputs(string,stream). The fputs
5438 builtin will take care of special cases like length == 1. */
5439 if (fn_fputs)
5440 fn = build_call_expr (fn_fputs, 2, fmt, fp);
5443 if (!fn)
5444 return NULL_RTX;
5445 if (TREE_CODE (fn) == CALL_EXPR)
5446 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5447 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5450 /* Expand a call EXP to sprintf. Return NULL_RTX if
5451 a normal call should be emitted rather than expanding the function
5452 inline. If convenient, the result should be placed in TARGET with
5453 mode MODE. */
5455 static rtx
5456 expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode)
5458 tree dest, fmt;
5459 const char *fmt_str;
5460 int nargs = call_expr_nargs (exp);
5462 /* Verify the required arguments in the original call. */
5463 if (nargs < 2)
5464 return NULL_RTX;
5465 dest = CALL_EXPR_ARG (exp, 0);
5466 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5467 return NULL_RTX;
5468 fmt = CALL_EXPR_ARG (exp, 0);
5469 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5470 return NULL_RTX;
5472 /* Check whether the format is a literal string constant. */
5473 fmt_str = c_getstr (fmt);
5474 if (fmt_str == NULL)
5475 return NULL_RTX;
5477 if (!init_target_chars ())
5478 return NULL_RTX;
5480 /* If the format doesn't contain % args or %%, use strcpy. */
5481 if (strchr (fmt_str, target_percent) == 0)
5483 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5484 tree exp;
5486 if ((nargs > 2) || ! fn)
5487 return NULL_RTX;
5488 expand_expr (build_call_expr (fn, 2, dest, fmt),
5489 const0_rtx, VOIDmode, EXPAND_NORMAL);
5490 if (target == const0_rtx)
5491 return const0_rtx;
5492 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5493 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5495 /* If the format is "%s", use strcpy if the result isn't used. */
5496 else if (strcmp (fmt_str, target_percent_s) == 0)
5498 tree fn, arg, len;
5499 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5501 if (! fn)
5502 return NULL_RTX;
5503 if (nargs != 3)
5504 return NULL_RTX;
5505 arg = CALL_EXPR_ARG (exp, 2);
5506 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5507 return NULL_RTX;
5509 if (target != const0_rtx)
5511 len = c_strlen (arg, 1);
5512 if (! len || TREE_CODE (len) != INTEGER_CST)
5513 return NULL_RTX;
5515 else
5516 len = NULL_TREE;
5518 expand_expr (build_call_expr (fn, 2, dest, arg),
5519 const0_rtx, VOIDmode, EXPAND_NORMAL);
5521 if (target == const0_rtx)
5522 return const0_rtx;
5523 return expand_expr (len, target, mode, EXPAND_NORMAL);
5526 return NULL_RTX;
5529 /* Expand a call to either the entry or exit function profiler. */
5531 static rtx
5532 expand_builtin_profile_func (bool exitp)
5534 rtx this, which;
5536 this = DECL_RTL (current_function_decl);
5537 gcc_assert (MEM_P (this));
5538 this = XEXP (this, 0);
5540 if (exitp)
5541 which = profile_function_exit_libfunc;
5542 else
5543 which = profile_function_entry_libfunc;
5545 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5546 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5548 Pmode);
5550 return const0_rtx;
5553 /* Expand a call to __builtin___clear_cache. */
5555 static rtx
5556 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
5558 #ifndef HAVE_clear_cache
5559 #ifdef CLEAR_INSN_CACHE
5560 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5561 does something. Just do the default expansion to a call to
5562 __clear_cache(). */
5563 return NULL_RTX;
5564 #else
5565 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5566 does nothing. There is no need to call it. Do nothing. */
5567 return const0_rtx;
5568 #endif /* CLEAR_INSN_CACHE */
5569 #else
5570 /* We have a "clear_cache" insn, and it will handle everything. */
5571 tree begin, end;
5572 rtx begin_rtx, end_rtx;
5573 enum insn_code icode;
5575 /* We must not expand to a library call. If we did, any
5576 fallback library function in libgcc that might contain a call to
5577 __builtin___clear_cache() would recurse infinitely. */
5578 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5580 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5581 return const0_rtx;
5584 if (HAVE_clear_cache)
5586 icode = CODE_FOR_clear_cache;
5588 begin = CALL_EXPR_ARG (exp, 0);
5589 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5590 begin_rtx = convert_memory_address (Pmode, begin_rtx);
5591 if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode))
5592 begin_rtx = copy_to_mode_reg (Pmode, begin_rtx);
5594 end = CALL_EXPR_ARG (exp, 1);
5595 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5596 end_rtx = convert_memory_address (Pmode, end_rtx);
5597 if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode))
5598 end_rtx = copy_to_mode_reg (Pmode, end_rtx);
5600 emit_insn (gen_clear_cache (begin_rtx, end_rtx));
5602 return const0_rtx;
5603 #endif /* HAVE_clear_cache */
5606 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5608 static rtx
5609 round_trampoline_addr (rtx tramp)
5611 rtx temp, addend, mask;
5613 /* If we don't need too much alignment, we'll have been guaranteed
5614 proper alignment by get_trampoline_type. */
5615 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5616 return tramp;
5618 /* Round address up to desired boundary. */
5619 temp = gen_reg_rtx (Pmode);
5620 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5621 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5623 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5624 temp, 0, OPTAB_LIB_WIDEN);
5625 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5626 temp, 0, OPTAB_LIB_WIDEN);
5628 return tramp;
5631 static rtx
5632 expand_builtin_init_trampoline (tree exp)
5634 tree t_tramp, t_func, t_chain;
5635 rtx r_tramp, r_func, r_chain;
5636 #ifdef TRAMPOLINE_TEMPLATE
5637 rtx blktramp;
5638 #endif
5640 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5641 POINTER_TYPE, VOID_TYPE))
5642 return NULL_RTX;
5644 t_tramp = CALL_EXPR_ARG (exp, 0);
5645 t_func = CALL_EXPR_ARG (exp, 1);
5646 t_chain = CALL_EXPR_ARG (exp, 2);
5648 r_tramp = expand_normal (t_tramp);
5649 r_func = expand_normal (t_func);
5650 r_chain = expand_normal (t_chain);
5652 /* Generate insns to initialize the trampoline. */
5653 r_tramp = round_trampoline_addr (r_tramp);
5654 #ifdef TRAMPOLINE_TEMPLATE
5655 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5656 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5657 emit_block_move (blktramp, assemble_trampoline_template (),
5658 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5659 #endif
5660 trampolines_created = 1;
5661 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5663 return const0_rtx;
5666 static rtx
5667 expand_builtin_adjust_trampoline (tree exp)
5669 rtx tramp;
5671 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5672 return NULL_RTX;
5674 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5675 tramp = round_trampoline_addr (tramp);
5676 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5677 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5678 #endif
5680 return tramp;
5683 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5684 function. The function first checks whether the back end provides
5685 an insn to implement signbit for the respective mode. If not, it
5686 checks whether the floating point format of the value is such that
5687 the sign bit can be extracted. If that is not the case, the
5688 function returns NULL_RTX to indicate that a normal call should be
5689 emitted rather than expanding the function in-line. EXP is the
5690 expression that is a call to the builtin function; if convenient,
5691 the result should be placed in TARGET. */
5692 static rtx
5693 expand_builtin_signbit (tree exp, rtx target)
5695 const struct real_format *fmt;
5696 enum machine_mode fmode, imode, rmode;
5697 HOST_WIDE_INT hi, lo;
5698 tree arg;
5699 int word, bitpos;
5700 enum insn_code icode;
5701 rtx temp;
5703 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5704 return NULL_RTX;
5706 arg = CALL_EXPR_ARG (exp, 0);
5707 fmode = TYPE_MODE (TREE_TYPE (arg));
5708 rmode = TYPE_MODE (TREE_TYPE (exp));
5709 fmt = REAL_MODE_FORMAT (fmode);
5711 arg = builtin_save_expr (arg);
5713 /* Expand the argument yielding a RTX expression. */
5714 temp = expand_normal (arg);
5716 /* Check if the back end provides an insn that handles signbit for the
5717 argument's mode. */
5718 icode = signbit_optab->handlers [(int) fmode].insn_code;
5719 if (icode != CODE_FOR_nothing)
5721 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5722 emit_unop_insn (icode, target, temp, UNKNOWN);
5723 return target;
5726 /* For floating point formats without a sign bit, implement signbit
5727 as "ARG < 0.0". */
5728 bitpos = fmt->signbit_ro;
5729 if (bitpos < 0)
5731 /* But we can't do this if the format supports signed zero. */
5732 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5733 return NULL_RTX;
5735 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5736 build_real (TREE_TYPE (arg), dconst0));
5737 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5740 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5742 imode = int_mode_for_mode (fmode);
5743 if (imode == BLKmode)
5744 return NULL_RTX;
5745 temp = gen_lowpart (imode, temp);
5747 else
5749 imode = word_mode;
5750 /* Handle targets with different FP word orders. */
5751 if (FLOAT_WORDS_BIG_ENDIAN)
5752 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5753 else
5754 word = bitpos / BITS_PER_WORD;
5755 temp = operand_subword_force (temp, word, fmode);
5756 bitpos = bitpos % BITS_PER_WORD;
5759 /* Force the intermediate word_mode (or narrower) result into a
5760 register. This avoids attempting to create paradoxical SUBREGs
5761 of floating point modes below. */
5762 temp = force_reg (imode, temp);
5764 /* If the bitpos is within the "result mode" lowpart, the operation
5765 can be implement with a single bitwise AND. Otherwise, we need
5766 a right shift and an AND. */
5768 if (bitpos < GET_MODE_BITSIZE (rmode))
5770 if (bitpos < HOST_BITS_PER_WIDE_INT)
5772 hi = 0;
5773 lo = (HOST_WIDE_INT) 1 << bitpos;
5775 else
5777 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5778 lo = 0;
5781 if (imode != rmode)
5782 temp = gen_lowpart (rmode, temp);
5783 temp = expand_binop (rmode, and_optab, temp,
5784 immed_double_const (lo, hi, rmode),
5785 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5787 else
5789 /* Perform a logical right shift to place the signbit in the least
5790 significant bit, then truncate the result to the desired mode
5791 and mask just this bit. */
5792 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5793 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5794 temp = gen_lowpart (rmode, temp);
5795 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5796 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5799 return temp;
5802 /* Expand fork or exec calls. TARGET is the desired target of the
5803 call. EXP is the call. FN is the
5804 identificator of the actual function. IGNORE is nonzero if the
5805 value is to be ignored. */
5807 static rtx
5808 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5810 tree id, decl;
5811 tree call;
5813 /* If we are not profiling, just call the function. */
5814 if (!profile_arc_flag)
5815 return NULL_RTX;
5817 /* Otherwise call the wrapper. This should be equivalent for the rest of
5818 compiler, so the code does not diverge, and the wrapper may run the
5819 code necessary for keeping the profiling sane. */
5821 switch (DECL_FUNCTION_CODE (fn))
5823 case BUILT_IN_FORK:
5824 id = get_identifier ("__gcov_fork");
5825 break;
5827 case BUILT_IN_EXECL:
5828 id = get_identifier ("__gcov_execl");
5829 break;
5831 case BUILT_IN_EXECV:
5832 id = get_identifier ("__gcov_execv");
5833 break;
5835 case BUILT_IN_EXECLP:
5836 id = get_identifier ("__gcov_execlp");
5837 break;
5839 case BUILT_IN_EXECLE:
5840 id = get_identifier ("__gcov_execle");
5841 break;
5843 case BUILT_IN_EXECVP:
5844 id = get_identifier ("__gcov_execvp");
5845 break;
5847 case BUILT_IN_EXECVE:
5848 id = get_identifier ("__gcov_execve");
5849 break;
5851 default:
5852 gcc_unreachable ();
5855 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5856 DECL_EXTERNAL (decl) = 1;
5857 TREE_PUBLIC (decl) = 1;
5858 DECL_ARTIFICIAL (decl) = 1;
5859 TREE_NOTHROW (decl) = 1;
5860 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5861 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5862 call = rewrite_call_expr (exp, 0, decl, 0);
5863 return expand_call (call, target, ignore);
5868 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5869 the pointer in these functions is void*, the tree optimizers may remove
5870 casts. The mode computed in expand_builtin isn't reliable either, due
5871 to __sync_bool_compare_and_swap.
5873 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5874 group of builtins. This gives us log2 of the mode size. */
5876 static inline enum machine_mode
5877 get_builtin_sync_mode (int fcode_diff)
5879 /* The size is not negotiable, so ask not to get BLKmode in return
5880 if the target indicates that a smaller size would be better. */
5881 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5884 /* Expand the memory expression LOC and return the appropriate memory operand
5885 for the builtin_sync operations. */
5887 static rtx
5888 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5890 rtx addr, mem;
5892 addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM);
5894 /* Note that we explicitly do not want any alias information for this
5895 memory, so that we kill all other live memories. Otherwise we don't
5896 satisfy the full barrier semantics of the intrinsic. */
5897 mem = validize_mem (gen_rtx_MEM (mode, addr));
5899 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5900 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5901 MEM_VOLATILE_P (mem) = 1;
5903 return mem;
5906 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5907 EXP is the CALL_EXPR. CODE is the rtx code
5908 that corresponds to the arithmetic or logical operation from the name;
5909 an exception here is that NOT actually means NAND. TARGET is an optional
5910 place for us to store the results; AFTER is true if this is the
5911 fetch_and_xxx form. IGNORE is true if we don't actually care about
5912 the result of the operation at all. */
5914 static rtx
5915 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5916 enum rtx_code code, bool after,
5917 rtx target, bool ignore)
5919 rtx val, mem;
5920 enum machine_mode old_mode;
5922 /* Expand the operands. */
5923 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5925 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5926 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5927 of CONST_INTs, where we know the old_mode only from the call argument. */
5928 old_mode = GET_MODE (val);
5929 if (old_mode == VOIDmode)
5930 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5931 val = convert_modes (mode, old_mode, val, 1);
5933 if (ignore)
5934 return expand_sync_operation (mem, val, code);
5935 else
5936 return expand_sync_fetch_operation (mem, val, code, after, target);
5939 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5940 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5941 true if this is the boolean form. TARGET is a place for us to store the
5942 results; this is NOT optional if IS_BOOL is true. */
5944 static rtx
5945 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5946 bool is_bool, rtx target)
5948 rtx old_val, new_val, mem;
5949 enum machine_mode old_mode;
5951 /* Expand the operands. */
5952 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5955 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5956 mode, EXPAND_NORMAL);
5957 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5958 of CONST_INTs, where we know the old_mode only from the call argument. */
5959 old_mode = GET_MODE (old_val);
5960 if (old_mode == VOIDmode)
5961 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5962 old_val = convert_modes (mode, old_mode, old_val, 1);
5964 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5965 mode, EXPAND_NORMAL);
5966 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5967 of CONST_INTs, where we know the old_mode only from the call argument. */
5968 old_mode = GET_MODE (new_val);
5969 if (old_mode == VOIDmode)
5970 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5971 new_val = convert_modes (mode, old_mode, new_val, 1);
5973 if (is_bool)
5974 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5975 else
5976 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5979 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5980 general form is actually an atomic exchange, and some targets only
5981 support a reduced form with the second argument being a constant 1.
5982 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5983 the results. */
5985 static rtx
5986 expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp,
5987 rtx target)
5989 rtx val, mem;
5990 enum machine_mode old_mode;
5992 /* Expand the operands. */
5993 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5994 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5995 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5996 of CONST_INTs, where we know the old_mode only from the call argument. */
5997 old_mode = GET_MODE (val);
5998 if (old_mode == VOIDmode)
5999 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
6000 val = convert_modes (mode, old_mode, val, 1);
6002 return expand_sync_lock_test_and_set (mem, val, target);
6005 /* Expand the __sync_synchronize intrinsic. */
6007 static void
6008 expand_builtin_synchronize (void)
6010 tree x;
6012 #ifdef HAVE_memory_barrier
6013 if (HAVE_memory_barrier)
6015 emit_insn (gen_memory_barrier ());
6016 return;
6018 #endif
6020 /* If no explicit memory barrier instruction is available, create an
6021 empty asm stmt with a memory clobber. */
6022 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
6023 tree_cons (NULL, build_string (6, "memory"), NULL));
6024 ASM_VOLATILE_P (x) = 1;
6025 expand_asm_expr (x);
6028 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6030 static void
6031 expand_builtin_lock_release (enum machine_mode mode, tree exp)
6033 enum insn_code icode;
6034 rtx mem, insn;
6035 rtx val = const0_rtx;
6037 /* Expand the operands. */
6038 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6040 /* If there is an explicit operation in the md file, use it. */
6041 icode = sync_lock_release[mode];
6042 if (icode != CODE_FOR_nothing)
6044 if (!insn_data[icode].operand[1].predicate (val, mode))
6045 val = force_reg (mode, val);
6047 insn = GEN_FCN (icode) (mem, val);
6048 if (insn)
6050 emit_insn (insn);
6051 return;
6055 /* Otherwise we can implement this operation by emitting a barrier
6056 followed by a store of zero. */
6057 expand_builtin_synchronize ();
6058 emit_move_insn (mem, val);
6061 /* Expand an expression EXP that calls a built-in function,
6062 with result going to TARGET if that's convenient
6063 (and in mode MODE if that's convenient).
6064 SUBTARGET may be used as the target for computing one of EXP's operands.
6065 IGNORE is nonzero if the value is to be ignored. */
6068 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
6069 int ignore)
6071 tree fndecl = get_callee_fndecl (exp);
6072 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6073 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6075 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6076 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6078 /* When not optimizing, generate calls to library functions for a certain
6079 set of builtins. */
6080 if (!optimize
6081 && !called_as_built_in (fndecl)
6082 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
6083 && fcode != BUILT_IN_ALLOCA)
6084 return expand_call (exp, target, ignore);
6086 /* The built-in function expanders test for target == const0_rtx
6087 to determine whether the function's result will be ignored. */
6088 if (ignore)
6089 target = const0_rtx;
6091 /* If the result of a pure or const built-in function is ignored, and
6092 none of its arguments are volatile, we can avoid expanding the
6093 built-in call and just evaluate the arguments for side-effects. */
6094 if (target == const0_rtx
6095 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
6097 bool volatilep = false;
6098 tree arg;
6099 call_expr_arg_iterator iter;
6101 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6102 if (TREE_THIS_VOLATILE (arg))
6104 volatilep = true;
6105 break;
6108 if (! volatilep)
6110 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6111 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6112 return const0_rtx;
6116 switch (fcode)
6118 CASE_FLT_FN (BUILT_IN_FABS):
6119 target = expand_builtin_fabs (exp, target, subtarget);
6120 if (target)
6121 return target;
6122 break;
6124 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6125 target = expand_builtin_copysign (exp, target, subtarget);
6126 if (target)
6127 return target;
6128 break;
6130 /* Just do a normal library call if we were unable to fold
6131 the values. */
6132 CASE_FLT_FN (BUILT_IN_CABS):
6133 break;
6135 CASE_FLT_FN (BUILT_IN_EXP):
6136 CASE_FLT_FN (BUILT_IN_EXP10):
6137 CASE_FLT_FN (BUILT_IN_POW10):
6138 CASE_FLT_FN (BUILT_IN_EXP2):
6139 CASE_FLT_FN (BUILT_IN_EXPM1):
6140 CASE_FLT_FN (BUILT_IN_LOGB):
6141 CASE_FLT_FN (BUILT_IN_LOG):
6142 CASE_FLT_FN (BUILT_IN_LOG10):
6143 CASE_FLT_FN (BUILT_IN_LOG2):
6144 CASE_FLT_FN (BUILT_IN_LOG1P):
6145 CASE_FLT_FN (BUILT_IN_TAN):
6146 CASE_FLT_FN (BUILT_IN_ASIN):
6147 CASE_FLT_FN (BUILT_IN_ACOS):
6148 CASE_FLT_FN (BUILT_IN_ATAN):
6149 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6150 because of possible accuracy problems. */
6151 if (! flag_unsafe_math_optimizations)
6152 break;
6153 CASE_FLT_FN (BUILT_IN_SQRT):
6154 CASE_FLT_FN (BUILT_IN_FLOOR):
6155 CASE_FLT_FN (BUILT_IN_CEIL):
6156 CASE_FLT_FN (BUILT_IN_TRUNC):
6157 CASE_FLT_FN (BUILT_IN_ROUND):
6158 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6159 CASE_FLT_FN (BUILT_IN_RINT):
6160 target = expand_builtin_mathfn (exp, target, subtarget);
6161 if (target)
6162 return target;
6163 break;
6165 CASE_FLT_FN (BUILT_IN_ILOGB):
6166 if (! flag_unsafe_math_optimizations)
6167 break;
6168 CASE_FLT_FN (BUILT_IN_ISINF):
6169 CASE_FLT_FN (BUILT_IN_FINITE):
6170 case BUILT_IN_ISFINITE:
6171 case BUILT_IN_ISNORMAL:
6172 target = expand_builtin_interclass_mathfn (exp, target, subtarget);
6173 if (target)
6174 return target;
6175 break;
6177 CASE_FLT_FN (BUILT_IN_LCEIL):
6178 CASE_FLT_FN (BUILT_IN_LLCEIL):
6179 CASE_FLT_FN (BUILT_IN_LFLOOR):
6180 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6181 target = expand_builtin_int_roundingfn (exp, target, subtarget);
6182 if (target)
6183 return target;
6184 break;
6186 CASE_FLT_FN (BUILT_IN_LRINT):
6187 CASE_FLT_FN (BUILT_IN_LLRINT):
6188 CASE_FLT_FN (BUILT_IN_LROUND):
6189 CASE_FLT_FN (BUILT_IN_LLROUND):
6190 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
6191 if (target)
6192 return target;
6193 break;
6195 CASE_FLT_FN (BUILT_IN_POW):
6196 target = expand_builtin_pow (exp, target, subtarget);
6197 if (target)
6198 return target;
6199 break;
6201 CASE_FLT_FN (BUILT_IN_POWI):
6202 target = expand_builtin_powi (exp, target, subtarget);
6203 if (target)
6204 return target;
6205 break;
6207 CASE_FLT_FN (BUILT_IN_ATAN2):
6208 CASE_FLT_FN (BUILT_IN_LDEXP):
6209 CASE_FLT_FN (BUILT_IN_SCALB):
6210 CASE_FLT_FN (BUILT_IN_SCALBN):
6211 CASE_FLT_FN (BUILT_IN_SCALBLN):
6212 if (! flag_unsafe_math_optimizations)
6213 break;
6215 CASE_FLT_FN (BUILT_IN_FMOD):
6216 CASE_FLT_FN (BUILT_IN_REMAINDER):
6217 CASE_FLT_FN (BUILT_IN_DREM):
6218 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6219 if (target)
6220 return target;
6221 break;
6223 CASE_FLT_FN (BUILT_IN_CEXPI):
6224 target = expand_builtin_cexpi (exp, target, subtarget);
6225 gcc_assert (target);
6226 return target;
6228 CASE_FLT_FN (BUILT_IN_SIN):
6229 CASE_FLT_FN (BUILT_IN_COS):
6230 if (! flag_unsafe_math_optimizations)
6231 break;
6232 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6233 if (target)
6234 return target;
6235 break;
6237 CASE_FLT_FN (BUILT_IN_SINCOS):
6238 if (! flag_unsafe_math_optimizations)
6239 break;
6240 target = expand_builtin_sincos (exp);
6241 if (target)
6242 return target;
6243 break;
6245 case BUILT_IN_APPLY_ARGS:
6246 return expand_builtin_apply_args ();
6248 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6249 FUNCTION with a copy of the parameters described by
6250 ARGUMENTS, and ARGSIZE. It returns a block of memory
6251 allocated on the stack into which is stored all the registers
6252 that might possibly be used for returning the result of a
6253 function. ARGUMENTS is the value returned by
6254 __builtin_apply_args. ARGSIZE is the number of bytes of
6255 arguments that must be copied. ??? How should this value be
6256 computed? We'll also need a safe worst case value for varargs
6257 functions. */
6258 case BUILT_IN_APPLY:
6259 if (!validate_arglist (exp, POINTER_TYPE,
6260 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6261 && !validate_arglist (exp, REFERENCE_TYPE,
6262 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6263 return const0_rtx;
6264 else
6266 rtx ops[3];
6268 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6269 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6270 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6272 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6275 /* __builtin_return (RESULT) causes the function to return the
6276 value described by RESULT. RESULT is address of the block of
6277 memory returned by __builtin_apply. */
6278 case BUILT_IN_RETURN:
6279 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6280 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6281 return const0_rtx;
6283 case BUILT_IN_SAVEREGS:
6284 return expand_builtin_saveregs ();
6286 case BUILT_IN_ARGS_INFO:
6287 return expand_builtin_args_info (exp);
6289 case BUILT_IN_VA_ARG_PACK:
6290 /* All valid uses of __builtin_va_arg_pack () are removed during
6291 inlining. */
6292 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6293 return const0_rtx;
6295 case BUILT_IN_VA_ARG_PACK_LEN:
6296 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6297 inlining. */
6298 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6299 return const0_rtx;
6301 /* Return the address of the first anonymous stack arg. */
6302 case BUILT_IN_NEXT_ARG:
6303 if (fold_builtin_next_arg (exp, false))
6304 return const0_rtx;
6305 return expand_builtin_next_arg ();
6307 case BUILT_IN_CLEAR_CACHE:
6308 target = expand_builtin___clear_cache (exp);
6309 if (target)
6310 return target;
6311 break;
6313 case BUILT_IN_CLASSIFY_TYPE:
6314 return expand_builtin_classify_type (exp);
6316 case BUILT_IN_CONSTANT_P:
6317 return const0_rtx;
6319 case BUILT_IN_FRAME_ADDRESS:
6320 case BUILT_IN_RETURN_ADDRESS:
6321 return expand_builtin_frame_address (fndecl, exp);
6323 /* Returns the address of the area where the structure is returned.
6324 0 otherwise. */
6325 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6326 if (call_expr_nargs (exp) != 0
6327 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6328 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6329 return const0_rtx;
6330 else
6331 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6333 case BUILT_IN_ALLOCA:
6334 target = expand_builtin_alloca (exp, target);
6335 if (target)
6336 return target;
6337 break;
6339 case BUILT_IN_STACK_SAVE:
6340 return expand_stack_save ();
6342 case BUILT_IN_STACK_RESTORE:
6343 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6344 return const0_rtx;
6346 case BUILT_IN_BSWAP32:
6347 case BUILT_IN_BSWAP64:
6348 target = expand_builtin_bswap (exp, target, subtarget);
6350 if (target)
6351 return target;
6352 break;
6354 CASE_INT_FN (BUILT_IN_FFS):
6355 case BUILT_IN_FFSIMAX:
6356 target = expand_builtin_unop (target_mode, exp, target,
6357 subtarget, ffs_optab);
6358 if (target)
6359 return target;
6360 break;
6362 CASE_INT_FN (BUILT_IN_CLZ):
6363 case BUILT_IN_CLZIMAX:
6364 target = expand_builtin_unop (target_mode, exp, target,
6365 subtarget, clz_optab);
6366 if (target)
6367 return target;
6368 break;
6370 CASE_INT_FN (BUILT_IN_CTZ):
6371 case BUILT_IN_CTZIMAX:
6372 target = expand_builtin_unop (target_mode, exp, target,
6373 subtarget, ctz_optab);
6374 if (target)
6375 return target;
6376 break;
6378 CASE_INT_FN (BUILT_IN_POPCOUNT):
6379 case BUILT_IN_POPCOUNTIMAX:
6380 target = expand_builtin_unop (target_mode, exp, target,
6381 subtarget, popcount_optab);
6382 if (target)
6383 return target;
6384 break;
6386 CASE_INT_FN (BUILT_IN_PARITY):
6387 case BUILT_IN_PARITYIMAX:
6388 target = expand_builtin_unop (target_mode, exp, target,
6389 subtarget, parity_optab);
6390 if (target)
6391 return target;
6392 break;
6394 case BUILT_IN_STRLEN:
6395 target = expand_builtin_strlen (exp, target, target_mode);
6396 if (target)
6397 return target;
6398 break;
6400 case BUILT_IN_STRCPY:
6401 target = expand_builtin_strcpy (fndecl, exp, target, mode);
6402 if (target)
6403 return target;
6404 break;
6406 case BUILT_IN_STRNCPY:
6407 target = expand_builtin_strncpy (exp, target, mode);
6408 if (target)
6409 return target;
6410 break;
6412 case BUILT_IN_STPCPY:
6413 target = expand_builtin_stpcpy (exp, target, mode);
6414 if (target)
6415 return target;
6416 break;
6418 case BUILT_IN_STRCAT:
6419 target = expand_builtin_strcat (fndecl, exp, target, mode);
6420 if (target)
6421 return target;
6422 break;
6424 case BUILT_IN_STRNCAT:
6425 target = expand_builtin_strncat (exp, target, mode);
6426 if (target)
6427 return target;
6428 break;
6430 case BUILT_IN_STRSPN:
6431 target = expand_builtin_strspn (exp, target, mode);
6432 if (target)
6433 return target;
6434 break;
6436 case BUILT_IN_STRCSPN:
6437 target = expand_builtin_strcspn (exp, target, mode);
6438 if (target)
6439 return target;
6440 break;
6442 case BUILT_IN_STRSTR:
6443 target = expand_builtin_strstr (exp, target, mode);
6444 if (target)
6445 return target;
6446 break;
6448 case BUILT_IN_STRPBRK:
6449 target = expand_builtin_strpbrk (exp, target, mode);
6450 if (target)
6451 return target;
6452 break;
6454 case BUILT_IN_INDEX:
6455 case BUILT_IN_STRCHR:
6456 target = expand_builtin_strchr (exp, target, mode);
6457 if (target)
6458 return target;
6459 break;
6461 case BUILT_IN_RINDEX:
6462 case BUILT_IN_STRRCHR:
6463 target = expand_builtin_strrchr (exp, target, mode);
6464 if (target)
6465 return target;
6466 break;
6468 case BUILT_IN_MEMCPY:
6469 target = expand_builtin_memcpy (exp, target, mode);
6470 if (target)
6471 return target;
6472 break;
6474 case BUILT_IN_MEMPCPY:
6475 target = expand_builtin_mempcpy (exp, target, mode);
6476 if (target)
6477 return target;
6478 break;
6480 case BUILT_IN_MEMMOVE:
6481 target = expand_builtin_memmove (exp, target, mode, ignore);
6482 if (target)
6483 return target;
6484 break;
6486 case BUILT_IN_BCOPY:
6487 target = expand_builtin_bcopy (exp, ignore);
6488 if (target)
6489 return target;
6490 break;
6492 case BUILT_IN_MEMSET:
6493 target = expand_builtin_memset (exp, target, mode);
6494 if (target)
6495 return target;
6496 break;
6498 case BUILT_IN_BZERO:
6499 target = expand_builtin_bzero (exp);
6500 if (target)
6501 return target;
6502 break;
6504 case BUILT_IN_STRCMP:
6505 target = expand_builtin_strcmp (exp, target, mode);
6506 if (target)
6507 return target;
6508 break;
6510 case BUILT_IN_STRNCMP:
6511 target = expand_builtin_strncmp (exp, target, mode);
6512 if (target)
6513 return target;
6514 break;
6516 case BUILT_IN_MEMCHR:
6517 target = expand_builtin_memchr (exp, target, mode);
6518 if (target)
6519 return target;
6520 break;
6522 case BUILT_IN_BCMP:
6523 case BUILT_IN_MEMCMP:
6524 target = expand_builtin_memcmp (exp, target, mode);
6525 if (target)
6526 return target;
6527 break;
6529 case BUILT_IN_SETJMP:
6530 /* This should have been lowered to the builtins below. */
6531 gcc_unreachable ();
6533 case BUILT_IN_SETJMP_SETUP:
6534 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6535 and the receiver label. */
6536 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6538 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6539 VOIDmode, EXPAND_NORMAL);
6540 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6541 rtx label_r = label_rtx (label);
6543 /* This is copied from the handling of non-local gotos. */
6544 expand_builtin_setjmp_setup (buf_addr, label_r);
6545 nonlocal_goto_handler_labels
6546 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6547 nonlocal_goto_handler_labels);
6548 /* ??? Do not let expand_label treat us as such since we would
6549 not want to be both on the list of non-local labels and on
6550 the list of forced labels. */
6551 FORCED_LABEL (label) = 0;
6552 return const0_rtx;
6554 break;
6556 case BUILT_IN_SETJMP_DISPATCHER:
6557 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6558 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6560 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6561 rtx label_r = label_rtx (label);
6563 /* Remove the dispatcher label from the list of non-local labels
6564 since the receiver labels have been added to it above. */
6565 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6566 return const0_rtx;
6568 break;
6570 case BUILT_IN_SETJMP_RECEIVER:
6571 /* __builtin_setjmp_receiver is passed the receiver label. */
6572 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6574 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6575 rtx label_r = label_rtx (label);
6577 expand_builtin_setjmp_receiver (label_r);
6578 return const0_rtx;
6580 break;
6582 /* __builtin_longjmp is passed a pointer to an array of five words.
6583 It's similar to the C library longjmp function but works with
6584 __builtin_setjmp above. */
6585 case BUILT_IN_LONGJMP:
6586 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6588 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6589 VOIDmode, EXPAND_NORMAL);
6590 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6592 if (value != const1_rtx)
6594 error ("%<__builtin_longjmp%> second argument must be 1");
6595 return const0_rtx;
6598 expand_builtin_longjmp (buf_addr, value);
6599 return const0_rtx;
6601 break;
6603 case BUILT_IN_NONLOCAL_GOTO:
6604 target = expand_builtin_nonlocal_goto (exp);
6605 if (target)
6606 return target;
6607 break;
6609 /* This updates the setjmp buffer that is its argument with the value
6610 of the current stack pointer. */
6611 case BUILT_IN_UPDATE_SETJMP_BUF:
6612 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6614 rtx buf_addr
6615 = expand_normal (CALL_EXPR_ARG (exp, 0));
6617 expand_builtin_update_setjmp_buf (buf_addr);
6618 return const0_rtx;
6620 break;
6622 case BUILT_IN_TRAP:
6623 expand_builtin_trap ();
6624 return const0_rtx;
6626 case BUILT_IN_PRINTF:
6627 target = expand_builtin_printf (exp, target, mode, false);
6628 if (target)
6629 return target;
6630 break;
6632 case BUILT_IN_PRINTF_UNLOCKED:
6633 target = expand_builtin_printf (exp, target, mode, true);
6634 if (target)
6635 return target;
6636 break;
6638 case BUILT_IN_FPUTS:
6639 target = expand_builtin_fputs (exp, target, false);
6640 if (target)
6641 return target;
6642 break;
6643 case BUILT_IN_FPUTS_UNLOCKED:
6644 target = expand_builtin_fputs (exp, target, true);
6645 if (target)
6646 return target;
6647 break;
6649 case BUILT_IN_FPRINTF:
6650 target = expand_builtin_fprintf (exp, target, mode, false);
6651 if (target)
6652 return target;
6653 break;
6655 case BUILT_IN_FPRINTF_UNLOCKED:
6656 target = expand_builtin_fprintf (exp, target, mode, true);
6657 if (target)
6658 return target;
6659 break;
6661 case BUILT_IN_SPRINTF:
6662 target = expand_builtin_sprintf (exp, target, mode);
6663 if (target)
6664 return target;
6665 break;
6667 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6668 case BUILT_IN_SIGNBITD32:
6669 case BUILT_IN_SIGNBITD64:
6670 case BUILT_IN_SIGNBITD128:
6671 target = expand_builtin_signbit (exp, target);
6672 if (target)
6673 return target;
6674 break;
6676 /* Various hooks for the DWARF 2 __throw routine. */
6677 case BUILT_IN_UNWIND_INIT:
6678 expand_builtin_unwind_init ();
6679 return const0_rtx;
6680 case BUILT_IN_DWARF_CFA:
6681 return virtual_cfa_rtx;
6682 #ifdef DWARF2_UNWIND_INFO
6683 case BUILT_IN_DWARF_SP_COLUMN:
6684 return expand_builtin_dwarf_sp_column ();
6685 case BUILT_IN_INIT_DWARF_REG_SIZES:
6686 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6687 return const0_rtx;
6688 #endif
6689 case BUILT_IN_FROB_RETURN_ADDR:
6690 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6691 case BUILT_IN_EXTRACT_RETURN_ADDR:
6692 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6693 case BUILT_IN_EH_RETURN:
6694 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6695 CALL_EXPR_ARG (exp, 1));
6696 return const0_rtx;
6697 #ifdef EH_RETURN_DATA_REGNO
6698 case BUILT_IN_EH_RETURN_DATA_REGNO:
6699 return expand_builtin_eh_return_data_regno (exp);
6700 #endif
6701 case BUILT_IN_EXTEND_POINTER:
6702 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6704 case BUILT_IN_VA_START:
6705 case BUILT_IN_STDARG_START:
6706 return expand_builtin_va_start (exp);
6707 case BUILT_IN_VA_END:
6708 return expand_builtin_va_end (exp);
6709 case BUILT_IN_VA_COPY:
6710 return expand_builtin_va_copy (exp);
6711 case BUILT_IN_EXPECT:
6712 return expand_builtin_expect (exp, target);
6713 case BUILT_IN_PREFETCH:
6714 expand_builtin_prefetch (exp);
6715 return const0_rtx;
6717 case BUILT_IN_PROFILE_FUNC_ENTER:
6718 return expand_builtin_profile_func (false);
6719 case BUILT_IN_PROFILE_FUNC_EXIT:
6720 return expand_builtin_profile_func (true);
6722 case BUILT_IN_INIT_TRAMPOLINE:
6723 return expand_builtin_init_trampoline (exp);
6724 case BUILT_IN_ADJUST_TRAMPOLINE:
6725 return expand_builtin_adjust_trampoline (exp);
6727 case BUILT_IN_FORK:
6728 case BUILT_IN_EXECL:
6729 case BUILT_IN_EXECV:
6730 case BUILT_IN_EXECLP:
6731 case BUILT_IN_EXECLE:
6732 case BUILT_IN_EXECVP:
6733 case BUILT_IN_EXECVE:
6734 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6735 if (target)
6736 return target;
6737 break;
6739 case BUILT_IN_FETCH_AND_ADD_1:
6740 case BUILT_IN_FETCH_AND_ADD_2:
6741 case BUILT_IN_FETCH_AND_ADD_4:
6742 case BUILT_IN_FETCH_AND_ADD_8:
6743 case BUILT_IN_FETCH_AND_ADD_16:
6744 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6745 target = expand_builtin_sync_operation (mode, exp, PLUS,
6746 false, target, ignore);
6747 if (target)
6748 return target;
6749 break;
6751 case BUILT_IN_FETCH_AND_SUB_1:
6752 case BUILT_IN_FETCH_AND_SUB_2:
6753 case BUILT_IN_FETCH_AND_SUB_4:
6754 case BUILT_IN_FETCH_AND_SUB_8:
6755 case BUILT_IN_FETCH_AND_SUB_16:
6756 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6757 target = expand_builtin_sync_operation (mode, exp, MINUS,
6758 false, target, ignore);
6759 if (target)
6760 return target;
6761 break;
6763 case BUILT_IN_FETCH_AND_OR_1:
6764 case BUILT_IN_FETCH_AND_OR_2:
6765 case BUILT_IN_FETCH_AND_OR_4:
6766 case BUILT_IN_FETCH_AND_OR_8:
6767 case BUILT_IN_FETCH_AND_OR_16:
6768 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6769 target = expand_builtin_sync_operation (mode, exp, IOR,
6770 false, target, ignore);
6771 if (target)
6772 return target;
6773 break;
6775 case BUILT_IN_FETCH_AND_AND_1:
6776 case BUILT_IN_FETCH_AND_AND_2:
6777 case BUILT_IN_FETCH_AND_AND_4:
6778 case BUILT_IN_FETCH_AND_AND_8:
6779 case BUILT_IN_FETCH_AND_AND_16:
6780 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6781 target = expand_builtin_sync_operation (mode, exp, AND,
6782 false, target, ignore);
6783 if (target)
6784 return target;
6785 break;
6787 case BUILT_IN_FETCH_AND_XOR_1:
6788 case BUILT_IN_FETCH_AND_XOR_2:
6789 case BUILT_IN_FETCH_AND_XOR_4:
6790 case BUILT_IN_FETCH_AND_XOR_8:
6791 case BUILT_IN_FETCH_AND_XOR_16:
6792 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6793 target = expand_builtin_sync_operation (mode, exp, XOR,
6794 false, target, ignore);
6795 if (target)
6796 return target;
6797 break;
6799 case BUILT_IN_FETCH_AND_NAND_1:
6800 case BUILT_IN_FETCH_AND_NAND_2:
6801 case BUILT_IN_FETCH_AND_NAND_4:
6802 case BUILT_IN_FETCH_AND_NAND_8:
6803 case BUILT_IN_FETCH_AND_NAND_16:
6804 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6805 target = expand_builtin_sync_operation (mode, exp, NOT,
6806 false, target, ignore);
6807 if (target)
6808 return target;
6809 break;
6811 case BUILT_IN_ADD_AND_FETCH_1:
6812 case BUILT_IN_ADD_AND_FETCH_2:
6813 case BUILT_IN_ADD_AND_FETCH_4:
6814 case BUILT_IN_ADD_AND_FETCH_8:
6815 case BUILT_IN_ADD_AND_FETCH_16:
6816 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6817 target = expand_builtin_sync_operation (mode, exp, PLUS,
6818 true, target, ignore);
6819 if (target)
6820 return target;
6821 break;
6823 case BUILT_IN_SUB_AND_FETCH_1:
6824 case BUILT_IN_SUB_AND_FETCH_2:
6825 case BUILT_IN_SUB_AND_FETCH_4:
6826 case BUILT_IN_SUB_AND_FETCH_8:
6827 case BUILT_IN_SUB_AND_FETCH_16:
6828 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6829 target = expand_builtin_sync_operation (mode, exp, MINUS,
6830 true, target, ignore);
6831 if (target)
6832 return target;
6833 break;
6835 case BUILT_IN_OR_AND_FETCH_1:
6836 case BUILT_IN_OR_AND_FETCH_2:
6837 case BUILT_IN_OR_AND_FETCH_4:
6838 case BUILT_IN_OR_AND_FETCH_8:
6839 case BUILT_IN_OR_AND_FETCH_16:
6840 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6841 target = expand_builtin_sync_operation (mode, exp, IOR,
6842 true, target, ignore);
6843 if (target)
6844 return target;
6845 break;
6847 case BUILT_IN_AND_AND_FETCH_1:
6848 case BUILT_IN_AND_AND_FETCH_2:
6849 case BUILT_IN_AND_AND_FETCH_4:
6850 case BUILT_IN_AND_AND_FETCH_8:
6851 case BUILT_IN_AND_AND_FETCH_16:
6852 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6853 target = expand_builtin_sync_operation (mode, exp, AND,
6854 true, target, ignore);
6855 if (target)
6856 return target;
6857 break;
6859 case BUILT_IN_XOR_AND_FETCH_1:
6860 case BUILT_IN_XOR_AND_FETCH_2:
6861 case BUILT_IN_XOR_AND_FETCH_4:
6862 case BUILT_IN_XOR_AND_FETCH_8:
6863 case BUILT_IN_XOR_AND_FETCH_16:
6864 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6865 target = expand_builtin_sync_operation (mode, exp, XOR,
6866 true, target, ignore);
6867 if (target)
6868 return target;
6869 break;
6871 case BUILT_IN_NAND_AND_FETCH_1:
6872 case BUILT_IN_NAND_AND_FETCH_2:
6873 case BUILT_IN_NAND_AND_FETCH_4:
6874 case BUILT_IN_NAND_AND_FETCH_8:
6875 case BUILT_IN_NAND_AND_FETCH_16:
6876 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6877 target = expand_builtin_sync_operation (mode, exp, NOT,
6878 true, target, ignore);
6879 if (target)
6880 return target;
6881 break;
6883 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6884 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6885 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6886 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6887 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6888 if (mode == VOIDmode)
6889 mode = TYPE_MODE (boolean_type_node);
6890 if (!target || !register_operand (target, mode))
6891 target = gen_reg_rtx (mode);
6893 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6894 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6895 if (target)
6896 return target;
6897 break;
6899 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6900 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6901 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6902 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6903 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6904 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6905 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6906 if (target)
6907 return target;
6908 break;
6910 case BUILT_IN_LOCK_TEST_AND_SET_1:
6911 case BUILT_IN_LOCK_TEST_AND_SET_2:
6912 case BUILT_IN_LOCK_TEST_AND_SET_4:
6913 case BUILT_IN_LOCK_TEST_AND_SET_8:
6914 case BUILT_IN_LOCK_TEST_AND_SET_16:
6915 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6916 target = expand_builtin_lock_test_and_set (mode, exp, target);
6917 if (target)
6918 return target;
6919 break;
6921 case BUILT_IN_LOCK_RELEASE_1:
6922 case BUILT_IN_LOCK_RELEASE_2:
6923 case BUILT_IN_LOCK_RELEASE_4:
6924 case BUILT_IN_LOCK_RELEASE_8:
6925 case BUILT_IN_LOCK_RELEASE_16:
6926 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6927 expand_builtin_lock_release (mode, exp);
6928 return const0_rtx;
6930 case BUILT_IN_SYNCHRONIZE:
6931 expand_builtin_synchronize ();
6932 return const0_rtx;
6934 case BUILT_IN_OBJECT_SIZE:
6935 return expand_builtin_object_size (exp);
6937 case BUILT_IN_MEMCPY_CHK:
6938 case BUILT_IN_MEMPCPY_CHK:
6939 case BUILT_IN_MEMMOVE_CHK:
6940 case BUILT_IN_MEMSET_CHK:
6941 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6942 if (target)
6943 return target;
6944 break;
6946 case BUILT_IN_STRCPY_CHK:
6947 case BUILT_IN_STPCPY_CHK:
6948 case BUILT_IN_STRNCPY_CHK:
6949 case BUILT_IN_STRCAT_CHK:
6950 case BUILT_IN_STRNCAT_CHK:
6951 case BUILT_IN_SNPRINTF_CHK:
6952 case BUILT_IN_VSNPRINTF_CHK:
6953 maybe_emit_chk_warning (exp, fcode);
6954 break;
6956 case BUILT_IN_SPRINTF_CHK:
6957 case BUILT_IN_VSPRINTF_CHK:
6958 maybe_emit_sprintf_chk_warning (exp, fcode);
6959 break;
6961 default: /* just do library call, if unknown builtin */
6962 break;
6965 /* The switch statement above can drop through to cause the function
6966 to be called normally. */
6967 return expand_call (exp, target, ignore);
6970 /* Determine whether a tree node represents a call to a built-in
6971 function. If the tree T is a call to a built-in function with
6972 the right number of arguments of the appropriate types, return
6973 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6974 Otherwise the return value is END_BUILTINS. */
6976 enum built_in_function
6977 builtin_mathfn_code (const_tree t)
6979 const_tree fndecl, arg, parmlist;
6980 const_tree argtype, parmtype;
6981 const_call_expr_arg_iterator iter;
6983 if (TREE_CODE (t) != CALL_EXPR
6984 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6985 return END_BUILTINS;
6987 fndecl = get_callee_fndecl (t);
6988 if (fndecl == NULL_TREE
6989 || TREE_CODE (fndecl) != FUNCTION_DECL
6990 || ! DECL_BUILT_IN (fndecl)
6991 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6992 return END_BUILTINS;
6994 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6995 init_const_call_expr_arg_iterator (t, &iter);
6996 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6998 /* If a function doesn't take a variable number of arguments,
6999 the last element in the list will have type `void'. */
7000 parmtype = TREE_VALUE (parmlist);
7001 if (VOID_TYPE_P (parmtype))
7003 if (more_const_call_expr_args_p (&iter))
7004 return END_BUILTINS;
7005 return DECL_FUNCTION_CODE (fndecl);
7008 if (! more_const_call_expr_args_p (&iter))
7009 return END_BUILTINS;
7011 arg = next_const_call_expr_arg (&iter);
7012 argtype = TREE_TYPE (arg);
7014 if (SCALAR_FLOAT_TYPE_P (parmtype))
7016 if (! SCALAR_FLOAT_TYPE_P (argtype))
7017 return END_BUILTINS;
7019 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7021 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7022 return END_BUILTINS;
7024 else if (POINTER_TYPE_P (parmtype))
7026 if (! POINTER_TYPE_P (argtype))
7027 return END_BUILTINS;
7029 else if (INTEGRAL_TYPE_P (parmtype))
7031 if (! INTEGRAL_TYPE_P (argtype))
7032 return END_BUILTINS;
7034 else
7035 return END_BUILTINS;
7038 /* Variable-length argument list. */
7039 return DECL_FUNCTION_CODE (fndecl);
7042 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7043 evaluate to a constant. */
7045 static tree
7046 fold_builtin_constant_p (tree arg)
7048 /* We return 1 for a numeric type that's known to be a constant
7049 value at compile-time or for an aggregate type that's a
7050 literal constant. */
7051 STRIP_NOPS (arg);
7053 /* If we know this is a constant, emit the constant of one. */
7054 if (CONSTANT_CLASS_P (arg)
7055 || (TREE_CODE (arg) == CONSTRUCTOR
7056 && TREE_CONSTANT (arg)))
7057 return integer_one_node;
7058 if (TREE_CODE (arg) == ADDR_EXPR)
7060 tree op = TREE_OPERAND (arg, 0);
7061 if (TREE_CODE (op) == STRING_CST
7062 || (TREE_CODE (op) == ARRAY_REF
7063 && integer_zerop (TREE_OPERAND (op, 1))
7064 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7065 return integer_one_node;
7068 /* If this expression has side effects, show we don't know it to be a
7069 constant. Likewise if it's a pointer or aggregate type since in
7070 those case we only want literals, since those are only optimized
7071 when generating RTL, not later.
7072 And finally, if we are compiling an initializer, not code, we
7073 need to return a definite result now; there's not going to be any
7074 more optimization done. */
7075 if (TREE_SIDE_EFFECTS (arg)
7076 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7077 || POINTER_TYPE_P (TREE_TYPE (arg))
7078 || cfun == 0
7079 || folding_initializer)
7080 return integer_zero_node;
7082 return NULL_TREE;
7085 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7086 return it as a truthvalue. */
7088 static tree
7089 build_builtin_expect_predicate (tree pred, tree expected)
7091 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7093 fn = built_in_decls[BUILT_IN_EXPECT];
7094 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7095 ret_type = TREE_TYPE (TREE_TYPE (fn));
7096 pred_type = TREE_VALUE (arg_types);
7097 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7099 pred = fold_convert (pred_type, pred);
7100 expected = fold_convert (expected_type, expected);
7101 call_expr = build_call_expr (fn, 2, pred, expected);
7103 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7104 build_int_cst (ret_type, 0));
7107 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7108 NULL_TREE if no simplification is possible. */
7110 static tree
7111 fold_builtin_expect (tree arg0, tree arg1)
7113 tree inner, fndecl;
7114 enum tree_code code;
7116 /* If this is a builtin_expect within a builtin_expect keep the
7117 inner one. See through a comparison against a constant. It
7118 might have been added to create a thruthvalue. */
7119 inner = arg0;
7120 if (COMPARISON_CLASS_P (inner)
7121 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7122 inner = TREE_OPERAND (inner, 0);
7124 if (TREE_CODE (inner) == CALL_EXPR
7125 && (fndecl = get_callee_fndecl (inner))
7126 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7127 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7128 return arg0;
7130 /* Distribute the expected value over short-circuiting operators.
7131 See through the cast from truthvalue_type_node to long. */
7132 inner = arg0;
7133 while (TREE_CODE (inner) == NOP_EXPR
7134 && INTEGRAL_TYPE_P (TREE_TYPE (inner))
7135 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0))))
7136 inner = TREE_OPERAND (inner, 0);
7138 code = TREE_CODE (inner);
7139 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7141 tree op0 = TREE_OPERAND (inner, 0);
7142 tree op1 = TREE_OPERAND (inner, 1);
7144 op0 = build_builtin_expect_predicate (op0, arg1);
7145 op1 = build_builtin_expect_predicate (op1, arg1);
7146 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7148 return fold_convert (TREE_TYPE (arg0), inner);
7151 /* If the argument isn't invariant then there's nothing else we can do. */
7152 if (!TREE_INVARIANT (arg0))
7153 return NULL_TREE;
7155 /* If we expect that a comparison against the argument will fold to
7156 a constant return the constant. In practice, this means a true
7157 constant or the address of a non-weak symbol. */
7158 inner = arg0;
7159 STRIP_NOPS (inner);
7160 if (TREE_CODE (inner) == ADDR_EXPR)
7164 inner = TREE_OPERAND (inner, 0);
7166 while (TREE_CODE (inner) == COMPONENT_REF
7167 || TREE_CODE (inner) == ARRAY_REF);
7168 if (DECL_P (inner) && DECL_WEAK (inner))
7169 return NULL_TREE;
7172 /* Otherwise, ARG0 already has the proper type for the return value. */
7173 return arg0;
7176 /* Fold a call to __builtin_classify_type with argument ARG. */
7178 static tree
7179 fold_builtin_classify_type (tree arg)
7181 if (arg == 0)
7182 return build_int_cst (NULL_TREE, no_type_class);
7184 return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg)));
7187 /* Fold a call to __builtin_strlen with argument ARG. */
7189 static tree
7190 fold_builtin_strlen (tree arg)
7192 if (!validate_arg (arg, POINTER_TYPE))
7193 return NULL_TREE;
7194 else
7196 tree len = c_strlen (arg, 0);
7198 if (len)
7200 /* Convert from the internal "sizetype" type to "size_t". */
7201 if (size_type_node)
7202 len = fold_convert (size_type_node, len);
7203 return len;
7206 return NULL_TREE;
7210 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7212 static tree
7213 fold_builtin_inf (tree type, int warn)
7215 REAL_VALUE_TYPE real;
7217 /* __builtin_inff is intended to be usable to define INFINITY on all
7218 targets. If an infinity is not available, INFINITY expands "to a
7219 positive constant of type float that overflows at translation
7220 time", footnote "In this case, using INFINITY will violate the
7221 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7222 Thus we pedwarn to ensure this constraint violation is
7223 diagnosed. */
7224 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7225 pedwarn ("target format does not support infinity");
7227 real_inf (&real);
7228 return build_real (type, real);
7231 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7233 static tree
7234 fold_builtin_nan (tree arg, tree type, int quiet)
7236 REAL_VALUE_TYPE real;
7237 const char *str;
7239 if (!validate_arg (arg, POINTER_TYPE))
7240 return NULL_TREE;
7241 str = c_getstr (arg);
7242 if (!str)
7243 return NULL_TREE;
7245 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7246 return NULL_TREE;
7248 return build_real (type, real);
7251 /* Return true if the floating point expression T has an integer value.
7252 We also allow +Inf, -Inf and NaN to be considered integer values. */
7254 static bool
7255 integer_valued_real_p (tree t)
7257 switch (TREE_CODE (t))
7259 case FLOAT_EXPR:
7260 return true;
7262 case ABS_EXPR:
7263 case SAVE_EXPR:
7264 case NON_LVALUE_EXPR:
7265 return integer_valued_real_p (TREE_OPERAND (t, 0));
7267 case COMPOUND_EXPR:
7268 case MODIFY_EXPR:
7269 case BIND_EXPR:
7270 return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1));
7272 case PLUS_EXPR:
7273 case MINUS_EXPR:
7274 case MULT_EXPR:
7275 case MIN_EXPR:
7276 case MAX_EXPR:
7277 return integer_valued_real_p (TREE_OPERAND (t, 0))
7278 && integer_valued_real_p (TREE_OPERAND (t, 1));
7280 case COND_EXPR:
7281 return integer_valued_real_p (TREE_OPERAND (t, 1))
7282 && integer_valued_real_p (TREE_OPERAND (t, 2));
7284 case REAL_CST:
7285 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7287 case NOP_EXPR:
7289 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7290 if (TREE_CODE (type) == INTEGER_TYPE)
7291 return true;
7292 if (TREE_CODE (type) == REAL_TYPE)
7293 return integer_valued_real_p (TREE_OPERAND (t, 0));
7294 break;
7297 case CALL_EXPR:
7298 switch (builtin_mathfn_code (t))
7300 CASE_FLT_FN (BUILT_IN_CEIL):
7301 CASE_FLT_FN (BUILT_IN_FLOOR):
7302 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7303 CASE_FLT_FN (BUILT_IN_RINT):
7304 CASE_FLT_FN (BUILT_IN_ROUND):
7305 CASE_FLT_FN (BUILT_IN_TRUNC):
7306 return true;
7308 CASE_FLT_FN (BUILT_IN_FMIN):
7309 CASE_FLT_FN (BUILT_IN_FMAX):
7310 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7311 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7313 default:
7314 break;
7316 break;
7318 default:
7319 break;
7321 return false;
7324 /* FNDECL is assumed to be a builtin where truncation can be propagated
7325 across (for instance floor((double)f) == (double)floorf (f).
7326 Do the transformation for a call with argument ARG. */
7328 static tree
7329 fold_trunc_transparent_mathfn (tree fndecl, tree arg)
7331 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7333 if (!validate_arg (arg, REAL_TYPE))
7334 return NULL_TREE;
7336 /* Integer rounding functions are idempotent. */
7337 if (fcode == builtin_mathfn_code (arg))
7338 return arg;
7340 /* If argument is already integer valued, and we don't need to worry
7341 about setting errno, there's no need to perform rounding. */
7342 if (! flag_errno_math && integer_valued_real_p (arg))
7343 return arg;
7345 if (optimize)
7347 tree arg0 = strip_float_extensions (arg);
7348 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7349 tree newtype = TREE_TYPE (arg0);
7350 tree decl;
7352 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7353 && (decl = mathfn_built_in (newtype, fcode)))
7354 return fold_convert (ftype,
7355 build_call_expr (decl, 1,
7356 fold_convert (newtype, arg0)));
7358 return NULL_TREE;
7361 /* FNDECL is assumed to be builtin which can narrow the FP type of
7362 the argument, for instance lround((double)f) -> lroundf (f).
7363 Do the transformation for a call with argument ARG. */
7365 static tree
7366 fold_fixed_mathfn (tree fndecl, tree arg)
7368 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7370 if (!validate_arg (arg, REAL_TYPE))
7371 return NULL_TREE;
7373 /* If argument is already integer valued, and we don't need to worry
7374 about setting errno, there's no need to perform rounding. */
7375 if (! flag_errno_math && integer_valued_real_p (arg))
7376 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
7378 if (optimize)
7380 tree ftype = TREE_TYPE (arg);
7381 tree arg0 = strip_float_extensions (arg);
7382 tree newtype = TREE_TYPE (arg0);
7383 tree decl;
7385 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7386 && (decl = mathfn_built_in (newtype, fcode)))
7387 return build_call_expr (decl, 1, fold_convert (newtype, arg0));
7390 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7391 sizeof (long long) == sizeof (long). */
7392 if (TYPE_PRECISION (long_long_integer_type_node)
7393 == TYPE_PRECISION (long_integer_type_node))
7395 tree newfn = NULL_TREE;
7396 switch (fcode)
7398 CASE_FLT_FN (BUILT_IN_LLCEIL):
7399 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7400 break;
7402 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7403 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7404 break;
7406 CASE_FLT_FN (BUILT_IN_LLROUND):
7407 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7408 break;
7410 CASE_FLT_FN (BUILT_IN_LLRINT):
7411 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7412 break;
7414 default:
7415 break;
7418 if (newfn)
7420 tree newcall = build_call_expr(newfn, 1, arg);
7421 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7425 return NULL_TREE;
7428 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7429 return type. Return NULL_TREE if no simplification can be made. */
7431 static tree
7432 fold_builtin_cabs (tree arg, tree type, tree fndecl)
7434 tree res;
7436 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7437 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7438 return NULL_TREE;
7440 /* Calculate the result when the argument is a constant. */
7441 if (TREE_CODE (arg) == COMPLEX_CST
7442 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7443 type, mpfr_hypot)))
7444 return res;
7446 if (TREE_CODE (arg) == COMPLEX_EXPR)
7448 tree real = TREE_OPERAND (arg, 0);
7449 tree imag = TREE_OPERAND (arg, 1);
7451 /* If either part is zero, cabs is fabs of the other. */
7452 if (real_zerop (real))
7453 return fold_build1 (ABS_EXPR, type, imag);
7454 if (real_zerop (imag))
7455 return fold_build1 (ABS_EXPR, type, real);
7457 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7458 if (flag_unsafe_math_optimizations
7459 && operand_equal_p (real, imag, OEP_PURE_SAME))
7461 const REAL_VALUE_TYPE sqrt2_trunc
7462 = real_value_truncate (TYPE_MODE (type), dconstsqrt2);
7463 STRIP_NOPS (real);
7464 return fold_build2 (MULT_EXPR, type,
7465 fold_build1 (ABS_EXPR, type, real),
7466 build_real (type, sqrt2_trunc));
7470 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7471 if (TREE_CODE (arg) == NEGATE_EXPR
7472 || TREE_CODE (arg) == CONJ_EXPR)
7473 return build_call_expr (fndecl, 1, TREE_OPERAND (arg, 0));
7475 /* Don't do this when optimizing for size. */
7476 if (flag_unsafe_math_optimizations
7477 && optimize && !optimize_size)
7479 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7481 if (sqrtfn != NULL_TREE)
7483 tree rpart, ipart, result;
7485 arg = builtin_save_expr (arg);
7487 rpart = fold_build1 (REALPART_EXPR, type, arg);
7488 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7490 rpart = builtin_save_expr (rpart);
7491 ipart = builtin_save_expr (ipart);
7493 result = fold_build2 (PLUS_EXPR, type,
7494 fold_build2 (MULT_EXPR, type,
7495 rpart, rpart),
7496 fold_build2 (MULT_EXPR, type,
7497 ipart, ipart));
7499 return build_call_expr (sqrtfn, 1, result);
7503 return NULL_TREE;
7506 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7507 Return NULL_TREE if no simplification can be made. */
7509 static tree
7510 fold_builtin_sqrt (tree arg, tree type)
7513 enum built_in_function fcode;
7514 tree res;
7516 if (!validate_arg (arg, REAL_TYPE))
7517 return NULL_TREE;
7519 /* Calculate the result when the argument is a constant. */
7520 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7521 return res;
7523 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7524 fcode = builtin_mathfn_code (arg);
7525 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7527 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7528 arg = fold_build2 (MULT_EXPR, type,
7529 CALL_EXPR_ARG (arg, 0),
7530 build_real (type, dconsthalf));
7531 return build_call_expr (expfn, 1, arg);
7534 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7535 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7537 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7539 if (powfn)
7541 tree arg0 = CALL_EXPR_ARG (arg, 0);
7542 tree tree_root;
7543 /* The inner root was either sqrt or cbrt. */
7544 REAL_VALUE_TYPE dconstroot =
7545 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7547 /* Adjust for the outer root. */
7548 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7549 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7550 tree_root = build_real (type, dconstroot);
7551 return build_call_expr (powfn, 2, arg0, tree_root);
7555 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7556 if (flag_unsafe_math_optimizations
7557 && (fcode == BUILT_IN_POW
7558 || fcode == BUILT_IN_POWF
7559 || fcode == BUILT_IN_POWL))
7561 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7562 tree arg0 = CALL_EXPR_ARG (arg, 0);
7563 tree arg1 = CALL_EXPR_ARG (arg, 1);
7564 tree narg1;
7565 if (!tree_expr_nonnegative_p (arg0))
7566 arg0 = build1 (ABS_EXPR, type, arg0);
7567 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7568 build_real (type, dconsthalf));
7569 return build_call_expr (powfn, 2, arg0, narg1);
7572 return NULL_TREE;
7575 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7576 Return NULL_TREE if no simplification can be made. */
7578 static tree
7579 fold_builtin_cbrt (tree arg, tree type)
7581 const enum built_in_function fcode = builtin_mathfn_code (arg);
7582 tree res;
7584 if (!validate_arg (arg, REAL_TYPE))
7585 return NULL_TREE;
7587 /* Calculate the result when the argument is a constant. */
7588 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7589 return res;
7591 if (flag_unsafe_math_optimizations)
7593 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7594 if (BUILTIN_EXPONENT_P (fcode))
7596 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7597 const REAL_VALUE_TYPE third_trunc =
7598 real_value_truncate (TYPE_MODE (type), dconstthird);
7599 arg = fold_build2 (MULT_EXPR, type,
7600 CALL_EXPR_ARG (arg, 0),
7601 build_real (type, third_trunc));
7602 return build_call_expr (expfn, 1, arg);
7605 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7606 if (BUILTIN_SQRT_P (fcode))
7608 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7610 if (powfn)
7612 tree arg0 = CALL_EXPR_ARG (arg, 0);
7613 tree tree_root;
7614 REAL_VALUE_TYPE dconstroot = dconstthird;
7616 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7617 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7618 tree_root = build_real (type, dconstroot);
7619 return build_call_expr (powfn, 2, arg0, tree_root);
7623 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7624 if (BUILTIN_CBRT_P (fcode))
7626 tree arg0 = CALL_EXPR_ARG (arg, 0);
7627 if (tree_expr_nonnegative_p (arg0))
7629 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7631 if (powfn)
7633 tree tree_root;
7634 REAL_VALUE_TYPE dconstroot;
7636 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7637 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7638 tree_root = build_real (type, dconstroot);
7639 return build_call_expr (powfn, 2, arg0, tree_root);
7644 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7645 if (fcode == BUILT_IN_POW
7646 || fcode == BUILT_IN_POWF
7647 || fcode == BUILT_IN_POWL)
7649 tree arg00 = CALL_EXPR_ARG (arg, 0);
7650 tree arg01 = CALL_EXPR_ARG (arg, 1);
7651 if (tree_expr_nonnegative_p (arg00))
7653 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7654 const REAL_VALUE_TYPE dconstroot
7655 = real_value_truncate (TYPE_MODE (type), dconstthird);
7656 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7657 build_real (type, dconstroot));
7658 return build_call_expr (powfn, 2, arg00, narg01);
7662 return NULL_TREE;
7665 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7666 TYPE is the type of the return value. Return NULL_TREE if no
7667 simplification can be made. */
7669 static tree
7670 fold_builtin_cos (tree arg, tree type, tree fndecl)
7672 tree res, narg;
7674 if (!validate_arg (arg, REAL_TYPE))
7675 return NULL_TREE;
7677 /* Calculate the result when the argument is a constant. */
7678 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7679 return res;
7681 /* Optimize cos(-x) into cos (x). */
7682 if ((narg = fold_strip_sign_ops (arg)))
7683 return build_call_expr (fndecl, 1, narg);
7685 return NULL_TREE;
7688 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7689 Return NULL_TREE if no simplification can be made. */
7691 static tree
7692 fold_builtin_cosh (tree arg, tree type, tree fndecl)
7694 if (validate_arg (arg, REAL_TYPE))
7696 tree res, narg;
7698 /* Calculate the result when the argument is a constant. */
7699 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7700 return res;
7702 /* Optimize cosh(-x) into cosh (x). */
7703 if ((narg = fold_strip_sign_ops (arg)))
7704 return build_call_expr (fndecl, 1, narg);
7707 return NULL_TREE;
7710 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7711 Return NULL_TREE if no simplification can be made. */
7713 static tree
7714 fold_builtin_tan (tree arg, tree type)
7716 enum built_in_function fcode;
7717 tree res;
7719 if (!validate_arg (arg, REAL_TYPE))
7720 return NULL_TREE;
7722 /* Calculate the result when the argument is a constant. */
7723 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7724 return res;
7726 /* Optimize tan(atan(x)) = x. */
7727 fcode = builtin_mathfn_code (arg);
7728 if (flag_unsafe_math_optimizations
7729 && (fcode == BUILT_IN_ATAN
7730 || fcode == BUILT_IN_ATANF
7731 || fcode == BUILT_IN_ATANL))
7732 return CALL_EXPR_ARG (arg, 0);
7734 return NULL_TREE;
7737 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7738 NULL_TREE if no simplification can be made. */
7740 static tree
7741 fold_builtin_sincos (tree arg0, tree arg1, tree arg2)
7743 tree type;
7744 tree res, fn, call;
7746 if (!validate_arg (arg0, REAL_TYPE)
7747 || !validate_arg (arg1, POINTER_TYPE)
7748 || !validate_arg (arg2, POINTER_TYPE))
7749 return NULL_TREE;
7751 type = TREE_TYPE (arg0);
7753 /* Calculate the result when the argument is a constant. */
7754 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7755 return res;
7757 /* Canonicalize sincos to cexpi. */
7758 if (!TARGET_C99_FUNCTIONS)
7759 return NULL_TREE;
7760 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7761 if (!fn)
7762 return NULL_TREE;
7764 call = build_call_expr (fn, 1, arg0);
7765 call = builtin_save_expr (call);
7767 return build2 (COMPOUND_EXPR, type,
7768 build2 (MODIFY_EXPR, void_type_node,
7769 build_fold_indirect_ref (arg1),
7770 build1 (IMAGPART_EXPR, type, call)),
7771 build2 (MODIFY_EXPR, void_type_node,
7772 build_fold_indirect_ref (arg2),
7773 build1 (REALPART_EXPR, type, call)));
7776 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7777 NULL_TREE if no simplification can be made. */
7779 static tree
7780 fold_builtin_cexp (tree arg0, tree type)
7782 tree rtype;
7783 tree realp, imagp, ifn;
7785 if (!validate_arg (arg0, COMPLEX_TYPE))
7786 return NULL_TREE;
7788 rtype = TREE_TYPE (TREE_TYPE (arg0));
7790 /* In case we can figure out the real part of arg0 and it is constant zero
7791 fold to cexpi. */
7792 if (!TARGET_C99_FUNCTIONS)
7793 return NULL_TREE;
7794 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7795 if (!ifn)
7796 return NULL_TREE;
7798 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
7799 && real_zerop (realp))
7801 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
7802 return build_call_expr (ifn, 1, narg);
7805 /* In case we can easily decompose real and imaginary parts split cexp
7806 to exp (r) * cexpi (i). */
7807 if (flag_unsafe_math_optimizations
7808 && realp)
7810 tree rfn, rcall, icall;
7812 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7813 if (!rfn)
7814 return NULL_TREE;
7816 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
7817 if (!imagp)
7818 return NULL_TREE;
7820 icall = build_call_expr (ifn, 1, imagp);
7821 icall = builtin_save_expr (icall);
7822 rcall = build_call_expr (rfn, 1, realp);
7823 rcall = builtin_save_expr (rcall);
7824 return fold_build2 (COMPLEX_EXPR, type,
7825 fold_build2 (MULT_EXPR, rtype,
7826 rcall,
7827 fold_build1 (REALPART_EXPR, rtype, icall)),
7828 fold_build2 (MULT_EXPR, rtype,
7829 rcall,
7830 fold_build1 (IMAGPART_EXPR, rtype, icall)));
7833 return NULL_TREE;
7836 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7837 Return NULL_TREE if no simplification can be made. */
7839 static tree
7840 fold_builtin_trunc (tree fndecl, tree arg)
7842 if (!validate_arg (arg, REAL_TYPE))
7843 return NULL_TREE;
7845 /* Optimize trunc of constant value. */
7846 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7848 REAL_VALUE_TYPE r, x;
7849 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7851 x = TREE_REAL_CST (arg);
7852 real_trunc (&r, TYPE_MODE (type), &x);
7853 return build_real (type, r);
7856 return fold_trunc_transparent_mathfn (fndecl, arg);
7859 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7860 Return NULL_TREE if no simplification can be made. */
7862 static tree
7863 fold_builtin_floor (tree fndecl, tree arg)
7865 if (!validate_arg (arg, REAL_TYPE))
7866 return NULL_TREE;
7868 /* Optimize floor of constant value. */
7869 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7871 REAL_VALUE_TYPE x;
7873 x = TREE_REAL_CST (arg);
7874 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7876 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7877 REAL_VALUE_TYPE r;
7879 real_floor (&r, TYPE_MODE (type), &x);
7880 return build_real (type, r);
7884 /* Fold floor (x) where x is nonnegative to trunc (x). */
7885 if (tree_expr_nonnegative_p (arg))
7887 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7888 if (truncfn)
7889 return build_call_expr (truncfn, 1, arg);
7892 return fold_trunc_transparent_mathfn (fndecl, arg);
7895 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7896 Return NULL_TREE if no simplification can be made. */
7898 static tree
7899 fold_builtin_ceil (tree fndecl, tree arg)
7901 if (!validate_arg (arg, REAL_TYPE))
7902 return NULL_TREE;
7904 /* Optimize ceil of constant value. */
7905 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7907 REAL_VALUE_TYPE x;
7909 x = TREE_REAL_CST (arg);
7910 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7912 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7913 REAL_VALUE_TYPE r;
7915 real_ceil (&r, TYPE_MODE (type), &x);
7916 return build_real (type, r);
7920 return fold_trunc_transparent_mathfn (fndecl, arg);
7923 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7924 Return NULL_TREE if no simplification can be made. */
7926 static tree
7927 fold_builtin_round (tree fndecl, tree arg)
7929 if (!validate_arg (arg, REAL_TYPE))
7930 return NULL_TREE;
7932 /* Optimize round of constant value. */
7933 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7935 REAL_VALUE_TYPE x;
7937 x = TREE_REAL_CST (arg);
7938 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7940 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7941 REAL_VALUE_TYPE r;
7943 real_round (&r, TYPE_MODE (type), &x);
7944 return build_real (type, r);
7948 return fold_trunc_transparent_mathfn (fndecl, arg);
7951 /* Fold function call to builtin lround, lroundf or lroundl (or the
7952 corresponding long long versions) and other rounding functions. ARG
7953 is the argument to the call. Return NULL_TREE if no simplification
7954 can be made. */
7956 static tree
7957 fold_builtin_int_roundingfn (tree fndecl, tree arg)
7959 if (!validate_arg (arg, REAL_TYPE))
7960 return NULL_TREE;
7962 /* Optimize lround of constant value. */
7963 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7965 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7967 if (real_isfinite (&x))
7969 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7970 tree ftype = TREE_TYPE (arg);
7971 unsigned HOST_WIDE_INT lo2;
7972 HOST_WIDE_INT hi, lo;
7973 REAL_VALUE_TYPE r;
7975 switch (DECL_FUNCTION_CODE (fndecl))
7977 CASE_FLT_FN (BUILT_IN_LFLOOR):
7978 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7979 real_floor (&r, TYPE_MODE (ftype), &x);
7980 break;
7982 CASE_FLT_FN (BUILT_IN_LCEIL):
7983 CASE_FLT_FN (BUILT_IN_LLCEIL):
7984 real_ceil (&r, TYPE_MODE (ftype), &x);
7985 break;
7987 CASE_FLT_FN (BUILT_IN_LROUND):
7988 CASE_FLT_FN (BUILT_IN_LLROUND):
7989 real_round (&r, TYPE_MODE (ftype), &x);
7990 break;
7992 default:
7993 gcc_unreachable ();
7996 REAL_VALUE_TO_INT (&lo, &hi, r);
7997 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
7998 return build_int_cst_wide (itype, lo2, hi);
8002 switch (DECL_FUNCTION_CODE (fndecl))
8004 CASE_FLT_FN (BUILT_IN_LFLOOR):
8005 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8006 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8007 if (tree_expr_nonnegative_p (arg))
8008 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
8009 arg);
8010 break;
8011 default:;
8014 return fold_fixed_mathfn (fndecl, arg);
8017 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8018 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8019 the argument to the call. Return NULL_TREE if no simplification can
8020 be made. */
8022 static tree
8023 fold_builtin_bitop (tree fndecl, tree arg)
8025 if (!validate_arg (arg, INTEGER_TYPE))
8026 return NULL_TREE;
8028 /* Optimize for constant argument. */
8029 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8031 HOST_WIDE_INT hi, width, result;
8032 unsigned HOST_WIDE_INT lo;
8033 tree type;
8035 type = TREE_TYPE (arg);
8036 width = TYPE_PRECISION (type);
8037 lo = TREE_INT_CST_LOW (arg);
8039 /* Clear all the bits that are beyond the type's precision. */
8040 if (width > HOST_BITS_PER_WIDE_INT)
8042 hi = TREE_INT_CST_HIGH (arg);
8043 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8044 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
8046 else
8048 hi = 0;
8049 if (width < HOST_BITS_PER_WIDE_INT)
8050 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8053 switch (DECL_FUNCTION_CODE (fndecl))
8055 CASE_INT_FN (BUILT_IN_FFS):
8056 if (lo != 0)
8057 result = exact_log2 (lo & -lo) + 1;
8058 else if (hi != 0)
8059 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
8060 else
8061 result = 0;
8062 break;
8064 CASE_INT_FN (BUILT_IN_CLZ):
8065 if (hi != 0)
8066 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8067 else if (lo != 0)
8068 result = width - floor_log2 (lo) - 1;
8069 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8070 result = width;
8071 break;
8073 CASE_INT_FN (BUILT_IN_CTZ):
8074 if (lo != 0)
8075 result = exact_log2 (lo & -lo);
8076 else if (hi != 0)
8077 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
8078 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8079 result = width;
8080 break;
8082 CASE_INT_FN (BUILT_IN_POPCOUNT):
8083 result = 0;
8084 while (lo)
8085 result++, lo &= lo - 1;
8086 while (hi)
8087 result++, hi &= hi - 1;
8088 break;
8090 CASE_INT_FN (BUILT_IN_PARITY):
8091 result = 0;
8092 while (lo)
8093 result++, lo &= lo - 1;
8094 while (hi)
8095 result++, hi &= hi - 1;
8096 result &= 1;
8097 break;
8099 default:
8100 gcc_unreachable ();
8103 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8106 return NULL_TREE;
8109 /* Fold function call to builtin_bswap and the long and long long
8110 variants. Return NULL_TREE if no simplification can be made. */
8111 static tree
8112 fold_builtin_bswap (tree fndecl, tree arg)
8114 if (! validate_arg (arg, INTEGER_TYPE))
8115 return NULL_TREE;
8117 /* Optimize constant value. */
8118 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8120 HOST_WIDE_INT hi, width, r_hi = 0;
8121 unsigned HOST_WIDE_INT lo, r_lo = 0;
8122 tree type;
8124 type = TREE_TYPE (arg);
8125 width = TYPE_PRECISION (type);
8126 lo = TREE_INT_CST_LOW (arg);
8127 hi = TREE_INT_CST_HIGH (arg);
8129 switch (DECL_FUNCTION_CODE (fndecl))
8131 case BUILT_IN_BSWAP32:
8132 case BUILT_IN_BSWAP64:
8134 int s;
8136 for (s = 0; s < width; s += 8)
8138 int d = width - s - 8;
8139 unsigned HOST_WIDE_INT byte;
8141 if (s < HOST_BITS_PER_WIDE_INT)
8142 byte = (lo >> s) & 0xff;
8143 else
8144 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8146 if (d < HOST_BITS_PER_WIDE_INT)
8147 r_lo |= byte << d;
8148 else
8149 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8153 break;
8155 default:
8156 gcc_unreachable ();
8159 if (width < HOST_BITS_PER_WIDE_INT)
8160 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8161 else
8162 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8165 return NULL_TREE;
8168 /* Return true if EXPR is the real constant contained in VALUE. */
8170 static bool
8171 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
8173 STRIP_NOPS (expr);
8175 return ((TREE_CODE (expr) == REAL_CST
8176 && !TREE_OVERFLOW (expr)
8177 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
8178 || (TREE_CODE (expr) == COMPLEX_CST
8179 && real_dconstp (TREE_REALPART (expr), value)
8180 && real_zerop (TREE_IMAGPART (expr))));
8183 /* A subroutine of fold_builtin to fold the various logarithmic
8184 functions. Return NULL_TREE if no simplification can me made.
8185 FUNC is the corresponding MPFR logarithm function. */
8187 static tree
8188 fold_builtin_logarithm (tree fndecl, tree arg,
8189 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8191 if (validate_arg (arg, REAL_TYPE))
8193 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8194 tree res;
8195 const enum built_in_function fcode = builtin_mathfn_code (arg);
8197 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
8198 instead we'll look for 'e' truncated to MODE. So only do
8199 this if flag_unsafe_math_optimizations is set. */
8200 if (flag_unsafe_math_optimizations && func == mpfr_log)
8202 const REAL_VALUE_TYPE e_truncated =
8203 real_value_truncate (TYPE_MODE (type), dconste);
8204 if (real_dconstp (arg, &e_truncated))
8205 return build_real (type, dconst1);
8208 /* Calculate the result when the argument is a constant. */
8209 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8210 return res;
8212 /* Special case, optimize logN(expN(x)) = x. */
8213 if (flag_unsafe_math_optimizations
8214 && ((func == mpfr_log
8215 && (fcode == BUILT_IN_EXP
8216 || fcode == BUILT_IN_EXPF
8217 || fcode == BUILT_IN_EXPL))
8218 || (func == mpfr_log2
8219 && (fcode == BUILT_IN_EXP2
8220 || fcode == BUILT_IN_EXP2F
8221 || fcode == BUILT_IN_EXP2L))
8222 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8223 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8225 /* Optimize logN(func()) for various exponential functions. We
8226 want to determine the value "x" and the power "exponent" in
8227 order to transform logN(x**exponent) into exponent*logN(x). */
8228 if (flag_unsafe_math_optimizations)
8230 tree exponent = 0, x = 0;
8232 switch (fcode)
8234 CASE_FLT_FN (BUILT_IN_EXP):
8235 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8236 x = build_real (type,
8237 real_value_truncate (TYPE_MODE (type), dconste));
8238 exponent = CALL_EXPR_ARG (arg, 0);
8239 break;
8240 CASE_FLT_FN (BUILT_IN_EXP2):
8241 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8242 x = build_real (type, dconst2);
8243 exponent = CALL_EXPR_ARG (arg, 0);
8244 break;
8245 CASE_FLT_FN (BUILT_IN_EXP10):
8246 CASE_FLT_FN (BUILT_IN_POW10):
8247 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8248 x = build_real (type, dconst10);
8249 exponent = CALL_EXPR_ARG (arg, 0);
8250 break;
8251 CASE_FLT_FN (BUILT_IN_SQRT):
8252 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8253 x = CALL_EXPR_ARG (arg, 0);
8254 exponent = build_real (type, dconsthalf);
8255 break;
8256 CASE_FLT_FN (BUILT_IN_CBRT):
8257 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8258 x = CALL_EXPR_ARG (arg, 0);
8259 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8260 dconstthird));
8261 break;
8262 CASE_FLT_FN (BUILT_IN_POW):
8263 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8264 x = CALL_EXPR_ARG (arg, 0);
8265 exponent = CALL_EXPR_ARG (arg, 1);
8266 break;
8267 default:
8268 break;
8271 /* Now perform the optimization. */
8272 if (x && exponent)
8274 tree logfn = build_call_expr (fndecl, 1, x);
8275 return fold_build2 (MULT_EXPR, type, exponent, logfn);
8280 return NULL_TREE;
8283 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8284 NULL_TREE if no simplification can be made. */
8286 static tree
8287 fold_builtin_hypot (tree fndecl, tree arg0, tree arg1, tree type)
8289 tree res, narg0, narg1;
8291 if (!validate_arg (arg0, REAL_TYPE)
8292 || !validate_arg (arg1, REAL_TYPE))
8293 return NULL_TREE;
8295 /* Calculate the result when the argument is a constant. */
8296 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8297 return res;
8299 /* If either argument to hypot has a negate or abs, strip that off.
8300 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8301 narg0 = fold_strip_sign_ops (arg0);
8302 narg1 = fold_strip_sign_ops (arg1);
8303 if (narg0 || narg1)
8305 return build_call_expr (fndecl, 2, narg0 ? narg0 : arg0,
8306 narg1 ? narg1 : arg1);
8309 /* If either argument is zero, hypot is fabs of the other. */
8310 if (real_zerop (arg0))
8311 return fold_build1 (ABS_EXPR, type, arg1);
8312 else if (real_zerop (arg1))
8313 return fold_build1 (ABS_EXPR, type, arg0);
8315 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8316 if (flag_unsafe_math_optimizations
8317 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8319 const REAL_VALUE_TYPE sqrt2_trunc
8320 = real_value_truncate (TYPE_MODE (type), dconstsqrt2);
8321 return fold_build2 (MULT_EXPR, type,
8322 fold_build1 (ABS_EXPR, type, arg0),
8323 build_real (type, sqrt2_trunc));
8326 return NULL_TREE;
8330 /* Fold a builtin function call to pow, powf, or powl. Return
8331 NULL_TREE if no simplification can be made. */
8332 static tree
8333 fold_builtin_pow (tree fndecl, tree arg0, tree arg1, tree type)
8335 tree res;
8337 if (!validate_arg (arg0, REAL_TYPE)
8338 || !validate_arg (arg1, REAL_TYPE))
8339 return NULL_TREE;
8341 /* Calculate the result when the argument is a constant. */
8342 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8343 return res;
8345 /* Optimize pow(1.0,y) = 1.0. */
8346 if (real_onep (arg0))
8347 return omit_one_operand (type, build_real (type, dconst1), arg1);
8349 if (TREE_CODE (arg1) == REAL_CST
8350 && !TREE_OVERFLOW (arg1))
8352 REAL_VALUE_TYPE cint;
8353 REAL_VALUE_TYPE c;
8354 HOST_WIDE_INT n;
8356 c = TREE_REAL_CST (arg1);
8358 /* Optimize pow(x,0.0) = 1.0. */
8359 if (REAL_VALUES_EQUAL (c, dconst0))
8360 return omit_one_operand (type, build_real (type, dconst1),
8361 arg0);
8363 /* Optimize pow(x,1.0) = x. */
8364 if (REAL_VALUES_EQUAL (c, dconst1))
8365 return arg0;
8367 /* Optimize pow(x,-1.0) = 1.0/x. */
8368 if (REAL_VALUES_EQUAL (c, dconstm1))
8369 return fold_build2 (RDIV_EXPR, type,
8370 build_real (type, dconst1), arg0);
8372 /* Optimize pow(x,0.5) = sqrt(x). */
8373 if (flag_unsafe_math_optimizations
8374 && REAL_VALUES_EQUAL (c, dconsthalf))
8376 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8378 if (sqrtfn != NULL_TREE)
8379 return build_call_expr (sqrtfn, 1, arg0);
8382 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8383 if (flag_unsafe_math_optimizations)
8385 const REAL_VALUE_TYPE dconstroot
8386 = real_value_truncate (TYPE_MODE (type), dconstthird);
8388 if (REAL_VALUES_EQUAL (c, dconstroot))
8390 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8391 if (cbrtfn != NULL_TREE)
8392 return build_call_expr (cbrtfn, 1, arg0);
8396 /* Check for an integer exponent. */
8397 n = real_to_integer (&c);
8398 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8399 if (real_identical (&c, &cint))
8401 /* Attempt to evaluate pow at compile-time. */
8402 if (TREE_CODE (arg0) == REAL_CST
8403 && !TREE_OVERFLOW (arg0))
8405 REAL_VALUE_TYPE x;
8406 bool inexact;
8408 x = TREE_REAL_CST (arg0);
8409 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8410 if (flag_unsafe_math_optimizations || !inexact)
8411 return build_real (type, x);
8414 /* Strip sign ops from even integer powers. */
8415 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8417 tree narg0 = fold_strip_sign_ops (arg0);
8418 if (narg0)
8419 return build_call_expr (fndecl, 2, narg0, arg1);
8424 if (flag_unsafe_math_optimizations)
8426 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8428 /* Optimize pow(expN(x),y) = expN(x*y). */
8429 if (BUILTIN_EXPONENT_P (fcode))
8431 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8432 tree arg = CALL_EXPR_ARG (arg0, 0);
8433 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8434 return build_call_expr (expfn, 1, arg);
8437 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8438 if (BUILTIN_SQRT_P (fcode))
8440 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8441 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8442 build_real (type, dconsthalf));
8443 return build_call_expr (fndecl, 2, narg0, narg1);
8446 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8447 if (BUILTIN_CBRT_P (fcode))
8449 tree arg = CALL_EXPR_ARG (arg0, 0);
8450 if (tree_expr_nonnegative_p (arg))
8452 const REAL_VALUE_TYPE dconstroot
8453 = real_value_truncate (TYPE_MODE (type), dconstthird);
8454 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8455 build_real (type, dconstroot));
8456 return build_call_expr (fndecl, 2, arg, narg1);
8460 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8461 if (fcode == BUILT_IN_POW
8462 || fcode == BUILT_IN_POWF
8463 || fcode == BUILT_IN_POWL)
8465 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8466 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8467 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8468 return build_call_expr (fndecl, 2, arg00, narg1);
8472 return NULL_TREE;
8475 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8476 Return NULL_TREE if no simplification can be made. */
8477 static tree
8478 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED,
8479 tree arg0, tree arg1, tree type)
8481 if (!validate_arg (arg0, REAL_TYPE)
8482 || !validate_arg (arg1, INTEGER_TYPE))
8483 return NULL_TREE;
8485 /* Optimize pow(1.0,y) = 1.0. */
8486 if (real_onep (arg0))
8487 return omit_one_operand (type, build_real (type, dconst1), arg1);
8489 if (host_integerp (arg1, 0))
8491 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8493 /* Evaluate powi at compile-time. */
8494 if (TREE_CODE (arg0) == REAL_CST
8495 && !TREE_OVERFLOW (arg0))
8497 REAL_VALUE_TYPE x;
8498 x = TREE_REAL_CST (arg0);
8499 real_powi (&x, TYPE_MODE (type), &x, c);
8500 return build_real (type, x);
8503 /* Optimize pow(x,0) = 1.0. */
8504 if (c == 0)
8505 return omit_one_operand (type, build_real (type, dconst1),
8506 arg0);
8508 /* Optimize pow(x,1) = x. */
8509 if (c == 1)
8510 return arg0;
8512 /* Optimize pow(x,-1) = 1.0/x. */
8513 if (c == -1)
8514 return fold_build2 (RDIV_EXPR, type,
8515 build_real (type, dconst1), arg0);
8518 return NULL_TREE;
8521 /* A subroutine of fold_builtin to fold the various exponent
8522 functions. Return NULL_TREE if no simplification can be made.
8523 FUNC is the corresponding MPFR exponent function. */
8525 static tree
8526 fold_builtin_exponent (tree fndecl, tree arg,
8527 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8529 if (validate_arg (arg, REAL_TYPE))
8531 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8532 tree res;
8534 /* Calculate the result when the argument is a constant. */
8535 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8536 return res;
8538 /* Optimize expN(logN(x)) = x. */
8539 if (flag_unsafe_math_optimizations)
8541 const enum built_in_function fcode = builtin_mathfn_code (arg);
8543 if ((func == mpfr_exp
8544 && (fcode == BUILT_IN_LOG
8545 || fcode == BUILT_IN_LOGF
8546 || fcode == BUILT_IN_LOGL))
8547 || (func == mpfr_exp2
8548 && (fcode == BUILT_IN_LOG2
8549 || fcode == BUILT_IN_LOG2F
8550 || fcode == BUILT_IN_LOG2L))
8551 || (func == mpfr_exp10
8552 && (fcode == BUILT_IN_LOG10
8553 || fcode == BUILT_IN_LOG10F
8554 || fcode == BUILT_IN_LOG10L)))
8555 return fold_convert (type, CALL_EXPR_ARG (arg, 0));
8559 return NULL_TREE;
8562 /* Return true if VAR is a VAR_DECL or a component thereof. */
8564 static bool
8565 var_decl_component_p (tree var)
8567 tree inner = var;
8568 while (handled_component_p (inner))
8569 inner = TREE_OPERAND (inner, 0);
8570 return SSA_VAR_P (inner);
8573 /* Fold function call to builtin memset. Return
8574 NULL_TREE if no simplification can be made. */
8576 static tree
8577 fold_builtin_memset (tree dest, tree c, tree len, tree type, bool ignore)
8579 tree var, ret;
8580 unsigned HOST_WIDE_INT length, cval;
8582 if (! validate_arg (dest, POINTER_TYPE)
8583 || ! validate_arg (c, INTEGER_TYPE)
8584 || ! validate_arg (len, INTEGER_TYPE))
8585 return NULL_TREE;
8587 if (! host_integerp (len, 1))
8588 return NULL_TREE;
8590 /* If the LEN parameter is zero, return DEST. */
8591 if (integer_zerop (len))
8592 return omit_one_operand (type, dest, c);
8594 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8595 return NULL_TREE;
8597 var = dest;
8598 STRIP_NOPS (var);
8599 if (TREE_CODE (var) != ADDR_EXPR)
8600 return NULL_TREE;
8602 var = TREE_OPERAND (var, 0);
8603 if (TREE_THIS_VOLATILE (var))
8604 return NULL_TREE;
8606 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8607 && !POINTER_TYPE_P (TREE_TYPE (var)))
8608 return NULL_TREE;
8610 if (! var_decl_component_p (var))
8611 return NULL_TREE;
8613 length = tree_low_cst (len, 1);
8614 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8615 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8616 < (int) length)
8617 return NULL_TREE;
8619 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8620 return NULL_TREE;
8622 if (integer_zerop (c))
8623 cval = 0;
8624 else
8626 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8627 return NULL_TREE;
8629 cval = tree_low_cst (c, 1);
8630 cval &= 0xff;
8631 cval |= cval << 8;
8632 cval |= cval << 16;
8633 cval |= (cval << 31) << 1;
8636 ret = build_int_cst_type (TREE_TYPE (var), cval);
8637 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
8638 if (ignore)
8639 return ret;
8641 return omit_one_operand (type, dest, ret);
8644 /* Fold function call to builtin memset. Return
8645 NULL_TREE if no simplification can be made. */
8647 static tree
8648 fold_builtin_bzero (tree dest, tree size, bool ignore)
8650 if (! validate_arg (dest, POINTER_TYPE)
8651 || ! validate_arg (size, INTEGER_TYPE))
8652 return NULL_TREE;
8654 if (!ignore)
8655 return NULL_TREE;
8657 /* New argument list transforming bzero(ptr x, int y) to
8658 memset(ptr x, int 0, size_t y). This is done this way
8659 so that if it isn't expanded inline, we fallback to
8660 calling bzero instead of memset. */
8662 return fold_builtin_memset (dest, integer_zero_node,
8663 fold_convert (sizetype, size),
8664 void_type_node, ignore);
8667 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8668 NULL_TREE if no simplification can be made.
8669 If ENDP is 0, return DEST (like memcpy).
8670 If ENDP is 1, return DEST+LEN (like mempcpy).
8671 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8672 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8673 (memmove). */
8675 static tree
8676 fold_builtin_memory_op (tree dest, tree src, tree len, tree type, bool ignore, int endp)
8678 tree destvar, srcvar, expr;
8680 if (! validate_arg (dest, POINTER_TYPE)
8681 || ! validate_arg (src, POINTER_TYPE)
8682 || ! validate_arg (len, INTEGER_TYPE))
8683 return NULL_TREE;
8685 /* If the LEN parameter is zero, return DEST. */
8686 if (integer_zerop (len))
8687 return omit_one_operand (type, dest, src);
8689 /* If SRC and DEST are the same (and not volatile), return
8690 DEST{,+LEN,+LEN-1}. */
8691 if (operand_equal_p (src, dest, 0))
8692 expr = len;
8693 else
8695 tree srctype, desttype;
8696 if (endp == 3)
8698 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8699 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8701 /* Both DEST and SRC must be pointer types.
8702 ??? This is what old code did. Is the testing for pointer types
8703 really mandatory?
8705 If either SRC is readonly or length is 1, we can use memcpy. */
8706 if (dest_align && src_align
8707 && (readonly_data_expr (src)
8708 || (host_integerp (len, 1)
8709 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8710 tree_low_cst (len, 1)))))
8712 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8713 if (!fn)
8714 return NULL_TREE;
8715 return build_call_expr (fn, 3, dest, src, len);
8717 return NULL_TREE;
8720 if (!host_integerp (len, 0))
8721 return NULL_TREE;
8722 /* FIXME:
8723 This logic lose for arguments like (type *)malloc (sizeof (type)),
8724 since we strip the casts of up to VOID return value from malloc.
8725 Perhaps we ought to inherit type from non-VOID argument here? */
8726 STRIP_NOPS (src);
8727 STRIP_NOPS (dest);
8728 srctype = TREE_TYPE (TREE_TYPE (src));
8729 desttype = TREE_TYPE (TREE_TYPE (dest));
8730 if (!srctype || !desttype
8731 || !TYPE_SIZE_UNIT (srctype)
8732 || !TYPE_SIZE_UNIT (desttype)
8733 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8734 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8735 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
8736 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8737 return NULL_TREE;
8739 if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT)
8740 < (int) TYPE_ALIGN (desttype)
8741 || (get_pointer_alignment (src, BIGGEST_ALIGNMENT)
8742 < (int) TYPE_ALIGN (srctype)))
8743 return NULL_TREE;
8745 if (!ignore)
8746 dest = builtin_save_expr (dest);
8748 srcvar = build_fold_indirect_ref (src);
8749 if (TREE_THIS_VOLATILE (srcvar))
8750 return NULL_TREE;
8751 if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8752 return NULL_TREE;
8753 /* With memcpy, it is possible to bypass aliasing rules, so without
8754 this check i. e. execute/20060930-2.c would be misoptimized, because
8755 it use conflicting alias set to hold argument for the memcpy call.
8756 This check is probably unnecesary with -fno-strict-aliasing.
8757 Similarly for destvar. See also PR29286. */
8758 if (!var_decl_component_p (srcvar)
8759 /* Accept: memcpy (*char_var, "test", 1); that simplify
8760 to char_var='t'; */
8761 || is_gimple_min_invariant (srcvar)
8762 || readonly_data_expr (src))
8763 return NULL_TREE;
8765 destvar = build_fold_indirect_ref (dest);
8766 if (TREE_THIS_VOLATILE (destvar))
8767 return NULL_TREE;
8768 if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8769 return NULL_TREE;
8770 if (!var_decl_component_p (destvar))
8771 return NULL_TREE;
8773 if (srctype == desttype
8774 || (gimple_in_ssa_p (cfun)
8775 && useless_type_conversion_p (desttype, srctype)))
8776 expr = srcvar;
8777 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8778 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8779 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8780 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8781 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8782 else
8783 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8784 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8787 if (ignore)
8788 return expr;
8790 if (endp == 0 || endp == 3)
8791 return omit_one_operand (type, dest, expr);
8793 if (expr == len)
8794 expr = NULL_TREE;
8796 if (endp == 2)
8797 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8798 ssize_int (1));
8800 dest = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
8801 dest = fold_convert (type, dest);
8802 if (expr)
8803 dest = omit_one_operand (type, dest, expr);
8804 return dest;
8807 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8808 If LEN is not NULL, it represents the length of the string to be
8809 copied. Return NULL_TREE if no simplification can be made. */
8811 tree
8812 fold_builtin_strcpy (tree fndecl, tree dest, tree src, tree len)
8814 tree fn;
8816 if (!validate_arg (dest, POINTER_TYPE)
8817 || !validate_arg (src, POINTER_TYPE))
8818 return NULL_TREE;
8820 /* If SRC and DEST are the same (and not volatile), return DEST. */
8821 if (operand_equal_p (src, dest, 0))
8822 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8824 if (optimize_size)
8825 return NULL_TREE;
8827 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8828 if (!fn)
8829 return NULL_TREE;
8831 if (!len)
8833 len = c_strlen (src, 1);
8834 if (! len || TREE_SIDE_EFFECTS (len))
8835 return NULL_TREE;
8838 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8839 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8840 build_call_expr (fn, 3, dest, src, len));
8843 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8844 If SLEN is not NULL, it represents the length of the source string.
8845 Return NULL_TREE if no simplification can be made. */
8847 tree
8848 fold_builtin_strncpy (tree fndecl, tree dest, tree src, tree len, tree slen)
8850 tree fn;
8852 if (!validate_arg (dest, POINTER_TYPE)
8853 || !validate_arg (src, POINTER_TYPE)
8854 || !validate_arg (len, INTEGER_TYPE))
8855 return NULL_TREE;
8857 /* If the LEN parameter is zero, return DEST. */
8858 if (integer_zerop (len))
8859 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8861 /* We can't compare slen with len as constants below if len is not a
8862 constant. */
8863 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8864 return NULL_TREE;
8866 if (!slen)
8867 slen = c_strlen (src, 1);
8869 /* Now, we must be passed a constant src ptr parameter. */
8870 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8871 return NULL_TREE;
8873 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8875 /* We do not support simplification of this case, though we do
8876 support it when expanding trees into RTL. */
8877 /* FIXME: generate a call to __builtin_memset. */
8878 if (tree_int_cst_lt (slen, len))
8879 return NULL_TREE;
8881 /* OK transform into builtin memcpy. */
8882 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8883 if (!fn)
8884 return NULL_TREE;
8885 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8886 build_call_expr (fn, 3, dest, src, len));
8889 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8890 arguments to the call, and TYPE is its return type.
8891 Return NULL_TREE if no simplification can be made. */
8893 static tree
8894 fold_builtin_memchr (tree arg1, tree arg2, tree len, tree type)
8896 if (!validate_arg (arg1, POINTER_TYPE)
8897 || !validate_arg (arg2, INTEGER_TYPE)
8898 || !validate_arg (len, INTEGER_TYPE))
8899 return NULL_TREE;
8900 else
8902 const char *p1;
8904 if (TREE_CODE (arg2) != INTEGER_CST
8905 || !host_integerp (len, 1))
8906 return NULL_TREE;
8908 p1 = c_getstr (arg1);
8909 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8911 char c;
8912 const char *r;
8913 tree tem;
8915 if (target_char_cast (arg2, &c))
8916 return NULL_TREE;
8918 r = memchr (p1, c, tree_low_cst (len, 1));
8920 if (r == NULL)
8921 return build_int_cst (TREE_TYPE (arg1), 0);
8923 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1,
8924 size_int (r - p1));
8925 return fold_convert (type, tem);
8927 return NULL_TREE;
8931 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8932 Return NULL_TREE if no simplification can be made. */
8934 static tree
8935 fold_builtin_memcmp (tree arg1, tree arg2, tree len)
8937 const char *p1, *p2;
8939 if (!validate_arg (arg1, POINTER_TYPE)
8940 || !validate_arg (arg2, POINTER_TYPE)
8941 || !validate_arg (len, INTEGER_TYPE))
8942 return NULL_TREE;
8944 /* If the LEN parameter is zero, return zero. */
8945 if (integer_zerop (len))
8946 return omit_two_operands (integer_type_node, integer_zero_node,
8947 arg1, arg2);
8949 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8950 if (operand_equal_p (arg1, arg2, 0))
8951 return omit_one_operand (integer_type_node, integer_zero_node, len);
8953 p1 = c_getstr (arg1);
8954 p2 = c_getstr (arg2);
8956 /* If all arguments are constant, and the value of len is not greater
8957 than the lengths of arg1 and arg2, evaluate at compile-time. */
8958 if (host_integerp (len, 1) && p1 && p2
8959 && compare_tree_int (len, strlen (p1) + 1) <= 0
8960 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8962 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8964 if (r > 0)
8965 return integer_one_node;
8966 else if (r < 0)
8967 return integer_minus_one_node;
8968 else
8969 return integer_zero_node;
8972 /* If len parameter is one, return an expression corresponding to
8973 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8974 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8976 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8977 tree cst_uchar_ptr_node
8978 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8980 tree ind1 = fold_convert (integer_type_node,
8981 build1 (INDIRECT_REF, cst_uchar_node,
8982 fold_convert (cst_uchar_ptr_node,
8983 arg1)));
8984 tree ind2 = fold_convert (integer_type_node,
8985 build1 (INDIRECT_REF, cst_uchar_node,
8986 fold_convert (cst_uchar_ptr_node,
8987 arg2)));
8988 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8991 return NULL_TREE;
8994 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8995 Return NULL_TREE if no simplification can be made. */
8997 static tree
8998 fold_builtin_strcmp (tree arg1, tree arg2)
9000 const char *p1, *p2;
9002 if (!validate_arg (arg1, POINTER_TYPE)
9003 || !validate_arg (arg2, POINTER_TYPE))
9004 return NULL_TREE;
9006 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9007 if (operand_equal_p (arg1, arg2, 0))
9008 return integer_zero_node;
9010 p1 = c_getstr (arg1);
9011 p2 = c_getstr (arg2);
9013 if (p1 && p2)
9015 const int i = strcmp (p1, p2);
9016 if (i < 0)
9017 return integer_minus_one_node;
9018 else if (i > 0)
9019 return integer_one_node;
9020 else
9021 return integer_zero_node;
9024 /* If the second arg is "", return *(const unsigned char*)arg1. */
9025 if (p2 && *p2 == '\0')
9027 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9028 tree cst_uchar_ptr_node
9029 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9031 return fold_convert (integer_type_node,
9032 build1 (INDIRECT_REF, cst_uchar_node,
9033 fold_convert (cst_uchar_ptr_node,
9034 arg1)));
9037 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9038 if (p1 && *p1 == '\0')
9040 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9041 tree cst_uchar_ptr_node
9042 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9044 tree temp = fold_convert (integer_type_node,
9045 build1 (INDIRECT_REF, cst_uchar_node,
9046 fold_convert (cst_uchar_ptr_node,
9047 arg2)));
9048 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9051 return NULL_TREE;
9054 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9055 Return NULL_TREE if no simplification can be made. */
9057 static tree
9058 fold_builtin_strncmp (tree arg1, tree arg2, tree len)
9060 const char *p1, *p2;
9062 if (!validate_arg (arg1, POINTER_TYPE)
9063 || !validate_arg (arg2, POINTER_TYPE)
9064 || !validate_arg (len, INTEGER_TYPE))
9065 return NULL_TREE;
9067 /* If the LEN parameter is zero, return zero. */
9068 if (integer_zerop (len))
9069 return omit_two_operands (integer_type_node, integer_zero_node,
9070 arg1, arg2);
9072 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9073 if (operand_equal_p (arg1, arg2, 0))
9074 return omit_one_operand (integer_type_node, integer_zero_node, len);
9076 p1 = c_getstr (arg1);
9077 p2 = c_getstr (arg2);
9079 if (host_integerp (len, 1) && p1 && p2)
9081 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9082 if (i > 0)
9083 return integer_one_node;
9084 else if (i < 0)
9085 return integer_minus_one_node;
9086 else
9087 return integer_zero_node;
9090 /* If the second arg is "", and the length is greater than zero,
9091 return *(const unsigned char*)arg1. */
9092 if (p2 && *p2 == '\0'
9093 && TREE_CODE (len) == INTEGER_CST
9094 && tree_int_cst_sgn (len) == 1)
9096 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9097 tree cst_uchar_ptr_node
9098 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9100 return fold_convert (integer_type_node,
9101 build1 (INDIRECT_REF, cst_uchar_node,
9102 fold_convert (cst_uchar_ptr_node,
9103 arg1)));
9106 /* If the first arg is "", and the length is greater than zero,
9107 return -*(const unsigned char*)arg2. */
9108 if (p1 && *p1 == '\0'
9109 && TREE_CODE (len) == INTEGER_CST
9110 && tree_int_cst_sgn (len) == 1)
9112 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9113 tree cst_uchar_ptr_node
9114 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9116 tree temp = fold_convert (integer_type_node,
9117 build1 (INDIRECT_REF, cst_uchar_node,
9118 fold_convert (cst_uchar_ptr_node,
9119 arg2)));
9120 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
9123 /* If len parameter is one, return an expression corresponding to
9124 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9125 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9127 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9128 tree cst_uchar_ptr_node
9129 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9131 tree ind1 = fold_convert (integer_type_node,
9132 build1 (INDIRECT_REF, cst_uchar_node,
9133 fold_convert (cst_uchar_ptr_node,
9134 arg1)));
9135 tree ind2 = fold_convert (integer_type_node,
9136 build1 (INDIRECT_REF, cst_uchar_node,
9137 fold_convert (cst_uchar_ptr_node,
9138 arg2)));
9139 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
9142 return NULL_TREE;
9145 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9146 ARG. Return NULL_TREE if no simplification can be made. */
9148 static tree
9149 fold_builtin_signbit (tree arg, tree type)
9151 tree temp;
9153 if (!validate_arg (arg, REAL_TYPE))
9154 return NULL_TREE;
9156 /* If ARG is a compile-time constant, determine the result. */
9157 if (TREE_CODE (arg) == REAL_CST
9158 && !TREE_OVERFLOW (arg))
9160 REAL_VALUE_TYPE c;
9162 c = TREE_REAL_CST (arg);
9163 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
9164 return fold_convert (type, temp);
9167 /* If ARG is non-negative, the result is always zero. */
9168 if (tree_expr_nonnegative_p (arg))
9169 return omit_one_operand (type, integer_zero_node, arg);
9171 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9172 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9173 return fold_build2 (LT_EXPR, type, arg,
9174 build_real (TREE_TYPE (arg), dconst0));
9176 return NULL_TREE;
9179 /* Fold function call to builtin copysign, copysignf or copysignl with
9180 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9181 be made. */
9183 static tree
9184 fold_builtin_copysign (tree fndecl, tree arg1, tree arg2, tree type)
9186 tree tem;
9188 if (!validate_arg (arg1, REAL_TYPE)
9189 || !validate_arg (arg2, REAL_TYPE))
9190 return NULL_TREE;
9192 /* copysign(X,X) is X. */
9193 if (operand_equal_p (arg1, arg2, 0))
9194 return fold_convert (type, arg1);
9196 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9197 if (TREE_CODE (arg1) == REAL_CST
9198 && TREE_CODE (arg2) == REAL_CST
9199 && !TREE_OVERFLOW (arg1)
9200 && !TREE_OVERFLOW (arg2))
9202 REAL_VALUE_TYPE c1, c2;
9204 c1 = TREE_REAL_CST (arg1);
9205 c2 = TREE_REAL_CST (arg2);
9206 /* c1.sign := c2.sign. */
9207 real_copysign (&c1, &c2);
9208 return build_real (type, c1);
9211 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9212 Remember to evaluate Y for side-effects. */
9213 if (tree_expr_nonnegative_p (arg2))
9214 return omit_one_operand (type,
9215 fold_build1 (ABS_EXPR, type, arg1),
9216 arg2);
9218 /* Strip sign changing operations for the first argument. */
9219 tem = fold_strip_sign_ops (arg1);
9220 if (tem)
9221 return build_call_expr (fndecl, 2, tem, arg2);
9223 return NULL_TREE;
9226 /* Fold a call to builtin isascii with argument ARG. */
9228 static tree
9229 fold_builtin_isascii (tree arg)
9231 if (!validate_arg (arg, INTEGER_TYPE))
9232 return NULL_TREE;
9233 else
9235 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9236 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
9237 build_int_cst (NULL_TREE,
9238 ~ (unsigned HOST_WIDE_INT) 0x7f));
9239 return fold_build2 (EQ_EXPR, integer_type_node,
9240 arg, integer_zero_node);
9244 /* Fold a call to builtin toascii with argument ARG. */
9246 static tree
9247 fold_builtin_toascii (tree arg)
9249 if (!validate_arg (arg, INTEGER_TYPE))
9250 return NULL_TREE;
9252 /* Transform toascii(c) -> (c & 0x7f). */
9253 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9254 build_int_cst (NULL_TREE, 0x7f));
9257 /* Fold a call to builtin isdigit with argument ARG. */
9259 static tree
9260 fold_builtin_isdigit (tree arg)
9262 if (!validate_arg (arg, INTEGER_TYPE))
9263 return NULL_TREE;
9264 else
9266 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9267 /* According to the C standard, isdigit is unaffected by locale.
9268 However, it definitely is affected by the target character set. */
9269 unsigned HOST_WIDE_INT target_digit0
9270 = lang_hooks.to_target_charset ('0');
9272 if (target_digit0 == 0)
9273 return NULL_TREE;
9275 arg = fold_convert (unsigned_type_node, arg);
9276 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
9277 build_int_cst (unsigned_type_node, target_digit0));
9278 return fold_build2 (LE_EXPR, integer_type_node, arg,
9279 build_int_cst (unsigned_type_node, 9));
9283 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9285 static tree
9286 fold_builtin_fabs (tree arg, tree type)
9288 if (!validate_arg (arg, REAL_TYPE))
9289 return NULL_TREE;
9291 arg = fold_convert (type, arg);
9292 if (TREE_CODE (arg) == REAL_CST)
9293 return fold_abs_const (arg, type);
9294 return fold_build1 (ABS_EXPR, type, arg);
9297 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9299 static tree
9300 fold_builtin_abs (tree arg, tree type)
9302 if (!validate_arg (arg, INTEGER_TYPE))
9303 return NULL_TREE;
9305 arg = fold_convert (type, arg);
9306 if (TREE_CODE (arg) == INTEGER_CST)
9307 return fold_abs_const (arg, type);
9308 return fold_build1 (ABS_EXPR, type, arg);
9311 /* Fold a call to builtin fmin or fmax. */
9313 static tree
9314 fold_builtin_fmin_fmax (tree arg0, tree arg1, tree type, bool max)
9316 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9318 /* Calculate the result when the argument is a constant. */
9319 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9321 if (res)
9322 return res;
9324 /* If either argument is NaN, return the other one. Avoid the
9325 transformation if we get (and honor) a signalling NaN. Using
9326 omit_one_operand() ensures we create a non-lvalue. */
9327 if (TREE_CODE (arg0) == REAL_CST
9328 && real_isnan (&TREE_REAL_CST (arg0))
9329 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9330 || ! TREE_REAL_CST (arg0).signalling))
9331 return omit_one_operand (type, arg1, arg0);
9332 if (TREE_CODE (arg1) == REAL_CST
9333 && real_isnan (&TREE_REAL_CST (arg1))
9334 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9335 || ! TREE_REAL_CST (arg1).signalling))
9336 return omit_one_operand (type, arg0, arg1);
9338 /* Transform fmin/fmax(x,x) -> x. */
9339 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9340 return omit_one_operand (type, arg0, arg1);
9342 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9343 functions to return the numeric arg if the other one is NaN.
9344 These tree codes don't honor that, so only transform if
9345 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9346 handled, so we don't have to worry about it either. */
9347 if (flag_finite_math_only)
9348 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9349 fold_convert (type, arg0),
9350 fold_convert (type, arg1));
9352 return NULL_TREE;
9355 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9357 static tree
9358 fold_builtin_carg (tree arg, tree type)
9360 if (validate_arg (arg, COMPLEX_TYPE))
9362 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9364 if (atan2_fn)
9366 tree new_arg = builtin_save_expr (arg);
9367 tree r_arg = fold_build1 (REALPART_EXPR, type, new_arg);
9368 tree i_arg = fold_build1 (IMAGPART_EXPR, type, new_arg);
9369 return build_call_expr (atan2_fn, 2, i_arg, r_arg);
9373 return NULL_TREE;
9376 /* Fold a call to builtin logb/ilogb. */
9378 static tree
9379 fold_builtin_logb (tree arg, tree rettype)
9381 if (! validate_arg (arg, REAL_TYPE))
9382 return NULL_TREE;
9384 STRIP_NOPS (arg);
9386 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9388 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9390 switch (value->cl)
9392 case rvc_nan:
9393 case rvc_inf:
9394 /* If arg is Inf or NaN and we're logb, return it. */
9395 if (TREE_CODE (rettype) == REAL_TYPE)
9396 return fold_convert (rettype, arg);
9397 /* Fall through... */
9398 case rvc_zero:
9399 /* Zero may set errno and/or raise an exception for logb, also
9400 for ilogb we don't know FP_ILOGB0. */
9401 return NULL_TREE;
9402 case rvc_normal:
9403 /* For normal numbers, proceed iff radix == 2. In GCC,
9404 normalized significands are in the range [0.5, 1.0). We
9405 want the exponent as if they were [1.0, 2.0) so get the
9406 exponent and subtract 1. */
9407 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9408 return fold_convert (rettype, build_int_cst (NULL_TREE,
9409 REAL_EXP (value)-1));
9410 break;
9414 return NULL_TREE;
9417 /* Fold a call to builtin significand, if radix == 2. */
9419 static tree
9420 fold_builtin_significand (tree arg, tree rettype)
9422 if (! validate_arg (arg, REAL_TYPE))
9423 return NULL_TREE;
9425 STRIP_NOPS (arg);
9427 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9429 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9431 switch (value->cl)
9433 case rvc_zero:
9434 case rvc_nan:
9435 case rvc_inf:
9436 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9437 return fold_convert (rettype, arg);
9438 case rvc_normal:
9439 /* For normal numbers, proceed iff radix == 2. */
9440 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9442 REAL_VALUE_TYPE result = *value;
9443 /* In GCC, normalized significands are in the range [0.5,
9444 1.0). We want them to be [1.0, 2.0) so set the
9445 exponent to 1. */
9446 SET_REAL_EXP (&result, 1);
9447 return build_real (rettype, result);
9449 break;
9453 return NULL_TREE;
9456 /* Fold a call to builtin frexp, we can assume the base is 2. */
9458 static tree
9459 fold_builtin_frexp (tree arg0, tree arg1, tree rettype)
9461 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9462 return NULL_TREE;
9464 STRIP_NOPS (arg0);
9466 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9467 return NULL_TREE;
9469 arg1 = build_fold_indirect_ref (arg1);
9471 /* Proceed if a valid pointer type was passed in. */
9472 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9474 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9475 tree frac, exp;
9477 switch (value->cl)
9479 case rvc_zero:
9480 /* For +-0, return (*exp = 0, +-0). */
9481 exp = integer_zero_node;
9482 frac = arg0;
9483 break;
9484 case rvc_nan:
9485 case rvc_inf:
9486 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9487 return omit_one_operand (rettype, arg0, arg1);
9488 case rvc_normal:
9490 /* Since the frexp function always expects base 2, and in
9491 GCC normalized significands are already in the range
9492 [0.5, 1.0), we have exactly what frexp wants. */
9493 REAL_VALUE_TYPE frac_rvt = *value;
9494 SET_REAL_EXP (&frac_rvt, 0);
9495 frac = build_real (rettype, frac_rvt);
9496 exp = build_int_cst (NULL_TREE, REAL_EXP (value));
9498 break;
9499 default:
9500 gcc_unreachable ();
9503 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9504 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, exp);
9505 TREE_SIDE_EFFECTS (arg1) = 1;
9506 return fold_build2 (COMPOUND_EXPR, rettype, arg1, frac);
9509 return NULL_TREE;
9512 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9513 then we can assume the base is two. If it's false, then we have to
9514 check the mode of the TYPE parameter in certain cases. */
9516 static tree
9517 fold_builtin_load_exponent (tree arg0, tree arg1, tree type, bool ldexp)
9519 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9521 STRIP_NOPS (arg0);
9522 STRIP_NOPS (arg1);
9524 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9525 if (real_zerop (arg0) || integer_zerop (arg1)
9526 || (TREE_CODE (arg0) == REAL_CST
9527 && !real_isfinite (&TREE_REAL_CST (arg0))))
9528 return omit_one_operand (type, arg0, arg1);
9530 /* If both arguments are constant, then try to evaluate it. */
9531 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9532 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9533 && host_integerp (arg1, 0))
9535 /* Bound the maximum adjustment to twice the range of the
9536 mode's valid exponents. Use abs to ensure the range is
9537 positive as a sanity check. */
9538 const long max_exp_adj = 2 *
9539 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9540 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9542 /* Get the user-requested adjustment. */
9543 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9545 /* The requested adjustment must be inside this range. This
9546 is a preliminary cap to avoid things like overflow, we
9547 may still fail to compute the result for other reasons. */
9548 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9550 REAL_VALUE_TYPE initial_result;
9552 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9554 /* Ensure we didn't overflow. */
9555 if (! real_isinf (&initial_result))
9557 const REAL_VALUE_TYPE trunc_result
9558 = real_value_truncate (TYPE_MODE (type), initial_result);
9560 /* Only proceed if the target mode can hold the
9561 resulting value. */
9562 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9563 return build_real (type, trunc_result);
9569 return NULL_TREE;
9572 /* Fold a call to builtin modf. */
9574 static tree
9575 fold_builtin_modf (tree arg0, tree arg1, tree rettype)
9577 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9578 return NULL_TREE;
9580 STRIP_NOPS (arg0);
9582 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9583 return NULL_TREE;
9585 arg1 = build_fold_indirect_ref (arg1);
9587 /* Proceed if a valid pointer type was passed in. */
9588 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9590 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9591 REAL_VALUE_TYPE trunc, frac;
9593 switch (value->cl)
9595 case rvc_nan:
9596 case rvc_zero:
9597 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9598 trunc = frac = *value;
9599 break;
9600 case rvc_inf:
9601 /* For +-Inf, return (*arg1 = arg0, +-0). */
9602 frac = dconst0;
9603 frac.sign = value->sign;
9604 trunc = *value;
9605 break;
9606 case rvc_normal:
9607 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9608 real_trunc (&trunc, VOIDmode, value);
9609 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9610 /* If the original number was negative and already
9611 integral, then the fractional part is -0.0. */
9612 if (value->sign && frac.cl == rvc_zero)
9613 frac.sign = value->sign;
9614 break;
9617 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9618 arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1,
9619 build_real (rettype, trunc));
9620 TREE_SIDE_EFFECTS (arg1) = 1;
9621 return fold_build2 (COMPOUND_EXPR, rettype, arg1,
9622 build_real (rettype, frac));
9625 return NULL_TREE;
9628 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9629 ARG is the argument for the call. */
9631 static tree
9632 fold_builtin_classify (tree fndecl, tree arg, int builtin_index)
9634 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9635 REAL_VALUE_TYPE r;
9637 if (!validate_arg (arg, REAL_TYPE))
9639 error ("non-floating-point argument to function %qs",
9640 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9641 return error_mark_node;
9644 switch (builtin_index)
9646 case BUILT_IN_ISINF:
9647 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9648 return omit_one_operand (type, integer_zero_node, arg);
9650 if (TREE_CODE (arg) == REAL_CST)
9652 r = TREE_REAL_CST (arg);
9653 if (real_isinf (&r))
9654 return real_compare (GT_EXPR, &r, &dconst0)
9655 ? integer_one_node : integer_minus_one_node;
9656 else
9657 return integer_zero_node;
9660 return NULL_TREE;
9662 case BUILT_IN_ISFINITE:
9663 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9664 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9665 return omit_one_operand (type, integer_one_node, arg);
9667 if (TREE_CODE (arg) == REAL_CST)
9669 r = TREE_REAL_CST (arg);
9670 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9673 return NULL_TREE;
9675 case BUILT_IN_ISNAN:
9676 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9677 return omit_one_operand (type, integer_zero_node, arg);
9679 if (TREE_CODE (arg) == REAL_CST)
9681 r = TREE_REAL_CST (arg);
9682 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9685 arg = builtin_save_expr (arg);
9686 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
9688 default:
9689 gcc_unreachable ();
9693 /* Fold a call to an unordered comparison function such as
9694 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9695 being called and ARG0 and ARG1 are the arguments for the call.
9696 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9697 the opposite of the desired result. UNORDERED_CODE is used
9698 for modes that can hold NaNs and ORDERED_CODE is used for
9699 the rest. */
9701 static tree
9702 fold_builtin_unordered_cmp (tree fndecl, tree arg0, tree arg1,
9703 enum tree_code unordered_code,
9704 enum tree_code ordered_code)
9706 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9707 enum tree_code code;
9708 tree type0, type1;
9709 enum tree_code code0, code1;
9710 tree cmp_type = NULL_TREE;
9712 type0 = TREE_TYPE (arg0);
9713 type1 = TREE_TYPE (arg1);
9715 code0 = TREE_CODE (type0);
9716 code1 = TREE_CODE (type1);
9718 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9719 /* Choose the wider of two real types. */
9720 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9721 ? type0 : type1;
9722 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9723 cmp_type = type0;
9724 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9725 cmp_type = type1;
9726 else
9728 error ("non-floating-point argument to function %qs",
9729 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9730 return error_mark_node;
9733 arg0 = fold_convert (cmp_type, arg0);
9734 arg1 = fold_convert (cmp_type, arg1);
9736 if (unordered_code == UNORDERED_EXPR)
9738 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9739 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9740 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9743 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9744 : ordered_code;
9745 return fold_build1 (TRUTH_NOT_EXPR, type,
9746 fold_build2 (code, type, arg0, arg1));
9749 /* Fold a call to built-in function FNDECL with 0 arguments.
9750 IGNORE is true if the result of the function call is ignored. This
9751 function returns NULL_TREE if no simplification was possible. */
9753 static tree
9754 fold_builtin_0 (tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9756 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9757 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9758 switch (fcode)
9760 CASE_FLT_FN (BUILT_IN_INF):
9761 case BUILT_IN_INFD32:
9762 case BUILT_IN_INFD64:
9763 case BUILT_IN_INFD128:
9764 return fold_builtin_inf (type, true);
9766 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9767 return fold_builtin_inf (type, false);
9769 case BUILT_IN_CLASSIFY_TYPE:
9770 return fold_builtin_classify_type (NULL_TREE);
9772 default:
9773 break;
9775 return NULL_TREE;
9778 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9779 IGNORE is true if the result of the function call is ignored. This
9780 function returns NULL_TREE if no simplification was possible. */
9782 static tree
9783 fold_builtin_1 (tree fndecl, tree arg0, bool ignore)
9785 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9786 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9787 switch (fcode)
9790 case BUILT_IN_CONSTANT_P:
9792 tree val = fold_builtin_constant_p (arg0);
9794 /* Gimplification will pull the CALL_EXPR for the builtin out of
9795 an if condition. When not optimizing, we'll not CSE it back.
9796 To avoid link error types of regressions, return false now. */
9797 if (!val && !optimize)
9798 val = integer_zero_node;
9800 return val;
9803 case BUILT_IN_CLASSIFY_TYPE:
9804 return fold_builtin_classify_type (arg0);
9806 case BUILT_IN_STRLEN:
9807 return fold_builtin_strlen (arg0);
9809 CASE_FLT_FN (BUILT_IN_FABS):
9810 return fold_builtin_fabs (arg0, type);
9812 case BUILT_IN_ABS:
9813 case BUILT_IN_LABS:
9814 case BUILT_IN_LLABS:
9815 case BUILT_IN_IMAXABS:
9816 return fold_builtin_abs (arg0, type);
9818 CASE_FLT_FN (BUILT_IN_CONJ):
9819 if (validate_arg (arg0, COMPLEX_TYPE))
9820 return fold_build1 (CONJ_EXPR, type, arg0);
9821 break;
9823 CASE_FLT_FN (BUILT_IN_CREAL):
9824 if (validate_arg (arg0, COMPLEX_TYPE))
9825 return non_lvalue (fold_build1 (REALPART_EXPR, type, arg0));;
9826 break;
9828 CASE_FLT_FN (BUILT_IN_CIMAG):
9829 if (validate_arg (arg0, COMPLEX_TYPE))
9830 return non_lvalue (fold_build1 (IMAGPART_EXPR, type, arg0));
9831 break;
9833 CASE_FLT_FN (BUILT_IN_CCOS):
9834 CASE_FLT_FN (BUILT_IN_CCOSH):
9835 /* These functions are "even", i.e. f(x) == f(-x). */
9836 if (validate_arg (arg0, COMPLEX_TYPE))
9838 tree narg = fold_strip_sign_ops (arg0);
9839 if (narg)
9840 return build_call_expr (fndecl, 1, narg);
9842 break;
9844 CASE_FLT_FN (BUILT_IN_CABS):
9845 return fold_builtin_cabs (arg0, type, fndecl);
9847 CASE_FLT_FN (BUILT_IN_CARG):
9848 return fold_builtin_carg (arg0, type);
9850 CASE_FLT_FN (BUILT_IN_SQRT):
9851 return fold_builtin_sqrt (arg0, type);
9853 CASE_FLT_FN (BUILT_IN_CBRT):
9854 return fold_builtin_cbrt (arg0, type);
9856 CASE_FLT_FN (BUILT_IN_ASIN):
9857 if (validate_arg (arg0, REAL_TYPE))
9858 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9859 &dconstm1, &dconst1, true);
9860 break;
9862 CASE_FLT_FN (BUILT_IN_ACOS):
9863 if (validate_arg (arg0, REAL_TYPE))
9864 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9865 &dconstm1, &dconst1, true);
9866 break;
9868 CASE_FLT_FN (BUILT_IN_ATAN):
9869 if (validate_arg (arg0, REAL_TYPE))
9870 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9871 break;
9873 CASE_FLT_FN (BUILT_IN_ASINH):
9874 if (validate_arg (arg0, REAL_TYPE))
9875 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9876 break;
9878 CASE_FLT_FN (BUILT_IN_ACOSH):
9879 if (validate_arg (arg0, REAL_TYPE))
9880 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9881 &dconst1, NULL, true);
9882 break;
9884 CASE_FLT_FN (BUILT_IN_ATANH):
9885 if (validate_arg (arg0, REAL_TYPE))
9886 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9887 &dconstm1, &dconst1, false);
9888 break;
9890 CASE_FLT_FN (BUILT_IN_SIN):
9891 if (validate_arg (arg0, REAL_TYPE))
9892 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9893 break;
9895 CASE_FLT_FN (BUILT_IN_COS):
9896 return fold_builtin_cos (arg0, type, fndecl);
9897 break;
9899 CASE_FLT_FN (BUILT_IN_TAN):
9900 return fold_builtin_tan (arg0, type);
9902 CASE_FLT_FN (BUILT_IN_CEXP):
9903 return fold_builtin_cexp (arg0, type);
9905 CASE_FLT_FN (BUILT_IN_CEXPI):
9906 if (validate_arg (arg0, REAL_TYPE))
9907 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9908 break;
9910 CASE_FLT_FN (BUILT_IN_SINH):
9911 if (validate_arg (arg0, REAL_TYPE))
9912 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9913 break;
9915 CASE_FLT_FN (BUILT_IN_COSH):
9916 return fold_builtin_cosh (arg0, type, fndecl);
9918 CASE_FLT_FN (BUILT_IN_TANH):
9919 if (validate_arg (arg0, REAL_TYPE))
9920 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9921 break;
9923 CASE_FLT_FN (BUILT_IN_ERF):
9924 if (validate_arg (arg0, REAL_TYPE))
9925 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9926 break;
9928 CASE_FLT_FN (BUILT_IN_ERFC):
9929 if (validate_arg (arg0, REAL_TYPE))
9930 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9931 break;
9933 CASE_FLT_FN (BUILT_IN_TGAMMA):
9934 if (validate_arg (arg0, REAL_TYPE))
9935 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9936 break;
9938 CASE_FLT_FN (BUILT_IN_EXP):
9939 return fold_builtin_exponent (fndecl, arg0, mpfr_exp);
9941 CASE_FLT_FN (BUILT_IN_EXP2):
9942 return fold_builtin_exponent (fndecl, arg0, mpfr_exp2);
9944 CASE_FLT_FN (BUILT_IN_EXP10):
9945 CASE_FLT_FN (BUILT_IN_POW10):
9946 return fold_builtin_exponent (fndecl, arg0, mpfr_exp10);
9948 CASE_FLT_FN (BUILT_IN_EXPM1):
9949 if (validate_arg (arg0, REAL_TYPE))
9950 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
9951 break;
9953 CASE_FLT_FN (BUILT_IN_LOG):
9954 return fold_builtin_logarithm (fndecl, arg0, mpfr_log);
9956 CASE_FLT_FN (BUILT_IN_LOG2):
9957 return fold_builtin_logarithm (fndecl, arg0, mpfr_log2);
9959 CASE_FLT_FN (BUILT_IN_LOG10):
9960 return fold_builtin_logarithm (fndecl, arg0, mpfr_log10);
9962 CASE_FLT_FN (BUILT_IN_LOG1P):
9963 if (validate_arg (arg0, REAL_TYPE))
9964 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
9965 &dconstm1, NULL, false);
9966 break;
9968 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
9969 CASE_FLT_FN (BUILT_IN_J0):
9970 if (validate_arg (arg0, REAL_TYPE))
9971 return do_mpfr_arg1 (arg0, type, mpfr_j0,
9972 NULL, NULL, 0);
9973 break;
9975 CASE_FLT_FN (BUILT_IN_J1):
9976 if (validate_arg (arg0, REAL_TYPE))
9977 return do_mpfr_arg1 (arg0, type, mpfr_j1,
9978 NULL, NULL, 0);
9979 break;
9981 CASE_FLT_FN (BUILT_IN_Y0):
9982 if (validate_arg (arg0, REAL_TYPE))
9983 return do_mpfr_arg1 (arg0, type, mpfr_y0,
9984 &dconst0, NULL, false);
9985 break;
9987 CASE_FLT_FN (BUILT_IN_Y1):
9988 if (validate_arg (arg0, REAL_TYPE))
9989 return do_mpfr_arg1 (arg0, type, mpfr_y1,
9990 &dconst0, NULL, false);
9991 break;
9992 #endif
9994 CASE_FLT_FN (BUILT_IN_NAN):
9995 case BUILT_IN_NAND32:
9996 case BUILT_IN_NAND64:
9997 case BUILT_IN_NAND128:
9998 return fold_builtin_nan (arg0, type, true);
10000 CASE_FLT_FN (BUILT_IN_NANS):
10001 return fold_builtin_nan (arg0, type, false);
10003 CASE_FLT_FN (BUILT_IN_FLOOR):
10004 return fold_builtin_floor (fndecl, arg0);
10006 CASE_FLT_FN (BUILT_IN_CEIL):
10007 return fold_builtin_ceil (fndecl, arg0);
10009 CASE_FLT_FN (BUILT_IN_TRUNC):
10010 return fold_builtin_trunc (fndecl, arg0);
10012 CASE_FLT_FN (BUILT_IN_ROUND):
10013 return fold_builtin_round (fndecl, arg0);
10015 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10016 CASE_FLT_FN (BUILT_IN_RINT):
10017 return fold_trunc_transparent_mathfn (fndecl, arg0);
10019 CASE_FLT_FN (BUILT_IN_LCEIL):
10020 CASE_FLT_FN (BUILT_IN_LLCEIL):
10021 CASE_FLT_FN (BUILT_IN_LFLOOR):
10022 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10023 CASE_FLT_FN (BUILT_IN_LROUND):
10024 CASE_FLT_FN (BUILT_IN_LLROUND):
10025 return fold_builtin_int_roundingfn (fndecl, arg0);
10027 CASE_FLT_FN (BUILT_IN_LRINT):
10028 CASE_FLT_FN (BUILT_IN_LLRINT):
10029 return fold_fixed_mathfn (fndecl, arg0);
10031 case BUILT_IN_BSWAP32:
10032 case BUILT_IN_BSWAP64:
10033 return fold_builtin_bswap (fndecl, arg0);
10035 CASE_INT_FN (BUILT_IN_FFS):
10036 CASE_INT_FN (BUILT_IN_CLZ):
10037 CASE_INT_FN (BUILT_IN_CTZ):
10038 CASE_INT_FN (BUILT_IN_POPCOUNT):
10039 CASE_INT_FN (BUILT_IN_PARITY):
10040 return fold_builtin_bitop (fndecl, arg0);
10042 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10043 return fold_builtin_signbit (arg0, type);
10045 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10046 return fold_builtin_significand (arg0, type);
10048 CASE_FLT_FN (BUILT_IN_ILOGB):
10049 CASE_FLT_FN (BUILT_IN_LOGB):
10050 return fold_builtin_logb (arg0, type);
10052 case BUILT_IN_ISASCII:
10053 return fold_builtin_isascii (arg0);
10055 case BUILT_IN_TOASCII:
10056 return fold_builtin_toascii (arg0);
10058 case BUILT_IN_ISDIGIT:
10059 return fold_builtin_isdigit (arg0);
10061 CASE_FLT_FN (BUILT_IN_FINITE):
10062 case BUILT_IN_FINITED32:
10063 case BUILT_IN_FINITED64:
10064 case BUILT_IN_FINITED128:
10065 case BUILT_IN_ISFINITE:
10066 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISFINITE);
10068 CASE_FLT_FN (BUILT_IN_ISINF):
10069 case BUILT_IN_ISINFD32:
10070 case BUILT_IN_ISINFD64:
10071 case BUILT_IN_ISINFD128:
10072 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF);
10074 CASE_FLT_FN (BUILT_IN_ISNAN):
10075 case BUILT_IN_ISNAND32:
10076 case BUILT_IN_ISNAND64:
10077 case BUILT_IN_ISNAND128:
10078 return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISNAN);
10080 case BUILT_IN_ISNORMAL:
10081 if (!validate_arg (arg0, REAL_TYPE))
10083 error ("non-floating-point argument to function %qs",
10084 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
10085 return error_mark_node;
10087 break;
10089 case BUILT_IN_PRINTF:
10090 case BUILT_IN_PRINTF_UNLOCKED:
10091 case BUILT_IN_VPRINTF:
10092 return fold_builtin_printf (fndecl, arg0, NULL_TREE, ignore, fcode);
10094 default:
10095 break;
10098 return NULL_TREE;
10102 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10103 IGNORE is true if the result of the function call is ignored. This
10104 function returns NULL_TREE if no simplification was possible. */
10106 static tree
10107 fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore)
10109 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10110 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10112 switch (fcode)
10114 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
10115 CASE_FLT_FN (BUILT_IN_JN):
10116 if (validate_arg (arg0, INTEGER_TYPE)
10117 && validate_arg (arg1, REAL_TYPE))
10118 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10119 break;
10121 CASE_FLT_FN (BUILT_IN_YN):
10122 if (validate_arg (arg0, INTEGER_TYPE)
10123 && validate_arg (arg1, REAL_TYPE))
10124 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10125 &dconst0, false);
10126 break;
10128 CASE_FLT_FN (BUILT_IN_DREM):
10129 CASE_FLT_FN (BUILT_IN_REMAINDER):
10130 if (validate_arg (arg0, REAL_TYPE)
10131 && validate_arg(arg1, REAL_TYPE))
10132 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10133 break;
10135 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10136 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10137 if (validate_arg (arg0, REAL_TYPE)
10138 && validate_arg(arg1, POINTER_TYPE))
10139 return do_mpfr_lgamma_r (arg0, arg1, type);
10140 break;
10141 #endif
10143 CASE_FLT_FN (BUILT_IN_ATAN2):
10144 if (validate_arg (arg0, REAL_TYPE)
10145 && validate_arg(arg1, REAL_TYPE))
10146 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10147 break;
10149 CASE_FLT_FN (BUILT_IN_FDIM):
10150 if (validate_arg (arg0, REAL_TYPE)
10151 && validate_arg(arg1, REAL_TYPE))
10152 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10153 break;
10155 CASE_FLT_FN (BUILT_IN_HYPOT):
10156 return fold_builtin_hypot (fndecl, arg0, arg1, type);
10158 CASE_FLT_FN (BUILT_IN_LDEXP):
10159 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/true);
10160 CASE_FLT_FN (BUILT_IN_SCALBN):
10161 CASE_FLT_FN (BUILT_IN_SCALBLN):
10162 return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/false);
10164 CASE_FLT_FN (BUILT_IN_FREXP):
10165 return fold_builtin_frexp (arg0, arg1, type);
10167 CASE_FLT_FN (BUILT_IN_MODF):
10168 return fold_builtin_modf (arg0, arg1, type);
10170 case BUILT_IN_BZERO:
10171 return fold_builtin_bzero (arg0, arg1, ignore);
10173 case BUILT_IN_FPUTS:
10174 return fold_builtin_fputs (arg0, arg1, ignore, false, NULL_TREE);
10176 case BUILT_IN_FPUTS_UNLOCKED:
10177 return fold_builtin_fputs (arg0, arg1, ignore, true, NULL_TREE);
10179 case BUILT_IN_STRSTR:
10180 return fold_builtin_strstr (arg0, arg1, type);
10182 case BUILT_IN_STRCAT:
10183 return fold_builtin_strcat (arg0, arg1);
10185 case BUILT_IN_STRSPN:
10186 return fold_builtin_strspn (arg0, arg1);
10188 case BUILT_IN_STRCSPN:
10189 return fold_builtin_strcspn (arg0, arg1);
10191 case BUILT_IN_STRCHR:
10192 case BUILT_IN_INDEX:
10193 return fold_builtin_strchr (arg0, arg1, type);
10195 case BUILT_IN_STRRCHR:
10196 case BUILT_IN_RINDEX:
10197 return fold_builtin_strrchr (arg0, arg1, type);
10199 case BUILT_IN_STRCPY:
10200 return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE);
10202 case BUILT_IN_STRCMP:
10203 return fold_builtin_strcmp (arg0, arg1);
10205 case BUILT_IN_STRPBRK:
10206 return fold_builtin_strpbrk (arg0, arg1, type);
10208 case BUILT_IN_EXPECT:
10209 return fold_builtin_expect (arg0, arg1);
10211 CASE_FLT_FN (BUILT_IN_POW):
10212 return fold_builtin_pow (fndecl, arg0, arg1, type);
10214 CASE_FLT_FN (BUILT_IN_POWI):
10215 return fold_builtin_powi (fndecl, arg0, arg1, type);
10217 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10218 return fold_builtin_copysign (fndecl, arg0, arg1, type);
10220 CASE_FLT_FN (BUILT_IN_FMIN):
10221 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/false);
10223 CASE_FLT_FN (BUILT_IN_FMAX):
10224 return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/true);
10226 case BUILT_IN_ISGREATER:
10227 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLE_EXPR, LE_EXPR);
10228 case BUILT_IN_ISGREATEREQUAL:
10229 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLT_EXPR, LT_EXPR);
10230 case BUILT_IN_ISLESS:
10231 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGE_EXPR, GE_EXPR);
10232 case BUILT_IN_ISLESSEQUAL:
10233 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGT_EXPR, GT_EXPR);
10234 case BUILT_IN_ISLESSGREATER:
10235 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10236 case BUILT_IN_ISUNORDERED:
10237 return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNORDERED_EXPR,
10238 NOP_EXPR);
10240 /* We do the folding for va_start in the expander. */
10241 case BUILT_IN_VA_START:
10242 break;
10244 case BUILT_IN_SPRINTF:
10245 return fold_builtin_sprintf (arg0, arg1, NULL_TREE, ignore);
10247 case BUILT_IN_OBJECT_SIZE:
10248 return fold_builtin_object_size (arg0, arg1);
10250 case BUILT_IN_PRINTF:
10251 case BUILT_IN_PRINTF_UNLOCKED:
10252 case BUILT_IN_VPRINTF:
10253 return fold_builtin_printf (fndecl, arg0, arg1, ignore, fcode);
10255 case BUILT_IN_PRINTF_CHK:
10256 case BUILT_IN_VPRINTF_CHK:
10257 if (!validate_arg (arg0, INTEGER_TYPE)
10258 || TREE_SIDE_EFFECTS (arg0))
10259 return NULL_TREE;
10260 else
10261 return fold_builtin_printf (fndecl, arg1, NULL_TREE, ignore, fcode);
10262 break;
10264 case BUILT_IN_FPRINTF:
10265 case BUILT_IN_FPRINTF_UNLOCKED:
10266 case BUILT_IN_VFPRINTF:
10267 return fold_builtin_fprintf (fndecl, arg0, arg1, NULL_TREE,
10268 ignore, fcode);
10270 default:
10271 break;
10273 return NULL_TREE;
10276 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10277 and ARG2. IGNORE is true if the result of the function call is ignored.
10278 This function returns NULL_TREE if no simplification was possible. */
10280 static tree
10281 fold_builtin_3 (tree fndecl, tree arg0, tree arg1, tree arg2, bool ignore)
10283 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10284 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10285 switch (fcode)
10288 CASE_FLT_FN (BUILT_IN_SINCOS):
10289 return fold_builtin_sincos (arg0, arg1, arg2);
10291 CASE_FLT_FN (BUILT_IN_FMA):
10292 if (validate_arg (arg0, REAL_TYPE)
10293 && validate_arg(arg1, REAL_TYPE)
10294 && validate_arg(arg2, REAL_TYPE))
10295 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
10296 break;
10298 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
10299 CASE_FLT_FN (BUILT_IN_REMQUO):
10300 if (validate_arg (arg0, REAL_TYPE)
10301 && validate_arg(arg1, REAL_TYPE)
10302 && validate_arg(arg2, POINTER_TYPE))
10303 return do_mpfr_remquo (arg0, arg1, arg2);
10304 break;
10305 #endif
10307 case BUILT_IN_MEMSET:
10308 return fold_builtin_memset (arg0, arg1, arg2, type, ignore);
10310 case BUILT_IN_BCOPY:
10311 return fold_builtin_memory_op (arg1, arg0, arg2, void_type_node, true, /*endp=*/3);
10313 case BUILT_IN_MEMCPY:
10314 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/0);
10316 case BUILT_IN_MEMPCPY:
10317 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/1);
10319 case BUILT_IN_MEMMOVE:
10320 return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/3);
10322 case BUILT_IN_STRNCAT:
10323 return fold_builtin_strncat (arg0, arg1, arg2);
10325 case BUILT_IN_STRNCPY:
10326 return fold_builtin_strncpy (fndecl, arg0, arg1, arg2, NULL_TREE);
10328 case BUILT_IN_STRNCMP:
10329 return fold_builtin_strncmp (arg0, arg1, arg2);
10331 case BUILT_IN_MEMCHR:
10332 return fold_builtin_memchr (arg0, arg1, arg2, type);
10334 case BUILT_IN_BCMP:
10335 case BUILT_IN_MEMCMP:
10336 return fold_builtin_memcmp (arg0, arg1, arg2);;
10338 case BUILT_IN_SPRINTF:
10339 return fold_builtin_sprintf (arg0, arg1, arg2, ignore);
10341 case BUILT_IN_STRCPY_CHK:
10342 case BUILT_IN_STPCPY_CHK:
10343 return fold_builtin_stxcpy_chk (fndecl, arg0, arg1, arg2, NULL_TREE,
10344 ignore, fcode);
10346 case BUILT_IN_STRCAT_CHK:
10347 return fold_builtin_strcat_chk (fndecl, arg0, arg1, arg2);
10349 case BUILT_IN_PRINTF_CHK:
10350 case BUILT_IN_VPRINTF_CHK:
10351 if (!validate_arg (arg0, INTEGER_TYPE)
10352 || TREE_SIDE_EFFECTS (arg0))
10353 return NULL_TREE;
10354 else
10355 return fold_builtin_printf (fndecl, arg1, arg2, ignore, fcode);
10356 break;
10358 case BUILT_IN_FPRINTF:
10359 case BUILT_IN_FPRINTF_UNLOCKED:
10360 case BUILT_IN_VFPRINTF:
10361 return fold_builtin_fprintf (fndecl, arg0, arg1, arg2, ignore, fcode);
10363 case BUILT_IN_FPRINTF_CHK:
10364 case BUILT_IN_VFPRINTF_CHK:
10365 if (!validate_arg (arg1, INTEGER_TYPE)
10366 || TREE_SIDE_EFFECTS (arg1))
10367 return NULL_TREE;
10368 else
10369 return fold_builtin_fprintf (fndecl, arg0, arg2, NULL_TREE,
10370 ignore, fcode);
10372 default:
10373 break;
10375 return NULL_TREE;
10378 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10379 ARG2, and ARG3. IGNORE is true if the result of the function call is
10380 ignored. This function returns NULL_TREE if no simplification was
10381 possible. */
10383 static tree
10384 fold_builtin_4 (tree fndecl, tree arg0, tree arg1, tree arg2, tree arg3,
10385 bool ignore)
10387 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10389 switch (fcode)
10391 case BUILT_IN_MEMCPY_CHK:
10392 case BUILT_IN_MEMPCPY_CHK:
10393 case BUILT_IN_MEMMOVE_CHK:
10394 case BUILT_IN_MEMSET_CHK:
10395 return fold_builtin_memory_chk (fndecl, arg0, arg1, arg2, arg3,
10396 NULL_TREE, ignore,
10397 DECL_FUNCTION_CODE (fndecl));
10399 case BUILT_IN_STRNCPY_CHK:
10400 return fold_builtin_strncpy_chk (arg0, arg1, arg2, arg3, NULL_TREE);
10402 case BUILT_IN_STRNCAT_CHK:
10403 return fold_builtin_strncat_chk (fndecl, arg0, arg1, arg2, arg3);
10405 case BUILT_IN_FPRINTF_CHK:
10406 case BUILT_IN_VFPRINTF_CHK:
10407 if (!validate_arg (arg1, INTEGER_TYPE)
10408 || TREE_SIDE_EFFECTS (arg1))
10409 return NULL_TREE;
10410 else
10411 return fold_builtin_fprintf (fndecl, arg0, arg2, arg3,
10412 ignore, fcode);
10413 break;
10415 default:
10416 break;
10418 return NULL_TREE;
10421 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10422 arguments, where NARGS <= 4. IGNORE is true if the result of the
10423 function call is ignored. This function returns NULL_TREE if no
10424 simplification was possible. Note that this only folds builtins with
10425 fixed argument patterns. Foldings that do varargs-to-varargs
10426 transformations, or that match calls with more than 4 arguments,
10427 need to be handled with fold_builtin_varargs instead. */
10429 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10431 static tree
10432 fold_builtin_n (tree fndecl, tree *args, int nargs, bool ignore)
10434 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10435 tree ret = NULL_TREE;
10437 /* Verify the number of arguments for type-generic and thus variadic
10438 builtins. */
10439 switch (fcode)
10441 case BUILT_IN_ISFINITE:
10442 case BUILT_IN_ISINF:
10443 case BUILT_IN_ISNAN:
10444 case BUILT_IN_ISNORMAL:
10445 if (nargs < 1)
10447 error ("too few arguments to function %qs",
10448 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
10449 return error_mark_node;
10451 else if (nargs > 1)
10453 error ("too many arguments to function %qs",
10454 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
10455 return error_mark_node;
10457 break;
10459 case BUILT_IN_ISGREATER:
10460 case BUILT_IN_ISGREATEREQUAL:
10461 case BUILT_IN_ISLESS:
10462 case BUILT_IN_ISLESSEQUAL:
10463 case BUILT_IN_ISLESSGREATER:
10464 case BUILT_IN_ISUNORDERED:
10465 if (nargs < 2)
10467 error ("too few arguments to function %qs",
10468 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
10469 return error_mark_node;
10471 else if (nargs > 2)
10473 error ("too many arguments to function %qs",
10474 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
10475 return error_mark_node;
10477 break;
10479 default:
10480 break;
10483 switch (nargs)
10485 case 0:
10486 ret = fold_builtin_0 (fndecl, ignore);
10487 break;
10488 case 1:
10489 ret = fold_builtin_1 (fndecl, args[0], ignore);
10490 break;
10491 case 2:
10492 ret = fold_builtin_2 (fndecl, args[0], args[1], ignore);
10493 break;
10494 case 3:
10495 ret = fold_builtin_3 (fndecl, args[0], args[1], args[2], ignore);
10496 break;
10497 case 4:
10498 ret = fold_builtin_4 (fndecl, args[0], args[1], args[2], args[3],
10499 ignore);
10500 break;
10501 default:
10502 break;
10504 if (ret)
10506 ret = build1 (NOP_EXPR, GENERIC_TREE_TYPE (ret), ret);
10507 TREE_NO_WARNING (ret) = 1;
10508 return ret;
10510 return NULL_TREE;
10513 /* Builtins with folding operations that operate on "..." arguments
10514 need special handling; we need to store the arguments in a convenient
10515 data structure before attempting any folding. Fortunately there are
10516 only a few builtins that fall into this category. FNDECL is the
10517 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10518 result of the function call is ignored. */
10520 static tree
10521 fold_builtin_varargs (tree fndecl, tree exp, bool ignore ATTRIBUTE_UNUSED)
10523 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10524 tree ret = NULL_TREE;
10526 switch (fcode)
10528 case BUILT_IN_SPRINTF_CHK:
10529 case BUILT_IN_VSPRINTF_CHK:
10530 ret = fold_builtin_sprintf_chk (exp, fcode);
10531 break;
10533 case BUILT_IN_SNPRINTF_CHK:
10534 case BUILT_IN_VSNPRINTF_CHK:
10535 ret = fold_builtin_snprintf_chk (exp, NULL_TREE, fcode);
10537 default:
10538 break;
10540 if (ret)
10542 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10543 TREE_NO_WARNING (ret) = 1;
10544 return ret;
10546 return NULL_TREE;
10549 /* A wrapper function for builtin folding that prevents warnings for
10550 "statement without effect" and the like, caused by removing the
10551 call node earlier than the warning is generated. */
10553 tree
10554 fold_call_expr (tree exp, bool ignore)
10556 tree ret = NULL_TREE;
10557 tree fndecl = get_callee_fndecl (exp);
10558 if (fndecl
10559 && TREE_CODE (fndecl) == FUNCTION_DECL
10560 && DECL_BUILT_IN (fndecl)
10561 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10562 yet. Defer folding until we see all the arguments
10563 (after inlining). */
10564 && !CALL_EXPR_VA_ARG_PACK (exp))
10566 int nargs = call_expr_nargs (exp);
10568 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10569 instead last argument is __builtin_va_arg_pack (). Defer folding
10570 even in that case, until arguments are finalized. */
10571 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10573 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10574 if (fndecl2
10575 && TREE_CODE (fndecl2) == FUNCTION_DECL
10576 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10577 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10578 return NULL_TREE;
10581 /* FIXME: Don't use a list in this interface. */
10582 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10583 return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore);
10584 else
10586 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10588 tree *args = CALL_EXPR_ARGP (exp);
10589 ret = fold_builtin_n (fndecl, args, nargs, ignore);
10591 if (!ret)
10592 ret = fold_builtin_varargs (fndecl, exp, ignore);
10593 if (ret)
10595 /* Propagate location information from original call to
10596 expansion of builtin. Otherwise things like
10597 maybe_emit_chk_warning, that operate on the expansion
10598 of a builtin, will use the wrong location information. */
10599 if (CAN_HAVE_LOCATION_P (exp) && EXPR_HAS_LOCATION (exp))
10601 tree realret = ret;
10602 if (TREE_CODE (ret) == NOP_EXPR)
10603 realret = TREE_OPERAND (ret, 0);
10604 if (CAN_HAVE_LOCATION_P (realret)
10605 && !EXPR_HAS_LOCATION (realret))
10606 SET_EXPR_LOCATION (realret, EXPR_LOCATION (exp));
10608 return ret;
10612 return NULL_TREE;
10615 /* Conveniently construct a function call expression. FNDECL names the
10616 function to be called and ARGLIST is a TREE_LIST of arguments. */
10618 tree
10619 build_function_call_expr (tree fndecl, tree arglist)
10621 tree fntype = TREE_TYPE (fndecl);
10622 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10623 int n = list_length (arglist);
10624 tree *argarray = (tree *) alloca (n * sizeof (tree));
10625 int i;
10627 for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist))
10628 argarray[i] = TREE_VALUE (arglist);
10629 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
10632 /* Conveniently construct a function call expression. FNDECL names the
10633 function to be called, N is the number of arguments, and the "..."
10634 parameters are the argument expressions. */
10636 tree
10637 build_call_expr (tree fndecl, int n, ...)
10639 va_list ap;
10640 tree fntype = TREE_TYPE (fndecl);
10641 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10642 tree *argarray = (tree *) alloca (n * sizeof (tree));
10643 int i;
10645 va_start (ap, n);
10646 for (i = 0; i < n; i++)
10647 argarray[i] = va_arg (ap, tree);
10648 va_end (ap);
10649 return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray);
10652 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10653 N arguments are passed in the array ARGARRAY. */
10655 tree
10656 fold_builtin_call_array (tree type,
10657 tree fn,
10658 int n,
10659 tree *argarray)
10661 tree ret = NULL_TREE;
10662 int i;
10663 tree exp;
10665 if (TREE_CODE (fn) == ADDR_EXPR)
10667 tree fndecl = TREE_OPERAND (fn, 0);
10668 if (TREE_CODE (fndecl) == FUNCTION_DECL
10669 && DECL_BUILT_IN (fndecl))
10671 /* If last argument is __builtin_va_arg_pack (), arguments to this
10672 function are not finalized yet. Defer folding until they are. */
10673 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10675 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10676 if (fndecl2
10677 && TREE_CODE (fndecl2) == FUNCTION_DECL
10678 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10679 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10680 return build_call_array (type, fn, n, argarray);
10682 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10684 tree arglist = NULL_TREE;
10685 for (i = n - 1; i >= 0; i--)
10686 arglist = tree_cons (NULL_TREE, argarray[i], arglist);
10687 ret = targetm.fold_builtin (fndecl, arglist, false);
10688 if (ret)
10689 return ret;
10691 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10693 /* First try the transformations that don't require consing up
10694 an exp. */
10695 ret = fold_builtin_n (fndecl, argarray, n, false);
10696 if (ret)
10697 return ret;
10700 /* If we got this far, we need to build an exp. */
10701 exp = build_call_array (type, fn, n, argarray);
10702 ret = fold_builtin_varargs (fndecl, exp, false);
10703 return ret ? ret : exp;
10707 return build_call_array (type, fn, n, argarray);
10710 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10711 along with N new arguments specified as the "..." parameters. SKIP
10712 is the number of arguments in EXP to be omitted. This function is used
10713 to do varargs-to-varargs transformations. */
10715 static tree
10716 rewrite_call_expr (tree exp, int skip, tree fndecl, int n, ...)
10718 int oldnargs = call_expr_nargs (exp);
10719 int nargs = oldnargs - skip + n;
10720 tree fntype = TREE_TYPE (fndecl);
10721 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10722 tree *buffer;
10724 if (n > 0)
10726 int i, j;
10727 va_list ap;
10729 buffer = alloca (nargs * sizeof (tree));
10730 va_start (ap, n);
10731 for (i = 0; i < n; i++)
10732 buffer[i] = va_arg (ap, tree);
10733 va_end (ap);
10734 for (j = skip; j < oldnargs; j++, i++)
10735 buffer[i] = CALL_EXPR_ARG (exp, j);
10737 else
10738 buffer = CALL_EXPR_ARGP (exp) + skip;
10740 return fold (build_call_array (TREE_TYPE (exp), fn, nargs, buffer));
10743 /* Validate a single argument ARG against a tree code CODE representing
10744 a type. */
10746 static bool
10747 validate_arg (const_tree arg, enum tree_code code)
10749 if (!arg)
10750 return false;
10751 else if (code == POINTER_TYPE)
10752 return POINTER_TYPE_P (TREE_TYPE (arg));
10753 else if (code == INTEGER_TYPE)
10754 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10755 return code == TREE_CODE (TREE_TYPE (arg));
10758 /* This function validates the types of a function call argument list
10759 against a specified list of tree_codes. If the last specifier is a 0,
10760 that represents an ellipses, otherwise the last specifier must be a
10761 VOID_TYPE. */
10763 bool
10764 validate_arglist (const_tree callexpr, ...)
10766 enum tree_code code;
10767 bool res = 0;
10768 va_list ap;
10769 const_call_expr_arg_iterator iter;
10770 const_tree arg;
10772 va_start (ap, callexpr);
10773 init_const_call_expr_arg_iterator (callexpr, &iter);
10777 code = va_arg (ap, enum tree_code);
10778 switch (code)
10780 case 0:
10781 /* This signifies an ellipses, any further arguments are all ok. */
10782 res = true;
10783 goto end;
10784 case VOID_TYPE:
10785 /* This signifies an endlink, if no arguments remain, return
10786 true, otherwise return false. */
10787 res = !more_const_call_expr_args_p (&iter);
10788 goto end;
10789 default:
10790 /* If no parameters remain or the parameter's code does not
10791 match the specified code, return false. Otherwise continue
10792 checking any remaining arguments. */
10793 arg = next_const_call_expr_arg (&iter);
10794 if (!validate_arg (arg, code))
10795 goto end;
10796 break;
10799 while (1);
10801 /* We need gotos here since we can only have one VA_CLOSE in a
10802 function. */
10803 end: ;
10804 va_end (ap);
10806 return res;
10809 /* Default target-specific builtin expander that does nothing. */
10812 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10813 rtx target ATTRIBUTE_UNUSED,
10814 rtx subtarget ATTRIBUTE_UNUSED,
10815 enum machine_mode mode ATTRIBUTE_UNUSED,
10816 int ignore ATTRIBUTE_UNUSED)
10818 return NULL_RTX;
10821 /* Returns true is EXP represents data that would potentially reside
10822 in a readonly section. */
10824 static bool
10825 readonly_data_expr (tree exp)
10827 STRIP_NOPS (exp);
10829 if (TREE_CODE (exp) != ADDR_EXPR)
10830 return false;
10832 exp = get_base_address (TREE_OPERAND (exp, 0));
10833 if (!exp)
10834 return false;
10836 /* Make sure we call decl_readonly_section only for trees it
10837 can handle (since it returns true for everything it doesn't
10838 understand). */
10839 if (TREE_CODE (exp) == STRING_CST
10840 || TREE_CODE (exp) == CONSTRUCTOR
10841 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10842 return decl_readonly_section (exp, 0);
10843 else
10844 return false;
10847 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10848 to the call, and TYPE is its return type.
10850 Return NULL_TREE if no simplification was possible, otherwise return the
10851 simplified form of the call as a tree.
10853 The simplified form may be a constant or other expression which
10854 computes the same value, but in a more efficient manner (including
10855 calls to other builtin functions).
10857 The call may contain arguments which need to be evaluated, but
10858 which are not useful to determine the result of the call. In
10859 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10860 COMPOUND_EXPR will be an argument which must be evaluated.
10861 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10862 COMPOUND_EXPR in the chain will contain the tree for the simplified
10863 form of the builtin function call. */
10865 static tree
10866 fold_builtin_strstr (tree s1, tree s2, tree type)
10868 if (!validate_arg (s1, POINTER_TYPE)
10869 || !validate_arg (s2, POINTER_TYPE))
10870 return NULL_TREE;
10871 else
10873 tree fn;
10874 const char *p1, *p2;
10876 p2 = c_getstr (s2);
10877 if (p2 == NULL)
10878 return NULL_TREE;
10880 p1 = c_getstr (s1);
10881 if (p1 != NULL)
10883 const char *r = strstr (p1, p2);
10884 tree tem;
10886 if (r == NULL)
10887 return build_int_cst (TREE_TYPE (s1), 0);
10889 /* Return an offset into the constant string argument. */
10890 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
10891 s1, size_int (r - p1));
10892 return fold_convert (type, tem);
10895 /* The argument is const char *, and the result is char *, so we need
10896 a type conversion here to avoid a warning. */
10897 if (p2[0] == '\0')
10898 return fold_convert (type, s1);
10900 if (p2[1] != '\0')
10901 return NULL_TREE;
10903 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10904 if (!fn)
10905 return NULL_TREE;
10907 /* New argument list transforming strstr(s1, s2) to
10908 strchr(s1, s2[0]). */
10909 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
10913 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10914 the call, and TYPE is its return type.
10916 Return NULL_TREE if no simplification was possible, otherwise return the
10917 simplified form of the call as a tree.
10919 The simplified form may be a constant or other expression which
10920 computes the same value, but in a more efficient manner (including
10921 calls to other builtin functions).
10923 The call may contain arguments which need to be evaluated, but
10924 which are not useful to determine the result of the call. In
10925 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10926 COMPOUND_EXPR will be an argument which must be evaluated.
10927 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10928 COMPOUND_EXPR in the chain will contain the tree for the simplified
10929 form of the builtin function call. */
10931 static tree
10932 fold_builtin_strchr (tree s1, tree s2, tree type)
10934 if (!validate_arg (s1, POINTER_TYPE)
10935 || !validate_arg (s2, INTEGER_TYPE))
10936 return NULL_TREE;
10937 else
10939 const char *p1;
10941 if (TREE_CODE (s2) != INTEGER_CST)
10942 return NULL_TREE;
10944 p1 = c_getstr (s1);
10945 if (p1 != NULL)
10947 char c;
10948 const char *r;
10949 tree tem;
10951 if (target_char_cast (s2, &c))
10952 return NULL_TREE;
10954 r = strchr (p1, c);
10956 if (r == NULL)
10957 return build_int_cst (TREE_TYPE (s1), 0);
10959 /* Return an offset into the constant string argument. */
10960 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
10961 s1, size_int (r - p1));
10962 return fold_convert (type, tem);
10964 return NULL_TREE;
10968 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10969 the call, and TYPE is its return type.
10971 Return NULL_TREE if no simplification was possible, otherwise return the
10972 simplified form of the call as a tree.
10974 The simplified form may be a constant or other expression which
10975 computes the same value, but in a more efficient manner (including
10976 calls to other builtin functions).
10978 The call may contain arguments which need to be evaluated, but
10979 which are not useful to determine the result of the call. In
10980 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10981 COMPOUND_EXPR will be an argument which must be evaluated.
10982 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10983 COMPOUND_EXPR in the chain will contain the tree for the simplified
10984 form of the builtin function call. */
10986 static tree
10987 fold_builtin_strrchr (tree s1, tree s2, tree type)
10989 if (!validate_arg (s1, POINTER_TYPE)
10990 || !validate_arg (s2, INTEGER_TYPE))
10991 return NULL_TREE;
10992 else
10994 tree fn;
10995 const char *p1;
10997 if (TREE_CODE (s2) != INTEGER_CST)
10998 return NULL_TREE;
11000 p1 = c_getstr (s1);
11001 if (p1 != NULL)
11003 char c;
11004 const char *r;
11005 tree tem;
11007 if (target_char_cast (s2, &c))
11008 return NULL_TREE;
11010 r = strrchr (p1, c);
11012 if (r == NULL)
11013 return build_int_cst (TREE_TYPE (s1), 0);
11015 /* Return an offset into the constant string argument. */
11016 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11017 s1, size_int (r - p1));
11018 return fold_convert (type, tem);
11021 if (! integer_zerop (s2))
11022 return NULL_TREE;
11024 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11025 if (!fn)
11026 return NULL_TREE;
11028 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11029 return build_call_expr (fn, 2, s1, s2);
11033 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11034 to the call, and TYPE is its return type.
11036 Return NULL_TREE if no simplification was possible, otherwise return the
11037 simplified form of the call as a tree.
11039 The simplified form may be a constant or other expression which
11040 computes the same value, but in a more efficient manner (including
11041 calls to other builtin functions).
11043 The call may contain arguments which need to be evaluated, but
11044 which are not useful to determine the result of the call. In
11045 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11046 COMPOUND_EXPR will be an argument which must be evaluated.
11047 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11048 COMPOUND_EXPR in the chain will contain the tree for the simplified
11049 form of the builtin function call. */
11051 static tree
11052 fold_builtin_strpbrk (tree s1, tree s2, tree type)
11054 if (!validate_arg (s1, POINTER_TYPE)
11055 || !validate_arg (s2, POINTER_TYPE))
11056 return NULL_TREE;
11057 else
11059 tree fn;
11060 const char *p1, *p2;
11062 p2 = c_getstr (s2);
11063 if (p2 == NULL)
11064 return NULL_TREE;
11066 p1 = c_getstr (s1);
11067 if (p1 != NULL)
11069 const char *r = strpbrk (p1, p2);
11070 tree tem;
11072 if (r == NULL)
11073 return build_int_cst (TREE_TYPE (s1), 0);
11075 /* Return an offset into the constant string argument. */
11076 tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1),
11077 s1, size_int (r - p1));
11078 return fold_convert (type, tem);
11081 if (p2[0] == '\0')
11082 /* strpbrk(x, "") == NULL.
11083 Evaluate and ignore s1 in case it had side-effects. */
11084 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
11086 if (p2[1] != '\0')
11087 return NULL_TREE; /* Really call strpbrk. */
11089 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11090 if (!fn)
11091 return NULL_TREE;
11093 /* New argument list transforming strpbrk(s1, s2) to
11094 strchr(s1, s2[0]). */
11095 return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0]));
11099 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11100 to the call.
11102 Return NULL_TREE if no simplification was possible, otherwise return the
11103 simplified form of the call as a tree.
11105 The simplified form may be a constant or other expression which
11106 computes the same value, but in a more efficient manner (including
11107 calls to other builtin functions).
11109 The call may contain arguments which need to be evaluated, but
11110 which are not useful to determine the result of the call. In
11111 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11112 COMPOUND_EXPR will be an argument which must be evaluated.
11113 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11114 COMPOUND_EXPR in the chain will contain the tree for the simplified
11115 form of the builtin function call. */
11117 static tree
11118 fold_builtin_strcat (tree dst, tree src)
11120 if (!validate_arg (dst, POINTER_TYPE)
11121 || !validate_arg (src, POINTER_TYPE))
11122 return NULL_TREE;
11123 else
11125 const char *p = c_getstr (src);
11127 /* If the string length is zero, return the dst parameter. */
11128 if (p && *p == '\0')
11129 return dst;
11131 return NULL_TREE;
11135 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11136 arguments to the call.
11138 Return NULL_TREE if no simplification was possible, otherwise return the
11139 simplified form of the call as a tree.
11141 The simplified form may be a constant or other expression which
11142 computes the same value, but in a more efficient manner (including
11143 calls to other builtin functions).
11145 The call may contain arguments which need to be evaluated, but
11146 which are not useful to determine the result of the call. In
11147 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11148 COMPOUND_EXPR will be an argument which must be evaluated.
11149 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11150 COMPOUND_EXPR in the chain will contain the tree for the simplified
11151 form of the builtin function call. */
11153 static tree
11154 fold_builtin_strncat (tree dst, tree src, tree len)
11156 if (!validate_arg (dst, POINTER_TYPE)
11157 || !validate_arg (src, POINTER_TYPE)
11158 || !validate_arg (len, INTEGER_TYPE))
11159 return NULL_TREE;
11160 else
11162 const char *p = c_getstr (src);
11164 /* If the requested length is zero, or the src parameter string
11165 length is zero, return the dst parameter. */
11166 if (integer_zerop (len) || (p && *p == '\0'))
11167 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
11169 /* If the requested len is greater than or equal to the string
11170 length, call strcat. */
11171 if (TREE_CODE (len) == INTEGER_CST && p
11172 && compare_tree_int (len, strlen (p)) >= 0)
11174 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11176 /* If the replacement _DECL isn't initialized, don't do the
11177 transformation. */
11178 if (!fn)
11179 return NULL_TREE;
11181 return build_call_expr (fn, 2, dst, src);
11183 return NULL_TREE;
11187 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11188 to the call.
11190 Return NULL_TREE if no simplification was possible, otherwise return the
11191 simplified form of the call as a tree.
11193 The simplified form may be a constant or other expression which
11194 computes the same value, but in a more efficient manner (including
11195 calls to other builtin functions).
11197 The call may contain arguments which need to be evaluated, but
11198 which are not useful to determine the result of the call. In
11199 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11200 COMPOUND_EXPR will be an argument which must be evaluated.
11201 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11202 COMPOUND_EXPR in the chain will contain the tree for the simplified
11203 form of the builtin function call. */
11205 static tree
11206 fold_builtin_strspn (tree s1, tree s2)
11208 if (!validate_arg (s1, POINTER_TYPE)
11209 || !validate_arg (s2, POINTER_TYPE))
11210 return NULL_TREE;
11211 else
11213 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11215 /* If both arguments are constants, evaluate at compile-time. */
11216 if (p1 && p2)
11218 const size_t r = strspn (p1, p2);
11219 return size_int (r);
11222 /* If either argument is "", return NULL_TREE. */
11223 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11224 /* Evaluate and ignore both arguments in case either one has
11225 side-effects. */
11226 return omit_two_operands (integer_type_node, integer_zero_node,
11227 s1, s2);
11228 return NULL_TREE;
11232 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11233 to the call.
11235 Return NULL_TREE if no simplification was possible, otherwise return the
11236 simplified form of the call as a tree.
11238 The simplified form may be a constant or other expression which
11239 computes the same value, but in a more efficient manner (including
11240 calls to other builtin functions).
11242 The call may contain arguments which need to be evaluated, but
11243 which are not useful to determine the result of the call. In
11244 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11245 COMPOUND_EXPR will be an argument which must be evaluated.
11246 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11247 COMPOUND_EXPR in the chain will contain the tree for the simplified
11248 form of the builtin function call. */
11250 static tree
11251 fold_builtin_strcspn (tree s1, tree s2)
11253 if (!validate_arg (s1, POINTER_TYPE)
11254 || !validate_arg (s2, POINTER_TYPE))
11255 return NULL_TREE;
11256 else
11258 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11260 /* If both arguments are constants, evaluate at compile-time. */
11261 if (p1 && p2)
11263 const size_t r = strcspn (p1, p2);
11264 return size_int (r);
11267 /* If the first argument is "", return NULL_TREE. */
11268 if (p1 && *p1 == '\0')
11270 /* Evaluate and ignore argument s2 in case it has
11271 side-effects. */
11272 return omit_one_operand (integer_type_node,
11273 integer_zero_node, s2);
11276 /* If the second argument is "", return __builtin_strlen(s1). */
11277 if (p2 && *p2 == '\0')
11279 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11281 /* If the replacement _DECL isn't initialized, don't do the
11282 transformation. */
11283 if (!fn)
11284 return NULL_TREE;
11286 return build_call_expr (fn, 1, s1);
11288 return NULL_TREE;
11292 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11293 to the call. IGNORE is true if the value returned
11294 by the builtin will be ignored. UNLOCKED is true is true if this
11295 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11296 the known length of the string. Return NULL_TREE if no simplification
11297 was possible. */
11299 tree
11300 fold_builtin_fputs (tree arg0, tree arg1, bool ignore, bool unlocked, tree len)
11302 /* If we're using an unlocked function, assume the other unlocked
11303 functions exist explicitly. */
11304 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11305 : implicit_built_in_decls[BUILT_IN_FPUTC];
11306 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11307 : implicit_built_in_decls[BUILT_IN_FWRITE];
11309 /* If the return value is used, don't do the transformation. */
11310 if (!ignore)
11311 return NULL_TREE;
11313 /* Verify the arguments in the original call. */
11314 if (!validate_arg (arg0, POINTER_TYPE)
11315 || !validate_arg (arg1, POINTER_TYPE))
11316 return NULL_TREE;
11318 if (! len)
11319 len = c_strlen (arg0, 0);
11321 /* Get the length of the string passed to fputs. If the length
11322 can't be determined, punt. */
11323 if (!len
11324 || TREE_CODE (len) != INTEGER_CST)
11325 return NULL_TREE;
11327 switch (compare_tree_int (len, 1))
11329 case -1: /* length is 0, delete the call entirely . */
11330 return omit_one_operand (integer_type_node, integer_zero_node, arg1);;
11332 case 0: /* length is 1, call fputc. */
11334 const char *p = c_getstr (arg0);
11336 if (p != NULL)
11338 if (fn_fputc)
11339 return build_call_expr (fn_fputc, 2,
11340 build_int_cst (NULL_TREE, p[0]), arg1);
11341 else
11342 return NULL_TREE;
11345 /* FALLTHROUGH */
11346 case 1: /* length is greater than 1, call fwrite. */
11348 /* If optimizing for size keep fputs. */
11349 if (optimize_size)
11350 return NULL_TREE;
11351 /* New argument list transforming fputs(string, stream) to
11352 fwrite(string, 1, len, stream). */
11353 if (fn_fwrite)
11354 return build_call_expr (fn_fwrite, 4, arg0, size_one_node, len, arg1);
11355 else
11356 return NULL_TREE;
11358 default:
11359 gcc_unreachable ();
11361 return NULL_TREE;
11364 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11365 produced. False otherwise. This is done so that we don't output the error
11366 or warning twice or three times. */
11367 bool
11368 fold_builtin_next_arg (tree exp, bool va_start_p)
11370 tree fntype = TREE_TYPE (current_function_decl);
11371 int nargs = call_expr_nargs (exp);
11372 tree arg;
11374 if (TYPE_ARG_TYPES (fntype) == 0
11375 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
11376 == void_type_node))
11378 error ("%<va_start%> used in function with fixed args");
11379 return true;
11382 if (va_start_p)
11384 if (va_start_p && (nargs != 2))
11386 error ("wrong number of arguments to function %<va_start%>");
11387 return true;
11389 arg = CALL_EXPR_ARG (exp, 1);
11391 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11392 when we checked the arguments and if needed issued a warning. */
11393 else
11395 if (nargs == 0)
11397 /* Evidently an out of date version of <stdarg.h>; can't validate
11398 va_start's second argument, but can still work as intended. */
11399 warning (0, "%<__builtin_next_arg%> called without an argument");
11400 return true;
11402 else if (nargs > 1)
11404 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11405 return true;
11407 arg = CALL_EXPR_ARG (exp, 0);
11410 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11411 or __builtin_next_arg (0) the first time we see it, after checking
11412 the arguments and if needed issuing a warning. */
11413 if (!integer_zerop (arg))
11415 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11417 /* Strip off all nops for the sake of the comparison. This
11418 is not quite the same as STRIP_NOPS. It does more.
11419 We must also strip off INDIRECT_EXPR for C++ reference
11420 parameters. */
11421 while (TREE_CODE (arg) == NOP_EXPR
11422 || TREE_CODE (arg) == CONVERT_EXPR
11423 || TREE_CODE (arg) == NON_LVALUE_EXPR
11424 || TREE_CODE (arg) == INDIRECT_REF)
11425 arg = TREE_OPERAND (arg, 0);
11426 if (arg != last_parm)
11428 /* FIXME: Sometimes with the tree optimizers we can get the
11429 not the last argument even though the user used the last
11430 argument. We just warn and set the arg to be the last
11431 argument so that we will get wrong-code because of
11432 it. */
11433 warning (0, "second parameter of %<va_start%> not last named argument");
11435 /* We want to verify the second parameter just once before the tree
11436 optimizers are run and then avoid keeping it in the tree,
11437 as otherwise we could warn even for correct code like:
11438 void foo (int i, ...)
11439 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11440 if (va_start_p)
11441 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11442 else
11443 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11445 return false;
11449 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11450 ORIG may be null if this is a 2-argument call. We don't attempt to
11451 simplify calls with more than 3 arguments.
11453 Return NULL_TREE if no simplification was possible, otherwise return the
11454 simplified form of the call as a tree. If IGNORED is true, it means that
11455 the caller does not use the returned value of the function. */
11457 static tree
11458 fold_builtin_sprintf (tree dest, tree fmt, tree orig, int ignored)
11460 tree call, retval;
11461 const char *fmt_str = NULL;
11463 /* Verify the required arguments in the original call. We deal with two
11464 types of sprintf() calls: 'sprintf (str, fmt)' and
11465 'sprintf (dest, "%s", orig)'. */
11466 if (!validate_arg (dest, POINTER_TYPE)
11467 || !validate_arg (fmt, POINTER_TYPE))
11468 return NULL_TREE;
11469 if (orig && !validate_arg (orig, POINTER_TYPE))
11470 return NULL_TREE;
11472 /* Check whether the format is a literal string constant. */
11473 fmt_str = c_getstr (fmt);
11474 if (fmt_str == NULL)
11475 return NULL_TREE;
11477 call = NULL_TREE;
11478 retval = NULL_TREE;
11480 if (!init_target_chars ())
11481 return NULL_TREE;
11483 /* If the format doesn't contain % args or %%, use strcpy. */
11484 if (strchr (fmt_str, target_percent) == NULL)
11486 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11488 if (!fn)
11489 return NULL_TREE;
11491 /* Don't optimize sprintf (buf, "abc", ptr++). */
11492 if (orig)
11493 return NULL_TREE;
11495 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11496 'format' is known to contain no % formats. */
11497 call = build_call_expr (fn, 2, dest, fmt);
11498 if (!ignored)
11499 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
11502 /* If the format is "%s", use strcpy if the result isn't used. */
11503 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11505 tree fn;
11506 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11508 if (!fn)
11509 return NULL_TREE;
11511 /* Don't crash on sprintf (str1, "%s"). */
11512 if (!orig)
11513 return NULL_TREE;
11515 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11516 if (!ignored)
11518 retval = c_strlen (orig, 1);
11519 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11520 return NULL_TREE;
11522 call = build_call_expr (fn, 2, dest, orig);
11525 if (call && retval)
11527 retval = fold_convert
11528 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11529 retval);
11530 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11532 else
11533 return call;
11536 /* Expand a call EXP to __builtin_object_size. */
11539 expand_builtin_object_size (tree exp)
11541 tree ost;
11542 int object_size_type;
11543 tree fndecl = get_callee_fndecl (exp);
11545 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11547 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11548 exp, fndecl);
11549 expand_builtin_trap ();
11550 return const0_rtx;
11553 ost = CALL_EXPR_ARG (exp, 1);
11554 STRIP_NOPS (ost);
11556 if (TREE_CODE (ost) != INTEGER_CST
11557 || tree_int_cst_sgn (ost) < 0
11558 || compare_tree_int (ost, 3) > 0)
11560 error ("%Klast argument of %D is not integer constant between 0 and 3",
11561 exp, fndecl);
11562 expand_builtin_trap ();
11563 return const0_rtx;
11566 object_size_type = tree_low_cst (ost, 0);
11568 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11571 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11572 FCODE is the BUILT_IN_* to use.
11573 Return NULL_RTX if we failed; the caller should emit a normal call,
11574 otherwise try to get the result in TARGET, if convenient (and in
11575 mode MODE if that's convenient). */
11577 static rtx
11578 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11579 enum built_in_function fcode)
11581 tree dest, src, len, size;
11583 if (!validate_arglist (exp,
11584 POINTER_TYPE,
11585 fcode == BUILT_IN_MEMSET_CHK
11586 ? INTEGER_TYPE : POINTER_TYPE,
11587 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11588 return NULL_RTX;
11590 dest = CALL_EXPR_ARG (exp, 0);
11591 src = CALL_EXPR_ARG (exp, 1);
11592 len = CALL_EXPR_ARG (exp, 2);
11593 size = CALL_EXPR_ARG (exp, 3);
11595 if (! host_integerp (size, 1))
11596 return NULL_RTX;
11598 if (host_integerp (len, 1) || integer_all_onesp (size))
11600 tree fn;
11602 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11604 warning (0, "%Kcall to %D will always overflow destination buffer",
11605 exp, get_callee_fndecl (exp));
11606 return NULL_RTX;
11609 fn = NULL_TREE;
11610 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11611 mem{cpy,pcpy,move,set} is available. */
11612 switch (fcode)
11614 case BUILT_IN_MEMCPY_CHK:
11615 fn = built_in_decls[BUILT_IN_MEMCPY];
11616 break;
11617 case BUILT_IN_MEMPCPY_CHK:
11618 fn = built_in_decls[BUILT_IN_MEMPCPY];
11619 break;
11620 case BUILT_IN_MEMMOVE_CHK:
11621 fn = built_in_decls[BUILT_IN_MEMMOVE];
11622 break;
11623 case BUILT_IN_MEMSET_CHK:
11624 fn = built_in_decls[BUILT_IN_MEMSET];
11625 break;
11626 default:
11627 break;
11630 if (! fn)
11631 return NULL_RTX;
11633 fn = build_call_expr (fn, 3, dest, src, len);
11634 STRIP_TYPE_NOPS (fn);
11635 while (TREE_CODE (fn) == COMPOUND_EXPR)
11637 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
11638 EXPAND_NORMAL);
11639 fn = TREE_OPERAND (fn, 1);
11641 if (TREE_CODE (fn) == CALL_EXPR)
11642 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11643 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11645 else if (fcode == BUILT_IN_MEMSET_CHK)
11646 return NULL_RTX;
11647 else
11649 unsigned int dest_align
11650 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
11652 /* If DEST is not a pointer type, call the normal function. */
11653 if (dest_align == 0)
11654 return NULL_RTX;
11656 /* If SRC and DEST are the same (and not volatile), do nothing. */
11657 if (operand_equal_p (src, dest, 0))
11659 tree expr;
11661 if (fcode != BUILT_IN_MEMPCPY_CHK)
11663 /* Evaluate and ignore LEN in case it has side-effects. */
11664 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11665 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11668 expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
11669 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11672 /* __memmove_chk special case. */
11673 if (fcode == BUILT_IN_MEMMOVE_CHK)
11675 unsigned int src_align
11676 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
11678 if (src_align == 0)
11679 return NULL_RTX;
11681 /* If src is categorized for a readonly section we can use
11682 normal __memcpy_chk. */
11683 if (readonly_data_expr (src))
11685 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11686 if (!fn)
11687 return NULL_RTX;
11688 fn = build_call_expr (fn, 4, dest, src, len, size);
11689 STRIP_TYPE_NOPS (fn);
11690 while (TREE_CODE (fn) == COMPOUND_EXPR)
11692 expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
11693 EXPAND_NORMAL);
11694 fn = TREE_OPERAND (fn, 1);
11696 if (TREE_CODE (fn) == CALL_EXPR)
11697 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11698 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11701 return NULL_RTX;
11705 /* Emit warning if a buffer overflow is detected at compile time. */
11707 static void
11708 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11710 int is_strlen = 0;
11711 tree len, size;
11713 switch (fcode)
11715 case BUILT_IN_STRCPY_CHK:
11716 case BUILT_IN_STPCPY_CHK:
11717 /* For __strcat_chk the warning will be emitted only if overflowing
11718 by at least strlen (dest) + 1 bytes. */
11719 case BUILT_IN_STRCAT_CHK:
11720 len = CALL_EXPR_ARG (exp, 1);
11721 size = CALL_EXPR_ARG (exp, 2);
11722 is_strlen = 1;
11723 break;
11724 case BUILT_IN_STRNCAT_CHK:
11725 case BUILT_IN_STRNCPY_CHK:
11726 len = CALL_EXPR_ARG (exp, 2);
11727 size = CALL_EXPR_ARG (exp, 3);
11728 break;
11729 case BUILT_IN_SNPRINTF_CHK:
11730 case BUILT_IN_VSNPRINTF_CHK:
11731 len = CALL_EXPR_ARG (exp, 1);
11732 size = CALL_EXPR_ARG (exp, 3);
11733 break;
11734 default:
11735 gcc_unreachable ();
11738 if (!len || !size)
11739 return;
11741 if (! host_integerp (size, 1) || integer_all_onesp (size))
11742 return;
11744 if (is_strlen)
11746 len = c_strlen (len, 1);
11747 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11748 return;
11750 else if (fcode == BUILT_IN_STRNCAT_CHK)
11752 tree src = CALL_EXPR_ARG (exp, 1);
11753 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11754 return;
11755 src = c_strlen (src, 1);
11756 if (! src || ! host_integerp (src, 1))
11758 warning (0, "%Kcall to %D might overflow destination buffer",
11759 exp, get_callee_fndecl (exp));
11760 return;
11762 else if (tree_int_cst_lt (src, size))
11763 return;
11765 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
11766 return;
11768 warning (0, "%Kcall to %D will always overflow destination buffer",
11769 exp, get_callee_fndecl (exp));
11772 /* Emit warning if a buffer overflow is detected at compile time
11773 in __sprintf_chk/__vsprintf_chk calls. */
11775 static void
11776 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11778 tree dest, size, len, fmt, flag;
11779 const char *fmt_str;
11780 int nargs = call_expr_nargs (exp);
11782 /* Verify the required arguments in the original call. */
11784 if (nargs < 4)
11785 return;
11786 dest = CALL_EXPR_ARG (exp, 0);
11787 flag = CALL_EXPR_ARG (exp, 1);
11788 size = CALL_EXPR_ARG (exp, 2);
11789 fmt = CALL_EXPR_ARG (exp, 3);
11791 if (! host_integerp (size, 1) || integer_all_onesp (size))
11792 return;
11794 /* Check whether the format is a literal string constant. */
11795 fmt_str = c_getstr (fmt);
11796 if (fmt_str == NULL)
11797 return;
11799 if (!init_target_chars ())
11800 return;
11802 /* If the format doesn't contain % args or %%, we know its size. */
11803 if (strchr (fmt_str, target_percent) == 0)
11804 len = build_int_cstu (size_type_node, strlen (fmt_str));
11805 /* If the format is "%s" and first ... argument is a string literal,
11806 we know it too. */
11807 else if (fcode == BUILT_IN_SPRINTF_CHK
11808 && strcmp (fmt_str, target_percent_s) == 0)
11810 tree arg;
11812 if (nargs < 5)
11813 return;
11814 arg = CALL_EXPR_ARG (exp, 4);
11815 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11816 return;
11818 len = c_strlen (arg, 1);
11819 if (!len || ! host_integerp (len, 1))
11820 return;
11822 else
11823 return;
11825 if (! tree_int_cst_lt (len, size))
11827 warning (0, "%Kcall to %D will always overflow destination buffer",
11828 exp, get_callee_fndecl (exp));
11832 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11833 if possible. */
11835 tree
11836 fold_builtin_object_size (tree ptr, tree ost)
11838 tree ret = NULL_TREE;
11839 int object_size_type;
11841 if (!validate_arg (ptr, POINTER_TYPE)
11842 || !validate_arg (ost, INTEGER_TYPE))
11843 return NULL_TREE;
11845 STRIP_NOPS (ost);
11847 if (TREE_CODE (ost) != INTEGER_CST
11848 || tree_int_cst_sgn (ost) < 0
11849 || compare_tree_int (ost, 3) > 0)
11850 return NULL_TREE;
11852 object_size_type = tree_low_cst (ost, 0);
11854 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11855 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11856 and (size_t) 0 for types 2 and 3. */
11857 if (TREE_SIDE_EFFECTS (ptr))
11858 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11860 if (TREE_CODE (ptr) == ADDR_EXPR)
11861 ret = build_int_cstu (size_type_node,
11862 compute_builtin_object_size (ptr, object_size_type));
11864 else if (TREE_CODE (ptr) == SSA_NAME)
11866 unsigned HOST_WIDE_INT bytes;
11868 /* If object size is not known yet, delay folding until
11869 later. Maybe subsequent passes will help determining
11870 it. */
11871 bytes = compute_builtin_object_size (ptr, object_size_type);
11872 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
11873 ? -1 : 0))
11874 ret = build_int_cstu (size_type_node, bytes);
11877 if (ret)
11879 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
11880 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
11881 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
11882 ret = NULL_TREE;
11885 return ret;
11888 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11889 DEST, SRC, LEN, and SIZE are the arguments to the call.
11890 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
11891 code of the builtin. If MAXLEN is not NULL, it is maximum length
11892 passed as third argument. */
11894 tree
11895 fold_builtin_memory_chk (tree fndecl,
11896 tree dest, tree src, tree len, tree size,
11897 tree maxlen, bool ignore,
11898 enum built_in_function fcode)
11900 tree fn;
11902 if (!validate_arg (dest, POINTER_TYPE)
11903 || !validate_arg (src,
11904 (fcode == BUILT_IN_MEMSET_CHK
11905 ? INTEGER_TYPE : POINTER_TYPE))
11906 || !validate_arg (len, INTEGER_TYPE)
11907 || !validate_arg (size, INTEGER_TYPE))
11908 return NULL_TREE;
11910 /* If SRC and DEST are the same (and not volatile), return DEST
11911 (resp. DEST+LEN for __mempcpy_chk). */
11912 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
11914 if (fcode != BUILT_IN_MEMPCPY_CHK)
11915 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
11916 else
11918 tree temp = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len);
11919 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
11923 if (! host_integerp (size, 1))
11924 return NULL_TREE;
11926 if (! integer_all_onesp (size))
11928 if (! host_integerp (len, 1))
11930 /* If LEN is not constant, try MAXLEN too.
11931 For MAXLEN only allow optimizing into non-_ocs function
11932 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11933 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11935 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
11937 /* (void) __mempcpy_chk () can be optimized into
11938 (void) __memcpy_chk (). */
11939 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11940 if (!fn)
11941 return NULL_TREE;
11943 return build_call_expr (fn, 4, dest, src, len, size);
11945 return NULL_TREE;
11948 else
11949 maxlen = len;
11951 if (tree_int_cst_lt (size, maxlen))
11952 return NULL_TREE;
11955 fn = NULL_TREE;
11956 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11957 mem{cpy,pcpy,move,set} is available. */
11958 switch (fcode)
11960 case BUILT_IN_MEMCPY_CHK:
11961 fn = built_in_decls[BUILT_IN_MEMCPY];
11962 break;
11963 case BUILT_IN_MEMPCPY_CHK:
11964 fn = built_in_decls[BUILT_IN_MEMPCPY];
11965 break;
11966 case BUILT_IN_MEMMOVE_CHK:
11967 fn = built_in_decls[BUILT_IN_MEMMOVE];
11968 break;
11969 case BUILT_IN_MEMSET_CHK:
11970 fn = built_in_decls[BUILT_IN_MEMSET];
11971 break;
11972 default:
11973 break;
11976 if (!fn)
11977 return NULL_TREE;
11979 return build_call_expr (fn, 3, dest, src, len);
11982 /* Fold a call to the __st[rp]cpy_chk builtin.
11983 DEST, SRC, and SIZE are the arguments to the call.
11984 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
11985 code of the builtin. If MAXLEN is not NULL, it is maximum length of
11986 strings passed as second argument. */
11988 tree
11989 fold_builtin_stxcpy_chk (tree fndecl, tree dest, tree src, tree size,
11990 tree maxlen, bool ignore,
11991 enum built_in_function fcode)
11993 tree len, fn;
11995 if (!validate_arg (dest, POINTER_TYPE)
11996 || !validate_arg (src, POINTER_TYPE)
11997 || !validate_arg (size, INTEGER_TYPE))
11998 return NULL_TREE;
12000 /* If SRC and DEST are the same (and not volatile), return DEST. */
12001 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12002 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
12004 if (! host_integerp (size, 1))
12005 return NULL_TREE;
12007 if (! integer_all_onesp (size))
12009 len = c_strlen (src, 1);
12010 if (! len || ! host_integerp (len, 1))
12012 /* If LEN is not constant, try MAXLEN too.
12013 For MAXLEN only allow optimizing into non-_ocs function
12014 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12015 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12017 if (fcode == BUILT_IN_STPCPY_CHK)
12019 if (! ignore)
12020 return NULL_TREE;
12022 /* If return value of __stpcpy_chk is ignored,
12023 optimize into __strcpy_chk. */
12024 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12025 if (!fn)
12026 return NULL_TREE;
12028 return build_call_expr (fn, 3, dest, src, size);
12031 if (! len || TREE_SIDE_EFFECTS (len))
12032 return NULL_TREE;
12034 /* If c_strlen returned something, but not a constant,
12035 transform __strcpy_chk into __memcpy_chk. */
12036 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12037 if (!fn)
12038 return NULL_TREE;
12040 len = size_binop (PLUS_EXPR, len, ssize_int (1));
12041 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
12042 build_call_expr (fn, 4,
12043 dest, src, len, size));
12046 else
12047 maxlen = len;
12049 if (! tree_int_cst_lt (maxlen, size))
12050 return NULL_TREE;
12053 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12054 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12055 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12056 if (!fn)
12057 return NULL_TREE;
12059 return build_call_expr (fn, 2, dest, src);
12062 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12063 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12064 length passed as third argument. */
12066 tree
12067 fold_builtin_strncpy_chk (tree dest, tree src, tree len, tree size,
12068 tree maxlen)
12070 tree fn;
12072 if (!validate_arg (dest, POINTER_TYPE)
12073 || !validate_arg (src, POINTER_TYPE)
12074 || !validate_arg (len, INTEGER_TYPE)
12075 || !validate_arg (size, INTEGER_TYPE))
12076 return NULL_TREE;
12078 if (! host_integerp (size, 1))
12079 return NULL_TREE;
12081 if (! integer_all_onesp (size))
12083 if (! host_integerp (len, 1))
12085 /* If LEN is not constant, try MAXLEN too.
12086 For MAXLEN only allow optimizing into non-_ocs function
12087 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12088 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12089 return NULL_TREE;
12091 else
12092 maxlen = len;
12094 if (tree_int_cst_lt (size, maxlen))
12095 return NULL_TREE;
12098 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12099 fn = built_in_decls[BUILT_IN_STRNCPY];
12100 if (!fn)
12101 return NULL_TREE;
12103 return build_call_expr (fn, 3, dest, src, len);
12106 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12107 are the arguments to the call. */
12109 static tree
12110 fold_builtin_strcat_chk (tree fndecl, tree dest, tree src, tree size)
12112 tree fn;
12113 const char *p;
12115 if (!validate_arg (dest, POINTER_TYPE)
12116 || !validate_arg (src, POINTER_TYPE)
12117 || !validate_arg (size, INTEGER_TYPE))
12118 return NULL_TREE;
12120 p = c_getstr (src);
12121 /* If the SRC parameter is "", return DEST. */
12122 if (p && *p == '\0')
12123 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12125 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12126 return NULL_TREE;
12128 /* If __builtin_strcat_chk is used, assume strcat is available. */
12129 fn = built_in_decls[BUILT_IN_STRCAT];
12130 if (!fn)
12131 return NULL_TREE;
12133 return build_call_expr (fn, 2, dest, src);
12136 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12137 LEN, and SIZE. */
12139 static tree
12140 fold_builtin_strncat_chk (tree fndecl,
12141 tree dest, tree src, tree len, tree size)
12143 tree fn;
12144 const char *p;
12146 if (!validate_arg (dest, POINTER_TYPE)
12147 || !validate_arg (src, POINTER_TYPE)
12148 || !validate_arg (size, INTEGER_TYPE)
12149 || !validate_arg (size, INTEGER_TYPE))
12150 return NULL_TREE;
12152 p = c_getstr (src);
12153 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12154 if (p && *p == '\0')
12155 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12156 else if (integer_zerop (len))
12157 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12159 if (! host_integerp (size, 1))
12160 return NULL_TREE;
12162 if (! integer_all_onesp (size))
12164 tree src_len = c_strlen (src, 1);
12165 if (src_len
12166 && host_integerp (src_len, 1)
12167 && host_integerp (len, 1)
12168 && ! tree_int_cst_lt (len, src_len))
12170 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12171 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12172 if (!fn)
12173 return NULL_TREE;
12175 return build_call_expr (fn, 3, dest, src, size);
12177 return NULL_TREE;
12180 /* If __builtin_strncat_chk is used, assume strncat is available. */
12181 fn = built_in_decls[BUILT_IN_STRNCAT];
12182 if (!fn)
12183 return NULL_TREE;
12185 return build_call_expr (fn, 3, dest, src, len);
12188 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12189 a normal call should be emitted rather than expanding the function
12190 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12192 static tree
12193 fold_builtin_sprintf_chk (tree exp, enum built_in_function fcode)
12195 tree dest, size, len, fn, fmt, flag;
12196 const char *fmt_str;
12197 int nargs = call_expr_nargs (exp);
12199 /* Verify the required arguments in the original call. */
12200 if (nargs < 4)
12201 return NULL_TREE;
12202 dest = CALL_EXPR_ARG (exp, 0);
12203 if (!validate_arg (dest, POINTER_TYPE))
12204 return NULL_TREE;
12205 flag = CALL_EXPR_ARG (exp, 1);
12206 if (!validate_arg (flag, INTEGER_TYPE))
12207 return NULL_TREE;
12208 size = CALL_EXPR_ARG (exp, 2);
12209 if (!validate_arg (size, INTEGER_TYPE))
12210 return NULL_TREE;
12211 fmt = CALL_EXPR_ARG (exp, 3);
12212 if (!validate_arg (fmt, POINTER_TYPE))
12213 return NULL_TREE;
12215 if (! host_integerp (size, 1))
12216 return NULL_TREE;
12218 len = NULL_TREE;
12220 if (!init_target_chars ())
12221 return NULL_TREE;
12223 /* Check whether the format is a literal string constant. */
12224 fmt_str = c_getstr (fmt);
12225 if (fmt_str != NULL)
12227 /* If the format doesn't contain % args or %%, we know the size. */
12228 if (strchr (fmt_str, target_percent) == 0)
12230 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12231 len = build_int_cstu (size_type_node, strlen (fmt_str));
12233 /* If the format is "%s" and first ... argument is a string literal,
12234 we know the size too. */
12235 else if (fcode == BUILT_IN_SPRINTF_CHK
12236 && strcmp (fmt_str, target_percent_s) == 0)
12238 tree arg;
12240 if (nargs == 5)
12242 arg = CALL_EXPR_ARG (exp, 4);
12243 if (validate_arg (arg, POINTER_TYPE))
12245 len = c_strlen (arg, 1);
12246 if (! len || ! host_integerp (len, 1))
12247 len = NULL_TREE;
12253 if (! integer_all_onesp (size))
12255 if (! len || ! tree_int_cst_lt (len, size))
12256 return NULL_TREE;
12259 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12260 or if format doesn't contain % chars or is "%s". */
12261 if (! integer_zerop (flag))
12263 if (fmt_str == NULL)
12264 return NULL_TREE;
12265 if (strchr (fmt_str, target_percent) != NULL
12266 && strcmp (fmt_str, target_percent_s))
12267 return NULL_TREE;
12270 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12271 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12272 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12273 if (!fn)
12274 return NULL_TREE;
12276 return rewrite_call_expr (exp, 4, fn, 2, dest, fmt);
12279 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12280 a normal call should be emitted rather than expanding the function
12281 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12282 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12283 passed as second argument. */
12285 tree
12286 fold_builtin_snprintf_chk (tree exp, tree maxlen,
12287 enum built_in_function fcode)
12289 tree dest, size, len, fn, fmt, flag;
12290 const char *fmt_str;
12292 /* Verify the required arguments in the original call. */
12293 if (call_expr_nargs (exp) < 5)
12294 return NULL_TREE;
12295 dest = CALL_EXPR_ARG (exp, 0);
12296 if (!validate_arg (dest, POINTER_TYPE))
12297 return NULL_TREE;
12298 len = CALL_EXPR_ARG (exp, 1);
12299 if (!validate_arg (len, INTEGER_TYPE))
12300 return NULL_TREE;
12301 flag = CALL_EXPR_ARG (exp, 2);
12302 if (!validate_arg (flag, INTEGER_TYPE))
12303 return NULL_TREE;
12304 size = CALL_EXPR_ARG (exp, 3);
12305 if (!validate_arg (size, INTEGER_TYPE))
12306 return NULL_TREE;
12307 fmt = CALL_EXPR_ARG (exp, 4);
12308 if (!validate_arg (fmt, POINTER_TYPE))
12309 return NULL_TREE;
12311 if (! host_integerp (size, 1))
12312 return NULL_TREE;
12314 if (! integer_all_onesp (size))
12316 if (! host_integerp (len, 1))
12318 /* If LEN is not constant, try MAXLEN too.
12319 For MAXLEN only allow optimizing into non-_ocs function
12320 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12321 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12322 return NULL_TREE;
12324 else
12325 maxlen = len;
12327 if (tree_int_cst_lt (size, maxlen))
12328 return NULL_TREE;
12331 if (!init_target_chars ())
12332 return NULL_TREE;
12334 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12335 or if format doesn't contain % chars or is "%s". */
12336 if (! integer_zerop (flag))
12338 fmt_str = c_getstr (fmt);
12339 if (fmt_str == NULL)
12340 return NULL_TREE;
12341 if (strchr (fmt_str, target_percent) != NULL
12342 && strcmp (fmt_str, target_percent_s))
12343 return NULL_TREE;
12346 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12347 available. */
12348 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12349 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12350 if (!fn)
12351 return NULL_TREE;
12353 return rewrite_call_expr (exp, 5, fn, 3, dest, len, fmt);
12356 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12357 FMT and ARG are the arguments to the call; we don't fold cases with
12358 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12360 Return NULL_TREE if no simplification was possible, otherwise return the
12361 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12362 code of the function to be simplified. */
12364 static tree
12365 fold_builtin_printf (tree fndecl, tree fmt, tree arg, bool ignore,
12366 enum built_in_function fcode)
12368 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12369 const char *fmt_str = NULL;
12371 /* If the return value is used, don't do the transformation. */
12372 if (! ignore)
12373 return NULL_TREE;
12375 /* Verify the required arguments in the original call. */
12376 if (!validate_arg (fmt, POINTER_TYPE))
12377 return NULL_TREE;
12379 /* Check whether the format is a literal string constant. */
12380 fmt_str = c_getstr (fmt);
12381 if (fmt_str == NULL)
12382 return NULL_TREE;
12384 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12386 /* If we're using an unlocked function, assume the other
12387 unlocked functions exist explicitly. */
12388 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12389 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12391 else
12393 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12394 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12397 if (!init_target_chars ())
12398 return NULL_TREE;
12400 if (strcmp (fmt_str, target_percent_s) == 0
12401 || strchr (fmt_str, target_percent) == NULL)
12403 const char *str;
12405 if (strcmp (fmt_str, target_percent_s) == 0)
12407 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12408 return NULL_TREE;
12410 if (!arg || !validate_arg (arg, POINTER_TYPE))
12411 return NULL_TREE;
12413 str = c_getstr (arg);
12414 if (str == NULL)
12415 return NULL_TREE;
12417 else
12419 /* The format specifier doesn't contain any '%' characters. */
12420 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12421 && arg)
12422 return NULL_TREE;
12423 str = fmt_str;
12426 /* If the string was "", printf does nothing. */
12427 if (str[0] == '\0')
12428 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12430 /* If the string has length of 1, call putchar. */
12431 if (str[1] == '\0')
12433 /* Given printf("c"), (where c is any one character,)
12434 convert "c"[0] to an int and pass that to the replacement
12435 function. */
12436 newarg = build_int_cst (NULL_TREE, str[0]);
12437 if (fn_putchar)
12438 call = build_call_expr (fn_putchar, 1, newarg);
12440 else
12442 /* If the string was "string\n", call puts("string"). */
12443 size_t len = strlen (str);
12444 if ((unsigned char)str[len - 1] == target_newline)
12446 /* Create a NUL-terminated string that's one char shorter
12447 than the original, stripping off the trailing '\n'. */
12448 char *newstr = alloca (len);
12449 memcpy (newstr, str, len - 1);
12450 newstr[len - 1] = 0;
12452 newarg = build_string_literal (len, newstr);
12453 if (fn_puts)
12454 call = build_call_expr (fn_puts, 1, newarg);
12456 else
12457 /* We'd like to arrange to call fputs(string,stdout) here,
12458 but we need stdout and don't have a way to get it yet. */
12459 return NULL_TREE;
12463 /* The other optimizations can be done only on the non-va_list variants. */
12464 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12465 return NULL_TREE;
12467 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12468 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12470 if (!arg || !validate_arg (arg, POINTER_TYPE))
12471 return NULL_TREE;
12472 if (fn_puts)
12473 call = build_call_expr (fn_puts, 1, arg);
12476 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12477 else if (strcmp (fmt_str, target_percent_c) == 0)
12479 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12480 return NULL_TREE;
12481 if (fn_putchar)
12482 call = build_call_expr (fn_putchar, 1, arg);
12485 if (!call)
12486 return NULL_TREE;
12488 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
12491 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12492 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12493 more than 3 arguments, and ARG may be null in the 2-argument case.
12495 Return NULL_TREE if no simplification was possible, otherwise return the
12496 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12497 code of the function to be simplified. */
12499 static tree
12500 fold_builtin_fprintf (tree fndecl, tree fp, tree fmt, tree arg, bool ignore,
12501 enum built_in_function fcode)
12503 tree fn_fputc, fn_fputs, call = NULL_TREE;
12504 const char *fmt_str = NULL;
12506 /* If the return value is used, don't do the transformation. */
12507 if (! ignore)
12508 return NULL_TREE;
12510 /* Verify the required arguments in the original call. */
12511 if (!validate_arg (fp, POINTER_TYPE))
12512 return NULL_TREE;
12513 if (!validate_arg (fmt, POINTER_TYPE))
12514 return NULL_TREE;
12516 /* Check whether the format is a literal string constant. */
12517 fmt_str = c_getstr (fmt);
12518 if (fmt_str == NULL)
12519 return NULL_TREE;
12521 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12523 /* If we're using an unlocked function, assume the other
12524 unlocked functions exist explicitly. */
12525 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12526 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12528 else
12530 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12531 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12534 if (!init_target_chars ())
12535 return NULL_TREE;
12537 /* If the format doesn't contain % args or %%, use strcpy. */
12538 if (strchr (fmt_str, target_percent) == NULL)
12540 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12541 && arg)
12542 return NULL_TREE;
12544 /* If the format specifier was "", fprintf does nothing. */
12545 if (fmt_str[0] == '\0')
12547 /* If FP has side-effects, just wait until gimplification is
12548 done. */
12549 if (TREE_SIDE_EFFECTS (fp))
12550 return NULL_TREE;
12552 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12555 /* When "string" doesn't contain %, replace all cases of
12556 fprintf (fp, string) with fputs (string, fp). The fputs
12557 builtin will take care of special cases like length == 1. */
12558 if (fn_fputs)
12559 call = build_call_expr (fn_fputs, 2, fmt, fp);
12562 /* The other optimizations can be done only on the non-va_list variants. */
12563 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12564 return NULL_TREE;
12566 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12567 else if (strcmp (fmt_str, target_percent_s) == 0)
12569 if (!arg || !validate_arg (arg, POINTER_TYPE))
12570 return NULL_TREE;
12571 if (fn_fputs)
12572 call = build_call_expr (fn_fputs, 2, arg, fp);
12575 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12576 else if (strcmp (fmt_str, target_percent_c) == 0)
12578 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12579 return NULL_TREE;
12580 if (fn_fputc)
12581 call = build_call_expr (fn_fputc, 2, arg, fp);
12584 if (!call)
12585 return NULL_TREE;
12586 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
12589 /* Initialize format string characters in the target charset. */
12591 static bool
12592 init_target_chars (void)
12594 static bool init;
12595 if (!init)
12597 target_newline = lang_hooks.to_target_charset ('\n');
12598 target_percent = lang_hooks.to_target_charset ('%');
12599 target_c = lang_hooks.to_target_charset ('c');
12600 target_s = lang_hooks.to_target_charset ('s');
12601 if (target_newline == 0 || target_percent == 0 || target_c == 0
12602 || target_s == 0)
12603 return false;
12605 target_percent_c[0] = target_percent;
12606 target_percent_c[1] = target_c;
12607 target_percent_c[2] = '\0';
12609 target_percent_s[0] = target_percent;
12610 target_percent_s[1] = target_s;
12611 target_percent_s[2] = '\0';
12613 target_percent_s_newline[0] = target_percent;
12614 target_percent_s_newline[1] = target_s;
12615 target_percent_s_newline[2] = target_newline;
12616 target_percent_s_newline[3] = '\0';
12618 init = true;
12620 return true;
12623 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12624 and no overflow/underflow occurred. INEXACT is true if M was not
12625 exactly calculated. TYPE is the tree type for the result. This
12626 function assumes that you cleared the MPFR flags and then
12627 calculated M to see if anything subsequently set a flag prior to
12628 entering this function. Return NULL_TREE if any checks fail. */
12630 static tree
12631 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12633 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12634 overflow/underflow occurred. If -frounding-math, proceed iff the
12635 result of calling FUNC was exact. */
12636 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12637 && (!flag_rounding_math || !inexact))
12639 REAL_VALUE_TYPE rr;
12641 real_from_mpfr (&rr, m, type, GMP_RNDN);
12642 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12643 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12644 but the mpft_t is not, then we underflowed in the
12645 conversion. */
12646 if (real_isfinite (&rr)
12647 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12649 REAL_VALUE_TYPE rmode;
12651 real_convert (&rmode, TYPE_MODE (type), &rr);
12652 /* Proceed iff the specified mode can hold the value. */
12653 if (real_identical (&rmode, &rr))
12654 return build_real (type, rmode);
12657 return NULL_TREE;
12660 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12661 FUNC on it and return the resulting value as a tree with type TYPE.
12662 If MIN and/or MAX are not NULL, then the supplied ARG must be
12663 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12664 acceptable values, otherwise they are not. The mpfr precision is
12665 set to the precision of TYPE. We assume that function FUNC returns
12666 zero if the result could be calculated exactly within the requested
12667 precision. */
12669 static tree
12670 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
12671 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
12672 bool inclusive)
12674 tree result = NULL_TREE;
12676 STRIP_NOPS (arg);
12678 /* To proceed, MPFR must exactly represent the target floating point
12679 format, which only happens when the target base equals two. */
12680 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12681 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12683 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12685 if (real_isfinite (ra)
12686 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12687 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12689 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12690 int inexact;
12691 mpfr_t m;
12693 mpfr_init2 (m, prec);
12694 mpfr_from_real (m, ra, GMP_RNDN);
12695 mpfr_clear_flags ();
12696 inexact = func (m, m, GMP_RNDN);
12697 result = do_mpfr_ckconv (m, type, inexact);
12698 mpfr_clear (m);
12702 return result;
12705 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12706 FUNC on it and return the resulting value as a tree with type TYPE.
12707 The mpfr precision is set to the precision of TYPE. We assume that
12708 function FUNC returns zero if the result could be calculated
12709 exactly within the requested precision. */
12711 static tree
12712 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12713 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12715 tree result = NULL_TREE;
12717 STRIP_NOPS (arg1);
12718 STRIP_NOPS (arg2);
12720 /* To proceed, MPFR must exactly represent the target floating point
12721 format, which only happens when the target base equals two. */
12722 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12723 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12724 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12726 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12727 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12729 if (real_isfinite (ra1) && real_isfinite (ra2))
12731 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12732 int inexact;
12733 mpfr_t m1, m2;
12735 mpfr_inits2 (prec, m1, m2, NULL);
12736 mpfr_from_real (m1, ra1, GMP_RNDN);
12737 mpfr_from_real (m2, ra2, GMP_RNDN);
12738 mpfr_clear_flags ();
12739 inexact = func (m1, m1, m2, GMP_RNDN);
12740 result = do_mpfr_ckconv (m1, type, inexact);
12741 mpfr_clears (m1, m2, NULL);
12745 return result;
12748 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12749 FUNC on it and return the resulting value as a tree with type TYPE.
12750 The mpfr precision is set to the precision of TYPE. We assume that
12751 function FUNC returns zero if the result could be calculated
12752 exactly within the requested precision. */
12754 static tree
12755 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12756 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12758 tree result = NULL_TREE;
12760 STRIP_NOPS (arg1);
12761 STRIP_NOPS (arg2);
12762 STRIP_NOPS (arg3);
12764 /* To proceed, MPFR must exactly represent the target floating point
12765 format, which only happens when the target base equals two. */
12766 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12767 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12768 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12769 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12771 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12772 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12773 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12775 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12777 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12778 int inexact;
12779 mpfr_t m1, m2, m3;
12781 mpfr_inits2 (prec, m1, m2, m3, NULL);
12782 mpfr_from_real (m1, ra1, GMP_RNDN);
12783 mpfr_from_real (m2, ra2, GMP_RNDN);
12784 mpfr_from_real (m3, ra3, GMP_RNDN);
12785 mpfr_clear_flags ();
12786 inexact = func (m1, m1, m2, m3, GMP_RNDN);
12787 result = do_mpfr_ckconv (m1, type, inexact);
12788 mpfr_clears (m1, m2, m3, NULL);
12792 return result;
12795 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12796 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12797 If ARG_SINP and ARG_COSP are NULL then the result is returned
12798 as a complex value.
12799 The type is taken from the type of ARG and is used for setting the
12800 precision of the calculation and results. */
12802 static tree
12803 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
12805 tree const type = TREE_TYPE (arg);
12806 tree result = NULL_TREE;
12808 STRIP_NOPS (arg);
12810 /* To proceed, MPFR must exactly represent the target floating point
12811 format, which only happens when the target base equals two. */
12812 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12813 && TREE_CODE (arg) == REAL_CST
12814 && !TREE_OVERFLOW (arg))
12816 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12818 if (real_isfinite (ra))
12820 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12821 tree result_s, result_c;
12822 int inexact;
12823 mpfr_t m, ms, mc;
12825 mpfr_inits2 (prec, m, ms, mc, NULL);
12826 mpfr_from_real (m, ra, GMP_RNDN);
12827 mpfr_clear_flags ();
12828 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
12829 result_s = do_mpfr_ckconv (ms, type, inexact);
12830 result_c = do_mpfr_ckconv (mc, type, inexact);
12831 mpfr_clears (m, ms, mc, NULL);
12832 if (result_s && result_c)
12834 /* If we are to return in a complex value do so. */
12835 if (!arg_sinp && !arg_cosp)
12836 return build_complex (build_complex_type (type),
12837 result_c, result_s);
12839 /* Dereference the sin/cos pointer arguments. */
12840 arg_sinp = build_fold_indirect_ref (arg_sinp);
12841 arg_cosp = build_fold_indirect_ref (arg_cosp);
12842 /* Proceed if valid pointer type were passed in. */
12843 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
12844 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
12846 /* Set the values. */
12847 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12848 result_s);
12849 TREE_SIDE_EFFECTS (result_s) = 1;
12850 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12851 result_c);
12852 TREE_SIDE_EFFECTS (result_c) = 1;
12853 /* Combine the assignments into a compound expr. */
12854 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12855 result_s, result_c));
12860 return result;
12863 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
12864 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12865 two-argument mpfr order N Bessel function FUNC on them and return
12866 the resulting value as a tree with type TYPE. The mpfr precision
12867 is set to the precision of TYPE. We assume that function FUNC
12868 returns zero if the result could be calculated exactly within the
12869 requested precision. */
12870 static tree
12871 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12872 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12873 const REAL_VALUE_TYPE *min, bool inclusive)
12875 tree result = NULL_TREE;
12877 STRIP_NOPS (arg1);
12878 STRIP_NOPS (arg2);
12880 /* To proceed, MPFR must exactly represent the target floating point
12881 format, which only happens when the target base equals two. */
12882 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12883 && host_integerp (arg1, 0)
12884 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12886 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
12887 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12889 if (n == (long)n
12890 && real_isfinite (ra)
12891 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12893 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12894 int inexact;
12895 mpfr_t m;
12897 mpfr_init2 (m, prec);
12898 mpfr_from_real (m, ra, GMP_RNDN);
12899 mpfr_clear_flags ();
12900 inexact = func (m, n, m, GMP_RNDN);
12901 result = do_mpfr_ckconv (m, type, inexact);
12902 mpfr_clear (m);
12906 return result;
12909 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12910 the pointer *(ARG_QUO) and return the result. The type is taken
12911 from the type of ARG0 and is used for setting the precision of the
12912 calculation and results. */
12914 static tree
12915 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12917 tree const type = TREE_TYPE (arg0);
12918 tree result = NULL_TREE;
12920 STRIP_NOPS (arg0);
12921 STRIP_NOPS (arg1);
12923 /* To proceed, MPFR must exactly represent the target floating point
12924 format, which only happens when the target base equals two. */
12925 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12926 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12927 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12929 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12930 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12932 if (real_isfinite (ra0) && real_isfinite (ra1))
12934 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
12935 tree result_rem;
12936 long integer_quo;
12937 mpfr_t m0, m1;
12939 mpfr_inits2 (prec, m0, m1, NULL);
12940 mpfr_from_real (m0, ra0, GMP_RNDN);
12941 mpfr_from_real (m1, ra1, GMP_RNDN);
12942 mpfr_clear_flags ();
12943 mpfr_remquo (m0, &integer_quo, m0, m1, GMP_RNDN);
12944 /* Remquo is independent of the rounding mode, so pass
12945 inexact=0 to do_mpfr_ckconv(). */
12946 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12947 mpfr_clears (m0, m1, NULL);
12948 if (result_rem)
12950 /* MPFR calculates quo in the host's long so it may
12951 return more bits in quo than the target int can hold
12952 if sizeof(host long) > sizeof(target int). This can
12953 happen even for native compilers in LP64 mode. In
12954 these cases, modulo the quo value with the largest
12955 number that the target int can hold while leaving one
12956 bit for the sign. */
12957 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12958 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12960 /* Dereference the quo pointer argument. */
12961 arg_quo = build_fold_indirect_ref (arg_quo);
12962 /* Proceed iff a valid pointer type was passed in. */
12963 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12965 /* Set the value. */
12966 tree result_quo = fold_build2 (MODIFY_EXPR,
12967 TREE_TYPE (arg_quo), arg_quo,
12968 build_int_cst (NULL, integer_quo));
12969 TREE_SIDE_EFFECTS (result_quo) = 1;
12970 /* Combine the quo assignment with the rem. */
12971 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12972 result_quo, result_rem));
12977 return result;
12980 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12981 resulting value as a tree with type TYPE. The mpfr precision is
12982 set to the precision of TYPE. We assume that this mpfr function
12983 returns zero if the result could be calculated exactly within the
12984 requested precision. In addition, the integer pointer represented
12985 by ARG_SG will be dereferenced and set to the appropriate signgam
12986 (-1,1) value. */
12988 static tree
12989 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12991 tree result = NULL_TREE;
12993 STRIP_NOPS (arg);
12995 /* To proceed, MPFR must exactly represent the target floating point
12996 format, which only happens when the target base equals two. Also
12997 verify ARG is a constant and that ARG_SG is an int pointer. */
12998 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12999 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13000 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13001 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13003 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13005 /* In addition to NaN and Inf, the argument cannot be zero or a
13006 negative integer. */
13007 if (real_isfinite (ra)
13008 && ra->cl != rvc_zero
13009 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13011 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
13012 int inexact, sg;
13013 mpfr_t m;
13014 tree result_lg;
13016 mpfr_init2 (m, prec);
13017 mpfr_from_real (m, ra, GMP_RNDN);
13018 mpfr_clear_flags ();
13019 inexact = mpfr_lgamma (m, &sg, m, GMP_RNDN);
13020 result_lg = do_mpfr_ckconv (m, type, inexact);
13021 mpfr_clear (m);
13022 if (result_lg)
13024 tree result_sg;
13026 /* Dereference the arg_sg pointer argument. */
13027 arg_sg = build_fold_indirect_ref (arg_sg);
13028 /* Assign the signgam value into *arg_sg. */
13029 result_sg = fold_build2 (MODIFY_EXPR,
13030 TREE_TYPE (arg_sg), arg_sg,
13031 build_int_cst (NULL, sg));
13032 TREE_SIDE_EFFECTS (result_sg) = 1;
13033 /* Combine the signgam assignment with the lgamma result. */
13034 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13035 result_sg, result_lg));
13040 return result;
13042 #endif