* config/i386/i386.md (*fp_jcc_7_387): Use 'const0_operand' instead
[official-gcc.git] / gcc / builtins.c
blob034934cd153c768168f1adf5d8e9c62cad50997b
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);
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_int_roundingfn (tree, rtx, rtx);
102 static rtx expand_builtin_args_info (tree);
103 static rtx expand_builtin_next_arg (void);
104 static rtx expand_builtin_va_start (tree);
105 static rtx expand_builtin_va_end (tree);
106 static rtx expand_builtin_va_copy (tree);
107 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
110 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
111 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
117 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
118 static rtx expand_builtin_bcopy (tree, tree);
119 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
120 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
121 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
123 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
125 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
132 static rtx expand_builtin_alloca (tree, rtx);
133 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
134 static rtx expand_builtin_frame_address (tree, tree);
135 static rtx expand_builtin_fputs (tree, rtx, bool);
136 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
138 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
139 static tree stabilize_va_list (tree, int);
140 static rtx expand_builtin_expect (tree, rtx);
141 static tree fold_builtin_constant_p (tree);
142 static tree fold_builtin_classify_type (tree);
143 static tree fold_builtin_strlen (tree);
144 static tree fold_builtin_inf (tree, int);
145 static tree fold_builtin_nan (tree, tree, int);
146 static int validate_arglist (tree, ...);
147 static bool integer_valued_real_p (tree);
148 static tree fold_trunc_transparent_mathfn (tree, tree);
149 static bool readonly_data_expr (tree);
150 static rtx expand_builtin_fabs (tree, rtx, rtx);
151 static rtx expand_builtin_signbit (tree, rtx);
152 static tree fold_builtin_cabs (tree, tree);
153 static tree fold_builtin_sqrt (tree, tree);
154 static tree fold_builtin_cbrt (tree, tree);
155 static tree fold_builtin_pow (tree, tree, tree);
156 static tree fold_builtin_powi (tree, tree, tree);
157 static tree fold_builtin_sin (tree);
158 static tree fold_builtin_cos (tree, tree, tree);
159 static tree fold_builtin_tan (tree);
160 static tree fold_builtin_atan (tree, tree);
161 static tree fold_builtin_trunc (tree, tree);
162 static tree fold_builtin_floor (tree, tree);
163 static tree fold_builtin_ceil (tree, tree);
164 static tree fold_builtin_round (tree, tree);
165 static tree fold_builtin_int_roundingfn (tree, tree);
166 static tree fold_builtin_bitop (tree, tree);
167 static tree fold_builtin_memcpy (tree, tree);
168 static tree fold_builtin_mempcpy (tree, tree, int);
169 static tree fold_builtin_memmove (tree, tree);
170 static tree fold_builtin_strchr (tree, tree);
171 static tree fold_builtin_memcmp (tree);
172 static tree fold_builtin_strcmp (tree);
173 static tree fold_builtin_strncmp (tree);
174 static tree fold_builtin_signbit (tree, tree);
175 static tree fold_builtin_copysign (tree, tree, tree);
176 static tree fold_builtin_isascii (tree);
177 static tree fold_builtin_toascii (tree);
178 static tree fold_builtin_isdigit (tree);
179 static tree fold_builtin_fabs (tree, tree);
180 static tree fold_builtin_abs (tree, tree);
181 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
182 enum tree_code);
183 static tree fold_builtin_1 (tree, tree, bool);
185 static tree fold_builtin_strpbrk (tree, tree);
186 static tree fold_builtin_strstr (tree, tree);
187 static tree fold_builtin_strrchr (tree, tree);
188 static tree fold_builtin_strcat (tree);
189 static tree fold_builtin_strncat (tree);
190 static tree fold_builtin_strspn (tree);
191 static tree fold_builtin_strcspn (tree);
192 static tree fold_builtin_sprintf (tree, int);
195 /* Return the alignment in bits of EXP, a pointer valued expression.
196 But don't return more than MAX_ALIGN no matter what.
197 The alignment returned is, by default, the alignment of the thing that
198 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
200 Otherwise, look at the expression to see if we can do better, i.e., if the
201 expression is actually pointing at an object whose alignment is tighter. */
203 static int
204 get_pointer_alignment (tree exp, unsigned int max_align)
206 unsigned int align, inner;
208 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
209 return 0;
211 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
212 align = MIN (align, max_align);
214 while (1)
216 switch (TREE_CODE (exp))
218 case NOP_EXPR:
219 case CONVERT_EXPR:
220 case NON_LVALUE_EXPR:
221 exp = TREE_OPERAND (exp, 0);
222 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
223 return align;
225 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
226 align = MIN (inner, max_align);
227 break;
229 case PLUS_EXPR:
230 /* If sum of pointer + int, restrict our maximum alignment to that
231 imposed by the integer. If not, we can't do any better than
232 ALIGN. */
233 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
234 return align;
236 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
237 & (max_align / BITS_PER_UNIT - 1))
238 != 0)
239 max_align >>= 1;
241 exp = TREE_OPERAND (exp, 0);
242 break;
244 case ADDR_EXPR:
245 /* See what we are pointing at and look at its alignment. */
246 exp = TREE_OPERAND (exp, 0);
247 if (TREE_CODE (exp) == FUNCTION_DECL)
248 align = FUNCTION_BOUNDARY;
249 else if (DECL_P (exp))
250 align = DECL_ALIGN (exp);
251 #ifdef CONSTANT_ALIGNMENT
252 else if (CONSTANT_CLASS_P (exp))
253 align = CONSTANT_ALIGNMENT (exp, align);
254 #endif
255 return MIN (align, max_align);
257 default:
258 return align;
263 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
264 way, because it could contain a zero byte in the middle.
265 TREE_STRING_LENGTH is the size of the character array, not the string.
267 ONLY_VALUE should be nonzero if the result is not going to be emitted
268 into the instruction stream and zero if it is going to be expanded.
269 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
270 is returned, otherwise NULL, since
271 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
272 evaluate the side-effects.
274 The value returned is of type `ssizetype'.
276 Unfortunately, string_constant can't access the values of const char
277 arrays with initializers, so neither can we do so here. */
279 tree
280 c_strlen (tree src, int only_value)
282 tree offset_node;
283 HOST_WIDE_INT offset;
284 int max;
285 const char *ptr;
287 STRIP_NOPS (src);
288 if (TREE_CODE (src) == COND_EXPR
289 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
291 tree len1, len2;
293 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
294 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
295 if (tree_int_cst_equal (len1, len2))
296 return len1;
299 if (TREE_CODE (src) == COMPOUND_EXPR
300 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
301 return c_strlen (TREE_OPERAND (src, 1), only_value);
303 src = string_constant (src, &offset_node);
304 if (src == 0)
305 return 0;
307 max = TREE_STRING_LENGTH (src) - 1;
308 ptr = TREE_STRING_POINTER (src);
310 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
312 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
313 compute the offset to the following null if we don't know where to
314 start searching for it. */
315 int i;
317 for (i = 0; i < max; i++)
318 if (ptr[i] == 0)
319 return 0;
321 /* We don't know the starting offset, but we do know that the string
322 has no internal zero bytes. We can assume that the offset falls
323 within the bounds of the string; otherwise, the programmer deserves
324 what he gets. Subtract the offset from the length of the string,
325 and return that. This would perhaps not be valid if we were dealing
326 with named arrays in addition to literal string constants. */
328 return size_diffop (size_int (max), offset_node);
331 /* We have a known offset into the string. Start searching there for
332 a null character if we can represent it as a single HOST_WIDE_INT. */
333 if (offset_node == 0)
334 offset = 0;
335 else if (! host_integerp (offset_node, 0))
336 offset = -1;
337 else
338 offset = tree_low_cst (offset_node, 0);
340 /* If the offset is known to be out of bounds, warn, and call strlen at
341 runtime. */
342 if (offset < 0 || offset > max)
344 warning ("offset outside bounds of constant string");
345 return 0;
348 /* Use strlen to search for the first zero byte. Since any strings
349 constructed with build_string will have nulls appended, we win even
350 if we get handed something like (char[4])"abcd".
352 Since OFFSET is our starting index into the string, no further
353 calculation is needed. */
354 return ssize_int (strlen (ptr + offset));
357 /* Return a char pointer for a C string if it is a string constant
358 or sum of string constant and integer constant. */
360 static const char *
361 c_getstr (tree src)
363 tree offset_node;
365 src = string_constant (src, &offset_node);
366 if (src == 0)
367 return 0;
369 if (offset_node == 0)
370 return TREE_STRING_POINTER (src);
371 else if (!host_integerp (offset_node, 1)
372 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
373 return 0;
375 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
378 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
379 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
381 static rtx
382 c_readstr (const char *str, enum machine_mode mode)
384 HOST_WIDE_INT c[2];
385 HOST_WIDE_INT ch;
386 unsigned int i, j;
388 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
390 c[0] = 0;
391 c[1] = 0;
392 ch = 1;
393 for (i = 0; i < GET_MODE_SIZE (mode); i++)
395 j = i;
396 if (WORDS_BIG_ENDIAN)
397 j = GET_MODE_SIZE (mode) - i - 1;
398 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
399 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
400 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
401 j *= BITS_PER_UNIT;
402 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
404 if (ch)
405 ch = (unsigned char) str[i];
406 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
408 return immed_double_const (c[0], c[1], mode);
411 /* Cast a target constant CST to target CHAR and if that value fits into
412 host char type, return zero and put that value into variable pointed by
413 P. */
415 static int
416 target_char_cast (tree cst, char *p)
418 unsigned HOST_WIDE_INT val, hostval;
420 if (!host_integerp (cst, 1)
421 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
422 return 1;
424 val = tree_low_cst (cst, 1);
425 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
426 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
428 hostval = val;
429 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
430 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
432 if (val != hostval)
433 return 1;
435 *p = hostval;
436 return 0;
439 /* Similar to save_expr, but assumes that arbitrary code is not executed
440 in between the multiple evaluations. In particular, we assume that a
441 non-addressable local variable will not be modified. */
443 static tree
444 builtin_save_expr (tree exp)
446 if (TREE_ADDRESSABLE (exp) == 0
447 && (TREE_CODE (exp) == PARM_DECL
448 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
449 return exp;
451 return save_expr (exp);
454 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
455 times to get the address of either a higher stack frame, or a return
456 address located within it (depending on FNDECL_CODE). */
458 static rtx
459 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
461 int i;
463 #ifdef INITIAL_FRAME_ADDRESS_RTX
464 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
465 #else
466 rtx tem = hard_frame_pointer_rtx;
467 #endif
469 /* Some machines need special handling before we can access
470 arbitrary frames. For example, on the sparc, we must first flush
471 all register windows to the stack. */
472 #ifdef SETUP_FRAME_ADDRESSES
473 if (count > 0)
474 SETUP_FRAME_ADDRESSES ();
475 #endif
477 /* On the sparc, the return address is not in the frame, it is in a
478 register. There is no way to access it off of the current frame
479 pointer, but it can be accessed off the previous frame pointer by
480 reading the value from the register window save area. */
481 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
482 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
483 count--;
484 #endif
486 /* Scan back COUNT frames to the specified frame. */
487 for (i = 0; i < count; i++)
489 /* Assume the dynamic chain pointer is in the word that the
490 frame address points to, unless otherwise specified. */
491 #ifdef DYNAMIC_CHAIN_ADDRESS
492 tem = DYNAMIC_CHAIN_ADDRESS (tem);
493 #endif
494 tem = memory_address (Pmode, tem);
495 tem = gen_rtx_MEM (Pmode, tem);
496 set_mem_alias_set (tem, get_frame_alias_set ());
497 tem = copy_to_reg (tem);
500 /* For __builtin_frame_address, return what we've got. */
501 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
502 return tem;
504 /* For __builtin_return_address, Get the return address from that
505 frame. */
506 #ifdef RETURN_ADDR_RTX
507 tem = RETURN_ADDR_RTX (count, tem);
508 #else
509 tem = memory_address (Pmode,
510 plus_constant (tem, GET_MODE_SIZE (Pmode)));
511 tem = gen_rtx_MEM (Pmode, tem);
512 set_mem_alias_set (tem, get_frame_alias_set ());
513 #endif
514 return tem;
517 /* Alias set used for setjmp buffer. */
518 static HOST_WIDE_INT setjmp_alias_set = -1;
520 /* Construct the leading half of a __builtin_setjmp call. Control will
521 return to RECEIVER_LABEL. This is used directly by sjlj exception
522 handling code. */
524 void
525 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
527 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
528 rtx stack_save;
529 rtx mem;
531 if (setjmp_alias_set == -1)
532 setjmp_alias_set = new_alias_set ();
534 buf_addr = convert_memory_address (Pmode, buf_addr);
536 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
538 /* We store the frame pointer and the address of receiver_label in
539 the buffer and use the rest of it for the stack save area, which
540 is machine-dependent. */
542 mem = gen_rtx_MEM (Pmode, buf_addr);
543 set_mem_alias_set (mem, setjmp_alias_set);
544 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
546 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
547 set_mem_alias_set (mem, setjmp_alias_set);
549 emit_move_insn (validize_mem (mem),
550 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
552 stack_save = gen_rtx_MEM (sa_mode,
553 plus_constant (buf_addr,
554 2 * GET_MODE_SIZE (Pmode)));
555 set_mem_alias_set (stack_save, setjmp_alias_set);
556 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
558 /* If there is further processing to do, do it. */
559 #ifdef HAVE_builtin_setjmp_setup
560 if (HAVE_builtin_setjmp_setup)
561 emit_insn (gen_builtin_setjmp_setup (buf_addr));
562 #endif
564 /* Tell optimize_save_area_alloca that extra work is going to
565 need to go on during alloca. */
566 current_function_calls_setjmp = 1;
568 /* Set this so all the registers get saved in our frame; we need to be
569 able to copy the saved values for any registers from frames we unwind. */
570 current_function_has_nonlocal_label = 1;
573 /* Construct the trailing part of a __builtin_setjmp call.
574 This is used directly by sjlj exception handling code. */
576 void
577 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
579 /* Clobber the FP when we get here, so we have to make sure it's
580 marked as used by this function. */
581 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
583 /* Mark the static chain as clobbered here so life information
584 doesn't get messed up for it. */
585 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
587 /* Now put in the code to restore the frame pointer, and argument
588 pointer, if needed. */
589 #ifdef HAVE_nonlocal_goto
590 if (! HAVE_nonlocal_goto)
591 #endif
592 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
594 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
595 if (fixed_regs[ARG_POINTER_REGNUM])
597 #ifdef ELIMINABLE_REGS
598 size_t i;
599 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
601 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
602 if (elim_regs[i].from == ARG_POINTER_REGNUM
603 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
604 break;
606 if (i == ARRAY_SIZE (elim_regs))
607 #endif
609 /* Now restore our arg pointer from the address at which it
610 was saved in our stack frame. */
611 emit_move_insn (virtual_incoming_args_rtx,
612 copy_to_reg (get_arg_pointer_save_area (cfun)));
615 #endif
617 #ifdef HAVE_builtin_setjmp_receiver
618 if (HAVE_builtin_setjmp_receiver)
619 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
620 else
621 #endif
622 #ifdef HAVE_nonlocal_goto_receiver
623 if (HAVE_nonlocal_goto_receiver)
624 emit_insn (gen_nonlocal_goto_receiver ());
625 else
626 #endif
627 { /* Nothing */ }
629 /* @@@ This is a kludge. Not all machine descriptions define a blockage
630 insn, but we must not allow the code we just generated to be reordered
631 by scheduling. Specifically, the update of the frame pointer must
632 happen immediately, not later. So emit an ASM_INPUT to act as blockage
633 insn. */
634 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
637 /* __builtin_setjmp is passed a pointer to an array of five words (not
638 all will be used on all machines). It operates similarly to the C
639 library function of the same name, but is more efficient. Much of
640 the code below (and for longjmp) is copied from the handling of
641 non-local gotos.
643 NOTE: This is intended for use by GNAT and the exception handling
644 scheme in the compiler and will only work in the method used by
645 them. */
647 static rtx
648 expand_builtin_setjmp (tree arglist, rtx target)
650 rtx buf_addr, next_lab, cont_lab;
652 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
653 return NULL_RTX;
655 if (target == 0 || !REG_P (target)
656 || REGNO (target) < FIRST_PSEUDO_REGISTER)
657 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
659 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
661 next_lab = gen_label_rtx ();
662 cont_lab = gen_label_rtx ();
664 expand_builtin_setjmp_setup (buf_addr, next_lab);
666 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
667 ensure that pending stack adjustments are flushed. */
668 emit_move_insn (target, const0_rtx);
669 emit_jump (cont_lab);
671 emit_label (next_lab);
673 expand_builtin_setjmp_receiver (next_lab);
675 /* Set TARGET to one. */
676 emit_move_insn (target, const1_rtx);
677 emit_label (cont_lab);
679 /* Tell flow about the strange goings on. Putting `next_lab' on
680 `nonlocal_goto_handler_labels' to indicates that function
681 calls may traverse the arc back to this label. */
683 current_function_has_nonlocal_label = 1;
684 nonlocal_goto_handler_labels
685 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
687 return target;
690 /* __builtin_longjmp is passed a pointer to an array of five words (not
691 all will be used on all machines). It operates similarly to the C
692 library function of the same name, but is more efficient. Much of
693 the code below is copied from the handling of non-local gotos.
695 NOTE: This is intended for use by GNAT and the exception handling
696 scheme in the compiler and will only work in the method used by
697 them. */
699 static void
700 expand_builtin_longjmp (rtx buf_addr, rtx value)
702 rtx fp, lab, stack, insn, last;
703 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
705 if (setjmp_alias_set == -1)
706 setjmp_alias_set = new_alias_set ();
708 buf_addr = convert_memory_address (Pmode, buf_addr);
710 buf_addr = force_reg (Pmode, buf_addr);
712 /* We used to store value in static_chain_rtx, but that fails if pointers
713 are smaller than integers. We instead require that the user must pass
714 a second argument of 1, because that is what builtin_setjmp will
715 return. This also makes EH slightly more efficient, since we are no
716 longer copying around a value that we don't care about. */
717 gcc_assert (value == const1_rtx);
719 last = get_last_insn ();
720 #ifdef HAVE_builtin_longjmp
721 if (HAVE_builtin_longjmp)
722 emit_insn (gen_builtin_longjmp (buf_addr));
723 else
724 #endif
726 fp = gen_rtx_MEM (Pmode, buf_addr);
727 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
728 GET_MODE_SIZE (Pmode)));
730 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
731 2 * GET_MODE_SIZE (Pmode)));
732 set_mem_alias_set (fp, setjmp_alias_set);
733 set_mem_alias_set (lab, setjmp_alias_set);
734 set_mem_alias_set (stack, setjmp_alias_set);
736 /* Pick up FP, label, and SP from the block and jump. This code is
737 from expand_goto in stmt.c; see there for detailed comments. */
738 #if HAVE_nonlocal_goto
739 if (HAVE_nonlocal_goto)
740 /* We have to pass a value to the nonlocal_goto pattern that will
741 get copied into the static_chain pointer, but it does not matter
742 what that value is, because builtin_setjmp does not use it. */
743 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
744 else
745 #endif
747 lab = copy_to_reg (lab);
749 emit_insn (gen_rtx_CLOBBER (VOIDmode,
750 gen_rtx_MEM (BLKmode,
751 gen_rtx_SCRATCH (VOIDmode))));
752 emit_insn (gen_rtx_CLOBBER (VOIDmode,
753 gen_rtx_MEM (BLKmode,
754 hard_frame_pointer_rtx)));
756 emit_move_insn (hard_frame_pointer_rtx, fp);
757 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
759 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
760 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
761 emit_indirect_jump (lab);
765 /* Search backwards and mark the jump insn as a non-local goto.
766 Note that this precludes the use of __builtin_longjmp to a
767 __builtin_setjmp target in the same function. However, we've
768 already cautioned the user that these functions are for
769 internal exception handling use only. */
770 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
772 gcc_assert (insn != last);
774 if (JUMP_P (insn))
776 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
777 REG_NOTES (insn));
778 break;
780 else if (CALL_P (insn))
781 break;
785 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
786 and the address of the save area. */
788 static rtx
789 expand_builtin_nonlocal_goto (tree arglist)
791 tree t_label, t_save_area;
792 rtx r_label, r_save_area, r_fp, r_sp, insn;
794 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
795 return NULL_RTX;
797 t_label = TREE_VALUE (arglist);
798 arglist = TREE_CHAIN (arglist);
799 t_save_area = TREE_VALUE (arglist);
801 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
802 r_label = convert_memory_address (Pmode, r_label);
803 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
804 r_save_area = convert_memory_address (Pmode, r_save_area);
805 r_fp = gen_rtx_MEM (Pmode, r_save_area);
806 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
807 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
809 current_function_has_nonlocal_goto = 1;
811 #if HAVE_nonlocal_goto
812 /* ??? We no longer need to pass the static chain value, afaik. */
813 if (HAVE_nonlocal_goto)
814 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
815 else
816 #endif
818 r_label = copy_to_reg (r_label);
820 emit_insn (gen_rtx_CLOBBER (VOIDmode,
821 gen_rtx_MEM (BLKmode,
822 gen_rtx_SCRATCH (VOIDmode))));
824 emit_insn (gen_rtx_CLOBBER (VOIDmode,
825 gen_rtx_MEM (BLKmode,
826 hard_frame_pointer_rtx)));
828 /* Restore frame pointer for containing function.
829 This sets the actual hard register used for the frame pointer
830 to the location of the function's incoming static chain info.
831 The non-local goto handler will then adjust it to contain the
832 proper value and reload the argument pointer, if needed. */
833 emit_move_insn (hard_frame_pointer_rtx, r_fp);
834 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
836 /* USE of hard_frame_pointer_rtx added for consistency;
837 not clear if really needed. */
838 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
839 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
840 emit_indirect_jump (r_label);
843 /* Search backwards to the jump insn and mark it as a
844 non-local goto. */
845 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
847 if (JUMP_P (insn))
849 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
850 const0_rtx, REG_NOTES (insn));
851 break;
853 else if (CALL_P (insn))
854 break;
857 return const0_rtx;
860 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
861 (not all will be used on all machines) that was passed to __builtin_setjmp.
862 It updates the stack pointer in that block to correspond to the current
863 stack pointer. */
865 static void
866 expand_builtin_update_setjmp_buf (rtx buf_addr)
868 enum machine_mode sa_mode = Pmode;
869 rtx stack_save;
872 #ifdef HAVE_save_stack_nonlocal
873 if (HAVE_save_stack_nonlocal)
874 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
875 #endif
876 #ifdef STACK_SAVEAREA_MODE
877 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
878 #endif
880 stack_save
881 = gen_rtx_MEM (sa_mode,
882 memory_address
883 (sa_mode,
884 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
886 #ifdef HAVE_setjmp
887 if (HAVE_setjmp)
888 emit_insn (gen_setjmp ());
889 #endif
891 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
894 /* Expand a call to __builtin_prefetch. For a target that does not support
895 data prefetch, evaluate the memory address argument in case it has side
896 effects. */
898 static void
899 expand_builtin_prefetch (tree arglist)
901 tree arg0, arg1, arg2;
902 rtx op0, op1, op2;
904 if (!validate_arglist (arglist, POINTER_TYPE, 0))
905 return;
907 arg0 = TREE_VALUE (arglist);
908 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
909 zero (read) and argument 2 (locality) defaults to 3 (high degree of
910 locality). */
911 if (TREE_CHAIN (arglist))
913 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
914 if (TREE_CHAIN (TREE_CHAIN (arglist)))
915 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
916 else
917 arg2 = build_int_cst (NULL_TREE, 3);
919 else
921 arg1 = integer_zero_node;
922 arg2 = build_int_cst (NULL_TREE, 3);
925 /* Argument 0 is an address. */
926 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
928 /* Argument 1 (read/write flag) must be a compile-time constant int. */
929 if (TREE_CODE (arg1) != INTEGER_CST)
931 error ("second argument to %<__builtin_prefetch%> must be a constant");
932 arg1 = integer_zero_node;
934 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
935 /* Argument 1 must be either zero or one. */
936 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
938 warning ("invalid second argument to %<__builtin_prefetch%>;"
939 " using zero");
940 op1 = const0_rtx;
943 /* Argument 2 (locality) must be a compile-time constant int. */
944 if (TREE_CODE (arg2) != INTEGER_CST)
946 error ("third argument to %<__builtin_prefetch%> must be a constant");
947 arg2 = integer_zero_node;
949 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
950 /* Argument 2 must be 0, 1, 2, or 3. */
951 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
953 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
954 op2 = const0_rtx;
957 #ifdef HAVE_prefetch
958 if (HAVE_prefetch)
960 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
961 (op0,
962 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
963 || (GET_MODE (op0) != Pmode))
965 op0 = convert_memory_address (Pmode, op0);
966 op0 = force_reg (Pmode, op0);
968 emit_insn (gen_prefetch (op0, op1, op2));
970 #endif
972 /* Don't do anything with direct references to volatile memory, but
973 generate code to handle other side effects. */
974 if (!MEM_P (op0) && side_effects_p (op0))
975 emit_insn (op0);
978 /* Get a MEM rtx for expression EXP which is the address of an operand
979 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
981 static rtx
982 get_memory_rtx (tree exp)
984 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
985 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
987 /* Get an expression we can use to find the attributes to assign to MEM.
988 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
989 we can. First remove any nops. */
990 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
991 || TREE_CODE (exp) == NON_LVALUE_EXPR)
992 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
993 exp = TREE_OPERAND (exp, 0);
995 if (TREE_CODE (exp) == ADDR_EXPR)
996 exp = TREE_OPERAND (exp, 0);
997 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
998 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
999 else
1000 exp = NULL;
1002 /* Honor attributes derived from exp, except for the alias set
1003 (as builtin stringops may alias with anything) and the size
1004 (as stringops may access multiple array elements). */
1005 if (exp)
1007 set_mem_attributes (mem, exp, 0);
1008 set_mem_alias_set (mem, 0);
1009 set_mem_size (mem, NULL_RTX);
1012 return mem;
1015 /* Built-in functions to perform an untyped call and return. */
1017 /* For each register that may be used for calling a function, this
1018 gives a mode used to copy the register's value. VOIDmode indicates
1019 the register is not used for calling a function. If the machine
1020 has register windows, this gives only the outbound registers.
1021 INCOMING_REGNO gives the corresponding inbound register. */
1022 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1024 /* For each register that may be used for returning values, this gives
1025 a mode used to copy the register's value. VOIDmode indicates the
1026 register is not used for returning values. If the machine has
1027 register windows, this gives only the outbound registers.
1028 INCOMING_REGNO gives the corresponding inbound register. */
1029 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1031 /* For each register that may be used for calling a function, this
1032 gives the offset of that register into the block returned by
1033 __builtin_apply_args. 0 indicates that the register is not
1034 used for calling a function. */
1035 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1037 /* Return the size required for the block returned by __builtin_apply_args,
1038 and initialize apply_args_mode. */
1040 static int
1041 apply_args_size (void)
1043 static int size = -1;
1044 int align;
1045 unsigned int regno;
1046 enum machine_mode mode;
1048 /* The values computed by this function never change. */
1049 if (size < 0)
1051 /* The first value is the incoming arg-pointer. */
1052 size = GET_MODE_SIZE (Pmode);
1054 /* The second value is the structure value address unless this is
1055 passed as an "invisible" first argument. */
1056 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1057 size += GET_MODE_SIZE (Pmode);
1059 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1060 if (FUNCTION_ARG_REGNO_P (regno))
1062 mode = reg_raw_mode[regno];
1064 gcc_assert (mode != VOIDmode);
1066 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1067 if (size % align != 0)
1068 size = CEIL (size, align) * align;
1069 apply_args_reg_offset[regno] = size;
1070 size += GET_MODE_SIZE (mode);
1071 apply_args_mode[regno] = mode;
1073 else
1075 apply_args_mode[regno] = VOIDmode;
1076 apply_args_reg_offset[regno] = 0;
1079 return size;
1082 /* Return the size required for the block returned by __builtin_apply,
1083 and initialize apply_result_mode. */
1085 static int
1086 apply_result_size (void)
1088 static int size = -1;
1089 int align, regno;
1090 enum machine_mode mode;
1092 /* The values computed by this function never change. */
1093 if (size < 0)
1095 size = 0;
1097 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1098 if (FUNCTION_VALUE_REGNO_P (regno))
1100 mode = reg_raw_mode[regno];
1102 gcc_assert (mode != VOIDmode);
1104 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1105 if (size % align != 0)
1106 size = CEIL (size, align) * align;
1107 size += GET_MODE_SIZE (mode);
1108 apply_result_mode[regno] = mode;
1110 else
1111 apply_result_mode[regno] = VOIDmode;
1113 /* Allow targets that use untyped_call and untyped_return to override
1114 the size so that machine-specific information can be stored here. */
1115 #ifdef APPLY_RESULT_SIZE
1116 size = APPLY_RESULT_SIZE;
1117 #endif
1119 return size;
1122 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1123 /* Create a vector describing the result block RESULT. If SAVEP is true,
1124 the result block is used to save the values; otherwise it is used to
1125 restore the values. */
1127 static rtx
1128 result_vector (int savep, rtx result)
1130 int regno, size, align, nelts;
1131 enum machine_mode mode;
1132 rtx reg, mem;
1133 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1135 size = nelts = 0;
1136 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1137 if ((mode = apply_result_mode[regno]) != VOIDmode)
1139 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1140 if (size % align != 0)
1141 size = CEIL (size, align) * align;
1142 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1143 mem = adjust_address (result, mode, size);
1144 savevec[nelts++] = (savep
1145 ? gen_rtx_SET (VOIDmode, mem, reg)
1146 : gen_rtx_SET (VOIDmode, reg, mem));
1147 size += GET_MODE_SIZE (mode);
1149 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1151 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1153 /* Save the state required to perform an untyped call with the same
1154 arguments as were passed to the current function. */
1156 static rtx
1157 expand_builtin_apply_args_1 (void)
1159 rtx registers, tem;
1160 int size, align, regno;
1161 enum machine_mode mode;
1162 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1164 /* Create a block where the arg-pointer, structure value address,
1165 and argument registers can be saved. */
1166 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1168 /* Walk past the arg-pointer and structure value address. */
1169 size = GET_MODE_SIZE (Pmode);
1170 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1171 size += GET_MODE_SIZE (Pmode);
1173 /* Save each register used in calling a function to the block. */
1174 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1175 if ((mode = apply_args_mode[regno]) != VOIDmode)
1177 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1178 if (size % align != 0)
1179 size = CEIL (size, align) * align;
1181 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1183 emit_move_insn (adjust_address (registers, mode, size), tem);
1184 size += GET_MODE_SIZE (mode);
1187 /* Save the arg pointer to the block. */
1188 tem = copy_to_reg (virtual_incoming_args_rtx);
1189 #ifdef STACK_GROWS_DOWNWARD
1190 /* We need the pointer as the caller actually passed them to us, not
1191 as we might have pretended they were passed. Make sure it's a valid
1192 operand, as emit_move_insn isn't expected to handle a PLUS. */
1194 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1195 NULL_RTX);
1196 #endif
1197 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1199 size = GET_MODE_SIZE (Pmode);
1201 /* Save the structure value address unless this is passed as an
1202 "invisible" first argument. */
1203 if (struct_incoming_value)
1205 emit_move_insn (adjust_address (registers, Pmode, size),
1206 copy_to_reg (struct_incoming_value));
1207 size += GET_MODE_SIZE (Pmode);
1210 /* Return the address of the block. */
1211 return copy_addr_to_reg (XEXP (registers, 0));
1214 /* __builtin_apply_args returns block of memory allocated on
1215 the stack into which is stored the arg pointer, structure
1216 value address, static chain, and all the registers that might
1217 possibly be used in performing a function call. The code is
1218 moved to the start of the function so the incoming values are
1219 saved. */
1221 static rtx
1222 expand_builtin_apply_args (void)
1224 /* Don't do __builtin_apply_args more than once in a function.
1225 Save the result of the first call and reuse it. */
1226 if (apply_args_value != 0)
1227 return apply_args_value;
1229 /* When this function is called, it means that registers must be
1230 saved on entry to this function. So we migrate the
1231 call to the first insn of this function. */
1232 rtx temp;
1233 rtx seq;
1235 start_sequence ();
1236 temp = expand_builtin_apply_args_1 ();
1237 seq = get_insns ();
1238 end_sequence ();
1240 apply_args_value = temp;
1242 /* Put the insns after the NOTE that starts the function.
1243 If this is inside a start_sequence, make the outer-level insn
1244 chain current, so the code is placed at the start of the
1245 function. */
1246 push_topmost_sequence ();
1247 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1248 pop_topmost_sequence ();
1249 return temp;
1253 /* Perform an untyped call and save the state required to perform an
1254 untyped return of whatever value was returned by the given function. */
1256 static rtx
1257 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1259 int size, align, regno;
1260 enum machine_mode mode;
1261 rtx incoming_args, result, reg, dest, src, call_insn;
1262 rtx old_stack_level = 0;
1263 rtx call_fusage = 0;
1264 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1266 arguments = convert_memory_address (Pmode, arguments);
1268 /* Create a block where the return registers can be saved. */
1269 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1271 /* Fetch the arg pointer from the ARGUMENTS block. */
1272 incoming_args = gen_reg_rtx (Pmode);
1273 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1274 #ifndef STACK_GROWS_DOWNWARD
1275 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1276 incoming_args, 0, OPTAB_LIB_WIDEN);
1277 #endif
1279 /* Push a new argument block and copy the arguments. Do not allow
1280 the (potential) memcpy call below to interfere with our stack
1281 manipulations. */
1282 do_pending_stack_adjust ();
1283 NO_DEFER_POP;
1285 /* Save the stack with nonlocal if available. */
1286 #ifdef HAVE_save_stack_nonlocal
1287 if (HAVE_save_stack_nonlocal)
1288 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1289 else
1290 #endif
1291 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1293 /* Allocate a block of memory onto the stack and copy the memory
1294 arguments to the outgoing arguments address. */
1295 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1296 dest = virtual_outgoing_args_rtx;
1297 #ifndef STACK_GROWS_DOWNWARD
1298 if (GET_CODE (argsize) == CONST_INT)
1299 dest = plus_constant (dest, -INTVAL (argsize));
1300 else
1301 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1302 #endif
1303 dest = gen_rtx_MEM (BLKmode, dest);
1304 set_mem_align (dest, PARM_BOUNDARY);
1305 src = gen_rtx_MEM (BLKmode, incoming_args);
1306 set_mem_align (src, PARM_BOUNDARY);
1307 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1309 /* Refer to the argument block. */
1310 apply_args_size ();
1311 arguments = gen_rtx_MEM (BLKmode, arguments);
1312 set_mem_align (arguments, PARM_BOUNDARY);
1314 /* Walk past the arg-pointer and structure value address. */
1315 size = GET_MODE_SIZE (Pmode);
1316 if (struct_value)
1317 size += GET_MODE_SIZE (Pmode);
1319 /* Restore each of the registers previously saved. Make USE insns
1320 for each of these registers for use in making the call. */
1321 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1322 if ((mode = apply_args_mode[regno]) != VOIDmode)
1324 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1325 if (size % align != 0)
1326 size = CEIL (size, align) * align;
1327 reg = gen_rtx_REG (mode, regno);
1328 emit_move_insn (reg, adjust_address (arguments, mode, size));
1329 use_reg (&call_fusage, reg);
1330 size += GET_MODE_SIZE (mode);
1333 /* Restore the structure value address unless this is passed as an
1334 "invisible" first argument. */
1335 size = GET_MODE_SIZE (Pmode);
1336 if (struct_value)
1338 rtx value = gen_reg_rtx (Pmode);
1339 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1340 emit_move_insn (struct_value, value);
1341 if (REG_P (struct_value))
1342 use_reg (&call_fusage, struct_value);
1343 size += GET_MODE_SIZE (Pmode);
1346 /* All arguments and registers used for the call are set up by now! */
1347 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1349 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1350 and we don't want to load it into a register as an optimization,
1351 because prepare_call_address already did it if it should be done. */
1352 if (GET_CODE (function) != SYMBOL_REF)
1353 function = memory_address (FUNCTION_MODE, function);
1355 /* Generate the actual call instruction and save the return value. */
1356 #ifdef HAVE_untyped_call
1357 if (HAVE_untyped_call)
1358 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1359 result, result_vector (1, result)));
1360 else
1361 #endif
1362 #ifdef HAVE_call_value
1363 if (HAVE_call_value)
1365 rtx valreg = 0;
1367 /* Locate the unique return register. It is not possible to
1368 express a call that sets more than one return register using
1369 call_value; use untyped_call for that. In fact, untyped_call
1370 only needs to save the return registers in the given block. */
1371 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1372 if ((mode = apply_result_mode[regno]) != VOIDmode)
1374 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1376 valreg = gen_rtx_REG (mode, regno);
1379 emit_call_insn (GEN_CALL_VALUE (valreg,
1380 gen_rtx_MEM (FUNCTION_MODE, function),
1381 const0_rtx, NULL_RTX, const0_rtx));
1383 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1385 else
1386 #endif
1387 gcc_unreachable ();
1389 /* Find the CALL insn we just emitted, and attach the register usage
1390 information. */
1391 call_insn = last_call_insn ();
1392 add_function_usage_to (call_insn, call_fusage);
1394 /* Restore the stack. */
1395 #ifdef HAVE_save_stack_nonlocal
1396 if (HAVE_save_stack_nonlocal)
1397 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1398 else
1399 #endif
1400 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1402 OK_DEFER_POP;
1404 /* Return the address of the result block. */
1405 result = copy_addr_to_reg (XEXP (result, 0));
1406 return convert_memory_address (ptr_mode, result);
1409 /* Perform an untyped return. */
1411 static void
1412 expand_builtin_return (rtx result)
1414 int size, align, regno;
1415 enum machine_mode mode;
1416 rtx reg;
1417 rtx call_fusage = 0;
1419 result = convert_memory_address (Pmode, result);
1421 apply_result_size ();
1422 result = gen_rtx_MEM (BLKmode, result);
1424 #ifdef HAVE_untyped_return
1425 if (HAVE_untyped_return)
1427 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1428 emit_barrier ();
1429 return;
1431 #endif
1433 /* Restore the return value and note that each value is used. */
1434 size = 0;
1435 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1436 if ((mode = apply_result_mode[regno]) != VOIDmode)
1438 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1439 if (size % align != 0)
1440 size = CEIL (size, align) * align;
1441 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1442 emit_move_insn (reg, adjust_address (result, mode, size));
1444 push_to_sequence (call_fusage);
1445 emit_insn (gen_rtx_USE (VOIDmode, reg));
1446 call_fusage = get_insns ();
1447 end_sequence ();
1448 size += GET_MODE_SIZE (mode);
1451 /* Put the USE insns before the return. */
1452 emit_insn (call_fusage);
1454 /* Return whatever values was restored by jumping directly to the end
1455 of the function. */
1456 expand_naked_return ();
1459 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1461 static enum type_class
1462 type_to_class (tree type)
1464 switch (TREE_CODE (type))
1466 case VOID_TYPE: return void_type_class;
1467 case INTEGER_TYPE: return integer_type_class;
1468 case CHAR_TYPE: return char_type_class;
1469 case ENUMERAL_TYPE: return enumeral_type_class;
1470 case BOOLEAN_TYPE: return boolean_type_class;
1471 case POINTER_TYPE: return pointer_type_class;
1472 case REFERENCE_TYPE: return reference_type_class;
1473 case OFFSET_TYPE: return offset_type_class;
1474 case REAL_TYPE: return real_type_class;
1475 case COMPLEX_TYPE: return complex_type_class;
1476 case FUNCTION_TYPE: return function_type_class;
1477 case METHOD_TYPE: return method_type_class;
1478 case RECORD_TYPE: return record_type_class;
1479 case UNION_TYPE:
1480 case QUAL_UNION_TYPE: return union_type_class;
1481 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1482 ? string_type_class : array_type_class);
1483 case LANG_TYPE: return lang_type_class;
1484 default: return no_type_class;
1488 /* Expand a call to __builtin_classify_type with arguments found in
1489 ARGLIST. */
1491 static rtx
1492 expand_builtin_classify_type (tree arglist)
1494 if (arglist != 0)
1495 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1496 return GEN_INT (no_type_class);
1499 /* This helper macro, meant to be used in mathfn_built_in below,
1500 determines which among a set of three builtin math functions is
1501 appropriate for a given type mode. The `F' and `L' cases are
1502 automatically generated from the `double' case. */
1503 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1504 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1505 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1506 fcodel = BUILT_IN_MATHFN##L ; break;
1508 /* Return mathematic function equivalent to FN but operating directly
1509 on TYPE, if available. If we can't do the conversion, return zero. */
1510 tree
1511 mathfn_built_in (tree type, enum built_in_function fn)
1513 enum built_in_function fcode, fcodef, fcodel;
1515 switch (fn)
1517 CASE_MATHFN (BUILT_IN_ACOS)
1518 CASE_MATHFN (BUILT_IN_ACOSH)
1519 CASE_MATHFN (BUILT_IN_ASIN)
1520 CASE_MATHFN (BUILT_IN_ASINH)
1521 CASE_MATHFN (BUILT_IN_ATAN)
1522 CASE_MATHFN (BUILT_IN_ATAN2)
1523 CASE_MATHFN (BUILT_IN_ATANH)
1524 CASE_MATHFN (BUILT_IN_CBRT)
1525 CASE_MATHFN (BUILT_IN_CEIL)
1526 CASE_MATHFN (BUILT_IN_COPYSIGN)
1527 CASE_MATHFN (BUILT_IN_COS)
1528 CASE_MATHFN (BUILT_IN_COSH)
1529 CASE_MATHFN (BUILT_IN_DREM)
1530 CASE_MATHFN (BUILT_IN_ERF)
1531 CASE_MATHFN (BUILT_IN_ERFC)
1532 CASE_MATHFN (BUILT_IN_EXP)
1533 CASE_MATHFN (BUILT_IN_EXP10)
1534 CASE_MATHFN (BUILT_IN_EXP2)
1535 CASE_MATHFN (BUILT_IN_EXPM1)
1536 CASE_MATHFN (BUILT_IN_FABS)
1537 CASE_MATHFN (BUILT_IN_FDIM)
1538 CASE_MATHFN (BUILT_IN_FLOOR)
1539 CASE_MATHFN (BUILT_IN_FMA)
1540 CASE_MATHFN (BUILT_IN_FMAX)
1541 CASE_MATHFN (BUILT_IN_FMIN)
1542 CASE_MATHFN (BUILT_IN_FMOD)
1543 CASE_MATHFN (BUILT_IN_FREXP)
1544 CASE_MATHFN (BUILT_IN_GAMMA)
1545 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1546 CASE_MATHFN (BUILT_IN_HYPOT)
1547 CASE_MATHFN (BUILT_IN_ILOGB)
1548 CASE_MATHFN (BUILT_IN_INF)
1549 CASE_MATHFN (BUILT_IN_J0)
1550 CASE_MATHFN (BUILT_IN_J1)
1551 CASE_MATHFN (BUILT_IN_JN)
1552 CASE_MATHFN (BUILT_IN_LDEXP)
1553 CASE_MATHFN (BUILT_IN_LFLOOR)
1554 CASE_MATHFN (BUILT_IN_LGAMMA)
1555 CASE_MATHFN (BUILT_IN_LLFLOOR)
1556 CASE_MATHFN (BUILT_IN_LLRINT)
1557 CASE_MATHFN (BUILT_IN_LLROUND)
1558 CASE_MATHFN (BUILT_IN_LOG)
1559 CASE_MATHFN (BUILT_IN_LOG10)
1560 CASE_MATHFN (BUILT_IN_LOG1P)
1561 CASE_MATHFN (BUILT_IN_LOG2)
1562 CASE_MATHFN (BUILT_IN_LOGB)
1563 CASE_MATHFN (BUILT_IN_LRINT)
1564 CASE_MATHFN (BUILT_IN_LROUND)
1565 CASE_MATHFN (BUILT_IN_MODF)
1566 CASE_MATHFN (BUILT_IN_NAN)
1567 CASE_MATHFN (BUILT_IN_NANS)
1568 CASE_MATHFN (BUILT_IN_NEARBYINT)
1569 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1570 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1571 CASE_MATHFN (BUILT_IN_POW)
1572 CASE_MATHFN (BUILT_IN_POWI)
1573 CASE_MATHFN (BUILT_IN_POW10)
1574 CASE_MATHFN (BUILT_IN_REMAINDER)
1575 CASE_MATHFN (BUILT_IN_REMQUO)
1576 CASE_MATHFN (BUILT_IN_RINT)
1577 CASE_MATHFN (BUILT_IN_ROUND)
1578 CASE_MATHFN (BUILT_IN_SCALB)
1579 CASE_MATHFN (BUILT_IN_SCALBLN)
1580 CASE_MATHFN (BUILT_IN_SCALBN)
1581 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1582 CASE_MATHFN (BUILT_IN_SIN)
1583 CASE_MATHFN (BUILT_IN_SINCOS)
1584 CASE_MATHFN (BUILT_IN_SINH)
1585 CASE_MATHFN (BUILT_IN_SQRT)
1586 CASE_MATHFN (BUILT_IN_TAN)
1587 CASE_MATHFN (BUILT_IN_TANH)
1588 CASE_MATHFN (BUILT_IN_TGAMMA)
1589 CASE_MATHFN (BUILT_IN_TRUNC)
1590 CASE_MATHFN (BUILT_IN_Y0)
1591 CASE_MATHFN (BUILT_IN_Y1)
1592 CASE_MATHFN (BUILT_IN_YN)
1594 default:
1595 return 0;
1598 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1599 return implicit_built_in_decls[fcode];
1600 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1601 return implicit_built_in_decls[fcodef];
1602 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1603 return implicit_built_in_decls[fcodel];
1604 else
1605 return 0;
1608 /* If errno must be maintained, expand the RTL to check if the result,
1609 TARGET, of a built-in function call, EXP, is NaN, and if so set
1610 errno to EDOM. */
1612 static void
1613 expand_errno_check (tree exp, rtx target)
1615 rtx lab = gen_label_rtx ();
1617 /* Test the result; if it is NaN, set errno=EDOM because
1618 the argument was not in the domain. */
1619 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1620 0, lab);
1622 #ifdef TARGET_EDOM
1623 /* If this built-in doesn't throw an exception, set errno directly. */
1624 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1626 #ifdef GEN_ERRNO_RTX
1627 rtx errno_rtx = GEN_ERRNO_RTX;
1628 #else
1629 rtx errno_rtx
1630 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1631 #endif
1632 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1633 emit_label (lab);
1634 return;
1636 #endif
1638 /* We can't set errno=EDOM directly; let the library call do it.
1639 Pop the arguments right away in case the call gets deleted. */
1640 NO_DEFER_POP;
1641 expand_call (exp, target, 0);
1642 OK_DEFER_POP;
1643 emit_label (lab);
1647 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1648 Return 0 if a normal call should be emitted rather than expanding the
1649 function in-line. EXP is the expression that is a call to the builtin
1650 function; if convenient, the result should be placed in TARGET.
1651 SUBTARGET may be used as the target for computing one of EXP's operands. */
1653 static rtx
1654 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1656 optab builtin_optab;
1657 rtx op0, insns, before_call;
1658 tree fndecl = get_callee_fndecl (exp);
1659 tree arglist = TREE_OPERAND (exp, 1);
1660 enum machine_mode mode;
1661 bool errno_set = false;
1662 tree arg, narg;
1664 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1665 return 0;
1667 arg = TREE_VALUE (arglist);
1669 switch (DECL_FUNCTION_CODE (fndecl))
1671 case BUILT_IN_SQRT:
1672 case BUILT_IN_SQRTF:
1673 case BUILT_IN_SQRTL:
1674 errno_set = ! tree_expr_nonnegative_p (arg);
1675 builtin_optab = sqrt_optab;
1676 break;
1677 case BUILT_IN_EXP:
1678 case BUILT_IN_EXPF:
1679 case BUILT_IN_EXPL:
1680 errno_set = true; builtin_optab = exp_optab; break;
1681 case BUILT_IN_EXP10:
1682 case BUILT_IN_EXP10F:
1683 case BUILT_IN_EXP10L:
1684 case BUILT_IN_POW10:
1685 case BUILT_IN_POW10F:
1686 case BUILT_IN_POW10L:
1687 errno_set = true; builtin_optab = exp10_optab; break;
1688 case BUILT_IN_EXP2:
1689 case BUILT_IN_EXP2F:
1690 case BUILT_IN_EXP2L:
1691 errno_set = true; builtin_optab = exp2_optab; break;
1692 case BUILT_IN_EXPM1:
1693 case BUILT_IN_EXPM1F:
1694 case BUILT_IN_EXPM1L:
1695 errno_set = true; builtin_optab = expm1_optab; break;
1696 case BUILT_IN_LOGB:
1697 case BUILT_IN_LOGBF:
1698 case BUILT_IN_LOGBL:
1699 errno_set = true; builtin_optab = logb_optab; break;
1700 case BUILT_IN_ILOGB:
1701 case BUILT_IN_ILOGBF:
1702 case BUILT_IN_ILOGBL:
1703 errno_set = true; builtin_optab = ilogb_optab; break;
1704 case BUILT_IN_LOG:
1705 case BUILT_IN_LOGF:
1706 case BUILT_IN_LOGL:
1707 errno_set = true; builtin_optab = log_optab; break;
1708 case BUILT_IN_LOG10:
1709 case BUILT_IN_LOG10F:
1710 case BUILT_IN_LOG10L:
1711 errno_set = true; builtin_optab = log10_optab; break;
1712 case BUILT_IN_LOG2:
1713 case BUILT_IN_LOG2F:
1714 case BUILT_IN_LOG2L:
1715 errno_set = true; builtin_optab = log2_optab; break;
1716 case BUILT_IN_LOG1P:
1717 case BUILT_IN_LOG1PF:
1718 case BUILT_IN_LOG1PL:
1719 errno_set = true; builtin_optab = log1p_optab; break;
1720 case BUILT_IN_ASIN:
1721 case BUILT_IN_ASINF:
1722 case BUILT_IN_ASINL:
1723 builtin_optab = asin_optab; break;
1724 case BUILT_IN_ACOS:
1725 case BUILT_IN_ACOSF:
1726 case BUILT_IN_ACOSL:
1727 builtin_optab = acos_optab; break;
1728 case BUILT_IN_TAN:
1729 case BUILT_IN_TANF:
1730 case BUILT_IN_TANL:
1731 builtin_optab = tan_optab; break;
1732 case BUILT_IN_ATAN:
1733 case BUILT_IN_ATANF:
1734 case BUILT_IN_ATANL:
1735 builtin_optab = atan_optab; break;
1736 case BUILT_IN_FLOOR:
1737 case BUILT_IN_FLOORF:
1738 case BUILT_IN_FLOORL:
1739 builtin_optab = floor_optab; break;
1740 case BUILT_IN_CEIL:
1741 case BUILT_IN_CEILF:
1742 case BUILT_IN_CEILL:
1743 builtin_optab = ceil_optab; break;
1744 case BUILT_IN_TRUNC:
1745 case BUILT_IN_TRUNCF:
1746 case BUILT_IN_TRUNCL:
1747 builtin_optab = btrunc_optab; break;
1748 case BUILT_IN_ROUND:
1749 case BUILT_IN_ROUNDF:
1750 case BUILT_IN_ROUNDL:
1751 builtin_optab = round_optab; break;
1752 case BUILT_IN_NEARBYINT:
1753 case BUILT_IN_NEARBYINTF:
1754 case BUILT_IN_NEARBYINTL:
1755 builtin_optab = nearbyint_optab; break;
1756 case BUILT_IN_RINT:
1757 case BUILT_IN_RINTF:
1758 case BUILT_IN_RINTL:
1759 builtin_optab = rint_optab; break;
1760 case BUILT_IN_LRINT:
1761 case BUILT_IN_LRINTF:
1762 case BUILT_IN_LRINTL:
1763 case BUILT_IN_LLRINT:
1764 case BUILT_IN_LLRINTF:
1765 case BUILT_IN_LLRINTL:
1766 builtin_optab = lrint_optab; break;
1767 default:
1768 gcc_unreachable ();
1771 /* Make a suitable register to place result in. */
1772 mode = TYPE_MODE (TREE_TYPE (exp));
1774 if (! flag_errno_math || ! HONOR_NANS (mode))
1775 errno_set = false;
1777 /* Before working hard, check whether the instruction is available. */
1778 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1780 target = gen_reg_rtx (mode);
1782 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1783 need to expand the argument again. This way, we will not perform
1784 side-effects more the once. */
1785 narg = builtin_save_expr (arg);
1786 if (narg != arg)
1788 arg = narg;
1789 arglist = build_tree_list (NULL_TREE, arg);
1790 exp = build_function_call_expr (fndecl, arglist);
1793 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1795 start_sequence ();
1797 /* Compute into TARGET.
1798 Set TARGET to wherever the result comes back. */
1799 target = expand_unop (mode, builtin_optab, op0, target, 0);
1801 if (target != 0)
1803 if (errno_set)
1804 expand_errno_check (exp, target);
1806 /* Output the entire sequence. */
1807 insns = get_insns ();
1808 end_sequence ();
1809 emit_insn (insns);
1810 return target;
1813 /* If we were unable to expand via the builtin, stop the sequence
1814 (without outputting the insns) and call to the library function
1815 with the stabilized argument list. */
1816 end_sequence ();
1819 before_call = get_last_insn ();
1821 target = expand_call (exp, target, target == const0_rtx);
1823 /* If this is a sqrt operation and we don't care about errno, try to
1824 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1825 This allows the semantics of the libcall to be visible to the RTL
1826 optimizers. */
1827 if (builtin_optab == sqrt_optab && !errno_set)
1829 /* Search backwards through the insns emitted by expand_call looking
1830 for the instruction with the REG_RETVAL note. */
1831 rtx last = get_last_insn ();
1832 while (last != before_call)
1834 if (find_reg_note (last, REG_RETVAL, NULL))
1836 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1837 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1838 two elements, i.e. symbol_ref(sqrt) and the operand. */
1839 if (note
1840 && GET_CODE (note) == EXPR_LIST
1841 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1842 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1843 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1845 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1846 /* Check operand is a register with expected mode. */
1847 if (operand
1848 && REG_P (operand)
1849 && GET_MODE (operand) == mode)
1851 /* Replace the REG_EQUAL note with a SQRT rtx. */
1852 rtx equiv = gen_rtx_SQRT (mode, operand);
1853 set_unique_reg_note (last, REG_EQUAL, equiv);
1856 break;
1858 last = PREV_INSN (last);
1862 return target;
1865 /* Expand a call to the builtin binary math functions (pow and atan2).
1866 Return 0 if a normal call should be emitted rather than expanding the
1867 function in-line. EXP is the expression that is a call to the builtin
1868 function; if convenient, the result should be placed in TARGET.
1869 SUBTARGET may be used as the target for computing one of EXP's
1870 operands. */
1872 static rtx
1873 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1875 optab builtin_optab;
1876 rtx op0, op1, insns;
1877 int op1_type = REAL_TYPE;
1878 tree fndecl = get_callee_fndecl (exp);
1879 tree arglist = TREE_OPERAND (exp, 1);
1880 tree arg0, arg1, temp, narg;
1881 enum machine_mode mode;
1882 bool errno_set = true;
1883 bool stable = true;
1885 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1886 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1887 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1888 op1_type = INTEGER_TYPE;
1890 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1891 return 0;
1893 arg0 = TREE_VALUE (arglist);
1894 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1896 switch (DECL_FUNCTION_CODE (fndecl))
1898 case BUILT_IN_POW:
1899 case BUILT_IN_POWF:
1900 case BUILT_IN_POWL:
1901 builtin_optab = pow_optab; break;
1902 case BUILT_IN_ATAN2:
1903 case BUILT_IN_ATAN2F:
1904 case BUILT_IN_ATAN2L:
1905 builtin_optab = atan2_optab; break;
1906 case BUILT_IN_LDEXP:
1907 case BUILT_IN_LDEXPF:
1908 case BUILT_IN_LDEXPL:
1909 builtin_optab = ldexp_optab; break;
1910 case BUILT_IN_FMOD:
1911 case BUILT_IN_FMODF:
1912 case BUILT_IN_FMODL:
1913 builtin_optab = fmod_optab; break;
1914 case BUILT_IN_DREM:
1915 case BUILT_IN_DREMF:
1916 case BUILT_IN_DREML:
1917 builtin_optab = drem_optab; break;
1918 default:
1919 gcc_unreachable ();
1922 /* Make a suitable register to place result in. */
1923 mode = TYPE_MODE (TREE_TYPE (exp));
1925 /* Before working hard, check whether the instruction is available. */
1926 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1927 return 0;
1929 target = gen_reg_rtx (mode);
1931 if (! flag_errno_math || ! HONOR_NANS (mode))
1932 errno_set = false;
1934 /* Always stabilize the argument list. */
1935 narg = builtin_save_expr (arg1);
1936 if (narg != arg1)
1938 arg1 = narg;
1939 temp = build_tree_list (NULL_TREE, narg);
1940 stable = false;
1942 else
1943 temp = TREE_CHAIN (arglist);
1945 narg = builtin_save_expr (arg0);
1946 if (narg != arg0)
1948 arg0 = narg;
1949 arglist = tree_cons (NULL_TREE, narg, temp);
1950 stable = false;
1952 else if (! stable)
1953 arglist = tree_cons (NULL_TREE, arg0, temp);
1955 if (! stable)
1956 exp = build_function_call_expr (fndecl, arglist);
1958 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1959 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1961 start_sequence ();
1963 /* Compute into TARGET.
1964 Set TARGET to wherever the result comes back. */
1965 target = expand_binop (mode, builtin_optab, op0, op1,
1966 target, 0, OPTAB_DIRECT);
1968 /* If we were unable to expand via the builtin, stop the sequence
1969 (without outputting the insns) and call to the library function
1970 with the stabilized argument list. */
1971 if (target == 0)
1973 end_sequence ();
1974 return expand_call (exp, target, target == const0_rtx);
1977 if (errno_set)
1978 expand_errno_check (exp, target);
1980 /* Output the entire sequence. */
1981 insns = get_insns ();
1982 end_sequence ();
1983 emit_insn (insns);
1985 return target;
1988 /* Expand a call to the builtin sin and cos math functions.
1989 Return 0 if a normal call should be emitted rather than expanding the
1990 function in-line. EXP is the expression that is a call to the builtin
1991 function; if convenient, the result should be placed in TARGET.
1992 SUBTARGET may be used as the target for computing one of EXP's
1993 operands. */
1995 static rtx
1996 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1998 optab builtin_optab;
1999 rtx op0, insns;
2000 tree fndecl = get_callee_fndecl (exp);
2001 tree arglist = TREE_OPERAND (exp, 1);
2002 enum machine_mode mode;
2003 bool errno_set = false;
2004 tree arg, narg;
2006 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2007 return 0;
2009 arg = TREE_VALUE (arglist);
2011 switch (DECL_FUNCTION_CODE (fndecl))
2013 case BUILT_IN_SIN:
2014 case BUILT_IN_SINF:
2015 case BUILT_IN_SINL:
2016 case BUILT_IN_COS:
2017 case BUILT_IN_COSF:
2018 case BUILT_IN_COSL:
2019 builtin_optab = sincos_optab; break;
2020 default:
2021 gcc_unreachable ();
2024 /* Make a suitable register to place result in. */
2025 mode = TYPE_MODE (TREE_TYPE (exp));
2027 if (! flag_errno_math || ! HONOR_NANS (mode))
2028 errno_set = false;
2030 /* Check if sincos insn is available, otherwise fallback
2031 to sin or cos insn. */
2032 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2033 switch (DECL_FUNCTION_CODE (fndecl))
2035 case BUILT_IN_SIN:
2036 case BUILT_IN_SINF:
2037 case BUILT_IN_SINL:
2038 builtin_optab = sin_optab; break;
2039 case BUILT_IN_COS:
2040 case BUILT_IN_COSF:
2041 case BUILT_IN_COSL:
2042 builtin_optab = cos_optab; break;
2043 default:
2044 gcc_unreachable ();
2048 /* Before working hard, check whether the instruction is available. */
2049 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2051 target = gen_reg_rtx (mode);
2053 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2054 need to expand the argument again. This way, we will not perform
2055 side-effects more the once. */
2056 narg = save_expr (arg);
2057 if (narg != arg)
2059 arg = narg;
2060 arglist = build_tree_list (NULL_TREE, arg);
2061 exp = build_function_call_expr (fndecl, arglist);
2064 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2066 start_sequence ();
2068 /* Compute into TARGET.
2069 Set TARGET to wherever the result comes back. */
2070 if (builtin_optab == sincos_optab)
2072 int result;
2074 switch (DECL_FUNCTION_CODE (fndecl))
2076 case BUILT_IN_SIN:
2077 case BUILT_IN_SINF:
2078 case BUILT_IN_SINL:
2079 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2080 break;
2081 case BUILT_IN_COS:
2082 case BUILT_IN_COSF:
2083 case BUILT_IN_COSL:
2084 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2085 break;
2086 default:
2087 gcc_unreachable ();
2089 gcc_assert (result);
2091 else
2093 target = expand_unop (mode, builtin_optab, op0, target, 0);
2096 if (target != 0)
2098 if (errno_set)
2099 expand_errno_check (exp, target);
2101 /* Output the entire sequence. */
2102 insns = get_insns ();
2103 end_sequence ();
2104 emit_insn (insns);
2105 return target;
2108 /* If we were unable to expand via the builtin, stop the sequence
2109 (without outputting the insns) and call to the library function
2110 with the stabilized argument list. */
2111 end_sequence ();
2114 target = expand_call (exp, target, target == const0_rtx);
2116 return target;
2119 /* Expand a call to one of the builtin rounding functions (lfloor).
2120 If expanding via optab fails, lower expression to (int)(floor(x)).
2121 EXP is the expression that is a call to the builtin function;
2122 if convenient, the result should be placed in TARGET. SUBTARGET may
2123 be used as the target for computing one of EXP's operands. */
2125 static rtx
2126 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2128 optab builtin_optab;
2129 rtx op0, insns, tmp;
2130 tree fndecl = get_callee_fndecl (exp);
2131 tree arglist = TREE_OPERAND (exp, 1);
2132 enum built_in_function fallback_fn;
2133 tree fallback_fndecl;
2134 enum machine_mode mode;
2135 tree arg, narg;
2137 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2138 gcc_unreachable ();
2140 arg = TREE_VALUE (arglist);
2142 switch (DECL_FUNCTION_CODE (fndecl))
2144 case BUILT_IN_LFLOOR:
2145 case BUILT_IN_LFLOORF:
2146 case BUILT_IN_LFLOORL:
2147 case BUILT_IN_LLFLOOR:
2148 case BUILT_IN_LLFLOORF:
2149 case BUILT_IN_LLFLOORL:
2150 builtin_optab = lfloor_optab;
2151 fallback_fn = BUILT_IN_FLOOR;
2152 break;
2154 default:
2155 gcc_unreachable ();
2158 /* Make a suitable register to place result in. */
2159 mode = TYPE_MODE (TREE_TYPE (exp));
2161 /* Before working hard, check whether the instruction is available. */
2162 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2164 target = gen_reg_rtx (mode);
2166 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2167 need to expand the argument again. This way, we will not perform
2168 side-effects more the once. */
2169 narg = builtin_save_expr (arg);
2170 if (narg != arg)
2172 arg = narg;
2173 arglist = build_tree_list (NULL_TREE, arg);
2174 exp = build_function_call_expr (fndecl, arglist);
2177 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2179 start_sequence ();
2181 /* Compute into TARGET.
2182 Set TARGET to wherever the result comes back. */
2183 target = expand_unop (mode, builtin_optab, op0, target, 0);
2185 if (target != 0)
2187 /* Output the entire sequence. */
2188 insns = get_insns ();
2189 end_sequence ();
2190 emit_insn (insns);
2191 return target;
2194 /* If we were unable to expand via the builtin, stop the sequence
2195 (without outputting the insns). */
2196 end_sequence ();
2199 /* Fall back to floating point rounding optab. */
2200 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2201 exp = build_function_call_expr (fallback_fndecl, arglist);
2203 tmp = expand_builtin_mathfn (exp, NULL_RTX, NULL_RTX);
2205 /* Truncate the result of floating point optab to integer
2206 via expand_fix (). */
2207 target = gen_reg_rtx (mode);
2208 expand_fix (target, tmp, 0);
2210 return target;
2213 /* To evaluate powi(x,n), the floating point value x raised to the
2214 constant integer exponent n, we use a hybrid algorithm that
2215 combines the "window method" with look-up tables. For an
2216 introduction to exponentiation algorithms and "addition chains",
2217 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2218 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2219 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2220 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2222 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2223 multiplications to inline before calling the system library's pow
2224 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2225 so this default never requires calling pow, powf or powl. */
2227 #ifndef POWI_MAX_MULTS
2228 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2229 #endif
2231 /* The size of the "optimal power tree" lookup table. All
2232 exponents less than this value are simply looked up in the
2233 powi_table below. This threshold is also used to size the
2234 cache of pseudo registers that hold intermediate results. */
2235 #define POWI_TABLE_SIZE 256
2237 /* The size, in bits of the window, used in the "window method"
2238 exponentiation algorithm. This is equivalent to a radix of
2239 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2240 #define POWI_WINDOW_SIZE 3
2242 /* The following table is an efficient representation of an
2243 "optimal power tree". For each value, i, the corresponding
2244 value, j, in the table states than an optimal evaluation
2245 sequence for calculating pow(x,i) can be found by evaluating
2246 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2247 100 integers is given in Knuth's "Seminumerical algorithms". */
2249 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2251 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2252 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2253 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2254 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2255 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2256 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2257 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2258 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2259 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2260 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2261 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2262 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2263 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2264 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2265 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2266 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2267 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2268 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2269 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2270 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2271 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2272 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2273 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2274 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2275 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2276 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2277 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2278 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2279 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2280 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2281 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2282 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2286 /* Return the number of multiplications required to calculate
2287 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2288 subroutine of powi_cost. CACHE is an array indicating
2289 which exponents have already been calculated. */
2291 static int
2292 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2294 /* If we've already calculated this exponent, then this evaluation
2295 doesn't require any additional multiplications. */
2296 if (cache[n])
2297 return 0;
2299 cache[n] = true;
2300 return powi_lookup_cost (n - powi_table[n], cache)
2301 + powi_lookup_cost (powi_table[n], cache) + 1;
2304 /* Return the number of multiplications required to calculate
2305 powi(x,n) for an arbitrary x, given the exponent N. This
2306 function needs to be kept in sync with expand_powi below. */
2308 static int
2309 powi_cost (HOST_WIDE_INT n)
2311 bool cache[POWI_TABLE_SIZE];
2312 unsigned HOST_WIDE_INT digit;
2313 unsigned HOST_WIDE_INT val;
2314 int result;
2316 if (n == 0)
2317 return 0;
2319 /* Ignore the reciprocal when calculating the cost. */
2320 val = (n < 0) ? -n : n;
2322 /* Initialize the exponent cache. */
2323 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2324 cache[1] = true;
2326 result = 0;
2328 while (val >= POWI_TABLE_SIZE)
2330 if (val & 1)
2332 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2333 result += powi_lookup_cost (digit, cache)
2334 + POWI_WINDOW_SIZE + 1;
2335 val >>= POWI_WINDOW_SIZE;
2337 else
2339 val >>= 1;
2340 result++;
2344 return result + powi_lookup_cost (val, cache);
2347 /* Recursive subroutine of expand_powi. This function takes the array,
2348 CACHE, of already calculated exponents and an exponent N and returns
2349 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2351 static rtx
2352 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2354 unsigned HOST_WIDE_INT digit;
2355 rtx target, result;
2356 rtx op0, op1;
2358 if (n < POWI_TABLE_SIZE)
2360 if (cache[n])
2361 return cache[n];
2363 target = gen_reg_rtx (mode);
2364 cache[n] = target;
2366 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2367 op1 = expand_powi_1 (mode, powi_table[n], cache);
2369 else if (n & 1)
2371 target = gen_reg_rtx (mode);
2372 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2373 op0 = expand_powi_1 (mode, n - digit, cache);
2374 op1 = expand_powi_1 (mode, digit, cache);
2376 else
2378 target = gen_reg_rtx (mode);
2379 op0 = expand_powi_1 (mode, n >> 1, cache);
2380 op1 = op0;
2383 result = expand_mult (mode, op0, op1, target, 0);
2384 if (result != target)
2385 emit_move_insn (target, result);
2386 return target;
2389 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2390 floating point operand in mode MODE, and N is the exponent. This
2391 function needs to be kept in sync with powi_cost above. */
2393 static rtx
2394 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2396 unsigned HOST_WIDE_INT val;
2397 rtx cache[POWI_TABLE_SIZE];
2398 rtx result;
2400 if (n == 0)
2401 return CONST1_RTX (mode);
2403 val = (n < 0) ? -n : n;
2405 memset (cache, 0, sizeof (cache));
2406 cache[1] = x;
2408 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2410 /* If the original exponent was negative, reciprocate the result. */
2411 if (n < 0)
2412 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2413 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2415 return result;
2418 /* Expand a call to the pow built-in mathematical function. Return 0 if
2419 a normal call should be emitted rather than expanding the function
2420 in-line. EXP is the expression that is a call to the builtin
2421 function; if convenient, the result should be placed in TARGET. */
2423 static rtx
2424 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2426 tree arglist = TREE_OPERAND (exp, 1);
2427 tree arg0, arg1;
2429 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2430 return 0;
2432 arg0 = TREE_VALUE (arglist);
2433 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2435 if (TREE_CODE (arg1) == REAL_CST
2436 && ! TREE_CONSTANT_OVERFLOW (arg1))
2438 REAL_VALUE_TYPE cint;
2439 REAL_VALUE_TYPE c;
2440 HOST_WIDE_INT n;
2442 c = TREE_REAL_CST (arg1);
2443 n = real_to_integer (&c);
2444 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2445 if (real_identical (&c, &cint))
2447 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2448 Otherwise, check the number of multiplications required.
2449 Note that pow never sets errno for an integer exponent. */
2450 if ((n >= -1 && n <= 2)
2451 || (flag_unsafe_math_optimizations
2452 && ! optimize_size
2453 && powi_cost (n) <= POWI_MAX_MULTS))
2455 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2456 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2457 op = force_reg (mode, op);
2458 return expand_powi (op, mode, n);
2463 if (! flag_unsafe_math_optimizations)
2464 return NULL_RTX;
2465 return expand_builtin_mathfn_2 (exp, target, subtarget);
2468 /* Expand a call to the powi built-in mathematical function. Return 0 if
2469 a normal call should be emitted rather than expanding the function
2470 in-line. EXP is the expression that is a call to the builtin
2471 function; if convenient, the result should be placed in TARGET. */
2473 static rtx
2474 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2476 tree arglist = TREE_OPERAND (exp, 1);
2477 tree arg0, arg1;
2478 rtx op0, op1;
2479 enum machine_mode mode;
2480 enum machine_mode mode2;
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 /* Mode of the 2nd argument must match that of an int. */
2513 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2515 if (target == NULL_RTX)
2516 target = gen_reg_rtx (mode);
2518 op0 = expand_expr (arg0, subtarget, mode, 0);
2519 if (GET_MODE (op0) != mode)
2520 op0 = convert_to_mode (mode, op0, 0);
2521 op1 = expand_expr (arg1, 0, mode2, 0);
2522 if (GET_MODE (op1) != mode2)
2523 op1 = convert_to_mode (mode2, op1, 0);
2525 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2526 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2527 op0, mode, op1, mode2);
2529 return target;
2532 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2533 if we failed the caller should emit a normal call, otherwise
2534 try to get the result in TARGET, if convenient. */
2536 static rtx
2537 expand_builtin_strlen (tree arglist, rtx target,
2538 enum machine_mode target_mode)
2540 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2541 return 0;
2542 else
2544 rtx pat;
2545 tree len, src = TREE_VALUE (arglist);
2546 rtx result, src_reg, char_rtx, before_strlen;
2547 enum machine_mode insn_mode = target_mode, char_mode;
2548 enum insn_code icode = CODE_FOR_nothing;
2549 int align;
2551 /* If the length can be computed at compile-time, return it. */
2552 len = c_strlen (src, 0);
2553 if (len)
2554 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2556 /* If the length can be computed at compile-time and is constant
2557 integer, but there are side-effects in src, evaluate
2558 src for side-effects, then return len.
2559 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2560 can be optimized into: i++; x = 3; */
2561 len = c_strlen (src, 1);
2562 if (len && TREE_CODE (len) == INTEGER_CST)
2564 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2565 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2568 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2570 /* If SRC is not a pointer type, don't do this operation inline. */
2571 if (align == 0)
2572 return 0;
2574 /* Bail out if we can't compute strlen in the right mode. */
2575 while (insn_mode != VOIDmode)
2577 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2578 if (icode != CODE_FOR_nothing)
2579 break;
2581 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2583 if (insn_mode == VOIDmode)
2584 return 0;
2586 /* Make a place to write the result of the instruction. */
2587 result = target;
2588 if (! (result != 0
2589 && REG_P (result)
2590 && GET_MODE (result) == insn_mode
2591 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2592 result = gen_reg_rtx (insn_mode);
2594 /* Make a place to hold the source address. We will not expand
2595 the actual source until we are sure that the expansion will
2596 not fail -- there are trees that cannot be expanded twice. */
2597 src_reg = gen_reg_rtx (Pmode);
2599 /* Mark the beginning of the strlen sequence so we can emit the
2600 source operand later. */
2601 before_strlen = get_last_insn ();
2603 char_rtx = const0_rtx;
2604 char_mode = insn_data[(int) icode].operand[2].mode;
2605 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2606 char_mode))
2607 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2609 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2610 char_rtx, GEN_INT (align));
2611 if (! pat)
2612 return 0;
2613 emit_insn (pat);
2615 /* Now that we are assured of success, expand the source. */
2616 start_sequence ();
2617 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2618 if (pat != src_reg)
2619 emit_move_insn (src_reg, pat);
2620 pat = get_insns ();
2621 end_sequence ();
2623 if (before_strlen)
2624 emit_insn_after (pat, before_strlen);
2625 else
2626 emit_insn_before (pat, get_insns ());
2628 /* Return the value in the proper mode for this function. */
2629 if (GET_MODE (result) == target_mode)
2630 target = result;
2631 else if (target != 0)
2632 convert_move (target, result, 0);
2633 else
2634 target = convert_to_mode (target_mode, result, 0);
2636 return target;
2640 /* Expand a call to the strstr builtin. Return 0 if we failed the
2641 caller should emit a normal call, otherwise try to get the result
2642 in TARGET, if convenient (and in mode MODE if that's convenient). */
2644 static rtx
2645 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2647 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2649 tree result = fold_builtin_strstr (arglist, type);
2650 if (result)
2651 return expand_expr (result, target, mode, EXPAND_NORMAL);
2653 return 0;
2656 /* Expand a call to the strchr builtin. Return 0 if we failed the
2657 caller should emit a normal call, otherwise try to get the result
2658 in TARGET, if convenient (and in mode MODE if that's convenient). */
2660 static rtx
2661 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2663 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2665 tree result = fold_builtin_strchr (arglist, type);
2666 if (result)
2667 return expand_expr (result, target, mode, EXPAND_NORMAL);
2669 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2671 return 0;
2674 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2675 caller should emit a normal call, otherwise try to get the result
2676 in TARGET, if convenient (and in mode MODE if that's convenient). */
2678 static rtx
2679 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2681 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2683 tree result = fold_builtin_strrchr (arglist, type);
2684 if (result)
2685 return expand_expr (result, target, mode, EXPAND_NORMAL);
2687 return 0;
2690 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2691 caller should emit a normal call, otherwise try to get the result
2692 in TARGET, if convenient (and in mode MODE if that's convenient). */
2694 static rtx
2695 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2697 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2699 tree result = fold_builtin_strpbrk (arglist, type);
2700 if (result)
2701 return expand_expr (result, target, mode, EXPAND_NORMAL);
2703 return 0;
2706 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2707 bytes from constant string DATA + OFFSET and return it as target
2708 constant. */
2710 static rtx
2711 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2712 enum machine_mode mode)
2714 const char *str = (const char *) data;
2716 gcc_assert (offset >= 0
2717 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2718 <= strlen (str) + 1));
2720 return c_readstr (str + offset, mode);
2723 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2724 Return 0 if we failed, the caller should emit a normal call,
2725 otherwise try to get the result in TARGET, if convenient (and in
2726 mode MODE if that's convenient). */
2727 static rtx
2728 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2730 tree fndecl = get_callee_fndecl (exp);
2731 tree arglist = TREE_OPERAND (exp, 1);
2732 if (!validate_arglist (arglist,
2733 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2734 return 0;
2735 else
2737 tree dest = TREE_VALUE (arglist);
2738 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2739 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2740 const char *src_str;
2741 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2742 unsigned int dest_align
2743 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2744 rtx dest_mem, src_mem, dest_addr, len_rtx;
2745 tree result = fold_builtin_memcpy (fndecl, arglist);
2747 if (result)
2748 return expand_expr (result, target, mode, EXPAND_NORMAL);
2750 /* If DEST is not a pointer type, call the normal function. */
2751 if (dest_align == 0)
2752 return 0;
2754 /* If either SRC is not a pointer type, don't do this
2755 operation in-line. */
2756 if (src_align == 0)
2757 return 0;
2759 dest_mem = get_memory_rtx (dest);
2760 set_mem_align (dest_mem, dest_align);
2761 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2762 src_str = c_getstr (src);
2764 /* If SRC is a string constant and block move would be done
2765 by pieces, we can avoid loading the string from memory
2766 and only stored the computed constants. */
2767 if (src_str
2768 && GET_CODE (len_rtx) == CONST_INT
2769 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2770 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2771 (void *) src_str, dest_align))
2773 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2774 builtin_memcpy_read_str,
2775 (void *) src_str, dest_align, 0);
2776 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2777 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2778 return dest_mem;
2781 src_mem = get_memory_rtx (src);
2782 set_mem_align (src_mem, src_align);
2784 /* Copy word part most expediently. */
2785 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2786 BLOCK_OP_NORMAL);
2788 if (dest_addr == 0)
2790 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2791 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2793 return dest_addr;
2797 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2798 Return 0 if we failed the caller should emit a normal call,
2799 otherwise try to get the result in TARGET, if convenient (and in
2800 mode MODE if that's convenient). If ENDP is 0 return the
2801 destination pointer, if ENDP is 1 return the end pointer ala
2802 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2803 stpcpy. */
2805 static rtx
2806 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2807 int endp)
2809 if (!validate_arglist (arglist,
2810 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2811 return 0;
2812 /* If return value is ignored, transform mempcpy into memcpy. */
2813 else if (target == const0_rtx)
2815 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2817 if (!fn)
2818 return 0;
2820 return expand_expr (build_function_call_expr (fn, arglist),
2821 target, mode, EXPAND_NORMAL);
2823 else
2825 tree dest = TREE_VALUE (arglist);
2826 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2827 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2828 const char *src_str;
2829 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2830 unsigned int dest_align
2831 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2832 rtx dest_mem, src_mem, len_rtx;
2833 tree result = fold_builtin_mempcpy (arglist, type, endp);
2835 if (result)
2836 return expand_expr (result, target, mode, EXPAND_NORMAL);
2838 /* If either SRC or DEST is not a pointer type, don't do this
2839 operation in-line. */
2840 if (dest_align == 0 || src_align == 0)
2841 return 0;
2843 /* If LEN is not constant, call the normal function. */
2844 if (! host_integerp (len, 1))
2845 return 0;
2847 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2848 src_str = c_getstr (src);
2850 /* If SRC is a string constant and block move would be done
2851 by pieces, we can avoid loading the string from memory
2852 and only stored the computed constants. */
2853 if (src_str
2854 && GET_CODE (len_rtx) == CONST_INT
2855 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2856 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2857 (void *) src_str, dest_align))
2859 dest_mem = get_memory_rtx (dest);
2860 set_mem_align (dest_mem, dest_align);
2861 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2862 builtin_memcpy_read_str,
2863 (void *) src_str, dest_align, endp);
2864 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2865 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2866 return dest_mem;
2869 if (GET_CODE (len_rtx) == CONST_INT
2870 && can_move_by_pieces (INTVAL (len_rtx),
2871 MIN (dest_align, src_align)))
2873 dest_mem = get_memory_rtx (dest);
2874 set_mem_align (dest_mem, dest_align);
2875 src_mem = get_memory_rtx (src);
2876 set_mem_align (src_mem, src_align);
2877 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2878 MIN (dest_align, src_align), endp);
2879 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2880 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2881 return dest_mem;
2884 return 0;
2888 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2889 if we failed the caller should emit a normal call. */
2891 static rtx
2892 expand_builtin_memmove (tree arglist, tree type, rtx target,
2893 enum machine_mode mode)
2895 if (!validate_arglist (arglist,
2896 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2897 return 0;
2898 else
2900 tree dest = TREE_VALUE (arglist);
2901 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2902 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2904 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2905 unsigned int dest_align
2906 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2907 tree result = fold_builtin_memmove (arglist, type);
2909 if (result)
2910 return expand_expr (result, target, mode, EXPAND_NORMAL);
2912 /* If DEST is not a pointer type, call the normal function. */
2913 if (dest_align == 0)
2914 return 0;
2916 /* If either SRC is not a pointer type, don't do this
2917 operation in-line. */
2918 if (src_align == 0)
2919 return 0;
2921 /* If src is categorized for a readonly section we can use
2922 normal memcpy. */
2923 if (readonly_data_expr (src))
2925 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2926 if (!fn)
2927 return 0;
2928 return expand_expr (build_function_call_expr (fn, arglist),
2929 target, mode, EXPAND_NORMAL);
2932 /* If length is 1 and we can expand memcpy call inline,
2933 it is ok to use memcpy as well. */
2934 if (integer_onep (len))
2936 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2937 /*endp=*/0);
2938 if (ret)
2939 return ret;
2942 /* Otherwise, call the normal function. */
2943 return 0;
2947 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2948 if we failed the caller should emit a normal call. */
2950 static rtx
2951 expand_builtin_bcopy (tree arglist, tree type)
2953 tree src, dest, size, newarglist;
2955 if (!validate_arglist (arglist,
2956 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2957 return NULL_RTX;
2959 src = TREE_VALUE (arglist);
2960 dest = TREE_VALUE (TREE_CHAIN (arglist));
2961 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2963 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2964 memmove(ptr y, ptr x, size_t z). This is done this way
2965 so that if it isn't expanded inline, we fallback to
2966 calling bcopy instead of memmove. */
2968 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2969 newarglist = tree_cons (NULL_TREE, src, newarglist);
2970 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2972 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2975 #ifndef HAVE_movstr
2976 # define HAVE_movstr 0
2977 # define CODE_FOR_movstr CODE_FOR_nothing
2978 #endif
2980 /* Expand into a movstr instruction, if one is available. Return 0 if
2981 we failed, the caller should emit a normal call, otherwise try to
2982 get the result in TARGET, if convenient. If ENDP is 0 return the
2983 destination pointer, if ENDP is 1 return the end pointer ala
2984 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2985 stpcpy. */
2987 static rtx
2988 expand_movstr (tree dest, tree src, rtx target, int endp)
2990 rtx end;
2991 rtx dest_mem;
2992 rtx src_mem;
2993 rtx insn;
2994 const struct insn_data * data;
2996 if (!HAVE_movstr)
2997 return 0;
2999 dest_mem = get_memory_rtx (dest);
3000 src_mem = get_memory_rtx (src);
3001 if (!endp)
3003 target = force_reg (Pmode, XEXP (dest_mem, 0));
3004 dest_mem = replace_equiv_address (dest_mem, target);
3005 end = gen_reg_rtx (Pmode);
3007 else
3009 if (target == 0 || target == const0_rtx)
3011 end = gen_reg_rtx (Pmode);
3012 if (target == 0)
3013 target = end;
3015 else
3016 end = target;
3019 data = insn_data + CODE_FOR_movstr;
3021 if (data->operand[0].mode != VOIDmode)
3022 end = gen_lowpart (data->operand[0].mode, end);
3024 insn = data->genfun (end, dest_mem, src_mem);
3026 gcc_assert (insn);
3028 emit_insn (insn);
3030 /* movstr is supposed to set end to the address of the NUL
3031 terminator. If the caller requested a mempcpy-like return value,
3032 adjust it. */
3033 if (endp == 1 && target != const0_rtx)
3035 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3036 emit_move_insn (target, force_operand (tem, NULL_RTX));
3039 return target;
3042 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3043 if we failed the caller should emit a normal call, otherwise try to get
3044 the result in TARGET, if convenient (and in mode MODE if that's
3045 convenient). */
3047 static rtx
3048 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
3050 tree fndecl = get_callee_fndecl (exp);
3051 tree arglist = TREE_OPERAND (exp, 1);
3052 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3054 tree result = fold_builtin_strcpy (fndecl, arglist, 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 fndecl = get_callee_fndecl (exp);
3172 tree arglist = TREE_OPERAND (exp, 1);
3173 if (validate_arglist (arglist,
3174 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3176 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3177 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3178 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3180 if (result)
3181 return expand_expr (result, target, mode, EXPAND_NORMAL);
3183 /* We must be passed a constant len and src parameter. */
3184 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3185 return 0;
3187 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3189 /* We're required to pad with trailing zeros if the requested
3190 len is greater than strlen(s2)+1. In that case try to
3191 use store_by_pieces, if it fails, punt. */
3192 if (tree_int_cst_lt (slen, len))
3194 tree dest = TREE_VALUE (arglist);
3195 unsigned int dest_align
3196 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3197 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3198 rtx dest_mem;
3200 if (!p || dest_align == 0 || !host_integerp (len, 1)
3201 || !can_store_by_pieces (tree_low_cst (len, 1),
3202 builtin_strncpy_read_str,
3203 (void *) p, dest_align))
3204 return 0;
3206 dest_mem = get_memory_rtx (dest);
3207 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3208 builtin_strncpy_read_str,
3209 (void *) p, dest_align, 0);
3210 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3211 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3212 return dest_mem;
3215 return 0;
3218 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3219 bytes from constant string DATA + OFFSET and return it as target
3220 constant. */
3222 static rtx
3223 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3224 enum machine_mode mode)
3226 const char *c = (const char *) data;
3227 char *p = alloca (GET_MODE_SIZE (mode));
3229 memset (p, *c, GET_MODE_SIZE (mode));
3231 return c_readstr (p, mode);
3234 /* Callback routine for store_by_pieces. Return the RTL of a register
3235 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3236 char value given in the RTL register data. For example, if mode is
3237 4 bytes wide, return the RTL for 0x01010101*data. */
3239 static rtx
3240 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3241 enum machine_mode mode)
3243 rtx target, coeff;
3244 size_t size;
3245 char *p;
3247 size = GET_MODE_SIZE (mode);
3248 if (size == 1)
3249 return (rtx) data;
3251 p = alloca (size);
3252 memset (p, 1, size);
3253 coeff = c_readstr (p, mode);
3255 target = convert_to_mode (mode, (rtx) data, 1);
3256 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3257 return force_reg (mode, target);
3260 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3261 if we failed the caller should emit a normal call, otherwise try to get
3262 the result in TARGET, if convenient (and in mode MODE if that's
3263 convenient). */
3265 static rtx
3266 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
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);
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);
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);
3351 set_mem_align (dest_mem, dest_align);
3352 dest_addr = clear_storage (dest_mem, len_rtx);
3354 if (dest_addr == 0)
3356 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3357 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3360 return dest_addr;
3364 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3365 if we failed the caller should emit a normal call. */
3367 static rtx
3368 expand_builtin_bzero (tree arglist)
3370 tree dest, size, newarglist;
3372 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3373 return NULL_RTX;
3375 dest = TREE_VALUE (arglist);
3376 size = TREE_VALUE (TREE_CHAIN (arglist));
3378 /* New argument list transforming bzero(ptr x, int y) to
3379 memset(ptr x, int 0, size_t y). This is done this way
3380 so that if it isn't expanded inline, we fallback to
3381 calling bzero instead of memset. */
3383 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3384 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3385 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3387 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3390 /* Expand expression EXP, which is a call to the memcmp built-in function.
3391 ARGLIST is the argument list for this call. Return 0 if we failed and the
3392 caller should emit a normal call, otherwise try to get the result in
3393 TARGET, if convenient (and in mode MODE, if that's convenient). */
3395 static rtx
3396 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3397 enum machine_mode mode)
3399 if (!validate_arglist (arglist,
3400 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3401 return 0;
3402 else
3404 tree result = fold_builtin_memcmp (arglist);
3405 if (result)
3406 return expand_expr (result, target, mode, EXPAND_NORMAL);
3409 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3411 tree arg1 = TREE_VALUE (arglist);
3412 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3413 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3414 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3415 rtx result;
3416 rtx insn;
3418 int arg1_align
3419 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3420 int arg2_align
3421 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3422 enum machine_mode insn_mode;
3424 #ifdef HAVE_cmpmemsi
3425 if (HAVE_cmpmemsi)
3426 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3427 else
3428 #endif
3429 #ifdef HAVE_cmpstrsi
3430 if (HAVE_cmpstrsi)
3431 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3432 else
3433 #endif
3434 return 0;
3436 /* If we don't have POINTER_TYPE, call the function. */
3437 if (arg1_align == 0 || arg2_align == 0)
3438 return 0;
3440 /* Make a place to write the result of the instruction. */
3441 result = target;
3442 if (! (result != 0
3443 && REG_P (result) && GET_MODE (result) == insn_mode
3444 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3445 result = gen_reg_rtx (insn_mode);
3447 arg1_rtx = get_memory_rtx (arg1);
3448 arg2_rtx = get_memory_rtx (arg2);
3449 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3451 /* Set MEM_SIZE as appropriate. */
3452 if (GET_CODE (arg3_rtx) == CONST_INT)
3454 set_mem_size (arg1_rtx, arg3_rtx);
3455 set_mem_size (arg2_rtx, arg3_rtx);
3458 #ifdef HAVE_cmpmemsi
3459 if (HAVE_cmpmemsi)
3460 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3461 GEN_INT (MIN (arg1_align, arg2_align)));
3462 else
3463 #endif
3464 #ifdef HAVE_cmpstrsi
3465 if (HAVE_cmpstrsi)
3466 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3467 GEN_INT (MIN (arg1_align, arg2_align)));
3468 else
3469 #endif
3470 gcc_unreachable ();
3472 if (insn)
3473 emit_insn (insn);
3474 else
3475 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3476 TYPE_MODE (integer_type_node), 3,
3477 XEXP (arg1_rtx, 0), Pmode,
3478 XEXP (arg2_rtx, 0), Pmode,
3479 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3480 TYPE_UNSIGNED (sizetype)),
3481 TYPE_MODE (sizetype));
3483 /* Return the value in the proper mode for this function. */
3484 mode = TYPE_MODE (TREE_TYPE (exp));
3485 if (GET_MODE (result) == mode)
3486 return result;
3487 else if (target != 0)
3489 convert_move (target, result, 0);
3490 return target;
3492 else
3493 return convert_to_mode (mode, result, 0);
3495 #endif
3497 return 0;
3500 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3501 if we failed the caller should emit a normal call, otherwise try to get
3502 the result in TARGET, if convenient. */
3504 static rtx
3505 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3507 tree arglist = TREE_OPERAND (exp, 1);
3509 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3510 return 0;
3511 else
3513 tree result = fold_builtin_strcmp (arglist);
3514 if (result)
3515 return expand_expr (result, target, mode, EXPAND_NORMAL);
3518 #ifdef HAVE_cmpstrsi
3519 if (HAVE_cmpstrsi)
3521 tree arg1 = TREE_VALUE (arglist);
3522 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3523 tree len, len1, len2;
3524 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3525 rtx result, insn;
3526 tree fndecl;
3528 int arg1_align
3529 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3530 int arg2_align
3531 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3532 enum machine_mode insn_mode
3533 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3535 len1 = c_strlen (arg1, 1);
3536 len2 = c_strlen (arg2, 1);
3538 if (len1)
3539 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3540 if (len2)
3541 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3543 /* If we don't have a constant length for the first, use the length
3544 of the second, if we know it. We don't require a constant for
3545 this case; some cost analysis could be done if both are available
3546 but neither is constant. For now, assume they're equally cheap,
3547 unless one has side effects. If both strings have constant lengths,
3548 use the smaller. */
3550 if (!len1)
3551 len = len2;
3552 else if (!len2)
3553 len = len1;
3554 else if (TREE_SIDE_EFFECTS (len1))
3555 len = len2;
3556 else if (TREE_SIDE_EFFECTS (len2))
3557 len = len1;
3558 else if (TREE_CODE (len1) != INTEGER_CST)
3559 len = len2;
3560 else if (TREE_CODE (len2) != INTEGER_CST)
3561 len = len1;
3562 else if (tree_int_cst_lt (len1, len2))
3563 len = len1;
3564 else
3565 len = len2;
3567 /* If both arguments have side effects, we cannot optimize. */
3568 if (!len || TREE_SIDE_EFFECTS (len))
3569 return 0;
3571 /* If we don't have POINTER_TYPE, call the function. */
3572 if (arg1_align == 0 || arg2_align == 0)
3573 return 0;
3575 /* Make a place to write the result of the instruction. */
3576 result = target;
3577 if (! (result != 0
3578 && REG_P (result) && GET_MODE (result) == insn_mode
3579 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3580 result = gen_reg_rtx (insn_mode);
3582 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3583 arg1 = builtin_save_expr (arg1);
3584 arg2 = builtin_save_expr (arg2);
3586 arg1_rtx = get_memory_rtx (arg1);
3587 arg2_rtx = get_memory_rtx (arg2);
3588 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3589 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3590 GEN_INT (MIN (arg1_align, arg2_align)));
3591 if (insn)
3593 emit_insn (insn);
3595 /* Return the value in the proper mode for this function. */
3596 mode = TYPE_MODE (TREE_TYPE (exp));
3597 if (GET_MODE (result) == mode)
3598 return result;
3599 if (target == 0)
3600 return convert_to_mode (mode, result, 0);
3601 convert_move (target, result, 0);
3602 return target;
3605 /* Expand the library call ourselves using a stabilized argument
3606 list to avoid re-evaluating the function's arguments twice. */
3607 arglist = build_tree_list (NULL_TREE, arg2);
3608 arglist = tree_cons (NULL_TREE, arg1, arglist);
3609 fndecl = get_callee_fndecl (exp);
3610 exp = build_function_call_expr (fndecl, arglist);
3611 return expand_call (exp, target, target == const0_rtx);
3613 #endif
3614 return 0;
3617 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3618 if we failed the caller should emit a normal call, otherwise try to get
3619 the result in TARGET, if convenient. */
3621 static rtx
3622 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3624 tree arglist = TREE_OPERAND (exp, 1);
3626 if (!validate_arglist (arglist,
3627 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3628 return 0;
3629 else
3631 tree result = fold_builtin_strncmp (arglist);
3632 if (result)
3633 return expand_expr (result, target, mode, EXPAND_NORMAL);
3636 /* If c_strlen can determine an expression for one of the string
3637 lengths, and it doesn't have side effects, then emit cmpstrsi
3638 using length MIN(strlen(string)+1, arg3). */
3639 #ifdef HAVE_cmpstrsi
3640 if (HAVE_cmpstrsi)
3642 tree arg1 = TREE_VALUE (arglist);
3643 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3644 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3645 tree len, len1, len2;
3646 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3647 rtx result, insn;
3648 tree fndecl;
3650 int arg1_align
3651 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3652 int arg2_align
3653 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3654 enum machine_mode insn_mode
3655 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3657 len1 = c_strlen (arg1, 1);
3658 len2 = c_strlen (arg2, 1);
3660 if (len1)
3661 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3662 if (len2)
3663 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3665 /* If we don't have a constant length for the first, use the length
3666 of the second, if we know it. We don't require a constant for
3667 this case; some cost analysis could be done if both are available
3668 but neither is constant. For now, assume they're equally cheap,
3669 unless one has side effects. If both strings have constant lengths,
3670 use the smaller. */
3672 if (!len1)
3673 len = len2;
3674 else if (!len2)
3675 len = len1;
3676 else if (TREE_SIDE_EFFECTS (len1))
3677 len = len2;
3678 else if (TREE_SIDE_EFFECTS (len2))
3679 len = len1;
3680 else if (TREE_CODE (len1) != INTEGER_CST)
3681 len = len2;
3682 else if (TREE_CODE (len2) != INTEGER_CST)
3683 len = len1;
3684 else if (tree_int_cst_lt (len1, len2))
3685 len = len1;
3686 else
3687 len = len2;
3689 /* If both arguments have side effects, we cannot optimize. */
3690 if (!len || TREE_SIDE_EFFECTS (len))
3691 return 0;
3693 /* The actual new length parameter is MIN(len,arg3). */
3694 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3695 fold_convert (TREE_TYPE (len), arg3)));
3697 /* If we don't have POINTER_TYPE, call the function. */
3698 if (arg1_align == 0 || arg2_align == 0)
3699 return 0;
3701 /* Make a place to write the result of the instruction. */
3702 result = target;
3703 if (! (result != 0
3704 && REG_P (result) && GET_MODE (result) == insn_mode
3705 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3706 result = gen_reg_rtx (insn_mode);
3708 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3709 arg1 = builtin_save_expr (arg1);
3710 arg2 = builtin_save_expr (arg2);
3711 len = builtin_save_expr (len);
3713 arg1_rtx = get_memory_rtx (arg1);
3714 arg2_rtx = get_memory_rtx (arg2);
3715 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3716 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3717 GEN_INT (MIN (arg1_align, arg2_align)));
3718 if (insn)
3720 emit_insn (insn);
3722 /* Return the value in the proper mode for this function. */
3723 mode = TYPE_MODE (TREE_TYPE (exp));
3724 if (GET_MODE (result) == mode)
3725 return result;
3726 if (target == 0)
3727 return convert_to_mode (mode, result, 0);
3728 convert_move (target, result, 0);
3729 return target;
3732 /* Expand the library call ourselves using a stabilized argument
3733 list to avoid re-evaluating the function's arguments twice. */
3734 arglist = build_tree_list (NULL_TREE, len);
3735 arglist = tree_cons (NULL_TREE, arg2, arglist);
3736 arglist = tree_cons (NULL_TREE, arg1, arglist);
3737 fndecl = get_callee_fndecl (exp);
3738 exp = build_function_call_expr (fndecl, arglist);
3739 return expand_call (exp, target, target == const0_rtx);
3741 #endif
3742 return 0;
3745 /* Expand expression EXP, which is a call to the strcat builtin.
3746 Return 0 if we failed the caller should emit a normal call,
3747 otherwise try to get the result in TARGET, if convenient. */
3749 static rtx
3750 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3752 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3753 return 0;
3754 else
3756 tree dst = TREE_VALUE (arglist),
3757 src = TREE_VALUE (TREE_CHAIN (arglist));
3758 const char *p = c_getstr (src);
3760 if (p)
3762 /* If the string length is zero, return the dst parameter. */
3763 if (*p == '\0')
3764 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3765 else if (!optimize_size)
3767 /* Otherwise if !optimize_size, see if we can store by
3768 pieces into (dst + strlen(dst)). */
3769 tree newdst, arglist,
3770 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3772 /* This is the length argument. */
3773 arglist = build_tree_list (NULL_TREE,
3774 fold (size_binop (PLUS_EXPR,
3775 c_strlen (src, 0),
3776 ssize_int (1))));
3777 /* Prepend src argument. */
3778 arglist = tree_cons (NULL_TREE, src, arglist);
3780 /* We're going to use dst more than once. */
3781 dst = builtin_save_expr (dst);
3783 /* Create strlen (dst). */
3784 newdst =
3785 fold (build_function_call_expr (strlen_fn,
3786 build_tree_list (NULL_TREE,
3787 dst)));
3788 /* Create (dst + strlen (dst)). */
3789 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3791 /* Prepend the new dst argument. */
3792 arglist = tree_cons (NULL_TREE, newdst, arglist);
3794 /* We don't want to get turned into a memcpy if the
3795 target is const0_rtx, i.e. when the return value
3796 isn't used. That would produce pessimized code so
3797 pass in a target of zero, it should never actually be
3798 used. If this was successful return the original
3799 dst, not the result of mempcpy. */
3800 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3801 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3802 else
3803 return 0;
3807 return 0;
3811 /* Expand expression EXP, which is a call to the strncat builtin.
3812 Return 0 if we failed the caller should emit a normal call,
3813 otherwise try to get the result in TARGET, if convenient. */
3815 static rtx
3816 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3818 if (validate_arglist (arglist,
3819 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3821 tree result = fold_builtin_strncat (arglist);
3822 if (result)
3823 return expand_expr (result, target, mode, EXPAND_NORMAL);
3825 return 0;
3828 /* Expand expression EXP, which is a call to the strspn builtin.
3829 Return 0 if we failed the caller should emit a normal call,
3830 otherwise try to get the result in TARGET, if convenient. */
3832 static rtx
3833 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3835 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3837 tree result = fold_builtin_strspn (arglist);
3838 if (result)
3839 return expand_expr (result, target, mode, EXPAND_NORMAL);
3841 return 0;
3844 /* Expand expression EXP, which is a call to the strcspn builtin.
3845 Return 0 if we failed the caller should emit a normal call,
3846 otherwise try to get the result in TARGET, if convenient. */
3848 static rtx
3849 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3851 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3853 tree result = fold_builtin_strcspn (arglist);
3854 if (result)
3855 return expand_expr (result, target, mode, EXPAND_NORMAL);
3857 return 0;
3860 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3861 if that's convenient. */
3864 expand_builtin_saveregs (void)
3866 rtx val, seq;
3868 /* Don't do __builtin_saveregs more than once in a function.
3869 Save the result of the first call and reuse it. */
3870 if (saveregs_value != 0)
3871 return saveregs_value;
3873 /* When this function is called, it means that registers must be
3874 saved on entry to this function. So we migrate the call to the
3875 first insn of this function. */
3877 start_sequence ();
3879 /* Do whatever the machine needs done in this case. */
3880 val = targetm.calls.expand_builtin_saveregs ();
3882 seq = get_insns ();
3883 end_sequence ();
3885 saveregs_value = val;
3887 /* Put the insns after the NOTE that starts the function. If this
3888 is inside a start_sequence, make the outer-level insn chain current, so
3889 the code is placed at the start of the function. */
3890 push_topmost_sequence ();
3891 emit_insn_after (seq, entry_of_function ());
3892 pop_topmost_sequence ();
3894 return val;
3897 /* __builtin_args_info (N) returns word N of the arg space info
3898 for the current function. The number and meanings of words
3899 is controlled by the definition of CUMULATIVE_ARGS. */
3901 static rtx
3902 expand_builtin_args_info (tree arglist)
3904 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3905 int *word_ptr = (int *) &current_function_args_info;
3907 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3909 if (arglist != 0)
3911 if (!host_integerp (TREE_VALUE (arglist), 0))
3912 error ("argument of %<__builtin_args_info%> must be constant");
3913 else
3915 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3917 if (wordnum < 0 || wordnum >= nwords)
3918 error ("argument of %<__builtin_args_info%> out of range");
3919 else
3920 return GEN_INT (word_ptr[wordnum]);
3923 else
3924 error ("missing argument in %<__builtin_args_info%>");
3926 return const0_rtx;
3929 /* Expand a call to __builtin_next_arg. */
3931 static rtx
3932 expand_builtin_next_arg (void)
3934 /* Checking arguments is already done in fold_builtin_next_arg
3935 that must be called before this function. */
3936 return expand_binop (Pmode, add_optab,
3937 current_function_internal_arg_pointer,
3938 current_function_arg_offset_rtx,
3939 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3942 /* Make it easier for the backends by protecting the valist argument
3943 from multiple evaluations. */
3945 static tree
3946 stabilize_va_list (tree valist, int needs_lvalue)
3948 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3950 if (TREE_SIDE_EFFECTS (valist))
3951 valist = save_expr (valist);
3953 /* For this case, the backends will be expecting a pointer to
3954 TREE_TYPE (va_list_type_node), but it's possible we've
3955 actually been given an array (an actual va_list_type_node).
3956 So fix it. */
3957 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3959 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3960 valist = build_fold_addr_expr_with_type (valist, p1);
3963 else
3965 tree pt;
3967 if (! needs_lvalue)
3969 if (! TREE_SIDE_EFFECTS (valist))
3970 return valist;
3972 pt = build_pointer_type (va_list_type_node);
3973 valist = fold (build1 (ADDR_EXPR, pt, valist));
3974 TREE_SIDE_EFFECTS (valist) = 1;
3977 if (TREE_SIDE_EFFECTS (valist))
3978 valist = save_expr (valist);
3979 valist = build_fold_indirect_ref (valist);
3982 return valist;
3985 /* The "standard" definition of va_list is void*. */
3987 tree
3988 std_build_builtin_va_list (void)
3990 return ptr_type_node;
3993 /* The "standard" implementation of va_start: just assign `nextarg' to
3994 the variable. */
3996 void
3997 std_expand_builtin_va_start (tree valist, rtx nextarg)
3999 tree t;
4001 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4002 make_tree (ptr_type_node, nextarg));
4003 TREE_SIDE_EFFECTS (t) = 1;
4005 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4008 /* Expand ARGLIST, from a call to __builtin_va_start. */
4010 static rtx
4011 expand_builtin_va_start (tree arglist)
4013 rtx nextarg;
4014 tree chain, valist;
4016 chain = TREE_CHAIN (arglist);
4018 if (!chain)
4020 error ("too few arguments to function %<va_start%>");
4021 return const0_rtx;
4024 if (fold_builtin_next_arg (chain))
4025 return const0_rtx;
4027 nextarg = expand_builtin_next_arg ();
4028 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4030 #ifdef EXPAND_BUILTIN_VA_START
4031 EXPAND_BUILTIN_VA_START (valist, nextarg);
4032 #else
4033 std_expand_builtin_va_start (valist, nextarg);
4034 #endif
4036 return const0_rtx;
4039 /* The "standard" implementation of va_arg: read the value from the
4040 current (padded) address and increment by the (padded) size. */
4042 tree
4043 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4045 tree addr, t, type_size, rounded_size, valist_tmp;
4046 unsigned HOST_WIDE_INT align, boundary;
4047 bool indirect;
4049 #ifdef ARGS_GROW_DOWNWARD
4050 /* All of the alignment and movement below is for args-grow-up machines.
4051 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4052 implement their own specialized gimplify_va_arg_expr routines. */
4053 gcc_unreachable ();
4054 #endif
4056 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4057 if (indirect)
4058 type = build_pointer_type (type);
4060 align = PARM_BOUNDARY / BITS_PER_UNIT;
4061 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4063 /* Hoist the valist value into a temporary for the moment. */
4064 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4066 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4067 requires greater alignment, we must perform dynamic alignment. */
4068 if (boundary > align)
4070 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4071 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4072 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4073 gimplify_and_add (t, pre_p);
4075 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4076 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4077 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4078 gimplify_and_add (t, pre_p);
4080 else
4081 boundary = align;
4083 /* If the actual alignment is less than the alignment of the type,
4084 adjust the type accordingly so that we don't assume strict alignment
4085 when deferencing the pointer. */
4086 boundary *= BITS_PER_UNIT;
4087 if (boundary < TYPE_ALIGN (type))
4089 type = build_variant_type_copy (type);
4090 TYPE_ALIGN (type) = boundary;
4093 /* Compute the rounded size of the type. */
4094 type_size = size_in_bytes (type);
4095 rounded_size = round_up (type_size, align);
4097 /* Reduce rounded_size so it's sharable with the postqueue. */
4098 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4100 /* Get AP. */
4101 addr = valist_tmp;
4102 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4104 /* Small args are padded downward. */
4105 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4106 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4107 size_binop (MINUS_EXPR, rounded_size, type_size)));
4108 t = fold_convert (TREE_TYPE (addr), t);
4109 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4112 /* Compute new value for AP. */
4113 t = fold_convert (TREE_TYPE (valist), rounded_size);
4114 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4115 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4116 gimplify_and_add (t, pre_p);
4118 addr = fold_convert (build_pointer_type (type), addr);
4120 if (indirect)
4121 addr = build_va_arg_indirect_ref (addr);
4123 return build_va_arg_indirect_ref (addr);
4126 /* Build an indirect-ref expression over the given TREE, which represents a
4127 piece of a va_arg() expansion. */
4128 tree
4129 build_va_arg_indirect_ref (tree addr)
4131 addr = build_fold_indirect_ref (addr);
4133 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4134 mf_mark (addr);
4136 return addr;
4139 /* Return a dummy expression of type TYPE in order to keep going after an
4140 error. */
4142 static tree
4143 dummy_object (tree type)
4145 tree t = convert (build_pointer_type (type), null_pointer_node);
4146 return build1 (INDIRECT_REF, type, t);
4149 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4150 builtin function, but a very special sort of operator. */
4152 enum gimplify_status
4153 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4155 tree promoted_type, want_va_type, have_va_type;
4156 tree valist = TREE_OPERAND (*expr_p, 0);
4157 tree type = TREE_TYPE (*expr_p);
4158 tree t;
4160 /* Verify that valist is of the proper type. */
4161 want_va_type = va_list_type_node;
4162 have_va_type = TREE_TYPE (valist);
4164 if (have_va_type == error_mark_node)
4165 return GS_ERROR;
4167 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4169 /* If va_list is an array type, the argument may have decayed
4170 to a pointer type, e.g. by being passed to another function.
4171 In that case, unwrap both types so that we can compare the
4172 underlying records. */
4173 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4174 || POINTER_TYPE_P (have_va_type))
4176 want_va_type = TREE_TYPE (want_va_type);
4177 have_va_type = TREE_TYPE (have_va_type);
4181 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4183 error ("first argument to %<va_arg%> not of type %<va_list%>");
4184 return GS_ERROR;
4187 /* Generate a diagnostic for requesting data of a type that cannot
4188 be passed through `...' due to type promotion at the call site. */
4189 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4190 != type)
4192 static bool gave_help;
4194 /* Unfortunately, this is merely undefined, rather than a constraint
4195 violation, so we cannot make this an error. If this call is never
4196 executed, the program is still strictly conforming. */
4197 warning ("%qT is promoted to %qT when passed through %<...%>",
4198 type, promoted_type);
4199 if (! gave_help)
4201 gave_help = true;
4202 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4203 promoted_type, type);
4206 /* We can, however, treat "undefined" any way we please.
4207 Call abort to encourage the user to fix the program. */
4208 inform ("if this code is reached, the program will abort");
4209 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4210 NULL);
4211 append_to_statement_list (t, pre_p);
4213 /* This is dead code, but go ahead and finish so that the
4214 mode of the result comes out right. */
4215 *expr_p = dummy_object (type);
4216 return GS_ALL_DONE;
4218 else
4220 /* Make it easier for the backends by protecting the valist argument
4221 from multiple evaluations. */
4222 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4224 /* For this case, the backends will be expecting a pointer to
4225 TREE_TYPE (va_list_type_node), but it's possible we've
4226 actually been given an array (an actual va_list_type_node).
4227 So fix it. */
4228 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4230 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4231 valist = build_fold_addr_expr_with_type (valist, p1);
4233 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4235 else
4236 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4238 if (!targetm.gimplify_va_arg_expr)
4239 /* Once most targets are converted this should abort. */
4240 return GS_ALL_DONE;
4242 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4243 return GS_OK;
4247 /* Expand ARGLIST, from a call to __builtin_va_end. */
4249 static rtx
4250 expand_builtin_va_end (tree arglist)
4252 tree valist = TREE_VALUE (arglist);
4254 /* Evaluate for side effects, if needed. I hate macros that don't
4255 do that. */
4256 if (TREE_SIDE_EFFECTS (valist))
4257 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4259 return const0_rtx;
4262 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4263 builtin rather than just as an assignment in stdarg.h because of the
4264 nastiness of array-type va_list types. */
4266 static rtx
4267 expand_builtin_va_copy (tree arglist)
4269 tree dst, src, t;
4271 dst = TREE_VALUE (arglist);
4272 src = TREE_VALUE (TREE_CHAIN (arglist));
4274 dst = stabilize_va_list (dst, 1);
4275 src = stabilize_va_list (src, 0);
4277 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4279 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4280 TREE_SIDE_EFFECTS (t) = 1;
4281 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4283 else
4285 rtx dstb, srcb, size;
4287 /* Evaluate to pointers. */
4288 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4289 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4290 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4291 VOIDmode, EXPAND_NORMAL);
4293 dstb = convert_memory_address (Pmode, dstb);
4294 srcb = convert_memory_address (Pmode, srcb);
4296 /* "Dereference" to BLKmode memories. */
4297 dstb = gen_rtx_MEM (BLKmode, dstb);
4298 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4299 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4300 srcb = gen_rtx_MEM (BLKmode, srcb);
4301 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4302 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4304 /* Copy. */
4305 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4308 return const0_rtx;
4311 /* Expand a call to one of the builtin functions __builtin_frame_address or
4312 __builtin_return_address. */
4314 static rtx
4315 expand_builtin_frame_address (tree fndecl, tree arglist)
4317 /* The argument must be a nonnegative integer constant.
4318 It counts the number of frames to scan up the stack.
4319 The value is the return address saved in that frame. */
4320 if (arglist == 0)
4321 /* Warning about missing arg was already issued. */
4322 return const0_rtx;
4323 else if (! host_integerp (TREE_VALUE (arglist), 1))
4325 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4326 error ("invalid argument to %<__builtin_frame_address%>");
4327 else
4328 error ("invalid argument to %<__builtin_return_address%>");
4329 return const0_rtx;
4331 else
4333 rtx tem
4334 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4335 tree_low_cst (TREE_VALUE (arglist), 1));
4337 /* Some ports cannot access arbitrary stack frames. */
4338 if (tem == NULL)
4340 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4341 warning ("unsupported argument to %<__builtin_frame_address%>");
4342 else
4343 warning ("unsupported argument to %<__builtin_return_address%>");
4344 return const0_rtx;
4347 /* For __builtin_frame_address, return what we've got. */
4348 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4349 return tem;
4351 if (!REG_P (tem)
4352 && ! CONSTANT_P (tem))
4353 tem = copy_to_mode_reg (Pmode, tem);
4354 return tem;
4358 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4359 we failed and the caller should emit a normal call, otherwise try to get
4360 the result in TARGET, if convenient. */
4362 static rtx
4363 expand_builtin_alloca (tree arglist, rtx target)
4365 rtx op0;
4366 rtx result;
4368 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4369 should always expand to function calls. These can be intercepted
4370 in libmudflap. */
4371 if (flag_mudflap)
4372 return 0;
4374 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4375 return 0;
4377 /* Compute the argument. */
4378 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4380 /* Allocate the desired space. */
4381 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4382 result = convert_memory_address (ptr_mode, result);
4384 return result;
4387 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4388 Return 0 if a normal call should be emitted rather than expanding the
4389 function in-line. If convenient, the result should be placed in TARGET.
4390 SUBTARGET may be used as the target for computing one of EXP's operands. */
4392 static rtx
4393 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4394 rtx subtarget, optab op_optab)
4396 rtx op0;
4397 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4398 return 0;
4400 /* Compute the argument. */
4401 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4402 /* Compute op, into TARGET if possible.
4403 Set TARGET to wherever the result comes back. */
4404 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4405 op_optab, op0, target, 1);
4406 gcc_assert (target);
4408 return convert_to_mode (target_mode, target, 0);
4411 /* If the string passed to fputs is a constant and is one character
4412 long, we attempt to transform this call into __builtin_fputc(). */
4414 static rtx
4415 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4417 /* Verify the arguments in the original call. */
4418 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4420 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4421 unlocked, NULL_TREE);
4422 if (result)
4423 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4425 return 0;
4428 /* Expand a call to __builtin_expect. We return our argument and emit a
4429 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4430 a non-jump context. */
4432 static rtx
4433 expand_builtin_expect (tree arglist, rtx target)
4435 tree exp, c;
4436 rtx note, rtx_c;
4438 if (arglist == NULL_TREE
4439 || TREE_CHAIN (arglist) == NULL_TREE)
4440 return const0_rtx;
4441 exp = TREE_VALUE (arglist);
4442 c = TREE_VALUE (TREE_CHAIN (arglist));
4444 if (TREE_CODE (c) != INTEGER_CST)
4446 error ("second argument to %<__builtin_expect%> must be a constant");
4447 c = integer_zero_node;
4450 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4452 /* Don't bother with expected value notes for integral constants. */
4453 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4455 /* We do need to force this into a register so that we can be
4456 moderately sure to be able to correctly interpret the branch
4457 condition later. */
4458 target = force_reg (GET_MODE (target), target);
4460 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4462 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4463 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4466 return target;
4469 /* Like expand_builtin_expect, except do this in a jump context. This is
4470 called from do_jump if the conditional is a __builtin_expect. Return either
4471 a list of insns to emit the jump or NULL if we cannot optimize
4472 __builtin_expect. We need to optimize this at jump time so that machines
4473 like the PowerPC don't turn the test into a SCC operation, and then jump
4474 based on the test being 0/1. */
4477 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4479 tree arglist = TREE_OPERAND (exp, 1);
4480 tree arg0 = TREE_VALUE (arglist);
4481 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4482 rtx ret = NULL_RTX;
4484 /* Only handle __builtin_expect (test, 0) and
4485 __builtin_expect (test, 1). */
4486 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4487 && (integer_zerop (arg1) || integer_onep (arg1)))
4489 rtx insn, drop_through_label, temp;
4491 /* Expand the jump insns. */
4492 start_sequence ();
4493 do_jump (arg0, if_false_label, if_true_label);
4494 ret = get_insns ();
4496 drop_through_label = get_last_insn ();
4497 if (drop_through_label && NOTE_P (drop_through_label))
4498 drop_through_label = prev_nonnote_insn (drop_through_label);
4499 if (drop_through_label && !LABEL_P (drop_through_label))
4500 drop_through_label = NULL_RTX;
4501 end_sequence ();
4503 if (! if_true_label)
4504 if_true_label = drop_through_label;
4505 if (! if_false_label)
4506 if_false_label = drop_through_label;
4508 /* Go through and add the expect's to each of the conditional jumps. */
4509 insn = ret;
4510 while (insn != NULL_RTX)
4512 rtx next = NEXT_INSN (insn);
4514 if (JUMP_P (insn) && any_condjump_p (insn))
4516 rtx ifelse = SET_SRC (pc_set (insn));
4517 rtx then_dest = XEXP (ifelse, 1);
4518 rtx else_dest = XEXP (ifelse, 2);
4519 int taken = -1;
4521 /* First check if we recognize any of the labels. */
4522 if (GET_CODE (then_dest) == LABEL_REF
4523 && XEXP (then_dest, 0) == if_true_label)
4524 taken = 1;
4525 else if (GET_CODE (then_dest) == LABEL_REF
4526 && XEXP (then_dest, 0) == if_false_label)
4527 taken = 0;
4528 else if (GET_CODE (else_dest) == LABEL_REF
4529 && XEXP (else_dest, 0) == if_false_label)
4530 taken = 1;
4531 else if (GET_CODE (else_dest) == LABEL_REF
4532 && XEXP (else_dest, 0) == if_true_label)
4533 taken = 0;
4534 /* Otherwise check where we drop through. */
4535 else if (else_dest == pc_rtx)
4537 if (next && NOTE_P (next))
4538 next = next_nonnote_insn (next);
4540 if (next && JUMP_P (next)
4541 && any_uncondjump_p (next))
4542 temp = XEXP (SET_SRC (pc_set (next)), 0);
4543 else
4544 temp = next;
4546 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4547 else that can't possibly match either target label. */
4548 if (temp == if_false_label)
4549 taken = 1;
4550 else if (temp == if_true_label)
4551 taken = 0;
4553 else if (then_dest == pc_rtx)
4555 if (next && NOTE_P (next))
4556 next = next_nonnote_insn (next);
4558 if (next && JUMP_P (next)
4559 && any_uncondjump_p (next))
4560 temp = XEXP (SET_SRC (pc_set (next)), 0);
4561 else
4562 temp = next;
4564 if (temp == if_false_label)
4565 taken = 0;
4566 else if (temp == if_true_label)
4567 taken = 1;
4570 if (taken != -1)
4572 /* If the test is expected to fail, reverse the
4573 probabilities. */
4574 if (integer_zerop (arg1))
4575 taken = 1 - taken;
4576 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4580 insn = next;
4584 return ret;
4587 static void
4588 expand_builtin_trap (void)
4590 #ifdef HAVE_trap
4591 if (HAVE_trap)
4592 emit_insn (gen_trap ());
4593 else
4594 #endif
4595 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4596 emit_barrier ();
4599 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4600 Return 0 if a normal call should be emitted rather than expanding
4601 the function inline. If convenient, the result should be placed
4602 in TARGET. SUBTARGET may be used as the target for computing
4603 the operand. */
4605 static rtx
4606 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4608 enum machine_mode mode;
4609 tree arg;
4610 rtx op0;
4612 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4613 return 0;
4615 arg = TREE_VALUE (arglist);
4616 mode = TYPE_MODE (TREE_TYPE (arg));
4617 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4618 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4621 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4622 Return NULL is a normal call should be emitted rather than expanding the
4623 function inline. If convenient, the result should be placed in TARGET.
4624 SUBTARGET may be used as the target for computing the operand. */
4626 static rtx
4627 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4629 rtx op0, op1;
4630 tree arg;
4632 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4633 return 0;
4635 arg = TREE_VALUE (arglist);
4636 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4638 arg = TREE_VALUE (TREE_CHAIN (arglist));
4639 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4641 return expand_copysign (op0, op1, target);
4644 /* Create a new constant string literal and return a char* pointer to it.
4645 The STRING_CST value is the LEN characters at STR. */
4646 static tree
4647 build_string_literal (int len, const char *str)
4649 tree t, elem, index, type;
4651 t = build_string (len, str);
4652 elem = build_type_variant (char_type_node, 1, 0);
4653 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4654 type = build_array_type (elem, index);
4655 TREE_TYPE (t) = type;
4656 TREE_CONSTANT (t) = 1;
4657 TREE_INVARIANT (t) = 1;
4658 TREE_READONLY (t) = 1;
4659 TREE_STATIC (t) = 1;
4661 type = build_pointer_type (type);
4662 t = build1 (ADDR_EXPR, type, t);
4664 type = build_pointer_type (elem);
4665 t = build1 (NOP_EXPR, type, t);
4666 return t;
4669 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4670 Return 0 if a normal call should be emitted rather than transforming
4671 the function inline. If convenient, the result should be placed in
4672 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4673 call. */
4674 static rtx
4675 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4676 bool unlocked)
4678 tree fn_putchar = unlocked
4679 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4680 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4681 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4682 : implicit_built_in_decls[BUILT_IN_PUTS];
4683 const char *fmt_str;
4684 tree fn, fmt, arg;
4686 /* If the return value is used, don't do the transformation. */
4687 if (target != const0_rtx)
4688 return 0;
4690 /* Verify the required arguments in the original call. */
4691 if (! arglist)
4692 return 0;
4693 fmt = TREE_VALUE (arglist);
4694 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4695 return 0;
4696 arglist = TREE_CHAIN (arglist);
4698 /* Check whether the format is a literal string constant. */
4699 fmt_str = c_getstr (fmt);
4700 if (fmt_str == NULL)
4701 return 0;
4703 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4704 if (strcmp (fmt_str, "%s\n") == 0)
4706 if (! arglist
4707 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4708 || TREE_CHAIN (arglist))
4709 return 0;
4710 fn = fn_puts;
4712 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4713 else if (strcmp (fmt_str, "%c") == 0)
4715 if (! arglist
4716 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4717 || TREE_CHAIN (arglist))
4718 return 0;
4719 fn = fn_putchar;
4721 else
4723 /* We can't handle anything else with % args or %% ... yet. */
4724 if (strchr (fmt_str, '%'))
4725 return 0;
4727 if (arglist)
4728 return 0;
4730 /* If the format specifier was "", printf does nothing. */
4731 if (fmt_str[0] == '\0')
4732 return const0_rtx;
4733 /* If the format specifier has length of 1, call putchar. */
4734 if (fmt_str[1] == '\0')
4736 /* Given printf("c"), (where c is any one character,)
4737 convert "c"[0] to an int and pass that to the replacement
4738 function. */
4739 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4740 arglist = build_tree_list (NULL_TREE, arg);
4741 fn = fn_putchar;
4743 else
4745 /* If the format specifier was "string\n", call puts("string"). */
4746 size_t len = strlen (fmt_str);
4747 if (fmt_str[len - 1] == '\n')
4749 /* Create a NUL-terminated string that's one char shorter
4750 than the original, stripping off the trailing '\n'. */
4751 char *newstr = alloca (len);
4752 memcpy (newstr, fmt_str, len - 1);
4753 newstr[len - 1] = 0;
4755 arg = build_string_literal (len, newstr);
4756 arglist = build_tree_list (NULL_TREE, arg);
4757 fn = fn_puts;
4759 else
4760 /* We'd like to arrange to call fputs(string,stdout) here,
4761 but we need stdout and don't have a way to get it yet. */
4762 return 0;
4766 if (!fn)
4767 return 0;
4768 return expand_expr (build_function_call_expr (fn, arglist),
4769 target, mode, EXPAND_NORMAL);
4772 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4773 Return 0 if a normal call should be emitted rather than transforming
4774 the function inline. If convenient, the result should be placed in
4775 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4776 call. */
4777 static rtx
4778 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4779 bool unlocked)
4781 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4782 : implicit_built_in_decls[BUILT_IN_FPUTC];
4783 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4784 : implicit_built_in_decls[BUILT_IN_FPUTS];
4785 const char *fmt_str;
4786 tree fn, fmt, fp, arg;
4788 /* If the return value is used, don't do the transformation. */
4789 if (target != const0_rtx)
4790 return 0;
4792 /* Verify the required arguments in the original call. */
4793 if (! arglist)
4794 return 0;
4795 fp = TREE_VALUE (arglist);
4796 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4797 return 0;
4798 arglist = TREE_CHAIN (arglist);
4799 if (! arglist)
4800 return 0;
4801 fmt = TREE_VALUE (arglist);
4802 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4803 return 0;
4804 arglist = TREE_CHAIN (arglist);
4806 /* Check whether the format is a literal string constant. */
4807 fmt_str = c_getstr (fmt);
4808 if (fmt_str == NULL)
4809 return 0;
4811 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4812 if (strcmp (fmt_str, "%s") == 0)
4814 if (! arglist
4815 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4816 || TREE_CHAIN (arglist))
4817 return 0;
4818 arg = TREE_VALUE (arglist);
4819 arglist = build_tree_list (NULL_TREE, fp);
4820 arglist = tree_cons (NULL_TREE, arg, arglist);
4821 fn = fn_fputs;
4823 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4824 else if (strcmp (fmt_str, "%c") == 0)
4826 if (! arglist
4827 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4828 || TREE_CHAIN (arglist))
4829 return 0;
4830 arg = TREE_VALUE (arglist);
4831 arglist = build_tree_list (NULL_TREE, fp);
4832 arglist = tree_cons (NULL_TREE, arg, arglist);
4833 fn = fn_fputc;
4835 else
4837 /* We can't handle anything else with % args or %% ... yet. */
4838 if (strchr (fmt_str, '%'))
4839 return 0;
4841 if (arglist)
4842 return 0;
4844 /* If the format specifier was "", fprintf does nothing. */
4845 if (fmt_str[0] == '\0')
4847 /* Evaluate and ignore FILE* argument for side-effects. */
4848 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4849 return const0_rtx;
4852 /* When "string" doesn't contain %, replace all cases of
4853 fprintf(stream,string) with fputs(string,stream). The fputs
4854 builtin will take care of special cases like length == 1. */
4855 arglist = build_tree_list (NULL_TREE, fp);
4856 arglist = tree_cons (NULL_TREE, fmt, arglist);
4857 fn = fn_fputs;
4860 if (!fn)
4861 return 0;
4862 return expand_expr (build_function_call_expr (fn, arglist),
4863 target, mode, EXPAND_NORMAL);
4866 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4867 a normal call should be emitted rather than expanding the function
4868 inline. If convenient, the result should be placed in TARGET with
4869 mode MODE. */
4871 static rtx
4872 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4874 tree orig_arglist, dest, fmt;
4875 const char *fmt_str;
4877 orig_arglist = arglist;
4879 /* Verify the required arguments in the original call. */
4880 if (! arglist)
4881 return 0;
4882 dest = TREE_VALUE (arglist);
4883 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4884 return 0;
4885 arglist = TREE_CHAIN (arglist);
4886 if (! arglist)
4887 return 0;
4888 fmt = TREE_VALUE (arglist);
4889 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4890 return 0;
4891 arglist = TREE_CHAIN (arglist);
4893 /* Check whether the format is a literal string constant. */
4894 fmt_str = c_getstr (fmt);
4895 if (fmt_str == NULL)
4896 return 0;
4898 /* If the format doesn't contain % args or %%, use strcpy. */
4899 if (strchr (fmt_str, '%') == 0)
4901 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4902 tree exp;
4904 if (arglist || ! fn)
4905 return 0;
4906 expand_expr (build_function_call_expr (fn, orig_arglist),
4907 const0_rtx, VOIDmode, EXPAND_NORMAL);
4908 if (target == const0_rtx)
4909 return const0_rtx;
4910 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4911 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4913 /* If the format is "%s", use strcpy if the result isn't used. */
4914 else if (strcmp (fmt_str, "%s") == 0)
4916 tree fn, arg, len;
4917 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4919 if (! fn)
4920 return 0;
4922 if (! arglist || TREE_CHAIN (arglist))
4923 return 0;
4924 arg = TREE_VALUE (arglist);
4925 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4926 return 0;
4928 if (target != const0_rtx)
4930 len = c_strlen (arg, 1);
4931 if (! len || TREE_CODE (len) != INTEGER_CST)
4932 return 0;
4934 else
4935 len = NULL_TREE;
4937 arglist = build_tree_list (NULL_TREE, arg);
4938 arglist = tree_cons (NULL_TREE, dest, arglist);
4939 expand_expr (build_function_call_expr (fn, arglist),
4940 const0_rtx, VOIDmode, EXPAND_NORMAL);
4942 if (target == const0_rtx)
4943 return const0_rtx;
4944 return expand_expr (len, target, mode, EXPAND_NORMAL);
4947 return 0;
4950 /* Expand a call to either the entry or exit function profiler. */
4952 static rtx
4953 expand_builtin_profile_func (bool exitp)
4955 rtx this, which;
4957 this = DECL_RTL (current_function_decl);
4958 gcc_assert (MEM_P (this));
4959 this = XEXP (this, 0);
4961 if (exitp)
4962 which = profile_function_exit_libfunc;
4963 else
4964 which = profile_function_entry_libfunc;
4966 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4967 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4969 Pmode);
4971 return const0_rtx;
4974 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4976 static rtx
4977 round_trampoline_addr (rtx tramp)
4979 rtx temp, addend, mask;
4981 /* If we don't need too much alignment, we'll have been guaranteed
4982 proper alignment by get_trampoline_type. */
4983 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4984 return tramp;
4986 /* Round address up to desired boundary. */
4987 temp = gen_reg_rtx (Pmode);
4988 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4989 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4991 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4992 temp, 0, OPTAB_LIB_WIDEN);
4993 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4994 temp, 0, OPTAB_LIB_WIDEN);
4996 return tramp;
4999 static rtx
5000 expand_builtin_init_trampoline (tree arglist)
5002 tree t_tramp, t_func, t_chain;
5003 rtx r_tramp, r_func, r_chain;
5004 #ifdef TRAMPOLINE_TEMPLATE
5005 rtx blktramp;
5006 #endif
5008 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5009 POINTER_TYPE, VOID_TYPE))
5010 return NULL_RTX;
5012 t_tramp = TREE_VALUE (arglist);
5013 arglist = TREE_CHAIN (arglist);
5014 t_func = TREE_VALUE (arglist);
5015 arglist = TREE_CHAIN (arglist);
5016 t_chain = TREE_VALUE (arglist);
5018 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
5019 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
5020 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
5022 /* Generate insns to initialize the trampoline. */
5023 r_tramp = round_trampoline_addr (r_tramp);
5024 #ifdef TRAMPOLINE_TEMPLATE
5025 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5026 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5027 emit_block_move (blktramp, assemble_trampoline_template (),
5028 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5029 #endif
5030 trampolines_created = 1;
5031 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5033 return const0_rtx;
5036 static rtx
5037 expand_builtin_adjust_trampoline (tree arglist)
5039 rtx tramp;
5041 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5042 return NULL_RTX;
5044 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5045 tramp = round_trampoline_addr (tramp);
5046 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5047 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5048 #endif
5050 return tramp;
5053 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5054 Return NULL_RTX if a normal call should be emitted rather than expanding
5055 the function in-line. EXP is the expression that is a call to the builtin
5056 function; if convenient, the result should be placed in TARGET. */
5058 static rtx
5059 expand_builtin_signbit (tree exp, rtx target)
5061 const struct real_format *fmt;
5062 enum machine_mode fmode, imode, rmode;
5063 HOST_WIDE_INT hi, lo;
5064 tree arg, arglist;
5065 int word, bitpos;
5066 rtx temp;
5068 arglist = TREE_OPERAND (exp, 1);
5069 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5070 return 0;
5072 arg = TREE_VALUE (arglist);
5073 fmode = TYPE_MODE (TREE_TYPE (arg));
5074 rmode = TYPE_MODE (TREE_TYPE (exp));
5075 fmt = REAL_MODE_FORMAT (fmode);
5077 /* For floating point formats without a sign bit, implement signbit
5078 as "ARG < 0.0". */
5079 bitpos = fmt->signbit_ro;
5080 if (bitpos < 0)
5082 /* But we can't do this if the format supports signed zero. */
5083 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5084 return 0;
5086 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
5087 build_real (TREE_TYPE (arg), dconst0)));
5088 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5091 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
5092 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5094 imode = int_mode_for_mode (fmode);
5095 if (imode == BLKmode)
5096 return 0;
5097 temp = gen_lowpart (imode, temp);
5099 else
5101 imode = word_mode;
5102 /* Handle targets with different FP word orders. */
5103 if (FLOAT_WORDS_BIG_ENDIAN)
5104 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5105 else
5106 word = bitpos / BITS_PER_WORD;
5107 temp = operand_subword_force (temp, word, fmode);
5108 bitpos = bitpos % BITS_PER_WORD;
5111 /* Force the intermediate word_mode (or narrower) result into a
5112 register. This avoids attempting to create paradoxical SUBREGs
5113 of floating point modes below. */
5114 temp = force_reg (imode, temp);
5116 /* If the bitpos is within the "result mode" lowpart, the operation
5117 can be implement with a single bitwise AND. Otherwise, we need
5118 a right shift and an AND. */
5120 if (bitpos < GET_MODE_BITSIZE (rmode))
5122 if (bitpos < HOST_BITS_PER_WIDE_INT)
5124 hi = 0;
5125 lo = (HOST_WIDE_INT) 1 << bitpos;
5127 else
5129 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5130 lo = 0;
5133 if (imode != rmode)
5134 temp = gen_lowpart (rmode, temp);
5135 temp = expand_binop (rmode, and_optab, temp,
5136 immed_double_const (lo, hi, rmode),
5137 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5139 else
5141 /* Perform a logical right shift to place the signbit in the least
5142 significant bit, then truncate the result to the desired mode
5143 and mask just this bit. */
5144 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5145 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5146 temp = gen_lowpart (rmode, temp);
5147 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5148 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5151 return temp;
5154 /* Expand fork or exec calls. TARGET is the desired target of the
5155 call. ARGLIST is the list of arguments of the call. FN is the
5156 identificator of the actual function. IGNORE is nonzero if the
5157 value is to be ignored. */
5159 static rtx
5160 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5162 tree id, decl;
5163 tree call;
5165 /* If we are not profiling, just call the function. */
5166 if (!profile_arc_flag)
5167 return NULL_RTX;
5169 /* Otherwise call the wrapper. This should be equivalent for the rest of
5170 compiler, so the code does not diverge, and the wrapper may run the
5171 code necessary for keeping the profiling sane. */
5173 switch (DECL_FUNCTION_CODE (fn))
5175 case BUILT_IN_FORK:
5176 id = get_identifier ("__gcov_fork");
5177 break;
5179 case BUILT_IN_EXECL:
5180 id = get_identifier ("__gcov_execl");
5181 break;
5183 case BUILT_IN_EXECV:
5184 id = get_identifier ("__gcov_execv");
5185 break;
5187 case BUILT_IN_EXECLP:
5188 id = get_identifier ("__gcov_execlp");
5189 break;
5191 case BUILT_IN_EXECLE:
5192 id = get_identifier ("__gcov_execle");
5193 break;
5195 case BUILT_IN_EXECVP:
5196 id = get_identifier ("__gcov_execvp");
5197 break;
5199 case BUILT_IN_EXECVE:
5200 id = get_identifier ("__gcov_execve");
5201 break;
5203 default:
5204 gcc_unreachable ();
5207 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5208 DECL_EXTERNAL (decl) = 1;
5209 TREE_PUBLIC (decl) = 1;
5210 DECL_ARTIFICIAL (decl) = 1;
5211 TREE_NOTHROW (decl) = 1;
5212 call = build_function_call_expr (decl, arglist);
5214 return expand_call (call, target, ignore);
5217 /* Expand an expression EXP that calls a built-in function,
5218 with result going to TARGET if that's convenient
5219 (and in mode MODE if that's convenient).
5220 SUBTARGET may be used as the target for computing one of EXP's operands.
5221 IGNORE is nonzero if the value is to be ignored. */
5224 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5225 int ignore)
5227 tree fndecl = get_callee_fndecl (exp);
5228 tree arglist = TREE_OPERAND (exp, 1);
5229 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5230 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5232 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5233 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5235 /* When not optimizing, generate calls to library functions for a certain
5236 set of builtins. */
5237 if (!optimize
5238 && !CALLED_AS_BUILT_IN (fndecl)
5239 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5240 && fcode != BUILT_IN_ALLOCA)
5241 return expand_call (exp, target, ignore);
5243 /* The built-in function expanders test for target == const0_rtx
5244 to determine whether the function's result will be ignored. */
5245 if (ignore)
5246 target = const0_rtx;
5248 /* If the result of a pure or const built-in function is ignored, and
5249 none of its arguments are volatile, we can avoid expanding the
5250 built-in call and just evaluate the arguments for side-effects. */
5251 if (target == const0_rtx
5252 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5254 bool volatilep = false;
5255 tree arg;
5257 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5258 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5260 volatilep = true;
5261 break;
5264 if (! volatilep)
5266 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5267 expand_expr (TREE_VALUE (arg), const0_rtx,
5268 VOIDmode, EXPAND_NORMAL);
5269 return const0_rtx;
5273 switch (fcode)
5275 case BUILT_IN_FABS:
5276 case BUILT_IN_FABSF:
5277 case BUILT_IN_FABSL:
5278 target = expand_builtin_fabs (arglist, target, subtarget);
5279 if (target)
5280 return target;
5281 break;
5283 case BUILT_IN_COPYSIGN:
5284 case BUILT_IN_COPYSIGNF:
5285 case BUILT_IN_COPYSIGNL:
5286 target = expand_builtin_copysign (arglist, target, subtarget);
5287 if (target)
5288 return target;
5289 break;
5291 /* Just do a normal library call if we were unable to fold
5292 the values. */
5293 case BUILT_IN_CABS:
5294 case BUILT_IN_CABSF:
5295 case BUILT_IN_CABSL:
5296 break;
5298 case BUILT_IN_EXP:
5299 case BUILT_IN_EXPF:
5300 case BUILT_IN_EXPL:
5301 case BUILT_IN_EXP10:
5302 case BUILT_IN_EXP10F:
5303 case BUILT_IN_EXP10L:
5304 case BUILT_IN_POW10:
5305 case BUILT_IN_POW10F:
5306 case BUILT_IN_POW10L:
5307 case BUILT_IN_EXP2:
5308 case BUILT_IN_EXP2F:
5309 case BUILT_IN_EXP2L:
5310 case BUILT_IN_EXPM1:
5311 case BUILT_IN_EXPM1F:
5312 case BUILT_IN_EXPM1L:
5313 case BUILT_IN_LOGB:
5314 case BUILT_IN_LOGBF:
5315 case BUILT_IN_LOGBL:
5316 case BUILT_IN_ILOGB:
5317 case BUILT_IN_ILOGBF:
5318 case BUILT_IN_ILOGBL:
5319 case BUILT_IN_LOG:
5320 case BUILT_IN_LOGF:
5321 case BUILT_IN_LOGL:
5322 case BUILT_IN_LOG10:
5323 case BUILT_IN_LOG10F:
5324 case BUILT_IN_LOG10L:
5325 case BUILT_IN_LOG2:
5326 case BUILT_IN_LOG2F:
5327 case BUILT_IN_LOG2L:
5328 case BUILT_IN_LOG1P:
5329 case BUILT_IN_LOG1PF:
5330 case BUILT_IN_LOG1PL:
5331 case BUILT_IN_TAN:
5332 case BUILT_IN_TANF:
5333 case BUILT_IN_TANL:
5334 case BUILT_IN_ASIN:
5335 case BUILT_IN_ASINF:
5336 case BUILT_IN_ASINL:
5337 case BUILT_IN_ACOS:
5338 case BUILT_IN_ACOSF:
5339 case BUILT_IN_ACOSL:
5340 case BUILT_IN_ATAN:
5341 case BUILT_IN_ATANF:
5342 case BUILT_IN_ATANL:
5343 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5344 because of possible accuracy problems. */
5345 if (! flag_unsafe_math_optimizations)
5346 break;
5347 case BUILT_IN_SQRT:
5348 case BUILT_IN_SQRTF:
5349 case BUILT_IN_SQRTL:
5350 case BUILT_IN_FLOOR:
5351 case BUILT_IN_FLOORF:
5352 case BUILT_IN_FLOORL:
5353 case BUILT_IN_CEIL:
5354 case BUILT_IN_CEILF:
5355 case BUILT_IN_CEILL:
5356 case BUILT_IN_TRUNC:
5357 case BUILT_IN_TRUNCF:
5358 case BUILT_IN_TRUNCL:
5359 case BUILT_IN_ROUND:
5360 case BUILT_IN_ROUNDF:
5361 case BUILT_IN_ROUNDL:
5362 case BUILT_IN_NEARBYINT:
5363 case BUILT_IN_NEARBYINTF:
5364 case BUILT_IN_NEARBYINTL:
5365 case BUILT_IN_RINT:
5366 case BUILT_IN_RINTF:
5367 case BUILT_IN_RINTL:
5368 case BUILT_IN_LRINT:
5369 case BUILT_IN_LRINTF:
5370 case BUILT_IN_LRINTL:
5371 case BUILT_IN_LLRINT:
5372 case BUILT_IN_LLRINTF:
5373 case BUILT_IN_LLRINTL:
5374 target = expand_builtin_mathfn (exp, target, subtarget);
5375 if (target)
5376 return target;
5377 break;
5379 case BUILT_IN_LFLOOR:
5380 case BUILT_IN_LFLOORF:
5381 case BUILT_IN_LFLOORL:
5382 case BUILT_IN_LLFLOOR:
5383 case BUILT_IN_LLFLOORF:
5384 case BUILT_IN_LLFLOORL:
5385 target = expand_builtin_int_roundingfn (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, mode);
5661 if (target)
5662 return target;
5663 break;
5665 case BUILT_IN_BCOPY:
5666 target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5667 if (target)
5668 return target;
5669 break;
5671 case BUILT_IN_MEMSET:
5672 target = expand_builtin_memset (arglist, target, mode);
5673 if (target)
5674 return target;
5675 break;
5677 case BUILT_IN_BZERO:
5678 target = expand_builtin_bzero (arglist);
5679 if (target)
5680 return target;
5681 break;
5683 case BUILT_IN_STRCMP:
5684 target = expand_builtin_strcmp (exp, target, mode);
5685 if (target)
5686 return target;
5687 break;
5689 case BUILT_IN_STRNCMP:
5690 target = expand_builtin_strncmp (exp, target, mode);
5691 if (target)
5692 return target;
5693 break;
5695 case BUILT_IN_BCMP:
5696 case BUILT_IN_MEMCMP:
5697 target = expand_builtin_memcmp (exp, arglist, target, mode);
5698 if (target)
5699 return target;
5700 break;
5702 case BUILT_IN_SETJMP:
5703 target = expand_builtin_setjmp (arglist, target);
5704 if (target)
5705 return target;
5706 break;
5708 /* __builtin_longjmp is passed a pointer to an array of five words.
5709 It's similar to the C library longjmp function but works with
5710 __builtin_setjmp above. */
5711 case BUILT_IN_LONGJMP:
5712 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5713 break;
5714 else
5716 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5717 VOIDmode, 0);
5718 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5719 NULL_RTX, VOIDmode, 0);
5721 if (value != const1_rtx)
5723 error ("%<__builtin_longjmp%> second argument must be 1");
5724 return const0_rtx;
5727 expand_builtin_longjmp (buf_addr, value);
5728 return const0_rtx;
5731 case BUILT_IN_NONLOCAL_GOTO:
5732 target = expand_builtin_nonlocal_goto (arglist);
5733 if (target)
5734 return target;
5735 break;
5737 /* This updates the setjmp buffer that is its argument with the value
5738 of the current stack pointer. */
5739 case BUILT_IN_UPDATE_SETJMP_BUF:
5740 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5742 rtx buf_addr
5743 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5745 expand_builtin_update_setjmp_buf (buf_addr);
5746 return const0_rtx;
5748 break;
5750 case BUILT_IN_TRAP:
5751 expand_builtin_trap ();
5752 return const0_rtx;
5754 case BUILT_IN_PRINTF:
5755 target = expand_builtin_printf (arglist, target, mode, false);
5756 if (target)
5757 return target;
5758 break;
5760 case BUILT_IN_PRINTF_UNLOCKED:
5761 target = expand_builtin_printf (arglist, target, mode, true);
5762 if (target)
5763 return target;
5764 break;
5766 case BUILT_IN_FPUTS:
5767 target = expand_builtin_fputs (arglist, target, false);
5768 if (target)
5769 return target;
5770 break;
5771 case BUILT_IN_FPUTS_UNLOCKED:
5772 target = expand_builtin_fputs (arglist, target, true);
5773 if (target)
5774 return target;
5775 break;
5777 case BUILT_IN_FPRINTF:
5778 target = expand_builtin_fprintf (arglist, target, mode, false);
5779 if (target)
5780 return target;
5781 break;
5783 case BUILT_IN_FPRINTF_UNLOCKED:
5784 target = expand_builtin_fprintf (arglist, target, mode, true);
5785 if (target)
5786 return target;
5787 break;
5789 case BUILT_IN_SPRINTF:
5790 target = expand_builtin_sprintf (arglist, target, mode);
5791 if (target)
5792 return target;
5793 break;
5795 case BUILT_IN_SIGNBIT:
5796 case BUILT_IN_SIGNBITF:
5797 case BUILT_IN_SIGNBITL:
5798 target = expand_builtin_signbit (exp, target);
5799 if (target)
5800 return target;
5801 break;
5803 /* Various hooks for the DWARF 2 __throw routine. */
5804 case BUILT_IN_UNWIND_INIT:
5805 expand_builtin_unwind_init ();
5806 return const0_rtx;
5807 case BUILT_IN_DWARF_CFA:
5808 return virtual_cfa_rtx;
5809 #ifdef DWARF2_UNWIND_INFO
5810 case BUILT_IN_DWARF_SP_COLUMN:
5811 return expand_builtin_dwarf_sp_column ();
5812 case BUILT_IN_INIT_DWARF_REG_SIZES:
5813 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5814 return const0_rtx;
5815 #endif
5816 case BUILT_IN_FROB_RETURN_ADDR:
5817 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5818 case BUILT_IN_EXTRACT_RETURN_ADDR:
5819 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5820 case BUILT_IN_EH_RETURN:
5821 expand_builtin_eh_return (TREE_VALUE (arglist),
5822 TREE_VALUE (TREE_CHAIN (arglist)));
5823 return const0_rtx;
5824 #ifdef EH_RETURN_DATA_REGNO
5825 case BUILT_IN_EH_RETURN_DATA_REGNO:
5826 return expand_builtin_eh_return_data_regno (arglist);
5827 #endif
5828 case BUILT_IN_EXTEND_POINTER:
5829 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5831 case BUILT_IN_VA_START:
5832 case BUILT_IN_STDARG_START:
5833 return expand_builtin_va_start (arglist);
5834 case BUILT_IN_VA_END:
5835 return expand_builtin_va_end (arglist);
5836 case BUILT_IN_VA_COPY:
5837 return expand_builtin_va_copy (arglist);
5838 case BUILT_IN_EXPECT:
5839 return expand_builtin_expect (arglist, target);
5840 case BUILT_IN_PREFETCH:
5841 expand_builtin_prefetch (arglist);
5842 return const0_rtx;
5844 case BUILT_IN_PROFILE_FUNC_ENTER:
5845 return expand_builtin_profile_func (false);
5846 case BUILT_IN_PROFILE_FUNC_EXIT:
5847 return expand_builtin_profile_func (true);
5849 case BUILT_IN_INIT_TRAMPOLINE:
5850 return expand_builtin_init_trampoline (arglist);
5851 case BUILT_IN_ADJUST_TRAMPOLINE:
5852 return expand_builtin_adjust_trampoline (arglist);
5854 case BUILT_IN_FORK:
5855 case BUILT_IN_EXECL:
5856 case BUILT_IN_EXECV:
5857 case BUILT_IN_EXECLP:
5858 case BUILT_IN_EXECLE:
5859 case BUILT_IN_EXECVP:
5860 case BUILT_IN_EXECVE:
5861 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5862 if (target)
5863 return target;
5864 break;
5866 default: /* just do library call, if unknown builtin */
5867 break;
5870 /* The switch statement above can drop through to cause the function
5871 to be called normally. */
5872 return expand_call (exp, target, ignore);
5875 /* Determine whether a tree node represents a call to a built-in
5876 function. If the tree T is a call to a built-in function with
5877 the right number of arguments of the appropriate types, return
5878 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5879 Otherwise the return value is END_BUILTINS. */
5881 enum built_in_function
5882 builtin_mathfn_code (tree t)
5884 tree fndecl, arglist, parmlist;
5885 tree argtype, parmtype;
5887 if (TREE_CODE (t) != CALL_EXPR
5888 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5889 return END_BUILTINS;
5891 fndecl = get_callee_fndecl (t);
5892 if (fndecl == NULL_TREE
5893 || TREE_CODE (fndecl) != FUNCTION_DECL
5894 || ! DECL_BUILT_IN (fndecl)
5895 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5896 return END_BUILTINS;
5898 arglist = TREE_OPERAND (t, 1);
5899 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5900 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5902 /* If a function doesn't take a variable number of arguments,
5903 the last element in the list will have type `void'. */
5904 parmtype = TREE_VALUE (parmlist);
5905 if (VOID_TYPE_P (parmtype))
5907 if (arglist)
5908 return END_BUILTINS;
5909 return DECL_FUNCTION_CODE (fndecl);
5912 if (! arglist)
5913 return END_BUILTINS;
5915 argtype = TREE_TYPE (TREE_VALUE (arglist));
5917 if (SCALAR_FLOAT_TYPE_P (parmtype))
5919 if (! SCALAR_FLOAT_TYPE_P (argtype))
5920 return END_BUILTINS;
5922 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5924 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5925 return END_BUILTINS;
5927 else if (POINTER_TYPE_P (parmtype))
5929 if (! POINTER_TYPE_P (argtype))
5930 return END_BUILTINS;
5932 else if (INTEGRAL_TYPE_P (parmtype))
5934 if (! INTEGRAL_TYPE_P (argtype))
5935 return END_BUILTINS;
5937 else
5938 return END_BUILTINS;
5940 arglist = TREE_CHAIN (arglist);
5943 /* Variable-length argument list. */
5944 return DECL_FUNCTION_CODE (fndecl);
5947 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5948 constant. ARGLIST is the argument list of the call. */
5950 static tree
5951 fold_builtin_constant_p (tree arglist)
5953 if (arglist == 0)
5954 return 0;
5956 arglist = TREE_VALUE (arglist);
5958 /* We return 1 for a numeric type that's known to be a constant
5959 value at compile-time or for an aggregate type that's a
5960 literal constant. */
5961 STRIP_NOPS (arglist);
5963 /* If we know this is a constant, emit the constant of one. */
5964 if (CONSTANT_CLASS_P (arglist)
5965 || (TREE_CODE (arglist) == CONSTRUCTOR
5966 && TREE_CONSTANT (arglist))
5967 || (TREE_CODE (arglist) == ADDR_EXPR
5968 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5969 return integer_one_node;
5971 /* If this expression has side effects, show we don't know it to be a
5972 constant. Likewise if it's a pointer or aggregate type since in
5973 those case we only want literals, since those are only optimized
5974 when generating RTL, not later.
5975 And finally, if we are compiling an initializer, not code, we
5976 need to return a definite result now; there's not going to be any
5977 more optimization done. */
5978 if (TREE_SIDE_EFFECTS (arglist)
5979 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5980 || POINTER_TYPE_P (TREE_TYPE (arglist))
5981 || cfun == 0)
5982 return integer_zero_node;
5984 return 0;
5987 /* Fold a call to __builtin_expect, if we expect that a comparison against
5988 the argument will fold to a constant. In practice, this means a true
5989 constant or the address of a non-weak symbol. ARGLIST is the argument
5990 list of the call. */
5992 static tree
5993 fold_builtin_expect (tree arglist)
5995 tree arg, inner;
5997 if (arglist == 0)
5998 return 0;
6000 arg = TREE_VALUE (arglist);
6002 /* If the argument isn't invariant, then there's nothing we can do. */
6003 if (!TREE_INVARIANT (arg))
6004 return 0;
6006 /* If we're looking at an address of a weak decl, then do not fold. */
6007 inner = arg;
6008 STRIP_NOPS (inner);
6009 if (TREE_CODE (inner) == ADDR_EXPR)
6013 inner = TREE_OPERAND (inner, 0);
6015 while (TREE_CODE (inner) == COMPONENT_REF
6016 || TREE_CODE (inner) == ARRAY_REF);
6017 if (DECL_P (inner) && DECL_WEAK (inner))
6018 return 0;
6021 /* Otherwise, ARG already has the proper type for the return value. */
6022 return arg;
6025 /* Fold a call to __builtin_classify_type. */
6027 static tree
6028 fold_builtin_classify_type (tree arglist)
6030 if (arglist == 0)
6031 return build_int_cst (NULL_TREE, no_type_class);
6033 return build_int_cst (NULL_TREE,
6034 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6037 /* Fold a call to __builtin_strlen. */
6039 static tree
6040 fold_builtin_strlen (tree arglist)
6042 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6043 return NULL_TREE;
6044 else
6046 tree len = c_strlen (TREE_VALUE (arglist), 0);
6048 if (len)
6050 /* Convert from the internal "sizetype" type to "size_t". */
6051 if (size_type_node)
6052 len = fold_convert (size_type_node, len);
6053 return len;
6056 return NULL_TREE;
6060 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6062 static tree
6063 fold_builtin_inf (tree type, int warn)
6065 REAL_VALUE_TYPE real;
6067 /* __builtin_inff is intended to be usable to define INFINITY on all
6068 targets. If an infinity is not available, INFINITY expands "to a
6069 positive constant of type float that overflows at translation
6070 time", footnote "In this case, using INFINITY will violate the
6071 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6072 Thus we pedwarn to ensure this constraint violation is
6073 diagnosed. */
6074 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6075 pedwarn ("target format does not support infinity");
6077 real_inf (&real);
6078 return build_real (type, real);
6081 /* Fold a call to __builtin_nan or __builtin_nans. */
6083 static tree
6084 fold_builtin_nan (tree arglist, tree type, int quiet)
6086 REAL_VALUE_TYPE real;
6087 const char *str;
6089 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6090 return 0;
6091 str = c_getstr (TREE_VALUE (arglist));
6092 if (!str)
6093 return 0;
6095 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6096 return 0;
6098 return build_real (type, real);
6101 /* Return true if the floating point expression T has an integer value.
6102 We also allow +Inf, -Inf and NaN to be considered integer values. */
6104 static bool
6105 integer_valued_real_p (tree t)
6107 switch (TREE_CODE (t))
6109 case FLOAT_EXPR:
6110 return true;
6112 case ABS_EXPR:
6113 case SAVE_EXPR:
6114 case NON_LVALUE_EXPR:
6115 return integer_valued_real_p (TREE_OPERAND (t, 0));
6117 case COMPOUND_EXPR:
6118 case MODIFY_EXPR:
6119 case BIND_EXPR:
6120 return integer_valued_real_p (TREE_OPERAND (t, 1));
6122 case PLUS_EXPR:
6123 case MINUS_EXPR:
6124 case MULT_EXPR:
6125 case MIN_EXPR:
6126 case MAX_EXPR:
6127 return integer_valued_real_p (TREE_OPERAND (t, 0))
6128 && integer_valued_real_p (TREE_OPERAND (t, 1));
6130 case COND_EXPR:
6131 return integer_valued_real_p (TREE_OPERAND (t, 1))
6132 && integer_valued_real_p (TREE_OPERAND (t, 2));
6134 case REAL_CST:
6135 if (! TREE_CONSTANT_OVERFLOW (t))
6137 REAL_VALUE_TYPE c, cint;
6139 c = TREE_REAL_CST (t);
6140 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6141 return real_identical (&c, &cint);
6144 case NOP_EXPR:
6146 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6147 if (TREE_CODE (type) == INTEGER_TYPE)
6148 return true;
6149 if (TREE_CODE (type) == REAL_TYPE)
6150 return integer_valued_real_p (TREE_OPERAND (t, 0));
6151 break;
6154 case CALL_EXPR:
6155 switch (builtin_mathfn_code (t))
6157 case BUILT_IN_CEIL:
6158 case BUILT_IN_CEILF:
6159 case BUILT_IN_CEILL:
6160 case BUILT_IN_FLOOR:
6161 case BUILT_IN_FLOORF:
6162 case BUILT_IN_FLOORL:
6163 case BUILT_IN_NEARBYINT:
6164 case BUILT_IN_NEARBYINTF:
6165 case BUILT_IN_NEARBYINTL:
6166 case BUILT_IN_RINT:
6167 case BUILT_IN_RINTF:
6168 case BUILT_IN_RINTL:
6169 case BUILT_IN_ROUND:
6170 case BUILT_IN_ROUNDF:
6171 case BUILT_IN_ROUNDL:
6172 case BUILT_IN_TRUNC:
6173 case BUILT_IN_TRUNCF:
6174 case BUILT_IN_TRUNCL:
6175 return true;
6177 default:
6178 break;
6180 break;
6182 default:
6183 break;
6185 return false;
6188 /* EXP is assumed to be builtin call where truncation can be propagated
6189 across (for instance floor((double)f) == (double)floorf (f).
6190 Do the transformation. */
6192 static tree
6193 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6195 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6196 tree arg;
6198 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6199 return 0;
6201 arg = TREE_VALUE (arglist);
6202 /* Integer rounding functions are idempotent. */
6203 if (fcode == builtin_mathfn_code (arg))
6204 return arg;
6206 /* If argument is already integer valued, and we don't need to worry
6207 about setting errno, there's no need to perform rounding. */
6208 if (! flag_errno_math && integer_valued_real_p (arg))
6209 return arg;
6211 if (optimize)
6213 tree arg0 = strip_float_extensions (arg);
6214 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6215 tree newtype = TREE_TYPE (arg0);
6216 tree decl;
6218 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6219 && (decl = mathfn_built_in (newtype, fcode)))
6221 arglist =
6222 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6223 return fold_convert (ftype,
6224 build_function_call_expr (decl, arglist));
6227 return 0;
6230 /* EXP is assumed to be builtin call which can narrow the FP type of
6231 the argument, for instance lround((double)f) -> lroundf (f). */
6233 static tree
6234 fold_fixed_mathfn (tree fndecl, tree arglist)
6236 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6237 tree arg;
6239 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6240 return 0;
6242 arg = TREE_VALUE (arglist);
6244 /* If argument is already integer valued, and we don't need to worry
6245 about setting errno, there's no need to perform rounding. */
6246 if (! flag_errno_math && integer_valued_real_p (arg))
6247 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg));
6249 if (optimize)
6251 tree ftype = TREE_TYPE (arg);
6252 tree arg0 = strip_float_extensions (arg);
6253 tree newtype = TREE_TYPE (arg0);
6254 tree decl;
6256 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6257 && (decl = mathfn_built_in (newtype, fcode)))
6259 arglist =
6260 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6261 return build_function_call_expr (decl, arglist);
6264 return 0;
6267 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6268 is the argument list and TYPE is the return type. Return
6269 NULL_TREE if no if no simplification can be made. */
6271 static tree
6272 fold_builtin_cabs (tree arglist, tree type)
6274 tree arg;
6276 if (!arglist || TREE_CHAIN (arglist))
6277 return NULL_TREE;
6279 arg = TREE_VALUE (arglist);
6280 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6281 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6282 return NULL_TREE;
6284 /* Evaluate cabs of a constant at compile-time. */
6285 if (flag_unsafe_math_optimizations
6286 && TREE_CODE (arg) == COMPLEX_CST
6287 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6288 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6289 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6290 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6292 REAL_VALUE_TYPE r, i;
6294 r = TREE_REAL_CST (TREE_REALPART (arg));
6295 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6297 real_arithmetic (&r, MULT_EXPR, &r, &r);
6298 real_arithmetic (&i, MULT_EXPR, &i, &i);
6299 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6300 if (real_sqrt (&r, TYPE_MODE (type), &r)
6301 || ! flag_trapping_math)
6302 return build_real (type, r);
6305 /* If either part is zero, cabs is fabs of the other. */
6306 if (TREE_CODE (arg) == COMPLEX_EXPR
6307 && real_zerop (TREE_OPERAND (arg, 0)))
6308 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6309 if (TREE_CODE (arg) == COMPLEX_EXPR
6310 && real_zerop (TREE_OPERAND (arg, 1)))
6311 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6313 /* Don't do this when optimizing for size. */
6314 if (flag_unsafe_math_optimizations
6315 && optimize && !optimize_size)
6317 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6319 if (sqrtfn != NULL_TREE)
6321 tree rpart, ipart, result, arglist;
6323 arg = builtin_save_expr (arg);
6325 rpart = fold (build1 (REALPART_EXPR, type, arg));
6326 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6328 rpart = builtin_save_expr (rpart);
6329 ipart = builtin_save_expr (ipart);
6331 result = fold (build2 (PLUS_EXPR, type,
6332 fold (build2 (MULT_EXPR, type,
6333 rpart, rpart)),
6334 fold (build2 (MULT_EXPR, type,
6335 ipart, ipart))));
6337 arglist = build_tree_list (NULL_TREE, result);
6338 return build_function_call_expr (sqrtfn, arglist);
6342 return NULL_TREE;
6345 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6346 NULL_TREE if no simplification can be made. */
6348 static tree
6349 fold_builtin_sqrt (tree arglist, tree type)
6352 enum built_in_function fcode;
6353 tree arg = TREE_VALUE (arglist);
6355 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6356 return NULL_TREE;
6358 /* Optimize sqrt of constant value. */
6359 if (TREE_CODE (arg) == REAL_CST
6360 && ! TREE_CONSTANT_OVERFLOW (arg))
6362 REAL_VALUE_TYPE r, x;
6364 x = TREE_REAL_CST (arg);
6365 if (real_sqrt (&r, TYPE_MODE (type), &x)
6366 || (!flag_trapping_math && !flag_errno_math))
6367 return build_real (type, r);
6370 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6371 fcode = builtin_mathfn_code (arg);
6372 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6374 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6375 arg = fold (build2 (MULT_EXPR, type,
6376 TREE_VALUE (TREE_OPERAND (arg, 1)),
6377 build_real (type, dconsthalf)));
6378 arglist = build_tree_list (NULL_TREE, arg);
6379 return build_function_call_expr (expfn, arglist);
6382 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6383 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6385 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6387 if (powfn)
6389 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6390 tree tree_root;
6391 /* The inner root was either sqrt or cbrt. */
6392 REAL_VALUE_TYPE dconstroot =
6393 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6395 /* Adjust for the outer root. */
6396 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6397 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6398 tree_root = build_real (type, dconstroot);
6399 arglist = tree_cons (NULL_TREE, arg0,
6400 build_tree_list (NULL_TREE, tree_root));
6401 return build_function_call_expr (powfn, arglist);
6405 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6406 if (flag_unsafe_math_optimizations
6407 && (fcode == BUILT_IN_POW
6408 || fcode == BUILT_IN_POWF
6409 || fcode == BUILT_IN_POWL))
6411 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6412 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6413 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6414 tree narg1;
6415 if (!tree_expr_nonnegative_p (arg0))
6416 arg0 = build1 (ABS_EXPR, type, arg0);
6417 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6418 build_real (type, dconsthalf)));
6419 arglist = tree_cons (NULL_TREE, arg0,
6420 build_tree_list (NULL_TREE, narg1));
6421 return build_function_call_expr (powfn, arglist);
6424 return NULL_TREE;
6427 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6428 NULL_TREE if no simplification can be made. */
6429 static tree
6430 fold_builtin_cbrt (tree arglist, tree type)
6432 tree arg = TREE_VALUE (arglist);
6433 const enum built_in_function fcode = builtin_mathfn_code (arg);
6435 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6436 return NULL_TREE;
6438 /* Optimize cbrt of constant value. */
6439 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6440 return arg;
6442 if (flag_unsafe_math_optimizations)
6444 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6445 if (BUILTIN_EXPONENT_P (fcode))
6447 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6448 const REAL_VALUE_TYPE third_trunc =
6449 real_value_truncate (TYPE_MODE (type), dconstthird);
6450 arg = fold (build2 (MULT_EXPR, type,
6451 TREE_VALUE (TREE_OPERAND (arg, 1)),
6452 build_real (type, third_trunc)));
6453 arglist = build_tree_list (NULL_TREE, arg);
6454 return build_function_call_expr (expfn, arglist);
6457 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6458 if (BUILTIN_SQRT_P (fcode))
6460 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6462 if (powfn)
6464 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6465 tree tree_root;
6466 REAL_VALUE_TYPE dconstroot = dconstthird;
6468 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6469 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6470 tree_root = build_real (type, dconstroot);
6471 arglist = tree_cons (NULL_TREE, arg0,
6472 build_tree_list (NULL_TREE, tree_root));
6473 return build_function_call_expr (powfn, arglist);
6477 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
6478 if (BUILTIN_CBRT_P (fcode))
6480 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6481 if (tree_expr_nonnegative_p (arg0))
6483 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6485 if (powfn)
6487 tree tree_root;
6488 REAL_VALUE_TYPE dconstroot;
6490 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
6491 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6492 tree_root = build_real (type, dconstroot);
6493 arglist = tree_cons (NULL_TREE, arg0,
6494 build_tree_list (NULL_TREE, tree_root));
6495 return build_function_call_expr (powfn, arglist);
6500 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
6501 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
6502 || fcode == BUILT_IN_POWL)
6504 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
6505 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6506 if (tree_expr_nonnegative_p (arg00))
6508 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6509 const REAL_VALUE_TYPE dconstroot
6510 = real_value_truncate (TYPE_MODE (type), dconstthird);
6511 tree narg01 = fold (build2 (MULT_EXPR, type, arg01,
6512 build_real (type, dconstroot)));
6513 arglist = tree_cons (NULL_TREE, arg00,
6514 build_tree_list (NULL_TREE, narg01));
6515 return build_function_call_expr (powfn, arglist);
6519 return NULL_TREE;
6522 /* Fold function call to builtin sin, sinf, or sinl. Return
6523 NULL_TREE if no simplification can be made. */
6524 static tree
6525 fold_builtin_sin (tree arglist)
6527 tree arg = TREE_VALUE (arglist);
6529 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6530 return NULL_TREE;
6532 /* Optimize sin (0.0) = 0.0. */
6533 if (real_zerop (arg))
6534 return arg;
6536 return NULL_TREE;
6539 /* Fold function call to builtin cos, cosf, or cosl. Return
6540 NULL_TREE if no simplification can be made. */
6541 static tree
6542 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6544 tree arg = TREE_VALUE (arglist);
6546 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6547 return NULL_TREE;
6549 /* Optimize cos (0.0) = 1.0. */
6550 if (real_zerop (arg))
6551 return build_real (type, dconst1);
6553 /* Optimize cos(-x) into cos (x). */
6554 if (TREE_CODE (arg) == NEGATE_EXPR)
6556 tree args = build_tree_list (NULL_TREE,
6557 TREE_OPERAND (arg, 0));
6558 return build_function_call_expr (fndecl, args);
6561 return NULL_TREE;
6564 /* Fold function call to builtin tan, tanf, or tanl. Return
6565 NULL_TREE if no simplification can be made. */
6566 static tree
6567 fold_builtin_tan (tree arglist)
6569 enum built_in_function fcode;
6570 tree arg = TREE_VALUE (arglist);
6572 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6573 return NULL_TREE;
6575 /* Optimize tan(0.0) = 0.0. */
6576 if (real_zerop (arg))
6577 return arg;
6579 /* Optimize tan(atan(x)) = x. */
6580 fcode = builtin_mathfn_code (arg);
6581 if (flag_unsafe_math_optimizations
6582 && (fcode == BUILT_IN_ATAN
6583 || fcode == BUILT_IN_ATANF
6584 || fcode == BUILT_IN_ATANL))
6585 return TREE_VALUE (TREE_OPERAND (arg, 1));
6587 return NULL_TREE;
6590 /* Fold function call to builtin atan, atanf, or atanl. Return
6591 NULL_TREE if no simplification can be made. */
6593 static tree
6594 fold_builtin_atan (tree arglist, tree type)
6597 tree arg = TREE_VALUE (arglist);
6599 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6600 return NULL_TREE;
6602 /* Optimize atan(0.0) = 0.0. */
6603 if (real_zerop (arg))
6604 return arg;
6606 /* Optimize atan(1.0) = pi/4. */
6607 if (real_onep (arg))
6609 REAL_VALUE_TYPE cst;
6611 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6612 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6613 return build_real (type, cst);
6616 return NULL_TREE;
6619 /* Fold function call to builtin trunc, truncf or truncl. Return
6620 NULL_TREE if no simplification can be made. */
6622 static tree
6623 fold_builtin_trunc (tree fndecl, tree arglist)
6625 tree arg;
6627 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6628 return 0;
6630 /* Optimize trunc of constant value. */
6631 arg = TREE_VALUE (arglist);
6632 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6634 REAL_VALUE_TYPE r, x;
6635 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6637 x = TREE_REAL_CST (arg);
6638 real_trunc (&r, TYPE_MODE (type), &x);
6639 return build_real (type, r);
6642 return fold_trunc_transparent_mathfn (fndecl, arglist);
6645 /* Fold function call to builtin floor, floorf or floorl. Return
6646 NULL_TREE if no simplification can be made. */
6648 static tree
6649 fold_builtin_floor (tree fndecl, tree arglist)
6651 tree arg;
6653 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6654 return 0;
6656 /* Optimize floor of constant value. */
6657 arg = TREE_VALUE (arglist);
6658 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6660 REAL_VALUE_TYPE x;
6662 x = TREE_REAL_CST (arg);
6663 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6665 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6666 REAL_VALUE_TYPE r;
6668 real_floor (&r, TYPE_MODE (type), &x);
6669 return build_real (type, r);
6673 return fold_trunc_transparent_mathfn (fndecl, arglist);
6676 /* Fold function call to builtin ceil, ceilf or ceill. Return
6677 NULL_TREE if no simplification can be made. */
6679 static tree
6680 fold_builtin_ceil (tree fndecl, tree arglist)
6682 tree arg;
6684 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6685 return 0;
6687 /* Optimize ceil of constant value. */
6688 arg = TREE_VALUE (arglist);
6689 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6691 REAL_VALUE_TYPE x;
6693 x = TREE_REAL_CST (arg);
6694 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6696 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6697 REAL_VALUE_TYPE r;
6699 real_ceil (&r, TYPE_MODE (type), &x);
6700 return build_real (type, r);
6704 return fold_trunc_transparent_mathfn (fndecl, arglist);
6707 /* Fold function call to builtin round, roundf or roundl. Return
6708 NULL_TREE if no simplification can be made. */
6710 static tree
6711 fold_builtin_round (tree fndecl, tree arglist)
6713 tree arg;
6715 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6716 return 0;
6718 /* Optimize round of constant value. */
6719 arg = TREE_VALUE (arglist);
6720 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6722 REAL_VALUE_TYPE x;
6724 x = TREE_REAL_CST (arg);
6725 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6727 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6728 REAL_VALUE_TYPE r;
6730 real_round (&r, TYPE_MODE (type), &x);
6731 return build_real (type, r);
6735 return fold_trunc_transparent_mathfn (fndecl, arglist);
6738 /* Fold function call to builtin lround, lroundf or lroundl (or the
6739 corresponding long long versions) and other rounding functions.
6740 Return NULL_TREE if no simplification can be made. */
6742 static tree
6743 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
6745 tree arg;
6747 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6748 return 0;
6750 /* Optimize lround of constant value. */
6751 arg = TREE_VALUE (arglist);
6752 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6754 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6756 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6758 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
6759 tree ftype = TREE_TYPE (arg), result;
6760 HOST_WIDE_INT hi, lo;
6761 REAL_VALUE_TYPE r;
6763 switch (DECL_FUNCTION_CODE (fndecl))
6765 case BUILT_IN_LFLOOR:
6766 case BUILT_IN_LFLOORF:
6767 case BUILT_IN_LFLOORL:
6768 case BUILT_IN_LLFLOOR:
6769 case BUILT_IN_LLFLOORF:
6770 case BUILT_IN_LLFLOORL:
6771 real_floor (&r, TYPE_MODE (ftype), &x);
6772 break;
6774 case BUILT_IN_LROUND:
6775 case BUILT_IN_LROUNDF:
6776 case BUILT_IN_LROUNDL:
6777 case BUILT_IN_LLROUND:
6778 case BUILT_IN_LLROUNDF:
6779 case BUILT_IN_LLROUNDL:
6780 real_round (&r, TYPE_MODE (ftype), &x);
6781 break;
6783 default:
6784 gcc_unreachable ();
6787 REAL_VALUE_TO_INT (&lo, &hi, r);
6788 result = build_int_cst_wide (NULL_TREE, lo, hi);
6789 if (int_fits_type_p (result, itype))
6790 return fold_convert (itype, result);
6794 return fold_fixed_mathfn (fndecl, arglist);
6797 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6798 and their long and long long variants (i.e. ffsl and ffsll).
6799 Return NULL_TREE if no simplification can be made. */
6801 static tree
6802 fold_builtin_bitop (tree fndecl, tree arglist)
6804 tree arg;
6806 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6807 return NULL_TREE;
6809 /* Optimize for constant argument. */
6810 arg = TREE_VALUE (arglist);
6811 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6813 HOST_WIDE_INT hi, width, result;
6814 unsigned HOST_WIDE_INT lo;
6815 tree type;
6817 type = TREE_TYPE (arg);
6818 width = TYPE_PRECISION (type);
6819 lo = TREE_INT_CST_LOW (arg);
6821 /* Clear all the bits that are beyond the type's precision. */
6822 if (width > HOST_BITS_PER_WIDE_INT)
6824 hi = TREE_INT_CST_HIGH (arg);
6825 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6826 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6828 else
6830 hi = 0;
6831 if (width < HOST_BITS_PER_WIDE_INT)
6832 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6835 switch (DECL_FUNCTION_CODE (fndecl))
6837 case BUILT_IN_FFS:
6838 case BUILT_IN_FFSL:
6839 case BUILT_IN_FFSLL:
6840 if (lo != 0)
6841 result = exact_log2 (lo & -lo) + 1;
6842 else if (hi != 0)
6843 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6844 else
6845 result = 0;
6846 break;
6848 case BUILT_IN_CLZ:
6849 case BUILT_IN_CLZL:
6850 case BUILT_IN_CLZLL:
6851 if (hi != 0)
6852 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6853 else if (lo != 0)
6854 result = width - floor_log2 (lo) - 1;
6855 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6856 result = width;
6857 break;
6859 case BUILT_IN_CTZ:
6860 case BUILT_IN_CTZL:
6861 case BUILT_IN_CTZLL:
6862 if (lo != 0)
6863 result = exact_log2 (lo & -lo);
6864 else if (hi != 0)
6865 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6866 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6867 result = width;
6868 break;
6870 case BUILT_IN_POPCOUNT:
6871 case BUILT_IN_POPCOUNTL:
6872 case BUILT_IN_POPCOUNTLL:
6873 result = 0;
6874 while (lo)
6875 result++, lo &= lo - 1;
6876 while (hi)
6877 result++, hi &= hi - 1;
6878 break;
6880 case BUILT_IN_PARITY:
6881 case BUILT_IN_PARITYL:
6882 case BUILT_IN_PARITYLL:
6883 result = 0;
6884 while (lo)
6885 result++, lo &= lo - 1;
6886 while (hi)
6887 result++, hi &= hi - 1;
6888 result &= 1;
6889 break;
6891 default:
6892 gcc_unreachable ();
6895 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
6898 return NULL_TREE;
6901 /* Return true if EXPR is the real constant contained in VALUE. */
6903 static bool
6904 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6906 STRIP_NOPS (expr);
6908 return ((TREE_CODE (expr) == REAL_CST
6909 && ! TREE_CONSTANT_OVERFLOW (expr)
6910 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6911 || (TREE_CODE (expr) == COMPLEX_CST
6912 && real_dconstp (TREE_REALPART (expr), value)
6913 && real_zerop (TREE_IMAGPART (expr))));
6916 /* A subroutine of fold_builtin to fold the various logarithmic
6917 functions. EXP is the CALL_EXPR of a call to a builtin logN
6918 function. VALUE is the base of the logN function. */
6920 static tree
6921 fold_builtin_logarithm (tree fndecl, tree arglist,
6922 const REAL_VALUE_TYPE *value)
6924 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6926 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6927 tree arg = TREE_VALUE (arglist);
6928 const enum built_in_function fcode = builtin_mathfn_code (arg);
6930 /* Optimize logN(1.0) = 0.0. */
6931 if (real_onep (arg))
6932 return build_real (type, dconst0);
6934 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6935 exactly, then only do this if flag_unsafe_math_optimizations. */
6936 if (exact_real_truncate (TYPE_MODE (type), value)
6937 || flag_unsafe_math_optimizations)
6939 const REAL_VALUE_TYPE value_truncate =
6940 real_value_truncate (TYPE_MODE (type), *value);
6941 if (real_dconstp (arg, &value_truncate))
6942 return build_real (type, dconst1);
6945 /* Special case, optimize logN(expN(x)) = x. */
6946 if (flag_unsafe_math_optimizations
6947 && ((value == &dconste
6948 && (fcode == BUILT_IN_EXP
6949 || fcode == BUILT_IN_EXPF
6950 || fcode == BUILT_IN_EXPL))
6951 || (value == &dconst2
6952 && (fcode == BUILT_IN_EXP2
6953 || fcode == BUILT_IN_EXP2F
6954 || fcode == BUILT_IN_EXP2L))
6955 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6956 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6958 /* Optimize logN(func()) for various exponential functions. We
6959 want to determine the value "x" and the power "exponent" in
6960 order to transform logN(x**exponent) into exponent*logN(x). */
6961 if (flag_unsafe_math_optimizations)
6963 tree exponent = 0, x = 0;
6965 switch (fcode)
6967 case BUILT_IN_EXP:
6968 case BUILT_IN_EXPF:
6969 case BUILT_IN_EXPL:
6970 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6971 x = build_real (type,
6972 real_value_truncate (TYPE_MODE (type), dconste));
6973 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6974 break;
6975 case BUILT_IN_EXP2:
6976 case BUILT_IN_EXP2F:
6977 case BUILT_IN_EXP2L:
6978 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6979 x = build_real (type, dconst2);
6980 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6981 break;
6982 case BUILT_IN_EXP10:
6983 case BUILT_IN_EXP10F:
6984 case BUILT_IN_EXP10L:
6985 case BUILT_IN_POW10:
6986 case BUILT_IN_POW10F:
6987 case BUILT_IN_POW10L:
6988 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6989 x = build_real (type, dconst10);
6990 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6991 break;
6992 case BUILT_IN_SQRT:
6993 case BUILT_IN_SQRTF:
6994 case BUILT_IN_SQRTL:
6995 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6996 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6997 exponent = build_real (type, dconsthalf);
6998 break;
6999 case BUILT_IN_CBRT:
7000 case BUILT_IN_CBRTF:
7001 case BUILT_IN_CBRTL:
7002 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7003 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7004 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7005 dconstthird));
7006 break;
7007 case BUILT_IN_POW:
7008 case BUILT_IN_POWF:
7009 case BUILT_IN_POWL:
7010 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7011 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7012 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7013 break;
7014 default:
7015 break;
7018 /* Now perform the optimization. */
7019 if (x && exponent)
7021 tree logfn;
7022 arglist = build_tree_list (NULL_TREE, x);
7023 logfn = build_function_call_expr (fndecl, arglist);
7024 return fold (build2 (MULT_EXPR, type, exponent, logfn));
7029 return 0;
7032 /* Fold a builtin function call to pow, powf, or powl. Return
7033 NULL_TREE if no simplification can be made. */
7034 static tree
7035 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7037 tree arg0 = TREE_VALUE (arglist);
7038 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7040 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7041 return NULL_TREE;
7043 /* Optimize pow(1.0,y) = 1.0. */
7044 if (real_onep (arg0))
7045 return omit_one_operand (type, build_real (type, dconst1), arg1);
7047 if (TREE_CODE (arg1) == REAL_CST
7048 && ! TREE_CONSTANT_OVERFLOW (arg1))
7050 REAL_VALUE_TYPE cint;
7051 REAL_VALUE_TYPE c;
7052 HOST_WIDE_INT n;
7054 c = TREE_REAL_CST (arg1);
7056 /* Optimize pow(x,0.0) = 1.0. */
7057 if (REAL_VALUES_EQUAL (c, dconst0))
7058 return omit_one_operand (type, build_real (type, dconst1),
7059 arg0);
7061 /* Optimize pow(x,1.0) = x. */
7062 if (REAL_VALUES_EQUAL (c, dconst1))
7063 return arg0;
7065 /* Optimize pow(x,-1.0) = 1.0/x. */
7066 if (REAL_VALUES_EQUAL (c, dconstm1))
7067 return fold (build2 (RDIV_EXPR, type,
7068 build_real (type, dconst1), arg0));
7070 /* Optimize pow(x,0.5) = sqrt(x). */
7071 if (flag_unsafe_math_optimizations
7072 && REAL_VALUES_EQUAL (c, dconsthalf))
7074 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7076 if (sqrtfn != NULL_TREE)
7078 tree arglist = build_tree_list (NULL_TREE, arg0);
7079 return build_function_call_expr (sqrtfn, arglist);
7083 /* Check for an integer exponent. */
7084 n = real_to_integer (&c);
7085 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7086 if (real_identical (&c, &cint))
7088 /* Attempt to evaluate pow at compile-time. */
7089 if (TREE_CODE (arg0) == REAL_CST
7090 && ! TREE_CONSTANT_OVERFLOW (arg0))
7092 REAL_VALUE_TYPE x;
7093 bool inexact;
7095 x = TREE_REAL_CST (arg0);
7096 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7097 if (flag_unsafe_math_optimizations || !inexact)
7098 return build_real (type, x);
7101 /* Strip sign ops from even integer powers. */
7102 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7104 tree narg0 = fold_strip_sign_ops (arg0);
7105 if (narg0)
7107 arglist = build_tree_list (NULL_TREE, arg1);
7108 arglist = tree_cons (NULL_TREE, narg0, arglist);
7109 return build_function_call_expr (fndecl, arglist);
7115 if (flag_unsafe_math_optimizations)
7117 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7119 /* Optimize pow(expN(x),y) = expN(x*y). */
7120 if (BUILTIN_EXPONENT_P (fcode))
7122 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7123 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7124 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
7125 arglist = build_tree_list (NULL_TREE, arg);
7126 return build_function_call_expr (expfn, arglist);
7129 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7130 if (BUILTIN_SQRT_P (fcode))
7132 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7133 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7134 build_real (type, dconsthalf)));
7136 arglist = tree_cons (NULL_TREE, narg0,
7137 build_tree_list (NULL_TREE, narg1));
7138 return build_function_call_expr (fndecl, arglist);
7141 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7142 if (BUILTIN_CBRT_P (fcode))
7144 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7145 if (tree_expr_nonnegative_p (arg))
7147 const REAL_VALUE_TYPE dconstroot
7148 = real_value_truncate (TYPE_MODE (type), dconstthird);
7149 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7150 build_real (type, dconstroot)));
7151 arglist = tree_cons (NULL_TREE, arg,
7152 build_tree_list (NULL_TREE, narg1));
7153 return build_function_call_expr (fndecl, arglist);
7157 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7158 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7159 || fcode == BUILT_IN_POWL)
7161 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7162 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7163 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7164 arglist = tree_cons (NULL_TREE, arg00,
7165 build_tree_list (NULL_TREE, narg1));
7166 return build_function_call_expr (fndecl, arglist);
7170 return NULL_TREE;
7173 /* Fold a builtin function call to powi, powif, or powil. Return
7174 NULL_TREE if no simplification can be made. */
7175 static tree
7176 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7178 tree arg0 = TREE_VALUE (arglist);
7179 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7181 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7182 return NULL_TREE;
7184 /* Optimize pow(1.0,y) = 1.0. */
7185 if (real_onep (arg0))
7186 return omit_one_operand (type, build_real (type, dconst1), arg1);
7188 if (host_integerp (arg1, 0))
7190 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7192 /* Evaluate powi at compile-time. */
7193 if (TREE_CODE (arg0) == REAL_CST
7194 && ! TREE_CONSTANT_OVERFLOW (arg0))
7196 REAL_VALUE_TYPE x;
7197 x = TREE_REAL_CST (arg0);
7198 real_powi (&x, TYPE_MODE (type), &x, c);
7199 return build_real (type, x);
7202 /* Optimize pow(x,0) = 1.0. */
7203 if (c == 0)
7204 return omit_one_operand (type, build_real (type, dconst1),
7205 arg0);
7207 /* Optimize pow(x,1) = x. */
7208 if (c == 1)
7209 return arg0;
7211 /* Optimize pow(x,-1) = 1.0/x. */
7212 if (c == -1)
7213 return fold (build2 (RDIV_EXPR, type,
7214 build_real (type, dconst1), arg0));
7217 return NULL_TREE;
7220 /* A subroutine of fold_builtin to fold the various exponent
7221 functions. EXP is the CALL_EXPR of a call to a builtin function.
7222 VALUE is the value which will be raised to a power. */
7224 static tree
7225 fold_builtin_exponent (tree fndecl, tree arglist,
7226 const REAL_VALUE_TYPE *value)
7228 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7230 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7231 tree arg = TREE_VALUE (arglist);
7233 /* Optimize exp*(0.0) = 1.0. */
7234 if (real_zerop (arg))
7235 return build_real (type, dconst1);
7237 /* Optimize expN(1.0) = N. */
7238 if (real_onep (arg))
7240 REAL_VALUE_TYPE cst;
7242 real_convert (&cst, TYPE_MODE (type), value);
7243 return build_real (type, cst);
7246 /* Attempt to evaluate expN(integer) at compile-time. */
7247 if (flag_unsafe_math_optimizations
7248 && TREE_CODE (arg) == REAL_CST
7249 && ! TREE_CONSTANT_OVERFLOW (arg))
7251 REAL_VALUE_TYPE cint;
7252 REAL_VALUE_TYPE c;
7253 HOST_WIDE_INT n;
7255 c = TREE_REAL_CST (arg);
7256 n = real_to_integer (&c);
7257 real_from_integer (&cint, VOIDmode, n,
7258 n < 0 ? -1 : 0, 0);
7259 if (real_identical (&c, &cint))
7261 REAL_VALUE_TYPE x;
7263 real_powi (&x, TYPE_MODE (type), value, n);
7264 return build_real (type, x);
7268 /* Optimize expN(logN(x)) = x. */
7269 if (flag_unsafe_math_optimizations)
7271 const enum built_in_function fcode = builtin_mathfn_code (arg);
7273 if ((value == &dconste
7274 && (fcode == BUILT_IN_LOG
7275 || fcode == BUILT_IN_LOGF
7276 || fcode == BUILT_IN_LOGL))
7277 || (value == &dconst2
7278 && (fcode == BUILT_IN_LOG2
7279 || fcode == BUILT_IN_LOG2F
7280 || fcode == BUILT_IN_LOG2L))
7281 || (value == &dconst10
7282 && (fcode == BUILT_IN_LOG10
7283 || fcode == BUILT_IN_LOG10F
7284 || fcode == BUILT_IN_LOG10L)))
7285 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7289 return 0;
7292 /* Fold function call to builtin memcpy. Return
7293 NULL_TREE if no simplification can be made. */
7295 static tree
7296 fold_builtin_memcpy (tree fndecl, tree arglist)
7298 tree dest, src, len;
7300 if (!validate_arglist (arglist,
7301 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7302 return 0;
7304 dest = TREE_VALUE (arglist);
7305 src = TREE_VALUE (TREE_CHAIN (arglist));
7306 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7308 /* If the LEN parameter is zero, return DEST. */
7309 if (integer_zerop (len))
7310 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
7312 /* If SRC and DEST are the same (and not volatile), return DEST. */
7313 if (operand_equal_p (src, dest, 0))
7314 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
7316 return 0;
7319 /* Fold function call to builtin mempcpy. Return
7320 NULL_TREE if no simplification can be made. */
7322 static tree
7323 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7325 if (validate_arglist (arglist,
7326 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7328 tree dest = TREE_VALUE (arglist);
7329 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7330 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7332 /* If the LEN parameter is zero, return DEST. */
7333 if (integer_zerop (len))
7334 return omit_one_operand (type, dest, src);
7336 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7337 if (operand_equal_p (src, dest, 0))
7339 if (endp == 0)
7340 return omit_one_operand (type, dest, len);
7342 if (endp == 2)
7343 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7344 ssize_int (1)));
7346 len = fold_convert (TREE_TYPE (dest), len);
7347 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7348 return fold_convert (type, len);
7351 return 0;
7354 /* Fold function call to builtin memmove. Return
7355 NULL_TREE if no simplification can be made. */
7357 static tree
7358 fold_builtin_memmove (tree arglist, tree type)
7360 tree dest, src, len;
7362 if (!validate_arglist (arglist,
7363 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7364 return 0;
7366 dest = TREE_VALUE (arglist);
7367 src = TREE_VALUE (TREE_CHAIN (arglist));
7368 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7370 /* If the LEN parameter is zero, return DEST. */
7371 if (integer_zerop (len))
7372 return omit_one_operand (type, dest, src);
7374 /* If SRC and DEST are the same (and not volatile), return DEST. */
7375 if (operand_equal_p (src, dest, 0))
7376 return omit_one_operand (type, dest, len);
7378 return 0;
7381 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7382 the length of the string to be copied. Return NULL_TREE if no
7383 simplification can be made. */
7385 tree
7386 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
7388 tree dest, src, fn;
7390 if (!validate_arglist (arglist,
7391 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7392 return 0;
7394 dest = TREE_VALUE (arglist);
7395 src = TREE_VALUE (TREE_CHAIN (arglist));
7397 /* If SRC and DEST are the same (and not volatile), return DEST. */
7398 if (operand_equal_p (src, dest, 0))
7399 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
7401 if (optimize_size)
7402 return 0;
7404 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7405 if (!fn)
7406 return 0;
7408 if (!len)
7410 len = c_strlen (src, 1);
7411 if (! len || TREE_SIDE_EFFECTS (len))
7412 return 0;
7415 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7416 arglist = build_tree_list (NULL_TREE, len);
7417 arglist = tree_cons (NULL_TREE, src, arglist);
7418 arglist = tree_cons (NULL_TREE, dest, arglist);
7419 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7420 build_function_call_expr (fn, arglist));
7423 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7424 the length of the source string. Return NULL_TREE if no simplification
7425 can be made. */
7427 tree
7428 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
7430 tree dest, src, len, fn;
7432 if (!validate_arglist (arglist,
7433 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7434 return 0;
7436 dest = TREE_VALUE (arglist);
7437 src = TREE_VALUE (TREE_CHAIN (arglist));
7438 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7440 /* If the LEN parameter is zero, return DEST. */
7441 if (integer_zerop (len))
7442 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
7444 /* We can't compare slen with len as constants below if len is not a
7445 constant. */
7446 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7447 return 0;
7449 if (!slen)
7450 slen = c_strlen (src, 1);
7452 /* Now, we must be passed a constant src ptr parameter. */
7453 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7454 return 0;
7456 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7458 /* We do not support simplification of this case, though we do
7459 support it when expanding trees into RTL. */
7460 /* FIXME: generate a call to __builtin_memset. */
7461 if (tree_int_cst_lt (slen, len))
7462 return 0;
7464 /* OK transform into builtin memcpy. */
7465 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7466 if (!fn)
7467 return 0;
7468 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7469 build_function_call_expr (fn, arglist));
7472 /* Fold function call to builtin memcmp. Return
7473 NULL_TREE if no simplification can be made. */
7475 static tree
7476 fold_builtin_memcmp (tree arglist)
7478 tree arg1, arg2, len;
7479 const char *p1, *p2;
7481 if (!validate_arglist (arglist,
7482 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7483 return 0;
7485 arg1 = TREE_VALUE (arglist);
7486 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7487 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7489 /* If the LEN parameter is zero, return zero. */
7490 if (integer_zerop (len))
7491 return omit_two_operands (integer_type_node, integer_zero_node,
7492 arg1, arg2);
7494 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7495 if (operand_equal_p (arg1, arg2, 0))
7496 return omit_one_operand (integer_type_node, integer_zero_node, len);
7498 p1 = c_getstr (arg1);
7499 p2 = c_getstr (arg2);
7501 /* If all arguments are constant, and the value of len is not greater
7502 than the lengths of arg1 and arg2, evaluate at compile-time. */
7503 if (host_integerp (len, 1) && p1 && p2
7504 && compare_tree_int (len, strlen (p1) + 1) <= 0
7505 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7507 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7509 if (r > 0)
7510 return integer_one_node;
7511 else if (r < 0)
7512 return integer_minus_one_node;
7513 else
7514 return integer_zero_node;
7517 /* If len parameter is one, return an expression corresponding to
7518 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7519 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7521 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7522 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7523 tree ind1 = fold_convert (integer_type_node,
7524 build1 (INDIRECT_REF, cst_uchar_node,
7525 fold_convert (cst_uchar_ptr_node,
7526 arg1)));
7527 tree ind2 = fold_convert (integer_type_node,
7528 build1 (INDIRECT_REF, cst_uchar_node,
7529 fold_convert (cst_uchar_ptr_node,
7530 arg2)));
7531 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7534 return 0;
7537 /* Fold function call to builtin strcmp. Return
7538 NULL_TREE if no simplification can be made. */
7540 static tree
7541 fold_builtin_strcmp (tree arglist)
7543 tree arg1, arg2;
7544 const char *p1, *p2;
7546 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7547 return 0;
7549 arg1 = TREE_VALUE (arglist);
7550 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7552 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7553 if (operand_equal_p (arg1, arg2, 0))
7554 return integer_zero_node;
7556 p1 = c_getstr (arg1);
7557 p2 = c_getstr (arg2);
7559 if (p1 && p2)
7561 const int i = strcmp (p1, p2);
7562 if (i < 0)
7563 return integer_minus_one_node;
7564 else if (i > 0)
7565 return integer_one_node;
7566 else
7567 return integer_zero_node;
7570 /* If the second arg is "", return *(const unsigned char*)arg1. */
7571 if (p2 && *p2 == '\0')
7573 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7574 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7575 return fold_convert (integer_type_node,
7576 build1 (INDIRECT_REF, cst_uchar_node,
7577 fold_convert (cst_uchar_ptr_node,
7578 arg1)));
7581 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7582 if (p1 && *p1 == '\0')
7584 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7585 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7586 tree temp = fold_convert (integer_type_node,
7587 build1 (INDIRECT_REF, cst_uchar_node,
7588 fold_convert (cst_uchar_ptr_node,
7589 arg2)));
7590 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7593 return 0;
7596 /* Fold function call to builtin strncmp. Return
7597 NULL_TREE if no simplification can be made. */
7599 static tree
7600 fold_builtin_strncmp (tree arglist)
7602 tree arg1, arg2, len;
7603 const char *p1, *p2;
7605 if (!validate_arglist (arglist,
7606 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7607 return 0;
7609 arg1 = TREE_VALUE (arglist);
7610 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7611 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7613 /* If the LEN parameter is zero, return zero. */
7614 if (integer_zerop (len))
7615 return omit_two_operands (integer_type_node, integer_zero_node,
7616 arg1, arg2);
7618 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7619 if (operand_equal_p (arg1, arg2, 0))
7620 return omit_one_operand (integer_type_node, integer_zero_node, len);
7622 p1 = c_getstr (arg1);
7623 p2 = c_getstr (arg2);
7625 if (host_integerp (len, 1) && p1 && p2)
7627 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7628 if (i > 0)
7629 return integer_one_node;
7630 else if (i < 0)
7631 return integer_minus_one_node;
7632 else
7633 return integer_zero_node;
7636 /* If the second arg is "", and the length is greater than zero,
7637 return *(const unsigned char*)arg1. */
7638 if (p2 && *p2 == '\0'
7639 && TREE_CODE (len) == INTEGER_CST
7640 && tree_int_cst_sgn (len) == 1)
7642 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7643 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7644 return fold_convert (integer_type_node,
7645 build1 (INDIRECT_REF, cst_uchar_node,
7646 fold_convert (cst_uchar_ptr_node,
7647 arg1)));
7650 /* If the first arg is "", and the length is greater than zero,
7651 return -*(const unsigned char*)arg2. */
7652 if (p1 && *p1 == '\0'
7653 && TREE_CODE (len) == INTEGER_CST
7654 && tree_int_cst_sgn (len) == 1)
7656 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7657 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7658 tree temp = fold_convert (integer_type_node,
7659 build1 (INDIRECT_REF, cst_uchar_node,
7660 fold_convert (cst_uchar_ptr_node,
7661 arg2)));
7662 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7665 /* If len parameter is one, return an expression corresponding to
7666 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7667 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7669 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7670 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7671 tree ind1 = fold_convert (integer_type_node,
7672 build1 (INDIRECT_REF, cst_uchar_node,
7673 fold_convert (cst_uchar_ptr_node,
7674 arg1)));
7675 tree ind2 = fold_convert (integer_type_node,
7676 build1 (INDIRECT_REF, cst_uchar_node,
7677 fold_convert (cst_uchar_ptr_node,
7678 arg2)));
7679 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7682 return 0;
7685 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7686 NULL_TREE if no simplification can be made. */
7688 static tree
7689 fold_builtin_signbit (tree fndecl, tree arglist)
7691 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7692 tree arg, temp;
7694 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7695 return NULL_TREE;
7697 arg = TREE_VALUE (arglist);
7699 /* If ARG is a compile-time constant, determine the result. */
7700 if (TREE_CODE (arg) == REAL_CST
7701 && !TREE_CONSTANT_OVERFLOW (arg))
7703 REAL_VALUE_TYPE c;
7705 c = TREE_REAL_CST (arg);
7706 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7707 return fold_convert (type, temp);
7710 /* If ARG is non-negative, the result is always zero. */
7711 if (tree_expr_nonnegative_p (arg))
7712 return omit_one_operand (type, integer_zero_node, arg);
7714 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7715 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7716 return fold (build2 (LT_EXPR, type, arg,
7717 build_real (TREE_TYPE (arg), dconst0)));
7719 return NULL_TREE;
7722 /* Fold function call to builtin copysign, copysignf or copysignl.
7723 Return NULL_TREE if no simplification can be made. */
7725 static tree
7726 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7728 tree arg1, arg2, tem;
7730 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7731 return NULL_TREE;
7733 arg1 = TREE_VALUE (arglist);
7734 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7736 /* copysign(X,X) is X. */
7737 if (operand_equal_p (arg1, arg2, 0))
7738 return fold_convert (type, arg1);
7740 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7741 if (TREE_CODE (arg1) == REAL_CST
7742 && TREE_CODE (arg2) == REAL_CST
7743 && !TREE_CONSTANT_OVERFLOW (arg1)
7744 && !TREE_CONSTANT_OVERFLOW (arg2))
7746 REAL_VALUE_TYPE c1, c2;
7748 c1 = TREE_REAL_CST (arg1);
7749 c2 = TREE_REAL_CST (arg2);
7750 real_copysign (&c1, &c2);
7751 return build_real (type, c1);
7752 c1.sign = c2.sign;
7755 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7756 Remember to evaluate Y for side-effects. */
7757 if (tree_expr_nonnegative_p (arg2))
7758 return omit_one_operand (type,
7759 fold (build1 (ABS_EXPR, type, arg1)),
7760 arg2);
7762 /* Strip sign changing operations for the first argument. */
7763 tem = fold_strip_sign_ops (arg1);
7764 if (tem)
7766 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7767 return build_function_call_expr (fndecl, arglist);
7770 return NULL_TREE;
7773 /* Fold a call to builtin isascii. */
7775 static tree
7776 fold_builtin_isascii (tree arglist)
7778 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7779 return 0;
7780 else
7782 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7783 tree arg = TREE_VALUE (arglist);
7785 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7786 build_int_cst (NULL_TREE,
7787 ~ (unsigned HOST_WIDE_INT) 0x7f));
7788 arg = fold (build2 (EQ_EXPR, integer_type_node,
7789 arg, integer_zero_node));
7791 if (in_gimple_form && !TREE_CONSTANT (arg))
7792 return NULL_TREE;
7793 else
7794 return arg;
7798 /* Fold a call to builtin toascii. */
7800 static tree
7801 fold_builtin_toascii (tree arglist)
7803 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7804 return 0;
7805 else
7807 /* Transform toascii(c) -> (c & 0x7f). */
7808 tree arg = TREE_VALUE (arglist);
7810 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7811 build_int_cst (NULL_TREE, 0x7f)));
7815 /* Fold a call to builtin isdigit. */
7817 static tree
7818 fold_builtin_isdigit (tree arglist)
7820 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7821 return 0;
7822 else
7824 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7825 /* According to the C standard, isdigit is unaffected by locale.
7826 However, it definitely is affected by the target character set. */
7827 tree arg;
7828 unsigned HOST_WIDE_INT target_digit0
7829 = lang_hooks.to_target_charset ('0');
7831 if (target_digit0 == 0)
7832 return NULL_TREE;
7834 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
7835 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7836 build_int_cst (unsigned_type_node, target_digit0));
7837 arg = build2 (LE_EXPR, integer_type_node, arg,
7838 build_int_cst (unsigned_type_node, 9));
7839 arg = fold (arg);
7840 if (in_gimple_form && !TREE_CONSTANT (arg))
7841 return NULL_TREE;
7842 else
7843 return arg;
7847 /* Fold a call to fabs, fabsf or fabsl. */
7849 static tree
7850 fold_builtin_fabs (tree arglist, tree type)
7852 tree arg;
7854 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7855 return 0;
7857 arg = TREE_VALUE (arglist);
7858 arg = fold_convert (type, arg);
7859 if (TREE_CODE (arg) == REAL_CST)
7860 return fold_abs_const (arg, type);
7861 return fold (build1 (ABS_EXPR, type, arg));
7864 /* Fold a call to abs, labs, llabs or imaxabs. */
7866 static tree
7867 fold_builtin_abs (tree arglist, tree type)
7869 tree arg;
7871 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7872 return 0;
7874 arg = TREE_VALUE (arglist);
7875 arg = fold_convert (type, arg);
7876 if (TREE_CODE (arg) == INTEGER_CST)
7877 return fold_abs_const (arg, type);
7878 return fold (build1 (ABS_EXPR, type, arg));
7881 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7882 EXP is the CALL_EXPR for the call. */
7884 static tree
7885 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
7887 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7888 tree arg;
7889 REAL_VALUE_TYPE r;
7891 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7893 /* Check that we have exactly one argument. */
7894 if (arglist == 0)
7896 error ("too few arguments to function %qs",
7897 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7898 return error_mark_node;
7900 else if (TREE_CHAIN (arglist) != 0)
7902 error ("too many arguments to function %qs",
7903 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7904 return error_mark_node;
7906 else
7908 error ("non-floating-point argument to function %qs",
7909 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7910 return error_mark_node;
7914 arg = TREE_VALUE (arglist);
7915 switch (builtin_index)
7917 case BUILT_IN_ISINF:
7918 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7919 return omit_one_operand (type, integer_zero_node, arg);
7921 if (TREE_CODE (arg) == REAL_CST)
7923 r = TREE_REAL_CST (arg);
7924 if (real_isinf (&r))
7925 return real_compare (GT_EXPR, &r, &dconst0)
7926 ? integer_one_node : integer_minus_one_node;
7927 else
7928 return integer_zero_node;
7931 return NULL_TREE;
7933 case BUILT_IN_FINITE:
7934 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7935 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7936 return omit_one_operand (type, integer_zero_node, arg);
7938 if (TREE_CODE (arg) == REAL_CST)
7940 r = TREE_REAL_CST (arg);
7941 return real_isinf (&r) || real_isnan (&r)
7942 ? integer_zero_node : integer_one_node;
7945 return NULL_TREE;
7947 case BUILT_IN_ISNAN:
7948 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7949 return omit_one_operand (type, integer_zero_node, arg);
7951 if (TREE_CODE (arg) == REAL_CST)
7953 r = TREE_REAL_CST (arg);
7954 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7957 arg = builtin_save_expr (arg);
7958 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7960 default:
7961 gcc_unreachable ();
7965 /* Fold a call to an unordered comparison function such as
7966 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
7967 being called and ARGLIST is the argument list for the call.
7968 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7969 the opposite of the desired result. UNORDERED_CODE is used
7970 for modes that can hold NaNs and ORDERED_CODE is used for
7971 the rest. */
7973 static tree
7974 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
7975 enum tree_code unordered_code,
7976 enum tree_code ordered_code)
7978 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7979 enum tree_code code;
7980 tree arg0, arg1;
7981 tree type0, type1;
7982 enum tree_code code0, code1;
7983 tree cmp_type = NULL_TREE;
7985 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7987 /* Check that we have exactly two arguments. */
7988 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7990 error ("too few arguments to function %qs",
7991 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7992 return error_mark_node;
7994 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7996 error ("too many arguments to function %qs",
7997 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7998 return error_mark_node;
8002 arg0 = TREE_VALUE (arglist);
8003 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8005 type0 = TREE_TYPE (arg0);
8006 type1 = TREE_TYPE (arg1);
8008 code0 = TREE_CODE (type0);
8009 code1 = TREE_CODE (type1);
8011 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8012 /* Choose the wider of two real types. */
8013 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8014 ? type0 : type1;
8015 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8016 cmp_type = type0;
8017 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8018 cmp_type = type1;
8019 else
8021 error ("non-floating-point argument to function %qs",
8022 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8023 return error_mark_node;
8026 arg0 = fold_convert (cmp_type, arg0);
8027 arg1 = fold_convert (cmp_type, arg1);
8029 if (unordered_code == UNORDERED_EXPR)
8031 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8032 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8033 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
8036 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8037 : ordered_code;
8038 return fold (build1 (TRUTH_NOT_EXPR, type,
8039 fold (build2 (code, type, arg0, arg1))));
8042 /* Fold a call to one of the external complex multiply libcalls. */
8044 static tree
8045 fold_builtin_complex_mul (tree type, tree arglist)
8047 tree ar, ai, br, bi;
8049 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
8050 REAL_TYPE, VOID_TYPE))
8051 return NULL;
8053 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8054 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8055 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8056 bi = TREE_VALUE (arglist);
8058 return fold_complex_mult_parts (type, ar, ai, br, bi);
8061 /* Fold a call to one of the external complex division libcalls. */
8063 static tree
8064 fold_builtin_complex_div (tree type, tree arglist)
8066 tree ar, ai, br, bi;
8068 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
8069 REAL_TYPE, VOID_TYPE))
8070 return NULL;
8072 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8073 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8074 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
8075 bi = TREE_VALUE (arglist);
8077 return fold_complex_div_parts (type, ar, ai, br, bi, RDIV_EXPR);
8080 /* Used by constant folding to simplify calls to builtin functions. EXP is
8081 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8082 result of the function call is ignored. This function returns NULL_TREE
8083 if no simplification was possible. */
8085 static tree
8086 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
8088 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8089 enum built_in_function fcode;
8091 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8092 return targetm.fold_builtin (fndecl, arglist, ignore);
8094 fcode = DECL_FUNCTION_CODE (fndecl);
8095 switch (fcode)
8097 case BUILT_IN_FPUTS:
8098 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8100 case BUILT_IN_FPUTS_UNLOCKED:
8101 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8103 case BUILT_IN_STRSTR:
8104 return fold_builtin_strstr (arglist, type);
8106 case BUILT_IN_STRCAT:
8107 return fold_builtin_strcat (arglist);
8109 case BUILT_IN_STRNCAT:
8110 return fold_builtin_strncat (arglist);
8112 case BUILT_IN_STRSPN:
8113 return fold_builtin_strspn (arglist);
8115 case BUILT_IN_STRCSPN:
8116 return fold_builtin_strcspn (arglist);
8118 case BUILT_IN_STRCHR:
8119 case BUILT_IN_INDEX:
8120 return fold_builtin_strchr (arglist, type);
8122 case BUILT_IN_STRRCHR:
8123 case BUILT_IN_RINDEX:
8124 return fold_builtin_strrchr (arglist, type);
8126 case BUILT_IN_STRCPY:
8127 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
8129 case BUILT_IN_STRNCPY:
8130 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8132 case BUILT_IN_STRCMP:
8133 return fold_builtin_strcmp (arglist);
8135 case BUILT_IN_STRNCMP:
8136 return fold_builtin_strncmp (arglist);
8138 case BUILT_IN_STRPBRK:
8139 return fold_builtin_strpbrk (arglist, type);
8141 case BUILT_IN_BCMP:
8142 case BUILT_IN_MEMCMP:
8143 return fold_builtin_memcmp (arglist);
8145 case BUILT_IN_SPRINTF:
8146 return fold_builtin_sprintf (arglist, ignore);
8148 case BUILT_IN_CONSTANT_P:
8150 tree val;
8152 val = fold_builtin_constant_p (arglist);
8153 /* Gimplification will pull the CALL_EXPR for the builtin out of
8154 an if condition. When not optimizing, we'll not CSE it back.
8155 To avoid link error types of regressions, return false now. */
8156 if (!val && !optimize)
8157 val = integer_zero_node;
8159 return val;
8162 case BUILT_IN_EXPECT:
8163 return fold_builtin_expect (arglist);
8165 case BUILT_IN_CLASSIFY_TYPE:
8166 return fold_builtin_classify_type (arglist);
8168 case BUILT_IN_STRLEN:
8169 return fold_builtin_strlen (arglist);
8171 case BUILT_IN_FABS:
8172 case BUILT_IN_FABSF:
8173 case BUILT_IN_FABSL:
8174 return fold_builtin_fabs (arglist, type);
8176 case BUILT_IN_ABS:
8177 case BUILT_IN_LABS:
8178 case BUILT_IN_LLABS:
8179 case BUILT_IN_IMAXABS:
8180 return fold_builtin_abs (arglist, type);
8182 case BUILT_IN_CONJ:
8183 case BUILT_IN_CONJF:
8184 case BUILT_IN_CONJL:
8185 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8186 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
8187 break;
8189 case BUILT_IN_CREAL:
8190 case BUILT_IN_CREALF:
8191 case BUILT_IN_CREALL:
8192 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8193 return non_lvalue (fold (build1 (REALPART_EXPR, type,
8194 TREE_VALUE (arglist))));
8195 break;
8197 case BUILT_IN_CIMAG:
8198 case BUILT_IN_CIMAGF:
8199 case BUILT_IN_CIMAGL:
8200 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8201 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8202 TREE_VALUE (arglist))));
8203 break;
8205 case BUILT_IN_CABS:
8206 case BUILT_IN_CABSF:
8207 case BUILT_IN_CABSL:
8208 return fold_builtin_cabs (arglist, type);
8210 case BUILT_IN_SQRT:
8211 case BUILT_IN_SQRTF:
8212 case BUILT_IN_SQRTL:
8213 return fold_builtin_sqrt (arglist, type);
8215 case BUILT_IN_CBRT:
8216 case BUILT_IN_CBRTF:
8217 case BUILT_IN_CBRTL:
8218 return fold_builtin_cbrt (arglist, type);
8220 case BUILT_IN_SIN:
8221 case BUILT_IN_SINF:
8222 case BUILT_IN_SINL:
8223 return fold_builtin_sin (arglist);
8225 case BUILT_IN_COS:
8226 case BUILT_IN_COSF:
8227 case BUILT_IN_COSL:
8228 return fold_builtin_cos (arglist, type, fndecl);
8230 case BUILT_IN_EXP:
8231 case BUILT_IN_EXPF:
8232 case BUILT_IN_EXPL:
8233 return fold_builtin_exponent (fndecl, arglist, &dconste);
8235 case BUILT_IN_EXP2:
8236 case BUILT_IN_EXP2F:
8237 case BUILT_IN_EXP2L:
8238 return fold_builtin_exponent (fndecl, arglist, &dconst2);
8240 case BUILT_IN_EXP10:
8241 case BUILT_IN_EXP10F:
8242 case BUILT_IN_EXP10L:
8243 case BUILT_IN_POW10:
8244 case BUILT_IN_POW10F:
8245 case BUILT_IN_POW10L:
8246 return fold_builtin_exponent (fndecl, arglist, &dconst10);
8248 case BUILT_IN_LOG:
8249 case BUILT_IN_LOGF:
8250 case BUILT_IN_LOGL:
8251 return fold_builtin_logarithm (fndecl, arglist, &dconste);
8253 case BUILT_IN_LOG2:
8254 case BUILT_IN_LOG2F:
8255 case BUILT_IN_LOG2L:
8256 return fold_builtin_logarithm (fndecl, arglist, &dconst2);
8258 case BUILT_IN_LOG10:
8259 case BUILT_IN_LOG10F:
8260 case BUILT_IN_LOG10L:
8261 return fold_builtin_logarithm (fndecl, arglist, &dconst10);
8263 case BUILT_IN_TAN:
8264 case BUILT_IN_TANF:
8265 case BUILT_IN_TANL:
8266 return fold_builtin_tan (arglist);
8268 case BUILT_IN_ATAN:
8269 case BUILT_IN_ATANF:
8270 case BUILT_IN_ATANL:
8271 return fold_builtin_atan (arglist, type);
8273 case BUILT_IN_POW:
8274 case BUILT_IN_POWF:
8275 case BUILT_IN_POWL:
8276 return fold_builtin_pow (fndecl, arglist, type);
8278 case BUILT_IN_POWI:
8279 case BUILT_IN_POWIF:
8280 case BUILT_IN_POWIL:
8281 return fold_builtin_powi (fndecl, arglist, type);
8283 case BUILT_IN_INF:
8284 case BUILT_IN_INFF:
8285 case BUILT_IN_INFL:
8286 return fold_builtin_inf (type, true);
8288 case BUILT_IN_HUGE_VAL:
8289 case BUILT_IN_HUGE_VALF:
8290 case BUILT_IN_HUGE_VALL:
8291 return fold_builtin_inf (type, false);
8293 case BUILT_IN_NAN:
8294 case BUILT_IN_NANF:
8295 case BUILT_IN_NANL:
8296 return fold_builtin_nan (arglist, type, true);
8298 case BUILT_IN_NANS:
8299 case BUILT_IN_NANSF:
8300 case BUILT_IN_NANSL:
8301 return fold_builtin_nan (arglist, type, false);
8303 case BUILT_IN_FLOOR:
8304 case BUILT_IN_FLOORF:
8305 case BUILT_IN_FLOORL:
8306 return fold_builtin_floor (fndecl, arglist);
8308 case BUILT_IN_CEIL:
8309 case BUILT_IN_CEILF:
8310 case BUILT_IN_CEILL:
8311 return fold_builtin_ceil (fndecl, arglist);
8313 case BUILT_IN_TRUNC:
8314 case BUILT_IN_TRUNCF:
8315 case BUILT_IN_TRUNCL:
8316 return fold_builtin_trunc (fndecl, arglist);
8318 case BUILT_IN_ROUND:
8319 case BUILT_IN_ROUNDF:
8320 case BUILT_IN_ROUNDL:
8321 return fold_builtin_round (fndecl, arglist);
8323 case BUILT_IN_NEARBYINT:
8324 case BUILT_IN_NEARBYINTF:
8325 case BUILT_IN_NEARBYINTL:
8326 case BUILT_IN_RINT:
8327 case BUILT_IN_RINTF:
8328 case BUILT_IN_RINTL:
8329 return fold_trunc_transparent_mathfn (fndecl, arglist);
8331 case BUILT_IN_LFLOOR:
8332 case BUILT_IN_LFLOORF:
8333 case BUILT_IN_LFLOORL:
8334 case BUILT_IN_LLFLOOR:
8335 case BUILT_IN_LLFLOORF:
8336 case BUILT_IN_LLFLOORL:
8337 case BUILT_IN_LROUND:
8338 case BUILT_IN_LROUNDF:
8339 case BUILT_IN_LROUNDL:
8340 case BUILT_IN_LLROUND:
8341 case BUILT_IN_LLROUNDF:
8342 case BUILT_IN_LLROUNDL:
8343 return fold_builtin_int_roundingfn (fndecl, arglist);
8345 case BUILT_IN_LRINT:
8346 case BUILT_IN_LRINTF:
8347 case BUILT_IN_LRINTL:
8348 case BUILT_IN_LLRINT:
8349 case BUILT_IN_LLRINTF:
8350 case BUILT_IN_LLRINTL:
8351 return fold_fixed_mathfn (fndecl, arglist);
8353 case BUILT_IN_FFS:
8354 case BUILT_IN_FFSL:
8355 case BUILT_IN_FFSLL:
8356 case BUILT_IN_CLZ:
8357 case BUILT_IN_CLZL:
8358 case BUILT_IN_CLZLL:
8359 case BUILT_IN_CTZ:
8360 case BUILT_IN_CTZL:
8361 case BUILT_IN_CTZLL:
8362 case BUILT_IN_POPCOUNT:
8363 case BUILT_IN_POPCOUNTL:
8364 case BUILT_IN_POPCOUNTLL:
8365 case BUILT_IN_PARITY:
8366 case BUILT_IN_PARITYL:
8367 case BUILT_IN_PARITYLL:
8368 return fold_builtin_bitop (fndecl, arglist);
8370 case BUILT_IN_MEMCPY:
8371 return fold_builtin_memcpy (fndecl, arglist);
8373 case BUILT_IN_MEMPCPY:
8374 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8376 case BUILT_IN_MEMMOVE:
8377 return fold_builtin_memmove (arglist, type);
8379 case BUILT_IN_SIGNBIT:
8380 case BUILT_IN_SIGNBITF:
8381 case BUILT_IN_SIGNBITL:
8382 return fold_builtin_signbit (fndecl, arglist);
8384 case BUILT_IN_ISASCII:
8385 return fold_builtin_isascii (arglist);
8387 case BUILT_IN_TOASCII:
8388 return fold_builtin_toascii (arglist);
8390 case BUILT_IN_ISDIGIT:
8391 return fold_builtin_isdigit (arglist);
8393 case BUILT_IN_COPYSIGN:
8394 case BUILT_IN_COPYSIGNF:
8395 case BUILT_IN_COPYSIGNL:
8396 return fold_builtin_copysign (fndecl, arglist, type);
8398 case BUILT_IN_FINITE:
8399 case BUILT_IN_FINITEF:
8400 case BUILT_IN_FINITEL:
8401 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
8403 case BUILT_IN_ISINF:
8404 case BUILT_IN_ISINFF:
8405 case BUILT_IN_ISINFL:
8406 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
8408 case BUILT_IN_ISNAN:
8409 case BUILT_IN_ISNANF:
8410 case BUILT_IN_ISNANL:
8411 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
8413 case BUILT_IN_ISGREATER:
8414 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
8415 case BUILT_IN_ISGREATEREQUAL:
8416 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
8417 case BUILT_IN_ISLESS:
8418 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
8419 case BUILT_IN_ISLESSEQUAL:
8420 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
8421 case BUILT_IN_ISLESSGREATER:
8422 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
8423 case BUILT_IN_ISUNORDERED:
8424 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
8425 NOP_EXPR);
8427 /* We do the folding for va_start in the expander. */
8428 case BUILT_IN_VA_START:
8429 break;
8431 default:
8432 if (fcode >= BUILT_IN_COMPLEX_MUL_MIN
8433 && fcode <= BUILT_IN_COMPLEX_MUL_MAX)
8434 return fold_builtin_complex_mul (type, arglist);
8435 if (fcode >= BUILT_IN_COMPLEX_DIV_MIN
8436 && fcode <= BUILT_IN_COMPLEX_DIV_MAX)
8437 return fold_builtin_complex_div (type, arglist);
8438 break;
8441 return 0;
8444 /* A wrapper function for builtin folding that prevents warnings for
8445 "statement without effect" and the like, caused by removing the
8446 call node earlier than the warning is generated. */
8448 tree
8449 fold_builtin (tree fndecl, tree arglist, bool ignore)
8451 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
8452 if (exp)
8454 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8455 if (CONSTANT_CLASS_P (exp))
8456 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8457 TREE_NO_WARNING (exp) = 1;
8460 return exp;
8463 /* Conveniently construct a function call expression. */
8465 tree
8466 build_function_call_expr (tree fn, tree arglist)
8468 tree call_expr;
8470 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8471 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8472 call_expr, arglist, NULL_TREE);
8473 return fold (call_expr);
8476 /* This function validates the types of a function call argument list
8477 represented as a tree chain of parameters against a specified list
8478 of tree_codes. If the last specifier is a 0, that represents an
8479 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8481 static int
8482 validate_arglist (tree arglist, ...)
8484 enum tree_code code;
8485 int res = 0;
8486 va_list ap;
8488 va_start (ap, arglist);
8492 code = va_arg (ap, enum tree_code);
8493 switch (code)
8495 case 0:
8496 /* This signifies an ellipses, any further arguments are all ok. */
8497 res = 1;
8498 goto end;
8499 case VOID_TYPE:
8500 /* This signifies an endlink, if no arguments remain, return
8501 true, otherwise return false. */
8502 res = arglist == 0;
8503 goto end;
8504 default:
8505 /* If no parameters remain or the parameter's code does not
8506 match the specified code, return false. Otherwise continue
8507 checking any remaining arguments. */
8508 if (arglist == 0)
8509 goto end;
8510 if (code == POINTER_TYPE)
8512 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8513 goto end;
8515 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8516 goto end;
8517 break;
8519 arglist = TREE_CHAIN (arglist);
8521 while (1);
8523 /* We need gotos here since we can only have one VA_CLOSE in a
8524 function. */
8525 end: ;
8526 va_end (ap);
8528 return res;
8531 /* Default target-specific builtin expander that does nothing. */
8534 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8535 rtx target ATTRIBUTE_UNUSED,
8536 rtx subtarget ATTRIBUTE_UNUSED,
8537 enum machine_mode mode ATTRIBUTE_UNUSED,
8538 int ignore ATTRIBUTE_UNUSED)
8540 return NULL_RTX;
8543 /* Returns true is EXP represents data that would potentially reside
8544 in a readonly section. */
8546 static bool
8547 readonly_data_expr (tree exp)
8549 STRIP_NOPS (exp);
8551 if (TREE_CODE (exp) != ADDR_EXPR)
8552 return false;
8554 exp = get_base_address (TREE_OPERAND (exp, 0));
8555 if (!exp)
8556 return false;
8558 /* Make sure we call decl_readonly_section only for trees it
8559 can handle (since it returns true for everything it doesn't
8560 understand). */
8561 if (TREE_CODE (exp) == STRING_CST
8562 || TREE_CODE (exp) == CONSTRUCTOR
8563 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8564 return decl_readonly_section (exp, 0);
8565 else
8566 return false;
8569 /* Simplify a call to the strstr builtin.
8571 Return 0 if no simplification was possible, otherwise return the
8572 simplified form of the call as a tree.
8574 The simplified form may be a constant or other expression which
8575 computes the same value, but in a more efficient manner (including
8576 calls to other builtin functions).
8578 The call may contain arguments which need to be evaluated, but
8579 which are not useful to determine the result of the call. In
8580 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8581 COMPOUND_EXPR will be an argument which must be evaluated.
8582 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8583 COMPOUND_EXPR in the chain will contain the tree for the simplified
8584 form of the builtin function call. */
8586 static tree
8587 fold_builtin_strstr (tree arglist, tree type)
8589 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8590 return 0;
8591 else
8593 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8594 tree fn;
8595 const char *p1, *p2;
8597 p2 = c_getstr (s2);
8598 if (p2 == NULL)
8599 return 0;
8601 p1 = c_getstr (s1);
8602 if (p1 != NULL)
8604 const char *r = strstr (p1, p2);
8605 tree tem;
8607 if (r == NULL)
8608 return build_int_cst (TREE_TYPE (s1), 0);
8610 /* Return an offset into the constant string argument. */
8611 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8612 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8613 return fold_convert (type, tem);
8616 if (p2[0] == '\0')
8617 return s1;
8619 if (p2[1] != '\0')
8620 return 0;
8622 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8623 if (!fn)
8624 return 0;
8626 /* New argument list transforming strstr(s1, s2) to
8627 strchr(s1, s2[0]). */
8628 arglist = build_tree_list (NULL_TREE,
8629 build_int_cst (NULL_TREE, p2[0]));
8630 arglist = tree_cons (NULL_TREE, s1, arglist);
8631 return build_function_call_expr (fn, arglist);
8635 /* Simplify a call to the strchr builtin.
8637 Return 0 if no simplification was possible, otherwise return the
8638 simplified form of the call as a tree.
8640 The simplified form may be a constant or other expression which
8641 computes the same value, but in a more efficient manner (including
8642 calls to other builtin functions).
8644 The call may contain arguments which need to be evaluated, but
8645 which are not useful to determine the result of the call. In
8646 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8647 COMPOUND_EXPR will be an argument which must be evaluated.
8648 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8649 COMPOUND_EXPR in the chain will contain the tree for the simplified
8650 form of the builtin function call. */
8652 static tree
8653 fold_builtin_strchr (tree arglist, tree type)
8655 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8656 return 0;
8657 else
8659 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8660 const char *p1;
8662 if (TREE_CODE (s2) != INTEGER_CST)
8663 return 0;
8665 p1 = c_getstr (s1);
8666 if (p1 != NULL)
8668 char c;
8669 const char *r;
8670 tree tem;
8672 if (target_char_cast (s2, &c))
8673 return 0;
8675 r = strchr (p1, c);
8677 if (r == NULL)
8678 return build_int_cst (TREE_TYPE (s1), 0);
8680 /* Return an offset into the constant string argument. */
8681 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8682 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8683 return fold_convert (type, tem);
8685 return 0;
8689 /* Simplify a call to the strrchr builtin.
8691 Return 0 if no simplification was possible, otherwise return the
8692 simplified form of the call as a tree.
8694 The simplified form may be a constant or other expression which
8695 computes the same value, but in a more efficient manner (including
8696 calls to other builtin functions).
8698 The call may contain arguments which need to be evaluated, but
8699 which are not useful to determine the result of the call. In
8700 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8701 COMPOUND_EXPR will be an argument which must be evaluated.
8702 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8703 COMPOUND_EXPR in the chain will contain the tree for the simplified
8704 form of the builtin function call. */
8706 static tree
8707 fold_builtin_strrchr (tree arglist, tree type)
8709 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8710 return 0;
8711 else
8713 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8714 tree fn;
8715 const char *p1;
8717 if (TREE_CODE (s2) != INTEGER_CST)
8718 return 0;
8720 p1 = c_getstr (s1);
8721 if (p1 != NULL)
8723 char c;
8724 const char *r;
8725 tree tem;
8727 if (target_char_cast (s2, &c))
8728 return 0;
8730 r = strrchr (p1, c);
8732 if (r == NULL)
8733 return build_int_cst (TREE_TYPE (s1), 0);
8735 /* Return an offset into the constant string argument. */
8736 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8737 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8738 return fold_convert (type, tem);
8741 if (! integer_zerop (s2))
8742 return 0;
8744 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8745 if (!fn)
8746 return 0;
8748 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8749 return build_function_call_expr (fn, arglist);
8753 /* Simplify a call to the strpbrk builtin.
8755 Return 0 if no simplification was possible, otherwise return the
8756 simplified form of the call as a tree.
8758 The simplified form may be a constant or other expression which
8759 computes the same value, but in a more efficient manner (including
8760 calls to other builtin functions).
8762 The call may contain arguments which need to be evaluated, but
8763 which are not useful to determine the result of the call. In
8764 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8765 COMPOUND_EXPR will be an argument which must be evaluated.
8766 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8767 COMPOUND_EXPR in the chain will contain the tree for the simplified
8768 form of the builtin function call. */
8770 static tree
8771 fold_builtin_strpbrk (tree arglist, tree type)
8773 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8774 return 0;
8775 else
8777 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8778 tree fn;
8779 const char *p1, *p2;
8781 p2 = c_getstr (s2);
8782 if (p2 == NULL)
8783 return 0;
8785 p1 = c_getstr (s1);
8786 if (p1 != NULL)
8788 const char *r = strpbrk (p1, p2);
8789 tree tem;
8791 if (r == NULL)
8792 return build_int_cst (TREE_TYPE (s1), 0);
8794 /* Return an offset into the constant string argument. */
8795 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8796 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8797 return fold_convert (type, tem);
8800 if (p2[0] == '\0')
8801 /* strpbrk(x, "") == NULL.
8802 Evaluate and ignore s1 in case it had side-effects. */
8803 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8805 if (p2[1] != '\0')
8806 return 0; /* Really call strpbrk. */
8808 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8809 if (!fn)
8810 return 0;
8812 /* New argument list transforming strpbrk(s1, s2) to
8813 strchr(s1, s2[0]). */
8814 arglist = build_tree_list (NULL_TREE,
8815 build_int_cst (NULL_TREE, p2[0]));
8816 arglist = tree_cons (NULL_TREE, s1, arglist);
8817 return build_function_call_expr (fn, arglist);
8821 /* Simplify a call to the strcat builtin.
8823 Return 0 if no simplification was possible, otherwise return the
8824 simplified form of the call as a tree.
8826 The simplified form may be a constant or other expression which
8827 computes the same value, but in a more efficient manner (including
8828 calls to other builtin functions).
8830 The call may contain arguments which need to be evaluated, but
8831 which are not useful to determine the result of the call. In
8832 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8833 COMPOUND_EXPR will be an argument which must be evaluated.
8834 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8835 COMPOUND_EXPR in the chain will contain the tree for the simplified
8836 form of the builtin function call. */
8838 static tree
8839 fold_builtin_strcat (tree arglist)
8841 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8842 return 0;
8843 else
8845 tree dst = TREE_VALUE (arglist),
8846 src = TREE_VALUE (TREE_CHAIN (arglist));
8847 const char *p = c_getstr (src);
8849 /* If the string length is zero, return the dst parameter. */
8850 if (p && *p == '\0')
8851 return dst;
8853 return 0;
8857 /* Simplify a call to the strncat builtin.
8859 Return 0 if no simplification was possible, otherwise return the
8860 simplified form of the call as a tree.
8862 The simplified form may be a constant or other expression which
8863 computes the same value, but in a more efficient manner (including
8864 calls to other builtin functions).
8866 The call may contain arguments which need to be evaluated, but
8867 which are not useful to determine the result of the call. In
8868 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8869 COMPOUND_EXPR will be an argument which must be evaluated.
8870 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8871 COMPOUND_EXPR in the chain will contain the tree for the simplified
8872 form of the builtin function call. */
8874 static tree
8875 fold_builtin_strncat (tree arglist)
8877 if (!validate_arglist (arglist,
8878 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8879 return 0;
8880 else
8882 tree dst = TREE_VALUE (arglist);
8883 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8884 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8885 const char *p = c_getstr (src);
8887 /* If the requested length is zero, or the src parameter string
8888 length is zero, return the dst parameter. */
8889 if (integer_zerop (len) || (p && *p == '\0'))
8890 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8892 /* If the requested len is greater than or equal to the string
8893 length, call strcat. */
8894 if (TREE_CODE (len) == INTEGER_CST && p
8895 && compare_tree_int (len, strlen (p)) >= 0)
8897 tree newarglist
8898 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8899 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8901 /* If the replacement _DECL isn't initialized, don't do the
8902 transformation. */
8903 if (!fn)
8904 return 0;
8906 return build_function_call_expr (fn, newarglist);
8908 return 0;
8912 /* Simplify a call to the strspn builtin.
8914 Return 0 if no simplification was possible, otherwise return the
8915 simplified form of the call as a tree.
8917 The simplified form may be a constant or other expression which
8918 computes the same value, but in a more efficient manner (including
8919 calls to other builtin functions).
8921 The call may contain arguments which need to be evaluated, but
8922 which are not useful to determine the result of the call. In
8923 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8924 COMPOUND_EXPR will be an argument which must be evaluated.
8925 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8926 COMPOUND_EXPR in the chain will contain the tree for the simplified
8927 form of the builtin function call. */
8929 static tree
8930 fold_builtin_strspn (tree arglist)
8932 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8933 return 0;
8934 else
8936 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8937 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8939 /* If both arguments are constants, evaluate at compile-time. */
8940 if (p1 && p2)
8942 const size_t r = strspn (p1, p2);
8943 return size_int (r);
8946 /* If either argument is "", return 0. */
8947 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8948 /* Evaluate and ignore both arguments in case either one has
8949 side-effects. */
8950 return omit_two_operands (integer_type_node, integer_zero_node,
8951 s1, s2);
8952 return 0;
8956 /* Simplify a call to the strcspn builtin.
8958 Return 0 if no simplification was possible, otherwise return the
8959 simplified form of the call as a tree.
8961 The simplified form may be a constant or other expression which
8962 computes the same value, but in a more efficient manner (including
8963 calls to other builtin functions).
8965 The call may contain arguments which need to be evaluated, but
8966 which are not useful to determine the result of the call. In
8967 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8968 COMPOUND_EXPR will be an argument which must be evaluated.
8969 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8970 COMPOUND_EXPR in the chain will contain the tree for the simplified
8971 form of the builtin function call. */
8973 static tree
8974 fold_builtin_strcspn (tree arglist)
8976 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8977 return 0;
8978 else
8980 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8981 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8983 /* If both arguments are constants, evaluate at compile-time. */
8984 if (p1 && p2)
8986 const size_t r = strcspn (p1, p2);
8987 return size_int (r);
8990 /* If the first argument is "", return 0. */
8991 if (p1 && *p1 == '\0')
8993 /* Evaluate and ignore argument s2 in case it has
8994 side-effects. */
8995 return omit_one_operand (integer_type_node,
8996 integer_zero_node, s2);
8999 /* If the second argument is "", return __builtin_strlen(s1). */
9000 if (p2 && *p2 == '\0')
9002 tree newarglist = build_tree_list (NULL_TREE, s1),
9003 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
9005 /* If the replacement _DECL isn't initialized, don't do the
9006 transformation. */
9007 if (!fn)
9008 return 0;
9010 return build_function_call_expr (fn, newarglist);
9012 return 0;
9016 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
9017 by the builtin will be ignored. UNLOCKED is true is true if this
9018 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9019 the known length of the string. Return NULL_TREE if no simplification
9020 was possible. */
9022 tree
9023 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
9025 tree fn;
9026 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9027 : implicit_built_in_decls[BUILT_IN_FPUTC];
9028 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9029 : implicit_built_in_decls[BUILT_IN_FWRITE];
9031 /* If the return value is used, or the replacement _DECL isn't
9032 initialized, don't do the transformation. */
9033 if (!ignore || !fn_fputc || !fn_fwrite)
9034 return 0;
9036 /* Verify the arguments in the original call. */
9037 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9038 return 0;
9040 if (! len)
9041 len = c_strlen (TREE_VALUE (arglist), 0);
9043 /* Get the length of the string passed to fputs. If the length
9044 can't be determined, punt. */
9045 if (!len
9046 || TREE_CODE (len) != INTEGER_CST)
9047 return 0;
9049 switch (compare_tree_int (len, 1))
9051 case -1: /* length is 0, delete the call entirely . */
9052 return omit_one_operand (integer_type_node, integer_zero_node,
9053 TREE_VALUE (TREE_CHAIN (arglist)));
9055 case 0: /* length is 1, call fputc. */
9057 const char *p = c_getstr (TREE_VALUE (arglist));
9059 if (p != NULL)
9061 /* New argument list transforming fputs(string, stream) to
9062 fputc(string[0], stream). */
9063 arglist = build_tree_list (NULL_TREE,
9064 TREE_VALUE (TREE_CHAIN (arglist)));
9065 arglist = tree_cons (NULL_TREE,
9066 build_int_cst (NULL_TREE, p[0]),
9067 arglist);
9068 fn = fn_fputc;
9069 break;
9072 /* FALLTHROUGH */
9073 case 1: /* length is greater than 1, call fwrite. */
9075 tree string_arg;
9077 /* If optimizing for size keep fputs. */
9078 if (optimize_size)
9079 return 0;
9080 string_arg = TREE_VALUE (arglist);
9081 /* New argument list transforming fputs(string, stream) to
9082 fwrite(string, 1, len, stream). */
9083 arglist = build_tree_list (NULL_TREE,
9084 TREE_VALUE (TREE_CHAIN (arglist)));
9085 arglist = tree_cons (NULL_TREE, len, arglist);
9086 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9087 arglist = tree_cons (NULL_TREE, string_arg, arglist);
9088 fn = fn_fwrite;
9089 break;
9091 default:
9092 gcc_unreachable ();
9095 /* These optimizations are only performed when the result is ignored,
9096 hence there's no need to cast the result to integer_type_node. */
9097 return build_function_call_expr (fn, arglist);
9100 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9101 produced. False otherwise. This is done so that we don't output the error
9102 or warning twice or three times. */
9103 bool
9104 fold_builtin_next_arg (tree arglist)
9106 tree fntype = TREE_TYPE (current_function_decl);
9108 if (TYPE_ARG_TYPES (fntype) == 0
9109 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9110 == void_type_node))
9112 error ("%<va_start%> used in function with fixed args");
9113 return true;
9115 else if (!arglist)
9117 /* Evidently an out of date version of <stdarg.h>; can't validate
9118 va_start's second argument, but can still work as intended. */
9119 warning ("%<__builtin_next_arg%> called without an argument");
9120 return true;
9122 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9123 when we checked the arguments and if needed issued a warning. */
9124 else if (!TREE_CHAIN (arglist)
9125 || !integer_zerop (TREE_VALUE (arglist))
9126 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
9127 || TREE_CHAIN (TREE_CHAIN (arglist)))
9129 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9130 tree arg = TREE_VALUE (arglist);
9132 if (TREE_CHAIN (arglist))
9134 error ("%<va_start%> used with too many arguments");
9135 return true;
9138 /* Strip off all nops for the sake of the comparison. This
9139 is not quite the same as STRIP_NOPS. It does more.
9140 We must also strip off INDIRECT_EXPR for C++ reference
9141 parameters. */
9142 while (TREE_CODE (arg) == NOP_EXPR
9143 || TREE_CODE (arg) == CONVERT_EXPR
9144 || TREE_CODE (arg) == NON_LVALUE_EXPR
9145 || TREE_CODE (arg) == INDIRECT_REF)
9146 arg = TREE_OPERAND (arg, 0);
9147 if (arg != last_parm)
9149 /* FIXME: Sometimes with the tree optimizers we can get the
9150 not the last argument even though the user used the last
9151 argument. We just warn and set the arg to be the last
9152 argument so that we will get wrong-code because of
9153 it. */
9154 warning ("second parameter of %<va_start%> not last named argument");
9156 /* We want to verify the second parameter just once before the tree
9157 optimizers are run and then avoid keeping it in the tree,
9158 as otherwise we could warn even for correct code like:
9159 void foo (int i, ...)
9160 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9161 TREE_VALUE (arglist) = integer_zero_node;
9162 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9164 return false;
9168 /* Simplify a call to the sprintf builtin.
9170 Return 0 if no simplification was possible, otherwise return the
9171 simplified form of the call as a tree. If IGNORED is true, it means that
9172 the caller does not use the returned value of the function. */
9174 static tree
9175 fold_builtin_sprintf (tree arglist, int ignored)
9177 tree call, retval, dest, fmt;
9178 const char *fmt_str = NULL;
9180 /* Verify the required arguments in the original call. We deal with two
9181 types of sprintf() calls: 'sprintf (str, fmt)' and
9182 'sprintf (dest, "%s", orig)'. */
9183 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9184 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9185 VOID_TYPE))
9186 return NULL_TREE;
9188 /* Get the destination string and the format specifier. */
9189 dest = TREE_VALUE (arglist);
9190 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9192 /* Check whether the format is a literal string constant. */
9193 fmt_str = c_getstr (fmt);
9194 if (fmt_str == NULL)
9195 return NULL_TREE;
9197 call = NULL_TREE;
9198 retval = NULL_TREE;
9200 /* If the format doesn't contain % args or %%, use strcpy. */
9201 if (strchr (fmt_str, '%') == NULL)
9203 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9205 if (!fn)
9206 return NULL_TREE;
9208 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9209 'format' is known to contain no % formats. */
9210 arglist = build_tree_list (NULL_TREE, fmt);
9211 arglist = tree_cons (NULL_TREE, dest, arglist);
9212 call = build_function_call_expr (fn, arglist);
9213 if (!ignored)
9214 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9217 /* If the format is "%s", use strcpy if the result isn't used. */
9218 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9220 tree fn, orig;
9221 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9223 if (!fn)
9224 return NULL_TREE;
9226 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9227 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9228 arglist = build_tree_list (NULL_TREE, orig);
9229 arglist = tree_cons (NULL_TREE, dest, arglist);
9230 if (!ignored)
9232 retval = c_strlen (orig, 1);
9233 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9234 return NULL_TREE;
9236 call = build_function_call_expr (fn, arglist);
9239 if (call && retval)
9241 retval = convert
9242 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9243 retval);
9244 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
9246 else
9247 return call;