Mark ChangeLog
[official-gcc.git] / gcc / builtins.c
blob7969174d51850ff6ee9599b7b2acd73c410fe426
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "real.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tree-gimple.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "typeclass.h"
43 #include "toplev.h"
44 #include "predict.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "basic-block.h"
49 #include "tree-mudflap.h"
51 #define CALLED_AS_BUILT_IN(NODE) \
52 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
54 #ifndef PAD_VARARGS_DOWN
55 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
56 #endif
58 /* Define the names of the builtin function types and codes. */
59 const char *const built_in_class_names[4]
60 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
62 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
63 const char * built_in_names[(int) END_BUILTINS] =
65 #include "builtins.def"
67 #undef DEF_BUILTIN
69 /* Setup an array of _DECL trees, make sure each element is
70 initialized to NULL_TREE. */
71 tree built_in_decls[(int) END_BUILTINS];
72 /* Declarations used when constructing the builtin implicitly in the compiler.
73 It may be NULL_TREE when this is invalid (for instance runtime is not
74 required to implement the function call in all cases). */
75 tree implicit_built_in_decls[(int) END_BUILTINS];
77 static int get_pointer_alignment (tree, unsigned int);
78 static const char *c_getstr (tree);
79 static rtx c_readstr (const char *, enum machine_mode);
80 static int target_char_cast (tree, char *);
81 static rtx get_memory_rtx (tree, tree);
82 static tree build_string_literal (int, const char *);
83 static int apply_args_size (void);
84 static int apply_result_size (void);
85 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
86 static rtx result_vector (int, rtx);
87 #endif
88 static rtx expand_builtin_setjmp (tree, rtx);
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (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, tree);
117 static rtx expand_builtin_bcopy (tree);
118 static rtx expand_builtin_strcpy (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_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
125 static rtx expand_builtin_bzero (tree);
126 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_alloca (tree, rtx);
132 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133 static rtx expand_builtin_frame_address (tree, tree);
134 static rtx expand_builtin_fputs (tree, rtx, bool);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list (tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (tree);
143 static tree fold_builtin_inf (tree, int);
144 static tree fold_builtin_nan (tree, tree, int);
145 static int validate_arglist (tree, ...);
146 static bool integer_valued_real_p (tree);
147 static tree fold_trunc_transparent_mathfn (tree);
148 static bool readonly_data_expr (tree);
149 static rtx expand_builtin_fabs (tree, rtx, rtx);
150 static rtx expand_builtin_signbit (tree, rtx);
151 static tree fold_builtin_cabs (tree, tree);
152 static tree fold_builtin_sqrt (tree, tree);
153 static tree fold_builtin_cbrt (tree, tree);
154 static tree fold_builtin_pow (tree, tree, tree);
155 static tree fold_builtin_powi (tree, tree, tree);
156 static tree fold_builtin_sin (tree);
157 static tree fold_builtin_cos (tree, tree, tree);
158 static tree fold_builtin_tan (tree);
159 static tree fold_builtin_atan (tree, tree);
160 static tree fold_builtin_trunc (tree);
161 static tree fold_builtin_floor (tree);
162 static tree fold_builtin_ceil (tree);
163 static tree fold_builtin_round (tree);
164 static tree fold_builtin_bitop (tree);
165 static tree fold_builtin_memcpy (tree);
166 static tree fold_builtin_mempcpy (tree, tree, int);
167 static tree fold_builtin_memmove (tree, tree);
168 static tree fold_builtin_strchr (tree, tree);
169 static tree fold_builtin_memcmp (tree);
170 static tree fold_builtin_strcmp (tree);
171 static tree fold_builtin_strncmp (tree);
172 static tree fold_builtin_signbit (tree);
173 static tree fold_builtin_copysign (tree, tree, tree);
174 static tree fold_builtin_isascii (tree);
175 static tree fold_builtin_toascii (tree);
176 static tree fold_builtin_isdigit (tree);
177 static tree fold_builtin_fabs (tree, tree);
178 static tree fold_builtin_abs (tree, tree);
179 static tree fold_builtin_unordered_cmp (tree, enum tree_code, enum tree_code);
180 static tree fold_builtin_1 (tree, bool);
182 static tree fold_builtin_strpbrk (tree, tree);
183 static tree fold_builtin_strstr (tree, tree);
184 static tree fold_builtin_strrchr (tree, tree);
185 static tree fold_builtin_strcat (tree);
186 static tree fold_builtin_strncat (tree);
187 static tree fold_builtin_strspn (tree);
188 static tree fold_builtin_strcspn (tree);
189 static tree fold_builtin_sprintf (tree, int);
190 static bool init_target_chars (void);
192 static unsigned HOST_WIDE_INT target_newline;
193 static unsigned HOST_WIDE_INT target_percent;
194 static unsigned HOST_WIDE_INT target_c;
195 static unsigned HOST_WIDE_INT target_s;
196 static char target_percent_c[3];
197 static char target_percent_s[3];
198 static char target_percent_s_newline[4];
200 /* Return the alignment in bits of EXP, a pointer valued expression.
201 But don't return more than MAX_ALIGN no matter what.
202 The alignment returned is, by default, the alignment of the thing that
203 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
205 Otherwise, look at the expression to see if we can do better, i.e., if the
206 expression is actually pointing at an object whose alignment is tighter. */
208 static int
209 get_pointer_alignment (tree exp, unsigned int max_align)
211 unsigned int align, inner;
213 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
214 return 0;
216 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
217 align = MIN (align, max_align);
219 while (1)
221 switch (TREE_CODE (exp))
223 case NOP_EXPR:
224 case CONVERT_EXPR:
225 case NON_LVALUE_EXPR:
226 exp = TREE_OPERAND (exp, 0);
227 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
228 return align;
230 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
231 align = MIN (inner, max_align);
232 break;
234 case PLUS_EXPR:
235 /* If sum of pointer + int, restrict our maximum alignment to that
236 imposed by the integer. If not, we can't do any better than
237 ALIGN. */
238 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
239 return align;
241 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
242 & (max_align / BITS_PER_UNIT - 1))
243 != 0)
244 max_align >>= 1;
246 exp = TREE_OPERAND (exp, 0);
247 break;
249 case ADDR_EXPR:
250 /* See what we are pointing at and look at its alignment. */
251 exp = TREE_OPERAND (exp, 0);
252 inner = max_align;
253 while (handled_component_p (exp))
255 /* Fields in a structure can be packed, honour DECL_ALIGN
256 of the FIELD_DECL. For all other references the conservative
257 alignment is the element type alignment. */
258 if (TREE_CODE (exp) == COMPONENT_REF)
259 inner = MIN (inner, DECL_ALIGN (TREE_OPERAND (exp, 1)));
260 else
261 inner = MIN (inner, TYPE_ALIGN (TREE_TYPE (exp)));
262 exp = TREE_OPERAND (exp, 0);
264 if (TREE_CODE (exp) == FUNCTION_DECL)
265 align = FUNCTION_BOUNDARY;
266 else if (DECL_P (exp))
267 align = MIN (inner, DECL_ALIGN (exp));
268 #ifdef CONSTANT_ALIGNMENT
269 else if (CONSTANT_CLASS_P (exp))
270 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
271 #endif
272 else
273 align = MIN (align, inner);
274 return MIN (align, max_align);
276 default:
277 return align;
282 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
283 way, because it could contain a zero byte in the middle.
284 TREE_STRING_LENGTH is the size of the character array, not the string.
286 ONLY_VALUE should be nonzero if the result is not going to be emitted
287 into the instruction stream and zero if it is going to be expanded.
288 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
289 is returned, otherwise NULL, since
290 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
291 evaluate the side-effects.
293 The value returned is of type `ssizetype'.
295 Unfortunately, string_constant can't access the values of const char
296 arrays with initializers, so neither can we do so here. */
298 tree
299 c_strlen (tree src, int only_value)
301 tree offset_node;
302 HOST_WIDE_INT offset;
303 int max;
304 const char *ptr;
306 STRIP_NOPS (src);
307 if (TREE_CODE (src) == COND_EXPR
308 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
310 tree len1, len2;
312 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
313 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
314 if (tree_int_cst_equal (len1, len2))
315 return len1;
318 if (TREE_CODE (src) == COMPOUND_EXPR
319 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
320 return c_strlen (TREE_OPERAND (src, 1), only_value);
322 src = string_constant (src, &offset_node);
323 if (src == 0)
324 return 0;
326 max = TREE_STRING_LENGTH (src) - 1;
327 ptr = TREE_STRING_POINTER (src);
329 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
331 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
332 compute the offset to the following null if we don't know where to
333 start searching for it. */
334 int i;
336 for (i = 0; i < max; i++)
337 if (ptr[i] == 0)
338 return 0;
340 /* We don't know the starting offset, but we do know that the string
341 has no internal zero bytes. We can assume that the offset falls
342 within the bounds of the string; otherwise, the programmer deserves
343 what he gets. Subtract the offset from the length of the string,
344 and return that. This would perhaps not be valid if we were dealing
345 with named arrays in addition to literal string constants. */
347 return size_diffop (size_int (max), offset_node);
350 /* We have a known offset into the string. Start searching there for
351 a null character if we can represent it as a single HOST_WIDE_INT. */
352 if (offset_node == 0)
353 offset = 0;
354 else if (! host_integerp (offset_node, 0))
355 offset = -1;
356 else
357 offset = tree_low_cst (offset_node, 0);
359 /* If the offset is known to be out of bounds, warn, and call strlen at
360 runtime. */
361 if (offset < 0 || offset > max)
363 warning ("offset outside bounds of constant string");
364 return 0;
367 /* Use strlen to search for the first zero byte. Since any strings
368 constructed with build_string will have nulls appended, we win even
369 if we get handed something like (char[4])"abcd".
371 Since OFFSET is our starting index into the string, no further
372 calculation is needed. */
373 return ssize_int (strlen (ptr + offset));
376 /* Return a char pointer for a C string if it is a string constant
377 or sum of string constant and integer constant. */
379 static const char *
380 c_getstr (tree src)
382 tree offset_node;
384 src = string_constant (src, &offset_node);
385 if (src == 0)
386 return 0;
388 if (offset_node == 0)
389 return TREE_STRING_POINTER (src);
390 else if (!host_integerp (offset_node, 1)
391 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
392 return 0;
394 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
397 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
398 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
400 static rtx
401 c_readstr (const char *str, enum machine_mode mode)
403 HOST_WIDE_INT c[2];
404 HOST_WIDE_INT ch;
405 unsigned int i, j;
407 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
409 c[0] = 0;
410 c[1] = 0;
411 ch = 1;
412 for (i = 0; i < GET_MODE_SIZE (mode); i++)
414 j = i;
415 if (WORDS_BIG_ENDIAN)
416 j = GET_MODE_SIZE (mode) - i - 1;
417 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
418 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
419 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
420 j *= BITS_PER_UNIT;
421 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
423 if (ch)
424 ch = (unsigned char) str[i];
425 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
427 return immed_double_const (c[0], c[1], mode);
430 /* Cast a target constant CST to target CHAR and if that value fits into
431 host char type, return zero and put that value into variable pointed by
432 P. */
434 static int
435 target_char_cast (tree cst, char *p)
437 unsigned HOST_WIDE_INT val, hostval;
439 if (!host_integerp (cst, 1)
440 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
441 return 1;
443 val = tree_low_cst (cst, 1);
444 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
445 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
447 hostval = val;
448 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
449 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
451 if (val != hostval)
452 return 1;
454 *p = hostval;
455 return 0;
458 /* Similar to save_expr, but assumes that arbitrary code is not executed
459 in between the multiple evaluations. In particular, we assume that a
460 non-addressable local variable will not be modified. */
462 static tree
463 builtin_save_expr (tree exp)
465 if (TREE_ADDRESSABLE (exp) == 0
466 && (TREE_CODE (exp) == PARM_DECL
467 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
468 return exp;
470 return save_expr (exp);
473 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
474 times to get the address of either a higher stack frame, or a return
475 address located within it (depending on FNDECL_CODE). */
477 static rtx
478 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
480 int i;
482 #ifdef INITIAL_FRAME_ADDRESS_RTX
483 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
484 #else
485 rtx tem = hard_frame_pointer_rtx;
486 #endif
488 /* Some machines need special handling before we can access
489 arbitrary frames. For example, on the sparc, we must first flush
490 all register windows to the stack. */
491 #ifdef SETUP_FRAME_ADDRESSES
492 if (count > 0)
493 SETUP_FRAME_ADDRESSES ();
494 #endif
496 /* On the sparc, the return address is not in the frame, it is in a
497 register. There is no way to access it off of the current frame
498 pointer, but it can be accessed off the previous frame pointer by
499 reading the value from the register window save area. */
500 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
501 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
502 count--;
503 #endif
505 /* Scan back COUNT frames to the specified frame. */
506 for (i = 0; i < count; i++)
508 /* Assume the dynamic chain pointer is in the word that the
509 frame address points to, unless otherwise specified. */
510 #ifdef DYNAMIC_CHAIN_ADDRESS
511 tem = DYNAMIC_CHAIN_ADDRESS (tem);
512 #endif
513 tem = memory_address (Pmode, tem);
514 tem = gen_rtx_MEM (Pmode, tem);
515 set_mem_alias_set (tem, get_frame_alias_set ());
516 tem = copy_to_reg (tem);
519 /* For __builtin_frame_address, return what we've got. */
520 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
521 return tem;
523 /* For __builtin_return_address, Get the return address from that
524 frame. */
525 #ifdef RETURN_ADDR_RTX
526 tem = RETURN_ADDR_RTX (count, tem);
527 #else
528 tem = memory_address (Pmode,
529 plus_constant (tem, GET_MODE_SIZE (Pmode)));
530 tem = gen_rtx_MEM (Pmode, tem);
531 set_mem_alias_set (tem, get_frame_alias_set ());
532 #endif
533 return tem;
536 /* Alias set used for setjmp buffer. */
537 static HOST_WIDE_INT setjmp_alias_set = -1;
539 /* Construct the leading half of a __builtin_setjmp call. Control will
540 return to RECEIVER_LABEL. This is used directly by sjlj exception
541 handling code. */
543 void
544 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
546 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
547 rtx stack_save;
548 rtx mem;
550 if (setjmp_alias_set == -1)
551 setjmp_alias_set = new_alias_set ();
553 buf_addr = convert_memory_address (Pmode, buf_addr);
555 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
557 /* We store the frame pointer and the address of receiver_label in
558 the buffer and use the rest of it for the stack save area, which
559 is machine-dependent. */
561 mem = gen_rtx_MEM (Pmode, buf_addr);
562 set_mem_alias_set (mem, setjmp_alias_set);
563 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
565 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
566 set_mem_alias_set (mem, setjmp_alias_set);
568 emit_move_insn (validize_mem (mem),
569 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
571 stack_save = gen_rtx_MEM (sa_mode,
572 plus_constant (buf_addr,
573 2 * GET_MODE_SIZE (Pmode)));
574 set_mem_alias_set (stack_save, setjmp_alias_set);
575 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
577 /* If there is further processing to do, do it. */
578 #ifdef HAVE_builtin_setjmp_setup
579 if (HAVE_builtin_setjmp_setup)
580 emit_insn (gen_builtin_setjmp_setup (buf_addr));
581 #endif
583 /* Tell optimize_save_area_alloca that extra work is going to
584 need to go on during alloca. */
585 current_function_calls_setjmp = 1;
587 /* Set this so all the registers get saved in our frame; we need to be
588 able to copy the saved values for any registers from frames we unwind. */
589 current_function_has_nonlocal_label = 1;
592 /* Construct the trailing part of a __builtin_setjmp call.
593 This is used directly by sjlj exception handling code. */
595 void
596 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
598 /* Clobber the FP when we get here, so we have to make sure it's
599 marked as used by this function. */
600 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
602 /* Mark the static chain as clobbered here so life information
603 doesn't get messed up for it. */
604 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
606 /* Now put in the code to restore the frame pointer, and argument
607 pointer, if needed. */
608 #ifdef HAVE_nonlocal_goto
609 if (! HAVE_nonlocal_goto)
610 #endif
611 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
613 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
614 if (fixed_regs[ARG_POINTER_REGNUM])
616 #ifdef ELIMINABLE_REGS
617 size_t i;
618 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
620 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
621 if (elim_regs[i].from == ARG_POINTER_REGNUM
622 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
623 break;
625 if (i == ARRAY_SIZE (elim_regs))
626 #endif
628 /* Now restore our arg pointer from the address at which it
629 was saved in our stack frame. */
630 emit_move_insn (virtual_incoming_args_rtx,
631 copy_to_reg (get_arg_pointer_save_area (cfun)));
634 #endif
636 #ifdef HAVE_builtin_setjmp_receiver
637 if (HAVE_builtin_setjmp_receiver)
638 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
639 else
640 #endif
641 #ifdef HAVE_nonlocal_goto_receiver
642 if (HAVE_nonlocal_goto_receiver)
643 emit_insn (gen_nonlocal_goto_receiver ());
644 else
645 #endif
646 { /* Nothing */ }
648 /* @@@ This is a kludge. Not all machine descriptions define a blockage
649 insn, but we must not allow the code we just generated to be reordered
650 by scheduling. Specifically, the update of the frame pointer must
651 happen immediately, not later. So emit an ASM_INPUT to act as blockage
652 insn. */
653 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
656 /* __builtin_setjmp is passed a pointer to an array of five words (not
657 all will be used on all machines). It operates similarly to the C
658 library function of the same name, but is more efficient. Much of
659 the code below (and for longjmp) is copied from the handling of
660 non-local gotos.
662 NOTE: This is intended for use by GNAT and the exception handling
663 scheme in the compiler and will only work in the method used by
664 them. */
666 static rtx
667 expand_builtin_setjmp (tree arglist, rtx target)
669 rtx buf_addr, next_lab, cont_lab;
671 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
672 return NULL_RTX;
674 if (target == 0 || !REG_P (target)
675 || REGNO (target) < FIRST_PSEUDO_REGISTER)
676 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
678 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
680 next_lab = gen_label_rtx ();
681 cont_lab = gen_label_rtx ();
683 expand_builtin_setjmp_setup (buf_addr, next_lab);
685 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
686 ensure that pending stack adjustments are flushed. */
687 emit_move_insn (target, const0_rtx);
688 emit_jump (cont_lab);
690 emit_label (next_lab);
692 /* Because setjmp and longjmp are not represented in the CFG, a cfgcleanup
693 may find that the basic block starting with NEXT_LAB is unreachable.
694 The whole block, along with NEXT_LAB, would be removed (see PR26983).
695 Make sure that never happens. */
696 LABEL_PRESERVE_P (next_lab) = 1;
698 expand_builtin_setjmp_receiver (next_lab);
700 /* Set TARGET to one. */
701 emit_move_insn (target, const1_rtx);
702 emit_label (cont_lab);
704 /* Tell flow about the strange goings on. Putting `next_lab' on
705 `nonlocal_goto_handler_labels' to indicates that function
706 calls may traverse the arc back to this label. */
708 current_function_has_nonlocal_label = 1;
709 nonlocal_goto_handler_labels
710 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
712 return target;
715 /* __builtin_longjmp is passed a pointer to an array of five words (not
716 all will be used on all machines). It operates similarly to the C
717 library function of the same name, but is more efficient. Much of
718 the code below is copied from the handling of non-local gotos.
720 NOTE: This is intended for use by GNAT and the exception handling
721 scheme in the compiler and will only work in the method used by
722 them. */
724 static void
725 expand_builtin_longjmp (rtx buf_addr, rtx value)
727 rtx fp, lab, stack, insn, last;
728 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
730 if (setjmp_alias_set == -1)
731 setjmp_alias_set = new_alias_set ();
733 buf_addr = convert_memory_address (Pmode, buf_addr);
735 buf_addr = force_reg (Pmode, buf_addr);
737 /* We used to store value in static_chain_rtx, but that fails if pointers
738 are smaller than integers. We instead require that the user must pass
739 a second argument of 1, because that is what builtin_setjmp will
740 return. This also makes EH slightly more efficient, since we are no
741 longer copying around a value that we don't care about. */
742 gcc_assert (value == const1_rtx);
744 last = get_last_insn ();
745 #ifdef HAVE_builtin_longjmp
746 if (HAVE_builtin_longjmp)
747 emit_insn (gen_builtin_longjmp (buf_addr));
748 else
749 #endif
751 fp = gen_rtx_MEM (Pmode, buf_addr);
752 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
753 GET_MODE_SIZE (Pmode)));
755 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
756 2 * GET_MODE_SIZE (Pmode)));
757 set_mem_alias_set (fp, setjmp_alias_set);
758 set_mem_alias_set (lab, setjmp_alias_set);
759 set_mem_alias_set (stack, setjmp_alias_set);
761 /* Pick up FP, label, and SP from the block and jump. This code is
762 from expand_goto in stmt.c; see there for detailed comments. */
763 #if HAVE_nonlocal_goto
764 if (HAVE_nonlocal_goto)
765 /* We have to pass a value to the nonlocal_goto pattern that will
766 get copied into the static_chain pointer, but it does not matter
767 what that value is, because builtin_setjmp does not use it. */
768 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
769 else
770 #endif
772 lab = copy_to_reg (lab);
774 emit_insn (gen_rtx_CLOBBER (VOIDmode,
775 gen_rtx_MEM (BLKmode,
776 gen_rtx_SCRATCH (VOIDmode))));
777 emit_insn (gen_rtx_CLOBBER (VOIDmode,
778 gen_rtx_MEM (BLKmode,
779 hard_frame_pointer_rtx)));
781 emit_move_insn (hard_frame_pointer_rtx, fp);
782 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
784 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
785 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
786 emit_indirect_jump (lab);
790 /* Search backwards and mark the jump insn as a non-local goto.
791 Note that this precludes the use of __builtin_longjmp to a
792 __builtin_setjmp target in the same function. However, we've
793 already cautioned the user that these functions are for
794 internal exception handling use only. */
795 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
797 gcc_assert (insn != last);
799 if (JUMP_P (insn))
801 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
802 REG_NOTES (insn));
803 break;
805 else if (CALL_P (insn))
806 break;
810 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
811 and the address of the save area. */
813 static rtx
814 expand_builtin_nonlocal_goto (tree arglist)
816 tree t_label, t_save_area;
817 rtx r_label, r_save_area, r_fp, r_sp, insn;
819 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
820 return NULL_RTX;
822 t_label = TREE_VALUE (arglist);
823 arglist = TREE_CHAIN (arglist);
824 t_save_area = TREE_VALUE (arglist);
826 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
827 r_label = convert_memory_address (Pmode, r_label);
828 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
829 r_save_area = convert_memory_address (Pmode, r_save_area);
830 r_fp = gen_rtx_MEM (Pmode, r_save_area);
831 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
832 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
834 current_function_has_nonlocal_goto = 1;
836 #if HAVE_nonlocal_goto
837 /* ??? We no longer need to pass the static chain value, afaik. */
838 if (HAVE_nonlocal_goto)
839 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
840 else
841 #endif
843 r_label = copy_to_reg (r_label);
845 emit_insn (gen_rtx_CLOBBER (VOIDmode,
846 gen_rtx_MEM (BLKmode,
847 gen_rtx_SCRATCH (VOIDmode))));
849 emit_insn (gen_rtx_CLOBBER (VOIDmode,
850 gen_rtx_MEM (BLKmode,
851 hard_frame_pointer_rtx)));
853 /* Restore frame pointer for containing function.
854 This sets the actual hard register used for the frame pointer
855 to the location of the function's incoming static chain info.
856 The non-local goto handler will then adjust it to contain the
857 proper value and reload the argument pointer, if needed. */
858 emit_move_insn (hard_frame_pointer_rtx, r_fp);
859 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
861 /* USE of hard_frame_pointer_rtx added for consistency;
862 not clear if really needed. */
863 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
864 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
865 emit_indirect_jump (r_label);
868 /* Search backwards to the jump insn and mark it as a
869 non-local goto. */
870 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
872 if (JUMP_P (insn))
874 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
875 const0_rtx, REG_NOTES (insn));
876 break;
878 else if (CALL_P (insn))
879 break;
882 return const0_rtx;
885 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
886 (not all will be used on all machines) that was passed to __builtin_setjmp.
887 It updates the stack pointer in that block to correspond to the current
888 stack pointer. */
890 static void
891 expand_builtin_update_setjmp_buf (rtx buf_addr)
893 enum machine_mode sa_mode = Pmode;
894 rtx stack_save;
897 #ifdef HAVE_save_stack_nonlocal
898 if (HAVE_save_stack_nonlocal)
899 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
900 #endif
901 #ifdef STACK_SAVEAREA_MODE
902 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
903 #endif
905 stack_save
906 = gen_rtx_MEM (sa_mode,
907 memory_address
908 (sa_mode,
909 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
911 #ifdef HAVE_setjmp
912 if (HAVE_setjmp)
913 emit_insn (gen_setjmp ());
914 #endif
916 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
919 /* Expand a call to __builtin_prefetch. For a target that does not support
920 data prefetch, evaluate the memory address argument in case it has side
921 effects. */
923 static void
924 expand_builtin_prefetch (tree arglist)
926 tree arg0, arg1, arg2;
927 rtx op0, op1, op2;
929 if (!validate_arglist (arglist, POINTER_TYPE, 0))
930 return;
932 arg0 = TREE_VALUE (arglist);
933 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
934 zero (read) and argument 2 (locality) defaults to 3 (high degree of
935 locality). */
936 if (TREE_CHAIN (arglist))
938 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
939 if (TREE_CHAIN (TREE_CHAIN (arglist)))
940 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
941 else
942 arg2 = build_int_cst (NULL_TREE, 3);
944 else
946 arg1 = integer_zero_node;
947 arg2 = build_int_cst (NULL_TREE, 3);
950 /* Argument 0 is an address. */
951 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
953 /* Argument 1 (read/write flag) must be a compile-time constant int. */
954 if (TREE_CODE (arg1) != INTEGER_CST)
956 error ("second argument to %<__builtin_prefetch%> must be a constant");
957 arg1 = integer_zero_node;
959 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
960 /* Argument 1 must be either zero or one. */
961 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
963 warning ("invalid second argument to %<__builtin_prefetch%>;"
964 " using zero");
965 op1 = const0_rtx;
968 /* Argument 2 (locality) must be a compile-time constant int. */
969 if (TREE_CODE (arg2) != INTEGER_CST)
971 error ("third argument to %<__builtin_prefetch%> must be a constant");
972 arg2 = integer_zero_node;
974 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
975 /* Argument 2 must be 0, 1, 2, or 3. */
976 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
978 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
979 op2 = const0_rtx;
982 #ifdef HAVE_prefetch
983 if (HAVE_prefetch)
985 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
986 (op0,
987 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
988 || (GET_MODE (op0) != Pmode))
990 op0 = convert_memory_address (Pmode, op0);
991 op0 = force_reg (Pmode, op0);
993 emit_insn (gen_prefetch (op0, op1, op2));
995 #endif
997 /* Don't do anything with direct references to volatile memory, but
998 generate code to handle other side effects. */
999 if (!MEM_P (op0) && side_effects_p (op0))
1000 emit_insn (op0);
1003 /* Get a MEM rtx for expression EXP which is the address of an operand
1004 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1005 the maximum length of the block of memory that might be accessed or
1006 NULL if unknown. */
1008 static rtx
1009 get_memory_rtx (tree exp, tree len)
1011 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1012 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1014 /* Get an expression we can use to find the attributes to assign to MEM.
1015 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1016 we can. First remove any nops. */
1017 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1018 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1019 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1020 exp = TREE_OPERAND (exp, 0);
1022 if (TREE_CODE (exp) == ADDR_EXPR)
1023 exp = TREE_OPERAND (exp, 0);
1024 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1025 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1026 else
1027 exp = NULL;
1029 /* Honor attributes derived from exp, except for the alias set
1030 (as builtin stringops may alias with anything) and the size
1031 (as stringops may access multiple array elements). */
1032 if (exp)
1034 set_mem_attributes (mem, exp, 0);
1036 /* Allow the string and memory builtins to overflow from one
1037 field into another, see http://gcc.gnu.org/PR23561.
1038 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1039 memory accessed by the string or memory builtin will fit
1040 within the field. */
1041 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1043 tree mem_expr = MEM_EXPR (mem);
1044 HOST_WIDE_INT offset = -1, length = -1;
1045 tree inner = exp;
1047 while (TREE_CODE (inner) == ARRAY_REF
1048 || TREE_CODE (inner) == NOP_EXPR
1049 || TREE_CODE (inner) == CONVERT_EXPR
1050 || TREE_CODE (inner) == NON_LVALUE_EXPR
1051 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1052 || TREE_CODE (inner) == SAVE_EXPR)
1053 inner = TREE_OPERAND (inner, 0);
1055 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1057 if (MEM_OFFSET (mem)
1058 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1059 offset = INTVAL (MEM_OFFSET (mem));
1061 if (offset >= 0 && len && host_integerp (len, 0))
1062 length = tree_low_cst (len, 0);
1064 while (TREE_CODE (inner) == COMPONENT_REF)
1066 tree field = TREE_OPERAND (inner, 1);
1067 gcc_assert (! DECL_BIT_FIELD (field));
1068 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1069 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1071 if (length >= 0
1072 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1073 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1075 HOST_WIDE_INT size
1076 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1077 /* If we can prove the memory starting at XEXP (mem, 0)
1078 and ending at XEXP (mem, 0) + LENGTH will fit into
1079 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1080 if (offset <= size
1081 && length <= size
1082 && offset + length <= size)
1083 break;
1086 if (offset >= 0
1087 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1088 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1089 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1090 / BITS_PER_UNIT;
1091 else
1093 offset = -1;
1094 length = -1;
1097 mem_expr = TREE_OPERAND (mem_expr, 0);
1098 inner = TREE_OPERAND (inner, 0);
1101 if (mem_expr == NULL)
1102 offset = -1;
1103 if (mem_expr != MEM_EXPR (mem))
1105 set_mem_expr (mem, mem_expr);
1106 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1109 set_mem_alias_set (mem, 0);
1110 set_mem_size (mem, NULL_RTX);
1113 return mem;
1116 /* Built-in functions to perform an untyped call and return. */
1118 /* For each register that may be used for calling a function, this
1119 gives a mode used to copy the register's value. VOIDmode indicates
1120 the register is not used for calling a function. If the machine
1121 has register windows, this gives only the outbound registers.
1122 INCOMING_REGNO gives the corresponding inbound register. */
1123 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1125 /* For each register that may be used for returning values, this gives
1126 a mode used to copy the register's value. VOIDmode indicates the
1127 register is not used for returning values. If the machine has
1128 register windows, this gives only the outbound registers.
1129 INCOMING_REGNO gives the corresponding inbound register. */
1130 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1132 /* For each register that may be used for calling a function, this
1133 gives the offset of that register into the block returned by
1134 __builtin_apply_args. 0 indicates that the register is not
1135 used for calling a function. */
1136 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1138 /* Return the size required for the block returned by __builtin_apply_args,
1139 and initialize apply_args_mode. */
1141 static int
1142 apply_args_size (void)
1144 static int size = -1;
1145 int align;
1146 unsigned int regno;
1147 enum machine_mode mode;
1149 /* The values computed by this function never change. */
1150 if (size < 0)
1152 /* The first value is the incoming arg-pointer. */
1153 size = GET_MODE_SIZE (Pmode);
1155 /* The second value is the structure value address unless this is
1156 passed as an "invisible" first argument. */
1157 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1158 size += GET_MODE_SIZE (Pmode);
1160 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1161 if (FUNCTION_ARG_REGNO_P (regno))
1163 mode = reg_raw_mode[regno];
1165 gcc_assert (mode != VOIDmode);
1167 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1168 if (size % align != 0)
1169 size = CEIL (size, align) * align;
1170 apply_args_reg_offset[regno] = size;
1171 size += GET_MODE_SIZE (mode);
1172 apply_args_mode[regno] = mode;
1174 else
1176 apply_args_mode[regno] = VOIDmode;
1177 apply_args_reg_offset[regno] = 0;
1180 return size;
1183 /* Return the size required for the block returned by __builtin_apply,
1184 and initialize apply_result_mode. */
1186 static int
1187 apply_result_size (void)
1189 static int size = -1;
1190 int align, regno;
1191 enum machine_mode mode;
1193 /* The values computed by this function never change. */
1194 if (size < 0)
1196 size = 0;
1198 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1199 if (FUNCTION_VALUE_REGNO_P (regno))
1201 mode = reg_raw_mode[regno];
1203 gcc_assert (mode != VOIDmode);
1205 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1206 if (size % align != 0)
1207 size = CEIL (size, align) * align;
1208 size += GET_MODE_SIZE (mode);
1209 apply_result_mode[regno] = mode;
1211 else
1212 apply_result_mode[regno] = VOIDmode;
1214 /* Allow targets that use untyped_call and untyped_return to override
1215 the size so that machine-specific information can be stored here. */
1216 #ifdef APPLY_RESULT_SIZE
1217 size = APPLY_RESULT_SIZE;
1218 #endif
1220 return size;
1223 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1224 /* Create a vector describing the result block RESULT. If SAVEP is true,
1225 the result block is used to save the values; otherwise it is used to
1226 restore the values. */
1228 static rtx
1229 result_vector (int savep, rtx result)
1231 int regno, size, align, nelts;
1232 enum machine_mode mode;
1233 rtx reg, mem;
1234 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1236 size = nelts = 0;
1237 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1238 if ((mode = apply_result_mode[regno]) != VOIDmode)
1240 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1241 if (size % align != 0)
1242 size = CEIL (size, align) * align;
1243 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1244 mem = adjust_address (result, mode, size);
1245 savevec[nelts++] = (savep
1246 ? gen_rtx_SET (VOIDmode, mem, reg)
1247 : gen_rtx_SET (VOIDmode, reg, mem));
1248 size += GET_MODE_SIZE (mode);
1250 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1252 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1254 /* Save the state required to perform an untyped call with the same
1255 arguments as were passed to the current function. */
1257 static rtx
1258 expand_builtin_apply_args_1 (void)
1260 rtx registers, tem;
1261 int size, align, regno;
1262 enum machine_mode mode;
1263 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1265 /* Create a block where the arg-pointer, structure value address,
1266 and argument registers can be saved. */
1267 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1269 /* Walk past the arg-pointer and structure value address. */
1270 size = GET_MODE_SIZE (Pmode);
1271 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1272 size += GET_MODE_SIZE (Pmode);
1274 /* Save each register used in calling a function to the block. */
1275 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1276 if ((mode = apply_args_mode[regno]) != VOIDmode)
1278 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1279 if (size % align != 0)
1280 size = CEIL (size, align) * align;
1282 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1284 emit_move_insn (adjust_address (registers, mode, size), tem);
1285 size += GET_MODE_SIZE (mode);
1288 /* Save the arg pointer to the block. */
1289 tem = copy_to_reg (virtual_incoming_args_rtx);
1290 #ifdef STACK_GROWS_DOWNWARD
1291 /* We need the pointer as the caller actually passed them to us, not
1292 as we might have pretended they were passed. Make sure it's a valid
1293 operand, as emit_move_insn isn't expected to handle a PLUS. */
1295 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1296 NULL_RTX);
1297 #endif
1298 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1300 size = GET_MODE_SIZE (Pmode);
1302 /* Save the structure value address unless this is passed as an
1303 "invisible" first argument. */
1304 if (struct_incoming_value)
1306 emit_move_insn (adjust_address (registers, Pmode, size),
1307 copy_to_reg (struct_incoming_value));
1308 size += GET_MODE_SIZE (Pmode);
1311 /* Return the address of the block. */
1312 return copy_addr_to_reg (XEXP (registers, 0));
1315 /* __builtin_apply_args returns block of memory allocated on
1316 the stack into which is stored the arg pointer, structure
1317 value address, static chain, and all the registers that might
1318 possibly be used in performing a function call. The code is
1319 moved to the start of the function so the incoming values are
1320 saved. */
1322 static rtx
1323 expand_builtin_apply_args (void)
1325 /* Don't do __builtin_apply_args more than once in a function.
1326 Save the result of the first call and reuse it. */
1327 if (apply_args_value != 0)
1328 return apply_args_value;
1330 /* When this function is called, it means that registers must be
1331 saved on entry to this function. So we migrate the
1332 call to the first insn of this function. */
1333 rtx temp;
1334 rtx seq;
1336 start_sequence ();
1337 temp = expand_builtin_apply_args_1 ();
1338 seq = get_insns ();
1339 end_sequence ();
1341 apply_args_value = temp;
1343 /* Put the insns after the NOTE that starts the function.
1344 If this is inside a start_sequence, make the outer-level insn
1345 chain current, so the code is placed at the start of the
1346 function. */
1347 push_topmost_sequence ();
1348 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1349 pop_topmost_sequence ();
1350 return temp;
1354 /* Perform an untyped call and save the state required to perform an
1355 untyped return of whatever value was returned by the given function. */
1357 static rtx
1358 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1360 int size, align, regno;
1361 enum machine_mode mode;
1362 rtx incoming_args, result, reg, dest, src, call_insn;
1363 rtx old_stack_level = 0;
1364 rtx call_fusage = 0;
1365 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1367 arguments = convert_memory_address (Pmode, arguments);
1369 /* Create a block where the return registers can be saved. */
1370 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1372 /* Fetch the arg pointer from the ARGUMENTS block. */
1373 incoming_args = gen_reg_rtx (Pmode);
1374 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1375 #ifndef STACK_GROWS_DOWNWARD
1376 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1377 incoming_args, 0, OPTAB_LIB_WIDEN);
1378 #endif
1380 /* Push a new argument block and copy the arguments. Do not allow
1381 the (potential) memcpy call below to interfere with our stack
1382 manipulations. */
1383 do_pending_stack_adjust ();
1384 NO_DEFER_POP;
1386 /* Save the stack with nonlocal if available. */
1387 #ifdef HAVE_save_stack_nonlocal
1388 if (HAVE_save_stack_nonlocal)
1389 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1390 else
1391 #endif
1392 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1394 /* Allocate a block of memory onto the stack and copy the memory
1395 arguments to the outgoing arguments address. */
1396 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1397 dest = virtual_outgoing_args_rtx;
1398 #ifndef STACK_GROWS_DOWNWARD
1399 if (GET_CODE (argsize) == CONST_INT)
1400 dest = plus_constant (dest, -INTVAL (argsize));
1401 else
1402 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1403 #endif
1404 dest = gen_rtx_MEM (BLKmode, dest);
1405 set_mem_align (dest, PARM_BOUNDARY);
1406 src = gen_rtx_MEM (BLKmode, incoming_args);
1407 set_mem_align (src, PARM_BOUNDARY);
1408 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1410 /* Refer to the argument block. */
1411 apply_args_size ();
1412 arguments = gen_rtx_MEM (BLKmode, arguments);
1413 set_mem_align (arguments, PARM_BOUNDARY);
1415 /* Walk past the arg-pointer and structure value address. */
1416 size = GET_MODE_SIZE (Pmode);
1417 if (struct_value)
1418 size += GET_MODE_SIZE (Pmode);
1420 /* Restore each of the registers previously saved. Make USE insns
1421 for each of these registers for use in making the call. */
1422 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1423 if ((mode = apply_args_mode[regno]) != VOIDmode)
1425 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1426 if (size % align != 0)
1427 size = CEIL (size, align) * align;
1428 reg = gen_rtx_REG (mode, regno);
1429 emit_move_insn (reg, adjust_address (arguments, mode, size));
1430 use_reg (&call_fusage, reg);
1431 size += GET_MODE_SIZE (mode);
1434 /* Restore the structure value address unless this is passed as an
1435 "invisible" first argument. */
1436 size = GET_MODE_SIZE (Pmode);
1437 if (struct_value)
1439 rtx value = gen_reg_rtx (Pmode);
1440 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1441 emit_move_insn (struct_value, value);
1442 if (REG_P (struct_value))
1443 use_reg (&call_fusage, struct_value);
1444 size += GET_MODE_SIZE (Pmode);
1447 /* All arguments and registers used for the call are set up by now! */
1448 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1450 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1451 and we don't want to load it into a register as an optimization,
1452 because prepare_call_address already did it if it should be done. */
1453 if (GET_CODE (function) != SYMBOL_REF)
1454 function = memory_address (FUNCTION_MODE, function);
1456 /* Generate the actual call instruction and save the return value. */
1457 #ifdef HAVE_untyped_call
1458 if (HAVE_untyped_call)
1459 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1460 result, result_vector (1, result)));
1461 else
1462 #endif
1463 #ifdef HAVE_call_value
1464 if (HAVE_call_value)
1466 rtx valreg = 0;
1468 /* Locate the unique return register. It is not possible to
1469 express a call that sets more than one return register using
1470 call_value; use untyped_call for that. In fact, untyped_call
1471 only needs to save the return registers in the given block. */
1472 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 if ((mode = apply_result_mode[regno]) != VOIDmode)
1475 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1477 valreg = gen_rtx_REG (mode, regno);
1480 emit_call_insn (GEN_CALL_VALUE (valreg,
1481 gen_rtx_MEM (FUNCTION_MODE, function),
1482 const0_rtx, NULL_RTX, const0_rtx));
1484 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1486 else
1487 #endif
1488 gcc_unreachable ();
1490 /* Find the CALL insn we just emitted, and attach the register usage
1491 information. */
1492 call_insn = last_call_insn ();
1493 add_function_usage_to (call_insn, call_fusage);
1495 /* Restore the stack. */
1496 #ifdef HAVE_save_stack_nonlocal
1497 if (HAVE_save_stack_nonlocal)
1498 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1499 else
1500 #endif
1501 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1503 OK_DEFER_POP;
1505 /* Return the address of the result block. */
1506 result = copy_addr_to_reg (XEXP (result, 0));
1507 return convert_memory_address (ptr_mode, result);
1510 /* Perform an untyped return. */
1512 static void
1513 expand_builtin_return (rtx result)
1515 int size, align, regno;
1516 enum machine_mode mode;
1517 rtx reg;
1518 rtx call_fusage = 0;
1520 result = convert_memory_address (Pmode, result);
1522 apply_result_size ();
1523 result = gen_rtx_MEM (BLKmode, result);
1525 #ifdef HAVE_untyped_return
1526 if (HAVE_untyped_return)
1528 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1529 emit_barrier ();
1530 return;
1532 #endif
1534 /* Restore the return value and note that each value is used. */
1535 size = 0;
1536 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1537 if ((mode = apply_result_mode[regno]) != VOIDmode)
1539 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1540 if (size % align != 0)
1541 size = CEIL (size, align) * align;
1542 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1543 emit_move_insn (reg, adjust_address (result, mode, size));
1545 push_to_sequence (call_fusage);
1546 emit_insn (gen_rtx_USE (VOIDmode, reg));
1547 call_fusage = get_insns ();
1548 end_sequence ();
1549 size += GET_MODE_SIZE (mode);
1552 /* Put the USE insns before the return. */
1553 emit_insn (call_fusage);
1555 /* Return whatever values was restored by jumping directly to the end
1556 of the function. */
1557 expand_naked_return ();
1560 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1562 static enum type_class
1563 type_to_class (tree type)
1565 switch (TREE_CODE (type))
1567 case VOID_TYPE: return void_type_class;
1568 case INTEGER_TYPE: return integer_type_class;
1569 case CHAR_TYPE: return char_type_class;
1570 case ENUMERAL_TYPE: return enumeral_type_class;
1571 case BOOLEAN_TYPE: return boolean_type_class;
1572 case POINTER_TYPE: return pointer_type_class;
1573 case REFERENCE_TYPE: return reference_type_class;
1574 case OFFSET_TYPE: return offset_type_class;
1575 case REAL_TYPE: return real_type_class;
1576 case COMPLEX_TYPE: return complex_type_class;
1577 case FUNCTION_TYPE: return function_type_class;
1578 case METHOD_TYPE: return method_type_class;
1579 case RECORD_TYPE: return record_type_class;
1580 case UNION_TYPE:
1581 case QUAL_UNION_TYPE: return union_type_class;
1582 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1583 ? string_type_class : array_type_class);
1584 case FILE_TYPE: return file_type_class;
1585 case LANG_TYPE: return lang_type_class;
1586 default: return no_type_class;
1590 /* Expand a call to __builtin_classify_type with arguments found in
1591 ARGLIST. */
1593 static rtx
1594 expand_builtin_classify_type (tree arglist)
1596 if (arglist != 0)
1597 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1598 return GEN_INT (no_type_class);
1601 /* This helper macro, meant to be used in mathfn_built_in below,
1602 determines which among a set of three builtin math functions is
1603 appropriate for a given type mode. The `F' and `L' cases are
1604 automatically generated from the `double' case. */
1605 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1606 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1607 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1608 fcodel = BUILT_IN_MATHFN##L ; break;
1610 /* Return mathematic function equivalent to FN but operating directly
1611 on TYPE, if available. If we can't do the conversion, return zero. */
1612 tree
1613 mathfn_built_in (tree type, enum built_in_function fn)
1615 enum built_in_function fcode, fcodef, fcodel;
1617 switch (fn)
1619 CASE_MATHFN (BUILT_IN_ACOS)
1620 CASE_MATHFN (BUILT_IN_ACOSH)
1621 CASE_MATHFN (BUILT_IN_ASIN)
1622 CASE_MATHFN (BUILT_IN_ASINH)
1623 CASE_MATHFN (BUILT_IN_ATAN)
1624 CASE_MATHFN (BUILT_IN_ATAN2)
1625 CASE_MATHFN (BUILT_IN_ATANH)
1626 CASE_MATHFN (BUILT_IN_CBRT)
1627 CASE_MATHFN (BUILT_IN_CEIL)
1628 CASE_MATHFN (BUILT_IN_COPYSIGN)
1629 CASE_MATHFN (BUILT_IN_COS)
1630 CASE_MATHFN (BUILT_IN_COSH)
1631 CASE_MATHFN (BUILT_IN_DREM)
1632 CASE_MATHFN (BUILT_IN_ERF)
1633 CASE_MATHFN (BUILT_IN_ERFC)
1634 CASE_MATHFN (BUILT_IN_EXP)
1635 CASE_MATHFN (BUILT_IN_EXP10)
1636 CASE_MATHFN (BUILT_IN_EXP2)
1637 CASE_MATHFN (BUILT_IN_EXPM1)
1638 CASE_MATHFN (BUILT_IN_FABS)
1639 CASE_MATHFN (BUILT_IN_FDIM)
1640 CASE_MATHFN (BUILT_IN_FLOOR)
1641 CASE_MATHFN (BUILT_IN_FMA)
1642 CASE_MATHFN (BUILT_IN_FMAX)
1643 CASE_MATHFN (BUILT_IN_FMIN)
1644 CASE_MATHFN (BUILT_IN_FMOD)
1645 CASE_MATHFN (BUILT_IN_FREXP)
1646 CASE_MATHFN (BUILT_IN_GAMMA)
1647 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1648 CASE_MATHFN (BUILT_IN_HYPOT)
1649 CASE_MATHFN (BUILT_IN_ILOGB)
1650 CASE_MATHFN (BUILT_IN_INF)
1651 CASE_MATHFN (BUILT_IN_J0)
1652 CASE_MATHFN (BUILT_IN_J1)
1653 CASE_MATHFN (BUILT_IN_JN)
1654 CASE_MATHFN (BUILT_IN_LDEXP)
1655 CASE_MATHFN (BUILT_IN_LGAMMA)
1656 CASE_MATHFN (BUILT_IN_LLRINT)
1657 CASE_MATHFN (BUILT_IN_LLROUND)
1658 CASE_MATHFN (BUILT_IN_LOG)
1659 CASE_MATHFN (BUILT_IN_LOG10)
1660 CASE_MATHFN (BUILT_IN_LOG1P)
1661 CASE_MATHFN (BUILT_IN_LOG2)
1662 CASE_MATHFN (BUILT_IN_LOGB)
1663 CASE_MATHFN (BUILT_IN_LRINT)
1664 CASE_MATHFN (BUILT_IN_LROUND)
1665 CASE_MATHFN (BUILT_IN_MODF)
1666 CASE_MATHFN (BUILT_IN_NAN)
1667 CASE_MATHFN (BUILT_IN_NANS)
1668 CASE_MATHFN (BUILT_IN_NEARBYINT)
1669 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1670 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1671 CASE_MATHFN (BUILT_IN_POW)
1672 CASE_MATHFN (BUILT_IN_POWI)
1673 CASE_MATHFN (BUILT_IN_POW10)
1674 CASE_MATHFN (BUILT_IN_REMAINDER)
1675 CASE_MATHFN (BUILT_IN_REMQUO)
1676 CASE_MATHFN (BUILT_IN_RINT)
1677 CASE_MATHFN (BUILT_IN_ROUND)
1678 CASE_MATHFN (BUILT_IN_SCALB)
1679 CASE_MATHFN (BUILT_IN_SCALBLN)
1680 CASE_MATHFN (BUILT_IN_SCALBN)
1681 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1682 CASE_MATHFN (BUILT_IN_SIN)
1683 CASE_MATHFN (BUILT_IN_SINCOS)
1684 CASE_MATHFN (BUILT_IN_SINH)
1685 CASE_MATHFN (BUILT_IN_SQRT)
1686 CASE_MATHFN (BUILT_IN_TAN)
1687 CASE_MATHFN (BUILT_IN_TANH)
1688 CASE_MATHFN (BUILT_IN_TGAMMA)
1689 CASE_MATHFN (BUILT_IN_TRUNC)
1690 CASE_MATHFN (BUILT_IN_Y0)
1691 CASE_MATHFN (BUILT_IN_Y1)
1692 CASE_MATHFN (BUILT_IN_YN)
1694 default:
1695 return 0;
1698 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1699 return implicit_built_in_decls[fcode];
1700 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1701 return implicit_built_in_decls[fcodef];
1702 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1703 return implicit_built_in_decls[fcodel];
1704 else
1705 return 0;
1708 /* If errno must be maintained, expand the RTL to check if the result,
1709 TARGET, of a built-in function call, EXP, is NaN, and if so set
1710 errno to EDOM. */
1712 static void
1713 expand_errno_check (tree exp, rtx target)
1715 rtx lab = gen_label_rtx ();
1717 /* Test the result; if it is NaN, set errno=EDOM because
1718 the argument was not in the domain. */
1719 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1720 0, lab);
1722 #ifdef TARGET_EDOM
1723 /* If this built-in doesn't throw an exception, set errno directly. */
1724 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1726 #ifdef GEN_ERRNO_RTX
1727 rtx errno_rtx = GEN_ERRNO_RTX;
1728 #else
1729 rtx errno_rtx
1730 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1731 #endif
1732 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1733 emit_label (lab);
1734 return;
1736 #endif
1738 /* We can't set errno=EDOM directly; let the library call do it.
1739 Pop the arguments right away in case the call gets deleted. */
1740 NO_DEFER_POP;
1741 expand_call (exp, target, 0);
1742 OK_DEFER_POP;
1743 emit_label (lab);
1747 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1748 Return 0 if a normal call should be emitted rather than expanding the
1749 function in-line. EXP is the expression that is a call to the builtin
1750 function; if convenient, the result should be placed in TARGET.
1751 SUBTARGET may be used as the target for computing one of EXP's operands. */
1753 static rtx
1754 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1756 optab builtin_optab;
1757 rtx op0, insns, before_call;
1758 tree fndecl = get_callee_fndecl (exp);
1759 tree arglist = TREE_OPERAND (exp, 1);
1760 enum machine_mode mode;
1761 bool errno_set = false;
1762 tree arg, narg;
1764 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1765 return 0;
1767 arg = TREE_VALUE (arglist);
1769 switch (DECL_FUNCTION_CODE (fndecl))
1771 case BUILT_IN_SQRT:
1772 case BUILT_IN_SQRTF:
1773 case BUILT_IN_SQRTL:
1774 errno_set = ! tree_expr_nonnegative_p (arg);
1775 builtin_optab = sqrt_optab;
1776 break;
1777 case BUILT_IN_EXP:
1778 case BUILT_IN_EXPF:
1779 case BUILT_IN_EXPL:
1780 errno_set = true; builtin_optab = exp_optab; break;
1781 case BUILT_IN_EXP10:
1782 case BUILT_IN_EXP10F:
1783 case BUILT_IN_EXP10L:
1784 case BUILT_IN_POW10:
1785 case BUILT_IN_POW10F:
1786 case BUILT_IN_POW10L:
1787 errno_set = true; builtin_optab = exp10_optab; break;
1788 case BUILT_IN_EXP2:
1789 case BUILT_IN_EXP2F:
1790 case BUILT_IN_EXP2L:
1791 errno_set = true; builtin_optab = exp2_optab; break;
1792 case BUILT_IN_EXPM1:
1793 case BUILT_IN_EXPM1F:
1794 case BUILT_IN_EXPM1L:
1795 errno_set = true; builtin_optab = expm1_optab; break;
1796 case BUILT_IN_LOGB:
1797 case BUILT_IN_LOGBF:
1798 case BUILT_IN_LOGBL:
1799 errno_set = true; builtin_optab = logb_optab; break;
1800 case BUILT_IN_ILOGB:
1801 case BUILT_IN_ILOGBF:
1802 case BUILT_IN_ILOGBL:
1803 errno_set = true; builtin_optab = ilogb_optab; break;
1804 case BUILT_IN_LOG:
1805 case BUILT_IN_LOGF:
1806 case BUILT_IN_LOGL:
1807 errno_set = true; builtin_optab = log_optab; break;
1808 case BUILT_IN_LOG10:
1809 case BUILT_IN_LOG10F:
1810 case BUILT_IN_LOG10L:
1811 errno_set = true; builtin_optab = log10_optab; break;
1812 case BUILT_IN_LOG2:
1813 case BUILT_IN_LOG2F:
1814 case BUILT_IN_LOG2L:
1815 errno_set = true; builtin_optab = log2_optab; break;
1816 case BUILT_IN_LOG1P:
1817 case BUILT_IN_LOG1PF:
1818 case BUILT_IN_LOG1PL:
1819 errno_set = true; builtin_optab = log1p_optab; break;
1820 case BUILT_IN_ASIN:
1821 case BUILT_IN_ASINF:
1822 case BUILT_IN_ASINL:
1823 builtin_optab = asin_optab; break;
1824 case BUILT_IN_ACOS:
1825 case BUILT_IN_ACOSF:
1826 case BUILT_IN_ACOSL:
1827 builtin_optab = acos_optab; break;
1828 case BUILT_IN_TAN:
1829 case BUILT_IN_TANF:
1830 case BUILT_IN_TANL:
1831 builtin_optab = tan_optab; break;
1832 case BUILT_IN_ATAN:
1833 case BUILT_IN_ATANF:
1834 case BUILT_IN_ATANL:
1835 builtin_optab = atan_optab; break;
1836 case BUILT_IN_FLOOR:
1837 case BUILT_IN_FLOORF:
1838 case BUILT_IN_FLOORL:
1839 builtin_optab = floor_optab; break;
1840 case BUILT_IN_CEIL:
1841 case BUILT_IN_CEILF:
1842 case BUILT_IN_CEILL:
1843 builtin_optab = ceil_optab; break;
1844 case BUILT_IN_TRUNC:
1845 case BUILT_IN_TRUNCF:
1846 case BUILT_IN_TRUNCL:
1847 builtin_optab = btrunc_optab; break;
1848 case BUILT_IN_ROUND:
1849 case BUILT_IN_ROUNDF:
1850 case BUILT_IN_ROUNDL:
1851 builtin_optab = round_optab; break;
1852 case BUILT_IN_NEARBYINT:
1853 case BUILT_IN_NEARBYINTF:
1854 case BUILT_IN_NEARBYINTL:
1855 builtin_optab = nearbyint_optab; break;
1856 case BUILT_IN_RINT:
1857 case BUILT_IN_RINTF:
1858 case BUILT_IN_RINTL:
1859 builtin_optab = rint_optab; break;
1860 default:
1861 gcc_unreachable ();
1864 /* Make a suitable register to place result in. */
1865 mode = TYPE_MODE (TREE_TYPE (exp));
1867 if (! flag_errno_math || ! HONOR_NANS (mode))
1868 errno_set = false;
1870 /* Before working hard, check whether the instruction is available. */
1871 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1873 target = gen_reg_rtx (mode);
1875 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1876 need to expand the argument again. This way, we will not perform
1877 side-effects more the once. */
1878 narg = builtin_save_expr (arg);
1879 if (narg != arg)
1881 arg = narg;
1882 arglist = build_tree_list (NULL_TREE, arg);
1883 exp = build_function_call_expr (fndecl, arglist);
1886 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1888 start_sequence ();
1890 /* Compute into TARGET.
1891 Set TARGET to wherever the result comes back. */
1892 target = expand_unop (mode, builtin_optab, op0, target, 0);
1894 if (target != 0)
1896 if (errno_set)
1897 expand_errno_check (exp, target);
1899 /* Output the entire sequence. */
1900 insns = get_insns ();
1901 end_sequence ();
1902 emit_insn (insns);
1903 return target;
1906 /* If we were unable to expand via the builtin, stop the sequence
1907 (without outputting the insns) and call to the library function
1908 with the stabilized argument list. */
1909 end_sequence ();
1912 before_call = get_last_insn ();
1914 target = expand_call (exp, target, target == const0_rtx);
1916 /* If this is a sqrt operation and we don't care about errno, try to
1917 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1918 This allows the semantics of the libcall to be visible to the RTL
1919 optimizers. */
1920 if (builtin_optab == sqrt_optab && !errno_set)
1922 /* Search backwards through the insns emitted by expand_call looking
1923 for the instruction with the REG_RETVAL note. */
1924 rtx last = get_last_insn ();
1925 while (last != before_call)
1927 if (find_reg_note (last, REG_RETVAL, NULL))
1929 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1930 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1931 two elements, i.e. symbol_ref(sqrt) and the operand. */
1932 if (note
1933 && GET_CODE (note) == EXPR_LIST
1934 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1935 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1936 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1938 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1939 /* Check operand is a register with expected mode. */
1940 if (operand
1941 && REG_P (operand)
1942 && GET_MODE (operand) == mode)
1944 /* Replace the REG_EQUAL note with a SQRT rtx. */
1945 rtx equiv = gen_rtx_SQRT (mode, operand);
1946 set_unique_reg_note (last, REG_EQUAL, equiv);
1949 break;
1951 last = PREV_INSN (last);
1955 return target;
1958 /* Expand a call to the builtin binary math functions (pow and atan2).
1959 Return 0 if a normal call should be emitted rather than expanding the
1960 function in-line. EXP is the expression that is a call to the builtin
1961 function; if convenient, the result should be placed in TARGET.
1962 SUBTARGET may be used as the target for computing one of EXP's
1963 operands. */
1965 static rtx
1966 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1968 optab builtin_optab;
1969 rtx op0, op1, insns;
1970 int op1_type = REAL_TYPE;
1971 tree fndecl = get_callee_fndecl (exp);
1972 tree arglist = TREE_OPERAND (exp, 1);
1973 tree arg0, arg1, temp, narg;
1974 enum machine_mode mode;
1975 bool errno_set = true;
1976 bool stable = true;
1978 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1979 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1980 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1981 op1_type = INTEGER_TYPE;
1983 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1984 return 0;
1986 arg0 = TREE_VALUE (arglist);
1987 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1989 switch (DECL_FUNCTION_CODE (fndecl))
1991 case BUILT_IN_POW:
1992 case BUILT_IN_POWF:
1993 case BUILT_IN_POWL:
1994 builtin_optab = pow_optab; break;
1995 case BUILT_IN_ATAN2:
1996 case BUILT_IN_ATAN2F:
1997 case BUILT_IN_ATAN2L:
1998 builtin_optab = atan2_optab; break;
1999 case BUILT_IN_LDEXP:
2000 case BUILT_IN_LDEXPF:
2001 case BUILT_IN_LDEXPL:
2002 builtin_optab = ldexp_optab; break;
2003 case BUILT_IN_FMOD:
2004 case BUILT_IN_FMODF:
2005 case BUILT_IN_FMODL:
2006 builtin_optab = fmod_optab; break;
2007 case BUILT_IN_DREM:
2008 case BUILT_IN_DREMF:
2009 case BUILT_IN_DREML:
2010 builtin_optab = drem_optab; break;
2011 default:
2012 gcc_unreachable ();
2015 /* Make a suitable register to place result in. */
2016 mode = TYPE_MODE (TREE_TYPE (exp));
2018 /* Before working hard, check whether the instruction is available. */
2019 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2020 return 0;
2022 target = gen_reg_rtx (mode);
2024 if (! flag_errno_math || ! HONOR_NANS (mode))
2025 errno_set = false;
2027 /* Always stabilize the argument list. */
2028 narg = builtin_save_expr (arg1);
2029 if (narg != arg1)
2031 arg1 = narg;
2032 temp = build_tree_list (NULL_TREE, narg);
2033 stable = false;
2035 else
2036 temp = TREE_CHAIN (arglist);
2038 narg = builtin_save_expr (arg0);
2039 if (narg != arg0)
2041 arg0 = narg;
2042 arglist = tree_cons (NULL_TREE, narg, temp);
2043 stable = false;
2045 else if (! stable)
2046 arglist = tree_cons (NULL_TREE, arg0, temp);
2048 if (! stable)
2049 exp = build_function_call_expr (fndecl, arglist);
2051 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2052 op1 = expand_expr (arg1, 0, VOIDmode, 0);
2054 start_sequence ();
2056 /* Compute into TARGET.
2057 Set TARGET to wherever the result comes back. */
2058 target = expand_binop (mode, builtin_optab, op0, op1,
2059 target, 0, OPTAB_DIRECT);
2061 /* If we were unable to expand via the builtin, stop the sequence
2062 (without outputting the insns) and call to the library function
2063 with the stabilized argument list. */
2064 if (target == 0)
2066 end_sequence ();
2067 return expand_call (exp, target, target == const0_rtx);
2070 if (errno_set)
2071 expand_errno_check (exp, target);
2073 /* Output the entire sequence. */
2074 insns = get_insns ();
2075 end_sequence ();
2076 emit_insn (insns);
2078 return target;
2081 /* Expand a call to the builtin sin and cos math functions.
2082 Return 0 if a normal call should be emitted rather than expanding the
2083 function in-line. EXP is the expression that is a call to the builtin
2084 function; if convenient, the result should be placed in TARGET.
2085 SUBTARGET may be used as the target for computing one of EXP's
2086 operands. */
2088 static rtx
2089 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2091 optab builtin_optab;
2092 rtx op0, insns, before_call;
2093 tree fndecl = get_callee_fndecl (exp);
2094 tree arglist = TREE_OPERAND (exp, 1);
2095 enum machine_mode mode;
2096 bool errno_set = false;
2097 tree arg, narg;
2099 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2100 return 0;
2102 arg = TREE_VALUE (arglist);
2104 switch (DECL_FUNCTION_CODE (fndecl))
2106 case BUILT_IN_SIN:
2107 case BUILT_IN_SINF:
2108 case BUILT_IN_SINL:
2109 case BUILT_IN_COS:
2110 case BUILT_IN_COSF:
2111 case BUILT_IN_COSL:
2112 builtin_optab = sincos_optab; break;
2113 default:
2114 gcc_unreachable ();
2117 /* Make a suitable register to place result in. */
2118 mode = TYPE_MODE (TREE_TYPE (exp));
2120 if (! flag_errno_math || ! HONOR_NANS (mode))
2121 errno_set = false;
2123 /* Check if sincos insn is available, otherwise fallback
2124 to sin or cos insn. */
2125 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2126 switch (DECL_FUNCTION_CODE (fndecl))
2128 case BUILT_IN_SIN:
2129 case BUILT_IN_SINF:
2130 case BUILT_IN_SINL:
2131 builtin_optab = sin_optab; break;
2132 case BUILT_IN_COS:
2133 case BUILT_IN_COSF:
2134 case BUILT_IN_COSL:
2135 builtin_optab = cos_optab; break;
2136 default:
2137 gcc_unreachable ();
2141 /* Before working hard, check whether the instruction is available. */
2142 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2144 target = gen_reg_rtx (mode);
2146 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2147 need to expand the argument again. This way, we will not perform
2148 side-effects more the once. */
2149 narg = save_expr (arg);
2150 if (narg != arg)
2152 arg = narg;
2153 arglist = build_tree_list (NULL_TREE, arg);
2154 exp = build_function_call_expr (fndecl, arglist);
2157 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2159 start_sequence ();
2161 /* Compute into TARGET.
2162 Set TARGET to wherever the result comes back. */
2163 if (builtin_optab == sincos_optab)
2165 int result;
2167 switch (DECL_FUNCTION_CODE (fndecl))
2169 case BUILT_IN_SIN:
2170 case BUILT_IN_SINF:
2171 case BUILT_IN_SINL:
2172 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2173 break;
2174 case BUILT_IN_COS:
2175 case BUILT_IN_COSF:
2176 case BUILT_IN_COSL:
2177 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2178 break;
2179 default:
2180 gcc_unreachable ();
2182 gcc_assert (result);
2184 else
2186 target = expand_unop (mode, builtin_optab, op0, target, 0);
2189 if (target != 0)
2191 if (errno_set)
2192 expand_errno_check (exp, target);
2194 /* Output the entire sequence. */
2195 insns = get_insns ();
2196 end_sequence ();
2197 emit_insn (insns);
2198 return target;
2201 /* If we were unable to expand via the builtin, stop the sequence
2202 (without outputting the insns) and call to the library function
2203 with the stabilized argument list. */
2204 end_sequence ();
2207 before_call = get_last_insn ();
2209 target = expand_call (exp, target, target == const0_rtx);
2211 return target;
2214 /* To evaluate powi(x,n), the floating point value x raised to the
2215 constant integer exponent n, we use a hybrid algorithm that
2216 combines the "window method" with look-up tables. For an
2217 introduction to exponentiation algorithms and "addition chains",
2218 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2219 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2220 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2221 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2223 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2224 multiplications to inline before calling the system library's pow
2225 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2226 so this default never requires calling pow, powf or powl. */
2228 #ifndef POWI_MAX_MULTS
2229 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2230 #endif
2232 /* The size of the "optimal power tree" lookup table. All
2233 exponents less than this value are simply looked up in the
2234 powi_table below. This threshold is also used to size the
2235 cache of pseudo registers that hold intermediate results. */
2236 #define POWI_TABLE_SIZE 256
2238 /* The size, in bits of the window, used in the "window method"
2239 exponentiation algorithm. This is equivalent to a radix of
2240 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2241 #define POWI_WINDOW_SIZE 3
2243 /* The following table is an efficient representation of an
2244 "optimal power tree". For each value, i, the corresponding
2245 value, j, in the table states than an optimal evaluation
2246 sequence for calculating pow(x,i) can be found by evaluating
2247 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2248 100 integers is given in Knuth's "Seminumerical algorithms". */
2250 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2252 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2253 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2254 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2255 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2256 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2257 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2258 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2259 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2260 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2261 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2262 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2263 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2264 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2265 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2266 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2267 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2268 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2269 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2270 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2271 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2272 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2273 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2274 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2275 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2276 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2277 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2278 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2279 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2280 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2281 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2282 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2283 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2287 /* Return the number of multiplications required to calculate
2288 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2289 subroutine of powi_cost. CACHE is an array indicating
2290 which exponents have already been calculated. */
2292 static int
2293 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2295 /* If we've already calculated this exponent, then this evaluation
2296 doesn't require any additional multiplications. */
2297 if (cache[n])
2298 return 0;
2300 cache[n] = true;
2301 return powi_lookup_cost (n - powi_table[n], cache)
2302 + powi_lookup_cost (powi_table[n], cache) + 1;
2305 /* Return the number of multiplications required to calculate
2306 powi(x,n) for an arbitrary x, given the exponent N. This
2307 function needs to be kept in sync with expand_powi below. */
2309 static int
2310 powi_cost (HOST_WIDE_INT n)
2312 bool cache[POWI_TABLE_SIZE];
2313 unsigned HOST_WIDE_INT digit;
2314 unsigned HOST_WIDE_INT val;
2315 int result;
2317 if (n == 0)
2318 return 0;
2320 /* Ignore the reciprocal when calculating the cost. */
2321 val = (n < 0) ? -n : n;
2323 /* Initialize the exponent cache. */
2324 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2325 cache[1] = true;
2327 result = 0;
2329 while (val >= POWI_TABLE_SIZE)
2331 if (val & 1)
2333 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2334 result += powi_lookup_cost (digit, cache)
2335 + POWI_WINDOW_SIZE + 1;
2336 val >>= POWI_WINDOW_SIZE;
2338 else
2340 val >>= 1;
2341 result++;
2345 return result + powi_lookup_cost (val, cache);
2348 /* Recursive subroutine of expand_powi. This function takes the array,
2349 CACHE, of already calculated exponents and an exponent N and returns
2350 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2352 static rtx
2353 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2355 unsigned HOST_WIDE_INT digit;
2356 rtx target, result;
2357 rtx op0, op1;
2359 if (n < POWI_TABLE_SIZE)
2361 if (cache[n])
2362 return cache[n];
2364 target = gen_reg_rtx (mode);
2365 cache[n] = target;
2367 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2368 op1 = expand_powi_1 (mode, powi_table[n], cache);
2370 else if (n & 1)
2372 target = gen_reg_rtx (mode);
2373 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2374 op0 = expand_powi_1 (mode, n - digit, cache);
2375 op1 = expand_powi_1 (mode, digit, cache);
2377 else
2379 target = gen_reg_rtx (mode);
2380 op0 = expand_powi_1 (mode, n >> 1, cache);
2381 op1 = op0;
2384 result = expand_mult (mode, op0, op1, target, 0);
2385 if (result != target)
2386 emit_move_insn (target, result);
2387 return target;
2390 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2391 floating point operand in mode MODE, and N is the exponent. This
2392 function needs to be kept in sync with powi_cost above. */
2394 static rtx
2395 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2397 unsigned HOST_WIDE_INT val;
2398 rtx cache[POWI_TABLE_SIZE];
2399 rtx result;
2401 if (n == 0)
2402 return CONST1_RTX (mode);
2404 val = (n < 0) ? -n : n;
2406 memset (cache, 0, sizeof (cache));
2407 cache[1] = x;
2409 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2411 /* If the original exponent was negative, reciprocate the result. */
2412 if (n < 0)
2413 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2414 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2416 return result;
2419 /* Expand a call to the pow built-in mathematical function. Return 0 if
2420 a normal call should be emitted rather than expanding the function
2421 in-line. EXP is the expression that is a call to the builtin
2422 function; if convenient, the result should be placed in TARGET. */
2424 static rtx
2425 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2427 tree arglist = TREE_OPERAND (exp, 1);
2428 tree arg0, arg1;
2430 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2431 return 0;
2433 arg0 = TREE_VALUE (arglist);
2434 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2436 if (TREE_CODE (arg1) == REAL_CST
2437 && ! TREE_CONSTANT_OVERFLOW (arg1))
2439 REAL_VALUE_TYPE cint;
2440 REAL_VALUE_TYPE c;
2441 HOST_WIDE_INT n;
2443 c = TREE_REAL_CST (arg1);
2444 n = real_to_integer (&c);
2445 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2446 if (real_identical (&c, &cint))
2448 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2449 Otherwise, check the number of multiplications required.
2450 Note that pow never sets errno for an integer exponent. */
2451 if ((n >= -1 && n <= 2)
2452 || (flag_unsafe_math_optimizations
2453 && ! optimize_size
2454 && powi_cost (n) <= POWI_MAX_MULTS))
2456 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2457 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2458 op = force_reg (mode, op);
2459 return expand_powi (op, mode, n);
2464 if (! flag_unsafe_math_optimizations)
2465 return NULL_RTX;
2466 return expand_builtin_mathfn_2 (exp, target, subtarget);
2469 /* Expand a call to the powi built-in mathematical function. Return 0 if
2470 a normal call should be emitted rather than expanding the function
2471 in-line. EXP is the expression that is a call to the builtin
2472 function; if convenient, the result should be placed in TARGET. */
2474 static rtx
2475 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2477 tree arglist = TREE_OPERAND (exp, 1);
2478 tree arg0, arg1;
2479 rtx op0, op1;
2480 enum machine_mode mode;
2482 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2483 return 0;
2485 arg0 = TREE_VALUE (arglist);
2486 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2487 mode = TYPE_MODE (TREE_TYPE (exp));
2489 /* Handle constant power. */
2491 if (TREE_CODE (arg1) == INTEGER_CST
2492 && ! TREE_CONSTANT_OVERFLOW (arg1))
2494 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2496 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2497 Otherwise, check the number of multiplications required. */
2498 if ((TREE_INT_CST_HIGH (arg1) == 0
2499 || TREE_INT_CST_HIGH (arg1) == -1)
2500 && ((n >= -1 && n <= 2)
2501 || (! optimize_size
2502 && powi_cost (n) <= POWI_MAX_MULTS)))
2504 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2505 op0 = force_reg (mode, op0);
2506 return expand_powi (op0, mode, n);
2510 /* Emit a libcall to libgcc. */
2512 if (target == NULL_RTX)
2513 target = gen_reg_rtx (mode);
2515 op0 = expand_expr (arg0, subtarget, mode, 0);
2516 if (GET_MODE (op0) != mode)
2517 op0 = convert_to_mode (mode, op0, 0);
2518 op1 = expand_expr (arg1, 0, word_mode, 0);
2519 if (GET_MODE (op1) != word_mode)
2520 op1 = convert_to_mode (word_mode, op1, 0);
2522 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2523 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2524 op0, mode, op1, word_mode);
2526 return target;
2529 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2530 if we failed the caller should emit a normal call, otherwise
2531 try to get the result in TARGET, if convenient. */
2533 static rtx
2534 expand_builtin_strlen (tree arglist, rtx target,
2535 enum machine_mode target_mode)
2537 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2538 return 0;
2539 else
2541 rtx pat;
2542 tree len, src = TREE_VALUE (arglist);
2543 rtx result, src_reg, char_rtx, before_strlen;
2544 enum machine_mode insn_mode = target_mode, char_mode;
2545 enum insn_code icode = CODE_FOR_nothing;
2546 int align;
2548 /* If the length can be computed at compile-time, return it. */
2549 len = c_strlen (src, 0);
2550 if (len)
2551 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2553 /* If the length can be computed at compile-time and is constant
2554 integer, but there are side-effects in src, evaluate
2555 src for side-effects, then return len.
2556 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2557 can be optimized into: i++; x = 3; */
2558 len = c_strlen (src, 1);
2559 if (len && TREE_CODE (len) == INTEGER_CST)
2561 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2562 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2565 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2567 /* If SRC is not a pointer type, don't do this operation inline. */
2568 if (align == 0)
2569 return 0;
2571 /* Bail out if we can't compute strlen in the right mode. */
2572 while (insn_mode != VOIDmode)
2574 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2575 if (icode != CODE_FOR_nothing)
2576 break;
2578 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2580 if (insn_mode == VOIDmode)
2581 return 0;
2583 /* Make a place to write the result of the instruction. */
2584 result = target;
2585 if (! (result != 0
2586 && REG_P (result)
2587 && GET_MODE (result) == insn_mode
2588 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2589 result = gen_reg_rtx (insn_mode);
2591 /* Make a place to hold the source address. We will not expand
2592 the actual source until we are sure that the expansion will
2593 not fail -- there are trees that cannot be expanded twice. */
2594 src_reg = gen_reg_rtx (Pmode);
2596 /* Mark the beginning of the strlen sequence so we can emit the
2597 source operand later. */
2598 before_strlen = get_last_insn ();
2600 char_rtx = const0_rtx;
2601 char_mode = insn_data[(int) icode].operand[2].mode;
2602 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2603 char_mode))
2604 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2606 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2607 char_rtx, GEN_INT (align));
2608 if (! pat)
2609 return 0;
2610 emit_insn (pat);
2612 /* Now that we are assured of success, expand the source. */
2613 start_sequence ();
2614 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2615 if (pat != src_reg)
2616 emit_move_insn (src_reg, pat);
2617 pat = get_insns ();
2618 end_sequence ();
2620 if (before_strlen)
2621 emit_insn_after (pat, before_strlen);
2622 else
2623 emit_insn_before (pat, get_insns ());
2625 /* Return the value in the proper mode for this function. */
2626 if (GET_MODE (result) == target_mode)
2627 target = result;
2628 else if (target != 0)
2629 convert_move (target, result, 0);
2630 else
2631 target = convert_to_mode (target_mode, result, 0);
2633 return target;
2637 /* Expand a call to the strstr builtin. Return 0 if we failed the
2638 caller should emit a normal call, otherwise try to get the result
2639 in TARGET, if convenient (and in mode MODE if that's convenient). */
2641 static rtx
2642 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2644 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2646 tree result = fold_builtin_strstr (arglist, type);
2647 if (result)
2648 return expand_expr (result, target, mode, EXPAND_NORMAL);
2650 return 0;
2653 /* Expand a call to the strchr builtin. Return 0 if we failed the
2654 caller should emit a normal call, otherwise try to get the result
2655 in TARGET, if convenient (and in mode MODE if that's convenient). */
2657 static rtx
2658 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2660 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2662 tree result = fold_builtin_strchr (arglist, type);
2663 if (result)
2664 return expand_expr (result, target, mode, EXPAND_NORMAL);
2666 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2668 return 0;
2671 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2672 caller should emit a normal call, otherwise try to get the result
2673 in TARGET, if convenient (and in mode MODE if that's convenient). */
2675 static rtx
2676 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2678 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2680 tree result = fold_builtin_strrchr (arglist, type);
2681 if (result)
2682 return expand_expr (result, target, mode, EXPAND_NORMAL);
2684 return 0;
2687 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2688 caller should emit a normal call, otherwise try to get the result
2689 in TARGET, if convenient (and in mode MODE if that's convenient). */
2691 static rtx
2692 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2694 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2696 tree result = fold_builtin_strpbrk (arglist, type);
2697 if (result)
2698 return expand_expr (result, target, mode, EXPAND_NORMAL);
2700 return 0;
2703 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2704 bytes from constant string DATA + OFFSET and return it as target
2705 constant. */
2707 static rtx
2708 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2709 enum machine_mode mode)
2711 const char *str = (const char *) data;
2713 gcc_assert (offset >= 0
2714 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2715 <= strlen (str) + 1));
2717 return c_readstr (str + offset, mode);
2720 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2721 Return 0 if we failed, the caller should emit a normal call,
2722 otherwise try to get the result in TARGET, if convenient (and in
2723 mode MODE if that's convenient). */
2724 static rtx
2725 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2727 tree arglist = TREE_OPERAND (exp, 1);
2728 if (!validate_arglist (arglist,
2729 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2730 return 0;
2731 else
2733 tree dest = TREE_VALUE (arglist);
2734 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2735 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2736 const char *src_str;
2737 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2738 unsigned int dest_align
2739 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2740 rtx dest_mem, src_mem, dest_addr, len_rtx;
2741 tree result = fold_builtin_memcpy (exp);
2743 if (result)
2744 return expand_expr (result, target, mode, EXPAND_NORMAL);
2746 /* If DEST is not a pointer type, call the normal function. */
2747 if (dest_align == 0)
2748 return 0;
2750 /* If either SRC is not a pointer type, don't do this
2751 operation in-line. */
2752 if (src_align == 0)
2753 return 0;
2755 dest_mem = get_memory_rtx (dest, len);
2756 set_mem_align (dest_mem, dest_align);
2757 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2758 src_str = c_getstr (src);
2760 /* If SRC is a string constant and block move would be done
2761 by pieces, we can avoid loading the string from memory
2762 and only stored the computed constants. */
2763 if (src_str
2764 && GET_CODE (len_rtx) == CONST_INT
2765 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2766 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2767 (void *) src_str, dest_align))
2769 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2770 builtin_memcpy_read_str,
2771 (void *) src_str, dest_align, 0);
2772 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2773 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2774 return dest_mem;
2777 src_mem = get_memory_rtx (src, len);
2778 set_mem_align (src_mem, src_align);
2780 /* Copy word part most expediently. */
2781 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2782 CALL_EXPR_TAILCALL (exp)
2783 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2785 if (dest_addr == 0)
2787 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2788 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2790 return dest_addr;
2794 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2795 Return 0 if we failed the caller should emit a normal call,
2796 otherwise try to get the result in TARGET, if convenient (and in
2797 mode MODE if that's convenient). If ENDP is 0 return the
2798 destination pointer, if ENDP is 1 return the end pointer ala
2799 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2800 stpcpy. */
2802 static rtx
2803 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2804 int endp)
2806 if (!validate_arglist (arglist,
2807 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2808 return 0;
2809 /* If return value is ignored, transform mempcpy into memcpy. */
2810 else if (target == const0_rtx)
2812 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2814 if (!fn)
2815 return 0;
2817 return expand_expr (build_function_call_expr (fn, arglist),
2818 target, mode, EXPAND_NORMAL);
2820 else
2822 tree dest = TREE_VALUE (arglist);
2823 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2824 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2825 const char *src_str;
2826 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2827 unsigned int dest_align
2828 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2829 rtx dest_mem, src_mem, len_rtx;
2830 tree result = fold_builtin_mempcpy (arglist, type, endp);
2832 if (result)
2833 return expand_expr (result, target, mode, EXPAND_NORMAL);
2835 /* If either SRC or DEST is not a pointer type, don't do this
2836 operation in-line. */
2837 if (dest_align == 0 || src_align == 0)
2838 return 0;
2840 /* If LEN is not constant, call the normal function. */
2841 if (! host_integerp (len, 1))
2842 return 0;
2844 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2845 src_str = c_getstr (src);
2847 /* If SRC is a string constant and block move would be done
2848 by pieces, we can avoid loading the string from memory
2849 and only stored the computed constants. */
2850 if (src_str
2851 && GET_CODE (len_rtx) == CONST_INT
2852 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2853 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2854 (void *) src_str, dest_align))
2856 dest_mem = get_memory_rtx (dest, len);
2857 set_mem_align (dest_mem, dest_align);
2858 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2859 builtin_memcpy_read_str,
2860 (void *) src_str, dest_align, endp);
2861 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2862 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2863 return dest_mem;
2866 if (GET_CODE (len_rtx) == CONST_INT
2867 && can_move_by_pieces (INTVAL (len_rtx),
2868 MIN (dest_align, src_align)))
2870 dest_mem = get_memory_rtx (dest, len);
2871 set_mem_align (dest_mem, dest_align);
2872 src_mem = get_memory_rtx (src, len);
2873 set_mem_align (src_mem, src_align);
2874 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2875 MIN (dest_align, src_align), endp);
2876 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2877 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2878 return dest_mem;
2881 return 0;
2885 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2886 if we failed the caller should emit a normal call. */
2888 static rtx
2889 expand_builtin_memmove (tree arglist, tree type, rtx target,
2890 enum machine_mode mode, tree orig_exp)
2892 if (!validate_arglist (arglist,
2893 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2894 return 0;
2895 else
2897 tree dest = TREE_VALUE (arglist);
2898 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2899 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2901 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2902 unsigned int dest_align
2903 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2904 tree result = fold_builtin_memmove (arglist, type);
2906 if (result)
2907 return expand_expr (result, target, mode, EXPAND_NORMAL);
2909 /* If DEST is not a pointer type, call the normal function. */
2910 if (dest_align == 0)
2911 return 0;
2913 /* If either SRC is not a pointer type, don't do this
2914 operation in-line. */
2915 if (src_align == 0)
2916 return 0;
2918 /* If src is categorized for a readonly section we can use
2919 normal memcpy. */
2920 if (readonly_data_expr (src))
2922 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2923 if (!fn)
2924 return 0;
2925 fn = build_function_call_expr (fn, arglist);
2926 if (TREE_CODE (fn) == CALL_EXPR)
2927 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
2928 return expand_expr (fn, target, mode, EXPAND_NORMAL);
2931 /* If length is 1 and we can expand memcpy call inline,
2932 it is ok to use memcpy as well. */
2933 if (integer_onep (len))
2935 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2936 /*endp=*/0);
2937 if (ret)
2938 return ret;
2941 /* Otherwise, call the normal function. */
2942 return 0;
2946 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2947 if we failed the caller should emit a normal call. */
2949 static rtx
2950 expand_builtin_bcopy (tree exp)
2952 tree arglist = TREE_OPERAND (exp, 1);
2953 tree type = TREE_TYPE (exp);
2954 tree src, dest, size, newarglist;
2956 if (!validate_arglist (arglist,
2957 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2958 return NULL_RTX;
2960 src = TREE_VALUE (arglist);
2961 dest = TREE_VALUE (TREE_CHAIN (arglist));
2962 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2964 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2965 memmove(ptr y, ptr x, size_t z). This is done this way
2966 so that if it isn't expanded inline, we fallback to
2967 calling bcopy instead of memmove. */
2969 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2970 newarglist = tree_cons (NULL_TREE, src, newarglist);
2971 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2973 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode, exp);
2976 #ifndef HAVE_movstr
2977 # define HAVE_movstr 0
2978 # define CODE_FOR_movstr CODE_FOR_nothing
2979 #endif
2981 /* Expand into a movstr instruction, if one is available. Return 0 if
2982 we failed, the caller should emit a normal call, otherwise try to
2983 get the result in TARGET, if convenient. If ENDP is 0 return the
2984 destination pointer, if ENDP is 1 return the end pointer ala
2985 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2986 stpcpy. */
2988 static rtx
2989 expand_movstr (tree dest, tree src, rtx target, int endp)
2991 rtx end;
2992 rtx dest_mem;
2993 rtx src_mem;
2994 rtx insn;
2995 const struct insn_data * data;
2997 if (!HAVE_movstr)
2998 return 0;
3000 dest_mem = get_memory_rtx (dest, NULL);
3001 src_mem = get_memory_rtx (src, NULL);
3002 if (!endp)
3004 target = force_reg (Pmode, XEXP (dest_mem, 0));
3005 dest_mem = replace_equiv_address (dest_mem, target);
3006 end = gen_reg_rtx (Pmode);
3008 else
3010 if (target == 0 || target == const0_rtx)
3012 end = gen_reg_rtx (Pmode);
3013 if (target == 0)
3014 target = end;
3016 else
3017 end = target;
3020 data = insn_data + CODE_FOR_movstr;
3022 if (data->operand[0].mode != VOIDmode)
3023 end = gen_lowpart (data->operand[0].mode, end);
3025 insn = data->genfun (end, dest_mem, src_mem);
3027 gcc_assert (insn);
3029 emit_insn (insn);
3031 /* movstr is supposed to set end to the address of the NUL
3032 terminator. If the caller requested a mempcpy-like return value,
3033 adjust it. */
3034 if (endp == 1 && target != const0_rtx)
3036 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3037 emit_move_insn (target, force_operand (tem, NULL_RTX));
3040 return target;
3043 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3044 if we failed the caller should emit a normal call, otherwise try to get
3045 the result in TARGET, if convenient (and in mode MODE if that's
3046 convenient). */
3048 static rtx
3049 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
3051 tree arglist = TREE_OPERAND (exp, 1);
3052 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3054 tree result = fold_builtin_strcpy (exp, 0);
3055 if (result)
3056 return expand_expr (result, target, mode, EXPAND_NORMAL);
3058 return expand_movstr (TREE_VALUE (arglist),
3059 TREE_VALUE (TREE_CHAIN (arglist)),
3060 target, /*endp=*/0);
3062 return 0;
3065 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3066 Return 0 if we failed the caller should emit a normal call,
3067 otherwise try to get the result in TARGET, if convenient (and in
3068 mode MODE if that's convenient). */
3070 static rtx
3071 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3073 tree arglist = TREE_OPERAND (exp, 1);
3074 /* If return value is ignored, transform stpcpy into strcpy. */
3075 if (target == const0_rtx)
3077 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3078 if (!fn)
3079 return 0;
3081 return expand_expr (build_function_call_expr (fn, arglist),
3082 target, mode, EXPAND_NORMAL);
3085 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3086 return 0;
3087 else
3089 tree dst, src, len, lenp1;
3090 tree narglist;
3091 rtx ret;
3093 /* Ensure we get an actual string whose length can be evaluated at
3094 compile-time, not an expression containing a string. This is
3095 because the latter will potentially produce pessimized code
3096 when used to produce the return value. */
3097 src = TREE_VALUE (TREE_CHAIN (arglist));
3098 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3099 return expand_movstr (TREE_VALUE (arglist),
3100 TREE_VALUE (TREE_CHAIN (arglist)),
3101 target, /*endp=*/2);
3103 dst = TREE_VALUE (arglist);
3104 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3105 narglist = build_tree_list (NULL_TREE, lenp1);
3106 narglist = tree_cons (NULL_TREE, src, narglist);
3107 narglist = tree_cons (NULL_TREE, dst, narglist);
3108 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3109 target, mode, /*endp=*/2);
3111 if (ret)
3112 return ret;
3114 if (TREE_CODE (len) == INTEGER_CST)
3116 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3118 if (GET_CODE (len_rtx) == CONST_INT)
3120 ret = expand_builtin_strcpy (exp, target, mode);
3122 if (ret)
3124 if (! target)
3126 if (mode != VOIDmode)
3127 target = gen_reg_rtx (mode);
3128 else
3129 target = gen_reg_rtx (GET_MODE (ret));
3131 if (GET_MODE (target) != GET_MODE (ret))
3132 ret = gen_lowpart (GET_MODE (target), ret);
3134 ret = plus_constant (ret, INTVAL (len_rtx));
3135 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3136 gcc_assert (ret);
3138 return target;
3143 return expand_movstr (TREE_VALUE (arglist),
3144 TREE_VALUE (TREE_CHAIN (arglist)),
3145 target, /*endp=*/2);
3149 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3150 bytes from constant string DATA + OFFSET and return it as target
3151 constant. */
3153 static rtx
3154 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3155 enum machine_mode mode)
3157 const char *str = (const char *) data;
3159 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3160 return const0_rtx;
3162 return c_readstr (str + offset, mode);
3165 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3166 if we failed the caller should emit a normal call. */
3168 static rtx
3169 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3171 tree arglist = TREE_OPERAND (exp, 1);
3172 if (validate_arglist (arglist,
3173 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3175 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3176 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3177 tree result = fold_builtin_strncpy (exp, slen);
3179 if (result)
3180 return expand_expr (result, target, mode, EXPAND_NORMAL);
3182 /* We must be passed a constant len and src parameter. */
3183 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3184 return 0;
3186 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3188 /* We're required to pad with trailing zeros if the requested
3189 len is greater than strlen(s2)+1. In that case try to
3190 use store_by_pieces, if it fails, punt. */
3191 if (tree_int_cst_lt (slen, len))
3193 tree dest = TREE_VALUE (arglist);
3194 unsigned int dest_align
3195 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3196 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3197 rtx dest_mem;
3199 if (!p || dest_align == 0 || !host_integerp (len, 1)
3200 || !can_store_by_pieces (tree_low_cst (len, 1),
3201 builtin_strncpy_read_str,
3202 (void *) p, dest_align))
3203 return 0;
3205 dest_mem = get_memory_rtx (dest, len);
3206 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3207 builtin_strncpy_read_str,
3208 (void *) p, dest_align, 0);
3209 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3210 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3211 return dest_mem;
3214 return 0;
3217 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3218 bytes from constant string DATA + OFFSET and return it as target
3219 constant. */
3221 static rtx
3222 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3223 enum machine_mode mode)
3225 const char *c = (const char *) data;
3226 char *p = alloca (GET_MODE_SIZE (mode));
3228 memset (p, *c, GET_MODE_SIZE (mode));
3230 return c_readstr (p, mode);
3233 /* Callback routine for store_by_pieces. Return the RTL of a register
3234 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3235 char value given in the RTL register data. For example, if mode is
3236 4 bytes wide, return the RTL for 0x01010101*data. */
3238 static rtx
3239 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3240 enum machine_mode mode)
3242 rtx target, coeff;
3243 size_t size;
3244 char *p;
3246 size = GET_MODE_SIZE (mode);
3247 if (size == 1)
3248 return (rtx) data;
3250 p = alloca (size);
3251 memset (p, 1, size);
3252 coeff = c_readstr (p, mode);
3254 target = convert_to_mode (mode, (rtx) data, 1);
3255 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3256 return force_reg (mode, target);
3259 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3260 if we failed the caller should emit a normal call, otherwise try to get
3261 the result in TARGET, if convenient (and in mode MODE if that's
3262 convenient). */
3264 static rtx
3265 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3266 tree orig_exp)
3268 if (!validate_arglist (arglist,
3269 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3270 return 0;
3271 else
3273 tree dest = TREE_VALUE (arglist);
3274 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3275 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3276 char c;
3278 unsigned int dest_align
3279 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3280 rtx dest_mem, dest_addr, len_rtx;
3282 /* If DEST is not a pointer type, don't do this
3283 operation in-line. */
3284 if (dest_align == 0)
3285 return 0;
3287 /* If the LEN parameter is zero, return DEST. */
3288 if (integer_zerop (len))
3290 /* Evaluate and ignore VAL in case it has side-effects. */
3291 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3292 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3295 if (TREE_CODE (val) != INTEGER_CST)
3297 rtx val_rtx;
3299 if (!host_integerp (len, 1))
3300 return 0;
3302 if (optimize_size && tree_low_cst (len, 1) > 1)
3303 return 0;
3305 /* Assume that we can memset by pieces if we can store the
3306 * the coefficients by pieces (in the required modes).
3307 * We can't pass builtin_memset_gen_str as that emits RTL. */
3308 c = 1;
3309 if (!can_store_by_pieces (tree_low_cst (len, 1),
3310 builtin_memset_read_str,
3311 &c, dest_align))
3312 return 0;
3314 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3315 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3316 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3317 val_rtx);
3318 dest_mem = get_memory_rtx (dest, len);
3319 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3320 builtin_memset_gen_str,
3321 val_rtx, dest_align, 0);
3322 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3323 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3324 return dest_mem;
3327 if (target_char_cast (val, &c))
3328 return 0;
3330 if (c)
3332 if (!host_integerp (len, 1))
3333 return 0;
3334 if (!can_store_by_pieces (tree_low_cst (len, 1),
3335 builtin_memset_read_str, &c,
3336 dest_align))
3337 return 0;
3339 dest_mem = get_memory_rtx (dest, len);
3340 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3341 builtin_memset_read_str,
3342 &c, dest_align, 0);
3343 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3344 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3345 return dest_mem;
3348 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3350 dest_mem = get_memory_rtx (dest, len);
3351 set_mem_align (dest_mem, dest_align);
3352 dest_addr = clear_storage (dest_mem, len_rtx,
3353 CALL_EXPR_TAILCALL (orig_exp)
3354 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3356 if (dest_addr == 0)
3358 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3359 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3362 return dest_addr;
3366 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3367 if we failed the caller should emit a normal call. */
3369 static rtx
3370 expand_builtin_bzero (tree exp)
3372 tree arglist = TREE_OPERAND (exp, 1);
3373 tree dest, size, newarglist;
3375 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3376 return NULL_RTX;
3378 dest = TREE_VALUE (arglist);
3379 size = TREE_VALUE (TREE_CHAIN (arglist));
3381 /* New argument list transforming bzero(ptr x, int y) to
3382 memset(ptr x, int 0, size_t y). This is done this way
3383 so that if it isn't expanded inline, we fallback to
3384 calling bzero instead of memset. */
3386 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3387 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3388 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3390 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3393 /* Expand expression EXP, which is a call to the memcmp built-in function.
3394 ARGLIST is the argument list for this call. Return 0 if we failed and the
3395 caller should emit a normal call, otherwise try to get the result in
3396 TARGET, if convenient (and in mode MODE, if that's convenient). */
3398 static rtx
3399 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3400 enum machine_mode mode)
3402 if (!validate_arglist (arglist,
3403 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3404 return 0;
3405 else
3407 tree result = fold_builtin_memcmp (arglist);
3408 if (result)
3409 return expand_expr (result, target, mode, EXPAND_NORMAL);
3412 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3414 tree arg1 = TREE_VALUE (arglist);
3415 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3416 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3417 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3418 rtx result;
3419 rtx insn;
3421 int arg1_align
3422 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3423 int arg2_align
3424 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3425 enum machine_mode insn_mode;
3427 #ifdef HAVE_cmpmemsi
3428 if (HAVE_cmpmemsi)
3429 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3430 else
3431 #endif
3432 #ifdef HAVE_cmpstrsi
3433 if (HAVE_cmpstrsi)
3434 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3435 else
3436 #endif
3437 return 0;
3439 /* If we don't have POINTER_TYPE, call the function. */
3440 if (arg1_align == 0 || arg2_align == 0)
3441 return 0;
3443 /* Make a place to write the result of the instruction. */
3444 result = target;
3445 if (! (result != 0
3446 && REG_P (result) && GET_MODE (result) == insn_mode
3447 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3448 result = gen_reg_rtx (insn_mode);
3450 arg1_rtx = get_memory_rtx (arg1, len);
3451 arg2_rtx = get_memory_rtx (arg2, len);
3452 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3454 /* Set MEM_SIZE as appropriate. */
3455 if (GET_CODE (arg3_rtx) == CONST_INT)
3457 set_mem_size (arg1_rtx, arg3_rtx);
3458 set_mem_size (arg2_rtx, arg3_rtx);
3461 #ifdef HAVE_cmpmemsi
3462 if (HAVE_cmpmemsi)
3463 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3464 GEN_INT (MIN (arg1_align, arg2_align)));
3465 else
3466 #endif
3467 #ifdef HAVE_cmpstrsi
3468 if (HAVE_cmpstrsi)
3469 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3470 GEN_INT (MIN (arg1_align, arg2_align)));
3471 else
3472 #endif
3473 gcc_unreachable ();
3475 if (insn)
3476 emit_insn (insn);
3477 else
3478 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3479 TYPE_MODE (integer_type_node), 3,
3480 XEXP (arg1_rtx, 0), Pmode,
3481 XEXP (arg2_rtx, 0), Pmode,
3482 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3483 TYPE_UNSIGNED (sizetype)),
3484 TYPE_MODE (sizetype));
3486 /* Return the value in the proper mode for this function. */
3487 mode = TYPE_MODE (TREE_TYPE (exp));
3488 if (GET_MODE (result) == mode)
3489 return result;
3490 else if (target != 0)
3492 convert_move (target, result, 0);
3493 return target;
3495 else
3496 return convert_to_mode (mode, result, 0);
3498 #endif
3500 return 0;
3503 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3504 if we failed the caller should emit a normal call, otherwise try to get
3505 the result in TARGET, if convenient. */
3507 static rtx
3508 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3510 tree arglist = TREE_OPERAND (exp, 1);
3512 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3513 return 0;
3514 else
3516 tree result = fold_builtin_strcmp (arglist);
3517 if (result)
3518 return expand_expr (result, target, mode, EXPAND_NORMAL);
3521 #ifdef HAVE_cmpstrsi
3522 if (HAVE_cmpstrsi)
3524 tree arg1 = TREE_VALUE (arglist);
3525 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3526 tree len, len1, len2;
3527 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3528 rtx result, insn;
3529 tree fndecl, fn;
3531 int arg1_align
3532 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3533 int arg2_align
3534 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3535 enum machine_mode insn_mode
3536 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3538 len1 = c_strlen (arg1, 1);
3539 len2 = c_strlen (arg2, 1);
3541 if (len1)
3542 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3543 if (len2)
3544 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3546 /* If we don't have a constant length for the first, use the length
3547 of the second, if we know it. We don't require a constant for
3548 this case; some cost analysis could be done if both are available
3549 but neither is constant. For now, assume they're equally cheap,
3550 unless one has side effects. If both strings have constant lengths,
3551 use the smaller. */
3553 if (!len1)
3554 len = len2;
3555 else if (!len2)
3556 len = len1;
3557 else if (TREE_SIDE_EFFECTS (len1))
3558 len = len2;
3559 else if (TREE_SIDE_EFFECTS (len2))
3560 len = len1;
3561 else if (TREE_CODE (len1) != INTEGER_CST)
3562 len = len2;
3563 else if (TREE_CODE (len2) != INTEGER_CST)
3564 len = len1;
3565 else if (tree_int_cst_lt (len1, len2))
3566 len = len1;
3567 else
3568 len = len2;
3570 /* If both arguments have side effects, we cannot optimize. */
3571 if (!len || TREE_SIDE_EFFECTS (len))
3572 return 0;
3574 /* If we don't have POINTER_TYPE, call the function. */
3575 if (arg1_align == 0 || arg2_align == 0)
3576 return 0;
3578 /* Make a place to write the result of the instruction. */
3579 result = target;
3580 if (! (result != 0
3581 && REG_P (result) && GET_MODE (result) == insn_mode
3582 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3583 result = gen_reg_rtx (insn_mode);
3585 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3586 arg1 = builtin_save_expr (arg1);
3587 arg2 = builtin_save_expr (arg2);
3589 arg1_rtx = get_memory_rtx (arg1, NULL);
3590 arg2_rtx = get_memory_rtx (arg2, NULL);
3591 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3592 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3593 GEN_INT (MIN (arg1_align, arg2_align)));
3594 if (insn)
3596 emit_insn (insn);
3598 /* Return the value in the proper mode for this function. */
3599 mode = TYPE_MODE (TREE_TYPE (exp));
3600 if (GET_MODE (result) == mode)
3601 return result;
3602 if (target == 0)
3603 return convert_to_mode (mode, result, 0);
3604 convert_move (target, result, 0);
3605 return target;
3608 /* Expand the library call ourselves using a stabilized argument
3609 list to avoid re-evaluating the function's arguments twice. */
3610 arglist = build_tree_list (NULL_TREE, arg2);
3611 arglist = tree_cons (NULL_TREE, arg1, arglist);
3612 fndecl = get_callee_fndecl (exp);
3613 fn = build_function_call_expr (fndecl, arglist);
3614 if (TREE_CODE (fn) == CALL_EXPR)
3615 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3616 return expand_call (fn, target, target == const0_rtx);
3618 #endif
3619 return 0;
3622 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3623 if we failed the caller should emit a normal call, otherwise try to get
3624 the result in TARGET, if convenient. */
3626 static rtx
3627 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3629 tree arglist = TREE_OPERAND (exp, 1);
3631 if (!validate_arglist (arglist,
3632 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3633 return 0;
3634 else
3636 tree result = fold_builtin_strncmp (arglist);
3637 if (result)
3638 return expand_expr (result, target, mode, EXPAND_NORMAL);
3641 /* If c_strlen can determine an expression for one of the string
3642 lengths, and it doesn't have side effects, then emit cmpstrsi
3643 using length MIN(strlen(string)+1, arg3). */
3644 #ifdef HAVE_cmpstrsi
3645 if (HAVE_cmpstrsi)
3647 tree arg1 = TREE_VALUE (arglist);
3648 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3649 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3650 tree len, len1, len2;
3651 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3652 rtx result, insn;
3653 tree fndecl, fn;
3655 int arg1_align
3656 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3657 int arg2_align
3658 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3659 enum machine_mode insn_mode
3660 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3662 len1 = c_strlen (arg1, 1);
3663 len2 = c_strlen (arg2, 1);
3665 if (len1)
3666 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3667 if (len2)
3668 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3670 /* If we don't have a constant length for the first, use the length
3671 of the second, if we know it. We don't require a constant for
3672 this case; some cost analysis could be done if both are available
3673 but neither is constant. For now, assume they're equally cheap,
3674 unless one has side effects. If both strings have constant lengths,
3675 use the smaller. */
3677 if (!len1)
3678 len = len2;
3679 else if (!len2)
3680 len = len1;
3681 else if (TREE_SIDE_EFFECTS (len1))
3682 len = len2;
3683 else if (TREE_SIDE_EFFECTS (len2))
3684 len = len1;
3685 else if (TREE_CODE (len1) != INTEGER_CST)
3686 len = len2;
3687 else if (TREE_CODE (len2) != INTEGER_CST)
3688 len = len1;
3689 else if (tree_int_cst_lt (len1, len2))
3690 len = len1;
3691 else
3692 len = len2;
3694 /* If both arguments have side effects, we cannot optimize. */
3695 if (!len || TREE_SIDE_EFFECTS (len))
3696 return 0;
3698 /* The actual new length parameter is MIN(len,arg3). */
3699 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3700 fold_convert (TREE_TYPE (len), arg3)));
3702 /* If we don't have POINTER_TYPE, call the function. */
3703 if (arg1_align == 0 || arg2_align == 0)
3704 return 0;
3706 /* Make a place to write the result of the instruction. */
3707 result = target;
3708 if (! (result != 0
3709 && REG_P (result) && GET_MODE (result) == insn_mode
3710 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3711 result = gen_reg_rtx (insn_mode);
3713 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3714 arg1 = builtin_save_expr (arg1);
3715 arg2 = builtin_save_expr (arg2);
3716 len = builtin_save_expr (len);
3718 arg1_rtx = get_memory_rtx (arg1, len);
3719 arg2_rtx = get_memory_rtx (arg2, len);
3720 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3721 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3722 GEN_INT (MIN (arg1_align, arg2_align)));
3723 if (insn)
3725 emit_insn (insn);
3727 /* Return the value in the proper mode for this function. */
3728 mode = TYPE_MODE (TREE_TYPE (exp));
3729 if (GET_MODE (result) == mode)
3730 return result;
3731 if (target == 0)
3732 return convert_to_mode (mode, result, 0);
3733 convert_move (target, result, 0);
3734 return target;
3737 /* Expand the library call ourselves using a stabilized argument
3738 list to avoid re-evaluating the function's arguments twice. */
3739 arglist = build_tree_list (NULL_TREE, len);
3740 arglist = tree_cons (NULL_TREE, arg2, arglist);
3741 arglist = tree_cons (NULL_TREE, arg1, arglist);
3742 fndecl = get_callee_fndecl (exp);
3743 fn = build_function_call_expr (fndecl, arglist);
3744 if (TREE_CODE (fn) == CALL_EXPR)
3745 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3746 return expand_call (fn, target, target == const0_rtx);
3748 #endif
3749 return 0;
3752 /* Expand expression EXP, which is a call to the strcat builtin.
3753 Return 0 if we failed the caller should emit a normal call,
3754 otherwise try to get the result in TARGET, if convenient. */
3756 static rtx
3757 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3759 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3760 return 0;
3761 else
3763 tree dst = TREE_VALUE (arglist),
3764 src = TREE_VALUE (TREE_CHAIN (arglist));
3765 const char *p = c_getstr (src);
3767 if (p)
3769 /* If the string length is zero, return the dst parameter. */
3770 if (*p == '\0')
3771 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3772 else if (!optimize_size)
3774 /* Otherwise if !optimize_size, see if we can store by
3775 pieces into (dst + strlen(dst)). */
3776 tree newdst, arglist,
3777 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3779 /* This is the length argument. */
3780 arglist = build_tree_list (NULL_TREE,
3781 fold (size_binop (PLUS_EXPR,
3782 c_strlen (src, 0),
3783 ssize_int (1))));
3784 /* Prepend src argument. */
3785 arglist = tree_cons (NULL_TREE, src, arglist);
3787 /* We're going to use dst more than once. */
3788 dst = builtin_save_expr (dst);
3790 /* Create strlen (dst). */
3791 newdst =
3792 fold (build_function_call_expr (strlen_fn,
3793 build_tree_list (NULL_TREE,
3794 dst)));
3795 /* Create (dst + strlen (dst)). */
3796 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3798 /* Prepend the new dst argument. */
3799 arglist = tree_cons (NULL_TREE, newdst, arglist);
3801 /* We don't want to get turned into a memcpy if the
3802 target is const0_rtx, i.e. when the return value
3803 isn't used. That would produce pessimized code so
3804 pass in a target of zero, it should never actually be
3805 used. If this was successful return the original
3806 dst, not the result of mempcpy. */
3807 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3808 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3809 else
3810 return 0;
3814 return 0;
3818 /* Expand expression EXP, which is a call to the strncat builtin.
3819 Return 0 if we failed the caller should emit a normal call,
3820 otherwise try to get the result in TARGET, if convenient. */
3822 static rtx
3823 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3825 if (validate_arglist (arglist,
3826 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3828 tree result = fold_builtin_strncat (arglist);
3829 if (result)
3830 return expand_expr (result, target, mode, EXPAND_NORMAL);
3832 return 0;
3835 /* Expand expression EXP, which is a call to the strspn builtin.
3836 Return 0 if we failed the caller should emit a normal call,
3837 otherwise try to get the result in TARGET, if convenient. */
3839 static rtx
3840 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3842 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3844 tree result = fold_builtin_strspn (arglist);
3845 if (result)
3846 return expand_expr (result, target, mode, EXPAND_NORMAL);
3848 return 0;
3851 /* Expand expression EXP, which is a call to the strcspn builtin.
3852 Return 0 if we failed the caller should emit a normal call,
3853 otherwise try to get the result in TARGET, if convenient. */
3855 static rtx
3856 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3858 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3860 tree result = fold_builtin_strcspn (arglist);
3861 if (result)
3862 return expand_expr (result, target, mode, EXPAND_NORMAL);
3864 return 0;
3867 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3868 if that's convenient. */
3871 expand_builtin_saveregs (void)
3873 rtx val, seq;
3875 /* Don't do __builtin_saveregs more than once in a function.
3876 Save the result of the first call and reuse it. */
3877 if (saveregs_value != 0)
3878 return saveregs_value;
3880 /* When this function is called, it means that registers must be
3881 saved on entry to this function. So we migrate the call to the
3882 first insn of this function. */
3884 start_sequence ();
3886 /* Do whatever the machine needs done in this case. */
3887 val = targetm.calls.expand_builtin_saveregs ();
3889 seq = get_insns ();
3890 end_sequence ();
3892 saveregs_value = val;
3894 /* Put the insns after the NOTE that starts the function. If this
3895 is inside a start_sequence, make the outer-level insn chain current, so
3896 the code is placed at the start of the function. */
3897 push_topmost_sequence ();
3898 emit_insn_after (seq, entry_of_function ());
3899 pop_topmost_sequence ();
3901 return val;
3904 /* __builtin_args_info (N) returns word N of the arg space info
3905 for the current function. The number and meanings of words
3906 is controlled by the definition of CUMULATIVE_ARGS. */
3908 static rtx
3909 expand_builtin_args_info (tree arglist)
3911 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3912 int *word_ptr = (int *) &current_function_args_info;
3914 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3916 if (arglist != 0)
3918 if (!host_integerp (TREE_VALUE (arglist), 0))
3919 error ("argument of %<__builtin_args_info%> must be constant");
3920 else
3922 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3924 if (wordnum < 0 || wordnum >= nwords)
3925 error ("argument of %<__builtin_args_info%> out of range");
3926 else
3927 return GEN_INT (word_ptr[wordnum]);
3930 else
3931 error ("missing argument in %<__builtin_args_info%>");
3933 return const0_rtx;
3936 /* Expand a call to __builtin_next_arg. */
3938 static rtx
3939 expand_builtin_next_arg (void)
3941 /* Checking arguments is already done in fold_builtin_next_arg
3942 that must be called before this function. */
3943 return expand_binop (Pmode, add_optab,
3944 current_function_internal_arg_pointer,
3945 current_function_arg_offset_rtx,
3946 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3949 /* Make it easier for the backends by protecting the valist argument
3950 from multiple evaluations. */
3952 static tree
3953 stabilize_va_list (tree valist, int needs_lvalue)
3955 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3957 if (TREE_SIDE_EFFECTS (valist))
3958 valist = save_expr (valist);
3960 /* For this case, the backends will be expecting a pointer to
3961 TREE_TYPE (va_list_type_node), but it's possible we've
3962 actually been given an array (an actual va_list_type_node).
3963 So fix it. */
3964 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3966 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3967 valist = build_fold_addr_expr_with_type (valist, p1);
3970 else
3972 tree pt;
3974 if (! needs_lvalue)
3976 if (! TREE_SIDE_EFFECTS (valist))
3977 return valist;
3979 pt = build_pointer_type (va_list_type_node);
3980 valist = fold (build1 (ADDR_EXPR, pt, valist));
3981 TREE_SIDE_EFFECTS (valist) = 1;
3984 if (TREE_SIDE_EFFECTS (valist))
3985 valist = save_expr (valist);
3986 valist = build_fold_indirect_ref (valist);
3989 return valist;
3992 /* The "standard" definition of va_list is void*. */
3994 tree
3995 std_build_builtin_va_list (void)
3997 return ptr_type_node;
4000 /* The "standard" implementation of va_start: just assign `nextarg' to
4001 the variable. */
4003 void
4004 std_expand_builtin_va_start (tree valist, rtx nextarg)
4006 tree t;
4008 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4009 make_tree (ptr_type_node, nextarg));
4010 TREE_SIDE_EFFECTS (t) = 1;
4012 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4015 /* Expand ARGLIST, from a call to __builtin_va_start. */
4017 static rtx
4018 expand_builtin_va_start (tree arglist)
4020 rtx nextarg;
4021 tree chain, valist;
4023 chain = TREE_CHAIN (arglist);
4025 if (!chain)
4027 error ("too few arguments to function %<va_start%>");
4028 return const0_rtx;
4031 if (fold_builtin_next_arg (chain))
4032 return const0_rtx;
4034 nextarg = expand_builtin_next_arg ();
4035 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4037 #ifdef EXPAND_BUILTIN_VA_START
4038 EXPAND_BUILTIN_VA_START (valist, nextarg);
4039 #else
4040 std_expand_builtin_va_start (valist, nextarg);
4041 #endif
4043 return const0_rtx;
4046 /* The "standard" implementation of va_arg: read the value from the
4047 current (padded) address and increment by the (padded) size. */
4049 tree
4050 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4052 tree addr, t, type_size, rounded_size, valist_tmp;
4053 unsigned HOST_WIDE_INT align, boundary;
4054 bool indirect;
4056 #ifdef ARGS_GROW_DOWNWARD
4057 /* All of the alignment and movement below is for args-grow-up machines.
4058 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4059 implement their own specialized gimplify_va_arg_expr routines. */
4060 gcc_unreachable ();
4061 #endif
4063 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4064 if (indirect)
4065 type = build_pointer_type (type);
4067 align = PARM_BOUNDARY / BITS_PER_UNIT;
4068 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4070 /* Hoist the valist value into a temporary for the moment. */
4071 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4073 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4074 requires greater alignment, we must perform dynamic alignment. */
4075 if (boundary > align)
4077 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4078 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4079 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4080 gimplify_and_add (t, pre_p);
4082 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4083 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4084 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4085 gimplify_and_add (t, pre_p);
4087 else
4088 boundary = align;
4090 /* If the actual alignment is less than the alignment of the type,
4091 adjust the type accordingly so that we don't assume strict alignment
4092 when deferencing the pointer. */
4093 boundary *= BITS_PER_UNIT;
4094 if (boundary < TYPE_ALIGN (type))
4096 type = build_variant_type_copy (type);
4097 TYPE_ALIGN (type) = boundary;
4100 /* Compute the rounded size of the type. */
4101 type_size = size_in_bytes (type);
4102 rounded_size = round_up (type_size, align);
4104 /* Reduce rounded_size so it's sharable with the postqueue. */
4105 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4107 /* Get AP. */
4108 addr = valist_tmp;
4109 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4111 /* Small args are padded downward. */
4112 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4113 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4114 size_binop (MINUS_EXPR, rounded_size, type_size)));
4115 t = fold_convert (TREE_TYPE (addr), t);
4116 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4119 /* Compute new value for AP. */
4120 t = fold_convert (TREE_TYPE (valist), rounded_size);
4121 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4122 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4123 gimplify_and_add (t, pre_p);
4125 addr = fold_convert (build_pointer_type (type), addr);
4127 if (indirect)
4128 addr = build_va_arg_indirect_ref (addr);
4130 return build_va_arg_indirect_ref (addr);
4133 /* Build an indirect-ref expression over the given TREE, which represents a
4134 piece of a va_arg() expansion. */
4135 tree
4136 build_va_arg_indirect_ref (tree addr)
4138 addr = build_fold_indirect_ref (addr);
4140 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4141 mf_mark (addr);
4143 return addr;
4146 /* Return a dummy expression of type TYPE in order to keep going after an
4147 error. */
4149 static tree
4150 dummy_object (tree type)
4152 tree t = convert (build_pointer_type (type), null_pointer_node);
4153 return build1 (INDIRECT_REF, type, t);
4156 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4157 builtin function, but a very special sort of operator. */
4159 enum gimplify_status
4160 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4162 tree promoted_type, want_va_type, have_va_type;
4163 tree valist = TREE_OPERAND (*expr_p, 0);
4164 tree type = TREE_TYPE (*expr_p);
4165 tree t;
4167 /* Verify that valist is of the proper type. */
4168 want_va_type = va_list_type_node;
4169 have_va_type = TREE_TYPE (valist);
4171 if (have_va_type == error_mark_node)
4172 return GS_ERROR;
4174 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4176 /* If va_list is an array type, the argument may have decayed
4177 to a pointer type, e.g. by being passed to another function.
4178 In that case, unwrap both types so that we can compare the
4179 underlying records. */
4180 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4181 || POINTER_TYPE_P (have_va_type))
4183 want_va_type = TREE_TYPE (want_va_type);
4184 have_va_type = TREE_TYPE (have_va_type);
4188 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4190 error ("first argument to %<va_arg%> not of type %<va_list%>");
4191 return GS_ERROR;
4194 /* Generate a diagnostic for requesting data of a type that cannot
4195 be passed through `...' due to type promotion at the call site. */
4196 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4197 != type)
4199 static bool gave_help;
4201 /* Unfortunately, this is merely undefined, rather than a constraint
4202 violation, so we cannot make this an error. If this call is never
4203 executed, the program is still strictly conforming. */
4204 warning ("%qT is promoted to %qT when passed through %<...%>",
4205 type, promoted_type);
4206 if (! gave_help)
4208 gave_help = true;
4209 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4210 promoted_type, type);
4213 /* We can, however, treat "undefined" any way we please.
4214 Call abort to encourage the user to fix the program. */
4215 inform ("if this code is reached, the program will abort");
4216 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4217 NULL);
4218 append_to_statement_list (t, pre_p);
4220 /* This is dead code, but go ahead and finish so that the
4221 mode of the result comes out right. */
4222 *expr_p = dummy_object (type);
4223 return GS_ALL_DONE;
4225 else
4227 /* Make it easier for the backends by protecting the valist argument
4228 from multiple evaluations. */
4229 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4231 /* For this case, the backends will be expecting a pointer to
4232 TREE_TYPE (va_list_type_node), but it's possible we've
4233 actually been given an array (an actual va_list_type_node).
4234 So fix it. */
4235 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4237 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4238 valist = build_fold_addr_expr_with_type (valist, p1);
4240 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4242 else
4243 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4245 if (!targetm.gimplify_va_arg_expr)
4246 /* Once most targets are converted this should abort. */
4247 return GS_ALL_DONE;
4249 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4250 return GS_OK;
4254 /* Expand ARGLIST, from a call to __builtin_va_end. */
4256 static rtx
4257 expand_builtin_va_end (tree arglist)
4259 tree valist = TREE_VALUE (arglist);
4261 /* Evaluate for side effects, if needed. I hate macros that don't
4262 do that. */
4263 if (TREE_SIDE_EFFECTS (valist))
4264 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4266 return const0_rtx;
4269 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4270 builtin rather than just as an assignment in stdarg.h because of the
4271 nastiness of array-type va_list types. */
4273 static rtx
4274 expand_builtin_va_copy (tree arglist)
4276 tree dst, src, t;
4278 dst = TREE_VALUE (arglist);
4279 src = TREE_VALUE (TREE_CHAIN (arglist));
4281 dst = stabilize_va_list (dst, 1);
4282 src = stabilize_va_list (src, 0);
4284 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4286 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4287 TREE_SIDE_EFFECTS (t) = 1;
4288 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4290 else
4292 rtx dstb, srcb, size;
4294 /* Evaluate to pointers. */
4295 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4296 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4297 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4298 VOIDmode, EXPAND_NORMAL);
4300 dstb = convert_memory_address (Pmode, dstb);
4301 srcb = convert_memory_address (Pmode, srcb);
4303 /* "Dereference" to BLKmode memories. */
4304 dstb = gen_rtx_MEM (BLKmode, dstb);
4305 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4306 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4307 srcb = gen_rtx_MEM (BLKmode, srcb);
4308 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4309 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4311 /* Copy. */
4312 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4315 return const0_rtx;
4318 /* Expand a call to one of the builtin functions __builtin_frame_address or
4319 __builtin_return_address. */
4321 static rtx
4322 expand_builtin_frame_address (tree fndecl, tree arglist)
4324 /* The argument must be a nonnegative integer constant.
4325 It counts the number of frames to scan up the stack.
4326 The value is the return address saved in that frame. */
4327 if (arglist == 0)
4328 /* Warning about missing arg was already issued. */
4329 return const0_rtx;
4330 else if (! host_integerp (TREE_VALUE (arglist), 1))
4332 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4333 error ("invalid argument to %<__builtin_frame_address%>");
4334 else
4335 error ("invalid argument to %<__builtin_return_address%>");
4336 return const0_rtx;
4338 else
4340 rtx tem
4341 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4342 tree_low_cst (TREE_VALUE (arglist), 1));
4344 /* Some ports cannot access arbitrary stack frames. */
4345 if (tem == NULL)
4347 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4348 warning ("unsupported argument to %<__builtin_frame_address%>");
4349 else
4350 warning ("unsupported argument to %<__builtin_return_address%>");
4351 return const0_rtx;
4354 /* For __builtin_frame_address, return what we've got. */
4355 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4356 return tem;
4358 if (!REG_P (tem)
4359 && ! CONSTANT_P (tem))
4360 tem = copy_to_mode_reg (Pmode, tem);
4361 return tem;
4365 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4366 we failed and the caller should emit a normal call, otherwise try to get
4367 the result in TARGET, if convenient. */
4369 static rtx
4370 expand_builtin_alloca (tree arglist, rtx target)
4372 rtx op0;
4373 rtx result;
4375 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4376 should always expand to function calls. These can be intercepted
4377 in libmudflap. */
4378 if (flag_mudflap)
4379 return 0;
4381 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4382 return 0;
4384 /* Compute the argument. */
4385 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4387 /* Allocate the desired space. */
4388 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4389 result = convert_memory_address (ptr_mode, result);
4391 return result;
4394 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4395 Return 0 if a normal call should be emitted rather than expanding the
4396 function in-line. If convenient, the result should be placed in TARGET.
4397 SUBTARGET may be used as the target for computing one of EXP's operands. */
4399 static rtx
4400 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4401 rtx subtarget, optab op_optab)
4403 rtx op0;
4404 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4405 return 0;
4407 /* Compute the argument. */
4408 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4409 /* Compute op, into TARGET if possible.
4410 Set TARGET to wherever the result comes back. */
4411 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4412 op_optab, op0, target, 1);
4413 gcc_assert (target);
4415 return convert_to_mode (target_mode, target, 0);
4418 /* If the string passed to fputs is a constant and is one character
4419 long, we attempt to transform this call into __builtin_fputc(). */
4421 static rtx
4422 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4424 /* Verify the arguments in the original call. */
4425 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4427 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4428 unlocked, NULL_TREE);
4429 if (result)
4430 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4432 return 0;
4435 /* Expand a call to __builtin_expect. We return our argument and emit a
4436 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4437 a non-jump context. */
4439 static rtx
4440 expand_builtin_expect (tree arglist, rtx target)
4442 tree exp, c;
4443 rtx note, rtx_c;
4445 if (arglist == NULL_TREE
4446 || TREE_CHAIN (arglist) == NULL_TREE)
4447 return const0_rtx;
4448 exp = TREE_VALUE (arglist);
4449 c = TREE_VALUE (TREE_CHAIN (arglist));
4451 if (TREE_CODE (c) != INTEGER_CST)
4453 error ("second argument to %<__builtin_expect%> must be a constant");
4454 c = integer_zero_node;
4457 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4459 /* Don't bother with expected value notes for integral constants. */
4460 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4462 /* We do need to force this into a register so that we can be
4463 moderately sure to be able to correctly interpret the branch
4464 condition later. */
4465 target = force_reg (GET_MODE (target), target);
4467 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4469 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4470 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4473 return target;
4476 /* Like expand_builtin_expect, except do this in a jump context. This is
4477 called from do_jump if the conditional is a __builtin_expect. Return either
4478 a list of insns to emit the jump or NULL if we cannot optimize
4479 __builtin_expect. We need to optimize this at jump time so that machines
4480 like the PowerPC don't turn the test into a SCC operation, and then jump
4481 based on the test being 0/1. */
4484 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4486 tree arglist = TREE_OPERAND (exp, 1);
4487 tree arg0 = TREE_VALUE (arglist);
4488 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4489 rtx ret = NULL_RTX;
4491 /* Only handle __builtin_expect (test, 0) and
4492 __builtin_expect (test, 1). */
4493 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4494 && (integer_zerop (arg1) || integer_onep (arg1)))
4496 rtx insn, drop_through_label, temp;
4498 /* Expand the jump insns. */
4499 start_sequence ();
4500 do_jump (arg0, if_false_label, if_true_label);
4501 ret = get_insns ();
4503 drop_through_label = get_last_insn ();
4504 if (drop_through_label && NOTE_P (drop_through_label))
4505 drop_through_label = prev_nonnote_insn (drop_through_label);
4506 if (drop_through_label && !LABEL_P (drop_through_label))
4507 drop_through_label = NULL_RTX;
4508 end_sequence ();
4510 if (! if_true_label)
4511 if_true_label = drop_through_label;
4512 if (! if_false_label)
4513 if_false_label = drop_through_label;
4515 /* Go through and add the expect's to each of the conditional jumps. */
4516 insn = ret;
4517 while (insn != NULL_RTX)
4519 rtx next = NEXT_INSN (insn);
4521 if (JUMP_P (insn) && any_condjump_p (insn))
4523 rtx ifelse = SET_SRC (pc_set (insn));
4524 rtx then_dest = XEXP (ifelse, 1);
4525 rtx else_dest = XEXP (ifelse, 2);
4526 int taken = -1;
4528 /* First check if we recognize any of the labels. */
4529 if (GET_CODE (then_dest) == LABEL_REF
4530 && XEXP (then_dest, 0) == if_true_label)
4531 taken = 1;
4532 else if (GET_CODE (then_dest) == LABEL_REF
4533 && XEXP (then_dest, 0) == if_false_label)
4534 taken = 0;
4535 else if (GET_CODE (else_dest) == LABEL_REF
4536 && XEXP (else_dest, 0) == if_false_label)
4537 taken = 1;
4538 else if (GET_CODE (else_dest) == LABEL_REF
4539 && XEXP (else_dest, 0) == if_true_label)
4540 taken = 0;
4541 /* Otherwise check where we drop through. */
4542 else if (else_dest == pc_rtx)
4544 if (next && NOTE_P (next))
4545 next = next_nonnote_insn (next);
4547 if (next && JUMP_P (next)
4548 && any_uncondjump_p (next))
4549 temp = XEXP (SET_SRC (pc_set (next)), 0);
4550 else
4551 temp = next;
4553 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4554 else that can't possibly match either target label. */
4555 if (temp == if_false_label)
4556 taken = 1;
4557 else if (temp == if_true_label)
4558 taken = 0;
4560 else if (then_dest == pc_rtx)
4562 if (next && NOTE_P (next))
4563 next = next_nonnote_insn (next);
4565 if (next && JUMP_P (next)
4566 && any_uncondjump_p (next))
4567 temp = XEXP (SET_SRC (pc_set (next)), 0);
4568 else
4569 temp = next;
4571 if (temp == if_false_label)
4572 taken = 0;
4573 else if (temp == if_true_label)
4574 taken = 1;
4577 if (taken != -1)
4579 /* If the test is expected to fail, reverse the
4580 probabilities. */
4581 if (integer_zerop (arg1))
4582 taken = 1 - taken;
4583 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4587 insn = next;
4591 return ret;
4594 static void
4595 expand_builtin_trap (void)
4597 #ifdef HAVE_trap
4598 if (HAVE_trap)
4599 emit_insn (gen_trap ());
4600 else
4601 #endif
4602 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4603 emit_barrier ();
4606 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4607 Return 0 if a normal call should be emitted rather than expanding
4608 the function inline. If convenient, the result should be placed
4609 in TARGET. SUBTARGET may be used as the target for computing
4610 the operand. */
4612 static rtx
4613 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4615 enum machine_mode mode;
4616 tree arg;
4617 rtx op0;
4619 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4620 return 0;
4622 arg = TREE_VALUE (arglist);
4623 mode = TYPE_MODE (TREE_TYPE (arg));
4624 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4625 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4628 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4629 Return NULL is a normal call should be emitted rather than expanding the
4630 function inline. If convenient, the result should be placed in TARGET.
4631 SUBTARGET may be used as the target for computing the operand. */
4633 static rtx
4634 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4636 rtx op0, op1;
4637 tree arg;
4639 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4640 return 0;
4642 arg = TREE_VALUE (arglist);
4643 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4645 arg = TREE_VALUE (TREE_CHAIN (arglist));
4646 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4648 return expand_copysign (op0, op1, target);
4651 /* Create a new constant string literal and return a char* pointer to it.
4652 The STRING_CST value is the LEN characters at STR. */
4653 static tree
4654 build_string_literal (int len, const char *str)
4656 tree t, elem, index, type;
4658 t = build_string (len, str);
4659 elem = build_type_variant (char_type_node, 1, 0);
4660 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4661 type = build_array_type (elem, index);
4662 TREE_TYPE (t) = type;
4663 TREE_CONSTANT (t) = 1;
4664 TREE_INVARIANT (t) = 1;
4665 TREE_READONLY (t) = 1;
4666 TREE_STATIC (t) = 1;
4668 type = build_pointer_type (type);
4669 t = build1 (ADDR_EXPR, type, t);
4671 type = build_pointer_type (elem);
4672 t = build1 (NOP_EXPR, type, t);
4673 return t;
4676 /* Expand EXP, a call to printf or printf_unlocked.
4677 Return 0 if a normal call should be emitted rather than transforming
4678 the function inline. If convenient, the result should be placed in
4679 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4680 call. */
4681 static rtx
4682 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4683 bool unlocked)
4685 tree arglist = TREE_OPERAND (exp, 1);
4686 /* If we're using an unlocked function, assume the other unlocked
4687 functions exist explicitly. */
4688 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4689 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4690 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4691 : implicit_built_in_decls[BUILT_IN_PUTS];
4692 const char *fmt_str;
4693 tree fn, fmt, arg;
4695 /* If the return value is used, don't do the transformation. */
4696 if (target != const0_rtx)
4697 return 0;
4699 /* Verify the required arguments in the original call. */
4700 if (! arglist)
4701 return 0;
4702 fmt = TREE_VALUE (arglist);
4703 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4704 return 0;
4705 arglist = TREE_CHAIN (arglist);
4707 /* Check whether the format is a literal string constant. */
4708 fmt_str = c_getstr (fmt);
4709 if (fmt_str == NULL)
4710 return 0;
4712 if (!init_target_chars())
4713 return 0;
4715 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4716 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4718 if (! arglist
4719 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4720 || TREE_CHAIN (arglist))
4721 return 0;
4722 fn = fn_puts;
4724 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4725 else if (strcmp (fmt_str, target_percent_c) == 0)
4727 if (! arglist
4728 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4729 || TREE_CHAIN (arglist))
4730 return 0;
4731 fn = fn_putchar;
4733 else
4735 /* We can't handle anything else with % args or %% ... yet. */
4736 if (strchr (fmt_str, target_percent))
4737 return 0;
4739 if (arglist)
4740 return 0;
4742 /* If the format specifier was "", printf does nothing. */
4743 if (fmt_str[0] == '\0')
4744 return const0_rtx;
4745 /* If the format specifier has length of 1, call putchar. */
4746 if (fmt_str[1] == '\0')
4748 /* Given printf("c"), (where c is any one character,)
4749 convert "c"[0] to an int and pass that to the replacement
4750 function. */
4751 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4752 arglist = build_tree_list (NULL_TREE, arg);
4753 fn = fn_putchar;
4755 else
4757 /* If the format specifier was "string\n", call puts("string"). */
4758 size_t len = strlen (fmt_str);
4759 if ((unsigned char)fmt_str[len - 1] == target_newline)
4761 /* Create a NUL-terminated string that's one char shorter
4762 than the original, stripping off the trailing '\n'. */
4763 char *newstr = alloca (len);
4764 memcpy (newstr, fmt_str, len - 1);
4765 newstr[len - 1] = 0;
4767 arg = build_string_literal (len, newstr);
4768 arglist = build_tree_list (NULL_TREE, arg);
4769 fn = fn_puts;
4771 else
4772 /* We'd like to arrange to call fputs(string,stdout) here,
4773 but we need stdout and don't have a way to get it yet. */
4774 return 0;
4778 if (!fn)
4779 return 0;
4780 fn = build_function_call_expr (fn, arglist);
4781 if (TREE_CODE (fn) == CALL_EXPR)
4782 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4783 return expand_expr (fn, target, mode, EXPAND_NORMAL);
4786 /* Expand EXP, a call to fprintf or fprintf_unlocked.
4787 Return 0 if a normal call should be emitted rather than transforming
4788 the function inline. If convenient, the result should be placed in
4789 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4790 call. */
4791 static rtx
4792 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
4793 bool unlocked)
4795 tree arglist = TREE_OPERAND (exp, 1);
4796 /* If we're using an unlocked function, assume the other unlocked
4797 functions exist explicitly. */
4798 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4799 : implicit_built_in_decls[BUILT_IN_FPUTC];
4800 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4801 : implicit_built_in_decls[BUILT_IN_FPUTS];
4802 const char *fmt_str;
4803 tree fn, fmt, fp, arg;
4805 /* If the return value is used, don't do the transformation. */
4806 if (target != const0_rtx)
4807 return 0;
4809 /* Verify the required arguments in the original call. */
4810 if (! arglist)
4811 return 0;
4812 fp = TREE_VALUE (arglist);
4813 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4814 return 0;
4815 arglist = TREE_CHAIN (arglist);
4816 if (! arglist)
4817 return 0;
4818 fmt = TREE_VALUE (arglist);
4819 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4820 return 0;
4821 arglist = TREE_CHAIN (arglist);
4823 /* Check whether the format is a literal string constant. */
4824 fmt_str = c_getstr (fmt);
4825 if (fmt_str == NULL)
4826 return 0;
4828 if (!init_target_chars())
4829 return 0;
4831 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4832 if (strcmp (fmt_str, target_percent_s) == 0)
4834 if (! arglist
4835 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4836 || TREE_CHAIN (arglist))
4837 return 0;
4838 arg = TREE_VALUE (arglist);
4839 arglist = build_tree_list (NULL_TREE, fp);
4840 arglist = tree_cons (NULL_TREE, arg, arglist);
4841 fn = fn_fputs;
4843 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4844 else if (strcmp (fmt_str, target_percent_c) == 0)
4846 if (! arglist
4847 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4848 || TREE_CHAIN (arglist))
4849 return 0;
4850 arg = TREE_VALUE (arglist);
4851 arglist = build_tree_list (NULL_TREE, fp);
4852 arglist = tree_cons (NULL_TREE, arg, arglist);
4853 fn = fn_fputc;
4855 else
4857 /* We can't handle anything else with % args or %% ... yet. */
4858 if (strchr (fmt_str, target_percent))
4859 return 0;
4861 if (arglist)
4862 return 0;
4864 /* If the format specifier was "", fprintf does nothing. */
4865 if (fmt_str[0] == '\0')
4867 /* Evaluate and ignore FILE* argument for side-effects. */
4868 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4869 return const0_rtx;
4872 /* When "string" doesn't contain %, replace all cases of
4873 fprintf(stream,string) with fputs(string,stream). The fputs
4874 builtin will take care of special cases like length == 1. */
4875 arglist = build_tree_list (NULL_TREE, fp);
4876 arglist = tree_cons (NULL_TREE, fmt, arglist);
4877 fn = fn_fputs;
4880 if (!fn)
4881 return 0;
4882 fn = build_function_call_expr (fn, arglist);
4883 if (TREE_CODE (fn) == CALL_EXPR)
4884 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4885 return expand_expr (fn, target, mode, EXPAND_NORMAL);
4888 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4889 a normal call should be emitted rather than expanding the function
4890 inline. If convenient, the result should be placed in TARGET with
4891 mode MODE. */
4893 static rtx
4894 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4896 tree orig_arglist, dest, fmt;
4897 const char *fmt_str;
4899 orig_arglist = arglist;
4901 /* Verify the required arguments in the original call. */
4902 if (! arglist)
4903 return 0;
4904 dest = TREE_VALUE (arglist);
4905 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4906 return 0;
4907 arglist = TREE_CHAIN (arglist);
4908 if (! arglist)
4909 return 0;
4910 fmt = TREE_VALUE (arglist);
4911 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4912 return 0;
4913 arglist = TREE_CHAIN (arglist);
4915 /* Check whether the format is a literal string constant. */
4916 fmt_str = c_getstr (fmt);
4917 if (fmt_str == NULL)
4918 return 0;
4920 if (!init_target_chars())
4921 return 0;
4923 /* If the format doesn't contain % args or %%, use strcpy. */
4924 if (strchr (fmt_str, target_percent) == 0)
4926 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4927 tree exp;
4929 if (arglist || ! fn)
4930 return 0;
4931 expand_expr (build_function_call_expr (fn, orig_arglist),
4932 const0_rtx, VOIDmode, EXPAND_NORMAL);
4933 if (target == const0_rtx)
4934 return const0_rtx;
4935 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4936 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4938 /* If the format is "%s", use strcpy if the result isn't used. */
4939 else if (strcmp (fmt_str, target_percent_s) == 0)
4941 tree fn, arg, len;
4942 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4944 if (! fn)
4945 return 0;
4947 if (! arglist || TREE_CHAIN (arglist))
4948 return 0;
4949 arg = TREE_VALUE (arglist);
4950 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4951 return 0;
4953 if (target != const0_rtx)
4955 len = c_strlen (arg, 1);
4956 if (! len || TREE_CODE (len) != INTEGER_CST)
4957 return 0;
4959 else
4960 len = NULL_TREE;
4962 arglist = build_tree_list (NULL_TREE, arg);
4963 arglist = tree_cons (NULL_TREE, dest, arglist);
4964 expand_expr (build_function_call_expr (fn, arglist),
4965 const0_rtx, VOIDmode, EXPAND_NORMAL);
4967 if (target == const0_rtx)
4968 return const0_rtx;
4969 return expand_expr (len, target, mode, EXPAND_NORMAL);
4972 return 0;
4975 /* Expand a call to either the entry or exit function profiler. */
4977 static rtx
4978 expand_builtin_profile_func (bool exitp)
4980 rtx this, which;
4982 this = DECL_RTL (current_function_decl);
4983 gcc_assert (MEM_P (this));
4984 this = XEXP (this, 0);
4986 if (exitp)
4987 which = profile_function_exit_libfunc;
4988 else
4989 which = profile_function_entry_libfunc;
4991 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4992 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4994 Pmode);
4996 return const0_rtx;
4999 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5001 static rtx
5002 round_trampoline_addr (rtx tramp)
5004 rtx temp, addend, mask;
5006 /* If we don't need too much alignment, we'll have been guaranteed
5007 proper alignment by get_trampoline_type. */
5008 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5009 return tramp;
5011 /* Round address up to desired boundary. */
5012 temp = gen_reg_rtx (Pmode);
5013 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5014 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5016 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5017 temp, 0, OPTAB_LIB_WIDEN);
5018 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5019 temp, 0, OPTAB_LIB_WIDEN);
5021 return tramp;
5024 static rtx
5025 expand_builtin_init_trampoline (tree arglist)
5027 tree t_tramp, t_func, t_chain;
5028 rtx r_tramp, r_func, r_chain;
5029 #ifdef TRAMPOLINE_TEMPLATE
5030 rtx blktramp;
5031 #endif
5033 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5034 POINTER_TYPE, VOID_TYPE))
5035 return NULL_RTX;
5037 t_tramp = TREE_VALUE (arglist);
5038 arglist = TREE_CHAIN (arglist);
5039 t_func = TREE_VALUE (arglist);
5040 arglist = TREE_CHAIN (arglist);
5041 t_chain = TREE_VALUE (arglist);
5043 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
5044 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
5045 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
5047 /* Generate insns to initialize the trampoline. */
5048 r_tramp = round_trampoline_addr (r_tramp);
5049 #ifdef TRAMPOLINE_TEMPLATE
5050 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5051 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5052 emit_block_move (blktramp, assemble_trampoline_template (),
5053 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5054 #endif
5055 trampolines_created = 1;
5056 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5058 return const0_rtx;
5061 static rtx
5062 expand_builtin_adjust_trampoline (tree arglist)
5064 rtx tramp;
5066 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5067 return NULL_RTX;
5069 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5070 tramp = round_trampoline_addr (tramp);
5071 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5072 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5073 #endif
5075 return tramp;
5078 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5079 Return NULL_RTX if a normal call should be emitted rather than expanding
5080 the function in-line. EXP is the expression that is a call to the builtin
5081 function; if convenient, the result should be placed in TARGET. */
5083 static rtx
5084 expand_builtin_signbit (tree exp, rtx target)
5086 const struct real_format *fmt;
5087 enum machine_mode fmode, imode, rmode;
5088 HOST_WIDE_INT hi, lo;
5089 tree arg, arglist;
5090 int bitpos;
5091 rtx temp;
5093 arglist = TREE_OPERAND (exp, 1);
5094 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5095 return 0;
5097 arg = TREE_VALUE (arglist);
5098 fmode = TYPE_MODE (TREE_TYPE (arg));
5099 rmode = TYPE_MODE (TREE_TYPE (exp));
5100 fmt = REAL_MODE_FORMAT (fmode);
5102 /* For floating point formats without a sign bit, implement signbit
5103 as "ARG < 0.0". */
5104 if (fmt->signbit < 0)
5106 /* But we can't do this if the format supports signed zero. */
5107 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5108 return 0;
5110 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
5111 build_real (TREE_TYPE (arg), dconst0)));
5112 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5115 imode = int_mode_for_mode (fmode);
5116 if (imode == BLKmode)
5117 return 0;
5119 bitpos = fmt->signbit;
5120 /* Handle targets with different FP word orders. */
5121 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5123 int nwords = GET_MODE_BITSIZE (fmode) / BITS_PER_WORD;
5124 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
5125 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
5128 /* If the sign bit is not in the lowpart and the floating point format
5129 is wider than an integer, check that is twice the size of an integer
5130 so that we can use gen_highpart below. */
5131 if (bitpos >= GET_MODE_BITSIZE (rmode)
5132 && GET_MODE_BITSIZE (imode) != 2 * GET_MODE_BITSIZE (rmode))
5133 return 0;
5135 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5136 temp = gen_lowpart (imode, temp);
5138 if (GET_MODE_BITSIZE (imode) > GET_MODE_BITSIZE (rmode))
5140 if (BYTES_BIG_ENDIAN)
5141 bitpos = GET_MODE_BITSIZE (imode) - 1 - bitpos;
5142 temp = copy_to_mode_reg (imode, temp);
5143 temp = extract_bit_field (temp, 1, bitpos, 1,
5144 NULL_RTX, rmode, rmode);
5146 else
5148 if (GET_MODE_BITSIZE (imode) < GET_MODE_BITSIZE (rmode))
5149 temp = gen_lowpart (rmode, temp);
5150 if (bitpos < HOST_BITS_PER_WIDE_INT)
5152 hi = 0;
5153 lo = (HOST_WIDE_INT) 1 << bitpos;
5155 else
5157 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5158 lo = 0;
5161 temp = force_reg (rmode, temp);
5162 temp = expand_binop (rmode, and_optab, temp,
5163 immed_double_const (lo, hi, rmode),
5164 target, 1, OPTAB_LIB_WIDEN);
5166 return temp;
5169 /* Expand fork or exec calls. TARGET is the desired target of the
5170 call. ARGLIST is the list of arguments of the call. FN is the
5171 identificator of the actual function. IGNORE is nonzero if the
5172 value is to be ignored. */
5174 static rtx
5175 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5177 tree id, decl;
5178 tree call;
5180 /* If we are not profiling, just call the function. */
5181 if (!profile_arc_flag)
5182 return NULL_RTX;
5184 /* Otherwise call the wrapper. This should be equivalent for the rest of
5185 compiler, so the code does not diverge, and the wrapper may run the
5186 code necessary for keeping the profiling sane. */
5188 switch (DECL_FUNCTION_CODE (fn))
5190 case BUILT_IN_FORK:
5191 id = get_identifier ("__gcov_fork");
5192 break;
5194 case BUILT_IN_EXECL:
5195 id = get_identifier ("__gcov_execl");
5196 break;
5198 case BUILT_IN_EXECV:
5199 id = get_identifier ("__gcov_execv");
5200 break;
5202 case BUILT_IN_EXECLP:
5203 id = get_identifier ("__gcov_execlp");
5204 break;
5206 case BUILT_IN_EXECLE:
5207 id = get_identifier ("__gcov_execle");
5208 break;
5210 case BUILT_IN_EXECVP:
5211 id = get_identifier ("__gcov_execvp");
5212 break;
5214 case BUILT_IN_EXECVE:
5215 id = get_identifier ("__gcov_execve");
5216 break;
5218 default:
5219 gcc_unreachable ();
5222 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5223 DECL_EXTERNAL (decl) = 1;
5224 TREE_PUBLIC (decl) = 1;
5225 DECL_ARTIFICIAL (decl) = 1;
5226 TREE_NOTHROW (decl) = 1;
5227 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5228 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5229 call = build_function_call_expr (decl, arglist);
5231 return expand_call (call, target, ignore);
5234 /* Expand an expression EXP that calls a built-in function,
5235 with result going to TARGET if that's convenient
5236 (and in mode MODE if that's convenient).
5237 SUBTARGET may be used as the target for computing one of EXP's operands.
5238 IGNORE is nonzero if the value is to be ignored. */
5241 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5242 int ignore)
5244 tree fndecl = get_callee_fndecl (exp);
5245 tree arglist = TREE_OPERAND (exp, 1);
5246 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5247 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5249 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5250 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5252 /* When not optimizing, generate calls to library functions for a certain
5253 set of builtins. */
5254 if (!optimize
5255 && !CALLED_AS_BUILT_IN (fndecl)
5256 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5257 && fcode != BUILT_IN_ALLOCA)
5258 return expand_call (exp, target, ignore);
5260 /* The built-in function expanders test for target == const0_rtx
5261 to determine whether the function's result will be ignored. */
5262 if (ignore)
5263 target = const0_rtx;
5265 /* If the result of a pure or const built-in function is ignored, and
5266 none of its arguments are volatile, we can avoid expanding the
5267 built-in call and just evaluate the arguments for side-effects. */
5268 if (target == const0_rtx
5269 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5271 bool volatilep = false;
5272 tree arg;
5274 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5275 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5277 volatilep = true;
5278 break;
5281 if (! volatilep)
5283 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5284 expand_expr (TREE_VALUE (arg), const0_rtx,
5285 VOIDmode, EXPAND_NORMAL);
5286 return const0_rtx;
5290 switch (fcode)
5292 case BUILT_IN_FABS:
5293 case BUILT_IN_FABSF:
5294 case BUILT_IN_FABSL:
5295 target = expand_builtin_fabs (arglist, target, subtarget);
5296 if (target)
5297 return target;
5298 break;
5300 case BUILT_IN_COPYSIGN:
5301 case BUILT_IN_COPYSIGNF:
5302 case BUILT_IN_COPYSIGNL:
5303 target = expand_builtin_copysign (arglist, target, subtarget);
5304 if (target)
5305 return target;
5306 break;
5308 /* Just do a normal library call if we were unable to fold
5309 the values. */
5310 case BUILT_IN_CABS:
5311 case BUILT_IN_CABSF:
5312 case BUILT_IN_CABSL:
5313 break;
5315 case BUILT_IN_EXP:
5316 case BUILT_IN_EXPF:
5317 case BUILT_IN_EXPL:
5318 case BUILT_IN_EXP10:
5319 case BUILT_IN_EXP10F:
5320 case BUILT_IN_EXP10L:
5321 case BUILT_IN_POW10:
5322 case BUILT_IN_POW10F:
5323 case BUILT_IN_POW10L:
5324 case BUILT_IN_EXP2:
5325 case BUILT_IN_EXP2F:
5326 case BUILT_IN_EXP2L:
5327 case BUILT_IN_EXPM1:
5328 case BUILT_IN_EXPM1F:
5329 case BUILT_IN_EXPM1L:
5330 case BUILT_IN_LOGB:
5331 case BUILT_IN_LOGBF:
5332 case BUILT_IN_LOGBL:
5333 case BUILT_IN_ILOGB:
5334 case BUILT_IN_ILOGBF:
5335 case BUILT_IN_ILOGBL:
5336 case BUILT_IN_LOG:
5337 case BUILT_IN_LOGF:
5338 case BUILT_IN_LOGL:
5339 case BUILT_IN_LOG10:
5340 case BUILT_IN_LOG10F:
5341 case BUILT_IN_LOG10L:
5342 case BUILT_IN_LOG2:
5343 case BUILT_IN_LOG2F:
5344 case BUILT_IN_LOG2L:
5345 case BUILT_IN_LOG1P:
5346 case BUILT_IN_LOG1PF:
5347 case BUILT_IN_LOG1PL:
5348 case BUILT_IN_TAN:
5349 case BUILT_IN_TANF:
5350 case BUILT_IN_TANL:
5351 case BUILT_IN_ASIN:
5352 case BUILT_IN_ASINF:
5353 case BUILT_IN_ASINL:
5354 case BUILT_IN_ACOS:
5355 case BUILT_IN_ACOSF:
5356 case BUILT_IN_ACOSL:
5357 case BUILT_IN_ATAN:
5358 case BUILT_IN_ATANF:
5359 case BUILT_IN_ATANL:
5360 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5361 because of possible accuracy problems. */
5362 if (! flag_unsafe_math_optimizations)
5363 break;
5364 case BUILT_IN_SQRT:
5365 case BUILT_IN_SQRTF:
5366 case BUILT_IN_SQRTL:
5367 case BUILT_IN_FLOOR:
5368 case BUILT_IN_FLOORF:
5369 case BUILT_IN_FLOORL:
5370 case BUILT_IN_CEIL:
5371 case BUILT_IN_CEILF:
5372 case BUILT_IN_CEILL:
5373 case BUILT_IN_TRUNC:
5374 case BUILT_IN_TRUNCF:
5375 case BUILT_IN_TRUNCL:
5376 case BUILT_IN_ROUND:
5377 case BUILT_IN_ROUNDF:
5378 case BUILT_IN_ROUNDL:
5379 case BUILT_IN_NEARBYINT:
5380 case BUILT_IN_NEARBYINTF:
5381 case BUILT_IN_NEARBYINTL:
5382 case BUILT_IN_RINT:
5383 case BUILT_IN_RINTF:
5384 case BUILT_IN_RINTL:
5385 target = expand_builtin_mathfn (exp, target, subtarget);
5386 if (target)
5387 return target;
5388 break;
5390 case BUILT_IN_POW:
5391 case BUILT_IN_POWF:
5392 case BUILT_IN_POWL:
5393 target = expand_builtin_pow (exp, target, subtarget);
5394 if (target)
5395 return target;
5396 break;
5398 case BUILT_IN_POWI:
5399 case BUILT_IN_POWIF:
5400 case BUILT_IN_POWIL:
5401 target = expand_builtin_powi (exp, target, subtarget);
5402 if (target)
5403 return target;
5404 break;
5406 case BUILT_IN_ATAN2:
5407 case BUILT_IN_ATAN2F:
5408 case BUILT_IN_ATAN2L:
5409 case BUILT_IN_LDEXP:
5410 case BUILT_IN_LDEXPF:
5411 case BUILT_IN_LDEXPL:
5412 case BUILT_IN_FMOD:
5413 case BUILT_IN_FMODF:
5414 case BUILT_IN_FMODL:
5415 case BUILT_IN_DREM:
5416 case BUILT_IN_DREMF:
5417 case BUILT_IN_DREML:
5418 if (! flag_unsafe_math_optimizations)
5419 break;
5420 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5421 if (target)
5422 return target;
5423 break;
5425 case BUILT_IN_SIN:
5426 case BUILT_IN_SINF:
5427 case BUILT_IN_SINL:
5428 case BUILT_IN_COS:
5429 case BUILT_IN_COSF:
5430 case BUILT_IN_COSL:
5431 if (! flag_unsafe_math_optimizations)
5432 break;
5433 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5434 if (target)
5435 return target;
5436 break;
5438 case BUILT_IN_APPLY_ARGS:
5439 return expand_builtin_apply_args ();
5441 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5442 FUNCTION with a copy of the parameters described by
5443 ARGUMENTS, and ARGSIZE. It returns a block of memory
5444 allocated on the stack into which is stored all the registers
5445 that might possibly be used for returning the result of a
5446 function. ARGUMENTS is the value returned by
5447 __builtin_apply_args. ARGSIZE is the number of bytes of
5448 arguments that must be copied. ??? How should this value be
5449 computed? We'll also need a safe worst case value for varargs
5450 functions. */
5451 case BUILT_IN_APPLY:
5452 if (!validate_arglist (arglist, POINTER_TYPE,
5453 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5454 && !validate_arglist (arglist, REFERENCE_TYPE,
5455 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5456 return const0_rtx;
5457 else
5459 int i;
5460 tree t;
5461 rtx ops[3];
5463 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5464 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5466 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5469 /* __builtin_return (RESULT) causes the function to return the
5470 value described by RESULT. RESULT is address of the block of
5471 memory returned by __builtin_apply. */
5472 case BUILT_IN_RETURN:
5473 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5474 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5475 NULL_RTX, VOIDmode, 0));
5476 return const0_rtx;
5478 case BUILT_IN_SAVEREGS:
5479 return expand_builtin_saveregs ();
5481 case BUILT_IN_ARGS_INFO:
5482 return expand_builtin_args_info (arglist);
5484 /* Return the address of the first anonymous stack arg. */
5485 case BUILT_IN_NEXT_ARG:
5486 if (fold_builtin_next_arg (arglist))
5487 return const0_rtx;
5488 return expand_builtin_next_arg ();
5490 case BUILT_IN_CLASSIFY_TYPE:
5491 return expand_builtin_classify_type (arglist);
5493 case BUILT_IN_CONSTANT_P:
5494 return const0_rtx;
5496 case BUILT_IN_FRAME_ADDRESS:
5497 case BUILT_IN_RETURN_ADDRESS:
5498 return expand_builtin_frame_address (fndecl, arglist);
5500 /* Returns the address of the area where the structure is returned.
5501 0 otherwise. */
5502 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5503 if (arglist != 0
5504 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5505 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5506 return const0_rtx;
5507 else
5508 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5510 case BUILT_IN_ALLOCA:
5511 target = expand_builtin_alloca (arglist, target);
5512 if (target)
5513 return target;
5514 break;
5516 case BUILT_IN_STACK_SAVE:
5517 return expand_stack_save ();
5519 case BUILT_IN_STACK_RESTORE:
5520 expand_stack_restore (TREE_VALUE (arglist));
5521 return const0_rtx;
5523 case BUILT_IN_FFS:
5524 case BUILT_IN_FFSL:
5525 case BUILT_IN_FFSLL:
5526 case BUILT_IN_FFSIMAX:
5527 target = expand_builtin_unop (target_mode, arglist, target,
5528 subtarget, ffs_optab);
5529 if (target)
5530 return target;
5531 break;
5533 case BUILT_IN_CLZ:
5534 case BUILT_IN_CLZL:
5535 case BUILT_IN_CLZLL:
5536 case BUILT_IN_CLZIMAX:
5537 target = expand_builtin_unop (target_mode, arglist, target,
5538 subtarget, clz_optab);
5539 if (target)
5540 return target;
5541 break;
5543 case BUILT_IN_CTZ:
5544 case BUILT_IN_CTZL:
5545 case BUILT_IN_CTZLL:
5546 case BUILT_IN_CTZIMAX:
5547 target = expand_builtin_unop (target_mode, arglist, target,
5548 subtarget, ctz_optab);
5549 if (target)
5550 return target;
5551 break;
5553 case BUILT_IN_POPCOUNT:
5554 case BUILT_IN_POPCOUNTL:
5555 case BUILT_IN_POPCOUNTLL:
5556 case BUILT_IN_POPCOUNTIMAX:
5557 target = expand_builtin_unop (target_mode, arglist, target,
5558 subtarget, popcount_optab);
5559 if (target)
5560 return target;
5561 break;
5563 case BUILT_IN_PARITY:
5564 case BUILT_IN_PARITYL:
5565 case BUILT_IN_PARITYLL:
5566 case BUILT_IN_PARITYIMAX:
5567 target = expand_builtin_unop (target_mode, arglist, target,
5568 subtarget, parity_optab);
5569 if (target)
5570 return target;
5571 break;
5573 case BUILT_IN_STRLEN:
5574 target = expand_builtin_strlen (arglist, target, target_mode);
5575 if (target)
5576 return target;
5577 break;
5579 case BUILT_IN_STRCPY:
5580 target = expand_builtin_strcpy (exp, target, mode);
5581 if (target)
5582 return target;
5583 break;
5585 case BUILT_IN_STRNCPY:
5586 target = expand_builtin_strncpy (exp, target, mode);
5587 if (target)
5588 return target;
5589 break;
5591 case BUILT_IN_STPCPY:
5592 target = expand_builtin_stpcpy (exp, target, mode);
5593 if (target)
5594 return target;
5595 break;
5597 case BUILT_IN_STRCAT:
5598 target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5599 if (target)
5600 return target;
5601 break;
5603 case BUILT_IN_STRNCAT:
5604 target = expand_builtin_strncat (arglist, target, mode);
5605 if (target)
5606 return target;
5607 break;
5609 case BUILT_IN_STRSPN:
5610 target = expand_builtin_strspn (arglist, target, mode);
5611 if (target)
5612 return target;
5613 break;
5615 case BUILT_IN_STRCSPN:
5616 target = expand_builtin_strcspn (arglist, target, mode);
5617 if (target)
5618 return target;
5619 break;
5621 case BUILT_IN_STRSTR:
5622 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5623 if (target)
5624 return target;
5625 break;
5627 case BUILT_IN_STRPBRK:
5628 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5629 if (target)
5630 return target;
5631 break;
5633 case BUILT_IN_INDEX:
5634 case BUILT_IN_STRCHR:
5635 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5636 if (target)
5637 return target;
5638 break;
5640 case BUILT_IN_RINDEX:
5641 case BUILT_IN_STRRCHR:
5642 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5643 if (target)
5644 return target;
5645 break;
5647 case BUILT_IN_MEMCPY:
5648 target = expand_builtin_memcpy (exp, target, mode);
5649 if (target)
5650 return target;
5651 break;
5653 case BUILT_IN_MEMPCPY:
5654 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5655 if (target)
5656 return target;
5657 break;
5659 case BUILT_IN_MEMMOVE:
5660 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
5661 mode, exp);
5662 if (target)
5663 return target;
5664 break;
5666 case BUILT_IN_BCOPY:
5667 target = expand_builtin_bcopy (exp);
5668 if (target)
5669 return target;
5670 break;
5672 case BUILT_IN_MEMSET:
5673 target = expand_builtin_memset (arglist, target, mode, exp);
5674 if (target)
5675 return target;
5676 break;
5678 case BUILT_IN_BZERO:
5679 target = expand_builtin_bzero (exp);
5680 if (target)
5681 return target;
5682 break;
5684 case BUILT_IN_STRCMP:
5685 target = expand_builtin_strcmp (exp, target, mode);
5686 if (target)
5687 return target;
5688 break;
5690 case BUILT_IN_STRNCMP:
5691 target = expand_builtin_strncmp (exp, target, mode);
5692 if (target)
5693 return target;
5694 break;
5696 case BUILT_IN_BCMP:
5697 case BUILT_IN_MEMCMP:
5698 target = expand_builtin_memcmp (exp, arglist, target, mode);
5699 if (target)
5700 return target;
5701 break;
5703 case BUILT_IN_SETJMP:
5704 target = expand_builtin_setjmp (arglist, target);
5705 if (target)
5706 return target;
5707 break;
5709 /* __builtin_longjmp is passed a pointer to an array of five words.
5710 It's similar to the C library longjmp function but works with
5711 __builtin_setjmp above. */
5712 case BUILT_IN_LONGJMP:
5713 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5714 break;
5715 else
5717 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5718 VOIDmode, 0);
5719 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5720 NULL_RTX, VOIDmode, 0);
5722 if (value != const1_rtx)
5724 error ("%<__builtin_longjmp%> second argument must be 1");
5725 return const0_rtx;
5728 expand_builtin_longjmp (buf_addr, value);
5729 return const0_rtx;
5732 case BUILT_IN_NONLOCAL_GOTO:
5733 target = expand_builtin_nonlocal_goto (arglist);
5734 if (target)
5735 return target;
5736 break;
5738 /* This updates the setjmp buffer that is its argument with the value
5739 of the current stack pointer. */
5740 case BUILT_IN_UPDATE_SETJMP_BUF:
5741 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5743 rtx buf_addr
5744 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5746 expand_builtin_update_setjmp_buf (buf_addr);
5747 return const0_rtx;
5749 break;
5751 case BUILT_IN_TRAP:
5752 expand_builtin_trap ();
5753 return const0_rtx;
5755 case BUILT_IN_PRINTF:
5756 target = expand_builtin_printf (exp, target, mode, false);
5757 if (target)
5758 return target;
5759 break;
5761 case BUILT_IN_PRINTF_UNLOCKED:
5762 target = expand_builtin_printf (exp, target, mode, true);
5763 if (target)
5764 return target;
5765 break;
5767 case BUILT_IN_FPUTS:
5768 target = expand_builtin_fputs (arglist, target, false);
5769 if (target)
5770 return target;
5771 break;
5772 case BUILT_IN_FPUTS_UNLOCKED:
5773 target = expand_builtin_fputs (arglist, target, true);
5774 if (target)
5775 return target;
5776 break;
5778 case BUILT_IN_FPRINTF:
5779 target = expand_builtin_fprintf (exp, target, mode, false);
5780 if (target)
5781 return target;
5782 break;
5784 case BUILT_IN_FPRINTF_UNLOCKED:
5785 target = expand_builtin_fprintf (exp, target, mode, true);
5786 if (target)
5787 return target;
5788 break;
5790 case BUILT_IN_SPRINTF:
5791 target = expand_builtin_sprintf (arglist, target, mode);
5792 if (target)
5793 return target;
5794 break;
5796 case BUILT_IN_SIGNBIT:
5797 case BUILT_IN_SIGNBITF:
5798 case BUILT_IN_SIGNBITL:
5799 target = expand_builtin_signbit (exp, target);
5800 if (target)
5801 return target;
5802 break;
5804 /* Various hooks for the DWARF 2 __throw routine. */
5805 case BUILT_IN_UNWIND_INIT:
5806 expand_builtin_unwind_init ();
5807 return const0_rtx;
5808 case BUILT_IN_DWARF_CFA:
5809 return virtual_cfa_rtx;
5810 #ifdef DWARF2_UNWIND_INFO
5811 case BUILT_IN_DWARF_SP_COLUMN:
5812 return expand_builtin_dwarf_sp_column ();
5813 case BUILT_IN_INIT_DWARF_REG_SIZES:
5814 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5815 return const0_rtx;
5816 #endif
5817 case BUILT_IN_FROB_RETURN_ADDR:
5818 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5819 case BUILT_IN_EXTRACT_RETURN_ADDR:
5820 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5821 case BUILT_IN_EH_RETURN:
5822 expand_builtin_eh_return (TREE_VALUE (arglist),
5823 TREE_VALUE (TREE_CHAIN (arglist)));
5824 return const0_rtx;
5825 #ifdef EH_RETURN_DATA_REGNO
5826 case BUILT_IN_EH_RETURN_DATA_REGNO:
5827 return expand_builtin_eh_return_data_regno (arglist);
5828 #endif
5829 case BUILT_IN_EXTEND_POINTER:
5830 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5832 case BUILT_IN_VA_START:
5833 case BUILT_IN_STDARG_START:
5834 return expand_builtin_va_start (arglist);
5835 case BUILT_IN_VA_END:
5836 return expand_builtin_va_end (arglist);
5837 case BUILT_IN_VA_COPY:
5838 return expand_builtin_va_copy (arglist);
5839 case BUILT_IN_EXPECT:
5840 return expand_builtin_expect (arglist, target);
5841 case BUILT_IN_PREFETCH:
5842 expand_builtin_prefetch (arglist);
5843 return const0_rtx;
5845 case BUILT_IN_PROFILE_FUNC_ENTER:
5846 return expand_builtin_profile_func (false);
5847 case BUILT_IN_PROFILE_FUNC_EXIT:
5848 return expand_builtin_profile_func (true);
5850 case BUILT_IN_INIT_TRAMPOLINE:
5851 return expand_builtin_init_trampoline (arglist);
5852 case BUILT_IN_ADJUST_TRAMPOLINE:
5853 return expand_builtin_adjust_trampoline (arglist);
5855 case BUILT_IN_FORK:
5856 case BUILT_IN_EXECL:
5857 case BUILT_IN_EXECV:
5858 case BUILT_IN_EXECLP:
5859 case BUILT_IN_EXECLE:
5860 case BUILT_IN_EXECVP:
5861 case BUILT_IN_EXECVE:
5862 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5863 if (target)
5864 return target;
5865 break;
5867 default: /* just do library call, if unknown builtin */
5868 break;
5871 /* The switch statement above can drop through to cause the function
5872 to be called normally. */
5873 return expand_call (exp, target, ignore);
5876 /* Determine whether a tree node represents a call to a built-in
5877 function. If the tree T is a call to a built-in function with
5878 the right number of arguments of the appropriate types, return
5879 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5880 Otherwise the return value is END_BUILTINS. */
5882 enum built_in_function
5883 builtin_mathfn_code (tree t)
5885 tree fndecl, arglist, parmlist;
5886 tree argtype, parmtype;
5888 if (TREE_CODE (t) != CALL_EXPR
5889 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5890 return END_BUILTINS;
5892 fndecl = get_callee_fndecl (t);
5893 if (fndecl == NULL_TREE
5894 || TREE_CODE (fndecl) != FUNCTION_DECL
5895 || ! DECL_BUILT_IN (fndecl)
5896 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5897 return END_BUILTINS;
5899 arglist = TREE_OPERAND (t, 1);
5900 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5901 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5903 /* If a function doesn't take a variable number of arguments,
5904 the last element in the list will have type `void'. */
5905 parmtype = TREE_VALUE (parmlist);
5906 if (VOID_TYPE_P (parmtype))
5908 if (arglist)
5909 return END_BUILTINS;
5910 return DECL_FUNCTION_CODE (fndecl);
5913 if (! arglist)
5914 return END_BUILTINS;
5916 argtype = TREE_TYPE (TREE_VALUE (arglist));
5918 if (SCALAR_FLOAT_TYPE_P (parmtype))
5920 if (! SCALAR_FLOAT_TYPE_P (argtype))
5921 return END_BUILTINS;
5923 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5925 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5926 return END_BUILTINS;
5928 else if (POINTER_TYPE_P (parmtype))
5930 if (! POINTER_TYPE_P (argtype))
5931 return END_BUILTINS;
5933 else if (INTEGRAL_TYPE_P (parmtype))
5935 if (! INTEGRAL_TYPE_P (argtype))
5936 return END_BUILTINS;
5938 else
5939 return END_BUILTINS;
5941 arglist = TREE_CHAIN (arglist);
5944 /* Variable-length argument list. */
5945 return DECL_FUNCTION_CODE (fndecl);
5948 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5949 constant. ARGLIST is the argument list of the call. */
5951 static tree
5952 fold_builtin_constant_p (tree arglist)
5954 if (arglist == 0)
5955 return 0;
5957 arglist = TREE_VALUE (arglist);
5959 /* We return 1 for a numeric type that's known to be a constant
5960 value at compile-time or for an aggregate type that's a
5961 literal constant. */
5962 STRIP_NOPS (arglist);
5964 /* If we know this is a constant, emit the constant of one. */
5965 if (CONSTANT_CLASS_P (arglist)
5966 || (TREE_CODE (arglist) == CONSTRUCTOR
5967 && TREE_CONSTANT (arglist)))
5968 return integer_one_node;
5969 if (TREE_CODE (arglist) == ADDR_EXPR)
5971 tree op = TREE_OPERAND (arglist, 0);
5972 if (TREE_CODE (op) == STRING_CST
5973 || (TREE_CODE (op) == ARRAY_REF
5974 && integer_zerop (TREE_OPERAND (op, 1))
5975 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
5976 return integer_one_node;
5979 /* If this expression has side effects, show we don't know it to be a
5980 constant. Likewise if it's a pointer or aggregate type since in
5981 those case we only want literals, since those are only optimized
5982 when generating RTL, not later.
5983 And finally, if we are compiling an initializer, not code, we
5984 need to return a definite result now; there's not going to be any
5985 more optimization done. */
5986 if (TREE_SIDE_EFFECTS (arglist)
5987 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5988 || POINTER_TYPE_P (TREE_TYPE (arglist))
5989 || cfun == 0)
5990 return integer_zero_node;
5992 return 0;
5995 /* Fold a call to __builtin_expect, if we expect that a comparison against
5996 the argument will fold to a constant. In practice, this means a true
5997 constant or the address of a non-weak symbol. ARGLIST is the argument
5998 list of the call. */
6000 static tree
6001 fold_builtin_expect (tree arglist)
6003 tree arg, inner;
6005 if (arglist == 0)
6006 return 0;
6008 arg = TREE_VALUE (arglist);
6010 /* If the argument isn't invariant, then there's nothing we can do. */
6011 if (!TREE_INVARIANT (arg))
6012 return 0;
6014 /* If we're looking at an address of a weak decl, then do not fold. */
6015 inner = arg;
6016 STRIP_NOPS (inner);
6017 if (TREE_CODE (inner) == ADDR_EXPR)
6021 inner = TREE_OPERAND (inner, 0);
6023 while (TREE_CODE (inner) == COMPONENT_REF
6024 || TREE_CODE (inner) == ARRAY_REF);
6025 if (DECL_P (inner) && DECL_WEAK (inner))
6026 return 0;
6029 /* Otherwise, ARG already has the proper type for the return value. */
6030 return arg;
6033 /* Fold a call to __builtin_classify_type. */
6035 static tree
6036 fold_builtin_classify_type (tree arglist)
6038 if (arglist == 0)
6039 return build_int_cst (NULL_TREE, no_type_class);
6041 return build_int_cst (NULL_TREE,
6042 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6045 /* Fold a call to __builtin_strlen. */
6047 static tree
6048 fold_builtin_strlen (tree arglist)
6050 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6051 return NULL_TREE;
6052 else
6054 tree len = c_strlen (TREE_VALUE (arglist), 0);
6056 if (len)
6058 /* Convert from the internal "sizetype" type to "size_t". */
6059 if (size_type_node)
6060 len = fold_convert (size_type_node, len);
6061 return len;
6064 return NULL_TREE;
6068 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6070 static tree
6071 fold_builtin_inf (tree type, int warn)
6073 REAL_VALUE_TYPE real;
6075 /* __builtin_inff is intended to be usable to define INFINITY on all
6076 targets. If an infinity is not available, INFINITY expands "to a
6077 positive constant of type float that overflows at translation
6078 time", footnote "In this case, using INFINITY will violate the
6079 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6080 Thus we pedwarn to ensure this constraint violation is
6081 diagnosed. */
6082 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6083 pedwarn ("target format does not support infinity");
6085 real_inf (&real);
6086 return build_real (type, real);
6089 /* Fold a call to __builtin_nan or __builtin_nans. */
6091 static tree
6092 fold_builtin_nan (tree arglist, tree type, int quiet)
6094 REAL_VALUE_TYPE real;
6095 const char *str;
6097 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6098 return 0;
6099 str = c_getstr (TREE_VALUE (arglist));
6100 if (!str)
6101 return 0;
6103 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6104 return 0;
6106 return build_real (type, real);
6109 /* Return true if the floating point expression T has an integer value.
6110 We also allow +Inf, -Inf and NaN to be considered integer values. */
6112 static bool
6113 integer_valued_real_p (tree t)
6115 switch (TREE_CODE (t))
6117 case FLOAT_EXPR:
6118 return true;
6120 case ABS_EXPR:
6121 case SAVE_EXPR:
6122 case NON_LVALUE_EXPR:
6123 return integer_valued_real_p (TREE_OPERAND (t, 0));
6125 case COMPOUND_EXPR:
6126 case MODIFY_EXPR:
6127 case BIND_EXPR:
6128 return integer_valued_real_p (TREE_OPERAND (t, 1));
6130 case PLUS_EXPR:
6131 case MINUS_EXPR:
6132 case MULT_EXPR:
6133 case MIN_EXPR:
6134 case MAX_EXPR:
6135 return integer_valued_real_p (TREE_OPERAND (t, 0))
6136 && integer_valued_real_p (TREE_OPERAND (t, 1));
6138 case COND_EXPR:
6139 return integer_valued_real_p (TREE_OPERAND (t, 1))
6140 && integer_valued_real_p (TREE_OPERAND (t, 2));
6142 case REAL_CST:
6143 if (! TREE_CONSTANT_OVERFLOW (t))
6145 REAL_VALUE_TYPE c, cint;
6147 c = TREE_REAL_CST (t);
6148 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6149 return real_identical (&c, &cint);
6151 break;
6153 case NOP_EXPR:
6155 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6156 if (TREE_CODE (type) == INTEGER_TYPE)
6157 return true;
6158 if (TREE_CODE (type) == REAL_TYPE)
6159 return integer_valued_real_p (TREE_OPERAND (t, 0));
6160 break;
6163 case CALL_EXPR:
6164 switch (builtin_mathfn_code (t))
6166 case BUILT_IN_CEIL:
6167 case BUILT_IN_CEILF:
6168 case BUILT_IN_CEILL:
6169 case BUILT_IN_FLOOR:
6170 case BUILT_IN_FLOORF:
6171 case BUILT_IN_FLOORL:
6172 case BUILT_IN_NEARBYINT:
6173 case BUILT_IN_NEARBYINTF:
6174 case BUILT_IN_NEARBYINTL:
6175 case BUILT_IN_RINT:
6176 case BUILT_IN_RINTF:
6177 case BUILT_IN_RINTL:
6178 case BUILT_IN_ROUND:
6179 case BUILT_IN_ROUNDF:
6180 case BUILT_IN_ROUNDL:
6181 case BUILT_IN_TRUNC:
6182 case BUILT_IN_TRUNCF:
6183 case BUILT_IN_TRUNCL:
6184 return true;
6186 default:
6187 break;
6189 break;
6191 default:
6192 break;
6194 return false;
6197 /* EXP is assumed to be builtin call where truncation can be propagated
6198 across (for instance floor((double)f) == (double)floorf (f).
6199 Do the transformation. */
6201 static tree
6202 fold_trunc_transparent_mathfn (tree exp)
6204 tree fndecl = get_callee_fndecl (exp);
6205 tree arglist = TREE_OPERAND (exp, 1);
6206 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6207 tree arg;
6209 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6210 return 0;
6212 arg = TREE_VALUE (arglist);
6213 /* Integer rounding functions are idempotent. */
6214 if (fcode == builtin_mathfn_code (arg))
6215 return arg;
6217 /* If argument is already integer valued, and we don't need to worry
6218 about setting errno, there's no need to perform rounding. */
6219 if (! flag_errno_math && integer_valued_real_p (arg))
6220 return arg;
6222 if (optimize)
6224 tree arg0 = strip_float_extensions (arg);
6225 tree ftype = TREE_TYPE (exp);
6226 tree newtype = TREE_TYPE (arg0);
6227 tree decl;
6229 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6230 && (decl = mathfn_built_in (newtype, fcode)))
6232 arglist =
6233 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6234 return fold_convert (ftype,
6235 build_function_call_expr (decl, arglist));
6238 return 0;
6241 /* EXP is assumed to be builtin call which can narrow the FP type of
6242 the argument, for instance lround((double)f) -> lroundf (f). */
6244 static tree
6245 fold_fixed_mathfn (tree exp)
6247 tree fndecl = get_callee_fndecl (exp);
6248 tree arglist = TREE_OPERAND (exp, 1);
6249 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6250 tree arg;
6252 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6253 return 0;
6255 arg = TREE_VALUE (arglist);
6257 /* If argument is already integer valued, and we don't need to worry
6258 about setting errno, there's no need to perform rounding. */
6259 if (! flag_errno_math && integer_valued_real_p (arg))
6260 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (exp), arg));
6262 if (optimize)
6264 tree ftype = TREE_TYPE (arg);
6265 tree arg0 = strip_float_extensions (arg);
6266 tree newtype = TREE_TYPE (arg0);
6267 tree decl;
6269 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6270 && (decl = mathfn_built_in (newtype, fcode)))
6272 arglist =
6273 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6274 return build_function_call_expr (decl, arglist);
6278 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6279 sizeof (long long) == sizeof (long). */
6280 if (TYPE_PRECISION (long_long_integer_type_node)
6281 == TYPE_PRECISION (long_integer_type_node))
6283 tree newfn = NULL_TREE;
6284 switch (fcode)
6286 case BUILT_IN_LLROUND:
6287 case BUILT_IN_LLROUNDF:
6288 case BUILT_IN_LLROUNDL:
6289 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6290 break;
6292 case BUILT_IN_LLRINT:
6293 case BUILT_IN_LLRINTF:
6294 case BUILT_IN_LLRINTL:
6295 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6296 break;
6298 default:
6299 break;
6302 if (newfn)
6304 tree newcall = build_function_call_expr (newfn, arglist);
6305 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6309 return 0;
6312 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6313 is the argument list and TYPE is the return type. Return
6314 NULL_TREE if no if no simplification can be made. */
6316 static tree
6317 fold_builtin_cabs (tree arglist, tree type)
6319 tree arg;
6321 if (!arglist || TREE_CHAIN (arglist))
6322 return NULL_TREE;
6324 arg = TREE_VALUE (arglist);
6325 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6326 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6327 return NULL_TREE;
6329 /* Evaluate cabs of a constant at compile-time. */
6330 if (flag_unsafe_math_optimizations
6331 && TREE_CODE (arg) == COMPLEX_CST
6332 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6333 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6334 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6335 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6337 REAL_VALUE_TYPE r, i;
6339 r = TREE_REAL_CST (TREE_REALPART (arg));
6340 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6342 real_arithmetic (&r, MULT_EXPR, &r, &r);
6343 real_arithmetic (&i, MULT_EXPR, &i, &i);
6344 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6345 if (real_sqrt (&r, TYPE_MODE (type), &r)
6346 || ! flag_trapping_math)
6347 return build_real (type, r);
6350 /* If either part is zero, cabs is fabs of the other. */
6351 if (TREE_CODE (arg) == COMPLEX_EXPR
6352 && real_zerop (TREE_OPERAND (arg, 0)))
6353 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6354 if (TREE_CODE (arg) == COMPLEX_EXPR
6355 && real_zerop (TREE_OPERAND (arg, 1)))
6356 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6358 /* Don't do this when optimizing for size. */
6359 if (flag_unsafe_math_optimizations
6360 && optimize && !optimize_size)
6362 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6364 if (sqrtfn != NULL_TREE)
6366 tree rpart, ipart, result, arglist;
6368 arg = builtin_save_expr (arg);
6370 rpart = fold (build1 (REALPART_EXPR, type, arg));
6371 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6373 rpart = builtin_save_expr (rpart);
6374 ipart = builtin_save_expr (ipart);
6376 result = fold (build2 (PLUS_EXPR, type,
6377 fold (build2 (MULT_EXPR, type,
6378 rpart, rpart)),
6379 fold (build2 (MULT_EXPR, type,
6380 ipart, ipart))));
6382 arglist = build_tree_list (NULL_TREE, result);
6383 return build_function_call_expr (sqrtfn, arglist);
6387 return NULL_TREE;
6390 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6391 NULL_TREE if no simplification can be made. */
6393 static tree
6394 fold_builtin_sqrt (tree arglist, tree type)
6397 enum built_in_function fcode;
6398 tree arg = TREE_VALUE (arglist);
6400 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6401 return NULL_TREE;
6403 /* Optimize sqrt of constant value. */
6404 if (TREE_CODE (arg) == REAL_CST
6405 && ! TREE_CONSTANT_OVERFLOW (arg))
6407 REAL_VALUE_TYPE r, x;
6409 x = TREE_REAL_CST (arg);
6410 if (real_sqrt (&r, TYPE_MODE (type), &x)
6411 || (!flag_trapping_math && !flag_errno_math))
6412 return build_real (type, r);
6415 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6416 fcode = builtin_mathfn_code (arg);
6417 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6419 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6420 arg = fold (build2 (MULT_EXPR, type,
6421 TREE_VALUE (TREE_OPERAND (arg, 1)),
6422 build_real (type, dconsthalf)));
6423 arglist = build_tree_list (NULL_TREE, arg);
6424 return build_function_call_expr (expfn, arglist);
6427 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6428 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6430 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6432 if (powfn)
6434 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6435 tree tree_root;
6436 /* The inner root was either sqrt or cbrt. */
6437 REAL_VALUE_TYPE dconstroot =
6438 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6440 /* Adjust for the outer root. */
6441 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6442 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6443 tree_root = build_real (type, dconstroot);
6444 arglist = tree_cons (NULL_TREE, arg0,
6445 build_tree_list (NULL_TREE, tree_root));
6446 return build_function_call_expr (powfn, arglist);
6450 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6451 if (flag_unsafe_math_optimizations
6452 && (fcode == BUILT_IN_POW
6453 || fcode == BUILT_IN_POWF
6454 || fcode == BUILT_IN_POWL))
6456 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6457 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6458 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6459 tree narg1;
6460 if (!tree_expr_nonnegative_p (arg0))
6461 arg0 = build1 (ABS_EXPR, type, arg0);
6462 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6463 build_real (type, dconsthalf)));
6464 arglist = tree_cons (NULL_TREE, arg0,
6465 build_tree_list (NULL_TREE, narg1));
6466 return build_function_call_expr (powfn, arglist);
6469 return NULL_TREE;
6472 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6473 NULL_TREE if no simplification can be made. */
6474 static tree
6475 fold_builtin_cbrt (tree arglist, tree type)
6477 tree arg = TREE_VALUE (arglist);
6478 const enum built_in_function fcode = builtin_mathfn_code (arg);
6480 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6481 return NULL_TREE;
6483 /* Optimize cbrt of constant value. */
6484 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6485 return arg;
6487 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6488 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6490 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6491 const REAL_VALUE_TYPE third_trunc =
6492 real_value_truncate (TYPE_MODE (type), dconstthird);
6493 arg = fold (build2 (MULT_EXPR, type,
6494 TREE_VALUE (TREE_OPERAND (arg, 1)),
6495 build_real (type, third_trunc)));
6496 arglist = build_tree_list (NULL_TREE, arg);
6497 return build_function_call_expr (expfn, arglist);
6500 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6501 /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
6502 x is negative pow will error but cbrt won't. */
6503 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
6505 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6507 if (powfn)
6509 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6510 tree tree_root;
6511 REAL_VALUE_TYPE dconstroot = dconstthird;
6513 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6514 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6515 tree_root = build_real (type, dconstroot);
6516 arglist = tree_cons (NULL_TREE, arg0,
6517 build_tree_list (NULL_TREE, tree_root));
6518 return build_function_call_expr (powfn, arglist);
6522 return NULL_TREE;
6525 /* Fold function call to builtin sin, sinf, or sinl. Return
6526 NULL_TREE if no simplification can be made. */
6527 static tree
6528 fold_builtin_sin (tree arglist)
6530 tree arg = TREE_VALUE (arglist);
6532 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6533 return NULL_TREE;
6535 /* Optimize sin (0.0) = 0.0. */
6536 if (real_zerop (arg))
6537 return arg;
6539 return NULL_TREE;
6542 /* Fold function call to builtin cos, cosf, or cosl. Return
6543 NULL_TREE if no simplification can be made. */
6544 static tree
6545 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6547 tree arg = TREE_VALUE (arglist);
6549 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6550 return NULL_TREE;
6552 /* Optimize cos (0.0) = 1.0. */
6553 if (real_zerop (arg))
6554 return build_real (type, dconst1);
6556 /* Optimize cos(-x) into cos (x). */
6557 if (TREE_CODE (arg) == NEGATE_EXPR)
6559 tree args = build_tree_list (NULL_TREE,
6560 TREE_OPERAND (arg, 0));
6561 return build_function_call_expr (fndecl, args);
6564 return NULL_TREE;
6567 /* Fold function call to builtin tan, tanf, or tanl. Return
6568 NULL_TREE if no simplification can be made. */
6569 static tree
6570 fold_builtin_tan (tree arglist)
6572 enum built_in_function fcode;
6573 tree arg = TREE_VALUE (arglist);
6575 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6576 return NULL_TREE;
6578 /* Optimize tan(0.0) = 0.0. */
6579 if (real_zerop (arg))
6580 return arg;
6582 /* Optimize tan(atan(x)) = x. */
6583 fcode = builtin_mathfn_code (arg);
6584 if (flag_unsafe_math_optimizations
6585 && (fcode == BUILT_IN_ATAN
6586 || fcode == BUILT_IN_ATANF
6587 || fcode == BUILT_IN_ATANL))
6588 return TREE_VALUE (TREE_OPERAND (arg, 1));
6590 return NULL_TREE;
6593 /* Fold function call to builtin atan, atanf, or atanl. Return
6594 NULL_TREE if no simplification can be made. */
6596 static tree
6597 fold_builtin_atan (tree arglist, tree type)
6600 tree arg = TREE_VALUE (arglist);
6602 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6603 return NULL_TREE;
6605 /* Optimize atan(0.0) = 0.0. */
6606 if (real_zerop (arg))
6607 return arg;
6609 /* Optimize atan(1.0) = pi/4. */
6610 if (real_onep (arg))
6612 REAL_VALUE_TYPE cst;
6614 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6615 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6616 return build_real (type, cst);
6619 return NULL_TREE;
6622 /* Fold function call to builtin trunc, truncf or truncl. Return
6623 NULL_TREE if no simplification can be made. */
6625 static tree
6626 fold_builtin_trunc (tree exp)
6628 tree arglist = TREE_OPERAND (exp, 1);
6629 tree arg;
6631 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6632 return 0;
6634 /* Optimize trunc of constant value. */
6635 arg = TREE_VALUE (arglist);
6636 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6638 REAL_VALUE_TYPE r, x;
6639 tree type = TREE_TYPE (exp);
6641 x = TREE_REAL_CST (arg);
6642 real_trunc (&r, TYPE_MODE (type), &x);
6643 return build_real (type, r);
6646 return fold_trunc_transparent_mathfn (exp);
6649 /* Fold function call to builtin floor, floorf or floorl. Return
6650 NULL_TREE if no simplification can be made. */
6652 static tree
6653 fold_builtin_floor (tree exp)
6655 tree arglist = TREE_OPERAND (exp, 1);
6656 tree arg;
6658 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6659 return 0;
6661 /* Optimize floor of constant value. */
6662 arg = TREE_VALUE (arglist);
6663 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6665 REAL_VALUE_TYPE x;
6667 x = TREE_REAL_CST (arg);
6668 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6670 tree type = TREE_TYPE (exp);
6671 REAL_VALUE_TYPE r;
6673 real_floor (&r, TYPE_MODE (type), &x);
6674 return build_real (type, r);
6678 return fold_trunc_transparent_mathfn (exp);
6681 /* Fold function call to builtin ceil, ceilf or ceill. Return
6682 NULL_TREE if no simplification can be made. */
6684 static tree
6685 fold_builtin_ceil (tree exp)
6687 tree arglist = TREE_OPERAND (exp, 1);
6688 tree arg;
6690 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6691 return 0;
6693 /* Optimize ceil of constant value. */
6694 arg = TREE_VALUE (arglist);
6695 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6697 REAL_VALUE_TYPE x;
6699 x = TREE_REAL_CST (arg);
6700 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6702 tree type = TREE_TYPE (exp);
6703 REAL_VALUE_TYPE r;
6705 real_ceil (&r, TYPE_MODE (type), &x);
6706 return build_real (type, r);
6710 return fold_trunc_transparent_mathfn (exp);
6713 /* Fold function call to builtin round, roundf or roundl. Return
6714 NULL_TREE if no simplification can be made. */
6716 static tree
6717 fold_builtin_round (tree exp)
6719 tree arglist = TREE_OPERAND (exp, 1);
6720 tree arg;
6722 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6723 return 0;
6725 /* Optimize round of constant value. */
6726 arg = TREE_VALUE (arglist);
6727 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6729 REAL_VALUE_TYPE x;
6731 x = TREE_REAL_CST (arg);
6732 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6734 tree type = TREE_TYPE (exp);
6735 REAL_VALUE_TYPE r;
6737 real_round (&r, TYPE_MODE (type), &x);
6738 return build_real (type, r);
6742 return fold_trunc_transparent_mathfn (exp);
6745 /* Fold function call to builtin lround, lroundf or lroundl (or the
6746 corresponding long long versions). Return NULL_TREE if no
6747 simplification can be made. */
6749 static tree
6750 fold_builtin_lround (tree exp)
6752 tree arglist = TREE_OPERAND (exp, 1);
6753 tree arg;
6755 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6756 return 0;
6758 /* Optimize lround of constant value. */
6759 arg = TREE_VALUE (arglist);
6760 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6762 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6764 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6766 tree itype = TREE_TYPE (exp), ftype = TREE_TYPE (arg), result;
6767 HOST_WIDE_INT hi, lo;
6768 REAL_VALUE_TYPE r;
6770 real_round (&r, TYPE_MODE (ftype), &x);
6771 REAL_VALUE_TO_INT (&lo, &hi, r);
6772 result = build_int_cst_wide (NULL_TREE, lo, hi);
6773 if (int_fits_type_p (result, itype))
6774 return fold_convert (itype, result);
6778 return fold_fixed_mathfn (exp);
6781 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6782 and their long and long long variants (i.e. ffsl and ffsll).
6783 Return NULL_TREE if no simplification can be made. */
6785 static tree
6786 fold_builtin_bitop (tree exp)
6788 tree fndecl = get_callee_fndecl (exp);
6789 tree arglist = TREE_OPERAND (exp, 1);
6790 tree arg;
6792 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6793 return NULL_TREE;
6795 /* Optimize for constant argument. */
6796 arg = TREE_VALUE (arglist);
6797 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6799 HOST_WIDE_INT hi, width, result;
6800 unsigned HOST_WIDE_INT lo;
6801 tree type;
6803 type = TREE_TYPE (arg);
6804 width = TYPE_PRECISION (type);
6805 lo = TREE_INT_CST_LOW (arg);
6807 /* Clear all the bits that are beyond the type's precision. */
6808 if (width > HOST_BITS_PER_WIDE_INT)
6810 hi = TREE_INT_CST_HIGH (arg);
6811 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6812 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6814 else
6816 hi = 0;
6817 if (width < HOST_BITS_PER_WIDE_INT)
6818 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6821 switch (DECL_FUNCTION_CODE (fndecl))
6823 case BUILT_IN_FFS:
6824 case BUILT_IN_FFSL:
6825 case BUILT_IN_FFSLL:
6826 if (lo != 0)
6827 result = exact_log2 (lo & -lo) + 1;
6828 else if (hi != 0)
6829 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6830 else
6831 result = 0;
6832 break;
6834 case BUILT_IN_CLZ:
6835 case BUILT_IN_CLZL:
6836 case BUILT_IN_CLZLL:
6837 if (hi != 0)
6838 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6839 else if (lo != 0)
6840 result = width - floor_log2 (lo) - 1;
6841 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6842 result = width;
6843 break;
6845 case BUILT_IN_CTZ:
6846 case BUILT_IN_CTZL:
6847 case BUILT_IN_CTZLL:
6848 if (lo != 0)
6849 result = exact_log2 (lo & -lo);
6850 else if (hi != 0)
6851 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6852 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6853 result = width;
6854 break;
6856 case BUILT_IN_POPCOUNT:
6857 case BUILT_IN_POPCOUNTL:
6858 case BUILT_IN_POPCOUNTLL:
6859 result = 0;
6860 while (lo)
6861 result++, lo &= lo - 1;
6862 while (hi)
6863 result++, hi &= hi - 1;
6864 break;
6866 case BUILT_IN_PARITY:
6867 case BUILT_IN_PARITYL:
6868 case BUILT_IN_PARITYLL:
6869 result = 0;
6870 while (lo)
6871 result++, lo &= lo - 1;
6872 while (hi)
6873 result++, hi &= hi - 1;
6874 result &= 1;
6875 break;
6877 default:
6878 gcc_unreachable ();
6881 return build_int_cst (TREE_TYPE (exp), result);
6884 return NULL_TREE;
6887 /* Return true if EXPR is the real constant contained in VALUE. */
6889 static bool
6890 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6892 STRIP_NOPS (expr);
6894 return ((TREE_CODE (expr) == REAL_CST
6895 && ! TREE_CONSTANT_OVERFLOW (expr)
6896 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6897 || (TREE_CODE (expr) == COMPLEX_CST
6898 && real_dconstp (TREE_REALPART (expr), value)
6899 && real_zerop (TREE_IMAGPART (expr))));
6902 /* A subroutine of fold_builtin to fold the various logarithmic
6903 functions. EXP is the CALL_EXPR of a call to a builtin logN
6904 function. VALUE is the base of the logN function. */
6906 static tree
6907 fold_builtin_logarithm (tree exp, const REAL_VALUE_TYPE *value)
6909 tree arglist = TREE_OPERAND (exp, 1);
6911 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6913 tree fndecl = get_callee_fndecl (exp);
6914 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6915 tree arg = TREE_VALUE (arglist);
6916 const enum built_in_function fcode = builtin_mathfn_code (arg);
6918 /* Optimize logN(1.0) = 0.0. */
6919 if (real_onep (arg))
6920 return build_real (type, dconst0);
6922 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6923 exactly, then only do this if flag_unsafe_math_optimizations. */
6924 if (exact_real_truncate (TYPE_MODE (type), value)
6925 || flag_unsafe_math_optimizations)
6927 const REAL_VALUE_TYPE value_truncate =
6928 real_value_truncate (TYPE_MODE (type), *value);
6929 if (real_dconstp (arg, &value_truncate))
6930 return build_real (type, dconst1);
6933 /* Special case, optimize logN(expN(x)) = x. */
6934 if (flag_unsafe_math_optimizations
6935 && ((value == &dconste
6936 && (fcode == BUILT_IN_EXP
6937 || fcode == BUILT_IN_EXPF
6938 || fcode == BUILT_IN_EXPL))
6939 || (value == &dconst2
6940 && (fcode == BUILT_IN_EXP2
6941 || fcode == BUILT_IN_EXP2F
6942 || fcode == BUILT_IN_EXP2L))
6943 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6944 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6946 /* Optimize logN(func()) for various exponential functions. We
6947 want to determine the value "x" and the power "exponent" in
6948 order to transform logN(x**exponent) into exponent*logN(x). */
6949 if (flag_unsafe_math_optimizations)
6951 tree exponent = 0, x = 0;
6953 switch (fcode)
6955 case BUILT_IN_EXP:
6956 case BUILT_IN_EXPF:
6957 case BUILT_IN_EXPL:
6958 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6959 x = build_real (type,
6960 real_value_truncate (TYPE_MODE (type), dconste));
6961 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6962 break;
6963 case BUILT_IN_EXP2:
6964 case BUILT_IN_EXP2F:
6965 case BUILT_IN_EXP2L:
6966 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6967 x = build_real (type, dconst2);
6968 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6969 break;
6970 case BUILT_IN_EXP10:
6971 case BUILT_IN_EXP10F:
6972 case BUILT_IN_EXP10L:
6973 case BUILT_IN_POW10:
6974 case BUILT_IN_POW10F:
6975 case BUILT_IN_POW10L:
6976 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6977 x = build_real (type, dconst10);
6978 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6979 break;
6980 case BUILT_IN_SQRT:
6981 case BUILT_IN_SQRTF:
6982 case BUILT_IN_SQRTL:
6983 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6984 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6985 exponent = build_real (type, dconsthalf);
6986 break;
6987 case BUILT_IN_CBRT:
6988 case BUILT_IN_CBRTF:
6989 case BUILT_IN_CBRTL:
6990 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6991 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6992 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6993 dconstthird));
6994 break;
6995 case BUILT_IN_POW:
6996 case BUILT_IN_POWF:
6997 case BUILT_IN_POWL:
6998 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6999 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7000 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7001 break;
7002 default:
7003 break;
7006 /* Now perform the optimization. */
7007 if (x && exponent)
7009 tree logfn;
7010 arglist = build_tree_list (NULL_TREE, x);
7011 logfn = build_function_call_expr (fndecl, arglist);
7012 return fold (build2 (MULT_EXPR, type, exponent, logfn));
7017 return 0;
7020 /* Fold a builtin function call to pow, powf, or powl. Return
7021 NULL_TREE if no simplification can be made. */
7022 static tree
7023 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7025 enum built_in_function fcode;
7026 tree arg0 = TREE_VALUE (arglist);
7027 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7029 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7030 return NULL_TREE;
7032 /* Optimize pow(1.0,y) = 1.0. */
7033 if (real_onep (arg0))
7034 return omit_one_operand (type, build_real (type, dconst1), arg1);
7036 if (TREE_CODE (arg1) == REAL_CST
7037 && ! TREE_CONSTANT_OVERFLOW (arg1))
7039 REAL_VALUE_TYPE cint;
7040 REAL_VALUE_TYPE c;
7041 HOST_WIDE_INT n;
7043 c = TREE_REAL_CST (arg1);
7045 /* Optimize pow(x,0.0) = 1.0. */
7046 if (REAL_VALUES_EQUAL (c, dconst0))
7047 return omit_one_operand (type, build_real (type, dconst1),
7048 arg0);
7050 /* Optimize pow(x,1.0) = x. */
7051 if (REAL_VALUES_EQUAL (c, dconst1))
7052 return arg0;
7054 /* Optimize pow(x,-1.0) = 1.0/x. */
7055 if (REAL_VALUES_EQUAL (c, dconstm1))
7056 return fold (build2 (RDIV_EXPR, type,
7057 build_real (type, dconst1), arg0));
7059 /* Optimize pow(x,0.5) = sqrt(x). */
7060 if (flag_unsafe_math_optimizations
7061 && REAL_VALUES_EQUAL (c, dconsthalf))
7063 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7065 if (sqrtfn != NULL_TREE)
7067 tree arglist = build_tree_list (NULL_TREE, arg0);
7068 return build_function_call_expr (sqrtfn, arglist);
7072 /* Check for an integer exponent. */
7073 n = real_to_integer (&c);
7074 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7075 if (real_identical (&c, &cint))
7077 /* Attempt to evaluate pow at compile-time. */
7078 if (TREE_CODE (arg0) == REAL_CST
7079 && ! TREE_CONSTANT_OVERFLOW (arg0))
7081 REAL_VALUE_TYPE x;
7082 bool inexact;
7084 x = TREE_REAL_CST (arg0);
7085 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7086 if (flag_unsafe_math_optimizations || !inexact)
7087 return build_real (type, x);
7090 /* Strip sign ops from even integer powers. */
7091 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7093 tree narg0 = fold_strip_sign_ops (arg0);
7094 if (narg0)
7096 arglist = build_tree_list (NULL_TREE, arg1);
7097 arglist = tree_cons (NULL_TREE, narg0, arglist);
7098 return build_function_call_expr (fndecl, arglist);
7104 /* Optimize pow(expN(x),y) = expN(x*y). */
7105 fcode = builtin_mathfn_code (arg0);
7106 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7108 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7109 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7110 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
7111 arglist = build_tree_list (NULL_TREE, arg);
7112 return build_function_call_expr (expfn, arglist);
7115 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7116 if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
7118 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7119 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7120 build_real (type, dconsthalf)));
7122 arglist = tree_cons (NULL_TREE, narg0,
7123 build_tree_list (NULL_TREE, narg1));
7124 return build_function_call_expr (fndecl, arglist);
7127 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7128 if (flag_unsafe_math_optimizations
7129 && (fcode == BUILT_IN_POW
7130 || fcode == BUILT_IN_POWF
7131 || fcode == BUILT_IN_POWL))
7133 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7134 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7135 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7136 arglist = tree_cons (NULL_TREE, arg00,
7137 build_tree_list (NULL_TREE, narg1));
7138 return build_function_call_expr (fndecl, arglist);
7140 return NULL_TREE;
7143 /* Fold a builtin function call to powi, powif, or powil. Return
7144 NULL_TREE if no simplification can be made. */
7145 static tree
7146 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7148 tree arg0 = TREE_VALUE (arglist);
7149 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7151 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7152 return NULL_TREE;
7154 /* Optimize pow(1.0,y) = 1.0. */
7155 if (real_onep (arg0))
7156 return omit_one_operand (type, build_real (type, dconst1), arg1);
7158 if (host_integerp (arg1, 0))
7160 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7162 /* Evaluate powi at compile-time. */
7163 if (TREE_CODE (arg0) == REAL_CST
7164 && ! TREE_CONSTANT_OVERFLOW (arg0))
7166 REAL_VALUE_TYPE x;
7167 x = TREE_REAL_CST (arg0);
7168 real_powi (&x, TYPE_MODE (type), &x, c);
7169 return build_real (type, x);
7172 /* Optimize pow(x,0) = 1.0. */
7173 if (c == 0)
7174 return omit_one_operand (type, build_real (type, dconst1),
7175 arg0);
7177 /* Optimize pow(x,1) = x. */
7178 if (c == 1)
7179 return arg0;
7181 /* Optimize pow(x,-1) = 1.0/x. */
7182 if (c == -1)
7183 return fold (build2 (RDIV_EXPR, type,
7184 build_real (type, dconst1), arg0));
7187 return NULL_TREE;
7190 /* A subroutine of fold_builtin to fold the various exponent
7191 functions. EXP is the CALL_EXPR of a call to a builtin function.
7192 VALUE is the value which will be raised to a power. */
7194 static tree
7195 fold_builtin_exponent (tree exp, const REAL_VALUE_TYPE *value)
7197 tree arglist = TREE_OPERAND (exp, 1);
7199 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7201 tree fndecl = get_callee_fndecl (exp);
7202 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7203 tree arg = TREE_VALUE (arglist);
7205 /* Optimize exp*(0.0) = 1.0. */
7206 if (real_zerop (arg))
7207 return build_real (type, dconst1);
7209 /* Optimize expN(1.0) = N. */
7210 if (real_onep (arg))
7212 REAL_VALUE_TYPE cst;
7214 real_convert (&cst, TYPE_MODE (type), value);
7215 return build_real (type, cst);
7218 /* Attempt to evaluate expN(integer) at compile-time. */
7219 if (flag_unsafe_math_optimizations
7220 && TREE_CODE (arg) == REAL_CST
7221 && ! TREE_CONSTANT_OVERFLOW (arg))
7223 REAL_VALUE_TYPE cint;
7224 REAL_VALUE_TYPE c;
7225 HOST_WIDE_INT n;
7227 c = TREE_REAL_CST (arg);
7228 n = real_to_integer (&c);
7229 real_from_integer (&cint, VOIDmode, n,
7230 n < 0 ? -1 : 0, 0);
7231 if (real_identical (&c, &cint))
7233 REAL_VALUE_TYPE x;
7235 real_powi (&x, TYPE_MODE (type), value, n);
7236 return build_real (type, x);
7240 /* Optimize expN(logN(x)) = x. */
7241 if (flag_unsafe_math_optimizations)
7243 const enum built_in_function fcode = builtin_mathfn_code (arg);
7245 if ((value == &dconste
7246 && (fcode == BUILT_IN_LOG
7247 || fcode == BUILT_IN_LOGF
7248 || fcode == BUILT_IN_LOGL))
7249 || (value == &dconst2
7250 && (fcode == BUILT_IN_LOG2
7251 || fcode == BUILT_IN_LOG2F
7252 || fcode == BUILT_IN_LOG2L))
7253 || (value == &dconst10
7254 && (fcode == BUILT_IN_LOG10
7255 || fcode == BUILT_IN_LOG10F
7256 || fcode == BUILT_IN_LOG10L)))
7257 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7261 return 0;
7264 /* Fold function call to builtin memcpy. Return
7265 NULL_TREE if no simplification can be made. */
7267 static tree
7268 fold_builtin_memcpy (tree exp)
7270 tree arglist = TREE_OPERAND (exp, 1);
7271 tree dest, src, len;
7273 if (!validate_arglist (arglist,
7274 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7275 return 0;
7277 dest = TREE_VALUE (arglist);
7278 src = TREE_VALUE (TREE_CHAIN (arglist));
7279 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7281 /* If the LEN parameter is zero, return DEST. */
7282 if (integer_zerop (len))
7283 return omit_one_operand (TREE_TYPE (exp), dest, src);
7285 /* If SRC and DEST are the same (and not volatile), return DEST. */
7286 if (operand_equal_p (src, dest, 0))
7287 return omit_one_operand (TREE_TYPE (exp), dest, len);
7289 return 0;
7292 /* Fold function call to builtin mempcpy. Return
7293 NULL_TREE if no simplification can be made. */
7295 static tree
7296 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7298 if (validate_arglist (arglist,
7299 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7301 tree dest = TREE_VALUE (arglist);
7302 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7303 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7305 /* If the LEN parameter is zero, return DEST. */
7306 if (integer_zerop (len))
7307 return omit_one_operand (type, dest, src);
7309 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7310 if (operand_equal_p (src, dest, 0))
7312 if (endp == 0)
7313 return omit_one_operand (type, dest, len);
7315 if (endp == 2)
7316 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7317 ssize_int (1)));
7319 len = fold_convert (TREE_TYPE (dest), len);
7320 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7321 return fold_convert (type, len);
7324 return 0;
7327 /* Fold function call to builtin memmove. Return
7328 NULL_TREE if no simplification can be made. */
7330 static tree
7331 fold_builtin_memmove (tree arglist, tree type)
7333 tree dest, src, len;
7335 if (!validate_arglist (arglist,
7336 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7337 return 0;
7339 dest = TREE_VALUE (arglist);
7340 src = TREE_VALUE (TREE_CHAIN (arglist));
7341 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7343 /* If the LEN parameter is zero, return DEST. */
7344 if (integer_zerop (len))
7345 return omit_one_operand (type, dest, src);
7347 /* If SRC and DEST are the same (and not volatile), return DEST. */
7348 if (operand_equal_p (src, dest, 0))
7349 return omit_one_operand (type, dest, len);
7351 return 0;
7354 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7355 the length of the string to be copied. Return NULL_TREE if no
7356 simplification can be made. */
7358 tree
7359 fold_builtin_strcpy (tree exp, tree len)
7361 tree arglist = TREE_OPERAND (exp, 1);
7362 tree dest, src, fn;
7364 if (!validate_arglist (arglist,
7365 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7366 return 0;
7368 dest = TREE_VALUE (arglist);
7369 src = TREE_VALUE (TREE_CHAIN (arglist));
7371 /* If SRC and DEST are the same (and not volatile), return DEST. */
7372 if (operand_equal_p (src, dest, 0))
7373 return fold_convert (TREE_TYPE (exp), dest);
7375 if (optimize_size)
7376 return 0;
7378 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7379 if (!fn)
7380 return 0;
7382 if (!len)
7384 len = c_strlen (src, 1);
7385 if (! len || TREE_SIDE_EFFECTS (len))
7386 return 0;
7389 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7390 arglist = build_tree_list (NULL_TREE, len);
7391 arglist = tree_cons (NULL_TREE, src, arglist);
7392 arglist = tree_cons (NULL_TREE, dest, arglist);
7393 return fold_convert (TREE_TYPE (exp),
7394 build_function_call_expr (fn, arglist));
7397 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7398 the length of the source string. Return NULL_TREE if no simplification
7399 can be made. */
7401 tree
7402 fold_builtin_strncpy (tree exp, tree slen)
7404 tree arglist = TREE_OPERAND (exp, 1);
7405 tree dest, src, len, fn;
7407 if (!validate_arglist (arglist,
7408 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7409 return 0;
7411 dest = TREE_VALUE (arglist);
7412 src = TREE_VALUE (TREE_CHAIN (arglist));
7413 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7415 /* If the LEN parameter is zero, return DEST. */
7416 if (integer_zerop (len))
7417 return omit_one_operand (TREE_TYPE (exp), dest, src);
7419 /* We can't compare slen with len as constants below if len is not a
7420 constant. */
7421 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7422 return 0;
7424 if (!slen)
7425 slen = c_strlen (src, 1);
7427 /* Now, we must be passed a constant src ptr parameter. */
7428 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7429 return 0;
7431 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7433 /* We do not support simplification of this case, though we do
7434 support it when expanding trees into RTL. */
7435 /* FIXME: generate a call to __builtin_memset. */
7436 if (tree_int_cst_lt (slen, len))
7437 return 0;
7439 /* OK transform into builtin memcpy. */
7440 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7441 if (!fn)
7442 return 0;
7443 return fold_convert (TREE_TYPE (exp),
7444 build_function_call_expr (fn, arglist));
7447 /* Fold function call to builtin memcmp. Return
7448 NULL_TREE if no simplification can be made. */
7450 static tree
7451 fold_builtin_memcmp (tree arglist)
7453 tree arg1, arg2, len;
7454 const char *p1, *p2;
7456 if (!validate_arglist (arglist,
7457 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7458 return 0;
7460 arg1 = TREE_VALUE (arglist);
7461 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7462 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7464 /* If the LEN parameter is zero, return zero. */
7465 if (integer_zerop (len))
7466 return omit_two_operands (integer_type_node, integer_zero_node,
7467 arg1, arg2);
7469 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7470 if (operand_equal_p (arg1, arg2, 0))
7471 return omit_one_operand (integer_type_node, integer_zero_node, len);
7473 p1 = c_getstr (arg1);
7474 p2 = c_getstr (arg2);
7476 /* If all arguments are constant, and the value of len is not greater
7477 than the lengths of arg1 and arg2, evaluate at compile-time. */
7478 if (host_integerp (len, 1) && p1 && p2
7479 && compare_tree_int (len, strlen (p1) + 1) <= 0
7480 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7482 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7484 if (r > 0)
7485 return integer_one_node;
7486 else if (r < 0)
7487 return integer_minus_one_node;
7488 else
7489 return integer_zero_node;
7492 /* If len parameter is one, return an expression corresponding to
7493 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7494 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7496 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7497 tree cst_uchar_ptr_node
7498 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7500 tree ind1 = fold_convert (integer_type_node,
7501 build1 (INDIRECT_REF, cst_uchar_node,
7502 fold_convert (cst_uchar_ptr_node,
7503 arg1)));
7504 tree ind2 = fold_convert (integer_type_node,
7505 build1 (INDIRECT_REF, cst_uchar_node,
7506 fold_convert (cst_uchar_ptr_node,
7507 arg2)));
7508 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7511 return 0;
7514 /* Fold function call to builtin strcmp. Return
7515 NULL_TREE if no simplification can be made. */
7517 static tree
7518 fold_builtin_strcmp (tree arglist)
7520 tree arg1, arg2;
7521 const char *p1, *p2;
7523 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7524 return 0;
7526 arg1 = TREE_VALUE (arglist);
7527 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7529 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7530 if (operand_equal_p (arg1, arg2, 0))
7531 return integer_zero_node;
7533 p1 = c_getstr (arg1);
7534 p2 = c_getstr (arg2);
7536 if (p1 && p2)
7538 const int i = strcmp (p1, p2);
7539 if (i < 0)
7540 return integer_minus_one_node;
7541 else if (i > 0)
7542 return integer_one_node;
7543 else
7544 return integer_zero_node;
7547 /* If the second arg is "", return *(const unsigned char*)arg1. */
7548 if (p2 && *p2 == '\0')
7550 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7551 tree cst_uchar_ptr_node
7552 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7554 return fold_convert (integer_type_node,
7555 build1 (INDIRECT_REF, cst_uchar_node,
7556 fold_convert (cst_uchar_ptr_node,
7557 arg1)));
7560 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7561 if (p1 && *p1 == '\0')
7563 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7564 tree cst_uchar_ptr_node
7565 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7567 tree temp = fold_convert (integer_type_node,
7568 build1 (INDIRECT_REF, cst_uchar_node,
7569 fold_convert (cst_uchar_ptr_node,
7570 arg2)));
7571 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7574 return 0;
7577 /* Fold function call to builtin strncmp. Return
7578 NULL_TREE if no simplification can be made. */
7580 static tree
7581 fold_builtin_strncmp (tree arglist)
7583 tree arg1, arg2, len;
7584 const char *p1, *p2;
7586 if (!validate_arglist (arglist,
7587 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7588 return 0;
7590 arg1 = TREE_VALUE (arglist);
7591 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7592 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7594 /* If the LEN parameter is zero, return zero. */
7595 if (integer_zerop (len))
7596 return omit_two_operands (integer_type_node, integer_zero_node,
7597 arg1, arg2);
7599 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7600 if (operand_equal_p (arg1, arg2, 0))
7601 return omit_one_operand (integer_type_node, integer_zero_node, len);
7603 p1 = c_getstr (arg1);
7604 p2 = c_getstr (arg2);
7606 if (host_integerp (len, 1) && p1 && p2)
7608 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7609 if (i > 0)
7610 return integer_one_node;
7611 else if (i < 0)
7612 return integer_minus_one_node;
7613 else
7614 return integer_zero_node;
7617 /* If the second arg is "", and the length is greater than zero,
7618 return *(const unsigned char*)arg1. */
7619 if (p2 && *p2 == '\0'
7620 && TREE_CODE (len) == INTEGER_CST
7621 && tree_int_cst_sgn (len) == 1)
7623 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7624 tree cst_uchar_ptr_node
7625 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7627 return fold_convert (integer_type_node,
7628 build1 (INDIRECT_REF, cst_uchar_node,
7629 fold_convert (cst_uchar_ptr_node,
7630 arg1)));
7633 /* If the first arg is "", and the length is greater than zero,
7634 return -*(const unsigned char*)arg2. */
7635 if (p1 && *p1 == '\0'
7636 && TREE_CODE (len) == INTEGER_CST
7637 && tree_int_cst_sgn (len) == 1)
7639 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7640 tree cst_uchar_ptr_node
7641 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7643 tree temp = fold_convert (integer_type_node,
7644 build1 (INDIRECT_REF, cst_uchar_node,
7645 fold_convert (cst_uchar_ptr_node,
7646 arg2)));
7647 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7650 /* If len parameter is one, return an expression corresponding to
7651 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7652 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7654 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7655 tree cst_uchar_ptr_node
7656 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
7658 tree ind1 = fold_convert (integer_type_node,
7659 build1 (INDIRECT_REF, cst_uchar_node,
7660 fold_convert (cst_uchar_ptr_node,
7661 arg1)));
7662 tree ind2 = fold_convert (integer_type_node,
7663 build1 (INDIRECT_REF, cst_uchar_node,
7664 fold_convert (cst_uchar_ptr_node,
7665 arg2)));
7666 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7669 return 0;
7672 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7673 NULL_TREE if no simplification can be made. */
7675 static tree
7676 fold_builtin_signbit (tree exp)
7678 tree arglist = TREE_OPERAND (exp, 1);
7679 tree arg, temp;
7681 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7682 return NULL_TREE;
7684 arg = TREE_VALUE (arglist);
7686 /* If ARG is a compile-time constant, determine the result. */
7687 if (TREE_CODE (arg) == REAL_CST
7688 && !TREE_CONSTANT_OVERFLOW (arg))
7690 REAL_VALUE_TYPE c;
7692 c = TREE_REAL_CST (arg);
7693 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7694 return fold_convert (TREE_TYPE (exp), temp);
7697 /* If ARG is non-negative, the result is always zero. */
7698 if (tree_expr_nonnegative_p (arg))
7699 return omit_one_operand (TREE_TYPE (exp), integer_zero_node, arg);
7701 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7702 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7703 return fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
7704 build_real (TREE_TYPE (arg), dconst0)));
7706 return NULL_TREE;
7709 /* Fold function call to builtin copysign, copysignf or copysignl.
7710 Return NULL_TREE if no simplification can be made. */
7712 static tree
7713 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7715 tree arg1, arg2, tem;
7717 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7718 return NULL_TREE;
7720 arg1 = TREE_VALUE (arglist);
7721 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7723 /* copysign(X,X) is X. */
7724 if (operand_equal_p (arg1, arg2, 0))
7725 return fold_convert (type, arg1);
7727 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7728 if (TREE_CODE (arg1) == REAL_CST
7729 && TREE_CODE (arg2) == REAL_CST
7730 && !TREE_CONSTANT_OVERFLOW (arg1)
7731 && !TREE_CONSTANT_OVERFLOW (arg2))
7733 REAL_VALUE_TYPE c1, c2;
7735 c1 = TREE_REAL_CST (arg1);
7736 c2 = TREE_REAL_CST (arg2);
7737 real_copysign (&c1, &c2);
7738 return build_real (type, c1);
7739 c1.sign = c2.sign;
7742 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7743 Remember to evaluate Y for side-effects. */
7744 if (tree_expr_nonnegative_p (arg2))
7745 return omit_one_operand (type,
7746 fold (build1 (ABS_EXPR, type, arg1)),
7747 arg2);
7749 /* Strip sign changing operations for the first argument. */
7750 tem = fold_strip_sign_ops (arg1);
7751 if (tem)
7753 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7754 return build_function_call_expr (fndecl, arglist);
7757 return NULL_TREE;
7760 /* Fold a call to builtin isascii. */
7762 static tree
7763 fold_builtin_isascii (tree arglist)
7765 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7766 return 0;
7767 else
7769 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7770 tree arg = TREE_VALUE (arglist);
7772 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7773 build_int_cst (NULL_TREE,
7774 ~ (unsigned HOST_WIDE_INT) 0x7f));
7775 arg = fold (build2 (EQ_EXPR, integer_type_node,
7776 arg, integer_zero_node));
7778 if (in_gimple_form && !TREE_CONSTANT (arg))
7779 return NULL_TREE;
7780 else
7781 return arg;
7785 /* Fold a call to builtin toascii. */
7787 static tree
7788 fold_builtin_toascii (tree arglist)
7790 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7791 return 0;
7792 else
7794 /* Transform toascii(c) -> (c & 0x7f). */
7795 tree arg = TREE_VALUE (arglist);
7797 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7798 build_int_cst (NULL_TREE, 0x7f)));
7802 /* Fold a call to builtin isdigit. */
7804 static tree
7805 fold_builtin_isdigit (tree arglist)
7807 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7808 return 0;
7809 else
7811 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7812 /* According to the C standard, isdigit is unaffected by locale.
7813 However, it definitely is affected by the target character set. */
7814 tree arg;
7815 unsigned HOST_WIDE_INT target_digit0
7816 = lang_hooks.to_target_charset ('0');
7818 if (target_digit0 == 0)
7819 return NULL_TREE;
7821 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
7822 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7823 build_int_cst (unsigned_type_node, target_digit0));
7824 arg = build2 (LE_EXPR, integer_type_node, arg,
7825 build_int_cst (unsigned_type_node, 9));
7826 arg = fold (arg);
7827 if (in_gimple_form && !TREE_CONSTANT (arg))
7828 return NULL_TREE;
7829 else
7830 return arg;
7834 /* Fold a call to fabs, fabsf or fabsl. */
7836 static tree
7837 fold_builtin_fabs (tree arglist, tree type)
7839 tree arg;
7841 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7842 return 0;
7844 arg = TREE_VALUE (arglist);
7845 arg = fold_convert (type, arg);
7846 if (TREE_CODE (arg) == REAL_CST)
7847 return fold_abs_const (arg, type);
7848 return fold (build1 (ABS_EXPR, type, arg));
7851 /* Fold a call to abs, labs, llabs or imaxabs. */
7853 static tree
7854 fold_builtin_abs (tree arglist, tree type)
7856 tree arg;
7858 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7859 return 0;
7861 arg = TREE_VALUE (arglist);
7862 arg = fold_convert (type, arg);
7863 if (TREE_CODE (arg) == INTEGER_CST)
7864 return fold_abs_const (arg, type);
7865 return fold (build1 (ABS_EXPR, type, arg));
7868 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7869 EXP is the CALL_EXPR for the call. */
7871 static tree
7872 fold_builtin_classify (tree exp, int builtin_index)
7874 tree fndecl = get_callee_fndecl (exp);
7875 tree arglist = TREE_OPERAND (exp, 1);
7876 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7877 tree arg;
7878 REAL_VALUE_TYPE r;
7880 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7882 /* Check that we have exactly one argument. */
7883 if (arglist == 0)
7885 error ("too few arguments to function %qs",
7886 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7887 return error_mark_node;
7889 else if (TREE_CHAIN (arglist) != 0)
7891 error ("too many arguments to function %qs",
7892 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7893 return error_mark_node;
7895 else
7897 error ("non-floating-point argument to function %qs",
7898 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7899 return error_mark_node;
7903 arg = TREE_VALUE (arglist);
7904 switch (builtin_index)
7906 case BUILT_IN_ISINF:
7907 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7908 return omit_one_operand (type, integer_zero_node, arg);
7910 if (TREE_CODE (arg) == REAL_CST)
7912 r = TREE_REAL_CST (arg);
7913 if (real_isinf (&r))
7914 return real_compare (GT_EXPR, &r, &dconst0)
7915 ? integer_one_node : integer_minus_one_node;
7916 else
7917 return integer_zero_node;
7920 return NULL_TREE;
7922 case BUILT_IN_FINITE:
7923 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7924 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7925 return omit_one_operand (type, integer_zero_node, arg);
7927 if (TREE_CODE (arg) == REAL_CST)
7929 r = TREE_REAL_CST (arg);
7930 return real_isinf (&r) || real_isnan (&r)
7931 ? integer_zero_node : integer_one_node;
7934 return NULL_TREE;
7936 case BUILT_IN_ISNAN:
7937 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7938 return omit_one_operand (type, integer_zero_node, arg);
7940 if (TREE_CODE (arg) == REAL_CST)
7942 r = TREE_REAL_CST (arg);
7943 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7946 arg = builtin_save_expr (arg);
7947 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7949 default:
7950 gcc_unreachable ();
7954 /* Fold a call to an unordered comparison function such as
7955 __builtin_isgreater(). EXP is the CALL_EXPR for the call.
7956 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7957 the opposite of the desired result. UNORDERED_CODE is used
7958 for modes that can hold NaNs and ORDERED_CODE is used for
7959 the rest. */
7961 static tree
7962 fold_builtin_unordered_cmp (tree exp,
7963 enum tree_code unordered_code,
7964 enum tree_code ordered_code)
7966 tree fndecl = get_callee_fndecl (exp);
7967 tree arglist = TREE_OPERAND (exp, 1);
7968 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7969 enum tree_code code;
7970 tree arg0, arg1;
7971 tree type0, type1;
7972 enum tree_code code0, code1;
7973 tree cmp_type = NULL_TREE;
7975 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7977 /* Check that we have exactly two arguments. */
7978 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7980 error ("too few arguments to function %qs",
7981 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7982 return error_mark_node;
7984 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7986 error ("too many arguments to function %qs",
7987 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7988 return error_mark_node;
7992 arg0 = TREE_VALUE (arglist);
7993 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7995 type0 = TREE_TYPE (arg0);
7996 type1 = TREE_TYPE (arg1);
7998 code0 = TREE_CODE (type0);
7999 code1 = TREE_CODE (type1);
8001 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8002 /* Choose the wider of two real types. */
8003 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8004 ? type0 : type1;
8005 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8006 cmp_type = type0;
8007 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8008 cmp_type = type1;
8009 else
8011 error ("non-floating-point argument to function %qs",
8012 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8013 return error_mark_node;
8016 arg0 = fold_convert (cmp_type, arg0);
8017 arg1 = fold_convert (cmp_type, arg1);
8019 if (unordered_code == UNORDERED_EXPR)
8021 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8022 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8023 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
8026 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8027 : ordered_code;
8028 return fold (build1 (TRUTH_NOT_EXPR, type,
8029 fold (build2 (code, type, arg0, arg1))));
8032 /* Fold a call to one of the external complex multiply libcalls. */
8034 static tree
8035 fold_builtin_complex_mul (tree type, tree arglist)
8037 tree ar, ai, br, bi;
8039 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
8040 REAL_TYPE, VOID_TYPE))
8041 return NULL;
8043 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8044 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8045 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8046 bi = TREE_VALUE (arglist);
8048 return fold_complex_mult_parts (type, ar, ai, br, bi);
8051 /* Fold a call to one of the external complex division libcalls. */
8053 static tree
8054 fold_builtin_complex_div (tree type, tree arglist)
8056 tree ar, ai, br, bi;
8058 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
8059 REAL_TYPE, VOID_TYPE))
8060 return NULL;
8062 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8063 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8064 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8065 bi = TREE_VALUE (arglist);
8067 return fold_complex_div_parts (type, ar, ai, br, bi, RDIV_EXPR);
8070 /* Used by constant folding to simplify calls to builtin functions. EXP is
8071 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8072 result of the function call is ignored. This function returns NULL_TREE
8073 if no simplification was possible. */
8075 static tree
8076 fold_builtin_1 (tree exp, bool ignore)
8078 tree fndecl = get_callee_fndecl (exp);
8079 tree arglist = TREE_OPERAND (exp, 1);
8080 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8081 enum built_in_function fcode;
8083 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8084 return targetm.fold_builtin (exp, ignore);
8086 fcode = DECL_FUNCTION_CODE (fndecl);
8087 switch (fcode)
8089 case BUILT_IN_FPUTS:
8090 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8092 case BUILT_IN_FPUTS_UNLOCKED:
8093 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8095 case BUILT_IN_STRSTR:
8096 return fold_builtin_strstr (arglist, type);
8098 case BUILT_IN_STRCAT:
8099 return fold_builtin_strcat (arglist);
8101 case BUILT_IN_STRNCAT:
8102 return fold_builtin_strncat (arglist);
8104 case BUILT_IN_STRSPN:
8105 return fold_builtin_strspn (arglist);
8107 case BUILT_IN_STRCSPN:
8108 return fold_builtin_strcspn (arglist);
8110 case BUILT_IN_STRCHR:
8111 case BUILT_IN_INDEX:
8112 return fold_builtin_strchr (arglist, type);
8114 case BUILT_IN_STRRCHR:
8115 case BUILT_IN_RINDEX:
8116 return fold_builtin_strrchr (arglist, type);
8118 case BUILT_IN_STRCPY:
8119 return fold_builtin_strcpy (exp, NULL_TREE);
8121 case BUILT_IN_STRNCPY:
8122 return fold_builtin_strncpy (exp, NULL_TREE);
8124 case BUILT_IN_STRCMP:
8125 return fold_builtin_strcmp (arglist);
8127 case BUILT_IN_STRNCMP:
8128 return fold_builtin_strncmp (arglist);
8130 case BUILT_IN_STRPBRK:
8131 return fold_builtin_strpbrk (arglist, type);
8133 case BUILT_IN_BCMP:
8134 case BUILT_IN_MEMCMP:
8135 return fold_builtin_memcmp (arglist);
8137 case BUILT_IN_SPRINTF:
8138 return fold_builtin_sprintf (arglist, ignore);
8140 case BUILT_IN_CONSTANT_P:
8142 tree val;
8144 val = fold_builtin_constant_p (arglist);
8145 /* Gimplification will pull the CALL_EXPR for the builtin out of
8146 an if condition. When not optimizing, we'll not CSE it back.
8147 To avoid link error types of regressions, return false now. */
8148 if (!val && !optimize)
8149 val = integer_zero_node;
8151 return val;
8154 case BUILT_IN_EXPECT:
8155 return fold_builtin_expect (arglist);
8157 case BUILT_IN_CLASSIFY_TYPE:
8158 return fold_builtin_classify_type (arglist);
8160 case BUILT_IN_STRLEN:
8161 return fold_builtin_strlen (arglist);
8163 case BUILT_IN_FABS:
8164 case BUILT_IN_FABSF:
8165 case BUILT_IN_FABSL:
8166 return fold_builtin_fabs (arglist, type);
8168 case BUILT_IN_ABS:
8169 case BUILT_IN_LABS:
8170 case BUILT_IN_LLABS:
8171 case BUILT_IN_IMAXABS:
8172 return fold_builtin_abs (arglist, type);
8174 case BUILT_IN_CONJ:
8175 case BUILT_IN_CONJF:
8176 case BUILT_IN_CONJL:
8177 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8178 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
8179 break;
8181 case BUILT_IN_CREAL:
8182 case BUILT_IN_CREALF:
8183 case BUILT_IN_CREALL:
8184 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8185 return non_lvalue (fold (build1 (REALPART_EXPR, type,
8186 TREE_VALUE (arglist))));
8187 break;
8189 case BUILT_IN_CIMAG:
8190 case BUILT_IN_CIMAGF:
8191 case BUILT_IN_CIMAGL:
8192 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8193 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8194 TREE_VALUE (arglist))));
8195 break;
8197 case BUILT_IN_CABS:
8198 case BUILT_IN_CABSF:
8199 case BUILT_IN_CABSL:
8200 return fold_builtin_cabs (arglist, type);
8202 case BUILT_IN_SQRT:
8203 case BUILT_IN_SQRTF:
8204 case BUILT_IN_SQRTL:
8205 return fold_builtin_sqrt (arglist, type);
8207 case BUILT_IN_CBRT:
8208 case BUILT_IN_CBRTF:
8209 case BUILT_IN_CBRTL:
8210 return fold_builtin_cbrt (arglist, type);
8212 case BUILT_IN_SIN:
8213 case BUILT_IN_SINF:
8214 case BUILT_IN_SINL:
8215 return fold_builtin_sin (arglist);
8217 case BUILT_IN_COS:
8218 case BUILT_IN_COSF:
8219 case BUILT_IN_COSL:
8220 return fold_builtin_cos (arglist, type, fndecl);
8222 case BUILT_IN_EXP:
8223 case BUILT_IN_EXPF:
8224 case BUILT_IN_EXPL:
8225 return fold_builtin_exponent (exp, &dconste);
8227 case BUILT_IN_EXP2:
8228 case BUILT_IN_EXP2F:
8229 case BUILT_IN_EXP2L:
8230 return fold_builtin_exponent (exp, &dconst2);
8232 case BUILT_IN_EXP10:
8233 case BUILT_IN_EXP10F:
8234 case BUILT_IN_EXP10L:
8235 case BUILT_IN_POW10:
8236 case BUILT_IN_POW10F:
8237 case BUILT_IN_POW10L:
8238 return fold_builtin_exponent (exp, &dconst10);
8240 case BUILT_IN_LOG:
8241 case BUILT_IN_LOGF:
8242 case BUILT_IN_LOGL:
8243 return fold_builtin_logarithm (exp, &dconste);
8245 case BUILT_IN_LOG2:
8246 case BUILT_IN_LOG2F:
8247 case BUILT_IN_LOG2L:
8248 return fold_builtin_logarithm (exp, &dconst2);
8250 case BUILT_IN_LOG10:
8251 case BUILT_IN_LOG10F:
8252 case BUILT_IN_LOG10L:
8253 return fold_builtin_logarithm (exp, &dconst10);
8255 case BUILT_IN_TAN:
8256 case BUILT_IN_TANF:
8257 case BUILT_IN_TANL:
8258 return fold_builtin_tan (arglist);
8260 case BUILT_IN_ATAN:
8261 case BUILT_IN_ATANF:
8262 case BUILT_IN_ATANL:
8263 return fold_builtin_atan (arglist, type);
8265 case BUILT_IN_POW:
8266 case BUILT_IN_POWF:
8267 case BUILT_IN_POWL:
8268 return fold_builtin_pow (fndecl, arglist, type);
8270 case BUILT_IN_POWI:
8271 case BUILT_IN_POWIF:
8272 case BUILT_IN_POWIL:
8273 return fold_builtin_powi (fndecl, arglist, type);
8275 case BUILT_IN_INF:
8276 case BUILT_IN_INFF:
8277 case BUILT_IN_INFL:
8278 return fold_builtin_inf (type, true);
8280 case BUILT_IN_HUGE_VAL:
8281 case BUILT_IN_HUGE_VALF:
8282 case BUILT_IN_HUGE_VALL:
8283 return fold_builtin_inf (type, false);
8285 case BUILT_IN_NAN:
8286 case BUILT_IN_NANF:
8287 case BUILT_IN_NANL:
8288 return fold_builtin_nan (arglist, type, true);
8290 case BUILT_IN_NANS:
8291 case BUILT_IN_NANSF:
8292 case BUILT_IN_NANSL:
8293 return fold_builtin_nan (arglist, type, false);
8295 case BUILT_IN_FLOOR:
8296 case BUILT_IN_FLOORF:
8297 case BUILT_IN_FLOORL:
8298 return fold_builtin_floor (exp);
8300 case BUILT_IN_CEIL:
8301 case BUILT_IN_CEILF:
8302 case BUILT_IN_CEILL:
8303 return fold_builtin_ceil (exp);
8305 case BUILT_IN_TRUNC:
8306 case BUILT_IN_TRUNCF:
8307 case BUILT_IN_TRUNCL:
8308 return fold_builtin_trunc (exp);
8310 case BUILT_IN_ROUND:
8311 case BUILT_IN_ROUNDF:
8312 case BUILT_IN_ROUNDL:
8313 return fold_builtin_round (exp);
8315 case BUILT_IN_NEARBYINT:
8316 case BUILT_IN_NEARBYINTF:
8317 case BUILT_IN_NEARBYINTL:
8318 case BUILT_IN_RINT:
8319 case BUILT_IN_RINTF:
8320 case BUILT_IN_RINTL:
8321 return fold_trunc_transparent_mathfn (exp);
8323 case BUILT_IN_LROUND:
8324 case BUILT_IN_LROUNDF:
8325 case BUILT_IN_LROUNDL:
8326 case BUILT_IN_LLROUND:
8327 case BUILT_IN_LLROUNDF:
8328 case BUILT_IN_LLROUNDL:
8329 return fold_builtin_lround (exp);
8331 case BUILT_IN_LRINT:
8332 case BUILT_IN_LRINTF:
8333 case BUILT_IN_LRINTL:
8334 case BUILT_IN_LLRINT:
8335 case BUILT_IN_LLRINTF:
8336 case BUILT_IN_LLRINTL:
8337 return fold_fixed_mathfn (exp);
8339 case BUILT_IN_FFS:
8340 case BUILT_IN_FFSL:
8341 case BUILT_IN_FFSLL:
8342 case BUILT_IN_CLZ:
8343 case BUILT_IN_CLZL:
8344 case BUILT_IN_CLZLL:
8345 case BUILT_IN_CTZ:
8346 case BUILT_IN_CTZL:
8347 case BUILT_IN_CTZLL:
8348 case BUILT_IN_POPCOUNT:
8349 case BUILT_IN_POPCOUNTL:
8350 case BUILT_IN_POPCOUNTLL:
8351 case BUILT_IN_PARITY:
8352 case BUILT_IN_PARITYL:
8353 case BUILT_IN_PARITYLL:
8354 return fold_builtin_bitop (exp);
8356 case BUILT_IN_MEMCPY:
8357 return fold_builtin_memcpy (exp);
8359 case BUILT_IN_MEMPCPY:
8360 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8362 case BUILT_IN_MEMMOVE:
8363 return fold_builtin_memmove (arglist, type);
8365 case BUILT_IN_SIGNBIT:
8366 case BUILT_IN_SIGNBITF:
8367 case BUILT_IN_SIGNBITL:
8368 return fold_builtin_signbit (exp);
8370 case BUILT_IN_ISASCII:
8371 return fold_builtin_isascii (arglist);
8373 case BUILT_IN_TOASCII:
8374 return fold_builtin_toascii (arglist);
8376 case BUILT_IN_ISDIGIT:
8377 return fold_builtin_isdigit (arglist);
8379 case BUILT_IN_COPYSIGN:
8380 case BUILT_IN_COPYSIGNF:
8381 case BUILT_IN_COPYSIGNL:
8382 return fold_builtin_copysign (fndecl, arglist, type);
8384 case BUILT_IN_FINITE:
8385 case BUILT_IN_FINITEF:
8386 case BUILT_IN_FINITEL:
8387 return fold_builtin_classify (exp, BUILT_IN_FINITE);
8389 case BUILT_IN_ISINF:
8390 case BUILT_IN_ISINFF:
8391 case BUILT_IN_ISINFL:
8392 return fold_builtin_classify (exp, BUILT_IN_ISINF);
8394 case BUILT_IN_ISNAN:
8395 case BUILT_IN_ISNANF:
8396 case BUILT_IN_ISNANL:
8397 return fold_builtin_classify (exp, BUILT_IN_ISNAN);
8399 case BUILT_IN_ISGREATER:
8400 return fold_builtin_unordered_cmp (exp, UNLE_EXPR, LE_EXPR);
8401 case BUILT_IN_ISGREATEREQUAL:
8402 return fold_builtin_unordered_cmp (exp, UNLT_EXPR, LT_EXPR);
8403 case BUILT_IN_ISLESS:
8404 return fold_builtin_unordered_cmp (exp, UNGE_EXPR, GE_EXPR);
8405 case BUILT_IN_ISLESSEQUAL:
8406 return fold_builtin_unordered_cmp (exp, UNGT_EXPR, GT_EXPR);
8407 case BUILT_IN_ISLESSGREATER:
8408 return fold_builtin_unordered_cmp (exp, UNEQ_EXPR, EQ_EXPR);
8409 case BUILT_IN_ISUNORDERED:
8410 return fold_builtin_unordered_cmp (exp, UNORDERED_EXPR, NOP_EXPR);
8412 /* We do the folding for va_start in the expander. */
8413 case BUILT_IN_VA_START:
8414 break;
8416 default:
8417 if (fcode >= BUILT_IN_COMPLEX_MUL_MIN
8418 && fcode <= BUILT_IN_COMPLEX_MUL_MAX)
8419 return fold_builtin_complex_mul (type, arglist);
8420 if (fcode >= BUILT_IN_COMPLEX_DIV_MIN
8421 && fcode <= BUILT_IN_COMPLEX_DIV_MAX)
8422 return fold_builtin_complex_div (type, arglist);
8423 break;
8426 return 0;
8429 /* A wrapper function for builtin folding that prevents warnings for
8430 "statement without effect" and the like, caused by removing the
8431 call node earlier than the warning is generated. */
8433 tree
8434 fold_builtin (tree exp, bool ignore)
8436 exp = fold_builtin_1 (exp, ignore);
8437 if (exp)
8439 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8440 TREE_NO_WARNING (exp) = 1;
8443 return exp;
8446 /* Conveniently construct a function call expression. */
8448 tree
8449 build_function_call_expr (tree fn, tree arglist)
8451 tree call_expr;
8453 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8454 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8455 call_expr, arglist, NULL_TREE);
8456 return fold (call_expr);
8459 /* This function validates the types of a function call argument list
8460 represented as a tree chain of parameters against a specified list
8461 of tree_codes. If the last specifier is a 0, that represents an
8462 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8464 static int
8465 validate_arglist (tree arglist, ...)
8467 enum tree_code code;
8468 int res = 0;
8469 va_list ap;
8471 va_start (ap, arglist);
8475 code = va_arg (ap, enum tree_code);
8476 switch (code)
8478 case 0:
8479 /* This signifies an ellipses, any further arguments are all ok. */
8480 res = 1;
8481 goto end;
8482 case VOID_TYPE:
8483 /* This signifies an endlink, if no arguments remain, return
8484 true, otherwise return false. */
8485 res = arglist == 0;
8486 goto end;
8487 default:
8488 /* If no parameters remain or the parameter's code does not
8489 match the specified code, return false. Otherwise continue
8490 checking any remaining arguments. */
8491 if (arglist == 0)
8492 goto end;
8493 if (code == POINTER_TYPE)
8495 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8496 goto end;
8498 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8499 goto end;
8500 break;
8502 arglist = TREE_CHAIN (arglist);
8504 while (1);
8506 /* We need gotos here since we can only have one VA_CLOSE in a
8507 function. */
8508 end: ;
8509 va_end (ap);
8511 return res;
8514 /* Default target-specific builtin expander that does nothing. */
8517 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8518 rtx target ATTRIBUTE_UNUSED,
8519 rtx subtarget ATTRIBUTE_UNUSED,
8520 enum machine_mode mode ATTRIBUTE_UNUSED,
8521 int ignore ATTRIBUTE_UNUSED)
8523 return NULL_RTX;
8526 /* Returns true is EXP represents data that would potentially reside
8527 in a readonly section. */
8529 static bool
8530 readonly_data_expr (tree exp)
8532 STRIP_NOPS (exp);
8534 if (TREE_CODE (exp) != ADDR_EXPR)
8535 return false;
8537 exp = get_base_address (TREE_OPERAND (exp, 0));
8538 if (!exp)
8539 return false;
8541 /* Make sure we call decl_readonly_section only for trees it
8542 can handle (since it returns true for everything it doesn't
8543 understand). */
8544 if (TREE_CODE (exp) == STRING_CST
8545 || TREE_CODE (exp) == CONSTRUCTOR
8546 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8547 return decl_readonly_section (exp, 0);
8548 else
8549 return false;
8552 /* Simplify a call to the strstr builtin.
8554 Return 0 if no simplification was possible, otherwise return the
8555 simplified form of the call as a tree.
8557 The simplified form may be a constant or other expression which
8558 computes the same value, but in a more efficient manner (including
8559 calls to other builtin functions).
8561 The call may contain arguments which need to be evaluated, but
8562 which are not useful to determine the result of the call. In
8563 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8564 COMPOUND_EXPR will be an argument which must be evaluated.
8565 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8566 COMPOUND_EXPR in the chain will contain the tree for the simplified
8567 form of the builtin function call. */
8569 static tree
8570 fold_builtin_strstr (tree arglist, tree type)
8572 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8573 return 0;
8574 else
8576 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8577 tree fn;
8578 const char *p1, *p2;
8580 p2 = c_getstr (s2);
8581 if (p2 == NULL)
8582 return 0;
8584 p1 = c_getstr (s1);
8585 if (p1 != NULL)
8587 const char *r = strstr (p1, p2);
8588 tree tem;
8590 if (r == NULL)
8591 return build_int_cst (TREE_TYPE (s1), 0);
8593 /* Return an offset into the constant string argument. */
8594 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8595 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8596 return fold_convert (type, tem);
8599 /* The argument is const char *, and the result is char *, so we need
8600 a type conversion here to avoid a warning. */
8601 if (p2[0] == '\0')
8602 return fold_convert (type, s1);
8604 if (p2[1] != '\0')
8605 return 0;
8607 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8608 if (!fn)
8609 return 0;
8611 /* New argument list transforming strstr(s1, s2) to
8612 strchr(s1, s2[0]). */
8613 arglist = build_tree_list (NULL_TREE,
8614 build_int_cst (NULL_TREE, p2[0]));
8615 arglist = tree_cons (NULL_TREE, s1, arglist);
8616 return build_function_call_expr (fn, arglist);
8620 /* Simplify a call to the strchr builtin.
8622 Return 0 if no simplification was possible, otherwise return the
8623 simplified form of the call as a tree.
8625 The simplified form may be a constant or other expression which
8626 computes the same value, but in a more efficient manner (including
8627 calls to other builtin functions).
8629 The call may contain arguments which need to be evaluated, but
8630 which are not useful to determine the result of the call. In
8631 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8632 COMPOUND_EXPR will be an argument which must be evaluated.
8633 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8634 COMPOUND_EXPR in the chain will contain the tree for the simplified
8635 form of the builtin function call. */
8637 static tree
8638 fold_builtin_strchr (tree arglist, tree type)
8640 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8641 return 0;
8642 else
8644 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8645 const char *p1;
8647 if (TREE_CODE (s2) != INTEGER_CST)
8648 return 0;
8650 p1 = c_getstr (s1);
8651 if (p1 != NULL)
8653 char c;
8654 const char *r;
8655 tree tem;
8657 if (target_char_cast (s2, &c))
8658 return 0;
8660 r = strchr (p1, c);
8662 if (r == NULL)
8663 return build_int_cst (TREE_TYPE (s1), 0);
8665 /* Return an offset into the constant string argument. */
8666 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8667 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8668 return fold_convert (type, tem);
8670 return 0;
8674 /* Simplify a call to the strrchr builtin.
8676 Return 0 if no simplification was possible, otherwise return the
8677 simplified form of the call as a tree.
8679 The simplified form may be a constant or other expression which
8680 computes the same value, but in a more efficient manner (including
8681 calls to other builtin functions).
8683 The call may contain arguments which need to be evaluated, but
8684 which are not useful to determine the result of the call. In
8685 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8686 COMPOUND_EXPR will be an argument which must be evaluated.
8687 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8688 COMPOUND_EXPR in the chain will contain the tree for the simplified
8689 form of the builtin function call. */
8691 static tree
8692 fold_builtin_strrchr (tree arglist, tree type)
8694 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8695 return 0;
8696 else
8698 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8699 tree fn;
8700 const char *p1;
8702 if (TREE_CODE (s2) != INTEGER_CST)
8703 return 0;
8705 p1 = c_getstr (s1);
8706 if (p1 != NULL)
8708 char c;
8709 const char *r;
8710 tree tem;
8712 if (target_char_cast (s2, &c))
8713 return 0;
8715 r = strrchr (p1, c);
8717 if (r == NULL)
8718 return build_int_cst (TREE_TYPE (s1), 0);
8720 /* Return an offset into the constant string argument. */
8721 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8722 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8723 return fold_convert (type, tem);
8726 if (! integer_zerop (s2))
8727 return 0;
8729 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8730 if (!fn)
8731 return 0;
8733 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8734 return build_function_call_expr (fn, arglist);
8738 /* Simplify a call to the strpbrk builtin.
8740 Return 0 if no simplification was possible, otherwise return the
8741 simplified form of the call as a tree.
8743 The simplified form may be a constant or other expression which
8744 computes the same value, but in a more efficient manner (including
8745 calls to other builtin functions).
8747 The call may contain arguments which need to be evaluated, but
8748 which are not useful to determine the result of the call. In
8749 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8750 COMPOUND_EXPR will be an argument which must be evaluated.
8751 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8752 COMPOUND_EXPR in the chain will contain the tree for the simplified
8753 form of the builtin function call. */
8755 static tree
8756 fold_builtin_strpbrk (tree arglist, tree type)
8758 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8759 return 0;
8760 else
8762 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8763 tree fn;
8764 const char *p1, *p2;
8766 p2 = c_getstr (s2);
8767 if (p2 == NULL)
8768 return 0;
8770 p1 = c_getstr (s1);
8771 if (p1 != NULL)
8773 const char *r = strpbrk (p1, p2);
8774 tree tem;
8776 if (r == NULL)
8777 return build_int_cst (TREE_TYPE (s1), 0);
8779 /* Return an offset into the constant string argument. */
8780 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8781 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8782 return fold_convert (type, tem);
8785 if (p2[0] == '\0')
8786 /* strpbrk(x, "") == NULL.
8787 Evaluate and ignore s1 in case it had side-effects. */
8788 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8790 if (p2[1] != '\0')
8791 return 0; /* Really call strpbrk. */
8793 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8794 if (!fn)
8795 return 0;
8797 /* New argument list transforming strpbrk(s1, s2) to
8798 strchr(s1, s2[0]). */
8799 arglist = build_tree_list (NULL_TREE,
8800 build_int_cst (NULL_TREE, p2[0]));
8801 arglist = tree_cons (NULL_TREE, s1, arglist);
8802 return build_function_call_expr (fn, arglist);
8806 /* Simplify a call to the strcat builtin.
8808 Return 0 if no simplification was possible, otherwise return the
8809 simplified form of the call as a tree.
8811 The simplified form may be a constant or other expression which
8812 computes the same value, but in a more efficient manner (including
8813 calls to other builtin functions).
8815 The call may contain arguments which need to be evaluated, but
8816 which are not useful to determine the result of the call. In
8817 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8818 COMPOUND_EXPR will be an argument which must be evaluated.
8819 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8820 COMPOUND_EXPR in the chain will contain the tree for the simplified
8821 form of the builtin function call. */
8823 static tree
8824 fold_builtin_strcat (tree arglist)
8826 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8827 return 0;
8828 else
8830 tree dst = TREE_VALUE (arglist),
8831 src = TREE_VALUE (TREE_CHAIN (arglist));
8832 const char *p = c_getstr (src);
8834 /* If the string length is zero, return the dst parameter. */
8835 if (p && *p == '\0')
8836 return dst;
8838 return 0;
8842 /* Simplify a call to the strncat builtin.
8844 Return 0 if no simplification was possible, otherwise return the
8845 simplified form of the call as a tree.
8847 The simplified form may be a constant or other expression which
8848 computes the same value, but in a more efficient manner (including
8849 calls to other builtin functions).
8851 The call may contain arguments which need to be evaluated, but
8852 which are not useful to determine the result of the call. In
8853 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8854 COMPOUND_EXPR will be an argument which must be evaluated.
8855 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8856 COMPOUND_EXPR in the chain will contain the tree for the simplified
8857 form of the builtin function call. */
8859 static tree
8860 fold_builtin_strncat (tree arglist)
8862 if (!validate_arglist (arglist,
8863 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8864 return 0;
8865 else
8867 tree dst = TREE_VALUE (arglist);
8868 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8869 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8870 const char *p = c_getstr (src);
8872 /* If the requested length is zero, or the src parameter string
8873 length is zero, return the dst parameter. */
8874 if (integer_zerop (len) || (p && *p == '\0'))
8875 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8877 /* If the requested len is greater than or equal to the string
8878 length, call strcat. */
8879 if (TREE_CODE (len) == INTEGER_CST && p
8880 && compare_tree_int (len, strlen (p)) >= 0)
8882 tree newarglist
8883 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8884 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8886 /* If the replacement _DECL isn't initialized, don't do the
8887 transformation. */
8888 if (!fn)
8889 return 0;
8891 return build_function_call_expr (fn, newarglist);
8893 return 0;
8897 /* Simplify a call to the strspn builtin.
8899 Return 0 if no simplification was possible, otherwise return the
8900 simplified form of the call as a tree.
8902 The simplified form may be a constant or other expression which
8903 computes the same value, but in a more efficient manner (including
8904 calls to other builtin functions).
8906 The call may contain arguments which need to be evaluated, but
8907 which are not useful to determine the result of the call. In
8908 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8909 COMPOUND_EXPR will be an argument which must be evaluated.
8910 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8911 COMPOUND_EXPR in the chain will contain the tree for the simplified
8912 form of the builtin function call. */
8914 static tree
8915 fold_builtin_strspn (tree arglist)
8917 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8918 return 0;
8919 else
8921 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8922 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8924 /* If both arguments are constants, evaluate at compile-time. */
8925 if (p1 && p2)
8927 const size_t r = strspn (p1, p2);
8928 return size_int (r);
8931 /* If either argument is "", return 0. */
8932 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8933 /* Evaluate and ignore both arguments in case either one has
8934 side-effects. */
8935 return omit_two_operands (integer_type_node, integer_zero_node,
8936 s1, s2);
8937 return 0;
8941 /* Simplify a call to the strcspn builtin.
8943 Return 0 if no simplification was possible, otherwise return the
8944 simplified form of the call as a tree.
8946 The simplified form may be a constant or other expression which
8947 computes the same value, but in a more efficient manner (including
8948 calls to other builtin functions).
8950 The call may contain arguments which need to be evaluated, but
8951 which are not useful to determine the result of the call. In
8952 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8953 COMPOUND_EXPR will be an argument which must be evaluated.
8954 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8955 COMPOUND_EXPR in the chain will contain the tree for the simplified
8956 form of the builtin function call. */
8958 static tree
8959 fold_builtin_strcspn (tree arglist)
8961 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8962 return 0;
8963 else
8965 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8966 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8968 /* If both arguments are constants, evaluate at compile-time. */
8969 if (p1 && p2)
8971 const size_t r = strcspn (p1, p2);
8972 return size_int (r);
8975 /* If the first argument is "", return 0. */
8976 if (p1 && *p1 == '\0')
8978 /* Evaluate and ignore argument s2 in case it has
8979 side-effects. */
8980 return omit_one_operand (integer_type_node,
8981 integer_zero_node, s2);
8984 /* If the second argument is "", return __builtin_strlen(s1). */
8985 if (p2 && *p2 == '\0')
8987 tree newarglist = build_tree_list (NULL_TREE, s1),
8988 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8990 /* If the replacement _DECL isn't initialized, don't do the
8991 transformation. */
8992 if (!fn)
8993 return 0;
8995 return build_function_call_expr (fn, newarglist);
8997 return 0;
9001 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
9002 by the builtin will be ignored. UNLOCKED is true is true if this
9003 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9004 the known length of the string. Return NULL_TREE if no simplification
9005 was possible. */
9007 tree
9008 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
9010 tree fn;
9011 /* If we're using an unlocked function, assume the other unlocked
9012 functions exist explicitly. */
9013 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9014 : implicit_built_in_decls[BUILT_IN_FPUTC];
9015 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9016 : implicit_built_in_decls[BUILT_IN_FWRITE];
9018 /* If the return value is used, don't do the transformation. */
9019 if (!ignore)
9020 return 0;
9022 /* Verify the arguments in the original call. */
9023 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9024 return 0;
9026 if (! len)
9027 len = c_strlen (TREE_VALUE (arglist), 0);
9029 /* Get the length of the string passed to fputs. If the length
9030 can't be determined, punt. */
9031 if (!len
9032 || TREE_CODE (len) != INTEGER_CST)
9033 return 0;
9035 switch (compare_tree_int (len, 1))
9037 case -1: /* length is 0, delete the call entirely . */
9038 return omit_one_operand (integer_type_node, integer_zero_node,
9039 TREE_VALUE (TREE_CHAIN (arglist)));
9041 case 0: /* length is 1, call fputc. */
9043 const char *p = c_getstr (TREE_VALUE (arglist));
9045 if (p != NULL)
9047 /* New argument list transforming fputs(string, stream) to
9048 fputc(string[0], stream). */
9049 arglist = build_tree_list (NULL_TREE,
9050 TREE_VALUE (TREE_CHAIN (arglist)));
9051 arglist = tree_cons (NULL_TREE,
9052 build_int_cst (NULL_TREE, p[0]),
9053 arglist);
9054 fn = fn_fputc;
9055 break;
9058 /* FALLTHROUGH */
9059 case 1: /* length is greater than 1, call fwrite. */
9061 tree string_arg;
9063 /* If optimizing for size keep fputs. */
9064 if (optimize_size)
9065 return 0;
9066 string_arg = TREE_VALUE (arglist);
9067 /* New argument list transforming fputs(string, stream) to
9068 fwrite(string, 1, len, stream). */
9069 arglist = build_tree_list (NULL_TREE,
9070 TREE_VALUE (TREE_CHAIN (arglist)));
9071 arglist = tree_cons (NULL_TREE, len, arglist);
9072 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9073 arglist = tree_cons (NULL_TREE, string_arg, arglist);
9074 fn = fn_fwrite;
9075 break;
9077 default:
9078 gcc_unreachable ();
9081 /* If the replacement _DECL isn't initialized, don't do the
9082 transformation. */
9083 if (!fn)
9084 return 0;
9086 /* These optimizations are only performed when the result is ignored,
9087 hence there's no need to cast the result to integer_type_node. */
9088 return build_function_call_expr (fn, arglist);
9091 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9092 produced. False otherwise. This is done so that we don't output the error
9093 or warning twice or three times. */
9094 bool
9095 fold_builtin_next_arg (tree arglist)
9097 tree fntype = TREE_TYPE (current_function_decl);
9099 if (TYPE_ARG_TYPES (fntype) == 0
9100 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9101 == void_type_node))
9103 error ("%<va_start%> used in function with fixed args");
9104 return true;
9106 else if (!arglist)
9108 /* Evidently an out of date version of <stdarg.h>; can't validate
9109 va_start's second argument, but can still work as intended. */
9110 warning ("%<__builtin_next_arg%> called without an argument");
9111 return true;
9113 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9114 when we checked the arguments and if needed issued a warning. */
9115 else if (!TREE_CHAIN (arglist)
9116 || !integer_zerop (TREE_VALUE (arglist))
9117 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
9118 || TREE_CHAIN (TREE_CHAIN (arglist)))
9120 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9121 tree arg = TREE_VALUE (arglist);
9123 if (TREE_CHAIN (arglist))
9125 error ("%<va_start%> used with too many arguments");
9126 return true;
9129 /* Strip off all nops for the sake of the comparison. This
9130 is not quite the same as STRIP_NOPS. It does more.
9131 We must also strip off INDIRECT_EXPR for C++ reference
9132 parameters. */
9133 while (TREE_CODE (arg) == NOP_EXPR
9134 || TREE_CODE (arg) == CONVERT_EXPR
9135 || TREE_CODE (arg) == NON_LVALUE_EXPR
9136 || TREE_CODE (arg) == INDIRECT_REF)
9137 arg = TREE_OPERAND (arg, 0);
9138 if (arg != last_parm)
9140 /* FIXME: Sometimes with the tree optimizers we can get the
9141 not the last argument even though the user used the last
9142 argument. We just warn and set the arg to be the last
9143 argument so that we will get wrong-code because of
9144 it. */
9145 warning ("second parameter of %<va_start%> not last named argument");
9147 /* We want to verify the second parameter just once before the tree
9148 optimizers are run and then avoid keeping it in the tree,
9149 as otherwise we could warn even for correct code like:
9150 void foo (int i, ...)
9151 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9152 TREE_VALUE (arglist) = integer_zero_node;
9153 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9155 return false;
9159 /* Simplify a call to the sprintf builtin.
9161 Return 0 if no simplification was possible, otherwise return the
9162 simplified form of the call as a tree. If IGNORED is true, it means that
9163 the caller does not use the returned value of the function. */
9165 static tree
9166 fold_builtin_sprintf (tree arglist, int ignored)
9168 tree call, retval, dest, fmt;
9169 const char *fmt_str = NULL;
9171 /* Verify the required arguments in the original call. We deal with two
9172 types of sprintf() calls: 'sprintf (str, fmt)' and
9173 'sprintf (dest, "%s", orig)'. */
9174 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9175 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9176 VOID_TYPE))
9177 return NULL_TREE;
9179 /* Get the destination string and the format specifier. */
9180 dest = TREE_VALUE (arglist);
9181 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9183 /* Check whether the format is a literal string constant. */
9184 fmt_str = c_getstr (fmt);
9185 if (fmt_str == NULL)
9186 return NULL_TREE;
9188 call = NULL_TREE;
9189 retval = NULL_TREE;
9191 if (!init_target_chars())
9192 return 0;
9194 /* If the format doesn't contain % args or %%, use strcpy. */
9195 if (strchr (fmt_str, target_percent) == NULL)
9197 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9199 if (!fn)
9200 return NULL_TREE;
9202 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9203 'format' is known to contain no % formats. */
9204 arglist = build_tree_list (NULL_TREE, fmt);
9205 arglist = tree_cons (NULL_TREE, dest, arglist);
9206 call = build_function_call_expr (fn, arglist);
9207 if (!ignored)
9208 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9211 /* If the format is "%s", use strcpy if the result isn't used. */
9212 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
9214 tree fn, orig;
9215 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9217 if (!fn)
9218 return NULL_TREE;
9220 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9221 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9222 arglist = build_tree_list (NULL_TREE, orig);
9223 arglist = tree_cons (NULL_TREE, dest, arglist);
9224 if (!ignored)
9226 retval = c_strlen (orig, 1);
9227 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9228 return NULL_TREE;
9230 call = build_function_call_expr (fn, arglist);
9233 if (call && retval)
9235 retval = convert
9236 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9237 retval);
9238 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
9240 else
9241 return call;
9244 /* Initialize format string characters in the target charset. */
9246 static bool
9247 init_target_chars (void)
9249 static bool init;
9250 if (!init)
9252 target_newline = lang_hooks.to_target_charset ('\n');
9253 target_percent = lang_hooks.to_target_charset ('%');
9254 target_c = lang_hooks.to_target_charset ('c');
9255 target_s = lang_hooks.to_target_charset ('s');
9256 if (target_newline == 0 || target_percent == 0 || target_c == 0
9257 || target_s == 0)
9258 return false;
9260 target_percent_c[0] = target_percent;
9261 target_percent_c[1] = target_c;
9262 target_percent_c[2] = '\0';
9264 target_percent_s[0] = target_percent;
9265 target_percent_s[1] = target_s;
9266 target_percent_s[2] = '\0';
9268 target_percent_s_newline[0] = target_percent;
9269 target_percent_s_newline[1] = target_s;
9270 target_percent_s_newline[2] = target_newline;
9271 target_percent_s_newline[3] = '\0';
9273 init = true;
9275 return true;