Fix asm string.
[official-gcc.git] / gcc / builtins.c
blob7bbd632c222f553ff9971631fc93c6a0d6618ccc
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"
51 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
53 #endif
55 /* Define the names of the builtin function types and codes. */
56 const char *const built_in_class_names[4]
57 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
60 const char * built_in_names[(int) END_BUILTINS] =
62 #include "builtins.def"
64 #undef DEF_BUILTIN
66 /* Setup an array of _DECL trees, make sure each element is
67 initialized to NULL_TREE. */
68 tree built_in_decls[(int) END_BUILTINS];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70 It may be NULL_TREE when this is invalid (for instance runtime is not
71 required to implement the function call in all cases). */
72 tree implicit_built_in_decls[(int) END_BUILTINS];
74 static int get_pointer_alignment (tree, unsigned int);
75 static const char *c_getstr (tree);
76 static rtx c_readstr (const char *, enum machine_mode);
77 static int target_char_cast (tree, char *);
78 static rtx get_memory_rtx (tree, tree);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx result_vector (int, rtx);
83 #endif
84 static void expand_builtin_update_setjmp_buf (rtx);
85 static void expand_builtin_prefetch (tree);
86 static rtx expand_builtin_apply_args (void);
87 static rtx expand_builtin_apply_args_1 (void);
88 static rtx expand_builtin_apply (rtx, rtx, rtx);
89 static void expand_builtin_return (rtx);
90 static enum type_class type_to_class (tree);
91 static rtx expand_builtin_classify_type (tree);
92 static void expand_errno_check (tree, rtx);
93 static rtx expand_builtin_mathfn (tree, rtx, rtx);
94 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
96 static rtx expand_builtin_sincos (tree);
97 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
98 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
99 static rtx expand_builtin_args_info (tree);
100 static rtx expand_builtin_next_arg (void);
101 static rtx expand_builtin_va_start (tree);
102 static rtx expand_builtin_va_end (tree);
103 static rtx expand_builtin_va_copy (tree);
104 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
107 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
108 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
114 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
115 static rtx expand_builtin_bcopy (tree);
116 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
118 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
120 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
123 static rtx expand_builtin_bzero (tree);
124 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, rtx);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static rtx expand_builtin_fputs (tree, rtx, bool);
133 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
136 static tree stabilize_va_list (tree, int);
137 static rtx expand_builtin_expect (tree, rtx);
138 static tree fold_builtin_constant_p (tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (tree);
141 static tree fold_builtin_inf (tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static int validate_arglist (tree, ...);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_tan (tree, tree);
155 static tree fold_builtin_trunc (tree, tree);
156 static tree fold_builtin_floor (tree, tree);
157 static tree fold_builtin_ceil (tree, tree);
158 static tree fold_builtin_round (tree, tree);
159 static tree fold_builtin_int_roundingfn (tree, tree);
160 static tree fold_builtin_bitop (tree, tree);
161 static tree fold_builtin_memory_op (tree, tree, bool, int);
162 static tree fold_builtin_strchr (tree, tree);
163 static tree fold_builtin_memcmp (tree);
164 static tree fold_builtin_strcmp (tree);
165 static tree fold_builtin_strncmp (tree);
166 static tree fold_builtin_signbit (tree, tree);
167 static tree fold_builtin_copysign (tree, tree, tree);
168 static tree fold_builtin_isascii (tree);
169 static tree fold_builtin_toascii (tree);
170 static tree fold_builtin_isdigit (tree);
171 static tree fold_builtin_fabs (tree, tree);
172 static tree fold_builtin_abs (tree, tree);
173 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
174 enum tree_code);
175 static tree fold_builtin_1 (tree, tree, bool);
177 static tree fold_builtin_strpbrk (tree, tree);
178 static tree fold_builtin_strstr (tree, tree);
179 static tree fold_builtin_strrchr (tree, tree);
180 static tree fold_builtin_strcat (tree);
181 static tree fold_builtin_strncat (tree);
182 static tree fold_builtin_strspn (tree);
183 static tree fold_builtin_strcspn (tree);
184 static tree fold_builtin_sprintf (tree, int);
186 static rtx expand_builtin_object_size (tree);
187 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
188 enum built_in_function);
189 static void maybe_emit_chk_warning (tree, enum built_in_function);
190 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
191 static tree fold_builtin_object_size (tree);
192 static tree fold_builtin_strcat_chk (tree, tree);
193 static tree fold_builtin_strncat_chk (tree, tree);
194 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
195 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
196 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
197 static bool init_target_chars (void);
199 static unsigned HOST_WIDE_INT target_newline;
200 static unsigned HOST_WIDE_INT target_percent;
201 static unsigned HOST_WIDE_INT target_c;
202 static unsigned HOST_WIDE_INT target_s;
203 static char target_percent_c[3];
204 static char target_percent_s[3];
205 static char target_percent_s_newline[4];
206 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
207 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
208 static tree do_mpfr_arg2 (tree, tree, tree,
209 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
211 /* Return true if NODE should be considered for inline expansion regardless
212 of the optimization level. This means whenever a function is invoked with
213 its "internal" name, which normally contains the prefix "__builtin". */
215 static bool called_as_built_in (tree node)
217 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
218 if (strncmp (name, "__builtin_", 10) == 0)
219 return true;
220 if (strncmp (name, "__sync_", 7) == 0)
221 return true;
222 return false;
225 /* Return the alignment in bits of EXP, a pointer valued expression.
226 But don't return more than MAX_ALIGN no matter what.
227 The alignment returned is, by default, the alignment of the thing that
228 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
230 Otherwise, look at the expression to see if we can do better, i.e., if the
231 expression is actually pointing at an object whose alignment is tighter. */
233 static int
234 get_pointer_alignment (tree exp, unsigned int max_align)
236 unsigned int align, inner;
238 /* We rely on TER to compute accurate alignment information. */
239 if (!(optimize && flag_tree_ter))
240 return 0;
242 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
243 return 0;
245 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
246 align = MIN (align, max_align);
248 while (1)
250 switch (TREE_CODE (exp))
252 case NOP_EXPR:
253 case CONVERT_EXPR:
254 case NON_LVALUE_EXPR:
255 exp = TREE_OPERAND (exp, 0);
256 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
257 return align;
259 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
260 align = MIN (inner, max_align);
261 break;
263 case PLUS_EXPR:
264 /* If sum of pointer + int, restrict our maximum alignment to that
265 imposed by the integer. If not, we can't do any better than
266 ALIGN. */
267 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
268 return align;
270 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
271 & (max_align / BITS_PER_UNIT - 1))
272 != 0)
273 max_align >>= 1;
275 exp = TREE_OPERAND (exp, 0);
276 break;
278 case ADDR_EXPR:
279 /* See what we are pointing at and look at its alignment. */
280 exp = TREE_OPERAND (exp, 0);
281 inner = max_align;
282 if (handled_component_p (exp))
284 HOST_WIDE_INT bitsize, bitpos;
285 tree offset;
286 enum machine_mode mode;
287 int unsignedp, volatilep;
289 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
290 &mode, &unsignedp, &volatilep, true);
291 if (bitpos)
292 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
293 if (offset && TREE_CODE (offset) == PLUS_EXPR
294 && host_integerp (TREE_OPERAND (offset, 1), 1))
296 /* Any overflow in calculating offset_bits won't change
297 the alignment. */
298 unsigned offset_bits
299 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
300 * BITS_PER_UNIT);
302 if (offset_bits)
303 inner = MIN (inner, (offset_bits & -offset_bits));
304 offset = TREE_OPERAND (offset, 0);
306 if (offset && TREE_CODE (offset) == MULT_EXPR
307 && host_integerp (TREE_OPERAND (offset, 1), 1))
309 /* Any overflow in calculating offset_factor won't change
310 the alignment. */
311 unsigned offset_factor
312 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
313 * BITS_PER_UNIT);
315 if (offset_factor)
316 inner = MIN (inner, (offset_factor & -offset_factor));
318 else if (offset)
319 inner = MIN (inner, BITS_PER_UNIT);
321 if (TREE_CODE (exp) == FUNCTION_DECL)
322 align = FUNCTION_BOUNDARY;
323 else if (DECL_P (exp))
324 align = MIN (inner, DECL_ALIGN (exp));
325 #ifdef CONSTANT_ALIGNMENT
326 else if (CONSTANT_CLASS_P (exp))
327 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
328 #endif
329 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
330 || TREE_CODE (exp) == INDIRECT_REF)
331 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
332 else
333 align = MIN (align, inner);
334 return MIN (align, max_align);
336 default:
337 return align;
342 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
343 way, because it could contain a zero byte in the middle.
344 TREE_STRING_LENGTH is the size of the character array, not the string.
346 ONLY_VALUE should be nonzero if the result is not going to be emitted
347 into the instruction stream and zero if it is going to be expanded.
348 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
349 is returned, otherwise NULL, since
350 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
351 evaluate the side-effects.
353 The value returned is of type `ssizetype'.
355 Unfortunately, string_constant can't access the values of const char
356 arrays with initializers, so neither can we do so here. */
358 tree
359 c_strlen (tree src, int only_value)
361 tree offset_node;
362 HOST_WIDE_INT offset;
363 int max;
364 const char *ptr;
366 STRIP_NOPS (src);
367 if (TREE_CODE (src) == COND_EXPR
368 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
370 tree len1, len2;
372 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
373 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
374 if (tree_int_cst_equal (len1, len2))
375 return len1;
378 if (TREE_CODE (src) == COMPOUND_EXPR
379 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
380 return c_strlen (TREE_OPERAND (src, 1), only_value);
382 src = string_constant (src, &offset_node);
383 if (src == 0)
384 return 0;
386 max = TREE_STRING_LENGTH (src) - 1;
387 ptr = TREE_STRING_POINTER (src);
389 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
391 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
392 compute the offset to the following null if we don't know where to
393 start searching for it. */
394 int i;
396 for (i = 0; i < max; i++)
397 if (ptr[i] == 0)
398 return 0;
400 /* We don't know the starting offset, but we do know that the string
401 has no internal zero bytes. We can assume that the offset falls
402 within the bounds of the string; otherwise, the programmer deserves
403 what he gets. Subtract the offset from the length of the string,
404 and return that. This would perhaps not be valid if we were dealing
405 with named arrays in addition to literal string constants. */
407 return size_diffop (size_int (max), offset_node);
410 /* We have a known offset into the string. Start searching there for
411 a null character if we can represent it as a single HOST_WIDE_INT. */
412 if (offset_node == 0)
413 offset = 0;
414 else if (! host_integerp (offset_node, 0))
415 offset = -1;
416 else
417 offset = tree_low_cst (offset_node, 0);
419 /* If the offset is known to be out of bounds, warn, and call strlen at
420 runtime. */
421 if (offset < 0 || offset > max)
423 warning (0, "offset outside bounds of constant string");
424 return 0;
427 /* Use strlen to search for the first zero byte. Since any strings
428 constructed with build_string will have nulls appended, we win even
429 if we get handed something like (char[4])"abcd".
431 Since OFFSET is our starting index into the string, no further
432 calculation is needed. */
433 return ssize_int (strlen (ptr + offset));
436 /* Return a char pointer for a C string if it is a string constant
437 or sum of string constant and integer constant. */
439 static const char *
440 c_getstr (tree src)
442 tree offset_node;
444 src = string_constant (src, &offset_node);
445 if (src == 0)
446 return 0;
448 if (offset_node == 0)
449 return TREE_STRING_POINTER (src);
450 else if (!host_integerp (offset_node, 1)
451 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
452 return 0;
454 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
457 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
458 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
460 static rtx
461 c_readstr (const char *str, enum machine_mode mode)
463 HOST_WIDE_INT c[2];
464 HOST_WIDE_INT ch;
465 unsigned int i, j;
467 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
469 c[0] = 0;
470 c[1] = 0;
471 ch = 1;
472 for (i = 0; i < GET_MODE_SIZE (mode); i++)
474 j = i;
475 if (WORDS_BIG_ENDIAN)
476 j = GET_MODE_SIZE (mode) - i - 1;
477 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
478 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
479 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
480 j *= BITS_PER_UNIT;
481 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
483 if (ch)
484 ch = (unsigned char) str[i];
485 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
487 return immed_double_const (c[0], c[1], mode);
490 /* Cast a target constant CST to target CHAR and if that value fits into
491 host char type, return zero and put that value into variable pointed to by
492 P. */
494 static int
495 target_char_cast (tree cst, char *p)
497 unsigned HOST_WIDE_INT val, hostval;
499 if (!host_integerp (cst, 1)
500 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
501 return 1;
503 val = tree_low_cst (cst, 1);
504 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
505 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
507 hostval = val;
508 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
509 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
511 if (val != hostval)
512 return 1;
514 *p = hostval;
515 return 0;
518 /* Similar to save_expr, but assumes that arbitrary code is not executed
519 in between the multiple evaluations. In particular, we assume that a
520 non-addressable local variable will not be modified. */
522 static tree
523 builtin_save_expr (tree exp)
525 if (TREE_ADDRESSABLE (exp) == 0
526 && (TREE_CODE (exp) == PARM_DECL
527 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
528 return exp;
530 return save_expr (exp);
533 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
534 times to get the address of either a higher stack frame, or a return
535 address located within it (depending on FNDECL_CODE). */
537 static rtx
538 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
540 int i;
542 #ifdef INITIAL_FRAME_ADDRESS_RTX
543 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
544 #else
545 rtx tem;
547 /* For a zero count with __builtin_return_address, we don't care what
548 frame address we return, because target-specific definitions will
549 override us. Therefore frame pointer elimination is OK, and using
550 the soft frame pointer is OK.
552 For a non-zero count, or a zero count with __builtin_frame_address,
553 we require a stable offset from the current frame pointer to the
554 previous one, so we must use the hard frame pointer, and
555 we must disable frame pointer elimination. */
556 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
557 tem = frame_pointer_rtx;
558 else
560 tem = hard_frame_pointer_rtx;
562 /* Tell reload not to eliminate the frame pointer. */
563 current_function_accesses_prior_frames = 1;
565 #endif
567 /* Some machines need special handling before we can access
568 arbitrary frames. For example, on the SPARC, we must first flush
569 all register windows to the stack. */
570 #ifdef SETUP_FRAME_ADDRESSES
571 if (count > 0)
572 SETUP_FRAME_ADDRESSES ();
573 #endif
575 /* On the SPARC, the return address is not in the frame, it is in a
576 register. There is no way to access it off of the current frame
577 pointer, but it can be accessed off the previous frame pointer by
578 reading the value from the register window save area. */
579 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
580 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
581 count--;
582 #endif
584 /* Scan back COUNT frames to the specified frame. */
585 for (i = 0; i < count; i++)
587 /* Assume the dynamic chain pointer is in the word that the
588 frame address points to, unless otherwise specified. */
589 #ifdef DYNAMIC_CHAIN_ADDRESS
590 tem = DYNAMIC_CHAIN_ADDRESS (tem);
591 #endif
592 tem = memory_address (Pmode, tem);
593 tem = gen_frame_mem (Pmode, tem);
594 tem = copy_to_reg (tem);
597 /* For __builtin_frame_address, return what we've got. But, on
598 the SPARC for example, we may have to add a bias. */
599 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
600 #ifdef FRAME_ADDR_RTX
601 return FRAME_ADDR_RTX (tem);
602 #else
603 return tem;
604 #endif
606 /* For __builtin_return_address, get the return address from that frame. */
607 #ifdef RETURN_ADDR_RTX
608 tem = RETURN_ADDR_RTX (count, tem);
609 #else
610 tem = memory_address (Pmode,
611 plus_constant (tem, GET_MODE_SIZE (Pmode)));
612 tem = gen_frame_mem (Pmode, tem);
613 #endif
614 return tem;
617 /* Alias set used for setjmp buffer. */
618 static HOST_WIDE_INT setjmp_alias_set = -1;
620 /* Construct the leading half of a __builtin_setjmp call. Control will
621 return to RECEIVER_LABEL. This is also called directly by the SJLJ
622 exception handling code. */
624 void
625 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
627 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
628 rtx stack_save;
629 rtx mem;
631 if (setjmp_alias_set == -1)
632 setjmp_alias_set = new_alias_set ();
634 buf_addr = convert_memory_address (Pmode, buf_addr);
636 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
638 /* We store the frame pointer and the address of receiver_label in
639 the buffer and use the rest of it for the stack save area, which
640 is machine-dependent. */
642 mem = gen_rtx_MEM (Pmode, buf_addr);
643 set_mem_alias_set (mem, setjmp_alias_set);
644 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
646 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
647 set_mem_alias_set (mem, setjmp_alias_set);
649 emit_move_insn (validize_mem (mem),
650 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
652 stack_save = gen_rtx_MEM (sa_mode,
653 plus_constant (buf_addr,
654 2 * GET_MODE_SIZE (Pmode)));
655 set_mem_alias_set (stack_save, setjmp_alias_set);
656 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
658 /* If there is further processing to do, do it. */
659 #ifdef HAVE_builtin_setjmp_setup
660 if (HAVE_builtin_setjmp_setup)
661 emit_insn (gen_builtin_setjmp_setup (buf_addr));
662 #endif
664 /* Tell optimize_save_area_alloca that extra work is going to
665 need to go on during alloca. */
666 current_function_calls_setjmp = 1;
668 /* Set this so all the registers get saved in our frame; we need to be
669 able to copy the saved values for any registers from frames we unwind. */
670 current_function_has_nonlocal_label = 1;
673 /* Construct the trailing part of a __builtin_setjmp call. This is
674 also called directly by the SJLJ exception handling code. */
676 void
677 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
679 /* Clobber the FP when we get here, so we have to make sure it's
680 marked as used by this function. */
681 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
683 /* Mark the static chain as clobbered here so life information
684 doesn't get messed up for it. */
685 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
687 /* Now put in the code to restore the frame pointer, and argument
688 pointer, if needed. */
689 #ifdef HAVE_nonlocal_goto
690 if (! HAVE_nonlocal_goto)
691 #endif
693 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
694 /* This might change the hard frame pointer in ways that aren't
695 apparent to early optimization passes, so force a clobber. */
696 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
699 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
700 if (fixed_regs[ARG_POINTER_REGNUM])
702 #ifdef ELIMINABLE_REGS
703 size_t i;
704 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
706 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
707 if (elim_regs[i].from == ARG_POINTER_REGNUM
708 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
709 break;
711 if (i == ARRAY_SIZE (elim_regs))
712 #endif
714 /* Now restore our arg pointer from the address at which it
715 was saved in our stack frame. */
716 emit_move_insn (virtual_incoming_args_rtx,
717 copy_to_reg (get_arg_pointer_save_area (cfun)));
720 #endif
722 #ifdef HAVE_builtin_setjmp_receiver
723 if (HAVE_builtin_setjmp_receiver)
724 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
725 else
726 #endif
727 #ifdef HAVE_nonlocal_goto_receiver
728 if (HAVE_nonlocal_goto_receiver)
729 emit_insn (gen_nonlocal_goto_receiver ());
730 else
731 #endif
732 { /* Nothing */ }
734 /* @@@ This is a kludge. Not all machine descriptions define a blockage
735 insn, but we must not allow the code we just generated to be reordered
736 by scheduling. Specifically, the update of the frame pointer must
737 happen immediately, not later. So emit an ASM_INPUT to act as blockage
738 insn. */
739 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
742 /* __builtin_longjmp is passed a pointer to an array of five words (not
743 all will be used on all machines). It operates similarly to the C
744 library function of the same name, but is more efficient. Much of
745 the code below is copied from the handling of non-local gotos. */
747 static void
748 expand_builtin_longjmp (rtx buf_addr, rtx value)
750 rtx fp, lab, stack, insn, last;
751 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
753 if (setjmp_alias_set == -1)
754 setjmp_alias_set = new_alias_set ();
756 buf_addr = convert_memory_address (Pmode, buf_addr);
758 buf_addr = force_reg (Pmode, buf_addr);
760 /* We used to store value in static_chain_rtx, but that fails if pointers
761 are smaller than integers. We instead require that the user must pass
762 a second argument of 1, because that is what builtin_setjmp will
763 return. This also makes EH slightly more efficient, since we are no
764 longer copying around a value that we don't care about. */
765 gcc_assert (value == const1_rtx);
767 last = get_last_insn ();
768 #ifdef HAVE_builtin_longjmp
769 if (HAVE_builtin_longjmp)
770 emit_insn (gen_builtin_longjmp (buf_addr));
771 else
772 #endif
774 fp = gen_rtx_MEM (Pmode, buf_addr);
775 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
776 GET_MODE_SIZE (Pmode)));
778 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
779 2 * GET_MODE_SIZE (Pmode)));
780 set_mem_alias_set (fp, setjmp_alias_set);
781 set_mem_alias_set (lab, setjmp_alias_set);
782 set_mem_alias_set (stack, setjmp_alias_set);
784 /* Pick up FP, label, and SP from the block and jump. This code is
785 from expand_goto in stmt.c; see there for detailed comments. */
786 #ifdef HAVE_nonlocal_goto
787 if (HAVE_nonlocal_goto)
788 /* We have to pass a value to the nonlocal_goto pattern that will
789 get copied into the static_chain pointer, but it does not matter
790 what that value is, because builtin_setjmp does not use it. */
791 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
792 else
793 #endif
795 lab = copy_to_reg (lab);
797 emit_insn (gen_rtx_CLOBBER (VOIDmode,
798 gen_rtx_MEM (BLKmode,
799 gen_rtx_SCRATCH (VOIDmode))));
800 emit_insn (gen_rtx_CLOBBER (VOIDmode,
801 gen_rtx_MEM (BLKmode,
802 hard_frame_pointer_rtx)));
804 emit_move_insn (hard_frame_pointer_rtx, fp);
805 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
807 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
808 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
809 emit_indirect_jump (lab);
813 /* Search backwards and mark the jump insn as a non-local goto.
814 Note that this precludes the use of __builtin_longjmp to a
815 __builtin_setjmp target in the same function. However, we've
816 already cautioned the user that these functions are for
817 internal exception handling use only. */
818 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
820 gcc_assert (insn != last);
822 if (JUMP_P (insn))
824 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
825 REG_NOTES (insn));
826 break;
828 else if (CALL_P (insn))
829 break;
833 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
834 and the address of the save area. */
836 static rtx
837 expand_builtin_nonlocal_goto (tree arglist)
839 tree t_label, t_save_area;
840 rtx r_label, r_save_area, r_fp, r_sp, insn;
842 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
843 return NULL_RTX;
845 t_label = TREE_VALUE (arglist);
846 arglist = TREE_CHAIN (arglist);
847 t_save_area = TREE_VALUE (arglist);
849 r_label = expand_normal (t_label);
850 r_label = convert_memory_address (Pmode, r_label);
851 r_save_area = expand_normal (t_save_area);
852 r_save_area = convert_memory_address (Pmode, r_save_area);
853 r_fp = gen_rtx_MEM (Pmode, r_save_area);
854 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
855 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
857 current_function_has_nonlocal_goto = 1;
859 #ifdef HAVE_nonlocal_goto
860 /* ??? We no longer need to pass the static chain value, afaik. */
861 if (HAVE_nonlocal_goto)
862 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
863 else
864 #endif
866 r_label = copy_to_reg (r_label);
868 emit_insn (gen_rtx_CLOBBER (VOIDmode,
869 gen_rtx_MEM (BLKmode,
870 gen_rtx_SCRATCH (VOIDmode))));
872 emit_insn (gen_rtx_CLOBBER (VOIDmode,
873 gen_rtx_MEM (BLKmode,
874 hard_frame_pointer_rtx)));
876 /* Restore frame pointer for containing function.
877 This sets the actual hard register used for the frame pointer
878 to the location of the function's incoming static chain info.
879 The non-local goto handler will then adjust it to contain the
880 proper value and reload the argument pointer, if needed. */
881 emit_move_insn (hard_frame_pointer_rtx, r_fp);
882 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
884 /* USE of hard_frame_pointer_rtx added for consistency;
885 not clear if really needed. */
886 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
887 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
888 emit_indirect_jump (r_label);
891 /* Search backwards to the jump insn and mark it as a
892 non-local goto. */
893 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
895 if (JUMP_P (insn))
897 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
898 const0_rtx, REG_NOTES (insn));
899 break;
901 else if (CALL_P (insn))
902 break;
905 return const0_rtx;
908 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
909 (not all will be used on all machines) that was passed to __builtin_setjmp.
910 It updates the stack pointer in that block to correspond to the current
911 stack pointer. */
913 static void
914 expand_builtin_update_setjmp_buf (rtx buf_addr)
916 enum machine_mode sa_mode = Pmode;
917 rtx stack_save;
920 #ifdef HAVE_save_stack_nonlocal
921 if (HAVE_save_stack_nonlocal)
922 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
923 #endif
924 #ifdef STACK_SAVEAREA_MODE
925 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
926 #endif
928 stack_save
929 = gen_rtx_MEM (sa_mode,
930 memory_address
931 (sa_mode,
932 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
934 #ifdef HAVE_setjmp
935 if (HAVE_setjmp)
936 emit_insn (gen_setjmp ());
937 #endif
939 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
942 /* Expand a call to __builtin_prefetch. For a target that does not support
943 data prefetch, evaluate the memory address argument in case it has side
944 effects. */
946 static void
947 expand_builtin_prefetch (tree arglist)
949 tree arg0, arg1, arg2;
950 rtx op0, op1, op2;
952 if (!validate_arglist (arglist, POINTER_TYPE, 0))
953 return;
955 arg0 = TREE_VALUE (arglist);
956 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
957 zero (read) and argument 2 (locality) defaults to 3 (high degree of
958 locality). */
959 if (TREE_CHAIN (arglist))
961 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
962 if (TREE_CHAIN (TREE_CHAIN (arglist)))
963 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
964 else
965 arg2 = build_int_cst (NULL_TREE, 3);
967 else
969 arg1 = integer_zero_node;
970 arg2 = build_int_cst (NULL_TREE, 3);
973 /* Argument 0 is an address. */
974 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
976 /* Argument 1 (read/write flag) must be a compile-time constant int. */
977 if (TREE_CODE (arg1) != INTEGER_CST)
979 error ("second argument to %<__builtin_prefetch%> must be a constant");
980 arg1 = integer_zero_node;
982 op1 = expand_normal (arg1);
983 /* Argument 1 must be either zero or one. */
984 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
986 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
987 " using zero");
988 op1 = const0_rtx;
991 /* Argument 2 (locality) must be a compile-time constant int. */
992 if (TREE_CODE (arg2) != INTEGER_CST)
994 error ("third argument to %<__builtin_prefetch%> must be a constant");
995 arg2 = integer_zero_node;
997 op2 = expand_normal (arg2);
998 /* Argument 2 must be 0, 1, 2, or 3. */
999 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1001 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1002 op2 = const0_rtx;
1005 #ifdef HAVE_prefetch
1006 if (HAVE_prefetch)
1008 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1009 (op0,
1010 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1011 || (GET_MODE (op0) != Pmode))
1013 op0 = convert_memory_address (Pmode, op0);
1014 op0 = force_reg (Pmode, op0);
1016 emit_insn (gen_prefetch (op0, op1, op2));
1018 #endif
1020 /* Don't do anything with direct references to volatile memory, but
1021 generate code to handle other side effects. */
1022 if (!MEM_P (op0) && side_effects_p (op0))
1023 emit_insn (op0);
1026 /* Get a MEM rtx for expression EXP which is the address of an operand
1027 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1028 the maximum length of the block of memory that might be accessed or
1029 NULL if unknown. */
1031 static rtx
1032 get_memory_rtx (tree exp, tree len)
1034 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1035 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1037 /* Get an expression we can use to find the attributes to assign to MEM.
1038 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1039 we can. First remove any nops. */
1040 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1041 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1042 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1043 exp = TREE_OPERAND (exp, 0);
1045 if (TREE_CODE (exp) == ADDR_EXPR)
1046 exp = TREE_OPERAND (exp, 0);
1047 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1048 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1049 else
1050 exp = NULL;
1052 /* Honor attributes derived from exp, except for the alias set
1053 (as builtin stringops may alias with anything) and the size
1054 (as stringops may access multiple array elements). */
1055 if (exp)
1057 set_mem_attributes (mem, exp, 0);
1059 /* Allow the string and memory builtins to overflow from one
1060 field into another, see http://gcc.gnu.org/PR23561.
1061 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1062 memory accessed by the string or memory builtin will fit
1063 within the field. */
1064 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1066 tree mem_expr = MEM_EXPR (mem);
1067 HOST_WIDE_INT offset = -1, length = -1;
1068 tree inner = exp;
1070 while (TREE_CODE (inner) == ARRAY_REF
1071 || TREE_CODE (inner) == NOP_EXPR
1072 || TREE_CODE (inner) == CONVERT_EXPR
1073 || TREE_CODE (inner) == NON_LVALUE_EXPR
1074 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1075 || TREE_CODE (inner) == SAVE_EXPR)
1076 inner = TREE_OPERAND (inner, 0);
1078 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1080 if (MEM_OFFSET (mem)
1081 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1082 offset = INTVAL (MEM_OFFSET (mem));
1084 if (offset >= 0 && len && host_integerp (len, 0))
1085 length = tree_low_cst (len, 0);
1087 while (TREE_CODE (inner) == COMPONENT_REF)
1089 tree field = TREE_OPERAND (inner, 1);
1090 gcc_assert (! DECL_BIT_FIELD (field));
1091 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1092 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1094 if (length >= 0
1095 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1096 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1098 HOST_WIDE_INT size
1099 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1100 /* If we can prove the memory starting at XEXP (mem, 0)
1101 and ending at XEXP (mem, 0) + LENGTH will fit into
1102 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1103 if (offset <= size
1104 && length <= size
1105 && offset + length <= size)
1106 break;
1109 if (offset >= 0
1110 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1111 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1112 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1113 / BITS_PER_UNIT;
1114 else
1116 offset = -1;
1117 length = -1;
1120 mem_expr = TREE_OPERAND (mem_expr, 0);
1121 inner = TREE_OPERAND (inner, 0);
1124 if (mem_expr == NULL)
1125 offset = -1;
1126 if (mem_expr != MEM_EXPR (mem))
1128 set_mem_expr (mem, mem_expr);
1129 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1132 set_mem_alias_set (mem, 0);
1133 set_mem_size (mem, NULL_RTX);
1136 return mem;
1139 /* Built-in functions to perform an untyped call and return. */
1141 /* For each register that may be used for calling a function, this
1142 gives a mode used to copy the register's value. VOIDmode indicates
1143 the register is not used for calling a function. If the machine
1144 has register windows, this gives only the outbound registers.
1145 INCOMING_REGNO gives the corresponding inbound register. */
1146 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1148 /* For each register that may be used for returning values, this gives
1149 a mode used to copy the register's value. VOIDmode indicates the
1150 register is not used for returning values. If the machine has
1151 register windows, this gives only the outbound registers.
1152 INCOMING_REGNO gives the corresponding inbound register. */
1153 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1155 /* For each register that may be used for calling a function, this
1156 gives the offset of that register into the block returned by
1157 __builtin_apply_args. 0 indicates that the register is not
1158 used for calling a function. */
1159 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1161 /* Return the size required for the block returned by __builtin_apply_args,
1162 and initialize apply_args_mode. */
1164 static int
1165 apply_args_size (void)
1167 static int size = -1;
1168 int align;
1169 unsigned int regno;
1170 enum machine_mode mode;
1172 /* The values computed by this function never change. */
1173 if (size < 0)
1175 /* The first value is the incoming arg-pointer. */
1176 size = GET_MODE_SIZE (Pmode);
1178 /* The second value is the structure value address unless this is
1179 passed as an "invisible" first argument. */
1180 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1181 size += GET_MODE_SIZE (Pmode);
1183 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1184 if (FUNCTION_ARG_REGNO_P (regno))
1186 mode = reg_raw_mode[regno];
1188 gcc_assert (mode != VOIDmode);
1190 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1191 if (size % align != 0)
1192 size = CEIL (size, align) * align;
1193 apply_args_reg_offset[regno] = size;
1194 size += GET_MODE_SIZE (mode);
1195 apply_args_mode[regno] = mode;
1197 else
1199 apply_args_mode[regno] = VOIDmode;
1200 apply_args_reg_offset[regno] = 0;
1203 return size;
1206 /* Return the size required for the block returned by __builtin_apply,
1207 and initialize apply_result_mode. */
1209 static int
1210 apply_result_size (void)
1212 static int size = -1;
1213 int align, regno;
1214 enum machine_mode mode;
1216 /* The values computed by this function never change. */
1217 if (size < 0)
1219 size = 0;
1221 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1222 if (FUNCTION_VALUE_REGNO_P (regno))
1224 mode = reg_raw_mode[regno];
1226 gcc_assert (mode != VOIDmode);
1228 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1229 if (size % align != 0)
1230 size = CEIL (size, align) * align;
1231 size += GET_MODE_SIZE (mode);
1232 apply_result_mode[regno] = mode;
1234 else
1235 apply_result_mode[regno] = VOIDmode;
1237 /* Allow targets that use untyped_call and untyped_return to override
1238 the size so that machine-specific information can be stored here. */
1239 #ifdef APPLY_RESULT_SIZE
1240 size = APPLY_RESULT_SIZE;
1241 #endif
1243 return size;
1246 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1247 /* Create a vector describing the result block RESULT. If SAVEP is true,
1248 the result block is used to save the values; otherwise it is used to
1249 restore the values. */
1251 static rtx
1252 result_vector (int savep, rtx result)
1254 int regno, size, align, nelts;
1255 enum machine_mode mode;
1256 rtx reg, mem;
1257 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1259 size = nelts = 0;
1260 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1261 if ((mode = apply_result_mode[regno]) != VOIDmode)
1263 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1264 if (size % align != 0)
1265 size = CEIL (size, align) * align;
1266 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1267 mem = adjust_address (result, mode, size);
1268 savevec[nelts++] = (savep
1269 ? gen_rtx_SET (VOIDmode, mem, reg)
1270 : gen_rtx_SET (VOIDmode, reg, mem));
1271 size += GET_MODE_SIZE (mode);
1273 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1275 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1277 /* Save the state required to perform an untyped call with the same
1278 arguments as were passed to the current function. */
1280 static rtx
1281 expand_builtin_apply_args_1 (void)
1283 rtx registers, tem;
1284 int size, align, regno;
1285 enum machine_mode mode;
1286 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1288 /* Create a block where the arg-pointer, structure value address,
1289 and argument registers can be saved. */
1290 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1292 /* Walk past the arg-pointer and structure value address. */
1293 size = GET_MODE_SIZE (Pmode);
1294 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1295 size += GET_MODE_SIZE (Pmode);
1297 /* Save each register used in calling a function to the block. */
1298 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1299 if ((mode = apply_args_mode[regno]) != VOIDmode)
1301 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1302 if (size % align != 0)
1303 size = CEIL (size, align) * align;
1305 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1307 emit_move_insn (adjust_address (registers, mode, size), tem);
1308 size += GET_MODE_SIZE (mode);
1311 /* Save the arg pointer to the block. */
1312 tem = copy_to_reg (virtual_incoming_args_rtx);
1313 #ifdef STACK_GROWS_DOWNWARD
1314 /* We need the pointer as the caller actually passed them to us, not
1315 as we might have pretended they were passed. Make sure it's a valid
1316 operand, as emit_move_insn isn't expected to handle a PLUS. */
1318 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1319 NULL_RTX);
1320 #endif
1321 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1323 size = GET_MODE_SIZE (Pmode);
1325 /* Save the structure value address unless this is passed as an
1326 "invisible" first argument. */
1327 if (struct_incoming_value)
1329 emit_move_insn (adjust_address (registers, Pmode, size),
1330 copy_to_reg (struct_incoming_value));
1331 size += GET_MODE_SIZE (Pmode);
1334 /* Return the address of the block. */
1335 return copy_addr_to_reg (XEXP (registers, 0));
1338 /* __builtin_apply_args returns block of memory allocated on
1339 the stack into which is stored the arg pointer, structure
1340 value address, static chain, and all the registers that might
1341 possibly be used in performing a function call. The code is
1342 moved to the start of the function so the incoming values are
1343 saved. */
1345 static rtx
1346 expand_builtin_apply_args (void)
1348 /* Don't do __builtin_apply_args more than once in a function.
1349 Save the result of the first call and reuse it. */
1350 if (apply_args_value != 0)
1351 return apply_args_value;
1353 /* When this function is called, it means that registers must be
1354 saved on entry to this function. So we migrate the
1355 call to the first insn of this function. */
1356 rtx temp;
1357 rtx seq;
1359 start_sequence ();
1360 temp = expand_builtin_apply_args_1 ();
1361 seq = get_insns ();
1362 end_sequence ();
1364 apply_args_value = temp;
1366 /* Put the insns after the NOTE that starts the function.
1367 If this is inside a start_sequence, make the outer-level insn
1368 chain current, so the code is placed at the start of the
1369 function. */
1370 push_topmost_sequence ();
1371 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1372 pop_topmost_sequence ();
1373 return temp;
1377 /* Perform an untyped call and save the state required to perform an
1378 untyped return of whatever value was returned by the given function. */
1380 static rtx
1381 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1383 int size, align, regno;
1384 enum machine_mode mode;
1385 rtx incoming_args, result, reg, dest, src, call_insn;
1386 rtx old_stack_level = 0;
1387 rtx call_fusage = 0;
1388 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1390 arguments = convert_memory_address (Pmode, arguments);
1392 /* Create a block where the return registers can be saved. */
1393 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1395 /* Fetch the arg pointer from the ARGUMENTS block. */
1396 incoming_args = gen_reg_rtx (Pmode);
1397 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1398 #ifndef STACK_GROWS_DOWNWARD
1399 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1400 incoming_args, 0, OPTAB_LIB_WIDEN);
1401 #endif
1403 /* Push a new argument block and copy the arguments. Do not allow
1404 the (potential) memcpy call below to interfere with our stack
1405 manipulations. */
1406 do_pending_stack_adjust ();
1407 NO_DEFER_POP;
1409 /* Save the stack with nonlocal if available. */
1410 #ifdef HAVE_save_stack_nonlocal
1411 if (HAVE_save_stack_nonlocal)
1412 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1413 else
1414 #endif
1415 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1417 /* Allocate a block of memory onto the stack and copy the memory
1418 arguments to the outgoing arguments address. */
1419 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1420 dest = virtual_outgoing_args_rtx;
1421 #ifndef STACK_GROWS_DOWNWARD
1422 if (GET_CODE (argsize) == CONST_INT)
1423 dest = plus_constant (dest, -INTVAL (argsize));
1424 else
1425 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1426 #endif
1427 dest = gen_rtx_MEM (BLKmode, dest);
1428 set_mem_align (dest, PARM_BOUNDARY);
1429 src = gen_rtx_MEM (BLKmode, incoming_args);
1430 set_mem_align (src, PARM_BOUNDARY);
1431 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1433 /* Refer to the argument block. */
1434 apply_args_size ();
1435 arguments = gen_rtx_MEM (BLKmode, arguments);
1436 set_mem_align (arguments, PARM_BOUNDARY);
1438 /* Walk past the arg-pointer and structure value address. */
1439 size = GET_MODE_SIZE (Pmode);
1440 if (struct_value)
1441 size += GET_MODE_SIZE (Pmode);
1443 /* Restore each of the registers previously saved. Make USE insns
1444 for each of these registers for use in making the call. */
1445 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1446 if ((mode = apply_args_mode[regno]) != VOIDmode)
1448 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1449 if (size % align != 0)
1450 size = CEIL (size, align) * align;
1451 reg = gen_rtx_REG (mode, regno);
1452 emit_move_insn (reg, adjust_address (arguments, mode, size));
1453 use_reg (&call_fusage, reg);
1454 size += GET_MODE_SIZE (mode);
1457 /* Restore the structure value address unless this is passed as an
1458 "invisible" first argument. */
1459 size = GET_MODE_SIZE (Pmode);
1460 if (struct_value)
1462 rtx value = gen_reg_rtx (Pmode);
1463 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1464 emit_move_insn (struct_value, value);
1465 if (REG_P (struct_value))
1466 use_reg (&call_fusage, struct_value);
1467 size += GET_MODE_SIZE (Pmode);
1470 /* All arguments and registers used for the call are set up by now! */
1471 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1473 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1474 and we don't want to load it into a register as an optimization,
1475 because prepare_call_address already did it if it should be done. */
1476 if (GET_CODE (function) != SYMBOL_REF)
1477 function = memory_address (FUNCTION_MODE, function);
1479 /* Generate the actual call instruction and save the return value. */
1480 #ifdef HAVE_untyped_call
1481 if (HAVE_untyped_call)
1482 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1483 result, result_vector (1, result)));
1484 else
1485 #endif
1486 #ifdef HAVE_call_value
1487 if (HAVE_call_value)
1489 rtx valreg = 0;
1491 /* Locate the unique return register. It is not possible to
1492 express a call that sets more than one return register using
1493 call_value; use untyped_call for that. In fact, untyped_call
1494 only needs to save the return registers in the given block. */
1495 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1496 if ((mode = apply_result_mode[regno]) != VOIDmode)
1498 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1500 valreg = gen_rtx_REG (mode, regno);
1503 emit_call_insn (GEN_CALL_VALUE (valreg,
1504 gen_rtx_MEM (FUNCTION_MODE, function),
1505 const0_rtx, NULL_RTX, const0_rtx));
1507 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1509 else
1510 #endif
1511 gcc_unreachable ();
1513 /* Find the CALL insn we just emitted, and attach the register usage
1514 information. */
1515 call_insn = last_call_insn ();
1516 add_function_usage_to (call_insn, call_fusage);
1518 /* Restore the stack. */
1519 #ifdef HAVE_save_stack_nonlocal
1520 if (HAVE_save_stack_nonlocal)
1521 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1522 else
1523 #endif
1524 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1526 OK_DEFER_POP;
1528 /* Return the address of the result block. */
1529 result = copy_addr_to_reg (XEXP (result, 0));
1530 return convert_memory_address (ptr_mode, result);
1533 /* Perform an untyped return. */
1535 static void
1536 expand_builtin_return (rtx result)
1538 int size, align, regno;
1539 enum machine_mode mode;
1540 rtx reg;
1541 rtx call_fusage = 0;
1543 result = convert_memory_address (Pmode, result);
1545 apply_result_size ();
1546 result = gen_rtx_MEM (BLKmode, result);
1548 #ifdef HAVE_untyped_return
1549 if (HAVE_untyped_return)
1551 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1552 emit_barrier ();
1553 return;
1555 #endif
1557 /* Restore the return value and note that each value is used. */
1558 size = 0;
1559 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1560 if ((mode = apply_result_mode[regno]) != VOIDmode)
1562 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1563 if (size % align != 0)
1564 size = CEIL (size, align) * align;
1565 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1566 emit_move_insn (reg, adjust_address (result, mode, size));
1568 push_to_sequence (call_fusage);
1569 emit_insn (gen_rtx_USE (VOIDmode, reg));
1570 call_fusage = get_insns ();
1571 end_sequence ();
1572 size += GET_MODE_SIZE (mode);
1575 /* Put the USE insns before the return. */
1576 emit_insn (call_fusage);
1578 /* Return whatever values was restored by jumping directly to the end
1579 of the function. */
1580 expand_naked_return ();
1583 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1585 static enum type_class
1586 type_to_class (tree type)
1588 switch (TREE_CODE (type))
1590 case VOID_TYPE: return void_type_class;
1591 case INTEGER_TYPE: return integer_type_class;
1592 case ENUMERAL_TYPE: return enumeral_type_class;
1593 case BOOLEAN_TYPE: return boolean_type_class;
1594 case POINTER_TYPE: return pointer_type_class;
1595 case REFERENCE_TYPE: return reference_type_class;
1596 case OFFSET_TYPE: return offset_type_class;
1597 case REAL_TYPE: return real_type_class;
1598 case COMPLEX_TYPE: return complex_type_class;
1599 case FUNCTION_TYPE: return function_type_class;
1600 case METHOD_TYPE: return method_type_class;
1601 case RECORD_TYPE: return record_type_class;
1602 case UNION_TYPE:
1603 case QUAL_UNION_TYPE: return union_type_class;
1604 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1605 ? string_type_class : array_type_class);
1606 case LANG_TYPE: return lang_type_class;
1607 default: return no_type_class;
1611 /* Expand a call to __builtin_classify_type with arguments found in
1612 ARGLIST. */
1614 static rtx
1615 expand_builtin_classify_type (tree arglist)
1617 if (arglist != 0)
1618 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1619 return GEN_INT (no_type_class);
1622 /* This helper macro, meant to be used in mathfn_built_in below,
1623 determines which among a set of three builtin math functions is
1624 appropriate for a given type mode. The `F' and `L' cases are
1625 automatically generated from the `double' case. */
1626 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1627 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1628 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1629 fcodel = BUILT_IN_MATHFN##L ; break;
1631 /* Return mathematic function equivalent to FN but operating directly
1632 on TYPE, if available. If we can't do the conversion, return zero. */
1633 tree
1634 mathfn_built_in (tree type, enum built_in_function fn)
1636 enum built_in_function fcode, fcodef, fcodel;
1638 switch (fn)
1640 CASE_MATHFN (BUILT_IN_ACOS)
1641 CASE_MATHFN (BUILT_IN_ACOSH)
1642 CASE_MATHFN (BUILT_IN_ASIN)
1643 CASE_MATHFN (BUILT_IN_ASINH)
1644 CASE_MATHFN (BUILT_IN_ATAN)
1645 CASE_MATHFN (BUILT_IN_ATAN2)
1646 CASE_MATHFN (BUILT_IN_ATANH)
1647 CASE_MATHFN (BUILT_IN_CBRT)
1648 CASE_MATHFN (BUILT_IN_CEIL)
1649 CASE_MATHFN (BUILT_IN_COPYSIGN)
1650 CASE_MATHFN (BUILT_IN_COS)
1651 CASE_MATHFN (BUILT_IN_COSH)
1652 CASE_MATHFN (BUILT_IN_DREM)
1653 CASE_MATHFN (BUILT_IN_ERF)
1654 CASE_MATHFN (BUILT_IN_ERFC)
1655 CASE_MATHFN (BUILT_IN_EXP)
1656 CASE_MATHFN (BUILT_IN_EXP10)
1657 CASE_MATHFN (BUILT_IN_EXP2)
1658 CASE_MATHFN (BUILT_IN_EXPM1)
1659 CASE_MATHFN (BUILT_IN_FABS)
1660 CASE_MATHFN (BUILT_IN_FDIM)
1661 CASE_MATHFN (BUILT_IN_FLOOR)
1662 CASE_MATHFN (BUILT_IN_FMA)
1663 CASE_MATHFN (BUILT_IN_FMAX)
1664 CASE_MATHFN (BUILT_IN_FMIN)
1665 CASE_MATHFN (BUILT_IN_FMOD)
1666 CASE_MATHFN (BUILT_IN_FREXP)
1667 CASE_MATHFN (BUILT_IN_GAMMA)
1668 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1669 CASE_MATHFN (BUILT_IN_HYPOT)
1670 CASE_MATHFN (BUILT_IN_ILOGB)
1671 CASE_MATHFN (BUILT_IN_INF)
1672 CASE_MATHFN (BUILT_IN_J0)
1673 CASE_MATHFN (BUILT_IN_J1)
1674 CASE_MATHFN (BUILT_IN_JN)
1675 CASE_MATHFN (BUILT_IN_LCEIL)
1676 CASE_MATHFN (BUILT_IN_LDEXP)
1677 CASE_MATHFN (BUILT_IN_LFLOOR)
1678 CASE_MATHFN (BUILT_IN_LGAMMA)
1679 CASE_MATHFN (BUILT_IN_LLCEIL)
1680 CASE_MATHFN (BUILT_IN_LLFLOOR)
1681 CASE_MATHFN (BUILT_IN_LLRINT)
1682 CASE_MATHFN (BUILT_IN_LLROUND)
1683 CASE_MATHFN (BUILT_IN_LOG)
1684 CASE_MATHFN (BUILT_IN_LOG10)
1685 CASE_MATHFN (BUILT_IN_LOG1P)
1686 CASE_MATHFN (BUILT_IN_LOG2)
1687 CASE_MATHFN (BUILT_IN_LOGB)
1688 CASE_MATHFN (BUILT_IN_LRINT)
1689 CASE_MATHFN (BUILT_IN_LROUND)
1690 CASE_MATHFN (BUILT_IN_MODF)
1691 CASE_MATHFN (BUILT_IN_NAN)
1692 CASE_MATHFN (BUILT_IN_NANS)
1693 CASE_MATHFN (BUILT_IN_NEARBYINT)
1694 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1695 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1696 CASE_MATHFN (BUILT_IN_POW)
1697 CASE_MATHFN (BUILT_IN_POWI)
1698 CASE_MATHFN (BUILT_IN_POW10)
1699 CASE_MATHFN (BUILT_IN_REMAINDER)
1700 CASE_MATHFN (BUILT_IN_REMQUO)
1701 CASE_MATHFN (BUILT_IN_RINT)
1702 CASE_MATHFN (BUILT_IN_ROUND)
1703 CASE_MATHFN (BUILT_IN_SCALB)
1704 CASE_MATHFN (BUILT_IN_SCALBLN)
1705 CASE_MATHFN (BUILT_IN_SCALBN)
1706 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1707 CASE_MATHFN (BUILT_IN_SIN)
1708 CASE_MATHFN (BUILT_IN_SINCOS)
1709 CASE_MATHFN (BUILT_IN_SINH)
1710 CASE_MATHFN (BUILT_IN_SQRT)
1711 CASE_MATHFN (BUILT_IN_TAN)
1712 CASE_MATHFN (BUILT_IN_TANH)
1713 CASE_MATHFN (BUILT_IN_TGAMMA)
1714 CASE_MATHFN (BUILT_IN_TRUNC)
1715 CASE_MATHFN (BUILT_IN_Y0)
1716 CASE_MATHFN (BUILT_IN_Y1)
1717 CASE_MATHFN (BUILT_IN_YN)
1719 default:
1720 return 0;
1723 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1724 return implicit_built_in_decls[fcode];
1725 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1726 return implicit_built_in_decls[fcodef];
1727 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1728 return implicit_built_in_decls[fcodel];
1729 else
1730 return 0;
1733 /* If errno must be maintained, expand the RTL to check if the result,
1734 TARGET, of a built-in function call, EXP, is NaN, and if so set
1735 errno to EDOM. */
1737 static void
1738 expand_errno_check (tree exp, rtx target)
1740 rtx lab = gen_label_rtx ();
1742 /* Test the result; if it is NaN, set errno=EDOM because
1743 the argument was not in the domain. */
1744 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1745 0, lab);
1747 #ifdef TARGET_EDOM
1748 /* If this built-in doesn't throw an exception, set errno directly. */
1749 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1751 #ifdef GEN_ERRNO_RTX
1752 rtx errno_rtx = GEN_ERRNO_RTX;
1753 #else
1754 rtx errno_rtx
1755 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1756 #endif
1757 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1758 emit_label (lab);
1759 return;
1761 #endif
1763 /* We can't set errno=EDOM directly; let the library call do it.
1764 Pop the arguments right away in case the call gets deleted. */
1765 NO_DEFER_POP;
1766 expand_call (exp, target, 0);
1767 OK_DEFER_POP;
1768 emit_label (lab);
1772 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1773 Return 0 if a normal call should be emitted rather than expanding the
1774 function in-line. EXP is the expression that is a call to the builtin
1775 function; if convenient, the result should be placed in TARGET.
1776 SUBTARGET may be used as the target for computing one of EXP's operands. */
1778 static rtx
1779 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1781 optab builtin_optab;
1782 rtx op0, insns, before_call;
1783 tree fndecl = get_callee_fndecl (exp);
1784 tree arglist = TREE_OPERAND (exp, 1);
1785 enum machine_mode mode;
1786 bool errno_set = false;
1787 tree arg, narg;
1789 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1790 return 0;
1792 arg = TREE_VALUE (arglist);
1794 switch (DECL_FUNCTION_CODE (fndecl))
1796 CASE_FLT_FN (BUILT_IN_SQRT):
1797 errno_set = ! tree_expr_nonnegative_p (arg);
1798 builtin_optab = sqrt_optab;
1799 break;
1800 CASE_FLT_FN (BUILT_IN_EXP):
1801 errno_set = true; builtin_optab = exp_optab; break;
1802 CASE_FLT_FN (BUILT_IN_EXP10):
1803 CASE_FLT_FN (BUILT_IN_POW10):
1804 errno_set = true; builtin_optab = exp10_optab; break;
1805 CASE_FLT_FN (BUILT_IN_EXP2):
1806 errno_set = true; builtin_optab = exp2_optab; break;
1807 CASE_FLT_FN (BUILT_IN_EXPM1):
1808 errno_set = true; builtin_optab = expm1_optab; break;
1809 CASE_FLT_FN (BUILT_IN_LOGB):
1810 errno_set = true; builtin_optab = logb_optab; break;
1811 CASE_FLT_FN (BUILT_IN_ILOGB):
1812 errno_set = true; builtin_optab = ilogb_optab; break;
1813 CASE_FLT_FN (BUILT_IN_LOG):
1814 errno_set = true; builtin_optab = log_optab; break;
1815 CASE_FLT_FN (BUILT_IN_LOG10):
1816 errno_set = true; builtin_optab = log10_optab; break;
1817 CASE_FLT_FN (BUILT_IN_LOG2):
1818 errno_set = true; builtin_optab = log2_optab; break;
1819 CASE_FLT_FN (BUILT_IN_LOG1P):
1820 errno_set = true; builtin_optab = log1p_optab; break;
1821 CASE_FLT_FN (BUILT_IN_ASIN):
1822 builtin_optab = asin_optab; break;
1823 CASE_FLT_FN (BUILT_IN_ACOS):
1824 builtin_optab = acos_optab; break;
1825 CASE_FLT_FN (BUILT_IN_TAN):
1826 builtin_optab = tan_optab; break;
1827 CASE_FLT_FN (BUILT_IN_ATAN):
1828 builtin_optab = atan_optab; break;
1829 CASE_FLT_FN (BUILT_IN_FLOOR):
1830 builtin_optab = floor_optab; break;
1831 CASE_FLT_FN (BUILT_IN_CEIL):
1832 builtin_optab = ceil_optab; break;
1833 CASE_FLT_FN (BUILT_IN_TRUNC):
1834 builtin_optab = btrunc_optab; break;
1835 CASE_FLT_FN (BUILT_IN_ROUND):
1836 builtin_optab = round_optab; break;
1837 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1838 builtin_optab = nearbyint_optab;
1839 if (flag_trapping_math)
1840 break;
1841 /* Else fallthrough and expand as rint. */
1842 CASE_FLT_FN (BUILT_IN_RINT):
1843 builtin_optab = rint_optab; break;
1844 default:
1845 gcc_unreachable ();
1848 /* Make a suitable register to place result in. */
1849 mode = TYPE_MODE (TREE_TYPE (exp));
1851 if (! flag_errno_math || ! HONOR_NANS (mode))
1852 errno_set = false;
1854 /* Before working hard, check whether the instruction is available. */
1855 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1857 target = gen_reg_rtx (mode);
1859 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1860 need to expand the argument again. This way, we will not perform
1861 side-effects more the once. */
1862 narg = builtin_save_expr (arg);
1863 if (narg != arg)
1865 arg = narg;
1866 arglist = build_tree_list (NULL_TREE, arg);
1867 exp = build_function_call_expr (fndecl, arglist);
1870 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1872 start_sequence ();
1874 /* Compute into TARGET.
1875 Set TARGET to wherever the result comes back. */
1876 target = expand_unop (mode, builtin_optab, op0, target, 0);
1878 if (target != 0)
1880 if (errno_set)
1881 expand_errno_check (exp, target);
1883 /* Output the entire sequence. */
1884 insns = get_insns ();
1885 end_sequence ();
1886 emit_insn (insns);
1887 return target;
1890 /* If we were unable to expand via the builtin, stop the sequence
1891 (without outputting the insns) and call to the library function
1892 with the stabilized argument list. */
1893 end_sequence ();
1896 before_call = get_last_insn ();
1898 target = expand_call (exp, target, target == const0_rtx);
1900 /* If this is a sqrt operation and we don't care about errno, try to
1901 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1902 This allows the semantics of the libcall to be visible to the RTL
1903 optimizers. */
1904 if (builtin_optab == sqrt_optab && !errno_set)
1906 /* Search backwards through the insns emitted by expand_call looking
1907 for the instruction with the REG_RETVAL note. */
1908 rtx last = get_last_insn ();
1909 while (last != before_call)
1911 if (find_reg_note (last, REG_RETVAL, NULL))
1913 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1914 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1915 two elements, i.e. symbol_ref(sqrt) and the operand. */
1916 if (note
1917 && GET_CODE (note) == EXPR_LIST
1918 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1919 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1920 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1922 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1923 /* Check operand is a register with expected mode. */
1924 if (operand
1925 && REG_P (operand)
1926 && GET_MODE (operand) == mode)
1928 /* Replace the REG_EQUAL note with a SQRT rtx. */
1929 rtx equiv = gen_rtx_SQRT (mode, operand);
1930 set_unique_reg_note (last, REG_EQUAL, equiv);
1933 break;
1935 last = PREV_INSN (last);
1939 return target;
1942 /* Expand a call to the builtin binary math functions (pow and atan2).
1943 Return 0 if a normal call should be emitted rather than expanding the
1944 function in-line. EXP is the expression that is a call to the builtin
1945 function; if convenient, the result should be placed in TARGET.
1946 SUBTARGET may be used as the target for computing one of EXP's
1947 operands. */
1949 static rtx
1950 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1952 optab builtin_optab;
1953 rtx op0, op1, insns;
1954 int op1_type = REAL_TYPE;
1955 tree fndecl = get_callee_fndecl (exp);
1956 tree arglist = TREE_OPERAND (exp, 1);
1957 tree arg0, arg1, temp, narg;
1958 enum machine_mode mode;
1959 bool errno_set = true;
1960 bool stable = true;
1962 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1963 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1964 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1965 op1_type = INTEGER_TYPE;
1967 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1968 return 0;
1970 arg0 = TREE_VALUE (arglist);
1971 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1973 switch (DECL_FUNCTION_CODE (fndecl))
1975 CASE_FLT_FN (BUILT_IN_POW):
1976 builtin_optab = pow_optab; break;
1977 CASE_FLT_FN (BUILT_IN_ATAN2):
1978 builtin_optab = atan2_optab; break;
1979 CASE_FLT_FN (BUILT_IN_LDEXP):
1980 builtin_optab = ldexp_optab; break;
1981 CASE_FLT_FN (BUILT_IN_FMOD):
1982 builtin_optab = fmod_optab; break;
1983 CASE_FLT_FN (BUILT_IN_REMAINDER):
1984 CASE_FLT_FN (BUILT_IN_DREM):
1985 builtin_optab = remainder_optab; break;
1986 default:
1987 gcc_unreachable ();
1990 /* Make a suitable register to place result in. */
1991 mode = TYPE_MODE (TREE_TYPE (exp));
1993 /* Before working hard, check whether the instruction is available. */
1994 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1995 return 0;
1997 target = gen_reg_rtx (mode);
1999 if (! flag_errno_math || ! HONOR_NANS (mode))
2000 errno_set = false;
2002 /* Always stabilize the argument list. */
2003 narg = builtin_save_expr (arg1);
2004 if (narg != arg1)
2006 arg1 = narg;
2007 temp = build_tree_list (NULL_TREE, narg);
2008 stable = false;
2010 else
2011 temp = TREE_CHAIN (arglist);
2013 narg = builtin_save_expr (arg0);
2014 if (narg != arg0)
2016 arg0 = narg;
2017 arglist = tree_cons (NULL_TREE, narg, temp);
2018 stable = false;
2020 else if (! stable)
2021 arglist = tree_cons (NULL_TREE, arg0, temp);
2023 if (! stable)
2024 exp = build_function_call_expr (fndecl, arglist);
2026 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2027 op1 = expand_normal (arg1);
2029 start_sequence ();
2031 /* Compute into TARGET.
2032 Set TARGET to wherever the result comes back. */
2033 target = expand_binop (mode, builtin_optab, op0, op1,
2034 target, 0, OPTAB_DIRECT);
2036 /* If we were unable to expand via the builtin, stop the sequence
2037 (without outputting the insns) and call to the library function
2038 with the stabilized argument list. */
2039 if (target == 0)
2041 end_sequence ();
2042 return expand_call (exp, target, target == const0_rtx);
2045 if (errno_set)
2046 expand_errno_check (exp, target);
2048 /* Output the entire sequence. */
2049 insns = get_insns ();
2050 end_sequence ();
2051 emit_insn (insns);
2053 return target;
2056 /* Expand a call to the builtin sin and cos math functions.
2057 Return 0 if a normal call should be emitted rather than expanding the
2058 function in-line. EXP is the expression that is a call to the builtin
2059 function; if convenient, the result should be placed in TARGET.
2060 SUBTARGET may be used as the target for computing one of EXP's
2061 operands. */
2063 static rtx
2064 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2066 optab builtin_optab;
2067 rtx op0, insns;
2068 tree fndecl = get_callee_fndecl (exp);
2069 tree arglist = TREE_OPERAND (exp, 1);
2070 enum machine_mode mode;
2071 tree arg, narg;
2073 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2074 return 0;
2076 arg = TREE_VALUE (arglist);
2078 switch (DECL_FUNCTION_CODE (fndecl))
2080 CASE_FLT_FN (BUILT_IN_SIN):
2081 CASE_FLT_FN (BUILT_IN_COS):
2082 builtin_optab = sincos_optab; break;
2083 default:
2084 gcc_unreachable ();
2087 /* Make a suitable register to place result in. */
2088 mode = TYPE_MODE (TREE_TYPE (exp));
2090 /* Check if sincos insn is available, otherwise fallback
2091 to sin or cos insn. */
2092 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2093 switch (DECL_FUNCTION_CODE (fndecl))
2095 CASE_FLT_FN (BUILT_IN_SIN):
2096 builtin_optab = sin_optab; break;
2097 CASE_FLT_FN (BUILT_IN_COS):
2098 builtin_optab = cos_optab; break;
2099 default:
2100 gcc_unreachable ();
2103 /* Before working hard, check whether the instruction is available. */
2104 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2106 target = gen_reg_rtx (mode);
2108 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2109 need to expand the argument again. This way, we will not perform
2110 side-effects more the once. */
2111 narg = save_expr (arg);
2112 if (narg != arg)
2114 arg = narg;
2115 arglist = build_tree_list (NULL_TREE, arg);
2116 exp = build_function_call_expr (fndecl, arglist);
2119 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2121 start_sequence ();
2123 /* Compute into TARGET.
2124 Set TARGET to wherever the result comes back. */
2125 if (builtin_optab == sincos_optab)
2127 int result;
2129 switch (DECL_FUNCTION_CODE (fndecl))
2131 CASE_FLT_FN (BUILT_IN_SIN):
2132 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2133 break;
2134 CASE_FLT_FN (BUILT_IN_COS):
2135 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2136 break;
2137 default:
2138 gcc_unreachable ();
2140 gcc_assert (result);
2142 else
2144 target = expand_unop (mode, builtin_optab, op0, target, 0);
2147 if (target != 0)
2149 /* Output the entire sequence. */
2150 insns = get_insns ();
2151 end_sequence ();
2152 emit_insn (insns);
2153 return target;
2156 /* If we were unable to expand via the builtin, stop the sequence
2157 (without outputting the insns) and call to the library function
2158 with the stabilized argument list. */
2159 end_sequence ();
2162 target = expand_call (exp, target, target == const0_rtx);
2164 return target;
2167 /* Expand a call to the builtin sincos math function.
2168 Return 0 if a normal call should be emitted rather than expanding the
2169 function in-line. EXP is the expression that is a call to the builtin
2170 function. */
2172 static rtx
2173 expand_builtin_sincos (tree exp)
2175 rtx op0, op1, op2, target1, target2;
2176 tree arglist = TREE_OPERAND (exp, 1);
2177 enum machine_mode mode;
2178 tree arg, sinp, cosp;
2179 int result;
2181 if (!validate_arglist (arglist, REAL_TYPE,
2182 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2183 return 0;
2185 arg = TREE_VALUE (arglist);
2186 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2187 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2189 /* Make a suitable register to place result in. */
2190 mode = TYPE_MODE (TREE_TYPE (arg));
2192 /* Check if sincos insn is available, otherwise emit the call. */
2193 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2194 return NULL_RTX;
2196 target1 = gen_reg_rtx (mode);
2197 target2 = gen_reg_rtx (mode);
2199 op0 = expand_normal (arg);
2200 op1 = expand_normal (build_fold_indirect_ref (sinp));
2201 op2 = expand_normal (build_fold_indirect_ref (cosp));
2203 /* Compute into target1 and target2.
2204 Set TARGET to wherever the result comes back. */
2205 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2206 gcc_assert (result);
2208 /* Move target1 and target2 to the memory locations indicated
2209 by op1 and op2. */
2210 emit_move_insn (op1, target1);
2211 emit_move_insn (op2, target2);
2213 return const0_rtx;
2216 /* Expand a call to one of the builtin rounding functions gcc defines
2217 as an extension (lfloor and lceil). As these are gcc extensions we
2218 do not need to worry about setting errno to EDOM.
2219 If expanding via optab fails, lower expression to (int)(floor(x)).
2220 EXP is the expression that is a call to the builtin function;
2221 if convenient, the result should be placed in TARGET. SUBTARGET may
2222 be used as the target for computing one of EXP's operands. */
2224 static rtx
2225 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2227 convert_optab builtin_optab;
2228 rtx op0, insns, tmp;
2229 tree fndecl = get_callee_fndecl (exp);
2230 tree arglist = TREE_OPERAND (exp, 1);
2231 enum built_in_function fallback_fn;
2232 tree fallback_fndecl;
2233 enum machine_mode mode;
2234 tree arg, narg;
2236 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2237 gcc_unreachable ();
2239 arg = TREE_VALUE (arglist);
2241 switch (DECL_FUNCTION_CODE (fndecl))
2243 CASE_FLT_FN (BUILT_IN_LCEIL):
2244 CASE_FLT_FN (BUILT_IN_LLCEIL):
2245 builtin_optab = lceil_optab;
2246 fallback_fn = BUILT_IN_CEIL;
2247 break;
2249 CASE_FLT_FN (BUILT_IN_LFLOOR):
2250 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2251 builtin_optab = lfloor_optab;
2252 fallback_fn = BUILT_IN_FLOOR;
2253 break;
2255 default:
2256 gcc_unreachable ();
2259 /* Make a suitable register to place result in. */
2260 mode = TYPE_MODE (TREE_TYPE (exp));
2262 target = gen_reg_rtx (mode);
2264 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2265 need to expand the argument again. This way, we will not perform
2266 side-effects more the once. */
2267 narg = builtin_save_expr (arg);
2268 if (narg != arg)
2270 arg = narg;
2271 arglist = build_tree_list (NULL_TREE, arg);
2272 exp = build_function_call_expr (fndecl, arglist);
2275 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2277 start_sequence ();
2279 /* Compute into TARGET. */
2280 if (expand_sfix_optab (target, op0, builtin_optab))
2282 /* Output the entire sequence. */
2283 insns = get_insns ();
2284 end_sequence ();
2285 emit_insn (insns);
2286 return target;
2289 /* If we were unable to expand via the builtin, stop the sequence
2290 (without outputting the insns). */
2291 end_sequence ();
2293 /* Fall back to floating point rounding optab. */
2294 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2295 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2296 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2297 gcc_assert (fallback_fndecl != NULL_TREE);
2298 exp = build_function_call_expr (fallback_fndecl, arglist);
2300 tmp = expand_normal (exp);
2302 /* Truncate the result of floating point optab to integer
2303 via expand_fix (). */
2304 target = gen_reg_rtx (mode);
2305 expand_fix (target, tmp, 0);
2307 return target;
2310 /* Expand a call to one of the builtin math functions doing integer
2311 conversion (lrint).
2312 Return 0 if a normal call should be emitted rather than expanding the
2313 function in-line. EXP is the expression that is a call to the builtin
2314 function; if convenient, the result should be placed in TARGET.
2315 SUBTARGET may be used as the target for computing one of EXP's operands. */
2317 static rtx
2318 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2320 convert_optab builtin_optab;
2321 rtx op0, insns;
2322 tree fndecl = get_callee_fndecl (exp);
2323 tree arglist = TREE_OPERAND (exp, 1);
2324 enum machine_mode mode;
2325 tree arg, narg;
2327 /* There's no easy way to detect the case we need to set EDOM. */
2328 if (flag_errno_math)
2329 return NULL_RTX;
2331 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2332 return NULL_RTX;
2334 arg = TREE_VALUE (arglist);
2336 switch (DECL_FUNCTION_CODE (fndecl))
2338 CASE_FLT_FN (BUILT_IN_LRINT):
2339 CASE_FLT_FN (BUILT_IN_LLRINT):
2340 builtin_optab = lrint_optab; break;
2341 CASE_FLT_FN (BUILT_IN_LROUND):
2342 CASE_FLT_FN (BUILT_IN_LLROUND):
2343 builtin_optab = lround_optab; break;
2344 default:
2345 gcc_unreachable ();
2348 /* Make a suitable register to place result in. */
2349 mode = TYPE_MODE (TREE_TYPE (exp));
2351 target = gen_reg_rtx (mode);
2353 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2354 need to expand the argument again. This way, we will not perform
2355 side-effects more the once. */
2356 narg = builtin_save_expr (arg);
2357 if (narg != arg)
2359 arg = narg;
2360 arglist = build_tree_list (NULL_TREE, arg);
2361 exp = build_function_call_expr (fndecl, arglist);
2364 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2366 start_sequence ();
2368 if (expand_sfix_optab (target, op0, builtin_optab))
2370 /* Output the entire sequence. */
2371 insns = get_insns ();
2372 end_sequence ();
2373 emit_insn (insns);
2374 return target;
2377 /* If we were unable to expand via the builtin, stop the sequence
2378 (without outputting the insns) and call to the library function
2379 with the stabilized argument list. */
2380 end_sequence ();
2382 target = expand_call (exp, target, target == const0_rtx);
2384 return target;
2387 /* To evaluate powi(x,n), the floating point value x raised to the
2388 constant integer exponent n, we use a hybrid algorithm that
2389 combines the "window method" with look-up tables. For an
2390 introduction to exponentiation algorithms and "addition chains",
2391 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2392 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2393 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2394 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2396 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2397 multiplications to inline before calling the system library's pow
2398 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2399 so this default never requires calling pow, powf or powl. */
2401 #ifndef POWI_MAX_MULTS
2402 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2403 #endif
2405 /* The size of the "optimal power tree" lookup table. All
2406 exponents less than this value are simply looked up in the
2407 powi_table below. This threshold is also used to size the
2408 cache of pseudo registers that hold intermediate results. */
2409 #define POWI_TABLE_SIZE 256
2411 /* The size, in bits of the window, used in the "window method"
2412 exponentiation algorithm. This is equivalent to a radix of
2413 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2414 #define POWI_WINDOW_SIZE 3
2416 /* The following table is an efficient representation of an
2417 "optimal power tree". For each value, i, the corresponding
2418 value, j, in the table states than an optimal evaluation
2419 sequence for calculating pow(x,i) can be found by evaluating
2420 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2421 100 integers is given in Knuth's "Seminumerical algorithms". */
2423 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2425 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2426 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2427 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2428 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2429 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2430 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2431 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2432 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2433 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2434 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2435 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2436 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2437 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2438 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2439 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2440 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2441 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2442 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2443 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2444 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2445 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2446 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2447 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2448 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2449 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2450 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2451 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2452 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2453 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2454 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2455 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2456 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2460 /* Return the number of multiplications required to calculate
2461 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2462 subroutine of powi_cost. CACHE is an array indicating
2463 which exponents have already been calculated. */
2465 static int
2466 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2468 /* If we've already calculated this exponent, then this evaluation
2469 doesn't require any additional multiplications. */
2470 if (cache[n])
2471 return 0;
2473 cache[n] = true;
2474 return powi_lookup_cost (n - powi_table[n], cache)
2475 + powi_lookup_cost (powi_table[n], cache) + 1;
2478 /* Return the number of multiplications required to calculate
2479 powi(x,n) for an arbitrary x, given the exponent N. This
2480 function needs to be kept in sync with expand_powi below. */
2482 static int
2483 powi_cost (HOST_WIDE_INT n)
2485 bool cache[POWI_TABLE_SIZE];
2486 unsigned HOST_WIDE_INT digit;
2487 unsigned HOST_WIDE_INT val;
2488 int result;
2490 if (n == 0)
2491 return 0;
2493 /* Ignore the reciprocal when calculating the cost. */
2494 val = (n < 0) ? -n : n;
2496 /* Initialize the exponent cache. */
2497 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2498 cache[1] = true;
2500 result = 0;
2502 while (val >= POWI_TABLE_SIZE)
2504 if (val & 1)
2506 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2507 result += powi_lookup_cost (digit, cache)
2508 + POWI_WINDOW_SIZE + 1;
2509 val >>= POWI_WINDOW_SIZE;
2511 else
2513 val >>= 1;
2514 result++;
2518 return result + powi_lookup_cost (val, cache);
2521 /* Recursive subroutine of expand_powi. This function takes the array,
2522 CACHE, of already calculated exponents and an exponent N and returns
2523 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2525 static rtx
2526 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2528 unsigned HOST_WIDE_INT digit;
2529 rtx target, result;
2530 rtx op0, op1;
2532 if (n < POWI_TABLE_SIZE)
2534 if (cache[n])
2535 return cache[n];
2537 target = gen_reg_rtx (mode);
2538 cache[n] = target;
2540 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2541 op1 = expand_powi_1 (mode, powi_table[n], cache);
2543 else if (n & 1)
2545 target = gen_reg_rtx (mode);
2546 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2547 op0 = expand_powi_1 (mode, n - digit, cache);
2548 op1 = expand_powi_1 (mode, digit, cache);
2550 else
2552 target = gen_reg_rtx (mode);
2553 op0 = expand_powi_1 (mode, n >> 1, cache);
2554 op1 = op0;
2557 result = expand_mult (mode, op0, op1, target, 0);
2558 if (result != target)
2559 emit_move_insn (target, result);
2560 return target;
2563 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2564 floating point operand in mode MODE, and N is the exponent. This
2565 function needs to be kept in sync with powi_cost above. */
2567 static rtx
2568 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2570 unsigned HOST_WIDE_INT val;
2571 rtx cache[POWI_TABLE_SIZE];
2572 rtx result;
2574 if (n == 0)
2575 return CONST1_RTX (mode);
2577 val = (n < 0) ? -n : n;
2579 memset (cache, 0, sizeof (cache));
2580 cache[1] = x;
2582 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2584 /* If the original exponent was negative, reciprocate the result. */
2585 if (n < 0)
2586 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2587 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2589 return result;
2592 /* Expand a call to the pow built-in mathematical function. Return 0 if
2593 a normal call should be emitted rather than expanding the function
2594 in-line. EXP is the expression that is a call to the builtin
2595 function; if convenient, the result should be placed in TARGET. */
2597 static rtx
2598 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2600 tree arglist = TREE_OPERAND (exp, 1);
2601 tree arg0, arg1;
2603 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2604 return 0;
2606 arg0 = TREE_VALUE (arglist);
2607 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2609 if (TREE_CODE (arg1) == REAL_CST
2610 && ! TREE_CONSTANT_OVERFLOW (arg1))
2612 REAL_VALUE_TYPE cint;
2613 REAL_VALUE_TYPE c;
2614 HOST_WIDE_INT n;
2616 c = TREE_REAL_CST (arg1);
2617 n = real_to_integer (&c);
2618 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2619 if (real_identical (&c, &cint))
2621 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2622 Otherwise, check the number of multiplications required.
2623 Note that pow never sets errno for an integer exponent. */
2624 if ((n >= -1 && n <= 2)
2625 || (flag_unsafe_math_optimizations
2626 && ! optimize_size
2627 && powi_cost (n) <= POWI_MAX_MULTS))
2629 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2630 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2631 op = force_reg (mode, op);
2632 return expand_powi (op, mode, n);
2637 if (! flag_unsafe_math_optimizations)
2638 return NULL_RTX;
2639 return expand_builtin_mathfn_2 (exp, target, subtarget);
2642 /* Expand a call to the powi built-in mathematical function. Return 0 if
2643 a normal call should be emitted rather than expanding the function
2644 in-line. EXP is the expression that is a call to the builtin
2645 function; if convenient, the result should be placed in TARGET. */
2647 static rtx
2648 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2650 tree arglist = TREE_OPERAND (exp, 1);
2651 tree arg0, arg1;
2652 rtx op0, op1;
2653 enum machine_mode mode;
2654 enum machine_mode mode2;
2656 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2657 return 0;
2659 arg0 = TREE_VALUE (arglist);
2660 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2661 mode = TYPE_MODE (TREE_TYPE (exp));
2663 /* Handle constant power. */
2665 if (TREE_CODE (arg1) == INTEGER_CST
2666 && ! TREE_CONSTANT_OVERFLOW (arg1))
2668 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2670 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2671 Otherwise, check the number of multiplications required. */
2672 if ((TREE_INT_CST_HIGH (arg1) == 0
2673 || TREE_INT_CST_HIGH (arg1) == -1)
2674 && ((n >= -1 && n <= 2)
2675 || (! optimize_size
2676 && powi_cost (n) <= POWI_MAX_MULTS)))
2678 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2679 op0 = force_reg (mode, op0);
2680 return expand_powi (op0, mode, n);
2684 /* Emit a libcall to libgcc. */
2686 /* Mode of the 2nd argument must match that of an int. */
2687 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2689 if (target == NULL_RTX)
2690 target = gen_reg_rtx (mode);
2692 op0 = expand_expr (arg0, subtarget, mode, 0);
2693 if (GET_MODE (op0) != mode)
2694 op0 = convert_to_mode (mode, op0, 0);
2695 op1 = expand_expr (arg1, 0, mode2, 0);
2696 if (GET_MODE (op1) != mode2)
2697 op1 = convert_to_mode (mode2, op1, 0);
2699 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2700 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2701 op0, mode, op1, mode2);
2703 return target;
2706 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2707 if we failed the caller should emit a normal call, otherwise
2708 try to get the result in TARGET, if convenient. */
2710 static rtx
2711 expand_builtin_strlen (tree arglist, rtx target,
2712 enum machine_mode target_mode)
2714 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2715 return 0;
2716 else
2718 rtx pat;
2719 tree len, src = TREE_VALUE (arglist);
2720 rtx result, src_reg, char_rtx, before_strlen;
2721 enum machine_mode insn_mode = target_mode, char_mode;
2722 enum insn_code icode = CODE_FOR_nothing;
2723 int align;
2725 /* If the length can be computed at compile-time, return it. */
2726 len = c_strlen (src, 0);
2727 if (len)
2728 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2730 /* If the length can be computed at compile-time and is constant
2731 integer, but there are side-effects in src, evaluate
2732 src for side-effects, then return len.
2733 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2734 can be optimized into: i++; x = 3; */
2735 len = c_strlen (src, 1);
2736 if (len && TREE_CODE (len) == INTEGER_CST)
2738 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2739 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2742 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2744 /* If SRC is not a pointer type, don't do this operation inline. */
2745 if (align == 0)
2746 return 0;
2748 /* Bail out if we can't compute strlen in the right mode. */
2749 while (insn_mode != VOIDmode)
2751 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2752 if (icode != CODE_FOR_nothing)
2753 break;
2755 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2757 if (insn_mode == VOIDmode)
2758 return 0;
2760 /* Make a place to write the result of the instruction. */
2761 result = target;
2762 if (! (result != 0
2763 && REG_P (result)
2764 && GET_MODE (result) == insn_mode
2765 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2766 result = gen_reg_rtx (insn_mode);
2768 /* Make a place to hold the source address. We will not expand
2769 the actual source until we are sure that the expansion will
2770 not fail -- there are trees that cannot be expanded twice. */
2771 src_reg = gen_reg_rtx (Pmode);
2773 /* Mark the beginning of the strlen sequence so we can emit the
2774 source operand later. */
2775 before_strlen = get_last_insn ();
2777 char_rtx = const0_rtx;
2778 char_mode = insn_data[(int) icode].operand[2].mode;
2779 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2780 char_mode))
2781 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2783 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2784 char_rtx, GEN_INT (align));
2785 if (! pat)
2786 return 0;
2787 emit_insn (pat);
2789 /* Now that we are assured of success, expand the source. */
2790 start_sequence ();
2791 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2792 if (pat != src_reg)
2793 emit_move_insn (src_reg, pat);
2794 pat = get_insns ();
2795 end_sequence ();
2797 if (before_strlen)
2798 emit_insn_after (pat, before_strlen);
2799 else
2800 emit_insn_before (pat, get_insns ());
2802 /* Return the value in the proper mode for this function. */
2803 if (GET_MODE (result) == target_mode)
2804 target = result;
2805 else if (target != 0)
2806 convert_move (target, result, 0);
2807 else
2808 target = convert_to_mode (target_mode, result, 0);
2810 return target;
2814 /* Expand a call to the strstr builtin. Return 0 if we failed the
2815 caller should emit a normal call, otherwise try to get the result
2816 in TARGET, if convenient (and in mode MODE if that's convenient). */
2818 static rtx
2819 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2821 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2823 tree result = fold_builtin_strstr (arglist, type);
2824 if (result)
2825 return expand_expr (result, target, mode, EXPAND_NORMAL);
2827 return 0;
2830 /* Expand a call to the strchr builtin. Return 0 if we failed the
2831 caller should emit a normal call, otherwise try to get the result
2832 in TARGET, if convenient (and in mode MODE if that's convenient). */
2834 static rtx
2835 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2837 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2839 tree result = fold_builtin_strchr (arglist, type);
2840 if (result)
2841 return expand_expr (result, target, mode, EXPAND_NORMAL);
2843 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2845 return 0;
2848 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2849 caller should emit a normal call, otherwise try to get the result
2850 in TARGET, if convenient (and in mode MODE if that's convenient). */
2852 static rtx
2853 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2855 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2857 tree result = fold_builtin_strrchr (arglist, type);
2858 if (result)
2859 return expand_expr (result, target, mode, EXPAND_NORMAL);
2861 return 0;
2864 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2865 caller should emit a normal call, otherwise try to get the result
2866 in TARGET, if convenient (and in mode MODE if that's convenient). */
2868 static rtx
2869 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2871 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2873 tree result = fold_builtin_strpbrk (arglist, type);
2874 if (result)
2875 return expand_expr (result, target, mode, EXPAND_NORMAL);
2877 return 0;
2880 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2881 bytes from constant string DATA + OFFSET and return it as target
2882 constant. */
2884 static rtx
2885 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2886 enum machine_mode mode)
2888 const char *str = (const char *) data;
2890 gcc_assert (offset >= 0
2891 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2892 <= strlen (str) + 1));
2894 return c_readstr (str + offset, mode);
2897 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2898 Return 0 if we failed, the caller should emit a normal call,
2899 otherwise try to get the result in TARGET, if convenient (and in
2900 mode MODE if that's convenient). */
2901 static rtx
2902 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2904 tree fndecl = get_callee_fndecl (exp);
2905 tree arglist = TREE_OPERAND (exp, 1);
2906 if (!validate_arglist (arglist,
2907 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2908 return 0;
2909 else
2911 tree dest = TREE_VALUE (arglist);
2912 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2913 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2914 const char *src_str;
2915 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2916 unsigned int dest_align
2917 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2918 rtx dest_mem, src_mem, dest_addr, len_rtx;
2919 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
2920 false, /*endp=*/0);
2922 if (result)
2924 while (TREE_CODE (result) == COMPOUND_EXPR)
2926 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2927 EXPAND_NORMAL);
2928 result = TREE_OPERAND (result, 1);
2930 return expand_expr (result, target, mode, EXPAND_NORMAL);
2933 /* If DEST is not a pointer type, call the normal function. */
2934 if (dest_align == 0)
2935 return 0;
2937 /* If either SRC is not a pointer type, don't do this
2938 operation in-line. */
2939 if (src_align == 0)
2940 return 0;
2942 dest_mem = get_memory_rtx (dest, len);
2943 set_mem_align (dest_mem, dest_align);
2944 len_rtx = expand_normal (len);
2945 src_str = c_getstr (src);
2947 /* If SRC is a string constant and block move would be done
2948 by pieces, we can avoid loading the string from memory
2949 and only stored the computed constants. */
2950 if (src_str
2951 && GET_CODE (len_rtx) == CONST_INT
2952 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2953 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2954 (void *) src_str, dest_align))
2956 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2957 builtin_memcpy_read_str,
2958 (void *) src_str, dest_align, 0);
2959 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2960 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2961 return dest_mem;
2964 src_mem = get_memory_rtx (src, len);
2965 set_mem_align (src_mem, src_align);
2967 /* Copy word part most expediently. */
2968 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2969 CALL_EXPR_TAILCALL (exp)
2970 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2972 if (dest_addr == 0)
2974 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2975 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2977 return dest_addr;
2981 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2982 Return 0 if we failed; the caller should emit a normal call,
2983 otherwise try to get the result in TARGET, if convenient (and in
2984 mode MODE if that's convenient). If ENDP is 0 return the
2985 destination pointer, if ENDP is 1 return the end pointer ala
2986 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2987 stpcpy. */
2989 static rtx
2990 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2991 int endp)
2993 if (!validate_arglist (arglist,
2994 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2995 return 0;
2996 /* If return value is ignored, transform mempcpy into memcpy. */
2997 else if (target == const0_rtx)
2999 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3001 if (!fn)
3002 return 0;
3004 return expand_expr (build_function_call_expr (fn, arglist),
3005 target, mode, EXPAND_NORMAL);
3007 else
3009 tree dest = TREE_VALUE (arglist);
3010 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3011 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3012 const char *src_str;
3013 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3014 unsigned int dest_align
3015 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3016 rtx dest_mem, src_mem, len_rtx;
3017 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3019 if (result)
3021 while (TREE_CODE (result) == COMPOUND_EXPR)
3023 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3024 EXPAND_NORMAL);
3025 result = TREE_OPERAND (result, 1);
3027 return expand_expr (result, target, mode, EXPAND_NORMAL);
3030 /* If either SRC or DEST is not a pointer type, don't do this
3031 operation in-line. */
3032 if (dest_align == 0 || src_align == 0)
3033 return 0;
3035 /* If LEN is not constant, call the normal function. */
3036 if (! host_integerp (len, 1))
3037 return 0;
3039 len_rtx = expand_normal (len);
3040 src_str = c_getstr (src);
3042 /* If SRC is a string constant and block move would be done
3043 by pieces, we can avoid loading the string from memory
3044 and only stored the computed constants. */
3045 if (src_str
3046 && GET_CODE (len_rtx) == CONST_INT
3047 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3048 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3049 (void *) src_str, dest_align))
3051 dest_mem = get_memory_rtx (dest, len);
3052 set_mem_align (dest_mem, dest_align);
3053 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3054 builtin_memcpy_read_str,
3055 (void *) src_str, dest_align, endp);
3056 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3057 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3058 return dest_mem;
3061 if (GET_CODE (len_rtx) == CONST_INT
3062 && can_move_by_pieces (INTVAL (len_rtx),
3063 MIN (dest_align, src_align)))
3065 dest_mem = get_memory_rtx (dest, len);
3066 set_mem_align (dest_mem, dest_align);
3067 src_mem = get_memory_rtx (src, len);
3068 set_mem_align (src_mem, src_align);
3069 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3070 MIN (dest_align, src_align), endp);
3071 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3072 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3073 return dest_mem;
3076 return 0;
3080 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3081 if we failed; the caller should emit a normal call. */
3083 static rtx
3084 expand_builtin_memmove (tree arglist, tree type, rtx target,
3085 enum machine_mode mode)
3087 if (!validate_arglist (arglist,
3088 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3089 return 0;
3090 else
3092 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3094 if (result)
3096 while (TREE_CODE (result) == COMPOUND_EXPR)
3098 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3099 EXPAND_NORMAL);
3100 result = TREE_OPERAND (result, 1);
3102 return expand_expr (result, target, mode, EXPAND_NORMAL);
3105 /* Otherwise, call the normal function. */
3106 return 0;
3110 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3111 if we failed the caller should emit a normal call. */
3113 static rtx
3114 expand_builtin_bcopy (tree exp)
3116 tree arglist = TREE_OPERAND (exp, 1);
3117 tree type = TREE_TYPE (exp);
3118 tree src, dest, size, newarglist;
3120 if (!validate_arglist (arglist,
3121 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3122 return NULL_RTX;
3124 src = TREE_VALUE (arglist);
3125 dest = TREE_VALUE (TREE_CHAIN (arglist));
3126 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3128 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3129 memmove(ptr y, ptr x, size_t z). This is done this way
3130 so that if it isn't expanded inline, we fallback to
3131 calling bcopy instead of memmove. */
3133 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3134 newarglist = tree_cons (NULL_TREE, src, newarglist);
3135 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3137 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3140 #ifndef HAVE_movstr
3141 # define HAVE_movstr 0
3142 # define CODE_FOR_movstr CODE_FOR_nothing
3143 #endif
3145 /* Expand into a movstr instruction, if one is available. Return 0 if
3146 we failed, the caller should emit a normal call, otherwise try to
3147 get the result in TARGET, if convenient. If ENDP is 0 return the
3148 destination pointer, if ENDP is 1 return the end pointer ala
3149 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3150 stpcpy. */
3152 static rtx
3153 expand_movstr (tree dest, tree src, rtx target, int endp)
3155 rtx end;
3156 rtx dest_mem;
3157 rtx src_mem;
3158 rtx insn;
3159 const struct insn_data * data;
3161 if (!HAVE_movstr)
3162 return 0;
3164 dest_mem = get_memory_rtx (dest, NULL);
3165 src_mem = get_memory_rtx (src, NULL);
3166 if (!endp)
3168 target = force_reg (Pmode, XEXP (dest_mem, 0));
3169 dest_mem = replace_equiv_address (dest_mem, target);
3170 end = gen_reg_rtx (Pmode);
3172 else
3174 if (target == 0 || target == const0_rtx)
3176 end = gen_reg_rtx (Pmode);
3177 if (target == 0)
3178 target = end;
3180 else
3181 end = target;
3184 data = insn_data + CODE_FOR_movstr;
3186 if (data->operand[0].mode != VOIDmode)
3187 end = gen_lowpart (data->operand[0].mode, end);
3189 insn = data->genfun (end, dest_mem, src_mem);
3191 gcc_assert (insn);
3193 emit_insn (insn);
3195 /* movstr is supposed to set end to the address of the NUL
3196 terminator. If the caller requested a mempcpy-like return value,
3197 adjust it. */
3198 if (endp == 1 && target != const0_rtx)
3200 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3201 emit_move_insn (target, force_operand (tem, NULL_RTX));
3204 return target;
3207 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3208 if we failed the caller should emit a normal call, otherwise try to get
3209 the result in TARGET, if convenient (and in mode MODE if that's
3210 convenient). */
3212 static rtx
3213 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3215 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3217 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3218 if (result)
3220 while (TREE_CODE (result) == COMPOUND_EXPR)
3222 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3223 EXPAND_NORMAL);
3224 result = TREE_OPERAND (result, 1);
3226 return expand_expr (result, target, mode, EXPAND_NORMAL);
3229 return expand_movstr (TREE_VALUE (arglist),
3230 TREE_VALUE (TREE_CHAIN (arglist)),
3231 target, /*endp=*/0);
3233 return 0;
3236 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3237 Return 0 if we failed the caller should emit a normal call,
3238 otherwise try to get the result in TARGET, if convenient (and in
3239 mode MODE if that's convenient). */
3241 static rtx
3242 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3244 tree arglist = TREE_OPERAND (exp, 1);
3245 /* If return value is ignored, transform stpcpy into strcpy. */
3246 if (target == const0_rtx)
3248 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3249 if (!fn)
3250 return 0;
3252 return expand_expr (build_function_call_expr (fn, arglist),
3253 target, mode, EXPAND_NORMAL);
3256 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3257 return 0;
3258 else
3260 tree dst, src, len, lenp1;
3261 tree narglist;
3262 rtx ret;
3264 /* Ensure we get an actual string whose length can be evaluated at
3265 compile-time, not an expression containing a string. This is
3266 because the latter will potentially produce pessimized code
3267 when used to produce the return value. */
3268 src = TREE_VALUE (TREE_CHAIN (arglist));
3269 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3270 return expand_movstr (TREE_VALUE (arglist),
3271 TREE_VALUE (TREE_CHAIN (arglist)),
3272 target, /*endp=*/2);
3274 dst = TREE_VALUE (arglist);
3275 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3276 narglist = build_tree_list (NULL_TREE, lenp1);
3277 narglist = tree_cons (NULL_TREE, src, narglist);
3278 narglist = tree_cons (NULL_TREE, dst, narglist);
3279 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3280 target, mode, /*endp=*/2);
3282 if (ret)
3283 return ret;
3285 if (TREE_CODE (len) == INTEGER_CST)
3287 rtx len_rtx = expand_normal (len);
3289 if (GET_CODE (len_rtx) == CONST_INT)
3291 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3292 arglist, target, mode);
3294 if (ret)
3296 if (! target)
3298 if (mode != VOIDmode)
3299 target = gen_reg_rtx (mode);
3300 else
3301 target = gen_reg_rtx (GET_MODE (ret));
3303 if (GET_MODE (target) != GET_MODE (ret))
3304 ret = gen_lowpart (GET_MODE (target), ret);
3306 ret = plus_constant (ret, INTVAL (len_rtx));
3307 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3308 gcc_assert (ret);
3310 return target;
3315 return expand_movstr (TREE_VALUE (arglist),
3316 TREE_VALUE (TREE_CHAIN (arglist)),
3317 target, /*endp=*/2);
3321 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3322 bytes from constant string DATA + OFFSET and return it as target
3323 constant. */
3325 static rtx
3326 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3327 enum machine_mode mode)
3329 const char *str = (const char *) data;
3331 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3332 return const0_rtx;
3334 return c_readstr (str + offset, mode);
3337 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3338 if we failed the caller should emit a normal call. */
3340 static rtx
3341 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3343 tree fndecl = get_callee_fndecl (exp);
3344 tree arglist = TREE_OPERAND (exp, 1);
3345 if (validate_arglist (arglist,
3346 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3348 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3349 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3350 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3352 if (result)
3354 while (TREE_CODE (result) == COMPOUND_EXPR)
3356 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3357 EXPAND_NORMAL);
3358 result = TREE_OPERAND (result, 1);
3360 return expand_expr (result, target, mode, EXPAND_NORMAL);
3363 /* We must be passed a constant len and src parameter. */
3364 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3365 return 0;
3367 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3369 /* We're required to pad with trailing zeros if the requested
3370 len is greater than strlen(s2)+1. In that case try to
3371 use store_by_pieces, if it fails, punt. */
3372 if (tree_int_cst_lt (slen, len))
3374 tree dest = TREE_VALUE (arglist);
3375 unsigned int dest_align
3376 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3377 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3378 rtx dest_mem;
3380 if (!p || dest_align == 0 || !host_integerp (len, 1)
3381 || !can_store_by_pieces (tree_low_cst (len, 1),
3382 builtin_strncpy_read_str,
3383 (void *) p, dest_align))
3384 return 0;
3386 dest_mem = get_memory_rtx (dest, len);
3387 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3388 builtin_strncpy_read_str,
3389 (void *) p, dest_align, 0);
3390 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3391 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3392 return dest_mem;
3395 return 0;
3398 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3399 bytes from constant string DATA + OFFSET and return it as target
3400 constant. */
3402 static rtx
3403 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3404 enum machine_mode mode)
3406 const char *c = (const char *) data;
3407 char *p = alloca (GET_MODE_SIZE (mode));
3409 memset (p, *c, GET_MODE_SIZE (mode));
3411 return c_readstr (p, mode);
3414 /* Callback routine for store_by_pieces. Return the RTL of a register
3415 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3416 char value given in the RTL register data. For example, if mode is
3417 4 bytes wide, return the RTL for 0x01010101*data. */
3419 static rtx
3420 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3421 enum machine_mode mode)
3423 rtx target, coeff;
3424 size_t size;
3425 char *p;
3427 size = GET_MODE_SIZE (mode);
3428 if (size == 1)
3429 return (rtx) data;
3431 p = alloca (size);
3432 memset (p, 1, size);
3433 coeff = c_readstr (p, mode);
3435 target = convert_to_mode (mode, (rtx) data, 1);
3436 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3437 return force_reg (mode, target);
3440 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3441 if we failed the caller should emit a normal call, otherwise try to get
3442 the result in TARGET, if convenient (and in mode MODE if that's
3443 convenient). */
3445 static rtx
3446 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3447 tree orig_exp)
3449 if (!validate_arglist (arglist,
3450 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3451 return 0;
3452 else
3454 tree dest = TREE_VALUE (arglist);
3455 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3456 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3457 tree fndecl, fn;
3458 enum built_in_function fcode;
3459 char c;
3460 unsigned int dest_align;
3461 rtx dest_mem, dest_addr, len_rtx;
3463 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3465 /* If DEST is not a pointer type, don't do this
3466 operation in-line. */
3467 if (dest_align == 0)
3468 return 0;
3470 /* If the LEN parameter is zero, return DEST. */
3471 if (integer_zerop (len))
3473 /* Evaluate and ignore VAL in case it has side-effects. */
3474 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3475 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3478 /* Stabilize the arguments in case we fail. */
3479 dest = builtin_save_expr (dest);
3480 val = builtin_save_expr (val);
3481 len = builtin_save_expr (len);
3483 len_rtx = expand_normal (len);
3484 dest_mem = get_memory_rtx (dest, len);
3486 if (TREE_CODE (val) != INTEGER_CST)
3488 rtx val_rtx;
3490 val_rtx = expand_normal (val);
3491 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3492 val_rtx, 0);
3494 /* Assume that we can memset by pieces if we can store the
3495 * the coefficients by pieces (in the required modes).
3496 * We can't pass builtin_memset_gen_str as that emits RTL. */
3497 c = 1;
3498 if (host_integerp (len, 1)
3499 && !(optimize_size && tree_low_cst (len, 1) > 1)
3500 && can_store_by_pieces (tree_low_cst (len, 1),
3501 builtin_memset_read_str, &c, dest_align))
3503 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3504 val_rtx);
3505 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3506 builtin_memset_gen_str, val_rtx, dest_align, 0);
3508 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3509 dest_align))
3510 goto do_libcall;
3512 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3513 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3514 return dest_mem;
3517 if (target_char_cast (val, &c))
3518 goto do_libcall;
3520 if (c)
3522 if (host_integerp (len, 1)
3523 && !(optimize_size && tree_low_cst (len, 1) > 1)
3524 && can_store_by_pieces (tree_low_cst (len, 1),
3525 builtin_memset_read_str, &c, dest_align))
3526 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3527 builtin_memset_read_str, &c, dest_align, 0);
3528 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3529 dest_align))
3530 goto do_libcall;
3532 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3533 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3534 return dest_mem;
3537 set_mem_align (dest_mem, dest_align);
3538 dest_addr = clear_storage (dest_mem, len_rtx,
3539 CALL_EXPR_TAILCALL (orig_exp)
3540 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3542 if (dest_addr == 0)
3544 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3545 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3548 return dest_addr;
3550 do_libcall:
3551 fndecl = get_callee_fndecl (orig_exp);
3552 fcode = DECL_FUNCTION_CODE (fndecl);
3553 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3554 arglist = build_tree_list (NULL_TREE, len);
3555 if (fcode == BUILT_IN_MEMSET)
3556 arglist = tree_cons (NULL_TREE, val, arglist);
3557 arglist = tree_cons (NULL_TREE, dest, arglist);
3558 fn = build_function_call_expr (fndecl, arglist);
3559 if (TREE_CODE (fn) == CALL_EXPR)
3560 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3561 return expand_call (fn, target, target == const0_rtx);
3565 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3566 if we failed the caller should emit a normal call. */
3568 static rtx
3569 expand_builtin_bzero (tree exp)
3571 tree arglist = TREE_OPERAND (exp, 1);
3572 tree dest, size, newarglist;
3574 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3575 return NULL_RTX;
3577 dest = TREE_VALUE (arglist);
3578 size = TREE_VALUE (TREE_CHAIN (arglist));
3580 /* New argument list transforming bzero(ptr x, int y) to
3581 memset(ptr x, int 0, size_t y). This is done this way
3582 so that if it isn't expanded inline, we fallback to
3583 calling bzero instead of memset. */
3585 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3586 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3587 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3589 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3592 /* Expand expression EXP, which is a call to the memcmp built-in function.
3593 ARGLIST is the argument list for this call. Return 0 if we failed and the
3594 caller should emit a normal call, otherwise try to get the result in
3595 TARGET, if convenient (and in mode MODE, if that's convenient). */
3597 static rtx
3598 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3599 enum machine_mode mode)
3601 if (!validate_arglist (arglist,
3602 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3603 return 0;
3604 else
3606 tree result = fold_builtin_memcmp (arglist);
3607 if (result)
3608 return expand_expr (result, target, mode, EXPAND_NORMAL);
3611 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3613 tree arg1 = TREE_VALUE (arglist);
3614 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3615 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3616 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3617 rtx result;
3618 rtx insn;
3620 int arg1_align
3621 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3622 int arg2_align
3623 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3624 enum machine_mode insn_mode;
3626 #ifdef HAVE_cmpmemsi
3627 if (HAVE_cmpmemsi)
3628 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3629 else
3630 #endif
3631 #ifdef HAVE_cmpstrnsi
3632 if (HAVE_cmpstrnsi)
3633 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3634 else
3635 #endif
3636 return 0;
3638 /* If we don't have POINTER_TYPE, call the function. */
3639 if (arg1_align == 0 || arg2_align == 0)
3640 return 0;
3642 /* Make a place to write the result of the instruction. */
3643 result = target;
3644 if (! (result != 0
3645 && REG_P (result) && GET_MODE (result) == insn_mode
3646 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3647 result = gen_reg_rtx (insn_mode);
3649 arg1_rtx = get_memory_rtx (arg1, len);
3650 arg2_rtx = get_memory_rtx (arg2, len);
3651 arg3_rtx = expand_normal (len);
3653 /* Set MEM_SIZE as appropriate. */
3654 if (GET_CODE (arg3_rtx) == CONST_INT)
3656 set_mem_size (arg1_rtx, arg3_rtx);
3657 set_mem_size (arg2_rtx, arg3_rtx);
3660 #ifdef HAVE_cmpmemsi
3661 if (HAVE_cmpmemsi)
3662 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3663 GEN_INT (MIN (arg1_align, arg2_align)));
3664 else
3665 #endif
3666 #ifdef HAVE_cmpstrnsi
3667 if (HAVE_cmpstrnsi)
3668 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3669 GEN_INT (MIN (arg1_align, arg2_align)));
3670 else
3671 #endif
3672 gcc_unreachable ();
3674 if (insn)
3675 emit_insn (insn);
3676 else
3677 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3678 TYPE_MODE (integer_type_node), 3,
3679 XEXP (arg1_rtx, 0), Pmode,
3680 XEXP (arg2_rtx, 0), Pmode,
3681 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3682 TYPE_UNSIGNED (sizetype)),
3683 TYPE_MODE (sizetype));
3685 /* Return the value in the proper mode for this function. */
3686 mode = TYPE_MODE (TREE_TYPE (exp));
3687 if (GET_MODE (result) == mode)
3688 return result;
3689 else if (target != 0)
3691 convert_move (target, result, 0);
3692 return target;
3694 else
3695 return convert_to_mode (mode, result, 0);
3697 #endif
3699 return 0;
3702 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3703 if we failed the caller should emit a normal call, otherwise try to get
3704 the result in TARGET, if convenient. */
3706 static rtx
3707 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3709 tree arglist = TREE_OPERAND (exp, 1);
3711 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3712 return 0;
3713 else
3715 tree result = fold_builtin_strcmp (arglist);
3716 if (result)
3717 return expand_expr (result, target, mode, EXPAND_NORMAL);
3720 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3721 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3722 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3724 rtx arg1_rtx, arg2_rtx;
3725 rtx result, insn = NULL_RTX;
3726 tree fndecl, fn;
3728 tree arg1 = TREE_VALUE (arglist);
3729 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3730 int arg1_align
3731 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3732 int arg2_align
3733 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3735 /* If we don't have POINTER_TYPE, call the function. */
3736 if (arg1_align == 0 || arg2_align == 0)
3737 return 0;
3739 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3740 arg1 = builtin_save_expr (arg1);
3741 arg2 = builtin_save_expr (arg2);
3743 arg1_rtx = get_memory_rtx (arg1, NULL);
3744 arg2_rtx = get_memory_rtx (arg2, NULL);
3746 #ifdef HAVE_cmpstrsi
3747 /* Try to call cmpstrsi. */
3748 if (HAVE_cmpstrsi)
3750 enum machine_mode insn_mode
3751 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3753 /* Make a place to write the result of the instruction. */
3754 result = target;
3755 if (! (result != 0
3756 && REG_P (result) && GET_MODE (result) == insn_mode
3757 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3758 result = gen_reg_rtx (insn_mode);
3760 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3761 GEN_INT (MIN (arg1_align, arg2_align)));
3763 #endif
3764 #ifdef HAVE_cmpstrnsi
3765 /* Try to determine at least one length and call cmpstrnsi. */
3766 if (!insn && HAVE_cmpstrnsi)
3768 tree len;
3769 rtx arg3_rtx;
3771 enum machine_mode insn_mode
3772 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3773 tree len1 = c_strlen (arg1, 1);
3774 tree len2 = c_strlen (arg2, 1);
3776 if (len1)
3777 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3778 if (len2)
3779 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3781 /* If we don't have a constant length for the first, use the length
3782 of the second, if we know it. We don't require a constant for
3783 this case; some cost analysis could be done if both are available
3784 but neither is constant. For now, assume they're equally cheap,
3785 unless one has side effects. If both strings have constant lengths,
3786 use the smaller. */
3788 if (!len1)
3789 len = len2;
3790 else if (!len2)
3791 len = len1;
3792 else if (TREE_SIDE_EFFECTS (len1))
3793 len = len2;
3794 else if (TREE_SIDE_EFFECTS (len2))
3795 len = len1;
3796 else if (TREE_CODE (len1) != INTEGER_CST)
3797 len = len2;
3798 else if (TREE_CODE (len2) != INTEGER_CST)
3799 len = len1;
3800 else if (tree_int_cst_lt (len1, len2))
3801 len = len1;
3802 else
3803 len = len2;
3805 /* If both arguments have side effects, we cannot optimize. */
3806 if (!len || TREE_SIDE_EFFECTS (len))
3807 goto do_libcall;
3809 arg3_rtx = expand_normal (len);
3811 /* Make a place to write the result of the instruction. */
3812 result = target;
3813 if (! (result != 0
3814 && REG_P (result) && GET_MODE (result) == insn_mode
3815 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3816 result = gen_reg_rtx (insn_mode);
3818 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3819 GEN_INT (MIN (arg1_align, arg2_align)));
3821 #endif
3823 if (insn)
3825 emit_insn (insn);
3827 /* Return the value in the proper mode for this function. */
3828 mode = TYPE_MODE (TREE_TYPE (exp));
3829 if (GET_MODE (result) == mode)
3830 return result;
3831 if (target == 0)
3832 return convert_to_mode (mode, result, 0);
3833 convert_move (target, result, 0);
3834 return target;
3837 /* Expand the library call ourselves using a stabilized argument
3838 list to avoid re-evaluating the function's arguments twice. */
3839 #ifdef HAVE_cmpstrnsi
3840 do_libcall:
3841 #endif
3842 arglist = build_tree_list (NULL_TREE, arg2);
3843 arglist = tree_cons (NULL_TREE, arg1, arglist);
3844 fndecl = get_callee_fndecl (exp);
3845 fn = build_function_call_expr (fndecl, arglist);
3846 if (TREE_CODE (fn) == CALL_EXPR)
3847 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3848 return expand_call (fn, target, target == const0_rtx);
3850 #endif
3851 return 0;
3854 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3855 if we failed the caller should emit a normal call, otherwise try to get
3856 the result in TARGET, if convenient. */
3858 static rtx
3859 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3861 tree arglist = TREE_OPERAND (exp, 1);
3863 if (!validate_arglist (arglist,
3864 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3865 return 0;
3866 else
3868 tree result = fold_builtin_strncmp (arglist);
3869 if (result)
3870 return expand_expr (result, target, mode, EXPAND_NORMAL);
3873 /* If c_strlen can determine an expression for one of the string
3874 lengths, and it doesn't have side effects, then emit cmpstrnsi
3875 using length MIN(strlen(string)+1, arg3). */
3876 #ifdef HAVE_cmpstrnsi
3877 if (HAVE_cmpstrnsi)
3879 tree arg1 = TREE_VALUE (arglist);
3880 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3881 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3882 tree len, len1, len2;
3883 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3884 rtx result, insn;
3885 tree fndecl, fn;
3887 int arg1_align
3888 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3889 int arg2_align
3890 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3891 enum machine_mode insn_mode
3892 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3894 len1 = c_strlen (arg1, 1);
3895 len2 = c_strlen (arg2, 1);
3897 if (len1)
3898 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3899 if (len2)
3900 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3902 /* If we don't have a constant length for the first, use the length
3903 of the second, if we know it. We don't require a constant for
3904 this case; some cost analysis could be done if both are available
3905 but neither is constant. For now, assume they're equally cheap,
3906 unless one has side effects. If both strings have constant lengths,
3907 use the smaller. */
3909 if (!len1)
3910 len = len2;
3911 else if (!len2)
3912 len = len1;
3913 else if (TREE_SIDE_EFFECTS (len1))
3914 len = len2;
3915 else if (TREE_SIDE_EFFECTS (len2))
3916 len = len1;
3917 else if (TREE_CODE (len1) != INTEGER_CST)
3918 len = len2;
3919 else if (TREE_CODE (len2) != INTEGER_CST)
3920 len = len1;
3921 else if (tree_int_cst_lt (len1, len2))
3922 len = len1;
3923 else
3924 len = len2;
3926 /* If both arguments have side effects, we cannot optimize. */
3927 if (!len || TREE_SIDE_EFFECTS (len))
3928 return 0;
3930 /* The actual new length parameter is MIN(len,arg3). */
3931 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
3932 fold_convert (TREE_TYPE (len), arg3));
3934 /* If we don't have POINTER_TYPE, call the function. */
3935 if (arg1_align == 0 || arg2_align == 0)
3936 return 0;
3938 /* Make a place to write the result of the instruction. */
3939 result = target;
3940 if (! (result != 0
3941 && REG_P (result) && GET_MODE (result) == insn_mode
3942 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3943 result = gen_reg_rtx (insn_mode);
3945 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3946 arg1 = builtin_save_expr (arg1);
3947 arg2 = builtin_save_expr (arg2);
3948 len = builtin_save_expr (len);
3950 arg1_rtx = get_memory_rtx (arg1, len);
3951 arg2_rtx = get_memory_rtx (arg2, len);
3952 arg3_rtx = expand_normal (len);
3953 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3954 GEN_INT (MIN (arg1_align, arg2_align)));
3955 if (insn)
3957 emit_insn (insn);
3959 /* Return the value in the proper mode for this function. */
3960 mode = TYPE_MODE (TREE_TYPE (exp));
3961 if (GET_MODE (result) == mode)
3962 return result;
3963 if (target == 0)
3964 return convert_to_mode (mode, result, 0);
3965 convert_move (target, result, 0);
3966 return target;
3969 /* Expand the library call ourselves using a stabilized argument
3970 list to avoid re-evaluating the function's arguments twice. */
3971 arglist = build_tree_list (NULL_TREE, len);
3972 arglist = tree_cons (NULL_TREE, arg2, arglist);
3973 arglist = tree_cons (NULL_TREE, arg1, arglist);
3974 fndecl = get_callee_fndecl (exp);
3975 fn = build_function_call_expr (fndecl, arglist);
3976 if (TREE_CODE (fn) == CALL_EXPR)
3977 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3978 return expand_call (fn, target, target == const0_rtx);
3980 #endif
3981 return 0;
3984 /* Expand expression EXP, which is a call to the strcat builtin.
3985 Return 0 if we failed the caller should emit a normal call,
3986 otherwise try to get the result in TARGET, if convenient. */
3988 static rtx
3989 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3991 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3992 return 0;
3993 else
3995 tree dst = TREE_VALUE (arglist),
3996 src = TREE_VALUE (TREE_CHAIN (arglist));
3997 const char *p = c_getstr (src);
3999 /* If the string length is zero, return the dst parameter. */
4000 if (p && *p == '\0')
4001 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4003 if (!optimize_size)
4005 /* See if we can store by pieces into (dst + strlen(dst)). */
4006 tree newsrc, newdst,
4007 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4008 rtx insns;
4010 /* Stabilize the argument list. */
4011 newsrc = builtin_save_expr (src);
4012 if (newsrc != src)
4013 arglist = build_tree_list (NULL_TREE, newsrc);
4014 else
4015 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4017 dst = builtin_save_expr (dst);
4019 start_sequence ();
4021 /* Create strlen (dst). */
4022 newdst =
4023 build_function_call_expr (strlen_fn,
4024 build_tree_list (NULL_TREE, dst));
4025 /* Create (dst + (cast) strlen (dst)). */
4026 newdst = fold_convert (TREE_TYPE (dst), newdst);
4027 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4029 newdst = builtin_save_expr (newdst);
4030 arglist = tree_cons (NULL_TREE, newdst, arglist);
4032 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4034 end_sequence (); /* Stop sequence. */
4035 return 0;
4038 /* Output the entire sequence. */
4039 insns = get_insns ();
4040 end_sequence ();
4041 emit_insn (insns);
4043 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4046 return 0;
4050 /* Expand expression EXP, which is a call to the strncat builtin.
4051 Return 0 if we failed the caller should emit a normal call,
4052 otherwise try to get the result in TARGET, if convenient. */
4054 static rtx
4055 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4057 if (validate_arglist (arglist,
4058 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4060 tree result = fold_builtin_strncat (arglist);
4061 if (result)
4062 return expand_expr (result, target, mode, EXPAND_NORMAL);
4064 return 0;
4067 /* Expand expression EXP, which is a call to the strspn builtin.
4068 Return 0 if we failed the caller should emit a normal call,
4069 otherwise try to get the result in TARGET, if convenient. */
4071 static rtx
4072 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4074 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4076 tree result = fold_builtin_strspn (arglist);
4077 if (result)
4078 return expand_expr (result, target, mode, EXPAND_NORMAL);
4080 return 0;
4083 /* Expand expression EXP, which is a call to the strcspn builtin.
4084 Return 0 if we failed the caller should emit a normal call,
4085 otherwise try to get the result in TARGET, if convenient. */
4087 static rtx
4088 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4090 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4092 tree result = fold_builtin_strcspn (arglist);
4093 if (result)
4094 return expand_expr (result, target, mode, EXPAND_NORMAL);
4096 return 0;
4099 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4100 if that's convenient. */
4103 expand_builtin_saveregs (void)
4105 rtx val, seq;
4107 /* Don't do __builtin_saveregs more than once in a function.
4108 Save the result of the first call and reuse it. */
4109 if (saveregs_value != 0)
4110 return saveregs_value;
4112 /* When this function is called, it means that registers must be
4113 saved on entry to this function. So we migrate the call to the
4114 first insn of this function. */
4116 start_sequence ();
4118 /* Do whatever the machine needs done in this case. */
4119 val = targetm.calls.expand_builtin_saveregs ();
4121 seq = get_insns ();
4122 end_sequence ();
4124 saveregs_value = val;
4126 /* Put the insns after the NOTE that starts the function. If this
4127 is inside a start_sequence, make the outer-level insn chain current, so
4128 the code is placed at the start of the function. */
4129 push_topmost_sequence ();
4130 emit_insn_after (seq, entry_of_function ());
4131 pop_topmost_sequence ();
4133 return val;
4136 /* __builtin_args_info (N) returns word N of the arg space info
4137 for the current function. The number and meanings of words
4138 is controlled by the definition of CUMULATIVE_ARGS. */
4140 static rtx
4141 expand_builtin_args_info (tree arglist)
4143 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4144 int *word_ptr = (int *) &current_function_args_info;
4146 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4148 if (arglist != 0)
4150 if (!host_integerp (TREE_VALUE (arglist), 0))
4151 error ("argument of %<__builtin_args_info%> must be constant");
4152 else
4154 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4156 if (wordnum < 0 || wordnum >= nwords)
4157 error ("argument of %<__builtin_args_info%> out of range");
4158 else
4159 return GEN_INT (word_ptr[wordnum]);
4162 else
4163 error ("missing argument in %<__builtin_args_info%>");
4165 return const0_rtx;
4168 /* Expand a call to __builtin_next_arg. */
4170 static rtx
4171 expand_builtin_next_arg (void)
4173 /* Checking arguments is already done in fold_builtin_next_arg
4174 that must be called before this function. */
4175 return expand_binop (Pmode, add_optab,
4176 current_function_internal_arg_pointer,
4177 current_function_arg_offset_rtx,
4178 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4181 /* Make it easier for the backends by protecting the valist argument
4182 from multiple evaluations. */
4184 static tree
4185 stabilize_va_list (tree valist, int needs_lvalue)
4187 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4189 if (TREE_SIDE_EFFECTS (valist))
4190 valist = save_expr (valist);
4192 /* For this case, the backends will be expecting a pointer to
4193 TREE_TYPE (va_list_type_node), but it's possible we've
4194 actually been given an array (an actual va_list_type_node).
4195 So fix it. */
4196 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4198 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4199 valist = build_fold_addr_expr_with_type (valist, p1);
4202 else
4204 tree pt;
4206 if (! needs_lvalue)
4208 if (! TREE_SIDE_EFFECTS (valist))
4209 return valist;
4211 pt = build_pointer_type (va_list_type_node);
4212 valist = fold_build1 (ADDR_EXPR, pt, valist);
4213 TREE_SIDE_EFFECTS (valist) = 1;
4216 if (TREE_SIDE_EFFECTS (valist))
4217 valist = save_expr (valist);
4218 valist = build_fold_indirect_ref (valist);
4221 return valist;
4224 /* The "standard" definition of va_list is void*. */
4226 tree
4227 std_build_builtin_va_list (void)
4229 return ptr_type_node;
4232 /* The "standard" implementation of va_start: just assign `nextarg' to
4233 the variable. */
4235 void
4236 std_expand_builtin_va_start (tree valist, rtx nextarg)
4238 tree t;
4240 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4241 make_tree (ptr_type_node, nextarg));
4242 TREE_SIDE_EFFECTS (t) = 1;
4244 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4247 /* Expand ARGLIST, from a call to __builtin_va_start. */
4249 static rtx
4250 expand_builtin_va_start (tree arglist)
4252 rtx nextarg;
4253 tree chain, valist;
4255 chain = TREE_CHAIN (arglist);
4257 if (!chain)
4259 error ("too few arguments to function %<va_start%>");
4260 return const0_rtx;
4263 if (fold_builtin_next_arg (chain))
4264 return const0_rtx;
4266 nextarg = expand_builtin_next_arg ();
4267 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4269 #ifdef EXPAND_BUILTIN_VA_START
4270 EXPAND_BUILTIN_VA_START (valist, nextarg);
4271 #else
4272 std_expand_builtin_va_start (valist, nextarg);
4273 #endif
4275 return const0_rtx;
4278 /* The "standard" implementation of va_arg: read the value from the
4279 current (padded) address and increment by the (padded) size. */
4281 tree
4282 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4284 tree addr, t, type_size, rounded_size, valist_tmp;
4285 unsigned HOST_WIDE_INT align, boundary;
4286 bool indirect;
4288 #ifdef ARGS_GROW_DOWNWARD
4289 /* All of the alignment and movement below is for args-grow-up machines.
4290 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4291 implement their own specialized gimplify_va_arg_expr routines. */
4292 gcc_unreachable ();
4293 #endif
4295 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4296 if (indirect)
4297 type = build_pointer_type (type);
4299 align = PARM_BOUNDARY / BITS_PER_UNIT;
4300 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4302 /* Hoist the valist value into a temporary for the moment. */
4303 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4305 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4306 requires greater alignment, we must perform dynamic alignment. */
4307 if (boundary > align
4308 && !integer_zerop (TYPE_SIZE (type)))
4310 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4311 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4312 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4313 gimplify_and_add (t, pre_p);
4315 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4316 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4317 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4318 gimplify_and_add (t, pre_p);
4320 else
4321 boundary = align;
4323 /* If the actual alignment is less than the alignment of the type,
4324 adjust the type accordingly so that we don't assume strict alignment
4325 when deferencing the pointer. */
4326 boundary *= BITS_PER_UNIT;
4327 if (boundary < TYPE_ALIGN (type))
4329 type = build_variant_type_copy (type);
4330 TYPE_ALIGN (type) = boundary;
4333 /* Compute the rounded size of the type. */
4334 type_size = size_in_bytes (type);
4335 rounded_size = round_up (type_size, align);
4337 /* Reduce rounded_size so it's sharable with the postqueue. */
4338 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4340 /* Get AP. */
4341 addr = valist_tmp;
4342 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4344 /* Small args are padded downward. */
4345 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4346 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4347 size_binop (MINUS_EXPR, rounded_size, type_size));
4348 t = fold_convert (TREE_TYPE (addr), t);
4349 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4352 /* Compute new value for AP. */
4353 t = fold_convert (TREE_TYPE (valist), rounded_size);
4354 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4355 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4356 gimplify_and_add (t, pre_p);
4358 addr = fold_convert (build_pointer_type (type), addr);
4360 if (indirect)
4361 addr = build_va_arg_indirect_ref (addr);
4363 return build_va_arg_indirect_ref (addr);
4366 /* Build an indirect-ref expression over the given TREE, which represents a
4367 piece of a va_arg() expansion. */
4368 tree
4369 build_va_arg_indirect_ref (tree addr)
4371 addr = build_fold_indirect_ref (addr);
4373 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4374 mf_mark (addr);
4376 return addr;
4379 /* Return a dummy expression of type TYPE in order to keep going after an
4380 error. */
4382 static tree
4383 dummy_object (tree type)
4385 tree t = build_int_cst (build_pointer_type (type), 0);
4386 return build1 (INDIRECT_REF, type, t);
4389 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4390 builtin function, but a very special sort of operator. */
4392 enum gimplify_status
4393 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4395 tree promoted_type, want_va_type, have_va_type;
4396 tree valist = TREE_OPERAND (*expr_p, 0);
4397 tree type = TREE_TYPE (*expr_p);
4398 tree t;
4400 /* Verify that valist is of the proper type. */
4401 want_va_type = va_list_type_node;
4402 have_va_type = TREE_TYPE (valist);
4404 if (have_va_type == error_mark_node)
4405 return GS_ERROR;
4407 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4409 /* If va_list is an array type, the argument may have decayed
4410 to a pointer type, e.g. by being passed to another function.
4411 In that case, unwrap both types so that we can compare the
4412 underlying records. */
4413 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4414 || POINTER_TYPE_P (have_va_type))
4416 want_va_type = TREE_TYPE (want_va_type);
4417 have_va_type = TREE_TYPE (have_va_type);
4421 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4423 error ("first argument to %<va_arg%> not of type %<va_list%>");
4424 return GS_ERROR;
4427 /* Generate a diagnostic for requesting data of a type that cannot
4428 be passed through `...' due to type promotion at the call site. */
4429 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4430 != type)
4432 static bool gave_help;
4434 /* Unfortunately, this is merely undefined, rather than a constraint
4435 violation, so we cannot make this an error. If this call is never
4436 executed, the program is still strictly conforming. */
4437 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4438 type, promoted_type);
4439 if (! gave_help)
4441 gave_help = true;
4442 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4443 promoted_type, type);
4446 /* We can, however, treat "undefined" any way we please.
4447 Call abort to encourage the user to fix the program. */
4448 inform ("if this code is reached, the program will abort");
4449 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4450 NULL);
4451 append_to_statement_list (t, pre_p);
4453 /* This is dead code, but go ahead and finish so that the
4454 mode of the result comes out right. */
4455 *expr_p = dummy_object (type);
4456 return GS_ALL_DONE;
4458 else
4460 /* Make it easier for the backends by protecting the valist argument
4461 from multiple evaluations. */
4462 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4464 /* For this case, the backends will be expecting a pointer to
4465 TREE_TYPE (va_list_type_node), but it's possible we've
4466 actually been given an array (an actual va_list_type_node).
4467 So fix it. */
4468 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4470 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4471 valist = build_fold_addr_expr_with_type (valist, p1);
4473 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4475 else
4476 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4478 if (!targetm.gimplify_va_arg_expr)
4479 /* FIXME:Once most targets are converted we should merely
4480 assert this is non-null. */
4481 return GS_ALL_DONE;
4483 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4484 return GS_OK;
4488 /* Expand ARGLIST, from a call to __builtin_va_end. */
4490 static rtx
4491 expand_builtin_va_end (tree arglist)
4493 tree valist = TREE_VALUE (arglist);
4495 /* Evaluate for side effects, if needed. I hate macros that don't
4496 do that. */
4497 if (TREE_SIDE_EFFECTS (valist))
4498 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4500 return const0_rtx;
4503 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4504 builtin rather than just as an assignment in stdarg.h because of the
4505 nastiness of array-type va_list types. */
4507 static rtx
4508 expand_builtin_va_copy (tree arglist)
4510 tree dst, src, t;
4512 dst = TREE_VALUE (arglist);
4513 src = TREE_VALUE (TREE_CHAIN (arglist));
4515 dst = stabilize_va_list (dst, 1);
4516 src = stabilize_va_list (src, 0);
4518 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4520 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4521 TREE_SIDE_EFFECTS (t) = 1;
4522 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4524 else
4526 rtx dstb, srcb, size;
4528 /* Evaluate to pointers. */
4529 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4530 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4531 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4532 VOIDmode, EXPAND_NORMAL);
4534 dstb = convert_memory_address (Pmode, dstb);
4535 srcb = convert_memory_address (Pmode, srcb);
4537 /* "Dereference" to BLKmode memories. */
4538 dstb = gen_rtx_MEM (BLKmode, dstb);
4539 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4540 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4541 srcb = gen_rtx_MEM (BLKmode, srcb);
4542 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4543 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4545 /* Copy. */
4546 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4549 return const0_rtx;
4552 /* Expand a call to one of the builtin functions __builtin_frame_address or
4553 __builtin_return_address. */
4555 static rtx
4556 expand_builtin_frame_address (tree fndecl, tree arglist)
4558 /* The argument must be a nonnegative integer constant.
4559 It counts the number of frames to scan up the stack.
4560 The value is the return address saved in that frame. */
4561 if (arglist == 0)
4562 /* Warning about missing arg was already issued. */
4563 return const0_rtx;
4564 else if (! host_integerp (TREE_VALUE (arglist), 1))
4566 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4567 error ("invalid argument to %<__builtin_frame_address%>");
4568 else
4569 error ("invalid argument to %<__builtin_return_address%>");
4570 return const0_rtx;
4572 else
4574 rtx tem
4575 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4576 tree_low_cst (TREE_VALUE (arglist), 1));
4578 /* Some ports cannot access arbitrary stack frames. */
4579 if (tem == NULL)
4581 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4582 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4583 else
4584 warning (0, "unsupported argument to %<__builtin_return_address%>");
4585 return const0_rtx;
4588 /* For __builtin_frame_address, return what we've got. */
4589 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4590 return tem;
4592 if (!REG_P (tem)
4593 && ! CONSTANT_P (tem))
4594 tem = copy_to_mode_reg (Pmode, tem);
4595 return tem;
4599 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4600 we failed and the caller should emit a normal call, otherwise try to get
4601 the result in TARGET, if convenient. */
4603 static rtx
4604 expand_builtin_alloca (tree arglist, rtx target)
4606 rtx op0;
4607 rtx result;
4609 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4610 should always expand to function calls. These can be intercepted
4611 in libmudflap. */
4612 if (flag_mudflap)
4613 return 0;
4615 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4616 return 0;
4618 /* Compute the argument. */
4619 op0 = expand_normal (TREE_VALUE (arglist));
4621 /* Allocate the desired space. */
4622 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4623 result = convert_memory_address (ptr_mode, result);
4625 return result;
4628 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4629 is the mode to expand with. */
4631 static rtx
4632 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4634 enum machine_mode mode;
4635 tree arg;
4636 rtx op0;
4638 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4639 return 0;
4641 arg = TREE_VALUE (arglist);
4642 mode = TYPE_MODE (TREE_TYPE (arg));
4643 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4645 target = expand_unop (mode, bswap_optab, op0, target, 1);
4647 gcc_assert (target);
4649 return convert_to_mode (mode, target, 0);
4652 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4653 Return 0 if a normal call should be emitted rather than expanding the
4654 function in-line. If convenient, the result should be placed in TARGET.
4655 SUBTARGET may be used as the target for computing one of EXP's operands. */
4657 static rtx
4658 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4659 rtx subtarget, optab op_optab)
4661 rtx op0;
4662 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4663 return 0;
4665 /* Compute the argument. */
4666 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4667 /* Compute op, into TARGET if possible.
4668 Set TARGET to wherever the result comes back. */
4669 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4670 op_optab, op0, target, 1);
4671 gcc_assert (target);
4673 return convert_to_mode (target_mode, target, 0);
4676 /* If the string passed to fputs is a constant and is one character
4677 long, we attempt to transform this call into __builtin_fputc(). */
4679 static rtx
4680 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4682 /* Verify the arguments in the original call. */
4683 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4685 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4686 unlocked, NULL_TREE);
4687 if (result)
4688 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4690 return 0;
4693 /* Expand a call to __builtin_expect. We return our argument and emit a
4694 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4695 a non-jump context. */
4697 static rtx
4698 expand_builtin_expect (tree arglist, rtx target)
4700 tree exp, c;
4701 rtx note, rtx_c;
4703 if (arglist == NULL_TREE
4704 || TREE_CHAIN (arglist) == NULL_TREE)
4705 return const0_rtx;
4706 exp = TREE_VALUE (arglist);
4707 c = TREE_VALUE (TREE_CHAIN (arglist));
4709 if (TREE_CODE (c) != INTEGER_CST)
4711 error ("second argument to %<__builtin_expect%> must be a constant");
4712 c = integer_zero_node;
4715 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4717 /* Don't bother with expected value notes for integral constants. */
4718 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4720 /* We do need to force this into a register so that we can be
4721 moderately sure to be able to correctly interpret the branch
4722 condition later. */
4723 target = force_reg (GET_MODE (target), target);
4725 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4727 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4728 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4731 return target;
4734 /* Like expand_builtin_expect, except do this in a jump context. This is
4735 called from do_jump if the conditional is a __builtin_expect. Return either
4736 a list of insns to emit the jump or NULL if we cannot optimize
4737 __builtin_expect. We need to optimize this at jump time so that machines
4738 like the PowerPC don't turn the test into a SCC operation, and then jump
4739 based on the test being 0/1. */
4742 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4744 tree arglist = TREE_OPERAND (exp, 1);
4745 tree arg0 = TREE_VALUE (arglist);
4746 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4747 rtx ret = NULL_RTX;
4749 /* Only handle __builtin_expect (test, 0) and
4750 __builtin_expect (test, 1). */
4751 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4752 && (integer_zerop (arg1) || integer_onep (arg1)))
4754 rtx insn, drop_through_label, temp;
4756 /* Expand the jump insns. */
4757 start_sequence ();
4758 do_jump (arg0, if_false_label, if_true_label);
4759 ret = get_insns ();
4761 drop_through_label = get_last_insn ();
4762 if (drop_through_label && NOTE_P (drop_through_label))
4763 drop_through_label = prev_nonnote_insn (drop_through_label);
4764 if (drop_through_label && !LABEL_P (drop_through_label))
4765 drop_through_label = NULL_RTX;
4766 end_sequence ();
4768 if (! if_true_label)
4769 if_true_label = drop_through_label;
4770 if (! if_false_label)
4771 if_false_label = drop_through_label;
4773 /* Go through and add the expect's to each of the conditional jumps. */
4774 insn = ret;
4775 while (insn != NULL_RTX)
4777 rtx next = NEXT_INSN (insn);
4779 if (JUMP_P (insn) && any_condjump_p (insn))
4781 rtx ifelse = SET_SRC (pc_set (insn));
4782 rtx then_dest = XEXP (ifelse, 1);
4783 rtx else_dest = XEXP (ifelse, 2);
4784 int taken = -1;
4786 /* First check if we recognize any of the labels. */
4787 if (GET_CODE (then_dest) == LABEL_REF
4788 && XEXP (then_dest, 0) == if_true_label)
4789 taken = 1;
4790 else if (GET_CODE (then_dest) == LABEL_REF
4791 && XEXP (then_dest, 0) == if_false_label)
4792 taken = 0;
4793 else if (GET_CODE (else_dest) == LABEL_REF
4794 && XEXP (else_dest, 0) == if_false_label)
4795 taken = 1;
4796 else if (GET_CODE (else_dest) == LABEL_REF
4797 && XEXP (else_dest, 0) == if_true_label)
4798 taken = 0;
4799 /* Otherwise check where we drop through. */
4800 else if (else_dest == pc_rtx)
4802 if (next && NOTE_P (next))
4803 next = next_nonnote_insn (next);
4805 if (next && JUMP_P (next)
4806 && any_uncondjump_p (next))
4807 temp = XEXP (SET_SRC (pc_set (next)), 0);
4808 else
4809 temp = next;
4811 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4812 else that can't possibly match either target label. */
4813 if (temp == if_false_label)
4814 taken = 1;
4815 else if (temp == if_true_label)
4816 taken = 0;
4818 else if (then_dest == pc_rtx)
4820 if (next && NOTE_P (next))
4821 next = next_nonnote_insn (next);
4823 if (next && JUMP_P (next)
4824 && any_uncondjump_p (next))
4825 temp = XEXP (SET_SRC (pc_set (next)), 0);
4826 else
4827 temp = next;
4829 if (temp == if_false_label)
4830 taken = 0;
4831 else if (temp == if_true_label)
4832 taken = 1;
4835 if (taken != -1)
4837 /* If the test is expected to fail, reverse the
4838 probabilities. */
4839 if (integer_zerop (arg1))
4840 taken = 1 - taken;
4841 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4845 insn = next;
4849 return ret;
4852 void
4853 expand_builtin_trap (void)
4855 #ifdef HAVE_trap
4856 if (HAVE_trap)
4857 emit_insn (gen_trap ());
4858 else
4859 #endif
4860 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4861 emit_barrier ();
4864 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4865 Return 0 if a normal call should be emitted rather than expanding
4866 the function inline. If convenient, the result should be placed
4867 in TARGET. SUBTARGET may be used as the target for computing
4868 the operand. */
4870 static rtx
4871 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4873 enum machine_mode mode;
4874 tree arg;
4875 rtx op0;
4877 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4878 return 0;
4880 arg = TREE_VALUE (arglist);
4881 mode = TYPE_MODE (TREE_TYPE (arg));
4882 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4883 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4886 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4887 Return NULL is a normal call should be emitted rather than expanding the
4888 function inline. If convenient, the result should be placed in TARGET.
4889 SUBTARGET may be used as the target for computing the operand. */
4891 static rtx
4892 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4894 rtx op0, op1;
4895 tree arg;
4897 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4898 return 0;
4900 arg = TREE_VALUE (arglist);
4901 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4903 arg = TREE_VALUE (TREE_CHAIN (arglist));
4904 op1 = expand_normal (arg);
4906 return expand_copysign (op0, op1, target);
4909 /* Create a new constant string literal and return a char* pointer to it.
4910 The STRING_CST value is the LEN characters at STR. */
4911 tree
4912 build_string_literal (int len, const char *str)
4914 tree t, elem, index, type;
4916 t = build_string (len, str);
4917 elem = build_type_variant (char_type_node, 1, 0);
4918 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4919 type = build_array_type (elem, index);
4920 TREE_TYPE (t) = type;
4921 TREE_CONSTANT (t) = 1;
4922 TREE_INVARIANT (t) = 1;
4923 TREE_READONLY (t) = 1;
4924 TREE_STATIC (t) = 1;
4926 type = build_pointer_type (type);
4927 t = build1 (ADDR_EXPR, type, t);
4929 type = build_pointer_type (elem);
4930 t = build1 (NOP_EXPR, type, t);
4931 return t;
4934 /* Expand EXP, a call to printf or printf_unlocked.
4935 Return 0 if a normal call should be emitted rather than transforming
4936 the function inline. If convenient, the result should be placed in
4937 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4938 call. */
4939 static rtx
4940 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4941 bool unlocked)
4943 tree arglist = TREE_OPERAND (exp, 1);
4944 /* If we're using an unlocked function, assume the other unlocked
4945 functions exist explicitly. */
4946 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4947 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4948 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4949 : implicit_built_in_decls[BUILT_IN_PUTS];
4950 const char *fmt_str;
4951 tree fn, fmt, arg;
4953 /* If the return value is used, don't do the transformation. */
4954 if (target != const0_rtx)
4955 return 0;
4957 /* Verify the required arguments in the original call. */
4958 if (! arglist)
4959 return 0;
4960 fmt = TREE_VALUE (arglist);
4961 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4962 return 0;
4963 arglist = TREE_CHAIN (arglist);
4965 /* Check whether the format is a literal string constant. */
4966 fmt_str = c_getstr (fmt);
4967 if (fmt_str == NULL)
4968 return 0;
4970 if (!init_target_chars())
4971 return 0;
4973 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4974 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4976 if (! arglist
4977 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4978 || TREE_CHAIN (arglist))
4979 return 0;
4980 fn = fn_puts;
4982 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4983 else if (strcmp (fmt_str, target_percent_c) == 0)
4985 if (! arglist
4986 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4987 || TREE_CHAIN (arglist))
4988 return 0;
4989 fn = fn_putchar;
4991 else
4993 /* We can't handle anything else with % args or %% ... yet. */
4994 if (strchr (fmt_str, target_percent))
4995 return 0;
4997 if (arglist)
4998 return 0;
5000 /* If the format specifier was "", printf does nothing. */
5001 if (fmt_str[0] == '\0')
5002 return const0_rtx;
5003 /* If the format specifier has length of 1, call putchar. */
5004 if (fmt_str[1] == '\0')
5006 /* Given printf("c"), (where c is any one character,)
5007 convert "c"[0] to an int and pass that to the replacement
5008 function. */
5009 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5010 arglist = build_tree_list (NULL_TREE, arg);
5011 fn = fn_putchar;
5013 else
5015 /* If the format specifier was "string\n", call puts("string"). */
5016 size_t len = strlen (fmt_str);
5017 if ((unsigned char)fmt_str[len - 1] == target_newline)
5019 /* Create a NUL-terminated string that's one char shorter
5020 than the original, stripping off the trailing '\n'. */
5021 char *newstr = alloca (len);
5022 memcpy (newstr, fmt_str, len - 1);
5023 newstr[len - 1] = 0;
5025 arg = build_string_literal (len, newstr);
5026 arglist = build_tree_list (NULL_TREE, arg);
5027 fn = fn_puts;
5029 else
5030 /* We'd like to arrange to call fputs(string,stdout) here,
5031 but we need stdout and don't have a way to get it yet. */
5032 return 0;
5036 if (!fn)
5037 return 0;
5038 fn = build_function_call_expr (fn, arglist);
5039 if (TREE_CODE (fn) == CALL_EXPR)
5040 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5041 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5044 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5045 Return 0 if a normal call should be emitted rather than transforming
5046 the function inline. If convenient, the result should be placed in
5047 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5048 call. */
5049 static rtx
5050 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5051 bool unlocked)
5053 tree arglist = TREE_OPERAND (exp, 1);
5054 /* If we're using an unlocked function, assume the other unlocked
5055 functions exist explicitly. */
5056 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5057 : implicit_built_in_decls[BUILT_IN_FPUTC];
5058 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5059 : implicit_built_in_decls[BUILT_IN_FPUTS];
5060 const char *fmt_str;
5061 tree fn, fmt, fp, arg;
5063 /* If the return value is used, don't do the transformation. */
5064 if (target != const0_rtx)
5065 return 0;
5067 /* Verify the required arguments in the original call. */
5068 if (! arglist)
5069 return 0;
5070 fp = TREE_VALUE (arglist);
5071 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5072 return 0;
5073 arglist = TREE_CHAIN (arglist);
5074 if (! arglist)
5075 return 0;
5076 fmt = TREE_VALUE (arglist);
5077 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5078 return 0;
5079 arglist = TREE_CHAIN (arglist);
5081 /* Check whether the format is a literal string constant. */
5082 fmt_str = c_getstr (fmt);
5083 if (fmt_str == NULL)
5084 return 0;
5086 if (!init_target_chars())
5087 return 0;
5089 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5090 if (strcmp (fmt_str, target_percent_s) == 0)
5092 if (! arglist
5093 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5094 || TREE_CHAIN (arglist))
5095 return 0;
5096 arg = TREE_VALUE (arglist);
5097 arglist = build_tree_list (NULL_TREE, fp);
5098 arglist = tree_cons (NULL_TREE, arg, arglist);
5099 fn = fn_fputs;
5101 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5102 else if (strcmp (fmt_str, target_percent_c) == 0)
5104 if (! arglist
5105 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5106 || TREE_CHAIN (arglist))
5107 return 0;
5108 arg = TREE_VALUE (arglist);
5109 arglist = build_tree_list (NULL_TREE, fp);
5110 arglist = tree_cons (NULL_TREE, arg, arglist);
5111 fn = fn_fputc;
5113 else
5115 /* We can't handle anything else with % args or %% ... yet. */
5116 if (strchr (fmt_str, target_percent))
5117 return 0;
5119 if (arglist)
5120 return 0;
5122 /* If the format specifier was "", fprintf does nothing. */
5123 if (fmt_str[0] == '\0')
5125 /* Evaluate and ignore FILE* argument for side-effects. */
5126 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5127 return const0_rtx;
5130 /* When "string" doesn't contain %, replace all cases of
5131 fprintf(stream,string) with fputs(string,stream). The fputs
5132 builtin will take care of special cases like length == 1. */
5133 arglist = build_tree_list (NULL_TREE, fp);
5134 arglist = tree_cons (NULL_TREE, fmt, arglist);
5135 fn = fn_fputs;
5138 if (!fn)
5139 return 0;
5140 fn = build_function_call_expr (fn, arglist);
5141 if (TREE_CODE (fn) == CALL_EXPR)
5142 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5143 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5146 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5147 a normal call should be emitted rather than expanding the function
5148 inline. If convenient, the result should be placed in TARGET with
5149 mode MODE. */
5151 static rtx
5152 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5154 tree orig_arglist, dest, fmt;
5155 const char *fmt_str;
5157 orig_arglist = arglist;
5159 /* Verify the required arguments in the original call. */
5160 if (! arglist)
5161 return 0;
5162 dest = TREE_VALUE (arglist);
5163 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5164 return 0;
5165 arglist = TREE_CHAIN (arglist);
5166 if (! arglist)
5167 return 0;
5168 fmt = TREE_VALUE (arglist);
5169 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5170 return 0;
5171 arglist = TREE_CHAIN (arglist);
5173 /* Check whether the format is a literal string constant. */
5174 fmt_str = c_getstr (fmt);
5175 if (fmt_str == NULL)
5176 return 0;
5178 if (!init_target_chars())
5179 return 0;
5181 /* If the format doesn't contain % args or %%, use strcpy. */
5182 if (strchr (fmt_str, target_percent) == 0)
5184 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5185 tree exp;
5187 if (arglist || ! fn)
5188 return 0;
5189 expand_expr (build_function_call_expr (fn, orig_arglist),
5190 const0_rtx, VOIDmode, EXPAND_NORMAL);
5191 if (target == const0_rtx)
5192 return const0_rtx;
5193 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5194 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5196 /* If the format is "%s", use strcpy if the result isn't used. */
5197 else if (strcmp (fmt_str, target_percent_s) == 0)
5199 tree fn, arg, len;
5200 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5202 if (! fn)
5203 return 0;
5205 if (! arglist || TREE_CHAIN (arglist))
5206 return 0;
5207 arg = TREE_VALUE (arglist);
5208 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5209 return 0;
5211 if (target != const0_rtx)
5213 len = c_strlen (arg, 1);
5214 if (! len || TREE_CODE (len) != INTEGER_CST)
5215 return 0;
5217 else
5218 len = NULL_TREE;
5220 arglist = build_tree_list (NULL_TREE, arg);
5221 arglist = tree_cons (NULL_TREE, dest, arglist);
5222 expand_expr (build_function_call_expr (fn, arglist),
5223 const0_rtx, VOIDmode, EXPAND_NORMAL);
5225 if (target == const0_rtx)
5226 return const0_rtx;
5227 return expand_expr (len, target, mode, EXPAND_NORMAL);
5230 return 0;
5233 /* Expand a call to either the entry or exit function profiler. */
5235 static rtx
5236 expand_builtin_profile_func (bool exitp)
5238 rtx this, which;
5240 this = DECL_RTL (current_function_decl);
5241 gcc_assert (MEM_P (this));
5242 this = XEXP (this, 0);
5244 if (exitp)
5245 which = profile_function_exit_libfunc;
5246 else
5247 which = profile_function_entry_libfunc;
5249 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5250 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5252 Pmode);
5254 return const0_rtx;
5257 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5259 static rtx
5260 round_trampoline_addr (rtx tramp)
5262 rtx temp, addend, mask;
5264 /* If we don't need too much alignment, we'll have been guaranteed
5265 proper alignment by get_trampoline_type. */
5266 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5267 return tramp;
5269 /* Round address up to desired boundary. */
5270 temp = gen_reg_rtx (Pmode);
5271 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5272 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5274 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5275 temp, 0, OPTAB_LIB_WIDEN);
5276 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5277 temp, 0, OPTAB_LIB_WIDEN);
5279 return tramp;
5282 static rtx
5283 expand_builtin_init_trampoline (tree arglist)
5285 tree t_tramp, t_func, t_chain;
5286 rtx r_tramp, r_func, r_chain;
5287 #ifdef TRAMPOLINE_TEMPLATE
5288 rtx blktramp;
5289 #endif
5291 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5292 POINTER_TYPE, VOID_TYPE))
5293 return NULL_RTX;
5295 t_tramp = TREE_VALUE (arglist);
5296 arglist = TREE_CHAIN (arglist);
5297 t_func = TREE_VALUE (arglist);
5298 arglist = TREE_CHAIN (arglist);
5299 t_chain = TREE_VALUE (arglist);
5301 r_tramp = expand_normal (t_tramp);
5302 r_func = expand_normal (t_func);
5303 r_chain = expand_normal (t_chain);
5305 /* Generate insns to initialize the trampoline. */
5306 r_tramp = round_trampoline_addr (r_tramp);
5307 #ifdef TRAMPOLINE_TEMPLATE
5308 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5309 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5310 emit_block_move (blktramp, assemble_trampoline_template (),
5311 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5312 #endif
5313 trampolines_created = 1;
5314 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5316 return const0_rtx;
5319 static rtx
5320 expand_builtin_adjust_trampoline (tree arglist)
5322 rtx tramp;
5324 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5325 return NULL_RTX;
5327 tramp = expand_normal (TREE_VALUE (arglist));
5328 tramp = round_trampoline_addr (tramp);
5329 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5330 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5331 #endif
5333 return tramp;
5336 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5337 Return NULL_RTX if a normal call should be emitted rather than expanding
5338 the function in-line. EXP is the expression that is a call to the builtin
5339 function; if convenient, the result should be placed in TARGET. */
5341 static rtx
5342 expand_builtin_signbit (tree exp, rtx target)
5344 const struct real_format *fmt;
5345 enum machine_mode fmode, imode, rmode;
5346 HOST_WIDE_INT hi, lo;
5347 tree arg, arglist;
5348 int word, bitpos;
5349 rtx temp;
5351 arglist = TREE_OPERAND (exp, 1);
5352 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5353 return 0;
5355 arg = TREE_VALUE (arglist);
5356 fmode = TYPE_MODE (TREE_TYPE (arg));
5357 rmode = TYPE_MODE (TREE_TYPE (exp));
5358 fmt = REAL_MODE_FORMAT (fmode);
5360 /* For floating point formats without a sign bit, implement signbit
5361 as "ARG < 0.0". */
5362 bitpos = fmt->signbit_ro;
5363 if (bitpos < 0)
5365 /* But we can't do this if the format supports signed zero. */
5366 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5367 return 0;
5369 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5370 build_real (TREE_TYPE (arg), dconst0));
5371 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5374 temp = expand_normal (arg);
5375 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5377 imode = int_mode_for_mode (fmode);
5378 if (imode == BLKmode)
5379 return 0;
5380 temp = gen_lowpart (imode, temp);
5382 else
5384 imode = word_mode;
5385 /* Handle targets with different FP word orders. */
5386 if (FLOAT_WORDS_BIG_ENDIAN)
5387 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5388 else
5389 word = bitpos / BITS_PER_WORD;
5390 temp = operand_subword_force (temp, word, fmode);
5391 bitpos = bitpos % BITS_PER_WORD;
5394 /* Force the intermediate word_mode (or narrower) result into a
5395 register. This avoids attempting to create paradoxical SUBREGs
5396 of floating point modes below. */
5397 temp = force_reg (imode, temp);
5399 /* If the bitpos is within the "result mode" lowpart, the operation
5400 can be implement with a single bitwise AND. Otherwise, we need
5401 a right shift and an AND. */
5403 if (bitpos < GET_MODE_BITSIZE (rmode))
5405 if (bitpos < HOST_BITS_PER_WIDE_INT)
5407 hi = 0;
5408 lo = (HOST_WIDE_INT) 1 << bitpos;
5410 else
5412 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5413 lo = 0;
5416 if (imode != rmode)
5417 temp = gen_lowpart (rmode, temp);
5418 temp = expand_binop (rmode, and_optab, temp,
5419 immed_double_const (lo, hi, rmode),
5420 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5422 else
5424 /* Perform a logical right shift to place the signbit in the least
5425 significant bit, then truncate the result to the desired mode
5426 and mask just this bit. */
5427 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5428 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5429 temp = gen_lowpart (rmode, temp);
5430 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5431 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5434 return temp;
5437 /* Expand fork or exec calls. TARGET is the desired target of the
5438 call. ARGLIST is the list of arguments of the call. FN is the
5439 identificator of the actual function. IGNORE is nonzero if the
5440 value is to be ignored. */
5442 static rtx
5443 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5445 tree id, decl;
5446 tree call;
5448 /* If we are not profiling, just call the function. */
5449 if (!profile_arc_flag)
5450 return NULL_RTX;
5452 /* Otherwise call the wrapper. This should be equivalent for the rest of
5453 compiler, so the code does not diverge, and the wrapper may run the
5454 code necessary for keeping the profiling sane. */
5456 switch (DECL_FUNCTION_CODE (fn))
5458 case BUILT_IN_FORK:
5459 id = get_identifier ("__gcov_fork");
5460 break;
5462 case BUILT_IN_EXECL:
5463 id = get_identifier ("__gcov_execl");
5464 break;
5466 case BUILT_IN_EXECV:
5467 id = get_identifier ("__gcov_execv");
5468 break;
5470 case BUILT_IN_EXECLP:
5471 id = get_identifier ("__gcov_execlp");
5472 break;
5474 case BUILT_IN_EXECLE:
5475 id = get_identifier ("__gcov_execle");
5476 break;
5478 case BUILT_IN_EXECVP:
5479 id = get_identifier ("__gcov_execvp");
5480 break;
5482 case BUILT_IN_EXECVE:
5483 id = get_identifier ("__gcov_execve");
5484 break;
5486 default:
5487 gcc_unreachable ();
5490 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5491 DECL_EXTERNAL (decl) = 1;
5492 TREE_PUBLIC (decl) = 1;
5493 DECL_ARTIFICIAL (decl) = 1;
5494 TREE_NOTHROW (decl) = 1;
5495 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5496 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5497 call = build_function_call_expr (decl, arglist);
5499 return expand_call (call, target, ignore);
5503 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5504 the pointer in these functions is void*, the tree optimizers may remove
5505 casts. The mode computed in expand_builtin isn't reliable either, due
5506 to __sync_bool_compare_and_swap.
5508 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5509 group of builtins. This gives us log2 of the mode size. */
5511 static inline enum machine_mode
5512 get_builtin_sync_mode (int fcode_diff)
5514 /* The size is not negotiable, so ask not to get BLKmode in return
5515 if the target indicates that a smaller size would be better. */
5516 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5519 /* Expand the memory expression LOC and return the appropriate memory operand
5520 for the builtin_sync operations. */
5522 static rtx
5523 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5525 rtx addr, mem;
5527 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5529 /* Note that we explicitly do not want any alias information for this
5530 memory, so that we kill all other live memories. Otherwise we don't
5531 satisfy the full barrier semantics of the intrinsic. */
5532 mem = validize_mem (gen_rtx_MEM (mode, addr));
5534 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5535 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5536 MEM_VOLATILE_P (mem) = 1;
5538 return mem;
5541 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5542 ARGLIST is the operands list to the function. CODE is the rtx code
5543 that corresponds to the arithmetic or logical operation from the name;
5544 an exception here is that NOT actually means NAND. TARGET is an optional
5545 place for us to store the results; AFTER is true if this is the
5546 fetch_and_xxx form. IGNORE is true if we don't actually care about
5547 the result of the operation at all. */
5549 static rtx
5550 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5551 enum rtx_code code, bool after,
5552 rtx target, bool ignore)
5554 rtx val, mem;
5556 /* Expand the operands. */
5557 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5559 arglist = TREE_CHAIN (arglist);
5560 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5561 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5562 val = convert_to_mode (mode, val, 1);
5564 if (ignore)
5565 return expand_sync_operation (mem, val, code);
5566 else
5567 return expand_sync_fetch_operation (mem, val, code, after, target);
5570 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5571 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5572 true if this is the boolean form. TARGET is a place for us to store the
5573 results; this is NOT optional if IS_BOOL is true. */
5575 static rtx
5576 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5577 bool is_bool, rtx target)
5579 rtx old_val, new_val, mem;
5581 /* Expand the operands. */
5582 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5584 arglist = TREE_CHAIN (arglist);
5585 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5586 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5587 old_val = convert_to_mode (mode, old_val, 1);
5589 arglist = TREE_CHAIN (arglist);
5590 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5591 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5592 new_val = convert_to_mode (mode, new_val, 1);
5594 if (is_bool)
5595 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5596 else
5597 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5600 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5601 general form is actually an atomic exchange, and some targets only
5602 support a reduced form with the second argument being a constant 1.
5603 ARGLIST is the operands list to the function; TARGET is an optional
5604 place for us to store the results. */
5606 static rtx
5607 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5608 rtx target)
5610 rtx val, mem;
5612 /* Expand the operands. */
5613 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5615 arglist = TREE_CHAIN (arglist);
5616 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5617 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5618 val = convert_to_mode (mode, val, 1);
5620 return expand_sync_lock_test_and_set (mem, val, target);
5623 /* Expand the __sync_synchronize intrinsic. */
5625 static void
5626 expand_builtin_synchronize (void)
5628 tree x;
5630 #ifdef HAVE_memory_barrier
5631 if (HAVE_memory_barrier)
5633 emit_insn (gen_memory_barrier ());
5634 return;
5636 #endif
5638 /* If no explicit memory barrier instruction is available, create an
5639 empty asm stmt with a memory clobber. */
5640 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5641 tree_cons (NULL, build_string (6, "memory"), NULL));
5642 ASM_VOLATILE_P (x) = 1;
5643 expand_asm_expr (x);
5646 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5647 to the function. */
5649 static void
5650 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5652 enum insn_code icode;
5653 rtx mem, insn;
5654 rtx val = const0_rtx;
5656 /* Expand the operands. */
5657 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5659 /* If there is an explicit operation in the md file, use it. */
5660 icode = sync_lock_release[mode];
5661 if (icode != CODE_FOR_nothing)
5663 if (!insn_data[icode].operand[1].predicate (val, mode))
5664 val = force_reg (mode, val);
5666 insn = GEN_FCN (icode) (mem, val);
5667 if (insn)
5669 emit_insn (insn);
5670 return;
5674 /* Otherwise we can implement this operation by emitting a barrier
5675 followed by a store of zero. */
5676 expand_builtin_synchronize ();
5677 emit_move_insn (mem, val);
5680 /* Expand an expression EXP that calls a built-in function,
5681 with result going to TARGET if that's convenient
5682 (and in mode MODE if that's convenient).
5683 SUBTARGET may be used as the target for computing one of EXP's operands.
5684 IGNORE is nonzero if the value is to be ignored. */
5687 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5688 int ignore)
5690 tree fndecl = get_callee_fndecl (exp);
5691 tree arglist = TREE_OPERAND (exp, 1);
5692 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5693 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5695 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5696 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5698 /* When not optimizing, generate calls to library functions for a certain
5699 set of builtins. */
5700 if (!optimize
5701 && !called_as_built_in (fndecl)
5702 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5703 && fcode != BUILT_IN_ALLOCA)
5704 return expand_call (exp, target, ignore);
5706 /* The built-in function expanders test for target == const0_rtx
5707 to determine whether the function's result will be ignored. */
5708 if (ignore)
5709 target = const0_rtx;
5711 /* If the result of a pure or const built-in function is ignored, and
5712 none of its arguments are volatile, we can avoid expanding the
5713 built-in call and just evaluate the arguments for side-effects. */
5714 if (target == const0_rtx
5715 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5717 bool volatilep = false;
5718 tree arg;
5720 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5721 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5723 volatilep = true;
5724 break;
5727 if (! volatilep)
5729 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5730 expand_expr (TREE_VALUE (arg), const0_rtx,
5731 VOIDmode, EXPAND_NORMAL);
5732 return const0_rtx;
5736 switch (fcode)
5738 CASE_FLT_FN (BUILT_IN_FABS):
5739 target = expand_builtin_fabs (arglist, target, subtarget);
5740 if (target)
5741 return target;
5742 break;
5744 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5745 target = expand_builtin_copysign (arglist, target, subtarget);
5746 if (target)
5747 return target;
5748 break;
5750 /* Just do a normal library call if we were unable to fold
5751 the values. */
5752 CASE_FLT_FN (BUILT_IN_CABS):
5753 break;
5755 CASE_FLT_FN (BUILT_IN_EXP):
5756 CASE_FLT_FN (BUILT_IN_EXP10):
5757 CASE_FLT_FN (BUILT_IN_POW10):
5758 CASE_FLT_FN (BUILT_IN_EXP2):
5759 CASE_FLT_FN (BUILT_IN_EXPM1):
5760 CASE_FLT_FN (BUILT_IN_LOGB):
5761 CASE_FLT_FN (BUILT_IN_ILOGB):
5762 CASE_FLT_FN (BUILT_IN_LOG):
5763 CASE_FLT_FN (BUILT_IN_LOG10):
5764 CASE_FLT_FN (BUILT_IN_LOG2):
5765 CASE_FLT_FN (BUILT_IN_LOG1P):
5766 CASE_FLT_FN (BUILT_IN_TAN):
5767 CASE_FLT_FN (BUILT_IN_ASIN):
5768 CASE_FLT_FN (BUILT_IN_ACOS):
5769 CASE_FLT_FN (BUILT_IN_ATAN):
5770 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5771 because of possible accuracy problems. */
5772 if (! flag_unsafe_math_optimizations)
5773 break;
5774 CASE_FLT_FN (BUILT_IN_SQRT):
5775 CASE_FLT_FN (BUILT_IN_FLOOR):
5776 CASE_FLT_FN (BUILT_IN_CEIL):
5777 CASE_FLT_FN (BUILT_IN_TRUNC):
5778 CASE_FLT_FN (BUILT_IN_ROUND):
5779 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5780 CASE_FLT_FN (BUILT_IN_RINT):
5781 target = expand_builtin_mathfn (exp, target, subtarget);
5782 if (target)
5783 return target;
5784 break;
5786 CASE_FLT_FN (BUILT_IN_LCEIL):
5787 CASE_FLT_FN (BUILT_IN_LLCEIL):
5788 CASE_FLT_FN (BUILT_IN_LFLOOR):
5789 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5790 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5791 if (target)
5792 return target;
5793 break;
5795 CASE_FLT_FN (BUILT_IN_LRINT):
5796 CASE_FLT_FN (BUILT_IN_LLRINT):
5797 CASE_FLT_FN (BUILT_IN_LROUND):
5798 CASE_FLT_FN (BUILT_IN_LLROUND):
5799 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5800 if (target)
5801 return target;
5802 break;
5804 CASE_FLT_FN (BUILT_IN_POW):
5805 target = expand_builtin_pow (exp, target, subtarget);
5806 if (target)
5807 return target;
5808 break;
5810 CASE_FLT_FN (BUILT_IN_POWI):
5811 target = expand_builtin_powi (exp, target, subtarget);
5812 if (target)
5813 return target;
5814 break;
5816 CASE_FLT_FN (BUILT_IN_ATAN2):
5817 CASE_FLT_FN (BUILT_IN_LDEXP):
5818 if (! flag_unsafe_math_optimizations)
5819 break;
5821 CASE_FLT_FN (BUILT_IN_FMOD):
5822 CASE_FLT_FN (BUILT_IN_REMAINDER):
5823 CASE_FLT_FN (BUILT_IN_DREM):
5824 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5825 if (target)
5826 return target;
5827 break;
5829 CASE_FLT_FN (BUILT_IN_SIN):
5830 CASE_FLT_FN (BUILT_IN_COS):
5831 if (! flag_unsafe_math_optimizations)
5832 break;
5833 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5834 if (target)
5835 return target;
5836 break;
5838 CASE_FLT_FN (BUILT_IN_SINCOS):
5839 if (! flag_unsafe_math_optimizations)
5840 break;
5841 target = expand_builtin_sincos (exp);
5842 if (target)
5843 return target;
5844 break;
5846 case BUILT_IN_APPLY_ARGS:
5847 return expand_builtin_apply_args ();
5849 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5850 FUNCTION with a copy of the parameters described by
5851 ARGUMENTS, and ARGSIZE. It returns a block of memory
5852 allocated on the stack into which is stored all the registers
5853 that might possibly be used for returning the result of a
5854 function. ARGUMENTS is the value returned by
5855 __builtin_apply_args. ARGSIZE is the number of bytes of
5856 arguments that must be copied. ??? How should this value be
5857 computed? We'll also need a safe worst case value for varargs
5858 functions. */
5859 case BUILT_IN_APPLY:
5860 if (!validate_arglist (arglist, POINTER_TYPE,
5861 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5862 && !validate_arglist (arglist, REFERENCE_TYPE,
5863 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5864 return const0_rtx;
5865 else
5867 int i;
5868 tree t;
5869 rtx ops[3];
5871 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5872 ops[i] = expand_normal (TREE_VALUE (t));
5874 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5877 /* __builtin_return (RESULT) causes the function to return the
5878 value described by RESULT. RESULT is address of the block of
5879 memory returned by __builtin_apply. */
5880 case BUILT_IN_RETURN:
5881 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5882 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5883 return const0_rtx;
5885 case BUILT_IN_SAVEREGS:
5886 return expand_builtin_saveregs ();
5888 case BUILT_IN_ARGS_INFO:
5889 return expand_builtin_args_info (arglist);
5891 /* Return the address of the first anonymous stack arg. */
5892 case BUILT_IN_NEXT_ARG:
5893 if (fold_builtin_next_arg (arglist))
5894 return const0_rtx;
5895 return expand_builtin_next_arg ();
5897 case BUILT_IN_CLASSIFY_TYPE:
5898 return expand_builtin_classify_type (arglist);
5900 case BUILT_IN_CONSTANT_P:
5901 return const0_rtx;
5903 case BUILT_IN_FRAME_ADDRESS:
5904 case BUILT_IN_RETURN_ADDRESS:
5905 return expand_builtin_frame_address (fndecl, arglist);
5907 /* Returns the address of the area where the structure is returned.
5908 0 otherwise. */
5909 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5910 if (arglist != 0
5911 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5912 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5913 return const0_rtx;
5914 else
5915 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5917 case BUILT_IN_ALLOCA:
5918 target = expand_builtin_alloca (arglist, target);
5919 if (target)
5920 return target;
5921 break;
5923 case BUILT_IN_STACK_SAVE:
5924 return expand_stack_save ();
5926 case BUILT_IN_STACK_RESTORE:
5927 expand_stack_restore (TREE_VALUE (arglist));
5928 return const0_rtx;
5930 case BUILT_IN_BSWAP32:
5931 case BUILT_IN_BSWAP64:
5932 target = expand_builtin_bswap (arglist, target, subtarget);
5934 if (target)
5935 return target;
5936 break;
5938 CASE_INT_FN (BUILT_IN_FFS):
5939 case BUILT_IN_FFSIMAX:
5940 target = expand_builtin_unop (target_mode, arglist, target,
5941 subtarget, ffs_optab);
5942 if (target)
5943 return target;
5944 break;
5946 CASE_INT_FN (BUILT_IN_CLZ):
5947 case BUILT_IN_CLZIMAX:
5948 target = expand_builtin_unop (target_mode, arglist, target,
5949 subtarget, clz_optab);
5950 if (target)
5951 return target;
5952 break;
5954 CASE_INT_FN (BUILT_IN_CTZ):
5955 case BUILT_IN_CTZIMAX:
5956 target = expand_builtin_unop (target_mode, arglist, target,
5957 subtarget, ctz_optab);
5958 if (target)
5959 return target;
5960 break;
5962 CASE_INT_FN (BUILT_IN_POPCOUNT):
5963 case BUILT_IN_POPCOUNTIMAX:
5964 target = expand_builtin_unop (target_mode, arglist, target,
5965 subtarget, popcount_optab);
5966 if (target)
5967 return target;
5968 break;
5970 CASE_INT_FN (BUILT_IN_PARITY):
5971 case BUILT_IN_PARITYIMAX:
5972 target = expand_builtin_unop (target_mode, arglist, target,
5973 subtarget, parity_optab);
5974 if (target)
5975 return target;
5976 break;
5978 case BUILT_IN_STRLEN:
5979 target = expand_builtin_strlen (arglist, target, target_mode);
5980 if (target)
5981 return target;
5982 break;
5984 case BUILT_IN_STRCPY:
5985 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5986 if (target)
5987 return target;
5988 break;
5990 case BUILT_IN_STRNCPY:
5991 target = expand_builtin_strncpy (exp, target, mode);
5992 if (target)
5993 return target;
5994 break;
5996 case BUILT_IN_STPCPY:
5997 target = expand_builtin_stpcpy (exp, target, mode);
5998 if (target)
5999 return target;
6000 break;
6002 case BUILT_IN_STRCAT:
6003 target = expand_builtin_strcat (fndecl, arglist, target, mode);
6004 if (target)
6005 return target;
6006 break;
6008 case BUILT_IN_STRNCAT:
6009 target = expand_builtin_strncat (arglist, target, mode);
6010 if (target)
6011 return target;
6012 break;
6014 case BUILT_IN_STRSPN:
6015 target = expand_builtin_strspn (arglist, target, mode);
6016 if (target)
6017 return target;
6018 break;
6020 case BUILT_IN_STRCSPN:
6021 target = expand_builtin_strcspn (arglist, target, mode);
6022 if (target)
6023 return target;
6024 break;
6026 case BUILT_IN_STRSTR:
6027 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6028 if (target)
6029 return target;
6030 break;
6032 case BUILT_IN_STRPBRK:
6033 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6034 if (target)
6035 return target;
6036 break;
6038 case BUILT_IN_INDEX:
6039 case BUILT_IN_STRCHR:
6040 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6041 if (target)
6042 return target;
6043 break;
6045 case BUILT_IN_RINDEX:
6046 case BUILT_IN_STRRCHR:
6047 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6048 if (target)
6049 return target;
6050 break;
6052 case BUILT_IN_MEMCPY:
6053 target = expand_builtin_memcpy (exp, target, mode);
6054 if (target)
6055 return target;
6056 break;
6058 case BUILT_IN_MEMPCPY:
6059 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6060 if (target)
6061 return target;
6062 break;
6064 case BUILT_IN_MEMMOVE:
6065 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6066 mode);
6067 if (target)
6068 return target;
6069 break;
6071 case BUILT_IN_BCOPY:
6072 target = expand_builtin_bcopy (exp);
6073 if (target)
6074 return target;
6075 break;
6077 case BUILT_IN_MEMSET:
6078 target = expand_builtin_memset (arglist, target, mode, exp);
6079 if (target)
6080 return target;
6081 break;
6083 case BUILT_IN_BZERO:
6084 target = expand_builtin_bzero (exp);
6085 if (target)
6086 return target;
6087 break;
6089 case BUILT_IN_STRCMP:
6090 target = expand_builtin_strcmp (exp, target, mode);
6091 if (target)
6092 return target;
6093 break;
6095 case BUILT_IN_STRNCMP:
6096 target = expand_builtin_strncmp (exp, target, mode);
6097 if (target)
6098 return target;
6099 break;
6101 case BUILT_IN_BCMP:
6102 case BUILT_IN_MEMCMP:
6103 target = expand_builtin_memcmp (exp, arglist, target, mode);
6104 if (target)
6105 return target;
6106 break;
6108 case BUILT_IN_SETJMP:
6109 /* This should have been lowered to the builtins below. */
6110 gcc_unreachable ();
6112 case BUILT_IN_SETJMP_SETUP:
6113 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6114 and the receiver label. */
6115 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6117 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6118 VOIDmode, EXPAND_NORMAL);
6119 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6120 rtx label_r = label_rtx (label);
6122 /* This is copied from the handling of non-local gotos. */
6123 expand_builtin_setjmp_setup (buf_addr, label_r);
6124 nonlocal_goto_handler_labels
6125 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6126 nonlocal_goto_handler_labels);
6127 /* ??? Do not let expand_label treat us as such since we would
6128 not want to be both on the list of non-local labels and on
6129 the list of forced labels. */
6130 FORCED_LABEL (label) = 0;
6131 return const0_rtx;
6133 break;
6135 case BUILT_IN_SETJMP_DISPATCHER:
6136 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6137 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6139 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6140 rtx label_r = label_rtx (label);
6142 /* Remove the dispatcher label from the list of non-local labels
6143 since the receiver labels have been added to it above. */
6144 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6145 return const0_rtx;
6147 break;
6149 case BUILT_IN_SETJMP_RECEIVER:
6150 /* __builtin_setjmp_receiver is passed the receiver label. */
6151 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6153 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6154 rtx label_r = label_rtx (label);
6156 expand_builtin_setjmp_receiver (label_r);
6157 return const0_rtx;
6159 break;
6161 /* __builtin_longjmp is passed a pointer to an array of five words.
6162 It's similar to the C library longjmp function but works with
6163 __builtin_setjmp above. */
6164 case BUILT_IN_LONGJMP:
6165 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6167 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6168 VOIDmode, EXPAND_NORMAL);
6169 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6171 if (value != const1_rtx)
6173 error ("%<__builtin_longjmp%> second argument must be 1");
6174 return const0_rtx;
6177 expand_builtin_longjmp (buf_addr, value);
6178 return const0_rtx;
6180 break;
6182 case BUILT_IN_NONLOCAL_GOTO:
6183 target = expand_builtin_nonlocal_goto (arglist);
6184 if (target)
6185 return target;
6186 break;
6188 /* This updates the setjmp buffer that is its argument with the value
6189 of the current stack pointer. */
6190 case BUILT_IN_UPDATE_SETJMP_BUF:
6191 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6193 rtx buf_addr
6194 = expand_normal (TREE_VALUE (arglist));
6196 expand_builtin_update_setjmp_buf (buf_addr);
6197 return const0_rtx;
6199 break;
6201 case BUILT_IN_TRAP:
6202 expand_builtin_trap ();
6203 return const0_rtx;
6205 case BUILT_IN_PRINTF:
6206 target = expand_builtin_printf (exp, target, mode, false);
6207 if (target)
6208 return target;
6209 break;
6211 case BUILT_IN_PRINTF_UNLOCKED:
6212 target = expand_builtin_printf (exp, target, mode, true);
6213 if (target)
6214 return target;
6215 break;
6217 case BUILT_IN_FPUTS:
6218 target = expand_builtin_fputs (arglist, target, false);
6219 if (target)
6220 return target;
6221 break;
6222 case BUILT_IN_FPUTS_UNLOCKED:
6223 target = expand_builtin_fputs (arglist, target, true);
6224 if (target)
6225 return target;
6226 break;
6228 case BUILT_IN_FPRINTF:
6229 target = expand_builtin_fprintf (exp, target, mode, false);
6230 if (target)
6231 return target;
6232 break;
6234 case BUILT_IN_FPRINTF_UNLOCKED:
6235 target = expand_builtin_fprintf (exp, target, mode, true);
6236 if (target)
6237 return target;
6238 break;
6240 case BUILT_IN_SPRINTF:
6241 target = expand_builtin_sprintf (arglist, target, mode);
6242 if (target)
6243 return target;
6244 break;
6246 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6247 target = expand_builtin_signbit (exp, target);
6248 if (target)
6249 return target;
6250 break;
6252 /* Various hooks for the DWARF 2 __throw routine. */
6253 case BUILT_IN_UNWIND_INIT:
6254 expand_builtin_unwind_init ();
6255 return const0_rtx;
6256 case BUILT_IN_DWARF_CFA:
6257 return virtual_cfa_rtx;
6258 #ifdef DWARF2_UNWIND_INFO
6259 case BUILT_IN_DWARF_SP_COLUMN:
6260 return expand_builtin_dwarf_sp_column ();
6261 case BUILT_IN_INIT_DWARF_REG_SIZES:
6262 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6263 return const0_rtx;
6264 #endif
6265 case BUILT_IN_FROB_RETURN_ADDR:
6266 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6267 case BUILT_IN_EXTRACT_RETURN_ADDR:
6268 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6269 case BUILT_IN_EH_RETURN:
6270 expand_builtin_eh_return (TREE_VALUE (arglist),
6271 TREE_VALUE (TREE_CHAIN (arglist)));
6272 return const0_rtx;
6273 #ifdef EH_RETURN_DATA_REGNO
6274 case BUILT_IN_EH_RETURN_DATA_REGNO:
6275 return expand_builtin_eh_return_data_regno (arglist);
6276 #endif
6277 case BUILT_IN_EXTEND_POINTER:
6278 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6280 case BUILT_IN_VA_START:
6281 case BUILT_IN_STDARG_START:
6282 return expand_builtin_va_start (arglist);
6283 case BUILT_IN_VA_END:
6284 return expand_builtin_va_end (arglist);
6285 case BUILT_IN_VA_COPY:
6286 return expand_builtin_va_copy (arglist);
6287 case BUILT_IN_EXPECT:
6288 return expand_builtin_expect (arglist, target);
6289 case BUILT_IN_PREFETCH:
6290 expand_builtin_prefetch (arglist);
6291 return const0_rtx;
6293 case BUILT_IN_PROFILE_FUNC_ENTER:
6294 return expand_builtin_profile_func (false);
6295 case BUILT_IN_PROFILE_FUNC_EXIT:
6296 return expand_builtin_profile_func (true);
6298 case BUILT_IN_INIT_TRAMPOLINE:
6299 return expand_builtin_init_trampoline (arglist);
6300 case BUILT_IN_ADJUST_TRAMPOLINE:
6301 return expand_builtin_adjust_trampoline (arglist);
6303 case BUILT_IN_FORK:
6304 case BUILT_IN_EXECL:
6305 case BUILT_IN_EXECV:
6306 case BUILT_IN_EXECLP:
6307 case BUILT_IN_EXECLE:
6308 case BUILT_IN_EXECVP:
6309 case BUILT_IN_EXECVE:
6310 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6311 if (target)
6312 return target;
6313 break;
6315 case BUILT_IN_FETCH_AND_ADD_1:
6316 case BUILT_IN_FETCH_AND_ADD_2:
6317 case BUILT_IN_FETCH_AND_ADD_4:
6318 case BUILT_IN_FETCH_AND_ADD_8:
6319 case BUILT_IN_FETCH_AND_ADD_16:
6320 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6321 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6322 false, target, ignore);
6323 if (target)
6324 return target;
6325 break;
6327 case BUILT_IN_FETCH_AND_SUB_1:
6328 case BUILT_IN_FETCH_AND_SUB_2:
6329 case BUILT_IN_FETCH_AND_SUB_4:
6330 case BUILT_IN_FETCH_AND_SUB_8:
6331 case BUILT_IN_FETCH_AND_SUB_16:
6332 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6333 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6334 false, target, ignore);
6335 if (target)
6336 return target;
6337 break;
6339 case BUILT_IN_FETCH_AND_OR_1:
6340 case BUILT_IN_FETCH_AND_OR_2:
6341 case BUILT_IN_FETCH_AND_OR_4:
6342 case BUILT_IN_FETCH_AND_OR_8:
6343 case BUILT_IN_FETCH_AND_OR_16:
6344 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6345 target = expand_builtin_sync_operation (mode, arglist, IOR,
6346 false, target, ignore);
6347 if (target)
6348 return target;
6349 break;
6351 case BUILT_IN_FETCH_AND_AND_1:
6352 case BUILT_IN_FETCH_AND_AND_2:
6353 case BUILT_IN_FETCH_AND_AND_4:
6354 case BUILT_IN_FETCH_AND_AND_8:
6355 case BUILT_IN_FETCH_AND_AND_16:
6356 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6357 target = expand_builtin_sync_operation (mode, arglist, AND,
6358 false, target, ignore);
6359 if (target)
6360 return target;
6361 break;
6363 case BUILT_IN_FETCH_AND_XOR_1:
6364 case BUILT_IN_FETCH_AND_XOR_2:
6365 case BUILT_IN_FETCH_AND_XOR_4:
6366 case BUILT_IN_FETCH_AND_XOR_8:
6367 case BUILT_IN_FETCH_AND_XOR_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6369 target = expand_builtin_sync_operation (mode, arglist, XOR,
6370 false, target, ignore);
6371 if (target)
6372 return target;
6373 break;
6375 case BUILT_IN_FETCH_AND_NAND_1:
6376 case BUILT_IN_FETCH_AND_NAND_2:
6377 case BUILT_IN_FETCH_AND_NAND_4:
6378 case BUILT_IN_FETCH_AND_NAND_8:
6379 case BUILT_IN_FETCH_AND_NAND_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6381 target = expand_builtin_sync_operation (mode, arglist, NOT,
6382 false, target, ignore);
6383 if (target)
6384 return target;
6385 break;
6387 case BUILT_IN_ADD_AND_FETCH_1:
6388 case BUILT_IN_ADD_AND_FETCH_2:
6389 case BUILT_IN_ADD_AND_FETCH_4:
6390 case BUILT_IN_ADD_AND_FETCH_8:
6391 case BUILT_IN_ADD_AND_FETCH_16:
6392 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6393 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6394 true, target, ignore);
6395 if (target)
6396 return target;
6397 break;
6399 case BUILT_IN_SUB_AND_FETCH_1:
6400 case BUILT_IN_SUB_AND_FETCH_2:
6401 case BUILT_IN_SUB_AND_FETCH_4:
6402 case BUILT_IN_SUB_AND_FETCH_8:
6403 case BUILT_IN_SUB_AND_FETCH_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6405 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6406 true, target, ignore);
6407 if (target)
6408 return target;
6409 break;
6411 case BUILT_IN_OR_AND_FETCH_1:
6412 case BUILT_IN_OR_AND_FETCH_2:
6413 case BUILT_IN_OR_AND_FETCH_4:
6414 case BUILT_IN_OR_AND_FETCH_8:
6415 case BUILT_IN_OR_AND_FETCH_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6417 target = expand_builtin_sync_operation (mode, arglist, IOR,
6418 true, target, ignore);
6419 if (target)
6420 return target;
6421 break;
6423 case BUILT_IN_AND_AND_FETCH_1:
6424 case BUILT_IN_AND_AND_FETCH_2:
6425 case BUILT_IN_AND_AND_FETCH_4:
6426 case BUILT_IN_AND_AND_FETCH_8:
6427 case BUILT_IN_AND_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, arglist, AND,
6430 true, target, ignore);
6431 if (target)
6432 return target;
6433 break;
6435 case BUILT_IN_XOR_AND_FETCH_1:
6436 case BUILT_IN_XOR_AND_FETCH_2:
6437 case BUILT_IN_XOR_AND_FETCH_4:
6438 case BUILT_IN_XOR_AND_FETCH_8:
6439 case BUILT_IN_XOR_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, arglist, XOR,
6442 true, target, ignore);
6443 if (target)
6444 return target;
6445 break;
6447 case BUILT_IN_NAND_AND_FETCH_1:
6448 case BUILT_IN_NAND_AND_FETCH_2:
6449 case BUILT_IN_NAND_AND_FETCH_4:
6450 case BUILT_IN_NAND_AND_FETCH_8:
6451 case BUILT_IN_NAND_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, arglist, NOT,
6454 true, target, ignore);
6455 if (target)
6456 return target;
6457 break;
6459 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6460 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6461 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6462 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6463 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6464 if (mode == VOIDmode)
6465 mode = TYPE_MODE (boolean_type_node);
6466 if (!target || !register_operand (target, mode))
6467 target = gen_reg_rtx (mode);
6469 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6470 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6471 if (target)
6472 return target;
6473 break;
6475 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6476 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6477 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6478 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6479 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6480 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6481 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6482 if (target)
6483 return target;
6484 break;
6486 case BUILT_IN_LOCK_TEST_AND_SET_1:
6487 case BUILT_IN_LOCK_TEST_AND_SET_2:
6488 case BUILT_IN_LOCK_TEST_AND_SET_4:
6489 case BUILT_IN_LOCK_TEST_AND_SET_8:
6490 case BUILT_IN_LOCK_TEST_AND_SET_16:
6491 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6492 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6493 if (target)
6494 return target;
6495 break;
6497 case BUILT_IN_LOCK_RELEASE_1:
6498 case BUILT_IN_LOCK_RELEASE_2:
6499 case BUILT_IN_LOCK_RELEASE_4:
6500 case BUILT_IN_LOCK_RELEASE_8:
6501 case BUILT_IN_LOCK_RELEASE_16:
6502 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6503 expand_builtin_lock_release (mode, arglist);
6504 return const0_rtx;
6506 case BUILT_IN_SYNCHRONIZE:
6507 expand_builtin_synchronize ();
6508 return const0_rtx;
6510 case BUILT_IN_OBJECT_SIZE:
6511 return expand_builtin_object_size (exp);
6513 case BUILT_IN_MEMCPY_CHK:
6514 case BUILT_IN_MEMPCPY_CHK:
6515 case BUILT_IN_MEMMOVE_CHK:
6516 case BUILT_IN_MEMSET_CHK:
6517 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6518 if (target)
6519 return target;
6520 break;
6522 case BUILT_IN_STRCPY_CHK:
6523 case BUILT_IN_STPCPY_CHK:
6524 case BUILT_IN_STRNCPY_CHK:
6525 case BUILT_IN_STRCAT_CHK:
6526 case BUILT_IN_STRNCAT_CHK:
6527 case BUILT_IN_SNPRINTF_CHK:
6528 case BUILT_IN_VSNPRINTF_CHK:
6529 maybe_emit_chk_warning (exp, fcode);
6530 break;
6532 case BUILT_IN_SPRINTF_CHK:
6533 case BUILT_IN_VSPRINTF_CHK:
6534 maybe_emit_sprintf_chk_warning (exp, fcode);
6535 break;
6537 default: /* just do library call, if unknown builtin */
6538 break;
6541 /* The switch statement above can drop through to cause the function
6542 to be called normally. */
6543 return expand_call (exp, target, ignore);
6546 /* Determine whether a tree node represents a call to a built-in
6547 function. If the tree T is a call to a built-in function with
6548 the right number of arguments of the appropriate types, return
6549 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6550 Otherwise the return value is END_BUILTINS. */
6552 enum built_in_function
6553 builtin_mathfn_code (tree t)
6555 tree fndecl, arglist, parmlist;
6556 tree argtype, parmtype;
6558 if (TREE_CODE (t) != CALL_EXPR
6559 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6560 return END_BUILTINS;
6562 fndecl = get_callee_fndecl (t);
6563 if (fndecl == NULL_TREE
6564 || TREE_CODE (fndecl) != FUNCTION_DECL
6565 || ! DECL_BUILT_IN (fndecl)
6566 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6567 return END_BUILTINS;
6569 arglist = TREE_OPERAND (t, 1);
6570 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6571 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6573 /* If a function doesn't take a variable number of arguments,
6574 the last element in the list will have type `void'. */
6575 parmtype = TREE_VALUE (parmlist);
6576 if (VOID_TYPE_P (parmtype))
6578 if (arglist)
6579 return END_BUILTINS;
6580 return DECL_FUNCTION_CODE (fndecl);
6583 if (! arglist)
6584 return END_BUILTINS;
6586 argtype = TREE_TYPE (TREE_VALUE (arglist));
6588 if (SCALAR_FLOAT_TYPE_P (parmtype))
6590 if (! SCALAR_FLOAT_TYPE_P (argtype))
6591 return END_BUILTINS;
6593 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6595 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6596 return END_BUILTINS;
6598 else if (POINTER_TYPE_P (parmtype))
6600 if (! POINTER_TYPE_P (argtype))
6601 return END_BUILTINS;
6603 else if (INTEGRAL_TYPE_P (parmtype))
6605 if (! INTEGRAL_TYPE_P (argtype))
6606 return END_BUILTINS;
6608 else
6609 return END_BUILTINS;
6611 arglist = TREE_CHAIN (arglist);
6614 /* Variable-length argument list. */
6615 return DECL_FUNCTION_CODE (fndecl);
6618 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6619 constant. ARGLIST is the argument list of the call. */
6621 static tree
6622 fold_builtin_constant_p (tree arglist)
6624 if (arglist == 0)
6625 return 0;
6627 arglist = TREE_VALUE (arglist);
6629 /* We return 1 for a numeric type that's known to be a constant
6630 value at compile-time or for an aggregate type that's a
6631 literal constant. */
6632 STRIP_NOPS (arglist);
6634 /* If we know this is a constant, emit the constant of one. */
6635 if (CONSTANT_CLASS_P (arglist)
6636 || (TREE_CODE (arglist) == CONSTRUCTOR
6637 && TREE_CONSTANT (arglist)))
6638 return integer_one_node;
6639 if (TREE_CODE (arglist) == ADDR_EXPR)
6641 tree op = TREE_OPERAND (arglist, 0);
6642 if (TREE_CODE (op) == STRING_CST
6643 || (TREE_CODE (op) == ARRAY_REF
6644 && integer_zerop (TREE_OPERAND (op, 1))
6645 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6646 return integer_one_node;
6649 /* If this expression has side effects, show we don't know it to be a
6650 constant. Likewise if it's a pointer or aggregate type since in
6651 those case we only want literals, since those are only optimized
6652 when generating RTL, not later.
6653 And finally, if we are compiling an initializer, not code, we
6654 need to return a definite result now; there's not going to be any
6655 more optimization done. */
6656 if (TREE_SIDE_EFFECTS (arglist)
6657 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6658 || POINTER_TYPE_P (TREE_TYPE (arglist))
6659 || cfun == 0
6660 || folding_initializer)
6661 return integer_zero_node;
6663 return 0;
6666 /* Fold a call to __builtin_expect, if we expect that a comparison against
6667 the argument will fold to a constant. In practice, this means a true
6668 constant or the address of a non-weak symbol. ARGLIST is the argument
6669 list of the call. */
6671 static tree
6672 fold_builtin_expect (tree arglist)
6674 tree arg, inner;
6676 if (arglist == 0)
6677 return 0;
6679 arg = TREE_VALUE (arglist);
6681 /* If the argument isn't invariant, then there's nothing we can do. */
6682 if (!TREE_INVARIANT (arg))
6683 return 0;
6685 /* If we're looking at an address of a weak decl, then do not fold. */
6686 inner = arg;
6687 STRIP_NOPS (inner);
6688 if (TREE_CODE (inner) == ADDR_EXPR)
6692 inner = TREE_OPERAND (inner, 0);
6694 while (TREE_CODE (inner) == COMPONENT_REF
6695 || TREE_CODE (inner) == ARRAY_REF);
6696 if (DECL_P (inner) && DECL_WEAK (inner))
6697 return 0;
6700 /* Otherwise, ARG already has the proper type for the return value. */
6701 return arg;
6704 /* Fold a call to __builtin_classify_type. */
6706 static tree
6707 fold_builtin_classify_type (tree arglist)
6709 if (arglist == 0)
6710 return build_int_cst (NULL_TREE, no_type_class);
6712 return build_int_cst (NULL_TREE,
6713 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6716 /* Fold a call to __builtin_strlen. */
6718 static tree
6719 fold_builtin_strlen (tree arglist)
6721 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6722 return NULL_TREE;
6723 else
6725 tree len = c_strlen (TREE_VALUE (arglist), 0);
6727 if (len)
6729 /* Convert from the internal "sizetype" type to "size_t". */
6730 if (size_type_node)
6731 len = fold_convert (size_type_node, len);
6732 return len;
6735 return NULL_TREE;
6739 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6741 static tree
6742 fold_builtin_inf (tree type, int warn)
6744 REAL_VALUE_TYPE real;
6746 /* __builtin_inff is intended to be usable to define INFINITY on all
6747 targets. If an infinity is not available, INFINITY expands "to a
6748 positive constant of type float that overflows at translation
6749 time", footnote "In this case, using INFINITY will violate the
6750 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6751 Thus we pedwarn to ensure this constraint violation is
6752 diagnosed. */
6753 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6754 pedwarn ("target format does not support infinity");
6756 real_inf (&real);
6757 return build_real (type, real);
6760 /* Fold a call to __builtin_nan or __builtin_nans. */
6762 static tree
6763 fold_builtin_nan (tree arglist, tree type, int quiet)
6765 REAL_VALUE_TYPE real;
6766 const char *str;
6768 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6769 return 0;
6770 str = c_getstr (TREE_VALUE (arglist));
6771 if (!str)
6772 return 0;
6774 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6775 return 0;
6777 return build_real (type, real);
6780 /* Return true if the floating point expression T has an integer value.
6781 We also allow +Inf, -Inf and NaN to be considered integer values. */
6783 static bool
6784 integer_valued_real_p (tree t)
6786 switch (TREE_CODE (t))
6788 case FLOAT_EXPR:
6789 return true;
6791 case ABS_EXPR:
6792 case SAVE_EXPR:
6793 case NON_LVALUE_EXPR:
6794 return integer_valued_real_p (TREE_OPERAND (t, 0));
6796 case COMPOUND_EXPR:
6797 case MODIFY_EXPR:
6798 case BIND_EXPR:
6799 return integer_valued_real_p (TREE_OPERAND (t, 1));
6801 case PLUS_EXPR:
6802 case MINUS_EXPR:
6803 case MULT_EXPR:
6804 case MIN_EXPR:
6805 case MAX_EXPR:
6806 return integer_valued_real_p (TREE_OPERAND (t, 0))
6807 && integer_valued_real_p (TREE_OPERAND (t, 1));
6809 case COND_EXPR:
6810 return integer_valued_real_p (TREE_OPERAND (t, 1))
6811 && integer_valued_real_p (TREE_OPERAND (t, 2));
6813 case REAL_CST:
6814 if (! TREE_CONSTANT_OVERFLOW (t))
6816 REAL_VALUE_TYPE c, cint;
6818 c = TREE_REAL_CST (t);
6819 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6820 return real_identical (&c, &cint);
6822 break;
6824 case NOP_EXPR:
6826 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6827 if (TREE_CODE (type) == INTEGER_TYPE)
6828 return true;
6829 if (TREE_CODE (type) == REAL_TYPE)
6830 return integer_valued_real_p (TREE_OPERAND (t, 0));
6831 break;
6834 case CALL_EXPR:
6835 switch (builtin_mathfn_code (t))
6837 CASE_FLT_FN (BUILT_IN_CEIL):
6838 CASE_FLT_FN (BUILT_IN_FLOOR):
6839 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6840 CASE_FLT_FN (BUILT_IN_RINT):
6841 CASE_FLT_FN (BUILT_IN_ROUND):
6842 CASE_FLT_FN (BUILT_IN_TRUNC):
6843 return true;
6845 default:
6846 break;
6848 break;
6850 default:
6851 break;
6853 return false;
6856 /* EXP is assumed to be builtin call where truncation can be propagated
6857 across (for instance floor((double)f) == (double)floorf (f).
6858 Do the transformation. */
6860 static tree
6861 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6863 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6864 tree arg;
6866 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6867 return 0;
6869 arg = TREE_VALUE (arglist);
6870 /* Integer rounding functions are idempotent. */
6871 if (fcode == builtin_mathfn_code (arg))
6872 return arg;
6874 /* If argument is already integer valued, and we don't need to worry
6875 about setting errno, there's no need to perform rounding. */
6876 if (! flag_errno_math && integer_valued_real_p (arg))
6877 return arg;
6879 if (optimize)
6881 tree arg0 = strip_float_extensions (arg);
6882 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6883 tree newtype = TREE_TYPE (arg0);
6884 tree decl;
6886 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6887 && (decl = mathfn_built_in (newtype, fcode)))
6889 arglist =
6890 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6891 return fold_convert (ftype,
6892 build_function_call_expr (decl, arglist));
6895 return 0;
6898 /* EXP is assumed to be builtin call which can narrow the FP type of
6899 the argument, for instance lround((double)f) -> lroundf (f). */
6901 static tree
6902 fold_fixed_mathfn (tree fndecl, tree arglist)
6904 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6905 tree arg;
6907 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6908 return 0;
6910 arg = TREE_VALUE (arglist);
6912 /* If argument is already integer valued, and we don't need to worry
6913 about setting errno, there's no need to perform rounding. */
6914 if (! flag_errno_math && integer_valued_real_p (arg))
6915 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6917 if (optimize)
6919 tree ftype = TREE_TYPE (arg);
6920 tree arg0 = strip_float_extensions (arg);
6921 tree newtype = TREE_TYPE (arg0);
6922 tree decl;
6924 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6925 && (decl = mathfn_built_in (newtype, fcode)))
6927 arglist =
6928 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6929 return build_function_call_expr (decl, arglist);
6933 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6934 sizeof (long long) == sizeof (long). */
6935 if (TYPE_PRECISION (long_long_integer_type_node)
6936 == TYPE_PRECISION (long_integer_type_node))
6938 tree newfn = NULL_TREE;
6939 switch (fcode)
6941 CASE_FLT_FN (BUILT_IN_LLCEIL):
6942 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6943 break;
6945 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6946 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6947 break;
6949 CASE_FLT_FN (BUILT_IN_LLROUND):
6950 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6951 break;
6953 CASE_FLT_FN (BUILT_IN_LLRINT):
6954 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6955 break;
6957 default:
6958 break;
6961 if (newfn)
6963 tree newcall = build_function_call_expr (newfn, arglist);
6964 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6968 return 0;
6971 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6972 is the argument list, TYPE is the return type and FNDECL is the
6973 original function DECL. Return NULL_TREE if no if no simplification
6974 can be made. */
6976 static tree
6977 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6979 tree arg;
6981 if (!arglist || TREE_CHAIN (arglist))
6982 return NULL_TREE;
6984 arg = TREE_VALUE (arglist);
6985 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6986 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6987 return NULL_TREE;
6989 /* Evaluate cabs of a constant at compile-time. */
6990 if (flag_unsafe_math_optimizations
6991 && TREE_CODE (arg) == COMPLEX_CST
6992 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6993 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6994 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6995 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6997 REAL_VALUE_TYPE r, i;
6999 r = TREE_REAL_CST (TREE_REALPART (arg));
7000 i = TREE_REAL_CST (TREE_IMAGPART (arg));
7002 real_arithmetic (&r, MULT_EXPR, &r, &r);
7003 real_arithmetic (&i, MULT_EXPR, &i, &i);
7004 real_arithmetic (&r, PLUS_EXPR, &r, &i);
7005 if (real_sqrt (&r, TYPE_MODE (type), &r)
7006 || ! flag_trapping_math)
7007 return build_real (type, r);
7010 /* If either part is zero, cabs is fabs of the other. */
7011 if (TREE_CODE (arg) == COMPLEX_EXPR
7012 && real_zerop (TREE_OPERAND (arg, 0)))
7013 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
7014 if (TREE_CODE (arg) == COMPLEX_EXPR
7015 && real_zerop (TREE_OPERAND (arg, 1)))
7016 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
7018 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7019 if (TREE_CODE (arg) == NEGATE_EXPR
7020 || TREE_CODE (arg) == CONJ_EXPR)
7022 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
7023 return build_function_call_expr (fndecl, arglist);
7026 /* Don't do this when optimizing for size. */
7027 if (flag_unsafe_math_optimizations
7028 && optimize && !optimize_size)
7030 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7032 if (sqrtfn != NULL_TREE)
7034 tree rpart, ipart, result, arglist;
7036 arg = builtin_save_expr (arg);
7038 rpart = fold_build1 (REALPART_EXPR, type, arg);
7039 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7041 rpart = builtin_save_expr (rpart);
7042 ipart = builtin_save_expr (ipart);
7044 result = fold_build2 (PLUS_EXPR, type,
7045 fold_build2 (MULT_EXPR, type,
7046 rpart, rpart),
7047 fold_build2 (MULT_EXPR, type,
7048 ipart, ipart));
7050 arglist = build_tree_list (NULL_TREE, result);
7051 return build_function_call_expr (sqrtfn, arglist);
7055 return NULL_TREE;
7058 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7059 NULL_TREE if no simplification can be made. */
7061 static tree
7062 fold_builtin_sqrt (tree arglist, tree type)
7065 enum built_in_function fcode;
7066 tree arg = TREE_VALUE (arglist);
7068 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7069 return NULL_TREE;
7071 /* Optimize sqrt of constant value. */
7072 if (TREE_CODE (arg) == REAL_CST
7073 && ! TREE_CONSTANT_OVERFLOW (arg))
7075 REAL_VALUE_TYPE r, x;
7077 x = TREE_REAL_CST (arg);
7078 if (real_sqrt (&r, TYPE_MODE (type), &x)
7079 || (!flag_trapping_math && !flag_errno_math))
7080 return build_real (type, r);
7083 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7084 fcode = builtin_mathfn_code (arg);
7085 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7087 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7088 arg = fold_build2 (MULT_EXPR, type,
7089 TREE_VALUE (TREE_OPERAND (arg, 1)),
7090 build_real (type, dconsthalf));
7091 arglist = build_tree_list (NULL_TREE, arg);
7092 return build_function_call_expr (expfn, arglist);
7095 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7096 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7098 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7100 if (powfn)
7102 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7103 tree tree_root;
7104 /* The inner root was either sqrt or cbrt. */
7105 REAL_VALUE_TYPE dconstroot =
7106 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7108 /* Adjust for the outer root. */
7109 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7110 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7111 tree_root = build_real (type, dconstroot);
7112 arglist = tree_cons (NULL_TREE, arg0,
7113 build_tree_list (NULL_TREE, tree_root));
7114 return build_function_call_expr (powfn, arglist);
7118 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7119 if (flag_unsafe_math_optimizations
7120 && (fcode == BUILT_IN_POW
7121 || fcode == BUILT_IN_POWF
7122 || fcode == BUILT_IN_POWL))
7124 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7125 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7126 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7127 tree narg1;
7128 if (!tree_expr_nonnegative_p (arg0))
7129 arg0 = build1 (ABS_EXPR, type, arg0);
7130 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7131 build_real (type, dconsthalf));
7132 arglist = tree_cons (NULL_TREE, arg0,
7133 build_tree_list (NULL_TREE, narg1));
7134 return build_function_call_expr (powfn, arglist);
7137 return NULL_TREE;
7140 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7141 NULL_TREE if no simplification can be made. */
7142 static tree
7143 fold_builtin_cbrt (tree arglist, tree type)
7145 tree arg = TREE_VALUE (arglist);
7146 const enum built_in_function fcode = builtin_mathfn_code (arg);
7147 tree res;
7149 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7150 return NULL_TREE;
7152 /* Calculate the result when the argument is a constant. */
7153 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7154 return res;
7156 if (flag_unsafe_math_optimizations)
7158 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7159 if (BUILTIN_EXPONENT_P (fcode))
7161 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7162 const REAL_VALUE_TYPE third_trunc =
7163 real_value_truncate (TYPE_MODE (type), dconstthird);
7164 arg = fold_build2 (MULT_EXPR, type,
7165 TREE_VALUE (TREE_OPERAND (arg, 1)),
7166 build_real (type, third_trunc));
7167 arglist = build_tree_list (NULL_TREE, arg);
7168 return build_function_call_expr (expfn, arglist);
7171 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7172 if (BUILTIN_SQRT_P (fcode))
7174 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7176 if (powfn)
7178 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7179 tree tree_root;
7180 REAL_VALUE_TYPE dconstroot = dconstthird;
7182 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7183 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7184 tree_root = build_real (type, dconstroot);
7185 arglist = tree_cons (NULL_TREE, arg0,
7186 build_tree_list (NULL_TREE, tree_root));
7187 return build_function_call_expr (powfn, arglist);
7191 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7192 if (BUILTIN_CBRT_P (fcode))
7194 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7195 if (tree_expr_nonnegative_p (arg0))
7197 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7199 if (powfn)
7201 tree tree_root;
7202 REAL_VALUE_TYPE dconstroot;
7204 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7205 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7206 tree_root = build_real (type, dconstroot);
7207 arglist = tree_cons (NULL_TREE, arg0,
7208 build_tree_list (NULL_TREE, tree_root));
7209 return build_function_call_expr (powfn, arglist);
7214 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7215 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7216 || fcode == BUILT_IN_POWL)
7218 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7219 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7220 if (tree_expr_nonnegative_p (arg00))
7222 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7223 const REAL_VALUE_TYPE dconstroot
7224 = real_value_truncate (TYPE_MODE (type), dconstthird);
7225 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7226 build_real (type, dconstroot));
7227 arglist = tree_cons (NULL_TREE, arg00,
7228 build_tree_list (NULL_TREE, narg01));
7229 return build_function_call_expr (powfn, arglist);
7233 return NULL_TREE;
7236 /* Fold function call to builtin cos, cosf, or cosl. Return
7237 NULL_TREE if no simplification can be made. */
7238 static tree
7239 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7241 tree arg = TREE_VALUE (arglist);
7242 tree res;
7244 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7245 return NULL_TREE;
7247 /* Calculate the result when the argument is a constant. */
7248 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7249 return res;
7251 /* Optimize cos(-x) into cos (x). */
7252 if (TREE_CODE (arg) == NEGATE_EXPR)
7254 tree args = build_tree_list (NULL_TREE,
7255 TREE_OPERAND (arg, 0));
7256 return build_function_call_expr (fndecl, args);
7259 return NULL_TREE;
7262 /* Fold function call to builtin tan, tanf, or tanl. Return
7263 NULL_TREE if no simplification can be made. */
7264 static tree
7265 fold_builtin_tan (tree arglist, tree type)
7267 enum built_in_function fcode;
7268 tree arg = TREE_VALUE (arglist);
7269 tree res;
7271 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7272 return NULL_TREE;
7274 /* Calculate the result when the argument is a constant. */
7275 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7276 return res;
7278 /* Optimize tan(atan(x)) = x. */
7279 fcode = builtin_mathfn_code (arg);
7280 if (flag_unsafe_math_optimizations
7281 && (fcode == BUILT_IN_ATAN
7282 || fcode == BUILT_IN_ATANF
7283 || fcode == BUILT_IN_ATANL))
7284 return TREE_VALUE (TREE_OPERAND (arg, 1));
7286 return NULL_TREE;
7289 /* Fold function call to builtin trunc, truncf or truncl. Return
7290 NULL_TREE if no simplification can be made. */
7292 static tree
7293 fold_builtin_trunc (tree fndecl, tree arglist)
7295 tree arg;
7297 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7298 return 0;
7300 /* Optimize trunc of constant value. */
7301 arg = TREE_VALUE (arglist);
7302 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7304 REAL_VALUE_TYPE r, x;
7305 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7307 x = TREE_REAL_CST (arg);
7308 real_trunc (&r, TYPE_MODE (type), &x);
7309 return build_real (type, r);
7312 return fold_trunc_transparent_mathfn (fndecl, arglist);
7315 /* Fold function call to builtin floor, floorf or floorl. Return
7316 NULL_TREE if no simplification can be made. */
7318 static tree
7319 fold_builtin_floor (tree fndecl, tree arglist)
7321 tree arg;
7323 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7324 return 0;
7326 /* Optimize floor of constant value. */
7327 arg = TREE_VALUE (arglist);
7328 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7330 REAL_VALUE_TYPE x;
7332 x = TREE_REAL_CST (arg);
7333 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7335 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7336 REAL_VALUE_TYPE r;
7338 real_floor (&r, TYPE_MODE (type), &x);
7339 return build_real (type, r);
7343 /* Fold floor (x) where x is nonnegative to trunc (x). */
7344 if (tree_expr_nonnegative_p (arg))
7346 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7347 if (truncfn)
7348 return build_function_call_expr (truncfn, arglist);
7351 return fold_trunc_transparent_mathfn (fndecl, arglist);
7354 /* Fold function call to builtin ceil, ceilf or ceill. Return
7355 NULL_TREE if no simplification can be made. */
7357 static tree
7358 fold_builtin_ceil (tree fndecl, tree arglist)
7360 tree arg;
7362 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7363 return 0;
7365 /* Optimize ceil of constant value. */
7366 arg = TREE_VALUE (arglist);
7367 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7369 REAL_VALUE_TYPE x;
7371 x = TREE_REAL_CST (arg);
7372 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7374 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7375 REAL_VALUE_TYPE r;
7377 real_ceil (&r, TYPE_MODE (type), &x);
7378 return build_real (type, r);
7382 return fold_trunc_transparent_mathfn (fndecl, arglist);
7385 /* Fold function call to builtin round, roundf or roundl. Return
7386 NULL_TREE if no simplification can be made. */
7388 static tree
7389 fold_builtin_round (tree fndecl, tree arglist)
7391 tree arg;
7393 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7394 return 0;
7396 /* Optimize round of constant value. */
7397 arg = TREE_VALUE (arglist);
7398 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7400 REAL_VALUE_TYPE x;
7402 x = TREE_REAL_CST (arg);
7403 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7405 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7406 REAL_VALUE_TYPE r;
7408 real_round (&r, TYPE_MODE (type), &x);
7409 return build_real (type, r);
7413 return fold_trunc_transparent_mathfn (fndecl, arglist);
7416 /* Fold function call to builtin lround, lroundf or lroundl (or the
7417 corresponding long long versions) and other rounding functions.
7418 Return NULL_TREE if no simplification can be made. */
7420 static tree
7421 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7423 tree arg;
7425 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7426 return 0;
7428 /* Optimize lround of constant value. */
7429 arg = TREE_VALUE (arglist);
7430 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7432 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7434 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7436 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7437 tree ftype = TREE_TYPE (arg), result;
7438 HOST_WIDE_INT hi, lo;
7439 REAL_VALUE_TYPE r;
7441 switch (DECL_FUNCTION_CODE (fndecl))
7443 CASE_FLT_FN (BUILT_IN_LFLOOR):
7444 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7445 real_floor (&r, TYPE_MODE (ftype), &x);
7446 break;
7448 CASE_FLT_FN (BUILT_IN_LCEIL):
7449 CASE_FLT_FN (BUILT_IN_LLCEIL):
7450 real_ceil (&r, TYPE_MODE (ftype), &x);
7451 break;
7453 CASE_FLT_FN (BUILT_IN_LROUND):
7454 CASE_FLT_FN (BUILT_IN_LLROUND):
7455 real_round (&r, TYPE_MODE (ftype), &x);
7456 break;
7458 default:
7459 gcc_unreachable ();
7462 REAL_VALUE_TO_INT (&lo, &hi, r);
7463 result = build_int_cst_wide (NULL_TREE, lo, hi);
7464 if (int_fits_type_p (result, itype))
7465 return fold_convert (itype, result);
7469 switch (DECL_FUNCTION_CODE (fndecl))
7471 CASE_FLT_FN (BUILT_IN_LFLOOR):
7472 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7473 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7474 if (tree_expr_nonnegative_p (arg))
7475 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7476 arg);
7477 break;
7478 default:;
7481 return fold_fixed_mathfn (fndecl, arglist);
7484 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7485 and their long and long long variants (i.e. ffsl and ffsll).
7486 Return NULL_TREE if no simplification can be made. */
7488 static tree
7489 fold_builtin_bitop (tree fndecl, tree arglist)
7491 tree arg;
7493 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7494 return NULL_TREE;
7496 /* Optimize for constant argument. */
7497 arg = TREE_VALUE (arglist);
7498 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7500 HOST_WIDE_INT hi, width, result;
7501 unsigned HOST_WIDE_INT lo;
7502 tree type;
7504 type = TREE_TYPE (arg);
7505 width = TYPE_PRECISION (type);
7506 lo = TREE_INT_CST_LOW (arg);
7508 /* Clear all the bits that are beyond the type's precision. */
7509 if (width > HOST_BITS_PER_WIDE_INT)
7511 hi = TREE_INT_CST_HIGH (arg);
7512 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7513 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7515 else
7517 hi = 0;
7518 if (width < HOST_BITS_PER_WIDE_INT)
7519 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7522 switch (DECL_FUNCTION_CODE (fndecl))
7524 CASE_INT_FN (BUILT_IN_FFS):
7525 if (lo != 0)
7526 result = exact_log2 (lo & -lo) + 1;
7527 else if (hi != 0)
7528 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7529 else
7530 result = 0;
7531 break;
7533 CASE_INT_FN (BUILT_IN_CLZ):
7534 if (hi != 0)
7535 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7536 else if (lo != 0)
7537 result = width - floor_log2 (lo) - 1;
7538 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7539 result = width;
7540 break;
7542 CASE_INT_FN (BUILT_IN_CTZ):
7543 if (lo != 0)
7544 result = exact_log2 (lo & -lo);
7545 else if (hi != 0)
7546 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7547 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7548 result = width;
7549 break;
7551 CASE_INT_FN (BUILT_IN_POPCOUNT):
7552 result = 0;
7553 while (lo)
7554 result++, lo &= lo - 1;
7555 while (hi)
7556 result++, hi &= hi - 1;
7557 break;
7559 CASE_INT_FN (BUILT_IN_PARITY):
7560 result = 0;
7561 while (lo)
7562 result++, lo &= lo - 1;
7563 while (hi)
7564 result++, hi &= hi - 1;
7565 result &= 1;
7566 break;
7568 default:
7569 gcc_unreachable ();
7572 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7575 return NULL_TREE;
7578 /* Fold function call to builtin_bswap and the long and long long
7579 variants. Return NULL_TREE if no simplification can be made. */
7580 static tree
7581 fold_builtin_bswap (tree fndecl, tree arglist)
7583 tree arg;
7585 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7586 return 0;
7588 /* Optimize constant value. */
7589 arg = TREE_VALUE (arglist);
7590 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7592 HOST_WIDE_INT hi, width, r_hi = 0;
7593 unsigned HOST_WIDE_INT lo, r_lo = 0;
7594 tree type;
7596 type = TREE_TYPE (arg);
7597 width = TYPE_PRECISION (type);
7598 lo = TREE_INT_CST_LOW (arg);
7599 hi = TREE_INT_CST_HIGH (arg);
7601 switch (DECL_FUNCTION_CODE (fndecl))
7603 case BUILT_IN_BSWAP32:
7604 case BUILT_IN_BSWAP64:
7606 int s;
7608 for (s = 0; s < width; s += 8)
7610 int d = width - s - 8;
7611 unsigned HOST_WIDE_INT byte;
7613 if (s < HOST_BITS_PER_WIDE_INT)
7614 byte = (lo >> s) & 0xff;
7615 else
7616 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7618 if (d < HOST_BITS_PER_WIDE_INT)
7619 r_lo |= byte << d;
7620 else
7621 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7625 break;
7627 default:
7628 gcc_unreachable ();
7631 if (width < HOST_BITS_PER_WIDE_INT)
7632 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7633 else
7634 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7637 return NULL_TREE;
7639 /* Return true if EXPR is the real constant contained in VALUE. */
7641 static bool
7642 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7644 STRIP_NOPS (expr);
7646 return ((TREE_CODE (expr) == REAL_CST
7647 && ! TREE_CONSTANT_OVERFLOW (expr)
7648 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7649 || (TREE_CODE (expr) == COMPLEX_CST
7650 && real_dconstp (TREE_REALPART (expr), value)
7651 && real_zerop (TREE_IMAGPART (expr))));
7654 /* A subroutine of fold_builtin to fold the various logarithmic
7655 functions. Return NULL_TREE if no simplification can me made.
7656 FUNC is the corresponding MPFR logarithm function. */
7658 static tree
7659 fold_builtin_logarithm (tree fndecl, tree arglist,
7660 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7662 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7664 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7665 tree arg = TREE_VALUE (arglist);
7666 tree res;
7667 const enum built_in_function fcode = builtin_mathfn_code (arg);
7669 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7670 instead we'll look for 'e' truncated to MODE. So only do
7671 this if flag_unsafe_math_optimizations is set. */
7672 if (flag_unsafe_math_optimizations && func == mpfr_log)
7674 const REAL_VALUE_TYPE e_truncated =
7675 real_value_truncate (TYPE_MODE (type), dconste);
7676 if (real_dconstp (arg, &e_truncated))
7677 return build_real (type, dconst1);
7680 /* Calculate the result when the argument is a constant. */
7681 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7682 return res;
7684 /* Special case, optimize logN(expN(x)) = x. */
7685 if (flag_unsafe_math_optimizations
7686 && ((func == mpfr_log
7687 && (fcode == BUILT_IN_EXP
7688 || fcode == BUILT_IN_EXPF
7689 || fcode == BUILT_IN_EXPL))
7690 || (func == mpfr_log2
7691 && (fcode == BUILT_IN_EXP2
7692 || fcode == BUILT_IN_EXP2F
7693 || fcode == BUILT_IN_EXP2L))
7694 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7695 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7697 /* Optimize logN(func()) for various exponential functions. We
7698 want to determine the value "x" and the power "exponent" in
7699 order to transform logN(x**exponent) into exponent*logN(x). */
7700 if (flag_unsafe_math_optimizations)
7702 tree exponent = 0, x = 0;
7704 switch (fcode)
7706 CASE_FLT_FN (BUILT_IN_EXP):
7707 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7708 x = build_real (type,
7709 real_value_truncate (TYPE_MODE (type), dconste));
7710 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7711 break;
7712 CASE_FLT_FN (BUILT_IN_EXP2):
7713 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7714 x = build_real (type, dconst2);
7715 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7716 break;
7717 CASE_FLT_FN (BUILT_IN_EXP10):
7718 CASE_FLT_FN (BUILT_IN_POW10):
7719 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7720 x = build_real (type, dconst10);
7721 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7722 break;
7723 CASE_FLT_FN (BUILT_IN_SQRT):
7724 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7725 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7726 exponent = build_real (type, dconsthalf);
7727 break;
7728 CASE_FLT_FN (BUILT_IN_CBRT):
7729 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7730 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7731 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7732 dconstthird));
7733 break;
7734 CASE_FLT_FN (BUILT_IN_POW):
7735 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7736 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7737 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7738 break;
7739 default:
7740 break;
7743 /* Now perform the optimization. */
7744 if (x && exponent)
7746 tree logfn;
7747 arglist = build_tree_list (NULL_TREE, x);
7748 logfn = build_function_call_expr (fndecl, arglist);
7749 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7754 return 0;
7757 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7758 NULL_TREE if no simplification can be made. */
7760 static tree
7761 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7763 tree arg0 = TREE_VALUE (arglist);
7764 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7765 tree res;
7767 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7768 return NULL_TREE;
7770 /* Calculate the result when the argument is a constant. */
7771 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7772 return res;
7774 /* If either argument to hypot has a negate or abs, strip that off.
7775 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7776 if (TREE_CODE (arg0) == NEGATE_EXPR || TREE_CODE (arg1) == NEGATE_EXPR
7777 || TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg1) == ABS_EXPR)
7779 tree narg0 = (TREE_CODE (arg0) == NEGATE_EXPR
7780 || TREE_CODE (arg0) == ABS_EXPR)
7781 ? TREE_OPERAND (arg0, 0) : arg0;
7782 tree narg1 = (TREE_CODE (arg1) == NEGATE_EXPR
7783 || TREE_CODE (arg1) == ABS_EXPR)
7784 ? TREE_OPERAND (arg1, 0) : arg1;
7785 tree narglist = tree_cons (NULL_TREE, narg0,
7786 build_tree_list (NULL_TREE, narg1));
7787 return build_function_call_expr (fndecl, narglist);
7790 /* If either argument is zero, hypot is fabs of the other. */
7791 if (real_zerop (arg0))
7792 return fold_build1 (ABS_EXPR, type, arg1);
7793 else if (real_zerop (arg1))
7794 return fold_build1 (ABS_EXPR, type, arg0);
7796 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7797 if (flag_unsafe_math_optimizations
7798 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7800 REAL_VALUE_TYPE sqrt2;
7802 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7803 return fold_build2 (MULT_EXPR, type,
7804 fold_build1 (ABS_EXPR, type, arg0),
7805 build_real (type, sqrt2));
7808 return NULL_TREE;
7812 /* Fold a builtin function call to pow, powf, or powl. Return
7813 NULL_TREE if no simplification can be made. */
7814 static tree
7815 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7817 tree arg0 = TREE_VALUE (arglist);
7818 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7819 tree res;
7821 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7822 return NULL_TREE;
7824 /* Calculate the result when the argument is a constant. */
7825 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7826 return res;
7828 /* Optimize pow(1.0,y) = 1.0. */
7829 if (real_onep (arg0))
7830 return omit_one_operand (type, build_real (type, dconst1), arg1);
7832 if (TREE_CODE (arg1) == REAL_CST
7833 && ! TREE_CONSTANT_OVERFLOW (arg1))
7835 REAL_VALUE_TYPE cint;
7836 REAL_VALUE_TYPE c;
7837 HOST_WIDE_INT n;
7839 c = TREE_REAL_CST (arg1);
7841 /* Optimize pow(x,0.0) = 1.0. */
7842 if (REAL_VALUES_EQUAL (c, dconst0))
7843 return omit_one_operand (type, build_real (type, dconst1),
7844 arg0);
7846 /* Optimize pow(x,1.0) = x. */
7847 if (REAL_VALUES_EQUAL (c, dconst1))
7848 return arg0;
7850 /* Optimize pow(x,-1.0) = 1.0/x. */
7851 if (REAL_VALUES_EQUAL (c, dconstm1))
7852 return fold_build2 (RDIV_EXPR, type,
7853 build_real (type, dconst1), arg0);
7855 /* Optimize pow(x,0.5) = sqrt(x). */
7856 if (flag_unsafe_math_optimizations
7857 && REAL_VALUES_EQUAL (c, dconsthalf))
7859 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7861 if (sqrtfn != NULL_TREE)
7863 tree arglist = build_tree_list (NULL_TREE, arg0);
7864 return build_function_call_expr (sqrtfn, arglist);
7868 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7869 if (flag_unsafe_math_optimizations)
7871 const REAL_VALUE_TYPE dconstroot
7872 = real_value_truncate (TYPE_MODE (type), dconstthird);
7874 if (REAL_VALUES_EQUAL (c, dconstroot))
7876 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7877 if (cbrtfn != NULL_TREE)
7879 tree arglist = build_tree_list (NULL_TREE, arg0);
7880 return build_function_call_expr (cbrtfn, arglist);
7885 /* Check for an integer exponent. */
7886 n = real_to_integer (&c);
7887 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7888 if (real_identical (&c, &cint))
7890 /* Attempt to evaluate pow at compile-time. */
7891 if (TREE_CODE (arg0) == REAL_CST
7892 && ! TREE_CONSTANT_OVERFLOW (arg0))
7894 REAL_VALUE_TYPE x;
7895 bool inexact;
7897 x = TREE_REAL_CST (arg0);
7898 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7899 if (flag_unsafe_math_optimizations || !inexact)
7900 return build_real (type, x);
7903 /* Strip sign ops from even integer powers. */
7904 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7906 tree narg0 = fold_strip_sign_ops (arg0);
7907 if (narg0)
7909 arglist = build_tree_list (NULL_TREE, arg1);
7910 arglist = tree_cons (NULL_TREE, narg0, arglist);
7911 return build_function_call_expr (fndecl, arglist);
7917 if (flag_unsafe_math_optimizations)
7919 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7921 /* Optimize pow(expN(x),y) = expN(x*y). */
7922 if (BUILTIN_EXPONENT_P (fcode))
7924 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7925 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7926 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7927 arglist = build_tree_list (NULL_TREE, arg);
7928 return build_function_call_expr (expfn, arglist);
7931 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7932 if (BUILTIN_SQRT_P (fcode))
7934 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7935 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7936 build_real (type, dconsthalf));
7938 arglist = tree_cons (NULL_TREE, narg0,
7939 build_tree_list (NULL_TREE, narg1));
7940 return build_function_call_expr (fndecl, arglist);
7943 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7944 if (BUILTIN_CBRT_P (fcode))
7946 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7947 if (tree_expr_nonnegative_p (arg))
7949 const REAL_VALUE_TYPE dconstroot
7950 = real_value_truncate (TYPE_MODE (type), dconstthird);
7951 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7952 build_real (type, dconstroot));
7953 arglist = tree_cons (NULL_TREE, arg,
7954 build_tree_list (NULL_TREE, narg1));
7955 return build_function_call_expr (fndecl, arglist);
7959 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7960 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7961 || fcode == BUILT_IN_POWL)
7963 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7964 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7965 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7966 arglist = tree_cons (NULL_TREE, arg00,
7967 build_tree_list (NULL_TREE, narg1));
7968 return build_function_call_expr (fndecl, arglist);
7972 return NULL_TREE;
7975 /* Fold a builtin function call to powi, powif, or powil. Return
7976 NULL_TREE if no simplification can be made. */
7977 static tree
7978 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7980 tree arg0 = TREE_VALUE (arglist);
7981 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7983 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7984 return NULL_TREE;
7986 /* Optimize pow(1.0,y) = 1.0. */
7987 if (real_onep (arg0))
7988 return omit_one_operand (type, build_real (type, dconst1), arg1);
7990 if (host_integerp (arg1, 0))
7992 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7994 /* Evaluate powi at compile-time. */
7995 if (TREE_CODE (arg0) == REAL_CST
7996 && ! TREE_CONSTANT_OVERFLOW (arg0))
7998 REAL_VALUE_TYPE x;
7999 x = TREE_REAL_CST (arg0);
8000 real_powi (&x, TYPE_MODE (type), &x, c);
8001 return build_real (type, x);
8004 /* Optimize pow(x,0) = 1.0. */
8005 if (c == 0)
8006 return omit_one_operand (type, build_real (type, dconst1),
8007 arg0);
8009 /* Optimize pow(x,1) = x. */
8010 if (c == 1)
8011 return arg0;
8013 /* Optimize pow(x,-1) = 1.0/x. */
8014 if (c == -1)
8015 return fold_build2 (RDIV_EXPR, type,
8016 build_real (type, dconst1), arg0);
8019 return NULL_TREE;
8022 /* A subroutine of fold_builtin to fold the various exponent
8023 functions. Return NULL_TREE if no simplification can me made.
8024 FUNC is the corresponding MPFR exponent function. */
8026 static tree
8027 fold_builtin_exponent (tree fndecl, tree arglist,
8028 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8030 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8032 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8033 tree arg = TREE_VALUE (arglist);
8034 tree res;
8036 /* Calculate the result when the argument is a constant. */
8037 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8038 return res;
8040 /* Optimize expN(logN(x)) = x. */
8041 if (flag_unsafe_math_optimizations)
8043 const enum built_in_function fcode = builtin_mathfn_code (arg);
8045 if ((func == mpfr_exp
8046 && (fcode == BUILT_IN_LOG
8047 || fcode == BUILT_IN_LOGF
8048 || fcode == BUILT_IN_LOGL))
8049 || (func == mpfr_exp2
8050 && (fcode == BUILT_IN_LOG2
8051 || fcode == BUILT_IN_LOG2F
8052 || fcode == BUILT_IN_LOG2L))
8053 || (func == mpfr_exp10
8054 && (fcode == BUILT_IN_LOG10
8055 || fcode == BUILT_IN_LOG10F
8056 || fcode == BUILT_IN_LOG10L)))
8057 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8061 return 0;
8064 /* Return true if VAR is a VAR_DECL or a component thereof. */
8066 static bool
8067 var_decl_component_p (tree var)
8069 tree inner = var;
8070 while (handled_component_p (inner))
8071 inner = TREE_OPERAND (inner, 0);
8072 return SSA_VAR_P (inner);
8075 /* Fold function call to builtin memset. Return
8076 NULL_TREE if no simplification can be made. */
8078 static tree
8079 fold_builtin_memset (tree arglist, tree type, bool ignore)
8081 tree dest, c, len, var, ret;
8082 unsigned HOST_WIDE_INT length, cval;
8084 if (!validate_arglist (arglist,
8085 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8086 return 0;
8088 dest = TREE_VALUE (arglist);
8089 c = TREE_VALUE (TREE_CHAIN (arglist));
8090 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8092 if (! host_integerp (len, 1))
8093 return 0;
8095 /* If the LEN parameter is zero, return DEST. */
8096 if (integer_zerop (len))
8097 return omit_one_operand (type, dest, c);
8099 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8100 return 0;
8102 var = dest;
8103 STRIP_NOPS (var);
8104 if (TREE_CODE (var) != ADDR_EXPR)
8105 return 0;
8107 var = TREE_OPERAND (var, 0);
8108 if (TREE_THIS_VOLATILE (var))
8109 return 0;
8111 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8112 && !POINTER_TYPE_P (TREE_TYPE (var)))
8113 return 0;
8115 if (! var_decl_component_p (var))
8116 return 0;
8118 length = tree_low_cst (len, 1);
8119 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8120 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8121 < (int) length)
8122 return 0;
8124 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8125 return 0;
8127 if (integer_zerop (c))
8128 cval = 0;
8129 else
8131 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8132 return 0;
8134 cval = tree_low_cst (c, 1);
8135 cval &= 0xff;
8136 cval |= cval << 8;
8137 cval |= cval << 16;
8138 cval |= (cval << 31) << 1;
8141 ret = build_int_cst_type (TREE_TYPE (var), cval);
8142 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
8143 if (ignore)
8144 return ret;
8146 return omit_one_operand (type, dest, ret);
8149 /* Fold function call to builtin memset. Return
8150 NULL_TREE if no simplification can be made. */
8152 static tree
8153 fold_builtin_bzero (tree arglist, bool ignore)
8155 tree dest, size, newarglist;
8157 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8158 return 0;
8160 if (!ignore)
8161 return 0;
8163 dest = TREE_VALUE (arglist);
8164 size = TREE_VALUE (TREE_CHAIN (arglist));
8166 /* New argument list transforming bzero(ptr x, int y) to
8167 memset(ptr x, int 0, size_t y). This is done this way
8168 so that if it isn't expanded inline, we fallback to
8169 calling bzero instead of memset. */
8171 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8172 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8173 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8174 return fold_builtin_memset (newarglist, void_type_node, ignore);
8177 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8178 NULL_TREE if no simplification can be made.
8179 If ENDP is 0, return DEST (like memcpy).
8180 If ENDP is 1, return DEST+LEN (like mempcpy).
8181 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8182 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8183 (memmove). */
8185 static tree
8186 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8188 tree dest, src, len, destvar, srcvar, expr;
8189 unsigned HOST_WIDE_INT length;
8191 if (! validate_arglist (arglist,
8192 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8193 return 0;
8195 dest = TREE_VALUE (arglist);
8196 src = TREE_VALUE (TREE_CHAIN (arglist));
8197 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8199 /* If the LEN parameter is zero, return DEST. */
8200 if (integer_zerop (len))
8201 return omit_one_operand (type, dest, src);
8203 /* If SRC and DEST are the same (and not volatile), return
8204 DEST{,+LEN,+LEN-1}. */
8205 if (operand_equal_p (src, dest, 0))
8206 expr = len;
8207 else
8209 if (endp == 3)
8211 unsigned int src_align
8212 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8213 unsigned int dest_align
8214 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8215 /* Both DEST and SRC must be pointer types.
8216 ??? This is what old code did. Is the testing for pointer types
8217 really mandatory?
8219 If either SRC is readonly or length is 1, we can use memcpy. */
8220 if (dest_align && src_align
8221 && (readonly_data_expr (src)
8222 || integer_onep (len)))
8224 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8225 if (!fn)
8226 return 0;
8227 return build_function_call_expr (fn, arglist);
8230 if (! host_integerp (len, 1))
8231 return 0;
8233 if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src))
8234 return 0;
8236 destvar = dest;
8237 STRIP_NOPS (destvar);
8238 if (TREE_CODE (destvar) != ADDR_EXPR)
8239 return 0;
8241 destvar = TREE_OPERAND (destvar, 0);
8242 if (TREE_THIS_VOLATILE (destvar))
8243 return 0;
8245 if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8246 && !POINTER_TYPE_P (TREE_TYPE (destvar))
8247 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar)))
8248 return 0;
8250 if (! var_decl_component_p (destvar))
8251 return 0;
8253 srcvar = src;
8254 STRIP_NOPS (srcvar);
8255 if (TREE_CODE (srcvar) != ADDR_EXPR)
8256 return 0;
8258 srcvar = TREE_OPERAND (srcvar, 0);
8259 if (TREE_THIS_VOLATILE (srcvar))
8260 return 0;
8262 if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8263 && !POINTER_TYPE_P (TREE_TYPE (srcvar))
8264 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar)))
8265 return 0;
8267 if (! var_decl_component_p (srcvar))
8268 return 0;
8270 length = tree_low_cst (len, 1);
8271 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length
8272 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8273 < (int) length
8274 || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length
8275 || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8276 < (int) length)
8277 return 0;
8279 if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8280 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8281 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8282 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8283 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8284 else
8285 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8286 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8289 if (ignore)
8290 return expr;
8292 if (endp == 0 || endp == 3)
8293 return omit_one_operand (type, dest, expr);
8295 if (expr == len)
8296 expr = 0;
8298 if (endp == 2)
8299 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8300 ssize_int (1));
8302 len = fold_convert (TREE_TYPE (dest), len);
8303 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8304 dest = fold_convert (type, dest);
8305 if (expr)
8306 dest = omit_one_operand (type, dest, expr);
8307 return dest;
8310 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8311 simplification can be made. */
8313 static tree
8314 fold_builtin_bcopy (tree arglist, bool ignore)
8316 tree src, dest, size, newarglist;
8318 if (!validate_arglist (arglist,
8319 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8320 return 0;
8322 if (! ignore)
8323 return 0;
8325 src = TREE_VALUE (arglist);
8326 dest = TREE_VALUE (TREE_CHAIN (arglist));
8327 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8329 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8330 memmove(ptr y, ptr x, size_t z). This is done this way
8331 so that if it isn't expanded inline, we fallback to
8332 calling bcopy instead of memmove. */
8334 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8335 newarglist = tree_cons (NULL_TREE, src, newarglist);
8336 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8338 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8341 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8342 the length of the string to be copied. Return NULL_TREE if no
8343 simplification can be made. */
8345 tree
8346 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8348 tree dest, src, fn;
8350 if (!validate_arglist (arglist,
8351 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8352 return 0;
8354 dest = TREE_VALUE (arglist);
8355 src = TREE_VALUE (TREE_CHAIN (arglist));
8357 /* If SRC and DEST are the same (and not volatile), return DEST. */
8358 if (operand_equal_p (src, dest, 0))
8359 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8361 if (optimize_size)
8362 return 0;
8364 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8365 if (!fn)
8366 return 0;
8368 if (!len)
8370 len = c_strlen (src, 1);
8371 if (! len || TREE_SIDE_EFFECTS (len))
8372 return 0;
8375 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8376 arglist = build_tree_list (NULL_TREE, len);
8377 arglist = tree_cons (NULL_TREE, src, arglist);
8378 arglist = tree_cons (NULL_TREE, dest, arglist);
8379 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8380 build_function_call_expr (fn, arglist));
8383 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8384 the length of the source string. Return NULL_TREE if no simplification
8385 can be made. */
8387 tree
8388 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8390 tree dest, src, len, fn;
8392 if (!validate_arglist (arglist,
8393 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8394 return 0;
8396 dest = TREE_VALUE (arglist);
8397 src = TREE_VALUE (TREE_CHAIN (arglist));
8398 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8400 /* If the LEN parameter is zero, return DEST. */
8401 if (integer_zerop (len))
8402 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8404 /* We can't compare slen with len as constants below if len is not a
8405 constant. */
8406 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8407 return 0;
8409 if (!slen)
8410 slen = c_strlen (src, 1);
8412 /* Now, we must be passed a constant src ptr parameter. */
8413 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8414 return 0;
8416 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8418 /* We do not support simplification of this case, though we do
8419 support it when expanding trees into RTL. */
8420 /* FIXME: generate a call to __builtin_memset. */
8421 if (tree_int_cst_lt (slen, len))
8422 return 0;
8424 /* OK transform into builtin memcpy. */
8425 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8426 if (!fn)
8427 return 0;
8428 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8429 build_function_call_expr (fn, arglist));
8432 /* Fold function call to builtin memcmp. Return
8433 NULL_TREE if no simplification can be made. */
8435 static tree
8436 fold_builtin_memcmp (tree arglist)
8438 tree arg1, arg2, len;
8439 const char *p1, *p2;
8441 if (!validate_arglist (arglist,
8442 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8443 return 0;
8445 arg1 = TREE_VALUE (arglist);
8446 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8447 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8449 /* If the LEN parameter is zero, return zero. */
8450 if (integer_zerop (len))
8451 return omit_two_operands (integer_type_node, integer_zero_node,
8452 arg1, arg2);
8454 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8455 if (operand_equal_p (arg1, arg2, 0))
8456 return omit_one_operand (integer_type_node, integer_zero_node, len);
8458 p1 = c_getstr (arg1);
8459 p2 = c_getstr (arg2);
8461 /* If all arguments are constant, and the value of len is not greater
8462 than the lengths of arg1 and arg2, evaluate at compile-time. */
8463 if (host_integerp (len, 1) && p1 && p2
8464 && compare_tree_int (len, strlen (p1) + 1) <= 0
8465 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8467 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8469 if (r > 0)
8470 return integer_one_node;
8471 else if (r < 0)
8472 return integer_minus_one_node;
8473 else
8474 return integer_zero_node;
8477 /* If len parameter is one, return an expression corresponding to
8478 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8479 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8481 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8482 tree cst_uchar_ptr_node
8483 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8485 tree ind1 = fold_convert (integer_type_node,
8486 build1 (INDIRECT_REF, cst_uchar_node,
8487 fold_convert (cst_uchar_ptr_node,
8488 arg1)));
8489 tree ind2 = fold_convert (integer_type_node,
8490 build1 (INDIRECT_REF, cst_uchar_node,
8491 fold_convert (cst_uchar_ptr_node,
8492 arg2)));
8493 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8496 return 0;
8499 /* Fold function call to builtin strcmp. Return
8500 NULL_TREE if no simplification can be made. */
8502 static tree
8503 fold_builtin_strcmp (tree arglist)
8505 tree arg1, arg2;
8506 const char *p1, *p2;
8508 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8509 return 0;
8511 arg1 = TREE_VALUE (arglist);
8512 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8514 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8515 if (operand_equal_p (arg1, arg2, 0))
8516 return integer_zero_node;
8518 p1 = c_getstr (arg1);
8519 p2 = c_getstr (arg2);
8521 if (p1 && p2)
8523 const int i = strcmp (p1, p2);
8524 if (i < 0)
8525 return integer_minus_one_node;
8526 else if (i > 0)
8527 return integer_one_node;
8528 else
8529 return integer_zero_node;
8532 /* If the second arg is "", return *(const unsigned char*)arg1. */
8533 if (p2 && *p2 == '\0')
8535 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8536 tree cst_uchar_ptr_node
8537 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8539 return fold_convert (integer_type_node,
8540 build1 (INDIRECT_REF, cst_uchar_node,
8541 fold_convert (cst_uchar_ptr_node,
8542 arg1)));
8545 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8546 if (p1 && *p1 == '\0')
8548 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8549 tree cst_uchar_ptr_node
8550 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8552 tree temp = fold_convert (integer_type_node,
8553 build1 (INDIRECT_REF, cst_uchar_node,
8554 fold_convert (cst_uchar_ptr_node,
8555 arg2)));
8556 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8559 return 0;
8562 /* Fold function call to builtin strncmp. Return
8563 NULL_TREE if no simplification can be made. */
8565 static tree
8566 fold_builtin_strncmp (tree arglist)
8568 tree arg1, arg2, len;
8569 const char *p1, *p2;
8571 if (!validate_arglist (arglist,
8572 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8573 return 0;
8575 arg1 = TREE_VALUE (arglist);
8576 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8577 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8579 /* If the LEN parameter is zero, return zero. */
8580 if (integer_zerop (len))
8581 return omit_two_operands (integer_type_node, integer_zero_node,
8582 arg1, arg2);
8584 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8585 if (operand_equal_p (arg1, arg2, 0))
8586 return omit_one_operand (integer_type_node, integer_zero_node, len);
8588 p1 = c_getstr (arg1);
8589 p2 = c_getstr (arg2);
8591 if (host_integerp (len, 1) && p1 && p2)
8593 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8594 if (i > 0)
8595 return integer_one_node;
8596 else if (i < 0)
8597 return integer_minus_one_node;
8598 else
8599 return integer_zero_node;
8602 /* If the second arg is "", and the length is greater than zero,
8603 return *(const unsigned char*)arg1. */
8604 if (p2 && *p2 == '\0'
8605 && TREE_CODE (len) == INTEGER_CST
8606 && tree_int_cst_sgn (len) == 1)
8608 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8609 tree cst_uchar_ptr_node
8610 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8612 return fold_convert (integer_type_node,
8613 build1 (INDIRECT_REF, cst_uchar_node,
8614 fold_convert (cst_uchar_ptr_node,
8615 arg1)));
8618 /* If the first arg is "", and the length is greater than zero,
8619 return -*(const unsigned char*)arg2. */
8620 if (p1 && *p1 == '\0'
8621 && TREE_CODE (len) == INTEGER_CST
8622 && tree_int_cst_sgn (len) == 1)
8624 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8625 tree cst_uchar_ptr_node
8626 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8628 tree temp = fold_convert (integer_type_node,
8629 build1 (INDIRECT_REF, cst_uchar_node,
8630 fold_convert (cst_uchar_ptr_node,
8631 arg2)));
8632 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8635 /* If len parameter is one, return an expression corresponding to
8636 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8637 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8639 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8640 tree cst_uchar_ptr_node
8641 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8643 tree ind1 = fold_convert (integer_type_node,
8644 build1 (INDIRECT_REF, cst_uchar_node,
8645 fold_convert (cst_uchar_ptr_node,
8646 arg1)));
8647 tree ind2 = fold_convert (integer_type_node,
8648 build1 (INDIRECT_REF, cst_uchar_node,
8649 fold_convert (cst_uchar_ptr_node,
8650 arg2)));
8651 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8654 return 0;
8657 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8658 NULL_TREE if no simplification can be made. */
8660 static tree
8661 fold_builtin_signbit (tree fndecl, tree arglist)
8663 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8664 tree arg, temp;
8666 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8667 return NULL_TREE;
8669 arg = TREE_VALUE (arglist);
8671 /* If ARG is a compile-time constant, determine the result. */
8672 if (TREE_CODE (arg) == REAL_CST
8673 && !TREE_CONSTANT_OVERFLOW (arg))
8675 REAL_VALUE_TYPE c;
8677 c = TREE_REAL_CST (arg);
8678 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8679 return fold_convert (type, temp);
8682 /* If ARG is non-negative, the result is always zero. */
8683 if (tree_expr_nonnegative_p (arg))
8684 return omit_one_operand (type, integer_zero_node, arg);
8686 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8687 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8688 return fold_build2 (LT_EXPR, type, arg,
8689 build_real (TREE_TYPE (arg), dconst0));
8691 return NULL_TREE;
8694 /* Fold function call to builtin copysign, copysignf or copysignl.
8695 Return NULL_TREE if no simplification can be made. */
8697 static tree
8698 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8700 tree arg1, arg2, tem;
8702 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8703 return NULL_TREE;
8705 arg1 = TREE_VALUE (arglist);
8706 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8708 /* copysign(X,X) is X. */
8709 if (operand_equal_p (arg1, arg2, 0))
8710 return fold_convert (type, arg1);
8712 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8713 if (TREE_CODE (arg1) == REAL_CST
8714 && TREE_CODE (arg2) == REAL_CST
8715 && !TREE_CONSTANT_OVERFLOW (arg1)
8716 && !TREE_CONSTANT_OVERFLOW (arg2))
8718 REAL_VALUE_TYPE c1, c2;
8720 c1 = TREE_REAL_CST (arg1);
8721 c2 = TREE_REAL_CST (arg2);
8722 /* c1.sign := c2.sign. */
8723 real_copysign (&c1, &c2);
8724 return build_real (type, c1);
8727 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8728 Remember to evaluate Y for side-effects. */
8729 if (tree_expr_nonnegative_p (arg2))
8730 return omit_one_operand (type,
8731 fold_build1 (ABS_EXPR, type, arg1),
8732 arg2);
8734 /* Strip sign changing operations for the first argument. */
8735 tem = fold_strip_sign_ops (arg1);
8736 if (tem)
8738 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8739 return build_function_call_expr (fndecl, arglist);
8742 return NULL_TREE;
8745 /* Fold a call to builtin isascii. */
8747 static tree
8748 fold_builtin_isascii (tree arglist)
8750 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8751 return 0;
8752 else
8754 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8755 tree arg = TREE_VALUE (arglist);
8757 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8758 build_int_cst (NULL_TREE,
8759 ~ (unsigned HOST_WIDE_INT) 0x7f));
8760 arg = fold_build2 (EQ_EXPR, integer_type_node,
8761 arg, integer_zero_node);
8763 if (in_gimple_form && !TREE_CONSTANT (arg))
8764 return NULL_TREE;
8765 else
8766 return arg;
8770 /* Fold a call to builtin toascii. */
8772 static tree
8773 fold_builtin_toascii (tree arglist)
8775 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8776 return 0;
8777 else
8779 /* Transform toascii(c) -> (c & 0x7f). */
8780 tree arg = TREE_VALUE (arglist);
8782 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8783 build_int_cst (NULL_TREE, 0x7f));
8787 /* Fold a call to builtin isdigit. */
8789 static tree
8790 fold_builtin_isdigit (tree arglist)
8792 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8793 return 0;
8794 else
8796 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8797 /* According to the C standard, isdigit is unaffected by locale.
8798 However, it definitely is affected by the target character set. */
8799 tree arg;
8800 unsigned HOST_WIDE_INT target_digit0
8801 = lang_hooks.to_target_charset ('0');
8803 if (target_digit0 == 0)
8804 return NULL_TREE;
8806 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8807 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8808 build_int_cst (unsigned_type_node, target_digit0));
8809 arg = fold_build2 (LE_EXPR, integer_type_node, arg,
8810 build_int_cst (unsigned_type_node, 9));
8811 if (in_gimple_form && !TREE_CONSTANT (arg))
8812 return NULL_TREE;
8813 else
8814 return arg;
8818 /* Fold a call to fabs, fabsf or fabsl. */
8820 static tree
8821 fold_builtin_fabs (tree arglist, tree type)
8823 tree arg;
8825 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8826 return 0;
8828 arg = TREE_VALUE (arglist);
8829 arg = fold_convert (type, arg);
8830 if (TREE_CODE (arg) == REAL_CST)
8831 return fold_abs_const (arg, type);
8832 return fold_build1 (ABS_EXPR, type, arg);
8835 /* Fold a call to abs, labs, llabs or imaxabs. */
8837 static tree
8838 fold_builtin_abs (tree arglist, tree type)
8840 tree arg;
8842 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8843 return 0;
8845 arg = TREE_VALUE (arglist);
8846 arg = fold_convert (type, arg);
8847 if (TREE_CODE (arg) == INTEGER_CST)
8848 return fold_abs_const (arg, type);
8849 return fold_build1 (ABS_EXPR, type, arg);
8852 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8853 EXP is the CALL_EXPR for the call. */
8855 static tree
8856 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8858 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8859 tree arg;
8860 REAL_VALUE_TYPE r;
8862 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8864 /* Check that we have exactly one argument. */
8865 if (arglist == 0)
8867 error ("too few arguments to function %qs",
8868 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8869 return error_mark_node;
8871 else if (TREE_CHAIN (arglist) != 0)
8873 error ("too many arguments to function %qs",
8874 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8875 return error_mark_node;
8877 else
8879 error ("non-floating-point argument to function %qs",
8880 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8881 return error_mark_node;
8885 arg = TREE_VALUE (arglist);
8886 switch (builtin_index)
8888 case BUILT_IN_ISINF:
8889 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8890 return omit_one_operand (type, integer_zero_node, arg);
8892 if (TREE_CODE (arg) == REAL_CST)
8894 r = TREE_REAL_CST (arg);
8895 if (real_isinf (&r))
8896 return real_compare (GT_EXPR, &r, &dconst0)
8897 ? integer_one_node : integer_minus_one_node;
8898 else
8899 return integer_zero_node;
8902 return NULL_TREE;
8904 case BUILT_IN_FINITE:
8905 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
8906 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8907 return omit_one_operand (type, integer_one_node, arg);
8909 if (TREE_CODE (arg) == REAL_CST)
8911 r = TREE_REAL_CST (arg);
8912 return real_isinf (&r) || real_isnan (&r)
8913 ? integer_zero_node : integer_one_node;
8916 return NULL_TREE;
8918 case BUILT_IN_ISNAN:
8919 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
8920 return omit_one_operand (type, integer_zero_node, arg);
8922 if (TREE_CODE (arg) == REAL_CST)
8924 r = TREE_REAL_CST (arg);
8925 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8928 arg = builtin_save_expr (arg);
8929 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8931 default:
8932 gcc_unreachable ();
8936 /* Fold a call to an unordered comparison function such as
8937 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8938 being called and ARGLIST is the argument list for the call.
8939 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8940 the opposite of the desired result. UNORDERED_CODE is used
8941 for modes that can hold NaNs and ORDERED_CODE is used for
8942 the rest. */
8944 static tree
8945 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8946 enum tree_code unordered_code,
8947 enum tree_code ordered_code)
8949 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8950 enum tree_code code;
8951 tree arg0, arg1;
8952 tree type0, type1;
8953 enum tree_code code0, code1;
8954 tree cmp_type = NULL_TREE;
8956 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8958 /* Check that we have exactly two arguments. */
8959 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8961 error ("too few arguments to function %qs",
8962 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8963 return error_mark_node;
8965 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8967 error ("too many arguments to function %qs",
8968 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8969 return error_mark_node;
8973 arg0 = TREE_VALUE (arglist);
8974 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8976 type0 = TREE_TYPE (arg0);
8977 type1 = TREE_TYPE (arg1);
8979 code0 = TREE_CODE (type0);
8980 code1 = TREE_CODE (type1);
8982 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8983 /* Choose the wider of two real types. */
8984 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8985 ? type0 : type1;
8986 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8987 cmp_type = type0;
8988 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8989 cmp_type = type1;
8990 else
8992 error ("non-floating-point argument to function %qs",
8993 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8994 return error_mark_node;
8997 arg0 = fold_convert (cmp_type, arg0);
8998 arg1 = fold_convert (cmp_type, arg1);
9000 if (unordered_code == UNORDERED_EXPR)
9002 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9003 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9004 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9007 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9008 : ordered_code;
9009 return fold_build1 (TRUTH_NOT_EXPR, type,
9010 fold_build2 (code, type, arg0, arg1));
9013 /* Used by constant folding to simplify calls to builtin functions. EXP is
9014 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9015 result of the function call is ignored. This function returns NULL_TREE
9016 if no simplification was possible. */
9018 static tree
9019 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9021 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9022 enum built_in_function fcode;
9024 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9025 return targetm.fold_builtin (fndecl, arglist, ignore);
9027 fcode = DECL_FUNCTION_CODE (fndecl);
9028 switch (fcode)
9030 case BUILT_IN_FPUTS:
9031 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9033 case BUILT_IN_FPUTS_UNLOCKED:
9034 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9036 case BUILT_IN_STRSTR:
9037 return fold_builtin_strstr (arglist, type);
9039 case BUILT_IN_STRCAT:
9040 return fold_builtin_strcat (arglist);
9042 case BUILT_IN_STRNCAT:
9043 return fold_builtin_strncat (arglist);
9045 case BUILT_IN_STRSPN:
9046 return fold_builtin_strspn (arglist);
9048 case BUILT_IN_STRCSPN:
9049 return fold_builtin_strcspn (arglist);
9051 case BUILT_IN_STRCHR:
9052 case BUILT_IN_INDEX:
9053 return fold_builtin_strchr (arglist, type);
9055 case BUILT_IN_STRRCHR:
9056 case BUILT_IN_RINDEX:
9057 return fold_builtin_strrchr (arglist, type);
9059 case BUILT_IN_STRCPY:
9060 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9062 case BUILT_IN_STRNCPY:
9063 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9065 case BUILT_IN_STRCMP:
9066 return fold_builtin_strcmp (arglist);
9068 case BUILT_IN_STRNCMP:
9069 return fold_builtin_strncmp (arglist);
9071 case BUILT_IN_STRPBRK:
9072 return fold_builtin_strpbrk (arglist, type);
9074 case BUILT_IN_BCMP:
9075 case BUILT_IN_MEMCMP:
9076 return fold_builtin_memcmp (arglist);
9078 case BUILT_IN_SPRINTF:
9079 return fold_builtin_sprintf (arglist, ignore);
9081 case BUILT_IN_CONSTANT_P:
9083 tree val;
9085 val = fold_builtin_constant_p (arglist);
9086 /* Gimplification will pull the CALL_EXPR for the builtin out of
9087 an if condition. When not optimizing, we'll not CSE it back.
9088 To avoid link error types of regressions, return false now. */
9089 if (!val && !optimize)
9090 val = integer_zero_node;
9092 return val;
9095 case BUILT_IN_EXPECT:
9096 return fold_builtin_expect (arglist);
9098 case BUILT_IN_CLASSIFY_TYPE:
9099 return fold_builtin_classify_type (arglist);
9101 case BUILT_IN_STRLEN:
9102 return fold_builtin_strlen (arglist);
9104 CASE_FLT_FN (BUILT_IN_FABS):
9105 return fold_builtin_fabs (arglist, type);
9107 case BUILT_IN_ABS:
9108 case BUILT_IN_LABS:
9109 case BUILT_IN_LLABS:
9110 case BUILT_IN_IMAXABS:
9111 return fold_builtin_abs (arglist, type);
9113 CASE_FLT_FN (BUILT_IN_CONJ):
9114 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9115 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9116 break;
9118 CASE_FLT_FN (BUILT_IN_CREAL):
9119 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9120 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9121 TREE_VALUE (arglist)));
9122 break;
9124 CASE_FLT_FN (BUILT_IN_CIMAG):
9125 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9126 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9127 TREE_VALUE (arglist)));
9128 break;
9130 CASE_FLT_FN (BUILT_IN_CABS):
9131 return fold_builtin_cabs (arglist, type, fndecl);
9133 CASE_FLT_FN (BUILT_IN_SQRT):
9134 return fold_builtin_sqrt (arglist, type);
9136 CASE_FLT_FN (BUILT_IN_CBRT):
9137 return fold_builtin_cbrt (arglist, type);
9139 CASE_FLT_FN (BUILT_IN_ASIN):
9140 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9141 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9142 &dconstm1, &dconst1, true);
9143 break;
9145 CASE_FLT_FN (BUILT_IN_ACOS):
9146 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9147 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9148 &dconstm1, &dconst1, true);
9149 break;
9151 CASE_FLT_FN (BUILT_IN_ATAN):
9152 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9153 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9154 NULL, NULL, 0);
9155 break;
9157 CASE_FLT_FN (BUILT_IN_ASINH):
9158 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9159 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9160 NULL, NULL, 0);
9161 break;
9163 CASE_FLT_FN (BUILT_IN_ACOSH):
9164 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9165 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9166 &dconst1, NULL, true);
9167 break;
9169 CASE_FLT_FN (BUILT_IN_ATANH):
9170 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9171 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9172 &dconstm1, &dconst1, false);
9173 break;
9175 CASE_FLT_FN (BUILT_IN_SIN):
9176 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9177 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9178 NULL, NULL, 0);
9179 break;
9181 CASE_FLT_FN (BUILT_IN_COS):
9182 return fold_builtin_cos (arglist, type, fndecl);
9184 CASE_FLT_FN (BUILT_IN_TAN):
9185 return fold_builtin_tan (arglist, type);
9187 CASE_FLT_FN (BUILT_IN_SINH):
9188 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9189 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9190 NULL, NULL, 0);
9191 break;
9193 CASE_FLT_FN (BUILT_IN_COSH):
9194 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9195 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_cosh,
9196 NULL, NULL, 0);
9197 break;
9199 CASE_FLT_FN (BUILT_IN_TANH):
9200 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9201 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9202 NULL, NULL, 0);
9203 break;
9205 CASE_FLT_FN (BUILT_IN_ERF):
9206 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9207 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9208 NULL, NULL, 0);
9209 break;
9211 CASE_FLT_FN (BUILT_IN_ERFC):
9212 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9213 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9214 NULL, NULL, 0);
9215 break;
9217 CASE_FLT_FN (BUILT_IN_TGAMMA):
9218 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9219 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9220 NULL, NULL, 0);
9221 break;
9223 CASE_FLT_FN (BUILT_IN_EXP):
9224 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9226 CASE_FLT_FN (BUILT_IN_EXP2):
9227 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9229 CASE_FLT_FN (BUILT_IN_EXP10):
9230 CASE_FLT_FN (BUILT_IN_POW10):
9231 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9233 CASE_FLT_FN (BUILT_IN_EXPM1):
9234 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9235 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9236 NULL, NULL, 0);
9237 break;
9239 CASE_FLT_FN (BUILT_IN_LOG):
9240 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9242 CASE_FLT_FN (BUILT_IN_LOG2):
9243 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9245 CASE_FLT_FN (BUILT_IN_LOG10):
9246 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9248 CASE_FLT_FN (BUILT_IN_LOG1P):
9249 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9250 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9251 &dconstm1, NULL, false);
9252 break;
9254 CASE_FLT_FN (BUILT_IN_ATAN2):
9255 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9256 return do_mpfr_arg2 (TREE_VALUE (arglist),
9257 TREE_VALUE (TREE_CHAIN (arglist)),
9258 type, mpfr_atan2);
9259 break;
9261 CASE_FLT_FN (BUILT_IN_HYPOT):
9262 return fold_builtin_hypot (fndecl, arglist, type);
9264 CASE_FLT_FN (BUILT_IN_POW):
9265 return fold_builtin_pow (fndecl, arglist, type);
9267 CASE_FLT_FN (BUILT_IN_POWI):
9268 return fold_builtin_powi (fndecl, arglist, type);
9270 CASE_FLT_FN (BUILT_IN_INF):
9271 case BUILT_IN_INFD32:
9272 case BUILT_IN_INFD64:
9273 case BUILT_IN_INFD128:
9274 return fold_builtin_inf (type, true);
9276 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9277 return fold_builtin_inf (type, false);
9279 CASE_FLT_FN (BUILT_IN_NAN):
9280 case BUILT_IN_NAND32:
9281 case BUILT_IN_NAND64:
9282 case BUILT_IN_NAND128:
9283 return fold_builtin_nan (arglist, type, true);
9285 CASE_FLT_FN (BUILT_IN_NANS):
9286 return fold_builtin_nan (arglist, type, false);
9288 CASE_FLT_FN (BUILT_IN_FLOOR):
9289 return fold_builtin_floor (fndecl, arglist);
9291 CASE_FLT_FN (BUILT_IN_CEIL):
9292 return fold_builtin_ceil (fndecl, arglist);
9294 CASE_FLT_FN (BUILT_IN_TRUNC):
9295 return fold_builtin_trunc (fndecl, arglist);
9297 CASE_FLT_FN (BUILT_IN_ROUND):
9298 return fold_builtin_round (fndecl, arglist);
9300 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9301 CASE_FLT_FN (BUILT_IN_RINT):
9302 return fold_trunc_transparent_mathfn (fndecl, arglist);
9304 CASE_FLT_FN (BUILT_IN_LCEIL):
9305 CASE_FLT_FN (BUILT_IN_LLCEIL):
9306 CASE_FLT_FN (BUILT_IN_LFLOOR):
9307 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9308 CASE_FLT_FN (BUILT_IN_LROUND):
9309 CASE_FLT_FN (BUILT_IN_LLROUND):
9310 return fold_builtin_int_roundingfn (fndecl, arglist);
9312 CASE_FLT_FN (BUILT_IN_LRINT):
9313 CASE_FLT_FN (BUILT_IN_LLRINT):
9314 return fold_fixed_mathfn (fndecl, arglist);
9316 case BUILT_IN_BSWAP32:
9317 case BUILT_IN_BSWAP64:
9318 return fold_builtin_bswap (fndecl, arglist);
9320 CASE_INT_FN (BUILT_IN_FFS):
9321 CASE_INT_FN (BUILT_IN_CLZ):
9322 CASE_INT_FN (BUILT_IN_CTZ):
9323 CASE_INT_FN (BUILT_IN_POPCOUNT):
9324 CASE_INT_FN (BUILT_IN_PARITY):
9325 return fold_builtin_bitop (fndecl, arglist);
9327 case BUILT_IN_MEMSET:
9328 return fold_builtin_memset (arglist, type, ignore);
9330 case BUILT_IN_MEMCPY:
9331 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9333 case BUILT_IN_MEMPCPY:
9334 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9336 case BUILT_IN_MEMMOVE:
9337 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9339 case BUILT_IN_BZERO:
9340 return fold_builtin_bzero (arglist, ignore);
9342 case BUILT_IN_BCOPY:
9343 return fold_builtin_bcopy (arglist, ignore);
9345 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9346 return fold_builtin_signbit (fndecl, arglist);
9348 case BUILT_IN_ISASCII:
9349 return fold_builtin_isascii (arglist);
9351 case BUILT_IN_TOASCII:
9352 return fold_builtin_toascii (arglist);
9354 case BUILT_IN_ISDIGIT:
9355 return fold_builtin_isdigit (arglist);
9357 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9358 return fold_builtin_copysign (fndecl, arglist, type);
9360 CASE_FLT_FN (BUILT_IN_FINITE):
9361 case BUILT_IN_FINITED32:
9362 case BUILT_IN_FINITED64:
9363 case BUILT_IN_FINITED128:
9364 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9366 CASE_FLT_FN (BUILT_IN_ISINF):
9367 case BUILT_IN_ISINFD32:
9368 case BUILT_IN_ISINFD64:
9369 case BUILT_IN_ISINFD128:
9370 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9372 CASE_FLT_FN (BUILT_IN_ISNAN):
9373 case BUILT_IN_ISNAND32:
9374 case BUILT_IN_ISNAND64:
9375 case BUILT_IN_ISNAND128:
9376 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9378 case BUILT_IN_ISGREATER:
9379 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9380 case BUILT_IN_ISGREATEREQUAL:
9381 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9382 case BUILT_IN_ISLESS:
9383 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9384 case BUILT_IN_ISLESSEQUAL:
9385 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9386 case BUILT_IN_ISLESSGREATER:
9387 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9388 case BUILT_IN_ISUNORDERED:
9389 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9390 NOP_EXPR);
9392 /* We do the folding for va_start in the expander. */
9393 case BUILT_IN_VA_START:
9394 break;
9396 case BUILT_IN_OBJECT_SIZE:
9397 return fold_builtin_object_size (arglist);
9398 case BUILT_IN_MEMCPY_CHK:
9399 case BUILT_IN_MEMPCPY_CHK:
9400 case BUILT_IN_MEMMOVE_CHK:
9401 case BUILT_IN_MEMSET_CHK:
9402 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9403 DECL_FUNCTION_CODE (fndecl));
9404 case BUILT_IN_STRCPY_CHK:
9405 case BUILT_IN_STPCPY_CHK:
9406 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9407 DECL_FUNCTION_CODE (fndecl));
9408 case BUILT_IN_STRNCPY_CHK:
9409 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9410 case BUILT_IN_STRCAT_CHK:
9411 return fold_builtin_strcat_chk (fndecl, arglist);
9412 case BUILT_IN_STRNCAT_CHK:
9413 return fold_builtin_strncat_chk (fndecl, arglist);
9414 case BUILT_IN_SPRINTF_CHK:
9415 case BUILT_IN_VSPRINTF_CHK:
9416 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9417 case BUILT_IN_SNPRINTF_CHK:
9418 case BUILT_IN_VSNPRINTF_CHK:
9419 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9420 DECL_FUNCTION_CODE (fndecl));
9422 case BUILT_IN_PRINTF:
9423 case BUILT_IN_PRINTF_UNLOCKED:
9424 case BUILT_IN_VPRINTF:
9425 case BUILT_IN_PRINTF_CHK:
9426 case BUILT_IN_VPRINTF_CHK:
9427 return fold_builtin_printf (fndecl, arglist, ignore,
9428 DECL_FUNCTION_CODE (fndecl));
9430 case BUILT_IN_FPRINTF:
9431 case BUILT_IN_FPRINTF_UNLOCKED:
9432 case BUILT_IN_VFPRINTF:
9433 case BUILT_IN_FPRINTF_CHK:
9434 case BUILT_IN_VFPRINTF_CHK:
9435 return fold_builtin_fprintf (fndecl, arglist, ignore,
9436 DECL_FUNCTION_CODE (fndecl));
9438 default:
9439 break;
9442 return 0;
9445 /* A wrapper function for builtin folding that prevents warnings for
9446 "statement without effect" and the like, caused by removing the
9447 call node earlier than the warning is generated. */
9449 tree
9450 fold_builtin (tree fndecl, tree arglist, bool ignore)
9452 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9453 if (exp && !ignore)
9455 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
9456 TREE_NO_WARNING (exp) = 1;
9459 return exp;
9462 /* Conveniently construct a function call expression. */
9464 tree
9465 build_function_call_expr (tree fn, tree arglist)
9467 tree call_expr;
9469 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9470 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9471 call_expr, arglist, NULL_TREE);
9474 /* This function validates the types of a function call argument list
9475 represented as a tree chain of parameters against a specified list
9476 of tree_codes. If the last specifier is a 0, that represents an
9477 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9479 static int
9480 validate_arglist (tree arglist, ...)
9482 enum tree_code code;
9483 int res = 0;
9484 va_list ap;
9486 va_start (ap, arglist);
9490 code = va_arg (ap, enum tree_code);
9491 switch (code)
9493 case 0:
9494 /* This signifies an ellipses, any further arguments are all ok. */
9495 res = 1;
9496 goto end;
9497 case VOID_TYPE:
9498 /* This signifies an endlink, if no arguments remain, return
9499 true, otherwise return false. */
9500 res = arglist == 0;
9501 goto end;
9502 default:
9503 /* If no parameters remain or the parameter's code does not
9504 match the specified code, return false. Otherwise continue
9505 checking any remaining arguments. */
9506 if (arglist == 0)
9507 goto end;
9508 if (code == POINTER_TYPE)
9510 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9511 goto end;
9513 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9514 goto end;
9515 break;
9517 arglist = TREE_CHAIN (arglist);
9519 while (1);
9521 /* We need gotos here since we can only have one VA_CLOSE in a
9522 function. */
9523 end: ;
9524 va_end (ap);
9526 return res;
9529 /* Default target-specific builtin expander that does nothing. */
9532 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9533 rtx target ATTRIBUTE_UNUSED,
9534 rtx subtarget ATTRIBUTE_UNUSED,
9535 enum machine_mode mode ATTRIBUTE_UNUSED,
9536 int ignore ATTRIBUTE_UNUSED)
9538 return NULL_RTX;
9541 /* Returns true is EXP represents data that would potentially reside
9542 in a readonly section. */
9544 static bool
9545 readonly_data_expr (tree exp)
9547 STRIP_NOPS (exp);
9549 if (TREE_CODE (exp) != ADDR_EXPR)
9550 return false;
9552 exp = get_base_address (TREE_OPERAND (exp, 0));
9553 if (!exp)
9554 return false;
9556 /* Make sure we call decl_readonly_section only for trees it
9557 can handle (since it returns true for everything it doesn't
9558 understand). */
9559 if (TREE_CODE (exp) == STRING_CST
9560 || TREE_CODE (exp) == CONSTRUCTOR
9561 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9562 return decl_readonly_section (exp, 0);
9563 else
9564 return false;
9567 /* Simplify a call to the strstr builtin.
9569 Return 0 if no simplification was possible, otherwise return the
9570 simplified form of the call as a tree.
9572 The simplified form may be a constant or other expression which
9573 computes the same value, but in a more efficient manner (including
9574 calls to other builtin functions).
9576 The call may contain arguments which need to be evaluated, but
9577 which are not useful to determine the result of the call. In
9578 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9579 COMPOUND_EXPR will be an argument which must be evaluated.
9580 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9581 COMPOUND_EXPR in the chain will contain the tree for the simplified
9582 form of the builtin function call. */
9584 static tree
9585 fold_builtin_strstr (tree arglist, tree type)
9587 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9588 return 0;
9589 else
9591 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9592 tree fn;
9593 const char *p1, *p2;
9595 p2 = c_getstr (s2);
9596 if (p2 == NULL)
9597 return 0;
9599 p1 = c_getstr (s1);
9600 if (p1 != NULL)
9602 const char *r = strstr (p1, p2);
9603 tree tem;
9605 if (r == NULL)
9606 return build_int_cst (TREE_TYPE (s1), 0);
9608 /* Return an offset into the constant string argument. */
9609 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9610 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9611 return fold_convert (type, tem);
9614 /* The argument is const char *, and the result is char *, so we need
9615 a type conversion here to avoid a warning. */
9616 if (p2[0] == '\0')
9617 return fold_convert (type, s1);
9619 if (p2[1] != '\0')
9620 return 0;
9622 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9623 if (!fn)
9624 return 0;
9626 /* New argument list transforming strstr(s1, s2) to
9627 strchr(s1, s2[0]). */
9628 arglist = build_tree_list (NULL_TREE,
9629 build_int_cst (NULL_TREE, p2[0]));
9630 arglist = tree_cons (NULL_TREE, s1, arglist);
9631 return build_function_call_expr (fn, arglist);
9635 /* Simplify a call to the strchr builtin.
9637 Return 0 if no simplification was possible, otherwise return the
9638 simplified form of the call as a tree.
9640 The simplified form may be a constant or other expression which
9641 computes the same value, but in a more efficient manner (including
9642 calls to other builtin functions).
9644 The call may contain arguments which need to be evaluated, but
9645 which are not useful to determine the result of the call. In
9646 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9647 COMPOUND_EXPR will be an argument which must be evaluated.
9648 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9649 COMPOUND_EXPR in the chain will contain the tree for the simplified
9650 form of the builtin function call. */
9652 static tree
9653 fold_builtin_strchr (tree arglist, tree type)
9655 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9656 return 0;
9657 else
9659 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9660 const char *p1;
9662 if (TREE_CODE (s2) != INTEGER_CST)
9663 return 0;
9665 p1 = c_getstr (s1);
9666 if (p1 != NULL)
9668 char c;
9669 const char *r;
9670 tree tem;
9672 if (target_char_cast (s2, &c))
9673 return 0;
9675 r = strchr (p1, c);
9677 if (r == NULL)
9678 return build_int_cst (TREE_TYPE (s1), 0);
9680 /* Return an offset into the constant string argument. */
9681 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9682 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9683 return fold_convert (type, tem);
9685 return 0;
9689 /* Simplify a call to the strrchr builtin.
9691 Return 0 if no simplification was possible, otherwise return the
9692 simplified form of the call as a tree.
9694 The simplified form may be a constant or other expression which
9695 computes the same value, but in a more efficient manner (including
9696 calls to other builtin functions).
9698 The call may contain arguments which need to be evaluated, but
9699 which are not useful to determine the result of the call. In
9700 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9701 COMPOUND_EXPR will be an argument which must be evaluated.
9702 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9703 COMPOUND_EXPR in the chain will contain the tree for the simplified
9704 form of the builtin function call. */
9706 static tree
9707 fold_builtin_strrchr (tree arglist, tree type)
9709 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9710 return 0;
9711 else
9713 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9714 tree fn;
9715 const char *p1;
9717 if (TREE_CODE (s2) != INTEGER_CST)
9718 return 0;
9720 p1 = c_getstr (s1);
9721 if (p1 != NULL)
9723 char c;
9724 const char *r;
9725 tree tem;
9727 if (target_char_cast (s2, &c))
9728 return 0;
9730 r = strrchr (p1, c);
9732 if (r == NULL)
9733 return build_int_cst (TREE_TYPE (s1), 0);
9735 /* Return an offset into the constant string argument. */
9736 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9737 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9738 return fold_convert (type, tem);
9741 if (! integer_zerop (s2))
9742 return 0;
9744 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9745 if (!fn)
9746 return 0;
9748 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9749 return build_function_call_expr (fn, arglist);
9753 /* Simplify a call to the strpbrk builtin.
9755 Return 0 if no simplification was possible, otherwise return the
9756 simplified form of the call as a tree.
9758 The simplified form may be a constant or other expression which
9759 computes the same value, but in a more efficient manner (including
9760 calls to other builtin functions).
9762 The call may contain arguments which need to be evaluated, but
9763 which are not useful to determine the result of the call. In
9764 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9765 COMPOUND_EXPR will be an argument which must be evaluated.
9766 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9767 COMPOUND_EXPR in the chain will contain the tree for the simplified
9768 form of the builtin function call. */
9770 static tree
9771 fold_builtin_strpbrk (tree arglist, tree type)
9773 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9774 return 0;
9775 else
9777 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9778 tree fn;
9779 const char *p1, *p2;
9781 p2 = c_getstr (s2);
9782 if (p2 == NULL)
9783 return 0;
9785 p1 = c_getstr (s1);
9786 if (p1 != NULL)
9788 const char *r = strpbrk (p1, p2);
9789 tree tem;
9791 if (r == NULL)
9792 return build_int_cst (TREE_TYPE (s1), 0);
9794 /* Return an offset into the constant string argument. */
9795 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9796 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9797 return fold_convert (type, tem);
9800 if (p2[0] == '\0')
9801 /* strpbrk(x, "") == NULL.
9802 Evaluate and ignore s1 in case it had side-effects. */
9803 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9805 if (p2[1] != '\0')
9806 return 0; /* Really call strpbrk. */
9808 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9809 if (!fn)
9810 return 0;
9812 /* New argument list transforming strpbrk(s1, s2) to
9813 strchr(s1, s2[0]). */
9814 arglist = build_tree_list (NULL_TREE,
9815 build_int_cst (NULL_TREE, p2[0]));
9816 arglist = tree_cons (NULL_TREE, s1, arglist);
9817 return build_function_call_expr (fn, arglist);
9821 /* Simplify a call to the strcat builtin.
9823 Return 0 if no simplification was possible, otherwise return the
9824 simplified form of the call as a tree.
9826 The simplified form may be a constant or other expression which
9827 computes the same value, but in a more efficient manner (including
9828 calls to other builtin functions).
9830 The call may contain arguments which need to be evaluated, but
9831 which are not useful to determine the result of the call. In
9832 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9833 COMPOUND_EXPR will be an argument which must be evaluated.
9834 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9835 COMPOUND_EXPR in the chain will contain the tree for the simplified
9836 form of the builtin function call. */
9838 static tree
9839 fold_builtin_strcat (tree arglist)
9841 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9842 return 0;
9843 else
9845 tree dst = TREE_VALUE (arglist),
9846 src = TREE_VALUE (TREE_CHAIN (arglist));
9847 const char *p = c_getstr (src);
9849 /* If the string length is zero, return the dst parameter. */
9850 if (p && *p == '\0')
9851 return dst;
9853 return 0;
9857 /* Simplify a call to the strncat builtin.
9859 Return 0 if no simplification was possible, otherwise return the
9860 simplified form of the call as a tree.
9862 The simplified form may be a constant or other expression which
9863 computes the same value, but in a more efficient manner (including
9864 calls to other builtin functions).
9866 The call may contain arguments which need to be evaluated, but
9867 which are not useful to determine the result of the call. In
9868 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9869 COMPOUND_EXPR will be an argument which must be evaluated.
9870 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9871 COMPOUND_EXPR in the chain will contain the tree for the simplified
9872 form of the builtin function call. */
9874 static tree
9875 fold_builtin_strncat (tree arglist)
9877 if (!validate_arglist (arglist,
9878 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9879 return 0;
9880 else
9882 tree dst = TREE_VALUE (arglist);
9883 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9884 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9885 const char *p = c_getstr (src);
9887 /* If the requested length is zero, or the src parameter string
9888 length is zero, return the dst parameter. */
9889 if (integer_zerop (len) || (p && *p == '\0'))
9890 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9892 /* If the requested len is greater than or equal to the string
9893 length, call strcat. */
9894 if (TREE_CODE (len) == INTEGER_CST && p
9895 && compare_tree_int (len, strlen (p)) >= 0)
9897 tree newarglist
9898 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9899 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9901 /* If the replacement _DECL isn't initialized, don't do the
9902 transformation. */
9903 if (!fn)
9904 return 0;
9906 return build_function_call_expr (fn, newarglist);
9908 return 0;
9912 /* Simplify a call to the strspn builtin.
9914 Return 0 if no simplification was possible, otherwise return the
9915 simplified form of the call as a tree.
9917 The simplified form may be a constant or other expression which
9918 computes the same value, but in a more efficient manner (including
9919 calls to other builtin functions).
9921 The call may contain arguments which need to be evaluated, but
9922 which are not useful to determine the result of the call. In
9923 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9924 COMPOUND_EXPR will be an argument which must be evaluated.
9925 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9926 COMPOUND_EXPR in the chain will contain the tree for the simplified
9927 form of the builtin function call. */
9929 static tree
9930 fold_builtin_strspn (tree arglist)
9932 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9933 return 0;
9934 else
9936 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9937 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9939 /* If both arguments are constants, evaluate at compile-time. */
9940 if (p1 && p2)
9942 const size_t r = strspn (p1, p2);
9943 return size_int (r);
9946 /* If either argument is "", return 0. */
9947 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9948 /* Evaluate and ignore both arguments in case either one has
9949 side-effects. */
9950 return omit_two_operands (integer_type_node, integer_zero_node,
9951 s1, s2);
9952 return 0;
9956 /* Simplify a call to the strcspn builtin.
9958 Return 0 if no simplification was possible, otherwise return the
9959 simplified form of the call as a tree.
9961 The simplified form may be a constant or other expression which
9962 computes the same value, but in a more efficient manner (including
9963 calls to other builtin functions).
9965 The call may contain arguments which need to be evaluated, but
9966 which are not useful to determine the result of the call. In
9967 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9968 COMPOUND_EXPR will be an argument which must be evaluated.
9969 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9970 COMPOUND_EXPR in the chain will contain the tree for the simplified
9971 form of the builtin function call. */
9973 static tree
9974 fold_builtin_strcspn (tree arglist)
9976 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9977 return 0;
9978 else
9980 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9981 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9983 /* If both arguments are constants, evaluate at compile-time. */
9984 if (p1 && p2)
9986 const size_t r = strcspn (p1, p2);
9987 return size_int (r);
9990 /* If the first argument is "", return 0. */
9991 if (p1 && *p1 == '\0')
9993 /* Evaluate and ignore argument s2 in case it has
9994 side-effects. */
9995 return omit_one_operand (integer_type_node,
9996 integer_zero_node, s2);
9999 /* If the second argument is "", return __builtin_strlen(s1). */
10000 if (p2 && *p2 == '\0')
10002 tree newarglist = build_tree_list (NULL_TREE, s1),
10003 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10005 /* If the replacement _DECL isn't initialized, don't do the
10006 transformation. */
10007 if (!fn)
10008 return 0;
10010 return build_function_call_expr (fn, newarglist);
10012 return 0;
10016 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10017 by the builtin will be ignored. UNLOCKED is true is true if this
10018 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10019 the known length of the string. Return NULL_TREE if no simplification
10020 was possible. */
10022 tree
10023 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10025 tree fn;
10026 /* If we're using an unlocked function, assume the other unlocked
10027 functions exist explicitly. */
10028 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10029 : implicit_built_in_decls[BUILT_IN_FPUTC];
10030 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10031 : implicit_built_in_decls[BUILT_IN_FWRITE];
10033 /* If the return value is used, don't do the transformation. */
10034 if (!ignore)
10035 return 0;
10037 /* Verify the arguments in the original call. */
10038 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10039 return 0;
10041 if (! len)
10042 len = c_strlen (TREE_VALUE (arglist), 0);
10044 /* Get the length of the string passed to fputs. If the length
10045 can't be determined, punt. */
10046 if (!len
10047 || TREE_CODE (len) != INTEGER_CST)
10048 return 0;
10050 switch (compare_tree_int (len, 1))
10052 case -1: /* length is 0, delete the call entirely . */
10053 return omit_one_operand (integer_type_node, integer_zero_node,
10054 TREE_VALUE (TREE_CHAIN (arglist)));
10056 case 0: /* length is 1, call fputc. */
10058 const char *p = c_getstr (TREE_VALUE (arglist));
10060 if (p != NULL)
10062 /* New argument list transforming fputs(string, stream) to
10063 fputc(string[0], stream). */
10064 arglist = build_tree_list (NULL_TREE,
10065 TREE_VALUE (TREE_CHAIN (arglist)));
10066 arglist = tree_cons (NULL_TREE,
10067 build_int_cst (NULL_TREE, p[0]),
10068 arglist);
10069 fn = fn_fputc;
10070 break;
10073 /* FALLTHROUGH */
10074 case 1: /* length is greater than 1, call fwrite. */
10076 tree string_arg;
10078 /* If optimizing for size keep fputs. */
10079 if (optimize_size)
10080 return 0;
10081 string_arg = TREE_VALUE (arglist);
10082 /* New argument list transforming fputs(string, stream) to
10083 fwrite(string, 1, len, stream). */
10084 arglist = build_tree_list (NULL_TREE,
10085 TREE_VALUE (TREE_CHAIN (arglist)));
10086 arglist = tree_cons (NULL_TREE, len, arglist);
10087 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10088 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10089 fn = fn_fwrite;
10090 break;
10092 default:
10093 gcc_unreachable ();
10096 /* If the replacement _DECL isn't initialized, don't do the
10097 transformation. */
10098 if (!fn)
10099 return 0;
10101 /* These optimizations are only performed when the result is ignored,
10102 hence there's no need to cast the result to integer_type_node. */
10103 return build_function_call_expr (fn, arglist);
10106 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10107 produced. False otherwise. This is done so that we don't output the error
10108 or warning twice or three times. */
10109 bool
10110 fold_builtin_next_arg (tree arglist)
10112 tree fntype = TREE_TYPE (current_function_decl);
10114 if (TYPE_ARG_TYPES (fntype) == 0
10115 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10116 == void_type_node))
10118 error ("%<va_start%> used in function with fixed args");
10119 return true;
10121 else if (!arglist)
10123 /* Evidently an out of date version of <stdarg.h>; can't validate
10124 va_start's second argument, but can still work as intended. */
10125 warning (0, "%<__builtin_next_arg%> called without an argument");
10126 return true;
10128 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10129 when we checked the arguments and if needed issued a warning. */
10130 else if (!TREE_CHAIN (arglist)
10131 || !integer_zerop (TREE_VALUE (arglist))
10132 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10133 || TREE_CHAIN (TREE_CHAIN (arglist)))
10135 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10136 tree arg = TREE_VALUE (arglist);
10138 if (TREE_CHAIN (arglist))
10140 error ("%<va_start%> used with too many arguments");
10141 return true;
10144 /* Strip off all nops for the sake of the comparison. This
10145 is not quite the same as STRIP_NOPS. It does more.
10146 We must also strip off INDIRECT_EXPR for C++ reference
10147 parameters. */
10148 while (TREE_CODE (arg) == NOP_EXPR
10149 || TREE_CODE (arg) == CONVERT_EXPR
10150 || TREE_CODE (arg) == NON_LVALUE_EXPR
10151 || TREE_CODE (arg) == INDIRECT_REF)
10152 arg = TREE_OPERAND (arg, 0);
10153 if (arg != last_parm)
10155 /* FIXME: Sometimes with the tree optimizers we can get the
10156 not the last argument even though the user used the last
10157 argument. We just warn and set the arg to be the last
10158 argument so that we will get wrong-code because of
10159 it. */
10160 warning (0, "second parameter of %<va_start%> not last named argument");
10162 /* We want to verify the second parameter just once before the tree
10163 optimizers are run and then avoid keeping it in the tree,
10164 as otherwise we could warn even for correct code like:
10165 void foo (int i, ...)
10166 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10167 TREE_VALUE (arglist) = integer_zero_node;
10168 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10170 return false;
10174 /* Simplify a call to the sprintf builtin.
10176 Return 0 if no simplification was possible, otherwise return the
10177 simplified form of the call as a tree. If IGNORED is true, it means that
10178 the caller does not use the returned value of the function. */
10180 static tree
10181 fold_builtin_sprintf (tree arglist, int ignored)
10183 tree call, retval, dest, fmt;
10184 const char *fmt_str = NULL;
10186 /* Verify the required arguments in the original call. We deal with two
10187 types of sprintf() calls: 'sprintf (str, fmt)' and
10188 'sprintf (dest, "%s", orig)'. */
10189 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10190 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10191 VOID_TYPE))
10192 return NULL_TREE;
10194 /* Get the destination string and the format specifier. */
10195 dest = TREE_VALUE (arglist);
10196 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10198 /* Check whether the format is a literal string constant. */
10199 fmt_str = c_getstr (fmt);
10200 if (fmt_str == NULL)
10201 return NULL_TREE;
10203 call = NULL_TREE;
10204 retval = NULL_TREE;
10206 if (!init_target_chars())
10207 return 0;
10209 /* If the format doesn't contain % args or %%, use strcpy. */
10210 if (strchr (fmt_str, target_percent) == NULL)
10212 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10214 if (!fn)
10215 return NULL_TREE;
10217 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10218 'format' is known to contain no % formats. */
10219 arglist = build_tree_list (NULL_TREE, fmt);
10220 arglist = tree_cons (NULL_TREE, dest, arglist);
10221 call = build_function_call_expr (fn, arglist);
10222 if (!ignored)
10223 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10226 /* If the format is "%s", use strcpy if the result isn't used. */
10227 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10229 tree fn, orig;
10230 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10232 if (!fn)
10233 return NULL_TREE;
10235 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10236 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10237 arglist = build_tree_list (NULL_TREE, orig);
10238 arglist = tree_cons (NULL_TREE, dest, arglist);
10239 if (!ignored)
10241 retval = c_strlen (orig, 1);
10242 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10243 return NULL_TREE;
10245 call = build_function_call_expr (fn, arglist);
10248 if (call && retval)
10250 retval = fold_convert
10251 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10252 retval);
10253 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10255 else
10256 return call;
10259 /* Expand a call to __builtin_object_size. */
10262 expand_builtin_object_size (tree exp)
10264 tree ost;
10265 int object_size_type;
10266 tree fndecl = get_callee_fndecl (exp);
10267 tree arglist = TREE_OPERAND (exp, 1);
10268 location_t locus = EXPR_LOCATION (exp);
10270 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10272 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10273 &locus, fndecl);
10274 expand_builtin_trap ();
10275 return const0_rtx;
10278 ost = TREE_VALUE (TREE_CHAIN (arglist));
10279 STRIP_NOPS (ost);
10281 if (TREE_CODE (ost) != INTEGER_CST
10282 || tree_int_cst_sgn (ost) < 0
10283 || compare_tree_int (ost, 3) > 0)
10285 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10286 &locus, fndecl);
10287 expand_builtin_trap ();
10288 return const0_rtx;
10291 object_size_type = tree_low_cst (ost, 0);
10293 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10296 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10297 FCODE is the BUILT_IN_* to use.
10298 Return 0 if we failed; the caller should emit a normal call,
10299 otherwise try to get the result in TARGET, if convenient (and in
10300 mode MODE if that's convenient). */
10302 static rtx
10303 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10304 enum built_in_function fcode)
10306 tree arglist = TREE_OPERAND (exp, 1);
10307 tree dest, src, len, size;
10309 if (!validate_arglist (arglist,
10310 POINTER_TYPE,
10311 fcode == BUILT_IN_MEMSET_CHK
10312 ? INTEGER_TYPE : POINTER_TYPE,
10313 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10314 return 0;
10316 dest = TREE_VALUE (arglist);
10317 src = TREE_VALUE (TREE_CHAIN (arglist));
10318 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10319 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10321 if (! host_integerp (size, 1))
10322 return 0;
10324 if (host_integerp (len, 1) || integer_all_onesp (size))
10326 tree fn;
10328 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10330 location_t locus = EXPR_LOCATION (exp);
10331 warning (0, "%Hcall to %D will always overflow destination buffer",
10332 &locus, get_callee_fndecl (exp));
10333 return 0;
10336 arglist = build_tree_list (NULL_TREE, len);
10337 arglist = tree_cons (NULL_TREE, src, arglist);
10338 arglist = tree_cons (NULL_TREE, dest, arglist);
10340 fn = NULL_TREE;
10341 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10342 mem{cpy,pcpy,move,set} is available. */
10343 switch (fcode)
10345 case BUILT_IN_MEMCPY_CHK:
10346 fn = built_in_decls[BUILT_IN_MEMCPY];
10347 break;
10348 case BUILT_IN_MEMPCPY_CHK:
10349 fn = built_in_decls[BUILT_IN_MEMPCPY];
10350 break;
10351 case BUILT_IN_MEMMOVE_CHK:
10352 fn = built_in_decls[BUILT_IN_MEMMOVE];
10353 break;
10354 case BUILT_IN_MEMSET_CHK:
10355 fn = built_in_decls[BUILT_IN_MEMSET];
10356 break;
10357 default:
10358 break;
10361 if (! fn)
10362 return 0;
10364 fn = build_function_call_expr (fn, arglist);
10365 if (TREE_CODE (fn) == CALL_EXPR)
10366 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10367 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10369 else if (fcode == BUILT_IN_MEMSET_CHK)
10370 return 0;
10371 else
10373 unsigned int dest_align
10374 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10376 /* If DEST is not a pointer type, call the normal function. */
10377 if (dest_align == 0)
10378 return 0;
10380 /* If SRC and DEST are the same (and not volatile), do nothing. */
10381 if (operand_equal_p (src, dest, 0))
10383 tree expr;
10385 if (fcode != BUILT_IN_MEMPCPY_CHK)
10387 /* Evaluate and ignore LEN in case it has side-effects. */
10388 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10389 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10392 len = fold_convert (TREE_TYPE (dest), len);
10393 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10394 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10397 /* __memmove_chk special case. */
10398 if (fcode == BUILT_IN_MEMMOVE_CHK)
10400 unsigned int src_align
10401 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10403 if (src_align == 0)
10404 return 0;
10406 /* If src is categorized for a readonly section we can use
10407 normal __memcpy_chk. */
10408 if (readonly_data_expr (src))
10410 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10411 if (!fn)
10412 return 0;
10413 fn = build_function_call_expr (fn, arglist);
10414 if (TREE_CODE (fn) == CALL_EXPR)
10415 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10416 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10419 return 0;
10423 /* Emit warning if a buffer overflow is detected at compile time. */
10425 static void
10426 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10428 int arg_mask, is_strlen = 0;
10429 tree arglist = TREE_OPERAND (exp, 1), a;
10430 tree len, size;
10431 location_t locus;
10433 switch (fcode)
10435 case BUILT_IN_STRCPY_CHK:
10436 case BUILT_IN_STPCPY_CHK:
10437 /* For __strcat_chk the warning will be emitted only if overflowing
10438 by at least strlen (dest) + 1 bytes. */
10439 case BUILT_IN_STRCAT_CHK:
10440 arg_mask = 6;
10441 is_strlen = 1;
10442 break;
10443 case BUILT_IN_STRNCAT_CHK:
10444 /* For __strncat_chk the warning will be emitted only if overflowing
10445 by at least strlen (dest) + 1 bytes. */
10446 arg_mask = 12;
10447 break;
10448 case BUILT_IN_STRNCPY_CHK:
10449 arg_mask = 12;
10450 break;
10451 case BUILT_IN_SNPRINTF_CHK:
10452 case BUILT_IN_VSNPRINTF_CHK:
10453 arg_mask = 10;
10454 break;
10455 default:
10456 gcc_unreachable ();
10459 len = NULL_TREE;
10460 size = NULL_TREE;
10461 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10462 if (arg_mask & 1)
10464 if (len)
10465 size = a;
10466 else
10467 len = a;
10470 if (!len || !size)
10471 return;
10473 len = TREE_VALUE (len);
10474 size = TREE_VALUE (size);
10476 if (! host_integerp (size, 1) || integer_all_onesp (size))
10477 return;
10479 if (is_strlen)
10481 len = c_strlen (len, 1);
10482 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10483 return;
10485 else if (fcode == BUILT_IN_STRNCAT_CHK)
10487 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10488 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10489 return;
10490 src = c_strlen (src, 1);
10491 if (! src || ! host_integerp (src, 1))
10493 locus = EXPR_LOCATION (exp);
10494 warning (0, "%Hcall to %D might overflow destination buffer",
10495 &locus, get_callee_fndecl (exp));
10496 return;
10498 else if (tree_int_cst_lt (src, size))
10499 return;
10501 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10502 return;
10504 locus = EXPR_LOCATION (exp);
10505 warning (0, "%Hcall to %D will always overflow destination buffer",
10506 &locus, get_callee_fndecl (exp));
10509 /* Emit warning if a buffer overflow is detected at compile time
10510 in __sprintf_chk/__vsprintf_chk calls. */
10512 static void
10513 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10515 tree arglist = TREE_OPERAND (exp, 1);
10516 tree dest, size, len, fmt, flag;
10517 const char *fmt_str;
10519 /* Verify the required arguments in the original call. */
10520 if (! arglist)
10521 return;
10522 dest = TREE_VALUE (arglist);
10523 arglist = TREE_CHAIN (arglist);
10524 if (! arglist)
10525 return;
10526 flag = TREE_VALUE (arglist);
10527 arglist = TREE_CHAIN (arglist);
10528 if (! arglist)
10529 return;
10530 size = TREE_VALUE (arglist);
10531 arglist = TREE_CHAIN (arglist);
10532 if (! arglist)
10533 return;
10534 fmt = TREE_VALUE (arglist);
10535 arglist = TREE_CHAIN (arglist);
10537 if (! host_integerp (size, 1) || integer_all_onesp (size))
10538 return;
10540 /* Check whether the format is a literal string constant. */
10541 fmt_str = c_getstr (fmt);
10542 if (fmt_str == NULL)
10543 return;
10545 if (!init_target_chars())
10546 return;
10548 /* If the format doesn't contain % args or %%, we know its size. */
10549 if (strchr (fmt_str, target_percent) == 0)
10550 len = build_int_cstu (size_type_node, strlen (fmt_str));
10551 /* If the format is "%s" and first ... argument is a string literal,
10552 we know it too. */
10553 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10555 tree arg;
10557 if (! arglist)
10558 return;
10559 arg = TREE_VALUE (arglist);
10560 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10561 return;
10563 len = c_strlen (arg, 1);
10564 if (!len || ! host_integerp (len, 1))
10565 return;
10567 else
10568 return;
10570 if (! tree_int_cst_lt (len, size))
10572 location_t locus = EXPR_LOCATION (exp);
10573 warning (0, "%Hcall to %D will always overflow destination buffer",
10574 &locus, get_callee_fndecl (exp));
10578 /* Fold a call to __builtin_object_size, if possible. */
10580 tree
10581 fold_builtin_object_size (tree arglist)
10583 tree ptr, ost, ret = 0;
10584 int object_size_type;
10586 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10587 return 0;
10589 ptr = TREE_VALUE (arglist);
10590 ost = TREE_VALUE (TREE_CHAIN (arglist));
10591 STRIP_NOPS (ost);
10593 if (TREE_CODE (ost) != INTEGER_CST
10594 || tree_int_cst_sgn (ost) < 0
10595 || compare_tree_int (ost, 3) > 0)
10596 return 0;
10598 object_size_type = tree_low_cst (ost, 0);
10600 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10601 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10602 and (size_t) 0 for types 2 and 3. */
10603 if (TREE_SIDE_EFFECTS (ptr))
10604 return fold_convert (size_type_node,
10605 object_size_type < 2
10606 ? integer_minus_one_node : integer_zero_node);
10608 if (TREE_CODE (ptr) == ADDR_EXPR)
10609 ret = build_int_cstu (size_type_node,
10610 compute_builtin_object_size (ptr, object_size_type));
10612 else if (TREE_CODE (ptr) == SSA_NAME)
10614 unsigned HOST_WIDE_INT bytes;
10616 /* If object size is not known yet, delay folding until
10617 later. Maybe subsequent passes will help determining
10618 it. */
10619 bytes = compute_builtin_object_size (ptr, object_size_type);
10620 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10621 ? -1 : 0))
10622 ret = build_int_cstu (size_type_node, bytes);
10625 if (ret)
10627 ret = force_fit_type (ret, -1, false, false);
10628 if (TREE_CONSTANT_OVERFLOW (ret))
10629 ret = 0;
10632 return ret;
10635 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10636 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10637 code of the builtin. If MAXLEN is not NULL, it is maximum length
10638 passed as third argument. */
10640 tree
10641 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10642 enum built_in_function fcode)
10644 tree dest, src, len, size, fn;
10646 if (!validate_arglist (arglist,
10647 POINTER_TYPE,
10648 fcode == BUILT_IN_MEMSET_CHK
10649 ? INTEGER_TYPE : POINTER_TYPE,
10650 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10651 return 0;
10653 dest = TREE_VALUE (arglist);
10654 /* Actually val for __memset_chk, but it doesn't matter. */
10655 src = TREE_VALUE (TREE_CHAIN (arglist));
10656 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10657 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10659 /* If SRC and DEST are the same (and not volatile), return DEST
10660 (resp. DEST+LEN for __mempcpy_chk). */
10661 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10663 if (fcode != BUILT_IN_MEMPCPY_CHK)
10664 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10665 else
10667 tree temp = fold_convert (TREE_TYPE (dest), len);
10668 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10669 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10673 if (! host_integerp (size, 1))
10674 return 0;
10676 if (! integer_all_onesp (size))
10678 if (! host_integerp (len, 1))
10680 /* If LEN is not constant, try MAXLEN too.
10681 For MAXLEN only allow optimizing into non-_ocs function
10682 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10683 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10685 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10687 /* (void) __mempcpy_chk () can be optimized into
10688 (void) __memcpy_chk (). */
10689 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10690 if (!fn)
10691 return 0;
10693 return build_function_call_expr (fn, arglist);
10695 return 0;
10698 else
10699 maxlen = len;
10701 if (tree_int_cst_lt (size, maxlen))
10702 return 0;
10705 arglist = build_tree_list (NULL_TREE, len);
10706 arglist = tree_cons (NULL_TREE, src, arglist);
10707 arglist = tree_cons (NULL_TREE, dest, arglist);
10709 fn = NULL_TREE;
10710 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10711 mem{cpy,pcpy,move,set} is available. */
10712 switch (fcode)
10714 case BUILT_IN_MEMCPY_CHK:
10715 fn = built_in_decls[BUILT_IN_MEMCPY];
10716 break;
10717 case BUILT_IN_MEMPCPY_CHK:
10718 fn = built_in_decls[BUILT_IN_MEMPCPY];
10719 break;
10720 case BUILT_IN_MEMMOVE_CHK:
10721 fn = built_in_decls[BUILT_IN_MEMMOVE];
10722 break;
10723 case BUILT_IN_MEMSET_CHK:
10724 fn = built_in_decls[BUILT_IN_MEMSET];
10725 break;
10726 default:
10727 break;
10730 if (!fn)
10731 return 0;
10733 return build_function_call_expr (fn, arglist);
10736 /* Fold a call to the __st[rp]cpy_chk builtin.
10737 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10738 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10739 strings passed as second argument. */
10741 tree
10742 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10743 enum built_in_function fcode)
10745 tree dest, src, size, len, fn;
10747 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10748 VOID_TYPE))
10749 return 0;
10751 dest = TREE_VALUE (arglist);
10752 src = TREE_VALUE (TREE_CHAIN (arglist));
10753 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10755 /* If SRC and DEST are the same (and not volatile), return DEST. */
10756 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10757 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10759 if (! host_integerp (size, 1))
10760 return 0;
10762 if (! integer_all_onesp (size))
10764 len = c_strlen (src, 1);
10765 if (! len || ! host_integerp (len, 1))
10767 /* If LEN is not constant, try MAXLEN too.
10768 For MAXLEN only allow optimizing into non-_ocs function
10769 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10770 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10772 if (fcode == BUILT_IN_STPCPY_CHK)
10774 if (! ignore)
10775 return 0;
10777 /* If return value of __stpcpy_chk is ignored,
10778 optimize into __strcpy_chk. */
10779 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10780 if (!fn)
10781 return 0;
10783 return build_function_call_expr (fn, arglist);
10786 if (! len || TREE_SIDE_EFFECTS (len))
10787 return 0;
10789 /* If c_strlen returned something, but not a constant,
10790 transform __strcpy_chk into __memcpy_chk. */
10791 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10792 if (!fn)
10793 return 0;
10795 len = size_binop (PLUS_EXPR, len, ssize_int (1));
10796 arglist = build_tree_list (NULL_TREE, size);
10797 arglist = tree_cons (NULL_TREE, len, arglist);
10798 arglist = tree_cons (NULL_TREE, src, arglist);
10799 arglist = tree_cons (NULL_TREE, dest, arglist);
10800 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10801 build_function_call_expr (fn, arglist));
10804 else
10805 maxlen = len;
10807 if (! tree_int_cst_lt (maxlen, size))
10808 return 0;
10811 arglist = build_tree_list (NULL_TREE, src);
10812 arglist = tree_cons (NULL_TREE, dest, arglist);
10814 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10815 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10816 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10817 if (!fn)
10818 return 0;
10820 return build_function_call_expr (fn, arglist);
10823 /* Fold a call to the __strncpy_chk builtin.
10824 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10826 tree
10827 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10829 tree dest, src, size, len, fn;
10831 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10832 INTEGER_TYPE, VOID_TYPE))
10833 return 0;
10835 dest = TREE_VALUE (arglist);
10836 src = TREE_VALUE (TREE_CHAIN (arglist));
10837 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10838 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10840 if (! host_integerp (size, 1))
10841 return 0;
10843 if (! integer_all_onesp (size))
10845 if (! host_integerp (len, 1))
10847 /* If LEN is not constant, try MAXLEN too.
10848 For MAXLEN only allow optimizing into non-_ocs function
10849 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10850 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10851 return 0;
10853 else
10854 maxlen = len;
10856 if (tree_int_cst_lt (size, maxlen))
10857 return 0;
10860 arglist = build_tree_list (NULL_TREE, len);
10861 arglist = tree_cons (NULL_TREE, src, arglist);
10862 arglist = tree_cons (NULL_TREE, dest, arglist);
10864 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10865 fn = built_in_decls[BUILT_IN_STRNCPY];
10866 if (!fn)
10867 return 0;
10869 return build_function_call_expr (fn, arglist);
10872 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10874 static tree
10875 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10877 tree dest, src, size, fn;
10878 const char *p;
10880 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10881 VOID_TYPE))
10882 return 0;
10884 dest = TREE_VALUE (arglist);
10885 src = TREE_VALUE (TREE_CHAIN (arglist));
10886 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10888 p = c_getstr (src);
10889 /* If the SRC parameter is "", return DEST. */
10890 if (p && *p == '\0')
10891 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10893 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10894 return 0;
10896 arglist = build_tree_list (NULL_TREE, src);
10897 arglist = tree_cons (NULL_TREE, dest, arglist);
10899 /* If __builtin_strcat_chk is used, assume strcat is available. */
10900 fn = built_in_decls[BUILT_IN_STRCAT];
10901 if (!fn)
10902 return 0;
10904 return build_function_call_expr (fn, arglist);
10907 /* Fold a call to the __strncat_chk builtin EXP. */
10909 static tree
10910 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10912 tree dest, src, size, len, fn;
10913 const char *p;
10915 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10916 INTEGER_TYPE, VOID_TYPE))
10917 return 0;
10919 dest = TREE_VALUE (arglist);
10920 src = TREE_VALUE (TREE_CHAIN (arglist));
10921 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10922 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10924 p = c_getstr (src);
10925 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10926 if (p && *p == '\0')
10927 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10928 else if (integer_zerop (len))
10929 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10931 if (! host_integerp (size, 1))
10932 return 0;
10934 if (! integer_all_onesp (size))
10936 tree src_len = c_strlen (src, 1);
10937 if (src_len
10938 && host_integerp (src_len, 1)
10939 && host_integerp (len, 1)
10940 && ! tree_int_cst_lt (len, src_len))
10942 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10943 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10944 if (!fn)
10945 return 0;
10947 arglist = build_tree_list (NULL_TREE, size);
10948 arglist = tree_cons (NULL_TREE, src, arglist);
10949 arglist = tree_cons (NULL_TREE, dest, arglist);
10950 return build_function_call_expr (fn, arglist);
10952 return 0;
10955 arglist = build_tree_list (NULL_TREE, len);
10956 arglist = tree_cons (NULL_TREE, src, arglist);
10957 arglist = tree_cons (NULL_TREE, dest, arglist);
10959 /* If __builtin_strncat_chk is used, assume strncat is available. */
10960 fn = built_in_decls[BUILT_IN_STRNCAT];
10961 if (!fn)
10962 return 0;
10964 return build_function_call_expr (fn, arglist);
10967 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10968 a normal call should be emitted rather than expanding the function
10969 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10971 static tree
10972 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
10974 tree dest, size, len, fn, fmt, flag;
10975 const char *fmt_str;
10977 /* Verify the required arguments in the original call. */
10978 if (! arglist)
10979 return 0;
10980 dest = TREE_VALUE (arglist);
10981 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10982 return 0;
10983 arglist = TREE_CHAIN (arglist);
10984 if (! arglist)
10985 return 0;
10986 flag = TREE_VALUE (arglist);
10987 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
10988 return 0;
10989 arglist = TREE_CHAIN (arglist);
10990 if (! arglist)
10991 return 0;
10992 size = TREE_VALUE (arglist);
10993 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10994 return 0;
10995 arglist = TREE_CHAIN (arglist);
10996 if (! arglist)
10997 return 0;
10998 fmt = TREE_VALUE (arglist);
10999 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11000 return 0;
11001 arglist = TREE_CHAIN (arglist);
11003 if (! host_integerp (size, 1))
11004 return 0;
11006 len = NULL_TREE;
11008 if (!init_target_chars())
11009 return 0;
11011 /* Check whether the format is a literal string constant. */
11012 fmt_str = c_getstr (fmt);
11013 if (fmt_str != NULL)
11015 /* If the format doesn't contain % args or %%, we know the size. */
11016 if (strchr (fmt_str, target_percent) == 0)
11018 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11019 len = build_int_cstu (size_type_node, strlen (fmt_str));
11021 /* If the format is "%s" and first ... argument is a string literal,
11022 we know the size too. */
11023 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11025 tree arg;
11027 if (arglist && !TREE_CHAIN (arglist))
11029 arg = TREE_VALUE (arglist);
11030 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11032 len = c_strlen (arg, 1);
11033 if (! len || ! host_integerp (len, 1))
11034 len = NULL_TREE;
11040 if (! integer_all_onesp (size))
11042 if (! len || ! tree_int_cst_lt (len, size))
11043 return 0;
11046 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11047 or if format doesn't contain % chars or is "%s". */
11048 if (! integer_zerop (flag))
11050 if (fmt_str == NULL)
11051 return 0;
11052 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11053 return 0;
11056 arglist = tree_cons (NULL_TREE, fmt, arglist);
11057 arglist = tree_cons (NULL_TREE, dest, arglist);
11059 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11060 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11061 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11062 if (!fn)
11063 return 0;
11065 return build_function_call_expr (fn, arglist);
11068 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11069 a normal call should be emitted rather than expanding the function
11070 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11071 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11072 passed as second argument. */
11074 tree
11075 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11076 enum built_in_function fcode)
11078 tree dest, size, len, fn, fmt, flag;
11079 const char *fmt_str;
11081 /* Verify the required arguments in the original call. */
11082 if (! arglist)
11083 return 0;
11084 dest = TREE_VALUE (arglist);
11085 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11086 return 0;
11087 arglist = TREE_CHAIN (arglist);
11088 if (! arglist)
11089 return 0;
11090 len = TREE_VALUE (arglist);
11091 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11092 return 0;
11093 arglist = TREE_CHAIN (arglist);
11094 if (! arglist)
11095 return 0;
11096 flag = TREE_VALUE (arglist);
11097 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11098 return 0;
11099 arglist = TREE_CHAIN (arglist);
11100 if (! arglist)
11101 return 0;
11102 size = TREE_VALUE (arglist);
11103 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11104 return 0;
11105 arglist = TREE_CHAIN (arglist);
11106 if (! arglist)
11107 return 0;
11108 fmt = TREE_VALUE (arglist);
11109 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11110 return 0;
11111 arglist = TREE_CHAIN (arglist);
11113 if (! host_integerp (size, 1))
11114 return 0;
11116 if (! integer_all_onesp (size))
11118 if (! host_integerp (len, 1))
11120 /* If LEN is not constant, try MAXLEN too.
11121 For MAXLEN only allow optimizing into non-_ocs function
11122 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11123 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11124 return 0;
11126 else
11127 maxlen = len;
11129 if (tree_int_cst_lt (size, maxlen))
11130 return 0;
11133 if (!init_target_chars())
11134 return 0;
11136 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11137 or if format doesn't contain % chars or is "%s". */
11138 if (! integer_zerop (flag))
11140 fmt_str = c_getstr (fmt);
11141 if (fmt_str == NULL)
11142 return 0;
11143 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11144 return 0;
11147 arglist = tree_cons (NULL_TREE, fmt, arglist);
11148 arglist = tree_cons (NULL_TREE, len, arglist);
11149 arglist = tree_cons (NULL_TREE, dest, arglist);
11151 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11152 available. */
11153 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11154 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11155 if (!fn)
11156 return 0;
11158 return build_function_call_expr (fn, arglist);
11161 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11163 Return 0 if no simplification was possible, otherwise return the
11164 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11165 code of the function to be simplified. */
11167 static tree
11168 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11169 enum built_in_function fcode)
11171 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11172 const char *fmt_str = NULL;
11174 /* If the return value is used, don't do the transformation. */
11175 if (! ignore)
11176 return 0;
11178 /* Verify the required arguments in the original call. */
11179 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11181 tree flag;
11183 if (! arglist)
11184 return 0;
11185 flag = TREE_VALUE (arglist);
11186 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11187 || TREE_SIDE_EFFECTS (flag))
11188 return 0;
11189 arglist = TREE_CHAIN (arglist);
11192 if (! arglist)
11193 return 0;
11194 fmt = TREE_VALUE (arglist);
11195 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11196 return 0;
11197 arglist = TREE_CHAIN (arglist);
11199 /* Check whether the format is a literal string constant. */
11200 fmt_str = c_getstr (fmt);
11201 if (fmt_str == NULL)
11202 return NULL_TREE;
11204 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11206 /* If we're using an unlocked function, assume the other
11207 unlocked functions exist explicitly. */
11208 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11209 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11211 else
11213 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11214 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11217 if (!init_target_chars())
11218 return 0;
11220 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11222 const char *str;
11224 if (strcmp (fmt_str, target_percent_s) == 0)
11226 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11227 return 0;
11229 if (! arglist
11230 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11231 || TREE_CHAIN (arglist))
11232 return 0;
11234 str = c_getstr (TREE_VALUE (arglist));
11235 if (str == NULL)
11236 return 0;
11238 else
11240 /* The format specifier doesn't contain any '%' characters. */
11241 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11242 && arglist)
11243 return 0;
11244 str = fmt_str;
11247 /* If the string was "", printf does nothing. */
11248 if (str[0] == '\0')
11249 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11251 /* If the string has length of 1, call putchar. */
11252 if (str[1] == '\0')
11254 /* Given printf("c"), (where c is any one character,)
11255 convert "c"[0] to an int and pass that to the replacement
11256 function. */
11257 arg = build_int_cst (NULL_TREE, str[0]);
11258 arglist = build_tree_list (NULL_TREE, arg);
11259 fn = fn_putchar;
11261 else
11263 /* If the string was "string\n", call puts("string"). */
11264 size_t len = strlen (str);
11265 if ((unsigned char)str[len - 1] == target_newline)
11267 /* Create a NUL-terminated string that's one char shorter
11268 than the original, stripping off the trailing '\n'. */
11269 char *newstr = alloca (len);
11270 memcpy (newstr, str, len - 1);
11271 newstr[len - 1] = 0;
11273 arg = build_string_literal (len, newstr);
11274 arglist = build_tree_list (NULL_TREE, arg);
11275 fn = fn_puts;
11277 else
11278 /* We'd like to arrange to call fputs(string,stdout) here,
11279 but we need stdout and don't have a way to get it yet. */
11280 return 0;
11284 /* The other optimizations can be done only on the non-va_list variants. */
11285 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11286 return 0;
11288 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11289 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11291 if (! arglist
11292 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11293 || TREE_CHAIN (arglist))
11294 return 0;
11295 fn = fn_puts;
11298 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11299 else if (strcmp (fmt_str, target_percent_c) == 0)
11301 if (! arglist
11302 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11303 || TREE_CHAIN (arglist))
11304 return 0;
11305 fn = fn_putchar;
11308 if (!fn)
11309 return 0;
11311 call = build_function_call_expr (fn, arglist);
11312 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11315 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11317 Return 0 if no simplification was possible, otherwise return the
11318 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11319 code of the function to be simplified. */
11321 static tree
11322 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11323 enum built_in_function fcode)
11325 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11326 const char *fmt_str = NULL;
11328 /* If the return value is used, don't do the transformation. */
11329 if (! ignore)
11330 return 0;
11332 /* Verify the required arguments in the original call. */
11333 if (! arglist)
11334 return 0;
11335 fp = TREE_VALUE (arglist);
11336 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11337 return 0;
11338 arglist = TREE_CHAIN (arglist);
11340 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11342 tree flag;
11344 if (! arglist)
11345 return 0;
11346 flag = TREE_VALUE (arglist);
11347 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11348 || TREE_SIDE_EFFECTS (flag))
11349 return 0;
11350 arglist = TREE_CHAIN (arglist);
11353 if (! arglist)
11354 return 0;
11355 fmt = TREE_VALUE (arglist);
11356 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11357 return 0;
11358 arglist = TREE_CHAIN (arglist);
11360 /* Check whether the format is a literal string constant. */
11361 fmt_str = c_getstr (fmt);
11362 if (fmt_str == NULL)
11363 return NULL_TREE;
11365 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11367 /* If we're using an unlocked function, assume the other
11368 unlocked functions exist explicitly. */
11369 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11370 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11372 else
11374 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11375 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11378 if (!init_target_chars())
11379 return 0;
11381 /* If the format doesn't contain % args or %%, use strcpy. */
11382 if (strchr (fmt_str, target_percent) == NULL)
11384 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11385 && arglist)
11386 return 0;
11388 /* If the format specifier was "", fprintf does nothing. */
11389 if (fmt_str[0] == '\0')
11391 /* If FP has side-effects, just wait until gimplification is
11392 done. */
11393 if (TREE_SIDE_EFFECTS (fp))
11394 return 0;
11396 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11399 /* When "string" doesn't contain %, replace all cases of
11400 fprintf (fp, string) with fputs (string, fp). The fputs
11401 builtin will take care of special cases like length == 1. */
11402 arglist = build_tree_list (NULL_TREE, fp);
11403 arglist = tree_cons (NULL_TREE, fmt, arglist);
11404 fn = fn_fputs;
11407 /* The other optimizations can be done only on the non-va_list variants. */
11408 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11409 return 0;
11411 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11412 else if (strcmp (fmt_str, target_percent_s) == 0)
11414 if (! arglist
11415 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11416 || TREE_CHAIN (arglist))
11417 return 0;
11418 arg = TREE_VALUE (arglist);
11419 arglist = build_tree_list (NULL_TREE, fp);
11420 arglist = tree_cons (NULL_TREE, arg, arglist);
11421 fn = fn_fputs;
11424 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11425 else if (strcmp (fmt_str, target_percent_c) == 0)
11427 if (! arglist
11428 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11429 || TREE_CHAIN (arglist))
11430 return 0;
11431 arg = TREE_VALUE (arglist);
11432 arglist = build_tree_list (NULL_TREE, fp);
11433 arglist = tree_cons (NULL_TREE, arg, arglist);
11434 fn = fn_fputc;
11437 if (!fn)
11438 return 0;
11440 call = build_function_call_expr (fn, arglist);
11441 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11444 /* Initialize format string characters in the target charset. */
11446 static bool
11447 init_target_chars (void)
11449 static bool init;
11450 if (!init)
11452 target_newline = lang_hooks.to_target_charset ('\n');
11453 target_percent = lang_hooks.to_target_charset ('%');
11454 target_c = lang_hooks.to_target_charset ('c');
11455 target_s = lang_hooks.to_target_charset ('s');
11456 if (target_newline == 0 || target_percent == 0 || target_c == 0
11457 || target_s == 0)
11458 return false;
11460 target_percent_c[0] = target_percent;
11461 target_percent_c[1] = target_c;
11462 target_percent_c[2] = '\0';
11464 target_percent_s[0] = target_percent;
11465 target_percent_s[1] = target_s;
11466 target_percent_s[2] = '\0';
11468 target_percent_s_newline[0] = target_percent;
11469 target_percent_s_newline[1] = target_s;
11470 target_percent_s_newline[2] = target_newline;
11471 target_percent_s_newline[3] = '\0';
11473 init = true;
11475 return true;
11478 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11479 and no overflow/underflow occurred. INEXACT is true if M was not
11480 exacly calculated. TYPE is the tree type for the result. This
11481 function assumes that you cleared the MPFR flags and then
11482 calculated M to see if anything subsequently set a flag prior to
11483 entering this function. Return NULL_TREE if any checks fail. */
11485 static tree
11486 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11488 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11489 overflow/underflow occurred. If -frounding-math, proceed iff the
11490 result of calling FUNC was exact. */
11491 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11492 && (!flag_rounding_math || !inexact))
11494 REAL_VALUE_TYPE rr;
11496 real_from_mpfr (&rr, m);
11497 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11498 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11499 but the mpft_t is not, then we underflowed in the
11500 conversion. */
11501 if (!real_isnan (&rr) && !real_isinf (&rr)
11502 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11504 REAL_VALUE_TYPE rmode;
11506 real_convert (&rmode, TYPE_MODE (type), &rr);
11507 /* Proceed iff the specified mode can hold the value. */
11508 if (real_identical (&rmode, &rr))
11509 return build_real (type, rmode);
11512 return NULL_TREE;
11515 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11516 FUNC on it and return the resulting value as a tree with type TYPE.
11517 If MIN and/or MAX are not NULL, then the supplied ARG must be
11518 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11519 acceptable values, otherwise they are not. The mpfr precision is
11520 set to the precision of TYPE. We assume that function FUNC returns
11521 zero if the result could be calculated exactly within the requested
11522 precision. */
11524 static tree
11525 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11526 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11527 bool inclusive)
11529 tree result = NULL_TREE;
11531 STRIP_NOPS (arg);
11533 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
11535 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11537 if (!real_isnan (ra) && !real_isinf (ra)
11538 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11539 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11541 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11542 int inexact;
11543 mpfr_t m;
11545 mpfr_init2 (m, prec);
11546 mpfr_from_real (m, ra);
11547 mpfr_clear_flags();
11548 inexact = func (m, m, GMP_RNDN);
11549 result = do_mpfr_ckconv (m, type, inexact);
11550 mpfr_clear (m);
11554 return result;
11557 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11558 FUNC on it and return the resulting value as a tree with type TYPE.
11559 The mpfr precision is set to the precision of TYPE. We assume that
11560 function FUNC returns zero if the result could be calculated
11561 exactly within the requested precision. */
11563 static tree
11564 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11565 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11567 tree result = NULL_TREE;
11569 STRIP_NOPS (arg1);
11570 STRIP_NOPS (arg2);
11572 if (TREE_CODE (arg1) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg1)
11573 && TREE_CODE (arg2) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg2))
11575 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11576 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11578 if (!real_isnan (ra1) && !real_isinf (ra1)
11579 && !real_isnan (ra2) && !real_isinf (ra2))
11581 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11582 int inexact;
11583 mpfr_t m1, m2;
11585 mpfr_inits2 (prec, m1, m2, NULL);
11586 mpfr_from_real (m1, ra1);
11587 mpfr_from_real (m2, ra2);
11588 mpfr_clear_flags();
11589 inexact = func (m1, m1, m2, GMP_RNDN);
11590 result = do_mpfr_ckconv (m1, type, inexact);
11591 mpfr_clears (m1, m2, NULL);
11595 return result;