1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
31 #include "tree-gimple.h"
34 #include "hard-reg-set.h"
37 #include "insn-config.h"
43 #include "typeclass.h"
48 #include "langhooks.h"
49 #include "basic-block.h"
50 #include "tree-mudflap.h"
51 #include "tree-flow.h"
53 #ifndef PAD_VARARGS_DOWN
54 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 /* Define the names of the builtin function types and codes. */
58 const char *const built_in_class_names[4]
59 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
61 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
62 const char * built_in_names[(int) END_BUILTINS] =
64 #include "builtins.def"
68 /* Setup an array of _DECL trees, make sure each element is
69 initialized to NULL_TREE. */
70 tree built_in_decls[(int) END_BUILTINS];
71 /* Declarations used when constructing the builtin implicitly in the compiler.
72 It may be NULL_TREE when this is invalid (for instance runtime is not
73 required to implement the function call in all cases). */
74 tree implicit_built_in_decls[(int) END_BUILTINS];
76 static const char *c_getstr (tree);
77 static rtx c_readstr (const char *, enum machine_mode);
78 static int target_char_cast (tree, char *);
79 static rtx get_memory_rtx (tree, tree);
80 static int apply_args_size (void);
81 static int apply_result_size (void);
82 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
83 static rtx result_vector (int, rtx);
85 static void expand_builtin_update_setjmp_buf (rtx);
86 static void expand_builtin_prefetch (tree);
87 static rtx expand_builtin_apply_args (void);
88 static rtx expand_builtin_apply_args_1 (void);
89 static rtx expand_builtin_apply (rtx, rtx, rtx);
90 static void expand_builtin_return (rtx);
91 static enum type_class type_to_class (tree);
92 static rtx expand_builtin_classify_type (tree);
93 static void expand_errno_check (tree, rtx);
94 static rtx expand_builtin_mathfn (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
97 static rtx expand_builtin_sincos (tree);
98 static rtx expand_builtin_cexpi (tree, rtx, rtx);
99 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
100 static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx);
101 static rtx expand_builtin_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree);
118 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
124 static rtx expand_builtin_bzero (tree);
125 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_alloca (tree, rtx);
131 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
132 static rtx expand_builtin_frame_address (tree, tree);
133 static rtx expand_builtin_fputs (tree, rtx, bool);
134 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
137 static tree stabilize_va_list (tree, int);
138 static rtx expand_builtin_expect (tree, rtx);
139 static tree fold_builtin_constant_p (tree);
140 static tree fold_builtin_classify_type (tree);
141 static tree fold_builtin_strlen (tree);
142 static tree fold_builtin_inf (tree, int);
143 static tree fold_builtin_nan (tree, tree, int);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_cos (tree, tree, tree);
154 static tree fold_builtin_cosh (tree, tree, tree);
155 static tree fold_builtin_tan (tree, tree);
156 static tree fold_builtin_trunc (tree, tree);
157 static tree fold_builtin_floor (tree, tree);
158 static tree fold_builtin_ceil (tree, tree);
159 static tree fold_builtin_round (tree, tree);
160 static tree fold_builtin_int_roundingfn (tree, tree);
161 static tree fold_builtin_bitop (tree, tree);
162 static tree fold_builtin_memory_op (tree, tree, bool, int);
163 static tree fold_builtin_strchr (tree, tree);
164 static tree fold_builtin_memcmp (tree);
165 static tree fold_builtin_strcmp (tree);
166 static tree fold_builtin_strncmp (tree);
167 static tree fold_builtin_signbit (tree, tree);
168 static tree fold_builtin_copysign (tree, tree, tree);
169 static tree fold_builtin_isascii (tree);
170 static tree fold_builtin_toascii (tree);
171 static tree fold_builtin_isdigit (tree);
172 static tree fold_builtin_fabs (tree, tree);
173 static tree fold_builtin_abs (tree, tree);
174 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
176 static tree fold_builtin_1 (tree, tree, bool);
178 static tree fold_builtin_strpbrk (tree, tree);
179 static tree fold_builtin_strstr (tree, tree);
180 static tree fold_builtin_strrchr (tree, tree);
181 static tree fold_builtin_strcat (tree);
182 static tree fold_builtin_strncat (tree);
183 static tree fold_builtin_strspn (tree);
184 static tree fold_builtin_strcspn (tree);
185 static tree fold_builtin_sprintf (tree, int);
187 static rtx expand_builtin_object_size (tree);
188 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
189 enum built_in_function);
190 static void maybe_emit_chk_warning (tree, enum built_in_function);
191 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
192 static tree fold_builtin_object_size (tree);
193 static tree fold_builtin_strcat_chk (tree, tree);
194 static tree fold_builtin_strncat_chk (tree, tree);
195 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
196 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
197 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
198 static bool init_target_chars (void);
200 static unsigned HOST_WIDE_INT target_newline;
201 static unsigned HOST_WIDE_INT target_percent;
202 static unsigned HOST_WIDE_INT target_c;
203 static unsigned HOST_WIDE_INT target_s;
204 static char target_percent_c[3];
205 static char target_percent_s[3];
206 static char target_percent_s_newline[4];
207 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
208 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
209 static tree do_mpfr_arg2 (tree, tree, tree,
210 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
211 static tree do_mpfr_arg3 (tree, tree, tree, tree,
212 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
213 static tree do_mpfr_sincos (tree, tree, tree);
215 /* Return true if NODE should be considered for inline expansion regardless
216 of the optimization level. This means whenever a function is invoked with
217 its "internal" name, which normally contains the prefix "__builtin". */
219 static bool called_as_built_in (tree node)
221 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
222 if (strncmp (name, "__builtin_", 10) == 0)
224 if (strncmp (name, "__sync_", 7) == 0)
229 /* Return the alignment in bits of EXP, a pointer valued expression.
230 But don't return more than MAX_ALIGN no matter what.
231 The alignment returned is, by default, the alignment of the thing that
232 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
234 Otherwise, look at the expression to see if we can do better, i.e., if the
235 expression is actually pointing at an object whose alignment is tighter. */
238 get_pointer_alignment (tree exp, unsigned int max_align)
240 unsigned int align, inner;
242 /* We rely on TER to compute accurate alignment information. */
243 if (!(optimize && flag_tree_ter))
246 if (!POINTER_TYPE_P (TREE_TYPE (exp)))
249 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
250 align = MIN (align, max_align);
254 switch (TREE_CODE (exp))
258 case NON_LVALUE_EXPR:
259 exp = TREE_OPERAND (exp, 0);
260 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
263 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
264 align = MIN (inner, max_align);
268 /* If sum of pointer + int, restrict our maximum alignment to that
269 imposed by the integer. If not, we can't do any better than
271 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
274 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
275 & (max_align / BITS_PER_UNIT - 1))
279 exp = TREE_OPERAND (exp, 0);
283 /* See what we are pointing at and look at its alignment. */
284 exp = TREE_OPERAND (exp, 0);
286 if (handled_component_p (exp))
288 HOST_WIDE_INT bitsize, bitpos;
290 enum machine_mode mode;
291 int unsignedp, volatilep;
293 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
294 &mode, &unsignedp, &volatilep, true);
296 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
297 if (offset && TREE_CODE (offset) == PLUS_EXPR
298 && host_integerp (TREE_OPERAND (offset, 1), 1))
300 /* Any overflow in calculating offset_bits won't change
303 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
307 inner = MIN (inner, (offset_bits & -offset_bits));
308 offset = TREE_OPERAND (offset, 0);
310 if (offset && TREE_CODE (offset) == MULT_EXPR
311 && host_integerp (TREE_OPERAND (offset, 1), 1))
313 /* Any overflow in calculating offset_factor won't change
315 unsigned offset_factor
316 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
320 inner = MIN (inner, (offset_factor & -offset_factor));
323 inner = MIN (inner, BITS_PER_UNIT);
325 if (TREE_CODE (exp) == FUNCTION_DECL)
326 align = FUNCTION_BOUNDARY;
327 else if (DECL_P (exp))
328 align = MIN (inner, DECL_ALIGN (exp));
329 #ifdef CONSTANT_ALIGNMENT
330 else if (CONSTANT_CLASS_P (exp))
331 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
333 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
334 || TREE_CODE (exp) == INDIRECT_REF)
335 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
337 align = MIN (align, inner);
338 return MIN (align, max_align);
346 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
347 way, because it could contain a zero byte in the middle.
348 TREE_STRING_LENGTH is the size of the character array, not the string.
350 ONLY_VALUE should be nonzero if the result is not going to be emitted
351 into the instruction stream and zero if it is going to be expanded.
352 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
353 is returned, otherwise NULL, since
354 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
355 evaluate the side-effects.
357 The value returned is of type `ssizetype'.
359 Unfortunately, string_constant can't access the values of const char
360 arrays with initializers, so neither can we do so here. */
363 c_strlen (tree src, int only_value)
366 HOST_WIDE_INT offset;
371 if (TREE_CODE (src) == COND_EXPR
372 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
376 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
377 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
378 if (tree_int_cst_equal (len1, len2))
382 if (TREE_CODE (src) == COMPOUND_EXPR
383 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
384 return c_strlen (TREE_OPERAND (src, 1), only_value);
386 src = string_constant (src, &offset_node);
390 max = TREE_STRING_LENGTH (src) - 1;
391 ptr = TREE_STRING_POINTER (src);
393 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
395 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
396 compute the offset to the following null if we don't know where to
397 start searching for it. */
400 for (i = 0; i < max; i++)
404 /* We don't know the starting offset, but we do know that the string
405 has no internal zero bytes. We can assume that the offset falls
406 within the bounds of the string; otherwise, the programmer deserves
407 what he gets. Subtract the offset from the length of the string,
408 and return that. This would perhaps not be valid if we were dealing
409 with named arrays in addition to literal string constants. */
411 return size_diffop (size_int (max), offset_node);
414 /* We have a known offset into the string. Start searching there for
415 a null character if we can represent it as a single HOST_WIDE_INT. */
416 if (offset_node == 0)
418 else if (! host_integerp (offset_node, 0))
421 offset = tree_low_cst (offset_node, 0);
423 /* If the offset is known to be out of bounds, warn, and call strlen at
425 if (offset < 0 || offset > max)
427 warning (0, "offset outside bounds of constant string");
431 /* Use strlen to search for the first zero byte. Since any strings
432 constructed with build_string will have nulls appended, we win even
433 if we get handed something like (char[4])"abcd".
435 Since OFFSET is our starting index into the string, no further
436 calculation is needed. */
437 return ssize_int (strlen (ptr + offset));
440 /* Return a char pointer for a C string if it is a string constant
441 or sum of string constant and integer constant. */
448 src = string_constant (src, &offset_node);
452 if (offset_node == 0)
453 return TREE_STRING_POINTER (src);
454 else if (!host_integerp (offset_node, 1)
455 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
458 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
461 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
462 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
465 c_readstr (const char *str, enum machine_mode mode)
471 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
476 for (i = 0; i < GET_MODE_SIZE (mode); i++)
479 if (WORDS_BIG_ENDIAN)
480 j = GET_MODE_SIZE (mode) - i - 1;
481 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
482 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
483 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
485 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
488 ch = (unsigned char) str[i];
489 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
491 return immed_double_const (c[0], c[1], mode);
494 /* Cast a target constant CST to target CHAR and if that value fits into
495 host char type, return zero and put that value into variable pointed to by
499 target_char_cast (tree cst, char *p)
501 unsigned HOST_WIDE_INT val, hostval;
503 if (!host_integerp (cst, 1)
504 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
507 val = tree_low_cst (cst, 1);
508 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
509 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
512 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
513 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
522 /* Similar to save_expr, but assumes that arbitrary code is not executed
523 in between the multiple evaluations. In particular, we assume that a
524 non-addressable local variable will not be modified. */
527 builtin_save_expr (tree exp)
529 if (TREE_ADDRESSABLE (exp) == 0
530 && (TREE_CODE (exp) == PARM_DECL
531 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
534 return save_expr (exp);
537 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
538 times to get the address of either a higher stack frame, or a return
539 address located within it (depending on FNDECL_CODE). */
542 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
546 #ifdef INITIAL_FRAME_ADDRESS_RTX
547 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
551 /* For a zero count with __builtin_return_address, we don't care what
552 frame address we return, because target-specific definitions will
553 override us. Therefore frame pointer elimination is OK, and using
554 the soft frame pointer is OK.
556 For a nonzero count, or a zero count with __builtin_frame_address,
557 we require a stable offset from the current frame pointer to the
558 previous one, so we must use the hard frame pointer, and
559 we must disable frame pointer elimination. */
560 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
561 tem = frame_pointer_rtx;
564 tem = hard_frame_pointer_rtx;
566 /* Tell reload not to eliminate the frame pointer. */
567 current_function_accesses_prior_frames = 1;
571 /* Some machines need special handling before we can access
572 arbitrary frames. For example, on the SPARC, we must first flush
573 all register windows to the stack. */
574 #ifdef SETUP_FRAME_ADDRESSES
576 SETUP_FRAME_ADDRESSES ();
579 /* On the SPARC, the return address is not in the frame, it is in a
580 register. There is no way to access it off of the current frame
581 pointer, but it can be accessed off the previous frame pointer by
582 reading the value from the register window save area. */
583 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
584 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
588 /* Scan back COUNT frames to the specified frame. */
589 for (i = 0; i < count; i++)
591 /* Assume the dynamic chain pointer is in the word that the
592 frame address points to, unless otherwise specified. */
593 #ifdef DYNAMIC_CHAIN_ADDRESS
594 tem = DYNAMIC_CHAIN_ADDRESS (tem);
596 tem = memory_address (Pmode, tem);
597 tem = gen_frame_mem (Pmode, tem);
598 tem = copy_to_reg (tem);
601 /* For __builtin_frame_address, return what we've got. But, on
602 the SPARC for example, we may have to add a bias. */
603 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
604 #ifdef FRAME_ADDR_RTX
605 return FRAME_ADDR_RTX (tem);
610 /* For __builtin_return_address, get the return address from that frame. */
611 #ifdef RETURN_ADDR_RTX
612 tem = RETURN_ADDR_RTX (count, tem);
614 tem = memory_address (Pmode,
615 plus_constant (tem, GET_MODE_SIZE (Pmode)));
616 tem = gen_frame_mem (Pmode, tem);
621 /* Alias set used for setjmp buffer. */
622 static HOST_WIDE_INT setjmp_alias_set = -1;
624 /* Construct the leading half of a __builtin_setjmp call. Control will
625 return to RECEIVER_LABEL. This is also called directly by the SJLJ
626 exception handling code. */
629 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
631 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
635 if (setjmp_alias_set == -1)
636 setjmp_alias_set = new_alias_set ();
638 buf_addr = convert_memory_address (Pmode, buf_addr);
640 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
642 /* We store the frame pointer and the address of receiver_label in
643 the buffer and use the rest of it for the stack save area, which
644 is machine-dependent. */
646 mem = gen_rtx_MEM (Pmode, buf_addr);
647 set_mem_alias_set (mem, setjmp_alias_set);
648 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
650 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
651 set_mem_alias_set (mem, setjmp_alias_set);
653 emit_move_insn (validize_mem (mem),
654 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
656 stack_save = gen_rtx_MEM (sa_mode,
657 plus_constant (buf_addr,
658 2 * GET_MODE_SIZE (Pmode)));
659 set_mem_alias_set (stack_save, setjmp_alias_set);
660 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
662 /* If there is further processing to do, do it. */
663 #ifdef HAVE_builtin_setjmp_setup
664 if (HAVE_builtin_setjmp_setup)
665 emit_insn (gen_builtin_setjmp_setup (buf_addr));
668 /* Tell optimize_save_area_alloca that extra work is going to
669 need to go on during alloca. */
670 current_function_calls_setjmp = 1;
672 /* Set this so all the registers get saved in our frame; we need to be
673 able to copy the saved values for any registers from frames we unwind. */
674 current_function_has_nonlocal_label = 1;
677 /* Construct the trailing part of a __builtin_setjmp call. This is
678 also called directly by the SJLJ exception handling code. */
681 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
683 /* Clobber the FP when we get here, so we have to make sure it's
684 marked as used by this function. */
685 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
687 /* Mark the static chain as clobbered here so life information
688 doesn't get messed up for it. */
689 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
691 /* Now put in the code to restore the frame pointer, and argument
692 pointer, if needed. */
693 #ifdef HAVE_nonlocal_goto
694 if (! HAVE_nonlocal_goto)
697 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
698 /* This might change the hard frame pointer in ways that aren't
699 apparent to early optimization passes, so force a clobber. */
700 emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx));
703 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
704 if (fixed_regs[ARG_POINTER_REGNUM])
706 #ifdef ELIMINABLE_REGS
708 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
710 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
711 if (elim_regs[i].from == ARG_POINTER_REGNUM
712 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
715 if (i == ARRAY_SIZE (elim_regs))
718 /* Now restore our arg pointer from the address at which it
719 was saved in our stack frame. */
720 emit_move_insn (virtual_incoming_args_rtx,
721 copy_to_reg (get_arg_pointer_save_area (cfun)));
726 #ifdef HAVE_builtin_setjmp_receiver
727 if (HAVE_builtin_setjmp_receiver)
728 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
731 #ifdef HAVE_nonlocal_goto_receiver
732 if (HAVE_nonlocal_goto_receiver)
733 emit_insn (gen_nonlocal_goto_receiver ());
738 /* @@@ This is a kludge. Not all machine descriptions define a blockage
739 insn, but we must not allow the code we just generated to be reordered
740 by scheduling. Specifically, the update of the frame pointer must
741 happen immediately, not later. So emit an ASM_INPUT to act as blockage
743 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
746 /* __builtin_longjmp is passed a pointer to an array of five words (not
747 all will be used on all machines). It operates similarly to the C
748 library function of the same name, but is more efficient. Much of
749 the code below is copied from the handling of non-local gotos. */
752 expand_builtin_longjmp (rtx buf_addr, rtx value)
754 rtx fp, lab, stack, insn, last;
755 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
757 if (setjmp_alias_set == -1)
758 setjmp_alias_set = new_alias_set ();
760 buf_addr = convert_memory_address (Pmode, buf_addr);
762 buf_addr = force_reg (Pmode, buf_addr);
764 /* We used to store value in static_chain_rtx, but that fails if pointers
765 are smaller than integers. We instead require that the user must pass
766 a second argument of 1, because that is what builtin_setjmp will
767 return. This also makes EH slightly more efficient, since we are no
768 longer copying around a value that we don't care about. */
769 gcc_assert (value == const1_rtx);
771 last = get_last_insn ();
772 #ifdef HAVE_builtin_longjmp
773 if (HAVE_builtin_longjmp)
774 emit_insn (gen_builtin_longjmp (buf_addr));
778 fp = gen_rtx_MEM (Pmode, buf_addr);
779 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
780 GET_MODE_SIZE (Pmode)));
782 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
783 2 * GET_MODE_SIZE (Pmode)));
784 set_mem_alias_set (fp, setjmp_alias_set);
785 set_mem_alias_set (lab, setjmp_alias_set);
786 set_mem_alias_set (stack, setjmp_alias_set);
788 /* Pick up FP, label, and SP from the block and jump. This code is
789 from expand_goto in stmt.c; see there for detailed comments. */
790 #ifdef HAVE_nonlocal_goto
791 if (HAVE_nonlocal_goto)
792 /* We have to pass a value to the nonlocal_goto pattern that will
793 get copied into the static_chain pointer, but it does not matter
794 what that value is, because builtin_setjmp does not use it. */
795 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
799 lab = copy_to_reg (lab);
801 emit_insn (gen_rtx_CLOBBER (VOIDmode,
802 gen_rtx_MEM (BLKmode,
803 gen_rtx_SCRATCH (VOIDmode))));
804 emit_insn (gen_rtx_CLOBBER (VOIDmode,
805 gen_rtx_MEM (BLKmode,
806 hard_frame_pointer_rtx)));
808 emit_move_insn (hard_frame_pointer_rtx, fp);
809 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
811 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
812 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
813 emit_indirect_jump (lab);
817 /* Search backwards and mark the jump insn as a non-local goto.
818 Note that this precludes the use of __builtin_longjmp to a
819 __builtin_setjmp target in the same function. However, we've
820 already cautioned the user that these functions are for
821 internal exception handling use only. */
822 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
824 gcc_assert (insn != last);
828 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
832 else if (CALL_P (insn))
837 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
838 and the address of the save area. */
841 expand_builtin_nonlocal_goto (tree arglist)
843 tree t_label, t_save_area;
844 rtx r_label, r_save_area, r_fp, r_sp, insn;
846 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
849 t_label = TREE_VALUE (arglist);
850 arglist = TREE_CHAIN (arglist);
851 t_save_area = TREE_VALUE (arglist);
853 r_label = expand_normal (t_label);
854 r_label = convert_memory_address (Pmode, r_label);
855 r_save_area = expand_normal (t_save_area);
856 r_save_area = convert_memory_address (Pmode, r_save_area);
857 r_fp = gen_rtx_MEM (Pmode, r_save_area);
858 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
859 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
861 current_function_has_nonlocal_goto = 1;
863 #ifdef HAVE_nonlocal_goto
864 /* ??? We no longer need to pass the static chain value, afaik. */
865 if (HAVE_nonlocal_goto)
866 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
870 r_label = copy_to_reg (r_label);
872 emit_insn (gen_rtx_CLOBBER (VOIDmode,
873 gen_rtx_MEM (BLKmode,
874 gen_rtx_SCRATCH (VOIDmode))));
876 emit_insn (gen_rtx_CLOBBER (VOIDmode,
877 gen_rtx_MEM (BLKmode,
878 hard_frame_pointer_rtx)));
880 /* Restore frame pointer for containing function.
881 This sets the actual hard register used for the frame pointer
882 to the location of the function's incoming static chain info.
883 The non-local goto handler will then adjust it to contain the
884 proper value and reload the argument pointer, if needed. */
885 emit_move_insn (hard_frame_pointer_rtx, r_fp);
886 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
888 /* USE of hard_frame_pointer_rtx added for consistency;
889 not clear if really needed. */
890 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
891 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
892 emit_indirect_jump (r_label);
895 /* Search backwards to the jump insn and mark it as a
897 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
901 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
902 const0_rtx, REG_NOTES (insn));
905 else if (CALL_P (insn))
912 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
913 (not all will be used on all machines) that was passed to __builtin_setjmp.
914 It updates the stack pointer in that block to correspond to the current
918 expand_builtin_update_setjmp_buf (rtx buf_addr)
920 enum machine_mode sa_mode = Pmode;
924 #ifdef HAVE_save_stack_nonlocal
925 if (HAVE_save_stack_nonlocal)
926 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
928 #ifdef STACK_SAVEAREA_MODE
929 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
933 = gen_rtx_MEM (sa_mode,
936 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
940 emit_insn (gen_setjmp ());
943 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
946 /* Expand a call to __builtin_prefetch. For a target that does not support
947 data prefetch, evaluate the memory address argument in case it has side
951 expand_builtin_prefetch (tree arglist)
953 tree arg0, arg1, arg2;
956 if (!validate_arglist (arglist, POINTER_TYPE, 0))
959 arg0 = TREE_VALUE (arglist);
960 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
961 zero (read) and argument 2 (locality) defaults to 3 (high degree of
963 if (TREE_CHAIN (arglist))
965 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
966 if (TREE_CHAIN (TREE_CHAIN (arglist)))
967 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
969 arg2 = build_int_cst (NULL_TREE, 3);
973 arg1 = integer_zero_node;
974 arg2 = build_int_cst (NULL_TREE, 3);
977 /* Argument 0 is an address. */
978 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
980 /* Argument 1 (read/write flag) must be a compile-time constant int. */
981 if (TREE_CODE (arg1) != INTEGER_CST)
983 error ("second argument to %<__builtin_prefetch%> must be a constant");
984 arg1 = integer_zero_node;
986 op1 = expand_normal (arg1);
987 /* Argument 1 must be either zero or one. */
988 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
990 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
995 /* Argument 2 (locality) must be a compile-time constant int. */
996 if (TREE_CODE (arg2) != INTEGER_CST)
998 error ("third argument to %<__builtin_prefetch%> must be a constant");
999 arg2 = integer_zero_node;
1001 op2 = expand_normal (arg2);
1002 /* Argument 2 must be 0, 1, 2, or 3. */
1003 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1005 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1009 #ifdef HAVE_prefetch
1012 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1014 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1015 || (GET_MODE (op0) != Pmode))
1017 op0 = convert_memory_address (Pmode, op0);
1018 op0 = force_reg (Pmode, op0);
1020 emit_insn (gen_prefetch (op0, op1, op2));
1024 /* Don't do anything with direct references to volatile memory, but
1025 generate code to handle other side effects. */
1026 if (!MEM_P (op0) && side_effects_p (op0))
1030 /* Get a MEM rtx for expression EXP which is the address of an operand
1031 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1032 the maximum length of the block of memory that might be accessed or
1036 get_memory_rtx (tree exp, tree len)
1038 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1039 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1041 /* Get an expression we can use to find the attributes to assign to MEM.
1042 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1043 we can. First remove any nops. */
1044 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1045 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1046 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1047 exp = TREE_OPERAND (exp, 0);
1049 if (TREE_CODE (exp) == ADDR_EXPR)
1050 exp = TREE_OPERAND (exp, 0);
1051 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1052 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1056 /* Honor attributes derived from exp, except for the alias set
1057 (as builtin stringops may alias with anything) and the size
1058 (as stringops may access multiple array elements). */
1061 set_mem_attributes (mem, exp, 0);
1063 /* Allow the string and memory builtins to overflow from one
1064 field into another, see http://gcc.gnu.org/PR23561.
1065 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1066 memory accessed by the string or memory builtin will fit
1067 within the field. */
1068 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1070 tree mem_expr = MEM_EXPR (mem);
1071 HOST_WIDE_INT offset = -1, length = -1;
1074 while (TREE_CODE (inner) == ARRAY_REF
1075 || TREE_CODE (inner) == NOP_EXPR
1076 || TREE_CODE (inner) == CONVERT_EXPR
1077 || TREE_CODE (inner) == NON_LVALUE_EXPR
1078 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1079 || TREE_CODE (inner) == SAVE_EXPR)
1080 inner = TREE_OPERAND (inner, 0);
1082 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1084 if (MEM_OFFSET (mem)
1085 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1086 offset = INTVAL (MEM_OFFSET (mem));
1088 if (offset >= 0 && len && host_integerp (len, 0))
1089 length = tree_low_cst (len, 0);
1091 while (TREE_CODE (inner) == COMPONENT_REF)
1093 tree field = TREE_OPERAND (inner, 1);
1094 gcc_assert (! DECL_BIT_FIELD (field));
1095 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1096 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1099 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1100 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1103 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1104 /* If we can prove the memory starting at XEXP (mem, 0)
1105 and ending at XEXP (mem, 0) + LENGTH will fit into
1106 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1109 && offset + length <= size)
1114 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1115 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1116 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1124 mem_expr = TREE_OPERAND (mem_expr, 0);
1125 inner = TREE_OPERAND (inner, 0);
1128 if (mem_expr == NULL)
1130 if (mem_expr != MEM_EXPR (mem))
1132 set_mem_expr (mem, mem_expr);
1133 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1136 set_mem_alias_set (mem, 0);
1137 set_mem_size (mem, NULL_RTX);
1143 /* Built-in functions to perform an untyped call and return. */
1145 /* For each register that may be used for calling a function, this
1146 gives a mode used to copy the register's value. VOIDmode indicates
1147 the register is not used for calling a function. If the machine
1148 has register windows, this gives only the outbound registers.
1149 INCOMING_REGNO gives the corresponding inbound register. */
1150 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1152 /* For each register that may be used for returning values, this gives
1153 a mode used to copy the register's value. VOIDmode indicates the
1154 register is not used for returning values. If the machine has
1155 register windows, this gives only the outbound registers.
1156 INCOMING_REGNO gives the corresponding inbound register. */
1157 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1159 /* For each register that may be used for calling a function, this
1160 gives the offset of that register into the block returned by
1161 __builtin_apply_args. 0 indicates that the register is not
1162 used for calling a function. */
1163 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1165 /* Return the size required for the block returned by __builtin_apply_args,
1166 and initialize apply_args_mode. */
1169 apply_args_size (void)
1171 static int size = -1;
1174 enum machine_mode mode;
1176 /* The values computed by this function never change. */
1179 /* The first value is the incoming arg-pointer. */
1180 size = GET_MODE_SIZE (Pmode);
1182 /* The second value is the structure value address unless this is
1183 passed as an "invisible" first argument. */
1184 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1185 size += GET_MODE_SIZE (Pmode);
1187 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1188 if (FUNCTION_ARG_REGNO_P (regno))
1190 mode = reg_raw_mode[regno];
1192 gcc_assert (mode != VOIDmode);
1194 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1195 if (size % align != 0)
1196 size = CEIL (size, align) * align;
1197 apply_args_reg_offset[regno] = size;
1198 size += GET_MODE_SIZE (mode);
1199 apply_args_mode[regno] = mode;
1203 apply_args_mode[regno] = VOIDmode;
1204 apply_args_reg_offset[regno] = 0;
1210 /* Return the size required for the block returned by __builtin_apply,
1211 and initialize apply_result_mode. */
1214 apply_result_size (void)
1216 static int size = -1;
1218 enum machine_mode mode;
1220 /* The values computed by this function never change. */
1225 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1226 if (FUNCTION_VALUE_REGNO_P (regno))
1228 mode = reg_raw_mode[regno];
1230 gcc_assert (mode != VOIDmode);
1232 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1233 if (size % align != 0)
1234 size = CEIL (size, align) * align;
1235 size += GET_MODE_SIZE (mode);
1236 apply_result_mode[regno] = mode;
1239 apply_result_mode[regno] = VOIDmode;
1241 /* Allow targets that use untyped_call and untyped_return to override
1242 the size so that machine-specific information can be stored here. */
1243 #ifdef APPLY_RESULT_SIZE
1244 size = APPLY_RESULT_SIZE;
1250 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1251 /* Create a vector describing the result block RESULT. If SAVEP is true,
1252 the result block is used to save the values; otherwise it is used to
1253 restore the values. */
1256 result_vector (int savep, rtx result)
1258 int regno, size, align, nelts;
1259 enum machine_mode mode;
1261 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1264 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1265 if ((mode = apply_result_mode[regno]) != VOIDmode)
1267 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1268 if (size % align != 0)
1269 size = CEIL (size, align) * align;
1270 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1271 mem = adjust_address (result, mode, size);
1272 savevec[nelts++] = (savep
1273 ? gen_rtx_SET (VOIDmode, mem, reg)
1274 : gen_rtx_SET (VOIDmode, reg, mem));
1275 size += GET_MODE_SIZE (mode);
1277 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1279 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1281 /* Save the state required to perform an untyped call with the same
1282 arguments as were passed to the current function. */
1285 expand_builtin_apply_args_1 (void)
1288 int size, align, regno;
1289 enum machine_mode mode;
1290 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1292 /* Create a block where the arg-pointer, structure value address,
1293 and argument registers can be saved. */
1294 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1296 /* Walk past the arg-pointer and structure value address. */
1297 size = GET_MODE_SIZE (Pmode);
1298 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1299 size += GET_MODE_SIZE (Pmode);
1301 /* Save each register used in calling a function to the block. */
1302 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1303 if ((mode = apply_args_mode[regno]) != VOIDmode)
1305 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1306 if (size % align != 0)
1307 size = CEIL (size, align) * align;
1309 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1311 emit_move_insn (adjust_address (registers, mode, size), tem);
1312 size += GET_MODE_SIZE (mode);
1315 /* Save the arg pointer to the block. */
1316 tem = copy_to_reg (virtual_incoming_args_rtx);
1317 #ifdef STACK_GROWS_DOWNWARD
1318 /* We need the pointer as the caller actually passed them to us, not
1319 as we might have pretended they were passed. Make sure it's a valid
1320 operand, as emit_move_insn isn't expected to handle a PLUS. */
1322 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1325 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1327 size = GET_MODE_SIZE (Pmode);
1329 /* Save the structure value address unless this is passed as an
1330 "invisible" first argument. */
1331 if (struct_incoming_value)
1333 emit_move_insn (adjust_address (registers, Pmode, size),
1334 copy_to_reg (struct_incoming_value));
1335 size += GET_MODE_SIZE (Pmode);
1338 /* Return the address of the block. */
1339 return copy_addr_to_reg (XEXP (registers, 0));
1342 /* __builtin_apply_args returns block of memory allocated on
1343 the stack into which is stored the arg pointer, structure
1344 value address, static chain, and all the registers that might
1345 possibly be used in performing a function call. The code is
1346 moved to the start of the function so the incoming values are
1350 expand_builtin_apply_args (void)
1352 /* Don't do __builtin_apply_args more than once in a function.
1353 Save the result of the first call and reuse it. */
1354 if (apply_args_value != 0)
1355 return apply_args_value;
1357 /* When this function is called, it means that registers must be
1358 saved on entry to this function. So we migrate the
1359 call to the first insn of this function. */
1364 temp = expand_builtin_apply_args_1 ();
1368 apply_args_value = temp;
1370 /* Put the insns after the NOTE that starts the function.
1371 If this is inside a start_sequence, make the outer-level insn
1372 chain current, so the code is placed at the start of the
1374 push_topmost_sequence ();
1375 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1376 pop_topmost_sequence ();
1381 /* Perform an untyped call and save the state required to perform an
1382 untyped return of whatever value was returned by the given function. */
1385 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1387 int size, align, regno;
1388 enum machine_mode mode;
1389 rtx incoming_args, result, reg, dest, src, call_insn;
1390 rtx old_stack_level = 0;
1391 rtx call_fusage = 0;
1392 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1394 arguments = convert_memory_address (Pmode, arguments);
1396 /* Create a block where the return registers can be saved. */
1397 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1399 /* Fetch the arg pointer from the ARGUMENTS block. */
1400 incoming_args = gen_reg_rtx (Pmode);
1401 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1402 #ifndef STACK_GROWS_DOWNWARD
1403 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1404 incoming_args, 0, OPTAB_LIB_WIDEN);
1407 /* Push a new argument block and copy the arguments. Do not allow
1408 the (potential) memcpy call below to interfere with our stack
1410 do_pending_stack_adjust ();
1413 /* Save the stack with nonlocal if available. */
1414 #ifdef HAVE_save_stack_nonlocal
1415 if (HAVE_save_stack_nonlocal)
1416 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1419 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1421 /* Allocate a block of memory onto the stack and copy the memory
1422 arguments to the outgoing arguments address. */
1423 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1424 dest = virtual_outgoing_args_rtx;
1425 #ifndef STACK_GROWS_DOWNWARD
1426 if (GET_CODE (argsize) == CONST_INT)
1427 dest = plus_constant (dest, -INTVAL (argsize));
1429 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1431 dest = gen_rtx_MEM (BLKmode, dest);
1432 set_mem_align (dest, PARM_BOUNDARY);
1433 src = gen_rtx_MEM (BLKmode, incoming_args);
1434 set_mem_align (src, PARM_BOUNDARY);
1435 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1437 /* Refer to the argument block. */
1439 arguments = gen_rtx_MEM (BLKmode, arguments);
1440 set_mem_align (arguments, PARM_BOUNDARY);
1442 /* Walk past the arg-pointer and structure value address. */
1443 size = GET_MODE_SIZE (Pmode);
1445 size += GET_MODE_SIZE (Pmode);
1447 /* Restore each of the registers previously saved. Make USE insns
1448 for each of these registers for use in making the call. */
1449 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1450 if ((mode = apply_args_mode[regno]) != VOIDmode)
1452 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1453 if (size % align != 0)
1454 size = CEIL (size, align) * align;
1455 reg = gen_rtx_REG (mode, regno);
1456 emit_move_insn (reg, adjust_address (arguments, mode, size));
1457 use_reg (&call_fusage, reg);
1458 size += GET_MODE_SIZE (mode);
1461 /* Restore the structure value address unless this is passed as an
1462 "invisible" first argument. */
1463 size = GET_MODE_SIZE (Pmode);
1466 rtx value = gen_reg_rtx (Pmode);
1467 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1468 emit_move_insn (struct_value, value);
1469 if (REG_P (struct_value))
1470 use_reg (&call_fusage, struct_value);
1471 size += GET_MODE_SIZE (Pmode);
1474 /* All arguments and registers used for the call are set up by now! */
1475 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1477 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1478 and we don't want to load it into a register as an optimization,
1479 because prepare_call_address already did it if it should be done. */
1480 if (GET_CODE (function) != SYMBOL_REF)
1481 function = memory_address (FUNCTION_MODE, function);
1483 /* Generate the actual call instruction and save the return value. */
1484 #ifdef HAVE_untyped_call
1485 if (HAVE_untyped_call)
1486 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1487 result, result_vector (1, result)));
1490 #ifdef HAVE_call_value
1491 if (HAVE_call_value)
1495 /* Locate the unique return register. It is not possible to
1496 express a call that sets more than one return register using
1497 call_value; use untyped_call for that. In fact, untyped_call
1498 only needs to save the return registers in the given block. */
1499 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1500 if ((mode = apply_result_mode[regno]) != VOIDmode)
1502 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1504 valreg = gen_rtx_REG (mode, regno);
1507 emit_call_insn (GEN_CALL_VALUE (valreg,
1508 gen_rtx_MEM (FUNCTION_MODE, function),
1509 const0_rtx, NULL_RTX, const0_rtx));
1511 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1517 /* Find the CALL insn we just emitted, and attach the register usage
1519 call_insn = last_call_insn ();
1520 add_function_usage_to (call_insn, call_fusage);
1522 /* Restore the stack. */
1523 #ifdef HAVE_save_stack_nonlocal
1524 if (HAVE_save_stack_nonlocal)
1525 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1528 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1532 /* Return the address of the result block. */
1533 result = copy_addr_to_reg (XEXP (result, 0));
1534 return convert_memory_address (ptr_mode, result);
1537 /* Perform an untyped return. */
1540 expand_builtin_return (rtx result)
1542 int size, align, regno;
1543 enum machine_mode mode;
1545 rtx call_fusage = 0;
1547 result = convert_memory_address (Pmode, result);
1549 apply_result_size ();
1550 result = gen_rtx_MEM (BLKmode, result);
1552 #ifdef HAVE_untyped_return
1553 if (HAVE_untyped_return)
1555 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1561 /* Restore the return value and note that each value is used. */
1563 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1564 if ((mode = apply_result_mode[regno]) != VOIDmode)
1566 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1567 if (size % align != 0)
1568 size = CEIL (size, align) * align;
1569 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1570 emit_move_insn (reg, adjust_address (result, mode, size));
1572 push_to_sequence (call_fusage);
1573 emit_insn (gen_rtx_USE (VOIDmode, reg));
1574 call_fusage = get_insns ();
1576 size += GET_MODE_SIZE (mode);
1579 /* Put the USE insns before the return. */
1580 emit_insn (call_fusage);
1582 /* Return whatever values was restored by jumping directly to the end
1584 expand_naked_return ();
1587 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1589 static enum type_class
1590 type_to_class (tree type)
1592 switch (TREE_CODE (type))
1594 case VOID_TYPE: return void_type_class;
1595 case INTEGER_TYPE: return integer_type_class;
1596 case ENUMERAL_TYPE: return enumeral_type_class;
1597 case BOOLEAN_TYPE: return boolean_type_class;
1598 case POINTER_TYPE: return pointer_type_class;
1599 case REFERENCE_TYPE: return reference_type_class;
1600 case OFFSET_TYPE: return offset_type_class;
1601 case REAL_TYPE: return real_type_class;
1602 case COMPLEX_TYPE: return complex_type_class;
1603 case FUNCTION_TYPE: return function_type_class;
1604 case METHOD_TYPE: return method_type_class;
1605 case RECORD_TYPE: return record_type_class;
1607 case QUAL_UNION_TYPE: return union_type_class;
1608 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1609 ? string_type_class : array_type_class);
1610 case LANG_TYPE: return lang_type_class;
1611 default: return no_type_class;
1615 /* Expand a call to __builtin_classify_type with arguments found in
1619 expand_builtin_classify_type (tree arglist)
1622 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1623 return GEN_INT (no_type_class);
1626 /* This helper macro, meant to be used in mathfn_built_in below,
1627 determines which among a set of three builtin math functions is
1628 appropriate for a given type mode. The `F' and `L' cases are
1629 automatically generated from the `double' case. */
1630 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1631 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1632 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1633 fcodel = BUILT_IN_MATHFN##L ; break;
1635 /* Return mathematic function equivalent to FN but operating directly
1636 on TYPE, if available. If we can't do the conversion, return zero. */
1638 mathfn_built_in (tree type, enum built_in_function fn)
1640 enum built_in_function fcode, fcodef, fcodel;
1644 CASE_MATHFN (BUILT_IN_ACOS)
1645 CASE_MATHFN (BUILT_IN_ACOSH)
1646 CASE_MATHFN (BUILT_IN_ASIN)
1647 CASE_MATHFN (BUILT_IN_ASINH)
1648 CASE_MATHFN (BUILT_IN_ATAN)
1649 CASE_MATHFN (BUILT_IN_ATAN2)
1650 CASE_MATHFN (BUILT_IN_ATANH)
1651 CASE_MATHFN (BUILT_IN_CBRT)
1652 CASE_MATHFN (BUILT_IN_CEIL)
1653 CASE_MATHFN (BUILT_IN_CEXPI)
1654 CASE_MATHFN (BUILT_IN_COPYSIGN)
1655 CASE_MATHFN (BUILT_IN_COS)
1656 CASE_MATHFN (BUILT_IN_COSH)
1657 CASE_MATHFN (BUILT_IN_DREM)
1658 CASE_MATHFN (BUILT_IN_ERF)
1659 CASE_MATHFN (BUILT_IN_ERFC)
1660 CASE_MATHFN (BUILT_IN_EXP)
1661 CASE_MATHFN (BUILT_IN_EXP10)
1662 CASE_MATHFN (BUILT_IN_EXP2)
1663 CASE_MATHFN (BUILT_IN_EXPM1)
1664 CASE_MATHFN (BUILT_IN_FABS)
1665 CASE_MATHFN (BUILT_IN_FDIM)
1666 CASE_MATHFN (BUILT_IN_FLOOR)
1667 CASE_MATHFN (BUILT_IN_FMA)
1668 CASE_MATHFN (BUILT_IN_FMAX)
1669 CASE_MATHFN (BUILT_IN_FMIN)
1670 CASE_MATHFN (BUILT_IN_FMOD)
1671 CASE_MATHFN (BUILT_IN_FREXP)
1672 CASE_MATHFN (BUILT_IN_GAMMA)
1673 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1674 CASE_MATHFN (BUILT_IN_HYPOT)
1675 CASE_MATHFN (BUILT_IN_ILOGB)
1676 CASE_MATHFN (BUILT_IN_INF)
1677 CASE_MATHFN (BUILT_IN_J0)
1678 CASE_MATHFN (BUILT_IN_J1)
1679 CASE_MATHFN (BUILT_IN_JN)
1680 CASE_MATHFN (BUILT_IN_LCEIL)
1681 CASE_MATHFN (BUILT_IN_LDEXP)
1682 CASE_MATHFN (BUILT_IN_LFLOOR)
1683 CASE_MATHFN (BUILT_IN_LGAMMA)
1684 CASE_MATHFN (BUILT_IN_LLCEIL)
1685 CASE_MATHFN (BUILT_IN_LLFLOOR)
1686 CASE_MATHFN (BUILT_IN_LLRINT)
1687 CASE_MATHFN (BUILT_IN_LLROUND)
1688 CASE_MATHFN (BUILT_IN_LOG)
1689 CASE_MATHFN (BUILT_IN_LOG10)
1690 CASE_MATHFN (BUILT_IN_LOG1P)
1691 CASE_MATHFN (BUILT_IN_LOG2)
1692 CASE_MATHFN (BUILT_IN_LOGB)
1693 CASE_MATHFN (BUILT_IN_LRINT)
1694 CASE_MATHFN (BUILT_IN_LROUND)
1695 CASE_MATHFN (BUILT_IN_MODF)
1696 CASE_MATHFN (BUILT_IN_NAN)
1697 CASE_MATHFN (BUILT_IN_NANS)
1698 CASE_MATHFN (BUILT_IN_NEARBYINT)
1699 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1700 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1701 CASE_MATHFN (BUILT_IN_POW)
1702 CASE_MATHFN (BUILT_IN_POWI)
1703 CASE_MATHFN (BUILT_IN_POW10)
1704 CASE_MATHFN (BUILT_IN_REMAINDER)
1705 CASE_MATHFN (BUILT_IN_REMQUO)
1706 CASE_MATHFN (BUILT_IN_RINT)
1707 CASE_MATHFN (BUILT_IN_ROUND)
1708 CASE_MATHFN (BUILT_IN_SCALB)
1709 CASE_MATHFN (BUILT_IN_SCALBLN)
1710 CASE_MATHFN (BUILT_IN_SCALBN)
1711 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1712 CASE_MATHFN (BUILT_IN_SIN)
1713 CASE_MATHFN (BUILT_IN_SINCOS)
1714 CASE_MATHFN (BUILT_IN_SINH)
1715 CASE_MATHFN (BUILT_IN_SQRT)
1716 CASE_MATHFN (BUILT_IN_TAN)
1717 CASE_MATHFN (BUILT_IN_TANH)
1718 CASE_MATHFN (BUILT_IN_TGAMMA)
1719 CASE_MATHFN (BUILT_IN_TRUNC)
1720 CASE_MATHFN (BUILT_IN_Y0)
1721 CASE_MATHFN (BUILT_IN_Y1)
1722 CASE_MATHFN (BUILT_IN_YN)
1728 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1729 return implicit_built_in_decls[fcode];
1730 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1731 return implicit_built_in_decls[fcodef];
1732 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1733 return implicit_built_in_decls[fcodel];
1738 /* If errno must be maintained, expand the RTL to check if the result,
1739 TARGET, of a built-in function call, EXP, is NaN, and if so set
1743 expand_errno_check (tree exp, rtx target)
1745 rtx lab = gen_label_rtx ();
1747 /* Test the result; if it is NaN, set errno=EDOM because
1748 the argument was not in the domain. */
1749 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1753 /* If this built-in doesn't throw an exception, set errno directly. */
1754 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1756 #ifdef GEN_ERRNO_RTX
1757 rtx errno_rtx = GEN_ERRNO_RTX;
1760 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1762 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1768 /* We can't set errno=EDOM directly; let the library call do it.
1769 Pop the arguments right away in case the call gets deleted. */
1771 expand_call (exp, target, 0);
1777 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1778 Return 0 if a normal call should be emitted rather than expanding the
1779 function in-line. EXP is the expression that is a call to the builtin
1780 function; if convenient, the result should be placed in TARGET.
1781 SUBTARGET may be used as the target for computing one of EXP's operands. */
1784 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1786 optab builtin_optab;
1787 rtx op0, insns, before_call;
1788 tree fndecl = get_callee_fndecl (exp);
1789 tree arglist = TREE_OPERAND (exp, 1);
1790 enum machine_mode mode;
1791 bool errno_set = false;
1794 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1797 arg = TREE_VALUE (arglist);
1799 switch (DECL_FUNCTION_CODE (fndecl))
1801 CASE_FLT_FN (BUILT_IN_SQRT):
1802 errno_set = ! tree_expr_nonnegative_p (arg);
1803 builtin_optab = sqrt_optab;
1805 CASE_FLT_FN (BUILT_IN_EXP):
1806 errno_set = true; builtin_optab = exp_optab; break;
1807 CASE_FLT_FN (BUILT_IN_EXP10):
1808 CASE_FLT_FN (BUILT_IN_POW10):
1809 errno_set = true; builtin_optab = exp10_optab; break;
1810 CASE_FLT_FN (BUILT_IN_EXP2):
1811 errno_set = true; builtin_optab = exp2_optab; break;
1812 CASE_FLT_FN (BUILT_IN_EXPM1):
1813 errno_set = true; builtin_optab = expm1_optab; break;
1814 CASE_FLT_FN (BUILT_IN_LOGB):
1815 errno_set = true; builtin_optab = logb_optab; break;
1816 CASE_FLT_FN (BUILT_IN_ILOGB):
1817 errno_set = true; builtin_optab = ilogb_optab; break;
1818 CASE_FLT_FN (BUILT_IN_LOG):
1819 errno_set = true; builtin_optab = log_optab; break;
1820 CASE_FLT_FN (BUILT_IN_LOG10):
1821 errno_set = true; builtin_optab = log10_optab; break;
1822 CASE_FLT_FN (BUILT_IN_LOG2):
1823 errno_set = true; builtin_optab = log2_optab; break;
1824 CASE_FLT_FN (BUILT_IN_LOG1P):
1825 errno_set = true; builtin_optab = log1p_optab; break;
1826 CASE_FLT_FN (BUILT_IN_ASIN):
1827 builtin_optab = asin_optab; break;
1828 CASE_FLT_FN (BUILT_IN_ACOS):
1829 builtin_optab = acos_optab; break;
1830 CASE_FLT_FN (BUILT_IN_TAN):
1831 builtin_optab = tan_optab; break;
1832 CASE_FLT_FN (BUILT_IN_ATAN):
1833 builtin_optab = atan_optab; break;
1834 CASE_FLT_FN (BUILT_IN_FLOOR):
1835 builtin_optab = floor_optab; break;
1836 CASE_FLT_FN (BUILT_IN_CEIL):
1837 builtin_optab = ceil_optab; break;
1838 CASE_FLT_FN (BUILT_IN_TRUNC):
1839 builtin_optab = btrunc_optab; break;
1840 CASE_FLT_FN (BUILT_IN_ROUND):
1841 builtin_optab = round_optab; break;
1842 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1843 builtin_optab = nearbyint_optab;
1844 if (flag_trapping_math)
1846 /* Else fallthrough and expand as rint. */
1847 CASE_FLT_FN (BUILT_IN_RINT):
1848 builtin_optab = rint_optab; break;
1853 /* Make a suitable register to place result in. */
1854 mode = TYPE_MODE (TREE_TYPE (exp));
1856 if (! flag_errno_math || ! HONOR_NANS (mode))
1859 /* Before working hard, check whether the instruction is available. */
1860 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1862 target = gen_reg_rtx (mode);
1864 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1865 need to expand the argument again. This way, we will not perform
1866 side-effects more the once. */
1867 narg = builtin_save_expr (arg);
1871 arglist = build_tree_list (NULL_TREE, arg);
1872 exp = build_function_call_expr (fndecl, arglist);
1875 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1879 /* Compute into TARGET.
1880 Set TARGET to wherever the result comes back. */
1881 target = expand_unop (mode, builtin_optab, op0, target, 0);
1886 expand_errno_check (exp, target);
1888 /* Output the entire sequence. */
1889 insns = get_insns ();
1895 /* If we were unable to expand via the builtin, stop the sequence
1896 (without outputting the insns) and call to the library function
1897 with the stabilized argument list. */
1901 before_call = get_last_insn ();
1903 target = expand_call (exp, target, target == const0_rtx);
1905 /* If this is a sqrt operation and we don't care about errno, try to
1906 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1907 This allows the semantics of the libcall to be visible to the RTL
1909 if (builtin_optab == sqrt_optab && !errno_set)
1911 /* Search backwards through the insns emitted by expand_call looking
1912 for the instruction with the REG_RETVAL note. */
1913 rtx last = get_last_insn ();
1914 while (last != before_call)
1916 if (find_reg_note (last, REG_RETVAL, NULL))
1918 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1919 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1920 two elements, i.e. symbol_ref(sqrt) and the operand. */
1922 && GET_CODE (note) == EXPR_LIST
1923 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1924 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1925 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1927 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1928 /* Check operand is a register with expected mode. */
1931 && GET_MODE (operand) == mode)
1933 /* Replace the REG_EQUAL note with a SQRT rtx. */
1934 rtx equiv = gen_rtx_SQRT (mode, operand);
1935 set_unique_reg_note (last, REG_EQUAL, equiv);
1940 last = PREV_INSN (last);
1947 /* Expand a call to the builtin binary math functions (pow and atan2).
1948 Return 0 if a normal call should be emitted rather than expanding the
1949 function in-line. EXP is the expression that is a call to the builtin
1950 function; if convenient, the result should be placed in TARGET.
1951 SUBTARGET may be used as the target for computing one of EXP's
1955 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1957 optab builtin_optab;
1958 rtx op0, op1, insns;
1959 int op1_type = REAL_TYPE;
1960 tree fndecl = get_callee_fndecl (exp);
1961 tree arglist = TREE_OPERAND (exp, 1);
1962 tree arg0, arg1, temp, narg;
1963 enum machine_mode mode;
1964 bool errno_set = true;
1967 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1968 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1969 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1970 op1_type = INTEGER_TYPE;
1972 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1975 arg0 = TREE_VALUE (arglist);
1976 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1978 switch (DECL_FUNCTION_CODE (fndecl))
1980 CASE_FLT_FN (BUILT_IN_POW):
1981 builtin_optab = pow_optab; break;
1982 CASE_FLT_FN (BUILT_IN_ATAN2):
1983 builtin_optab = atan2_optab; break;
1984 CASE_FLT_FN (BUILT_IN_LDEXP):
1985 builtin_optab = ldexp_optab; break;
1986 CASE_FLT_FN (BUILT_IN_FMOD):
1987 builtin_optab = fmod_optab; break;
1988 CASE_FLT_FN (BUILT_IN_REMAINDER):
1989 CASE_FLT_FN (BUILT_IN_DREM):
1990 builtin_optab = remainder_optab; break;
1995 /* Make a suitable register to place result in. */
1996 mode = TYPE_MODE (TREE_TYPE (exp));
1998 /* Before working hard, check whether the instruction is available. */
1999 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2002 target = gen_reg_rtx (mode);
2004 if (! flag_errno_math || ! HONOR_NANS (mode))
2007 /* Always stabilize the argument list. */
2008 narg = builtin_save_expr (arg1);
2012 temp = build_tree_list (NULL_TREE, narg);
2016 temp = TREE_CHAIN (arglist);
2018 narg = builtin_save_expr (arg0);
2022 arglist = tree_cons (NULL_TREE, narg, temp);
2026 arglist = tree_cons (NULL_TREE, arg0, temp);
2029 exp = build_function_call_expr (fndecl, arglist);
2031 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2032 op1 = expand_normal (arg1);
2036 /* Compute into TARGET.
2037 Set TARGET to wherever the result comes back. */
2038 target = expand_binop (mode, builtin_optab, op0, op1,
2039 target, 0, OPTAB_DIRECT);
2041 /* If we were unable to expand via the builtin, stop the sequence
2042 (without outputting the insns) and call to the library function
2043 with the stabilized argument list. */
2047 return expand_call (exp, target, target == const0_rtx);
2051 expand_errno_check (exp, target);
2053 /* Output the entire sequence. */
2054 insns = get_insns ();
2061 /* Expand a call to the builtin sin and cos math functions.
2062 Return 0 if a normal call should be emitted rather than expanding the
2063 function in-line. EXP is the expression that is a call to the builtin
2064 function; if convenient, the result should be placed in TARGET.
2065 SUBTARGET may be used as the target for computing one of EXP's
2069 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2071 optab builtin_optab;
2073 tree fndecl = get_callee_fndecl (exp);
2074 tree arglist = TREE_OPERAND (exp, 1);
2075 enum machine_mode mode;
2078 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2081 arg = TREE_VALUE (arglist);
2083 switch (DECL_FUNCTION_CODE (fndecl))
2085 CASE_FLT_FN (BUILT_IN_SIN):
2086 CASE_FLT_FN (BUILT_IN_COS):
2087 builtin_optab = sincos_optab; break;
2092 /* Make a suitable register to place result in. */
2093 mode = TYPE_MODE (TREE_TYPE (exp));
2095 /* Check if sincos insn is available, otherwise fallback
2096 to sin or cos insn. */
2097 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2098 switch (DECL_FUNCTION_CODE (fndecl))
2100 CASE_FLT_FN (BUILT_IN_SIN):
2101 builtin_optab = sin_optab; break;
2102 CASE_FLT_FN (BUILT_IN_COS):
2103 builtin_optab = cos_optab; break;
2108 /* Before working hard, check whether the instruction is available. */
2109 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2111 target = gen_reg_rtx (mode);
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 narg = save_expr (arg);
2120 arglist = build_tree_list (NULL_TREE, arg);
2121 exp = build_function_call_expr (fndecl, arglist);
2124 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2128 /* Compute into TARGET.
2129 Set TARGET to wherever the result comes back. */
2130 if (builtin_optab == sincos_optab)
2134 switch (DECL_FUNCTION_CODE (fndecl))
2136 CASE_FLT_FN (BUILT_IN_SIN):
2137 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2139 CASE_FLT_FN (BUILT_IN_COS):
2140 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2145 gcc_assert (result);
2149 target = expand_unop (mode, builtin_optab, op0, target, 0);
2154 /* Output the entire sequence. */
2155 insns = get_insns ();
2161 /* If we were unable to expand via the builtin, stop the sequence
2162 (without outputting the insns) and call to the library function
2163 with the stabilized argument list. */
2167 target = expand_call (exp, target, target == const0_rtx);
2172 /* Expand a call to the builtin sincos math function.
2173 Return 0 if a normal call should be emitted rather than expanding the
2174 function in-line. EXP is the expression that is a call to the builtin
2178 expand_builtin_sincos (tree exp)
2180 rtx op0, op1, op2, target1, target2;
2181 tree arglist = TREE_OPERAND (exp, 1);
2182 enum machine_mode mode;
2183 tree arg, sinp, cosp;
2186 if (!validate_arglist (arglist, REAL_TYPE,
2187 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2190 arg = TREE_VALUE (arglist);
2191 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2192 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2194 /* Make a suitable register to place result in. */
2195 mode = TYPE_MODE (TREE_TYPE (arg));
2197 /* Check if sincos insn is available, otherwise emit the call. */
2198 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2201 target1 = gen_reg_rtx (mode);
2202 target2 = gen_reg_rtx (mode);
2204 op0 = expand_normal (arg);
2205 op1 = expand_normal (build_fold_indirect_ref (sinp));
2206 op2 = expand_normal (build_fold_indirect_ref (cosp));
2208 /* Compute into target1 and target2.
2209 Set TARGET to wherever the result comes back. */
2210 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2211 gcc_assert (result);
2213 /* Move target1 and target2 to the memory locations indicated
2215 emit_move_insn (op1, target1);
2216 emit_move_insn (op2, target2);
2221 /* Expand a call to the internal cexpi builtin to the sincos math function.
2222 EXP is the expression that is a call to the builtin function; if convenient,
2223 the result should be placed in TARGET. SUBTARGET may be used as the target
2224 for computing one of EXP's operands. */
2227 expand_builtin_cexpi (tree exp, rtx target, rtx subtarget)
2229 tree fndecl = get_callee_fndecl (exp);
2230 tree arglist = TREE_OPERAND (exp, 1);
2231 enum machine_mode mode;
2235 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2238 arg = TREE_VALUE (arglist);
2239 type = TREE_TYPE (arg);
2240 mode = TYPE_MODE (TREE_TYPE (arg));
2242 /* Try expanding via a sincos optab, fall back to emitting a libcall
2243 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2244 is only generated from sincos, cexp or if we have either of them. */
2245 if (sincos_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2247 op1 = gen_reg_rtx (mode);
2248 op2 = gen_reg_rtx (mode);
2250 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2252 /* Compute into op1 and op2. */
2253 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2255 else if (TARGET_HAS_SINCOS)
2257 tree call, narglist, fn = NULL_TREE;
2261 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2262 fn = built_in_decls[BUILT_IN_SINCOSF];
2263 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2264 fn = built_in_decls[BUILT_IN_SINCOS];
2265 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2266 fn = built_in_decls[BUILT_IN_SINCOSL];
2268 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2269 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2270 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2271 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2272 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2273 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2275 narglist = build_tree_list (NULL_TREE, top2);
2276 narglist = tree_cons (NULL_TREE, top1, narglist);
2277 narglist = tree_cons (NULL_TREE, arg, narglist);
2279 /* Make sure not to fold the sincos call again. */
2280 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2281 expand_normal (build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
2282 call, narglist, NULL_TREE));
2286 tree call, fn = NULL_TREE, narg;
2287 tree ctype = build_complex_type (type);
2289 /* We can expand via the C99 cexp function. */
2290 gcc_assert (TARGET_C99_FUNCTIONS);
2292 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2293 fn = built_in_decls[BUILT_IN_CEXPF];
2294 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2295 fn = built_in_decls[BUILT_IN_CEXP];
2296 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2297 fn = built_in_decls[BUILT_IN_CEXPL];
2298 narg = fold_build2 (COMPLEX_EXPR, ctype,
2299 build_real (type, dconst0), arg);
2301 /* Make sure not to fold the cexp call again. */
2302 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2303 return expand_expr (build3 (CALL_EXPR, ctype, call,
2304 build_tree_list (NULL_TREE, narg),
2305 NULL_TREE), target, VOIDmode, 0);
2308 /* Now build the proper return type. */
2309 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2310 make_tree (TREE_TYPE (arg), op2),
2311 make_tree (TREE_TYPE (arg), op1)),
2312 target, VOIDmode, 0);
2315 /* Expand a call to one of the builtin rounding functions gcc defines
2316 as an extension (lfloor and lceil). As these are gcc extensions we
2317 do not need to worry about setting errno to EDOM.
2318 If expanding via optab fails, lower expression to (int)(floor(x)).
2319 EXP is the expression that is a call to the builtin function;
2320 if convenient, the result should be placed in TARGET. SUBTARGET may
2321 be used as the target for computing one of EXP's operands. */
2324 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2326 convert_optab builtin_optab;
2327 rtx op0, insns, tmp;
2328 tree fndecl = get_callee_fndecl (exp);
2329 tree arglist = TREE_OPERAND (exp, 1);
2330 enum built_in_function fallback_fn;
2331 tree fallback_fndecl;
2332 enum machine_mode mode;
2335 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2338 arg = TREE_VALUE (arglist);
2340 switch (DECL_FUNCTION_CODE (fndecl))
2342 CASE_FLT_FN (BUILT_IN_LCEIL):
2343 CASE_FLT_FN (BUILT_IN_LLCEIL):
2344 builtin_optab = lceil_optab;
2345 fallback_fn = BUILT_IN_CEIL;
2348 CASE_FLT_FN (BUILT_IN_LFLOOR):
2349 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2350 builtin_optab = lfloor_optab;
2351 fallback_fn = BUILT_IN_FLOOR;
2358 /* Make a suitable register to place result in. */
2359 mode = TYPE_MODE (TREE_TYPE (exp));
2361 target = gen_reg_rtx (mode);
2363 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2364 need to expand the argument again. This way, we will not perform
2365 side-effects more the once. */
2366 narg = builtin_save_expr (arg);
2370 arglist = build_tree_list (NULL_TREE, arg);
2371 exp = build_function_call_expr (fndecl, arglist);
2374 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2378 /* Compute into TARGET. */
2379 if (expand_sfix_optab (target, op0, builtin_optab))
2381 /* Output the entire sequence. */
2382 insns = get_insns ();
2388 /* If we were unable to expand via the builtin, stop the sequence
2389 (without outputting the insns). */
2392 /* Fall back to floating point rounding optab. */
2393 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2394 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2395 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2396 gcc_assert (fallback_fndecl != NULL_TREE);
2397 exp = build_function_call_expr (fallback_fndecl, arglist);
2399 tmp = expand_normal (exp);
2401 /* Truncate the result of floating point optab to integer
2402 via expand_fix (). */
2403 target = gen_reg_rtx (mode);
2404 expand_fix (target, tmp, 0);
2409 /* Expand a call to one of the builtin math functions doing integer
2411 Return 0 if a normal call should be emitted rather than expanding the
2412 function in-line. EXP is the expression that is a call to the builtin
2413 function; if convenient, the result should be placed in TARGET.
2414 SUBTARGET may be used as the target for computing one of EXP's operands. */
2417 expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget)
2419 convert_optab builtin_optab;
2421 tree fndecl = get_callee_fndecl (exp);
2422 tree arglist = TREE_OPERAND (exp, 1);
2423 enum machine_mode mode;
2426 /* There's no easy way to detect the case we need to set EDOM. */
2427 if (flag_errno_math)
2430 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2433 arg = TREE_VALUE (arglist);
2435 switch (DECL_FUNCTION_CODE (fndecl))
2437 CASE_FLT_FN (BUILT_IN_LRINT):
2438 CASE_FLT_FN (BUILT_IN_LLRINT):
2439 builtin_optab = lrint_optab; break;
2440 CASE_FLT_FN (BUILT_IN_LROUND):
2441 CASE_FLT_FN (BUILT_IN_LLROUND):
2442 builtin_optab = lround_optab; break;
2447 /* Make a suitable register to place result in. */
2448 mode = TYPE_MODE (TREE_TYPE (exp));
2450 target = gen_reg_rtx (mode);
2452 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2453 need to expand the argument again. This way, we will not perform
2454 side-effects more the once. */
2455 narg = builtin_save_expr (arg);
2459 arglist = build_tree_list (NULL_TREE, arg);
2460 exp = build_function_call_expr (fndecl, arglist);
2463 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2467 if (expand_sfix_optab (target, op0, builtin_optab))
2469 /* Output the entire sequence. */
2470 insns = get_insns ();
2476 /* If we were unable to expand via the builtin, stop the sequence
2477 (without outputting the insns) and call to the library function
2478 with the stabilized argument list. */
2481 target = expand_call (exp, target, target == const0_rtx);
2486 /* To evaluate powi(x,n), the floating point value x raised to the
2487 constant integer exponent n, we use a hybrid algorithm that
2488 combines the "window method" with look-up tables. For an
2489 introduction to exponentiation algorithms and "addition chains",
2490 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2491 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2492 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2493 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2495 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2496 multiplications to inline before calling the system library's pow
2497 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2498 so this default never requires calling pow, powf or powl. */
2500 #ifndef POWI_MAX_MULTS
2501 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2504 /* The size of the "optimal power tree" lookup table. All
2505 exponents less than this value are simply looked up in the
2506 powi_table below. This threshold is also used to size the
2507 cache of pseudo registers that hold intermediate results. */
2508 #define POWI_TABLE_SIZE 256
2510 /* The size, in bits of the window, used in the "window method"
2511 exponentiation algorithm. This is equivalent to a radix of
2512 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2513 #define POWI_WINDOW_SIZE 3
2515 /* The following table is an efficient representation of an
2516 "optimal power tree". For each value, i, the corresponding
2517 value, j, in the table states than an optimal evaluation
2518 sequence for calculating pow(x,i) can be found by evaluating
2519 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2520 100 integers is given in Knuth's "Seminumerical algorithms". */
2522 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2524 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2525 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2526 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2527 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2528 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2529 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2530 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2531 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2532 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2533 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2534 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2535 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2536 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2537 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2538 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2539 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2540 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2541 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2542 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2543 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2544 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2545 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2546 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2547 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2548 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2549 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2550 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2551 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2552 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2553 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2554 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2555 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2559 /* Return the number of multiplications required to calculate
2560 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2561 subroutine of powi_cost. CACHE is an array indicating
2562 which exponents have already been calculated. */
2565 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2567 /* If we've already calculated this exponent, then this evaluation
2568 doesn't require any additional multiplications. */
2573 return powi_lookup_cost (n - powi_table[n], cache)
2574 + powi_lookup_cost (powi_table[n], cache) + 1;
2577 /* Return the number of multiplications required to calculate
2578 powi(x,n) for an arbitrary x, given the exponent N. This
2579 function needs to be kept in sync with expand_powi below. */
2582 powi_cost (HOST_WIDE_INT n)
2584 bool cache[POWI_TABLE_SIZE];
2585 unsigned HOST_WIDE_INT digit;
2586 unsigned HOST_WIDE_INT val;
2592 /* Ignore the reciprocal when calculating the cost. */
2593 val = (n < 0) ? -n : n;
2595 /* Initialize the exponent cache. */
2596 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2601 while (val >= POWI_TABLE_SIZE)
2605 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2606 result += powi_lookup_cost (digit, cache)
2607 + POWI_WINDOW_SIZE + 1;
2608 val >>= POWI_WINDOW_SIZE;
2617 return result + powi_lookup_cost (val, cache);
2620 /* Recursive subroutine of expand_powi. This function takes the array,
2621 CACHE, of already calculated exponents and an exponent N and returns
2622 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2625 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2627 unsigned HOST_WIDE_INT digit;
2631 if (n < POWI_TABLE_SIZE)
2636 target = gen_reg_rtx (mode);
2639 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2640 op1 = expand_powi_1 (mode, powi_table[n], cache);
2644 target = gen_reg_rtx (mode);
2645 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2646 op0 = expand_powi_1 (mode, n - digit, cache);
2647 op1 = expand_powi_1 (mode, digit, cache);
2651 target = gen_reg_rtx (mode);
2652 op0 = expand_powi_1 (mode, n >> 1, cache);
2656 result = expand_mult (mode, op0, op1, target, 0);
2657 if (result != target)
2658 emit_move_insn (target, result);
2662 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2663 floating point operand in mode MODE, and N is the exponent. This
2664 function needs to be kept in sync with powi_cost above. */
2667 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2669 unsigned HOST_WIDE_INT val;
2670 rtx cache[POWI_TABLE_SIZE];
2674 return CONST1_RTX (mode);
2676 val = (n < 0) ? -n : n;
2678 memset (cache, 0, sizeof (cache));
2681 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2683 /* If the original exponent was negative, reciprocate the result. */
2685 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2686 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2691 /* Expand a call to the pow built-in mathematical function. Return 0 if
2692 a normal call should be emitted rather than expanding the function
2693 in-line. EXP is the expression that is a call to the builtin
2694 function; if convenient, the result should be placed in TARGET. */
2697 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2699 tree arg0, arg1, fn, narg0, narglist;
2700 tree arglist = TREE_OPERAND (exp, 1);
2701 tree type = TREE_TYPE (exp);
2702 REAL_VALUE_TYPE cint, c, c2;
2705 enum machine_mode mode = TYPE_MODE (type);
2707 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2710 arg0 = TREE_VALUE (arglist);
2711 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2713 if (TREE_CODE (arg1) != REAL_CST
2714 || TREE_OVERFLOW (arg1))
2715 return expand_builtin_mathfn_2 (exp, target, subtarget);
2717 /* Handle constant exponents. */
2719 /* For integer valued exponents we can expand to an optimal multiplication
2720 sequence using expand_powi. */
2721 c = TREE_REAL_CST (arg1);
2722 n = real_to_integer (&c);
2723 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2724 if (real_identical (&c, &cint)
2725 && ((n >= -1 && n <= 2)
2726 || (flag_unsafe_math_optimizations
2728 && powi_cost (n) <= POWI_MAX_MULTS)))
2730 op = expand_expr (arg0, subtarget, VOIDmode, 0);
2733 op = force_reg (mode, op);
2734 op = expand_powi (op, mode, n);
2739 narg0 = builtin_save_expr (arg0);
2740 narglist = build_tree_list (NULL_TREE, narg0);
2742 /* If the exponent is not integer valued, check if it is half of an integer.
2743 In this case we can expand to sqrt (x) * x**(n/2). */
2744 fn = mathfn_built_in (type, BUILT_IN_SQRT);
2745 if (fn != NULL_TREE)
2747 real_arithmetic (&c2, MULT_EXPR, &c, &dconst2);
2748 n = real_to_integer (&c2);
2749 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2750 if (real_identical (&c2, &cint)
2751 && ((flag_unsafe_math_optimizations
2753 && powi_cost (n/2) <= POWI_MAX_MULTS)
2756 tree call_expr = build_function_call_expr (fn, narglist);
2757 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2760 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2761 op2 = force_reg (mode, op2);
2762 op2 = expand_powi (op2, mode, abs (n / 2));
2763 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2764 0, OPTAB_LIB_WIDEN);
2765 /* If the original exponent was negative, reciprocate the
2768 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2769 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2775 /* Try if the exponent is a third of an integer. In this case
2776 we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is
2777 different from pow (x, 1./3.) due to rounding and behavior
2778 with negative x we need to constrain this transformation to
2779 unsafe math and positive x or finite math. */
2780 fn = mathfn_built_in (type, BUILT_IN_CBRT);
2782 && flag_unsafe_math_optimizations
2783 && (tree_expr_nonnegative_p (arg0)
2784 || !HONOR_NANS (mode)))
2786 real_arithmetic (&c2, MULT_EXPR, &c, &dconst3);
2787 real_round (&c2, mode, &c2);
2788 n = real_to_integer (&c2);
2789 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2790 real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3);
2791 real_convert (&c2, mode, &c2);
2792 if (real_identical (&c2, &c)
2794 && powi_cost (n/3) <= POWI_MAX_MULTS)
2797 tree call_expr = build_function_call_expr (fn, narglist);
2798 op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0);
2799 if (abs (n) % 3 == 2)
2800 op = expand_simple_binop (mode, MULT, op, op, op,
2801 0, OPTAB_LIB_WIDEN);
2804 op2 = expand_expr (narg0, subtarget, VOIDmode, 0);
2805 op2 = force_reg (mode, op2);
2806 op2 = expand_powi (op2, mode, abs (n / 3));
2807 op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX,
2808 0, OPTAB_LIB_WIDEN);
2809 /* If the original exponent was negative, reciprocate the
2812 op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2813 op, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2819 /* Fall back to optab expansion. */
2820 return expand_builtin_mathfn_2 (exp, target, subtarget);
2823 /* Expand a call to the powi built-in mathematical function. Return 0 if
2824 a normal call should be emitted rather than expanding the function
2825 in-line. EXP is the expression that is a call to the builtin
2826 function; if convenient, the result should be placed in TARGET. */
2829 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2831 tree arglist = TREE_OPERAND (exp, 1);
2834 enum machine_mode mode;
2835 enum machine_mode mode2;
2837 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2840 arg0 = TREE_VALUE (arglist);
2841 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2842 mode = TYPE_MODE (TREE_TYPE (exp));
2844 /* Handle constant power. */
2846 if (TREE_CODE (arg1) == INTEGER_CST
2847 && !TREE_OVERFLOW (arg1))
2849 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2851 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2852 Otherwise, check the number of multiplications required. */
2853 if ((TREE_INT_CST_HIGH (arg1) == 0
2854 || TREE_INT_CST_HIGH (arg1) == -1)
2855 && ((n >= -1 && n <= 2)
2857 && powi_cost (n) <= POWI_MAX_MULTS)))
2859 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2860 op0 = force_reg (mode, op0);
2861 return expand_powi (op0, mode, n);
2865 /* Emit a libcall to libgcc. */
2867 /* Mode of the 2nd argument must match that of an int. */
2868 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2870 if (target == NULL_RTX)
2871 target = gen_reg_rtx (mode);
2873 op0 = expand_expr (arg0, subtarget, mode, 0);
2874 if (GET_MODE (op0) != mode)
2875 op0 = convert_to_mode (mode, op0, 0);
2876 op1 = expand_expr (arg1, 0, mode2, 0);
2877 if (GET_MODE (op1) != mode2)
2878 op1 = convert_to_mode (mode2, op1, 0);
2880 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2881 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2882 op0, mode, op1, mode2);
2887 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2888 if we failed the caller should emit a normal call, otherwise
2889 try to get the result in TARGET, if convenient. */
2892 expand_builtin_strlen (tree arglist, rtx target,
2893 enum machine_mode target_mode)
2895 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2900 tree len, src = TREE_VALUE (arglist);
2901 rtx result, src_reg, char_rtx, before_strlen;
2902 enum machine_mode insn_mode = target_mode, char_mode;
2903 enum insn_code icode = CODE_FOR_nothing;
2906 /* If the length can be computed at compile-time, return it. */
2907 len = c_strlen (src, 0);
2909 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2911 /* If the length can be computed at compile-time and is constant
2912 integer, but there are side-effects in src, evaluate
2913 src for side-effects, then return len.
2914 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2915 can be optimized into: i++; x = 3; */
2916 len = c_strlen (src, 1);
2917 if (len && TREE_CODE (len) == INTEGER_CST)
2919 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2920 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2923 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2925 /* If SRC is not a pointer type, don't do this operation inline. */
2929 /* Bail out if we can't compute strlen in the right mode. */
2930 while (insn_mode != VOIDmode)
2932 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2933 if (icode != CODE_FOR_nothing)
2936 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2938 if (insn_mode == VOIDmode)
2941 /* Make a place to write the result of the instruction. */
2945 && GET_MODE (result) == insn_mode
2946 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2947 result = gen_reg_rtx (insn_mode);
2949 /* Make a place to hold the source address. We will not expand
2950 the actual source until we are sure that the expansion will
2951 not fail -- there are trees that cannot be expanded twice. */
2952 src_reg = gen_reg_rtx (Pmode);
2954 /* Mark the beginning of the strlen sequence so we can emit the
2955 source operand later. */
2956 before_strlen = get_last_insn ();
2958 char_rtx = const0_rtx;
2959 char_mode = insn_data[(int) icode].operand[2].mode;
2960 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2962 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2964 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2965 char_rtx, GEN_INT (align));
2970 /* Now that we are assured of success, expand the source. */
2972 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2974 emit_move_insn (src_reg, pat);
2979 emit_insn_after (pat, before_strlen);
2981 emit_insn_before (pat, get_insns ());
2983 /* Return the value in the proper mode for this function. */
2984 if (GET_MODE (result) == target_mode)
2986 else if (target != 0)
2987 convert_move (target, result, 0);
2989 target = convert_to_mode (target_mode, result, 0);
2995 /* Expand a call to the strstr builtin. Return 0 if we failed the
2996 caller should emit a normal call, otherwise try to get the result
2997 in TARGET, if convenient (and in mode MODE if that's convenient). */
3000 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
3002 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3004 tree result = fold_builtin_strstr (arglist, type);
3006 return expand_expr (result, target, mode, EXPAND_NORMAL);
3011 /* Expand a call to the strchr builtin. Return 0 if we failed the
3012 caller should emit a normal call, otherwise try to get the result
3013 in TARGET, if convenient (and in mode MODE if that's convenient). */
3016 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3018 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3020 tree result = fold_builtin_strchr (arglist, type);
3022 return expand_expr (result, target, mode, EXPAND_NORMAL);
3024 /* FIXME: Should use strchrM optab so that ports can optimize this. */
3029 /* Expand a call to the strrchr builtin. Return 0 if we failed the
3030 caller should emit a normal call, otherwise try to get the result
3031 in TARGET, if convenient (and in mode MODE if that's convenient). */
3034 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
3036 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3038 tree result = fold_builtin_strrchr (arglist, type);
3040 return expand_expr (result, target, mode, EXPAND_NORMAL);
3045 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
3046 caller should emit a normal call, otherwise try to get the result
3047 in TARGET, if convenient (and in mode MODE if that's convenient). */
3050 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
3052 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3054 tree result = fold_builtin_strpbrk (arglist, type);
3056 return expand_expr (result, target, mode, EXPAND_NORMAL);
3061 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3062 bytes from constant string DATA + OFFSET and return it as target
3066 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3067 enum machine_mode mode)
3069 const char *str = (const char *) data;
3071 gcc_assert (offset >= 0
3072 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3073 <= strlen (str) + 1));
3075 return c_readstr (str + offset, mode);
3078 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
3079 Return 0 if we failed, the caller should emit a normal call,
3080 otherwise try to get the result in TARGET, if convenient (and in
3081 mode MODE if that's convenient). */
3083 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
3085 tree fndecl = get_callee_fndecl (exp);
3086 tree arglist = TREE_OPERAND (exp, 1);
3087 if (!validate_arglist (arglist,
3088 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3092 tree dest = TREE_VALUE (arglist);
3093 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3094 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3095 const char *src_str;
3096 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3097 unsigned int dest_align
3098 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3099 rtx dest_mem, src_mem, dest_addr, len_rtx;
3100 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
3105 while (TREE_CODE (result) == COMPOUND_EXPR)
3107 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3109 result = TREE_OPERAND (result, 1);
3111 return expand_expr (result, target, mode, EXPAND_NORMAL);
3114 /* If DEST is not a pointer type, call the normal function. */
3115 if (dest_align == 0)
3118 /* If either SRC is not a pointer type, don't do this
3119 operation in-line. */
3123 dest_mem = get_memory_rtx (dest, len);
3124 set_mem_align (dest_mem, dest_align);
3125 len_rtx = expand_normal (len);
3126 src_str = c_getstr (src);
3128 /* If SRC is a string constant and block move would be done
3129 by pieces, we can avoid loading the string from memory
3130 and only stored the computed constants. */
3132 && GET_CODE (len_rtx) == CONST_INT
3133 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3134 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3135 (void *) src_str, dest_align))
3137 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3138 builtin_memcpy_read_str,
3139 (void *) src_str, dest_align, 0);
3140 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3141 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3145 src_mem = get_memory_rtx (src, len);
3146 set_mem_align (src_mem, src_align);
3148 /* Copy word part most expediently. */
3149 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
3150 CALL_EXPR_TAILCALL (exp)
3151 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3155 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3156 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3162 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
3163 Return 0 if we failed; the caller should emit a normal call,
3164 otherwise try to get the result in TARGET, if convenient (and in
3165 mode MODE if that's convenient). If ENDP is 0 return the
3166 destination pointer, if ENDP is 1 return the end pointer ala
3167 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3171 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
3174 if (!validate_arglist (arglist,
3175 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3177 /* If return value is ignored, transform mempcpy into memcpy. */
3178 else if (target == const0_rtx)
3180 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3185 return expand_expr (build_function_call_expr (fn, arglist),
3186 target, mode, EXPAND_NORMAL);
3190 tree dest = TREE_VALUE (arglist);
3191 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3192 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3193 const char *src_str;
3194 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3195 unsigned int dest_align
3196 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3197 rtx dest_mem, src_mem, len_rtx;
3198 tree result = fold_builtin_memory_op (arglist, type, false, endp);
3202 while (TREE_CODE (result) == COMPOUND_EXPR)
3204 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3206 result = TREE_OPERAND (result, 1);
3208 return expand_expr (result, target, mode, EXPAND_NORMAL);
3211 /* If either SRC or DEST is not a pointer type, don't do this
3212 operation in-line. */
3213 if (dest_align == 0 || src_align == 0)
3216 /* If LEN is not constant, call the normal function. */
3217 if (! host_integerp (len, 1))
3220 len_rtx = expand_normal (len);
3221 src_str = c_getstr (src);
3223 /* If SRC is a string constant and block move would be done
3224 by pieces, we can avoid loading the string from memory
3225 and only stored the computed constants. */
3227 && GET_CODE (len_rtx) == CONST_INT
3228 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3229 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3230 (void *) src_str, dest_align))
3232 dest_mem = get_memory_rtx (dest, len);
3233 set_mem_align (dest_mem, dest_align);
3234 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3235 builtin_memcpy_read_str,
3236 (void *) src_str, dest_align, endp);
3237 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3238 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3242 if (GET_CODE (len_rtx) == CONST_INT
3243 && can_move_by_pieces (INTVAL (len_rtx),
3244 MIN (dest_align, src_align)))
3246 dest_mem = get_memory_rtx (dest, len);
3247 set_mem_align (dest_mem, dest_align);
3248 src_mem = get_memory_rtx (src, len);
3249 set_mem_align (src_mem, src_align);
3250 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3251 MIN (dest_align, src_align), endp);
3252 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3253 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3261 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3262 if we failed; the caller should emit a normal call. */
3265 expand_builtin_memmove (tree arglist, tree type, rtx target,
3266 enum machine_mode mode)
3268 if (!validate_arglist (arglist,
3269 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3273 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3277 while (TREE_CODE (result) == COMPOUND_EXPR)
3279 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3281 result = TREE_OPERAND (result, 1);
3283 return expand_expr (result, target, mode, EXPAND_NORMAL);
3286 /* Otherwise, call the normal function. */
3291 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3292 if we failed the caller should emit a normal call. */
3295 expand_builtin_bcopy (tree exp)
3297 tree arglist = TREE_OPERAND (exp, 1);
3298 tree type = TREE_TYPE (exp);
3299 tree src, dest, size, newarglist;
3301 if (!validate_arglist (arglist,
3302 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3305 src = TREE_VALUE (arglist);
3306 dest = TREE_VALUE (TREE_CHAIN (arglist));
3307 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3309 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3310 memmove(ptr y, ptr x, size_t z). This is done this way
3311 so that if it isn't expanded inline, we fallback to
3312 calling bcopy instead of memmove. */
3314 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3315 newarglist = tree_cons (NULL_TREE, src, newarglist);
3316 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3318 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
3322 # define HAVE_movstr 0
3323 # define CODE_FOR_movstr CODE_FOR_nothing
3326 /* Expand into a movstr instruction, if one is available. Return 0 if
3327 we failed, the caller should emit a normal call, otherwise try to
3328 get the result in TARGET, if convenient. If ENDP is 0 return the
3329 destination pointer, if ENDP is 1 return the end pointer ala
3330 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3334 expand_movstr (tree dest, tree src, rtx target, int endp)
3340 const struct insn_data * data;
3345 dest_mem = get_memory_rtx (dest, NULL);
3346 src_mem = get_memory_rtx (src, NULL);
3349 target = force_reg (Pmode, XEXP (dest_mem, 0));
3350 dest_mem = replace_equiv_address (dest_mem, target);
3351 end = gen_reg_rtx (Pmode);
3355 if (target == 0 || target == const0_rtx)
3357 end = gen_reg_rtx (Pmode);
3365 data = insn_data + CODE_FOR_movstr;
3367 if (data->operand[0].mode != VOIDmode)
3368 end = gen_lowpart (data->operand[0].mode, end);
3370 insn = data->genfun (end, dest_mem, src_mem);
3376 /* movstr is supposed to set end to the address of the NUL
3377 terminator. If the caller requested a mempcpy-like return value,
3379 if (endp == 1 && target != const0_rtx)
3381 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3382 emit_move_insn (target, force_operand (tem, NULL_RTX));
3388 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3389 if we failed the caller should emit a normal call, otherwise try to get
3390 the result in TARGET, if convenient (and in mode MODE if that's
3394 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3396 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3398 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3401 while (TREE_CODE (result) == COMPOUND_EXPR)
3403 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3405 result = TREE_OPERAND (result, 1);
3407 return expand_expr (result, target, mode, EXPAND_NORMAL);
3410 return expand_movstr (TREE_VALUE (arglist),
3411 TREE_VALUE (TREE_CHAIN (arglist)),
3412 target, /*endp=*/0);
3417 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3418 Return 0 if we failed the caller should emit a normal call,
3419 otherwise try to get the result in TARGET, if convenient (and in
3420 mode MODE if that's convenient). */
3423 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3425 tree arglist = TREE_OPERAND (exp, 1);
3426 /* If return value is ignored, transform stpcpy into strcpy. */
3427 if (target == const0_rtx)
3429 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3433 return expand_expr (build_function_call_expr (fn, arglist),
3434 target, mode, EXPAND_NORMAL);
3437 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3441 tree dst, src, len, lenp1;
3445 /* Ensure we get an actual string whose length can be evaluated at
3446 compile-time, not an expression containing a string. This is
3447 because the latter will potentially produce pessimized code
3448 when used to produce the return value. */
3449 src = TREE_VALUE (TREE_CHAIN (arglist));
3450 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3451 return expand_movstr (TREE_VALUE (arglist),
3452 TREE_VALUE (TREE_CHAIN (arglist)),
3453 target, /*endp=*/2);
3455 dst = TREE_VALUE (arglist);
3456 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3457 narglist = build_tree_list (NULL_TREE, lenp1);
3458 narglist = tree_cons (NULL_TREE, src, narglist);
3459 narglist = tree_cons (NULL_TREE, dst, narglist);
3460 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3461 target, mode, /*endp=*/2);
3466 if (TREE_CODE (len) == INTEGER_CST)
3468 rtx len_rtx = expand_normal (len);
3470 if (GET_CODE (len_rtx) == CONST_INT)
3472 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3473 arglist, target, mode);
3479 if (mode != VOIDmode)
3480 target = gen_reg_rtx (mode);
3482 target = gen_reg_rtx (GET_MODE (ret));
3484 if (GET_MODE (target) != GET_MODE (ret))
3485 ret = gen_lowpart (GET_MODE (target), ret);
3487 ret = plus_constant (ret, INTVAL (len_rtx));
3488 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3496 return expand_movstr (TREE_VALUE (arglist),
3497 TREE_VALUE (TREE_CHAIN (arglist)),
3498 target, /*endp=*/2);
3502 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3503 bytes from constant string DATA + OFFSET and return it as target
3507 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3508 enum machine_mode mode)
3510 const char *str = (const char *) data;
3512 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3515 return c_readstr (str + offset, mode);
3518 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3519 if we failed the caller should emit a normal call. */
3522 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3524 tree fndecl = get_callee_fndecl (exp);
3525 tree arglist = TREE_OPERAND (exp, 1);
3526 if (validate_arglist (arglist,
3527 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3529 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3530 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3531 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3535 while (TREE_CODE (result) == COMPOUND_EXPR)
3537 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3539 result = TREE_OPERAND (result, 1);
3541 return expand_expr (result, target, mode, EXPAND_NORMAL);
3544 /* We must be passed a constant len and src parameter. */
3545 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3548 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3550 /* We're required to pad with trailing zeros if the requested
3551 len is greater than strlen(s2)+1. In that case try to
3552 use store_by_pieces, if it fails, punt. */
3553 if (tree_int_cst_lt (slen, len))
3555 tree dest = TREE_VALUE (arglist);
3556 unsigned int dest_align
3557 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3558 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3561 if (!p || dest_align == 0 || !host_integerp (len, 1)
3562 || !can_store_by_pieces (tree_low_cst (len, 1),
3563 builtin_strncpy_read_str,
3564 (void *) p, dest_align))
3567 dest_mem = get_memory_rtx (dest, len);
3568 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3569 builtin_strncpy_read_str,
3570 (void *) p, dest_align, 0);
3571 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3572 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3579 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3580 bytes from constant string DATA + OFFSET and return it as target
3584 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3585 enum machine_mode mode)
3587 const char *c = (const char *) data;
3588 char *p = alloca (GET_MODE_SIZE (mode));
3590 memset (p, *c, GET_MODE_SIZE (mode));
3592 return c_readstr (p, mode);
3595 /* Callback routine for store_by_pieces. Return the RTL of a register
3596 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3597 char value given in the RTL register data. For example, if mode is
3598 4 bytes wide, return the RTL for 0x01010101*data. */
3601 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3602 enum machine_mode mode)
3608 size = GET_MODE_SIZE (mode);
3613 memset (p, 1, size);
3614 coeff = c_readstr (p, mode);
3616 target = convert_to_mode (mode, (rtx) data, 1);
3617 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3618 return force_reg (mode, target);
3621 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3622 if we failed the caller should emit a normal call, otherwise try to get
3623 the result in TARGET, if convenient (and in mode MODE if that's
3627 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3630 if (!validate_arglist (arglist,
3631 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3635 tree dest = TREE_VALUE (arglist);
3636 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3637 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3639 enum built_in_function fcode;
3641 unsigned int dest_align;
3642 rtx dest_mem, dest_addr, len_rtx;
3644 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3646 /* If DEST is not a pointer type, don't do this
3647 operation in-line. */
3648 if (dest_align == 0)
3651 /* If the LEN parameter is zero, return DEST. */
3652 if (integer_zerop (len))
3654 /* Evaluate and ignore VAL in case it has side-effects. */
3655 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3656 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3659 /* Stabilize the arguments in case we fail. */
3660 dest = builtin_save_expr (dest);
3661 val = builtin_save_expr (val);
3662 len = builtin_save_expr (len);
3664 len_rtx = expand_normal (len);
3665 dest_mem = get_memory_rtx (dest, len);
3667 if (TREE_CODE (val) != INTEGER_CST)
3671 val_rtx = expand_normal (val);
3672 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3675 /* Assume that we can memset by pieces if we can store the
3676 * the coefficients by pieces (in the required modes).
3677 * We can't pass builtin_memset_gen_str as that emits RTL. */
3679 if (host_integerp (len, 1)
3680 && !(optimize_size && tree_low_cst (len, 1) > 1)
3681 && can_store_by_pieces (tree_low_cst (len, 1),
3682 builtin_memset_read_str, &c, dest_align))
3684 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3686 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3687 builtin_memset_gen_str, val_rtx, dest_align, 0);
3689 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3693 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3694 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3698 if (target_char_cast (val, &c))
3703 if (host_integerp (len, 1)
3704 && !(optimize_size && tree_low_cst (len, 1) > 1)
3705 && can_store_by_pieces (tree_low_cst (len, 1),
3706 builtin_memset_read_str, &c, dest_align))
3707 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3708 builtin_memset_read_str, &c, dest_align, 0);
3709 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3713 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3714 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3718 set_mem_align (dest_mem, dest_align);
3719 dest_addr = clear_storage (dest_mem, len_rtx,
3720 CALL_EXPR_TAILCALL (orig_exp)
3721 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3725 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3726 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3732 fndecl = get_callee_fndecl (orig_exp);
3733 fcode = DECL_FUNCTION_CODE (fndecl);
3734 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3735 arglist = build_tree_list (NULL_TREE, len);
3736 if (fcode == BUILT_IN_MEMSET)
3737 arglist = tree_cons (NULL_TREE, val, arglist);
3738 arglist = tree_cons (NULL_TREE, dest, arglist);
3739 fn = build_function_call_expr (fndecl, arglist);
3740 if (TREE_CODE (fn) == CALL_EXPR)
3741 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3742 return expand_call (fn, target, target == const0_rtx);
3746 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3747 if we failed the caller should emit a normal call. */
3750 expand_builtin_bzero (tree exp)
3752 tree arglist = TREE_OPERAND (exp, 1);
3753 tree dest, size, newarglist;
3755 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3758 dest = TREE_VALUE (arglist);
3759 size = TREE_VALUE (TREE_CHAIN (arglist));
3761 /* New argument list transforming bzero(ptr x, int y) to
3762 memset(ptr x, int 0, size_t y). This is done this way
3763 so that if it isn't expanded inline, we fallback to
3764 calling bzero instead of memset. */
3766 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3767 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3768 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3770 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3773 /* Expand expression EXP, which is a call to the memcmp built-in function.
3774 ARGLIST is the argument list for this call. Return 0 if we failed and the
3775 caller should emit a normal call, otherwise try to get the result in
3776 TARGET, if convenient (and in mode MODE, if that's convenient). */
3779 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3780 enum machine_mode mode)
3782 if (!validate_arglist (arglist,
3783 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3787 tree result = fold_builtin_memcmp (arglist);
3789 return expand_expr (result, target, mode, EXPAND_NORMAL);
3792 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3794 tree arg1 = TREE_VALUE (arglist);
3795 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3796 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3797 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3802 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3804 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3805 enum machine_mode insn_mode;
3807 #ifdef HAVE_cmpmemsi
3809 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3812 #ifdef HAVE_cmpstrnsi
3814 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3819 /* If we don't have POINTER_TYPE, call the function. */
3820 if (arg1_align == 0 || arg2_align == 0)
3823 /* Make a place to write the result of the instruction. */
3826 && REG_P (result) && GET_MODE (result) == insn_mode
3827 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3828 result = gen_reg_rtx (insn_mode);
3830 arg1_rtx = get_memory_rtx (arg1, len);
3831 arg2_rtx = get_memory_rtx (arg2, len);
3832 arg3_rtx = expand_normal (len);
3834 /* Set MEM_SIZE as appropriate. */
3835 if (GET_CODE (arg3_rtx) == CONST_INT)
3837 set_mem_size (arg1_rtx, arg3_rtx);
3838 set_mem_size (arg2_rtx, arg3_rtx);
3841 #ifdef HAVE_cmpmemsi
3843 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3844 GEN_INT (MIN (arg1_align, arg2_align)));
3847 #ifdef HAVE_cmpstrnsi
3849 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3850 GEN_INT (MIN (arg1_align, arg2_align)));
3858 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3859 TYPE_MODE (integer_type_node), 3,
3860 XEXP (arg1_rtx, 0), Pmode,
3861 XEXP (arg2_rtx, 0), Pmode,
3862 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3863 TYPE_UNSIGNED (sizetype)),
3864 TYPE_MODE (sizetype));
3866 /* Return the value in the proper mode for this function. */
3867 mode = TYPE_MODE (TREE_TYPE (exp));
3868 if (GET_MODE (result) == mode)
3870 else if (target != 0)
3872 convert_move (target, result, 0);
3876 return convert_to_mode (mode, result, 0);
3883 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3884 if we failed the caller should emit a normal call, otherwise try to get
3885 the result in TARGET, if convenient. */
3888 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3890 tree arglist = TREE_OPERAND (exp, 1);
3892 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3896 tree result = fold_builtin_strcmp (arglist);
3898 return expand_expr (result, target, mode, EXPAND_NORMAL);
3901 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3902 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3903 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3905 rtx arg1_rtx, arg2_rtx;
3906 rtx result, insn = NULL_RTX;
3909 tree arg1 = TREE_VALUE (arglist);
3910 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3912 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3914 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3916 /* If we don't have POINTER_TYPE, call the function. */
3917 if (arg1_align == 0 || arg2_align == 0)
3920 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3921 arg1 = builtin_save_expr (arg1);
3922 arg2 = builtin_save_expr (arg2);
3924 arg1_rtx = get_memory_rtx (arg1, NULL);
3925 arg2_rtx = get_memory_rtx (arg2, NULL);
3927 #ifdef HAVE_cmpstrsi
3928 /* Try to call cmpstrsi. */
3931 enum machine_mode insn_mode
3932 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3934 /* Make a place to write the result of the instruction. */
3937 && REG_P (result) && GET_MODE (result) == insn_mode
3938 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3939 result = gen_reg_rtx (insn_mode);
3941 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3942 GEN_INT (MIN (arg1_align, arg2_align)));
3945 #ifdef HAVE_cmpstrnsi
3946 /* Try to determine at least one length and call cmpstrnsi. */
3947 if (!insn && HAVE_cmpstrnsi)
3952 enum machine_mode insn_mode
3953 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3954 tree len1 = c_strlen (arg1, 1);
3955 tree len2 = c_strlen (arg2, 1);
3958 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3960 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3962 /* If we don't have a constant length for the first, use the length
3963 of the second, if we know it. We don't require a constant for
3964 this case; some cost analysis could be done if both are available
3965 but neither is constant. For now, assume they're equally cheap,
3966 unless one has side effects. If both strings have constant lengths,
3973 else if (TREE_SIDE_EFFECTS (len1))
3975 else if (TREE_SIDE_EFFECTS (len2))
3977 else if (TREE_CODE (len1) != INTEGER_CST)
3979 else if (TREE_CODE (len2) != INTEGER_CST)
3981 else if (tree_int_cst_lt (len1, len2))
3986 /* If both arguments have side effects, we cannot optimize. */
3987 if (!len || TREE_SIDE_EFFECTS (len))
3990 arg3_rtx = expand_normal (len);
3992 /* Make a place to write the result of the instruction. */
3995 && REG_P (result) && GET_MODE (result) == insn_mode
3996 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3997 result = gen_reg_rtx (insn_mode);
3999 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4000 GEN_INT (MIN (arg1_align, arg2_align)));
4008 /* Return the value in the proper mode for this function. */
4009 mode = TYPE_MODE (TREE_TYPE (exp));
4010 if (GET_MODE (result) == mode)
4013 return convert_to_mode (mode, result, 0);
4014 convert_move (target, result, 0);
4018 /* Expand the library call ourselves using a stabilized argument
4019 list to avoid re-evaluating the function's arguments twice. */
4020 #ifdef HAVE_cmpstrnsi
4023 arglist = build_tree_list (NULL_TREE, arg2);
4024 arglist = tree_cons (NULL_TREE, arg1, arglist);
4025 fndecl = get_callee_fndecl (exp);
4026 fn = build_function_call_expr (fndecl, arglist);
4027 if (TREE_CODE (fn) == CALL_EXPR)
4028 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4029 return expand_call (fn, target, target == const0_rtx);
4035 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
4036 if we failed the caller should emit a normal call, otherwise try to get
4037 the result in TARGET, if convenient. */
4040 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
4042 tree arglist = TREE_OPERAND (exp, 1);
4044 if (!validate_arglist (arglist,
4045 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4049 tree result = fold_builtin_strncmp (arglist);
4051 return expand_expr (result, target, mode, EXPAND_NORMAL);
4054 /* If c_strlen can determine an expression for one of the string
4055 lengths, and it doesn't have side effects, then emit cmpstrnsi
4056 using length MIN(strlen(string)+1, arg3). */
4057 #ifdef HAVE_cmpstrnsi
4060 tree arg1 = TREE_VALUE (arglist);
4061 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
4062 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4063 tree len, len1, len2;
4064 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4069 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4071 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
4072 enum machine_mode insn_mode
4073 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4075 len1 = c_strlen (arg1, 1);
4076 len2 = c_strlen (arg2, 1);
4079 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4081 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4083 /* If we don't have a constant length for the first, use the length
4084 of the second, if we know it. We don't require a constant for
4085 this case; some cost analysis could be done if both are available
4086 but neither is constant. For now, assume they're equally cheap,
4087 unless one has side effects. If both strings have constant lengths,
4094 else if (TREE_SIDE_EFFECTS (len1))
4096 else if (TREE_SIDE_EFFECTS (len2))
4098 else if (TREE_CODE (len1) != INTEGER_CST)
4100 else if (TREE_CODE (len2) != INTEGER_CST)
4102 else if (tree_int_cst_lt (len1, len2))
4107 /* If both arguments have side effects, we cannot optimize. */
4108 if (!len || TREE_SIDE_EFFECTS (len))
4111 /* The actual new length parameter is MIN(len,arg3). */
4112 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
4113 fold_convert (TREE_TYPE (len), arg3));
4115 /* If we don't have POINTER_TYPE, call the function. */
4116 if (arg1_align == 0 || arg2_align == 0)
4119 /* Make a place to write the result of the instruction. */
4122 && REG_P (result) && GET_MODE (result) == insn_mode
4123 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4124 result = gen_reg_rtx (insn_mode);
4126 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4127 arg1 = builtin_save_expr (arg1);
4128 arg2 = builtin_save_expr (arg2);
4129 len = builtin_save_expr (len);
4131 arg1_rtx = get_memory_rtx (arg1, len);
4132 arg2_rtx = get_memory_rtx (arg2, len);
4133 arg3_rtx = expand_normal (len);
4134 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4135 GEN_INT (MIN (arg1_align, arg2_align)));
4140 /* Return the value in the proper mode for this function. */
4141 mode = TYPE_MODE (TREE_TYPE (exp));
4142 if (GET_MODE (result) == mode)
4145 return convert_to_mode (mode, result, 0);
4146 convert_move (target, result, 0);
4150 /* Expand the library call ourselves using a stabilized argument
4151 list to avoid re-evaluating the function's arguments twice. */
4152 arglist = build_tree_list (NULL_TREE, len);
4153 arglist = tree_cons (NULL_TREE, arg2, arglist);
4154 arglist = tree_cons (NULL_TREE, arg1, arglist);
4155 fndecl = get_callee_fndecl (exp);
4156 fn = build_function_call_expr (fndecl, arglist);
4157 if (TREE_CODE (fn) == CALL_EXPR)
4158 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4159 return expand_call (fn, target, target == const0_rtx);
4165 /* Expand expression EXP, which is a call to the strcat builtin.
4166 Return 0 if we failed the caller should emit a normal call,
4167 otherwise try to get the result in TARGET, if convenient. */
4170 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
4172 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4176 tree dst = TREE_VALUE (arglist),
4177 src = TREE_VALUE (TREE_CHAIN (arglist));
4178 const char *p = c_getstr (src);
4180 /* If the string length is zero, return the dst parameter. */
4181 if (p && *p == '\0')
4182 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4186 /* See if we can store by pieces into (dst + strlen(dst)). */
4187 tree newsrc, newdst,
4188 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4191 /* Stabilize the argument list. */
4192 newsrc = builtin_save_expr (src);
4194 arglist = build_tree_list (NULL_TREE, newsrc);
4196 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4198 dst = builtin_save_expr (dst);
4202 /* Create strlen (dst). */
4204 build_function_call_expr (strlen_fn,
4205 build_tree_list (NULL_TREE, dst));
4206 /* Create (dst + (cast) strlen (dst)). */
4207 newdst = fold_convert (TREE_TYPE (dst), newdst);
4208 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4210 newdst = builtin_save_expr (newdst);
4211 arglist = tree_cons (NULL_TREE, newdst, arglist);
4213 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4215 end_sequence (); /* Stop sequence. */
4219 /* Output the entire sequence. */
4220 insns = get_insns ();
4224 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4231 /* Expand expression EXP, which is a call to the strncat builtin.
4232 Return 0 if we failed the caller should emit a normal call,
4233 otherwise try to get the result in TARGET, if convenient. */
4236 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4238 if (validate_arglist (arglist,
4239 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4241 tree result = fold_builtin_strncat (arglist);
4243 return expand_expr (result, target, mode, EXPAND_NORMAL);
4248 /* Expand expression EXP, which is a call to the strspn builtin.
4249 Return 0 if we failed the caller should emit a normal call,
4250 otherwise try to get the result in TARGET, if convenient. */
4253 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4255 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4257 tree result = fold_builtin_strspn (arglist);
4259 return expand_expr (result, target, mode, EXPAND_NORMAL);
4264 /* Expand expression EXP, which is a call to the strcspn builtin.
4265 Return 0 if we failed the caller should emit a normal call,
4266 otherwise try to get the result in TARGET, if convenient. */
4269 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4271 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4273 tree result = fold_builtin_strcspn (arglist);
4275 return expand_expr (result, target, mode, EXPAND_NORMAL);
4280 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4281 if that's convenient. */
4284 expand_builtin_saveregs (void)
4288 /* Don't do __builtin_saveregs more than once in a function.
4289 Save the result of the first call and reuse it. */
4290 if (saveregs_value != 0)
4291 return saveregs_value;
4293 /* When this function is called, it means that registers must be
4294 saved on entry to this function. So we migrate the call to the
4295 first insn of this function. */
4299 /* Do whatever the machine needs done in this case. */
4300 val = targetm.calls.expand_builtin_saveregs ();
4305 saveregs_value = val;
4307 /* Put the insns after the NOTE that starts the function. If this
4308 is inside a start_sequence, make the outer-level insn chain current, so
4309 the code is placed at the start of the function. */
4310 push_topmost_sequence ();
4311 emit_insn_after (seq, entry_of_function ());
4312 pop_topmost_sequence ();
4317 /* __builtin_args_info (N) returns word N of the arg space info
4318 for the current function. The number and meanings of words
4319 is controlled by the definition of CUMULATIVE_ARGS. */
4322 expand_builtin_args_info (tree arglist)
4324 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4325 int *word_ptr = (int *) ¤t_function_args_info;
4327 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4331 if (!host_integerp (TREE_VALUE (arglist), 0))
4332 error ("argument of %<__builtin_args_info%> must be constant");
4335 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4337 if (wordnum < 0 || wordnum >= nwords)
4338 error ("argument of %<__builtin_args_info%> out of range");
4340 return GEN_INT (word_ptr[wordnum]);
4344 error ("missing argument in %<__builtin_args_info%>");
4349 /* Expand a call to __builtin_next_arg. */
4352 expand_builtin_next_arg (void)
4354 /* Checking arguments is already done in fold_builtin_next_arg
4355 that must be called before this function. */
4356 return expand_binop (Pmode, add_optab,
4357 current_function_internal_arg_pointer,
4358 current_function_arg_offset_rtx,
4359 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4362 /* Make it easier for the backends by protecting the valist argument
4363 from multiple evaluations. */
4366 stabilize_va_list (tree valist, int needs_lvalue)
4368 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4370 if (TREE_SIDE_EFFECTS (valist))
4371 valist = save_expr (valist);
4373 /* For this case, the backends will be expecting a pointer to
4374 TREE_TYPE (va_list_type_node), but it's possible we've
4375 actually been given an array (an actual va_list_type_node).
4377 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4379 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4380 valist = build_fold_addr_expr_with_type (valist, p1);
4389 if (! TREE_SIDE_EFFECTS (valist))
4392 pt = build_pointer_type (va_list_type_node);
4393 valist = fold_build1 (ADDR_EXPR, pt, valist);
4394 TREE_SIDE_EFFECTS (valist) = 1;
4397 if (TREE_SIDE_EFFECTS (valist))
4398 valist = save_expr (valist);
4399 valist = build_fold_indirect_ref (valist);
4405 /* The "standard" definition of va_list is void*. */
4408 std_build_builtin_va_list (void)
4410 return ptr_type_node;
4413 /* The "standard" implementation of va_start: just assign `nextarg' to
4417 std_expand_builtin_va_start (tree valist, rtx nextarg)
4421 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist,
4422 make_tree (ptr_type_node, nextarg));
4423 TREE_SIDE_EFFECTS (t) = 1;
4425 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4428 /* Expand ARGLIST, from a call to __builtin_va_start. */
4431 expand_builtin_va_start (tree arglist)
4436 chain = TREE_CHAIN (arglist);
4440 error ("too few arguments to function %<va_start%>");
4444 if (fold_builtin_next_arg (chain))
4447 nextarg = expand_builtin_next_arg ();
4448 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4450 #ifdef EXPAND_BUILTIN_VA_START
4451 EXPAND_BUILTIN_VA_START (valist, nextarg);
4453 std_expand_builtin_va_start (valist, nextarg);
4459 /* The "standard" implementation of va_arg: read the value from the
4460 current (padded) address and increment by the (padded) size. */
4463 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4465 tree addr, t, type_size, rounded_size, valist_tmp;
4466 unsigned HOST_WIDE_INT align, boundary;
4469 #ifdef ARGS_GROW_DOWNWARD
4470 /* All of the alignment and movement below is for args-grow-up machines.
4471 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4472 implement their own specialized gimplify_va_arg_expr routines. */
4476 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4478 type = build_pointer_type (type);
4480 align = PARM_BOUNDARY / BITS_PER_UNIT;
4481 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4483 /* Hoist the valist value into a temporary for the moment. */
4484 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4486 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4487 requires greater alignment, we must perform dynamic alignment. */
4488 if (boundary > align
4489 && !integer_zerop (TYPE_SIZE (type)))
4491 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4492 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4493 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4494 gimplify_and_add (t, pre_p);
4496 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4497 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist_tmp,
4498 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4499 gimplify_and_add (t, pre_p);
4504 /* If the actual alignment is less than the alignment of the type,
4505 adjust the type accordingly so that we don't assume strict alignment
4506 when deferencing the pointer. */
4507 boundary *= BITS_PER_UNIT;
4508 if (boundary < TYPE_ALIGN (type))
4510 type = build_variant_type_copy (type);
4511 TYPE_ALIGN (type) = boundary;
4514 /* Compute the rounded size of the type. */
4515 type_size = size_in_bytes (type);
4516 rounded_size = round_up (type_size, align);
4518 /* Reduce rounded_size so it's sharable with the postqueue. */
4519 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4523 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4525 /* Small args are padded downward. */
4526 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4527 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4528 size_binop (MINUS_EXPR, rounded_size, type_size));
4529 t = fold_convert (TREE_TYPE (addr), t);
4530 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4533 /* Compute new value for AP. */
4534 t = fold_convert (TREE_TYPE (valist), rounded_size);
4535 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4536 t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist, t);
4537 gimplify_and_add (t, pre_p);
4539 addr = fold_convert (build_pointer_type (type), addr);
4542 addr = build_va_arg_indirect_ref (addr);
4544 return build_va_arg_indirect_ref (addr);
4547 /* Build an indirect-ref expression over the given TREE, which represents a
4548 piece of a va_arg() expansion. */
4550 build_va_arg_indirect_ref (tree addr)
4552 addr = build_fold_indirect_ref (addr);
4554 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4560 /* Return a dummy expression of type TYPE in order to keep going after an
4564 dummy_object (tree type)
4566 tree t = build_int_cst (build_pointer_type (type), 0);
4567 return build1 (INDIRECT_REF, type, t);
4570 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4571 builtin function, but a very special sort of operator. */
4573 enum gimplify_status
4574 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4576 tree promoted_type, want_va_type, have_va_type;
4577 tree valist = TREE_OPERAND (*expr_p, 0);
4578 tree type = TREE_TYPE (*expr_p);
4581 /* Verify that valist is of the proper type. */
4582 want_va_type = va_list_type_node;
4583 have_va_type = TREE_TYPE (valist);
4585 if (have_va_type == error_mark_node)
4588 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4590 /* If va_list is an array type, the argument may have decayed
4591 to a pointer type, e.g. by being passed to another function.
4592 In that case, unwrap both types so that we can compare the
4593 underlying records. */
4594 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4595 || POINTER_TYPE_P (have_va_type))
4597 want_va_type = TREE_TYPE (want_va_type);
4598 have_va_type = TREE_TYPE (have_va_type);
4602 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4604 error ("first argument to %<va_arg%> not of type %<va_list%>");
4608 /* Generate a diagnostic for requesting data of a type that cannot
4609 be passed through `...' due to type promotion at the call site. */
4610 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4613 static bool gave_help;
4615 /* Unfortunately, this is merely undefined, rather than a constraint
4616 violation, so we cannot make this an error. If this call is never
4617 executed, the program is still strictly conforming. */
4618 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4619 type, promoted_type);
4623 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4624 promoted_type, type);
4627 /* We can, however, treat "undefined" any way we please.
4628 Call abort to encourage the user to fix the program. */
4629 inform ("if this code is reached, the program will abort");
4630 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4632 append_to_statement_list (t, pre_p);
4634 /* This is dead code, but go ahead and finish so that the
4635 mode of the result comes out right. */
4636 *expr_p = dummy_object (type);
4641 /* Make it easier for the backends by protecting the valist argument
4642 from multiple evaluations. */
4643 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4645 /* For this case, the backends will be expecting a pointer to
4646 TREE_TYPE (va_list_type_node), but it's possible we've
4647 actually been given an array (an actual va_list_type_node).
4649 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4651 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4652 valist = build_fold_addr_expr_with_type (valist, p1);
4654 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4657 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4659 if (!targetm.gimplify_va_arg_expr)
4660 /* FIXME:Once most targets are converted we should merely
4661 assert this is non-null. */
4664 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4669 /* Expand ARGLIST, from a call to __builtin_va_end. */
4672 expand_builtin_va_end (tree arglist)
4674 tree valist = TREE_VALUE (arglist);
4676 /* Evaluate for side effects, if needed. I hate macros that don't
4678 if (TREE_SIDE_EFFECTS (valist))
4679 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4684 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4685 builtin rather than just as an assignment in stdarg.h because of the
4686 nastiness of array-type va_list types. */
4689 expand_builtin_va_copy (tree arglist)
4693 dst = TREE_VALUE (arglist);
4694 src = TREE_VALUE (TREE_CHAIN (arglist));
4696 dst = stabilize_va_list (dst, 1);
4697 src = stabilize_va_list (src, 0);
4699 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4701 t = build2 (GIMPLE_MODIFY_STMT, va_list_type_node, dst, src);
4702 TREE_SIDE_EFFECTS (t) = 1;
4703 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4707 rtx dstb, srcb, size;
4709 /* Evaluate to pointers. */
4710 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4711 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4712 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4713 VOIDmode, EXPAND_NORMAL);
4715 dstb = convert_memory_address (Pmode, dstb);
4716 srcb = convert_memory_address (Pmode, srcb);
4718 /* "Dereference" to BLKmode memories. */
4719 dstb = gen_rtx_MEM (BLKmode, dstb);
4720 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4721 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4722 srcb = gen_rtx_MEM (BLKmode, srcb);
4723 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4724 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4727 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4733 /* Expand a call to one of the builtin functions __builtin_frame_address or
4734 __builtin_return_address. */
4737 expand_builtin_frame_address (tree fndecl, tree arglist)
4739 /* The argument must be a nonnegative integer constant.
4740 It counts the number of frames to scan up the stack.
4741 The value is the return address saved in that frame. */
4743 /* Warning about missing arg was already issued. */
4745 else if (! host_integerp (TREE_VALUE (arglist), 1))
4747 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4748 error ("invalid argument to %<__builtin_frame_address%>");
4750 error ("invalid argument to %<__builtin_return_address%>");
4756 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4757 tree_low_cst (TREE_VALUE (arglist), 1));
4759 /* Some ports cannot access arbitrary stack frames. */
4762 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4763 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4765 warning (0, "unsupported argument to %<__builtin_return_address%>");
4769 /* For __builtin_frame_address, return what we've got. */
4770 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4774 && ! CONSTANT_P (tem))
4775 tem = copy_to_mode_reg (Pmode, tem);
4780 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4781 we failed and the caller should emit a normal call, otherwise try to get
4782 the result in TARGET, if convenient. */
4785 expand_builtin_alloca (tree arglist, rtx target)
4790 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4791 should always expand to function calls. These can be intercepted
4796 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4799 /* Compute the argument. */
4800 op0 = expand_normal (TREE_VALUE (arglist));
4802 /* Allocate the desired space. */
4803 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4804 result = convert_memory_address (ptr_mode, result);
4809 /* Expand a call to a bswap builtin. The arguments are in ARGLIST. MODE
4810 is the mode to expand with. */
4813 expand_builtin_bswap (tree arglist, rtx target, rtx subtarget)
4815 enum machine_mode mode;
4819 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4822 arg = TREE_VALUE (arglist);
4823 mode = TYPE_MODE (TREE_TYPE (arg));
4824 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4826 target = expand_unop (mode, bswap_optab, op0, target, 1);
4828 gcc_assert (target);
4830 return convert_to_mode (mode, target, 0);
4833 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4834 Return 0 if a normal call should be emitted rather than expanding the
4835 function in-line. If convenient, the result should be placed in TARGET.
4836 SUBTARGET may be used as the target for computing one of EXP's operands. */
4839 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4840 rtx subtarget, optab op_optab)
4843 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4846 /* Compute the argument. */
4847 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4848 /* Compute op, into TARGET if possible.
4849 Set TARGET to wherever the result comes back. */
4850 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4851 op_optab, op0, target, 1);
4852 gcc_assert (target);
4854 return convert_to_mode (target_mode, target, 0);
4857 /* If the string passed to fputs is a constant and is one character
4858 long, we attempt to transform this call into __builtin_fputc(). */
4861 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4863 /* Verify the arguments in the original call. */
4864 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4866 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4867 unlocked, NULL_TREE);
4869 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4874 /* Expand a call to __builtin_expect. We just return our argument
4875 as the builtin_expect semantic should've been already executed by
4876 tree branch prediction pass. */
4879 expand_builtin_expect (tree arglist, rtx target)
4883 if (arglist == NULL_TREE
4884 || TREE_CHAIN (arglist) == NULL_TREE)
4886 exp = TREE_VALUE (arglist);
4887 c = TREE_VALUE (TREE_CHAIN (arglist));
4889 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4890 /* When guessing was done, the hints should be already stripped away. */
4891 gcc_assert (!flag_guess_branch_prob);
4896 expand_builtin_trap (void)
4900 emit_insn (gen_trap ());
4903 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4907 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4908 Return 0 if a normal call should be emitted rather than expanding
4909 the function inline. If convenient, the result should be placed
4910 in TARGET. SUBTARGET may be used as the target for computing
4914 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4916 enum machine_mode mode;
4920 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4923 arg = TREE_VALUE (arglist);
4924 mode = TYPE_MODE (TREE_TYPE (arg));
4925 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4926 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4929 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4930 Return NULL is a normal call should be emitted rather than expanding the
4931 function inline. If convenient, the result should be placed in TARGET.
4932 SUBTARGET may be used as the target for computing the operand. */
4935 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4940 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4943 arg = TREE_VALUE (arglist);
4944 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4946 arg = TREE_VALUE (TREE_CHAIN (arglist));
4947 op1 = expand_normal (arg);
4949 return expand_copysign (op0, op1, target);
4952 /* Create a new constant string literal and return a char* pointer to it.
4953 The STRING_CST value is the LEN characters at STR. */
4955 build_string_literal (int len, const char *str)
4957 tree t, elem, index, type;
4959 t = build_string (len, str);
4960 elem = build_type_variant (char_type_node, 1, 0);
4961 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4962 type = build_array_type (elem, index);
4963 TREE_TYPE (t) = type;
4964 TREE_CONSTANT (t) = 1;
4965 TREE_INVARIANT (t) = 1;
4966 TREE_READONLY (t) = 1;
4967 TREE_STATIC (t) = 1;
4969 type = build_pointer_type (type);
4970 t = build1 (ADDR_EXPR, type, t);
4972 type = build_pointer_type (elem);
4973 t = build1 (NOP_EXPR, type, t);
4977 /* Expand EXP, a call to printf or printf_unlocked.
4978 Return 0 if a normal call should be emitted rather than transforming
4979 the function inline. If convenient, the result should be placed in
4980 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4983 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4986 tree arglist = TREE_OPERAND (exp, 1);
4987 /* If we're using an unlocked function, assume the other unlocked
4988 functions exist explicitly. */
4989 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4990 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4991 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4992 : implicit_built_in_decls[BUILT_IN_PUTS];
4993 const char *fmt_str;
4996 /* If the return value is used, don't do the transformation. */
4997 if (target != const0_rtx)
5000 /* Verify the required arguments in the original call. */
5003 fmt = TREE_VALUE (arglist);
5004 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5006 arglist = TREE_CHAIN (arglist);
5008 /* Check whether the format is a literal string constant. */
5009 fmt_str = c_getstr (fmt);
5010 if (fmt_str == NULL)
5013 if (!init_target_chars())
5016 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
5017 if (strcmp (fmt_str, target_percent_s_newline) == 0)
5020 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5021 || TREE_CHAIN (arglist))
5025 /* If the format specifier was "%c", call __builtin_putchar(arg). */
5026 else if (strcmp (fmt_str, target_percent_c) == 0)
5029 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5030 || TREE_CHAIN (arglist))
5036 /* We can't handle anything else with % args or %% ... yet. */
5037 if (strchr (fmt_str, target_percent))
5043 /* If the format specifier was "", printf does nothing. */
5044 if (fmt_str[0] == '\0')
5046 /* If the format specifier has length of 1, call putchar. */
5047 if (fmt_str[1] == '\0')
5049 /* Given printf("c"), (where c is any one character,)
5050 convert "c"[0] to an int and pass that to the replacement
5052 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5053 arglist = build_tree_list (NULL_TREE, arg);
5058 /* If the format specifier was "string\n", call puts("string"). */
5059 size_t len = strlen (fmt_str);
5060 if ((unsigned char)fmt_str[len - 1] == target_newline)
5062 /* Create a NUL-terminated string that's one char shorter
5063 than the original, stripping off the trailing '\n'. */
5064 char *newstr = alloca (len);
5065 memcpy (newstr, fmt_str, len - 1);
5066 newstr[len - 1] = 0;
5068 arg = build_string_literal (len, newstr);
5069 arglist = build_tree_list (NULL_TREE, arg);
5073 /* We'd like to arrange to call fputs(string,stdout) here,
5074 but we need stdout and don't have a way to get it yet. */
5081 fn = build_function_call_expr (fn, arglist);
5082 if (TREE_CODE (fn) == CALL_EXPR)
5083 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5084 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5087 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5088 Return 0 if a normal call should be emitted rather than transforming
5089 the function inline. If convenient, the result should be placed in
5090 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5093 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5096 tree arglist = TREE_OPERAND (exp, 1);
5097 /* If we're using an unlocked function, assume the other unlocked
5098 functions exist explicitly. */
5099 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5100 : implicit_built_in_decls[BUILT_IN_FPUTC];
5101 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5102 : implicit_built_in_decls[BUILT_IN_FPUTS];
5103 const char *fmt_str;
5104 tree fn, fmt, fp, arg;
5106 /* If the return value is used, don't do the transformation. */
5107 if (target != const0_rtx)
5110 /* Verify the required arguments in the original call. */
5113 fp = TREE_VALUE (arglist);
5114 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5116 arglist = TREE_CHAIN (arglist);
5119 fmt = TREE_VALUE (arglist);
5120 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5122 arglist = TREE_CHAIN (arglist);
5124 /* Check whether the format is a literal string constant. */
5125 fmt_str = c_getstr (fmt);
5126 if (fmt_str == NULL)
5129 if (!init_target_chars())
5132 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5133 if (strcmp (fmt_str, target_percent_s) == 0)
5136 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5137 || TREE_CHAIN (arglist))
5139 arg = TREE_VALUE (arglist);
5140 arglist = build_tree_list (NULL_TREE, fp);
5141 arglist = tree_cons (NULL_TREE, arg, arglist);
5144 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5145 else if (strcmp (fmt_str, target_percent_c) == 0)
5148 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5149 || TREE_CHAIN (arglist))
5151 arg = TREE_VALUE (arglist);
5152 arglist = build_tree_list (NULL_TREE, fp);
5153 arglist = tree_cons (NULL_TREE, arg, arglist);
5158 /* We can't handle anything else with % args or %% ... yet. */
5159 if (strchr (fmt_str, target_percent))
5165 /* If the format specifier was "", fprintf does nothing. */
5166 if (fmt_str[0] == '\0')
5168 /* Evaluate and ignore FILE* argument for side-effects. */
5169 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5173 /* When "string" doesn't contain %, replace all cases of
5174 fprintf(stream,string) with fputs(string,stream). The fputs
5175 builtin will take care of special cases like length == 1. */
5176 arglist = build_tree_list (NULL_TREE, fp);
5177 arglist = tree_cons (NULL_TREE, fmt, arglist);
5183 fn = build_function_call_expr (fn, arglist);
5184 if (TREE_CODE (fn) == CALL_EXPR)
5185 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5186 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5189 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5190 a normal call should be emitted rather than expanding the function
5191 inline. If convenient, the result should be placed in TARGET with
5195 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5197 tree orig_arglist, dest, fmt;
5198 const char *fmt_str;
5200 orig_arglist = arglist;
5202 /* Verify the required arguments in the original call. */
5205 dest = TREE_VALUE (arglist);
5206 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5208 arglist = TREE_CHAIN (arglist);
5211 fmt = TREE_VALUE (arglist);
5212 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5214 arglist = TREE_CHAIN (arglist);
5216 /* Check whether the format is a literal string constant. */
5217 fmt_str = c_getstr (fmt);
5218 if (fmt_str == NULL)
5221 if (!init_target_chars())
5224 /* If the format doesn't contain % args or %%, use strcpy. */
5225 if (strchr (fmt_str, target_percent) == 0)
5227 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5230 if (arglist || ! fn)
5232 expand_expr (build_function_call_expr (fn, orig_arglist),
5233 const0_rtx, VOIDmode, EXPAND_NORMAL);
5234 if (target == const0_rtx)
5236 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5237 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5239 /* If the format is "%s", use strcpy if the result isn't used. */
5240 else if (strcmp (fmt_str, target_percent_s) == 0)
5243 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5248 if (! arglist || TREE_CHAIN (arglist))
5250 arg = TREE_VALUE (arglist);
5251 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5254 if (target != const0_rtx)
5256 len = c_strlen (arg, 1);
5257 if (! len || TREE_CODE (len) != INTEGER_CST)
5263 arglist = build_tree_list (NULL_TREE, arg);
5264 arglist = tree_cons (NULL_TREE, dest, arglist);
5265 expand_expr (build_function_call_expr (fn, arglist),
5266 const0_rtx, VOIDmode, EXPAND_NORMAL);
5268 if (target == const0_rtx)
5270 return expand_expr (len, target, mode, EXPAND_NORMAL);
5276 /* Expand a call to either the entry or exit function profiler. */
5279 expand_builtin_profile_func (bool exitp)
5283 this = DECL_RTL (current_function_decl);
5284 gcc_assert (MEM_P (this));
5285 this = XEXP (this, 0);
5288 which = profile_function_exit_libfunc;
5290 which = profile_function_entry_libfunc;
5292 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5293 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5300 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5303 round_trampoline_addr (rtx tramp)
5305 rtx temp, addend, mask;
5307 /* If we don't need too much alignment, we'll have been guaranteed
5308 proper alignment by get_trampoline_type. */
5309 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5312 /* Round address up to desired boundary. */
5313 temp = gen_reg_rtx (Pmode);
5314 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5315 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5317 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5318 temp, 0, OPTAB_LIB_WIDEN);
5319 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5320 temp, 0, OPTAB_LIB_WIDEN);
5326 expand_builtin_init_trampoline (tree arglist)
5328 tree t_tramp, t_func, t_chain;
5329 rtx r_tramp, r_func, r_chain;
5330 #ifdef TRAMPOLINE_TEMPLATE
5334 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5335 POINTER_TYPE, VOID_TYPE))
5338 t_tramp = TREE_VALUE (arglist);
5339 arglist = TREE_CHAIN (arglist);
5340 t_func = TREE_VALUE (arglist);
5341 arglist = TREE_CHAIN (arglist);
5342 t_chain = TREE_VALUE (arglist);
5344 r_tramp = expand_normal (t_tramp);
5345 r_func = expand_normal (t_func);
5346 r_chain = expand_normal (t_chain);
5348 /* Generate insns to initialize the trampoline. */
5349 r_tramp = round_trampoline_addr (r_tramp);
5350 #ifdef TRAMPOLINE_TEMPLATE
5351 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5352 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5353 emit_block_move (blktramp, assemble_trampoline_template (),
5354 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5356 trampolines_created = 1;
5357 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5363 expand_builtin_adjust_trampoline (tree arglist)
5367 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5370 tramp = expand_normal (TREE_VALUE (arglist));
5371 tramp = round_trampoline_addr (tramp);
5372 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5373 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5379 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5380 Return NULL_RTX if a normal call should be emitted rather than expanding
5381 the function in-line. EXP is the expression that is a call to the builtin
5382 function; if convenient, the result should be placed in TARGET. */
5385 expand_builtin_signbit (tree exp, rtx target)
5387 const struct real_format *fmt;
5388 enum machine_mode fmode, imode, rmode;
5389 HOST_WIDE_INT hi, lo;
5394 arglist = TREE_OPERAND (exp, 1);
5395 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5398 arg = TREE_VALUE (arglist);
5399 fmode = TYPE_MODE (TREE_TYPE (arg));
5400 rmode = TYPE_MODE (TREE_TYPE (exp));
5401 fmt = REAL_MODE_FORMAT (fmode);
5403 /* For floating point formats without a sign bit, implement signbit
5405 bitpos = fmt->signbit_ro;
5408 /* But we can't do this if the format supports signed zero. */
5409 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5412 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5413 build_real (TREE_TYPE (arg), dconst0));
5414 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5417 temp = expand_normal (arg);
5418 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5420 imode = int_mode_for_mode (fmode);
5421 if (imode == BLKmode)
5423 temp = gen_lowpart (imode, temp);
5428 /* Handle targets with different FP word orders. */
5429 if (FLOAT_WORDS_BIG_ENDIAN)
5430 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5432 word = bitpos / BITS_PER_WORD;
5433 temp = operand_subword_force (temp, word, fmode);
5434 bitpos = bitpos % BITS_PER_WORD;
5437 /* Force the intermediate word_mode (or narrower) result into a
5438 register. This avoids attempting to create paradoxical SUBREGs
5439 of floating point modes below. */
5440 temp = force_reg (imode, temp);
5442 /* If the bitpos is within the "result mode" lowpart, the operation
5443 can be implement with a single bitwise AND. Otherwise, we need
5444 a right shift and an AND. */
5446 if (bitpos < GET_MODE_BITSIZE (rmode))
5448 if (bitpos < HOST_BITS_PER_WIDE_INT)
5451 lo = (HOST_WIDE_INT) 1 << bitpos;
5455 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5460 temp = gen_lowpart (rmode, temp);
5461 temp = expand_binop (rmode, and_optab, temp,
5462 immed_double_const (lo, hi, rmode),
5463 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5467 /* Perform a logical right shift to place the signbit in the least
5468 significant bit, then truncate the result to the desired mode
5469 and mask just this bit. */
5470 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5471 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5472 temp = gen_lowpart (rmode, temp);
5473 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5474 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5480 /* Expand fork or exec calls. TARGET is the desired target of the
5481 call. ARGLIST is the list of arguments of the call. FN is the
5482 identificator of the actual function. IGNORE is nonzero if the
5483 value is to be ignored. */
5486 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5491 /* If we are not profiling, just call the function. */
5492 if (!profile_arc_flag)
5495 /* Otherwise call the wrapper. This should be equivalent for the rest of
5496 compiler, so the code does not diverge, and the wrapper may run the
5497 code necessary for keeping the profiling sane. */
5499 switch (DECL_FUNCTION_CODE (fn))
5502 id = get_identifier ("__gcov_fork");
5505 case BUILT_IN_EXECL:
5506 id = get_identifier ("__gcov_execl");
5509 case BUILT_IN_EXECV:
5510 id = get_identifier ("__gcov_execv");
5513 case BUILT_IN_EXECLP:
5514 id = get_identifier ("__gcov_execlp");
5517 case BUILT_IN_EXECLE:
5518 id = get_identifier ("__gcov_execle");
5521 case BUILT_IN_EXECVP:
5522 id = get_identifier ("__gcov_execvp");
5525 case BUILT_IN_EXECVE:
5526 id = get_identifier ("__gcov_execve");
5533 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5534 DECL_EXTERNAL (decl) = 1;
5535 TREE_PUBLIC (decl) = 1;
5536 DECL_ARTIFICIAL (decl) = 1;
5537 TREE_NOTHROW (decl) = 1;
5538 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5539 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5540 call = build_function_call_expr (decl, arglist);
5542 return expand_call (call, target, ignore);
5546 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5547 the pointer in these functions is void*, the tree optimizers may remove
5548 casts. The mode computed in expand_builtin isn't reliable either, due
5549 to __sync_bool_compare_and_swap.
5551 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5552 group of builtins. This gives us log2 of the mode size. */
5554 static inline enum machine_mode
5555 get_builtin_sync_mode (int fcode_diff)
5557 /* The size is not negotiable, so ask not to get BLKmode in return
5558 if the target indicates that a smaller size would be better. */
5559 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5562 /* Expand the memory expression LOC and return the appropriate memory operand
5563 for the builtin_sync operations. */
5566 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5570 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5572 /* Note that we explicitly do not want any alias information for this
5573 memory, so that we kill all other live memories. Otherwise we don't
5574 satisfy the full barrier semantics of the intrinsic. */
5575 mem = validize_mem (gen_rtx_MEM (mode, addr));
5577 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5578 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5579 MEM_VOLATILE_P (mem) = 1;
5584 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5585 ARGLIST is the operands list to the function. CODE is the rtx code
5586 that corresponds to the arithmetic or logical operation from the name;
5587 an exception here is that NOT actually means NAND. TARGET is an optional
5588 place for us to store the results; AFTER is true if this is the
5589 fetch_and_xxx form. IGNORE is true if we don't actually care about
5590 the result of the operation at all. */
5593 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5594 enum rtx_code code, bool after,
5595 rtx target, bool ignore)
5599 /* Expand the operands. */
5600 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5602 arglist = TREE_CHAIN (arglist);
5603 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5604 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5605 val = convert_to_mode (mode, val, 1);
5608 return expand_sync_operation (mem, val, code);
5610 return expand_sync_fetch_operation (mem, val, code, after, target);
5613 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5614 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5615 true if this is the boolean form. TARGET is a place for us to store the
5616 results; this is NOT optional if IS_BOOL is true. */
5619 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5620 bool is_bool, rtx target)
5622 rtx old_val, new_val, mem;
5624 /* Expand the operands. */
5625 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5627 arglist = TREE_CHAIN (arglist);
5628 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5629 /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */
5630 old_val = convert_to_mode (mode, old_val, 1);
5632 arglist = TREE_CHAIN (arglist);
5633 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5634 /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */
5635 new_val = convert_to_mode (mode, new_val, 1);
5638 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5640 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5643 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5644 general form is actually an atomic exchange, and some targets only
5645 support a reduced form with the second argument being a constant 1.
5646 ARGLIST is the operands list to the function; TARGET is an optional
5647 place for us to store the results. */
5650 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5655 /* Expand the operands. */
5656 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5658 arglist = TREE_CHAIN (arglist);
5659 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5660 /* If VAL is promoted to a wider mode, convert it back to MODE. */
5661 val = convert_to_mode (mode, val, 1);
5663 return expand_sync_lock_test_and_set (mem, val, target);
5666 /* Expand the __sync_synchronize intrinsic. */
5669 expand_builtin_synchronize (void)
5673 #ifdef HAVE_memory_barrier
5674 if (HAVE_memory_barrier)
5676 emit_insn (gen_memory_barrier ());
5681 /* If no explicit memory barrier instruction is available, create an
5682 empty asm stmt with a memory clobber. */
5683 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5684 tree_cons (NULL, build_string (6, "memory"), NULL));
5685 ASM_VOLATILE_P (x) = 1;
5686 expand_asm_expr (x);
5689 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5693 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5695 enum insn_code icode;
5697 rtx val = const0_rtx;
5699 /* Expand the operands. */
5700 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5702 /* If there is an explicit operation in the md file, use it. */
5703 icode = sync_lock_release[mode];
5704 if (icode != CODE_FOR_nothing)
5706 if (!insn_data[icode].operand[1].predicate (val, mode))
5707 val = force_reg (mode, val);
5709 insn = GEN_FCN (icode) (mem, val);
5717 /* Otherwise we can implement this operation by emitting a barrier
5718 followed by a store of zero. */
5719 expand_builtin_synchronize ();
5720 emit_move_insn (mem, val);
5723 /* Expand an expression EXP that calls a built-in function,
5724 with result going to TARGET if that's convenient
5725 (and in mode MODE if that's convenient).
5726 SUBTARGET may be used as the target for computing one of EXP's operands.
5727 IGNORE is nonzero if the value is to be ignored. */
5730 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5733 tree fndecl = get_callee_fndecl (exp);
5734 tree arglist = TREE_OPERAND (exp, 1);
5735 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5736 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5738 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5739 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5741 /* When not optimizing, generate calls to library functions for a certain
5744 && !called_as_built_in (fndecl)
5745 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5746 && fcode != BUILT_IN_ALLOCA)
5747 return expand_call (exp, target, ignore);
5749 /* The built-in function expanders test for target == const0_rtx
5750 to determine whether the function's result will be ignored. */
5752 target = const0_rtx;
5754 /* If the result of a pure or const built-in function is ignored, and
5755 none of its arguments are volatile, we can avoid expanding the
5756 built-in call and just evaluate the arguments for side-effects. */
5757 if (target == const0_rtx
5758 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5760 bool volatilep = false;
5763 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5764 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5772 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5773 expand_expr (TREE_VALUE (arg), const0_rtx,
5774 VOIDmode, EXPAND_NORMAL);
5781 CASE_FLT_FN (BUILT_IN_FABS):
5782 target = expand_builtin_fabs (arglist, target, subtarget);
5787 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5788 target = expand_builtin_copysign (arglist, target, subtarget);
5793 /* Just do a normal library call if we were unable to fold
5795 CASE_FLT_FN (BUILT_IN_CABS):
5798 CASE_FLT_FN (BUILT_IN_EXP):
5799 CASE_FLT_FN (BUILT_IN_EXP10):
5800 CASE_FLT_FN (BUILT_IN_POW10):
5801 CASE_FLT_FN (BUILT_IN_EXP2):
5802 CASE_FLT_FN (BUILT_IN_EXPM1):
5803 CASE_FLT_FN (BUILT_IN_LOGB):
5804 CASE_FLT_FN (BUILT_IN_ILOGB):
5805 CASE_FLT_FN (BUILT_IN_LOG):
5806 CASE_FLT_FN (BUILT_IN_LOG10):
5807 CASE_FLT_FN (BUILT_IN_LOG2):
5808 CASE_FLT_FN (BUILT_IN_LOG1P):
5809 CASE_FLT_FN (BUILT_IN_TAN):
5810 CASE_FLT_FN (BUILT_IN_ASIN):
5811 CASE_FLT_FN (BUILT_IN_ACOS):
5812 CASE_FLT_FN (BUILT_IN_ATAN):
5813 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5814 because of possible accuracy problems. */
5815 if (! flag_unsafe_math_optimizations)
5817 CASE_FLT_FN (BUILT_IN_SQRT):
5818 CASE_FLT_FN (BUILT_IN_FLOOR):
5819 CASE_FLT_FN (BUILT_IN_CEIL):
5820 CASE_FLT_FN (BUILT_IN_TRUNC):
5821 CASE_FLT_FN (BUILT_IN_ROUND):
5822 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5823 CASE_FLT_FN (BUILT_IN_RINT):
5824 target = expand_builtin_mathfn (exp, target, subtarget);
5829 CASE_FLT_FN (BUILT_IN_LCEIL):
5830 CASE_FLT_FN (BUILT_IN_LLCEIL):
5831 CASE_FLT_FN (BUILT_IN_LFLOOR):
5832 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5833 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5838 CASE_FLT_FN (BUILT_IN_LRINT):
5839 CASE_FLT_FN (BUILT_IN_LLRINT):
5840 CASE_FLT_FN (BUILT_IN_LROUND):
5841 CASE_FLT_FN (BUILT_IN_LLROUND):
5842 target = expand_builtin_int_roundingfn_2 (exp, target, subtarget);
5847 CASE_FLT_FN (BUILT_IN_POW):
5848 target = expand_builtin_pow (exp, target, subtarget);
5853 CASE_FLT_FN (BUILT_IN_POWI):
5854 target = expand_builtin_powi (exp, target, subtarget);
5859 CASE_FLT_FN (BUILT_IN_ATAN2):
5860 CASE_FLT_FN (BUILT_IN_LDEXP):
5861 if (! flag_unsafe_math_optimizations)
5864 CASE_FLT_FN (BUILT_IN_FMOD):
5865 CASE_FLT_FN (BUILT_IN_REMAINDER):
5866 CASE_FLT_FN (BUILT_IN_DREM):
5867 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5872 CASE_FLT_FN (BUILT_IN_CEXPI):
5873 target = expand_builtin_cexpi (exp, target, subtarget);
5874 gcc_assert (target);
5877 CASE_FLT_FN (BUILT_IN_SIN):
5878 CASE_FLT_FN (BUILT_IN_COS):
5879 if (! flag_unsafe_math_optimizations)
5881 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5886 CASE_FLT_FN (BUILT_IN_SINCOS):
5887 if (! flag_unsafe_math_optimizations)
5889 target = expand_builtin_sincos (exp);
5894 case BUILT_IN_APPLY_ARGS:
5895 return expand_builtin_apply_args ();
5897 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5898 FUNCTION with a copy of the parameters described by
5899 ARGUMENTS, and ARGSIZE. It returns a block of memory
5900 allocated on the stack into which is stored all the registers
5901 that might possibly be used for returning the result of a
5902 function. ARGUMENTS is the value returned by
5903 __builtin_apply_args. ARGSIZE is the number of bytes of
5904 arguments that must be copied. ??? How should this value be
5905 computed? We'll also need a safe worst case value for varargs
5907 case BUILT_IN_APPLY:
5908 if (!validate_arglist (arglist, POINTER_TYPE,
5909 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5910 && !validate_arglist (arglist, REFERENCE_TYPE,
5911 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5919 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5920 ops[i] = expand_normal (TREE_VALUE (t));
5922 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5925 /* __builtin_return (RESULT) causes the function to return the
5926 value described by RESULT. RESULT is address of the block of
5927 memory returned by __builtin_apply. */
5928 case BUILT_IN_RETURN:
5929 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5930 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5933 case BUILT_IN_SAVEREGS:
5934 return expand_builtin_saveregs ();
5936 case BUILT_IN_ARGS_INFO:
5937 return expand_builtin_args_info (arglist);
5939 /* Return the address of the first anonymous stack arg. */
5940 case BUILT_IN_NEXT_ARG:
5941 if (fold_builtin_next_arg (arglist))
5943 return expand_builtin_next_arg ();
5945 case BUILT_IN_CLASSIFY_TYPE:
5946 return expand_builtin_classify_type (arglist);
5948 case BUILT_IN_CONSTANT_P:
5951 case BUILT_IN_FRAME_ADDRESS:
5952 case BUILT_IN_RETURN_ADDRESS:
5953 return expand_builtin_frame_address (fndecl, arglist);
5955 /* Returns the address of the area where the structure is returned.
5957 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5959 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5960 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5963 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5965 case BUILT_IN_ALLOCA:
5966 target = expand_builtin_alloca (arglist, target);
5971 case BUILT_IN_STACK_SAVE:
5972 return expand_stack_save ();
5974 case BUILT_IN_STACK_RESTORE:
5975 expand_stack_restore (TREE_VALUE (arglist));
5978 case BUILT_IN_BSWAP32:
5979 case BUILT_IN_BSWAP64:
5980 target = expand_builtin_bswap (arglist, target, subtarget);
5986 CASE_INT_FN (BUILT_IN_FFS):
5987 case BUILT_IN_FFSIMAX:
5988 target = expand_builtin_unop (target_mode, arglist, target,
5989 subtarget, ffs_optab);
5994 CASE_INT_FN (BUILT_IN_CLZ):
5995 case BUILT_IN_CLZIMAX:
5996 target = expand_builtin_unop (target_mode, arglist, target,
5997 subtarget, clz_optab);
6002 CASE_INT_FN (BUILT_IN_CTZ):
6003 case BUILT_IN_CTZIMAX:
6004 target = expand_builtin_unop (target_mode, arglist, target,
6005 subtarget, ctz_optab);
6010 CASE_INT_FN (BUILT_IN_POPCOUNT):
6011 case BUILT_IN_POPCOUNTIMAX:
6012 target = expand_builtin_unop (target_mode, arglist, target,
6013 subtarget, popcount_optab);
6018 CASE_INT_FN (BUILT_IN_PARITY):
6019 case BUILT_IN_PARITYIMAX:
6020 target = expand_builtin_unop (target_mode, arglist, target,
6021 subtarget, parity_optab);
6026 case BUILT_IN_STRLEN:
6027 target = expand_builtin_strlen (arglist, target, target_mode);
6032 case BUILT_IN_STRCPY:
6033 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
6038 case BUILT_IN_STRNCPY:
6039 target = expand_builtin_strncpy (exp, target, mode);
6044 case BUILT_IN_STPCPY:
6045 target = expand_builtin_stpcpy (exp, target, mode);
6050 case BUILT_IN_STRCAT:
6051 target = expand_builtin_strcat (fndecl, arglist, target, mode);
6056 case BUILT_IN_STRNCAT:
6057 target = expand_builtin_strncat (arglist, target, mode);
6062 case BUILT_IN_STRSPN:
6063 target = expand_builtin_strspn (arglist, target, mode);
6068 case BUILT_IN_STRCSPN:
6069 target = expand_builtin_strcspn (arglist, target, mode);
6074 case BUILT_IN_STRSTR:
6075 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
6080 case BUILT_IN_STRPBRK:
6081 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6086 case BUILT_IN_INDEX:
6087 case BUILT_IN_STRCHR:
6088 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6093 case BUILT_IN_RINDEX:
6094 case BUILT_IN_STRRCHR:
6095 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6100 case BUILT_IN_MEMCPY:
6101 target = expand_builtin_memcpy (exp, target, mode);
6106 case BUILT_IN_MEMPCPY:
6107 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6112 case BUILT_IN_MEMMOVE:
6113 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6119 case BUILT_IN_BCOPY:
6120 target = expand_builtin_bcopy (exp);
6125 case BUILT_IN_MEMSET:
6126 target = expand_builtin_memset (arglist, target, mode, exp);
6131 case BUILT_IN_BZERO:
6132 target = expand_builtin_bzero (exp);
6137 case BUILT_IN_STRCMP:
6138 target = expand_builtin_strcmp (exp, target, mode);
6143 case BUILT_IN_STRNCMP:
6144 target = expand_builtin_strncmp (exp, target, mode);
6150 case BUILT_IN_MEMCMP:
6151 target = expand_builtin_memcmp (exp, arglist, target, mode);
6156 case BUILT_IN_SETJMP:
6157 /* This should have been lowered to the builtins below. */
6160 case BUILT_IN_SETJMP_SETUP:
6161 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6162 and the receiver label. */
6163 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6165 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6166 VOIDmode, EXPAND_NORMAL);
6167 tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0);
6168 rtx label_r = label_rtx (label);
6170 /* This is copied from the handling of non-local gotos. */
6171 expand_builtin_setjmp_setup (buf_addr, label_r);
6172 nonlocal_goto_handler_labels
6173 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6174 nonlocal_goto_handler_labels);
6175 /* ??? Do not let expand_label treat us as such since we would
6176 not want to be both on the list of non-local labels and on
6177 the list of forced labels. */
6178 FORCED_LABEL (label) = 0;
6183 case BUILT_IN_SETJMP_DISPATCHER:
6184 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6185 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6187 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6188 rtx label_r = label_rtx (label);
6190 /* Remove the dispatcher label from the list of non-local labels
6191 since the receiver labels have been added to it above. */
6192 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6197 case BUILT_IN_SETJMP_RECEIVER:
6198 /* __builtin_setjmp_receiver is passed the receiver label. */
6199 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6201 tree label = TREE_OPERAND (TREE_VALUE (arglist), 0);
6202 rtx label_r = label_rtx (label);
6204 expand_builtin_setjmp_receiver (label_r);
6209 /* __builtin_longjmp is passed a pointer to an array of five words.
6210 It's similar to the C library longjmp function but works with
6211 __builtin_setjmp above. */
6212 case BUILT_IN_LONGJMP:
6213 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6215 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6216 VOIDmode, EXPAND_NORMAL);
6217 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6219 if (value != const1_rtx)
6221 error ("%<__builtin_longjmp%> second argument must be 1");
6225 expand_builtin_longjmp (buf_addr, value);
6230 case BUILT_IN_NONLOCAL_GOTO:
6231 target = expand_builtin_nonlocal_goto (arglist);
6236 /* This updates the setjmp buffer that is its argument with the value
6237 of the current stack pointer. */
6238 case BUILT_IN_UPDATE_SETJMP_BUF:
6239 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6242 = expand_normal (TREE_VALUE (arglist));
6244 expand_builtin_update_setjmp_buf (buf_addr);
6250 expand_builtin_trap ();
6253 case BUILT_IN_PRINTF:
6254 target = expand_builtin_printf (exp, target, mode, false);
6259 case BUILT_IN_PRINTF_UNLOCKED:
6260 target = expand_builtin_printf (exp, target, mode, true);
6265 case BUILT_IN_FPUTS:
6266 target = expand_builtin_fputs (arglist, target, false);
6270 case BUILT_IN_FPUTS_UNLOCKED:
6271 target = expand_builtin_fputs (arglist, target, true);
6276 case BUILT_IN_FPRINTF:
6277 target = expand_builtin_fprintf (exp, target, mode, false);
6282 case BUILT_IN_FPRINTF_UNLOCKED:
6283 target = expand_builtin_fprintf (exp, target, mode, true);
6288 case BUILT_IN_SPRINTF:
6289 target = expand_builtin_sprintf (arglist, target, mode);
6294 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6295 target = expand_builtin_signbit (exp, target);
6300 /* Various hooks for the DWARF 2 __throw routine. */
6301 case BUILT_IN_UNWIND_INIT:
6302 expand_builtin_unwind_init ();
6304 case BUILT_IN_DWARF_CFA:
6305 return virtual_cfa_rtx;
6306 #ifdef DWARF2_UNWIND_INFO
6307 case BUILT_IN_DWARF_SP_COLUMN:
6308 return expand_builtin_dwarf_sp_column ();
6309 case BUILT_IN_INIT_DWARF_REG_SIZES:
6310 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6313 case BUILT_IN_FROB_RETURN_ADDR:
6314 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6315 case BUILT_IN_EXTRACT_RETURN_ADDR:
6316 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6317 case BUILT_IN_EH_RETURN:
6318 expand_builtin_eh_return (TREE_VALUE (arglist),
6319 TREE_VALUE (TREE_CHAIN (arglist)));
6321 #ifdef EH_RETURN_DATA_REGNO
6322 case BUILT_IN_EH_RETURN_DATA_REGNO:
6323 return expand_builtin_eh_return_data_regno (arglist);
6325 case BUILT_IN_EXTEND_POINTER:
6326 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6328 case BUILT_IN_VA_START:
6329 case BUILT_IN_STDARG_START:
6330 return expand_builtin_va_start (arglist);
6331 case BUILT_IN_VA_END:
6332 return expand_builtin_va_end (arglist);
6333 case BUILT_IN_VA_COPY:
6334 return expand_builtin_va_copy (arglist);
6335 case BUILT_IN_EXPECT:
6336 return expand_builtin_expect (arglist, target);
6337 case BUILT_IN_PREFETCH:
6338 expand_builtin_prefetch (arglist);
6341 case BUILT_IN_PROFILE_FUNC_ENTER:
6342 return expand_builtin_profile_func (false);
6343 case BUILT_IN_PROFILE_FUNC_EXIT:
6344 return expand_builtin_profile_func (true);
6346 case BUILT_IN_INIT_TRAMPOLINE:
6347 return expand_builtin_init_trampoline (arglist);
6348 case BUILT_IN_ADJUST_TRAMPOLINE:
6349 return expand_builtin_adjust_trampoline (arglist);
6352 case BUILT_IN_EXECL:
6353 case BUILT_IN_EXECV:
6354 case BUILT_IN_EXECLP:
6355 case BUILT_IN_EXECLE:
6356 case BUILT_IN_EXECVP:
6357 case BUILT_IN_EXECVE:
6358 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6363 case BUILT_IN_FETCH_AND_ADD_1:
6364 case BUILT_IN_FETCH_AND_ADD_2:
6365 case BUILT_IN_FETCH_AND_ADD_4:
6366 case BUILT_IN_FETCH_AND_ADD_8:
6367 case BUILT_IN_FETCH_AND_ADD_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6369 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6370 false, target, ignore);
6375 case BUILT_IN_FETCH_AND_SUB_1:
6376 case BUILT_IN_FETCH_AND_SUB_2:
6377 case BUILT_IN_FETCH_AND_SUB_4:
6378 case BUILT_IN_FETCH_AND_SUB_8:
6379 case BUILT_IN_FETCH_AND_SUB_16:
6380 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6381 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6382 false, target, ignore);
6387 case BUILT_IN_FETCH_AND_OR_1:
6388 case BUILT_IN_FETCH_AND_OR_2:
6389 case BUILT_IN_FETCH_AND_OR_4:
6390 case BUILT_IN_FETCH_AND_OR_8:
6391 case BUILT_IN_FETCH_AND_OR_16:
6392 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6393 target = expand_builtin_sync_operation (mode, arglist, IOR,
6394 false, target, ignore);
6399 case BUILT_IN_FETCH_AND_AND_1:
6400 case BUILT_IN_FETCH_AND_AND_2:
6401 case BUILT_IN_FETCH_AND_AND_4:
6402 case BUILT_IN_FETCH_AND_AND_8:
6403 case BUILT_IN_FETCH_AND_AND_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6405 target = expand_builtin_sync_operation (mode, arglist, AND,
6406 false, target, ignore);
6411 case BUILT_IN_FETCH_AND_XOR_1:
6412 case BUILT_IN_FETCH_AND_XOR_2:
6413 case BUILT_IN_FETCH_AND_XOR_4:
6414 case BUILT_IN_FETCH_AND_XOR_8:
6415 case BUILT_IN_FETCH_AND_XOR_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6417 target = expand_builtin_sync_operation (mode, arglist, XOR,
6418 false, target, ignore);
6423 case BUILT_IN_FETCH_AND_NAND_1:
6424 case BUILT_IN_FETCH_AND_NAND_2:
6425 case BUILT_IN_FETCH_AND_NAND_4:
6426 case BUILT_IN_FETCH_AND_NAND_8:
6427 case BUILT_IN_FETCH_AND_NAND_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6429 target = expand_builtin_sync_operation (mode, arglist, NOT,
6430 false, target, ignore);
6435 case BUILT_IN_ADD_AND_FETCH_1:
6436 case BUILT_IN_ADD_AND_FETCH_2:
6437 case BUILT_IN_ADD_AND_FETCH_4:
6438 case BUILT_IN_ADD_AND_FETCH_8:
6439 case BUILT_IN_ADD_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6442 true, target, ignore);
6447 case BUILT_IN_SUB_AND_FETCH_1:
6448 case BUILT_IN_SUB_AND_FETCH_2:
6449 case BUILT_IN_SUB_AND_FETCH_4:
6450 case BUILT_IN_SUB_AND_FETCH_8:
6451 case BUILT_IN_SUB_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6454 true, target, ignore);
6459 case BUILT_IN_OR_AND_FETCH_1:
6460 case BUILT_IN_OR_AND_FETCH_2:
6461 case BUILT_IN_OR_AND_FETCH_4:
6462 case BUILT_IN_OR_AND_FETCH_8:
6463 case BUILT_IN_OR_AND_FETCH_16:
6464 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6465 target = expand_builtin_sync_operation (mode, arglist, IOR,
6466 true, target, ignore);
6471 case BUILT_IN_AND_AND_FETCH_1:
6472 case BUILT_IN_AND_AND_FETCH_2:
6473 case BUILT_IN_AND_AND_FETCH_4:
6474 case BUILT_IN_AND_AND_FETCH_8:
6475 case BUILT_IN_AND_AND_FETCH_16:
6476 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6477 target = expand_builtin_sync_operation (mode, arglist, AND,
6478 true, target, ignore);
6483 case BUILT_IN_XOR_AND_FETCH_1:
6484 case BUILT_IN_XOR_AND_FETCH_2:
6485 case BUILT_IN_XOR_AND_FETCH_4:
6486 case BUILT_IN_XOR_AND_FETCH_8:
6487 case BUILT_IN_XOR_AND_FETCH_16:
6488 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6489 target = expand_builtin_sync_operation (mode, arglist, XOR,
6490 true, target, ignore);
6495 case BUILT_IN_NAND_AND_FETCH_1:
6496 case BUILT_IN_NAND_AND_FETCH_2:
6497 case BUILT_IN_NAND_AND_FETCH_4:
6498 case BUILT_IN_NAND_AND_FETCH_8:
6499 case BUILT_IN_NAND_AND_FETCH_16:
6500 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6501 target = expand_builtin_sync_operation (mode, arglist, NOT,
6502 true, target, ignore);
6507 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6508 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6509 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6510 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6511 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6512 if (mode == VOIDmode)
6513 mode = TYPE_MODE (boolean_type_node);
6514 if (!target || !register_operand (target, mode))
6515 target = gen_reg_rtx (mode);
6517 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6518 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6523 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6524 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6525 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6526 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6527 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6528 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6529 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6534 case BUILT_IN_LOCK_TEST_AND_SET_1:
6535 case BUILT_IN_LOCK_TEST_AND_SET_2:
6536 case BUILT_IN_LOCK_TEST_AND_SET_4:
6537 case BUILT_IN_LOCK_TEST_AND_SET_8:
6538 case BUILT_IN_LOCK_TEST_AND_SET_16:
6539 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6540 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6545 case BUILT_IN_LOCK_RELEASE_1:
6546 case BUILT_IN_LOCK_RELEASE_2:
6547 case BUILT_IN_LOCK_RELEASE_4:
6548 case BUILT_IN_LOCK_RELEASE_8:
6549 case BUILT_IN_LOCK_RELEASE_16:
6550 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6551 expand_builtin_lock_release (mode, arglist);
6554 case BUILT_IN_SYNCHRONIZE:
6555 expand_builtin_synchronize ();
6558 case BUILT_IN_OBJECT_SIZE:
6559 return expand_builtin_object_size (exp);
6561 case BUILT_IN_MEMCPY_CHK:
6562 case BUILT_IN_MEMPCPY_CHK:
6563 case BUILT_IN_MEMMOVE_CHK:
6564 case BUILT_IN_MEMSET_CHK:
6565 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6570 case BUILT_IN_STRCPY_CHK:
6571 case BUILT_IN_STPCPY_CHK:
6572 case BUILT_IN_STRNCPY_CHK:
6573 case BUILT_IN_STRCAT_CHK:
6574 case BUILT_IN_STRNCAT_CHK:
6575 case BUILT_IN_SNPRINTF_CHK:
6576 case BUILT_IN_VSNPRINTF_CHK:
6577 maybe_emit_chk_warning (exp, fcode);
6580 case BUILT_IN_SPRINTF_CHK:
6581 case BUILT_IN_VSPRINTF_CHK:
6582 maybe_emit_sprintf_chk_warning (exp, fcode);
6585 default: /* just do library call, if unknown builtin */
6589 /* The switch statement above can drop through to cause the function
6590 to be called normally. */
6591 return expand_call (exp, target, ignore);
6594 /* Determine whether a tree node represents a call to a built-in
6595 function. If the tree T is a call to a built-in function with
6596 the right number of arguments of the appropriate types, return
6597 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6598 Otherwise the return value is END_BUILTINS. */
6600 enum built_in_function
6601 builtin_mathfn_code (tree t)
6603 tree fndecl, arglist, parmlist;
6604 tree argtype, parmtype;
6606 if (TREE_CODE (t) != CALL_EXPR
6607 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6608 return END_BUILTINS;
6610 fndecl = get_callee_fndecl (t);
6611 if (fndecl == NULL_TREE
6612 || TREE_CODE (fndecl) != FUNCTION_DECL
6613 || ! DECL_BUILT_IN (fndecl)
6614 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6615 return END_BUILTINS;
6617 arglist = TREE_OPERAND (t, 1);
6618 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6619 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6621 /* If a function doesn't take a variable number of arguments,
6622 the last element in the list will have type `void'. */
6623 parmtype = TREE_VALUE (parmlist);
6624 if (VOID_TYPE_P (parmtype))
6627 return END_BUILTINS;
6628 return DECL_FUNCTION_CODE (fndecl);
6632 return END_BUILTINS;
6634 argtype = TREE_TYPE (TREE_VALUE (arglist));
6636 if (SCALAR_FLOAT_TYPE_P (parmtype))
6638 if (! SCALAR_FLOAT_TYPE_P (argtype))
6639 return END_BUILTINS;
6641 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6643 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6644 return END_BUILTINS;
6646 else if (POINTER_TYPE_P (parmtype))
6648 if (! POINTER_TYPE_P (argtype))
6649 return END_BUILTINS;
6651 else if (INTEGRAL_TYPE_P (parmtype))
6653 if (! INTEGRAL_TYPE_P (argtype))
6654 return END_BUILTINS;
6657 return END_BUILTINS;
6659 arglist = TREE_CHAIN (arglist);
6662 /* Variable-length argument list. */
6663 return DECL_FUNCTION_CODE (fndecl);
6666 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6667 constant. ARGLIST is the argument list of the call. */
6670 fold_builtin_constant_p (tree arglist)
6675 arglist = TREE_VALUE (arglist);
6677 /* We return 1 for a numeric type that's known to be a constant
6678 value at compile-time or for an aggregate type that's a
6679 literal constant. */
6680 STRIP_NOPS (arglist);
6682 /* If we know this is a constant, emit the constant of one. */
6683 if (CONSTANT_CLASS_P (arglist)
6684 || (TREE_CODE (arglist) == CONSTRUCTOR
6685 && TREE_CONSTANT (arglist)))
6686 return integer_one_node;
6687 if (TREE_CODE (arglist) == ADDR_EXPR)
6689 tree op = TREE_OPERAND (arglist, 0);
6690 if (TREE_CODE (op) == STRING_CST
6691 || (TREE_CODE (op) == ARRAY_REF
6692 && integer_zerop (TREE_OPERAND (op, 1))
6693 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6694 return integer_one_node;
6697 /* If this expression has side effects, show we don't know it to be a
6698 constant. Likewise if it's a pointer or aggregate type since in
6699 those case we only want literals, since those are only optimized
6700 when generating RTL, not later.
6701 And finally, if we are compiling an initializer, not code, we
6702 need to return a definite result now; there's not going to be any
6703 more optimization done. */
6704 if (TREE_SIDE_EFFECTS (arglist)
6705 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6706 || POINTER_TYPE_P (TREE_TYPE (arglist))
6708 || folding_initializer)
6709 return integer_zero_node;
6714 /* Fold a call to __builtin_expect, if we expect that a comparison against
6715 the argument will fold to a constant. In practice, this means a true
6716 constant or the address of a non-weak symbol. ARGLIST is the argument
6717 list of the call. */
6720 fold_builtin_expect (tree arglist)
6727 arg = TREE_VALUE (arglist);
6729 /* If the argument isn't invariant, then there's nothing we can do. */
6730 if (!TREE_INVARIANT (arg))
6733 /* If we're looking at an address of a weak decl, then do not fold. */
6736 if (TREE_CODE (inner) == ADDR_EXPR)
6740 inner = TREE_OPERAND (inner, 0);
6742 while (TREE_CODE (inner) == COMPONENT_REF
6743 || TREE_CODE (inner) == ARRAY_REF);
6744 if (DECL_P (inner) && DECL_WEAK (inner))
6748 /* Otherwise, ARG already has the proper type for the return value. */
6752 /* Fold a call to __builtin_classify_type. */
6755 fold_builtin_classify_type (tree arglist)
6758 return build_int_cst (NULL_TREE, no_type_class);
6760 return build_int_cst (NULL_TREE,
6761 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6764 /* Fold a call to __builtin_strlen. */
6767 fold_builtin_strlen (tree arglist)
6769 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6773 tree len = c_strlen (TREE_VALUE (arglist), 0);
6777 /* Convert from the internal "sizetype" type to "size_t". */
6779 len = fold_convert (size_type_node, len);
6787 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6790 fold_builtin_inf (tree type, int warn)
6792 REAL_VALUE_TYPE real;
6794 /* __builtin_inff is intended to be usable to define INFINITY on all
6795 targets. If an infinity is not available, INFINITY expands "to a
6796 positive constant of type float that overflows at translation
6797 time", footnote "In this case, using INFINITY will violate the
6798 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6799 Thus we pedwarn to ensure this constraint violation is
6801 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6802 pedwarn ("target format does not support infinity");
6805 return build_real (type, real);
6808 /* Fold a call to __builtin_nan or __builtin_nans. */
6811 fold_builtin_nan (tree arglist, tree type, int quiet)
6813 REAL_VALUE_TYPE real;
6816 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6818 str = c_getstr (TREE_VALUE (arglist));
6822 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6825 return build_real (type, real);
6828 /* Return true if the floating point expression T has an integer value.
6829 We also allow +Inf, -Inf and NaN to be considered integer values. */
6832 integer_valued_real_p (tree t)
6834 switch (TREE_CODE (t))
6841 case NON_LVALUE_EXPR:
6842 return integer_valued_real_p (TREE_OPERAND (t, 0));
6845 case GIMPLE_MODIFY_STMT:
6847 return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1));
6854 return integer_valued_real_p (TREE_OPERAND (t, 0))
6855 && integer_valued_real_p (TREE_OPERAND (t, 1));
6858 return integer_valued_real_p (TREE_OPERAND (t, 1))
6859 && integer_valued_real_p (TREE_OPERAND (t, 2));
6862 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6866 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6867 if (TREE_CODE (type) == INTEGER_TYPE)
6869 if (TREE_CODE (type) == REAL_TYPE)
6870 return integer_valued_real_p (TREE_OPERAND (t, 0));
6875 switch (builtin_mathfn_code (t))
6877 CASE_FLT_FN (BUILT_IN_CEIL):
6878 CASE_FLT_FN (BUILT_IN_FLOOR):
6879 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6880 CASE_FLT_FN (BUILT_IN_RINT):
6881 CASE_FLT_FN (BUILT_IN_ROUND):
6882 CASE_FLT_FN (BUILT_IN_TRUNC):
6885 CASE_FLT_FN (BUILT_IN_FMIN):
6886 CASE_FLT_FN (BUILT_IN_FMAX):
6887 return integer_valued_real_p (TREE_VALUE (TREE_OPERAND (t, 1)))
6888 && integer_valued_real_p (TREE_VALUE (TREE_CHAIN (TREE_OPERAND (t, 1))));
6901 /* EXP is assumed to be builtin call where truncation can be propagated
6902 across (for instance floor((double)f) == (double)floorf (f).
6903 Do the transformation. */
6906 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6908 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6911 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6914 arg = TREE_VALUE (arglist);
6915 /* Integer rounding functions are idempotent. */
6916 if (fcode == builtin_mathfn_code (arg))
6919 /* If argument is already integer valued, and we don't need to worry
6920 about setting errno, there's no need to perform rounding. */
6921 if (! flag_errno_math && integer_valued_real_p (arg))
6926 tree arg0 = strip_float_extensions (arg);
6927 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6928 tree newtype = TREE_TYPE (arg0);
6931 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6932 && (decl = mathfn_built_in (newtype, fcode)))
6935 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6936 return fold_convert (ftype,
6937 build_function_call_expr (decl, arglist));
6943 /* EXP is assumed to be builtin call which can narrow the FP type of
6944 the argument, for instance lround((double)f) -> lroundf (f). */
6947 fold_fixed_mathfn (tree fndecl, tree arglist)
6949 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6952 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6955 arg = TREE_VALUE (arglist);
6957 /* If argument is already integer valued, and we don't need to worry
6958 about setting errno, there's no need to perform rounding. */
6959 if (! flag_errno_math && integer_valued_real_p (arg))
6960 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6964 tree ftype = TREE_TYPE (arg);
6965 tree arg0 = strip_float_extensions (arg);
6966 tree newtype = TREE_TYPE (arg0);
6969 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6970 && (decl = mathfn_built_in (newtype, fcode)))
6973 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6974 return build_function_call_expr (decl, arglist);
6978 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6979 sizeof (long long) == sizeof (long). */
6980 if (TYPE_PRECISION (long_long_integer_type_node)
6981 == TYPE_PRECISION (long_integer_type_node))
6983 tree newfn = NULL_TREE;
6986 CASE_FLT_FN (BUILT_IN_LLCEIL):
6987 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6990 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6991 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6994 CASE_FLT_FN (BUILT_IN_LLROUND):
6995 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6998 CASE_FLT_FN (BUILT_IN_LLRINT):
6999 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7008 tree newcall = build_function_call_expr (newfn, arglist);
7009 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7016 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
7017 is the argument list, TYPE is the return type and FNDECL is the
7018 original function DECL. Return NULL_TREE if no if no simplification
7022 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
7026 if (!arglist || TREE_CHAIN (arglist))
7029 arg = TREE_VALUE (arglist);
7030 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
7031 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7034 /* Evaluate cabs of a constant at compile-time. */
7035 if (flag_unsafe_math_optimizations
7036 && TREE_CODE (arg) == COMPLEX_CST
7037 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
7038 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
7039 && !TREE_OVERFLOW (TREE_REALPART (arg))
7040 && !TREE_OVERFLOW (TREE_IMAGPART (arg)))
7042 REAL_VALUE_TYPE r, i;
7044 r = TREE_REAL_CST (TREE_REALPART (arg));
7045 i = TREE_REAL_CST (TREE_IMAGPART (arg));
7047 real_arithmetic (&r, MULT_EXPR, &r, &r);
7048 real_arithmetic (&i, MULT_EXPR, &i, &i);
7049 real_arithmetic (&r, PLUS_EXPR, &r, &i);
7050 if (real_sqrt (&r, TYPE_MODE (type), &r)
7051 || ! flag_trapping_math)
7052 return build_real (type, r);
7055 /* If either part is zero, cabs is fabs of the other. */
7056 if (TREE_CODE (arg) == COMPLEX_EXPR
7057 && real_zerop (TREE_OPERAND (arg, 0)))
7058 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
7059 if (TREE_CODE (arg) == COMPLEX_EXPR
7060 && real_zerop (TREE_OPERAND (arg, 1)))
7061 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
7063 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7064 if (TREE_CODE (arg) == NEGATE_EXPR
7065 || TREE_CODE (arg) == CONJ_EXPR)
7067 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
7068 return build_function_call_expr (fndecl, arglist);
7071 /* Don't do this when optimizing for size. */
7072 if (flag_unsafe_math_optimizations
7073 && optimize && !optimize_size)
7075 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7077 if (sqrtfn != NULL_TREE)
7079 tree rpart, ipart, result, arglist;
7081 arg = builtin_save_expr (arg);
7083 rpart = fold_build1 (REALPART_EXPR, type, arg);
7084 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
7086 rpart = builtin_save_expr (rpart);
7087 ipart = builtin_save_expr (ipart);
7089 result = fold_build2 (PLUS_EXPR, type,
7090 fold_build2 (MULT_EXPR, type,
7092 fold_build2 (MULT_EXPR, type,
7095 arglist = build_tree_list (NULL_TREE, result);
7096 return build_function_call_expr (sqrtfn, arglist);
7103 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
7104 NULL_TREE if no simplification can be made. */
7107 fold_builtin_sqrt (tree arglist, tree type)
7110 enum built_in_function fcode;
7111 tree arg = TREE_VALUE (arglist);
7113 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7116 /* Optimize sqrt of constant value. */
7117 if (TREE_CODE (arg) == REAL_CST
7118 && !TREE_OVERFLOW (arg))
7120 REAL_VALUE_TYPE r, x;
7122 x = TREE_REAL_CST (arg);
7123 if (real_sqrt (&r, TYPE_MODE (type), &x)
7124 || (!flag_trapping_math && !flag_errno_math))
7125 return build_real (type, r);
7128 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7129 fcode = builtin_mathfn_code (arg);
7130 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7132 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7133 arg = fold_build2 (MULT_EXPR, type,
7134 TREE_VALUE (TREE_OPERAND (arg, 1)),
7135 build_real (type, dconsthalf));
7136 arglist = build_tree_list (NULL_TREE, arg);
7137 return build_function_call_expr (expfn, arglist);
7140 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7141 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7143 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7147 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7149 /* The inner root was either sqrt or cbrt. */
7150 REAL_VALUE_TYPE dconstroot =
7151 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7153 /* Adjust for the outer root. */
7154 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7155 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7156 tree_root = build_real (type, dconstroot);
7157 arglist = tree_cons (NULL_TREE, arg0,
7158 build_tree_list (NULL_TREE, tree_root));
7159 return build_function_call_expr (powfn, arglist);
7163 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7164 if (flag_unsafe_math_optimizations
7165 && (fcode == BUILT_IN_POW
7166 || fcode == BUILT_IN_POWF
7167 || fcode == BUILT_IN_POWL))
7169 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7170 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7171 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7173 if (!tree_expr_nonnegative_p (arg0))
7174 arg0 = build1 (ABS_EXPR, type, arg0);
7175 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7176 build_real (type, dconsthalf));
7177 arglist = tree_cons (NULL_TREE, arg0,
7178 build_tree_list (NULL_TREE, narg1));
7179 return build_function_call_expr (powfn, arglist);
7185 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7186 NULL_TREE if no simplification can be made. */
7188 fold_builtin_cbrt (tree arglist, tree type)
7190 tree arg = TREE_VALUE (arglist);
7191 const enum built_in_function fcode = builtin_mathfn_code (arg);
7194 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7197 /* Calculate the result when the argument is a constant. */
7198 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7201 if (flag_unsafe_math_optimizations)
7203 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7204 if (BUILTIN_EXPONENT_P (fcode))
7206 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7207 const REAL_VALUE_TYPE third_trunc =
7208 real_value_truncate (TYPE_MODE (type), dconstthird);
7209 arg = fold_build2 (MULT_EXPR, type,
7210 TREE_VALUE (TREE_OPERAND (arg, 1)),
7211 build_real (type, third_trunc));
7212 arglist = build_tree_list (NULL_TREE, arg);
7213 return build_function_call_expr (expfn, arglist);
7216 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7217 if (BUILTIN_SQRT_P (fcode))
7219 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7223 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7225 REAL_VALUE_TYPE dconstroot = dconstthird;
7227 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7228 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7229 tree_root = build_real (type, dconstroot);
7230 arglist = tree_cons (NULL_TREE, arg0,
7231 build_tree_list (NULL_TREE, tree_root));
7232 return build_function_call_expr (powfn, arglist);
7236 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7237 if (BUILTIN_CBRT_P (fcode))
7239 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7240 if (tree_expr_nonnegative_p (arg0))
7242 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7247 REAL_VALUE_TYPE dconstroot;
7249 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7250 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7251 tree_root = build_real (type, dconstroot);
7252 arglist = tree_cons (NULL_TREE, arg0,
7253 build_tree_list (NULL_TREE, tree_root));
7254 return build_function_call_expr (powfn, arglist);
7259 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7260 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7261 || fcode == BUILT_IN_POWL)
7263 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7264 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7265 if (tree_expr_nonnegative_p (arg00))
7267 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7268 const REAL_VALUE_TYPE dconstroot
7269 = real_value_truncate (TYPE_MODE (type), dconstthird);
7270 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7271 build_real (type, dconstroot));
7272 arglist = tree_cons (NULL_TREE, arg00,
7273 build_tree_list (NULL_TREE, narg01));
7274 return build_function_call_expr (powfn, arglist);
7281 /* Fold function call to builtin cos, cosf, or cosl. Return
7282 NULL_TREE if no simplification can be made. */
7284 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7286 tree arg = TREE_VALUE (arglist);
7289 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7292 /* Calculate the result when the argument is a constant. */
7293 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7296 /* Optimize cos(-x) into cos (x). */
7297 if ((narg = fold_strip_sign_ops (arg)))
7298 return build_function_call_expr (fndecl,
7299 build_tree_list (NULL_TREE, narg));
7304 /* Fold function call to builtin cosh, coshf, or coshl. Return
7305 NULL_TREE if no simplification can be made. */
7307 fold_builtin_cosh (tree arglist, tree type, tree fndecl)
7309 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7311 tree arg = TREE_VALUE (arglist);
7314 /* Calculate the result when the argument is a constant. */
7315 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7318 /* Optimize cosh(-x) into cosh (x). */
7319 if ((narg = fold_strip_sign_ops (arg)))
7320 return build_function_call_expr (fndecl,
7321 build_tree_list (NULL_TREE, narg));
7327 /* Fold function call to builtin tan, tanf, or tanl. Return
7328 NULL_TREE if no simplification can be made. */
7330 fold_builtin_tan (tree arglist, tree type)
7332 enum built_in_function fcode;
7333 tree arg = TREE_VALUE (arglist);
7336 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7339 /* Calculate the result when the argument is a constant. */
7340 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7343 /* Optimize tan(atan(x)) = x. */
7344 fcode = builtin_mathfn_code (arg);
7345 if (flag_unsafe_math_optimizations
7346 && (fcode == BUILT_IN_ATAN
7347 || fcode == BUILT_IN_ATANF
7348 || fcode == BUILT_IN_ATANL))
7349 return TREE_VALUE (TREE_OPERAND (arg, 1));
7354 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7355 NULL_TREE if no simplification can be made. */
7358 fold_builtin_sincos (tree arglist)
7360 tree type, arg0, arg1, arg2;
7363 if (!validate_arglist (arglist, REAL_TYPE, POINTER_TYPE,
7364 POINTER_TYPE, VOID_TYPE))
7367 arg0 = TREE_VALUE (arglist);
7368 type = TREE_TYPE (arg0);
7369 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7370 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7372 /* Calculate the result when the argument is a constant. */
7373 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7376 /* Canonicalize sincos to cexpi. */
7377 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7381 call = build_function_call_expr (fn, build_tree_list (NULL_TREE, arg0));
7382 call = builtin_save_expr (call);
7384 return build2 (COMPOUND_EXPR, type,
7385 build2 (MODIFY_EXPR, void_type_node,
7386 build_fold_indirect_ref (arg1),
7387 build1 (IMAGPART_EXPR, type, call)),
7388 build2 (MODIFY_EXPR, void_type_node,
7389 build_fold_indirect_ref (arg2),
7390 build1 (REALPART_EXPR, type, call)));
7393 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7394 NULL_TREE if no simplification can be made. */
7397 fold_builtin_cexp (tree arglist, tree type)
7400 tree realp, imagp, ifn;
7402 if (!validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
7405 arg0 = TREE_VALUE (arglist);
7406 rtype = TREE_TYPE (TREE_TYPE (arg0));
7408 /* In case we can figure out the real part of arg0 and it is constant zero
7410 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7414 if ((realp = fold_unary (REALPART_EXPR, rtype, arg0))
7415 && real_zerop (realp))
7417 tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0);
7418 return build_function_call_expr (ifn, build_tree_list (NULL_TREE, narg));
7421 /* In case we can easily decompose real and imaginary parts split cexp
7422 to exp (r) * cexpi (i). */
7423 if (flag_unsafe_math_optimizations
7426 tree rfn, rcall, icall;
7428 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7432 imagp = fold_unary (IMAGPART_EXPR, rtype, arg0);
7436 icall = build_function_call_expr (ifn,
7437 build_tree_list (NULL_TREE, imagp));
7438 icall = builtin_save_expr (icall);
7439 rcall = build_function_call_expr (rfn,
7440 build_tree_list (NULL_TREE, realp));
7441 rcall = builtin_save_expr (rcall);
7442 return build2 (COMPLEX_EXPR, type,
7443 build2 (MULT_EXPR, rtype,
7445 build1 (REALPART_EXPR, rtype, icall)),
7446 build2 (MULT_EXPR, rtype,
7448 build1 (IMAGPART_EXPR, rtype, icall)));
7454 /* Fold function call to builtin trunc, truncf or truncl. Return
7455 NULL_TREE if no simplification can be made. */
7458 fold_builtin_trunc (tree fndecl, tree arglist)
7462 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7465 /* Optimize trunc of constant value. */
7466 arg = TREE_VALUE (arglist);
7467 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7469 REAL_VALUE_TYPE r, x;
7470 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7472 x = TREE_REAL_CST (arg);
7473 real_trunc (&r, TYPE_MODE (type), &x);
7474 return build_real (type, r);
7477 return fold_trunc_transparent_mathfn (fndecl, arglist);
7480 /* Fold function call to builtin floor, floorf or floorl. Return
7481 NULL_TREE if no simplification can be made. */
7484 fold_builtin_floor (tree fndecl, tree arglist)
7488 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7491 /* Optimize floor of constant value. */
7492 arg = TREE_VALUE (arglist);
7493 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7497 x = TREE_REAL_CST (arg);
7498 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7500 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7503 real_floor (&r, TYPE_MODE (type), &x);
7504 return build_real (type, r);
7508 /* Fold floor (x) where x is nonnegative to trunc (x). */
7509 if (tree_expr_nonnegative_p (arg))
7511 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7513 return build_function_call_expr (truncfn, arglist);
7516 return fold_trunc_transparent_mathfn (fndecl, arglist);
7519 /* Fold function call to builtin ceil, ceilf or ceill. Return
7520 NULL_TREE if no simplification can be made. */
7523 fold_builtin_ceil (tree fndecl, tree arglist)
7527 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7530 /* Optimize ceil of constant value. */
7531 arg = TREE_VALUE (arglist);
7532 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7536 x = TREE_REAL_CST (arg);
7537 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7539 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7542 real_ceil (&r, TYPE_MODE (type), &x);
7543 return build_real (type, r);
7547 return fold_trunc_transparent_mathfn (fndecl, arglist);
7550 /* Fold function call to builtin round, roundf or roundl. Return
7551 NULL_TREE if no simplification can be made. */
7554 fold_builtin_round (tree fndecl, tree arglist)
7558 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7561 /* Optimize round of constant value. */
7562 arg = TREE_VALUE (arglist);
7563 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7567 x = TREE_REAL_CST (arg);
7568 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7570 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7573 real_round (&r, TYPE_MODE (type), &x);
7574 return build_real (type, r);
7578 return fold_trunc_transparent_mathfn (fndecl, arglist);
7581 /* Fold function call to builtin lround, lroundf or lroundl (or the
7582 corresponding long long versions) and other rounding functions.
7583 Return NULL_TREE if no simplification can be made. */
7586 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7590 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7593 /* Optimize lround of constant value. */
7594 arg = TREE_VALUE (arglist);
7595 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7597 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7599 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7601 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7602 tree ftype = TREE_TYPE (arg);
7603 unsigned HOST_WIDE_INT lo2;
7604 HOST_WIDE_INT hi, lo;
7607 switch (DECL_FUNCTION_CODE (fndecl))
7609 CASE_FLT_FN (BUILT_IN_LFLOOR):
7610 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7611 real_floor (&r, TYPE_MODE (ftype), &x);
7614 CASE_FLT_FN (BUILT_IN_LCEIL):
7615 CASE_FLT_FN (BUILT_IN_LLCEIL):
7616 real_ceil (&r, TYPE_MODE (ftype), &x);
7619 CASE_FLT_FN (BUILT_IN_LROUND):
7620 CASE_FLT_FN (BUILT_IN_LLROUND):
7621 real_round (&r, TYPE_MODE (ftype), &x);
7628 REAL_VALUE_TO_INT (&lo, &hi, r);
7629 if (!fit_double_type (lo, hi, &lo2, &hi, itype))
7630 return build_int_cst_wide (itype, lo2, hi);
7634 switch (DECL_FUNCTION_CODE (fndecl))
7636 CASE_FLT_FN (BUILT_IN_LFLOOR):
7637 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7638 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7639 if (tree_expr_nonnegative_p (arg))
7640 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)),
7646 return fold_fixed_mathfn (fndecl, arglist);
7649 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7650 and their long and long long variants (i.e. ffsl and ffsll).
7651 Return NULL_TREE if no simplification can be made. */
7654 fold_builtin_bitop (tree fndecl, tree arglist)
7658 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7661 /* Optimize for constant argument. */
7662 arg = TREE_VALUE (arglist);
7663 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7665 HOST_WIDE_INT hi, width, result;
7666 unsigned HOST_WIDE_INT lo;
7669 type = TREE_TYPE (arg);
7670 width = TYPE_PRECISION (type);
7671 lo = TREE_INT_CST_LOW (arg);
7673 /* Clear all the bits that are beyond the type's precision. */
7674 if (width > HOST_BITS_PER_WIDE_INT)
7676 hi = TREE_INT_CST_HIGH (arg);
7677 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7678 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7683 if (width < HOST_BITS_PER_WIDE_INT)
7684 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7687 switch (DECL_FUNCTION_CODE (fndecl))
7689 CASE_INT_FN (BUILT_IN_FFS):
7691 result = exact_log2 (lo & -lo) + 1;
7693 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7698 CASE_INT_FN (BUILT_IN_CLZ):
7700 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7702 result = width - floor_log2 (lo) - 1;
7703 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7707 CASE_INT_FN (BUILT_IN_CTZ):
7709 result = exact_log2 (lo & -lo);
7711 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7712 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7716 CASE_INT_FN (BUILT_IN_POPCOUNT):
7719 result++, lo &= lo - 1;
7721 result++, hi &= hi - 1;
7724 CASE_INT_FN (BUILT_IN_PARITY):
7727 result++, lo &= lo - 1;
7729 result++, hi &= hi - 1;
7737 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7743 /* Fold function call to builtin_bswap and the long and long long
7744 variants. Return NULL_TREE if no simplification can be made. */
7746 fold_builtin_bswap (tree fndecl, tree arglist)
7750 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7753 /* Optimize constant value. */
7754 arg = TREE_VALUE (arglist);
7755 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7757 HOST_WIDE_INT hi, width, r_hi = 0;
7758 unsigned HOST_WIDE_INT lo, r_lo = 0;
7761 type = TREE_TYPE (arg);
7762 width = TYPE_PRECISION (type);
7763 lo = TREE_INT_CST_LOW (arg);
7764 hi = TREE_INT_CST_HIGH (arg);
7766 switch (DECL_FUNCTION_CODE (fndecl))
7768 case BUILT_IN_BSWAP32:
7769 case BUILT_IN_BSWAP64:
7773 for (s = 0; s < width; s += 8)
7775 int d = width - s - 8;
7776 unsigned HOST_WIDE_INT byte;
7778 if (s < HOST_BITS_PER_WIDE_INT)
7779 byte = (lo >> s) & 0xff;
7781 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7783 if (d < HOST_BITS_PER_WIDE_INT)
7786 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7796 if (width < HOST_BITS_PER_WIDE_INT)
7797 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7799 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7804 /* Return true if EXPR is the real constant contained in VALUE. */
7807 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7811 return ((TREE_CODE (expr) == REAL_CST
7812 && !TREE_OVERFLOW (expr)
7813 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7814 || (TREE_CODE (expr) == COMPLEX_CST
7815 && real_dconstp (TREE_REALPART (expr), value)
7816 && real_zerop (TREE_IMAGPART (expr))));
7819 /* A subroutine of fold_builtin to fold the various logarithmic
7820 functions. Return NULL_TREE if no simplification can me made.
7821 FUNC is the corresponding MPFR logarithm function. */
7824 fold_builtin_logarithm (tree fndecl, tree arglist,
7825 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7827 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7829 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7830 tree arg = TREE_VALUE (arglist);
7832 const enum built_in_function fcode = builtin_mathfn_code (arg);
7834 /* Optimize log(e) = 1.0. We're never passed an exact 'e',
7835 instead we'll look for 'e' truncated to MODE. So only do
7836 this if flag_unsafe_math_optimizations is set. */
7837 if (flag_unsafe_math_optimizations && func == mpfr_log)
7839 const REAL_VALUE_TYPE e_truncated =
7840 real_value_truncate (TYPE_MODE (type), dconste);
7841 if (real_dconstp (arg, &e_truncated))
7842 return build_real (type, dconst1);
7845 /* Calculate the result when the argument is a constant. */
7846 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7849 /* Special case, optimize logN(expN(x)) = x. */
7850 if (flag_unsafe_math_optimizations
7851 && ((func == mpfr_log
7852 && (fcode == BUILT_IN_EXP
7853 || fcode == BUILT_IN_EXPF
7854 || fcode == BUILT_IN_EXPL))
7855 || (func == mpfr_log2
7856 && (fcode == BUILT_IN_EXP2
7857 || fcode == BUILT_IN_EXP2F
7858 || fcode == BUILT_IN_EXP2L))
7859 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7860 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7862 /* Optimize logN(func()) for various exponential functions. We
7863 want to determine the value "x" and the power "exponent" in
7864 order to transform logN(x**exponent) into exponent*logN(x). */
7865 if (flag_unsafe_math_optimizations)
7867 tree exponent = 0, x = 0;
7871 CASE_FLT_FN (BUILT_IN_EXP):
7872 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7873 x = build_real (type,
7874 real_value_truncate (TYPE_MODE (type), dconste));
7875 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7877 CASE_FLT_FN (BUILT_IN_EXP2):
7878 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7879 x = build_real (type, dconst2);
7880 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7882 CASE_FLT_FN (BUILT_IN_EXP10):
7883 CASE_FLT_FN (BUILT_IN_POW10):
7884 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7885 x = build_real (type, dconst10);
7886 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7888 CASE_FLT_FN (BUILT_IN_SQRT):
7889 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7890 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7891 exponent = build_real (type, dconsthalf);
7893 CASE_FLT_FN (BUILT_IN_CBRT):
7894 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7895 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7896 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7899 CASE_FLT_FN (BUILT_IN_POW):
7900 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7901 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7902 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7908 /* Now perform the optimization. */
7912 arglist = build_tree_list (NULL_TREE, x);
7913 logfn = build_function_call_expr (fndecl, arglist);
7914 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7922 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7923 NULL_TREE if no simplification can be made. */
7926 fold_builtin_hypot (tree fndecl, tree arglist, tree type)
7928 tree arg0 = TREE_VALUE (arglist);
7929 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7930 tree res, narg0, narg1;
7932 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7935 /* Calculate the result when the argument is a constant. */
7936 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7939 /* If either argument to hypot has a negate or abs, strip that off.
7940 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7941 narg0 = fold_strip_sign_ops (arg0);
7942 narg1 = fold_strip_sign_ops (arg1);
7945 tree narglist = tree_cons (NULL_TREE, narg0 ? narg0 : arg0,
7946 build_tree_list (NULL_TREE,
7947 narg1 ? narg1 : arg1));
7948 return build_function_call_expr (fndecl, narglist);
7951 /* If either argument is zero, hypot is fabs of the other. */
7952 if (real_zerop (arg0))
7953 return fold_build1 (ABS_EXPR, type, arg1);
7954 else if (real_zerop (arg1))
7955 return fold_build1 (ABS_EXPR, type, arg0);
7957 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7958 if (flag_unsafe_math_optimizations
7959 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7961 REAL_VALUE_TYPE sqrt2;
7963 real_sqrt (&sqrt2, TYPE_MODE (type), &dconst2);
7964 return fold_build2 (MULT_EXPR, type,
7965 fold_build1 (ABS_EXPR, type, arg0),
7966 build_real (type, sqrt2));
7973 /* Fold a builtin function call to pow, powf, or powl. Return
7974 NULL_TREE if no simplification can be made. */
7976 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7978 tree arg0 = TREE_VALUE (arglist);
7979 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7982 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7985 /* Calculate the result when the argument is a constant. */
7986 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7989 /* Optimize pow(1.0,y) = 1.0. */
7990 if (real_onep (arg0))
7991 return omit_one_operand (type, build_real (type, dconst1), arg1);
7993 if (TREE_CODE (arg1) == REAL_CST
7994 && !TREE_OVERFLOW (arg1))
7996 REAL_VALUE_TYPE cint;
8000 c = TREE_REAL_CST (arg1);
8002 /* Optimize pow(x,0.0) = 1.0. */
8003 if (REAL_VALUES_EQUAL (c, dconst0))
8004 return omit_one_operand (type, build_real (type, dconst1),
8007 /* Optimize pow(x,1.0) = x. */
8008 if (REAL_VALUES_EQUAL (c, dconst1))
8011 /* Optimize pow(x,-1.0) = 1.0/x. */
8012 if (REAL_VALUES_EQUAL (c, dconstm1))
8013 return fold_build2 (RDIV_EXPR, type,
8014 build_real (type, dconst1), arg0);
8016 /* Optimize pow(x,0.5) = sqrt(x). */
8017 if (flag_unsafe_math_optimizations
8018 && REAL_VALUES_EQUAL (c, dconsthalf))
8020 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8022 if (sqrtfn != NULL_TREE)
8024 tree arglist = build_tree_list (NULL_TREE, arg0);
8025 return build_function_call_expr (sqrtfn, arglist);
8029 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8030 if (flag_unsafe_math_optimizations)
8032 const REAL_VALUE_TYPE dconstroot
8033 = real_value_truncate (TYPE_MODE (type), dconstthird);
8035 if (REAL_VALUES_EQUAL (c, dconstroot))
8037 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8038 if (cbrtfn != NULL_TREE)
8040 tree arglist = build_tree_list (NULL_TREE, arg0);
8041 return build_function_call_expr (cbrtfn, arglist);
8046 /* Check for an integer exponent. */
8047 n = real_to_integer (&c);
8048 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8049 if (real_identical (&c, &cint))
8051 /* Attempt to evaluate pow at compile-time. */
8052 if (TREE_CODE (arg0) == REAL_CST
8053 && !TREE_OVERFLOW (arg0))
8058 x = TREE_REAL_CST (arg0);
8059 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8060 if (flag_unsafe_math_optimizations || !inexact)
8061 return build_real (type, x);
8064 /* Strip sign ops from even integer powers. */
8065 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8067 tree narg0 = fold_strip_sign_ops (arg0);
8070 arglist = build_tree_list (NULL_TREE, arg1);
8071 arglist = tree_cons (NULL_TREE, narg0, arglist);
8072 return build_function_call_expr (fndecl, arglist);
8078 if (flag_unsafe_math_optimizations)
8080 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8082 /* Optimize pow(expN(x),y) = expN(x*y). */
8083 if (BUILTIN_EXPONENT_P (fcode))
8085 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8086 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8087 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
8088 arglist = build_tree_list (NULL_TREE, arg);
8089 return build_function_call_expr (expfn, arglist);
8092 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8093 if (BUILTIN_SQRT_P (fcode))
8095 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8096 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8097 build_real (type, dconsthalf));
8099 arglist = tree_cons (NULL_TREE, narg0,
8100 build_tree_list (NULL_TREE, narg1));
8101 return build_function_call_expr (fndecl, arglist);
8104 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8105 if (BUILTIN_CBRT_P (fcode))
8107 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
8108 if (tree_expr_nonnegative_p (arg))
8110 const REAL_VALUE_TYPE dconstroot
8111 = real_value_truncate (TYPE_MODE (type), dconstthird);
8112 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
8113 build_real (type, dconstroot));
8114 arglist = tree_cons (NULL_TREE, arg,
8115 build_tree_list (NULL_TREE, narg1));
8116 return build_function_call_expr (fndecl, arglist);
8120 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
8121 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
8122 || fcode == BUILT_IN_POWL)
8124 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8125 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8126 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
8127 arglist = tree_cons (NULL_TREE, arg00,
8128 build_tree_list (NULL_TREE, narg1));
8129 return build_function_call_expr (fndecl, arglist);
8136 /* Fold a builtin function call to powi, powif, or powil. Return
8137 NULL_TREE if no simplification can be made. */
8139 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
8141 tree arg0 = TREE_VALUE (arglist);
8142 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8144 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
8147 /* Optimize pow(1.0,y) = 1.0. */
8148 if (real_onep (arg0))
8149 return omit_one_operand (type, build_real (type, dconst1), arg1);
8151 if (host_integerp (arg1, 0))
8153 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8155 /* Evaluate powi at compile-time. */
8156 if (TREE_CODE (arg0) == REAL_CST
8157 && !TREE_OVERFLOW (arg0))
8160 x = TREE_REAL_CST (arg0);
8161 real_powi (&x, TYPE_MODE (type), &x, c);
8162 return build_real (type, x);
8165 /* Optimize pow(x,0) = 1.0. */
8167 return omit_one_operand (type, build_real (type, dconst1),
8170 /* Optimize pow(x,1) = x. */
8174 /* Optimize pow(x,-1) = 1.0/x. */
8176 return fold_build2 (RDIV_EXPR, type,
8177 build_real (type, dconst1), arg0);
8183 /* A subroutine of fold_builtin to fold the various exponent
8184 functions. Return NULL_TREE if no simplification can me made.
8185 FUNC is the corresponding MPFR exponent function. */
8188 fold_builtin_exponent (tree fndecl, tree arglist,
8189 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8191 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8193 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8194 tree arg = TREE_VALUE (arglist);
8197 /* Calculate the result when the argument is a constant. */
8198 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8201 /* Optimize expN(logN(x)) = x. */
8202 if (flag_unsafe_math_optimizations)
8204 const enum built_in_function fcode = builtin_mathfn_code (arg);
8206 if ((func == mpfr_exp
8207 && (fcode == BUILT_IN_LOG
8208 || fcode == BUILT_IN_LOGF
8209 || fcode == BUILT_IN_LOGL))
8210 || (func == mpfr_exp2
8211 && (fcode == BUILT_IN_LOG2
8212 || fcode == BUILT_IN_LOG2F
8213 || fcode == BUILT_IN_LOG2L))
8214 || (func == mpfr_exp10
8215 && (fcode == BUILT_IN_LOG10
8216 || fcode == BUILT_IN_LOG10F
8217 || fcode == BUILT_IN_LOG10L)))
8218 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
8225 /* Return true if VAR is a VAR_DECL or a component thereof. */
8228 var_decl_component_p (tree var)
8231 while (handled_component_p (inner))
8232 inner = TREE_OPERAND (inner, 0);
8233 return SSA_VAR_P (inner);
8236 /* Fold function call to builtin memset. Return
8237 NULL_TREE if no simplification can be made. */
8240 fold_builtin_memset (tree arglist, tree type, bool ignore)
8242 tree dest, c, len, var, ret;
8243 unsigned HOST_WIDE_INT length, cval;
8245 if (!validate_arglist (arglist,
8246 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
8249 dest = TREE_VALUE (arglist);
8250 c = TREE_VALUE (TREE_CHAIN (arglist));
8251 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8253 if (! host_integerp (len, 1))
8256 /* If the LEN parameter is zero, return DEST. */
8257 if (integer_zerop (len))
8258 return omit_one_operand (type, dest, c);
8260 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
8265 if (TREE_CODE (var) != ADDR_EXPR)
8268 var = TREE_OPERAND (var, 0);
8269 if (TREE_THIS_VOLATILE (var))
8272 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
8273 && !POINTER_TYPE_P (TREE_TYPE (var)))
8276 if (! var_decl_component_p (var))
8279 length = tree_low_cst (len, 1);
8280 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
8281 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8285 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8288 if (integer_zerop (c))
8292 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8295 cval = tree_low_cst (c, 1);
8299 cval |= (cval << 31) << 1;
8302 ret = build_int_cst_type (TREE_TYPE (var), cval);
8303 ret = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, ret);
8307 return omit_one_operand (type, dest, ret);
8310 /* Fold function call to builtin memset. Return
8311 NULL_TREE if no simplification can be made. */
8314 fold_builtin_bzero (tree arglist, bool ignore)
8316 tree dest, size, newarglist;
8318 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8324 dest = TREE_VALUE (arglist);
8325 size = TREE_VALUE (TREE_CHAIN (arglist));
8327 /* New argument list transforming bzero(ptr x, int y) to
8328 memset(ptr x, int 0, size_t y). This is done this way
8329 so that if it isn't expanded inline, we fallback to
8330 calling bzero instead of memset. */
8332 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8333 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
8334 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8335 return fold_builtin_memset (newarglist, void_type_node, ignore);
8338 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8339 NULL_TREE if no simplification can be made.
8340 If ENDP is 0, return DEST (like memcpy).
8341 If ENDP is 1, return DEST+LEN (like mempcpy).
8342 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8343 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8347 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8349 tree dest, src, len, destvar, srcvar, expr;
8351 if (! validate_arglist (arglist,
8352 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8355 dest = TREE_VALUE (arglist);
8356 src = TREE_VALUE (TREE_CHAIN (arglist));
8357 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8359 /* If the LEN parameter is zero, return DEST. */
8360 if (integer_zerop (len))
8361 return omit_one_operand (type, dest, src);
8363 /* If SRC and DEST are the same (and not volatile), return
8364 DEST{,+LEN,+LEN-1}. */
8365 if (operand_equal_p (src, dest, 0))
8369 tree srctype, desttype;
8372 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
8373 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
8375 /* Both DEST and SRC must be pointer types.
8376 ??? This is what old code did. Is the testing for pointer types
8379 If either SRC is readonly or length is 1, we can use memcpy. */
8380 if (dest_align && src_align
8381 && (readonly_data_expr (src)
8382 || (host_integerp (len, 1)
8383 && (MIN (src_align, dest_align) / BITS_PER_UNIT >=
8384 tree_low_cst (len, 1)))))
8386 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8389 return build_function_call_expr (fn, arglist);
8394 if (!host_integerp (len, 0))
8397 This logic lose for arguments like (type *)malloc (sizeof (type)),
8398 since we strip the casts of up to VOID return value from malloc.
8399 Perhaps we ought to inherit type from non-VOID argument here? */
8402 srctype = TREE_TYPE (TREE_TYPE (src));
8403 desttype = TREE_TYPE (TREE_TYPE (dest));
8404 if (!srctype || !desttype
8405 || !TYPE_SIZE_UNIT (srctype)
8406 || !TYPE_SIZE_UNIT (desttype)
8407 || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST
8408 || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST
8409 || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
8410 || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8413 if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT)
8414 < (int) TYPE_ALIGN (desttype)
8415 || (get_pointer_alignment (src, BIGGEST_ALIGNMENT)
8416 < (int) TYPE_ALIGN (srctype)))
8420 dest = builtin_save_expr (dest);
8422 srcvar = build_fold_indirect_ref (src);
8423 if (TREE_THIS_VOLATILE (srcvar))
8425 if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len))
8427 /* With memcpy, it is possible to bypass aliasing rules, so without
8428 this check i. e. execute/20060930-2.c would be misoptimized, because
8429 it use conflicting alias set to hold argument for the memcpy call.
8430 This check is probably unnecesary with -fno-strict-aliasing.
8431 Similarly for destvar. See also PR29286. */
8432 if (!var_decl_component_p (srcvar)
8433 /* Accept: memcpy (*char_var, "test", 1); that simplify
8435 || is_gimple_min_invariant (srcvar)
8436 || readonly_data_expr (src))
8439 destvar = build_fold_indirect_ref (dest);
8440 if (TREE_THIS_VOLATILE (destvar))
8442 if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len))
8444 if (!var_decl_component_p (destvar))
8447 if (srctype == desttype
8448 || (gimple_in_ssa_p (cfun)
8449 && tree_ssa_useless_type_conversion_1 (desttype, srctype)))
8451 else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8452 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8453 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8454 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8455 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8457 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8458 expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (destvar), destvar, expr);
8464 if (endp == 0 || endp == 3)
8465 return omit_one_operand (type, dest, expr);
8471 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8474 len = fold_convert (TREE_TYPE (dest), len);
8475 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8476 dest = fold_convert (type, dest);
8478 dest = omit_one_operand (type, dest, expr);
8482 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8483 simplification can be made. */
8486 fold_builtin_bcopy (tree arglist, bool ignore)
8488 tree src, dest, size, newarglist;
8490 if (!validate_arglist (arglist,
8491 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8497 src = TREE_VALUE (arglist);
8498 dest = TREE_VALUE (TREE_CHAIN (arglist));
8499 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8501 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8502 memmove(ptr y, ptr x, size_t z). This is done this way
8503 so that if it isn't expanded inline, we fallback to
8504 calling bcopy instead of memmove. */
8506 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8507 newarglist = tree_cons (NULL_TREE, src, newarglist);
8508 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8510 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8513 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8514 the length of the string to be copied. Return NULL_TREE if no
8515 simplification can be made. */
8518 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8522 if (!validate_arglist (arglist,
8523 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8526 dest = TREE_VALUE (arglist);
8527 src = TREE_VALUE (TREE_CHAIN (arglist));
8529 /* If SRC and DEST are the same (and not volatile), return DEST. */
8530 if (operand_equal_p (src, dest, 0))
8531 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8536 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8542 len = c_strlen (src, 1);
8543 if (! len || TREE_SIDE_EFFECTS (len))
8547 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8548 arglist = build_tree_list (NULL_TREE, len);
8549 arglist = tree_cons (NULL_TREE, src, arglist);
8550 arglist = tree_cons (NULL_TREE, dest, arglist);
8551 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8552 build_function_call_expr (fn, arglist));
8555 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8556 the length of the source string. Return NULL_TREE if no simplification
8560 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8562 tree dest, src, len, fn;
8564 if (!validate_arglist (arglist,
8565 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8568 dest = TREE_VALUE (arglist);
8569 src = TREE_VALUE (TREE_CHAIN (arglist));
8570 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8572 /* If the LEN parameter is zero, return DEST. */
8573 if (integer_zerop (len))
8574 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8576 /* We can't compare slen with len as constants below if len is not a
8578 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8582 slen = c_strlen (src, 1);
8584 /* Now, we must be passed a constant src ptr parameter. */
8585 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8588 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8590 /* We do not support simplification of this case, though we do
8591 support it when expanding trees into RTL. */
8592 /* FIXME: generate a call to __builtin_memset. */
8593 if (tree_int_cst_lt (slen, len))
8596 /* OK transform into builtin memcpy. */
8597 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8600 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8601 build_function_call_expr (fn, arglist));
8604 /* Fold function call to builtin memcmp. Return
8605 NULL_TREE if no simplification can be made. */
8608 fold_builtin_memcmp (tree arglist)
8610 tree arg1, arg2, len;
8611 const char *p1, *p2;
8613 if (!validate_arglist (arglist,
8614 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8617 arg1 = TREE_VALUE (arglist);
8618 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8619 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8621 /* If the LEN parameter is zero, return zero. */
8622 if (integer_zerop (len))
8623 return omit_two_operands (integer_type_node, integer_zero_node,
8626 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8627 if (operand_equal_p (arg1, arg2, 0))
8628 return omit_one_operand (integer_type_node, integer_zero_node, len);
8630 p1 = c_getstr (arg1);
8631 p2 = c_getstr (arg2);
8633 /* If all arguments are constant, and the value of len is not greater
8634 than the lengths of arg1 and arg2, evaluate at compile-time. */
8635 if (host_integerp (len, 1) && p1 && p2
8636 && compare_tree_int (len, strlen (p1) + 1) <= 0
8637 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8639 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8642 return integer_one_node;
8644 return integer_minus_one_node;
8646 return integer_zero_node;
8649 /* If len parameter is one, return an expression corresponding to
8650 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8651 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8653 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8654 tree cst_uchar_ptr_node
8655 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8657 tree ind1 = fold_convert (integer_type_node,
8658 build1 (INDIRECT_REF, cst_uchar_node,
8659 fold_convert (cst_uchar_ptr_node,
8661 tree ind2 = fold_convert (integer_type_node,
8662 build1 (INDIRECT_REF, cst_uchar_node,
8663 fold_convert (cst_uchar_ptr_node,
8665 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8671 /* Fold function call to builtin strcmp. Return
8672 NULL_TREE if no simplification can be made. */
8675 fold_builtin_strcmp (tree arglist)
8678 const char *p1, *p2;
8680 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8683 arg1 = TREE_VALUE (arglist);
8684 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8686 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8687 if (operand_equal_p (arg1, arg2, 0))
8688 return integer_zero_node;
8690 p1 = c_getstr (arg1);
8691 p2 = c_getstr (arg2);
8695 const int i = strcmp (p1, p2);
8697 return integer_minus_one_node;
8699 return integer_one_node;
8701 return integer_zero_node;
8704 /* If the second arg is "", return *(const unsigned char*)arg1. */
8705 if (p2 && *p2 == '\0')
8707 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8708 tree cst_uchar_ptr_node
8709 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8711 return fold_convert (integer_type_node,
8712 build1 (INDIRECT_REF, cst_uchar_node,
8713 fold_convert (cst_uchar_ptr_node,
8717 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8718 if (p1 && *p1 == '\0')
8720 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8721 tree cst_uchar_ptr_node
8722 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8724 tree temp = fold_convert (integer_type_node,
8725 build1 (INDIRECT_REF, cst_uchar_node,
8726 fold_convert (cst_uchar_ptr_node,
8728 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8734 /* Fold function call to builtin strncmp. Return
8735 NULL_TREE if no simplification can be made. */
8738 fold_builtin_strncmp (tree arglist)
8740 tree arg1, arg2, len;
8741 const char *p1, *p2;
8743 if (!validate_arglist (arglist,
8744 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8747 arg1 = TREE_VALUE (arglist);
8748 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8749 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8751 /* If the LEN parameter is zero, return zero. */
8752 if (integer_zerop (len))
8753 return omit_two_operands (integer_type_node, integer_zero_node,
8756 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8757 if (operand_equal_p (arg1, arg2, 0))
8758 return omit_one_operand (integer_type_node, integer_zero_node, len);
8760 p1 = c_getstr (arg1);
8761 p2 = c_getstr (arg2);
8763 if (host_integerp (len, 1) && p1 && p2)
8765 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8767 return integer_one_node;
8769 return integer_minus_one_node;
8771 return integer_zero_node;
8774 /* If the second arg is "", and the length is greater than zero,
8775 return *(const unsigned char*)arg1. */
8776 if (p2 && *p2 == '\0'
8777 && TREE_CODE (len) == INTEGER_CST
8778 && tree_int_cst_sgn (len) == 1)
8780 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8781 tree cst_uchar_ptr_node
8782 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8784 return fold_convert (integer_type_node,
8785 build1 (INDIRECT_REF, cst_uchar_node,
8786 fold_convert (cst_uchar_ptr_node,
8790 /* If the first arg is "", and the length is greater than zero,
8791 return -*(const unsigned char*)arg2. */
8792 if (p1 && *p1 == '\0'
8793 && TREE_CODE (len) == INTEGER_CST
8794 && tree_int_cst_sgn (len) == 1)
8796 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8797 tree cst_uchar_ptr_node
8798 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8800 tree temp = fold_convert (integer_type_node,
8801 build1 (INDIRECT_REF, cst_uchar_node,
8802 fold_convert (cst_uchar_ptr_node,
8804 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8807 /* If len parameter is one, return an expression corresponding to
8808 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8809 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8811 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8812 tree cst_uchar_ptr_node
8813 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8815 tree ind1 = fold_convert (integer_type_node,
8816 build1 (INDIRECT_REF, cst_uchar_node,
8817 fold_convert (cst_uchar_ptr_node,
8819 tree ind2 = fold_convert (integer_type_node,
8820 build1 (INDIRECT_REF, cst_uchar_node,
8821 fold_convert (cst_uchar_ptr_node,
8823 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8829 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8830 NULL_TREE if no simplification can be made. */
8833 fold_builtin_signbit (tree fndecl, tree arglist)
8835 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8838 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8841 arg = TREE_VALUE (arglist);
8843 /* If ARG is a compile-time constant, determine the result. */
8844 if (TREE_CODE (arg) == REAL_CST
8845 && !TREE_OVERFLOW (arg))
8849 c = TREE_REAL_CST (arg);
8850 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8851 return fold_convert (type, temp);
8854 /* If ARG is non-negative, the result is always zero. */
8855 if (tree_expr_nonnegative_p (arg))
8856 return omit_one_operand (type, integer_zero_node, arg);
8858 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8859 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8860 return fold_build2 (LT_EXPR, type, arg,
8861 build_real (TREE_TYPE (arg), dconst0));
8866 /* Fold function call to builtin copysign, copysignf or copysignl.
8867 Return NULL_TREE if no simplification can be made. */
8870 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8872 tree arg1, arg2, tem;
8874 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8877 arg1 = TREE_VALUE (arglist);
8878 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8880 /* copysign(X,X) is X. */
8881 if (operand_equal_p (arg1, arg2, 0))
8882 return fold_convert (type, arg1);
8884 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8885 if (TREE_CODE (arg1) == REAL_CST
8886 && TREE_CODE (arg2) == REAL_CST
8887 && !TREE_OVERFLOW (arg1)
8888 && !TREE_OVERFLOW (arg2))
8890 REAL_VALUE_TYPE c1, c2;
8892 c1 = TREE_REAL_CST (arg1);
8893 c2 = TREE_REAL_CST (arg2);
8894 /* c1.sign := c2.sign. */
8895 real_copysign (&c1, &c2);
8896 return build_real (type, c1);
8899 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8900 Remember to evaluate Y for side-effects. */
8901 if (tree_expr_nonnegative_p (arg2))
8902 return omit_one_operand (type,
8903 fold_build1 (ABS_EXPR, type, arg1),
8906 /* Strip sign changing operations for the first argument. */
8907 tem = fold_strip_sign_ops (arg1);
8910 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8911 return build_function_call_expr (fndecl, arglist);
8917 /* Fold a call to builtin isascii. */
8920 fold_builtin_isascii (tree arglist)
8922 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8926 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8927 tree arg = TREE_VALUE (arglist);
8929 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8930 build_int_cst (NULL_TREE,
8931 ~ (unsigned HOST_WIDE_INT) 0x7f));
8932 return fold_build2 (EQ_EXPR, integer_type_node,
8933 arg, integer_zero_node);
8937 /* Fold a call to builtin toascii. */
8940 fold_builtin_toascii (tree arglist)
8942 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8946 /* Transform toascii(c) -> (c & 0x7f). */
8947 tree arg = TREE_VALUE (arglist);
8949 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8950 build_int_cst (NULL_TREE, 0x7f));
8954 /* Fold a call to builtin isdigit. */
8957 fold_builtin_isdigit (tree arglist)
8959 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8963 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8964 /* According to the C standard, isdigit is unaffected by locale.
8965 However, it definitely is affected by the target character set. */
8967 unsigned HOST_WIDE_INT target_digit0
8968 = lang_hooks.to_target_charset ('0');
8970 if (target_digit0 == 0)
8973 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8974 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8975 build_int_cst (unsigned_type_node, target_digit0));
8976 return fold_build2 (LE_EXPR, integer_type_node, arg,
8977 build_int_cst (unsigned_type_node, 9));
8981 /* Fold a call to fabs, fabsf or fabsl. */
8984 fold_builtin_fabs (tree arglist, tree type)
8988 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8991 arg = TREE_VALUE (arglist);
8992 arg = fold_convert (type, arg);
8993 if (TREE_CODE (arg) == REAL_CST)
8994 return fold_abs_const (arg, type);
8995 return fold_build1 (ABS_EXPR, type, arg);
8998 /* Fold a call to abs, labs, llabs or imaxabs. */
9001 fold_builtin_abs (tree arglist, tree type)
9005 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
9008 arg = TREE_VALUE (arglist);
9009 arg = fold_convert (type, arg);
9010 if (TREE_CODE (arg) == INTEGER_CST)
9011 return fold_abs_const (arg, type);
9012 return fold_build1 (ABS_EXPR, type, arg);
9015 /* Fold a call to builtin fmin or fmax. */
9018 fold_builtin_fmin_fmax (tree arglist, tree type, bool max)
9020 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9022 tree arg0 = TREE_VALUE (arglist);
9023 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9024 /* Calculate the result when the argument is a constant. */
9025 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9030 /* If either argument is NaN, return the other one. Avoid the
9031 transformation if we get (and honor) a signalling NaN. Using
9032 omit_one_operand() ensures we create a non-lvalue. */
9033 if (TREE_CODE (arg0) == REAL_CST
9034 && real_isnan (&TREE_REAL_CST (arg0))
9035 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9036 || ! TREE_REAL_CST (arg0).signalling))
9037 return omit_one_operand (type, arg1, arg0);
9038 if (TREE_CODE (arg1) == REAL_CST
9039 && real_isnan (&TREE_REAL_CST (arg1))
9040 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9041 || ! TREE_REAL_CST (arg1).signalling))
9042 return omit_one_operand (type, arg0, arg1);
9044 /* Transform fmin/fmax(x,x) -> x. */
9045 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9046 return omit_one_operand (type, arg0, arg1);
9048 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9049 functions to return the numeric arg if the other one is NaN.
9050 These tree codes don't honor that, so only transform if
9051 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9052 handled, so we don't have to worry about it either. */
9053 if (flag_finite_math_only)
9054 return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type,
9055 fold_convert (type, arg0),
9056 fold_convert (type, arg1));
9061 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9062 EXP is the CALL_EXPR for the call. */
9065 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
9067 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9071 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9073 /* Check that we have exactly one argument. */
9076 error ("too few arguments to function %qs",
9077 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9078 return error_mark_node;
9080 else if (TREE_CHAIN (arglist) != 0)
9082 error ("too many arguments to function %qs",
9083 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9084 return error_mark_node;
9088 error ("non-floating-point argument to function %qs",
9089 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9090 return error_mark_node;
9094 arg = TREE_VALUE (arglist);
9095 switch (builtin_index)
9097 case BUILT_IN_ISINF:
9098 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9099 return omit_one_operand (type, integer_zero_node, arg);
9101 if (TREE_CODE (arg) == REAL_CST)
9103 r = TREE_REAL_CST (arg);
9104 if (real_isinf (&r))
9105 return real_compare (GT_EXPR, &r, &dconst0)
9106 ? integer_one_node : integer_minus_one_node;
9108 return integer_zero_node;
9113 case BUILT_IN_FINITE:
9114 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9115 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9116 return omit_one_operand (type, integer_one_node, arg);
9118 if (TREE_CODE (arg) == REAL_CST)
9120 r = TREE_REAL_CST (arg);
9121 return real_isinf (&r) || real_isnan (&r)
9122 ? integer_zero_node : integer_one_node;
9127 case BUILT_IN_ISNAN:
9128 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9129 return omit_one_operand (type, integer_zero_node, arg);
9131 if (TREE_CODE (arg) == REAL_CST)
9133 r = TREE_REAL_CST (arg);
9134 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9137 arg = builtin_save_expr (arg);
9138 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
9145 /* Fold a call to an unordered comparison function such as
9146 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9147 being called and ARGLIST is the argument list for the call.
9148 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9149 the opposite of the desired result. UNORDERED_CODE is used
9150 for modes that can hold NaNs and ORDERED_CODE is used for
9154 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
9155 enum tree_code unordered_code,
9156 enum tree_code ordered_code)
9158 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9159 enum tree_code code;
9162 enum tree_code code0, code1;
9163 tree cmp_type = NULL_TREE;
9165 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9167 /* Check that we have exactly two arguments. */
9168 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
9170 error ("too few arguments to function %qs",
9171 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9172 return error_mark_node;
9174 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
9176 error ("too many arguments to function %qs",
9177 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9178 return error_mark_node;
9182 arg0 = TREE_VALUE (arglist);
9183 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
9185 type0 = TREE_TYPE (arg0);
9186 type1 = TREE_TYPE (arg1);
9188 code0 = TREE_CODE (type0);
9189 code1 = TREE_CODE (type1);
9191 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9192 /* Choose the wider of two real types. */
9193 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9195 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9197 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9201 error ("non-floating-point argument to function %qs",
9202 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
9203 return error_mark_node;
9206 arg0 = fold_convert (cmp_type, arg0);
9207 arg1 = fold_convert (cmp_type, arg1);
9209 if (unordered_code == UNORDERED_EXPR)
9211 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9212 return omit_two_operands (type, integer_zero_node, arg0, arg1);
9213 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
9216 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9218 return fold_build1 (TRUTH_NOT_EXPR, type,
9219 fold_build2 (code, type, arg0, arg1));
9222 /* Used by constant folding to simplify calls to builtin functions. EXP is
9223 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
9224 result of the function call is ignored. This function returns NULL_TREE
9225 if no simplification was possible. */
9228 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
9230 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9231 enum built_in_function fcode;
9233 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9234 return targetm.fold_builtin (fndecl, arglist, ignore);
9236 fcode = DECL_FUNCTION_CODE (fndecl);
9239 case BUILT_IN_FPUTS:
9240 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
9242 case BUILT_IN_FPUTS_UNLOCKED:
9243 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
9245 case BUILT_IN_STRSTR:
9246 return fold_builtin_strstr (arglist, type);
9248 case BUILT_IN_STRCAT:
9249 return fold_builtin_strcat (arglist);
9251 case BUILT_IN_STRNCAT:
9252 return fold_builtin_strncat (arglist);
9254 case BUILT_IN_STRSPN:
9255 return fold_builtin_strspn (arglist);
9257 case BUILT_IN_STRCSPN:
9258 return fold_builtin_strcspn (arglist);
9260 case BUILT_IN_STRCHR:
9261 case BUILT_IN_INDEX:
9262 return fold_builtin_strchr (arglist, type);
9264 case BUILT_IN_STRRCHR:
9265 case BUILT_IN_RINDEX:
9266 return fold_builtin_strrchr (arglist, type);
9268 case BUILT_IN_STRCPY:
9269 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
9271 case BUILT_IN_STRNCPY:
9272 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
9274 case BUILT_IN_STRCMP:
9275 return fold_builtin_strcmp (arglist);
9277 case BUILT_IN_STRNCMP:
9278 return fold_builtin_strncmp (arglist);
9280 case BUILT_IN_STRPBRK:
9281 return fold_builtin_strpbrk (arglist, type);
9284 case BUILT_IN_MEMCMP:
9285 return fold_builtin_memcmp (arglist);
9287 case BUILT_IN_SPRINTF:
9288 return fold_builtin_sprintf (arglist, ignore);
9290 case BUILT_IN_CONSTANT_P:
9294 val = fold_builtin_constant_p (arglist);
9295 /* Gimplification will pull the CALL_EXPR for the builtin out of
9296 an if condition. When not optimizing, we'll not CSE it back.
9297 To avoid link error types of regressions, return false now. */
9298 if (!val && !optimize)
9299 val = integer_zero_node;
9304 case BUILT_IN_EXPECT:
9305 return fold_builtin_expect (arglist);
9307 case BUILT_IN_CLASSIFY_TYPE:
9308 return fold_builtin_classify_type (arglist);
9310 case BUILT_IN_STRLEN:
9311 return fold_builtin_strlen (arglist);
9313 CASE_FLT_FN (BUILT_IN_FABS):
9314 return fold_builtin_fabs (arglist, type);
9318 case BUILT_IN_LLABS:
9319 case BUILT_IN_IMAXABS:
9320 return fold_builtin_abs (arglist, type);
9322 CASE_FLT_FN (BUILT_IN_CONJ):
9323 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9324 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
9327 CASE_FLT_FN (BUILT_IN_CREAL):
9328 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9329 return non_lvalue (fold_build1 (REALPART_EXPR, type,
9330 TREE_VALUE (arglist)));
9333 CASE_FLT_FN (BUILT_IN_CIMAG):
9334 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
9335 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
9336 TREE_VALUE (arglist)));
9339 CASE_FLT_FN (BUILT_IN_CABS):
9340 return fold_builtin_cabs (arglist, type, fndecl);
9342 CASE_FLT_FN (BUILT_IN_SQRT):
9343 return fold_builtin_sqrt (arglist, type);
9345 CASE_FLT_FN (BUILT_IN_CBRT):
9346 return fold_builtin_cbrt (arglist, type);
9348 CASE_FLT_FN (BUILT_IN_ASIN):
9349 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9350 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asin,
9351 &dconstm1, &dconst1, true);
9354 CASE_FLT_FN (BUILT_IN_ACOS):
9355 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9356 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acos,
9357 &dconstm1, &dconst1, true);
9360 CASE_FLT_FN (BUILT_IN_ATAN):
9361 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9362 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atan,
9366 CASE_FLT_FN (BUILT_IN_ASINH):
9367 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9368 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_asinh,
9372 CASE_FLT_FN (BUILT_IN_ACOSH):
9373 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9374 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_acosh,
9375 &dconst1, NULL, true);
9378 CASE_FLT_FN (BUILT_IN_ATANH):
9379 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9380 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_atanh,
9381 &dconstm1, &dconst1, false);
9384 CASE_FLT_FN (BUILT_IN_SIN):
9385 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9386 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sin,
9390 CASE_FLT_FN (BUILT_IN_COS):
9391 return fold_builtin_cos (arglist, type, fndecl);
9393 CASE_FLT_FN (BUILT_IN_TAN):
9394 return fold_builtin_tan (arglist, type);
9396 CASE_FLT_FN (BUILT_IN_SINCOS):
9397 return fold_builtin_sincos (arglist);
9399 CASE_FLT_FN (BUILT_IN_CEXP):
9400 return fold_builtin_cexp (arglist, type);
9402 CASE_FLT_FN (BUILT_IN_CEXPI):
9403 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9404 return do_mpfr_sincos (TREE_VALUE (arglist), NULL_TREE, NULL_TREE);
9406 CASE_FLT_FN (BUILT_IN_SINH):
9407 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9408 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_sinh,
9412 CASE_FLT_FN (BUILT_IN_COSH):
9413 return fold_builtin_cosh (arglist, type, fndecl);
9415 CASE_FLT_FN (BUILT_IN_TANH):
9416 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9417 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_tanh,
9421 CASE_FLT_FN (BUILT_IN_ERF):
9422 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9423 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erf,
9427 CASE_FLT_FN (BUILT_IN_ERFC):
9428 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9429 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_erfc,
9433 CASE_FLT_FN (BUILT_IN_TGAMMA):
9434 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9435 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_gamma,
9439 CASE_FLT_FN (BUILT_IN_EXP):
9440 return fold_builtin_exponent (fndecl, arglist, mpfr_exp);
9442 CASE_FLT_FN (BUILT_IN_EXP2):
9443 return fold_builtin_exponent (fndecl, arglist, mpfr_exp2);
9445 CASE_FLT_FN (BUILT_IN_EXP10):
9446 CASE_FLT_FN (BUILT_IN_POW10):
9447 return fold_builtin_exponent (fndecl, arglist, mpfr_exp10);
9449 CASE_FLT_FN (BUILT_IN_EXPM1):
9450 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9451 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_expm1,
9455 CASE_FLT_FN (BUILT_IN_LOG):
9456 return fold_builtin_logarithm (fndecl, arglist, mpfr_log);
9458 CASE_FLT_FN (BUILT_IN_LOG2):
9459 return fold_builtin_logarithm (fndecl, arglist, mpfr_log2);
9461 CASE_FLT_FN (BUILT_IN_LOG10):
9462 return fold_builtin_logarithm (fndecl, arglist, mpfr_log10);
9464 CASE_FLT_FN (BUILT_IN_LOG1P):
9465 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
9466 return do_mpfr_arg1 (TREE_VALUE (arglist), type, mpfr_log1p,
9467 &dconstm1, NULL, false);
9470 CASE_FLT_FN (BUILT_IN_ATAN2):
9471 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9472 return do_mpfr_arg2 (TREE_VALUE (arglist),
9473 TREE_VALUE (TREE_CHAIN (arglist)),
9477 CASE_FLT_FN (BUILT_IN_FDIM):
9478 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9479 return do_mpfr_arg2 (TREE_VALUE (arglist),
9480 TREE_VALUE (TREE_CHAIN (arglist)),
9484 CASE_FLT_FN (BUILT_IN_FMA):
9485 if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
9486 return do_mpfr_arg3 (TREE_VALUE (arglist),
9487 TREE_VALUE (TREE_CHAIN (arglist)),
9488 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
9492 CASE_FLT_FN (BUILT_IN_FMIN):
9493 return fold_builtin_fmin_fmax (arglist, type, /*max=*/false);
9495 CASE_FLT_FN (BUILT_IN_FMAX):
9496 return fold_builtin_fmin_fmax (arglist, type, /*max=*/true);
9498 CASE_FLT_FN (BUILT_IN_HYPOT):
9499 return fold_builtin_hypot (fndecl, arglist, type);
9501 CASE_FLT_FN (BUILT_IN_POW):
9502 return fold_builtin_pow (fndecl, arglist, type);
9504 CASE_FLT_FN (BUILT_IN_POWI):
9505 return fold_builtin_powi (fndecl, arglist, type);
9507 CASE_FLT_FN (BUILT_IN_INF):
9508 case BUILT_IN_INFD32:
9509 case BUILT_IN_INFD64:
9510 case BUILT_IN_INFD128:
9511 return fold_builtin_inf (type, true);
9513 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9514 return fold_builtin_inf (type, false);
9516 CASE_FLT_FN (BUILT_IN_NAN):
9517 case BUILT_IN_NAND32:
9518 case BUILT_IN_NAND64:
9519 case BUILT_IN_NAND128:
9520 return fold_builtin_nan (arglist, type, true);
9522 CASE_FLT_FN (BUILT_IN_NANS):
9523 return fold_builtin_nan (arglist, type, false);
9525 CASE_FLT_FN (BUILT_IN_FLOOR):
9526 return fold_builtin_floor (fndecl, arglist);
9528 CASE_FLT_FN (BUILT_IN_CEIL):
9529 return fold_builtin_ceil (fndecl, arglist);
9531 CASE_FLT_FN (BUILT_IN_TRUNC):
9532 return fold_builtin_trunc (fndecl, arglist);
9534 CASE_FLT_FN (BUILT_IN_ROUND):
9535 return fold_builtin_round (fndecl, arglist);
9537 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9538 CASE_FLT_FN (BUILT_IN_RINT):
9539 return fold_trunc_transparent_mathfn (fndecl, arglist);
9541 CASE_FLT_FN (BUILT_IN_LCEIL):
9542 CASE_FLT_FN (BUILT_IN_LLCEIL):
9543 CASE_FLT_FN (BUILT_IN_LFLOOR):
9544 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9545 CASE_FLT_FN (BUILT_IN_LROUND):
9546 CASE_FLT_FN (BUILT_IN_LLROUND):
9547 return fold_builtin_int_roundingfn (fndecl, arglist);
9549 CASE_FLT_FN (BUILT_IN_LRINT):
9550 CASE_FLT_FN (BUILT_IN_LLRINT):
9551 return fold_fixed_mathfn (fndecl, arglist);
9553 case BUILT_IN_BSWAP32:
9554 case BUILT_IN_BSWAP64:
9555 return fold_builtin_bswap (fndecl, arglist);
9557 CASE_INT_FN (BUILT_IN_FFS):
9558 CASE_INT_FN (BUILT_IN_CLZ):
9559 CASE_INT_FN (BUILT_IN_CTZ):
9560 CASE_INT_FN (BUILT_IN_POPCOUNT):
9561 CASE_INT_FN (BUILT_IN_PARITY):
9562 return fold_builtin_bitop (fndecl, arglist);
9564 case BUILT_IN_MEMSET:
9565 return fold_builtin_memset (arglist, type, ignore);
9567 case BUILT_IN_MEMCPY:
9568 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9570 case BUILT_IN_MEMPCPY:
9571 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9573 case BUILT_IN_MEMMOVE:
9574 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9576 case BUILT_IN_BZERO:
9577 return fold_builtin_bzero (arglist, ignore);
9579 case BUILT_IN_BCOPY:
9580 return fold_builtin_bcopy (arglist, ignore);
9582 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9583 return fold_builtin_signbit (fndecl, arglist);
9585 case BUILT_IN_ISASCII:
9586 return fold_builtin_isascii (arglist);
9588 case BUILT_IN_TOASCII:
9589 return fold_builtin_toascii (arglist);
9591 case BUILT_IN_ISDIGIT:
9592 return fold_builtin_isdigit (arglist);
9594 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9595 return fold_builtin_copysign (fndecl, arglist, type);
9597 CASE_FLT_FN (BUILT_IN_FINITE):
9598 case BUILT_IN_FINITED32:
9599 case BUILT_IN_FINITED64:
9600 case BUILT_IN_FINITED128:
9601 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9603 CASE_FLT_FN (BUILT_IN_ISINF):
9604 case BUILT_IN_ISINFD32:
9605 case BUILT_IN_ISINFD64:
9606 case BUILT_IN_ISINFD128:
9607 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9609 CASE_FLT_FN (BUILT_IN_ISNAN):
9610 case BUILT_IN_ISNAND32:
9611 case BUILT_IN_ISNAND64:
9612 case BUILT_IN_ISNAND128:
9613 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9615 case BUILT_IN_ISGREATER:
9616 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9617 case BUILT_IN_ISGREATEREQUAL:
9618 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9619 case BUILT_IN_ISLESS:
9620 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9621 case BUILT_IN_ISLESSEQUAL:
9622 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9623 case BUILT_IN_ISLESSGREATER:
9624 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9625 case BUILT_IN_ISUNORDERED:
9626 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9629 /* We do the folding for va_start in the expander. */
9630 case BUILT_IN_VA_START:
9633 case BUILT_IN_OBJECT_SIZE:
9634 return fold_builtin_object_size (arglist);
9635 case BUILT_IN_MEMCPY_CHK:
9636 case BUILT_IN_MEMPCPY_CHK:
9637 case BUILT_IN_MEMMOVE_CHK:
9638 case BUILT_IN_MEMSET_CHK:
9639 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9640 DECL_FUNCTION_CODE (fndecl));
9641 case BUILT_IN_STRCPY_CHK:
9642 case BUILT_IN_STPCPY_CHK:
9643 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9644 DECL_FUNCTION_CODE (fndecl));
9645 case BUILT_IN_STRNCPY_CHK:
9646 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9647 case BUILT_IN_STRCAT_CHK:
9648 return fold_builtin_strcat_chk (fndecl, arglist);
9649 case BUILT_IN_STRNCAT_CHK:
9650 return fold_builtin_strncat_chk (fndecl, arglist);
9651 case BUILT_IN_SPRINTF_CHK:
9652 case BUILT_IN_VSPRINTF_CHK:
9653 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9654 case BUILT_IN_SNPRINTF_CHK:
9655 case BUILT_IN_VSNPRINTF_CHK:
9656 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9657 DECL_FUNCTION_CODE (fndecl));
9659 case BUILT_IN_PRINTF:
9660 case BUILT_IN_PRINTF_UNLOCKED:
9661 case BUILT_IN_VPRINTF:
9662 case BUILT_IN_PRINTF_CHK:
9663 case BUILT_IN_VPRINTF_CHK:
9664 return fold_builtin_printf (fndecl, arglist, ignore,
9665 DECL_FUNCTION_CODE (fndecl));
9667 case BUILT_IN_FPRINTF:
9668 case BUILT_IN_FPRINTF_UNLOCKED:
9669 case BUILT_IN_VFPRINTF:
9670 case BUILT_IN_FPRINTF_CHK:
9671 case BUILT_IN_VFPRINTF_CHK:
9672 return fold_builtin_fprintf (fndecl, arglist, ignore,
9673 DECL_FUNCTION_CODE (fndecl));
9682 /* A wrapper function for builtin folding that prevents warnings for
9683 "statement without effect" and the like, caused by removing the
9684 call node earlier than the warning is generated. */
9687 fold_builtin (tree fndecl, tree arglist, bool ignore)
9689 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9692 exp = build1 (NOP_EXPR, GENERIC_TREE_TYPE (exp), exp);
9693 TREE_NO_WARNING (exp) = 1;
9699 /* Conveniently construct a function call expression. */
9702 build_function_call_expr (tree fn, tree arglist)
9706 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9707 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9708 call_expr, arglist, NULL_TREE);
9711 /* This function validates the types of a function call argument list
9712 represented as a tree chain of parameters against a specified list
9713 of tree_codes. If the last specifier is a 0, that represents an
9714 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9717 validate_arglist (tree arglist, ...)
9719 enum tree_code code;
9723 va_start (ap, arglist);
9727 code = va_arg (ap, enum tree_code);
9731 /* This signifies an ellipses, any further arguments are all ok. */
9735 /* This signifies an endlink, if no arguments remain, return
9736 true, otherwise return false. */
9740 /* If no parameters remain or the parameter's code does not
9741 match the specified code, return false. Otherwise continue
9742 checking any remaining arguments. */
9745 if (code == POINTER_TYPE)
9747 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9750 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9754 arglist = TREE_CHAIN (arglist);
9758 /* We need gotos here since we can only have one VA_CLOSE in a
9766 /* Default target-specific builtin expander that does nothing. */
9769 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9770 rtx target ATTRIBUTE_UNUSED,
9771 rtx subtarget ATTRIBUTE_UNUSED,
9772 enum machine_mode mode ATTRIBUTE_UNUSED,
9773 int ignore ATTRIBUTE_UNUSED)
9778 /* Returns true is EXP represents data that would potentially reside
9779 in a readonly section. */
9782 readonly_data_expr (tree exp)
9786 if (TREE_CODE (exp) != ADDR_EXPR)
9789 exp = get_base_address (TREE_OPERAND (exp, 0));
9793 /* Make sure we call decl_readonly_section only for trees it
9794 can handle (since it returns true for everything it doesn't
9796 if (TREE_CODE (exp) == STRING_CST
9797 || TREE_CODE (exp) == CONSTRUCTOR
9798 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9799 return decl_readonly_section (exp, 0);
9804 /* Simplify a call to the strstr builtin.
9806 Return 0 if no simplification was possible, otherwise return the
9807 simplified form of the call as a tree.
9809 The simplified form may be a constant or other expression which
9810 computes the same value, but in a more efficient manner (including
9811 calls to other builtin functions).
9813 The call may contain arguments which need to be evaluated, but
9814 which are not useful to determine the result of the call. In
9815 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9816 COMPOUND_EXPR will be an argument which must be evaluated.
9817 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9818 COMPOUND_EXPR in the chain will contain the tree for the simplified
9819 form of the builtin function call. */
9822 fold_builtin_strstr (tree arglist, tree type)
9824 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9828 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9830 const char *p1, *p2;
9839 const char *r = strstr (p1, p2);
9843 return build_int_cst (TREE_TYPE (s1), 0);
9845 /* Return an offset into the constant string argument. */
9846 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9847 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9848 return fold_convert (type, tem);
9851 /* The argument is const char *, and the result is char *, so we need
9852 a type conversion here to avoid a warning. */
9854 return fold_convert (type, s1);
9859 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9863 /* New argument list transforming strstr(s1, s2) to
9864 strchr(s1, s2[0]). */
9865 arglist = build_tree_list (NULL_TREE,
9866 build_int_cst (NULL_TREE, p2[0]));
9867 arglist = tree_cons (NULL_TREE, s1, arglist);
9868 return build_function_call_expr (fn, arglist);
9872 /* Simplify a call to the strchr builtin.
9874 Return 0 if no simplification was possible, otherwise return the
9875 simplified form of the call as a tree.
9877 The simplified form may be a constant or other expression which
9878 computes the same value, but in a more efficient manner (including
9879 calls to other builtin functions).
9881 The call may contain arguments which need to be evaluated, but
9882 which are not useful to determine the result of the call. In
9883 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9884 COMPOUND_EXPR will be an argument which must be evaluated.
9885 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9886 COMPOUND_EXPR in the chain will contain the tree for the simplified
9887 form of the builtin function call. */
9890 fold_builtin_strchr (tree arglist, tree type)
9892 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9896 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9899 if (TREE_CODE (s2) != INTEGER_CST)
9909 if (target_char_cast (s2, &c))
9915 return build_int_cst (TREE_TYPE (s1), 0);
9917 /* Return an offset into the constant string argument. */
9918 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9919 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9920 return fold_convert (type, tem);
9926 /* Simplify a call to the strrchr builtin.
9928 Return 0 if no simplification was possible, otherwise return the
9929 simplified form of the call as a tree.
9931 The simplified form may be a constant or other expression which
9932 computes the same value, but in a more efficient manner (including
9933 calls to other builtin functions).
9935 The call may contain arguments which need to be evaluated, but
9936 which are not useful to determine the result of the call. In
9937 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9938 COMPOUND_EXPR will be an argument which must be evaluated.
9939 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9940 COMPOUND_EXPR in the chain will contain the tree for the simplified
9941 form of the builtin function call. */
9944 fold_builtin_strrchr (tree arglist, tree type)
9946 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9950 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9954 if (TREE_CODE (s2) != INTEGER_CST)
9964 if (target_char_cast (s2, &c))
9967 r = strrchr (p1, c);
9970 return build_int_cst (TREE_TYPE (s1), 0);
9972 /* Return an offset into the constant string argument. */
9973 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9974 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9975 return fold_convert (type, tem);
9978 if (! integer_zerop (s2))
9981 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9985 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9986 return build_function_call_expr (fn, arglist);
9990 /* Simplify a call to the strpbrk builtin.
9992 Return 0 if no simplification was possible, otherwise return the
9993 simplified form of the call as a tree.
9995 The simplified form may be a constant or other expression which
9996 computes the same value, but in a more efficient manner (including
9997 calls to other builtin functions).
9999 The call may contain arguments which need to be evaluated, but
10000 which are not useful to determine the result of the call. In
10001 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10002 COMPOUND_EXPR will be an argument which must be evaluated.
10003 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10004 COMPOUND_EXPR in the chain will contain the tree for the simplified
10005 form of the builtin function call. */
10008 fold_builtin_strpbrk (tree arglist, tree type)
10010 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10014 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10016 const char *p1, *p2;
10018 p2 = c_getstr (s2);
10022 p1 = c_getstr (s1);
10025 const char *r = strpbrk (p1, p2);
10029 return build_int_cst (TREE_TYPE (s1), 0);
10031 /* Return an offset into the constant string argument. */
10032 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
10033 s1, build_int_cst (TREE_TYPE (s1), r - p1));
10034 return fold_convert (type, tem);
10038 /* strpbrk(x, "") == NULL.
10039 Evaluate and ignore s1 in case it had side-effects. */
10040 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
10043 return 0; /* Really call strpbrk. */
10045 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10049 /* New argument list transforming strpbrk(s1, s2) to
10050 strchr(s1, s2[0]). */
10051 arglist = build_tree_list (NULL_TREE,
10052 build_int_cst (NULL_TREE, p2[0]));
10053 arglist = tree_cons (NULL_TREE, s1, arglist);
10054 return build_function_call_expr (fn, arglist);
10058 /* Simplify a call to the strcat builtin.
10060 Return 0 if no simplification was possible, otherwise return the
10061 simplified form of the call as a tree.
10063 The simplified form may be a constant or other expression which
10064 computes the same value, but in a more efficient manner (including
10065 calls to other builtin functions).
10067 The call may contain arguments which need to be evaluated, but
10068 which are not useful to determine the result of the call. In
10069 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10070 COMPOUND_EXPR will be an argument which must be evaluated.
10071 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10072 COMPOUND_EXPR in the chain will contain the tree for the simplified
10073 form of the builtin function call. */
10076 fold_builtin_strcat (tree arglist)
10078 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10082 tree dst = TREE_VALUE (arglist),
10083 src = TREE_VALUE (TREE_CHAIN (arglist));
10084 const char *p = c_getstr (src);
10086 /* If the string length is zero, return the dst parameter. */
10087 if (p && *p == '\0')
10094 /* Simplify a call to the strncat builtin.
10096 Return 0 if no simplification was possible, otherwise return the
10097 simplified form of the call as a tree.
10099 The simplified form may be a constant or other expression which
10100 computes the same value, but in a more efficient manner (including
10101 calls to other builtin functions).
10103 The call may contain arguments which need to be evaluated, but
10104 which are not useful to determine the result of the call. In
10105 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10106 COMPOUND_EXPR will be an argument which must be evaluated.
10107 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10108 COMPOUND_EXPR in the chain will contain the tree for the simplified
10109 form of the builtin function call. */
10112 fold_builtin_strncat (tree arglist)
10114 if (!validate_arglist (arglist,
10115 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10119 tree dst = TREE_VALUE (arglist);
10120 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10121 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10122 const char *p = c_getstr (src);
10124 /* If the requested length is zero, or the src parameter string
10125 length is zero, return the dst parameter. */
10126 if (integer_zerop (len) || (p && *p == '\0'))
10127 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
10129 /* If the requested len is greater than or equal to the string
10130 length, call strcat. */
10131 if (TREE_CODE (len) == INTEGER_CST && p
10132 && compare_tree_int (len, strlen (p)) >= 0)
10135 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
10136 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
10138 /* If the replacement _DECL isn't initialized, don't do the
10143 return build_function_call_expr (fn, newarglist);
10149 /* Simplify a call to the strspn builtin.
10151 Return 0 if no simplification was possible, otherwise return the
10152 simplified form of the call as a tree.
10154 The simplified form may be a constant or other expression which
10155 computes the same value, but in a more efficient manner (including
10156 calls to other builtin functions).
10158 The call may contain arguments which need to be evaluated, but
10159 which are not useful to determine the result of the call. In
10160 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10161 COMPOUND_EXPR will be an argument which must be evaluated.
10162 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10163 COMPOUND_EXPR in the chain will contain the tree for the simplified
10164 form of the builtin function call. */
10167 fold_builtin_strspn (tree arglist)
10169 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10173 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10174 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10176 /* If both arguments are constants, evaluate at compile-time. */
10179 const size_t r = strspn (p1, p2);
10180 return size_int (r);
10183 /* If either argument is "", return 0. */
10184 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10185 /* Evaluate and ignore both arguments in case either one has
10187 return omit_two_operands (integer_type_node, integer_zero_node,
10193 /* Simplify a call to the strcspn builtin.
10195 Return 0 if no simplification was possible, otherwise return the
10196 simplified form of the call as a tree.
10198 The simplified form may be a constant or other expression which
10199 computes the same value, but in a more efficient manner (including
10200 calls to other builtin functions).
10202 The call may contain arguments which need to be evaluated, but
10203 which are not useful to determine the result of the call. In
10204 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10205 COMPOUND_EXPR will be an argument which must be evaluated.
10206 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10207 COMPOUND_EXPR in the chain will contain the tree for the simplified
10208 form of the builtin function call. */
10211 fold_builtin_strcspn (tree arglist)
10213 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10217 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
10218 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10220 /* If both arguments are constants, evaluate at compile-time. */
10223 const size_t r = strcspn (p1, p2);
10224 return size_int (r);
10227 /* If the first argument is "", return 0. */
10228 if (p1 && *p1 == '\0')
10230 /* Evaluate and ignore argument s2 in case it has
10232 return omit_one_operand (integer_type_node,
10233 integer_zero_node, s2);
10236 /* If the second argument is "", return __builtin_strlen(s1). */
10237 if (p2 && *p2 == '\0')
10239 tree newarglist = build_tree_list (NULL_TREE, s1),
10240 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
10242 /* If the replacement _DECL isn't initialized, don't do the
10247 return build_function_call_expr (fn, newarglist);
10253 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
10254 by the builtin will be ignored. UNLOCKED is true is true if this
10255 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
10256 the known length of the string. Return NULL_TREE if no simplification
10260 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
10263 /* If we're using an unlocked function, assume the other unlocked
10264 functions exist explicitly. */
10265 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
10266 : implicit_built_in_decls[BUILT_IN_FPUTC];
10267 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
10268 : implicit_built_in_decls[BUILT_IN_FWRITE];
10270 /* If the return value is used, don't do the transformation. */
10274 /* Verify the arguments in the original call. */
10275 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
10279 len = c_strlen (TREE_VALUE (arglist), 0);
10281 /* Get the length of the string passed to fputs. If the length
10282 can't be determined, punt. */
10284 || TREE_CODE (len) != INTEGER_CST)
10287 switch (compare_tree_int (len, 1))
10289 case -1: /* length is 0, delete the call entirely . */
10290 return omit_one_operand (integer_type_node, integer_zero_node,
10291 TREE_VALUE (TREE_CHAIN (arglist)));
10293 case 0: /* length is 1, call fputc. */
10295 const char *p = c_getstr (TREE_VALUE (arglist));
10299 /* New argument list transforming fputs(string, stream) to
10300 fputc(string[0], stream). */
10301 arglist = build_tree_list (NULL_TREE,
10302 TREE_VALUE (TREE_CHAIN (arglist)));
10303 arglist = tree_cons (NULL_TREE,
10304 build_int_cst (NULL_TREE, p[0]),
10311 case 1: /* length is greater than 1, call fwrite. */
10315 /* If optimizing for size keep fputs. */
10318 string_arg = TREE_VALUE (arglist);
10319 /* New argument list transforming fputs(string, stream) to
10320 fwrite(string, 1, len, stream). */
10321 arglist = build_tree_list (NULL_TREE,
10322 TREE_VALUE (TREE_CHAIN (arglist)));
10323 arglist = tree_cons (NULL_TREE, len, arglist);
10324 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
10325 arglist = tree_cons (NULL_TREE, string_arg, arglist);
10330 gcc_unreachable ();
10333 /* If the replacement _DECL isn't initialized, don't do the
10338 /* These optimizations are only performed when the result is ignored,
10339 hence there's no need to cast the result to integer_type_node. */
10340 return build_function_call_expr (fn, arglist);
10343 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
10344 produced. False otherwise. This is done so that we don't output the error
10345 or warning twice or three times. */
10347 fold_builtin_next_arg (tree arglist)
10349 tree fntype = TREE_TYPE (current_function_decl);
10351 if (TYPE_ARG_TYPES (fntype) == 0
10352 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
10353 == void_type_node))
10355 error ("%<va_start%> used in function with fixed args");
10360 /* Evidently an out of date version of <stdarg.h>; can't validate
10361 va_start's second argument, but can still work as intended. */
10362 warning (0, "%<__builtin_next_arg%> called without an argument");
10365 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10366 when we checked the arguments and if needed issued a warning. */
10367 else if (!TREE_CHAIN (arglist)
10368 || !integer_zerop (TREE_VALUE (arglist))
10369 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
10370 || TREE_CHAIN (TREE_CHAIN (arglist)))
10372 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10373 tree arg = TREE_VALUE (arglist);
10375 if (TREE_CHAIN (arglist))
10377 error ("%<va_start%> used with too many arguments");
10381 /* Strip off all nops for the sake of the comparison. This
10382 is not quite the same as STRIP_NOPS. It does more.
10383 We must also strip off INDIRECT_EXPR for C++ reference
10385 while (TREE_CODE (arg) == NOP_EXPR
10386 || TREE_CODE (arg) == CONVERT_EXPR
10387 || TREE_CODE (arg) == NON_LVALUE_EXPR
10388 || TREE_CODE (arg) == INDIRECT_REF)
10389 arg = TREE_OPERAND (arg, 0);
10390 if (arg != last_parm)
10392 /* FIXME: Sometimes with the tree optimizers we can get the
10393 not the last argument even though the user used the last
10394 argument. We just warn and set the arg to be the last
10395 argument so that we will get wrong-code because of
10397 warning (0, "second parameter of %<va_start%> not last named argument");
10399 /* We want to verify the second parameter just once before the tree
10400 optimizers are run and then avoid keeping it in the tree,
10401 as otherwise we could warn even for correct code like:
10402 void foo (int i, ...)
10403 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10404 TREE_VALUE (arglist) = integer_zero_node;
10405 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
10411 /* Simplify a call to the sprintf builtin.
10413 Return 0 if no simplification was possible, otherwise return the
10414 simplified form of the call as a tree. If IGNORED is true, it means that
10415 the caller does not use the returned value of the function. */
10418 fold_builtin_sprintf (tree arglist, int ignored)
10420 tree call, retval, dest, fmt;
10421 const char *fmt_str = NULL;
10423 /* Verify the required arguments in the original call. We deal with two
10424 types of sprintf() calls: 'sprintf (str, fmt)' and
10425 'sprintf (dest, "%s", orig)'. */
10426 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
10427 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
10431 /* Get the destination string and the format specifier. */
10432 dest = TREE_VALUE (arglist);
10433 fmt = TREE_VALUE (TREE_CHAIN (arglist));
10435 /* Check whether the format is a literal string constant. */
10436 fmt_str = c_getstr (fmt);
10437 if (fmt_str == NULL)
10441 retval = NULL_TREE;
10443 if (!init_target_chars())
10446 /* If the format doesn't contain % args or %%, use strcpy. */
10447 if (strchr (fmt_str, target_percent) == NULL)
10449 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10454 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
10455 'format' is known to contain no % formats. */
10456 arglist = build_tree_list (NULL_TREE, fmt);
10457 arglist = tree_cons (NULL_TREE, dest, arglist);
10458 call = build_function_call_expr (fn, arglist);
10460 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
10463 /* If the format is "%s", use strcpy if the result isn't used. */
10464 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
10467 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10472 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
10473 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10474 arglist = build_tree_list (NULL_TREE, orig);
10475 arglist = tree_cons (NULL_TREE, dest, arglist);
10478 retval = c_strlen (orig, 1);
10479 if (!retval || TREE_CODE (retval) != INTEGER_CST)
10482 call = build_function_call_expr (fn, arglist);
10485 if (call && retval)
10487 retval = fold_convert
10488 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
10490 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
10496 /* Expand a call to __builtin_object_size. */
10499 expand_builtin_object_size (tree exp)
10502 int object_size_type;
10503 tree fndecl = get_callee_fndecl (exp);
10504 tree arglist = TREE_OPERAND (exp, 1);
10505 location_t locus = EXPR_LOCATION (exp);
10507 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10509 error ("%Hfirst argument of %D must be a pointer, second integer constant",
10511 expand_builtin_trap ();
10515 ost = TREE_VALUE (TREE_CHAIN (arglist));
10518 if (TREE_CODE (ost) != INTEGER_CST
10519 || tree_int_cst_sgn (ost) < 0
10520 || compare_tree_int (ost, 3) > 0)
10522 error ("%Hlast argument of %D is not integer constant between 0 and 3",
10524 expand_builtin_trap ();
10528 object_size_type = tree_low_cst (ost, 0);
10530 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10533 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10534 FCODE is the BUILT_IN_* to use.
10535 Return 0 if we failed; the caller should emit a normal call,
10536 otherwise try to get the result in TARGET, if convenient (and in
10537 mode MODE if that's convenient). */
10540 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
10541 enum built_in_function fcode)
10543 tree arglist = TREE_OPERAND (exp, 1);
10544 tree dest, src, len, size;
10546 if (!validate_arglist (arglist,
10548 fcode == BUILT_IN_MEMSET_CHK
10549 ? INTEGER_TYPE : POINTER_TYPE,
10550 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10553 dest = TREE_VALUE (arglist);
10554 src = TREE_VALUE (TREE_CHAIN (arglist));
10555 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10556 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10558 if (! host_integerp (size, 1))
10561 if (host_integerp (len, 1) || integer_all_onesp (size))
10565 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10567 location_t locus = EXPR_LOCATION (exp);
10568 warning (0, "%Hcall to %D will always overflow destination buffer",
10569 &locus, get_callee_fndecl (exp));
10573 arglist = build_tree_list (NULL_TREE, len);
10574 arglist = tree_cons (NULL_TREE, src, arglist);
10575 arglist = tree_cons (NULL_TREE, dest, arglist);
10578 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10579 mem{cpy,pcpy,move,set} is available. */
10582 case BUILT_IN_MEMCPY_CHK:
10583 fn = built_in_decls[BUILT_IN_MEMCPY];
10585 case BUILT_IN_MEMPCPY_CHK:
10586 fn = built_in_decls[BUILT_IN_MEMPCPY];
10588 case BUILT_IN_MEMMOVE_CHK:
10589 fn = built_in_decls[BUILT_IN_MEMMOVE];
10591 case BUILT_IN_MEMSET_CHK:
10592 fn = built_in_decls[BUILT_IN_MEMSET];
10601 fn = build_function_call_expr (fn, arglist);
10602 if (TREE_CODE (fn) == CALL_EXPR)
10603 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10604 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10606 else if (fcode == BUILT_IN_MEMSET_CHK)
10610 unsigned int dest_align
10611 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10613 /* If DEST is not a pointer type, call the normal function. */
10614 if (dest_align == 0)
10617 /* If SRC and DEST are the same (and not volatile), do nothing. */
10618 if (operand_equal_p (src, dest, 0))
10622 if (fcode != BUILT_IN_MEMPCPY_CHK)
10624 /* Evaluate and ignore LEN in case it has side-effects. */
10625 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10626 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10629 len = fold_convert (TREE_TYPE (dest), len);
10630 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10631 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10634 /* __memmove_chk special case. */
10635 if (fcode == BUILT_IN_MEMMOVE_CHK)
10637 unsigned int src_align
10638 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10640 if (src_align == 0)
10643 /* If src is categorized for a readonly section we can use
10644 normal __memcpy_chk. */
10645 if (readonly_data_expr (src))
10647 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10650 fn = build_function_call_expr (fn, arglist);
10651 if (TREE_CODE (fn) == CALL_EXPR)
10652 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10653 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10660 /* Emit warning if a buffer overflow is detected at compile time. */
10663 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10665 int arg_mask, is_strlen = 0;
10666 tree arglist = TREE_OPERAND (exp, 1), a;
10672 case BUILT_IN_STRCPY_CHK:
10673 case BUILT_IN_STPCPY_CHK:
10674 /* For __strcat_chk the warning will be emitted only if overflowing
10675 by at least strlen (dest) + 1 bytes. */
10676 case BUILT_IN_STRCAT_CHK:
10680 case BUILT_IN_STRNCAT_CHK:
10681 /* For __strncat_chk the warning will be emitted only if overflowing
10682 by at least strlen (dest) + 1 bytes. */
10685 case BUILT_IN_STRNCPY_CHK:
10688 case BUILT_IN_SNPRINTF_CHK:
10689 case BUILT_IN_VSNPRINTF_CHK:
10693 gcc_unreachable ();
10698 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10710 len = TREE_VALUE (len);
10711 size = TREE_VALUE (size);
10713 if (! host_integerp (size, 1) || integer_all_onesp (size))
10718 len = c_strlen (len, 1);
10719 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10722 else if (fcode == BUILT_IN_STRNCAT_CHK)
10724 tree src = TREE_VALUE (TREE_CHAIN (arglist));
10725 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10727 src = c_strlen (src, 1);
10728 if (! src || ! host_integerp (src, 1))
10730 locus = EXPR_LOCATION (exp);
10731 warning (0, "%Hcall to %D might overflow destination buffer",
10732 &locus, get_callee_fndecl (exp));
10735 else if (tree_int_cst_lt (src, size))
10738 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10741 locus = EXPR_LOCATION (exp);
10742 warning (0, "%Hcall to %D will always overflow destination buffer",
10743 &locus, get_callee_fndecl (exp));
10746 /* Emit warning if a buffer overflow is detected at compile time
10747 in __sprintf_chk/__vsprintf_chk calls. */
10750 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10752 tree arglist = TREE_OPERAND (exp, 1);
10753 tree dest, size, len, fmt, flag;
10754 const char *fmt_str;
10756 /* Verify the required arguments in the original call. */
10759 dest = TREE_VALUE (arglist);
10760 arglist = TREE_CHAIN (arglist);
10763 flag = TREE_VALUE (arglist);
10764 arglist = TREE_CHAIN (arglist);
10767 size = TREE_VALUE (arglist);
10768 arglist = TREE_CHAIN (arglist);
10771 fmt = TREE_VALUE (arglist);
10772 arglist = TREE_CHAIN (arglist);
10774 if (! host_integerp (size, 1) || integer_all_onesp (size))
10777 /* Check whether the format is a literal string constant. */
10778 fmt_str = c_getstr (fmt);
10779 if (fmt_str == NULL)
10782 if (!init_target_chars())
10785 /* If the format doesn't contain % args or %%, we know its size. */
10786 if (strchr (fmt_str, target_percent) == 0)
10787 len = build_int_cstu (size_type_node, strlen (fmt_str));
10788 /* If the format is "%s" and first ... argument is a string literal,
10790 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10796 arg = TREE_VALUE (arglist);
10797 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10800 len = c_strlen (arg, 1);
10801 if (!len || ! host_integerp (len, 1))
10807 if (! tree_int_cst_lt (len, size))
10809 location_t locus = EXPR_LOCATION (exp);
10810 warning (0, "%Hcall to %D will always overflow destination buffer",
10811 &locus, get_callee_fndecl (exp));
10815 /* Fold a call to __builtin_object_size, if possible. */
10818 fold_builtin_object_size (tree arglist)
10820 tree ptr, ost, ret = 0;
10821 int object_size_type;
10823 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10826 ptr = TREE_VALUE (arglist);
10827 ost = TREE_VALUE (TREE_CHAIN (arglist));
10830 if (TREE_CODE (ost) != INTEGER_CST
10831 || tree_int_cst_sgn (ost) < 0
10832 || compare_tree_int (ost, 3) > 0)
10835 object_size_type = tree_low_cst (ost, 0);
10837 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10838 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10839 and (size_t) 0 for types 2 and 3. */
10840 if (TREE_SIDE_EFFECTS (ptr))
10841 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10843 if (TREE_CODE (ptr) == ADDR_EXPR)
10844 ret = build_int_cstu (size_type_node,
10845 compute_builtin_object_size (ptr, object_size_type));
10847 else if (TREE_CODE (ptr) == SSA_NAME)
10849 unsigned HOST_WIDE_INT bytes;
10851 /* If object size is not known yet, delay folding until
10852 later. Maybe subsequent passes will help determining
10854 bytes = compute_builtin_object_size (ptr, object_size_type);
10855 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10857 ret = build_int_cstu (size_type_node, bytes);
10862 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret);
10863 HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret);
10864 if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret)))
10871 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10872 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10873 code of the builtin. If MAXLEN is not NULL, it is maximum length
10874 passed as third argument. */
10877 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10878 enum built_in_function fcode)
10880 tree dest, src, len, size, fn;
10882 if (!validate_arglist (arglist,
10884 fcode == BUILT_IN_MEMSET_CHK
10885 ? INTEGER_TYPE : POINTER_TYPE,
10886 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10889 dest = TREE_VALUE (arglist);
10890 /* Actually val for __memset_chk, but it doesn't matter. */
10891 src = TREE_VALUE (TREE_CHAIN (arglist));
10892 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10893 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10895 /* If SRC and DEST are the same (and not volatile), return DEST
10896 (resp. DEST+LEN for __mempcpy_chk). */
10897 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10899 if (fcode != BUILT_IN_MEMPCPY_CHK)
10900 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10903 tree temp = fold_convert (TREE_TYPE (dest), len);
10904 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10905 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10909 if (! host_integerp (size, 1))
10912 if (! integer_all_onesp (size))
10914 if (! host_integerp (len, 1))
10916 /* If LEN is not constant, try MAXLEN too.
10917 For MAXLEN only allow optimizing into non-_ocs function
10918 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10919 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10921 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10923 /* (void) __mempcpy_chk () can be optimized into
10924 (void) __memcpy_chk (). */
10925 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10929 return build_function_call_expr (fn, arglist);
10937 if (tree_int_cst_lt (size, maxlen))
10941 arglist = build_tree_list (NULL_TREE, len);
10942 arglist = tree_cons (NULL_TREE, src, arglist);
10943 arglist = tree_cons (NULL_TREE, dest, arglist);
10946 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10947 mem{cpy,pcpy,move,set} is available. */
10950 case BUILT_IN_MEMCPY_CHK:
10951 fn = built_in_decls[BUILT_IN_MEMCPY];
10953 case BUILT_IN_MEMPCPY_CHK:
10954 fn = built_in_decls[BUILT_IN_MEMPCPY];
10956 case BUILT_IN_MEMMOVE_CHK:
10957 fn = built_in_decls[BUILT_IN_MEMMOVE];
10959 case BUILT_IN_MEMSET_CHK:
10960 fn = built_in_decls[BUILT_IN_MEMSET];
10969 return build_function_call_expr (fn, arglist);
10972 /* Fold a call to the __st[rp]cpy_chk builtin.
10973 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10974 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10975 strings passed as second argument. */
10978 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10979 enum built_in_function fcode)
10981 tree dest, src, size, len, fn;
10983 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10987 dest = TREE_VALUE (arglist);
10988 src = TREE_VALUE (TREE_CHAIN (arglist));
10989 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10991 /* If SRC and DEST are the same (and not volatile), return DEST. */
10992 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10993 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10995 if (! host_integerp (size, 1))
10998 if (! integer_all_onesp (size))
11000 len = c_strlen (src, 1);
11001 if (! len || ! host_integerp (len, 1))
11003 /* If LEN is not constant, try MAXLEN too.
11004 For MAXLEN only allow optimizing into non-_ocs function
11005 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11006 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11008 if (fcode == BUILT_IN_STPCPY_CHK)
11013 /* If return value of __stpcpy_chk is ignored,
11014 optimize into __strcpy_chk. */
11015 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
11019 return build_function_call_expr (fn, arglist);
11022 if (! len || TREE_SIDE_EFFECTS (len))
11025 /* If c_strlen returned something, but not a constant,
11026 transform __strcpy_chk into __memcpy_chk. */
11027 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11031 len = size_binop (PLUS_EXPR, len, ssize_int (1));
11032 arglist = build_tree_list (NULL_TREE, size);
11033 arglist = tree_cons (NULL_TREE, len, arglist);
11034 arglist = tree_cons (NULL_TREE, src, arglist);
11035 arglist = tree_cons (NULL_TREE, dest, arglist);
11036 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
11037 build_function_call_expr (fn, arglist));
11043 if (! tree_int_cst_lt (maxlen, size))
11047 arglist = build_tree_list (NULL_TREE, src);
11048 arglist = tree_cons (NULL_TREE, dest, arglist);
11050 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
11051 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
11052 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
11056 return build_function_call_expr (fn, arglist);
11059 /* Fold a call to the __strncpy_chk builtin.
11060 If MAXLEN is not NULL, it is maximum length passed as third argument. */
11063 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
11065 tree dest, src, size, len, fn;
11067 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11068 INTEGER_TYPE, VOID_TYPE))
11071 dest = TREE_VALUE (arglist);
11072 src = TREE_VALUE (TREE_CHAIN (arglist));
11073 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11074 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11076 if (! host_integerp (size, 1))
11079 if (! integer_all_onesp (size))
11081 if (! host_integerp (len, 1))
11083 /* If LEN is not constant, try MAXLEN too.
11084 For MAXLEN only allow optimizing into non-_ocs function
11085 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11086 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11092 if (tree_int_cst_lt (size, maxlen))
11096 arglist = build_tree_list (NULL_TREE, len);
11097 arglist = tree_cons (NULL_TREE, src, arglist);
11098 arglist = tree_cons (NULL_TREE, dest, arglist);
11100 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
11101 fn = built_in_decls[BUILT_IN_STRNCPY];
11105 return build_function_call_expr (fn, arglist);
11108 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
11111 fold_builtin_strcat_chk (tree fndecl, tree arglist)
11113 tree dest, src, size, fn;
11116 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11120 dest = TREE_VALUE (arglist);
11121 src = TREE_VALUE (TREE_CHAIN (arglist));
11122 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11124 p = c_getstr (src);
11125 /* If the SRC parameter is "", return DEST. */
11126 if (p && *p == '\0')
11127 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11129 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
11132 arglist = build_tree_list (NULL_TREE, src);
11133 arglist = tree_cons (NULL_TREE, dest, arglist);
11135 /* If __builtin_strcat_chk is used, assume strcat is available. */
11136 fn = built_in_decls[BUILT_IN_STRCAT];
11140 return build_function_call_expr (fn, arglist);
11143 /* Fold a call to the __strncat_chk builtin EXP. */
11146 fold_builtin_strncat_chk (tree fndecl, tree arglist)
11148 tree dest, src, size, len, fn;
11151 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
11152 INTEGER_TYPE, VOID_TYPE))
11155 dest = TREE_VALUE (arglist);
11156 src = TREE_VALUE (TREE_CHAIN (arglist));
11157 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11158 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
11160 p = c_getstr (src);
11161 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
11162 if (p && *p == '\0')
11163 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
11164 else if (integer_zerop (len))
11165 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
11167 if (! host_integerp (size, 1))
11170 if (! integer_all_onesp (size))
11172 tree src_len = c_strlen (src, 1);
11174 && host_integerp (src_len, 1)
11175 && host_integerp (len, 1)
11176 && ! tree_int_cst_lt (len, src_len))
11178 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
11179 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
11183 arglist = build_tree_list (NULL_TREE, size);
11184 arglist = tree_cons (NULL_TREE, src, arglist);
11185 arglist = tree_cons (NULL_TREE, dest, arglist);
11186 return build_function_call_expr (fn, arglist);
11191 arglist = build_tree_list (NULL_TREE, len);
11192 arglist = tree_cons (NULL_TREE, src, arglist);
11193 arglist = tree_cons (NULL_TREE, dest, arglist);
11195 /* If __builtin_strncat_chk is used, assume strncat is available. */
11196 fn = built_in_decls[BUILT_IN_STRNCAT];
11200 return build_function_call_expr (fn, arglist);
11203 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
11204 a normal call should be emitted rather than expanding the function
11205 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
11208 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
11210 tree dest, size, len, fn, fmt, flag;
11211 const char *fmt_str;
11213 /* Verify the required arguments in the original call. */
11216 dest = TREE_VALUE (arglist);
11217 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11219 arglist = TREE_CHAIN (arglist);
11222 flag = TREE_VALUE (arglist);
11223 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
11225 arglist = TREE_CHAIN (arglist);
11228 size = TREE_VALUE (arglist);
11229 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11231 arglist = TREE_CHAIN (arglist);
11234 fmt = TREE_VALUE (arglist);
11235 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11237 arglist = TREE_CHAIN (arglist);
11239 if (! host_integerp (size, 1))
11244 if (!init_target_chars())
11247 /* Check whether the format is a literal string constant. */
11248 fmt_str = c_getstr (fmt);
11249 if (fmt_str != NULL)
11251 /* If the format doesn't contain % args or %%, we know the size. */
11252 if (strchr (fmt_str, target_percent) == 0)
11254 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
11255 len = build_int_cstu (size_type_node, strlen (fmt_str));
11257 /* If the format is "%s" and first ... argument is a string literal,
11258 we know the size too. */
11259 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
11263 if (arglist && !TREE_CHAIN (arglist))
11265 arg = TREE_VALUE (arglist);
11266 if (POINTER_TYPE_P (TREE_TYPE (arg)))
11268 len = c_strlen (arg, 1);
11269 if (! len || ! host_integerp (len, 1))
11276 if (! integer_all_onesp (size))
11278 if (! len || ! tree_int_cst_lt (len, size))
11282 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
11283 or if format doesn't contain % chars or is "%s". */
11284 if (! integer_zerop (flag))
11286 if (fmt_str == NULL)
11288 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11292 arglist = tree_cons (NULL_TREE, fmt, arglist);
11293 arglist = tree_cons (NULL_TREE, dest, arglist);
11295 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
11296 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
11297 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
11301 return build_function_call_expr (fn, arglist);
11304 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
11305 a normal call should be emitted rather than expanding the function
11306 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
11307 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
11308 passed as second argument. */
11311 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
11312 enum built_in_function fcode)
11314 tree dest, size, len, fn, fmt, flag;
11315 const char *fmt_str;
11317 /* Verify the required arguments in the original call. */
11320 dest = TREE_VALUE (arglist);
11321 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
11323 arglist = TREE_CHAIN (arglist);
11326 len = TREE_VALUE (arglist);
11327 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11329 arglist = TREE_CHAIN (arglist);
11332 flag = TREE_VALUE (arglist);
11333 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
11335 arglist = TREE_CHAIN (arglist);
11338 size = TREE_VALUE (arglist);
11339 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
11341 arglist = TREE_CHAIN (arglist);
11344 fmt = TREE_VALUE (arglist);
11345 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11347 arglist = TREE_CHAIN (arglist);
11349 if (! host_integerp (size, 1))
11352 if (! integer_all_onesp (size))
11354 if (! host_integerp (len, 1))
11356 /* If LEN is not constant, try MAXLEN too.
11357 For MAXLEN only allow optimizing into non-_ocs function
11358 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
11359 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
11365 if (tree_int_cst_lt (size, maxlen))
11369 if (!init_target_chars())
11372 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
11373 or if format doesn't contain % chars or is "%s". */
11374 if (! integer_zerop (flag))
11376 fmt_str = c_getstr (fmt);
11377 if (fmt_str == NULL)
11379 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
11383 arglist = tree_cons (NULL_TREE, fmt, arglist);
11384 arglist = tree_cons (NULL_TREE, len, arglist);
11385 arglist = tree_cons (NULL_TREE, dest, arglist);
11387 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
11389 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
11390 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
11394 return build_function_call_expr (fn, arglist);
11397 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11399 Return 0 if no simplification was possible, otherwise return the
11400 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11401 code of the function to be simplified. */
11404 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
11405 enum built_in_function fcode)
11407 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
11408 const char *fmt_str = NULL;
11410 /* If the return value is used, don't do the transformation. */
11414 /* Verify the required arguments in the original call. */
11415 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
11421 flag = TREE_VALUE (arglist);
11422 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11423 || TREE_SIDE_EFFECTS (flag))
11425 arglist = TREE_CHAIN (arglist);
11430 fmt = TREE_VALUE (arglist);
11431 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11433 arglist = TREE_CHAIN (arglist);
11435 /* Check whether the format is a literal string constant. */
11436 fmt_str = c_getstr (fmt);
11437 if (fmt_str == NULL)
11440 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11442 /* If we're using an unlocked function, assume the other
11443 unlocked functions exist explicitly. */
11444 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
11445 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
11449 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
11450 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
11453 if (!init_target_chars())
11456 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
11460 if (strcmp (fmt_str, target_percent_s) == 0)
11462 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11466 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11467 || TREE_CHAIN (arglist))
11470 str = c_getstr (TREE_VALUE (arglist));
11476 /* The format specifier doesn't contain any '%' characters. */
11477 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11483 /* If the string was "", printf does nothing. */
11484 if (str[0] == '\0')
11485 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11487 /* If the string has length of 1, call putchar. */
11488 if (str[1] == '\0')
11490 /* Given printf("c"), (where c is any one character,)
11491 convert "c"[0] to an int and pass that to the replacement
11493 arg = build_int_cst (NULL_TREE, str[0]);
11494 arglist = build_tree_list (NULL_TREE, arg);
11499 /* If the string was "string\n", call puts("string"). */
11500 size_t len = strlen (str);
11501 if ((unsigned char)str[len - 1] == target_newline)
11503 /* Create a NUL-terminated string that's one char shorter
11504 than the original, stripping off the trailing '\n'. */
11505 char *newstr = alloca (len);
11506 memcpy (newstr, str, len - 1);
11507 newstr[len - 1] = 0;
11509 arg = build_string_literal (len, newstr);
11510 arglist = build_tree_list (NULL_TREE, arg);
11514 /* We'd like to arrange to call fputs(string,stdout) here,
11515 but we need stdout and don't have a way to get it yet. */
11520 /* The other optimizations can be done only on the non-va_list variants. */
11521 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11524 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11525 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11528 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11529 || TREE_CHAIN (arglist))
11534 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11535 else if (strcmp (fmt_str, target_percent_c) == 0)
11538 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11539 || TREE_CHAIN (arglist))
11547 call = build_function_call_expr (fn, arglist);
11548 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11551 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11553 Return 0 if no simplification was possible, otherwise return the
11554 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11555 code of the function to be simplified. */
11558 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
11559 enum built_in_function fcode)
11561 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
11562 const char *fmt_str = NULL;
11564 /* If the return value is used, don't do the transformation. */
11568 /* Verify the required arguments in the original call. */
11571 fp = TREE_VALUE (arglist);
11572 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11574 arglist = TREE_CHAIN (arglist);
11576 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11582 flag = TREE_VALUE (arglist);
11583 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11584 || TREE_SIDE_EFFECTS (flag))
11586 arglist = TREE_CHAIN (arglist);
11591 fmt = TREE_VALUE (arglist);
11592 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11594 arglist = TREE_CHAIN (arglist);
11596 /* Check whether the format is a literal string constant. */
11597 fmt_str = c_getstr (fmt);
11598 if (fmt_str == NULL)
11601 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11603 /* If we're using an unlocked function, assume the other
11604 unlocked functions exist explicitly. */
11605 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11606 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11610 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11611 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11614 if (!init_target_chars())
11617 /* If the format doesn't contain % args or %%, use strcpy. */
11618 if (strchr (fmt_str, target_percent) == NULL)
11620 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11624 /* If the format specifier was "", fprintf does nothing. */
11625 if (fmt_str[0] == '\0')
11627 /* If FP has side-effects, just wait until gimplification is
11629 if (TREE_SIDE_EFFECTS (fp))
11632 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11635 /* When "string" doesn't contain %, replace all cases of
11636 fprintf (fp, string) with fputs (string, fp). The fputs
11637 builtin will take care of special cases like length == 1. */
11638 arglist = build_tree_list (NULL_TREE, fp);
11639 arglist = tree_cons (NULL_TREE, fmt, arglist);
11643 /* The other optimizations can be done only on the non-va_list variants. */
11644 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11647 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11648 else if (strcmp (fmt_str, target_percent_s) == 0)
11651 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11652 || TREE_CHAIN (arglist))
11654 arg = TREE_VALUE (arglist);
11655 arglist = build_tree_list (NULL_TREE, fp);
11656 arglist = tree_cons (NULL_TREE, arg, arglist);
11660 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11661 else if (strcmp (fmt_str, target_percent_c) == 0)
11664 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11665 || TREE_CHAIN (arglist))
11667 arg = TREE_VALUE (arglist);
11668 arglist = build_tree_list (NULL_TREE, fp);
11669 arglist = tree_cons (NULL_TREE, arg, arglist);
11676 call = build_function_call_expr (fn, arglist);
11677 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11680 /* Initialize format string characters in the target charset. */
11683 init_target_chars (void)
11688 target_newline = lang_hooks.to_target_charset ('\n');
11689 target_percent = lang_hooks.to_target_charset ('%');
11690 target_c = lang_hooks.to_target_charset ('c');
11691 target_s = lang_hooks.to_target_charset ('s');
11692 if (target_newline == 0 || target_percent == 0 || target_c == 0
11696 target_percent_c[0] = target_percent;
11697 target_percent_c[1] = target_c;
11698 target_percent_c[2] = '\0';
11700 target_percent_s[0] = target_percent;
11701 target_percent_s[1] = target_s;
11702 target_percent_s[2] = '\0';
11704 target_percent_s_newline[0] = target_percent;
11705 target_percent_s_newline[1] = target_s;
11706 target_percent_s_newline[2] = target_newline;
11707 target_percent_s_newline[3] = '\0';
11714 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11715 and no overflow/underflow occurred. INEXACT is true if M was not
11716 exactly calculated. TYPE is the tree type for the result. This
11717 function assumes that you cleared the MPFR flags and then
11718 calculated M to see if anything subsequently set a flag prior to
11719 entering this function. Return NULL_TREE if any checks fail. */
11722 do_mpfr_ckconv(mpfr_srcptr m, tree type, int inexact)
11724 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11725 overflow/underflow occurred. If -frounding-math, proceed iff the
11726 result of calling FUNC was exact. */
11727 if (mpfr_number_p (m) && !mpfr_overflow_p() && !mpfr_underflow_p()
11728 && (!flag_rounding_math || !inexact))
11730 REAL_VALUE_TYPE rr;
11732 real_from_mpfr (&rr, m);
11733 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11734 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11735 but the mpft_t is not, then we underflowed in the
11737 if (!real_isnan (&rr) && !real_isinf (&rr)
11738 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11740 REAL_VALUE_TYPE rmode;
11742 real_convert (&rmode, TYPE_MODE (type), &rr);
11743 /* Proceed iff the specified mode can hold the value. */
11744 if (real_identical (&rmode, &rr))
11745 return build_real (type, rmode);
11751 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11752 FUNC on it and return the resulting value as a tree with type TYPE.
11753 If MIN and/or MAX are not NULL, then the supplied ARG must be
11754 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11755 acceptable values, otherwise they are not. The mpfr precision is
11756 set to the precision of TYPE. We assume that function FUNC returns
11757 zero if the result could be calculated exactly within the requested
11761 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11762 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11765 tree result = NULL_TREE;
11769 /* To proceed, MPFR must exactly represent the target floating point
11770 format, which only happens when the target base equals two. */
11771 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11772 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11774 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11776 if (!real_isnan (ra) && !real_isinf (ra)
11777 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11778 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11780 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11784 mpfr_init2 (m, prec);
11785 mpfr_from_real (m, ra);
11786 mpfr_clear_flags();
11787 inexact = func (m, m, GMP_RNDN);
11788 result = do_mpfr_ckconv (m, type, inexact);
11796 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11797 FUNC on it and return the resulting value as a tree with type TYPE.
11798 The mpfr precision is set to the precision of TYPE. We assume that
11799 function FUNC returns zero if the result could be calculated
11800 exactly within the requested precision. */
11803 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11804 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11806 tree result = NULL_TREE;
11811 /* To proceed, MPFR must exactly represent the target floating point
11812 format, which only happens when the target base equals two. */
11813 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11814 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11815 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11817 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11818 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11820 if (!real_isnan (ra1) && !real_isinf (ra1)
11821 && !real_isnan (ra2) && !real_isinf (ra2))
11823 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11827 mpfr_inits2 (prec, m1, m2, NULL);
11828 mpfr_from_real (m1, ra1);
11829 mpfr_from_real (m2, ra2);
11830 mpfr_clear_flags();
11831 inexact = func (m1, m1, m2, GMP_RNDN);
11832 result = do_mpfr_ckconv (m1, type, inexact);
11833 mpfr_clears (m1, m2, NULL);
11840 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11841 FUNC on it and return the resulting value as a tree with type TYPE.
11842 The mpfr precision is set to the precision of TYPE. We assume that
11843 function FUNC returns zero if the result could be calculated
11844 exactly within the requested precision. */
11847 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11848 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11850 tree result = NULL_TREE;
11856 /* To proceed, MPFR must exactly represent the target floating point
11857 format, which only happens when the target base equals two. */
11858 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11859 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11860 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11861 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11863 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11864 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11865 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11867 if (!real_isnan (ra1) && !real_isinf (ra1)
11868 && !real_isnan (ra2) && !real_isinf (ra2)
11869 && !real_isnan (ra3) && !real_isinf (ra3))
11871 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11875 mpfr_inits2 (prec, m1, m2, m3, NULL);
11876 mpfr_from_real (m1, ra1);
11877 mpfr_from_real (m2, ra2);
11878 mpfr_from_real (m3, ra3);
11879 mpfr_clear_flags();
11880 inexact = func (m1, m1, m2, m3, GMP_RNDN);
11881 result = do_mpfr_ckconv (m1, type, inexact);
11882 mpfr_clears (m1, m2, m3, NULL);
11889 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11890 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11891 If ARG_SINP and ARG_COSP are NULL then the result is returned
11892 as a complex value.
11893 The type is taken from the type of ARG and is used for setting the
11894 precision of the calculation and results. */
11897 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11899 tree const type = TREE_TYPE (arg);
11900 tree result = NULL_TREE;
11904 /* To proceed, MPFR must exactly represent the target floating point
11905 format, which only happens when the target base equals two. */
11906 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11907 && TREE_CODE (arg) == REAL_CST
11908 && !TREE_OVERFLOW (arg))
11910 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11912 if (!real_isnan (ra) && !real_isinf (ra))
11914 const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
11915 tree result_s, result_c;
11919 mpfr_inits2 (prec, m, ms, mc, NULL);
11920 mpfr_from_real (m, ra);
11921 mpfr_clear_flags();
11922 inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN);
11923 result_s = do_mpfr_ckconv (ms, type, inexact);
11924 result_c = do_mpfr_ckconv (mc, type, inexact);
11925 mpfr_clears (m, ms, mc, NULL);
11926 if (result_s && result_c)
11928 /* If we are to return in a complex value do so. */
11929 if (!arg_sinp && !arg_cosp)
11930 return build_complex (build_complex_type (type),
11931 result_c, result_s);
11933 /* Dereference the sin/cos pointer arguments. */
11934 arg_sinp = build_fold_indirect_ref (arg_sinp);
11935 arg_cosp = build_fold_indirect_ref (arg_cosp);
11936 /* Proceed if valid pointer type were passed in. */
11937 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11938 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11940 /* Set the values. */
11941 result_s = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_sinp,
11943 TREE_SIDE_EFFECTS (result_s) = 1;
11944 result_c = fold_build2 (GIMPLE_MODIFY_STMT, type, arg_cosp,
11946 TREE_SIDE_EFFECTS (result_c) = 1;
11947 /* Combine the assignments into a compound expr. */
11948 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11949 result_s, result_c));