* doc/loop.texi: Document number_of_latch_executions and
[official-gcc.git] / gcc / builtins.c
blob3c7d1052cba47ddaf83bd3e5797d4cf9316e8203
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
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"
52 #ifndef PAD_VARARGS_DOWN
53 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
54 #endif
56 /* Define the names of the builtin function types and codes. */
57 const char *const built_in_class_names[4]
58 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
60 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
61 const char * built_in_names[(int) END_BUILTINS] =
63 #include "builtins.def"
65 #undef DEF_BUILTIN
67 /* Setup an array of _DECL trees, make sure each element is
68 initialized to NULL_TREE. */
69 tree built_in_decls[(int) END_BUILTINS];
70 /* Declarations used when constructing the builtin implicitly in the compiler.
71 It may be NULL_TREE when this is invalid (for instance runtime is not
72 required to implement the function call in all cases). */
73 tree implicit_built_in_decls[(int) END_BUILTINS];
75 static int get_pointer_alignment (tree, unsigned int);
76 static const char *c_getstr (tree);
77 static rtx c_readstr (const char *, enum machine_mode);
78 static int target_char_cast (tree, char *);
79 static rtx get_memory_rtx (tree, tree);
80 static int apply_args_size (void);
81 static int apply_result_size (void);
82 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
83 static rtx result_vector (int, rtx);
84 #endif
85 static void expand_builtin_update_setjmp_buf (rtx);
86 static void expand_builtin_prefetch (tree);
87 static rtx expand_builtin_apply_args (void);
88 static rtx expand_builtin_apply_args_1 (void);
89 static rtx expand_builtin_apply (rtx, rtx, rtx);
90 static void expand_builtin_return (rtx);
91 static enum type_class type_to_class (tree);
92 static rtx expand_builtin_classify_type (tree);
93 static void expand_errno_check (tree, rtx);
94 static rtx expand_builtin_mathfn (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
97 static rtx expand_builtin_sincos (tree);
98 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
99 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_args_info (tree);
101 static rtx expand_builtin_next_arg (void);
102 static rtx expand_builtin_va_start (tree);
103 static rtx expand_builtin_va_end (tree);
104 static rtx expand_builtin_va_copy (tree);
105 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
108 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
109 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
115 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
116 static rtx expand_builtin_bcopy (tree);
117 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
118 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
119 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
120 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
121 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
124 static rtx expand_builtin_bzero (tree);
125 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_alloca (tree, rtx);
131 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
132 static rtx expand_builtin_frame_address (tree, tree);
133 static rtx expand_builtin_fputs (tree, rtx, bool);
134 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
137 static tree stabilize_va_list (tree, int);
138 static rtx expand_builtin_expect (tree, rtx);
139 static tree fold_builtin_constant_p (tree);
140 static tree fold_builtin_classify_type (tree);
141 static tree fold_builtin_strlen (tree);
142 static tree fold_builtin_inf (tree, int);
143 static tree fold_builtin_nan (tree, tree, int);
144 static int validate_arglist (tree, ...);
145 static bool integer_valued_real_p (tree);
146 static tree fold_trunc_transparent_mathfn (tree, tree);
147 static bool readonly_data_expr (tree);
148 static rtx expand_builtin_fabs (tree, rtx, rtx);
149 static rtx expand_builtin_signbit (tree, rtx);
150 static tree fold_builtin_sqrt (tree, tree);
151 static tree fold_builtin_cbrt (tree, tree);
152 static tree fold_builtin_pow (tree, tree, tree);
153 static tree fold_builtin_powi (tree, tree, tree);
154 static tree fold_builtin_cos (tree, tree, tree);
155 static tree fold_builtin_cosh (tree, tree, tree);
156 static tree fold_builtin_tan (tree, tree);
157 static tree fold_builtin_trunc (tree, tree);
158 static tree fold_builtin_floor (tree, tree);
159 static tree fold_builtin_ceil (tree, tree);
160 static tree fold_builtin_round (tree, tree);
161 static tree fold_builtin_int_roundingfn (tree, tree);
162 static tree fold_builtin_bitop (tree, tree);
163 static tree fold_builtin_memory_op (tree, tree, bool, int);
164 static tree fold_builtin_strchr (tree, tree);
165 static tree fold_builtin_memcmp (tree);
166 static tree fold_builtin_strcmp (tree);
167 static tree fold_builtin_strncmp (tree);
168 static tree fold_builtin_signbit (tree, tree);
169 static tree fold_builtin_copysign (tree, tree, tree);
170 static tree fold_builtin_isascii (tree);
171 static tree fold_builtin_toascii (tree);
172 static tree fold_builtin_isdigit (tree);
173 static tree fold_builtin_fabs (tree, tree);
174 static tree fold_builtin_abs (tree, tree);
175 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
176 enum tree_code);
177 static tree fold_builtin_1 (tree, tree, bool);
179 static tree fold_builtin_strpbrk (tree, tree);
180 static tree fold_builtin_strstr (tree, tree);
181 static tree fold_builtin_strrchr (tree, tree);
182 static tree fold_builtin_strcat (tree);
183 static tree fold_builtin_strncat (tree);
184 static tree fold_builtin_strspn (tree);
185 static tree fold_builtin_strcspn (tree);
186 static tree fold_builtin_sprintf (tree, int);
188 static rtx expand_builtin_object_size (tree);
189 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
190 enum built_in_function);
191 static void maybe_emit_chk_warning (tree, enum built_in_function);
192 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
193 static tree fold_builtin_object_size (tree);
194 static tree fold_builtin_strcat_chk (tree, tree);
195 static tree fold_builtin_strncat_chk (tree, tree);
196 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
197 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
198 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
199 static bool init_target_chars (void);
201 static unsigned HOST_WIDE_INT target_newline;
202 static unsigned HOST_WIDE_INT target_percent;
203 static unsigned HOST_WIDE_INT target_c;
204 static unsigned HOST_WIDE_INT target_s;
205 static char target_percent_c[3];
206 static char target_percent_s[3];
207 static char target_percent_s_newline[4];
208 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
209 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
210 static tree do_mpfr_arg2 (tree, tree, tree,
211 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
212 static tree do_mpfr_arg3 (tree, tree, tree, tree,
213 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
214 static tree do_mpfr_sincos (tree, tree, tree);
216 /* Return true if NODE should be considered for inline expansion regardless
217 of the optimization level. This means whenever a function is invoked with
218 its "internal" name, which normally contains the prefix "__builtin". */
220 static bool called_as_built_in (tree node)
222 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
223 if (strncmp (name, "__builtin_", 10) == 0)
224 return true;
225 if (strncmp (name, "__sync_", 7) == 0)
226 return true;
227 return false;
230 /* Return the alignment in bits of EXP, a pointer valued expression.
231 But don't return more than MAX_ALIGN no matter what.
232 The alignment returned is, by default, the alignment of the thing that
233 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
235 Otherwise, look at the expression to see if we can do better, i.e., if the
236 expression is actually pointing at an object whose alignment is tighter. */
238 static int
239 get_pointer_alignment (tree exp, unsigned int max_align)
241 unsigned int align, inner;
243 /* We rely on TER to compute accurate alignment information. */
244 if (!(optimize && flag_tree_ter))
245 return 0;
247 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
248 return 0;
250 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
251 align = MIN (align, max_align);
253 while (1)
255 switch (TREE_CODE (exp))
257 case NOP_EXPR:
258 case CONVERT_EXPR:
259 case NON_LVALUE_EXPR:
260 exp = TREE_OPERAND (exp, 0);
261 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
262 return align;
264 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
265 align = MIN (inner, max_align);
266 break;
268 case PLUS_EXPR:
269 /* If sum of pointer + int, restrict our maximum alignment to that
270 imposed by the integer. If not, we can't do any better than
271 ALIGN. */
272 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
273 return align;
275 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
276 & (max_align / BITS_PER_UNIT - 1))
277 != 0)
278 max_align >>= 1;
280 exp = TREE_OPERAND (exp, 0);
281 break;
283 case ADDR_EXPR:
284 /* See what we are pointing at and look at its alignment. */
285 exp = TREE_OPERAND (exp, 0);
286 inner = max_align;
287 if (handled_component_p (exp))
289 HOST_WIDE_INT bitsize, bitpos;
290 tree offset;
291 enum machine_mode mode;
292 int unsignedp, volatilep;
294 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
295 &mode, &unsignedp, &volatilep, true);
296 if (bitpos)
297 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
298 if (offset && TREE_CODE (offset) == PLUS_EXPR
299 && host_integerp (TREE_OPERAND (offset, 1), 1))
301 /* Any overflow in calculating offset_bits won't change
302 the alignment. */
303 unsigned offset_bits
304 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
305 * BITS_PER_UNIT);
307 if (offset_bits)
308 inner = MIN (inner, (offset_bits & -offset_bits));
309 offset = TREE_OPERAND (offset, 0);
311 if (offset && TREE_CODE (offset) == MULT_EXPR
312 && host_integerp (TREE_OPERAND (offset, 1), 1))
314 /* Any overflow in calculating offset_factor won't change
315 the alignment. */
316 unsigned offset_factor
317 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
318 * BITS_PER_UNIT);
320 if (offset_factor)
321 inner = MIN (inner, (offset_factor & -offset_factor));
323 else if (offset)
324 inner = MIN (inner, BITS_PER_UNIT);
326 if (TREE_CODE (exp) == FUNCTION_DECL)
327 align = FUNCTION_BOUNDARY;
328 else if (DECL_P (exp))
329 align = MIN (inner, DECL_ALIGN (exp));
330 #ifdef CONSTANT_ALIGNMENT
331 else if (CONSTANT_CLASS_P (exp))
332 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
333 #endif
334 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
335 || TREE_CODE (exp) == INDIRECT_REF)
336 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
337 else
338 align = MIN (align, inner);
339 return MIN (align, max_align);
341 default:
342 return align;
347 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
348 way, because it could contain a zero byte in the middle.
349 TREE_STRING_LENGTH is the size of the character array, not the string.
351 ONLY_VALUE should be nonzero if the result is not going to be emitted
352 into the instruction stream and zero if it is going to be expanded.
353 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
354 is returned, otherwise NULL, since
355 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
356 evaluate the side-effects.
358 The value returned is of type `ssizetype'.
360 Unfortunately, string_constant can't access the values of const char
361 arrays with initializers, so neither can we do so here. */
363 tree
364 c_strlen (tree src, int only_value)
366 tree offset_node;
367 HOST_WIDE_INT offset;
368 int max;
369 const char *ptr;
371 STRIP_NOPS (src);
372 if (TREE_CODE (src) == COND_EXPR
373 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
375 tree len1, len2;
377 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
378 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
379 if (tree_int_cst_equal (len1, len2))
380 return len1;
383 if (TREE_CODE (src) == COMPOUND_EXPR
384 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
385 return c_strlen (TREE_OPERAND (src, 1), only_value);
387 src = string_constant (src, &offset_node);
388 if (src == 0)
389 return 0;
391 max = TREE_STRING_LENGTH (src) - 1;
392 ptr = TREE_STRING_POINTER (src);
394 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
396 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
397 compute the offset to the following null if we don't know where to
398 start searching for it. */
399 int i;
401 for (i = 0; i < max; i++)
402 if (ptr[i] == 0)
403 return 0;
405 /* We don't know the starting offset, but we do know that the string
406 has no internal zero bytes. We can assume that the offset falls
407 within the bounds of the string; otherwise, the programmer deserves
408 what he gets. Subtract the offset from the length of the string,
409 and return that. This would perhaps not be valid if we were dealing
410 with named arrays in addition to literal string constants. */
412 return size_diffop (size_int (max), offset_node);
415 /* We have a known offset into the string. Start searching there for
416 a null character if we can represent it as a single HOST_WIDE_INT. */
417 if (offset_node == 0)
418 offset = 0;
419 else if (! host_integerp (offset_node, 0))
420 offset = -1;
421 else
422 offset = tree_low_cst (offset_node, 0);
424 /* If the offset is known to be out of bounds, warn, and call strlen at
425 runtime. */
426 if (offset < 0 || offset > max)
428 warning (0, "offset outside bounds of constant string");
429 return 0;
432 /* Use strlen to search for the first zero byte. Since any strings
433 constructed with build_string will have nulls appended, we win even
434 if we get handed something like (char[4])"abcd".
436 Since OFFSET is our starting index into the string, no further
437 calculation is needed. */
438 return ssize_int (strlen (ptr + offset));
441 /* Return a char pointer for a C string if it is a string constant
442 or sum of string constant and integer constant. */
444 static const char *
445 c_getstr (tree src)
447 tree offset_node;
449 src = string_constant (src, &offset_node);
450 if (src == 0)
451 return 0;
453 if (offset_node == 0)
454 return TREE_STRING_POINTER (src);
455 else if (!host_integerp (offset_node, 1)
456 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
457 return 0;
459 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
462 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
463 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
465 static rtx
466 c_readstr (const char *str, enum machine_mode mode)
468 HOST_WIDE_INT c[2];
469 HOST_WIDE_INT ch;
470 unsigned int i, j;
472 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
474 c[0] = 0;
475 c[1] = 0;
476 ch = 1;
477 for (i = 0; i < GET_MODE_SIZE (mode); i++)
479 j = i;
480 if (WORDS_BIG_ENDIAN)
481 j = GET_MODE_SIZE (mode) - i - 1;
482 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
483 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
484 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
485 j *= BITS_PER_UNIT;
486 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
488 if (ch)
489 ch = (unsigned char) str[i];
490 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
492 return immed_double_const (c[0], c[1], mode);
495 /* Cast a target constant CST to target CHAR and if that value fits into
496 host char type, return zero and put that value into variable pointed to by
497 P. */
499 static int
500 target_char_cast (tree cst, char *p)
502 unsigned HOST_WIDE_INT val, hostval;
504 if (!host_integerp (cst, 1)
505 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
506 return 1;
508 val = tree_low_cst (cst, 1);
509 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
510 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
512 hostval = val;
513 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
514 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
516 if (val != hostval)
517 return 1;
519 *p = hostval;
520 return 0;
523 /* Similar to save_expr, but assumes that arbitrary code is not executed
524 in between the multiple evaluations. In particular, we assume that a
525 non-addressable local variable will not be modified. */
527 static tree
528 builtin_save_expr (tree exp)
530 if (TREE_ADDRESSABLE (exp) == 0
531 && (TREE_CODE (exp) == PARM_DECL
532 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
533 return exp;
535 return save_expr (exp);
538 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
539 times to get the address of either a higher stack frame, or a return
540 address located within it (depending on FNDECL_CODE). */
542 static rtx
543 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
545 int i;
547 #ifdef INITIAL_FRAME_ADDRESS_RTX
548 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
549 #else
550 rtx tem;
552 /* For a zero count with __builtin_return_address, we don't care what
553 frame address we return, because target-specific definitions will
554 override us. Therefore frame pointer elimination is OK, and using
555 the soft frame pointer is OK.
557 For a nonzero count, or a zero count with __builtin_frame_address,
558 we require a stable offset from the current frame pointer to the
559 previous one, so we must use the hard frame pointer, and
560 we must disable frame pointer elimination. */
561 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
562 tem = frame_pointer_rtx;
563 else
565 tem = hard_frame_pointer_rtx;
567 /* Tell reload not to eliminate the frame pointer. */
568 current_function_accesses_prior_frames = 1;
570 #endif
572 /* Some machines need special handling before we can access
573 arbitrary frames. For example, on the SPARC, we must first flush
574 all register windows to the stack. */
575 #ifdef SETUP_FRAME_ADDRESSES
576 if (count > 0)
577 SETUP_FRAME_ADDRESSES ();
578 #endif
580 /* On the SPARC, the return address is not in the frame, it is in a
581 register. There is no way to access it off of the current frame
582 pointer, but it can be accessed off the previous frame pointer by
583 reading the value from the register window save area. */
584 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
585 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
586 count--;
587 #endif
589 /* Scan back COUNT frames to the specified frame. */
590 for (i = 0; i < count; i++)
592 /* Assume the dynamic chain pointer is in the word that the
593 frame address points to, unless otherwise specified. */
594 #ifdef DYNAMIC_CHAIN_ADDRESS
595 tem = DYNAMIC_CHAIN_ADDRESS (tem);
596 #endif
597 tem = memory_address (Pmode, tem);
598 tem = gen_frame_mem (Pmode, tem);
599 tem = copy_to_reg (tem);
602 /* For __builtin_frame_address, return what we've got. But, on
603 the SPARC for example, we may have to add a bias. */
604 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
605 #ifdef FRAME_ADDR_RTX
606 return FRAME_ADDR_RTX (tem);
607 #else
608 return tem;
609 #endif
611 /* For __builtin_return_address, get the return address from that frame. */
612 #ifdef RETURN_ADDR_RTX
613 tem = RETURN_ADDR_RTX (count, tem);
614 #else
615 tem = memory_address (Pmode,
616 plus_constant (tem, GET_MODE_SIZE (Pmode)));
617 tem = gen_frame_mem (Pmode, tem);
618 #endif
619 return tem;
622 /* Alias set used for setjmp buffer. */
623 static HOST_WIDE_INT setjmp_alias_set = -1;
625 /* Construct the leading half of a __builtin_setjmp call. Control will
626 return to RECEIVER_LABEL. This is also called directly by the SJLJ
627 exception handling code. */
629 void
630 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
632 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
633 rtx stack_save;
634 rtx mem;
636 if (setjmp_alias_set == -1)
637 setjmp_alias_set = new_alias_set ();
639 buf_addr = convert_memory_address (Pmode, buf_addr);
641 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
643 /* We store the frame pointer and the address of receiver_label in
644 the buffer and use the rest of it for the stack save area, which
645 is machine-dependent. */
647 mem = gen_rtx_MEM (Pmode, buf_addr);
648 set_mem_alias_set (mem, setjmp_alias_set);
649 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
651 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
652 set_mem_alias_set (mem, setjmp_alias_set);
654 emit_move_insn (validize_mem (mem),
655 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
657 stack_save = gen_rtx_MEM (sa_mode,
658 plus_constant (buf_addr,
659 2 * GET_MODE_SIZE (Pmode)));
660 set_mem_alias_set (stack_save, setjmp_alias_set);
661 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
663 /* If there is further processing to do, do it. */
664 #ifdef HAVE_builtin_setjmp_setup
665 if (HAVE_builtin_setjmp_setup)
666 emit_insn (gen_builtin_setjmp_setup (buf_addr));
667 #endif
669 /* Tell optimize_save_area_alloca that extra work is going to
670 need to go on during alloca. */
671 current_function_calls_setjmp = 1;
673 /* Set this so all the registers get saved in our frame; we need to be
674 able to copy the saved values for any registers from frames we unwind. */
675 current_function_has_nonlocal_label = 1;
678 /* Construct the trailing part of a __builtin_setjmp call. This is
679 also called directly by the SJLJ exception handling code. */
681 void
682 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
684 /* Clobber the FP when we get here, so we have to make sure it's
685 marked as used by this function. */
686 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
688 /* Mark the static chain as clobbered here so life information
689 doesn't get messed up for it. */
690 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
692 /* Now put in the code to restore the frame pointer, and argument
693 pointer, if needed. */
694 #ifdef HAVE_nonlocal_goto
695 if (! HAVE_nonlocal_goto)
696 #endif
698 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
699 /* This might change the hard frame pointer in ways that aren't
700 apparent to early optimization passes, so force a clobber. */
701 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
704 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
705 if (fixed_regs[ARG_POINTER_REGNUM])
707 #ifdef ELIMINABLE_REGS
708 size_t i;
709 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
711 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
712 if (elim_regs[i].from == ARG_POINTER_REGNUM
713 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
714 break;
716 if (i == ARRAY_SIZE (elim_regs))
717 #endif
719 /* Now restore our arg pointer from the address at which it
720 was saved in our stack frame. */
721 emit_move_insn (virtual_incoming_args_rtx,
722 copy_to_reg (get_arg_pointer_save_area (cfun)));
725 #endif
727 #ifdef HAVE_builtin_setjmp_receiver
728 if (HAVE_builtin_setjmp_receiver)
729 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
730 else
731 #endif
732 #ifdef HAVE_nonlocal_goto_receiver
733 if (HAVE_nonlocal_goto_receiver)
734 emit_insn (gen_nonlocal_goto_receiver ());
735 else
736 #endif
737 { /* Nothing */ }
739 /* @@@ This is a kludge. Not all machine descriptions define a blockage
740 insn, but we must not allow the code we just generated to be reordered
741 by scheduling. Specifically, the update of the frame pointer must
742 happen immediately, not later. So emit an ASM_INPUT to act as blockage
743 insn. */
744 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
747 /* __builtin_longjmp is passed a pointer to an array of five words (not
748 all will be used on all machines). It operates similarly to the C
749 library function of the same name, but is more efficient. Much of
750 the code below is copied from the handling of non-local gotos. */
752 static void
753 expand_builtin_longjmp (rtx buf_addr, rtx value)
755 rtx fp, lab, stack, insn, last;
756 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
758 if (setjmp_alias_set == -1)
759 setjmp_alias_set = new_alias_set ();
761 buf_addr = convert_memory_address (Pmode, buf_addr);
763 buf_addr = force_reg (Pmode, buf_addr);
765 /* We used to store value in static_chain_rtx, but that fails if pointers
766 are smaller than integers. We instead require that the user must pass
767 a second argument of 1, because that is what builtin_setjmp will
768 return. This also makes EH slightly more efficient, since we are no
769 longer copying around a value that we don't care about. */
770 gcc_assert (value == const1_rtx);
772 last = get_last_insn ();
773 #ifdef HAVE_builtin_longjmp
774 if (HAVE_builtin_longjmp)
775 emit_insn (gen_builtin_longjmp (buf_addr));
776 else
777 #endif
779 fp = gen_rtx_MEM (Pmode, buf_addr);
780 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
781 GET_MODE_SIZE (Pmode)));
783 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
784 2 * GET_MODE_SIZE (Pmode)));
785 set_mem_alias_set (fp, setjmp_alias_set);
786 set_mem_alias_set (lab, setjmp_alias_set);
787 set_mem_alias_set (stack, setjmp_alias_set);
789 /* Pick up FP, label, and SP from the block and jump. This code is
790 from expand_goto in stmt.c; see there for detailed comments. */
791 #ifdef HAVE_nonlocal_goto
792 if (HAVE_nonlocal_goto)
793 /* We have to pass a value to the nonlocal_goto pattern that will
794 get copied into the static_chain pointer, but it does not matter
795 what that value is, because builtin_setjmp does not use it. */
796 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
797 else
798 #endif
800 lab = copy_to_reg (lab);
802 emit_insn (gen_rtx_CLOBBER (VOIDmode,
803 gen_rtx_MEM (BLKmode,
804 gen_rtx_SCRATCH (VOIDmode))));
805 emit_insn (gen_rtx_CLOBBER (VOIDmode,
806 gen_rtx_MEM (BLKmode,
807 hard_frame_pointer_rtx)));
809 emit_move_insn (hard_frame_pointer_rtx, fp);
810 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
812 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
813 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
814 emit_indirect_jump (lab);
818 /* Search backwards and mark the jump insn as a non-local goto.
819 Note that this precludes the use of __builtin_longjmp to a
820 __builtin_setjmp target in the same function. However, we've
821 already cautioned the user that these functions are for
822 internal exception handling use only. */
823 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
825 gcc_assert (insn != last);
827 if (JUMP_P (insn))
829 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
830 REG_NOTES (insn));
831 break;
833 else if (CALL_P (insn))
834 break;
838 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
839 and the address of the save area. */
841 static rtx
842 expand_builtin_nonlocal_goto (tree arglist)
844 tree t_label, t_save_area;
845 rtx r_label, r_save_area, r_fp, r_sp, insn;
847 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
848 return NULL_RTX;
850 t_label = TREE_VALUE (arglist);
851 arglist = TREE_CHAIN (arglist);
852 t_save_area = TREE_VALUE (arglist);
854 r_label = expand_normal (t_label);
855 r_label = convert_memory_address (Pmode, r_label);
856 r_save_area = expand_normal (t_save_area);
857 r_save_area = convert_memory_address (Pmode, r_save_area);
858 r_fp = gen_rtx_MEM (Pmode, r_save_area);
859 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
860 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
862 current_function_has_nonlocal_goto = 1;
864 #ifdef HAVE_nonlocal_goto
865 /* ??? We no longer need to pass the static chain value, afaik. */
866 if (HAVE_nonlocal_goto)
867 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
868 else
869 #endif
871 r_label = copy_to_reg (r_label);
873 emit_insn (gen_rtx_CLOBBER (VOIDmode,
874 gen_rtx_MEM (BLKmode,
875 gen_rtx_SCRATCH (VOIDmode))));
877 emit_insn (gen_rtx_CLOBBER (VOIDmode,
878 gen_rtx_MEM (BLKmode,
879 hard_frame_pointer_rtx)));
881 /* Restore frame pointer for containing function.
882 This sets the actual hard register used for the frame pointer
883 to the location of the function's incoming static chain info.
884 The non-local goto handler will then adjust it to contain the
885 proper value and reload the argument pointer, if needed. */
886 emit_move_insn (hard_frame_pointer_rtx, r_fp);
887 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
889 /* USE of hard_frame_pointer_rtx added for consistency;
890 not clear if really needed. */
891 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
892 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
893 emit_indirect_jump (r_label);
896 /* Search backwards to the jump insn and mark it as a
897 non-local goto. */
898 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
900 if (JUMP_P (insn))
902 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
903 const0_rtx, REG_NOTES (insn));
904 break;
906 else if (CALL_P (insn))
907 break;
910 return const0_rtx;
913 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
914 (not all will be used on all machines) that was passed to __builtin_setjmp.
915 It updates the stack pointer in that block to correspond to the current
916 stack pointer. */
918 static void
919 expand_builtin_update_setjmp_buf (rtx buf_addr)
921 enum machine_mode sa_mode = Pmode;
922 rtx stack_save;
925 #ifdef HAVE_save_stack_nonlocal
926 if (HAVE_save_stack_nonlocal)
927 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
928 #endif
929 #ifdef STACK_SAVEAREA_MODE
930 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
931 #endif
933 stack_save
934 = gen_rtx_MEM (sa_mode,
935 memory_address
936 (sa_mode,
937 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
939 #ifdef HAVE_setjmp
940 if (HAVE_setjmp)
941 emit_insn (gen_setjmp ());
942 #endif
944 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
947 /* Expand a call to __builtin_prefetch. For a target that does not support
948 data prefetch, evaluate the memory address argument in case it has side
949 effects. */
951 static void
952 expand_builtin_prefetch (tree arglist)
954 tree arg0, arg1, arg2;
955 rtx op0, op1, op2;
957 if (!validate_arglist (arglist, POINTER_TYPE, 0))
958 return;
960 arg0 = TREE_VALUE (arglist);
961 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
962 zero (read) and argument 2 (locality) defaults to 3 (high degree of
963 locality). */
964 if (TREE_CHAIN (arglist))
966 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
967 if (TREE_CHAIN (TREE_CHAIN (arglist)))
968 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
969 else
970 arg2 = build_int_cst (NULL_TREE, 3);
972 else
974 arg1 = integer_zero_node;
975 arg2 = build_int_cst (NULL_TREE, 3);
978 /* Argument 0 is an address. */
979 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
981 /* Argument 1 (read/write flag) must be a compile-time constant int. */
982 if (TREE_CODE (arg1) != INTEGER_CST)
984 error ("second argument to %<__builtin_prefetch%> must be a constant");
985 arg1 = integer_zero_node;
987 op1 = expand_normal (arg1);
988 /* Argument 1 must be either zero or one. */
989 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
991 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
992 " using zero");
993 op1 = const0_rtx;
996 /* Argument 2 (locality) must be a compile-time constant int. */
997 if (TREE_CODE (arg2) != INTEGER_CST)
999 error ("third argument to %<__builtin_prefetch%> must be a constant");
1000 arg2 = integer_zero_node;
1002 op2 = expand_normal (arg2);
1003 /* Argument 2 must be 0, 1, 2, or 3. */
1004 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1006 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1007 op2 = const0_rtx;
1010 #ifdef HAVE_prefetch
1011 if (HAVE_prefetch)
1013 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1014 (op0,
1015 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1016 || (GET_MODE (op0) != Pmode))
1018 op0 = convert_memory_address (Pmode, op0);
1019 op0 = force_reg (Pmode, op0);
1021 emit_insn (gen_prefetch (op0, op1, op2));
1023 #endif
1025 /* Don't do anything with direct references to volatile memory, but
1026 generate code to handle other side effects. */
1027 if (!MEM_P (op0) && side_effects_p (op0))
1028 emit_insn (op0);
1031 /* Get a MEM rtx for expression EXP which is the address of an operand
1032 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1033 the maximum length of the block of memory that might be accessed or
1034 NULL if unknown. */
1036 static rtx
1037 get_memory_rtx (tree exp, tree len)
1039 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1040 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1042 /* Get an expression we can use to find the attributes to assign to MEM.
1043 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1044 we can. First remove any nops. */
1045 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1046 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1047 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1048 exp = TREE_OPERAND (exp, 0);
1050 if (TREE_CODE (exp) == ADDR_EXPR)
1051 exp = TREE_OPERAND (exp, 0);
1052 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1053 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1054 else
1055 exp = NULL;
1057 /* Honor attributes derived from exp, except for the alias set
1058 (as builtin stringops may alias with anything) and the size
1059 (as stringops may access multiple array elements). */
1060 if (exp)
1062 set_mem_attributes (mem, exp, 0);
1064 /* Allow the string and memory builtins to overflow from one
1065 field into another, see http://gcc.gnu.org/PR23561.
1066 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1067 memory accessed by the string or memory builtin will fit
1068 within the field. */
1069 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1071 tree mem_expr = MEM_EXPR (mem);
1072 HOST_WIDE_INT offset = -1, length = -1;
1073 tree inner = exp;
1075 while (TREE_CODE (inner) == ARRAY_REF
1076 || TREE_CODE (inner) == NOP_EXPR
1077 || TREE_CODE (inner) == CONVERT_EXPR
1078 || TREE_CODE (inner) == NON_LVALUE_EXPR
1079 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1080 || TREE_CODE (inner) == SAVE_EXPR)
1081 inner = TREE_OPERAND (inner, 0);
1083 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1085 if (MEM_OFFSET (mem)
1086 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1087 offset = INTVAL (MEM_OFFSET (mem));
1089 if (offset >= 0 && len && host_integerp (len, 0))
1090 length = tree_low_cst (len, 0);
1092 while (TREE_CODE (inner) == COMPONENT_REF)
1094 tree field = TREE_OPERAND (inner, 1);
1095 gcc_assert (! DECL_BIT_FIELD (field));
1096 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1097 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1099 if (length >= 0
1100 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1101 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1103 HOST_WIDE_INT size
1104 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1105 /* If we can prove the memory starting at XEXP (mem, 0)
1106 and ending at XEXP (mem, 0) + LENGTH will fit into
1107 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1108 if (offset <= size
1109 && length <= size
1110 && offset + length <= size)
1111 break;
1114 if (offset >= 0
1115 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1116 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1117 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1118 / BITS_PER_UNIT;
1119 else
1121 offset = -1;
1122 length = -1;
1125 mem_expr = TREE_OPERAND (mem_expr, 0);
1126 inner = TREE_OPERAND (inner, 0);
1129 if (mem_expr == NULL)
1130 offset = -1;
1131 if (mem_expr != MEM_EXPR (mem))
1133 set_mem_expr (mem, mem_expr);
1134 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1137 set_mem_alias_set (mem, 0);
1138 set_mem_size (mem, NULL_RTX);
1141 return mem;
1144 /* Built-in functions to perform an untyped call and return. */
1146 /* For each register that may be used for calling a function, this
1147 gives a mode used to copy the register's value. VOIDmode indicates
1148 the register is not used for calling a function. If the machine
1149 has register windows, this gives only the outbound registers.
1150 INCOMING_REGNO gives the corresponding inbound register. */
1151 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1153 /* For each register that may be used for returning values, this gives
1154 a mode used to copy the register's value. VOIDmode indicates the
1155 register is not used for returning values. If the machine has
1156 register windows, this gives only the outbound registers.
1157 INCOMING_REGNO gives the corresponding inbound register. */
1158 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1160 /* For each register that may be used for calling a function, this
1161 gives the offset of that register into the block returned by
1162 __builtin_apply_args. 0 indicates that the register is not
1163 used for calling a function. */
1164 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1166 /* Return the size required for the block returned by __builtin_apply_args,
1167 and initialize apply_args_mode. */
1169 static int
1170 apply_args_size (void)
1172 static int size = -1;
1173 int align;
1174 unsigned int regno;
1175 enum machine_mode mode;
1177 /* The values computed by this function never change. */
1178 if (size < 0)
1180 /* The first value is the incoming arg-pointer. */
1181 size = GET_MODE_SIZE (Pmode);
1183 /* The second value is the structure value address unless this is
1184 passed as an "invisible" first argument. */
1185 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1186 size += GET_MODE_SIZE (Pmode);
1188 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1189 if (FUNCTION_ARG_REGNO_P (regno))
1191 mode = reg_raw_mode[regno];
1193 gcc_assert (mode != VOIDmode);
1195 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1196 if (size % align != 0)
1197 size = CEIL (size, align) * align;
1198 apply_args_reg_offset[regno] = size;
1199 size += GET_MODE_SIZE (mode);
1200 apply_args_mode[regno] = mode;
1202 else
1204 apply_args_mode[regno] = VOIDmode;
1205 apply_args_reg_offset[regno] = 0;
1208 return size;
1211 /* Return the size required for the block returned by __builtin_apply,
1212 and initialize apply_result_mode. */
1214 static int
1215 apply_result_size (void)
1217 static int size = -1;
1218 int align, regno;
1219 enum machine_mode mode;
1221 /* The values computed by this function never change. */
1222 if (size < 0)
1224 size = 0;
1226 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1227 if (FUNCTION_VALUE_REGNO_P (regno))
1229 mode = reg_raw_mode[regno];
1231 gcc_assert (mode != VOIDmode);
1233 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1234 if (size % align != 0)
1235 size = CEIL (size, align) * align;
1236 size += GET_MODE_SIZE (mode);
1237 apply_result_mode[regno] = mode;
1239 else
1240 apply_result_mode[regno] = VOIDmode;
1242 /* Allow targets that use untyped_call and untyped_return to override
1243 the size so that machine-specific information can be stored here. */
1244 #ifdef APPLY_RESULT_SIZE
1245 size = APPLY_RESULT_SIZE;
1246 #endif
1248 return size;
1251 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1252 /* Create a vector describing the result block RESULT. If SAVEP is true,
1253 the result block is used to save the values; otherwise it is used to
1254 restore the values. */
1256 static rtx
1257 result_vector (int savep, rtx result)
1259 int regno, size, align, nelts;
1260 enum machine_mode mode;
1261 rtx reg, mem;
1262 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1264 size = nelts = 0;
1265 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1266 if ((mode = apply_result_mode[regno]) != VOIDmode)
1268 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1269 if (size % align != 0)
1270 size = CEIL (size, align) * align;
1271 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1272 mem = adjust_address (result, mode, size);
1273 savevec[nelts++] = (savep
1274 ? gen_rtx_SET (VOIDmode, mem, reg)
1275 : gen_rtx_SET (VOIDmode, reg, mem));
1276 size += GET_MODE_SIZE (mode);
1278 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1280 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1282 /* Save the state required to perform an untyped call with the same
1283 arguments as were passed to the current function. */
1285 static rtx
1286 expand_builtin_apply_args_1 (void)
1288 rtx registers, tem;
1289 int size, align, regno;
1290 enum machine_mode mode;
1291 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1293 /* Create a block where the arg-pointer, structure value address,
1294 and argument registers can be saved. */
1295 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1297 /* Walk past the arg-pointer and structure value address. */
1298 size = GET_MODE_SIZE (Pmode);
1299 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1300 size += GET_MODE_SIZE (Pmode);
1302 /* Save each register used in calling a function to the block. */
1303 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1304 if ((mode = apply_args_mode[regno]) != VOIDmode)
1306 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1307 if (size % align != 0)
1308 size = CEIL (size, align) * align;
1310 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1312 emit_move_insn (adjust_address (registers, mode, size), tem);
1313 size += GET_MODE_SIZE (mode);
1316 /* Save the arg pointer to the block. */
1317 tem = copy_to_reg (virtual_incoming_args_rtx);
1318 #ifdef STACK_GROWS_DOWNWARD
1319 /* We need the pointer as the caller actually passed them to us, not
1320 as we might have pretended they were passed. Make sure it's a valid
1321 operand, as emit_move_insn isn't expected to handle a PLUS. */
1323 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1324 NULL_RTX);
1325 #endif
1326 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1328 size = GET_MODE_SIZE (Pmode);
1330 /* Save the structure value address unless this is passed as an
1331 "invisible" first argument. */
1332 if (struct_incoming_value)
1334 emit_move_insn (adjust_address (registers, Pmode, size),
1335 copy_to_reg (struct_incoming_value));
1336 size += GET_MODE_SIZE (Pmode);
1339 /* Return the address of the block. */
1340 return copy_addr_to_reg (XEXP (registers, 0));
1343 /* __builtin_apply_args returns block of memory allocated on
1344 the stack into which is stored the arg pointer, structure
1345 value address, static chain, and all the registers that might
1346 possibly be used in performing a function call. The code is
1347 moved to the start of the function so the incoming values are
1348 saved. */
1350 static rtx
1351 expand_builtin_apply_args (void)
1353 /* Don't do __builtin_apply_args more than once in a function.
1354 Save the result of the first call and reuse it. */
1355 if (apply_args_value != 0)
1356 return apply_args_value;
1358 /* When this function is called, it means that registers must be
1359 saved on entry to this function. So we migrate the
1360 call to the first insn of this function. */
1361 rtx temp;
1362 rtx seq;
1364 start_sequence ();
1365 temp = expand_builtin_apply_args_1 ();
1366 seq = get_insns ();
1367 end_sequence ();
1369 apply_args_value = temp;
1371 /* Put the insns after the NOTE that starts the function.
1372 If this is inside a start_sequence, make the outer-level insn
1373 chain current, so the code is placed at the start of the
1374 function. */
1375 push_topmost_sequence ();
1376 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1377 pop_topmost_sequence ();
1378 return temp;
1382 /* Perform an untyped call and save the state required to perform an
1383 untyped return of whatever value was returned by the given function. */
1385 static rtx
1386 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1388 int size, align, regno;
1389 enum machine_mode mode;
1390 rtx incoming_args, result, reg, dest, src, call_insn;
1391 rtx old_stack_level = 0;
1392 rtx call_fusage = 0;
1393 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1395 arguments = convert_memory_address (Pmode, arguments);
1397 /* Create a block where the return registers can be saved. */
1398 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1400 /* Fetch the arg pointer from the ARGUMENTS block. */
1401 incoming_args = gen_reg_rtx (Pmode);
1402 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1403 #ifndef STACK_GROWS_DOWNWARD
1404 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1405 incoming_args, 0, OPTAB_LIB_WIDEN);
1406 #endif
1408 /* Push a new argument block and copy the arguments. Do not allow
1409 the (potential) memcpy call below to interfere with our stack
1410 manipulations. */
1411 do_pending_stack_adjust ();
1412 NO_DEFER_POP;
1414 /* Save the stack with nonlocal if available. */
1415 #ifdef HAVE_save_stack_nonlocal
1416 if (HAVE_save_stack_nonlocal)
1417 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1418 else
1419 #endif
1420 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1422 /* Allocate a block of memory onto the stack and copy the memory
1423 arguments to the outgoing arguments address. */
1424 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1425 dest = virtual_outgoing_args_rtx;
1426 #ifndef STACK_GROWS_DOWNWARD
1427 if (GET_CODE (argsize) == CONST_INT)
1428 dest = plus_constant (dest, -INTVAL (argsize));
1429 else
1430 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1431 #endif
1432 dest = gen_rtx_MEM (BLKmode, dest);
1433 set_mem_align (dest, PARM_BOUNDARY);
1434 src = gen_rtx_MEM (BLKmode, incoming_args);
1435 set_mem_align (src, PARM_BOUNDARY);
1436 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1438 /* Refer to the argument block. */
1439 apply_args_size ();
1440 arguments = gen_rtx_MEM (BLKmode, arguments);
1441 set_mem_align (arguments, PARM_BOUNDARY);
1443 /* Walk past the arg-pointer and structure value address. */
1444 size = GET_MODE_SIZE (Pmode);
1445 if (struct_value)
1446 size += GET_MODE_SIZE (Pmode);
1448 /* Restore each of the registers previously saved. Make USE insns
1449 for each of these registers for use in making the call. */
1450 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1451 if ((mode = apply_args_mode[regno]) != VOIDmode)
1453 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1454 if (size % align != 0)
1455 size = CEIL (size, align) * align;
1456 reg = gen_rtx_REG (mode, regno);
1457 emit_move_insn (reg, adjust_address (arguments, mode, size));
1458 use_reg (&call_fusage, reg);
1459 size += GET_MODE_SIZE (mode);
1462 /* Restore the structure value address unless this is passed as an
1463 "invisible" first argument. */
1464 size = GET_MODE_SIZE (Pmode);
1465 if (struct_value)
1467 rtx value = gen_reg_rtx (Pmode);
1468 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1469 emit_move_insn (struct_value, value);
1470 if (REG_P (struct_value))
1471 use_reg (&call_fusage, struct_value);
1472 size += GET_MODE_SIZE (Pmode);
1475 /* All arguments and registers used for the call are set up by now! */
1476 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1478 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1479 and we don't want to load it into a register as an optimization,
1480 because prepare_call_address already did it if it should be done. */
1481 if (GET_CODE (function) != SYMBOL_REF)
1482 function = memory_address (FUNCTION_MODE, function);
1484 /* Generate the actual call instruction and save the return value. */
1485 #ifdef HAVE_untyped_call
1486 if (HAVE_untyped_call)
1487 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1488 result, result_vector (1, result)));
1489 else
1490 #endif
1491 #ifdef HAVE_call_value
1492 if (HAVE_call_value)
1494 rtx valreg = 0;
1496 /* Locate the unique return register. It is not possible to
1497 express a call that sets more than one return register using
1498 call_value; use untyped_call for that. In fact, untyped_call
1499 only needs to save the return registers in the given block. */
1500 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1501 if ((mode = apply_result_mode[regno]) != VOIDmode)
1503 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1505 valreg = gen_rtx_REG (mode, regno);
1508 emit_call_insn (GEN_CALL_VALUE (valreg,
1509 gen_rtx_MEM (FUNCTION_MODE, function),
1510 const0_rtx, NULL_RTX, const0_rtx));
1512 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1514 else
1515 #endif
1516 gcc_unreachable ();
1518 /* Find the CALL insn we just emitted, and attach the register usage
1519 information. */
1520 call_insn = last_call_insn ();
1521 add_function_usage_to (call_insn, call_fusage);
1523 /* Restore the stack. */
1524 #ifdef HAVE_save_stack_nonlocal
1525 if (HAVE_save_stack_nonlocal)
1526 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1527 else
1528 #endif
1529 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1531 OK_DEFER_POP;
1533 /* Return the address of the result block. */
1534 result = copy_addr_to_reg (XEXP (result, 0));
1535 return convert_memory_address (ptr_mode, result);
1538 /* Perform an untyped return. */
1540 static void
1541 expand_builtin_return (rtx result)
1543 int size, align, regno;
1544 enum machine_mode mode;
1545 rtx reg;
1546 rtx call_fusage = 0;
1548 result = convert_memory_address (Pmode, result);
1550 apply_result_size ();
1551 result = gen_rtx_MEM (BLKmode, result);
1553 #ifdef HAVE_untyped_return
1554 if (HAVE_untyped_return)
1556 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1557 emit_barrier ();
1558 return;
1560 #endif
1562 /* Restore the return value and note that each value is used. */
1563 size = 0;
1564 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1565 if ((mode = apply_result_mode[regno]) != VOIDmode)
1567 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1568 if (size % align != 0)
1569 size = CEIL (size, align) * align;
1570 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1571 emit_move_insn (reg, adjust_address (result, mode, size));
1573 push_to_sequence (call_fusage);
1574 emit_insn (gen_rtx_USE (VOIDmode, reg));
1575 call_fusage = get_insns ();
1576 end_sequence ();
1577 size += GET_MODE_SIZE (mode);
1580 /* Put the USE insns before the return. */
1581 emit_insn (call_fusage);
1583 /* Return whatever values was restored by jumping directly to the end
1584 of the function. */
1585 expand_naked_return ();
1588 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1590 static enum type_class
1591 type_to_class (tree type)
1593 switch (TREE_CODE (type))
1595 case VOID_TYPE: return void_type_class;
1596 case INTEGER_TYPE: return integer_type_class;
1597 case ENUMERAL_TYPE: return enumeral_type_class;
1598 case BOOLEAN_TYPE: return boolean_type_class;
1599 case POINTER_TYPE: return pointer_type_class;
1600 case REFERENCE_TYPE: return reference_type_class;
1601 case OFFSET_TYPE: return offset_type_class;
1602 case REAL_TYPE: return real_type_class;
1603 case COMPLEX_TYPE: return complex_type_class;
1604 case FUNCTION_TYPE: return function_type_class;
1605 case METHOD_TYPE: return method_type_class;
1606 case RECORD_TYPE: return record_type_class;
1607 case UNION_TYPE:
1608 case QUAL_UNION_TYPE: return union_type_class;
1609 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1610 ? string_type_class : array_type_class);
1611 case LANG_TYPE: return lang_type_class;
1612 default: return no_type_class;
1616 /* Expand a call to __builtin_classify_type with arguments found in
1617 ARGLIST. */
1619 static rtx
1620 expand_builtin_classify_type (tree arglist)
1622 if (arglist != 0)
1623 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1624 return GEN_INT (no_type_class);
1627 /* This helper macro, meant to be used in mathfn_built_in below,
1628 determines which among a set of three builtin math functions is
1629 appropriate for a given type mode. The `F' and `L' cases are
1630 automatically generated from the `double' case. */
1631 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1632 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1633 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1634 fcodel = BUILT_IN_MATHFN##L ; break;
1636 /* Return mathematic function equivalent to FN but operating directly
1637 on TYPE, if available. If we can't do the conversion, return zero. */
1638 tree
1639 mathfn_built_in (tree type, enum built_in_function fn)
1641 enum built_in_function fcode, fcodef, fcodel;
1643 switch (fn)
1645 CASE_MATHFN (BUILT_IN_ACOS)
1646 CASE_MATHFN (BUILT_IN_ACOSH)
1647 CASE_MATHFN (BUILT_IN_ASIN)
1648 CASE_MATHFN (BUILT_IN_ASINH)
1649 CASE_MATHFN (BUILT_IN_ATAN)
1650 CASE_MATHFN (BUILT_IN_ATAN2)
1651 CASE_MATHFN (BUILT_IN_ATANH)
1652 CASE_MATHFN (BUILT_IN_CBRT)
1653 CASE_MATHFN (BUILT_IN_CEIL)
1654 CASE_MATHFN (BUILT_IN_COPYSIGN)
1655 CASE_MATHFN (BUILT_IN_COS)
1656 CASE_MATHFN (BUILT_IN_COSH)
1657 CASE_MATHFN (BUILT_IN_DREM)
1658 CASE_MATHFN (BUILT_IN_ERF)
1659 CASE_MATHFN (BUILT_IN_ERFC)
1660 CASE_MATHFN (BUILT_IN_EXP)
1661 CASE_MATHFN (BUILT_IN_EXP10)
1662 CASE_MATHFN (BUILT_IN_EXP2)
1663 CASE_MATHFN (BUILT_IN_EXPM1)
1664 CASE_MATHFN (BUILT_IN_FABS)
1665 CASE_MATHFN (BUILT_IN_FDIM)
1666 CASE_MATHFN (BUILT_IN_FLOOR)
1667 CASE_MATHFN (BUILT_IN_FMA)
1668 CASE_MATHFN (BUILT_IN_FMAX)
1669 CASE_MATHFN (BUILT_IN_FMIN)
1670 CASE_MATHFN (BUILT_IN_FMOD)
1671 CASE_MATHFN (BUILT_IN_FREXP)
1672 CASE_MATHFN (BUILT_IN_GAMMA)
1673 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1674 CASE_MATHFN (BUILT_IN_HYPOT)
1675 CASE_MATHFN (BUILT_IN_ILOGB)
1676 CASE_MATHFN (BUILT_IN_INF)
1677 CASE_MATHFN (BUILT_IN_J0)
1678 CASE_MATHFN (BUILT_IN_J1)
1679 CASE_MATHFN (BUILT_IN_JN)
1680 CASE_MATHFN (BUILT_IN_LCEIL)
1681 CASE_MATHFN (BUILT_IN_LDEXP)
1682 CASE_MATHFN (BUILT_IN_LFLOOR)
1683 CASE_MATHFN (BUILT_IN_LGAMMA)
1684 CASE_MATHFN (BUILT_IN_LLCEIL)
1685 CASE_MATHFN (BUILT_IN_LLFLOOR)
1686 CASE_MATHFN (BUILT_IN_LLRINT)
1687 CASE_MATHFN (BUILT_IN_LLROUND)
1688 CASE_MATHFN (BUILT_IN_LOG)
1689 CASE_MATHFN (BUILT_IN_LOG10)
1690 CASE_MATHFN (BUILT_IN_LOG1P)
1691 CASE_MATHFN (BUILT_IN_LOG2)
1692 CASE_MATHFN (BUILT_IN_LOGB)
1693 CASE_MATHFN (BUILT_IN_LRINT)
1694 CASE_MATHFN (BUILT_IN_LROUND)
1695 CASE_MATHFN (BUILT_IN_MODF)
1696 CASE_MATHFN (BUILT_IN_NAN)
1697 CASE_MATHFN (BUILT_IN_NANS)
1698 CASE_MATHFN (BUILT_IN_NEARBYINT)
1699 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1700 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1701 CASE_MATHFN (BUILT_IN_POW)
1702 CASE_MATHFN (BUILT_IN_POWI)
1703 CASE_MATHFN (BUILT_IN_POW10)
1704 CASE_MATHFN (BUILT_IN_REMAINDER)
1705 CASE_MATHFN (BUILT_IN_REMQUO)
1706 CASE_MATHFN (BUILT_IN_RINT)
1707 CASE_MATHFN (BUILT_IN_ROUND)
1708 CASE_MATHFN (BUILT_IN_SCALB)
1709 CASE_MATHFN (BUILT_IN_SCALBLN)
1710 CASE_MATHFN (BUILT_IN_SCALBN)
1711 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1712 CASE_MATHFN (BUILT_IN_SIN)
1713 CASE_MATHFN (BUILT_IN_SINCOS)
1714 CASE_MATHFN (BUILT_IN_SINH)
1715 CASE_MATHFN (BUILT_IN_SQRT)
1716 CASE_MATHFN (BUILT_IN_TAN)
1717 CASE_MATHFN (BUILT_IN_TANH)
1718 CASE_MATHFN (BUILT_IN_TGAMMA)
1719 CASE_MATHFN (BUILT_IN_TRUNC)
1720 CASE_MATHFN (BUILT_IN_Y0)
1721 CASE_MATHFN (BUILT_IN_Y1)
1722 CASE_MATHFN (BUILT_IN_YN)
1724 default:
1725 return 0;
1728 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1729 return implicit_built_in_decls[fcode];
1730 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1731 return implicit_built_in_decls[fcodef];
1732 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1733 return implicit_built_in_decls[fcodel];
1734 else
1735 return 0;
1738 /* If errno must be maintained, expand the RTL to check if the result,
1739 TARGET, of a built-in function call, EXP, is NaN, and if so set
1740 errno to EDOM. */
1742 static void
1743 expand_errno_check (tree exp, rtx target)
1745 rtx lab = gen_label_rtx ();
1747 /* Test the result; if it is NaN, set errno=EDOM because
1748 the argument was not in the domain. */
1749 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1750 0, lab);
1752 #ifdef TARGET_EDOM
1753 /* If this built-in doesn't throw an exception, set errno directly. */
1754 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1756 #ifdef GEN_ERRNO_RTX
1757 rtx errno_rtx = GEN_ERRNO_RTX;
1758 #else
1759 rtx errno_rtx
1760 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1761 #endif
1762 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1763 emit_label (lab);
1764 return;
1766 #endif
1768 /* We can't set errno=EDOM directly; let the library call do it.
1769 Pop the arguments right away in case the call gets deleted. */
1770 NO_DEFER_POP;
1771 expand_call (exp, target, 0);
1772 OK_DEFER_POP;
1773 emit_label (lab);
1777 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1778 Return 0 if a normal call should be emitted rather than expanding the
1779 function in-line. EXP is the expression that is a call to the builtin
1780 function; if convenient, the result should be placed in TARGET.
1781 SUBTARGET may be used as the target for computing one of EXP's operands. */
1783 static rtx
1784 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1786 optab builtin_optab;
1787 rtx op0, insns, before_call;
1788 tree fndecl = get_callee_fndecl (exp);
1789 tree arglist = TREE_OPERAND (exp, 1);
1790 enum machine_mode mode;
1791 bool errno_set = false;
1792 tree arg, narg;
1794 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1795 return 0;
1797 arg = TREE_VALUE (arglist);
1799 switch (DECL_FUNCTION_CODE (fndecl))
1801 CASE_FLT_FN (BUILT_IN_SQRT):
1802 errno_set = ! tree_expr_nonnegative_p (arg);
1803 builtin_optab = sqrt_optab;
1804 break;
1805 CASE_FLT_FN (BUILT_IN_EXP):
1806 errno_set = true; builtin_optab = exp_optab; break;
1807 CASE_FLT_FN (BUILT_IN_EXP10):
1808 CASE_FLT_FN (BUILT_IN_POW10):
1809 errno_set = true; builtin_optab = exp10_optab; break;
1810 CASE_FLT_FN (BUILT_IN_EXP2):
1811 errno_set = true; builtin_optab = exp2_optab; break;
1812 CASE_FLT_FN (BUILT_IN_EXPM1):
1813 errno_set = true; builtin_optab = expm1_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOGB):
1815 errno_set = true; builtin_optab = logb_optab; break;
1816 CASE_FLT_FN (BUILT_IN_ILOGB):
1817 errno_set = true; builtin_optab = ilogb_optab; break;
1818 CASE_FLT_FN (BUILT_IN_LOG):
1819 errno_set = true; builtin_optab = log_optab; break;
1820 CASE_FLT_FN (BUILT_IN_LOG10):
1821 errno_set = true; builtin_optab = log10_optab; break;
1822 CASE_FLT_FN (BUILT_IN_LOG2):
1823 errno_set = true; builtin_optab = log2_optab; break;
1824 CASE_FLT_FN (BUILT_IN_LOG1P):
1825 errno_set = true; builtin_optab = log1p_optab; break;
1826 CASE_FLT_FN (BUILT_IN_ASIN):
1827 builtin_optab = asin_optab; break;
1828 CASE_FLT_FN (BUILT_IN_ACOS):
1829 builtin_optab = acos_optab; break;
1830 CASE_FLT_FN (BUILT_IN_TAN):
1831 builtin_optab = tan_optab; break;
1832 CASE_FLT_FN (BUILT_IN_ATAN):
1833 builtin_optab = atan_optab; break;
1834 CASE_FLT_FN (BUILT_IN_FLOOR):
1835 builtin_optab = floor_optab; break;
1836 CASE_FLT_FN (BUILT_IN_CEIL):
1837 builtin_optab = ceil_optab; break;
1838 CASE_FLT_FN (BUILT_IN_TRUNC):
1839 builtin_optab = btrunc_optab; break;
1840 CASE_FLT_FN (BUILT_IN_ROUND):
1841 builtin_optab = round_optab; break;
1842 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1843 builtin_optab = nearbyint_optab;
1844 if (flag_trapping_math)
1845 break;
1846 /* Else fallthrough and expand as rint. */
1847 CASE_FLT_FN (BUILT_IN_RINT):
1848 builtin_optab = rint_optab; break;
1849 default:
1850 gcc_unreachable ();
1853 /* Make a suitable register to place result in. */
1854 mode = TYPE_MODE (TREE_TYPE (exp));
1856 if (! flag_errno_math || ! HONOR_NANS (mode))
1857 errno_set = false;
1859 /* Before working hard, check whether the instruction is available. */
1860 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1862 target = gen_reg_rtx (mode);
1864 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1865 need to expand the argument again. This way, we will not perform
1866 side-effects more the once. */
1867 narg = builtin_save_expr (arg);
1868 if (narg != arg)
1870 arg = narg;
1871 arglist = build_tree_list (NULL_TREE, arg);
1872 exp = build_function_call_expr (fndecl, arglist);
1875 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1877 start_sequence ();
1879 /* Compute into TARGET.
1880 Set TARGET to wherever the result comes back. */
1881 target = expand_unop (mode, builtin_optab, op0, target, 0);
1883 if (target != 0)
1885 if (errno_set)
1886 expand_errno_check (exp, target);
1888 /* Output the entire sequence. */
1889 insns = get_insns ();
1890 end_sequence ();
1891 emit_insn (insns);
1892 return target;
1895 /* If we were unable to expand via the builtin, stop the sequence
1896 (without outputting the insns) and call to the library function
1897 with the stabilized argument list. */
1898 end_sequence ();
1901 before_call = get_last_insn ();
1903 target = expand_call (exp, target, target == const0_rtx);
1905 /* If this is a sqrt operation and we don't care about errno, try to
1906 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1907 This allows the semantics of the libcall to be visible to the RTL
1908 optimizers. */
1909 if (builtin_optab == sqrt_optab && !errno_set)
1911 /* Search backwards through the insns emitted by expand_call looking
1912 for the instruction with the REG_RETVAL note. */
1913 rtx last = get_last_insn ();
1914 while (last != before_call)
1916 if (find_reg_note (last, REG_RETVAL, NULL))
1918 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1919 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1920 two elements, i.e. symbol_ref(sqrt) and the operand. */
1921 if (note
1922 && GET_CODE (note) == EXPR_LIST
1923 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1924 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1925 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1927 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1928 /* Check operand is a register with expected mode. */
1929 if (operand
1930 && REG_P (operand)
1931 && GET_MODE (operand) == mode)
1933 /* Replace the REG_EQUAL note with a SQRT rtx. */
1934 rtx equiv = gen_rtx_SQRT (mode, operand);
1935 set_unique_reg_note (last, REG_EQUAL, equiv);
1938 break;
1940 last = PREV_INSN (last);
1944 return target;
1947 /* Expand a call to the builtin binary math functions (pow and atan2).
1948 Return 0 if a normal call should be emitted rather than expanding the
1949 function in-line. EXP is the expression that is a call to the builtin
1950 function; if convenient, the result should be placed in TARGET.
1951 SUBTARGET may be used as the target for computing one of EXP's
1952 operands. */
1954 static rtx
1955 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1957 optab builtin_optab;
1958 rtx op0, op1, insns;
1959 int op1_type = REAL_TYPE;
1960 tree fndecl = get_callee_fndecl (exp);
1961 tree arglist = TREE_OPERAND (exp, 1);
1962 tree arg0, arg1, temp, narg;
1963 enum machine_mode mode;
1964 bool errno_set = true;
1965 bool stable = true;
1967 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1968 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1969 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1970 op1_type = INTEGER_TYPE;
1972 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1973 return 0;
1975 arg0 = TREE_VALUE (arglist);
1976 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1978 switch (DECL_FUNCTION_CODE (fndecl))
1980 CASE_FLT_FN (BUILT_IN_POW):
1981 builtin_optab = pow_optab; break;
1982 CASE_FLT_FN (BUILT_IN_ATAN2):
1983 builtin_optab = atan2_optab; break;
1984 CASE_FLT_FN (BUILT_IN_LDEXP):
1985 builtin_optab = ldexp_optab; break;
1986 CASE_FLT_FN (BUILT_IN_FMOD):
1987 builtin_optab = fmod_optab; break;
1988 CASE_FLT_FN (BUILT_IN_REMAINDER):
1989 CASE_FLT_FN (BUILT_IN_DREM):
1990 builtin_optab = remainder_optab; break;
1991 default:
1992 gcc_unreachable ();
1995 /* Make a suitable register to place result in. */
1996 mode = TYPE_MODE (TREE_TYPE (exp));
1998 /* Before working hard, check whether the instruction is available. */
1999 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2000 return 0;
2002 target = gen_reg_rtx (mode);
2004 if (! flag_errno_math || ! HONOR_NANS (mode))
2005 errno_set = false;
2007 /* Always stabilize the argument list. */
2008 narg = builtin_save_expr (arg1);
2009 if (narg != arg1)
2011 arg1 = narg;
2012 temp = build_tree_list (NULL_TREE, narg);
2013 stable = false;
2015 else
2016 temp = TREE_CHAIN (arglist);
2018 narg = builtin_save_expr (arg0);
2019 if (narg != arg0)
2021 arg0 = narg;
2022 arglist = tree_cons (NULL_TREE, narg, temp);
2023 stable = false;
2025 else if (! stable)
2026 arglist = tree_cons (NULL_TREE, arg0, temp);
2028 if (! stable)
2029 exp = build_function_call_expr (fndecl, arglist);
2031 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2032 op1 = expand_normal (arg1);
2034 start_sequence ();
2036 /* Compute into TARGET.
2037 Set TARGET to wherever the result comes back. */
2038 target = expand_binop (mode, builtin_optab, op0, op1,
2039 target, 0, OPTAB_DIRECT);
2041 /* If we were unable to expand via the builtin, stop the sequence
2042 (without outputting the insns) and call to the library function
2043 with the stabilized argument list. */
2044 if (target == 0)
2046 end_sequence ();
2047 return expand_call (exp, target, target == const0_rtx);
2050 if (errno_set)
2051 expand_errno_check (exp, target);
2053 /* Output the entire sequence. */
2054 insns = get_insns ();
2055 end_sequence ();
2056 emit_insn (insns);
2058 return target;
2061 /* Expand a call to the builtin sin and cos math functions.
2062 Return 0 if a normal call should be emitted rather than expanding the
2063 function in-line. EXP is the expression that is a call to the builtin
2064 function; if convenient, the result should be placed in TARGET.
2065 SUBTARGET may be used as the target for computing one of EXP's
2066 operands. */
2068 static rtx
2069 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2071 optab builtin_optab;
2072 rtx op0, insns;
2073 tree fndecl = get_callee_fndecl (exp);
2074 tree arglist = TREE_OPERAND (exp, 1);
2075 enum machine_mode mode;
2076 tree arg, narg;
2078 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2079 return 0;
2081 arg = TREE_VALUE (arglist);
2083 switch (DECL_FUNCTION_CODE (fndecl))
2085 CASE_FLT_FN (BUILT_IN_SIN):
2086 CASE_FLT_FN (BUILT_IN_COS):
2087 builtin_optab = sincos_optab; break;
2088 default:
2089 gcc_unreachable ();
2092 /* Make a suitable register to place result in. */
2093 mode = TYPE_MODE (TREE_TYPE (exp));
2095 /* Check if sincos insn is available, otherwise fallback
2096 to sin or cos insn. */
2097 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2098 switch (DECL_FUNCTION_CODE (fndecl))
2100 CASE_FLT_FN (BUILT_IN_SIN):
2101 builtin_optab = sin_optab; break;
2102 CASE_FLT_FN (BUILT_IN_COS):
2103 builtin_optab = cos_optab; break;
2104 default:
2105 gcc_unreachable ();
2108 /* Before working hard, check whether the instruction is available. */
2109 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2111 target = gen_reg_rtx (mode);
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 narg = save_expr (arg);
2117 if (narg != arg)
2119 arg = narg;
2120 arglist = build_tree_list (NULL_TREE, arg);
2121 exp = build_function_call_expr (fndecl, arglist);
2124 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2126 start_sequence ();
2128 /* Compute into TARGET.
2129 Set TARGET to wherever the result comes back. */
2130 if (builtin_optab == sincos_optab)
2132 int result;
2134 switch (DECL_FUNCTION_CODE (fndecl))
2136 CASE_FLT_FN (BUILT_IN_SIN):
2137 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2138 break;
2139 CASE_FLT_FN (BUILT_IN_COS):
2140 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2141 break;
2142 default:
2143 gcc_unreachable ();
2145 gcc_assert (result);
2147 else
2149 target = expand_unop (mode, builtin_optab, op0, target, 0);
2152 if (target != 0)
2154 /* Output the entire sequence. */
2155 insns = get_insns ();
2156 end_sequence ();
2157 emit_insn (insns);
2158 return target;
2161 /* If we were unable to expand via the builtin, stop the sequence
2162 (without outputting the insns) and call to the library function
2163 with the stabilized argument list. */
2164 end_sequence ();
2167 target = expand_call (exp, target, target == const0_rtx);
2169 return target;
2172 /* Expand a call to the builtin sincos math function.
2173 Return 0 if a normal call should be emitted rather than expanding the
2174 function in-line. EXP is the expression that is a call to the builtin
2175 function. */
2177 static rtx
2178 expand_builtin_sincos (tree exp)
2180 rtx op0, op1, op2, target1, target2;
2181 tree arglist = TREE_OPERAND (exp, 1);
2182 enum machine_mode mode;
2183 tree arg, sinp, cosp;
2184 int result;
2186 if (!validate_arglist (arglist, REAL_TYPE,
2187 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2188 return 0;
2190 arg = TREE_VALUE (arglist);
2191 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2192 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2194 /* Make a suitable register to place result in. */
2195 mode = TYPE_MODE (TREE_TYPE (arg));
2197 /* Check if sincos insn is available, otherwise emit the call. */
2198 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2199 return NULL_RTX;
2201 target1 = gen_reg_rtx (mode);
2202 target2 = gen_reg_rtx (mode);
2204 op0 = expand_normal (arg);
2205 op1 = expand_normal (build_fold_indirect_ref (sinp));
2206 op2 = expand_normal (build_fold_indirect_ref (cosp));
2208 /* Compute into target1 and target2.
2209 Set TARGET to wherever the result comes back. */
2210 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2211 gcc_assert (result);
2213 /* Move target1 and target2 to the memory locations indicated
2214 by op1 and op2. */
2215 emit_move_insn (op1, target1);
2216 emit_move_insn (op2, target2);
2218 return const0_rtx;
2221 /* Expand a call to one of the builtin rounding functions gcc defines
2222 as an extension (lfloor and lceil). As these are gcc extensions we
2223 do not need to worry about setting errno to EDOM.
2224 If expanding via optab fails, lower expression to (int)(floor(x)).
2225 EXP is the expression that is a call to the builtin function;
2226 if convenient, the result should be placed in TARGET. SUBTARGET may
2227 be used as the target for computing one of EXP's operands. */
2229 static rtx
2230 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2232 convert_optab builtin_optab;
2233 rtx op0, insns, tmp;
2234 tree fndecl = get_callee_fndecl (exp);
2235 tree arglist = TREE_OPERAND (exp, 1);
2236 enum built_in_function fallback_fn;
2237 tree fallback_fndecl;
2238 enum machine_mode mode;
2239 tree arg, narg;
2241 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2242 gcc_unreachable ();
2244 arg = TREE_VALUE (arglist);
2246 switch (DECL_FUNCTION_CODE (fndecl))
2248 CASE_FLT_FN (BUILT_IN_LCEIL):
2249 CASE_FLT_FN (BUILT_IN_LLCEIL):
2250 builtin_optab = lceil_optab;
2251 fallback_fn = BUILT_IN_CEIL;
2252 break;
2254 CASE_FLT_FN (BUILT_IN_LFLOOR):
2255 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2256 builtin_optab = lfloor_optab;
2257 fallback_fn = BUILT_IN_FLOOR;
2258 break;
2260 default:
2261 gcc_unreachable ();
2264 /* Make a suitable register to place result in. */
2265 mode = TYPE_MODE (TREE_TYPE (exp));
2267 target = gen_reg_rtx (mode);
2269 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2270 need to expand the argument again. This way, we will not perform
2271 side-effects more the once. */
2272 narg = builtin_save_expr (arg);
2273 if (narg != arg)
2275 arg = narg;
2276 arglist = build_tree_list (NULL_TREE, arg);
2277 exp = build_function_call_expr (fndecl, arglist);
2280 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2282 start_sequence ();
2284 /* Compute into TARGET. */
2285 if (expand_sfix_optab (target, op0, builtin_optab))
2287 /* Output the entire sequence. */
2288 insns = get_insns ();
2289 end_sequence ();
2290 emit_insn (insns);
2291 return target;
2294 /* If we were unable to expand via the builtin, stop the sequence
2295 (without outputting the insns). */
2296 end_sequence ();
2298 /* Fall back to floating point rounding optab. */
2299 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2300 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2301 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2302 gcc_assert (fallback_fndecl != NULL_TREE);
2303 exp = build_function_call_expr (fallback_fndecl, arglist);
2305 tmp = expand_normal (exp);
2307 /* Truncate the result of floating point optab to integer
2308 via expand_fix (). */
2309 target = gen_reg_rtx (mode);
2310 expand_fix (target, tmp, 0);
2312 return target;
2315 /* Expand a call to one of the builtin math functions doing integer
2316 conversion (lrint).
2317 Return 0 if a normal call should be emitted rather than expanding the
2318 function in-line. EXP is the expression that is a call to the builtin
2319 function; if convenient, the result should be placed in TARGET.
2320 SUBTARGET may be used as the target for computing one of EXP's operands. */
2322 static rtx
2323 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2325 convert_optab builtin_optab;
2326 rtx op0, insns;
2327 tree fndecl = get_callee_fndecl (exp);
2328 tree arglist = TREE_OPERAND (exp, 1);
2329 enum machine_mode mode;
2330 tree arg, narg;
2332 /* There's no easy way to detect the case we need to set EDOM. */
2333 if (flag_errno_math)
2334 return NULL_RTX;
2336 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2337 return NULL_RTX;
2339 arg = TREE_VALUE (arglist);
2341 switch (DECL_FUNCTION_CODE (fndecl))
2343 CASE_FLT_FN (BUILT_IN_LRINT):
2344 CASE_FLT_FN (BUILT_IN_LLRINT):
2345 builtin_optab = lrint_optab; break;
2346 CASE_FLT_FN (BUILT_IN_LROUND):
2347 CASE_FLT_FN (BUILT_IN_LLROUND):
2348 builtin_optab = lround_optab; break;
2349 default:
2350 gcc_unreachable ();
2353 /* Make a suitable register to place result in. */
2354 mode = TYPE_MODE (TREE_TYPE (exp));
2356 target = gen_reg_rtx (mode);
2358 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2359 need to expand the argument again. This way, we will not perform
2360 side-effects more the once. */
2361 narg = builtin_save_expr (arg);
2362 if (narg != arg)
2364 arg = narg;
2365 arglist = build_tree_list (NULL_TREE, arg);
2366 exp = build_function_call_expr (fndecl, arglist);
2369 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2371 start_sequence ();
2373 if (expand_sfix_optab (target, op0, builtin_optab))
2375 /* Output the entire sequence. */
2376 insns = get_insns ();
2377 end_sequence ();
2378 emit_insn (insns);
2379 return target;
2382 /* If we were unable to expand via the builtin, stop the sequence
2383 (without outputting the insns) and call to the library function
2384 with the stabilized argument list. */
2385 end_sequence ();
2387 target = expand_call (exp, target, target == const0_rtx);
2389 return target;
2392 /* To evaluate powi(x,n), the floating point value x raised to the
2393 constant integer exponent n, we use a hybrid algorithm that
2394 combines the "window method" with look-up tables. For an
2395 introduction to exponentiation algorithms and "addition chains",
2396 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2397 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2398 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2399 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2401 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2402 multiplications to inline before calling the system library's pow
2403 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2404 so this default never requires calling pow, powf or powl. */
2406 #ifndef POWI_MAX_MULTS
2407 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2408 #endif
2410 /* The size of the "optimal power tree" lookup table. All
2411 exponents less than this value are simply looked up in the
2412 powi_table below. This threshold is also used to size the
2413 cache of pseudo registers that hold intermediate results. */
2414 #define POWI_TABLE_SIZE 256
2416 /* The size, in bits of the window, used in the "window method"
2417 exponentiation algorithm. This is equivalent to a radix of
2418 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2419 #define POWI_WINDOW_SIZE 3
2421 /* The following table is an efficient representation of an
2422 "optimal power tree". For each value, i, the corresponding
2423 value, j, in the table states than an optimal evaluation
2424 sequence for calculating pow(x,i) can be found by evaluating
2425 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2426 100 integers is given in Knuth's "Seminumerical algorithms". */
2428 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2430 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2431 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2432 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2433 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2434 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2435 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2436 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2437 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2438 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2439 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2440 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2441 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2442 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2443 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2444 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2445 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2446 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2447 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2448 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2449 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2450 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2451 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2452 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2453 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2454 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2455 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2456 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2457 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2458 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2459 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2460 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2461 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2465 /* Return the number of multiplications required to calculate
2466 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2467 subroutine of powi_cost. CACHE is an array indicating
2468 which exponents have already been calculated. */
2470 static int
2471 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2473 /* If we've already calculated this exponent, then this evaluation
2474 doesn't require any additional multiplications. */
2475 if (cache[n])
2476 return 0;
2478 cache[n] = true;
2479 return powi_lookup_cost (n - powi_table[n], cache)
2480 + powi_lookup_cost (powi_table[n], cache) + 1;
2483 /* Return the number of multiplications required to calculate
2484 powi(x,n) for an arbitrary x, given the exponent N. This
2485 function needs to be kept in sync with expand_powi below. */
2487 static int
2488 powi_cost (HOST_WIDE_INT n)
2490 bool cache[POWI_TABLE_SIZE];
2491 unsigned HOST_WIDE_INT digit;
2492 unsigned HOST_WIDE_INT val;
2493 int result;
2495 if (n == 0)
2496 return 0;
2498 /* Ignore the reciprocal when calculating the cost. */
2499 val = (n < 0) ? -n : n;
2501 /* Initialize the exponent cache. */
2502 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2503 cache[1] = true;
2505 result = 0;
2507 while (val >= POWI_TABLE_SIZE)
2509 if (val & 1)
2511 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2512 result += powi_lookup_cost (digit, cache)
2513 + POWI_WINDOW_SIZE + 1;
2514 val >>= POWI_WINDOW_SIZE;
2516 else
2518 val >>= 1;
2519 result++;
2523 return result + powi_lookup_cost (val, cache);
2526 /* Recursive subroutine of expand_powi. This function takes the array,
2527 CACHE, of already calculated exponents and an exponent N and returns
2528 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2530 static rtx
2531 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2533 unsigned HOST_WIDE_INT digit;
2534 rtx target, result;
2535 rtx op0, op1;
2537 if (n < POWI_TABLE_SIZE)
2539 if (cache[n])
2540 return cache[n];
2542 target = gen_reg_rtx (mode);
2543 cache[n] = target;
2545 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2546 op1 = expand_powi_1 (mode, powi_table[n], cache);
2548 else if (n & 1)
2550 target = gen_reg_rtx (mode);
2551 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2552 op0 = expand_powi_1 (mode, n - digit, cache);
2553 op1 = expand_powi_1 (mode, digit, cache);
2555 else
2557 target = gen_reg_rtx (mode);
2558 op0 = expand_powi_1 (mode, n >> 1, cache);
2559 op1 = op0;
2562 result = expand_mult (mode, op0, op1, target, 0);
2563 if (result != target)
2564 emit_move_insn (target, result);
2565 return target;
2568 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2569 floating point operand in mode MODE, and N is the exponent. This
2570 function needs to be kept in sync with powi_cost above. */
2572 static rtx
2573 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2575 unsigned HOST_WIDE_INT val;
2576 rtx cache[POWI_TABLE_SIZE];
2577 rtx result;
2579 if (n == 0)
2580 return CONST1_RTX (mode);
2582 val = (n < 0) ? -n : n;
2584 memset (cache, 0, sizeof (cache));
2585 cache[1] = x;
2587 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2589 /* If the original exponent was negative, reciprocate the result. */
2590 if (n < 0)
2591 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2592 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2594 return result;
2597 /* Expand a call to the pow built-in mathematical function. Return 0 if
2598 a normal call should be emitted rather than expanding the function
2599 in-line. EXP is the expression that is a call to the builtin
2600 function; if convenient, the result should be placed in TARGET. */
2602 static rtx
2603 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2605 tree arg0, arg1, fn, narg0, narglist;
2606 tree arglist = TREE_OPERAND (exp, 1);
2607 tree type = TREE_TYPE (exp);
2608 REAL_VALUE_TYPE cint, c, c2;
2609 HOST_WIDE_INT n;
2610 rtx op, op2;
2611 enum machine_mode mode = TYPE_MODE (type);
2613 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2614 return 0;
2616 arg0 = TREE_VALUE (arglist);
2617 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2619 if (TREE_CODE (arg1) != REAL_CST
2620 || TREE_CONSTANT_OVERFLOW (arg1))
2621 return expand_builtin_mathfn_2 (exp, target, subtarget);
2623 /* Handle constant exponents. */
2625 /* For integer valued exponents we can expand to an optimal multiplication
2626 sequence using expand_powi. */
2627 c = TREE_REAL_CST (arg1);
2628 n = real_to_integer (&c);
2629 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2630 if (real_identical (&c, &cint)
2631 && ((n >= -1 && n <= 2)
2632 || (flag_unsafe_math_optimizations
2633 && !optimize_size
2634 && powi_cost (n) <= POWI_MAX_MULTS)))
2636 op = expand_expr (arg0, subtarget, VOIDmode, 0);
2637 if (n != 1)
2639 op = force_reg (mode, op);
2640 op = expand_powi (op, mode, n);
2642 return op;
2645 narg0 = builtin_save_expr (arg0);
2646 narglist = build_tree_list (NULL_TREE, narg0);
2648 /* If the exponent is not integer valued, check if it is half of an integer.
2649 In this case we can expand to sqrt (x) * x**(n/2). */
2650 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2651 if (fn != NULL_TREE)
2653 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2654 n = real_to_integer (&c2);
2655 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2656 if (real_identical (&c2, &cint)
2657 && ((flag_unsafe_math_optimizations
2658 && !optimize_size
2659 && powi_cost (n/2) <= POWI_MAX_MULTS)
2660 || n == 1))
2662 tree call_expr = build_function_call_expr (fn, narglist);
2663 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2664 if (n != 1)
2666 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2667 op2 = force_reg (mode, op2);
2668 op2 = expand_powi (op2, mode, abs (n / 2));
2669 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2670 0, OPTAB_LIB_WIDEN);
2671 /* If the original exponent was negative, reciprocate the
2672 result. */
2673 if (n < 0)
2674 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2675 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2677 return op;
2681 /* Try if the exponent is a third of an integer. In this case
2682 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2683 different from pow (x, 1./3.) due to rounding and behavior
2684 with negative x we need to constrain this transformation to
2685 unsafe math and positive x or finite math. */
2686 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2687 if (fn != NULL_TREE
2688 && flag_unsafe_math_optimizations
2689 && (tree_expr_nonnegative_p (arg0)
2690 || !HONOR_NANS (mode)))
2692 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2693 real_round (&c2, mode, &c2);
2694 n = real_to_integer (&c2);
2695 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2696 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2697 real_convert (&c2, mode, &c2);
2698 if (real_identical (&c2, &c)
2699 && ((!optimize_size
2700 && powi_cost (n/3) <= POWI_MAX_MULTS)
2701 || n == 1))
2703 tree call_expr = build_function_call_expr (fn, narglist);
2704 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2705 if (abs (n) % 3 == 2)
2706 op = expand_simple_binop (mode, MULT, op, op, op,
2707 0, OPTAB_LIB_WIDEN);
2708 if (n != 1)
2710 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2711 op2 = force_reg (mode, op2);
2712 op2 = expand_powi (op2, mode, abs (n / 3));
2713 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2714 0, OPTAB_LIB_WIDEN);
2715 /* If the original exponent was negative, reciprocate the
2716 result. */
2717 if (n < 0)
2718 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2719 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2721 return op;
2725 /* Fall back to optab expansion. */
2726 return expand_builtin_mathfn_2 (exp, target, subtarget);
2729 /* Expand a call to the powi built-in mathematical function. Return 0 if
2730 a normal call should be emitted rather than expanding the function
2731 in-line. EXP is the expression that is a call to the builtin
2732 function; if convenient, the result should be placed in TARGET. */
2734 static rtx
2735 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2737 tree arglist = TREE_OPERAND (exp, 1);
2738 tree arg0, arg1;
2739 rtx op0, op1;
2740 enum machine_mode mode;
2741 enum machine_mode mode2;
2743 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2744 return 0;
2746 arg0 = TREE_VALUE (arglist);
2747 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2748 mode = TYPE_MODE (TREE_TYPE (exp));
2750 /* Handle constant power. */
2752 if (TREE_CODE (arg1) == INTEGER_CST
2753 && ! TREE_CONSTANT_OVERFLOW (arg1))
2755 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2757 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2758 Otherwise, check the number of multiplications required. */
2759 if ((TREE_INT_CST_HIGH (arg1) == 0
2760 || TREE_INT_CST_HIGH (arg1) == -1)
2761 && ((n >= -1 && n <= 2)
2762 || (! optimize_size
2763 && powi_cost (n) <= POWI_MAX_MULTS)))
2765 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2766 op0 = force_reg (mode, op0);
2767 return expand_powi (op0, mode, n);
2771 /* Emit a libcall to libgcc. */
2773 /* Mode of the 2nd argument must match that of an int. */
2774 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2776 if (target == NULL_RTX)
2777 target = gen_reg_rtx (mode);
2779 op0 = expand_expr (arg0, subtarget, mode, 0);
2780 if (GET_MODE (op0) != mode)
2781 op0 = convert_to_mode (mode, op0, 0);
2782 op1 = expand_expr (arg1, 0, mode2, 0);
2783 if (GET_MODE (op1) != mode2)
2784 op1 = convert_to_mode (mode2, op1, 0);
2786 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2787 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2788 op0, mode, op1, mode2);
2790 return target;
2793 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2794 if we failed the caller should emit a normal call, otherwise
2795 try to get the result in TARGET, if convenient. */
2797 static rtx
2798 expand_builtin_strlen (tree arglist, rtx target,
2799 enum machine_mode target_mode)
2801 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2802 return 0;
2803 else
2805 rtx pat;
2806 tree len, src = TREE_VALUE (arglist);
2807 rtx result, src_reg, char_rtx, before_strlen;
2808 enum machine_mode insn_mode = target_mode, char_mode;
2809 enum insn_code icode = CODE_FOR_nothing;
2810 int align;
2812 /* If the length can be computed at compile-time, return it. */
2813 len = c_strlen (src, 0);
2814 if (len)
2815 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2817 /* If the length can be computed at compile-time and is constant
2818 integer, but there are side-effects in src, evaluate
2819 src for side-effects, then return len.
2820 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2821 can be optimized into: i++; x = 3; */
2822 len = c_strlen (src, 1);
2823 if (len && TREE_CODE (len) == INTEGER_CST)
2825 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2826 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2829 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2831 /* If SRC is not a pointer type, don't do this operation inline. */
2832 if (align == 0)
2833 return 0;
2835 /* Bail out if we can't compute strlen in the right mode. */
2836 while (insn_mode != VOIDmode)
2838 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2839 if (icode != CODE_FOR_nothing)
2840 break;
2842 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2844 if (insn_mode == VOIDmode)
2845 return 0;
2847 /* Make a place to write the result of the instruction. */
2848 result = target;
2849 if (! (result != 0
2850 && REG_P (result)
2851 && GET_MODE (result) == insn_mode
2852 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2853 result = gen_reg_rtx (insn_mode);
2855 /* Make a place to hold the source address. We will not expand
2856 the actual source until we are sure that the expansion will
2857 not fail -- there are trees that cannot be expanded twice. */
2858 src_reg = gen_reg_rtx (Pmode);
2860 /* Mark the beginning of the strlen sequence so we can emit the
2861 source operand later. */
2862 before_strlen = get_last_insn ();
2864 char_rtx = const0_rtx;
2865 char_mode = insn_data[(int) icode].operand[2].mode;
2866 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2867 char_mode))
2868 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2870 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2871 char_rtx, GEN_INT (align));
2872 if (! pat)
2873 return 0;
2874 emit_insn (pat);
2876 /* Now that we are assured of success, expand the source. */
2877 start_sequence ();
2878 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2879 if (pat != src_reg)
2880 emit_move_insn (src_reg, pat);
2881 pat = get_insns ();
2882 end_sequence ();
2884 if (before_strlen)
2885 emit_insn_after (pat, before_strlen);
2886 else
2887 emit_insn_before (pat, get_insns ());
2889 /* Return the value in the proper mode for this function. */
2890 if (GET_MODE (result) == target_mode)
2891 target = result;
2892 else if (target != 0)
2893 convert_move (target, result, 0);
2894 else
2895 target = convert_to_mode (target_mode, result, 0);
2897 return target;
2901 /* Expand a call to the strstr builtin. Return 0 if we failed the
2902 caller should emit a normal call, otherwise try to get the result
2903 in TARGET, if convenient (and in mode MODE if that's convenient). */
2905 static rtx
2906 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2908 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2910 tree result = fold_builtin_strstr (arglist, type);
2911 if (result)
2912 return expand_expr (result, target, mode, EXPAND_NORMAL);
2914 return 0;
2917 /* Expand a call to the strchr builtin. Return 0 if we failed the
2918 caller should emit a normal call, otherwise try to get the result
2919 in TARGET, if convenient (and in mode MODE if that's convenient). */
2921 static rtx
2922 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2924 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2926 tree result = fold_builtin_strchr (arglist, type);
2927 if (result)
2928 return expand_expr (result, target, mode, EXPAND_NORMAL);
2930 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2932 return 0;
2935 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2936 caller should emit a normal call, otherwise try to get the result
2937 in TARGET, if convenient (and in mode MODE if that's convenient). */
2939 static rtx
2940 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2942 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2944 tree result = fold_builtin_strrchr (arglist, type);
2945 if (result)
2946 return expand_expr (result, target, mode, EXPAND_NORMAL);
2948 return 0;
2951 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2952 caller should emit a normal call, otherwise try to get the result
2953 in TARGET, if convenient (and in mode MODE if that's convenient). */
2955 static rtx
2956 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2958 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2960 tree result = fold_builtin_strpbrk (arglist, type);
2961 if (result)
2962 return expand_expr (result, target, mode, EXPAND_NORMAL);
2964 return 0;
2967 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2968 bytes from constant string DATA + OFFSET and return it as target
2969 constant. */
2971 static rtx
2972 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2973 enum machine_mode mode)
2975 const char *str = (const char *) data;
2977 gcc_assert (offset >= 0
2978 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2979 <= strlen (str) + 1));
2981 return c_readstr (str + offset, mode);
2984 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2985 Return 0 if we failed, the caller should emit a normal call,
2986 otherwise try to get the result in TARGET, if convenient (and in
2987 mode MODE if that's convenient). */
2988 static rtx
2989 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2991 tree fndecl = get_callee_fndecl (exp);
2992 tree arglist = TREE_OPERAND (exp, 1);
2993 if (!validate_arglist (arglist,
2994 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2995 return 0;
2996 else
2998 tree dest = TREE_VALUE (arglist);
2999 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3000 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3001 const char *src_str;
3002 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3003 unsigned int dest_align
3004 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3005 rtx dest_mem, src_mem, dest_addr, len_rtx;
3006 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
3007 false, /*endp=*/0);
3009 if (result)
3011 while (TREE_CODE (result) == COMPOUND_EXPR)
3013 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3014 EXPAND_NORMAL);
3015 result = TREE_OPERAND (result, 1);
3017 return expand_expr (result, target, mode, EXPAND_NORMAL);
3020 /* If DEST is not a pointer type, call the normal function. */
3021 if (dest_align == 0)
3022 return 0;
3024 /* If either SRC is not a pointer type, don't do this
3025 operation in-line. */
3026 if (src_align == 0)
3027 return 0;
3029 dest_mem = get_memory_rtx (dest, len);
3030 set_mem_align (dest_mem, dest_align);
3031 len_rtx = expand_normal (len);
3032 src_str = c_getstr (src);
3034 /* If SRC is a string constant and block move would be done
3035 by pieces, we can avoid loading the string from memory
3036 and only stored the computed constants. */
3037 if (src_str
3038 && GET_CODE (len_rtx) == CONST_INT
3039 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3040 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3041 (void *) src_str, dest_align))
3043 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3044 builtin_memcpy_read_str,
3045 (void *) src_str, dest_align, 0);
3046 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3047 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3048 return dest_mem;
3051 src_mem = get_memory_rtx (src, len);
3052 set_mem_align (src_mem, src_align);
3054 /* Copy word part most expediently. */
3055 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
3056 CALL_EXPR_TAILCALL (exp)
3057 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3059 if (dest_addr == 0)
3061 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3062 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3064 return dest_addr;
3068 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
3069 Return 0 if we failed; the caller should emit a normal call,
3070 otherwise try to get the result in TARGET, if convenient (and in
3071 mode MODE if that's convenient). If ENDP is 0 return the
3072 destination pointer, if ENDP is 1 return the end pointer ala
3073 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3074 stpcpy. */
3076 static rtx
3077 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
3078 int endp)
3080 if (!validate_arglist (arglist,
3081 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3082 return 0;
3083 /* If return value is ignored, transform mempcpy into memcpy. */
3084 else if (target == const0_rtx)
3086 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3088 if (!fn)
3089 return 0;
3091 return expand_expr (build_function_call_expr (fn, arglist),
3092 target, mode, EXPAND_NORMAL);
3094 else
3096 tree dest = TREE_VALUE (arglist);
3097 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3098 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3099 const char *src_str;
3100 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3101 unsigned int dest_align
3102 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3103 rtx dest_mem, src_mem, len_rtx;
3104 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3106 if (result)
3108 while (TREE_CODE (result) == COMPOUND_EXPR)
3110 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3111 EXPAND_NORMAL);
3112 result = TREE_OPERAND (result, 1);
3114 return expand_expr (result, target, mode, EXPAND_NORMAL);
3117 /* If either SRC or DEST is not a pointer type, don't do this
3118 operation in-line. */
3119 if (dest_align == 0 || src_align == 0)
3120 return 0;
3122 /* If LEN is not constant, call the normal function. */
3123 if (! host_integerp (len, 1))
3124 return 0;
3126 len_rtx = expand_normal (len);
3127 src_str = c_getstr (src);
3129 /* If SRC is a string constant and block move would be done
3130 by pieces, we can avoid loading the string from memory
3131 and only stored the computed constants. */
3132 if (src_str
3133 && GET_CODE (len_rtx) == CONST_INT
3134 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3135 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3136 (void *) src_str, dest_align))
3138 dest_mem = get_memory_rtx (dest, len);
3139 set_mem_align (dest_mem, dest_align);
3140 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3141 builtin_memcpy_read_str,
3142 (void *) src_str, dest_align, endp);
3143 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3144 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3145 return dest_mem;
3148 if (GET_CODE (len_rtx) == CONST_INT
3149 && can_move_by_pieces (INTVAL (len_rtx),
3150 MIN (dest_align, src_align)))
3152 dest_mem = get_memory_rtx (dest, len);
3153 set_mem_align (dest_mem, dest_align);
3154 src_mem = get_memory_rtx (src, len);
3155 set_mem_align (src_mem, src_align);
3156 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3157 MIN (dest_align, src_align), endp);
3158 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3159 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3160 return dest_mem;
3163 return 0;
3167 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3168 if we failed; the caller should emit a normal call. */
3170 static rtx
3171 expand_builtin_memmove (tree arglist, tree type, rtx target,
3172 enum machine_mode mode)
3174 if (!validate_arglist (arglist,
3175 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3176 return 0;
3177 else
3179 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3181 if (result)
3183 while (TREE_CODE (result) == COMPOUND_EXPR)
3185 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3186 EXPAND_NORMAL);
3187 result = TREE_OPERAND (result, 1);
3189 return expand_expr (result, target, mode, EXPAND_NORMAL);
3192 /* Otherwise, call the normal function. */
3193 return 0;
3197 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3198 if we failed the caller should emit a normal call. */
3200 static rtx
3201 expand_builtin_bcopy (tree exp)
3203 tree arglist = TREE_OPERAND (exp, 1);
3204 tree type = TREE_TYPE (exp);
3205 tree src, dest, size, newarglist;
3207 if (!validate_arglist (arglist,
3208 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3209 return NULL_RTX;
3211 src = TREE_VALUE (arglist);
3212 dest = TREE_VALUE (TREE_CHAIN (arglist));
3213 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3215 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3216 memmove(ptr y, ptr x, size_t z). This is done this way
3217 so that if it isn't expanded inline, we fallback to
3218 calling bcopy instead of memmove. */
3220 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3221 newarglist = tree_cons (NULL_TREE, src, newarglist);
3222 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3224 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3227 #ifndef HAVE_movstr
3228 # define HAVE_movstr 0
3229 # define CODE_FOR_movstr CODE_FOR_nothing
3230 #endif
3232 /* Expand into a movstr instruction, if one is available. Return 0 if
3233 we failed, the caller should emit a normal call, otherwise try to
3234 get the result in TARGET, if convenient. If ENDP is 0 return the
3235 destination pointer, if ENDP is 1 return the end pointer ala
3236 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3237 stpcpy. */
3239 static rtx
3240 expand_movstr (tree dest, tree src, rtx target, int endp)
3242 rtx end;
3243 rtx dest_mem;
3244 rtx src_mem;
3245 rtx insn;
3246 const struct insn_data * data;
3248 if (!HAVE_movstr)
3249 return 0;
3251 dest_mem = get_memory_rtx (dest, NULL);
3252 src_mem = get_memory_rtx (src, NULL);
3253 if (!endp)
3255 target = force_reg (Pmode, XEXP (dest_mem, 0));
3256 dest_mem = replace_equiv_address (dest_mem, target);
3257 end = gen_reg_rtx (Pmode);
3259 else
3261 if (target == 0 || target == const0_rtx)
3263 end = gen_reg_rtx (Pmode);
3264 if (target == 0)
3265 target = end;
3267 else
3268 end = target;
3271 data = insn_data + CODE_FOR_movstr;
3273 if (data->operand[0].mode != VOIDmode)
3274 end = gen_lowpart (data->operand[0].mode, end);
3276 insn = data->genfun (end, dest_mem, src_mem);
3278 gcc_assert (insn);
3280 emit_insn (insn);
3282 /* movstr is supposed to set end to the address of the NUL
3283 terminator. If the caller requested a mempcpy-like return value,
3284 adjust it. */
3285 if (endp == 1 && target != const0_rtx)
3287 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3288 emit_move_insn (target, force_operand (tem, NULL_RTX));
3291 return target;
3294 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3295 if we failed the caller should emit a normal call, otherwise try to get
3296 the result in TARGET, if convenient (and in mode MODE if that's
3297 convenient). */
3299 static rtx
3300 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3302 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3304 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3305 if (result)
3307 while (TREE_CODE (result) == COMPOUND_EXPR)
3309 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3310 EXPAND_NORMAL);
3311 result = TREE_OPERAND (result, 1);
3313 return expand_expr (result, target, mode, EXPAND_NORMAL);
3316 return expand_movstr (TREE_VALUE (arglist),
3317 TREE_VALUE (TREE_CHAIN (arglist)),
3318 target, /*endp=*/0);
3320 return 0;
3323 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3324 Return 0 if we failed the caller should emit a normal call,
3325 otherwise try to get the result in TARGET, if convenient (and in
3326 mode MODE if that's convenient). */
3328 static rtx
3329 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3331 tree arglist = TREE_OPERAND (exp, 1);
3332 /* If return value is ignored, transform stpcpy into strcpy. */
3333 if (target == const0_rtx)
3335 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3336 if (!fn)
3337 return 0;
3339 return expand_expr (build_function_call_expr (fn, arglist),
3340 target, mode, EXPAND_NORMAL);
3343 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3344 return 0;
3345 else
3347 tree dst, src, len, lenp1;
3348 tree narglist;
3349 rtx ret;
3351 /* Ensure we get an actual string whose length can be evaluated at
3352 compile-time, not an expression containing a string. This is
3353 because the latter will potentially produce pessimized code
3354 when used to produce the return value. */
3355 src = TREE_VALUE (TREE_CHAIN (arglist));
3356 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3357 return expand_movstr (TREE_VALUE (arglist),
3358 TREE_VALUE (TREE_CHAIN (arglist)),
3359 target, /*endp=*/2);
3361 dst = TREE_VALUE (arglist);
3362 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3363 narglist = build_tree_list (NULL_TREE, lenp1);
3364 narglist = tree_cons (NULL_TREE, src, narglist);
3365 narglist = tree_cons (NULL_TREE, dst, narglist);
3366 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3367 target, mode, /*endp=*/2);
3369 if (ret)
3370 return ret;
3372 if (TREE_CODE (len) == INTEGER_CST)
3374 rtx len_rtx = expand_normal (len);
3376 if (GET_CODE (len_rtx) == CONST_INT)
3378 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3379 arglist, target, mode);
3381 if (ret)
3383 if (! target)
3385 if (mode != VOIDmode)
3386 target = gen_reg_rtx (mode);
3387 else
3388 target = gen_reg_rtx (GET_MODE (ret));
3390 if (GET_MODE (target) != GET_MODE (ret))
3391 ret = gen_lowpart (GET_MODE (target), ret);
3393 ret = plus_constant (ret, INTVAL (len_rtx));
3394 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3395 gcc_assert (ret);
3397 return target;
3402 return expand_movstr (TREE_VALUE (arglist),
3403 TREE_VALUE (TREE_CHAIN (arglist)),
3404 target, /*endp=*/2);
3408 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3409 bytes from constant string DATA + OFFSET and return it as target
3410 constant. */
3412 static rtx
3413 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3414 enum machine_mode mode)
3416 const char *str = (const char *) data;
3418 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3419 return const0_rtx;
3421 return c_readstr (str + offset, mode);
3424 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3425 if we failed the caller should emit a normal call. */
3427 static rtx
3428 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3430 tree fndecl = get_callee_fndecl (exp);
3431 tree arglist = TREE_OPERAND (exp, 1);
3432 if (validate_arglist (arglist,
3433 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3435 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3436 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3437 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3439 if (result)
3441 while (TREE_CODE (result) == COMPOUND_EXPR)
3443 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3444 EXPAND_NORMAL);
3445 result = TREE_OPERAND (result, 1);
3447 return expand_expr (result, target, mode, EXPAND_NORMAL);
3450 /* We must be passed a constant len and src parameter. */
3451 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3452 return 0;
3454 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3456 /* We're required to pad with trailing zeros if the requested
3457 len is greater than strlen(s2)+1. In that case try to
3458 use store_by_pieces, if it fails, punt. */
3459 if (tree_int_cst_lt (slen, len))
3461 tree dest = TREE_VALUE (arglist);
3462 unsigned int dest_align
3463 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3464 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3465 rtx dest_mem;
3467 if (!p || dest_align == 0 || !host_integerp (len, 1)
3468 || !can_store_by_pieces (tree_low_cst (len, 1),
3469 builtin_strncpy_read_str,
3470 (void *) p, dest_align))
3471 return 0;
3473 dest_mem = get_memory_rtx (dest, len);
3474 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3475 builtin_strncpy_read_str,
3476 (void *) p, dest_align, 0);
3477 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3478 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3479 return dest_mem;
3482 return 0;
3485 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3486 bytes from constant string DATA + OFFSET and return it as target
3487 constant. */
3489 static rtx
3490 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3491 enum machine_mode mode)
3493 const char *c = (const char *) data;
3494 char *p = alloca (GET_MODE_SIZE (mode));
3496 memset (p, *c, GET_MODE_SIZE (mode));
3498 return c_readstr (p, mode);
3501 /* Callback routine for store_by_pieces. Return the RTL of a register
3502 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3503 char value given in the RTL register data. For example, if mode is
3504 4 bytes wide, return the RTL for 0x01010101*data. */
3506 static rtx
3507 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3508 enum machine_mode mode)
3510 rtx target, coeff;
3511 size_t size;
3512 char *p;
3514 size = GET_MODE_SIZE (mode);
3515 if (size == 1)
3516 return (rtx) data;
3518 p = alloca (size);
3519 memset (p, 1, size);
3520 coeff = c_readstr (p, mode);
3522 target = convert_to_mode (mode, (rtx) data, 1);
3523 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3524 return force_reg (mode, target);
3527 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3528 if we failed the caller should emit a normal call, otherwise try to get
3529 the result in TARGET, if convenient (and in mode MODE if that's
3530 convenient). */
3532 static rtx
3533 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3534 tree orig_exp)
3536 if (!validate_arglist (arglist,
3537 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3538 return 0;
3539 else
3541 tree dest = TREE_VALUE (arglist);
3542 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3543 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3544 tree fndecl, fn;
3545 enum built_in_function fcode;
3546 char c;
3547 unsigned int dest_align;
3548 rtx dest_mem, dest_addr, len_rtx;
3550 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3552 /* If DEST is not a pointer type, don't do this
3553 operation in-line. */
3554 if (dest_align == 0)
3555 return 0;
3557 /* If the LEN parameter is zero, return DEST. */
3558 if (integer_zerop (len))
3560 /* Evaluate and ignore VAL in case it has side-effects. */
3561 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3562 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3565 /* Stabilize the arguments in case we fail. */
3566 dest = builtin_save_expr (dest);
3567 val = builtin_save_expr (val);
3568 len = builtin_save_expr (len);
3570 len_rtx = expand_normal (len);
3571 dest_mem = get_memory_rtx (dest, len);
3573 if (TREE_CODE (val) != INTEGER_CST)
3575 rtx val_rtx;
3577 val_rtx = expand_normal (val);
3578 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3579 val_rtx, 0);
3581 /* Assume that we can memset by pieces if we can store the
3582 * the coefficients by pieces (in the required modes).
3583 * We can't pass builtin_memset_gen_str as that emits RTL. */
3584 c = 1;
3585 if (host_integerp (len, 1)
3586 && !(optimize_size && tree_low_cst (len, 1) > 1)
3587 && can_store_by_pieces (tree_low_cst (len, 1),
3588 builtin_memset_read_str, &c, dest_align))
3590 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3591 val_rtx);
3592 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3593 builtin_memset_gen_str, val_rtx, dest_align, 0);
3595 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3596 dest_align))
3597 goto do_libcall;
3599 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3600 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3601 return dest_mem;
3604 if (target_char_cast (val, &c))
3605 goto do_libcall;
3607 if (c)
3609 if (host_integerp (len, 1)
3610 && !(optimize_size && tree_low_cst (len, 1) > 1)
3611 && can_store_by_pieces (tree_low_cst (len, 1),
3612 builtin_memset_read_str, &c, dest_align))
3613 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3614 builtin_memset_read_str, &c, dest_align, 0);
3615 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3616 dest_align))
3617 goto do_libcall;
3619 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3620 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3621 return dest_mem;
3624 set_mem_align (dest_mem, dest_align);
3625 dest_addr = clear_storage (dest_mem, len_rtx,
3626 CALL_EXPR_TAILCALL (orig_exp)
3627 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3629 if (dest_addr == 0)
3631 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3632 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3635 return dest_addr;
3637 do_libcall:
3638 fndecl = get_callee_fndecl (orig_exp);
3639 fcode = DECL_FUNCTION_CODE (fndecl);
3640 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3641 arglist = build_tree_list (NULL_TREE, len);
3642 if (fcode == BUILT_IN_MEMSET)
3643 arglist = tree_cons (NULL_TREE, val, arglist);
3644 arglist = tree_cons (NULL_TREE, dest, arglist);
3645 fn = build_function_call_expr (fndecl, arglist);
3646 if (TREE_CODE (fn) == CALL_EXPR)
3647 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3648 return expand_call (fn, target, target == const0_rtx);
3652 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3653 if we failed the caller should emit a normal call. */
3655 static rtx
3656 expand_builtin_bzero (tree exp)
3658 tree arglist = TREE_OPERAND (exp, 1);
3659 tree dest, size, newarglist;
3661 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3662 return NULL_RTX;
3664 dest = TREE_VALUE (arglist);
3665 size = TREE_VALUE (TREE_CHAIN (arglist));
3667 /* New argument list transforming bzero(ptr x, int y) to
3668 memset(ptr x, int 0, size_t y). This is done this way
3669 so that if it isn't expanded inline, we fallback to
3670 calling bzero instead of memset. */
3672 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3673 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3674 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3676 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3679 /* Expand expression EXP, which is a call to the memcmp built-in function.
3680 ARGLIST is the argument list for this call. Return 0 if we failed and the
3681 caller should emit a normal call, otherwise try to get the result in
3682 TARGET, if convenient (and in mode MODE, if that's convenient). */
3684 static rtx
3685 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3686 enum machine_mode mode)
3688 if (!validate_arglist (arglist,
3689 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3690 return 0;
3691 else
3693 tree result = fold_builtin_memcmp (arglist);
3694 if (result)
3695 return expand_expr (result, target, mode, EXPAND_NORMAL);
3698 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3700 tree arg1 = TREE_VALUE (arglist);
3701 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3702 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3703 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3704 rtx result;
3705 rtx insn;
3707 int arg1_align
3708 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3709 int arg2_align
3710 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3711 enum machine_mode insn_mode;
3713 #ifdef HAVE_cmpmemsi
3714 if (HAVE_cmpmemsi)
3715 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3716 else
3717 #endif
3718 #ifdef HAVE_cmpstrnsi
3719 if (HAVE_cmpstrnsi)
3720 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3721 else
3722 #endif
3723 return 0;
3725 /* If we don't have POINTER_TYPE, call the function. */
3726 if (arg1_align == 0 || arg2_align == 0)
3727 return 0;
3729 /* Make a place to write the result of the instruction. */
3730 result = target;
3731 if (! (result != 0
3732 && REG_P (result) && GET_MODE (result) == insn_mode
3733 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3734 result = gen_reg_rtx (insn_mode);
3736 arg1_rtx = get_memory_rtx (arg1, len);
3737 arg2_rtx = get_memory_rtx (arg2, len);
3738 arg3_rtx = expand_normal (len);
3740 /* Set MEM_SIZE as appropriate. */
3741 if (GET_CODE (arg3_rtx) == CONST_INT)
3743 set_mem_size (arg1_rtx, arg3_rtx);
3744 set_mem_size (arg2_rtx, arg3_rtx);
3747 #ifdef HAVE_cmpmemsi
3748 if (HAVE_cmpmemsi)
3749 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3750 GEN_INT (MIN (arg1_align, arg2_align)));
3751 else
3752 #endif
3753 #ifdef HAVE_cmpstrnsi
3754 if (HAVE_cmpstrnsi)
3755 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3756 GEN_INT (MIN (arg1_align, arg2_align)));
3757 else
3758 #endif
3759 gcc_unreachable ();
3761 if (insn)
3762 emit_insn (insn);
3763 else
3764 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3765 TYPE_MODE (integer_type_node), 3,
3766 XEXP (arg1_rtx, 0), Pmode,
3767 XEXP (arg2_rtx, 0), Pmode,
3768 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3769 TYPE_UNSIGNED (sizetype)),
3770 TYPE_MODE (sizetype));
3772 /* Return the value in the proper mode for this function. */
3773 mode = TYPE_MODE (TREE_TYPE (exp));
3774 if (GET_MODE (result) == mode)
3775 return result;
3776 else if (target != 0)
3778 convert_move (target, result, 0);
3779 return target;
3781 else
3782 return convert_to_mode (mode, result, 0);
3784 #endif
3786 return 0;
3789 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3790 if we failed the caller should emit a normal call, otherwise try to get
3791 the result in TARGET, if convenient. */
3793 static rtx
3794 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3796 tree arglist = TREE_OPERAND (exp, 1);
3798 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3799 return 0;
3800 else
3802 tree result = fold_builtin_strcmp (arglist);
3803 if (result)
3804 return expand_expr (result, target, mode, EXPAND_NORMAL);
3807 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3808 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3809 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3811 rtx arg1_rtx, arg2_rtx;
3812 rtx result, insn = NULL_RTX;
3813 tree fndecl, fn;
3815 tree arg1 = TREE_VALUE (arglist);
3816 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3817 int arg1_align
3818 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3819 int arg2_align
3820 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3822 /* If we don't have POINTER_TYPE, call the function. */
3823 if (arg1_align == 0 || arg2_align == 0)
3824 return 0;
3826 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3827 arg1 = builtin_save_expr (arg1);
3828 arg2 = builtin_save_expr (arg2);
3830 arg1_rtx = get_memory_rtx (arg1, NULL);
3831 arg2_rtx = get_memory_rtx (arg2, NULL);
3833 #ifdef HAVE_cmpstrsi
3834 /* Try to call cmpstrsi. */
3835 if (HAVE_cmpstrsi)
3837 enum machine_mode insn_mode
3838 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3840 /* Make a place to write the result of the instruction. */
3841 result = target;
3842 if (! (result != 0
3843 && REG_P (result) && GET_MODE (result) == insn_mode
3844 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3845 result = gen_reg_rtx (insn_mode);
3847 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3848 GEN_INT (MIN (arg1_align, arg2_align)));
3850 #endif
3851 #ifdef HAVE_cmpstrnsi
3852 /* Try to determine at least one length and call cmpstrnsi. */
3853 if (!insn && HAVE_cmpstrnsi)
3855 tree len;
3856 rtx arg3_rtx;
3858 enum machine_mode insn_mode
3859 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3860 tree len1 = c_strlen (arg1, 1);
3861 tree len2 = c_strlen (arg2, 1);
3863 if (len1)
3864 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3865 if (len2)
3866 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3868 /* If we don't have a constant length for the first, use the length
3869 of the second, if we know it. We don't require a constant for
3870 this case; some cost analysis could be done if both are available
3871 but neither is constant. For now, assume they're equally cheap,
3872 unless one has side effects. If both strings have constant lengths,
3873 use the smaller. */
3875 if (!len1)
3876 len = len2;
3877 else if (!len2)
3878 len = len1;
3879 else if (TREE_SIDE_EFFECTS (len1))
3880 len = len2;
3881 else if (TREE_SIDE_EFFECTS (len2))
3882 len = len1;
3883 else if (TREE_CODE (len1) != INTEGER_CST)
3884 len = len2;
3885 else if (TREE_CODE (len2) != INTEGER_CST)
3886 len = len1;
3887 else if (tree_int_cst_lt (len1, len2))
3888 len = len1;
3889 else
3890 len = len2;
3892 /* If both arguments have side effects, we cannot optimize. */
3893 if (!len || TREE_SIDE_EFFECTS (len))
3894 goto do_libcall;
3896 arg3_rtx = expand_normal (len);
3898 /* Make a place to write the result of the instruction. */
3899 result = target;
3900 if (! (result != 0
3901 && REG_P (result) && GET_MODE (result) == insn_mode
3902 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3903 result = gen_reg_rtx (insn_mode);
3905 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3906 GEN_INT (MIN (arg1_align, arg2_align)));
3908 #endif
3910 if (insn)
3912 emit_insn (insn);
3914 /* Return the value in the proper mode for this function. */
3915 mode = TYPE_MODE (TREE_TYPE (exp));
3916 if (GET_MODE (result) == mode)
3917 return result;
3918 if (target == 0)
3919 return convert_to_mode (mode, result, 0);
3920 convert_move (target, result, 0);
3921 return target;
3924 /* Expand the library call ourselves using a stabilized argument
3925 list to avoid re-evaluating the function's arguments twice. */
3926 #ifdef HAVE_cmpstrnsi
3927 do_libcall:
3928 #endif
3929 arglist = build_tree_list (NULL_TREE, arg2);
3930 arglist = tree_cons (NULL_TREE, arg1, arglist);
3931 fndecl = get_callee_fndecl (exp);
3932 fn = build_function_call_expr (fndecl, arglist);
3933 if (TREE_CODE (fn) == CALL_EXPR)
3934 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3935 return expand_call (fn, target, target == const0_rtx);
3937 #endif
3938 return 0;
3941 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3942 if we failed the caller should emit a normal call, otherwise try to get
3943 the result in TARGET, if convenient. */
3945 static rtx
3946 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3948 tree arglist = TREE_OPERAND (exp, 1);
3950 if (!validate_arglist (arglist,
3951 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3952 return 0;
3953 else
3955 tree result = fold_builtin_strncmp (arglist);
3956 if (result)
3957 return expand_expr (result, target, mode, EXPAND_NORMAL);
3960 /* If c_strlen can determine an expression for one of the string
3961 lengths, and it doesn't have side effects, then emit cmpstrnsi
3962 using length MIN(strlen(string)+1, arg3). */
3963 #ifdef HAVE_cmpstrnsi
3964 if (HAVE_cmpstrnsi)
3966 tree arg1 = TREE_VALUE (arglist);
3967 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3968 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3969 tree len, len1, len2;
3970 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3971 rtx result, insn;
3972 tree fndecl, fn;
3974 int arg1_align
3975 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3976 int arg2_align
3977 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3978 enum machine_mode insn_mode
3979 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3981 len1 = c_strlen (arg1, 1);
3982 len2 = c_strlen (arg2, 1);
3984 if (len1)
3985 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3986 if (len2)
3987 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3989 /* If we don't have a constant length for the first, use the length
3990 of the second, if we know it. We don't require a constant for
3991 this case; some cost analysis could be done if both are available
3992 but neither is constant. For now, assume they're equally cheap,
3993 unless one has side effects. If both strings have constant lengths,
3994 use the smaller. */
3996 if (!len1)
3997 len = len2;
3998 else if (!len2)
3999 len = len1;
4000 else if (TREE_SIDE_EFFECTS (len1))
4001 len = len2;
4002 else if (TREE_SIDE_EFFECTS (len2))
4003 len = len1;
4004 else if (TREE_CODE (len1) != INTEGER_CST)
4005 len = len2;
4006 else if (TREE_CODE (len2) != INTEGER_CST)
4007 len = len1;
4008 else if (tree_int_cst_lt (len1, len2))
4009 len = len1;
4010 else
4011 len = len2;
4013 /* If both arguments have side effects, we cannot optimize. */
4014 if (!len || TREE_SIDE_EFFECTS (len))
4015 return 0;
4017 /* The actual new length parameter is MIN(len,arg3). */
4018 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4019 fold_convert (TREE_TYPE (len), arg3));
4021 /* If we don't have POINTER_TYPE, call the function. */
4022 if (arg1_align == 0 || arg2_align == 0)
4023 return 0;
4025 /* Make a place to write the result of the instruction. */
4026 result = target;
4027 if (! (result != 0
4028 && REG_P (result) && GET_MODE (result) == insn_mode
4029 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4030 result = gen_reg_rtx (insn_mode);
4032 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4033 arg1 = builtin_save_expr (arg1);
4034 arg2 = builtin_save_expr (arg2);
4035 len = builtin_save_expr (len);
4037 arg1_rtx = get_memory_rtx (arg1, len);
4038 arg2_rtx = get_memory_rtx (arg2, len);
4039 arg3_rtx = expand_normal (len);
4040 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4041 GEN_INT (MIN (arg1_align, arg2_align)));
4042 if (insn)
4044 emit_insn (insn);
4046 /* Return the value in the proper mode for this function. */
4047 mode = TYPE_MODE (TREE_TYPE (exp));
4048 if (GET_MODE (result) == mode)
4049 return result;
4050 if (target == 0)
4051 return convert_to_mode (mode, result, 0);
4052 convert_move (target, result, 0);
4053 return target;
4056 /* Expand the library call ourselves using a stabilized argument
4057 list to avoid re-evaluating the function's arguments twice. */
4058 arglist = build_tree_list (NULL_TREE, len);
4059 arglist = tree_cons (NULL_TREE, arg2, arglist);
4060 arglist = tree_cons (NULL_TREE, arg1, arglist);
4061 fndecl = get_callee_fndecl (exp);
4062 fn = build_function_call_expr (fndecl, arglist);
4063 if (TREE_CODE (fn) == CALL_EXPR)
4064 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4065 return expand_call (fn, target, target == const0_rtx);
4067 #endif
4068 return 0;
4071 /* Expand expression EXP, which is a call to the strcat builtin.
4072 Return 0 if we failed the caller should emit a normal call,
4073 otherwise try to get the result in TARGET, if convenient. */
4075 static rtx
4076 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
4078 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4079 return 0;
4080 else
4082 tree dst = TREE_VALUE (arglist),
4083 src = TREE_VALUE (TREE_CHAIN (arglist));
4084 const char *p = c_getstr (src);
4086 /* If the string length is zero, return the dst parameter. */
4087 if (p && *p == '\0')
4088 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4090 if (!optimize_size)
4092 /* See if we can store by pieces into (dst + strlen(dst)). */
4093 tree newsrc, newdst,
4094 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4095 rtx insns;
4097 /* Stabilize the argument list. */
4098 newsrc = builtin_save_expr (src);
4099 if (newsrc != src)
4100 arglist = build_tree_list (NULL_TREE, newsrc);
4101 else
4102 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4104 dst = builtin_save_expr (dst);
4106 start_sequence ();
4108 /* Create strlen (dst). */
4109 newdst =
4110 build_function_call_expr (strlen_fn,
4111 build_tree_list (NULL_TREE, dst));
4112 /* Create (dst + (cast) strlen (dst)). */
4113 newdst = fold_convert (TREE_TYPE (dst), newdst);
4114 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4116 newdst = builtin_save_expr (newdst);
4117 arglist = tree_cons (NULL_TREE, newdst, arglist);
4119 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4121 end_sequence (); /* Stop sequence. */
4122 return 0;
4125 /* Output the entire sequence. */
4126 insns = get_insns ();
4127 end_sequence ();
4128 emit_insn (insns);
4130 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4133 return 0;
4137 /* Expand expression EXP, which is a call to the strncat builtin.
4138 Return 0 if we failed the caller should emit a normal call,
4139 otherwise try to get the result in TARGET, if convenient. */
4141 static rtx
4142 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4144 if (validate_arglist (arglist,
4145 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4147 tree result = fold_builtin_strncat (arglist);
4148 if (result)
4149 return expand_expr (result, target, mode, EXPAND_NORMAL);
4151 return 0;
4154 /* Expand expression EXP, which is a call to the strspn builtin.
4155 Return 0 if we failed the caller should emit a normal call,
4156 otherwise try to get the result in TARGET, if convenient. */
4158 static rtx
4159 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4161 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4163 tree result = fold_builtin_strspn (arglist);
4164 if (result)
4165 return expand_expr (result, target, mode, EXPAND_NORMAL);
4167 return 0;
4170 /* Expand expression EXP, which is a call to the strcspn builtin.
4171 Return 0 if we failed the caller should emit a normal call,
4172 otherwise try to get the result in TARGET, if convenient. */
4174 static rtx
4175 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4177 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4179 tree result = fold_builtin_strcspn (arglist);
4180 if (result)
4181 return expand_expr (result, target, mode, EXPAND_NORMAL);
4183 return 0;
4186 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4187 if that's convenient. */
4190 expand_builtin_saveregs (void)
4192 rtx val, seq;
4194 /* Don't do __builtin_saveregs more than once in a function.
4195 Save the result of the first call and reuse it. */
4196 if (saveregs_value != 0)
4197 return saveregs_value;
4199 /* When this function is called, it means that registers must be
4200 saved on entry to this function. So we migrate the call to the
4201 first insn of this function. */
4203 start_sequence ();
4205 /* Do whatever the machine needs done in this case. */
4206 val = targetm.calls.expand_builtin_saveregs ();
4208 seq = get_insns ();
4209 end_sequence ();
4211 saveregs_value = val;
4213 /* Put the insns after the NOTE that starts the function. If this
4214 is inside a start_sequence, make the outer-level insn chain current, so
4215 the code is placed at the start of the function. */
4216 push_topmost_sequence ();
4217 emit_insn_after (seq, entry_of_function ());
4218 pop_topmost_sequence ();
4220 return val;
4223 /* __builtin_args_info (N) returns word N of the arg space info
4224 for the current function. The number and meanings of words
4225 is controlled by the definition of CUMULATIVE_ARGS. */
4227 static rtx
4228 expand_builtin_args_info (tree arglist)
4230 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4231 int *word_ptr = (int *) &current_function_args_info;
4233 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4235 if (arglist != 0)
4237 if (!host_integerp (TREE_VALUE (arglist), 0))
4238 error ("argument of %<__builtin_args_info%> must be constant");
4239 else
4241 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4243 if (wordnum < 0 || wordnum >= nwords)
4244 error ("argument of %<__builtin_args_info%> out of range");
4245 else
4246 return GEN_INT (word_ptr[wordnum]);
4249 else
4250 error ("missing argument in %<__builtin_args_info%>");
4252 return const0_rtx;
4255 /* Expand a call to __builtin_next_arg. */
4257 static rtx
4258 expand_builtin_next_arg (void)
4260 /* Checking arguments is already done in fold_builtin_next_arg
4261 that must be called before this function. */
4262 return expand_binop (Pmode, add_optab,
4263 current_function_internal_arg_pointer,
4264 current_function_arg_offset_rtx,
4265 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4268 /* Make it easier for the backends by protecting the valist argument
4269 from multiple evaluations. */
4271 static tree
4272 stabilize_va_list (tree valist, int needs_lvalue)
4274 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4276 if (TREE_SIDE_EFFECTS (valist))
4277 valist = save_expr (valist);
4279 /* For this case, the backends will be expecting a pointer to
4280 TREE_TYPE (va_list_type_node), but it's possible we've
4281 actually been given an array (an actual va_list_type_node).
4282 So fix it. */
4283 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4285 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4286 valist = build_fold_addr_expr_with_type (valist, p1);
4289 else
4291 tree pt;
4293 if (! needs_lvalue)
4295 if (! TREE_SIDE_EFFECTS (valist))
4296 return valist;
4298 pt = build_pointer_type (va_list_type_node);
4299 valist = fold_build1 (ADDR_EXPR, pt, valist);
4300 TREE_SIDE_EFFECTS (valist) = 1;
4303 if (TREE_SIDE_EFFECTS (valist))
4304 valist = save_expr (valist);
4305 valist = build_fold_indirect_ref (valist);
4308 return valist;
4311 /* The "standard" definition of va_list is void*. */
4313 tree
4314 std_build_builtin_va_list (void)
4316 return ptr_type_node;
4319 /* The "standard" implementation of va_start: just assign `nextarg' to
4320 the variable. */
4322 void
4323 std_expand_builtin_va_start (tree valist, rtx nextarg)
4325 tree t;
4327 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist,
4328 make_tree (ptr_type_node, nextarg));
4329 TREE_SIDE_EFFECTS (t) = 1;
4331 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4334 /* Expand ARGLIST, from a call to __builtin_va_start. */
4336 static rtx
4337 expand_builtin_va_start (tree arglist)
4339 rtx nextarg;
4340 tree chain, valist;
4342 chain = TREE_CHAIN (arglist);
4344 if (!chain)
4346 error ("too few arguments to function %<va_start%>");
4347 return const0_rtx;
4350 if (fold_builtin_next_arg (chain))
4351 return const0_rtx;
4353 nextarg = expand_builtin_next_arg ();
4354 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4356 #ifdef EXPAND_BUILTIN_VA_START
4357 EXPAND_BUILTIN_VA_START (valist, nextarg);
4358 #else
4359 std_expand_builtin_va_start (valist, nextarg);
4360 #endif
4362 return const0_rtx;
4365 /* The "standard" implementation of va_arg: read the value from the
4366 current (padded) address and increment by the (padded) size. */
4368 tree
4369 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4371 tree addr, t, type_size, rounded_size, valist_tmp;
4372 unsigned HOST_WIDE_INT align, boundary;
4373 bool indirect;
4375 #ifdef ARGS_GROW_DOWNWARD
4376 /* All of the alignment and movement below is for args-grow-up machines.
4377 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4378 implement their own specialized gimplify_va_arg_expr routines. */
4379 gcc_unreachable ();
4380 #endif
4382 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4383 if (indirect)
4384 type = build_pointer_type (type);
4386 align = PARM_BOUNDARY / BITS_PER_UNIT;
4387 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4389 /* Hoist the valist value into a temporary for the moment. */
4390 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4392 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4393 requires greater alignment, we must perform dynamic alignment. */
4394 if (boundary > align
4395 && !integer_zerop (TYPE_SIZE (type)))
4397 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4398 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4399 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4400 gimplify_and_add (t, pre_p);
4402 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4403 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4404 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4405 gimplify_and_add (t, pre_p);
4407 else
4408 boundary = align;
4410 /* If the actual alignment is less than the alignment of the type,
4411 adjust the type accordingly so that we don't assume strict alignment
4412 when deferencing the pointer. */
4413 boundary *= BITS_PER_UNIT;
4414 if (boundary < TYPE_ALIGN (type))
4416 type = build_variant_type_copy (type);
4417 TYPE_ALIGN (type) = boundary;
4420 /* Compute the rounded size of the type. */
4421 type_size = size_in_bytes (type);
4422 rounded_size = round_up (type_size, align);
4424 /* Reduce rounded_size so it's sharable with the postqueue. */
4425 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4427 /* Get AP. */
4428 addr = valist_tmp;
4429 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4431 /* Small args are padded downward. */
4432 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4433 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4434 size_binop (MINUS_EXPR, rounded_size, type_size));
4435 t = fold_convert (TREE_TYPE (addr), t);
4436 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4439 /* Compute new value for AP. */
4440 t = fold_convert (TREE_TYPE (valist), rounded_size);
4441 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4442 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist, t);
4443 gimplify_and_add (t, pre_p);
4445 addr = fold_convert (build_pointer_type (type), addr);
4447 if (indirect)
4448 addr = build_va_arg_indirect_ref (addr);
4450 return build_va_arg_indirect_ref (addr);
4453 /* Build an indirect-ref expression over the given TREE, which represents a
4454 piece of a va_arg() expansion. */
4455 tree
4456 build_va_arg_indirect_ref (tree addr)
4458 addr = build_fold_indirect_ref (addr);
4460 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4461 mf_mark (addr);
4463 return addr;
4466 /* Return a dummy expression of type TYPE in order to keep going after an
4467 error. */
4469 static tree
4470 dummy_object (tree type)
4472 tree t = build_int_cst (build_pointer_type (type), 0);
4473 return build1 (INDIRECT_REF, type, t);
4476 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4477 builtin function, but a very special sort of operator. */
4479 enum gimplify_status
4480 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4482 tree promoted_type, want_va_type, have_va_type;
4483 tree valist = TREE_OPERAND (*expr_p, 0);
4484 tree type = TREE_TYPE (*expr_p);
4485 tree t;
4487 /* Verify that valist is of the proper type. */
4488 want_va_type = va_list_type_node;
4489 have_va_type = TREE_TYPE (valist);
4491 if (have_va_type == error_mark_node)
4492 return GS_ERROR;
4494 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4496 /* If va_list is an array type, the argument may have decayed
4497 to a pointer type, e.g. by being passed to another function.
4498 In that case, unwrap both types so that we can compare the
4499 underlying records. */
4500 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4501 || POINTER_TYPE_P (have_va_type))
4503 want_va_type = TREE_TYPE (want_va_type);
4504 have_va_type = TREE_TYPE (have_va_type);
4508 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4510 error ("first argument to %<va_arg%> not of type %<va_list%>");
4511 return GS_ERROR;
4514 /* Generate a diagnostic for requesting data of a type that cannot
4515 be passed through `...' due to type promotion at the call site. */
4516 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4517 != type)
4519 static bool gave_help;
4521 /* Unfortunately, this is merely undefined, rather than a constraint
4522 violation, so we cannot make this an error. If this call is never
4523 executed, the program is still strictly conforming. */
4524 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4525 type, promoted_type);
4526 if (! gave_help)
4528 gave_help = true;
4529 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4530 promoted_type, type);
4533 /* We can, however, treat "undefined" any way we please.
4534 Call abort to encourage the user to fix the program. */
4535 inform ("if this code is reached, the program will abort");
4536 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4537 NULL);
4538 append_to_statement_list (t, pre_p);
4540 /* This is dead code, but go ahead and finish so that the
4541 mode of the result comes out right. */
4542 *expr_p = dummy_object (type);
4543 return GS_ALL_DONE;
4545 else
4547 /* Make it easier for the backends by protecting the valist argument
4548 from multiple evaluations. */
4549 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4551 /* For this case, the backends will be expecting a pointer to
4552 TREE_TYPE (va_list_type_node), but it's possible we've
4553 actually been given an array (an actual va_list_type_node).
4554 So fix it. */
4555 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4557 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4558 valist = build_fold_addr_expr_with_type (valist, p1);
4560 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4562 else
4563 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4565 if (!targetm.gimplify_va_arg_expr)
4566 /* FIXME:Once most targets are converted we should merely
4567 assert this is non-null. */
4568 return GS_ALL_DONE;
4570 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4571 return GS_OK;
4575 /* Expand ARGLIST, from a call to __builtin_va_end. */
4577 static rtx
4578 expand_builtin_va_end (tree arglist)
4580 tree valist = TREE_VALUE (arglist);
4582 /* Evaluate for side effects, if needed. I hate macros that don't
4583 do that. */
4584 if (TREE_SIDE_EFFECTS (valist))
4585 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4587 return const0_rtx;
4590 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4591 builtin rather than just as an assignment in stdarg.h because of the
4592 nastiness of array-type va_list types. */
4594 static rtx
4595 expand_builtin_va_copy (tree arglist)
4597 tree dst, src, t;
4599 dst = TREE_VALUE (arglist);
4600 src = TREE_VALUE (TREE_CHAIN (arglist));
4602 dst = stabilize_va_list (dst, 1);
4603 src = stabilize_va_list (src, 0);
4605 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4607 t = build2 (GIMPLE_MODIFY_STMT, va_list_type_node, dst, src);
4608 TREE_SIDE_EFFECTS (t) = 1;
4609 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4611 else
4613 rtx dstb, srcb, size;
4615 /* Evaluate to pointers. */
4616 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4617 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4618 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4619 VOIDmode, EXPAND_NORMAL);
4621 dstb = convert_memory_address (Pmode, dstb);
4622 srcb = convert_memory_address (Pmode, srcb);
4624 /* "Dereference" to BLKmode memories. */
4625 dstb = gen_rtx_MEM (BLKmode, dstb);
4626 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4627 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4628 srcb = gen_rtx_MEM (BLKmode, srcb);
4629 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4630 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4632 /* Copy. */
4633 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4636 return const0_rtx;
4639 /* Expand a call to one of the builtin functions __builtin_frame_address or
4640 __builtin_return_address. */
4642 static rtx
4643 expand_builtin_frame_address (tree fndecl, tree arglist)
4645 /* The argument must be a nonnegative integer constant.
4646 It counts the number of frames to scan up the stack.
4647 The value is the return address saved in that frame. */
4648 if (arglist == 0)
4649 /* Warning about missing arg was already issued. */
4650 return const0_rtx;
4651 else if (! host_integerp (TREE_VALUE (arglist), 1))
4653 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4654 error ("invalid argument to %<__builtin_frame_address%>");
4655 else
4656 error ("invalid argument to %<__builtin_return_address%>");
4657 return const0_rtx;
4659 else
4661 rtx tem
4662 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4663 tree_low_cst (TREE_VALUE (arglist), 1));
4665 /* Some ports cannot access arbitrary stack frames. */
4666 if (tem == NULL)
4668 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4669 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4670 else
4671 warning (0, "unsupported argument to %<__builtin_return_address%>");
4672 return const0_rtx;
4675 /* For __builtin_frame_address, return what we've got. */
4676 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4677 return tem;
4679 if (!REG_P (tem)
4680 && ! CONSTANT_P (tem))
4681 tem = copy_to_mode_reg (Pmode, tem);
4682 return tem;
4686 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4687 we failed and the caller should emit a normal call, otherwise try to get
4688 the result in TARGET, if convenient. */
4690 static rtx
4691 expand_builtin_alloca (tree arglist, rtx target)
4693 rtx op0;
4694 rtx result;
4696 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4697 should always expand to function calls. These can be intercepted
4698 in libmudflap. */
4699 if (flag_mudflap)
4700 return 0;
4702 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4703 return 0;
4705 /* Compute the argument. */
4706 op0 = expand_normal (TREE_VALUE (arglist));
4708 /* Allocate the desired space. */
4709 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4710 result = convert_memory_address (ptr_mode, result);
4712 return result;
4715 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4716 is the mode to expand with. */
4718 static rtx
4719 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4721 enum machine_mode mode;
4722 tree arg;
4723 rtx op0;
4725 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4726 return 0;
4728 arg = TREE_VALUE (arglist);
4729 mode = TYPE_MODE (TREE_TYPE (arg));
4730 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4732 target = expand_unop (mode, bswap_optab, op0, target, 1);
4734 gcc_assert (target);
4736 return convert_to_mode (mode, target, 0);
4739 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4740 Return 0 if a normal call should be emitted rather than expanding the
4741 function in-line. If convenient, the result should be placed in TARGET.
4742 SUBTARGET may be used as the target for computing one of EXP's operands. */
4744 static rtx
4745 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4746 rtx subtarget, optab op_optab)
4748 rtx op0;
4749 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4750 return 0;
4752 /* Compute the argument. */
4753 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4754 /* Compute op, into TARGET if possible.
4755 Set TARGET to wherever the result comes back. */
4756 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4757 op_optab, op0, target, 1);
4758 gcc_assert (target);
4760 return convert_to_mode (target_mode, target, 0);
4763 /* If the string passed to fputs is a constant and is one character
4764 long, we attempt to transform this call into __builtin_fputc(). */
4766 static rtx
4767 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4769 /* Verify the arguments in the original call. */
4770 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4772 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4773 unlocked, NULL_TREE);
4774 if (result)
4775 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4777 return 0;
4780 /* Expand a call to __builtin_expect. We just return our argument
4781 as the builtin_expect semantic should've been already executed by
4782 tree branch prediction pass. */
4784 static rtx
4785 expand_builtin_expect (tree arglist, rtx target)
4787 tree exp, c;
4789 if (arglist == NULL_TREE
4790 || TREE_CHAIN (arglist) == NULL_TREE)
4791 return const0_rtx;
4792 exp = TREE_VALUE (arglist);
4793 c = TREE_VALUE (TREE_CHAIN (arglist));
4795 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4796 /* When guessing was done, the hints should be already stripped away. */
4797 gcc_assert (!flag_guess_branch_prob);
4798 return target;
4801 void
4802 expand_builtin_trap (void)
4804 #ifdef HAVE_trap
4805 if (HAVE_trap)
4806 emit_insn (gen_trap ());
4807 else
4808 #endif
4809 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4810 emit_barrier ();
4813 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4814 Return 0 if a normal call should be emitted rather than expanding
4815 the function inline. If convenient, the result should be placed
4816 in TARGET. SUBTARGET may be used as the target for computing
4817 the operand. */
4819 static rtx
4820 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4822 enum machine_mode mode;
4823 tree arg;
4824 rtx op0;
4826 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4827 return 0;
4829 arg = TREE_VALUE (arglist);
4830 mode = TYPE_MODE (TREE_TYPE (arg));
4831 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4832 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4835 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4836 Return NULL is a normal call should be emitted rather than expanding the
4837 function inline. If convenient, the result should be placed in TARGET.
4838 SUBTARGET may be used as the target for computing the operand. */
4840 static rtx
4841 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4843 rtx op0, op1;
4844 tree arg;
4846 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4847 return 0;
4849 arg = TREE_VALUE (arglist);
4850 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4852 arg = TREE_VALUE (TREE_CHAIN (arglist));
4853 op1 = expand_normal (arg);
4855 return expand_copysign (op0, op1, target);
4858 /* Create a new constant string literal and return a char* pointer to it.
4859 The STRING_CST value is the LEN characters at STR. */
4860 tree
4861 build_string_literal (int len, const char *str)
4863 tree t, elem, index, type;
4865 t = build_string (len, str);
4866 elem = build_type_variant (char_type_node, 1, 0);
4867 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4868 type = build_array_type (elem, index);
4869 TREE_TYPE (t) = type;
4870 TREE_CONSTANT (t) = 1;
4871 TREE_INVARIANT (t) = 1;
4872 TREE_READONLY (t) = 1;
4873 TREE_STATIC (t) = 1;
4875 type = build_pointer_type (type);
4876 t = build1 (ADDR_EXPR, type, t);
4878 type = build_pointer_type (elem);
4879 t = build1 (NOP_EXPR, type, t);
4880 return t;
4883 /* Expand EXP, a call to printf or printf_unlocked.
4884 Return 0 if a normal call should be emitted rather than transforming
4885 the function inline. If convenient, the result should be placed in
4886 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4887 call. */
4888 static rtx
4889 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4890 bool unlocked)
4892 tree arglist = TREE_OPERAND (exp, 1);
4893 /* If we're using an unlocked function, assume the other unlocked
4894 functions exist explicitly. */
4895 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4896 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4897 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4898 : implicit_built_in_decls[BUILT_IN_PUTS];
4899 const char *fmt_str;
4900 tree fn, fmt, arg;
4902 /* If the return value is used, don't do the transformation. */
4903 if (target != const0_rtx)
4904 return 0;
4906 /* Verify the required arguments in the original call. */
4907 if (! arglist)
4908 return 0;
4909 fmt = TREE_VALUE (arglist);
4910 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4911 return 0;
4912 arglist = TREE_CHAIN (arglist);
4914 /* Check whether the format is a literal string constant. */
4915 fmt_str = c_getstr (fmt);
4916 if (fmt_str == NULL)
4917 return 0;
4919 if (!init_target_chars())
4920 return 0;
4922 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4923 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4925 if (! arglist
4926 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4927 || TREE_CHAIN (arglist))
4928 return 0;
4929 fn = fn_puts;
4931 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4932 else if (strcmp (fmt_str, target_percent_c) == 0)
4934 if (! arglist
4935 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4936 || TREE_CHAIN (arglist))
4937 return 0;
4938 fn = fn_putchar;
4940 else
4942 /* We can't handle anything else with % args or %% ... yet. */
4943 if (strchr (fmt_str, target_percent))
4944 return 0;
4946 if (arglist)
4947 return 0;
4949 /* If the format specifier was "", printf does nothing. */
4950 if (fmt_str[0] == '\0')
4951 return const0_rtx;
4952 /* If the format specifier has length of 1, call putchar. */
4953 if (fmt_str[1] == '\0')
4955 /* Given printf("c"), (where c is any one character,)
4956 convert "c"[0] to an int and pass that to the replacement
4957 function. */
4958 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4959 arglist = build_tree_list (NULL_TREE, arg);
4960 fn = fn_putchar;
4962 else
4964 /* If the format specifier was "string\n", call puts("string"). */
4965 size_t len = strlen (fmt_str);
4966 if ((unsigned char)fmt_str[len - 1] == target_newline)
4968 /* Create a NUL-terminated string that's one char shorter
4969 than the original, stripping off the trailing '\n'. */
4970 char *newstr = alloca (len);
4971 memcpy (newstr, fmt_str, len - 1);
4972 newstr[len - 1] = 0;
4974 arg = build_string_literal (len, newstr);
4975 arglist = build_tree_list (NULL_TREE, arg);
4976 fn = fn_puts;
4978 else
4979 /* We'd like to arrange to call fputs(string,stdout) here,
4980 but we need stdout and don't have a way to get it yet. */
4981 return 0;
4985 if (!fn)
4986 return 0;
4987 fn = build_function_call_expr (fn, arglist);
4988 if (TREE_CODE (fn) == CALL_EXPR)
4989 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4990 return expand_expr (fn, target, mode, EXPAND_NORMAL);
4993 /* Expand EXP, a call to fprintf or fprintf_unlocked.
4994 Return 0 if a normal call should be emitted rather than transforming
4995 the function inline. If convenient, the result should be placed in
4996 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4997 call. */
4998 static rtx
4999 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5000 bool unlocked)
5002 tree arglist = TREE_OPERAND (exp, 1);
5003 /* If we're using an unlocked function, assume the other unlocked
5004 functions exist explicitly. */
5005 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5006 : implicit_built_in_decls[BUILT_IN_FPUTC];
5007 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5008 : implicit_built_in_decls[BUILT_IN_FPUTS];
5009 const char *fmt_str;
5010 tree fn, fmt, fp, arg;
5012 /* If the return value is used, don't do the transformation. */
5013 if (target != const0_rtx)
5014 return 0;
5016 /* Verify the required arguments in the original call. */
5017 if (! arglist)
5018 return 0;
5019 fp = TREE_VALUE (arglist);
5020 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5021 return 0;
5022 arglist = TREE_CHAIN (arglist);
5023 if (! arglist)
5024 return 0;
5025 fmt = TREE_VALUE (arglist);
5026 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5027 return 0;
5028 arglist = TREE_CHAIN (arglist);
5030 /* Check whether the format is a literal string constant. */
5031 fmt_str = c_getstr (fmt);
5032 if (fmt_str == NULL)
5033 return 0;
5035 if (!init_target_chars())
5036 return 0;
5038 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5039 if (strcmp (fmt_str, target_percent_s) == 0)
5041 if (! arglist
5042 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5043 || TREE_CHAIN (arglist))
5044 return 0;
5045 arg = TREE_VALUE (arglist);
5046 arglist = build_tree_list (NULL_TREE, fp);
5047 arglist = tree_cons (NULL_TREE, arg, arglist);
5048 fn = fn_fputs;
5050 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5051 else if (strcmp (fmt_str, target_percent_c) == 0)
5053 if (! arglist
5054 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5055 || TREE_CHAIN (arglist))
5056 return 0;
5057 arg = TREE_VALUE (arglist);
5058 arglist = build_tree_list (NULL_TREE, fp);
5059 arglist = tree_cons (NULL_TREE, arg, arglist);
5060 fn = fn_fputc;
5062 else
5064 /* We can't handle anything else with % args or %% ... yet. */
5065 if (strchr (fmt_str, target_percent))
5066 return 0;
5068 if (arglist)
5069 return 0;
5071 /* If the format specifier was "", fprintf does nothing. */
5072 if (fmt_str[0] == '\0')
5074 /* Evaluate and ignore FILE* argument for side-effects. */
5075 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5076 return const0_rtx;
5079 /* When "string" doesn't contain %, replace all cases of
5080 fprintf(stream,string) with fputs(string,stream). The fputs
5081 builtin will take care of special cases like length == 1. */
5082 arglist = build_tree_list (NULL_TREE, fp);
5083 arglist = tree_cons (NULL_TREE, fmt, arglist);
5084 fn = fn_fputs;
5087 if (!fn)
5088 return 0;
5089 fn = build_function_call_expr (fn, arglist);
5090 if (TREE_CODE (fn) == CALL_EXPR)
5091 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5092 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5095 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5096 a normal call should be emitted rather than expanding the function
5097 inline. If convenient, the result should be placed in TARGET with
5098 mode MODE. */
5100 static rtx
5101 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5103 tree orig_arglist, dest, fmt;
5104 const char *fmt_str;
5106 orig_arglist = arglist;
5108 /* Verify the required arguments in the original call. */
5109 if (! arglist)
5110 return 0;
5111 dest = TREE_VALUE (arglist);
5112 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5113 return 0;
5114 arglist = TREE_CHAIN (arglist);
5115 if (! arglist)
5116 return 0;
5117 fmt = TREE_VALUE (arglist);
5118 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5119 return 0;
5120 arglist = TREE_CHAIN (arglist);
5122 /* Check whether the format is a literal string constant. */
5123 fmt_str = c_getstr (fmt);
5124 if (fmt_str == NULL)
5125 return 0;
5127 if (!init_target_chars())
5128 return 0;
5130 /* If the format doesn't contain % args or %%, use strcpy. */
5131 if (strchr (fmt_str, target_percent) == 0)
5133 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5134 tree exp;
5136 if (arglist || ! fn)
5137 return 0;
5138 expand_expr (build_function_call_expr (fn, orig_arglist),
5139 const0_rtx, VOIDmode, EXPAND_NORMAL);
5140 if (target == const0_rtx)
5141 return const0_rtx;
5142 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5143 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5145 /* If the format is "%s", use strcpy if the result isn't used. */
5146 else if (strcmp (fmt_str, target_percent_s) == 0)
5148 tree fn, arg, len;
5149 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5151 if (! fn)
5152 return 0;
5154 if (! arglist || TREE_CHAIN (arglist))
5155 return 0;
5156 arg = TREE_VALUE (arglist);
5157 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5158 return 0;
5160 if (target != const0_rtx)
5162 len = c_strlen (arg, 1);
5163 if (! len || TREE_CODE (len) != INTEGER_CST)
5164 return 0;
5166 else
5167 len = NULL_TREE;
5169 arglist = build_tree_list (NULL_TREE, arg);
5170 arglist = tree_cons (NULL_TREE, dest, arglist);
5171 expand_expr (build_function_call_expr (fn, arglist),
5172 const0_rtx, VOIDmode, EXPAND_NORMAL);
5174 if (target == const0_rtx)
5175 return const0_rtx;
5176 return expand_expr (len, target, mode, EXPAND_NORMAL);
5179 return 0;
5182 /* Expand a call to either the entry or exit function profiler. */
5184 static rtx
5185 expand_builtin_profile_func (bool exitp)
5187 rtx this, which;
5189 this = DECL_RTL (current_function_decl);
5190 gcc_assert (MEM_P (this));
5191 this = XEXP (this, 0);
5193 if (exitp)
5194 which = profile_function_exit_libfunc;
5195 else
5196 which = profile_function_entry_libfunc;
5198 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5199 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5201 Pmode);
5203 return const0_rtx;
5206 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5208 static rtx
5209 round_trampoline_addr (rtx tramp)
5211 rtx temp, addend, mask;
5213 /* If we don't need too much alignment, we'll have been guaranteed
5214 proper alignment by get_trampoline_type. */
5215 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5216 return tramp;
5218 /* Round address up to desired boundary. */
5219 temp = gen_reg_rtx (Pmode);
5220 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5221 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5223 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5224 temp, 0, OPTAB_LIB_WIDEN);
5225 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5226 temp, 0, OPTAB_LIB_WIDEN);
5228 return tramp;
5231 static rtx
5232 expand_builtin_init_trampoline (tree arglist)
5234 tree t_tramp, t_func, t_chain;
5235 rtx r_tramp, r_func, r_chain;
5236 #ifdef TRAMPOLINE_TEMPLATE
5237 rtx blktramp;
5238 #endif
5240 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5241 POINTER_TYPE, VOID_TYPE))
5242 return NULL_RTX;
5244 t_tramp = TREE_VALUE (arglist);
5245 arglist = TREE_CHAIN (arglist);
5246 t_func = TREE_VALUE (arglist);
5247 arglist = TREE_CHAIN (arglist);
5248 t_chain = TREE_VALUE (arglist);
5250 r_tramp = expand_normal (t_tramp);
5251 r_func = expand_normal (t_func);
5252 r_chain = expand_normal (t_chain);
5254 /* Generate insns to initialize the trampoline. */
5255 r_tramp = round_trampoline_addr (r_tramp);
5256 #ifdef TRAMPOLINE_TEMPLATE
5257 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5258 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5259 emit_block_move (blktramp, assemble_trampoline_template (),
5260 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5261 #endif
5262 trampolines_created = 1;
5263 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5265 return const0_rtx;
5268 static rtx
5269 expand_builtin_adjust_trampoline (tree arglist)
5271 rtx tramp;
5273 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5274 return NULL_RTX;
5276 tramp = expand_normal (TREE_VALUE (arglist));
5277 tramp = round_trampoline_addr (tramp);
5278 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5279 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5280 #endif
5282 return tramp;
5285 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5286 Return NULL_RTX if a normal call should be emitted rather than expanding
5287 the function in-line. EXP is the expression that is a call to the builtin
5288 function; if convenient, the result should be placed in TARGET. */
5290 static rtx
5291 expand_builtin_signbit (tree exp, rtx target)
5293 const struct real_format *fmt;
5294 enum machine_mode fmode, imode, rmode;
5295 HOST_WIDE_INT hi, lo;
5296 tree arg, arglist;
5297 int word, bitpos;
5298 rtx temp;
5300 arglist = TREE_OPERAND (exp, 1);
5301 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5302 return 0;
5304 arg = TREE_VALUE (arglist);
5305 fmode = TYPE_MODE (TREE_TYPE (arg));
5306 rmode = TYPE_MODE (TREE_TYPE (exp));
5307 fmt = REAL_MODE_FORMAT (fmode);
5309 /* For floating point formats without a sign bit, implement signbit
5310 as "ARG < 0.0". */
5311 bitpos = fmt->signbit_ro;
5312 if (bitpos < 0)
5314 /* But we can't do this if the format supports signed zero. */
5315 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5316 return 0;
5318 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5319 build_real (TREE_TYPE (arg), dconst0));
5320 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5323 temp = expand_normal (arg);
5324 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5326 imode = int_mode_for_mode (fmode);
5327 if (imode == BLKmode)
5328 return 0;
5329 temp = gen_lowpart (imode, temp);
5331 else
5333 imode = word_mode;
5334 /* Handle targets with different FP word orders. */
5335 if (FLOAT_WORDS_BIG_ENDIAN)
5336 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5337 else
5338 word = bitpos / BITS_PER_WORD;
5339 temp = operand_subword_force (temp, word, fmode);
5340 bitpos = bitpos % BITS_PER_WORD;
5343 /* Force the intermediate word_mode (or narrower) result into a
5344 register. This avoids attempting to create paradoxical SUBREGs
5345 of floating point modes below. */
5346 temp = force_reg (imode, temp);
5348 /* If the bitpos is within the "result mode" lowpart, the operation
5349 can be implement with a single bitwise AND. Otherwise, we need
5350 a right shift and an AND. */
5352 if (bitpos < GET_MODE_BITSIZE (rmode))
5354 if (bitpos < HOST_BITS_PER_WIDE_INT)
5356 hi = 0;
5357 lo = (HOST_WIDE_INT) 1 << bitpos;
5359 else
5361 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5362 lo = 0;
5365 if (imode != rmode)
5366 temp = gen_lowpart (rmode, temp);
5367 temp = expand_binop (rmode, and_optab, temp,
5368 immed_double_const (lo, hi, rmode),
5369 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5371 else
5373 /* Perform a logical right shift to place the signbit in the least
5374 significant bit, then truncate the result to the desired mode
5375 and mask just this bit. */
5376 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5377 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5378 temp = gen_lowpart (rmode, temp);
5379 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5380 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5383 return temp;
5386 /* Expand fork or exec calls. TARGET is the desired target of the
5387 call. ARGLIST is the list of arguments of the call. FN is the
5388 identificator of the actual function. IGNORE is nonzero if the
5389 value is to be ignored. */
5391 static rtx
5392 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5394 tree id, decl;
5395 tree call;
5397 /* If we are not profiling, just call the function. */
5398 if (!profile_arc_flag)
5399 return NULL_RTX;
5401 /* Otherwise call the wrapper. This should be equivalent for the rest of
5402 compiler, so the code does not diverge, and the wrapper may run the
5403 code necessary for keeping the profiling sane. */
5405 switch (DECL_FUNCTION_CODE (fn))
5407 case BUILT_IN_FORK:
5408 id = get_identifier ("__gcov_fork");
5409 break;
5411 case BUILT_IN_EXECL:
5412 id = get_identifier ("__gcov_execl");
5413 break;
5415 case BUILT_IN_EXECV:
5416 id = get_identifier ("__gcov_execv");
5417 break;
5419 case BUILT_IN_EXECLP:
5420 id = get_identifier ("__gcov_execlp");
5421 break;
5423 case BUILT_IN_EXECLE:
5424 id = get_identifier ("__gcov_execle");
5425 break;
5427 case BUILT_IN_EXECVP:
5428 id = get_identifier ("__gcov_execvp");
5429 break;
5431 case BUILT_IN_EXECVE:
5432 id = get_identifier ("__gcov_execve");
5433 break;
5435 default:
5436 gcc_unreachable ();
5439 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5440 DECL_EXTERNAL (decl) = 1;
5441 TREE_PUBLIC (decl) = 1;
5442 DECL_ARTIFICIAL (decl) = 1;
5443 TREE_NOTHROW (decl) = 1;
5444 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5445 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5446 call = build_function_call_expr (decl, arglist);
5448 return expand_call (call, target, ignore);
5452 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5453 the pointer in these functions is void*, the tree optimizers may remove
5454 casts. The mode computed in expand_builtin isn't reliable either, due
5455 to __sync_bool_compare_and_swap.
5457 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5458 group of builtins. This gives us log2 of the mode size. */
5460 static inline enum machine_mode
5461 get_builtin_sync_mode (int fcode_diff)
5463 /* The size is not negotiable, so ask not to get BLKmode in return
5464 if the target indicates that a smaller size would be better. */
5465 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5468 /* Expand the memory expression LOC and return the appropriate memory operand
5469 for the builtin_sync operations. */
5471 static rtx
5472 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5474 rtx addr, mem;
5476 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5478 /* Note that we explicitly do not want any alias information for this
5479 memory, so that we kill all other live memories. Otherwise we don't
5480 satisfy the full barrier semantics of the intrinsic. */
5481 mem = validize_mem (gen_rtx_MEM (mode, addr));
5483 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5484 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5485 MEM_VOLATILE_P (mem) = 1;
5487 return mem;
5490 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5491 ARGLIST is the operands list to the function. CODE is the rtx code
5492 that corresponds to the arithmetic or logical operation from the name;
5493 an exception here is that NOT actually means NAND. TARGET is an optional
5494 place for us to store the results; AFTER is true if this is the
5495 fetch_and_xxx form. IGNORE is true if we don't actually care about
5496 the result of the operation at all. */
5498 static rtx
5499 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5500 enum rtx_code code, bool after,
5501 rtx target, bool ignore)
5503 rtx val, mem;
5505 /* Expand the operands. */
5506 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5508 arglist = TREE_CHAIN (arglist);
5509 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5510 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5511 val = convert_to_mode (mode, val, 1);
5513 if (ignore)
5514 return expand_sync_operation (mem, val, code);
5515 else
5516 return expand_sync_fetch_operation (mem, val, code, after, target);
5519 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5520 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5521 true if this is the boolean form. TARGET is a place for us to store the
5522 results; this is NOT optional if IS_BOOL is true. */
5524 static rtx
5525 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5526 bool is_bool, rtx target)
5528 rtx old_val, new_val, mem;
5530 /* Expand the operands. */
5531 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5533 arglist = TREE_CHAIN (arglist);
5534 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5535 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5536 old_val = convert_to_mode (mode, old_val, 1);
5538 arglist = TREE_CHAIN (arglist);
5539 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5540 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5541 new_val = convert_to_mode (mode, new_val, 1);
5543 if (is_bool)
5544 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5545 else
5546 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5549 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5550 general form is actually an atomic exchange, and some targets only
5551 support a reduced form with the second argument being a constant 1.
5552 ARGLIST is the operands list to the function; TARGET is an optional
5553 place for us to store the results. */
5555 static rtx
5556 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5557 rtx target)
5559 rtx val, mem;
5561 /* Expand the operands. */
5562 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5564 arglist = TREE_CHAIN (arglist);
5565 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5566 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5567 val = convert_to_mode (mode, val, 1);
5569 return expand_sync_lock_test_and_set (mem, val, target);
5572 /* Expand the __sync_synchronize intrinsic. */
5574 static void
5575 expand_builtin_synchronize (void)
5577 tree x;
5579 #ifdef HAVE_memory_barrier
5580 if (HAVE_memory_barrier)
5582 emit_insn (gen_memory_barrier ());
5583 return;
5585 #endif
5587 /* If no explicit memory barrier instruction is available, create an
5588 empty asm stmt with a memory clobber. */
5589 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5590 tree_cons (NULL, build_string (6, "memory"), NULL));
5591 ASM_VOLATILE_P (x) = 1;
5592 expand_asm_expr (x);
5595 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5596 to the function. */
5598 static void
5599 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5601 enum insn_code icode;
5602 rtx mem, insn;
5603 rtx val = const0_rtx;
5605 /* Expand the operands. */
5606 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5608 /* If there is an explicit operation in the md file, use it. */
5609 icode = sync_lock_release[mode];
5610 if (icode != CODE_FOR_nothing)
5612 if (!insn_data[icode].operand[1].predicate (val, mode))
5613 val = force_reg (mode, val);
5615 insn = GEN_FCN (icode) (mem, val);
5616 if (insn)
5618 emit_insn (insn);
5619 return;
5623 /* Otherwise we can implement this operation by emitting a barrier
5624 followed by a store of zero. */
5625 expand_builtin_synchronize ();
5626 emit_move_insn (mem, val);
5629 /* Expand an expression EXP that calls a built-in function,
5630 with result going to TARGET if that's convenient
5631 (and in mode MODE if that's convenient).
5632 SUBTARGET may be used as the target for computing one of EXP's operands.
5633 IGNORE is nonzero if the value is to be ignored. */
5636 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5637 int ignore)
5639 tree fndecl = get_callee_fndecl (exp);
5640 tree arglist = TREE_OPERAND (exp, 1);
5641 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5642 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5644 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5645 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5647 /* When not optimizing, generate calls to library functions for a certain
5648 set of builtins. */
5649 if (!optimize
5650 && !called_as_built_in (fndecl)
5651 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5652 && fcode != BUILT_IN_ALLOCA)
5653 return expand_call (exp, target, ignore);
5655 /* The built-in function expanders test for target == const0_rtx
5656 to determine whether the function's result will be ignored. */
5657 if (ignore)
5658 target = const0_rtx;
5660 /* If the result of a pure or const built-in function is ignored, and
5661 none of its arguments are volatile, we can avoid expanding the
5662 built-in call and just evaluate the arguments for side-effects. */
5663 if (target == const0_rtx
5664 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5666 bool volatilep = false;
5667 tree arg;
5669 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5670 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5672 volatilep = true;
5673 break;
5676 if (! volatilep)
5678 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5679 expand_expr (TREE_VALUE (arg), const0_rtx,
5680 VOIDmode, EXPAND_NORMAL);
5681 return const0_rtx;
5685 switch (fcode)
5687 CASE_FLT_FN (BUILT_IN_FABS):
5688 target = expand_builtin_fabs (arglist, target, subtarget);
5689 if (target)
5690 return target;
5691 break;
5693 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5694 target = expand_builtin_copysign (arglist, target, subtarget);
5695 if (target)
5696 return target;
5697 break;
5699 /* Just do a normal library call if we were unable to fold
5700 the values. */
5701 CASE_FLT_FN (BUILT_IN_CABS):
5702 break;
5704 CASE_FLT_FN (BUILT_IN_EXP):
5705 CASE_FLT_FN (BUILT_IN_EXP10):
5706 CASE_FLT_FN (BUILT_IN_POW10):
5707 CASE_FLT_FN (BUILT_IN_EXP2):
5708 CASE_FLT_FN (BUILT_IN_EXPM1):
5709 CASE_FLT_FN (BUILT_IN_LOGB):
5710 CASE_FLT_FN (BUILT_IN_ILOGB):
5711 CASE_FLT_FN (BUILT_IN_LOG):
5712 CASE_FLT_FN (BUILT_IN_LOG10):
5713 CASE_FLT_FN (BUILT_IN_LOG2):
5714 CASE_FLT_FN (BUILT_IN_LOG1P):
5715 CASE_FLT_FN (BUILT_IN_TAN):
5716 CASE_FLT_FN (BUILT_IN_ASIN):
5717 CASE_FLT_FN (BUILT_IN_ACOS):
5718 CASE_FLT_FN (BUILT_IN_ATAN):
5719 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5720 because of possible accuracy problems. */
5721 if (! flag_unsafe_math_optimizations)
5722 break;
5723 CASE_FLT_FN (BUILT_IN_SQRT):
5724 CASE_FLT_FN (BUILT_IN_FLOOR):
5725 CASE_FLT_FN (BUILT_IN_CEIL):
5726 CASE_FLT_FN (BUILT_IN_TRUNC):
5727 CASE_FLT_FN (BUILT_IN_ROUND):
5728 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5729 CASE_FLT_FN (BUILT_IN_RINT):
5730 target = expand_builtin_mathfn (exp, target, subtarget);
5731 if (target)
5732 return target;
5733 break;
5735 CASE_FLT_FN (BUILT_IN_LCEIL):
5736 CASE_FLT_FN (BUILT_IN_LLCEIL):
5737 CASE_FLT_FN (BUILT_IN_LFLOOR):
5738 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5739 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5740 if (target)
5741 return target;
5742 break;
5744 CASE_FLT_FN (BUILT_IN_LRINT):
5745 CASE_FLT_FN (BUILT_IN_LLRINT):
5746 CASE_FLT_FN (BUILT_IN_LROUND):
5747 CASE_FLT_FN (BUILT_IN_LLROUND):
5748 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5749 if (target)
5750 return target;
5751 break;
5753 CASE_FLT_FN (BUILT_IN_POW):
5754 target = expand_builtin_pow (exp, target, subtarget);
5755 if (target)
5756 return target;
5757 break;
5759 CASE_FLT_FN (BUILT_IN_POWI):
5760 target = expand_builtin_powi (exp, target, subtarget);
5761 if (target)
5762 return target;
5763 break;
5765 CASE_FLT_FN (BUILT_IN_ATAN2):
5766 CASE_FLT_FN (BUILT_IN_LDEXP):
5767 if (! flag_unsafe_math_optimizations)
5768 break;
5770 CASE_FLT_FN (BUILT_IN_FMOD):
5771 CASE_FLT_FN (BUILT_IN_REMAINDER):
5772 CASE_FLT_FN (BUILT_IN_DREM):
5773 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5774 if (target)
5775 return target;
5776 break;
5778 CASE_FLT_FN (BUILT_IN_SIN):
5779 CASE_FLT_FN (BUILT_IN_COS):
5780 if (! flag_unsafe_math_optimizations)
5781 break;
5782 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5783 if (target)
5784 return target;
5785 break;
5787 CASE_FLT_FN (BUILT_IN_SINCOS):
5788 if (! flag_unsafe_math_optimizations)
5789 break;
5790 target = expand_builtin_sincos (exp);
5791 if (target)
5792 return target;
5793 break;
5795 case BUILT_IN_APPLY_ARGS:
5796 return expand_builtin_apply_args ();
5798 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5799 FUNCTION with a copy of the parameters described by
5800 ARGUMENTS, and ARGSIZE. It returns a block of memory
5801 allocated on the stack into which is stored all the registers
5802 that might possibly be used for returning the result of a
5803 function. ARGUMENTS is the value returned by
5804 __builtin_apply_args. ARGSIZE is the number of bytes of
5805 arguments that must be copied. ??? How should this value be
5806 computed? We'll also need a safe worst case value for varargs
5807 functions. */
5808 case BUILT_IN_APPLY:
5809 if (!validate_arglist (arglist, POINTER_TYPE,
5810 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5811 && !validate_arglist (arglist, REFERENCE_TYPE,
5812 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5813 return const0_rtx;
5814 else
5816 int i;
5817 tree t;
5818 rtx ops[3];
5820 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5821 ops[i] = expand_normal (TREE_VALUE (t));
5823 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5826 /* __builtin_return (RESULT) causes the function to return the
5827 value described by RESULT. RESULT is address of the block of
5828 memory returned by __builtin_apply. */
5829 case BUILT_IN_RETURN:
5830 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5831 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5832 return const0_rtx;
5834 case BUILT_IN_SAVEREGS:
5835 return expand_builtin_saveregs ();
5837 case BUILT_IN_ARGS_INFO:
5838 return expand_builtin_args_info (arglist);
5840 /* Return the address of the first anonymous stack arg. */
5841 case BUILT_IN_NEXT_ARG:
5842 if (fold_builtin_next_arg (arglist))
5843 return const0_rtx;
5844 return expand_builtin_next_arg ();
5846 case BUILT_IN_CLASSIFY_TYPE:
5847 return expand_builtin_classify_type (arglist);
5849 case BUILT_IN_CONSTANT_P:
5850 return const0_rtx;
5852 case BUILT_IN_FRAME_ADDRESS:
5853 case BUILT_IN_RETURN_ADDRESS:
5854 return expand_builtin_frame_address (fndecl, arglist);
5856 /* Returns the address of the area where the structure is returned.
5857 0 otherwise. */
5858 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5859 if (arglist != 0
5860 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5861 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5862 return const0_rtx;
5863 else
5864 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5866 case BUILT_IN_ALLOCA:
5867 target = expand_builtin_alloca (arglist, target);
5868 if (target)
5869 return target;
5870 break;
5872 case BUILT_IN_STACK_SAVE:
5873 return expand_stack_save ();
5875 case BUILT_IN_STACK_RESTORE:
5876 expand_stack_restore (TREE_VALUE (arglist));
5877 return const0_rtx;
5879 case BUILT_IN_BSWAP32:
5880 case BUILT_IN_BSWAP64:
5881 target = expand_builtin_bswap (arglist, target, subtarget);
5883 if (target)
5884 return target;
5885 break;
5887 CASE_INT_FN (BUILT_IN_FFS):
5888 case BUILT_IN_FFSIMAX:
5889 target = expand_builtin_unop (target_mode, arglist, target,
5890 subtarget, ffs_optab);
5891 if (target)
5892 return target;
5893 break;
5895 CASE_INT_FN (BUILT_IN_CLZ):
5896 case BUILT_IN_CLZIMAX:
5897 target = expand_builtin_unop (target_mode, arglist, target,
5898 subtarget, clz_optab);
5899 if (target)
5900 return target;
5901 break;
5903 CASE_INT_FN (BUILT_IN_CTZ):
5904 case BUILT_IN_CTZIMAX:
5905 target = expand_builtin_unop (target_mode, arglist, target,
5906 subtarget, ctz_optab);
5907 if (target)
5908 return target;
5909 break;
5911 CASE_INT_FN (BUILT_IN_POPCOUNT):
5912 case BUILT_IN_POPCOUNTIMAX:
5913 target = expand_builtin_unop (target_mode, arglist, target,
5914 subtarget, popcount_optab);
5915 if (target)
5916 return target;
5917 break;
5919 CASE_INT_FN (BUILT_IN_PARITY):
5920 case BUILT_IN_PARITYIMAX:
5921 target = expand_builtin_unop (target_mode, arglist, target,
5922 subtarget, parity_optab);
5923 if (target)
5924 return target;
5925 break;
5927 case BUILT_IN_STRLEN:
5928 target = expand_builtin_strlen (arglist, target, target_mode);
5929 if (target)
5930 return target;
5931 break;
5933 case BUILT_IN_STRCPY:
5934 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5935 if (target)
5936 return target;
5937 break;
5939 case BUILT_IN_STRNCPY:
5940 target = expand_builtin_strncpy (exp, target, mode);
5941 if (target)
5942 return target;
5943 break;
5945 case BUILT_IN_STPCPY:
5946 target = expand_builtin_stpcpy (exp, target, mode);
5947 if (target)
5948 return target;
5949 break;
5951 case BUILT_IN_STRCAT:
5952 target = expand_builtin_strcat (fndecl, arglist, target, mode);
5953 if (target)
5954 return target;
5955 break;
5957 case BUILT_IN_STRNCAT:
5958 target = expand_builtin_strncat (arglist, target, mode);
5959 if (target)
5960 return target;
5961 break;
5963 case BUILT_IN_STRSPN:
5964 target = expand_builtin_strspn (arglist, target, mode);
5965 if (target)
5966 return target;
5967 break;
5969 case BUILT_IN_STRCSPN:
5970 target = expand_builtin_strcspn (arglist, target, mode);
5971 if (target)
5972 return target;
5973 break;
5975 case BUILT_IN_STRSTR:
5976 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5977 if (target)
5978 return target;
5979 break;
5981 case BUILT_IN_STRPBRK:
5982 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5983 if (target)
5984 return target;
5985 break;
5987 case BUILT_IN_INDEX:
5988 case BUILT_IN_STRCHR:
5989 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5990 if (target)
5991 return target;
5992 break;
5994 case BUILT_IN_RINDEX:
5995 case BUILT_IN_STRRCHR:
5996 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5997 if (target)
5998 return target;
5999 break;
6001 case BUILT_IN_MEMCPY:
6002 target = expand_builtin_memcpy (exp, target, mode);
6003 if (target)
6004 return target;
6005 break;
6007 case BUILT_IN_MEMPCPY:
6008 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6009 if (target)
6010 return target;
6011 break;
6013 case BUILT_IN_MEMMOVE:
6014 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6015 mode);
6016 if (target)
6017 return target;
6018 break;
6020 case BUILT_IN_BCOPY:
6021 target = expand_builtin_bcopy (exp);
6022 if (target)
6023 return target;
6024 break;
6026 case BUILT_IN_MEMSET:
6027 target = expand_builtin_memset (arglist, target, mode, exp);
6028 if (target)
6029 return target;
6030 break;
6032 case BUILT_IN_BZERO:
6033 target = expand_builtin_bzero (exp);
6034 if (target)
6035 return target;
6036 break;
6038 case BUILT_IN_STRCMP:
6039 target = expand_builtin_strcmp (exp, target, mode);
6040 if (target)
6041 return target;
6042 break;
6044 case BUILT_IN_STRNCMP:
6045 target = expand_builtin_strncmp (exp, target, mode);
6046 if (target)
6047 return target;
6048 break;
6050 case BUILT_IN_BCMP:
6051 case BUILT_IN_MEMCMP:
6052 target = expand_builtin_memcmp (exp, arglist, target, mode);
6053 if (target)
6054 return target;
6055 break;
6057 case BUILT_IN_SETJMP:
6058 /* This should have been lowered to the builtins below. */
6059 gcc_unreachable ();
6061 case BUILT_IN_SETJMP_SETUP:
6062 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6063 and the receiver label. */
6064 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6066 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6067 VOIDmode, EXPAND_NORMAL);
6068 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6069 rtx label_r = label_rtx (label);
6071 /* This is copied from the handling of non-local gotos. */
6072 expand_builtin_setjmp_setup (buf_addr, label_r);
6073 nonlocal_goto_handler_labels
6074 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6075 nonlocal_goto_handler_labels);
6076 /* ??? Do not let expand_label treat us as such since we would
6077 not want to be both on the list of non-local labels and on
6078 the list of forced labels. */
6079 FORCED_LABEL (label) = 0;
6080 return const0_rtx;
6082 break;
6084 case BUILT_IN_SETJMP_DISPATCHER:
6085 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6086 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6088 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6089 rtx label_r = label_rtx (label);
6091 /* Remove the dispatcher label from the list of non-local labels
6092 since the receiver labels have been added to it above. */
6093 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6094 return const0_rtx;
6096 break;
6098 case BUILT_IN_SETJMP_RECEIVER:
6099 /* __builtin_setjmp_receiver is passed the receiver label. */
6100 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6102 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6103 rtx label_r = label_rtx (label);
6105 expand_builtin_setjmp_receiver (label_r);
6106 return const0_rtx;
6108 break;
6110 /* __builtin_longjmp is passed a pointer to an array of five words.
6111 It's similar to the C library longjmp function but works with
6112 __builtin_setjmp above. */
6113 case BUILT_IN_LONGJMP:
6114 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6116 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6117 VOIDmode, EXPAND_NORMAL);
6118 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6120 if (value != const1_rtx)
6122 error ("%<__builtin_longjmp%> second argument must be 1");
6123 return const0_rtx;
6126 expand_builtin_longjmp (buf_addr, value);
6127 return const0_rtx;
6129 break;
6131 case BUILT_IN_NONLOCAL_GOTO:
6132 target = expand_builtin_nonlocal_goto (arglist);
6133 if (target)
6134 return target;
6135 break;
6137 /* This updates the setjmp buffer that is its argument with the value
6138 of the current stack pointer. */
6139 case BUILT_IN_UPDATE_SETJMP_BUF:
6140 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6142 rtx buf_addr
6143 = expand_normal (TREE_VALUE (arglist));
6145 expand_builtin_update_setjmp_buf (buf_addr);
6146 return const0_rtx;
6148 break;
6150 case BUILT_IN_TRAP:
6151 expand_builtin_trap ();
6152 return const0_rtx;
6154 case BUILT_IN_PRINTF:
6155 target = expand_builtin_printf (exp, target, mode, false);
6156 if (target)
6157 return target;
6158 break;
6160 case BUILT_IN_PRINTF_UNLOCKED:
6161 target = expand_builtin_printf (exp, target, mode, true);
6162 if (target)
6163 return target;
6164 break;
6166 case BUILT_IN_FPUTS:
6167 target = expand_builtin_fputs (arglist, target, false);
6168 if (target)
6169 return target;
6170 break;
6171 case BUILT_IN_FPUTS_UNLOCKED:
6172 target = expand_builtin_fputs (arglist, target, true);
6173 if (target)
6174 return target;
6175 break;
6177 case BUILT_IN_FPRINTF:
6178 target = expand_builtin_fprintf (exp, target, mode, false);
6179 if (target)
6180 return target;
6181 break;
6183 case BUILT_IN_FPRINTF_UNLOCKED:
6184 target = expand_builtin_fprintf (exp, target, mode, true);
6185 if (target)
6186 return target;
6187 break;
6189 case BUILT_IN_SPRINTF:
6190 target = expand_builtin_sprintf (arglist, target, mode);
6191 if (target)
6192 return target;
6193 break;
6195 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6196 target = expand_builtin_signbit (exp, target);
6197 if (target)
6198 return target;
6199 break;
6201 /* Various hooks for the DWARF 2 __throw routine. */
6202 case BUILT_IN_UNWIND_INIT:
6203 expand_builtin_unwind_init ();
6204 return const0_rtx;
6205 case BUILT_IN_DWARF_CFA:
6206 return virtual_cfa_rtx;
6207 #ifdef DWARF2_UNWIND_INFO
6208 case BUILT_IN_DWARF_SP_COLUMN:
6209 return expand_builtin_dwarf_sp_column ();
6210 case BUILT_IN_INIT_DWARF_REG_SIZES:
6211 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6212 return const0_rtx;
6213 #endif
6214 case BUILT_IN_FROB_RETURN_ADDR:
6215 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6216 case BUILT_IN_EXTRACT_RETURN_ADDR:
6217 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6218 case BUILT_IN_EH_RETURN:
6219 expand_builtin_eh_return (TREE_VALUE (arglist),
6220 TREE_VALUE (TREE_CHAIN (arglist)));
6221 return const0_rtx;
6222 #ifdef EH_RETURN_DATA_REGNO
6223 case BUILT_IN_EH_RETURN_DATA_REGNO:
6224 return expand_builtin_eh_return_data_regno (arglist);
6225 #endif
6226 case BUILT_IN_EXTEND_POINTER:
6227 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6229 case BUILT_IN_VA_START:
6230 case BUILT_IN_STDARG_START:
6231 return expand_builtin_va_start (arglist);
6232 case BUILT_IN_VA_END:
6233 return expand_builtin_va_end (arglist);
6234 case BUILT_IN_VA_COPY:
6235 return expand_builtin_va_copy (arglist);
6236 case BUILT_IN_EXPECT:
6237 return expand_builtin_expect (arglist, target);
6238 case BUILT_IN_PREFETCH:
6239 expand_builtin_prefetch (arglist);
6240 return const0_rtx;
6242 case BUILT_IN_PROFILE_FUNC_ENTER:
6243 return expand_builtin_profile_func (false);
6244 case BUILT_IN_PROFILE_FUNC_EXIT:
6245 return expand_builtin_profile_func (true);
6247 case BUILT_IN_INIT_TRAMPOLINE:
6248 return expand_builtin_init_trampoline (arglist);
6249 case BUILT_IN_ADJUST_TRAMPOLINE:
6250 return expand_builtin_adjust_trampoline (arglist);
6252 case BUILT_IN_FORK:
6253 case BUILT_IN_EXECL:
6254 case BUILT_IN_EXECV:
6255 case BUILT_IN_EXECLP:
6256 case BUILT_IN_EXECLE:
6257 case BUILT_IN_EXECVP:
6258 case BUILT_IN_EXECVE:
6259 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6260 if (target)
6261 return target;
6262 break;
6264 case BUILT_IN_FETCH_AND_ADD_1:
6265 case BUILT_IN_FETCH_AND_ADD_2:
6266 case BUILT_IN_FETCH_AND_ADD_4:
6267 case BUILT_IN_FETCH_AND_ADD_8:
6268 case BUILT_IN_FETCH_AND_ADD_16:
6269 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6270 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6271 false, target, ignore);
6272 if (target)
6273 return target;
6274 break;
6276 case BUILT_IN_FETCH_AND_SUB_1:
6277 case BUILT_IN_FETCH_AND_SUB_2:
6278 case BUILT_IN_FETCH_AND_SUB_4:
6279 case BUILT_IN_FETCH_AND_SUB_8:
6280 case BUILT_IN_FETCH_AND_SUB_16:
6281 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6282 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6283 false, target, ignore);
6284 if (target)
6285 return target;
6286 break;
6288 case BUILT_IN_FETCH_AND_OR_1:
6289 case BUILT_IN_FETCH_AND_OR_2:
6290 case BUILT_IN_FETCH_AND_OR_4:
6291 case BUILT_IN_FETCH_AND_OR_8:
6292 case BUILT_IN_FETCH_AND_OR_16:
6293 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6294 target = expand_builtin_sync_operation (mode, arglist, IOR,
6295 false, target, ignore);
6296 if (target)
6297 return target;
6298 break;
6300 case BUILT_IN_FETCH_AND_AND_1:
6301 case BUILT_IN_FETCH_AND_AND_2:
6302 case BUILT_IN_FETCH_AND_AND_4:
6303 case BUILT_IN_FETCH_AND_AND_8:
6304 case BUILT_IN_FETCH_AND_AND_16:
6305 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6306 target = expand_builtin_sync_operation (mode, arglist, AND,
6307 false, target, ignore);
6308 if (target)
6309 return target;
6310 break;
6312 case BUILT_IN_FETCH_AND_XOR_1:
6313 case BUILT_IN_FETCH_AND_XOR_2:
6314 case BUILT_IN_FETCH_AND_XOR_4:
6315 case BUILT_IN_FETCH_AND_XOR_8:
6316 case BUILT_IN_FETCH_AND_XOR_16:
6317 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6318 target = expand_builtin_sync_operation (mode, arglist, XOR,
6319 false, target, ignore);
6320 if (target)
6321 return target;
6322 break;
6324 case BUILT_IN_FETCH_AND_NAND_1:
6325 case BUILT_IN_FETCH_AND_NAND_2:
6326 case BUILT_IN_FETCH_AND_NAND_4:
6327 case BUILT_IN_FETCH_AND_NAND_8:
6328 case BUILT_IN_FETCH_AND_NAND_16:
6329 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6330 target = expand_builtin_sync_operation (mode, arglist, NOT,
6331 false, target, ignore);
6332 if (target)
6333 return target;
6334 break;
6336 case BUILT_IN_ADD_AND_FETCH_1:
6337 case BUILT_IN_ADD_AND_FETCH_2:
6338 case BUILT_IN_ADD_AND_FETCH_4:
6339 case BUILT_IN_ADD_AND_FETCH_8:
6340 case BUILT_IN_ADD_AND_FETCH_16:
6341 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6342 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6343 true, target, ignore);
6344 if (target)
6345 return target;
6346 break;
6348 case BUILT_IN_SUB_AND_FETCH_1:
6349 case BUILT_IN_SUB_AND_FETCH_2:
6350 case BUILT_IN_SUB_AND_FETCH_4:
6351 case BUILT_IN_SUB_AND_FETCH_8:
6352 case BUILT_IN_SUB_AND_FETCH_16:
6353 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6354 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6355 true, target, ignore);
6356 if (target)
6357 return target;
6358 break;
6360 case BUILT_IN_OR_AND_FETCH_1:
6361 case BUILT_IN_OR_AND_FETCH_2:
6362 case BUILT_IN_OR_AND_FETCH_4:
6363 case BUILT_IN_OR_AND_FETCH_8:
6364 case BUILT_IN_OR_AND_FETCH_16:
6365 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6366 target = expand_builtin_sync_operation (mode, arglist, IOR,
6367 true, target, ignore);
6368 if (target)
6369 return target;
6370 break;
6372 case BUILT_IN_AND_AND_FETCH_1:
6373 case BUILT_IN_AND_AND_FETCH_2:
6374 case BUILT_IN_AND_AND_FETCH_4:
6375 case BUILT_IN_AND_AND_FETCH_8:
6376 case BUILT_IN_AND_AND_FETCH_16:
6377 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6378 target = expand_builtin_sync_operation (mode, arglist, AND,
6379 true, target, ignore);
6380 if (target)
6381 return target;
6382 break;
6384 case BUILT_IN_XOR_AND_FETCH_1:
6385 case BUILT_IN_XOR_AND_FETCH_2:
6386 case BUILT_IN_XOR_AND_FETCH_4:
6387 case BUILT_IN_XOR_AND_FETCH_8:
6388 case BUILT_IN_XOR_AND_FETCH_16:
6389 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6390 target = expand_builtin_sync_operation (mode, arglist, XOR,
6391 true, target, ignore);
6392 if (target)
6393 return target;
6394 break;
6396 case BUILT_IN_NAND_AND_FETCH_1:
6397 case BUILT_IN_NAND_AND_FETCH_2:
6398 case BUILT_IN_NAND_AND_FETCH_4:
6399 case BUILT_IN_NAND_AND_FETCH_8:
6400 case BUILT_IN_NAND_AND_FETCH_16:
6401 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6402 target = expand_builtin_sync_operation (mode, arglist, NOT,
6403 true, target, ignore);
6404 if (target)
6405 return target;
6406 break;
6408 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6409 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6410 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6411 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6412 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6413 if (mode == VOIDmode)
6414 mode = TYPE_MODE (boolean_type_node);
6415 if (!target || !register_operand (target, mode))
6416 target = gen_reg_rtx (mode);
6418 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6419 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6420 if (target)
6421 return target;
6422 break;
6424 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6425 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6426 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6427 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6428 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6429 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6430 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6431 if (target)
6432 return target;
6433 break;
6435 case BUILT_IN_LOCK_TEST_AND_SET_1:
6436 case BUILT_IN_LOCK_TEST_AND_SET_2:
6437 case BUILT_IN_LOCK_TEST_AND_SET_4:
6438 case BUILT_IN_LOCK_TEST_AND_SET_8:
6439 case BUILT_IN_LOCK_TEST_AND_SET_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6441 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6442 if (target)
6443 return target;
6444 break;
6446 case BUILT_IN_LOCK_RELEASE_1:
6447 case BUILT_IN_LOCK_RELEASE_2:
6448 case BUILT_IN_LOCK_RELEASE_4:
6449 case BUILT_IN_LOCK_RELEASE_8:
6450 case BUILT_IN_LOCK_RELEASE_16:
6451 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6452 expand_builtin_lock_release (mode, arglist);
6453 return const0_rtx;
6455 case BUILT_IN_SYNCHRONIZE:
6456 expand_builtin_synchronize ();
6457 return const0_rtx;
6459 case BUILT_IN_OBJECT_SIZE:
6460 return expand_builtin_object_size (exp);
6462 case BUILT_IN_MEMCPY_CHK:
6463 case BUILT_IN_MEMPCPY_CHK:
6464 case BUILT_IN_MEMMOVE_CHK:
6465 case BUILT_IN_MEMSET_CHK:
6466 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6467 if (target)
6468 return target;
6469 break;
6471 case BUILT_IN_STRCPY_CHK:
6472 case BUILT_IN_STPCPY_CHK:
6473 case BUILT_IN_STRNCPY_CHK:
6474 case BUILT_IN_STRCAT_CHK:
6475 case BUILT_IN_STRNCAT_CHK:
6476 case BUILT_IN_SNPRINTF_CHK:
6477 case BUILT_IN_VSNPRINTF_CHK:
6478 maybe_emit_chk_warning (exp, fcode);
6479 break;
6481 case BUILT_IN_SPRINTF_CHK:
6482 case BUILT_IN_VSPRINTF_CHK:
6483 maybe_emit_sprintf_chk_warning (exp, fcode);
6484 break;
6486 default: /* just do library call, if unknown builtin */
6487 break;
6490 /* The switch statement above can drop through to cause the function
6491 to be called normally. */
6492 return expand_call (exp, target, ignore);
6495 /* Determine whether a tree node represents a call to a built-in
6496 function. If the tree T is a call to a built-in function with
6497 the right number of arguments of the appropriate types, return
6498 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6499 Otherwise the return value is END_BUILTINS. */
6501 enum built_in_function
6502 builtin_mathfn_code (tree t)
6504 tree fndecl, arglist, parmlist;
6505 tree argtype, parmtype;
6507 if (TREE_CODE (t) != CALL_EXPR
6508 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6509 return END_BUILTINS;
6511 fndecl = get_callee_fndecl (t);
6512 if (fndecl == NULL_TREE
6513 || TREE_CODE (fndecl) != FUNCTION_DECL
6514 || ! DECL_BUILT_IN (fndecl)
6515 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6516 return END_BUILTINS;
6518 arglist = TREE_OPERAND (t, 1);
6519 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6520 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6522 /* If a function doesn't take a variable number of arguments,
6523 the last element in the list will have type `void'. */
6524 parmtype = TREE_VALUE (parmlist);
6525 if (VOID_TYPE_P (parmtype))
6527 if (arglist)
6528 return END_BUILTINS;
6529 return DECL_FUNCTION_CODE (fndecl);
6532 if (! arglist)
6533 return END_BUILTINS;
6535 argtype = TREE_TYPE (TREE_VALUE (arglist));
6537 if (SCALAR_FLOAT_TYPE_P (parmtype))
6539 if (! SCALAR_FLOAT_TYPE_P (argtype))
6540 return END_BUILTINS;
6542 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6544 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6545 return END_BUILTINS;
6547 else if (POINTER_TYPE_P (parmtype))
6549 if (! POINTER_TYPE_P (argtype))
6550 return END_BUILTINS;
6552 else if (INTEGRAL_TYPE_P (parmtype))
6554 if (! INTEGRAL_TYPE_P (argtype))
6555 return END_BUILTINS;
6557 else
6558 return END_BUILTINS;
6560 arglist = TREE_CHAIN (arglist);
6563 /* Variable-length argument list. */
6564 return DECL_FUNCTION_CODE (fndecl);
6567 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6568 constant. ARGLIST is the argument list of the call. */
6570 static tree
6571 fold_builtin_constant_p (tree arglist)
6573 if (arglist == 0)
6574 return 0;
6576 arglist = TREE_VALUE (arglist);
6578 /* We return 1 for a numeric type that's known to be a constant
6579 value at compile-time or for an aggregate type that's a
6580 literal constant. */
6581 STRIP_NOPS (arglist);
6583 /* If we know this is a constant, emit the constant of one. */
6584 if (CONSTANT_CLASS_P (arglist)
6585 || (TREE_CODE (arglist) == CONSTRUCTOR
6586 && TREE_CONSTANT (arglist)))
6587 return integer_one_node;
6588 if (TREE_CODE (arglist) == ADDR_EXPR)
6590 tree op = TREE_OPERAND (arglist, 0);
6591 if (TREE_CODE (op) == STRING_CST
6592 || (TREE_CODE (op) == ARRAY_REF
6593 && integer_zerop (TREE_OPERAND (op, 1))
6594 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6595 return integer_one_node;
6598 /* If this expression has side effects, show we don't know it to be a
6599 constant. Likewise if it's a pointer or aggregate type since in
6600 those case we only want literals, since those are only optimized
6601 when generating RTL, not later.
6602 And finally, if we are compiling an initializer, not code, we
6603 need to return a definite result now; there's not going to be any
6604 more optimization done. */
6605 if (TREE_SIDE_EFFECTS (arglist)
6606 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6607 || POINTER_TYPE_P (TREE_TYPE (arglist))
6608 || cfun == 0
6609 || folding_initializer)
6610 return integer_zero_node;
6612 return 0;
6615 /* Fold a call to __builtin_expect, if we expect that a comparison against
6616 the argument will fold to a constant. In practice, this means a true
6617 constant or the address of a non-weak symbol. ARGLIST is the argument
6618 list of the call. */
6620 static tree
6621 fold_builtin_expect (tree arglist)
6623 tree arg, inner;
6625 if (arglist == 0)
6626 return 0;
6628 arg = TREE_VALUE (arglist);
6630 /* If the argument isn't invariant, then there's nothing we can do. */
6631 if (!TREE_INVARIANT (arg))
6632 return 0;
6634 /* If we're looking at an address of a weak decl, then do not fold. */
6635 inner = arg;
6636 STRIP_NOPS (inner);
6637 if (TREE_CODE (inner) == ADDR_EXPR)
6641 inner = TREE_OPERAND (inner, 0);
6643 while (TREE_CODE (inner) == COMPONENT_REF
6644 || TREE_CODE (inner) == ARRAY_REF);
6645 if (DECL_P (inner) && DECL_WEAK (inner))
6646 return 0;
6649 /* Otherwise, ARG already has the proper type for the return value. */
6650 return arg;
6653 /* Fold a call to __builtin_classify_type. */
6655 static tree
6656 fold_builtin_classify_type (tree arglist)
6658 if (arglist == 0)
6659 return build_int_cst (NULL_TREE, no_type_class);
6661 return build_int_cst (NULL_TREE,
6662 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6665 /* Fold a call to __builtin_strlen. */
6667 static tree
6668 fold_builtin_strlen (tree arglist)
6670 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6671 return NULL_TREE;
6672 else
6674 tree len = c_strlen (TREE_VALUE (arglist), 0);
6676 if (len)
6678 /* Convert from the internal "sizetype" type to "size_t". */
6679 if (size_type_node)
6680 len = fold_convert (size_type_node, len);
6681 return len;
6684 return NULL_TREE;
6688 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6690 static tree
6691 fold_builtin_inf (tree type, int warn)
6693 REAL_VALUE_TYPE real;
6695 /* __builtin_inff is intended to be usable to define INFINITY on all
6696 targets. If an infinity is not available, INFINITY expands "to a
6697 positive constant of type float that overflows at translation
6698 time", footnote "In this case, using INFINITY will violate the
6699 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6700 Thus we pedwarn to ensure this constraint violation is
6701 diagnosed. */
6702 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6703 pedwarn ("target format does not support infinity");
6705 real_inf (&real);
6706 return build_real (type, real);
6709 /* Fold a call to __builtin_nan or __builtin_nans. */
6711 static tree
6712 fold_builtin_nan (tree arglist, tree type, int quiet)
6714 REAL_VALUE_TYPE real;
6715 const char *str;
6717 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6718 return 0;
6719 str = c_getstr (TREE_VALUE (arglist));
6720 if (!str)
6721 return 0;
6723 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6724 return 0;
6726 return build_real (type, real);
6729 /* Return true if the floating point expression T has an integer value.
6730 We also allow +Inf, -Inf and NaN to be considered integer values. */
6732 static bool
6733 integer_valued_real_p (tree t)
6735 switch (TREE_CODE (t))
6737 case FLOAT_EXPR:
6738 return true;
6740 case ABS_EXPR:
6741 case SAVE_EXPR:
6742 case NON_LVALUE_EXPR:
6743 return integer_valued_real_p (TREE_OPERAND (t, 0));
6745 case COMPOUND_EXPR:
6746 case GIMPLE_MODIFY_STMT:
6747 case BIND_EXPR:
6748 return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1));
6750 case PLUS_EXPR:
6751 case MINUS_EXPR:
6752 case MULT_EXPR:
6753 case MIN_EXPR:
6754 case MAX_EXPR:
6755 return integer_valued_real_p (TREE_OPERAND (t, 0))
6756 && integer_valued_real_p (TREE_OPERAND (t, 1));
6758 case COND_EXPR:
6759 return integer_valued_real_p (TREE_OPERAND (t, 1))
6760 && integer_valued_real_p (TREE_OPERAND (t, 2));
6762 case REAL_CST:
6763 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6765 case NOP_EXPR:
6767 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6768 if (TREE_CODE (type) == INTEGER_TYPE)
6769 return true;
6770 if (TREE_CODE (type) == REAL_TYPE)
6771 return integer_valued_real_p (TREE_OPERAND (t, 0));
6772 break;
6775 case CALL_EXPR:
6776 switch (builtin_mathfn_code (t))
6778 CASE_FLT_FN (BUILT_IN_CEIL):
6779 CASE_FLT_FN (BUILT_IN_FLOOR):
6780 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6781 CASE_FLT_FN (BUILT_IN_RINT):
6782 CASE_FLT_FN (BUILT_IN_ROUND):
6783 CASE_FLT_FN (BUILT_IN_TRUNC):
6784 return true;
6786 CASE_FLT_FN (BUILT_IN_FMIN):
6787 CASE_FLT_FN (BUILT_IN_FMAX):
6788 return integer_valued_real_p (TREE_VALUE (TREE_OPERAND (t, 1)))
6789 && integer_valued_real_p (TREE_VALUE (TREE_CHAIN (TREE_OPERAND (t, 1))));
6791 default:
6792 break;
6794 break;
6796 default:
6797 break;
6799 return false;
6802 /* EXP is assumed to be builtin call where truncation can be propagated
6803 across (for instance floor((double)f) == (double)floorf (f).
6804 Do the transformation. */
6806 static tree
6807 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6809 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6810 tree arg;
6812 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6813 return 0;
6815 arg = TREE_VALUE (arglist);
6816 /* Integer rounding functions are idempotent. */
6817 if (fcode == builtin_mathfn_code (arg))
6818 return arg;
6820 /* If argument is already integer valued, and we don't need to worry
6821 about setting errno, there's no need to perform rounding. */
6822 if (! flag_errno_math && integer_valued_real_p (arg))
6823 return arg;
6825 if (optimize)
6827 tree arg0 = strip_float_extensions (arg);
6828 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6829 tree newtype = TREE_TYPE (arg0);
6830 tree decl;
6832 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6833 && (decl = mathfn_built_in (newtype, fcode)))
6835 arglist =
6836 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6837 return fold_convert (ftype,
6838 build_function_call_expr (decl, arglist));
6841 return 0;
6844 /* EXP is assumed to be builtin call which can narrow the FP type of
6845 the argument, for instance lround((double)f) -> lroundf (f). */
6847 static tree
6848 fold_fixed_mathfn (tree fndecl, tree arglist)
6850 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6851 tree arg;
6853 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6854 return 0;
6856 arg = TREE_VALUE (arglist);
6858 /* If argument is already integer valued, and we don't need to worry
6859 about setting errno, there's no need to perform rounding. */
6860 if (! flag_errno_math && integer_valued_real_p (arg))
6861 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6863 if (optimize)
6865 tree ftype = TREE_TYPE (arg);
6866 tree arg0 = strip_float_extensions (arg);
6867 tree newtype = TREE_TYPE (arg0);
6868 tree decl;
6870 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6871 && (decl = mathfn_built_in (newtype, fcode)))
6873 arglist =
6874 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6875 return build_function_call_expr (decl, arglist);
6879 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6880 sizeof (long long) == sizeof (long). */
6881 if (TYPE_PRECISION (long_long_integer_type_node)
6882 == TYPE_PRECISION (long_integer_type_node))
6884 tree newfn = NULL_TREE;
6885 switch (fcode)
6887 CASE_FLT_FN (BUILT_IN_LLCEIL):
6888 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6889 break;
6891 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6892 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6893 break;
6895 CASE_FLT_FN (BUILT_IN_LLROUND):
6896 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6897 break;
6899 CASE_FLT_FN (BUILT_IN_LLRINT):
6900 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6901 break;
6903 default:
6904 break;
6907 if (newfn)
6909 tree newcall = build_function_call_expr (newfn, arglist);
6910 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6914 return 0;
6917 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6918 is the argument list, TYPE is the return type and FNDECL is the
6919 original function DECL. Return NULL_TREE if no if no simplification
6920 can be made. */
6922 static tree
6923 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6925 tree arg;
6927 if (!arglist || TREE_CHAIN (arglist))
6928 return NULL_TREE;
6930 arg = TREE_VALUE (arglist);
6931 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6932 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6933 return NULL_TREE;
6935 /* Evaluate cabs of a constant at compile-time. */
6936 if (flag_unsafe_math_optimizations
6937 && TREE_CODE (arg) == COMPLEX_CST
6938 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6939 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6940 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6941 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6943 REAL_VALUE_TYPE r, i;
6945 r = TREE_REAL_CST (TREE_REALPART (arg));
6946 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6948 real_arithmetic (&r, MULT_EXPR, &r, &r);
6949 real_arithmetic (&i, MULT_EXPR, &i, &i);
6950 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6951 if (real_sqrt (&r, TYPE_MODE (type), &r)
6952 || ! flag_trapping_math)
6953 return build_real (type, r);
6956 /* If either part is zero, cabs is fabs of the other. */
6957 if (TREE_CODE (arg) == COMPLEX_EXPR
6958 && real_zerop (TREE_OPERAND (arg, 0)))
6959 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
6960 if (TREE_CODE (arg) == COMPLEX_EXPR
6961 && real_zerop (TREE_OPERAND (arg, 1)))
6962 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
6964 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6965 if (TREE_CODE (arg) == NEGATE_EXPR
6966 || TREE_CODE (arg) == CONJ_EXPR)
6968 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
6969 return build_function_call_expr (fndecl, arglist);
6972 /* Don't do this when optimizing for size. */
6973 if (flag_unsafe_math_optimizations
6974 && optimize && !optimize_size)
6976 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6978 if (sqrtfn != NULL_TREE)
6980 tree rpart, ipart, result, arglist;
6982 arg = builtin_save_expr (arg);
6984 rpart = fold_build1 (REALPART_EXPR, type, arg);
6985 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
6987 rpart = builtin_save_expr (rpart);
6988 ipart = builtin_save_expr (ipart);
6990 result = fold_build2 (PLUS_EXPR, type,
6991 fold_build2 (MULT_EXPR, type,
6992 rpart, rpart),
6993 fold_build2 (MULT_EXPR, type,
6994 ipart, ipart));
6996 arglist = build_tree_list (NULL_TREE, result);
6997 return build_function_call_expr (sqrtfn, arglist);
7001 return NULL_TREE;
7004 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7005 NULL_TREE if no simplification can be made. */
7007 static tree
7008 fold_builtin_sqrt (tree arglist, tree type)
7011 enum built_in_function fcode;
7012 tree arg = TREE_VALUE (arglist);
7014 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7015 return NULL_TREE;
7017 /* Optimize sqrt of constant value. */
7018 if (TREE_CODE (arg) == REAL_CST
7019 && ! TREE_CONSTANT_OVERFLOW (arg))
7021 REAL_VALUE_TYPE r, x;
7023 x = TREE_REAL_CST (arg);
7024 if (real_sqrt (&r, TYPE_MODE (type), &x)
7025 || (!flag_trapping_math && !flag_errno_math))
7026 return build_real (type, r);
7029 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7030 fcode = builtin_mathfn_code (arg);
7031 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7033 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7034 arg = fold_build2 (MULT_EXPR, type,
7035 TREE_VALUE (TREE_OPERAND (arg, 1)),
7036 build_real (type, dconsthalf));
7037 arglist = build_tree_list (NULL_TREE, arg);
7038 return build_function_call_expr (expfn, arglist);
7041 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7042 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7044 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7046 if (powfn)
7048 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7049 tree tree_root;
7050 /* The inner root was either sqrt or cbrt. */
7051 REAL_VALUE_TYPE dconstroot =
7052 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7054 /* Adjust for the outer root. */
7055 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7056 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7057 tree_root = build_real (type, dconstroot);
7058 arglist = tree_cons (NULL_TREE, arg0,
7059 build_tree_list (NULL_TREE, tree_root));
7060 return build_function_call_expr (powfn, arglist);
7064 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7065 if (flag_unsafe_math_optimizations
7066 && (fcode == BUILT_IN_POW
7067 || fcode == BUILT_IN_POWF
7068 || fcode == BUILT_IN_POWL))
7070 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7071 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7072 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7073 tree narg1;
7074 if (!tree_expr_nonnegative_p (arg0))
7075 arg0 = build1 (ABS_EXPR, type, arg0);
7076 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7077 build_real (type, dconsthalf));
7078 arglist = tree_cons (NULL_TREE, arg0,
7079 build_tree_list (NULL_TREE, narg1));
7080 return build_function_call_expr (powfn, arglist);
7083 return NULL_TREE;
7086 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7087 NULL_TREE if no simplification can be made. */
7088 static tree
7089 fold_builtin_cbrt (tree arglist, tree type)
7091 tree arg = TREE_VALUE (arglist);
7092 const enum built_in_function fcode = builtin_mathfn_code (arg);
7093 tree res;
7095 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7096 return NULL_TREE;
7098 /* Calculate the result when the argument is a constant. */
7099 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7100 return res;
7102 if (flag_unsafe_math_optimizations)
7104 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7105 if (BUILTIN_EXPONENT_P (fcode))
7107 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7108 const REAL_VALUE_TYPE third_trunc =
7109 real_value_truncate (TYPE_MODE (type), dconstthird);
7110 arg = fold_build2 (MULT_EXPR, type,
7111 TREE_VALUE (TREE_OPERAND (arg, 1)),
7112 build_real (type, third_trunc));
7113 arglist = build_tree_list (NULL_TREE, arg);
7114 return build_function_call_expr (expfn, arglist);
7117 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7118 if (BUILTIN_SQRT_P (fcode))
7120 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7122 if (powfn)
7124 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7125 tree tree_root;
7126 REAL_VALUE_TYPE dconstroot = dconstthird;
7128 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7129 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7130 tree_root = build_real (type, dconstroot);
7131 arglist = tree_cons (NULL_TREE, arg0,
7132 build_tree_list (NULL_TREE, tree_root));
7133 return build_function_call_expr (powfn, arglist);
7137 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7138 if (BUILTIN_CBRT_P (fcode))
7140 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7141 if (tree_expr_nonnegative_p (arg0))
7143 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7145 if (powfn)
7147 tree tree_root;
7148 REAL_VALUE_TYPE dconstroot;
7150 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7151 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7152 tree_root = build_real (type, dconstroot);
7153 arglist = tree_cons (NULL_TREE, arg0,
7154 build_tree_list (NULL_TREE, tree_root));
7155 return build_function_call_expr (powfn, arglist);
7160 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7161 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7162 || fcode == BUILT_IN_POWL)
7164 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7165 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7166 if (tree_expr_nonnegative_p (arg00))
7168 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7169 const REAL_VALUE_TYPE dconstroot
7170 = real_value_truncate (TYPE_MODE (type), dconstthird);
7171 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7172 build_real (type, dconstroot));
7173 arglist = tree_cons (NULL_TREE, arg00,
7174 build_tree_list (NULL_TREE, narg01));
7175 return build_function_call_expr (powfn, arglist);
7179 return NULL_TREE;
7182 /* Fold function call to builtin cos, cosf, or cosl. Return
7183 NULL_TREE if no simplification can be made. */
7184 static tree
7185 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7187 tree arg = TREE_VALUE (arglist);
7188 tree res, narg;
7190 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7191 return NULL_TREE;
7193 /* Calculate the result when the argument is a constant. */
7194 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7195 return res;
7197 /* Optimize cos(-x) into cos (x). */
7198 if ((narg = fold_strip_sign_ops (arg)))
7199 return build_function_call_expr (fndecl,
7200 build_tree_list (NULL_TREE, narg));
7202 return NULL_TREE;
7205 /* Fold function call to builtin cosh, coshf, or coshl. Return
7206 NULL_TREE if no simplification can be made. */
7207 static tree
7208 fold_builtin_cosh (tree arglist, tree type, tree fndecl)
7210 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7212 tree arg = TREE_VALUE (arglist);
7213 tree res, narg;
7215 /* Calculate the result when the argument is a constant. */
7216 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7217 return res;
7219 /* Optimize cosh(-x) into cosh (x). */
7220 if ((narg = fold_strip_sign_ops (arg)))
7221 return build_function_call_expr (fndecl,
7222 build_tree_list (NULL_TREE, narg));
7225 return NULL_TREE;
7228 /* Fold function call to builtin tan, tanf, or tanl. Return
7229 NULL_TREE if no simplification can be made. */
7230 static tree
7231 fold_builtin_tan (tree arglist, tree type)
7233 enum built_in_function fcode;
7234 tree arg = TREE_VALUE (arglist);
7235 tree res;
7237 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7238 return NULL_TREE;
7240 /* Calculate the result when the argument is a constant. */
7241 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7242 return res;
7244 /* Optimize tan(atan(x)) = x. */
7245 fcode = builtin_mathfn_code (arg);
7246 if (flag_unsafe_math_optimizations
7247 && (fcode == BUILT_IN_ATAN
7248 || fcode == BUILT_IN_ATANF
7249 || fcode == BUILT_IN_ATANL))
7250 return TREE_VALUE (TREE_OPERAND (arg, 1));
7252 return NULL_TREE;
7255 /* Fold function call to builtin trunc, truncf or truncl. Return
7256 NULL_TREE if no simplification can be made. */
7258 static tree
7259 fold_builtin_trunc (tree fndecl, tree arglist)
7261 tree arg;
7263 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7264 return 0;
7266 /* Optimize trunc of constant value. */
7267 arg = TREE_VALUE (arglist);
7268 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7270 REAL_VALUE_TYPE r, x;
7271 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7273 x = TREE_REAL_CST (arg);
7274 real_trunc (&r, TYPE_MODE (type), &x);
7275 return build_real (type, r);
7278 return fold_trunc_transparent_mathfn (fndecl, arglist);
7281 /* Fold function call to builtin floor, floorf or floorl. Return
7282 NULL_TREE if no simplification can be made. */
7284 static tree
7285 fold_builtin_floor (tree fndecl, tree arglist)
7287 tree arg;
7289 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7290 return 0;
7292 /* Optimize floor of constant value. */
7293 arg = TREE_VALUE (arglist);
7294 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7296 REAL_VALUE_TYPE x;
7298 x = TREE_REAL_CST (arg);
7299 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7301 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7302 REAL_VALUE_TYPE r;
7304 real_floor (&r, TYPE_MODE (type), &x);
7305 return build_real (type, r);
7309 /* Fold floor (x) where x is nonnegative to trunc (x). */
7310 if (tree_expr_nonnegative_p (arg))
7312 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7313 if (truncfn)
7314 return build_function_call_expr (truncfn, arglist);
7317 return fold_trunc_transparent_mathfn (fndecl, arglist);
7320 /* Fold function call to builtin ceil, ceilf or ceill. Return
7321 NULL_TREE if no simplification can be made. */
7323 static tree
7324 fold_builtin_ceil (tree fndecl, tree arglist)
7326 tree arg;
7328 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7329 return 0;
7331 /* Optimize ceil of constant value. */
7332 arg = TREE_VALUE (arglist);
7333 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7335 REAL_VALUE_TYPE x;
7337 x = TREE_REAL_CST (arg);
7338 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7340 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7341 REAL_VALUE_TYPE r;
7343 real_ceil (&r, TYPE_MODE (type), &x);
7344 return build_real (type, r);
7348 return fold_trunc_transparent_mathfn (fndecl, arglist);
7351 /* Fold function call to builtin round, roundf or roundl. Return
7352 NULL_TREE if no simplification can be made. */
7354 static tree
7355 fold_builtin_round (tree fndecl, tree arglist)
7357 tree arg;
7359 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7360 return 0;
7362 /* Optimize round of constant value. */
7363 arg = TREE_VALUE (arglist);
7364 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7366 REAL_VALUE_TYPE x;
7368 x = TREE_REAL_CST (arg);
7369 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7371 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7372 REAL_VALUE_TYPE r;
7374 real_round (&r, TYPE_MODE (type), &x);
7375 return build_real (type, r);
7379 return fold_trunc_transparent_mathfn (fndecl, arglist);
7382 /* Fold function call to builtin lround, lroundf or lroundl (or the
7383 corresponding long long versions) and other rounding functions.
7384 Return NULL_TREE if no simplification can be made. */
7386 static tree
7387 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7389 tree arg;
7391 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7392 return 0;
7394 /* Optimize lround of constant value. */
7395 arg = TREE_VALUE (arglist);
7396 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7398 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7400 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7402 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7403 tree ftype = TREE_TYPE (arg), result;
7404 HOST_WIDE_INT hi, lo;
7405 REAL_VALUE_TYPE r;
7407 switch (DECL_FUNCTION_CODE (fndecl))
7409 CASE_FLT_FN (BUILT_IN_LFLOOR):
7410 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7411 real_floor (&r, TYPE_MODE (ftype), &x);
7412 break;
7414 CASE_FLT_FN (BUILT_IN_LCEIL):
7415 CASE_FLT_FN (BUILT_IN_LLCEIL):
7416 real_ceil (&r, TYPE_MODE (ftype), &x);
7417 break;
7419 CASE_FLT_FN (BUILT_IN_LROUND):
7420 CASE_FLT_FN (BUILT_IN_LLROUND):
7421 real_round (&r, TYPE_MODE (ftype), &x);
7422 break;
7424 default:
7425 gcc_unreachable ();
7428 REAL_VALUE_TO_INT (&lo, &hi, r);
7429 result = build_int_cst_wide (NULL_TREE, lo, hi);
7430 if (int_fits_type_p (result, itype))
7431 return fold_convert (itype, result);
7435 switch (DECL_FUNCTION_CODE (fndecl))
7437 CASE_FLT_FN (BUILT_IN_LFLOOR):
7438 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7439 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7440 if (tree_expr_nonnegative_p (arg))
7441 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7442 arg);
7443 break;
7444 default:;
7447 return fold_fixed_mathfn (fndecl, arglist);
7450 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7451 and their long and long long variants (i.e. ffsl and ffsll).
7452 Return NULL_TREE if no simplification can be made. */
7454 static tree
7455 fold_builtin_bitop (tree fndecl, tree arglist)
7457 tree arg;
7459 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7460 return NULL_TREE;
7462 /* Optimize for constant argument. */
7463 arg = TREE_VALUE (arglist);
7464 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7466 HOST_WIDE_INT hi, width, result;
7467 unsigned HOST_WIDE_INT lo;
7468 tree type;
7470 type = TREE_TYPE (arg);
7471 width = TYPE_PRECISION (type);
7472 lo = TREE_INT_CST_LOW (arg);
7474 /* Clear all the bits that are beyond the type's precision. */
7475 if (width > HOST_BITS_PER_WIDE_INT)
7477 hi = TREE_INT_CST_HIGH (arg);
7478 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7479 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7481 else
7483 hi = 0;
7484 if (width < HOST_BITS_PER_WIDE_INT)
7485 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7488 switch (DECL_FUNCTION_CODE (fndecl))
7490 CASE_INT_FN (BUILT_IN_FFS):
7491 if (lo != 0)
7492 result = exact_log2 (lo & -lo) + 1;
7493 else if (hi != 0)
7494 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7495 else
7496 result = 0;
7497 break;
7499 CASE_INT_FN (BUILT_IN_CLZ):
7500 if (hi != 0)
7501 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7502 else if (lo != 0)
7503 result = width - floor_log2 (lo) - 1;
7504 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7505 result = width;
7506 break;
7508 CASE_INT_FN (BUILT_IN_CTZ):
7509 if (lo != 0)
7510 result = exact_log2 (lo & -lo);
7511 else if (hi != 0)
7512 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7513 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7514 result = width;
7515 break;
7517 CASE_INT_FN (BUILT_IN_POPCOUNT):
7518 result = 0;
7519 while (lo)
7520 result++, lo &= lo - 1;
7521 while (hi)
7522 result++, hi &= hi - 1;
7523 break;
7525 CASE_INT_FN (BUILT_IN_PARITY):
7526 result = 0;
7527 while (lo)
7528 result++, lo &= lo - 1;
7529 while (hi)
7530 result++, hi &= hi - 1;
7531 result &= 1;
7532 break;
7534 default:
7535 gcc_unreachable ();
7538 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7541 return NULL_TREE;
7544 /* Fold function call to builtin_bswap and the long and long long
7545 variants. Return NULL_TREE if no simplification can be made. */
7546 static tree
7547 fold_builtin_bswap (tree fndecl, tree arglist)
7549 tree arg;
7551 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7552 return 0;
7554 /* Optimize constant value. */
7555 arg = TREE_VALUE (arglist);
7556 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7558 HOST_WIDE_INT hi, width, r_hi = 0;
7559 unsigned HOST_WIDE_INT lo, r_lo = 0;
7560 tree type;
7562 type = TREE_TYPE (arg);
7563 width = TYPE_PRECISION (type);
7564 lo = TREE_INT_CST_LOW (arg);
7565 hi = TREE_INT_CST_HIGH (arg);
7567 switch (DECL_FUNCTION_CODE (fndecl))
7569 case BUILT_IN_BSWAP32:
7570 case BUILT_IN_BSWAP64:
7572 int s;
7574 for (s = 0; s < width; s += 8)
7576 int d = width - s - 8;
7577 unsigned HOST_WIDE_INT byte;
7579 if (s < HOST_BITS_PER_WIDE_INT)
7580 byte = (lo >> s) & 0xff;
7581 else
7582 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7584 if (d < HOST_BITS_PER_WIDE_INT)
7585 r_lo |= byte << d;
7586 else
7587 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7591 break;
7593 default:
7594 gcc_unreachable ();
7597 if (width < HOST_BITS_PER_WIDE_INT)
7598 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7599 else
7600 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7603 return NULL_TREE;
7605 /* Return true if EXPR is the real constant contained in VALUE. */
7607 static bool
7608 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7610 STRIP_NOPS (expr);
7612 return ((TREE_CODE (expr) == REAL_CST
7613 && ! TREE_CONSTANT_OVERFLOW (expr)
7614 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7615 || (TREE_CODE (expr) == COMPLEX_CST
7616 && real_dconstp (TREE_REALPART (expr), value)
7617 && real_zerop (TREE_IMAGPART (expr))));
7620 /* A subroutine of fold_builtin to fold the various logarithmic
7621 functions. Return NULL_TREE if no simplification can me made.
7622 FUNC is the corresponding MPFR logarithm function. */
7624 static tree
7625 fold_builtin_logarithm (tree fndecl, tree arglist,
7626 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7628 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7630 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7631 tree arg = TREE_VALUE (arglist);
7632 tree res;
7633 const enum built_in_function fcode = builtin_mathfn_code (arg);
7635 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7636 instead we'll look for 'e' truncated to MODE. So only do
7637 this if flag_unsafe_math_optimizations is set. */
7638 if (flag_unsafe_math_optimizations && func == mpfr_log)
7640 const REAL_VALUE_TYPE e_truncated =
7641 real_value_truncate (TYPE_MODE (type), dconste);
7642 if (real_dconstp (arg, &e_truncated))
7643 return build_real (type, dconst1);
7646 /* Calculate the result when the argument is a constant. */
7647 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7648 return res;
7650 /* Special case, optimize logN(expN(x)) = x. */
7651 if (flag_unsafe_math_optimizations
7652 && ((func == mpfr_log
7653 && (fcode == BUILT_IN_EXP
7654 || fcode == BUILT_IN_EXPF
7655 || fcode == BUILT_IN_EXPL))
7656 || (func == mpfr_log2
7657 && (fcode == BUILT_IN_EXP2
7658 || fcode == BUILT_IN_EXP2F
7659 || fcode == BUILT_IN_EXP2L))
7660 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7661 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7663 /* Optimize logN(func()) for various exponential functions. We
7664 want to determine the value "x" and the power "exponent" in
7665 order to transform logN(x**exponent) into exponent*logN(x). */
7666 if (flag_unsafe_math_optimizations)
7668 tree exponent = 0, x = 0;
7670 switch (fcode)
7672 CASE_FLT_FN (BUILT_IN_EXP):
7673 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7674 x = build_real (type,
7675 real_value_truncate (TYPE_MODE (type), dconste));
7676 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7677 break;
7678 CASE_FLT_FN (BUILT_IN_EXP2):
7679 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7680 x = build_real (type, dconst2);
7681 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7682 break;
7683 CASE_FLT_FN (BUILT_IN_EXP10):
7684 CASE_FLT_FN (BUILT_IN_POW10):
7685 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7686 x = build_real (type, dconst10);
7687 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7688 break;
7689 CASE_FLT_FN (BUILT_IN_SQRT):
7690 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7691 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7692 exponent = build_real (type, dconsthalf);
7693 break;
7694 CASE_FLT_FN (BUILT_IN_CBRT):
7695 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7696 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7697 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7698 dconstthird));
7699 break;
7700 CASE_FLT_FN (BUILT_IN_POW):
7701 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7702 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7703 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7704 break;
7705 default:
7706 break;
7709 /* Now perform the optimization. */
7710 if (x && exponent)
7712 tree logfn;
7713 arglist = build_tree_list (NULL_TREE, x);
7714 logfn = build_function_call_expr (fndecl, arglist);
7715 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7720 return 0;
7723 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7724 NULL_TREE if no simplification can be made. */
7726 static tree
7727 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7729 tree arg0 = TREE_VALUE (arglist);
7730 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7731 tree res, narg0, narg1;
7733 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7734 return NULL_TREE;
7736 /* Calculate the result when the argument is a constant. */
7737 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7738 return res;
7740 /* If either argument to hypot has a negate or abs, strip that off.
7741 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7742 narg0 = fold_strip_sign_ops (arg0);
7743 narg1 = fold_strip_sign_ops (arg1);
7744 if (narg0 || narg1)
7746 tree narglist = tree_cons (NULL_TREE, narg0 ? narg0 : arg0,
7747 build_tree_list (NULL_TREE,
7748 narg1 ? narg1 : arg1));
7749 return build_function_call_expr (fndecl, narglist);
7752 /* If either argument is zero, hypot is fabs of the other. */
7753 if (real_zerop (arg0))
7754 return fold_build1 (ABS_EXPR, type, arg1);
7755 else if (real_zerop (arg1))
7756 return fold_build1 (ABS_EXPR, type, arg0);
7758 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7759 if (flag_unsafe_math_optimizations
7760 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7762 REAL_VALUE_TYPE sqrt2;
7764 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7765 return fold_build2 (MULT_EXPR, type,
7766 fold_build1 (ABS_EXPR, type, arg0),
7767 build_real (type, sqrt2));
7770 return NULL_TREE;
7774 /* Fold a builtin function call to pow, powf, or powl. Return
7775 NULL_TREE if no simplification can be made. */
7776 static tree
7777 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7779 tree arg0 = TREE_VALUE (arglist);
7780 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7781 tree res;
7783 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7784 return NULL_TREE;
7786 /* Calculate the result when the argument is a constant. */
7787 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7788 return res;
7790 /* Optimize pow(1.0,y) = 1.0. */
7791 if (real_onep (arg0))
7792 return omit_one_operand (type, build_real (type, dconst1), arg1);
7794 if (TREE_CODE (arg1) == REAL_CST
7795 && ! TREE_CONSTANT_OVERFLOW (arg1))
7797 REAL_VALUE_TYPE cint;
7798 REAL_VALUE_TYPE c;
7799 HOST_WIDE_INT n;
7801 c = TREE_REAL_CST (arg1);
7803 /* Optimize pow(x,0.0) = 1.0. */
7804 if (REAL_VALUES_EQUAL (c, dconst0))
7805 return omit_one_operand (type, build_real (type, dconst1),
7806 arg0);
7808 /* Optimize pow(x,1.0) = x. */
7809 if (REAL_VALUES_EQUAL (c, dconst1))
7810 return arg0;
7812 /* Optimize pow(x,-1.0) = 1.0/x. */
7813 if (REAL_VALUES_EQUAL (c, dconstm1))
7814 return fold_build2 (RDIV_EXPR, type,
7815 build_real (type, dconst1), arg0);
7817 /* Optimize pow(x,0.5) = sqrt(x). */
7818 if (flag_unsafe_math_optimizations
7819 && REAL_VALUES_EQUAL (c, dconsthalf))
7821 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7823 if (sqrtfn != NULL_TREE)
7825 tree arglist = build_tree_list (NULL_TREE, arg0);
7826 return build_function_call_expr (sqrtfn, arglist);
7830 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7831 if (flag_unsafe_math_optimizations)
7833 const REAL_VALUE_TYPE dconstroot
7834 = real_value_truncate (TYPE_MODE (type), dconstthird);
7836 if (REAL_VALUES_EQUAL (c, dconstroot))
7838 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7839 if (cbrtfn != NULL_TREE)
7841 tree arglist = build_tree_list (NULL_TREE, arg0);
7842 return build_function_call_expr (cbrtfn, arglist);
7847 /* Check for an integer exponent. */
7848 n = real_to_integer (&c);
7849 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7850 if (real_identical (&c, &cint))
7852 /* Attempt to evaluate pow at compile-time. */
7853 if (TREE_CODE (arg0) == REAL_CST
7854 && ! TREE_CONSTANT_OVERFLOW (arg0))
7856 REAL_VALUE_TYPE x;
7857 bool inexact;
7859 x = TREE_REAL_CST (arg0);
7860 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7861 if (flag_unsafe_math_optimizations || !inexact)
7862 return build_real (type, x);
7865 /* Strip sign ops from even integer powers. */
7866 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7868 tree narg0 = fold_strip_sign_ops (arg0);
7869 if (narg0)
7871 arglist = build_tree_list (NULL_TREE, arg1);
7872 arglist = tree_cons (NULL_TREE, narg0, arglist);
7873 return build_function_call_expr (fndecl, arglist);
7879 if (flag_unsafe_math_optimizations)
7881 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7883 /* Optimize pow(expN(x),y) = expN(x*y). */
7884 if (BUILTIN_EXPONENT_P (fcode))
7886 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7887 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7888 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7889 arglist = build_tree_list (NULL_TREE, arg);
7890 return build_function_call_expr (expfn, arglist);
7893 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7894 if (BUILTIN_SQRT_P (fcode))
7896 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7897 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7898 build_real (type, dconsthalf));
7900 arglist = tree_cons (NULL_TREE, narg0,
7901 build_tree_list (NULL_TREE, narg1));
7902 return build_function_call_expr (fndecl, arglist);
7905 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7906 if (BUILTIN_CBRT_P (fcode))
7908 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7909 if (tree_expr_nonnegative_p (arg))
7911 const REAL_VALUE_TYPE dconstroot
7912 = real_value_truncate (TYPE_MODE (type), dconstthird);
7913 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7914 build_real (type, dconstroot));
7915 arglist = tree_cons (NULL_TREE, arg,
7916 build_tree_list (NULL_TREE, narg1));
7917 return build_function_call_expr (fndecl, arglist);
7921 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7922 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7923 || fcode == BUILT_IN_POWL)
7925 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7926 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7927 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7928 arglist = tree_cons (NULL_TREE, arg00,
7929 build_tree_list (NULL_TREE, narg1));
7930 return build_function_call_expr (fndecl, arglist);
7934 return NULL_TREE;
7937 /* Fold a builtin function call to powi, powif, or powil. Return
7938 NULL_TREE if no simplification can be made. */
7939 static tree
7940 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7942 tree arg0 = TREE_VALUE (arglist);
7943 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7945 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7946 return NULL_TREE;
7948 /* Optimize pow(1.0,y) = 1.0. */
7949 if (real_onep (arg0))
7950 return omit_one_operand (type, build_real (type, dconst1), arg1);
7952 if (host_integerp (arg1, 0))
7954 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7956 /* Evaluate powi at compile-time. */
7957 if (TREE_CODE (arg0) == REAL_CST
7958 && ! TREE_CONSTANT_OVERFLOW (arg0))
7960 REAL_VALUE_TYPE x;
7961 x = TREE_REAL_CST (arg0);
7962 real_powi (&x, TYPE_MODE (type), &x, c);
7963 return build_real (type, x);
7966 /* Optimize pow(x,0) = 1.0. */
7967 if (c == 0)
7968 return omit_one_operand (type, build_real (type, dconst1),
7969 arg0);
7971 /* Optimize pow(x,1) = x. */
7972 if (c == 1)
7973 return arg0;
7975 /* Optimize pow(x,-1) = 1.0/x. */
7976 if (c == -1)
7977 return fold_build2 (RDIV_EXPR, type,
7978 build_real (type, dconst1), arg0);
7981 return NULL_TREE;
7984 /* A subroutine of fold_builtin to fold the various exponent
7985 functions. Return NULL_TREE if no simplification can me made.
7986 FUNC is the corresponding MPFR exponent function. */
7988 static tree
7989 fold_builtin_exponent (tree fndecl, tree arglist,
7990 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7992 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7994 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7995 tree arg = TREE_VALUE (arglist);
7996 tree res;
7998 /* Calculate the result when the argument is a constant. */
7999 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8000 return res;
8002 /* Optimize expN(logN(x)) = x. */
8003 if (flag_unsafe_math_optimizations)
8005 const enum built_in_function fcode = builtin_mathfn_code (arg);
8007 if ((func == mpfr_exp
8008 && (fcode == BUILT_IN_LOG
8009 || fcode == BUILT_IN_LOGF
8010 || fcode == BUILT_IN_LOGL))
8011 || (func == mpfr_exp2
8012 && (fcode == BUILT_IN_LOG2
8013 || fcode == BUILT_IN_LOG2F
8014 || fcode == BUILT_IN_LOG2L))
8015 || (func == mpfr_exp10
8016 && (fcode == BUILT_IN_LOG10
8017 || fcode == BUILT_IN_LOG10F
8018 || fcode == BUILT_IN_LOG10L)))
8019 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8023 return 0;
8026 /* Return true if VAR is a VAR_DECL or a component thereof. */
8028 static bool
8029 var_decl_component_p (tree var)
8031 tree inner = var;
8032 while (handled_component_p (inner))
8033 inner = TREE_OPERAND (inner, 0);
8034 return SSA_VAR_P (inner);
8037 /* Fold function call to builtin memset. Return
8038 NULL_TREE if no simplification can be made. */
8040 static tree
8041 fold_builtin_memset (tree arglist, tree type, bool ignore)
8043 tree dest, c, len, var, ret;
8044 unsigned HOST_WIDE_INT length, cval;
8046 if (!validate_arglist (arglist,
8047 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8048 return 0;
8050 dest = TREE_VALUE (arglist);
8051 c = TREE_VALUE (TREE_CHAIN (arglist));
8052 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8054 if (! host_integerp (len, 1))
8055 return 0;
8057 /* If the LEN parameter is zero, return DEST. */
8058 if (integer_zerop (len))
8059 return omit_one_operand (type, dest, c);
8061 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8062 return 0;
8064 var = dest;
8065 STRIP_NOPS (var);
8066 if (TREE_CODE (var) != ADDR_EXPR)
8067 return 0;
8069 var = TREE_OPERAND (var, 0);
8070 if (TREE_THIS_VOLATILE (var))
8071 return 0;
8073 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8074 && !POINTER_TYPE_P (TREE_TYPE (var)))
8075 return 0;
8077 if (! var_decl_component_p (var))
8078 return 0;
8080 length = tree_low_cst (len, 1);
8081 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8082 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8083 < (int) length)
8084 return 0;
8086 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8087 return 0;
8089 if (integer_zerop (c))
8090 cval = 0;
8091 else
8093 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8094 return 0;
8096 cval = tree_low_cst (c, 1);
8097 cval &= 0xff;
8098 cval |= cval << 8;
8099 cval |= cval << 16;
8100 cval |= (cval << 31) << 1;
8103 ret = build_int_cst_type (TREE_TYPE (var), cval);
8104 ret = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, ret);
8105 if (ignore)
8106 return ret;
8108 return omit_one_operand (type, dest, ret);
8111 /* Fold function call to builtin memset. Return
8112 NULL_TREE if no simplification can be made. */
8114 static tree
8115 fold_builtin_bzero (tree arglist, bool ignore)
8117 tree dest, size, newarglist;
8119 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8120 return 0;
8122 if (!ignore)
8123 return 0;
8125 dest = TREE_VALUE (arglist);
8126 size = TREE_VALUE (TREE_CHAIN (arglist));
8128 /* New argument list transforming bzero(ptr x, int y) to
8129 memset(ptr x, int 0, size_t y). This is done this way
8130 so that if it isn't expanded inline, we fallback to
8131 calling bzero instead of memset. */
8133 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8134 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8135 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8136 return fold_builtin_memset (newarglist, void_type_node, ignore);
8139 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8140 NULL_TREE if no simplification can be made.
8141 If ENDP is 0, return DEST (like memcpy).
8142 If ENDP is 1, return DEST+LEN (like mempcpy).
8143 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8144 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8145 (memmove). */
8147 static tree
8148 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8150 tree dest, src, len, destvar, srcvar, expr;
8152 if (! validate_arglist (arglist,
8153 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8154 return 0;
8156 dest = TREE_VALUE (arglist);
8157 src = TREE_VALUE (TREE_CHAIN (arglist));
8158 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8160 /* If the LEN parameter is zero, return DEST. */
8161 if (integer_zerop (len))
8162 return omit_one_operand (type, dest, src);
8164 /* If SRC and DEST are the same (and not volatile), return
8165 DEST{,+LEN,+LEN-1}. */
8166 if (operand_equal_p (src, dest, 0))
8167 expr = len;
8168 else
8170 tree srctype, desttype;
8171 if (endp == 3)
8173 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8174 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8176 /* Both DEST and SRC must be pointer types.
8177 ??? This is what old code did. Is the testing for pointer types
8178 really mandatory?
8180 If either SRC is readonly or length is 1, we can use memcpy. */
8181 if (dest_align && src_align
8182 && (readonly_data_expr (src)
8183 || (host_integerp (len, 1)
8184 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8185 tree_low_cst (len, 1)))))
8187 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8188 if (!fn)
8189 return 0;
8190 return build_function_call_expr (fn, arglist);
8192 return 0;
8195 if (!host_integerp (len, 0))
8196 return 0;
8197 /* FIXME:
8198 This logic lose for arguments like (type *)malloc (sizeof (type)),
8199 since we strip the casts of up to VOID return value from malloc.
8200 Perhaps we ought to inherit type from non-VOID argument here? */
8201 STRIP_NOPS (src);
8202 STRIP_NOPS (dest);
8203 srctype = TREE_TYPE (TREE_TYPE (src));
8204 desttype = TREE_TYPE (TREE_TYPE (dest));
8205 if (!srctype || !desttype
8206 || !TYPE_SIZE_UNIT (srctype)
8207 || !TYPE_SIZE_UNIT (desttype)
8208 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8209 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8210 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
8211 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8212 return 0;
8214 if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT)
8215 < (int) TYPE_ALIGN (desttype)
8216 || (get_pointer_alignment (src, BIGGEST_ALIGNMENT)
8217 < (int) TYPE_ALIGN (srctype)))
8218 return 0;
8220 if (!ignore)
8221 dest = builtin_save_expr (dest);
8223 srcvar = build_fold_indirect_ref (src);
8224 if (TREE_THIS_VOLATILE (srcvar))
8225 return 0;
8226 if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8227 return 0;
8228 /* With memcpy, it is possible to bypass aliasing rules, so without
8229 this check i. e. execute/20060930-2.c would be misoptimized, because
8230 it use conflicting alias set to hold argument for the memcpy call.
8231 This check is probably unnecesary with -fno-strict-aliasing.
8232 Similarly for destvar. See also PR29286. */
8233 if (!var_decl_component_p (srcvar)
8234 /* Accept: memcpy (*char_var, "test", 1); that simplify
8235 to char_var='t'; */
8236 || is_gimple_min_invariant (srcvar)
8237 || readonly_data_expr (src))
8238 return 0;
8240 destvar = build_fold_indirect_ref (dest);
8241 if (TREE_THIS_VOLATILE (destvar))
8242 return 0;
8243 if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8244 return 0;
8245 if (!var_decl_component_p (destvar))
8246 return 0;
8248 if (srctype == desttype
8249 || (gimple_in_ssa_p (cfun)
8250 && tree_ssa_useless_type_conversion_1 (desttype, srctype)))
8251 expr = srcvar;
8252 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8253 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8254 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8255 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8256 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8257 else
8258 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8259 expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (destvar), destvar, expr);
8262 if (ignore)
8263 return expr;
8265 if (endp == 0 || endp == 3)
8266 return omit_one_operand (type, dest, expr);
8268 if (expr == len)
8269 expr = 0;
8271 if (endp == 2)
8272 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8273 ssize_int (1));
8275 len = fold_convert (TREE_TYPE (dest), len);
8276 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8277 dest = fold_convert (type, dest);
8278 if (expr)
8279 dest = omit_one_operand (type, dest, expr);
8280 return dest;
8283 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8284 simplification can be made. */
8286 static tree
8287 fold_builtin_bcopy (tree arglist, bool ignore)
8289 tree src, dest, size, newarglist;
8291 if (!validate_arglist (arglist,
8292 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8293 return 0;
8295 if (! ignore)
8296 return 0;
8298 src = TREE_VALUE (arglist);
8299 dest = TREE_VALUE (TREE_CHAIN (arglist));
8300 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8302 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8303 memmove(ptr y, ptr x, size_t z). This is done this way
8304 so that if it isn't expanded inline, we fallback to
8305 calling bcopy instead of memmove. */
8307 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8308 newarglist = tree_cons (NULL_TREE, src, newarglist);
8309 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8311 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8314 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8315 the length of the string to be copied. Return NULL_TREE if no
8316 simplification can be made. */
8318 tree
8319 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8321 tree dest, src, fn;
8323 if (!validate_arglist (arglist,
8324 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8325 return 0;
8327 dest = TREE_VALUE (arglist);
8328 src = TREE_VALUE (TREE_CHAIN (arglist));
8330 /* If SRC and DEST are the same (and not volatile), return DEST. */
8331 if (operand_equal_p (src, dest, 0))
8332 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8334 if (optimize_size)
8335 return 0;
8337 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8338 if (!fn)
8339 return 0;
8341 if (!len)
8343 len = c_strlen (src, 1);
8344 if (! len || TREE_SIDE_EFFECTS (len))
8345 return 0;
8348 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8349 arglist = build_tree_list (NULL_TREE, len);
8350 arglist = tree_cons (NULL_TREE, src, arglist);
8351 arglist = tree_cons (NULL_TREE, dest, arglist);
8352 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8353 build_function_call_expr (fn, arglist));
8356 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8357 the length of the source string. Return NULL_TREE if no simplification
8358 can be made. */
8360 tree
8361 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8363 tree dest, src, len, fn;
8365 if (!validate_arglist (arglist,
8366 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8367 return 0;
8369 dest = TREE_VALUE (arglist);
8370 src = TREE_VALUE (TREE_CHAIN (arglist));
8371 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8373 /* If the LEN parameter is zero, return DEST. */
8374 if (integer_zerop (len))
8375 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8377 /* We can't compare slen with len as constants below if len is not a
8378 constant. */
8379 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8380 return 0;
8382 if (!slen)
8383 slen = c_strlen (src, 1);
8385 /* Now, we must be passed a constant src ptr parameter. */
8386 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8387 return 0;
8389 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8391 /* We do not support simplification of this case, though we do
8392 support it when expanding trees into RTL. */
8393 /* FIXME: generate a call to __builtin_memset. */
8394 if (tree_int_cst_lt (slen, len))
8395 return 0;
8397 /* OK transform into builtin memcpy. */
8398 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8399 if (!fn)
8400 return 0;
8401 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8402 build_function_call_expr (fn, arglist));
8405 /* Fold function call to builtin memcmp. Return
8406 NULL_TREE if no simplification can be made. */
8408 static tree
8409 fold_builtin_memcmp (tree arglist)
8411 tree arg1, arg2, len;
8412 const char *p1, *p2;
8414 if (!validate_arglist (arglist,
8415 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8416 return 0;
8418 arg1 = TREE_VALUE (arglist);
8419 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8420 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8422 /* If the LEN parameter is zero, return zero. */
8423 if (integer_zerop (len))
8424 return omit_two_operands (integer_type_node, integer_zero_node,
8425 arg1, arg2);
8427 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8428 if (operand_equal_p (arg1, arg2, 0))
8429 return omit_one_operand (integer_type_node, integer_zero_node, len);
8431 p1 = c_getstr (arg1);
8432 p2 = c_getstr (arg2);
8434 /* If all arguments are constant, and the value of len is not greater
8435 than the lengths of arg1 and arg2, evaluate at compile-time. */
8436 if (host_integerp (len, 1) && p1 && p2
8437 && compare_tree_int (len, strlen (p1) + 1) <= 0
8438 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8440 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8442 if (r > 0)
8443 return integer_one_node;
8444 else if (r < 0)
8445 return integer_minus_one_node;
8446 else
8447 return integer_zero_node;
8450 /* If len parameter is one, return an expression corresponding to
8451 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8452 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8454 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8455 tree cst_uchar_ptr_node
8456 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8458 tree ind1 = fold_convert (integer_type_node,
8459 build1 (INDIRECT_REF, cst_uchar_node,
8460 fold_convert (cst_uchar_ptr_node,
8461 arg1)));
8462 tree ind2 = fold_convert (integer_type_node,
8463 build1 (INDIRECT_REF, cst_uchar_node,
8464 fold_convert (cst_uchar_ptr_node,
8465 arg2)));
8466 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8469 return 0;
8472 /* Fold function call to builtin strcmp. Return
8473 NULL_TREE if no simplification can be made. */
8475 static tree
8476 fold_builtin_strcmp (tree arglist)
8478 tree arg1, arg2;
8479 const char *p1, *p2;
8481 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8482 return 0;
8484 arg1 = TREE_VALUE (arglist);
8485 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8487 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8488 if (operand_equal_p (arg1, arg2, 0))
8489 return integer_zero_node;
8491 p1 = c_getstr (arg1);
8492 p2 = c_getstr (arg2);
8494 if (p1 && p2)
8496 const int i = strcmp (p1, p2);
8497 if (i < 0)
8498 return integer_minus_one_node;
8499 else if (i > 0)
8500 return integer_one_node;
8501 else
8502 return integer_zero_node;
8505 /* If the second arg is "", return *(const unsigned char*)arg1. */
8506 if (p2 && *p2 == '\0')
8508 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8509 tree cst_uchar_ptr_node
8510 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8512 return fold_convert (integer_type_node,
8513 build1 (INDIRECT_REF, cst_uchar_node,
8514 fold_convert (cst_uchar_ptr_node,
8515 arg1)));
8518 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8519 if (p1 && *p1 == '\0')
8521 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8522 tree cst_uchar_ptr_node
8523 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8525 tree temp = fold_convert (integer_type_node,
8526 build1 (INDIRECT_REF, cst_uchar_node,
8527 fold_convert (cst_uchar_ptr_node,
8528 arg2)));
8529 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8532 return 0;
8535 /* Fold function call to builtin strncmp. Return
8536 NULL_TREE if no simplification can be made. */
8538 static tree
8539 fold_builtin_strncmp (tree arglist)
8541 tree arg1, arg2, len;
8542 const char *p1, *p2;
8544 if (!validate_arglist (arglist,
8545 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8546 return 0;
8548 arg1 = TREE_VALUE (arglist);
8549 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8550 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8552 /* If the LEN parameter is zero, return zero. */
8553 if (integer_zerop (len))
8554 return omit_two_operands (integer_type_node, integer_zero_node,
8555 arg1, arg2);
8557 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8558 if (operand_equal_p (arg1, arg2, 0))
8559 return omit_one_operand (integer_type_node, integer_zero_node, len);
8561 p1 = c_getstr (arg1);
8562 p2 = c_getstr (arg2);
8564 if (host_integerp (len, 1) && p1 && p2)
8566 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8567 if (i > 0)
8568 return integer_one_node;
8569 else if (i < 0)
8570 return integer_minus_one_node;
8571 else
8572 return integer_zero_node;
8575 /* If the second arg is "", and the length is greater than zero,
8576 return *(const unsigned char*)arg1. */
8577 if (p2 && *p2 == '\0'
8578 && TREE_CODE (len) == INTEGER_CST
8579 && tree_int_cst_sgn (len) == 1)
8581 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8582 tree cst_uchar_ptr_node
8583 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8585 return fold_convert (integer_type_node,
8586 build1 (INDIRECT_REF, cst_uchar_node,
8587 fold_convert (cst_uchar_ptr_node,
8588 arg1)));
8591 /* If the first arg is "", and the length is greater than zero,
8592 return -*(const unsigned char*)arg2. */
8593 if (p1 && *p1 == '\0'
8594 && TREE_CODE (len) == INTEGER_CST
8595 && tree_int_cst_sgn (len) == 1)
8597 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8598 tree cst_uchar_ptr_node
8599 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8601 tree temp = fold_convert (integer_type_node,
8602 build1 (INDIRECT_REF, cst_uchar_node,
8603 fold_convert (cst_uchar_ptr_node,
8604 arg2)));
8605 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8608 /* If len parameter is one, return an expression corresponding to
8609 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8610 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8612 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8613 tree cst_uchar_ptr_node
8614 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8616 tree ind1 = fold_convert (integer_type_node,
8617 build1 (INDIRECT_REF, cst_uchar_node,
8618 fold_convert (cst_uchar_ptr_node,
8619 arg1)));
8620 tree ind2 = fold_convert (integer_type_node,
8621 build1 (INDIRECT_REF, cst_uchar_node,
8622 fold_convert (cst_uchar_ptr_node,
8623 arg2)));
8624 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8627 return 0;
8630 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8631 NULL_TREE if no simplification can be made. */
8633 static tree
8634 fold_builtin_signbit (tree fndecl, tree arglist)
8636 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8637 tree arg, temp;
8639 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8640 return NULL_TREE;
8642 arg = TREE_VALUE (arglist);
8644 /* If ARG is a compile-time constant, determine the result. */
8645 if (TREE_CODE (arg) == REAL_CST
8646 && !TREE_CONSTANT_OVERFLOW (arg))
8648 REAL_VALUE_TYPE c;
8650 c = TREE_REAL_CST (arg);
8651 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8652 return fold_convert (type, temp);
8655 /* If ARG is non-negative, the result is always zero. */
8656 if (tree_expr_nonnegative_p (arg))
8657 return omit_one_operand (type, integer_zero_node, arg);
8659 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8660 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8661 return fold_build2 (LT_EXPR, type, arg,
8662 build_real (TREE_TYPE (arg), dconst0));
8664 return NULL_TREE;
8667 /* Fold function call to builtin copysign, copysignf or copysignl.
8668 Return NULL_TREE if no simplification can be made. */
8670 static tree
8671 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8673 tree arg1, arg2, tem;
8675 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8676 return NULL_TREE;
8678 arg1 = TREE_VALUE (arglist);
8679 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8681 /* copysign(X,X) is X. */
8682 if (operand_equal_p (arg1, arg2, 0))
8683 return fold_convert (type, arg1);
8685 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8686 if (TREE_CODE (arg1) == REAL_CST
8687 && TREE_CODE (arg2) == REAL_CST
8688 && !TREE_CONSTANT_OVERFLOW (arg1)
8689 && !TREE_CONSTANT_OVERFLOW (arg2))
8691 REAL_VALUE_TYPE c1, c2;
8693 c1 = TREE_REAL_CST (arg1);
8694 c2 = TREE_REAL_CST (arg2);
8695 /* c1.sign := c2.sign. */
8696 real_copysign (&c1, &c2);
8697 return build_real (type, c1);
8700 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8701 Remember to evaluate Y for side-effects. */
8702 if (tree_expr_nonnegative_p (arg2))
8703 return omit_one_operand (type,
8704 fold_build1 (ABS_EXPR, type, arg1),
8705 arg2);
8707 /* Strip sign changing operations for the first argument. */
8708 tem = fold_strip_sign_ops (arg1);
8709 if (tem)
8711 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8712 return build_function_call_expr (fndecl, arglist);
8715 return NULL_TREE;
8718 /* Fold a call to builtin isascii. */
8720 static tree
8721 fold_builtin_isascii (tree arglist)
8723 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8724 return 0;
8725 else
8727 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8728 tree arg = TREE_VALUE (arglist);
8730 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8731 build_int_cst (NULL_TREE,
8732 ~ (unsigned HOST_WIDE_INT) 0x7f));
8733 return fold_build2 (EQ_EXPR, integer_type_node,
8734 arg, integer_zero_node);
8738 /* Fold a call to builtin toascii. */
8740 static tree
8741 fold_builtin_toascii (tree arglist)
8743 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8744 return 0;
8745 else
8747 /* Transform toascii(c) -> (c & 0x7f). */
8748 tree arg = TREE_VALUE (arglist);
8750 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8751 build_int_cst (NULL_TREE, 0x7f));
8755 /* Fold a call to builtin isdigit. */
8757 static tree
8758 fold_builtin_isdigit (tree arglist)
8760 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8761 return 0;
8762 else
8764 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8765 /* According to the C standard, isdigit is unaffected by locale.
8766 However, it definitely is affected by the target character set. */
8767 tree arg;
8768 unsigned HOST_WIDE_INT target_digit0
8769 = lang_hooks.to_target_charset ('0');
8771 if (target_digit0 == 0)
8772 return NULL_TREE;
8774 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8775 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8776 build_int_cst (unsigned_type_node, target_digit0));
8777 return fold_build2 (LE_EXPR, integer_type_node, arg,
8778 build_int_cst (unsigned_type_node, 9));
8782 /* Fold a call to fabs, fabsf or fabsl. */
8784 static tree
8785 fold_builtin_fabs (tree arglist, tree type)
8787 tree arg;
8789 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8790 return 0;
8792 arg = TREE_VALUE (arglist);
8793 arg = fold_convert (type, arg);
8794 if (TREE_CODE (arg) == REAL_CST)
8795 return fold_abs_const (arg, type);
8796 return fold_build1 (ABS_EXPR, type, arg);
8799 /* Fold a call to abs, labs, llabs or imaxabs. */
8801 static tree
8802 fold_builtin_abs (tree arglist, tree type)
8804 tree arg;
8806 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8807 return 0;
8809 arg = TREE_VALUE (arglist);
8810 arg = fold_convert (type, arg);
8811 if (TREE_CODE (arg) == INTEGER_CST)
8812 return fold_abs_const (arg, type);
8813 return fold_build1 (ABS_EXPR, type, arg);
8816 /* Fold a call to builtin fmin or fmax. */
8818 static tree
8819 fold_builtin_fmin_fmax (tree arglist, tree type, bool max)
8821 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8823 tree arg0 = TREE_VALUE (arglist);
8824 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8825 /* Calculate the result when the argument is a constant. */
8826 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
8828 if (res)
8829 return res;
8831 /* If either argument is NaN, return the other one. Avoid the
8832 transformation if we get (and honor) a signalling NaN. Using
8833 omit_one_operand() ensures we create a non-lvalue. */
8834 if (TREE_CODE (arg0) == REAL_CST
8835 && real_isnan (&TREE_REAL_CST (arg0))
8836 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8837 || ! TREE_REAL_CST (arg0).signalling))
8838 return omit_one_operand (type, arg1, arg0);
8839 if (TREE_CODE (arg1) == REAL_CST
8840 && real_isnan (&TREE_REAL_CST (arg1))
8841 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
8842 || ! TREE_REAL_CST (arg1).signalling))
8843 return omit_one_operand (type, arg0, arg1);
8845 /* Transform fmin/fmax(x,x) -> x. */
8846 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8847 return omit_one_operand (type, arg0, arg1);
8849 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
8850 functions to return the numeric arg if the other one is NaN.
8851 These tree codes don't honor that, so only transform if
8852 -ffinite-math-only is set. C99 doesn't require -0.0 to be
8853 handled, so we don't have to worry about it either. */
8854 if (flag_finite_math_only)
8855 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
8856 fold_convert (type, arg0),
8857 fold_convert (type, arg1));
8859 return NULL_TREE;
8862 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8863 EXP is the CALL_EXPR for the call. */
8865 static tree
8866 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8868 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8869 tree arg;
8870 REAL_VALUE_TYPE r;
8872 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8874 /* Check that we have exactly one argument. */
8875 if (arglist == 0)
8877 error ("too few arguments to function %qs",
8878 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8879 return error_mark_node;
8881 else if (TREE_CHAIN (arglist) != 0)
8883 error ("too many arguments to function %qs",
8884 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8885 return error_mark_node;
8887 else
8889 error ("non-floating-point argument to function %qs",
8890 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8891 return error_mark_node;
8895 arg = TREE_VALUE (arglist);
8896 switch (builtin_index)
8898 case BUILT_IN_ISINF:
8899 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8900 return omit_one_operand (type, integer_zero_node, arg);
8902 if (TREE_CODE (arg) == REAL_CST)
8904 r = TREE_REAL_CST (arg);
8905 if (real_isinf (&r))
8906 return real_compare (GT_EXPR, &r, &dconst0)
8907 ? integer_one_node : integer_minus_one_node;
8908 else
8909 return integer_zero_node;
8912 return NULL_TREE;
8914 case BUILT_IN_FINITE:
8915 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
8916 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8917 return omit_one_operand (type, integer_one_node, arg);
8919 if (TREE_CODE (arg) == REAL_CST)
8921 r = TREE_REAL_CST (arg);
8922 return real_isinf (&r) || real_isnan (&r)
8923 ? integer_zero_node : integer_one_node;
8926 return NULL_TREE;
8928 case BUILT_IN_ISNAN:
8929 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
8930 return omit_one_operand (type, integer_zero_node, arg);
8932 if (TREE_CODE (arg) == REAL_CST)
8934 r = TREE_REAL_CST (arg);
8935 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8938 arg = builtin_save_expr (arg);
8939 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8941 default:
8942 gcc_unreachable ();
8946 /* Fold a call to an unordered comparison function such as
8947 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8948 being called and ARGLIST is the argument list for the call.
8949 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8950 the opposite of the desired result. UNORDERED_CODE is used
8951 for modes that can hold NaNs and ORDERED_CODE is used for
8952 the rest. */
8954 static tree
8955 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8956 enum tree_code unordered_code,
8957 enum tree_code ordered_code)
8959 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8960 enum tree_code code;
8961 tree arg0, arg1;
8962 tree type0, type1;
8963 enum tree_code code0, code1;
8964 tree cmp_type = NULL_TREE;
8966 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8968 /* Check that we have exactly two arguments. */
8969 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8971 error ("too few arguments to function %qs",
8972 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8973 return error_mark_node;
8975 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8977 error ("too many arguments to function %qs",
8978 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8979 return error_mark_node;
8983 arg0 = TREE_VALUE (arglist);
8984 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8986 type0 = TREE_TYPE (arg0);
8987 type1 = TREE_TYPE (arg1);
8989 code0 = TREE_CODE (type0);
8990 code1 = TREE_CODE (type1);
8992 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8993 /* Choose the wider of two real types. */
8994 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8995 ? type0 : type1;
8996 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8997 cmp_type = type0;
8998 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8999 cmp_type = type1;
9000 else
9002 error ("non-floating-point argument to function %qs",
9003 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9004 return error_mark_node;
9007 arg0 = fold_convert (cmp_type, arg0);
9008 arg1 = fold_convert (cmp_type, arg1);
9010 if (unordered_code == UNORDERED_EXPR)
9012 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9013 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9014 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9017 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9018 : ordered_code;
9019 return fold_build1 (TRUTH_NOT_EXPR, type,
9020 fold_build2 (code, type, arg0, arg1));
9023 /* Used by constant folding to simplify calls to builtin functions. EXP is
9024 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9025 result of the function call is ignored. This function returns NULL_TREE
9026 if no simplification was possible. */
9028 static tree
9029 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9031 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9032 enum built_in_function fcode;
9034 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9035 return targetm.fold_builtin (fndecl, arglist, ignore);
9037 fcode = DECL_FUNCTION_CODE (fndecl);
9038 switch (fcode)
9040 case BUILT_IN_FPUTS:
9041 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9043 case BUILT_IN_FPUTS_UNLOCKED:
9044 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9046 case BUILT_IN_STRSTR:
9047 return fold_builtin_strstr (arglist, type);
9049 case BUILT_IN_STRCAT:
9050 return fold_builtin_strcat (arglist);
9052 case BUILT_IN_STRNCAT:
9053 return fold_builtin_strncat (arglist);
9055 case BUILT_IN_STRSPN:
9056 return fold_builtin_strspn (arglist);
9058 case BUILT_IN_STRCSPN:
9059 return fold_builtin_strcspn (arglist);
9061 case BUILT_IN_STRCHR:
9062 case BUILT_IN_INDEX:
9063 return fold_builtin_strchr (arglist, type);
9065 case BUILT_IN_STRRCHR:
9066 case BUILT_IN_RINDEX:
9067 return fold_builtin_strrchr (arglist, type);
9069 case BUILT_IN_STRCPY:
9070 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9072 case BUILT_IN_STRNCPY:
9073 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9075 case BUILT_IN_STRCMP:
9076 return fold_builtin_strcmp (arglist);
9078 case BUILT_IN_STRNCMP:
9079 return fold_builtin_strncmp (arglist);
9081 case BUILT_IN_STRPBRK:
9082 return fold_builtin_strpbrk (arglist, type);
9084 case BUILT_IN_BCMP:
9085 case BUILT_IN_MEMCMP:
9086 return fold_builtin_memcmp (arglist);
9088 case BUILT_IN_SPRINTF:
9089 return fold_builtin_sprintf (arglist, ignore);
9091 case BUILT_IN_CONSTANT_P:
9093 tree val;
9095 val = fold_builtin_constant_p (arglist);
9096 /* Gimplification will pull the CALL_EXPR for the builtin out of
9097 an if condition. When not optimizing, we'll not CSE it back.
9098 To avoid link error types of regressions, return false now. */
9099 if (!val && !optimize)
9100 val = integer_zero_node;
9102 return val;
9105 case BUILT_IN_EXPECT:
9106 return fold_builtin_expect (arglist);
9108 case BUILT_IN_CLASSIFY_TYPE:
9109 return fold_builtin_classify_type (arglist);
9111 case BUILT_IN_STRLEN:
9112 return fold_builtin_strlen (arglist);
9114 CASE_FLT_FN (BUILT_IN_FABS):
9115 return fold_builtin_fabs (arglist, type);
9117 case BUILT_IN_ABS:
9118 case BUILT_IN_LABS:
9119 case BUILT_IN_LLABS:
9120 case BUILT_IN_IMAXABS:
9121 return fold_builtin_abs (arglist, type);
9123 CASE_FLT_FN (BUILT_IN_CONJ):
9124 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9125 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9126 break;
9128 CASE_FLT_FN (BUILT_IN_CREAL):
9129 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9130 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9131 TREE_VALUE (arglist)));
9132 break;
9134 CASE_FLT_FN (BUILT_IN_CIMAG):
9135 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9136 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9137 TREE_VALUE (arglist)));
9138 break;
9140 CASE_FLT_FN (BUILT_IN_CABS):
9141 return fold_builtin_cabs (arglist, type, fndecl);
9143 CASE_FLT_FN (BUILT_IN_SQRT):
9144 return fold_builtin_sqrt (arglist, type);
9146 CASE_FLT_FN (BUILT_IN_CBRT):
9147 return fold_builtin_cbrt (arglist, type);
9149 CASE_FLT_FN (BUILT_IN_ASIN):
9150 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9151 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9152 &dconstm1, &dconst1, true);
9153 break;
9155 CASE_FLT_FN (BUILT_IN_ACOS):
9156 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9157 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9158 &dconstm1, &dconst1, true);
9159 break;
9161 CASE_FLT_FN (BUILT_IN_ATAN):
9162 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9163 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9164 NULL, NULL, 0);
9165 break;
9167 CASE_FLT_FN (BUILT_IN_ASINH):
9168 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9169 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9170 NULL, NULL, 0);
9171 break;
9173 CASE_FLT_FN (BUILT_IN_ACOSH):
9174 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9175 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9176 &dconst1, NULL, true);
9177 break;
9179 CASE_FLT_FN (BUILT_IN_ATANH):
9180 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9181 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9182 &dconstm1, &dconst1, false);
9183 break;
9185 CASE_FLT_FN (BUILT_IN_SIN):
9186 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9187 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9188 NULL, NULL, 0);
9189 break;
9191 CASE_FLT_FN (BUILT_IN_COS):
9192 return fold_builtin_cos (arglist, type, fndecl);
9194 CASE_FLT_FN (BUILT_IN_TAN):
9195 return fold_builtin_tan (arglist, type);
9197 CASE_FLT_FN (BUILT_IN_SINCOS):
9198 if (validate_arglist (arglist, REAL_TYPE, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9199 return do_mpfr_sincos (TREE_VALUE (arglist), TREE_VALUE (TREE_CHAIN (arglist)),
9200 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))));
9201 break;
9203 CASE_FLT_FN (BUILT_IN_SINH):
9204 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9205 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9206 NULL, NULL, 0);
9207 break;
9209 CASE_FLT_FN (BUILT_IN_COSH):
9210 return fold_builtin_cosh (arglist, type, fndecl);
9212 CASE_FLT_FN (BUILT_IN_TANH):
9213 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9214 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9215 NULL, NULL, 0);
9216 break;
9218 CASE_FLT_FN (BUILT_IN_ERF):
9219 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9220 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9221 NULL, NULL, 0);
9222 break;
9224 CASE_FLT_FN (BUILT_IN_ERFC):
9225 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9226 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9227 NULL, NULL, 0);
9228 break;
9230 CASE_FLT_FN (BUILT_IN_TGAMMA):
9231 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9232 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9233 NULL, NULL, 0);
9234 break;
9236 CASE_FLT_FN (BUILT_IN_EXP):
9237 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9239 CASE_FLT_FN (BUILT_IN_EXP2):
9240 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9242 CASE_FLT_FN (BUILT_IN_EXP10):
9243 CASE_FLT_FN (BUILT_IN_POW10):
9244 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9246 CASE_FLT_FN (BUILT_IN_EXPM1):
9247 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9248 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9249 NULL, NULL, 0);
9250 break;
9252 CASE_FLT_FN (BUILT_IN_LOG):
9253 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9255 CASE_FLT_FN (BUILT_IN_LOG2):
9256 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9258 CASE_FLT_FN (BUILT_IN_LOG10):
9259 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9261 CASE_FLT_FN (BUILT_IN_LOG1P):
9262 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9263 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9264 &dconstm1, NULL, false);
9265 break;
9267 CASE_FLT_FN (BUILT_IN_ATAN2):
9268 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9269 return do_mpfr_arg2 (TREE_VALUE (arglist),
9270 TREE_VALUE (TREE_CHAIN (arglist)),
9271 type, mpfr_atan2);
9272 break;
9274 CASE_FLT_FN (BUILT_IN_FMA):
9275 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9276 return do_mpfr_arg3 (TREE_VALUE (arglist),
9277 TREE_VALUE (TREE_CHAIN (arglist)),
9278 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
9279 type, mpfr_fma);
9280 break;
9282 CASE_FLT_FN (BUILT_IN_FMIN):
9283 return fold_builtin_fmin_fmax (arglist, type, /*max=*/false);
9285 CASE_FLT_FN (BUILT_IN_FMAX):
9286 return fold_builtin_fmin_fmax (arglist, type, /*max=*/true);
9288 CASE_FLT_FN (BUILT_IN_HYPOT):
9289 return fold_builtin_hypot (fndecl, arglist, type);
9291 CASE_FLT_FN (BUILT_IN_POW):
9292 return fold_builtin_pow (fndecl, arglist, type);
9294 CASE_FLT_FN (BUILT_IN_POWI):
9295 return fold_builtin_powi (fndecl, arglist, type);
9297 CASE_FLT_FN (BUILT_IN_INF):
9298 case BUILT_IN_INFD32:
9299 case BUILT_IN_INFD64:
9300 case BUILT_IN_INFD128:
9301 return fold_builtin_inf (type, true);
9303 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9304 return fold_builtin_inf (type, false);
9306 CASE_FLT_FN (BUILT_IN_NAN):
9307 case BUILT_IN_NAND32:
9308 case BUILT_IN_NAND64:
9309 case BUILT_IN_NAND128:
9310 return fold_builtin_nan (arglist, type, true);
9312 CASE_FLT_FN (BUILT_IN_NANS):
9313 return fold_builtin_nan (arglist, type, false);
9315 CASE_FLT_FN (BUILT_IN_FLOOR):
9316 return fold_builtin_floor (fndecl, arglist);
9318 CASE_FLT_FN (BUILT_IN_CEIL):
9319 return fold_builtin_ceil (fndecl, arglist);
9321 CASE_FLT_FN (BUILT_IN_TRUNC):
9322 return fold_builtin_trunc (fndecl, arglist);
9324 CASE_FLT_FN (BUILT_IN_ROUND):
9325 return fold_builtin_round (fndecl, arglist);
9327 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9328 CASE_FLT_FN (BUILT_IN_RINT):
9329 return fold_trunc_transparent_mathfn (fndecl, arglist);
9331 CASE_FLT_FN (BUILT_IN_LCEIL):
9332 CASE_FLT_FN (BUILT_IN_LLCEIL):
9333 CASE_FLT_FN (BUILT_IN_LFLOOR):
9334 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9335 CASE_FLT_FN (BUILT_IN_LROUND):
9336 CASE_FLT_FN (BUILT_IN_LLROUND):
9337 return fold_builtin_int_roundingfn (fndecl, arglist);
9339 CASE_FLT_FN (BUILT_IN_LRINT):
9340 CASE_FLT_FN (BUILT_IN_LLRINT):
9341 return fold_fixed_mathfn (fndecl, arglist);
9343 case BUILT_IN_BSWAP32:
9344 case BUILT_IN_BSWAP64:
9345 return fold_builtin_bswap (fndecl, arglist);
9347 CASE_INT_FN (BUILT_IN_FFS):
9348 CASE_INT_FN (BUILT_IN_CLZ):
9349 CASE_INT_FN (BUILT_IN_CTZ):
9350 CASE_INT_FN (BUILT_IN_POPCOUNT):
9351 CASE_INT_FN (BUILT_IN_PARITY):
9352 return fold_builtin_bitop (fndecl, arglist);
9354 case BUILT_IN_MEMSET:
9355 return fold_builtin_memset (arglist, type, ignore);
9357 case BUILT_IN_MEMCPY:
9358 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9360 case BUILT_IN_MEMPCPY:
9361 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9363 case BUILT_IN_MEMMOVE:
9364 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9366 case BUILT_IN_BZERO:
9367 return fold_builtin_bzero (arglist, ignore);
9369 case BUILT_IN_BCOPY:
9370 return fold_builtin_bcopy (arglist, ignore);
9372 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9373 return fold_builtin_signbit (fndecl, arglist);
9375 case BUILT_IN_ISASCII:
9376 return fold_builtin_isascii (arglist);
9378 case BUILT_IN_TOASCII:
9379 return fold_builtin_toascii (arglist);
9381 case BUILT_IN_ISDIGIT:
9382 return fold_builtin_isdigit (arglist);
9384 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9385 return fold_builtin_copysign (fndecl, arglist, type);
9387 CASE_FLT_FN (BUILT_IN_FINITE):
9388 case BUILT_IN_FINITED32:
9389 case BUILT_IN_FINITED64:
9390 case BUILT_IN_FINITED128:
9391 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9393 CASE_FLT_FN (BUILT_IN_ISINF):
9394 case BUILT_IN_ISINFD32:
9395 case BUILT_IN_ISINFD64:
9396 case BUILT_IN_ISINFD128:
9397 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9399 CASE_FLT_FN (BUILT_IN_ISNAN):
9400 case BUILT_IN_ISNAND32:
9401 case BUILT_IN_ISNAND64:
9402 case BUILT_IN_ISNAND128:
9403 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9405 case BUILT_IN_ISGREATER:
9406 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9407 case BUILT_IN_ISGREATEREQUAL:
9408 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9409 case BUILT_IN_ISLESS:
9410 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9411 case BUILT_IN_ISLESSEQUAL:
9412 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9413 case BUILT_IN_ISLESSGREATER:
9414 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9415 case BUILT_IN_ISUNORDERED:
9416 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9417 NOP_EXPR);
9419 /* We do the folding for va_start in the expander. */
9420 case BUILT_IN_VA_START:
9421 break;
9423 case BUILT_IN_OBJECT_SIZE:
9424 return fold_builtin_object_size (arglist);
9425 case BUILT_IN_MEMCPY_CHK:
9426 case BUILT_IN_MEMPCPY_CHK:
9427 case BUILT_IN_MEMMOVE_CHK:
9428 case BUILT_IN_MEMSET_CHK:
9429 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9430 DECL_FUNCTION_CODE (fndecl));
9431 case BUILT_IN_STRCPY_CHK:
9432 case BUILT_IN_STPCPY_CHK:
9433 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9434 DECL_FUNCTION_CODE (fndecl));
9435 case BUILT_IN_STRNCPY_CHK:
9436 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9437 case BUILT_IN_STRCAT_CHK:
9438 return fold_builtin_strcat_chk (fndecl, arglist);
9439 case BUILT_IN_STRNCAT_CHK:
9440 return fold_builtin_strncat_chk (fndecl, arglist);
9441 case BUILT_IN_SPRINTF_CHK:
9442 case BUILT_IN_VSPRINTF_CHK:
9443 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9444 case BUILT_IN_SNPRINTF_CHK:
9445 case BUILT_IN_VSNPRINTF_CHK:
9446 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9447 DECL_FUNCTION_CODE (fndecl));
9449 case BUILT_IN_PRINTF:
9450 case BUILT_IN_PRINTF_UNLOCKED:
9451 case BUILT_IN_VPRINTF:
9452 case BUILT_IN_PRINTF_CHK:
9453 case BUILT_IN_VPRINTF_CHK:
9454 return fold_builtin_printf (fndecl, arglist, ignore,
9455 DECL_FUNCTION_CODE (fndecl));
9457 case BUILT_IN_FPRINTF:
9458 case BUILT_IN_FPRINTF_UNLOCKED:
9459 case BUILT_IN_VFPRINTF:
9460 case BUILT_IN_FPRINTF_CHK:
9461 case BUILT_IN_VFPRINTF_CHK:
9462 return fold_builtin_fprintf (fndecl, arglist, ignore,
9463 DECL_FUNCTION_CODE (fndecl));
9465 default:
9466 break;
9469 return 0;
9472 /* A wrapper function for builtin folding that prevents warnings for
9473 "statement without effect" and the like, caused by removing the
9474 call node earlier than the warning is generated. */
9476 tree
9477 fold_builtin (tree fndecl, tree arglist, bool ignore)
9479 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9480 if (exp && !ignore)
9482 exp = build1 (NOP_EXPR, GENERIC_TREE_TYPE (exp), exp);
9483 TREE_NO_WARNING (exp) = 1;
9486 return exp;
9489 /* Conveniently construct a function call expression. */
9491 tree
9492 build_function_call_expr (tree fn, tree arglist)
9494 tree call_expr;
9496 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9497 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9498 call_expr, arglist, NULL_TREE);
9501 /* This function validates the types of a function call argument list
9502 represented as a tree chain of parameters against a specified list
9503 of tree_codes. If the last specifier is a 0, that represents an
9504 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9506 static int
9507 validate_arglist (tree arglist, ...)
9509 enum tree_code code;
9510 int res = 0;
9511 va_list ap;
9513 va_start (ap, arglist);
9517 code = va_arg (ap, enum tree_code);
9518 switch (code)
9520 case 0:
9521 /* This signifies an ellipses, any further arguments are all ok. */
9522 res = 1;
9523 goto end;
9524 case VOID_TYPE:
9525 /* This signifies an endlink, if no arguments remain, return
9526 true, otherwise return false. */
9527 res = arglist == 0;
9528 goto end;
9529 default:
9530 /* If no parameters remain or the parameter's code does not
9531 match the specified code, return false. Otherwise continue
9532 checking any remaining arguments. */
9533 if (arglist == 0)
9534 goto end;
9535 if (code == POINTER_TYPE)
9537 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9538 goto end;
9540 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9541 goto end;
9542 break;
9544 arglist = TREE_CHAIN (arglist);
9546 while (1);
9548 /* We need gotos here since we can only have one VA_CLOSE in a
9549 function. */
9550 end: ;
9551 va_end (ap);
9553 return res;
9556 /* Default target-specific builtin expander that does nothing. */
9559 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9560 rtx target ATTRIBUTE_UNUSED,
9561 rtx subtarget ATTRIBUTE_UNUSED,
9562 enum machine_mode mode ATTRIBUTE_UNUSED,
9563 int ignore ATTRIBUTE_UNUSED)
9565 return NULL_RTX;
9568 /* Returns true is EXP represents data that would potentially reside
9569 in a readonly section. */
9571 static bool
9572 readonly_data_expr (tree exp)
9574 STRIP_NOPS (exp);
9576 if (TREE_CODE (exp) != ADDR_EXPR)
9577 return false;
9579 exp = get_base_address (TREE_OPERAND (exp, 0));
9580 if (!exp)
9581 return false;
9583 /* Make sure we call decl_readonly_section only for trees it
9584 can handle (since it returns true for everything it doesn't
9585 understand). */
9586 if (TREE_CODE (exp) == STRING_CST
9587 || TREE_CODE (exp) == CONSTRUCTOR
9588 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9589 return decl_readonly_section (exp, 0);
9590 else
9591 return false;
9594 /* Simplify a call to the strstr builtin.
9596 Return 0 if no simplification was possible, otherwise return the
9597 simplified form of the call as a tree.
9599 The simplified form may be a constant or other expression which
9600 computes the same value, but in a more efficient manner (including
9601 calls to other builtin functions).
9603 The call may contain arguments which need to be evaluated, but
9604 which are not useful to determine the result of the call. In
9605 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9606 COMPOUND_EXPR will be an argument which must be evaluated.
9607 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9608 COMPOUND_EXPR in the chain will contain the tree for the simplified
9609 form of the builtin function call. */
9611 static tree
9612 fold_builtin_strstr (tree arglist, tree type)
9614 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9615 return 0;
9616 else
9618 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9619 tree fn;
9620 const char *p1, *p2;
9622 p2 = c_getstr (s2);
9623 if (p2 == NULL)
9624 return 0;
9626 p1 = c_getstr (s1);
9627 if (p1 != NULL)
9629 const char *r = strstr (p1, p2);
9630 tree tem;
9632 if (r == NULL)
9633 return build_int_cst (TREE_TYPE (s1), 0);
9635 /* Return an offset into the constant string argument. */
9636 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9637 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9638 return fold_convert (type, tem);
9641 /* The argument is const char *, and the result is char *, so we need
9642 a type conversion here to avoid a warning. */
9643 if (p2[0] == '\0')
9644 return fold_convert (type, s1);
9646 if (p2[1] != '\0')
9647 return 0;
9649 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9650 if (!fn)
9651 return 0;
9653 /* New argument list transforming strstr(s1, s2) to
9654 strchr(s1, s2[0]). */
9655 arglist = build_tree_list (NULL_TREE,
9656 build_int_cst (NULL_TREE, p2[0]));
9657 arglist = tree_cons (NULL_TREE, s1, arglist);
9658 return build_function_call_expr (fn, arglist);
9662 /* Simplify a call to the strchr builtin.
9664 Return 0 if no simplification was possible, otherwise return the
9665 simplified form of the call as a tree.
9667 The simplified form may be a constant or other expression which
9668 computes the same value, but in a more efficient manner (including
9669 calls to other builtin functions).
9671 The call may contain arguments which need to be evaluated, but
9672 which are not useful to determine the result of the call. In
9673 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9674 COMPOUND_EXPR will be an argument which must be evaluated.
9675 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9676 COMPOUND_EXPR in the chain will contain the tree for the simplified
9677 form of the builtin function call. */
9679 static tree
9680 fold_builtin_strchr (tree arglist, tree type)
9682 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9683 return 0;
9684 else
9686 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9687 const char *p1;
9689 if (TREE_CODE (s2) != INTEGER_CST)
9690 return 0;
9692 p1 = c_getstr (s1);
9693 if (p1 != NULL)
9695 char c;
9696 const char *r;
9697 tree tem;
9699 if (target_char_cast (s2, &c))
9700 return 0;
9702 r = strchr (p1, c);
9704 if (r == NULL)
9705 return build_int_cst (TREE_TYPE (s1), 0);
9707 /* Return an offset into the constant string argument. */
9708 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9709 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9710 return fold_convert (type, tem);
9712 return 0;
9716 /* Simplify a call to the strrchr builtin.
9718 Return 0 if no simplification was possible, otherwise return the
9719 simplified form of the call as a tree.
9721 The simplified form may be a constant or other expression which
9722 computes the same value, but in a more efficient manner (including
9723 calls to other builtin functions).
9725 The call may contain arguments which need to be evaluated, but
9726 which are not useful to determine the result of the call. In
9727 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9728 COMPOUND_EXPR will be an argument which must be evaluated.
9729 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9730 COMPOUND_EXPR in the chain will contain the tree for the simplified
9731 form of the builtin function call. */
9733 static tree
9734 fold_builtin_strrchr (tree arglist, tree type)
9736 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9737 return 0;
9738 else
9740 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9741 tree fn;
9742 const char *p1;
9744 if (TREE_CODE (s2) != INTEGER_CST)
9745 return 0;
9747 p1 = c_getstr (s1);
9748 if (p1 != NULL)
9750 char c;
9751 const char *r;
9752 tree tem;
9754 if (target_char_cast (s2, &c))
9755 return 0;
9757 r = strrchr (p1, c);
9759 if (r == NULL)
9760 return build_int_cst (TREE_TYPE (s1), 0);
9762 /* Return an offset into the constant string argument. */
9763 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9764 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9765 return fold_convert (type, tem);
9768 if (! integer_zerop (s2))
9769 return 0;
9771 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9772 if (!fn)
9773 return 0;
9775 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9776 return build_function_call_expr (fn, arglist);
9780 /* Simplify a call to the strpbrk builtin.
9782 Return 0 if no simplification was possible, otherwise return the
9783 simplified form of the call as a tree.
9785 The simplified form may be a constant or other expression which
9786 computes the same value, but in a more efficient manner (including
9787 calls to other builtin functions).
9789 The call may contain arguments which need to be evaluated, but
9790 which are not useful to determine the result of the call. In
9791 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9792 COMPOUND_EXPR will be an argument which must be evaluated.
9793 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9794 COMPOUND_EXPR in the chain will contain the tree for the simplified
9795 form of the builtin function call. */
9797 static tree
9798 fold_builtin_strpbrk (tree arglist, tree type)
9800 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9801 return 0;
9802 else
9804 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9805 tree fn;
9806 const char *p1, *p2;
9808 p2 = c_getstr (s2);
9809 if (p2 == NULL)
9810 return 0;
9812 p1 = c_getstr (s1);
9813 if (p1 != NULL)
9815 const char *r = strpbrk (p1, p2);
9816 tree tem;
9818 if (r == NULL)
9819 return build_int_cst (TREE_TYPE (s1), 0);
9821 /* Return an offset into the constant string argument. */
9822 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9823 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9824 return fold_convert (type, tem);
9827 if (p2[0] == '\0')
9828 /* strpbrk(x, "") == NULL.
9829 Evaluate and ignore s1 in case it had side-effects. */
9830 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9832 if (p2[1] != '\0')
9833 return 0; /* Really call strpbrk. */
9835 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9836 if (!fn)
9837 return 0;
9839 /* New argument list transforming strpbrk(s1, s2) to
9840 strchr(s1, s2[0]). */
9841 arglist = build_tree_list (NULL_TREE,
9842 build_int_cst (NULL_TREE, p2[0]));
9843 arglist = tree_cons (NULL_TREE, s1, arglist);
9844 return build_function_call_expr (fn, arglist);
9848 /* Simplify a call to the strcat builtin.
9850 Return 0 if no simplification was possible, otherwise return the
9851 simplified form of the call as a tree.
9853 The simplified form may be a constant or other expression which
9854 computes the same value, but in a more efficient manner (including
9855 calls to other builtin functions).
9857 The call may contain arguments which need to be evaluated, but
9858 which are not useful to determine the result of the call. In
9859 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9860 COMPOUND_EXPR will be an argument which must be evaluated.
9861 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9862 COMPOUND_EXPR in the chain will contain the tree for the simplified
9863 form of the builtin function call. */
9865 static tree
9866 fold_builtin_strcat (tree arglist)
9868 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9869 return 0;
9870 else
9872 tree dst = TREE_VALUE (arglist),
9873 src = TREE_VALUE (TREE_CHAIN (arglist));
9874 const char *p = c_getstr (src);
9876 /* If the string length is zero, return the dst parameter. */
9877 if (p && *p == '\0')
9878 return dst;
9880 return 0;
9884 /* Simplify a call to the strncat builtin.
9886 Return 0 if no simplification was possible, otherwise return the
9887 simplified form of the call as a tree.
9889 The simplified form may be a constant or other expression which
9890 computes the same value, but in a more efficient manner (including
9891 calls to other builtin functions).
9893 The call may contain arguments which need to be evaluated, but
9894 which are not useful to determine the result of the call. In
9895 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9896 COMPOUND_EXPR will be an argument which must be evaluated.
9897 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9898 COMPOUND_EXPR in the chain will contain the tree for the simplified
9899 form of the builtin function call. */
9901 static tree
9902 fold_builtin_strncat (tree arglist)
9904 if (!validate_arglist (arglist,
9905 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9906 return 0;
9907 else
9909 tree dst = TREE_VALUE (arglist);
9910 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9911 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9912 const char *p = c_getstr (src);
9914 /* If the requested length is zero, or the src parameter string
9915 length is zero, return the dst parameter. */
9916 if (integer_zerop (len) || (p && *p == '\0'))
9917 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9919 /* If the requested len is greater than or equal to the string
9920 length, call strcat. */
9921 if (TREE_CODE (len) == INTEGER_CST && p
9922 && compare_tree_int (len, strlen (p)) >= 0)
9924 tree newarglist
9925 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9926 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9928 /* If the replacement _DECL isn't initialized, don't do the
9929 transformation. */
9930 if (!fn)
9931 return 0;
9933 return build_function_call_expr (fn, newarglist);
9935 return 0;
9939 /* Simplify a call to the strspn builtin.
9941 Return 0 if no simplification was possible, otherwise return the
9942 simplified form of the call as a tree.
9944 The simplified form may be a constant or other expression which
9945 computes the same value, but in a more efficient manner (including
9946 calls to other builtin functions).
9948 The call may contain arguments which need to be evaluated, but
9949 which are not useful to determine the result of the call. In
9950 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9951 COMPOUND_EXPR will be an argument which must be evaluated.
9952 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9953 COMPOUND_EXPR in the chain will contain the tree for the simplified
9954 form of the builtin function call. */
9956 static tree
9957 fold_builtin_strspn (tree arglist)
9959 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9960 return 0;
9961 else
9963 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9964 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9966 /* If both arguments are constants, evaluate at compile-time. */
9967 if (p1 && p2)
9969 const size_t r = strspn (p1, p2);
9970 return size_int (r);
9973 /* If either argument is "", return 0. */
9974 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9975 /* Evaluate and ignore both arguments in case either one has
9976 side-effects. */
9977 return omit_two_operands (integer_type_node, integer_zero_node,
9978 s1, s2);
9979 return 0;
9983 /* Simplify a call to the strcspn builtin.
9985 Return 0 if no simplification was possible, otherwise return the
9986 simplified form of the call as a tree.
9988 The simplified form may be a constant or other expression which
9989 computes the same value, but in a more efficient manner (including
9990 calls to other builtin functions).
9992 The call may contain arguments which need to be evaluated, but
9993 which are not useful to determine the result of the call. In
9994 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9995 COMPOUND_EXPR will be an argument which must be evaluated.
9996 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9997 COMPOUND_EXPR in the chain will contain the tree for the simplified
9998 form of the builtin function call. */
10000 static tree
10001 fold_builtin_strcspn (tree arglist)
10003 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10004 return 0;
10005 else
10007 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10008 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10010 /* If both arguments are constants, evaluate at compile-time. */
10011 if (p1 && p2)
10013 const size_t r = strcspn (p1, p2);
10014 return size_int (r);
10017 /* If the first argument is "", return 0. */
10018 if (p1 && *p1 == '\0')
10020 /* Evaluate and ignore argument s2 in case it has
10021 side-effects. */
10022 return omit_one_operand (integer_type_node,
10023 integer_zero_node, s2);
10026 /* If the second argument is "", return __builtin_strlen(s1). */
10027 if (p2 && *p2 == '\0')
10029 tree newarglist = build_tree_list (NULL_TREE, s1),
10030 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10032 /* If the replacement _DECL isn't initialized, don't do the
10033 transformation. */
10034 if (!fn)
10035 return 0;
10037 return build_function_call_expr (fn, newarglist);
10039 return 0;
10043 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10044 by the builtin will be ignored. UNLOCKED is true is true if this
10045 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10046 the known length of the string. Return NULL_TREE if no simplification
10047 was possible. */
10049 tree
10050 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10052 tree fn;
10053 /* If we're using an unlocked function, assume the other unlocked
10054 functions exist explicitly. */
10055 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10056 : implicit_built_in_decls[BUILT_IN_FPUTC];
10057 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10058 : implicit_built_in_decls[BUILT_IN_FWRITE];
10060 /* If the return value is used, don't do the transformation. */
10061 if (!ignore)
10062 return 0;
10064 /* Verify the arguments in the original call. */
10065 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10066 return 0;
10068 if (! len)
10069 len = c_strlen (TREE_VALUE (arglist), 0);
10071 /* Get the length of the string passed to fputs. If the length
10072 can't be determined, punt. */
10073 if (!len
10074 || TREE_CODE (len) != INTEGER_CST)
10075 return 0;
10077 switch (compare_tree_int (len, 1))
10079 case -1: /* length is 0, delete the call entirely . */
10080 return omit_one_operand (integer_type_node, integer_zero_node,
10081 TREE_VALUE (TREE_CHAIN (arglist)));
10083 case 0: /* length is 1, call fputc. */
10085 const char *p = c_getstr (TREE_VALUE (arglist));
10087 if (p != NULL)
10089 /* New argument list transforming fputs(string, stream) to
10090 fputc(string[0], stream). */
10091 arglist = build_tree_list (NULL_TREE,
10092 TREE_VALUE (TREE_CHAIN (arglist)));
10093 arglist = tree_cons (NULL_TREE,
10094 build_int_cst (NULL_TREE, p[0]),
10095 arglist);
10096 fn = fn_fputc;
10097 break;
10100 /* FALLTHROUGH */
10101 case 1: /* length is greater than 1, call fwrite. */
10103 tree string_arg;
10105 /* If optimizing for size keep fputs. */
10106 if (optimize_size)
10107 return 0;
10108 string_arg = TREE_VALUE (arglist);
10109 /* New argument list transforming fputs(string, stream) to
10110 fwrite(string, 1, len, stream). */
10111 arglist = build_tree_list (NULL_TREE,
10112 TREE_VALUE (TREE_CHAIN (arglist)));
10113 arglist = tree_cons (NULL_TREE, len, arglist);
10114 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10115 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10116 fn = fn_fwrite;
10117 break;
10119 default:
10120 gcc_unreachable ();
10123 /* If the replacement _DECL isn't initialized, don't do the
10124 transformation. */
10125 if (!fn)
10126 return 0;
10128 /* These optimizations are only performed when the result is ignored,
10129 hence there's no need to cast the result to integer_type_node. */
10130 return build_function_call_expr (fn, arglist);
10133 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10134 produced. False otherwise. This is done so that we don't output the error
10135 or warning twice or three times. */
10136 bool
10137 fold_builtin_next_arg (tree arglist)
10139 tree fntype = TREE_TYPE (current_function_decl);
10141 if (TYPE_ARG_TYPES (fntype) == 0
10142 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10143 == void_type_node))
10145 error ("%<va_start%> used in function with fixed args");
10146 return true;
10148 else if (!arglist)
10150 /* Evidently an out of date version of <stdarg.h>; can't validate
10151 va_start's second argument, but can still work as intended. */
10152 warning (0, "%<__builtin_next_arg%> called without an argument");
10153 return true;
10155 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10156 when we checked the arguments and if needed issued a warning. */
10157 else if (!TREE_CHAIN (arglist)
10158 || !integer_zerop (TREE_VALUE (arglist))
10159 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10160 || TREE_CHAIN (TREE_CHAIN (arglist)))
10162 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10163 tree arg = TREE_VALUE (arglist);
10165 if (TREE_CHAIN (arglist))
10167 error ("%<va_start%> used with too many arguments");
10168 return true;
10171 /* Strip off all nops for the sake of the comparison. This
10172 is not quite the same as STRIP_NOPS. It does more.
10173 We must also strip off INDIRECT_EXPR for C++ reference
10174 parameters. */
10175 while (TREE_CODE (arg) == NOP_EXPR
10176 || TREE_CODE (arg) == CONVERT_EXPR
10177 || TREE_CODE (arg) == NON_LVALUE_EXPR
10178 || TREE_CODE (arg) == INDIRECT_REF)
10179 arg = TREE_OPERAND (arg, 0);
10180 if (arg != last_parm)
10182 /* FIXME: Sometimes with the tree optimizers we can get the
10183 not the last argument even though the user used the last
10184 argument. We just warn and set the arg to be the last
10185 argument so that we will get wrong-code because of
10186 it. */
10187 warning (0, "second parameter of %<va_start%> not last named argument");
10189 /* We want to verify the second parameter just once before the tree
10190 optimizers are run and then avoid keeping it in the tree,
10191 as otherwise we could warn even for correct code like:
10192 void foo (int i, ...)
10193 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10194 TREE_VALUE (arglist) = integer_zero_node;
10195 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10197 return false;
10201 /* Simplify a call to the sprintf builtin.
10203 Return 0 if no simplification was possible, otherwise return the
10204 simplified form of the call as a tree. If IGNORED is true, it means that
10205 the caller does not use the returned value of the function. */
10207 static tree
10208 fold_builtin_sprintf (tree arglist, int ignored)
10210 tree call, retval, dest, fmt;
10211 const char *fmt_str = NULL;
10213 /* Verify the required arguments in the original call. We deal with two
10214 types of sprintf() calls: 'sprintf (str, fmt)' and
10215 'sprintf (dest, "%s", orig)'. */
10216 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10217 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10218 VOID_TYPE))
10219 return NULL_TREE;
10221 /* Get the destination string and the format specifier. */
10222 dest = TREE_VALUE (arglist);
10223 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10225 /* Check whether the format is a literal string constant. */
10226 fmt_str = c_getstr (fmt);
10227 if (fmt_str == NULL)
10228 return NULL_TREE;
10230 call = NULL_TREE;
10231 retval = NULL_TREE;
10233 if (!init_target_chars())
10234 return 0;
10236 /* If the format doesn't contain % args or %%, use strcpy. */
10237 if (strchr (fmt_str, target_percent) == NULL)
10239 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10241 if (!fn)
10242 return NULL_TREE;
10244 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10245 'format' is known to contain no % formats. */
10246 arglist = build_tree_list (NULL_TREE, fmt);
10247 arglist = tree_cons (NULL_TREE, dest, arglist);
10248 call = build_function_call_expr (fn, arglist);
10249 if (!ignored)
10250 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10253 /* If the format is "%s", use strcpy if the result isn't used. */
10254 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10256 tree fn, orig;
10257 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10259 if (!fn)
10260 return NULL_TREE;
10262 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10263 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10264 arglist = build_tree_list (NULL_TREE, orig);
10265 arglist = tree_cons (NULL_TREE, dest, arglist);
10266 if (!ignored)
10268 retval = c_strlen (orig, 1);
10269 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10270 return NULL_TREE;
10272 call = build_function_call_expr (fn, arglist);
10275 if (call && retval)
10277 retval = fold_convert
10278 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10279 retval);
10280 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10282 else
10283 return call;
10286 /* Expand a call to __builtin_object_size. */
10289 expand_builtin_object_size (tree exp)
10291 tree ost;
10292 int object_size_type;
10293 tree fndecl = get_callee_fndecl (exp);
10294 tree arglist = TREE_OPERAND (exp, 1);
10295 location_t locus = EXPR_LOCATION (exp);
10297 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10299 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10300 &locus, fndecl);
10301 expand_builtin_trap ();
10302 return const0_rtx;
10305 ost = TREE_VALUE (TREE_CHAIN (arglist));
10306 STRIP_NOPS (ost);
10308 if (TREE_CODE (ost) != INTEGER_CST
10309 || tree_int_cst_sgn (ost) < 0
10310 || compare_tree_int (ost, 3) > 0)
10312 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10313 &locus, fndecl);
10314 expand_builtin_trap ();
10315 return const0_rtx;
10318 object_size_type = tree_low_cst (ost, 0);
10320 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10323 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10324 FCODE is the BUILT_IN_* to use.
10325 Return 0 if we failed; the caller should emit a normal call,
10326 otherwise try to get the result in TARGET, if convenient (and in
10327 mode MODE if that's convenient). */
10329 static rtx
10330 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10331 enum built_in_function fcode)
10333 tree arglist = TREE_OPERAND (exp, 1);
10334 tree dest, src, len, size;
10336 if (!validate_arglist (arglist,
10337 POINTER_TYPE,
10338 fcode == BUILT_IN_MEMSET_CHK
10339 ? INTEGER_TYPE : POINTER_TYPE,
10340 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10341 return 0;
10343 dest = TREE_VALUE (arglist);
10344 src = TREE_VALUE (TREE_CHAIN (arglist));
10345 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10346 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10348 if (! host_integerp (size, 1))
10349 return 0;
10351 if (host_integerp (len, 1) || integer_all_onesp (size))
10353 tree fn;
10355 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10357 location_t locus = EXPR_LOCATION (exp);
10358 warning (0, "%Hcall to %D will always overflow destination buffer",
10359 &locus, get_callee_fndecl (exp));
10360 return 0;
10363 arglist = build_tree_list (NULL_TREE, len);
10364 arglist = tree_cons (NULL_TREE, src, arglist);
10365 arglist = tree_cons (NULL_TREE, dest, arglist);
10367 fn = NULL_TREE;
10368 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10369 mem{cpy,pcpy,move,set} is available. */
10370 switch (fcode)
10372 case BUILT_IN_MEMCPY_CHK:
10373 fn = built_in_decls[BUILT_IN_MEMCPY];
10374 break;
10375 case BUILT_IN_MEMPCPY_CHK:
10376 fn = built_in_decls[BUILT_IN_MEMPCPY];
10377 break;
10378 case BUILT_IN_MEMMOVE_CHK:
10379 fn = built_in_decls[BUILT_IN_MEMMOVE];
10380 break;
10381 case BUILT_IN_MEMSET_CHK:
10382 fn = built_in_decls[BUILT_IN_MEMSET];
10383 break;
10384 default:
10385 break;
10388 if (! fn)
10389 return 0;
10391 fn = build_function_call_expr (fn, arglist);
10392 if (TREE_CODE (fn) == CALL_EXPR)
10393 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10394 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10396 else if (fcode == BUILT_IN_MEMSET_CHK)
10397 return 0;
10398 else
10400 unsigned int dest_align
10401 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10403 /* If DEST is not a pointer type, call the normal function. */
10404 if (dest_align == 0)
10405 return 0;
10407 /* If SRC and DEST are the same (and not volatile), do nothing. */
10408 if (operand_equal_p (src, dest, 0))
10410 tree expr;
10412 if (fcode != BUILT_IN_MEMPCPY_CHK)
10414 /* Evaluate and ignore LEN in case it has side-effects. */
10415 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10416 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10419 len = fold_convert (TREE_TYPE (dest), len);
10420 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10421 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10424 /* __memmove_chk special case. */
10425 if (fcode == BUILT_IN_MEMMOVE_CHK)
10427 unsigned int src_align
10428 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10430 if (src_align == 0)
10431 return 0;
10433 /* If src is categorized for a readonly section we can use
10434 normal __memcpy_chk. */
10435 if (readonly_data_expr (src))
10437 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10438 if (!fn)
10439 return 0;
10440 fn = build_function_call_expr (fn, arglist);
10441 if (TREE_CODE (fn) == CALL_EXPR)
10442 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10443 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10446 return 0;
10450 /* Emit warning if a buffer overflow is detected at compile time. */
10452 static void
10453 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10455 int arg_mask, is_strlen = 0;
10456 tree arglist = TREE_OPERAND (exp, 1), a;
10457 tree len, size;
10458 location_t locus;
10460 switch (fcode)
10462 case BUILT_IN_STRCPY_CHK:
10463 case BUILT_IN_STPCPY_CHK:
10464 /* For __strcat_chk the warning will be emitted only if overflowing
10465 by at least strlen (dest) + 1 bytes. */
10466 case BUILT_IN_STRCAT_CHK:
10467 arg_mask = 6;
10468 is_strlen = 1;
10469 break;
10470 case BUILT_IN_STRNCAT_CHK:
10471 /* For __strncat_chk the warning will be emitted only if overflowing
10472 by at least strlen (dest) + 1 bytes. */
10473 arg_mask = 12;
10474 break;
10475 case BUILT_IN_STRNCPY_CHK:
10476 arg_mask = 12;
10477 break;
10478 case BUILT_IN_SNPRINTF_CHK:
10479 case BUILT_IN_VSNPRINTF_CHK:
10480 arg_mask = 10;
10481 break;
10482 default:
10483 gcc_unreachable ();
10486 len = NULL_TREE;
10487 size = NULL_TREE;
10488 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10489 if (arg_mask & 1)
10491 if (len)
10492 size = a;
10493 else
10494 len = a;
10497 if (!len || !size)
10498 return;
10500 len = TREE_VALUE (len);
10501 size = TREE_VALUE (size);
10503 if (! host_integerp (size, 1) || integer_all_onesp (size))
10504 return;
10506 if (is_strlen)
10508 len = c_strlen (len, 1);
10509 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10510 return;
10512 else if (fcode == BUILT_IN_STRNCAT_CHK)
10514 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10515 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10516 return;
10517 src = c_strlen (src, 1);
10518 if (! src || ! host_integerp (src, 1))
10520 locus = EXPR_LOCATION (exp);
10521 warning (0, "%Hcall to %D might overflow destination buffer",
10522 &locus, get_callee_fndecl (exp));
10523 return;
10525 else if (tree_int_cst_lt (src, size))
10526 return;
10528 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10529 return;
10531 locus = EXPR_LOCATION (exp);
10532 warning (0, "%Hcall to %D will always overflow destination buffer",
10533 &locus, get_callee_fndecl (exp));
10536 /* Emit warning if a buffer overflow is detected at compile time
10537 in __sprintf_chk/__vsprintf_chk calls. */
10539 static void
10540 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10542 tree arglist = TREE_OPERAND (exp, 1);
10543 tree dest, size, len, fmt, flag;
10544 const char *fmt_str;
10546 /* Verify the required arguments in the original call. */
10547 if (! arglist)
10548 return;
10549 dest = TREE_VALUE (arglist);
10550 arglist = TREE_CHAIN (arglist);
10551 if (! arglist)
10552 return;
10553 flag = TREE_VALUE (arglist);
10554 arglist = TREE_CHAIN (arglist);
10555 if (! arglist)
10556 return;
10557 size = TREE_VALUE (arglist);
10558 arglist = TREE_CHAIN (arglist);
10559 if (! arglist)
10560 return;
10561 fmt = TREE_VALUE (arglist);
10562 arglist = TREE_CHAIN (arglist);
10564 if (! host_integerp (size, 1) || integer_all_onesp (size))
10565 return;
10567 /* Check whether the format is a literal string constant. */
10568 fmt_str = c_getstr (fmt);
10569 if (fmt_str == NULL)
10570 return;
10572 if (!init_target_chars())
10573 return;
10575 /* If the format doesn't contain % args or %%, we know its size. */
10576 if (strchr (fmt_str, target_percent) == 0)
10577 len = build_int_cstu (size_type_node, strlen (fmt_str));
10578 /* If the format is "%s" and first ... argument is a string literal,
10579 we know it too. */
10580 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10582 tree arg;
10584 if (! arglist)
10585 return;
10586 arg = TREE_VALUE (arglist);
10587 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10588 return;
10590 len = c_strlen (arg, 1);
10591 if (!len || ! host_integerp (len, 1))
10592 return;
10594 else
10595 return;
10597 if (! tree_int_cst_lt (len, size))
10599 location_t locus = EXPR_LOCATION (exp);
10600 warning (0, "%Hcall to %D will always overflow destination buffer",
10601 &locus, get_callee_fndecl (exp));
10605 /* Fold a call to __builtin_object_size, if possible. */
10607 tree
10608 fold_builtin_object_size (tree arglist)
10610 tree ptr, ost, ret = 0;
10611 int object_size_type;
10613 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10614 return 0;
10616 ptr = TREE_VALUE (arglist);
10617 ost = TREE_VALUE (TREE_CHAIN (arglist));
10618 STRIP_NOPS (ost);
10620 if (TREE_CODE (ost) != INTEGER_CST
10621 || tree_int_cst_sgn (ost) < 0
10622 || compare_tree_int (ost, 3) > 0)
10623 return 0;
10625 object_size_type = tree_low_cst (ost, 0);
10627 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10628 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10629 and (size_t) 0 for types 2 and 3. */
10630 if (TREE_SIDE_EFFECTS (ptr))
10631 return fold_convert (size_type_node,
10632 object_size_type < 2
10633 ? integer_minus_one_node : integer_zero_node);
10635 if (TREE_CODE (ptr) == ADDR_EXPR)
10636 ret = build_int_cstu (size_type_node,
10637 compute_builtin_object_size (ptr, object_size_type));
10639 else if (TREE_CODE (ptr) == SSA_NAME)
10641 unsigned HOST_WIDE_INT bytes;
10643 /* If object size is not known yet, delay folding until
10644 later. Maybe subsequent passes will help determining
10645 it. */
10646 bytes = compute_builtin_object_size (ptr, object_size_type);
10647 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10648 ? -1 : 0))
10649 ret = build_int_cstu (size_type_node, bytes);
10652 if (ret)
10654 ret = force_fit_type (ret, -1, false, false);
10655 if (TREE_CONSTANT_OVERFLOW (ret))
10656 ret = 0;
10659 return ret;
10662 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10663 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10664 code of the builtin. If MAXLEN is not NULL, it is maximum length
10665 passed as third argument. */
10667 tree
10668 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10669 enum built_in_function fcode)
10671 tree dest, src, len, size, fn;
10673 if (!validate_arglist (arglist,
10674 POINTER_TYPE,
10675 fcode == BUILT_IN_MEMSET_CHK
10676 ? INTEGER_TYPE : POINTER_TYPE,
10677 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10678 return 0;
10680 dest = TREE_VALUE (arglist);
10681 /* Actually val for __memset_chk, but it doesn't matter. */
10682 src = TREE_VALUE (TREE_CHAIN (arglist));
10683 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10684 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10686 /* If SRC and DEST are the same (and not volatile), return DEST
10687 (resp. DEST+LEN for __mempcpy_chk). */
10688 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10690 if (fcode != BUILT_IN_MEMPCPY_CHK)
10691 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10692 else
10694 tree temp = fold_convert (TREE_TYPE (dest), len);
10695 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10696 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10700 if (! host_integerp (size, 1))
10701 return 0;
10703 if (! integer_all_onesp (size))
10705 if (! host_integerp (len, 1))
10707 /* If LEN is not constant, try MAXLEN too.
10708 For MAXLEN only allow optimizing into non-_ocs function
10709 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10710 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10712 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10714 /* (void) __mempcpy_chk () can be optimized into
10715 (void) __memcpy_chk (). */
10716 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10717 if (!fn)
10718 return 0;
10720 return build_function_call_expr (fn, arglist);
10722 return 0;
10725 else
10726 maxlen = len;
10728 if (tree_int_cst_lt (size, maxlen))
10729 return 0;
10732 arglist = build_tree_list (NULL_TREE, len);
10733 arglist = tree_cons (NULL_TREE, src, arglist);
10734 arglist = tree_cons (NULL_TREE, dest, arglist);
10736 fn = NULL_TREE;
10737 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10738 mem{cpy,pcpy,move,set} is available. */
10739 switch (fcode)
10741 case BUILT_IN_MEMCPY_CHK:
10742 fn = built_in_decls[BUILT_IN_MEMCPY];
10743 break;
10744 case BUILT_IN_MEMPCPY_CHK:
10745 fn = built_in_decls[BUILT_IN_MEMPCPY];
10746 break;
10747 case BUILT_IN_MEMMOVE_CHK:
10748 fn = built_in_decls[BUILT_IN_MEMMOVE];
10749 break;
10750 case BUILT_IN_MEMSET_CHK:
10751 fn = built_in_decls[BUILT_IN_MEMSET];
10752 break;
10753 default:
10754 break;
10757 if (!fn)
10758 return 0;
10760 return build_function_call_expr (fn, arglist);
10763 /* Fold a call to the __st[rp]cpy_chk builtin.
10764 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10765 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10766 strings passed as second argument. */
10768 tree
10769 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10770 enum built_in_function fcode)
10772 tree dest, src, size, len, fn;
10774 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10775 VOID_TYPE))
10776 return 0;
10778 dest = TREE_VALUE (arglist);
10779 src = TREE_VALUE (TREE_CHAIN (arglist));
10780 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10782 /* If SRC and DEST are the same (and not volatile), return DEST. */
10783 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10784 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10786 if (! host_integerp (size, 1))
10787 return 0;
10789 if (! integer_all_onesp (size))
10791 len = c_strlen (src, 1);
10792 if (! len || ! host_integerp (len, 1))
10794 /* If LEN is not constant, try MAXLEN too.
10795 For MAXLEN only allow optimizing into non-_ocs function
10796 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10797 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10799 if (fcode == BUILT_IN_STPCPY_CHK)
10801 if (! ignore)
10802 return 0;
10804 /* If return value of __stpcpy_chk is ignored,
10805 optimize into __strcpy_chk. */
10806 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10807 if (!fn)
10808 return 0;
10810 return build_function_call_expr (fn, arglist);
10813 if (! len || TREE_SIDE_EFFECTS (len))
10814 return 0;
10816 /* If c_strlen returned something, but not a constant,
10817 transform __strcpy_chk into __memcpy_chk. */
10818 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10819 if (!fn)
10820 return 0;
10822 len = size_binop (PLUS_EXPR, len, ssize_int (1));
10823 arglist = build_tree_list (NULL_TREE, size);
10824 arglist = tree_cons (NULL_TREE, len, arglist);
10825 arglist = tree_cons (NULL_TREE, src, arglist);
10826 arglist = tree_cons (NULL_TREE, dest, arglist);
10827 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10828 build_function_call_expr (fn, arglist));
10831 else
10832 maxlen = len;
10834 if (! tree_int_cst_lt (maxlen, size))
10835 return 0;
10838 arglist = build_tree_list (NULL_TREE, src);
10839 arglist = tree_cons (NULL_TREE, dest, arglist);
10841 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10842 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10843 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10844 if (!fn)
10845 return 0;
10847 return build_function_call_expr (fn, arglist);
10850 /* Fold a call to the __strncpy_chk builtin.
10851 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10853 tree
10854 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10856 tree dest, src, size, len, fn;
10858 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10859 INTEGER_TYPE, VOID_TYPE))
10860 return 0;
10862 dest = TREE_VALUE (arglist);
10863 src = TREE_VALUE (TREE_CHAIN (arglist));
10864 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10865 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10867 if (! host_integerp (size, 1))
10868 return 0;
10870 if (! integer_all_onesp (size))
10872 if (! host_integerp (len, 1))
10874 /* If LEN is not constant, try MAXLEN too.
10875 For MAXLEN only allow optimizing into non-_ocs function
10876 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10877 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10878 return 0;
10880 else
10881 maxlen = len;
10883 if (tree_int_cst_lt (size, maxlen))
10884 return 0;
10887 arglist = build_tree_list (NULL_TREE, len);
10888 arglist = tree_cons (NULL_TREE, src, arglist);
10889 arglist = tree_cons (NULL_TREE, dest, arglist);
10891 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10892 fn = built_in_decls[BUILT_IN_STRNCPY];
10893 if (!fn)
10894 return 0;
10896 return build_function_call_expr (fn, arglist);
10899 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10901 static tree
10902 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10904 tree dest, src, size, fn;
10905 const char *p;
10907 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10908 VOID_TYPE))
10909 return 0;
10911 dest = TREE_VALUE (arglist);
10912 src = TREE_VALUE (TREE_CHAIN (arglist));
10913 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10915 p = c_getstr (src);
10916 /* If the SRC parameter is "", return DEST. */
10917 if (p && *p == '\0')
10918 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10920 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10921 return 0;
10923 arglist = build_tree_list (NULL_TREE, src);
10924 arglist = tree_cons (NULL_TREE, dest, arglist);
10926 /* If __builtin_strcat_chk is used, assume strcat is available. */
10927 fn = built_in_decls[BUILT_IN_STRCAT];
10928 if (!fn)
10929 return 0;
10931 return build_function_call_expr (fn, arglist);
10934 /* Fold a call to the __strncat_chk builtin EXP. */
10936 static tree
10937 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10939 tree dest, src, size, len, fn;
10940 const char *p;
10942 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10943 INTEGER_TYPE, VOID_TYPE))
10944 return 0;
10946 dest = TREE_VALUE (arglist);
10947 src = TREE_VALUE (TREE_CHAIN (arglist));
10948 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10949 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10951 p = c_getstr (src);
10952 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10953 if (p && *p == '\0')
10954 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10955 else if (integer_zerop (len))
10956 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10958 if (! host_integerp (size, 1))
10959 return 0;
10961 if (! integer_all_onesp (size))
10963 tree src_len = c_strlen (src, 1);
10964 if (src_len
10965 && host_integerp (src_len, 1)
10966 && host_integerp (len, 1)
10967 && ! tree_int_cst_lt (len, src_len))
10969 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10970 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10971 if (!fn)
10972 return 0;
10974 arglist = build_tree_list (NULL_TREE, size);
10975 arglist = tree_cons (NULL_TREE, src, arglist);
10976 arglist = tree_cons (NULL_TREE, dest, arglist);
10977 return build_function_call_expr (fn, arglist);
10979 return 0;
10982 arglist = build_tree_list (NULL_TREE, len);
10983 arglist = tree_cons (NULL_TREE, src, arglist);
10984 arglist = tree_cons (NULL_TREE, dest, arglist);
10986 /* If __builtin_strncat_chk is used, assume strncat is available. */
10987 fn = built_in_decls[BUILT_IN_STRNCAT];
10988 if (!fn)
10989 return 0;
10991 return build_function_call_expr (fn, arglist);
10994 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10995 a normal call should be emitted rather than expanding the function
10996 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10998 static tree
10999 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
11001 tree dest, size, len, fn, fmt, flag;
11002 const char *fmt_str;
11004 /* Verify the required arguments in the original call. */
11005 if (! arglist)
11006 return 0;
11007 dest = TREE_VALUE (arglist);
11008 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11009 return 0;
11010 arglist = TREE_CHAIN (arglist);
11011 if (! arglist)
11012 return 0;
11013 flag = TREE_VALUE (arglist);
11014 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
11015 return 0;
11016 arglist = TREE_CHAIN (arglist);
11017 if (! arglist)
11018 return 0;
11019 size = TREE_VALUE (arglist);
11020 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11021 return 0;
11022 arglist = TREE_CHAIN (arglist);
11023 if (! arglist)
11024 return 0;
11025 fmt = TREE_VALUE (arglist);
11026 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11027 return 0;
11028 arglist = TREE_CHAIN (arglist);
11030 if (! host_integerp (size, 1))
11031 return 0;
11033 len = NULL_TREE;
11035 if (!init_target_chars())
11036 return 0;
11038 /* Check whether the format is a literal string constant. */
11039 fmt_str = c_getstr (fmt);
11040 if (fmt_str != NULL)
11042 /* If the format doesn't contain % args or %%, we know the size. */
11043 if (strchr (fmt_str, target_percent) == 0)
11045 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11046 len = build_int_cstu (size_type_node, strlen (fmt_str));
11048 /* If the format is "%s" and first ... argument is a string literal,
11049 we know the size too. */
11050 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11052 tree arg;
11054 if (arglist && !TREE_CHAIN (arglist))
11056 arg = TREE_VALUE (arglist);
11057 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11059 len = c_strlen (arg, 1);
11060 if (! len || ! host_integerp (len, 1))
11061 len = NULL_TREE;
11067 if (! integer_all_onesp (size))
11069 if (! len || ! tree_int_cst_lt (len, size))
11070 return 0;
11073 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11074 or if format doesn't contain % chars or is "%s". */
11075 if (! integer_zerop (flag))
11077 if (fmt_str == NULL)
11078 return 0;
11079 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11080 return 0;
11083 arglist = tree_cons (NULL_TREE, fmt, arglist);
11084 arglist = tree_cons (NULL_TREE, dest, arglist);
11086 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11087 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11088 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11089 if (!fn)
11090 return 0;
11092 return build_function_call_expr (fn, arglist);
11095 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11096 a normal call should be emitted rather than expanding the function
11097 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11098 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11099 passed as second argument. */
11101 tree
11102 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11103 enum built_in_function fcode)
11105 tree dest, size, len, fn, fmt, flag;
11106 const char *fmt_str;
11108 /* Verify the required arguments in the original call. */
11109 if (! arglist)
11110 return 0;
11111 dest = TREE_VALUE (arglist);
11112 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11113 return 0;
11114 arglist = TREE_CHAIN (arglist);
11115 if (! arglist)
11116 return 0;
11117 len = TREE_VALUE (arglist);
11118 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11119 return 0;
11120 arglist = TREE_CHAIN (arglist);
11121 if (! arglist)
11122 return 0;
11123 flag = TREE_VALUE (arglist);
11124 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11125 return 0;
11126 arglist = TREE_CHAIN (arglist);
11127 if (! arglist)
11128 return 0;
11129 size = TREE_VALUE (arglist);
11130 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11131 return 0;
11132 arglist = TREE_CHAIN (arglist);
11133 if (! arglist)
11134 return 0;
11135 fmt = TREE_VALUE (arglist);
11136 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11137 return 0;
11138 arglist = TREE_CHAIN (arglist);
11140 if (! host_integerp (size, 1))
11141 return 0;
11143 if (! integer_all_onesp (size))
11145 if (! host_integerp (len, 1))
11147 /* If LEN is not constant, try MAXLEN too.
11148 For MAXLEN only allow optimizing into non-_ocs function
11149 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11150 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11151 return 0;
11153 else
11154 maxlen = len;
11156 if (tree_int_cst_lt (size, maxlen))
11157 return 0;
11160 if (!init_target_chars())
11161 return 0;
11163 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11164 or if format doesn't contain % chars or is "%s". */
11165 if (! integer_zerop (flag))
11167 fmt_str = c_getstr (fmt);
11168 if (fmt_str == NULL)
11169 return 0;
11170 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11171 return 0;
11174 arglist = tree_cons (NULL_TREE, fmt, arglist);
11175 arglist = tree_cons (NULL_TREE, len, arglist);
11176 arglist = tree_cons (NULL_TREE, dest, arglist);
11178 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11179 available. */
11180 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11181 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11182 if (!fn)
11183 return 0;
11185 return build_function_call_expr (fn, arglist);
11188 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11190 Return 0 if no simplification was possible, otherwise return the
11191 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11192 code of the function to be simplified. */
11194 static tree
11195 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11196 enum built_in_function fcode)
11198 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11199 const char *fmt_str = NULL;
11201 /* If the return value is used, don't do the transformation. */
11202 if (! ignore)
11203 return 0;
11205 /* Verify the required arguments in the original call. */
11206 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11208 tree flag;
11210 if (! arglist)
11211 return 0;
11212 flag = TREE_VALUE (arglist);
11213 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11214 || TREE_SIDE_EFFECTS (flag))
11215 return 0;
11216 arglist = TREE_CHAIN (arglist);
11219 if (! arglist)
11220 return 0;
11221 fmt = TREE_VALUE (arglist);
11222 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11223 return 0;
11224 arglist = TREE_CHAIN (arglist);
11226 /* Check whether the format is a literal string constant. */
11227 fmt_str = c_getstr (fmt);
11228 if (fmt_str == NULL)
11229 return NULL_TREE;
11231 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11233 /* If we're using an unlocked function, assume the other
11234 unlocked functions exist explicitly. */
11235 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11236 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11238 else
11240 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11241 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11244 if (!init_target_chars())
11245 return 0;
11247 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11249 const char *str;
11251 if (strcmp (fmt_str, target_percent_s) == 0)
11253 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11254 return 0;
11256 if (! arglist
11257 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11258 || TREE_CHAIN (arglist))
11259 return 0;
11261 str = c_getstr (TREE_VALUE (arglist));
11262 if (str == NULL)
11263 return 0;
11265 else
11267 /* The format specifier doesn't contain any '%' characters. */
11268 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11269 && arglist)
11270 return 0;
11271 str = fmt_str;
11274 /* If the string was "", printf does nothing. */
11275 if (str[0] == '\0')
11276 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11278 /* If the string has length of 1, call putchar. */
11279 if (str[1] == '\0')
11281 /* Given printf("c"), (where c is any one character,)
11282 convert "c"[0] to an int and pass that to the replacement
11283 function. */
11284 arg = build_int_cst (NULL_TREE, str[0]);
11285 arglist = build_tree_list (NULL_TREE, arg);
11286 fn = fn_putchar;
11288 else
11290 /* If the string was "string\n", call puts("string"). */
11291 size_t len = strlen (str);
11292 if ((unsigned char)str[len - 1] == target_newline)
11294 /* Create a NUL-terminated string that's one char shorter
11295 than the original, stripping off the trailing '\n'. */
11296 char *newstr = alloca (len);
11297 memcpy (newstr, str, len - 1);
11298 newstr[len - 1] = 0;
11300 arg = build_string_literal (len, newstr);
11301 arglist = build_tree_list (NULL_TREE, arg);
11302 fn = fn_puts;
11304 else
11305 /* We'd like to arrange to call fputs(string,stdout) here,
11306 but we need stdout and don't have a way to get it yet. */
11307 return 0;
11311 /* The other optimizations can be done only on the non-va_list variants. */
11312 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11313 return 0;
11315 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11316 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11318 if (! arglist
11319 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11320 || TREE_CHAIN (arglist))
11321 return 0;
11322 fn = fn_puts;
11325 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11326 else if (strcmp (fmt_str, target_percent_c) == 0)
11328 if (! arglist
11329 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11330 || TREE_CHAIN (arglist))
11331 return 0;
11332 fn = fn_putchar;
11335 if (!fn)
11336 return 0;
11338 call = build_function_call_expr (fn, arglist);
11339 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11342 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11344 Return 0 if no simplification was possible, otherwise return the
11345 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11346 code of the function to be simplified. */
11348 static tree
11349 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11350 enum built_in_function fcode)
11352 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11353 const char *fmt_str = NULL;
11355 /* If the return value is used, don't do the transformation. */
11356 if (! ignore)
11357 return 0;
11359 /* Verify the required arguments in the original call. */
11360 if (! arglist)
11361 return 0;
11362 fp = TREE_VALUE (arglist);
11363 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11364 return 0;
11365 arglist = TREE_CHAIN (arglist);
11367 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11369 tree flag;
11371 if (! arglist)
11372 return 0;
11373 flag = TREE_VALUE (arglist);
11374 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11375 || TREE_SIDE_EFFECTS (flag))
11376 return 0;
11377 arglist = TREE_CHAIN (arglist);
11380 if (! arglist)
11381 return 0;
11382 fmt = TREE_VALUE (arglist);
11383 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11384 return 0;
11385 arglist = TREE_CHAIN (arglist);
11387 /* Check whether the format is a literal string constant. */
11388 fmt_str = c_getstr (fmt);
11389 if (fmt_str == NULL)
11390 return NULL_TREE;
11392 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11394 /* If we're using an unlocked function, assume the other
11395 unlocked functions exist explicitly. */
11396 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11397 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11399 else
11401 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11402 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11405 if (!init_target_chars())
11406 return 0;
11408 /* If the format doesn't contain % args or %%, use strcpy. */
11409 if (strchr (fmt_str, target_percent) == NULL)
11411 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11412 && arglist)
11413 return 0;
11415 /* If the format specifier was "", fprintf does nothing. */
11416 if (fmt_str[0] == '\0')
11418 /* If FP has side-effects, just wait until gimplification is
11419 done. */
11420 if (TREE_SIDE_EFFECTS (fp))
11421 return 0;
11423 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11426 /* When "string" doesn't contain %, replace all cases of
11427 fprintf (fp, string) with fputs (string, fp). The fputs
11428 builtin will take care of special cases like length == 1. */
11429 arglist = build_tree_list (NULL_TREE, fp);
11430 arglist = tree_cons (NULL_TREE, fmt, arglist);
11431 fn = fn_fputs;
11434 /* The other optimizations can be done only on the non-va_list variants. */
11435 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11436 return 0;
11438 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11439 else if (strcmp (fmt_str, target_percent_s) == 0)
11441 if (! arglist
11442 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11443 || TREE_CHAIN (arglist))
11444 return 0;
11445 arg = TREE_VALUE (arglist);
11446 arglist = build_tree_list (NULL_TREE, fp);
11447 arglist = tree_cons (NULL_TREE, arg, arglist);
11448 fn = fn_fputs;
11451 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11452 else if (strcmp (fmt_str, target_percent_c) == 0)
11454 if (! arglist
11455 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11456 || TREE_CHAIN (arglist))
11457 return 0;
11458 arg = TREE_VALUE (arglist);
11459 arglist = build_tree_list (NULL_TREE, fp);
11460 arglist = tree_cons (NULL_TREE, arg, arglist);
11461 fn = fn_fputc;
11464 if (!fn)
11465 return 0;
11467 call = build_function_call_expr (fn, arglist);
11468 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11471 /* Initialize format string characters in the target charset. */
11473 static bool
11474 init_target_chars (void)
11476 static bool init;
11477 if (!init)
11479 target_newline = lang_hooks.to_target_charset ('\n');
11480 target_percent = lang_hooks.to_target_charset ('%');
11481 target_c = lang_hooks.to_target_charset ('c');
11482 target_s = lang_hooks.to_target_charset ('s');
11483 if (target_newline == 0 || target_percent == 0 || target_c == 0
11484 || target_s == 0)
11485 return false;
11487 target_percent_c[0] = target_percent;
11488 target_percent_c[1] = target_c;
11489 target_percent_c[2] = '\0';
11491 target_percent_s[0] = target_percent;
11492 target_percent_s[1] = target_s;
11493 target_percent_s[2] = '\0';
11495 target_percent_s_newline[0] = target_percent;
11496 target_percent_s_newline[1] = target_s;
11497 target_percent_s_newline[2] = target_newline;
11498 target_percent_s_newline[3] = '\0';
11500 init = true;
11502 return true;
11505 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11506 and no overflow/underflow occurred. INEXACT is true if M was not
11507 exactly calculated. TYPE is the tree type for the result. This
11508 function assumes that you cleared the MPFR flags and then
11509 calculated M to see if anything subsequently set a flag prior to
11510 entering this function. Return NULL_TREE if any checks fail. */
11512 static tree
11513 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11515 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11516 overflow/underflow occurred. If -frounding-math, proceed iff the
11517 result of calling FUNC was exact. */
11518 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11519 && (!flag_rounding_math || !inexact))
11521 REAL_VALUE_TYPE rr;
11523 real_from_mpfr (&rr, m);
11524 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11525 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11526 but the mpft_t is not, then we underflowed in the
11527 conversion. */
11528 if (!real_isnan (&rr) && !real_isinf (&rr)
11529 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11531 REAL_VALUE_TYPE rmode;
11533 real_convert (&rmode, TYPE_MODE (type), &rr);
11534 /* Proceed iff the specified mode can hold the value. */
11535 if (real_identical (&rmode, &rr))
11536 return build_real (type, rmode);
11539 return NULL_TREE;
11542 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11543 FUNC on it and return the resulting value as a tree with type TYPE.
11544 If MIN and/or MAX are not NULL, then the supplied ARG must be
11545 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11546 acceptable values, otherwise they are not. The mpfr precision is
11547 set to the precision of TYPE. We assume that function FUNC returns
11548 zero if the result could be calculated exactly within the requested
11549 precision. */
11551 static tree
11552 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11553 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11554 bool inclusive)
11556 tree result = NULL_TREE;
11558 STRIP_NOPS (arg);
11560 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11562 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11564 if (!real_isnan (ra) && !real_isinf (ra)
11565 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11566 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11568 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11569 int inexact;
11570 mpfr_t m;
11572 mpfr_init2 (m, prec);
11573 mpfr_from_real (m, ra);
11574 mpfr_clear_flags();
11575 inexact = func (m, m, GMP_RNDN);
11576 result = do_mpfr_ckconv (m, type, inexact);
11577 mpfr_clear (m);
11581 return result;
11584 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11585 FUNC on it and return the resulting value as a tree with type TYPE.
11586 The mpfr precision is set to the precision of TYPE. We assume that
11587 function FUNC returns zero if the result could be calculated
11588 exactly within the requested precision. */
11590 static tree
11591 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11592 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11594 tree result = NULL_TREE;
11596 STRIP_NOPS (arg1);
11597 STRIP_NOPS (arg2);
11599 if (TREE_CODE (arg1) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg1)
11600 && TREE_CODE (arg2) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg2))
11602 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11603 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11605 if (!real_isnan (ra1) && !real_isinf (ra1)
11606 && !real_isnan (ra2) && !real_isinf (ra2))
11608 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11609 int inexact;
11610 mpfr_t m1, m2;
11612 mpfr_inits2 (prec, m1, m2, NULL);
11613 mpfr_from_real (m1, ra1);
11614 mpfr_from_real (m2, ra2);
11615 mpfr_clear_flags();
11616 inexact = func (m1, m1, m2, GMP_RNDN);
11617 result = do_mpfr_ckconv (m1, type, inexact);
11618 mpfr_clears (m1, m2, NULL);
11622 return result;
11625 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11626 FUNC on it and return the resulting value as a tree with type TYPE.
11627 The mpfr precision is set to the precision of TYPE. We assume that
11628 function FUNC returns zero if the result could be calculated
11629 exactly within the requested precision. */
11631 static tree
11632 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11633 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11635 tree result = NULL_TREE;
11637 STRIP_NOPS (arg1);
11638 STRIP_NOPS (arg2);
11639 STRIP_NOPS (arg3);
11641 if (TREE_CODE (arg1) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg1)
11642 && TREE_CODE (arg2) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg2)
11643 && TREE_CODE (arg3) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg3))
11645 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11646 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11647 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11649 if (!real_isnan (ra1) && !real_isinf (ra1)
11650 && !real_isnan (ra2) && !real_isinf (ra2)
11651 && !real_isnan (ra3) && !real_isinf (ra3))
11653 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11654 int inexact;
11655 mpfr_t m1, m2, m3;
11657 mpfr_inits2 (prec, m1, m2, m3, NULL);
11658 mpfr_from_real (m1, ra1);
11659 mpfr_from_real (m2, ra2);
11660 mpfr_from_real (m3, ra3);
11661 mpfr_clear_flags();
11662 inexact = func (m1, m1, m2, m3, GMP_RNDN);
11663 result = do_mpfr_ckconv (m1, type, inexact);
11664 mpfr_clears (m1, m2, m3, NULL);
11668 return result;
11671 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11672 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11673 The type is taken from the type of ARG and is used for setting the
11674 precision of the calculation and results. */
11676 static tree
11677 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11679 tree result = NULL_TREE;
11681 STRIP_NOPS (arg);
11683 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11685 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11687 if (!real_isnan (ra) && !real_isinf (ra))
11689 tree const type = TREE_TYPE (arg);
11690 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11691 tree result_s, result_c;
11692 int inexact;
11693 mpfr_t m, ms, mc;
11695 mpfr_inits2 (prec, m, ms, mc, NULL);
11696 mpfr_from_real (m, ra);
11697 mpfr_clear_flags();
11698 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
11699 result_s = do_mpfr_ckconv (ms, type, inexact);
11700 result_c = do_mpfr_ckconv (mc, type, inexact);
11701 mpfr_clears (m, ms, mc, NULL);
11702 if (result_s && result_c)
11704 /* Dereference the sin/cos pointer arguments. */
11705 arg_sinp = build_fold_indirect_ref (arg_sinp);
11706 arg_cosp = build_fold_indirect_ref (arg_cosp);
11707 /* Proceed if valid pointer type were passed in. */
11708 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11709 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11711 /* Set the values. */
11712 result_s = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_sinp,
11713 result_s);
11714 TREE_SIDE_EFFECTS (result_s) = 1;
11715 result_c = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_cosp,
11716 result_c);
11717 TREE_SIDE_EFFECTS (result_c) = 1;
11718 /* Combine the assignments into a compound expr. */
11719 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11720 result_s, result_c));
11725 return result;