2005-04-05 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / builtins.c
blob56bdc873df345f3a967e6866505b4ef5e97a355c
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_args_info (tree);
102 static rtx expand_builtin_next_arg (void);
103 static rtx expand_builtin_va_start (tree);
104 static rtx expand_builtin_va_end (tree);
105 static rtx expand_builtin_va_copy (tree);
106 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
107 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
108 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
109 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
110 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
114 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
115 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
116 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_bcopy (tree, tree);
118 static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
119 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
120 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
122 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_bzero (tree);
126 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
130 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
131 static rtx expand_builtin_alloca (tree, rtx);
132 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
133 static rtx expand_builtin_frame_address (tree, tree);
134 static rtx expand_builtin_fputs (tree, rtx, bool);
135 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
136 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
137 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
138 static tree stabilize_va_list (tree, int);
139 static rtx expand_builtin_expect (tree, rtx);
140 static tree fold_builtin_constant_p (tree);
141 static tree fold_builtin_classify_type (tree);
142 static tree fold_builtin_strlen (tree);
143 static tree fold_builtin_inf (tree, int);
144 static tree fold_builtin_nan (tree, tree, int);
145 static int validate_arglist (tree, ...);
146 static bool integer_valued_real_p (tree);
147 static tree fold_trunc_transparent_mathfn (tree, tree);
148 static bool readonly_data_expr (tree);
149 static rtx expand_builtin_fabs (tree, rtx, rtx);
150 static rtx expand_builtin_signbit (tree, rtx);
151 static tree fold_builtin_cabs (tree, tree);
152 static tree fold_builtin_sqrt (tree, tree);
153 static tree fold_builtin_cbrt (tree, tree);
154 static tree fold_builtin_pow (tree, tree, tree);
155 static tree fold_builtin_powi (tree, tree, tree);
156 static tree fold_builtin_sin (tree);
157 static tree fold_builtin_cos (tree, tree, tree);
158 static tree fold_builtin_tan (tree);
159 static tree fold_builtin_atan (tree, tree);
160 static tree fold_builtin_trunc (tree, tree);
161 static tree fold_builtin_floor (tree, tree);
162 static tree fold_builtin_ceil (tree, tree);
163 static tree fold_builtin_round (tree, tree);
164 static tree fold_builtin_bitop (tree, tree);
165 static tree fold_builtin_memcpy (tree, tree);
166 static tree fold_builtin_mempcpy (tree, tree, int);
167 static tree fold_builtin_memmove (tree, tree);
168 static tree fold_builtin_strchr (tree, tree);
169 static tree fold_builtin_memcmp (tree);
170 static tree fold_builtin_strcmp (tree);
171 static tree fold_builtin_strncmp (tree);
172 static tree fold_builtin_signbit (tree, tree);
173 static tree fold_builtin_copysign (tree, tree, tree);
174 static tree fold_builtin_isascii (tree);
175 static tree fold_builtin_toascii (tree);
176 static tree fold_builtin_isdigit (tree);
177 static tree fold_builtin_fabs (tree, tree);
178 static tree fold_builtin_abs (tree, tree);
179 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
180 enum tree_code);
181 static tree fold_builtin_1 (tree, tree, bool);
183 static tree fold_builtin_strpbrk (tree, tree);
184 static tree fold_builtin_strstr (tree, tree);
185 static tree fold_builtin_strrchr (tree, tree);
186 static tree fold_builtin_strcat (tree);
187 static tree fold_builtin_strncat (tree);
188 static tree fold_builtin_strspn (tree);
189 static tree fold_builtin_strcspn (tree);
190 static tree fold_builtin_sprintf (tree, int);
193 /* Return the alignment in bits of EXP, a pointer valued expression.
194 But don't return more than MAX_ALIGN no matter what.
195 The alignment returned is, by default, the alignment of the thing that
196 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
198 Otherwise, look at the expression to see if we can do better, i.e., if the
199 expression is actually pointing at an object whose alignment is tighter. */
201 static int
202 get_pointer_alignment (tree exp, unsigned int max_align)
204 unsigned int align, inner;
206 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
207 return 0;
209 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
210 align = MIN (align, max_align);
212 while (1)
214 switch (TREE_CODE (exp))
216 case NOP_EXPR:
217 case CONVERT_EXPR:
218 case NON_LVALUE_EXPR:
219 exp = TREE_OPERAND (exp, 0);
220 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
221 return align;
223 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
224 align = MIN (inner, max_align);
225 break;
227 case PLUS_EXPR:
228 /* If sum of pointer + int, restrict our maximum alignment to that
229 imposed by the integer. If not, we can't do any better than
230 ALIGN. */
231 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
232 return align;
234 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
235 & (max_align / BITS_PER_UNIT - 1))
236 != 0)
237 max_align >>= 1;
239 exp = TREE_OPERAND (exp, 0);
240 break;
242 case ADDR_EXPR:
243 /* See what we are pointing at and look at its alignment. */
244 exp = TREE_OPERAND (exp, 0);
245 if (TREE_CODE (exp) == FUNCTION_DECL)
246 align = FUNCTION_BOUNDARY;
247 else if (DECL_P (exp))
248 align = DECL_ALIGN (exp);
249 #ifdef CONSTANT_ALIGNMENT
250 else if (CONSTANT_CLASS_P (exp))
251 align = CONSTANT_ALIGNMENT (exp, align);
252 #endif
253 return MIN (align, max_align);
255 default:
256 return align;
261 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
262 way, because it could contain a zero byte in the middle.
263 TREE_STRING_LENGTH is the size of the character array, not the string.
265 ONLY_VALUE should be nonzero if the result is not going to be emitted
266 into the instruction stream and zero if it is going to be expanded.
267 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
268 is returned, otherwise NULL, since
269 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
270 evaluate the side-effects.
272 The value returned is of type `ssizetype'.
274 Unfortunately, string_constant can't access the values of const char
275 arrays with initializers, so neither can we do so here. */
277 tree
278 c_strlen (tree src, int only_value)
280 tree offset_node;
281 HOST_WIDE_INT offset;
282 int max;
283 const char *ptr;
285 STRIP_NOPS (src);
286 if (TREE_CODE (src) == COND_EXPR
287 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
289 tree len1, len2;
291 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
292 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
293 if (tree_int_cst_equal (len1, len2))
294 return len1;
297 if (TREE_CODE (src) == COMPOUND_EXPR
298 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
299 return c_strlen (TREE_OPERAND (src, 1), only_value);
301 src = string_constant (src, &offset_node);
302 if (src == 0)
303 return 0;
305 max = TREE_STRING_LENGTH (src) - 1;
306 ptr = TREE_STRING_POINTER (src);
308 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
310 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
311 compute the offset to the following null if we don't know where to
312 start searching for it. */
313 int i;
315 for (i = 0; i < max; i++)
316 if (ptr[i] == 0)
317 return 0;
319 /* We don't know the starting offset, but we do know that the string
320 has no internal zero bytes. We can assume that the offset falls
321 within the bounds of the string; otherwise, the programmer deserves
322 what he gets. Subtract the offset from the length of the string,
323 and return that. This would perhaps not be valid if we were dealing
324 with named arrays in addition to literal string constants. */
326 return size_diffop (size_int (max), offset_node);
329 /* We have a known offset into the string. Start searching there for
330 a null character if we can represent it as a single HOST_WIDE_INT. */
331 if (offset_node == 0)
332 offset = 0;
333 else if (! host_integerp (offset_node, 0))
334 offset = -1;
335 else
336 offset = tree_low_cst (offset_node, 0);
338 /* If the offset is known to be out of bounds, warn, and call strlen at
339 runtime. */
340 if (offset < 0 || offset > max)
342 warning ("offset outside bounds of constant string");
343 return 0;
346 /* Use strlen to search for the first zero byte. Since any strings
347 constructed with build_string will have nulls appended, we win even
348 if we get handed something like (char[4])"abcd".
350 Since OFFSET is our starting index into the string, no further
351 calculation is needed. */
352 return ssize_int (strlen (ptr + offset));
355 /* Return a char pointer for a C string if it is a string constant
356 or sum of string constant and integer constant. */
358 static const char *
359 c_getstr (tree src)
361 tree offset_node;
363 src = string_constant (src, &offset_node);
364 if (src == 0)
365 return 0;
367 if (offset_node == 0)
368 return TREE_STRING_POINTER (src);
369 else if (!host_integerp (offset_node, 1)
370 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
371 return 0;
373 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
376 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
377 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
379 static rtx
380 c_readstr (const char *str, enum machine_mode mode)
382 HOST_WIDE_INT c[2];
383 HOST_WIDE_INT ch;
384 unsigned int i, j;
386 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
388 c[0] = 0;
389 c[1] = 0;
390 ch = 1;
391 for (i = 0; i < GET_MODE_SIZE (mode); i++)
393 j = i;
394 if (WORDS_BIG_ENDIAN)
395 j = GET_MODE_SIZE (mode) - i - 1;
396 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
397 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
398 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
399 j *= BITS_PER_UNIT;
400 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
402 if (ch)
403 ch = (unsigned char) str[i];
404 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
406 return immed_double_const (c[0], c[1], mode);
409 /* Cast a target constant CST to target CHAR and if that value fits into
410 host char type, return zero and put that value into variable pointed by
411 P. */
413 static int
414 target_char_cast (tree cst, char *p)
416 unsigned HOST_WIDE_INT val, hostval;
418 if (!host_integerp (cst, 1)
419 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
420 return 1;
422 val = tree_low_cst (cst, 1);
423 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
424 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
426 hostval = val;
427 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
428 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
430 if (val != hostval)
431 return 1;
433 *p = hostval;
434 return 0;
437 /* Similar to save_expr, but assumes that arbitrary code is not executed
438 in between the multiple evaluations. In particular, we assume that a
439 non-addressable local variable will not be modified. */
441 static tree
442 builtin_save_expr (tree exp)
444 if (TREE_ADDRESSABLE (exp) == 0
445 && (TREE_CODE (exp) == PARM_DECL
446 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
447 return exp;
449 return save_expr (exp);
452 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
453 times to get the address of either a higher stack frame, or a return
454 address located within it (depending on FNDECL_CODE). */
456 static rtx
457 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
459 int i;
461 #ifdef INITIAL_FRAME_ADDRESS_RTX
462 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
463 #else
464 rtx tem = hard_frame_pointer_rtx;
465 #endif
467 /* Some machines need special handling before we can access
468 arbitrary frames. For example, on the sparc, we must first flush
469 all register windows to the stack. */
470 #ifdef SETUP_FRAME_ADDRESSES
471 if (count > 0)
472 SETUP_FRAME_ADDRESSES ();
473 #endif
475 /* On the sparc, the return address is not in the frame, it is in a
476 register. There is no way to access it off of the current frame
477 pointer, but it can be accessed off the previous frame pointer by
478 reading the value from the register window save area. */
479 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
480 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
481 count--;
482 #endif
484 /* Scan back COUNT frames to the specified frame. */
485 for (i = 0; i < count; i++)
487 /* Assume the dynamic chain pointer is in the word that the
488 frame address points to, unless otherwise specified. */
489 #ifdef DYNAMIC_CHAIN_ADDRESS
490 tem = DYNAMIC_CHAIN_ADDRESS (tem);
491 #endif
492 tem = memory_address (Pmode, tem);
493 tem = gen_rtx_MEM (Pmode, tem);
494 set_mem_alias_set (tem, get_frame_alias_set ());
495 tem = copy_to_reg (tem);
498 /* For __builtin_frame_address, return what we've got. */
499 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
500 return tem;
502 /* For __builtin_return_address, Get the return address from that
503 frame. */
504 #ifdef RETURN_ADDR_RTX
505 tem = RETURN_ADDR_RTX (count, tem);
506 #else
507 tem = memory_address (Pmode,
508 plus_constant (tem, GET_MODE_SIZE (Pmode)));
509 tem = gen_rtx_MEM (Pmode, tem);
510 set_mem_alias_set (tem, get_frame_alias_set ());
511 #endif
512 return tem;
515 /* Alias set used for setjmp buffer. */
516 static HOST_WIDE_INT setjmp_alias_set = -1;
518 /* Construct the leading half of a __builtin_setjmp call. Control will
519 return to RECEIVER_LABEL. This is used directly by sjlj exception
520 handling code. */
522 void
523 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
525 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
526 rtx stack_save;
527 rtx mem;
529 if (setjmp_alias_set == -1)
530 setjmp_alias_set = new_alias_set ();
532 buf_addr = convert_memory_address (Pmode, buf_addr);
534 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
536 /* We store the frame pointer and the address of receiver_label in
537 the buffer and use the rest of it for the stack save area, which
538 is machine-dependent. */
540 mem = gen_rtx_MEM (Pmode, buf_addr);
541 set_mem_alias_set (mem, setjmp_alias_set);
542 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
544 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
545 set_mem_alias_set (mem, setjmp_alias_set);
547 emit_move_insn (validize_mem (mem),
548 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
550 stack_save = gen_rtx_MEM (sa_mode,
551 plus_constant (buf_addr,
552 2 * GET_MODE_SIZE (Pmode)));
553 set_mem_alias_set (stack_save, setjmp_alias_set);
554 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
556 /* If there is further processing to do, do it. */
557 #ifdef HAVE_builtin_setjmp_setup
558 if (HAVE_builtin_setjmp_setup)
559 emit_insn (gen_builtin_setjmp_setup (buf_addr));
560 #endif
562 /* Tell optimize_save_area_alloca that extra work is going to
563 need to go on during alloca. */
564 current_function_calls_setjmp = 1;
566 /* Set this so all the registers get saved in our frame; we need to be
567 able to copy the saved values for any registers from frames we unwind. */
568 current_function_has_nonlocal_label = 1;
571 /* Construct the trailing part of a __builtin_setjmp call.
572 This is used directly by sjlj exception handling code. */
574 void
575 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
577 /* Clobber the FP when we get here, so we have to make sure it's
578 marked as used by this function. */
579 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
581 /* Mark the static chain as clobbered here so life information
582 doesn't get messed up for it. */
583 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
585 /* Now put in the code to restore the frame pointer, and argument
586 pointer, if needed. */
587 #ifdef HAVE_nonlocal_goto
588 if (! HAVE_nonlocal_goto)
589 #endif
590 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
592 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
593 if (fixed_regs[ARG_POINTER_REGNUM])
595 #ifdef ELIMINABLE_REGS
596 size_t i;
597 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
599 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
600 if (elim_regs[i].from == ARG_POINTER_REGNUM
601 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
602 break;
604 if (i == ARRAY_SIZE (elim_regs))
605 #endif
607 /* Now restore our arg pointer from the address at which it
608 was saved in our stack frame. */
609 emit_move_insn (virtual_incoming_args_rtx,
610 copy_to_reg (get_arg_pointer_save_area (cfun)));
613 #endif
615 #ifdef HAVE_builtin_setjmp_receiver
616 if (HAVE_builtin_setjmp_receiver)
617 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
618 else
619 #endif
620 #ifdef HAVE_nonlocal_goto_receiver
621 if (HAVE_nonlocal_goto_receiver)
622 emit_insn (gen_nonlocal_goto_receiver ());
623 else
624 #endif
625 { /* Nothing */ }
627 /* @@@ This is a kludge. Not all machine descriptions define a blockage
628 insn, but we must not allow the code we just generated to be reordered
629 by scheduling. Specifically, the update of the frame pointer must
630 happen immediately, not later. So emit an ASM_INPUT to act as blockage
631 insn. */
632 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
635 /* __builtin_setjmp is passed a pointer to an array of five words (not
636 all will be used on all machines). It operates similarly to the C
637 library function of the same name, but is more efficient. Much of
638 the code below (and for longjmp) is copied from the handling of
639 non-local gotos.
641 NOTE: This is intended for use by GNAT and the exception handling
642 scheme in the compiler and will only work in the method used by
643 them. */
645 static rtx
646 expand_builtin_setjmp (tree arglist, rtx target)
648 rtx buf_addr, next_lab, cont_lab;
650 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
651 return NULL_RTX;
653 if (target == 0 || !REG_P (target)
654 || REGNO (target) < FIRST_PSEUDO_REGISTER)
655 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
657 buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
659 next_lab = gen_label_rtx ();
660 cont_lab = gen_label_rtx ();
662 expand_builtin_setjmp_setup (buf_addr, next_lab);
664 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
665 ensure that pending stack adjustments are flushed. */
666 emit_move_insn (target, const0_rtx);
667 emit_jump (cont_lab);
669 emit_label (next_lab);
671 expand_builtin_setjmp_receiver (next_lab);
673 /* Set TARGET to one. */
674 emit_move_insn (target, const1_rtx);
675 emit_label (cont_lab);
677 /* Tell flow about the strange goings on. Putting `next_lab' on
678 `nonlocal_goto_handler_labels' to indicates that function
679 calls may traverse the arc back to this label. */
681 current_function_has_nonlocal_label = 1;
682 nonlocal_goto_handler_labels
683 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
685 return target;
688 /* __builtin_longjmp is passed a pointer to an array of five words (not
689 all will be used on all machines). It operates similarly to the C
690 library function of the same name, but is more efficient. Much of
691 the code below is copied from the handling of non-local gotos.
693 NOTE: This is intended for use by GNAT and the exception handling
694 scheme in the compiler and will only work in the method used by
695 them. */
697 static void
698 expand_builtin_longjmp (rtx buf_addr, rtx value)
700 rtx fp, lab, stack, insn, last;
701 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
703 if (setjmp_alias_set == -1)
704 setjmp_alias_set = new_alias_set ();
706 buf_addr = convert_memory_address (Pmode, buf_addr);
708 buf_addr = force_reg (Pmode, buf_addr);
710 /* We used to store value in static_chain_rtx, but that fails if pointers
711 are smaller than integers. We instead require that the user must pass
712 a second argument of 1, because that is what builtin_setjmp will
713 return. This also makes EH slightly more efficient, since we are no
714 longer copying around a value that we don't care about. */
715 gcc_assert (value == const1_rtx);
717 last = get_last_insn ();
718 #ifdef HAVE_builtin_longjmp
719 if (HAVE_builtin_longjmp)
720 emit_insn (gen_builtin_longjmp (buf_addr));
721 else
722 #endif
724 fp = gen_rtx_MEM (Pmode, buf_addr);
725 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
726 GET_MODE_SIZE (Pmode)));
728 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
729 2 * GET_MODE_SIZE (Pmode)));
730 set_mem_alias_set (fp, setjmp_alias_set);
731 set_mem_alias_set (lab, setjmp_alias_set);
732 set_mem_alias_set (stack, setjmp_alias_set);
734 /* Pick up FP, label, and SP from the block and jump. This code is
735 from expand_goto in stmt.c; see there for detailed comments. */
736 #if HAVE_nonlocal_goto
737 if (HAVE_nonlocal_goto)
738 /* We have to pass a value to the nonlocal_goto pattern that will
739 get copied into the static_chain pointer, but it does not matter
740 what that value is, because builtin_setjmp does not use it. */
741 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
742 else
743 #endif
745 lab = copy_to_reg (lab);
747 emit_insn (gen_rtx_CLOBBER (VOIDmode,
748 gen_rtx_MEM (BLKmode,
749 gen_rtx_SCRATCH (VOIDmode))));
750 emit_insn (gen_rtx_CLOBBER (VOIDmode,
751 gen_rtx_MEM (BLKmode,
752 hard_frame_pointer_rtx)));
754 emit_move_insn (hard_frame_pointer_rtx, fp);
755 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
757 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
758 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
759 emit_indirect_jump (lab);
763 /* Search backwards and mark the jump insn as a non-local goto.
764 Note that this precludes the use of __builtin_longjmp to a
765 __builtin_setjmp target in the same function. However, we've
766 already cautioned the user that these functions are for
767 internal exception handling use only. */
768 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
770 gcc_assert (insn != last);
772 if (JUMP_P (insn))
774 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
775 REG_NOTES (insn));
776 break;
778 else if (CALL_P (insn))
779 break;
783 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
784 and the address of the save area. */
786 static rtx
787 expand_builtin_nonlocal_goto (tree arglist)
789 tree t_label, t_save_area;
790 rtx r_label, r_save_area, r_fp, r_sp, insn;
792 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
793 return NULL_RTX;
795 t_label = TREE_VALUE (arglist);
796 arglist = TREE_CHAIN (arglist);
797 t_save_area = TREE_VALUE (arglist);
799 r_label = expand_expr (t_label, NULL_RTX, VOIDmode, 0);
800 r_label = convert_memory_address (Pmode, r_label);
801 r_save_area = expand_expr (t_save_area, NULL_RTX, VOIDmode, 0);
802 r_save_area = convert_memory_address (Pmode, r_save_area);
803 r_fp = gen_rtx_MEM (Pmode, r_save_area);
804 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
805 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
807 current_function_has_nonlocal_goto = 1;
809 #if HAVE_nonlocal_goto
810 /* ??? We no longer need to pass the static chain value, afaik. */
811 if (HAVE_nonlocal_goto)
812 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
813 else
814 #endif
816 r_label = copy_to_reg (r_label);
818 emit_insn (gen_rtx_CLOBBER (VOIDmode,
819 gen_rtx_MEM (BLKmode,
820 gen_rtx_SCRATCH (VOIDmode))));
822 emit_insn (gen_rtx_CLOBBER (VOIDmode,
823 gen_rtx_MEM (BLKmode,
824 hard_frame_pointer_rtx)));
826 /* Restore frame pointer for containing function.
827 This sets the actual hard register used for the frame pointer
828 to the location of the function's incoming static chain info.
829 The non-local goto handler will then adjust it to contain the
830 proper value and reload the argument pointer, if needed. */
831 emit_move_insn (hard_frame_pointer_rtx, r_fp);
832 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
834 /* USE of hard_frame_pointer_rtx added for consistency;
835 not clear if really needed. */
836 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
837 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
838 emit_indirect_jump (r_label);
841 /* Search backwards to the jump insn and mark it as a
842 non-local goto. */
843 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
845 if (JUMP_P (insn))
847 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
848 const0_rtx, REG_NOTES (insn));
849 break;
851 else if (CALL_P (insn))
852 break;
855 return const0_rtx;
858 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
859 (not all will be used on all machines) that was passed to __builtin_setjmp.
860 It updates the stack pointer in that block to correspond to the current
861 stack pointer. */
863 static void
864 expand_builtin_update_setjmp_buf (rtx buf_addr)
866 enum machine_mode sa_mode = Pmode;
867 rtx stack_save;
870 #ifdef HAVE_save_stack_nonlocal
871 if (HAVE_save_stack_nonlocal)
872 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
873 #endif
874 #ifdef STACK_SAVEAREA_MODE
875 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
876 #endif
878 stack_save
879 = gen_rtx_MEM (sa_mode,
880 memory_address
881 (sa_mode,
882 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
884 #ifdef HAVE_setjmp
885 if (HAVE_setjmp)
886 emit_insn (gen_setjmp ());
887 #endif
889 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
892 /* Expand a call to __builtin_prefetch. For a target that does not support
893 data prefetch, evaluate the memory address argument in case it has side
894 effects. */
896 static void
897 expand_builtin_prefetch (tree arglist)
899 tree arg0, arg1, arg2;
900 rtx op0, op1, op2;
902 if (!validate_arglist (arglist, POINTER_TYPE, 0))
903 return;
905 arg0 = TREE_VALUE (arglist);
906 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
907 zero (read) and argument 2 (locality) defaults to 3 (high degree of
908 locality). */
909 if (TREE_CHAIN (arglist))
911 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
912 if (TREE_CHAIN (TREE_CHAIN (arglist)))
913 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
914 else
915 arg2 = build_int_cst (NULL_TREE, 3);
917 else
919 arg1 = integer_zero_node;
920 arg2 = build_int_cst (NULL_TREE, 3);
923 /* Argument 0 is an address. */
924 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
926 /* Argument 1 (read/write flag) must be a compile-time constant int. */
927 if (TREE_CODE (arg1) != INTEGER_CST)
929 error ("second argument to %<__builtin_prefetch%> must be a constant");
930 arg1 = integer_zero_node;
932 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
933 /* Argument 1 must be either zero or one. */
934 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
936 warning ("invalid second argument to %<__builtin_prefetch%>;"
937 " using zero");
938 op1 = const0_rtx;
941 /* Argument 2 (locality) must be a compile-time constant int. */
942 if (TREE_CODE (arg2) != INTEGER_CST)
944 error ("third argument to %<__builtin_prefetch%> must be a constant");
945 arg2 = integer_zero_node;
947 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
948 /* Argument 2 must be 0, 1, 2, or 3. */
949 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
951 warning ("invalid third argument to %<__builtin_prefetch%>; using zero");
952 op2 = const0_rtx;
955 #ifdef HAVE_prefetch
956 if (HAVE_prefetch)
958 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
959 (op0,
960 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
961 || (GET_MODE (op0) != Pmode))
963 op0 = convert_memory_address (Pmode, op0);
964 op0 = force_reg (Pmode, op0);
966 emit_insn (gen_prefetch (op0, op1, op2));
968 #endif
970 /* Don't do anything with direct references to volatile memory, but
971 generate code to handle other side effects. */
972 if (!MEM_P (op0) && side_effects_p (op0))
973 emit_insn (op0);
976 /* Get a MEM rtx for expression EXP which is the address of an operand
977 to be used to be used in a string instruction (cmpstrsi, movmemsi, ..). */
979 static rtx
980 get_memory_rtx (tree exp)
982 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
983 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
985 /* Get an expression we can use to find the attributes to assign to MEM.
986 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
987 we can. First remove any nops. */
988 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
989 || TREE_CODE (exp) == NON_LVALUE_EXPR)
990 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
991 exp = TREE_OPERAND (exp, 0);
993 if (TREE_CODE (exp) == ADDR_EXPR)
994 exp = TREE_OPERAND (exp, 0);
995 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
996 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
997 else
998 exp = NULL;
1000 /* Honor attributes derived from exp, except for the alias set
1001 (as builtin stringops may alias with anything) and the size
1002 (as stringops may access multiple array elements). */
1003 if (exp)
1005 set_mem_attributes (mem, exp, 0);
1006 set_mem_alias_set (mem, 0);
1007 set_mem_size (mem, NULL_RTX);
1010 return mem;
1013 /* Built-in functions to perform an untyped call and return. */
1015 /* For each register that may be used for calling a function, this
1016 gives a mode used to copy the register's value. VOIDmode indicates
1017 the register is not used for calling a function. If the machine
1018 has register windows, this gives only the outbound registers.
1019 INCOMING_REGNO gives the corresponding inbound register. */
1020 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1022 /* For each register that may be used for returning values, this gives
1023 a mode used to copy the register's value. VOIDmode indicates the
1024 register is not used for returning values. If the machine has
1025 register windows, this gives only the outbound registers.
1026 INCOMING_REGNO gives the corresponding inbound register. */
1027 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1029 /* For each register that may be used for calling a function, this
1030 gives the offset of that register into the block returned by
1031 __builtin_apply_args. 0 indicates that the register is not
1032 used for calling a function. */
1033 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1035 /* Return the size required for the block returned by __builtin_apply_args,
1036 and initialize apply_args_mode. */
1038 static int
1039 apply_args_size (void)
1041 static int size = -1;
1042 int align;
1043 unsigned int regno;
1044 enum machine_mode mode;
1046 /* The values computed by this function never change. */
1047 if (size < 0)
1049 /* The first value is the incoming arg-pointer. */
1050 size = GET_MODE_SIZE (Pmode);
1052 /* The second value is the structure value address unless this is
1053 passed as an "invisible" first argument. */
1054 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1055 size += GET_MODE_SIZE (Pmode);
1057 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1058 if (FUNCTION_ARG_REGNO_P (regno))
1060 mode = reg_raw_mode[regno];
1062 gcc_assert (mode != VOIDmode);
1064 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1065 if (size % align != 0)
1066 size = CEIL (size, align) * align;
1067 apply_args_reg_offset[regno] = size;
1068 size += GET_MODE_SIZE (mode);
1069 apply_args_mode[regno] = mode;
1071 else
1073 apply_args_mode[regno] = VOIDmode;
1074 apply_args_reg_offset[regno] = 0;
1077 return size;
1080 /* Return the size required for the block returned by __builtin_apply,
1081 and initialize apply_result_mode. */
1083 static int
1084 apply_result_size (void)
1086 static int size = -1;
1087 int align, regno;
1088 enum machine_mode mode;
1090 /* The values computed by this function never change. */
1091 if (size < 0)
1093 size = 0;
1095 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1096 if (FUNCTION_VALUE_REGNO_P (regno))
1098 mode = reg_raw_mode[regno];
1100 gcc_assert (mode != VOIDmode);
1102 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1103 if (size % align != 0)
1104 size = CEIL (size, align) * align;
1105 size += GET_MODE_SIZE (mode);
1106 apply_result_mode[regno] = mode;
1108 else
1109 apply_result_mode[regno] = VOIDmode;
1111 /* Allow targets that use untyped_call and untyped_return to override
1112 the size so that machine-specific information can be stored here. */
1113 #ifdef APPLY_RESULT_SIZE
1114 size = APPLY_RESULT_SIZE;
1115 #endif
1117 return size;
1120 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1121 /* Create a vector describing the result block RESULT. If SAVEP is true,
1122 the result block is used to save the values; otherwise it is used to
1123 restore the values. */
1125 static rtx
1126 result_vector (int savep, rtx result)
1128 int regno, size, align, nelts;
1129 enum machine_mode mode;
1130 rtx reg, mem;
1131 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1133 size = nelts = 0;
1134 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1135 if ((mode = apply_result_mode[regno]) != VOIDmode)
1137 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1138 if (size % align != 0)
1139 size = CEIL (size, align) * align;
1140 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1141 mem = adjust_address (result, mode, size);
1142 savevec[nelts++] = (savep
1143 ? gen_rtx_SET (VOIDmode, mem, reg)
1144 : gen_rtx_SET (VOIDmode, reg, mem));
1145 size += GET_MODE_SIZE (mode);
1147 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1149 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1151 /* Save the state required to perform an untyped call with the same
1152 arguments as were passed to the current function. */
1154 static rtx
1155 expand_builtin_apply_args_1 (void)
1157 rtx registers, tem;
1158 int size, align, regno;
1159 enum machine_mode mode;
1160 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1162 /* Create a block where the arg-pointer, structure value address,
1163 and argument registers can be saved. */
1164 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1166 /* Walk past the arg-pointer and structure value address. */
1167 size = GET_MODE_SIZE (Pmode);
1168 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1169 size += GET_MODE_SIZE (Pmode);
1171 /* Save each register used in calling a function to the block. */
1172 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1173 if ((mode = apply_args_mode[regno]) != VOIDmode)
1175 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1176 if (size % align != 0)
1177 size = CEIL (size, align) * align;
1179 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1181 emit_move_insn (adjust_address (registers, mode, size), tem);
1182 size += GET_MODE_SIZE (mode);
1185 /* Save the arg pointer to the block. */
1186 tem = copy_to_reg (virtual_incoming_args_rtx);
1187 #ifdef STACK_GROWS_DOWNWARD
1188 /* We need the pointer as the caller actually passed them to us, not
1189 as we might have pretended they were passed. Make sure it's a valid
1190 operand, as emit_move_insn isn't expected to handle a PLUS. */
1192 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1193 NULL_RTX);
1194 #endif
1195 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1197 size = GET_MODE_SIZE (Pmode);
1199 /* Save the structure value address unless this is passed as an
1200 "invisible" first argument. */
1201 if (struct_incoming_value)
1203 emit_move_insn (adjust_address (registers, Pmode, size),
1204 copy_to_reg (struct_incoming_value));
1205 size += GET_MODE_SIZE (Pmode);
1208 /* Return the address of the block. */
1209 return copy_addr_to_reg (XEXP (registers, 0));
1212 /* __builtin_apply_args returns block of memory allocated on
1213 the stack into which is stored the arg pointer, structure
1214 value address, static chain, and all the registers that might
1215 possibly be used in performing a function call. The code is
1216 moved to the start of the function so the incoming values are
1217 saved. */
1219 static rtx
1220 expand_builtin_apply_args (void)
1222 /* Don't do __builtin_apply_args more than once in a function.
1223 Save the result of the first call and reuse it. */
1224 if (apply_args_value != 0)
1225 return apply_args_value;
1227 /* When this function is called, it means that registers must be
1228 saved on entry to this function. So we migrate the
1229 call to the first insn of this function. */
1230 rtx temp;
1231 rtx seq;
1233 start_sequence ();
1234 temp = expand_builtin_apply_args_1 ();
1235 seq = get_insns ();
1236 end_sequence ();
1238 apply_args_value = temp;
1240 /* Put the insns after the NOTE that starts the function.
1241 If this is inside a start_sequence, make the outer-level insn
1242 chain current, so the code is placed at the start of the
1243 function. */
1244 push_topmost_sequence ();
1245 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1246 pop_topmost_sequence ();
1247 return temp;
1251 /* Perform an untyped call and save the state required to perform an
1252 untyped return of whatever value was returned by the given function. */
1254 static rtx
1255 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1257 int size, align, regno;
1258 enum machine_mode mode;
1259 rtx incoming_args, result, reg, dest, src, call_insn;
1260 rtx old_stack_level = 0;
1261 rtx call_fusage = 0;
1262 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1264 arguments = convert_memory_address (Pmode, arguments);
1266 /* Create a block where the return registers can be saved. */
1267 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1269 /* Fetch the arg pointer from the ARGUMENTS block. */
1270 incoming_args = gen_reg_rtx (Pmode);
1271 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1272 #ifndef STACK_GROWS_DOWNWARD
1273 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1274 incoming_args, 0, OPTAB_LIB_WIDEN);
1275 #endif
1277 /* Push a new argument block and copy the arguments. Do not allow
1278 the (potential) memcpy call below to interfere with our stack
1279 manipulations. */
1280 do_pending_stack_adjust ();
1281 NO_DEFER_POP;
1283 /* Save the stack with nonlocal if available. */
1284 #ifdef HAVE_save_stack_nonlocal
1285 if (HAVE_save_stack_nonlocal)
1286 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1287 else
1288 #endif
1289 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1291 /* Allocate a block of memory onto the stack and copy the memory
1292 arguments to the outgoing arguments address. */
1293 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1294 dest = virtual_outgoing_args_rtx;
1295 #ifndef STACK_GROWS_DOWNWARD
1296 if (GET_CODE (argsize) == CONST_INT)
1297 dest = plus_constant (dest, -INTVAL (argsize));
1298 else
1299 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1300 #endif
1301 dest = gen_rtx_MEM (BLKmode, dest);
1302 set_mem_align (dest, PARM_BOUNDARY);
1303 src = gen_rtx_MEM (BLKmode, incoming_args);
1304 set_mem_align (src, PARM_BOUNDARY);
1305 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1307 /* Refer to the argument block. */
1308 apply_args_size ();
1309 arguments = gen_rtx_MEM (BLKmode, arguments);
1310 set_mem_align (arguments, PARM_BOUNDARY);
1312 /* Walk past the arg-pointer and structure value address. */
1313 size = GET_MODE_SIZE (Pmode);
1314 if (struct_value)
1315 size += GET_MODE_SIZE (Pmode);
1317 /* Restore each of the registers previously saved. Make USE insns
1318 for each of these registers for use in making the call. */
1319 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1320 if ((mode = apply_args_mode[regno]) != VOIDmode)
1322 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1323 if (size % align != 0)
1324 size = CEIL (size, align) * align;
1325 reg = gen_rtx_REG (mode, regno);
1326 emit_move_insn (reg, adjust_address (arguments, mode, size));
1327 use_reg (&call_fusage, reg);
1328 size += GET_MODE_SIZE (mode);
1331 /* Restore the structure value address unless this is passed as an
1332 "invisible" first argument. */
1333 size = GET_MODE_SIZE (Pmode);
1334 if (struct_value)
1336 rtx value = gen_reg_rtx (Pmode);
1337 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1338 emit_move_insn (struct_value, value);
1339 if (REG_P (struct_value))
1340 use_reg (&call_fusage, struct_value);
1341 size += GET_MODE_SIZE (Pmode);
1344 /* All arguments and registers used for the call are set up by now! */
1345 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1347 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1348 and we don't want to load it into a register as an optimization,
1349 because prepare_call_address already did it if it should be done. */
1350 if (GET_CODE (function) != SYMBOL_REF)
1351 function = memory_address (FUNCTION_MODE, function);
1353 /* Generate the actual call instruction and save the return value. */
1354 #ifdef HAVE_untyped_call
1355 if (HAVE_untyped_call)
1356 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1357 result, result_vector (1, result)));
1358 else
1359 #endif
1360 #ifdef HAVE_call_value
1361 if (HAVE_call_value)
1363 rtx valreg = 0;
1365 /* Locate the unique return register. It is not possible to
1366 express a call that sets more than one return register using
1367 call_value; use untyped_call for that. In fact, untyped_call
1368 only needs to save the return registers in the given block. */
1369 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1370 if ((mode = apply_result_mode[regno]) != VOIDmode)
1372 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1374 valreg = gen_rtx_REG (mode, regno);
1377 emit_call_insn (GEN_CALL_VALUE (valreg,
1378 gen_rtx_MEM (FUNCTION_MODE, function),
1379 const0_rtx, NULL_RTX, const0_rtx));
1381 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1383 else
1384 #endif
1385 gcc_unreachable ();
1387 /* Find the CALL insn we just emitted, and attach the register usage
1388 information. */
1389 call_insn = last_call_insn ();
1390 add_function_usage_to (call_insn, call_fusage);
1392 /* Restore the stack. */
1393 #ifdef HAVE_save_stack_nonlocal
1394 if (HAVE_save_stack_nonlocal)
1395 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1396 else
1397 #endif
1398 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1400 OK_DEFER_POP;
1402 /* Return the address of the result block. */
1403 result = copy_addr_to_reg (XEXP (result, 0));
1404 return convert_memory_address (ptr_mode, result);
1407 /* Perform an untyped return. */
1409 static void
1410 expand_builtin_return (rtx result)
1412 int size, align, regno;
1413 enum machine_mode mode;
1414 rtx reg;
1415 rtx call_fusage = 0;
1417 result = convert_memory_address (Pmode, result);
1419 apply_result_size ();
1420 result = gen_rtx_MEM (BLKmode, result);
1422 #ifdef HAVE_untyped_return
1423 if (HAVE_untyped_return)
1425 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1426 emit_barrier ();
1427 return;
1429 #endif
1431 /* Restore the return value and note that each value is used. */
1432 size = 0;
1433 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1434 if ((mode = apply_result_mode[regno]) != VOIDmode)
1436 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1437 if (size % align != 0)
1438 size = CEIL (size, align) * align;
1439 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1440 emit_move_insn (reg, adjust_address (result, mode, size));
1442 push_to_sequence (call_fusage);
1443 emit_insn (gen_rtx_USE (VOIDmode, reg));
1444 call_fusage = get_insns ();
1445 end_sequence ();
1446 size += GET_MODE_SIZE (mode);
1449 /* Put the USE insns before the return. */
1450 emit_insn (call_fusage);
1452 /* Return whatever values was restored by jumping directly to the end
1453 of the function. */
1454 expand_naked_return ();
1457 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1459 static enum type_class
1460 type_to_class (tree type)
1462 switch (TREE_CODE (type))
1464 case VOID_TYPE: return void_type_class;
1465 case INTEGER_TYPE: return integer_type_class;
1466 case CHAR_TYPE: return char_type_class;
1467 case ENUMERAL_TYPE: return enumeral_type_class;
1468 case BOOLEAN_TYPE: return boolean_type_class;
1469 case POINTER_TYPE: return pointer_type_class;
1470 case REFERENCE_TYPE: return reference_type_class;
1471 case OFFSET_TYPE: return offset_type_class;
1472 case REAL_TYPE: return real_type_class;
1473 case COMPLEX_TYPE: return complex_type_class;
1474 case FUNCTION_TYPE: return function_type_class;
1475 case METHOD_TYPE: return method_type_class;
1476 case RECORD_TYPE: return record_type_class;
1477 case UNION_TYPE:
1478 case QUAL_UNION_TYPE: return union_type_class;
1479 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1480 ? string_type_class : array_type_class);
1481 case LANG_TYPE: return lang_type_class;
1482 default: return no_type_class;
1486 /* Expand a call to __builtin_classify_type with arguments found in
1487 ARGLIST. */
1489 static rtx
1490 expand_builtin_classify_type (tree arglist)
1492 if (arglist != 0)
1493 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1494 return GEN_INT (no_type_class);
1497 /* This helper macro, meant to be used in mathfn_built_in below,
1498 determines which among a set of three builtin math functions is
1499 appropriate for a given type mode. The `F' and `L' cases are
1500 automatically generated from the `double' case. */
1501 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1502 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1503 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1504 fcodel = BUILT_IN_MATHFN##L ; break;
1506 /* Return mathematic function equivalent to FN but operating directly
1507 on TYPE, if available. If we can't do the conversion, return zero. */
1508 tree
1509 mathfn_built_in (tree type, enum built_in_function fn)
1511 enum built_in_function fcode, fcodef, fcodel;
1513 switch (fn)
1515 CASE_MATHFN (BUILT_IN_ACOS)
1516 CASE_MATHFN (BUILT_IN_ACOSH)
1517 CASE_MATHFN (BUILT_IN_ASIN)
1518 CASE_MATHFN (BUILT_IN_ASINH)
1519 CASE_MATHFN (BUILT_IN_ATAN)
1520 CASE_MATHFN (BUILT_IN_ATAN2)
1521 CASE_MATHFN (BUILT_IN_ATANH)
1522 CASE_MATHFN (BUILT_IN_CBRT)
1523 CASE_MATHFN (BUILT_IN_CEIL)
1524 CASE_MATHFN (BUILT_IN_COPYSIGN)
1525 CASE_MATHFN (BUILT_IN_COS)
1526 CASE_MATHFN (BUILT_IN_COSH)
1527 CASE_MATHFN (BUILT_IN_DREM)
1528 CASE_MATHFN (BUILT_IN_ERF)
1529 CASE_MATHFN (BUILT_IN_ERFC)
1530 CASE_MATHFN (BUILT_IN_EXP)
1531 CASE_MATHFN (BUILT_IN_EXP10)
1532 CASE_MATHFN (BUILT_IN_EXP2)
1533 CASE_MATHFN (BUILT_IN_EXPM1)
1534 CASE_MATHFN (BUILT_IN_FABS)
1535 CASE_MATHFN (BUILT_IN_FDIM)
1536 CASE_MATHFN (BUILT_IN_FLOOR)
1537 CASE_MATHFN (BUILT_IN_FMA)
1538 CASE_MATHFN (BUILT_IN_FMAX)
1539 CASE_MATHFN (BUILT_IN_FMIN)
1540 CASE_MATHFN (BUILT_IN_FMOD)
1541 CASE_MATHFN (BUILT_IN_FREXP)
1542 CASE_MATHFN (BUILT_IN_GAMMA)
1543 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1544 CASE_MATHFN (BUILT_IN_HYPOT)
1545 CASE_MATHFN (BUILT_IN_ILOGB)
1546 CASE_MATHFN (BUILT_IN_INF)
1547 CASE_MATHFN (BUILT_IN_J0)
1548 CASE_MATHFN (BUILT_IN_J1)
1549 CASE_MATHFN (BUILT_IN_JN)
1550 CASE_MATHFN (BUILT_IN_LDEXP)
1551 CASE_MATHFN (BUILT_IN_LGAMMA)
1552 CASE_MATHFN (BUILT_IN_LLRINT)
1553 CASE_MATHFN (BUILT_IN_LLROUND)
1554 CASE_MATHFN (BUILT_IN_LOG)
1555 CASE_MATHFN (BUILT_IN_LOG10)
1556 CASE_MATHFN (BUILT_IN_LOG1P)
1557 CASE_MATHFN (BUILT_IN_LOG2)
1558 CASE_MATHFN (BUILT_IN_LOGB)
1559 CASE_MATHFN (BUILT_IN_LRINT)
1560 CASE_MATHFN (BUILT_IN_LROUND)
1561 CASE_MATHFN (BUILT_IN_MODF)
1562 CASE_MATHFN (BUILT_IN_NAN)
1563 CASE_MATHFN (BUILT_IN_NANS)
1564 CASE_MATHFN (BUILT_IN_NEARBYINT)
1565 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1566 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1567 CASE_MATHFN (BUILT_IN_POW)
1568 CASE_MATHFN (BUILT_IN_POWI)
1569 CASE_MATHFN (BUILT_IN_POW10)
1570 CASE_MATHFN (BUILT_IN_REMAINDER)
1571 CASE_MATHFN (BUILT_IN_REMQUO)
1572 CASE_MATHFN (BUILT_IN_RINT)
1573 CASE_MATHFN (BUILT_IN_ROUND)
1574 CASE_MATHFN (BUILT_IN_SCALB)
1575 CASE_MATHFN (BUILT_IN_SCALBLN)
1576 CASE_MATHFN (BUILT_IN_SCALBN)
1577 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1578 CASE_MATHFN (BUILT_IN_SIN)
1579 CASE_MATHFN (BUILT_IN_SINCOS)
1580 CASE_MATHFN (BUILT_IN_SINH)
1581 CASE_MATHFN (BUILT_IN_SQRT)
1582 CASE_MATHFN (BUILT_IN_TAN)
1583 CASE_MATHFN (BUILT_IN_TANH)
1584 CASE_MATHFN (BUILT_IN_TGAMMA)
1585 CASE_MATHFN (BUILT_IN_TRUNC)
1586 CASE_MATHFN (BUILT_IN_Y0)
1587 CASE_MATHFN (BUILT_IN_Y1)
1588 CASE_MATHFN (BUILT_IN_YN)
1590 default:
1591 return 0;
1594 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1595 return implicit_built_in_decls[fcode];
1596 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1597 return implicit_built_in_decls[fcodef];
1598 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1599 return implicit_built_in_decls[fcodel];
1600 else
1601 return 0;
1604 /* If errno must be maintained, expand the RTL to check if the result,
1605 TARGET, of a built-in function call, EXP, is NaN, and if so set
1606 errno to EDOM. */
1608 static void
1609 expand_errno_check (tree exp, rtx target)
1611 rtx lab = gen_label_rtx ();
1613 /* Test the result; if it is NaN, set errno=EDOM because
1614 the argument was not in the domain. */
1615 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1616 0, lab);
1618 #ifdef TARGET_EDOM
1619 /* If this built-in doesn't throw an exception, set errno directly. */
1620 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1622 #ifdef GEN_ERRNO_RTX
1623 rtx errno_rtx = GEN_ERRNO_RTX;
1624 #else
1625 rtx errno_rtx
1626 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1627 #endif
1628 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1629 emit_label (lab);
1630 return;
1632 #endif
1634 /* We can't set errno=EDOM directly; let the library call do it.
1635 Pop the arguments right away in case the call gets deleted. */
1636 NO_DEFER_POP;
1637 expand_call (exp, target, 0);
1638 OK_DEFER_POP;
1639 emit_label (lab);
1643 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1644 Return 0 if a normal call should be emitted rather than expanding the
1645 function in-line. EXP is the expression that is a call to the builtin
1646 function; if convenient, the result should be placed in TARGET.
1647 SUBTARGET may be used as the target for computing one of EXP's operands. */
1649 static rtx
1650 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1652 optab builtin_optab;
1653 rtx op0, insns, before_call;
1654 tree fndecl = get_callee_fndecl (exp);
1655 tree arglist = TREE_OPERAND (exp, 1);
1656 enum machine_mode mode;
1657 bool errno_set = false;
1658 tree arg, narg;
1660 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1661 return 0;
1663 arg = TREE_VALUE (arglist);
1665 switch (DECL_FUNCTION_CODE (fndecl))
1667 case BUILT_IN_SQRT:
1668 case BUILT_IN_SQRTF:
1669 case BUILT_IN_SQRTL:
1670 errno_set = ! tree_expr_nonnegative_p (arg);
1671 builtin_optab = sqrt_optab;
1672 break;
1673 case BUILT_IN_EXP:
1674 case BUILT_IN_EXPF:
1675 case BUILT_IN_EXPL:
1676 errno_set = true; builtin_optab = exp_optab; break;
1677 case BUILT_IN_EXP10:
1678 case BUILT_IN_EXP10F:
1679 case BUILT_IN_EXP10L:
1680 case BUILT_IN_POW10:
1681 case BUILT_IN_POW10F:
1682 case BUILT_IN_POW10L:
1683 errno_set = true; builtin_optab = exp10_optab; break;
1684 case BUILT_IN_EXP2:
1685 case BUILT_IN_EXP2F:
1686 case BUILT_IN_EXP2L:
1687 errno_set = true; builtin_optab = exp2_optab; break;
1688 case BUILT_IN_EXPM1:
1689 case BUILT_IN_EXPM1F:
1690 case BUILT_IN_EXPM1L:
1691 errno_set = true; builtin_optab = expm1_optab; break;
1692 case BUILT_IN_LOGB:
1693 case BUILT_IN_LOGBF:
1694 case BUILT_IN_LOGBL:
1695 errno_set = true; builtin_optab = logb_optab; break;
1696 case BUILT_IN_ILOGB:
1697 case BUILT_IN_ILOGBF:
1698 case BUILT_IN_ILOGBL:
1699 errno_set = true; builtin_optab = ilogb_optab; break;
1700 case BUILT_IN_LOG:
1701 case BUILT_IN_LOGF:
1702 case BUILT_IN_LOGL:
1703 errno_set = true; builtin_optab = log_optab; break;
1704 case BUILT_IN_LOG10:
1705 case BUILT_IN_LOG10F:
1706 case BUILT_IN_LOG10L:
1707 errno_set = true; builtin_optab = log10_optab; break;
1708 case BUILT_IN_LOG2:
1709 case BUILT_IN_LOG2F:
1710 case BUILT_IN_LOG2L:
1711 errno_set = true; builtin_optab = log2_optab; break;
1712 case BUILT_IN_LOG1P:
1713 case BUILT_IN_LOG1PF:
1714 case BUILT_IN_LOG1PL:
1715 errno_set = true; builtin_optab = log1p_optab; break;
1716 case BUILT_IN_ASIN:
1717 case BUILT_IN_ASINF:
1718 case BUILT_IN_ASINL:
1719 builtin_optab = asin_optab; break;
1720 case BUILT_IN_ACOS:
1721 case BUILT_IN_ACOSF:
1722 case BUILT_IN_ACOSL:
1723 builtin_optab = acos_optab; break;
1724 case BUILT_IN_TAN:
1725 case BUILT_IN_TANF:
1726 case BUILT_IN_TANL:
1727 builtin_optab = tan_optab; break;
1728 case BUILT_IN_ATAN:
1729 case BUILT_IN_ATANF:
1730 case BUILT_IN_ATANL:
1731 builtin_optab = atan_optab; break;
1732 case BUILT_IN_FLOOR:
1733 case BUILT_IN_FLOORF:
1734 case BUILT_IN_FLOORL:
1735 builtin_optab = floor_optab; break;
1736 case BUILT_IN_CEIL:
1737 case BUILT_IN_CEILF:
1738 case BUILT_IN_CEILL:
1739 builtin_optab = ceil_optab; break;
1740 case BUILT_IN_TRUNC:
1741 case BUILT_IN_TRUNCF:
1742 case BUILT_IN_TRUNCL:
1743 builtin_optab = btrunc_optab; break;
1744 case BUILT_IN_ROUND:
1745 case BUILT_IN_ROUNDF:
1746 case BUILT_IN_ROUNDL:
1747 builtin_optab = round_optab; break;
1748 case BUILT_IN_NEARBYINT:
1749 case BUILT_IN_NEARBYINTF:
1750 case BUILT_IN_NEARBYINTL:
1751 builtin_optab = nearbyint_optab; break;
1752 case BUILT_IN_RINT:
1753 case BUILT_IN_RINTF:
1754 case BUILT_IN_RINTL:
1755 builtin_optab = rint_optab; break;
1756 case BUILT_IN_LRINT:
1757 case BUILT_IN_LRINTF:
1758 case BUILT_IN_LRINTL:
1759 case BUILT_IN_LLRINT:
1760 case BUILT_IN_LLRINTF:
1761 case BUILT_IN_LLRINTL:
1762 builtin_optab = lrint_optab; break;
1763 default:
1764 gcc_unreachable ();
1767 /* Make a suitable register to place result in. */
1768 mode = TYPE_MODE (TREE_TYPE (exp));
1770 if (! flag_errno_math || ! HONOR_NANS (mode))
1771 errno_set = false;
1773 /* Before working hard, check whether the instruction is available. */
1774 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1776 target = gen_reg_rtx (mode);
1778 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1779 need to expand the argument again. This way, we will not perform
1780 side-effects more the once. */
1781 narg = builtin_save_expr (arg);
1782 if (narg != arg)
1784 arg = narg;
1785 arglist = build_tree_list (NULL_TREE, arg);
1786 exp = build_function_call_expr (fndecl, arglist);
1789 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1791 start_sequence ();
1793 /* Compute into TARGET.
1794 Set TARGET to wherever the result comes back. */
1795 target = expand_unop (mode, builtin_optab, op0, target, 0);
1797 if (target != 0)
1799 if (errno_set)
1800 expand_errno_check (exp, target);
1802 /* Output the entire sequence. */
1803 insns = get_insns ();
1804 end_sequence ();
1805 emit_insn (insns);
1806 return target;
1809 /* If we were unable to expand via the builtin, stop the sequence
1810 (without outputting the insns) and call to the library function
1811 with the stabilized argument list. */
1812 end_sequence ();
1815 before_call = get_last_insn ();
1817 target = expand_call (exp, target, target == const0_rtx);
1819 /* If this is a sqrt operation and we don't care about errno, try to
1820 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1821 This allows the semantics of the libcall to be visible to the RTL
1822 optimizers. */
1823 if (builtin_optab == sqrt_optab && !errno_set)
1825 /* Search backwards through the insns emitted by expand_call looking
1826 for the instruction with the REG_RETVAL note. */
1827 rtx last = get_last_insn ();
1828 while (last != before_call)
1830 if (find_reg_note (last, REG_RETVAL, NULL))
1832 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1833 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1834 two elements, i.e. symbol_ref(sqrt) and the operand. */
1835 if (note
1836 && GET_CODE (note) == EXPR_LIST
1837 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1838 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1839 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1841 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1842 /* Check operand is a register with expected mode. */
1843 if (operand
1844 && REG_P (operand)
1845 && GET_MODE (operand) == mode)
1847 /* Replace the REG_EQUAL note with a SQRT rtx. */
1848 rtx equiv = gen_rtx_SQRT (mode, operand);
1849 set_unique_reg_note (last, REG_EQUAL, equiv);
1852 break;
1854 last = PREV_INSN (last);
1858 return target;
1861 /* Expand a call to the builtin binary math functions (pow and atan2).
1862 Return 0 if a normal call should be emitted rather than expanding the
1863 function in-line. EXP is the expression that is a call to the builtin
1864 function; if convenient, the result should be placed in TARGET.
1865 SUBTARGET may be used as the target for computing one of EXP's
1866 operands. */
1868 static rtx
1869 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1871 optab builtin_optab;
1872 rtx op0, op1, insns;
1873 int op1_type = REAL_TYPE;
1874 tree fndecl = get_callee_fndecl (exp);
1875 tree arglist = TREE_OPERAND (exp, 1);
1876 tree arg0, arg1, temp, narg;
1877 enum machine_mode mode;
1878 bool errno_set = true;
1879 bool stable = true;
1881 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1882 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1883 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1884 op1_type = INTEGER_TYPE;
1886 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1887 return 0;
1889 arg0 = TREE_VALUE (arglist);
1890 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1892 switch (DECL_FUNCTION_CODE (fndecl))
1894 case BUILT_IN_POW:
1895 case BUILT_IN_POWF:
1896 case BUILT_IN_POWL:
1897 builtin_optab = pow_optab; break;
1898 case BUILT_IN_ATAN2:
1899 case BUILT_IN_ATAN2F:
1900 case BUILT_IN_ATAN2L:
1901 builtin_optab = atan2_optab; break;
1902 case BUILT_IN_LDEXP:
1903 case BUILT_IN_LDEXPF:
1904 case BUILT_IN_LDEXPL:
1905 builtin_optab = ldexp_optab; break;
1906 case BUILT_IN_FMOD:
1907 case BUILT_IN_FMODF:
1908 case BUILT_IN_FMODL:
1909 builtin_optab = fmod_optab; break;
1910 case BUILT_IN_DREM:
1911 case BUILT_IN_DREMF:
1912 case BUILT_IN_DREML:
1913 builtin_optab = drem_optab; break;
1914 default:
1915 gcc_unreachable ();
1918 /* Make a suitable register to place result in. */
1919 mode = TYPE_MODE (TREE_TYPE (exp));
1921 /* Before working hard, check whether the instruction is available. */
1922 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1923 return 0;
1925 target = gen_reg_rtx (mode);
1927 if (! flag_errno_math || ! HONOR_NANS (mode))
1928 errno_set = false;
1930 /* Always stabilize the argument list. */
1931 narg = builtin_save_expr (arg1);
1932 if (narg != arg1)
1934 arg1 = narg;
1935 temp = build_tree_list (NULL_TREE, narg);
1936 stable = false;
1938 else
1939 temp = TREE_CHAIN (arglist);
1941 narg = builtin_save_expr (arg0);
1942 if (narg != arg0)
1944 arg0 = narg;
1945 arglist = tree_cons (NULL_TREE, narg, temp);
1946 stable = false;
1948 else if (! stable)
1949 arglist = tree_cons (NULL_TREE, arg0, temp);
1951 if (! stable)
1952 exp = build_function_call_expr (fndecl, arglist);
1954 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1955 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1957 start_sequence ();
1959 /* Compute into TARGET.
1960 Set TARGET to wherever the result comes back. */
1961 target = expand_binop (mode, builtin_optab, op0, op1,
1962 target, 0, OPTAB_DIRECT);
1964 /* If we were unable to expand via the builtin, stop the sequence
1965 (without outputting the insns) and call to the library function
1966 with the stabilized argument list. */
1967 if (target == 0)
1969 end_sequence ();
1970 return expand_call (exp, target, target == const0_rtx);
1973 if (errno_set)
1974 expand_errno_check (exp, target);
1976 /* Output the entire sequence. */
1977 insns = get_insns ();
1978 end_sequence ();
1979 emit_insn (insns);
1981 return target;
1984 /* Expand a call to the builtin sin and cos math functions.
1985 Return 0 if a normal call should be emitted rather than expanding the
1986 function in-line. EXP is the expression that is a call to the builtin
1987 function; if convenient, the result should be placed in TARGET.
1988 SUBTARGET may be used as the target for computing one of EXP's
1989 operands. */
1991 static rtx
1992 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1994 optab builtin_optab;
1995 rtx op0, insns;
1996 tree fndecl = get_callee_fndecl (exp);
1997 tree arglist = TREE_OPERAND (exp, 1);
1998 enum machine_mode mode;
1999 bool errno_set = false;
2000 tree arg, narg;
2002 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2003 return 0;
2005 arg = TREE_VALUE (arglist);
2007 switch (DECL_FUNCTION_CODE (fndecl))
2009 case BUILT_IN_SIN:
2010 case BUILT_IN_SINF:
2011 case BUILT_IN_SINL:
2012 case BUILT_IN_COS:
2013 case BUILT_IN_COSF:
2014 case BUILT_IN_COSL:
2015 builtin_optab = sincos_optab; break;
2016 default:
2017 gcc_unreachable ();
2020 /* Make a suitable register to place result in. */
2021 mode = TYPE_MODE (TREE_TYPE (exp));
2023 if (! flag_errno_math || ! HONOR_NANS (mode))
2024 errno_set = false;
2026 /* Check if sincos insn is available, otherwise fallback
2027 to sin or cos insn. */
2028 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2029 switch (DECL_FUNCTION_CODE (fndecl))
2031 case BUILT_IN_SIN:
2032 case BUILT_IN_SINF:
2033 case BUILT_IN_SINL:
2034 builtin_optab = sin_optab; break;
2035 case BUILT_IN_COS:
2036 case BUILT_IN_COSF:
2037 case BUILT_IN_COSL:
2038 builtin_optab = cos_optab; break;
2039 default:
2040 gcc_unreachable ();
2044 /* Before working hard, check whether the instruction is available. */
2045 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2047 target = gen_reg_rtx (mode);
2049 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2050 need to expand the argument again. This way, we will not perform
2051 side-effects more the once. */
2052 narg = save_expr (arg);
2053 if (narg != arg)
2055 arg = narg;
2056 arglist = build_tree_list (NULL_TREE, arg);
2057 exp = build_function_call_expr (fndecl, arglist);
2060 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2062 start_sequence ();
2064 /* Compute into TARGET.
2065 Set TARGET to wherever the result comes back. */
2066 if (builtin_optab == sincos_optab)
2068 int result;
2070 switch (DECL_FUNCTION_CODE (fndecl))
2072 case BUILT_IN_SIN:
2073 case BUILT_IN_SINF:
2074 case BUILT_IN_SINL:
2075 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2076 break;
2077 case BUILT_IN_COS:
2078 case BUILT_IN_COSF:
2079 case BUILT_IN_COSL:
2080 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2081 break;
2082 default:
2083 gcc_unreachable ();
2085 gcc_assert (result);
2087 else
2089 target = expand_unop (mode, builtin_optab, op0, target, 0);
2092 if (target != 0)
2094 if (errno_set)
2095 expand_errno_check (exp, target);
2097 /* Output the entire sequence. */
2098 insns = get_insns ();
2099 end_sequence ();
2100 emit_insn (insns);
2101 return target;
2104 /* If we were unable to expand via the builtin, stop the sequence
2105 (without outputting the insns) and call to the library function
2106 with the stabilized argument list. */
2107 end_sequence ();
2110 target = expand_call (exp, target, target == const0_rtx);
2112 return target;
2115 /* To evaluate powi(x,n), the floating point value x raised to the
2116 constant integer exponent n, we use a hybrid algorithm that
2117 combines the "window method" with look-up tables. For an
2118 introduction to exponentiation algorithms and "addition chains",
2119 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2120 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2121 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2122 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2124 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2125 multiplications to inline before calling the system library's pow
2126 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2127 so this default never requires calling pow, powf or powl. */
2129 #ifndef POWI_MAX_MULTS
2130 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2131 #endif
2133 /* The size of the "optimal power tree" lookup table. All
2134 exponents less than this value are simply looked up in the
2135 powi_table below. This threshold is also used to size the
2136 cache of pseudo registers that hold intermediate results. */
2137 #define POWI_TABLE_SIZE 256
2139 /* The size, in bits of the window, used in the "window method"
2140 exponentiation algorithm. This is equivalent to a radix of
2141 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2142 #define POWI_WINDOW_SIZE 3
2144 /* The following table is an efficient representation of an
2145 "optimal power tree". For each value, i, the corresponding
2146 value, j, in the table states than an optimal evaluation
2147 sequence for calculating pow(x,i) can be found by evaluating
2148 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2149 100 integers is given in Knuth's "Seminumerical algorithms". */
2151 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2153 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2154 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2155 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2156 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2157 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2158 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2159 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2160 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2161 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2162 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2163 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2164 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2165 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2166 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2167 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2168 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2169 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2170 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2171 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2172 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2173 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2174 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2175 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2176 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2177 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2178 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2179 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2180 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2181 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2182 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2183 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2184 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2188 /* Return the number of multiplications required to calculate
2189 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2190 subroutine of powi_cost. CACHE is an array indicating
2191 which exponents have already been calculated. */
2193 static int
2194 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2196 /* If we've already calculated this exponent, then this evaluation
2197 doesn't require any additional multiplications. */
2198 if (cache[n])
2199 return 0;
2201 cache[n] = true;
2202 return powi_lookup_cost (n - powi_table[n], cache)
2203 + powi_lookup_cost (powi_table[n], cache) + 1;
2206 /* Return the number of multiplications required to calculate
2207 powi(x,n) for an arbitrary x, given the exponent N. This
2208 function needs to be kept in sync with expand_powi below. */
2210 static int
2211 powi_cost (HOST_WIDE_INT n)
2213 bool cache[POWI_TABLE_SIZE];
2214 unsigned HOST_WIDE_INT digit;
2215 unsigned HOST_WIDE_INT val;
2216 int result;
2218 if (n == 0)
2219 return 0;
2221 /* Ignore the reciprocal when calculating the cost. */
2222 val = (n < 0) ? -n : n;
2224 /* Initialize the exponent cache. */
2225 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2226 cache[1] = true;
2228 result = 0;
2230 while (val >= POWI_TABLE_SIZE)
2232 if (val & 1)
2234 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2235 result += powi_lookup_cost (digit, cache)
2236 + POWI_WINDOW_SIZE + 1;
2237 val >>= POWI_WINDOW_SIZE;
2239 else
2241 val >>= 1;
2242 result++;
2246 return result + powi_lookup_cost (val, cache);
2249 /* Recursive subroutine of expand_powi. This function takes the array,
2250 CACHE, of already calculated exponents and an exponent N and returns
2251 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2253 static rtx
2254 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2256 unsigned HOST_WIDE_INT digit;
2257 rtx target, result;
2258 rtx op0, op1;
2260 if (n < POWI_TABLE_SIZE)
2262 if (cache[n])
2263 return cache[n];
2265 target = gen_reg_rtx (mode);
2266 cache[n] = target;
2268 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2269 op1 = expand_powi_1 (mode, powi_table[n], cache);
2271 else if (n & 1)
2273 target = gen_reg_rtx (mode);
2274 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2275 op0 = expand_powi_1 (mode, n - digit, cache);
2276 op1 = expand_powi_1 (mode, digit, cache);
2278 else
2280 target = gen_reg_rtx (mode);
2281 op0 = expand_powi_1 (mode, n >> 1, cache);
2282 op1 = op0;
2285 result = expand_mult (mode, op0, op1, target, 0);
2286 if (result != target)
2287 emit_move_insn (target, result);
2288 return target;
2291 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2292 floating point operand in mode MODE, and N is the exponent. This
2293 function needs to be kept in sync with powi_cost above. */
2295 static rtx
2296 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2298 unsigned HOST_WIDE_INT val;
2299 rtx cache[POWI_TABLE_SIZE];
2300 rtx result;
2302 if (n == 0)
2303 return CONST1_RTX (mode);
2305 val = (n < 0) ? -n : n;
2307 memset (cache, 0, sizeof (cache));
2308 cache[1] = x;
2310 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2312 /* If the original exponent was negative, reciprocate the result. */
2313 if (n < 0)
2314 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2315 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2317 return result;
2320 /* Expand a call to the pow built-in mathematical function. Return 0 if
2321 a normal call should be emitted rather than expanding the function
2322 in-line. EXP is the expression that is a call to the builtin
2323 function; if convenient, the result should be placed in TARGET. */
2325 static rtx
2326 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2328 tree arglist = TREE_OPERAND (exp, 1);
2329 tree arg0, arg1;
2331 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2332 return 0;
2334 arg0 = TREE_VALUE (arglist);
2335 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2337 if (TREE_CODE (arg1) == REAL_CST
2338 && ! TREE_CONSTANT_OVERFLOW (arg1))
2340 REAL_VALUE_TYPE cint;
2341 REAL_VALUE_TYPE c;
2342 HOST_WIDE_INT n;
2344 c = TREE_REAL_CST (arg1);
2345 n = real_to_integer (&c);
2346 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2347 if (real_identical (&c, &cint))
2349 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2350 Otherwise, check the number of multiplications required.
2351 Note that pow never sets errno for an integer exponent. */
2352 if ((n >= -1 && n <= 2)
2353 || (flag_unsafe_math_optimizations
2354 && ! optimize_size
2355 && powi_cost (n) <= POWI_MAX_MULTS))
2357 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2358 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2359 op = force_reg (mode, op);
2360 return expand_powi (op, mode, n);
2365 if (! flag_unsafe_math_optimizations)
2366 return NULL_RTX;
2367 return expand_builtin_mathfn_2 (exp, target, subtarget);
2370 /* Expand a call to the powi built-in mathematical function. Return 0 if
2371 a normal call should be emitted rather than expanding the function
2372 in-line. EXP is the expression that is a call to the builtin
2373 function; if convenient, the result should be placed in TARGET. */
2375 static rtx
2376 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2378 tree arglist = TREE_OPERAND (exp, 1);
2379 tree arg0, arg1;
2380 rtx op0, op1;
2381 enum machine_mode mode;
2382 enum machine_mode mode2;
2384 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2385 return 0;
2387 arg0 = TREE_VALUE (arglist);
2388 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2389 mode = TYPE_MODE (TREE_TYPE (exp));
2391 /* Handle constant power. */
2393 if (TREE_CODE (arg1) == INTEGER_CST
2394 && ! TREE_CONSTANT_OVERFLOW (arg1))
2396 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2398 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2399 Otherwise, check the number of multiplications required. */
2400 if ((TREE_INT_CST_HIGH (arg1) == 0
2401 || TREE_INT_CST_HIGH (arg1) == -1)
2402 && ((n >= -1 && n <= 2)
2403 || (! optimize_size
2404 && powi_cost (n) <= POWI_MAX_MULTS)))
2406 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2407 op0 = force_reg (mode, op0);
2408 return expand_powi (op0, mode, n);
2412 /* Emit a libcall to libgcc. */
2414 /* Mode of the 2nd argument must match that of an int. */
2415 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2417 if (target == NULL_RTX)
2418 target = gen_reg_rtx (mode);
2420 op0 = expand_expr (arg0, subtarget, mode, 0);
2421 if (GET_MODE (op0) != mode)
2422 op0 = convert_to_mode (mode, op0, 0);
2423 op1 = expand_expr (arg1, 0, mode2, 0);
2424 if (GET_MODE (op1) != mode2)
2425 op1 = convert_to_mode (mode2, op1, 0);
2427 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2428 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2429 op0, mode, op1, mode2);
2431 return target;
2434 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2435 if we failed the caller should emit a normal call, otherwise
2436 try to get the result in TARGET, if convenient. */
2438 static rtx
2439 expand_builtin_strlen (tree arglist, rtx target,
2440 enum machine_mode target_mode)
2442 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2443 return 0;
2444 else
2446 rtx pat;
2447 tree len, src = TREE_VALUE (arglist);
2448 rtx result, src_reg, char_rtx, before_strlen;
2449 enum machine_mode insn_mode = target_mode, char_mode;
2450 enum insn_code icode = CODE_FOR_nothing;
2451 int align;
2453 /* If the length can be computed at compile-time, return it. */
2454 len = c_strlen (src, 0);
2455 if (len)
2456 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2458 /* If the length can be computed at compile-time and is constant
2459 integer, but there are side-effects in src, evaluate
2460 src for side-effects, then return len.
2461 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2462 can be optimized into: i++; x = 3; */
2463 len = c_strlen (src, 1);
2464 if (len && TREE_CODE (len) == INTEGER_CST)
2466 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2467 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2470 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2472 /* If SRC is not a pointer type, don't do this operation inline. */
2473 if (align == 0)
2474 return 0;
2476 /* Bail out if we can't compute strlen in the right mode. */
2477 while (insn_mode != VOIDmode)
2479 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2480 if (icode != CODE_FOR_nothing)
2481 break;
2483 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2485 if (insn_mode == VOIDmode)
2486 return 0;
2488 /* Make a place to write the result of the instruction. */
2489 result = target;
2490 if (! (result != 0
2491 && REG_P (result)
2492 && GET_MODE (result) == insn_mode
2493 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2494 result = gen_reg_rtx (insn_mode);
2496 /* Make a place to hold the source address. We will not expand
2497 the actual source until we are sure that the expansion will
2498 not fail -- there are trees that cannot be expanded twice. */
2499 src_reg = gen_reg_rtx (Pmode);
2501 /* Mark the beginning of the strlen sequence so we can emit the
2502 source operand later. */
2503 before_strlen = get_last_insn ();
2505 char_rtx = const0_rtx;
2506 char_mode = insn_data[(int) icode].operand[2].mode;
2507 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2508 char_mode))
2509 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2511 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2512 char_rtx, GEN_INT (align));
2513 if (! pat)
2514 return 0;
2515 emit_insn (pat);
2517 /* Now that we are assured of success, expand the source. */
2518 start_sequence ();
2519 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2520 if (pat != src_reg)
2521 emit_move_insn (src_reg, pat);
2522 pat = get_insns ();
2523 end_sequence ();
2525 if (before_strlen)
2526 emit_insn_after (pat, before_strlen);
2527 else
2528 emit_insn_before (pat, get_insns ());
2530 /* Return the value in the proper mode for this function. */
2531 if (GET_MODE (result) == target_mode)
2532 target = result;
2533 else if (target != 0)
2534 convert_move (target, result, 0);
2535 else
2536 target = convert_to_mode (target_mode, result, 0);
2538 return target;
2542 /* Expand a call to the strstr builtin. Return 0 if we failed the
2543 caller should emit a normal call, otherwise try to get the result
2544 in TARGET, if convenient (and in mode MODE if that's convenient). */
2546 static rtx
2547 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2549 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2551 tree result = fold_builtin_strstr (arglist, type);
2552 if (result)
2553 return expand_expr (result, target, mode, EXPAND_NORMAL);
2555 return 0;
2558 /* Expand a call to the strchr builtin. Return 0 if we failed the
2559 caller should emit a normal call, otherwise try to get the result
2560 in TARGET, if convenient (and in mode MODE if that's convenient). */
2562 static rtx
2563 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2565 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2567 tree result = fold_builtin_strchr (arglist, type);
2568 if (result)
2569 return expand_expr (result, target, mode, EXPAND_NORMAL);
2571 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2573 return 0;
2576 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2577 caller should emit a normal call, otherwise try to get the result
2578 in TARGET, if convenient (and in mode MODE if that's convenient). */
2580 static rtx
2581 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2583 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2585 tree result = fold_builtin_strrchr (arglist, type);
2586 if (result)
2587 return expand_expr (result, target, mode, EXPAND_NORMAL);
2589 return 0;
2592 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2593 caller should emit a normal call, otherwise try to get the result
2594 in TARGET, if convenient (and in mode MODE if that's convenient). */
2596 static rtx
2597 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2599 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2601 tree result = fold_builtin_strpbrk (arglist, type);
2602 if (result)
2603 return expand_expr (result, target, mode, EXPAND_NORMAL);
2605 return 0;
2608 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2609 bytes from constant string DATA + OFFSET and return it as target
2610 constant. */
2612 static rtx
2613 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2614 enum machine_mode mode)
2616 const char *str = (const char *) data;
2618 gcc_assert (offset >= 0
2619 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2620 <= strlen (str) + 1));
2622 return c_readstr (str + offset, mode);
2625 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2626 Return 0 if we failed, the caller should emit a normal call,
2627 otherwise try to get the result in TARGET, if convenient (and in
2628 mode MODE if that's convenient). */
2629 static rtx
2630 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2632 tree fndecl = get_callee_fndecl (exp);
2633 tree arglist = TREE_OPERAND (exp, 1);
2634 if (!validate_arglist (arglist,
2635 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2636 return 0;
2637 else
2639 tree dest = TREE_VALUE (arglist);
2640 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2641 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2642 const char *src_str;
2643 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2644 unsigned int dest_align
2645 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2646 rtx dest_mem, src_mem, dest_addr, len_rtx;
2647 tree result = fold_builtin_memcpy (fndecl, arglist);
2649 if (result)
2650 return expand_expr (result, target, mode, EXPAND_NORMAL);
2652 /* If DEST is not a pointer type, call the normal function. */
2653 if (dest_align == 0)
2654 return 0;
2656 /* If either SRC is not a pointer type, don't do this
2657 operation in-line. */
2658 if (src_align == 0)
2659 return 0;
2661 dest_mem = get_memory_rtx (dest);
2662 set_mem_align (dest_mem, dest_align);
2663 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2664 src_str = c_getstr (src);
2666 /* If SRC is a string constant and block move would be done
2667 by pieces, we can avoid loading the string from memory
2668 and only stored the computed constants. */
2669 if (src_str
2670 && GET_CODE (len_rtx) == CONST_INT
2671 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2672 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2673 (void *) src_str, dest_align))
2675 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2676 builtin_memcpy_read_str,
2677 (void *) src_str, dest_align, 0);
2678 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2679 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2680 return dest_mem;
2683 src_mem = get_memory_rtx (src);
2684 set_mem_align (src_mem, src_align);
2686 /* Copy word part most expediently. */
2687 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2688 BLOCK_OP_NORMAL);
2690 if (dest_addr == 0)
2692 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2693 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2695 return dest_addr;
2699 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2700 Return 0 if we failed the caller should emit a normal call,
2701 otherwise try to get the result in TARGET, if convenient (and in
2702 mode MODE if that's convenient). If ENDP is 0 return the
2703 destination pointer, if ENDP is 1 return the end pointer ala
2704 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2705 stpcpy. */
2707 static rtx
2708 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2709 int endp)
2711 if (!validate_arglist (arglist,
2712 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2713 return 0;
2714 /* If return value is ignored, transform mempcpy into memcpy. */
2715 else if (target == const0_rtx)
2717 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2719 if (!fn)
2720 return 0;
2722 return expand_expr (build_function_call_expr (fn, arglist),
2723 target, mode, EXPAND_NORMAL);
2725 else
2727 tree dest = TREE_VALUE (arglist);
2728 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2729 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2730 const char *src_str;
2731 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2732 unsigned int dest_align
2733 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2734 rtx dest_mem, src_mem, len_rtx;
2735 tree result = fold_builtin_mempcpy (arglist, type, endp);
2737 if (result)
2738 return expand_expr (result, target, mode, EXPAND_NORMAL);
2740 /* If either SRC or DEST is not a pointer type, don't do this
2741 operation in-line. */
2742 if (dest_align == 0 || src_align == 0)
2743 return 0;
2745 /* If LEN is not constant, call the normal function. */
2746 if (! host_integerp (len, 1))
2747 return 0;
2749 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2750 src_str = c_getstr (src);
2752 /* If SRC is a string constant and block move would be done
2753 by pieces, we can avoid loading the string from memory
2754 and only stored the computed constants. */
2755 if (src_str
2756 && GET_CODE (len_rtx) == CONST_INT
2757 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2758 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2759 (void *) src_str, dest_align))
2761 dest_mem = get_memory_rtx (dest);
2762 set_mem_align (dest_mem, dest_align);
2763 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2764 builtin_memcpy_read_str,
2765 (void *) src_str, dest_align, endp);
2766 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2767 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2768 return dest_mem;
2771 if (GET_CODE (len_rtx) == CONST_INT
2772 && can_move_by_pieces (INTVAL (len_rtx),
2773 MIN (dest_align, src_align)))
2775 dest_mem = get_memory_rtx (dest);
2776 set_mem_align (dest_mem, dest_align);
2777 src_mem = get_memory_rtx (src);
2778 set_mem_align (src_mem, src_align);
2779 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2780 MIN (dest_align, src_align), endp);
2781 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2782 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2783 return dest_mem;
2786 return 0;
2790 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2791 if we failed the caller should emit a normal call. */
2793 static rtx
2794 expand_builtin_memmove (tree arglist, tree type, rtx target,
2795 enum machine_mode mode)
2797 if (!validate_arglist (arglist,
2798 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2799 return 0;
2800 else
2802 tree dest = TREE_VALUE (arglist);
2803 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2804 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2806 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2807 unsigned int dest_align
2808 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2809 tree result = fold_builtin_memmove (arglist, type);
2811 if (result)
2812 return expand_expr (result, target, mode, EXPAND_NORMAL);
2814 /* If DEST is not a pointer type, call the normal function. */
2815 if (dest_align == 0)
2816 return 0;
2818 /* If either SRC is not a pointer type, don't do this
2819 operation in-line. */
2820 if (src_align == 0)
2821 return 0;
2823 /* If src is categorized for a readonly section we can use
2824 normal memcpy. */
2825 if (readonly_data_expr (src))
2827 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2828 if (!fn)
2829 return 0;
2830 return expand_expr (build_function_call_expr (fn, arglist),
2831 target, mode, EXPAND_NORMAL);
2834 /* If length is 1 and we can expand memcpy call inline,
2835 it is ok to use memcpy as well. */
2836 if (integer_onep (len))
2838 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2839 /*endp=*/0);
2840 if (ret)
2841 return ret;
2844 /* Otherwise, call the normal function. */
2845 return 0;
2849 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2850 if we failed the caller should emit a normal call. */
2852 static rtx
2853 expand_builtin_bcopy (tree arglist, tree type)
2855 tree src, dest, size, newarglist;
2857 if (!validate_arglist (arglist,
2858 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2859 return NULL_RTX;
2861 src = TREE_VALUE (arglist);
2862 dest = TREE_VALUE (TREE_CHAIN (arglist));
2863 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2865 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2866 memmove(ptr y, ptr x, size_t z). This is done this way
2867 so that if it isn't expanded inline, we fallback to
2868 calling bcopy instead of memmove. */
2870 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2871 newarglist = tree_cons (NULL_TREE, src, newarglist);
2872 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2874 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2877 #ifndef HAVE_movstr
2878 # define HAVE_movstr 0
2879 # define CODE_FOR_movstr CODE_FOR_nothing
2880 #endif
2882 /* Expand into a movstr instruction, if one is available. Return 0 if
2883 we failed, the caller should emit a normal call, otherwise try to
2884 get the result in TARGET, if convenient. If ENDP is 0 return the
2885 destination pointer, if ENDP is 1 return the end pointer ala
2886 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2887 stpcpy. */
2889 static rtx
2890 expand_movstr (tree dest, tree src, rtx target, int endp)
2892 rtx end;
2893 rtx dest_mem;
2894 rtx src_mem;
2895 rtx insn;
2896 const struct insn_data * data;
2898 if (!HAVE_movstr)
2899 return 0;
2901 dest_mem = get_memory_rtx (dest);
2902 src_mem = get_memory_rtx (src);
2903 if (!endp)
2905 target = force_reg (Pmode, XEXP (dest_mem, 0));
2906 dest_mem = replace_equiv_address (dest_mem, target);
2907 end = gen_reg_rtx (Pmode);
2909 else
2911 if (target == 0 || target == const0_rtx)
2913 end = gen_reg_rtx (Pmode);
2914 if (target == 0)
2915 target = end;
2917 else
2918 end = target;
2921 data = insn_data + CODE_FOR_movstr;
2923 if (data->operand[0].mode != VOIDmode)
2924 end = gen_lowpart (data->operand[0].mode, end);
2926 insn = data->genfun (end, dest_mem, src_mem);
2928 gcc_assert (insn);
2930 emit_insn (insn);
2932 /* movstr is supposed to set end to the address of the NUL
2933 terminator. If the caller requested a mempcpy-like return value,
2934 adjust it. */
2935 if (endp == 1 && target != const0_rtx)
2937 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
2938 emit_move_insn (target, force_operand (tem, NULL_RTX));
2941 return target;
2944 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2945 if we failed the caller should emit a normal call, otherwise try to get
2946 the result in TARGET, if convenient (and in mode MODE if that's
2947 convenient). */
2949 static rtx
2950 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
2952 tree fndecl = get_callee_fndecl (exp);
2953 tree arglist = TREE_OPERAND (exp, 1);
2954 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2956 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
2957 if (result)
2958 return expand_expr (result, target, mode, EXPAND_NORMAL);
2960 return expand_movstr (TREE_VALUE (arglist),
2961 TREE_VALUE (TREE_CHAIN (arglist)),
2962 target, /*endp=*/0);
2964 return 0;
2967 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2968 Return 0 if we failed the caller should emit a normal call,
2969 otherwise try to get the result in TARGET, if convenient (and in
2970 mode MODE if that's convenient). */
2972 static rtx
2973 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
2975 tree arglist = TREE_OPERAND (exp, 1);
2976 /* If return value is ignored, transform stpcpy into strcpy. */
2977 if (target == const0_rtx)
2979 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2980 if (!fn)
2981 return 0;
2983 return expand_expr (build_function_call_expr (fn, arglist),
2984 target, mode, EXPAND_NORMAL);
2987 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2988 return 0;
2989 else
2991 tree dst, src, len, lenp1;
2992 tree narglist;
2993 rtx ret;
2995 /* Ensure we get an actual string whose length can be evaluated at
2996 compile-time, not an expression containing a string. This is
2997 because the latter will potentially produce pessimized code
2998 when used to produce the return value. */
2999 src = TREE_VALUE (TREE_CHAIN (arglist));
3000 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3001 return expand_movstr (TREE_VALUE (arglist),
3002 TREE_VALUE (TREE_CHAIN (arglist)),
3003 target, /*endp=*/2);
3005 dst = TREE_VALUE (arglist);
3006 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3007 narglist = build_tree_list (NULL_TREE, lenp1);
3008 narglist = tree_cons (NULL_TREE, src, narglist);
3009 narglist = tree_cons (NULL_TREE, dst, narglist);
3010 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3011 target, mode, /*endp=*/2);
3013 if (ret)
3014 return ret;
3016 if (TREE_CODE (len) == INTEGER_CST)
3018 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3020 if (GET_CODE (len_rtx) == CONST_INT)
3022 ret = expand_builtin_strcpy (exp, target, mode);
3024 if (ret)
3026 if (! target)
3028 if (mode != VOIDmode)
3029 target = gen_reg_rtx (mode);
3030 else
3031 target = gen_reg_rtx (GET_MODE (ret));
3033 if (GET_MODE (target) != GET_MODE (ret))
3034 ret = gen_lowpart (GET_MODE (target), ret);
3036 ret = plus_constant (ret, INTVAL (len_rtx));
3037 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3038 gcc_assert (ret);
3040 return target;
3045 return expand_movstr (TREE_VALUE (arglist),
3046 TREE_VALUE (TREE_CHAIN (arglist)),
3047 target, /*endp=*/2);
3051 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3052 bytes from constant string DATA + OFFSET and return it as target
3053 constant. */
3055 static rtx
3056 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3057 enum machine_mode mode)
3059 const char *str = (const char *) data;
3061 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3062 return const0_rtx;
3064 return c_readstr (str + offset, mode);
3067 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3068 if we failed the caller should emit a normal call. */
3070 static rtx
3071 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3073 tree fndecl = get_callee_fndecl (exp);
3074 tree arglist = TREE_OPERAND (exp, 1);
3075 if (validate_arglist (arglist,
3076 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3078 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3079 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3080 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3082 if (result)
3083 return expand_expr (result, target, mode, EXPAND_NORMAL);
3085 /* We must be passed a constant len and src parameter. */
3086 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3087 return 0;
3089 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3091 /* We're required to pad with trailing zeros if the requested
3092 len is greater than strlen(s2)+1. In that case try to
3093 use store_by_pieces, if it fails, punt. */
3094 if (tree_int_cst_lt (slen, len))
3096 tree dest = TREE_VALUE (arglist);
3097 unsigned int dest_align
3098 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3099 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3100 rtx dest_mem;
3102 if (!p || dest_align == 0 || !host_integerp (len, 1)
3103 || !can_store_by_pieces (tree_low_cst (len, 1),
3104 builtin_strncpy_read_str,
3105 (void *) p, dest_align))
3106 return 0;
3108 dest_mem = get_memory_rtx (dest);
3109 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3110 builtin_strncpy_read_str,
3111 (void *) p, dest_align, 0);
3112 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3113 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3114 return dest_mem;
3117 return 0;
3120 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3121 bytes from constant string DATA + OFFSET and return it as target
3122 constant. */
3124 static rtx
3125 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3126 enum machine_mode mode)
3128 const char *c = (const char *) data;
3129 char *p = alloca (GET_MODE_SIZE (mode));
3131 memset (p, *c, GET_MODE_SIZE (mode));
3133 return c_readstr (p, mode);
3136 /* Callback routine for store_by_pieces. Return the RTL of a register
3137 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3138 char value given in the RTL register data. For example, if mode is
3139 4 bytes wide, return the RTL for 0x01010101*data. */
3141 static rtx
3142 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3143 enum machine_mode mode)
3145 rtx target, coeff;
3146 size_t size;
3147 char *p;
3149 size = GET_MODE_SIZE (mode);
3150 if (size == 1)
3151 return (rtx) data;
3153 p = alloca (size);
3154 memset (p, 1, size);
3155 coeff = c_readstr (p, mode);
3157 target = convert_to_mode (mode, (rtx) data, 1);
3158 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3159 return force_reg (mode, target);
3162 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3163 if we failed the caller should emit a normal call, otherwise try to get
3164 the result in TARGET, if convenient (and in mode MODE if that's
3165 convenient). */
3167 static rtx
3168 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3170 if (!validate_arglist (arglist,
3171 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3172 return 0;
3173 else
3175 tree dest = TREE_VALUE (arglist);
3176 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3177 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3178 char c;
3180 unsigned int dest_align
3181 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3182 rtx dest_mem, dest_addr, len_rtx;
3184 /* If DEST is not a pointer type, don't do this
3185 operation in-line. */
3186 if (dest_align == 0)
3187 return 0;
3189 /* If the LEN parameter is zero, return DEST. */
3190 if (integer_zerop (len))
3192 /* Evaluate and ignore VAL in case it has side-effects. */
3193 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3194 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3197 if (TREE_CODE (val) != INTEGER_CST)
3199 rtx val_rtx;
3201 if (!host_integerp (len, 1))
3202 return 0;
3204 if (optimize_size && tree_low_cst (len, 1) > 1)
3205 return 0;
3207 /* Assume that we can memset by pieces if we can store the
3208 * the coefficients by pieces (in the required modes).
3209 * We can't pass builtin_memset_gen_str as that emits RTL. */
3210 c = 1;
3211 if (!can_store_by_pieces (tree_low_cst (len, 1),
3212 builtin_memset_read_str,
3213 &c, dest_align))
3214 return 0;
3216 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3217 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3218 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3219 val_rtx);
3220 dest_mem = get_memory_rtx (dest);
3221 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3222 builtin_memset_gen_str,
3223 val_rtx, dest_align, 0);
3224 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3225 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3226 return dest_mem;
3229 if (target_char_cast (val, &c))
3230 return 0;
3232 if (c)
3234 if (!host_integerp (len, 1))
3235 return 0;
3236 if (!can_store_by_pieces (tree_low_cst (len, 1),
3237 builtin_memset_read_str, &c,
3238 dest_align))
3239 return 0;
3241 dest_mem = get_memory_rtx (dest);
3242 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3243 builtin_memset_read_str,
3244 &c, dest_align, 0);
3245 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3246 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3247 return dest_mem;
3250 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3252 dest_mem = get_memory_rtx (dest);
3253 set_mem_align (dest_mem, dest_align);
3254 dest_addr = clear_storage (dest_mem, len_rtx);
3256 if (dest_addr == 0)
3258 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3259 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3262 return dest_addr;
3266 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3267 if we failed the caller should emit a normal call. */
3269 static rtx
3270 expand_builtin_bzero (tree arglist)
3272 tree dest, size, newarglist;
3274 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3275 return NULL_RTX;
3277 dest = TREE_VALUE (arglist);
3278 size = TREE_VALUE (TREE_CHAIN (arglist));
3280 /* New argument list transforming bzero(ptr x, int y) to
3281 memset(ptr x, int 0, size_t y). This is done this way
3282 so that if it isn't expanded inline, we fallback to
3283 calling bzero instead of memset. */
3285 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3286 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3287 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3289 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3292 /* Expand expression EXP, which is a call to the memcmp built-in function.
3293 ARGLIST is the argument list for this call. Return 0 if we failed and the
3294 caller should emit a normal call, otherwise try to get the result in
3295 TARGET, if convenient (and in mode MODE, if that's convenient). */
3297 static rtx
3298 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3299 enum machine_mode mode)
3301 if (!validate_arglist (arglist,
3302 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3303 return 0;
3304 else
3306 tree result = fold_builtin_memcmp (arglist);
3307 if (result)
3308 return expand_expr (result, target, mode, EXPAND_NORMAL);
3311 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3313 tree arg1 = TREE_VALUE (arglist);
3314 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3315 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3316 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3317 rtx result;
3318 rtx insn;
3320 int arg1_align
3321 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3322 int arg2_align
3323 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3324 enum machine_mode insn_mode;
3326 #ifdef HAVE_cmpmemsi
3327 if (HAVE_cmpmemsi)
3328 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3329 else
3330 #endif
3331 #ifdef HAVE_cmpstrsi
3332 if (HAVE_cmpstrsi)
3333 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3334 else
3335 #endif
3336 return 0;
3338 /* If we don't have POINTER_TYPE, call the function. */
3339 if (arg1_align == 0 || arg2_align == 0)
3340 return 0;
3342 /* Make a place to write the result of the instruction. */
3343 result = target;
3344 if (! (result != 0
3345 && REG_P (result) && GET_MODE (result) == insn_mode
3346 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3347 result = gen_reg_rtx (insn_mode);
3349 arg1_rtx = get_memory_rtx (arg1);
3350 arg2_rtx = get_memory_rtx (arg2);
3351 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3353 /* Set MEM_SIZE as appropriate. */
3354 if (GET_CODE (arg3_rtx) == CONST_INT)
3356 set_mem_size (arg1_rtx, arg3_rtx);
3357 set_mem_size (arg2_rtx, arg3_rtx);
3360 #ifdef HAVE_cmpmemsi
3361 if (HAVE_cmpmemsi)
3362 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3363 GEN_INT (MIN (arg1_align, arg2_align)));
3364 else
3365 #endif
3366 #ifdef HAVE_cmpstrsi
3367 if (HAVE_cmpstrsi)
3368 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3369 GEN_INT (MIN (arg1_align, arg2_align)));
3370 else
3371 #endif
3372 gcc_unreachable ();
3374 if (insn)
3375 emit_insn (insn);
3376 else
3377 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3378 TYPE_MODE (integer_type_node), 3,
3379 XEXP (arg1_rtx, 0), Pmode,
3380 XEXP (arg2_rtx, 0), Pmode,
3381 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3382 TYPE_UNSIGNED (sizetype)),
3383 TYPE_MODE (sizetype));
3385 /* Return the value in the proper mode for this function. */
3386 mode = TYPE_MODE (TREE_TYPE (exp));
3387 if (GET_MODE (result) == mode)
3388 return result;
3389 else if (target != 0)
3391 convert_move (target, result, 0);
3392 return target;
3394 else
3395 return convert_to_mode (mode, result, 0);
3397 #endif
3399 return 0;
3402 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3403 if we failed the caller should emit a normal call, otherwise try to get
3404 the result in TARGET, if convenient. */
3406 static rtx
3407 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3409 tree arglist = TREE_OPERAND (exp, 1);
3411 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3412 return 0;
3413 else
3415 tree result = fold_builtin_strcmp (arglist);
3416 if (result)
3417 return expand_expr (result, target, mode, EXPAND_NORMAL);
3420 #ifdef HAVE_cmpstrsi
3421 if (HAVE_cmpstrsi)
3423 tree arg1 = TREE_VALUE (arglist);
3424 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3425 tree len, len1, len2;
3426 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3427 rtx result, insn;
3428 tree fndecl;
3430 int arg1_align
3431 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3432 int arg2_align
3433 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3434 enum machine_mode insn_mode
3435 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3437 len1 = c_strlen (arg1, 1);
3438 len2 = c_strlen (arg2, 1);
3440 if (len1)
3441 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3442 if (len2)
3443 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3445 /* If we don't have a constant length for the first, use the length
3446 of the second, if we know it. We don't require a constant for
3447 this case; some cost analysis could be done if both are available
3448 but neither is constant. For now, assume they're equally cheap,
3449 unless one has side effects. If both strings have constant lengths,
3450 use the smaller. */
3452 if (!len1)
3453 len = len2;
3454 else if (!len2)
3455 len = len1;
3456 else if (TREE_SIDE_EFFECTS (len1))
3457 len = len2;
3458 else if (TREE_SIDE_EFFECTS (len2))
3459 len = len1;
3460 else if (TREE_CODE (len1) != INTEGER_CST)
3461 len = len2;
3462 else if (TREE_CODE (len2) != INTEGER_CST)
3463 len = len1;
3464 else if (tree_int_cst_lt (len1, len2))
3465 len = len1;
3466 else
3467 len = len2;
3469 /* If both arguments have side effects, we cannot optimize. */
3470 if (!len || TREE_SIDE_EFFECTS (len))
3471 return 0;
3473 /* If we don't have POINTER_TYPE, call the function. */
3474 if (arg1_align == 0 || arg2_align == 0)
3475 return 0;
3477 /* Make a place to write the result of the instruction. */
3478 result = target;
3479 if (! (result != 0
3480 && REG_P (result) && GET_MODE (result) == insn_mode
3481 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3482 result = gen_reg_rtx (insn_mode);
3484 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3485 arg1 = builtin_save_expr (arg1);
3486 arg2 = builtin_save_expr (arg2);
3488 arg1_rtx = get_memory_rtx (arg1);
3489 arg2_rtx = get_memory_rtx (arg2);
3490 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3491 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3492 GEN_INT (MIN (arg1_align, arg2_align)));
3493 if (insn)
3495 emit_insn (insn);
3497 /* Return the value in the proper mode for this function. */
3498 mode = TYPE_MODE (TREE_TYPE (exp));
3499 if (GET_MODE (result) == mode)
3500 return result;
3501 if (target == 0)
3502 return convert_to_mode (mode, result, 0);
3503 convert_move (target, result, 0);
3504 return target;
3507 /* Expand the library call ourselves using a stabilized argument
3508 list to avoid re-evaluating the function's arguments twice. */
3509 arglist = build_tree_list (NULL_TREE, arg2);
3510 arglist = tree_cons (NULL_TREE, arg1, arglist);
3511 fndecl = get_callee_fndecl (exp);
3512 exp = build_function_call_expr (fndecl, arglist);
3513 return expand_call (exp, target, target == const0_rtx);
3515 #endif
3516 return 0;
3519 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3520 if we failed the caller should emit a normal call, otherwise try to get
3521 the result in TARGET, if convenient. */
3523 static rtx
3524 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3526 tree arglist = TREE_OPERAND (exp, 1);
3528 if (!validate_arglist (arglist,
3529 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3530 return 0;
3531 else
3533 tree result = fold_builtin_strncmp (arglist);
3534 if (result)
3535 return expand_expr (result, target, mode, EXPAND_NORMAL);
3538 /* If c_strlen can determine an expression for one of the string
3539 lengths, and it doesn't have side effects, then emit cmpstrsi
3540 using length MIN(strlen(string)+1, arg3). */
3541 #ifdef HAVE_cmpstrsi
3542 if (HAVE_cmpstrsi)
3544 tree arg1 = TREE_VALUE (arglist);
3545 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3546 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3547 tree len, len1, len2;
3548 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3549 rtx result, insn;
3550 tree fndecl;
3552 int arg1_align
3553 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3554 int arg2_align
3555 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3556 enum machine_mode insn_mode
3557 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3559 len1 = c_strlen (arg1, 1);
3560 len2 = c_strlen (arg2, 1);
3562 if (len1)
3563 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3564 if (len2)
3565 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3567 /* If we don't have a constant length for the first, use the length
3568 of the second, if we know it. We don't require a constant for
3569 this case; some cost analysis could be done if both are available
3570 but neither is constant. For now, assume they're equally cheap,
3571 unless one has side effects. If both strings have constant lengths,
3572 use the smaller. */
3574 if (!len1)
3575 len = len2;
3576 else if (!len2)
3577 len = len1;
3578 else if (TREE_SIDE_EFFECTS (len1))
3579 len = len2;
3580 else if (TREE_SIDE_EFFECTS (len2))
3581 len = len1;
3582 else if (TREE_CODE (len1) != INTEGER_CST)
3583 len = len2;
3584 else if (TREE_CODE (len2) != INTEGER_CST)
3585 len = len1;
3586 else if (tree_int_cst_lt (len1, len2))
3587 len = len1;
3588 else
3589 len = len2;
3591 /* If both arguments have side effects, we cannot optimize. */
3592 if (!len || TREE_SIDE_EFFECTS (len))
3593 return 0;
3595 /* The actual new length parameter is MIN(len,arg3). */
3596 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3597 fold_convert (TREE_TYPE (len), arg3)));
3599 /* If we don't have POINTER_TYPE, call the function. */
3600 if (arg1_align == 0 || arg2_align == 0)
3601 return 0;
3603 /* Make a place to write the result of the instruction. */
3604 result = target;
3605 if (! (result != 0
3606 && REG_P (result) && GET_MODE (result) == insn_mode
3607 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3608 result = gen_reg_rtx (insn_mode);
3610 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3611 arg1 = builtin_save_expr (arg1);
3612 arg2 = builtin_save_expr (arg2);
3613 len = builtin_save_expr (len);
3615 arg1_rtx = get_memory_rtx (arg1);
3616 arg2_rtx = get_memory_rtx (arg2);
3617 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3618 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3619 GEN_INT (MIN (arg1_align, arg2_align)));
3620 if (insn)
3622 emit_insn (insn);
3624 /* Return the value in the proper mode for this function. */
3625 mode = TYPE_MODE (TREE_TYPE (exp));
3626 if (GET_MODE (result) == mode)
3627 return result;
3628 if (target == 0)
3629 return convert_to_mode (mode, result, 0);
3630 convert_move (target, result, 0);
3631 return target;
3634 /* Expand the library call ourselves using a stabilized argument
3635 list to avoid re-evaluating the function's arguments twice. */
3636 arglist = build_tree_list (NULL_TREE, len);
3637 arglist = tree_cons (NULL_TREE, arg2, arglist);
3638 arglist = tree_cons (NULL_TREE, arg1, arglist);
3639 fndecl = get_callee_fndecl (exp);
3640 exp = build_function_call_expr (fndecl, arglist);
3641 return expand_call (exp, target, target == const0_rtx);
3643 #endif
3644 return 0;
3647 /* Expand expression EXP, which is a call to the strcat builtin.
3648 Return 0 if we failed the caller should emit a normal call,
3649 otherwise try to get the result in TARGET, if convenient. */
3651 static rtx
3652 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3654 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3655 return 0;
3656 else
3658 tree dst = TREE_VALUE (arglist),
3659 src = TREE_VALUE (TREE_CHAIN (arglist));
3660 const char *p = c_getstr (src);
3662 if (p)
3664 /* If the string length is zero, return the dst parameter. */
3665 if (*p == '\0')
3666 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3667 else if (!optimize_size)
3669 /* Otherwise if !optimize_size, see if we can store by
3670 pieces into (dst + strlen(dst)). */
3671 tree newdst, arglist,
3672 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3674 /* This is the length argument. */
3675 arglist = build_tree_list (NULL_TREE,
3676 fold (size_binop (PLUS_EXPR,
3677 c_strlen (src, 0),
3678 ssize_int (1))));
3679 /* Prepend src argument. */
3680 arglist = tree_cons (NULL_TREE, src, arglist);
3682 /* We're going to use dst more than once. */
3683 dst = builtin_save_expr (dst);
3685 /* Create strlen (dst). */
3686 newdst =
3687 fold (build_function_call_expr (strlen_fn,
3688 build_tree_list (NULL_TREE,
3689 dst)));
3690 /* Create (dst + strlen (dst)). */
3691 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3693 /* Prepend the new dst argument. */
3694 arglist = tree_cons (NULL_TREE, newdst, arglist);
3696 /* We don't want to get turned into a memcpy if the
3697 target is const0_rtx, i.e. when the return value
3698 isn't used. That would produce pessimized code so
3699 pass in a target of zero, it should never actually be
3700 used. If this was successful return the original
3701 dst, not the result of mempcpy. */
3702 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3703 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3704 else
3705 return 0;
3709 return 0;
3713 /* Expand expression EXP, which is a call to the strncat builtin.
3714 Return 0 if we failed the caller should emit a normal call,
3715 otherwise try to get the result in TARGET, if convenient. */
3717 static rtx
3718 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3720 if (validate_arglist (arglist,
3721 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3723 tree result = fold_builtin_strncat (arglist);
3724 if (result)
3725 return expand_expr (result, target, mode, EXPAND_NORMAL);
3727 return 0;
3730 /* Expand expression EXP, which is a call to the strspn builtin.
3731 Return 0 if we failed the caller should emit a normal call,
3732 otherwise try to get the result in TARGET, if convenient. */
3734 static rtx
3735 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3737 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3739 tree result = fold_builtin_strspn (arglist);
3740 if (result)
3741 return expand_expr (result, target, mode, EXPAND_NORMAL);
3743 return 0;
3746 /* Expand expression EXP, which is a call to the strcspn builtin.
3747 Return 0 if we failed the caller should emit a normal call,
3748 otherwise try to get the result in TARGET, if convenient. */
3750 static rtx
3751 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3753 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3755 tree result = fold_builtin_strcspn (arglist);
3756 if (result)
3757 return expand_expr (result, target, mode, EXPAND_NORMAL);
3759 return 0;
3762 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3763 if that's convenient. */
3766 expand_builtin_saveregs (void)
3768 rtx val, seq;
3770 /* Don't do __builtin_saveregs more than once in a function.
3771 Save the result of the first call and reuse it. */
3772 if (saveregs_value != 0)
3773 return saveregs_value;
3775 /* When this function is called, it means that registers must be
3776 saved on entry to this function. So we migrate the call to the
3777 first insn of this function. */
3779 start_sequence ();
3781 /* Do whatever the machine needs done in this case. */
3782 val = targetm.calls.expand_builtin_saveregs ();
3784 seq = get_insns ();
3785 end_sequence ();
3787 saveregs_value = val;
3789 /* Put the insns after the NOTE that starts the function. If this
3790 is inside a start_sequence, make the outer-level insn chain current, so
3791 the code is placed at the start of the function. */
3792 push_topmost_sequence ();
3793 emit_insn_after (seq, entry_of_function ());
3794 pop_topmost_sequence ();
3796 return val;
3799 /* __builtin_args_info (N) returns word N of the arg space info
3800 for the current function. The number and meanings of words
3801 is controlled by the definition of CUMULATIVE_ARGS. */
3803 static rtx
3804 expand_builtin_args_info (tree arglist)
3806 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3807 int *word_ptr = (int *) &current_function_args_info;
3809 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3811 if (arglist != 0)
3813 if (!host_integerp (TREE_VALUE (arglist), 0))
3814 error ("argument of %<__builtin_args_info%> must be constant");
3815 else
3817 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3819 if (wordnum < 0 || wordnum >= nwords)
3820 error ("argument of %<__builtin_args_info%> out of range");
3821 else
3822 return GEN_INT (word_ptr[wordnum]);
3825 else
3826 error ("missing argument in %<__builtin_args_info%>");
3828 return const0_rtx;
3831 /* Expand a call to __builtin_next_arg. */
3833 static rtx
3834 expand_builtin_next_arg (void)
3836 /* Checking arguments is already done in fold_builtin_next_arg
3837 that must be called before this function. */
3838 return expand_binop (Pmode, add_optab,
3839 current_function_internal_arg_pointer,
3840 current_function_arg_offset_rtx,
3841 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3844 /* Make it easier for the backends by protecting the valist argument
3845 from multiple evaluations. */
3847 static tree
3848 stabilize_va_list (tree valist, int needs_lvalue)
3850 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3852 if (TREE_SIDE_EFFECTS (valist))
3853 valist = save_expr (valist);
3855 /* For this case, the backends will be expecting a pointer to
3856 TREE_TYPE (va_list_type_node), but it's possible we've
3857 actually been given an array (an actual va_list_type_node).
3858 So fix it. */
3859 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3861 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3862 valist = build_fold_addr_expr_with_type (valist, p1);
3865 else
3867 tree pt;
3869 if (! needs_lvalue)
3871 if (! TREE_SIDE_EFFECTS (valist))
3872 return valist;
3874 pt = build_pointer_type (va_list_type_node);
3875 valist = fold (build1 (ADDR_EXPR, pt, valist));
3876 TREE_SIDE_EFFECTS (valist) = 1;
3879 if (TREE_SIDE_EFFECTS (valist))
3880 valist = save_expr (valist);
3881 valist = build_fold_indirect_ref (valist);
3884 return valist;
3887 /* The "standard" definition of va_list is void*. */
3889 tree
3890 std_build_builtin_va_list (void)
3892 return ptr_type_node;
3895 /* The "standard" implementation of va_start: just assign `nextarg' to
3896 the variable. */
3898 void
3899 std_expand_builtin_va_start (tree valist, rtx nextarg)
3901 tree t;
3903 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
3904 make_tree (ptr_type_node, nextarg));
3905 TREE_SIDE_EFFECTS (t) = 1;
3907 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3910 /* Expand ARGLIST, from a call to __builtin_va_start. */
3912 static rtx
3913 expand_builtin_va_start (tree arglist)
3915 rtx nextarg;
3916 tree chain, valist;
3918 chain = TREE_CHAIN (arglist);
3920 if (!chain)
3922 error ("too few arguments to function %<va_start%>");
3923 return const0_rtx;
3926 if (fold_builtin_next_arg (chain))
3927 return const0_rtx;
3929 nextarg = expand_builtin_next_arg ();
3930 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3932 #ifdef EXPAND_BUILTIN_VA_START
3933 EXPAND_BUILTIN_VA_START (valist, nextarg);
3934 #else
3935 std_expand_builtin_va_start (valist, nextarg);
3936 #endif
3938 return const0_rtx;
3941 /* The "standard" implementation of va_arg: read the value from the
3942 current (padded) address and increment by the (padded) size. */
3944 tree
3945 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
3947 tree addr, t, type_size, rounded_size, valist_tmp;
3948 unsigned HOST_WIDE_INT align, boundary;
3949 bool indirect;
3951 #ifdef ARGS_GROW_DOWNWARD
3952 /* All of the alignment and movement below is for args-grow-up machines.
3953 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3954 implement their own specialized gimplify_va_arg_expr routines. */
3955 gcc_unreachable ();
3956 #endif
3958 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3959 if (indirect)
3960 type = build_pointer_type (type);
3962 align = PARM_BOUNDARY / BITS_PER_UNIT;
3963 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
3965 /* Hoist the valist value into a temporary for the moment. */
3966 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
3968 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
3969 requires greater alignment, we must perform dynamic alignment. */
3970 if (boundary > align)
3972 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
3973 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3974 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
3975 gimplify_and_add (t, pre_p);
3977 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
3978 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3979 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
3980 gimplify_and_add (t, pre_p);
3982 else
3983 boundary = align;
3985 /* If the actual alignment is less than the alignment of the type,
3986 adjust the type accordingly so that we don't assume strict alignment
3987 when deferencing the pointer. */
3988 boundary *= BITS_PER_UNIT;
3989 if (boundary < TYPE_ALIGN (type))
3991 type = build_variant_type_copy (type);
3992 TYPE_ALIGN (type) = boundary;
3995 /* Compute the rounded size of the type. */
3996 type_size = size_in_bytes (type);
3997 rounded_size = round_up (type_size, align);
3999 /* Reduce rounded_size so it's sharable with the postqueue. */
4000 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4002 /* Get AP. */
4003 addr = valist_tmp;
4004 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4006 /* Small args are padded downward. */
4007 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4008 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4009 size_binop (MINUS_EXPR, rounded_size, type_size)));
4010 t = fold_convert (TREE_TYPE (addr), t);
4011 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4014 /* Compute new value for AP. */
4015 t = fold_convert (TREE_TYPE (valist), rounded_size);
4016 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4017 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4018 gimplify_and_add (t, pre_p);
4020 addr = fold_convert (build_pointer_type (type), addr);
4022 if (indirect)
4023 addr = build_va_arg_indirect_ref (addr);
4025 return build_va_arg_indirect_ref (addr);
4028 /* Build an indirect-ref expression over the given TREE, which represents a
4029 piece of a va_arg() expansion. */
4030 tree
4031 build_va_arg_indirect_ref (tree addr)
4033 addr = build_fold_indirect_ref (addr);
4035 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4036 mf_mark (addr);
4038 return addr;
4041 /* Return a dummy expression of type TYPE in order to keep going after an
4042 error. */
4044 static tree
4045 dummy_object (tree type)
4047 tree t = convert (build_pointer_type (type), null_pointer_node);
4048 return build1 (INDIRECT_REF, type, t);
4051 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4052 builtin function, but a very special sort of operator. */
4054 enum gimplify_status
4055 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4057 tree promoted_type, want_va_type, have_va_type;
4058 tree valist = TREE_OPERAND (*expr_p, 0);
4059 tree type = TREE_TYPE (*expr_p);
4060 tree t;
4062 /* Verify that valist is of the proper type. */
4063 want_va_type = va_list_type_node;
4064 have_va_type = TREE_TYPE (valist);
4066 if (have_va_type == error_mark_node)
4067 return GS_ERROR;
4069 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4071 /* If va_list is an array type, the argument may have decayed
4072 to a pointer type, e.g. by being passed to another function.
4073 In that case, unwrap both types so that we can compare the
4074 underlying records. */
4075 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4076 || POINTER_TYPE_P (have_va_type))
4078 want_va_type = TREE_TYPE (want_va_type);
4079 have_va_type = TREE_TYPE (have_va_type);
4083 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4085 error ("first argument to %<va_arg%> not of type %<va_list%>");
4086 return GS_ERROR;
4089 /* Generate a diagnostic for requesting data of a type that cannot
4090 be passed through `...' due to type promotion at the call site. */
4091 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4092 != type)
4094 static bool gave_help;
4096 /* Unfortunately, this is merely undefined, rather than a constraint
4097 violation, so we cannot make this an error. If this call is never
4098 executed, the program is still strictly conforming. */
4099 warning ("%qT is promoted to %qT when passed through %<...%>",
4100 type, promoted_type);
4101 if (! gave_help)
4103 gave_help = true;
4104 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4105 promoted_type, type);
4108 /* We can, however, treat "undefined" any way we please.
4109 Call abort to encourage the user to fix the program. */
4110 inform ("if this code is reached, the program will abort");
4111 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4112 NULL);
4113 append_to_statement_list (t, pre_p);
4115 /* This is dead code, but go ahead and finish so that the
4116 mode of the result comes out right. */
4117 *expr_p = dummy_object (type);
4118 return GS_ALL_DONE;
4120 else
4122 /* Make it easier for the backends by protecting the valist argument
4123 from multiple evaluations. */
4124 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4126 /* For this case, the backends will be expecting a pointer to
4127 TREE_TYPE (va_list_type_node), but it's possible we've
4128 actually been given an array (an actual va_list_type_node).
4129 So fix it. */
4130 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4132 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4133 valist = build_fold_addr_expr_with_type (valist, p1);
4135 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4137 else
4138 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4140 if (!targetm.gimplify_va_arg_expr)
4141 /* Once most targets are converted this should abort. */
4142 return GS_ALL_DONE;
4144 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4145 return GS_OK;
4149 /* Expand ARGLIST, from a call to __builtin_va_end. */
4151 static rtx
4152 expand_builtin_va_end (tree arglist)
4154 tree valist = TREE_VALUE (arglist);
4156 /* Evaluate for side effects, if needed. I hate macros that don't
4157 do that. */
4158 if (TREE_SIDE_EFFECTS (valist))
4159 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4161 return const0_rtx;
4164 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4165 builtin rather than just as an assignment in stdarg.h because of the
4166 nastiness of array-type va_list types. */
4168 static rtx
4169 expand_builtin_va_copy (tree arglist)
4171 tree dst, src, t;
4173 dst = TREE_VALUE (arglist);
4174 src = TREE_VALUE (TREE_CHAIN (arglist));
4176 dst = stabilize_va_list (dst, 1);
4177 src = stabilize_va_list (src, 0);
4179 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4181 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4182 TREE_SIDE_EFFECTS (t) = 1;
4183 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4185 else
4187 rtx dstb, srcb, size;
4189 /* Evaluate to pointers. */
4190 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4191 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4192 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4193 VOIDmode, EXPAND_NORMAL);
4195 dstb = convert_memory_address (Pmode, dstb);
4196 srcb = convert_memory_address (Pmode, srcb);
4198 /* "Dereference" to BLKmode memories. */
4199 dstb = gen_rtx_MEM (BLKmode, dstb);
4200 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4201 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4202 srcb = gen_rtx_MEM (BLKmode, srcb);
4203 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4204 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4206 /* Copy. */
4207 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4210 return const0_rtx;
4213 /* Expand a call to one of the builtin functions __builtin_frame_address or
4214 __builtin_return_address. */
4216 static rtx
4217 expand_builtin_frame_address (tree fndecl, tree arglist)
4219 /* The argument must be a nonnegative integer constant.
4220 It counts the number of frames to scan up the stack.
4221 The value is the return address saved in that frame. */
4222 if (arglist == 0)
4223 /* Warning about missing arg was already issued. */
4224 return const0_rtx;
4225 else if (! host_integerp (TREE_VALUE (arglist), 1))
4227 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4228 error ("invalid argument to %<__builtin_frame_address%>");
4229 else
4230 error ("invalid argument to %<__builtin_return_address%>");
4231 return const0_rtx;
4233 else
4235 rtx tem
4236 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4237 tree_low_cst (TREE_VALUE (arglist), 1));
4239 /* Some ports cannot access arbitrary stack frames. */
4240 if (tem == NULL)
4242 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4243 warning ("unsupported argument to %<__builtin_frame_address%>");
4244 else
4245 warning ("unsupported argument to %<__builtin_return_address%>");
4246 return const0_rtx;
4249 /* For __builtin_frame_address, return what we've got. */
4250 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4251 return tem;
4253 if (!REG_P (tem)
4254 && ! CONSTANT_P (tem))
4255 tem = copy_to_mode_reg (Pmode, tem);
4256 return tem;
4260 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4261 we failed and the caller should emit a normal call, otherwise try to get
4262 the result in TARGET, if convenient. */
4264 static rtx
4265 expand_builtin_alloca (tree arglist, rtx target)
4267 rtx op0;
4268 rtx result;
4270 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4271 should always expand to function calls. These can be intercepted
4272 in libmudflap. */
4273 if (flag_mudflap)
4274 return 0;
4276 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4277 return 0;
4279 /* Compute the argument. */
4280 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4282 /* Allocate the desired space. */
4283 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4284 result = convert_memory_address (ptr_mode, result);
4286 return result;
4289 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4290 Return 0 if a normal call should be emitted rather than expanding the
4291 function in-line. If convenient, the result should be placed in TARGET.
4292 SUBTARGET may be used as the target for computing one of EXP's operands. */
4294 static rtx
4295 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4296 rtx subtarget, optab op_optab)
4298 rtx op0;
4299 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4300 return 0;
4302 /* Compute the argument. */
4303 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4304 /* Compute op, into TARGET if possible.
4305 Set TARGET to wherever the result comes back. */
4306 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4307 op_optab, op0, target, 1);
4308 gcc_assert (target);
4310 return convert_to_mode (target_mode, target, 0);
4313 /* If the string passed to fputs is a constant and is one character
4314 long, we attempt to transform this call into __builtin_fputc(). */
4316 static rtx
4317 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4319 /* Verify the arguments in the original call. */
4320 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4322 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4323 unlocked, NULL_TREE);
4324 if (result)
4325 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4327 return 0;
4330 /* Expand a call to __builtin_expect. We return our argument and emit a
4331 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4332 a non-jump context. */
4334 static rtx
4335 expand_builtin_expect (tree arglist, rtx target)
4337 tree exp, c;
4338 rtx note, rtx_c;
4340 if (arglist == NULL_TREE
4341 || TREE_CHAIN (arglist) == NULL_TREE)
4342 return const0_rtx;
4343 exp = TREE_VALUE (arglist);
4344 c = TREE_VALUE (TREE_CHAIN (arglist));
4346 if (TREE_CODE (c) != INTEGER_CST)
4348 error ("second argument to %<__builtin_expect%> must be a constant");
4349 c = integer_zero_node;
4352 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4354 /* Don't bother with expected value notes for integral constants. */
4355 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4357 /* We do need to force this into a register so that we can be
4358 moderately sure to be able to correctly interpret the branch
4359 condition later. */
4360 target = force_reg (GET_MODE (target), target);
4362 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4364 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4365 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4368 return target;
4371 /* Like expand_builtin_expect, except do this in a jump context. This is
4372 called from do_jump if the conditional is a __builtin_expect. Return either
4373 a list of insns to emit the jump or NULL if we cannot optimize
4374 __builtin_expect. We need to optimize this at jump time so that machines
4375 like the PowerPC don't turn the test into a SCC operation, and then jump
4376 based on the test being 0/1. */
4379 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4381 tree arglist = TREE_OPERAND (exp, 1);
4382 tree arg0 = TREE_VALUE (arglist);
4383 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4384 rtx ret = NULL_RTX;
4386 /* Only handle __builtin_expect (test, 0) and
4387 __builtin_expect (test, 1). */
4388 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4389 && (integer_zerop (arg1) || integer_onep (arg1)))
4391 rtx insn, drop_through_label, temp;
4393 /* Expand the jump insns. */
4394 start_sequence ();
4395 do_jump (arg0, if_false_label, if_true_label);
4396 ret = get_insns ();
4398 drop_through_label = get_last_insn ();
4399 if (drop_through_label && NOTE_P (drop_through_label))
4400 drop_through_label = prev_nonnote_insn (drop_through_label);
4401 if (drop_through_label && !LABEL_P (drop_through_label))
4402 drop_through_label = NULL_RTX;
4403 end_sequence ();
4405 if (! if_true_label)
4406 if_true_label = drop_through_label;
4407 if (! if_false_label)
4408 if_false_label = drop_through_label;
4410 /* Go through and add the expect's to each of the conditional jumps. */
4411 insn = ret;
4412 while (insn != NULL_RTX)
4414 rtx next = NEXT_INSN (insn);
4416 if (JUMP_P (insn) && any_condjump_p (insn))
4418 rtx ifelse = SET_SRC (pc_set (insn));
4419 rtx then_dest = XEXP (ifelse, 1);
4420 rtx else_dest = XEXP (ifelse, 2);
4421 int taken = -1;
4423 /* First check if we recognize any of the labels. */
4424 if (GET_CODE (then_dest) == LABEL_REF
4425 && XEXP (then_dest, 0) == if_true_label)
4426 taken = 1;
4427 else if (GET_CODE (then_dest) == LABEL_REF
4428 && XEXP (then_dest, 0) == if_false_label)
4429 taken = 0;
4430 else if (GET_CODE (else_dest) == LABEL_REF
4431 && XEXP (else_dest, 0) == if_false_label)
4432 taken = 1;
4433 else if (GET_CODE (else_dest) == LABEL_REF
4434 && XEXP (else_dest, 0) == if_true_label)
4435 taken = 0;
4436 /* Otherwise check where we drop through. */
4437 else if (else_dest == pc_rtx)
4439 if (next && NOTE_P (next))
4440 next = next_nonnote_insn (next);
4442 if (next && JUMP_P (next)
4443 && any_uncondjump_p (next))
4444 temp = XEXP (SET_SRC (pc_set (next)), 0);
4445 else
4446 temp = next;
4448 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4449 else that can't possibly match either target label. */
4450 if (temp == if_false_label)
4451 taken = 1;
4452 else if (temp == if_true_label)
4453 taken = 0;
4455 else if (then_dest == pc_rtx)
4457 if (next && NOTE_P (next))
4458 next = next_nonnote_insn (next);
4460 if (next && JUMP_P (next)
4461 && any_uncondjump_p (next))
4462 temp = XEXP (SET_SRC (pc_set (next)), 0);
4463 else
4464 temp = next;
4466 if (temp == if_false_label)
4467 taken = 0;
4468 else if (temp == if_true_label)
4469 taken = 1;
4472 if (taken != -1)
4474 /* If the test is expected to fail, reverse the
4475 probabilities. */
4476 if (integer_zerop (arg1))
4477 taken = 1 - taken;
4478 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4482 insn = next;
4486 return ret;
4489 static void
4490 expand_builtin_trap (void)
4492 #ifdef HAVE_trap
4493 if (HAVE_trap)
4494 emit_insn (gen_trap ());
4495 else
4496 #endif
4497 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4498 emit_barrier ();
4501 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4502 Return 0 if a normal call should be emitted rather than expanding
4503 the function inline. If convenient, the result should be placed
4504 in TARGET. SUBTARGET may be used as the target for computing
4505 the operand. */
4507 static rtx
4508 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4510 enum machine_mode mode;
4511 tree arg;
4512 rtx op0;
4514 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4515 return 0;
4517 arg = TREE_VALUE (arglist);
4518 mode = TYPE_MODE (TREE_TYPE (arg));
4519 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4520 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4523 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4524 Return NULL is a normal call should be emitted rather than expanding the
4525 function inline. If convenient, the result should be placed in TARGET.
4526 SUBTARGET may be used as the target for computing the operand. */
4528 static rtx
4529 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4531 rtx op0, op1;
4532 tree arg;
4534 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4535 return 0;
4537 arg = TREE_VALUE (arglist);
4538 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4540 arg = TREE_VALUE (TREE_CHAIN (arglist));
4541 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4543 return expand_copysign (op0, op1, target);
4546 /* Create a new constant string literal and return a char* pointer to it.
4547 The STRING_CST value is the LEN characters at STR. */
4548 static tree
4549 build_string_literal (int len, const char *str)
4551 tree t, elem, index, type;
4553 t = build_string (len, str);
4554 elem = build_type_variant (char_type_node, 1, 0);
4555 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4556 type = build_array_type (elem, index);
4557 TREE_TYPE (t) = type;
4558 TREE_CONSTANT (t) = 1;
4559 TREE_INVARIANT (t) = 1;
4560 TREE_READONLY (t) = 1;
4561 TREE_STATIC (t) = 1;
4563 type = build_pointer_type (type);
4564 t = build1 (ADDR_EXPR, type, t);
4566 type = build_pointer_type (elem);
4567 t = build1 (NOP_EXPR, type, t);
4568 return t;
4571 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4572 Return 0 if a normal call should be emitted rather than transforming
4573 the function inline. If convenient, the result should be placed in
4574 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4575 call. */
4576 static rtx
4577 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4578 bool unlocked)
4580 tree fn_putchar = unlocked
4581 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4582 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4583 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4584 : implicit_built_in_decls[BUILT_IN_PUTS];
4585 const char *fmt_str;
4586 tree fn, fmt, arg;
4588 /* If the return value is used, don't do the transformation. */
4589 if (target != const0_rtx)
4590 return 0;
4592 /* Verify the required arguments in the original call. */
4593 if (! arglist)
4594 return 0;
4595 fmt = TREE_VALUE (arglist);
4596 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4597 return 0;
4598 arglist = TREE_CHAIN (arglist);
4600 /* Check whether the format is a literal string constant. */
4601 fmt_str = c_getstr (fmt);
4602 if (fmt_str == NULL)
4603 return 0;
4605 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4606 if (strcmp (fmt_str, "%s\n") == 0)
4608 if (! arglist
4609 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4610 || TREE_CHAIN (arglist))
4611 return 0;
4612 fn = fn_puts;
4614 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4615 else if (strcmp (fmt_str, "%c") == 0)
4617 if (! arglist
4618 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4619 || TREE_CHAIN (arglist))
4620 return 0;
4621 fn = fn_putchar;
4623 else
4625 /* We can't handle anything else with % args or %% ... yet. */
4626 if (strchr (fmt_str, '%'))
4627 return 0;
4629 if (arglist)
4630 return 0;
4632 /* If the format specifier was "", printf does nothing. */
4633 if (fmt_str[0] == '\0')
4634 return const0_rtx;
4635 /* If the format specifier has length of 1, call putchar. */
4636 if (fmt_str[1] == '\0')
4638 /* Given printf("c"), (where c is any one character,)
4639 convert "c"[0] to an int and pass that to the replacement
4640 function. */
4641 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4642 arglist = build_tree_list (NULL_TREE, arg);
4643 fn = fn_putchar;
4645 else
4647 /* If the format specifier was "string\n", call puts("string"). */
4648 size_t len = strlen (fmt_str);
4649 if (fmt_str[len - 1] == '\n')
4651 /* Create a NUL-terminated string that's one char shorter
4652 than the original, stripping off the trailing '\n'. */
4653 char *newstr = alloca (len);
4654 memcpy (newstr, fmt_str, len - 1);
4655 newstr[len - 1] = 0;
4657 arg = build_string_literal (len, newstr);
4658 arglist = build_tree_list (NULL_TREE, arg);
4659 fn = fn_puts;
4661 else
4662 /* We'd like to arrange to call fputs(string,stdout) here,
4663 but we need stdout and don't have a way to get it yet. */
4664 return 0;
4668 if (!fn)
4669 return 0;
4670 return expand_expr (build_function_call_expr (fn, arglist),
4671 target, mode, EXPAND_NORMAL);
4674 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4675 Return 0 if a normal call should be emitted rather than transforming
4676 the function inline. If convenient, the result should be placed in
4677 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4678 call. */
4679 static rtx
4680 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4681 bool unlocked)
4683 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4684 : implicit_built_in_decls[BUILT_IN_FPUTC];
4685 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4686 : implicit_built_in_decls[BUILT_IN_FPUTS];
4687 const char *fmt_str;
4688 tree fn, fmt, fp, arg;
4690 /* If the return value is used, don't do the transformation. */
4691 if (target != const0_rtx)
4692 return 0;
4694 /* Verify the required arguments in the original call. */
4695 if (! arglist)
4696 return 0;
4697 fp = TREE_VALUE (arglist);
4698 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4699 return 0;
4700 arglist = TREE_CHAIN (arglist);
4701 if (! arglist)
4702 return 0;
4703 fmt = TREE_VALUE (arglist);
4704 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4705 return 0;
4706 arglist = TREE_CHAIN (arglist);
4708 /* Check whether the format is a literal string constant. */
4709 fmt_str = c_getstr (fmt);
4710 if (fmt_str == NULL)
4711 return 0;
4713 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4714 if (strcmp (fmt_str, "%s") == 0)
4716 if (! arglist
4717 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4718 || TREE_CHAIN (arglist))
4719 return 0;
4720 arg = TREE_VALUE (arglist);
4721 arglist = build_tree_list (NULL_TREE, fp);
4722 arglist = tree_cons (NULL_TREE, arg, arglist);
4723 fn = fn_fputs;
4725 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4726 else if (strcmp (fmt_str, "%c") == 0)
4728 if (! arglist
4729 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4730 || TREE_CHAIN (arglist))
4731 return 0;
4732 arg = TREE_VALUE (arglist);
4733 arglist = build_tree_list (NULL_TREE, fp);
4734 arglist = tree_cons (NULL_TREE, arg, arglist);
4735 fn = fn_fputc;
4737 else
4739 /* We can't handle anything else with % args or %% ... yet. */
4740 if (strchr (fmt_str, '%'))
4741 return 0;
4743 if (arglist)
4744 return 0;
4746 /* If the format specifier was "", fprintf does nothing. */
4747 if (fmt_str[0] == '\0')
4749 /* Evaluate and ignore FILE* argument for side-effects. */
4750 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4751 return const0_rtx;
4754 /* When "string" doesn't contain %, replace all cases of
4755 fprintf(stream,string) with fputs(string,stream). The fputs
4756 builtin will take care of special cases like length == 1. */
4757 arglist = build_tree_list (NULL_TREE, fp);
4758 arglist = tree_cons (NULL_TREE, fmt, arglist);
4759 fn = fn_fputs;
4762 if (!fn)
4763 return 0;
4764 return expand_expr (build_function_call_expr (fn, arglist),
4765 target, mode, EXPAND_NORMAL);
4768 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4769 a normal call should be emitted rather than expanding the function
4770 inline. If convenient, the result should be placed in TARGET with
4771 mode MODE. */
4773 static rtx
4774 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4776 tree orig_arglist, dest, fmt;
4777 const char *fmt_str;
4779 orig_arglist = arglist;
4781 /* Verify the required arguments in the original call. */
4782 if (! arglist)
4783 return 0;
4784 dest = TREE_VALUE (arglist);
4785 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4786 return 0;
4787 arglist = TREE_CHAIN (arglist);
4788 if (! arglist)
4789 return 0;
4790 fmt = TREE_VALUE (arglist);
4791 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4792 return 0;
4793 arglist = TREE_CHAIN (arglist);
4795 /* Check whether the format is a literal string constant. */
4796 fmt_str = c_getstr (fmt);
4797 if (fmt_str == NULL)
4798 return 0;
4800 /* If the format doesn't contain % args or %%, use strcpy. */
4801 if (strchr (fmt_str, '%') == 0)
4803 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4804 tree exp;
4806 if (arglist || ! fn)
4807 return 0;
4808 expand_expr (build_function_call_expr (fn, orig_arglist),
4809 const0_rtx, VOIDmode, EXPAND_NORMAL);
4810 if (target == const0_rtx)
4811 return const0_rtx;
4812 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4813 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4815 /* If the format is "%s", use strcpy if the result isn't used. */
4816 else if (strcmp (fmt_str, "%s") == 0)
4818 tree fn, arg, len;
4819 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4821 if (! fn)
4822 return 0;
4824 if (! arglist || TREE_CHAIN (arglist))
4825 return 0;
4826 arg = TREE_VALUE (arglist);
4827 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4828 return 0;
4830 if (target != const0_rtx)
4832 len = c_strlen (arg, 1);
4833 if (! len || TREE_CODE (len) != INTEGER_CST)
4834 return 0;
4836 else
4837 len = NULL_TREE;
4839 arglist = build_tree_list (NULL_TREE, arg);
4840 arglist = tree_cons (NULL_TREE, dest, arglist);
4841 expand_expr (build_function_call_expr (fn, arglist),
4842 const0_rtx, VOIDmode, EXPAND_NORMAL);
4844 if (target == const0_rtx)
4845 return const0_rtx;
4846 return expand_expr (len, target, mode, EXPAND_NORMAL);
4849 return 0;
4852 /* Expand a call to either the entry or exit function profiler. */
4854 static rtx
4855 expand_builtin_profile_func (bool exitp)
4857 rtx this, which;
4859 this = DECL_RTL (current_function_decl);
4860 gcc_assert (MEM_P (this));
4861 this = XEXP (this, 0);
4863 if (exitp)
4864 which = profile_function_exit_libfunc;
4865 else
4866 which = profile_function_entry_libfunc;
4868 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4869 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4871 Pmode);
4873 return const0_rtx;
4876 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4878 static rtx
4879 round_trampoline_addr (rtx tramp)
4881 rtx temp, addend, mask;
4883 /* If we don't need too much alignment, we'll have been guaranteed
4884 proper alignment by get_trampoline_type. */
4885 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4886 return tramp;
4888 /* Round address up to desired boundary. */
4889 temp = gen_reg_rtx (Pmode);
4890 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4891 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4893 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4894 temp, 0, OPTAB_LIB_WIDEN);
4895 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4896 temp, 0, OPTAB_LIB_WIDEN);
4898 return tramp;
4901 static rtx
4902 expand_builtin_init_trampoline (tree arglist)
4904 tree t_tramp, t_func, t_chain;
4905 rtx r_tramp, r_func, r_chain;
4906 #ifdef TRAMPOLINE_TEMPLATE
4907 rtx blktramp;
4908 #endif
4910 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
4911 POINTER_TYPE, VOID_TYPE))
4912 return NULL_RTX;
4914 t_tramp = TREE_VALUE (arglist);
4915 arglist = TREE_CHAIN (arglist);
4916 t_func = TREE_VALUE (arglist);
4917 arglist = TREE_CHAIN (arglist);
4918 t_chain = TREE_VALUE (arglist);
4920 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
4921 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
4922 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
4924 /* Generate insns to initialize the trampoline. */
4925 r_tramp = round_trampoline_addr (r_tramp);
4926 #ifdef TRAMPOLINE_TEMPLATE
4927 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
4928 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
4929 emit_block_move (blktramp, assemble_trampoline_template (),
4930 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4931 #endif
4932 trampolines_created = 1;
4933 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
4935 return const0_rtx;
4938 static rtx
4939 expand_builtin_adjust_trampoline (tree arglist)
4941 rtx tramp;
4943 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4944 return NULL_RTX;
4946 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4947 tramp = round_trampoline_addr (tramp);
4948 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4949 TRAMPOLINE_ADJUST_ADDRESS (tramp);
4950 #endif
4952 return tramp;
4955 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4956 Return NULL_RTX if a normal call should be emitted rather than expanding
4957 the function in-line. EXP is the expression that is a call to the builtin
4958 function; if convenient, the result should be placed in TARGET. */
4960 static rtx
4961 expand_builtin_signbit (tree exp, rtx target)
4963 const struct real_format *fmt;
4964 enum machine_mode fmode, imode, rmode;
4965 HOST_WIDE_INT hi, lo;
4966 tree arg, arglist;
4967 int word, bitpos;
4968 rtx temp;
4970 arglist = TREE_OPERAND (exp, 1);
4971 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4972 return 0;
4974 arg = TREE_VALUE (arglist);
4975 fmode = TYPE_MODE (TREE_TYPE (arg));
4976 rmode = TYPE_MODE (TREE_TYPE (exp));
4977 fmt = REAL_MODE_FORMAT (fmode);
4979 /* For floating point formats without a sign bit, implement signbit
4980 as "ARG < 0.0". */
4981 bitpos = fmt->signbit_ro;
4982 if (bitpos < 0)
4984 /* But we can't do this if the format supports signed zero. */
4985 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4986 return 0;
4988 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
4989 build_real (TREE_TYPE (arg), dconst0)));
4990 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4993 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4994 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4996 imode = int_mode_for_mode (fmode);
4997 if (imode == BLKmode)
4998 return 0;
4999 temp = gen_lowpart (imode, temp);
5001 else
5003 imode = word_mode;
5004 /* Handle targets with different FP word orders. */
5005 if (FLOAT_WORDS_BIG_ENDIAN)
5006 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5007 else
5008 word = bitpos / BITS_PER_WORD;
5009 temp = operand_subword_force (temp, word, fmode);
5010 bitpos = bitpos % BITS_PER_WORD;
5013 /* Force the intermediate word_mode (or narrower) result into a
5014 register. This avoids attempting to create paradoxical SUBREGs
5015 of floating point modes below. */
5016 temp = force_reg (imode, temp);
5018 /* If the bitpos is within the "result mode" lowpart, the operation
5019 can be implement with a single bitwise AND. Otherwise, we need
5020 a right shift and an AND. */
5022 if (bitpos < GET_MODE_BITSIZE (rmode))
5024 if (bitpos < HOST_BITS_PER_WIDE_INT)
5026 hi = 0;
5027 lo = (HOST_WIDE_INT) 1 << bitpos;
5029 else
5031 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5032 lo = 0;
5035 if (imode != rmode)
5036 temp = gen_lowpart (rmode, temp);
5037 temp = expand_binop (rmode, and_optab, temp,
5038 immed_double_const (lo, hi, rmode),
5039 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5041 else
5043 /* Perform a logical right shift to place the signbit in the least
5044 significant bit, then truncate the result to the desired mode
5045 and mask just this bit. */
5046 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5047 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5048 temp = gen_lowpart (rmode, temp);
5049 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5050 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5053 return temp;
5056 /* Expand fork or exec calls. TARGET is the desired target of the
5057 call. ARGLIST is the list of arguments of the call. FN is the
5058 identificator of the actual function. IGNORE is nonzero if the
5059 value is to be ignored. */
5061 static rtx
5062 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5064 tree id, decl;
5065 tree call;
5067 /* If we are not profiling, just call the function. */
5068 if (!profile_arc_flag)
5069 return NULL_RTX;
5071 /* Otherwise call the wrapper. This should be equivalent for the rest of
5072 compiler, so the code does not diverge, and the wrapper may run the
5073 code necessary for keeping the profiling sane. */
5075 switch (DECL_FUNCTION_CODE (fn))
5077 case BUILT_IN_FORK:
5078 id = get_identifier ("__gcov_fork");
5079 break;
5081 case BUILT_IN_EXECL:
5082 id = get_identifier ("__gcov_execl");
5083 break;
5085 case BUILT_IN_EXECV:
5086 id = get_identifier ("__gcov_execv");
5087 break;
5089 case BUILT_IN_EXECLP:
5090 id = get_identifier ("__gcov_execlp");
5091 break;
5093 case BUILT_IN_EXECLE:
5094 id = get_identifier ("__gcov_execle");
5095 break;
5097 case BUILT_IN_EXECVP:
5098 id = get_identifier ("__gcov_execvp");
5099 break;
5101 case BUILT_IN_EXECVE:
5102 id = get_identifier ("__gcov_execve");
5103 break;
5105 default:
5106 gcc_unreachable ();
5109 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5110 DECL_EXTERNAL (decl) = 1;
5111 TREE_PUBLIC (decl) = 1;
5112 DECL_ARTIFICIAL (decl) = 1;
5113 TREE_NOTHROW (decl) = 1;
5114 call = build_function_call_expr (decl, arglist);
5116 return expand_call (call, target, ignore);
5119 /* Expand an expression EXP that calls a built-in function,
5120 with result going to TARGET if that's convenient
5121 (and in mode MODE if that's convenient).
5122 SUBTARGET may be used as the target for computing one of EXP's operands.
5123 IGNORE is nonzero if the value is to be ignored. */
5126 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5127 int ignore)
5129 tree fndecl = get_callee_fndecl (exp);
5130 tree arglist = TREE_OPERAND (exp, 1);
5131 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5132 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5134 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5135 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5137 /* When not optimizing, generate calls to library functions for a certain
5138 set of builtins. */
5139 if (!optimize
5140 && !CALLED_AS_BUILT_IN (fndecl)
5141 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5142 && fcode != BUILT_IN_ALLOCA)
5143 return expand_call (exp, target, ignore);
5145 /* The built-in function expanders test for target == const0_rtx
5146 to determine whether the function's result will be ignored. */
5147 if (ignore)
5148 target = const0_rtx;
5150 /* If the result of a pure or const built-in function is ignored, and
5151 none of its arguments are volatile, we can avoid expanding the
5152 built-in call and just evaluate the arguments for side-effects. */
5153 if (target == const0_rtx
5154 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5156 bool volatilep = false;
5157 tree arg;
5159 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5160 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5162 volatilep = true;
5163 break;
5166 if (! volatilep)
5168 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5169 expand_expr (TREE_VALUE (arg), const0_rtx,
5170 VOIDmode, EXPAND_NORMAL);
5171 return const0_rtx;
5175 switch (fcode)
5177 case BUILT_IN_FABS:
5178 case BUILT_IN_FABSF:
5179 case BUILT_IN_FABSL:
5180 target = expand_builtin_fabs (arglist, target, subtarget);
5181 if (target)
5182 return target;
5183 break;
5185 case BUILT_IN_COPYSIGN:
5186 case BUILT_IN_COPYSIGNF:
5187 case BUILT_IN_COPYSIGNL:
5188 target = expand_builtin_copysign (arglist, target, subtarget);
5189 if (target)
5190 return target;
5191 break;
5193 /* Just do a normal library call if we were unable to fold
5194 the values. */
5195 case BUILT_IN_CABS:
5196 case BUILT_IN_CABSF:
5197 case BUILT_IN_CABSL:
5198 break;
5200 case BUILT_IN_EXP:
5201 case BUILT_IN_EXPF:
5202 case BUILT_IN_EXPL:
5203 case BUILT_IN_EXP10:
5204 case BUILT_IN_EXP10F:
5205 case BUILT_IN_EXP10L:
5206 case BUILT_IN_POW10:
5207 case BUILT_IN_POW10F:
5208 case BUILT_IN_POW10L:
5209 case BUILT_IN_EXP2:
5210 case BUILT_IN_EXP2F:
5211 case BUILT_IN_EXP2L:
5212 case BUILT_IN_EXPM1:
5213 case BUILT_IN_EXPM1F:
5214 case BUILT_IN_EXPM1L:
5215 case BUILT_IN_LOGB:
5216 case BUILT_IN_LOGBF:
5217 case BUILT_IN_LOGBL:
5218 case BUILT_IN_ILOGB:
5219 case BUILT_IN_ILOGBF:
5220 case BUILT_IN_ILOGBL:
5221 case BUILT_IN_LOG:
5222 case BUILT_IN_LOGF:
5223 case BUILT_IN_LOGL:
5224 case BUILT_IN_LOG10:
5225 case BUILT_IN_LOG10F:
5226 case BUILT_IN_LOG10L:
5227 case BUILT_IN_LOG2:
5228 case BUILT_IN_LOG2F:
5229 case BUILT_IN_LOG2L:
5230 case BUILT_IN_LOG1P:
5231 case BUILT_IN_LOG1PF:
5232 case BUILT_IN_LOG1PL:
5233 case BUILT_IN_TAN:
5234 case BUILT_IN_TANF:
5235 case BUILT_IN_TANL:
5236 case BUILT_IN_ASIN:
5237 case BUILT_IN_ASINF:
5238 case BUILT_IN_ASINL:
5239 case BUILT_IN_ACOS:
5240 case BUILT_IN_ACOSF:
5241 case BUILT_IN_ACOSL:
5242 case BUILT_IN_ATAN:
5243 case BUILT_IN_ATANF:
5244 case BUILT_IN_ATANL:
5245 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5246 because of possible accuracy problems. */
5247 if (! flag_unsafe_math_optimizations)
5248 break;
5249 case BUILT_IN_SQRT:
5250 case BUILT_IN_SQRTF:
5251 case BUILT_IN_SQRTL:
5252 case BUILT_IN_FLOOR:
5253 case BUILT_IN_FLOORF:
5254 case BUILT_IN_FLOORL:
5255 case BUILT_IN_CEIL:
5256 case BUILT_IN_CEILF:
5257 case BUILT_IN_CEILL:
5258 case BUILT_IN_TRUNC:
5259 case BUILT_IN_TRUNCF:
5260 case BUILT_IN_TRUNCL:
5261 case BUILT_IN_ROUND:
5262 case BUILT_IN_ROUNDF:
5263 case BUILT_IN_ROUNDL:
5264 case BUILT_IN_NEARBYINT:
5265 case BUILT_IN_NEARBYINTF:
5266 case BUILT_IN_NEARBYINTL:
5267 case BUILT_IN_RINT:
5268 case BUILT_IN_RINTF:
5269 case BUILT_IN_RINTL:
5270 case BUILT_IN_LRINT:
5271 case BUILT_IN_LRINTF:
5272 case BUILT_IN_LRINTL:
5273 case BUILT_IN_LLRINT:
5274 case BUILT_IN_LLRINTF:
5275 case BUILT_IN_LLRINTL:
5276 target = expand_builtin_mathfn (exp, target, subtarget);
5277 if (target)
5278 return target;
5279 break;
5281 case BUILT_IN_POW:
5282 case BUILT_IN_POWF:
5283 case BUILT_IN_POWL:
5284 target = expand_builtin_pow (exp, target, subtarget);
5285 if (target)
5286 return target;
5287 break;
5289 case BUILT_IN_POWI:
5290 case BUILT_IN_POWIF:
5291 case BUILT_IN_POWIL:
5292 target = expand_builtin_powi (exp, target, subtarget);
5293 if (target)
5294 return target;
5295 break;
5297 case BUILT_IN_ATAN2:
5298 case BUILT_IN_ATAN2F:
5299 case BUILT_IN_ATAN2L:
5300 case BUILT_IN_LDEXP:
5301 case BUILT_IN_LDEXPF:
5302 case BUILT_IN_LDEXPL:
5303 case BUILT_IN_FMOD:
5304 case BUILT_IN_FMODF:
5305 case BUILT_IN_FMODL:
5306 case BUILT_IN_DREM:
5307 case BUILT_IN_DREMF:
5308 case BUILT_IN_DREML:
5309 if (! flag_unsafe_math_optimizations)
5310 break;
5311 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5312 if (target)
5313 return target;
5314 break;
5316 case BUILT_IN_SIN:
5317 case BUILT_IN_SINF:
5318 case BUILT_IN_SINL:
5319 case BUILT_IN_COS:
5320 case BUILT_IN_COSF:
5321 case BUILT_IN_COSL:
5322 if (! flag_unsafe_math_optimizations)
5323 break;
5324 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5325 if (target)
5326 return target;
5327 break;
5329 case BUILT_IN_APPLY_ARGS:
5330 return expand_builtin_apply_args ();
5332 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5333 FUNCTION with a copy of the parameters described by
5334 ARGUMENTS, and ARGSIZE. It returns a block of memory
5335 allocated on the stack into which is stored all the registers
5336 that might possibly be used for returning the result of a
5337 function. ARGUMENTS is the value returned by
5338 __builtin_apply_args. ARGSIZE is the number of bytes of
5339 arguments that must be copied. ??? How should this value be
5340 computed? We'll also need a safe worst case value for varargs
5341 functions. */
5342 case BUILT_IN_APPLY:
5343 if (!validate_arglist (arglist, POINTER_TYPE,
5344 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5345 && !validate_arglist (arglist, REFERENCE_TYPE,
5346 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5347 return const0_rtx;
5348 else
5350 int i;
5351 tree t;
5352 rtx ops[3];
5354 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5355 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5357 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5360 /* __builtin_return (RESULT) causes the function to return the
5361 value described by RESULT. RESULT is address of the block of
5362 memory returned by __builtin_apply. */
5363 case BUILT_IN_RETURN:
5364 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5365 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5366 NULL_RTX, VOIDmode, 0));
5367 return const0_rtx;
5369 case BUILT_IN_SAVEREGS:
5370 return expand_builtin_saveregs ();
5372 case BUILT_IN_ARGS_INFO:
5373 return expand_builtin_args_info (arglist);
5375 /* Return the address of the first anonymous stack arg. */
5376 case BUILT_IN_NEXT_ARG:
5377 if (fold_builtin_next_arg (arglist))
5378 return const0_rtx;
5379 return expand_builtin_next_arg ();
5381 case BUILT_IN_CLASSIFY_TYPE:
5382 return expand_builtin_classify_type (arglist);
5384 case BUILT_IN_CONSTANT_P:
5385 return const0_rtx;
5387 case BUILT_IN_FRAME_ADDRESS:
5388 case BUILT_IN_RETURN_ADDRESS:
5389 return expand_builtin_frame_address (fndecl, arglist);
5391 /* Returns the address of the area where the structure is returned.
5392 0 otherwise. */
5393 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5394 if (arglist != 0
5395 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5396 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5397 return const0_rtx;
5398 else
5399 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5401 case BUILT_IN_ALLOCA:
5402 target = expand_builtin_alloca (arglist, target);
5403 if (target)
5404 return target;
5405 break;
5407 case BUILT_IN_STACK_SAVE:
5408 return expand_stack_save ();
5410 case BUILT_IN_STACK_RESTORE:
5411 expand_stack_restore (TREE_VALUE (arglist));
5412 return const0_rtx;
5414 case BUILT_IN_FFS:
5415 case BUILT_IN_FFSL:
5416 case BUILT_IN_FFSLL:
5417 case BUILT_IN_FFSIMAX:
5418 target = expand_builtin_unop (target_mode, arglist, target,
5419 subtarget, ffs_optab);
5420 if (target)
5421 return target;
5422 break;
5424 case BUILT_IN_CLZ:
5425 case BUILT_IN_CLZL:
5426 case BUILT_IN_CLZLL:
5427 case BUILT_IN_CLZIMAX:
5428 target = expand_builtin_unop (target_mode, arglist, target,
5429 subtarget, clz_optab);
5430 if (target)
5431 return target;
5432 break;
5434 case BUILT_IN_CTZ:
5435 case BUILT_IN_CTZL:
5436 case BUILT_IN_CTZLL:
5437 case BUILT_IN_CTZIMAX:
5438 target = expand_builtin_unop (target_mode, arglist, target,
5439 subtarget, ctz_optab);
5440 if (target)
5441 return target;
5442 break;
5444 case BUILT_IN_POPCOUNT:
5445 case BUILT_IN_POPCOUNTL:
5446 case BUILT_IN_POPCOUNTLL:
5447 case BUILT_IN_POPCOUNTIMAX:
5448 target = expand_builtin_unop (target_mode, arglist, target,
5449 subtarget, popcount_optab);
5450 if (target)
5451 return target;
5452 break;
5454 case BUILT_IN_PARITY:
5455 case BUILT_IN_PARITYL:
5456 case BUILT_IN_PARITYLL:
5457 case BUILT_IN_PARITYIMAX:
5458 target = expand_builtin_unop (target_mode, arglist, target,
5459 subtarget, parity_optab);
5460 if (target)
5461 return target;
5462 break;
5464 case BUILT_IN_STRLEN:
5465 target = expand_builtin_strlen (arglist, target, target_mode);
5466 if (target)
5467 return target;
5468 break;
5470 case BUILT_IN_STRCPY:
5471 target = expand_builtin_strcpy (exp, target, mode);
5472 if (target)
5473 return target;
5474 break;
5476 case BUILT_IN_STRNCPY:
5477 target = expand_builtin_strncpy (exp, target, mode);
5478 if (target)
5479 return target;
5480 break;
5482 case BUILT_IN_STPCPY:
5483 target = expand_builtin_stpcpy (exp, target, mode);
5484 if (target)
5485 return target;
5486 break;
5488 case BUILT_IN_STRCAT:
5489 target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5490 if (target)
5491 return target;
5492 break;
5494 case BUILT_IN_STRNCAT:
5495 target = expand_builtin_strncat (arglist, target, mode);
5496 if (target)
5497 return target;
5498 break;
5500 case BUILT_IN_STRSPN:
5501 target = expand_builtin_strspn (arglist, target, mode);
5502 if (target)
5503 return target;
5504 break;
5506 case BUILT_IN_STRCSPN:
5507 target = expand_builtin_strcspn (arglist, target, mode);
5508 if (target)
5509 return target;
5510 break;
5512 case BUILT_IN_STRSTR:
5513 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5514 if (target)
5515 return target;
5516 break;
5518 case BUILT_IN_STRPBRK:
5519 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5520 if (target)
5521 return target;
5522 break;
5524 case BUILT_IN_INDEX:
5525 case BUILT_IN_STRCHR:
5526 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5527 if (target)
5528 return target;
5529 break;
5531 case BUILT_IN_RINDEX:
5532 case BUILT_IN_STRRCHR:
5533 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5534 if (target)
5535 return target;
5536 break;
5538 case BUILT_IN_MEMCPY:
5539 target = expand_builtin_memcpy (exp, target, mode);
5540 if (target)
5541 return target;
5542 break;
5544 case BUILT_IN_MEMPCPY:
5545 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5546 if (target)
5547 return target;
5548 break;
5550 case BUILT_IN_MEMMOVE:
5551 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, mode);
5552 if (target)
5553 return target;
5554 break;
5556 case BUILT_IN_BCOPY:
5557 target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5558 if (target)
5559 return target;
5560 break;
5562 case BUILT_IN_MEMSET:
5563 target = expand_builtin_memset (arglist, target, mode);
5564 if (target)
5565 return target;
5566 break;
5568 case BUILT_IN_BZERO:
5569 target = expand_builtin_bzero (arglist);
5570 if (target)
5571 return target;
5572 break;
5574 case BUILT_IN_STRCMP:
5575 target = expand_builtin_strcmp (exp, target, mode);
5576 if (target)
5577 return target;
5578 break;
5580 case BUILT_IN_STRNCMP:
5581 target = expand_builtin_strncmp (exp, target, mode);
5582 if (target)
5583 return target;
5584 break;
5586 case BUILT_IN_BCMP:
5587 case BUILT_IN_MEMCMP:
5588 target = expand_builtin_memcmp (exp, arglist, target, mode);
5589 if (target)
5590 return target;
5591 break;
5593 case BUILT_IN_SETJMP:
5594 target = expand_builtin_setjmp (arglist, target);
5595 if (target)
5596 return target;
5597 break;
5599 /* __builtin_longjmp is passed a pointer to an array of five words.
5600 It's similar to the C library longjmp function but works with
5601 __builtin_setjmp above. */
5602 case BUILT_IN_LONGJMP:
5603 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5604 break;
5605 else
5607 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5608 VOIDmode, 0);
5609 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5610 NULL_RTX, VOIDmode, 0);
5612 if (value != const1_rtx)
5614 error ("%<__builtin_longjmp%> second argument must be 1");
5615 return const0_rtx;
5618 expand_builtin_longjmp (buf_addr, value);
5619 return const0_rtx;
5622 case BUILT_IN_NONLOCAL_GOTO:
5623 target = expand_builtin_nonlocal_goto (arglist);
5624 if (target)
5625 return target;
5626 break;
5628 /* This updates the setjmp buffer that is its argument with the value
5629 of the current stack pointer. */
5630 case BUILT_IN_UPDATE_SETJMP_BUF:
5631 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5633 rtx buf_addr
5634 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5636 expand_builtin_update_setjmp_buf (buf_addr);
5637 return const0_rtx;
5639 break;
5641 case BUILT_IN_TRAP:
5642 expand_builtin_trap ();
5643 return const0_rtx;
5645 case BUILT_IN_PRINTF:
5646 target = expand_builtin_printf (arglist, target, mode, false);
5647 if (target)
5648 return target;
5649 break;
5651 case BUILT_IN_PRINTF_UNLOCKED:
5652 target = expand_builtin_printf (arglist, target, mode, true);
5653 if (target)
5654 return target;
5655 break;
5657 case BUILT_IN_FPUTS:
5658 target = expand_builtin_fputs (arglist, target, false);
5659 if (target)
5660 return target;
5661 break;
5662 case BUILT_IN_FPUTS_UNLOCKED:
5663 target = expand_builtin_fputs (arglist, target, true);
5664 if (target)
5665 return target;
5666 break;
5668 case BUILT_IN_FPRINTF:
5669 target = expand_builtin_fprintf (arglist, target, mode, false);
5670 if (target)
5671 return target;
5672 break;
5674 case BUILT_IN_FPRINTF_UNLOCKED:
5675 target = expand_builtin_fprintf (arglist, target, mode, true);
5676 if (target)
5677 return target;
5678 break;
5680 case BUILT_IN_SPRINTF:
5681 target = expand_builtin_sprintf (arglist, target, mode);
5682 if (target)
5683 return target;
5684 break;
5686 case BUILT_IN_SIGNBIT:
5687 case BUILT_IN_SIGNBITF:
5688 case BUILT_IN_SIGNBITL:
5689 target = expand_builtin_signbit (exp, target);
5690 if (target)
5691 return target;
5692 break;
5694 /* Various hooks for the DWARF 2 __throw routine. */
5695 case BUILT_IN_UNWIND_INIT:
5696 expand_builtin_unwind_init ();
5697 return const0_rtx;
5698 case BUILT_IN_DWARF_CFA:
5699 return virtual_cfa_rtx;
5700 #ifdef DWARF2_UNWIND_INFO
5701 case BUILT_IN_DWARF_SP_COLUMN:
5702 return expand_builtin_dwarf_sp_column ();
5703 case BUILT_IN_INIT_DWARF_REG_SIZES:
5704 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5705 return const0_rtx;
5706 #endif
5707 case BUILT_IN_FROB_RETURN_ADDR:
5708 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5709 case BUILT_IN_EXTRACT_RETURN_ADDR:
5710 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5711 case BUILT_IN_EH_RETURN:
5712 expand_builtin_eh_return (TREE_VALUE (arglist),
5713 TREE_VALUE (TREE_CHAIN (arglist)));
5714 return const0_rtx;
5715 #ifdef EH_RETURN_DATA_REGNO
5716 case BUILT_IN_EH_RETURN_DATA_REGNO:
5717 return expand_builtin_eh_return_data_regno (arglist);
5718 #endif
5719 case BUILT_IN_EXTEND_POINTER:
5720 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5722 case BUILT_IN_VA_START:
5723 case BUILT_IN_STDARG_START:
5724 return expand_builtin_va_start (arglist);
5725 case BUILT_IN_VA_END:
5726 return expand_builtin_va_end (arglist);
5727 case BUILT_IN_VA_COPY:
5728 return expand_builtin_va_copy (arglist);
5729 case BUILT_IN_EXPECT:
5730 return expand_builtin_expect (arglist, target);
5731 case BUILT_IN_PREFETCH:
5732 expand_builtin_prefetch (arglist);
5733 return const0_rtx;
5735 case BUILT_IN_PROFILE_FUNC_ENTER:
5736 return expand_builtin_profile_func (false);
5737 case BUILT_IN_PROFILE_FUNC_EXIT:
5738 return expand_builtin_profile_func (true);
5740 case BUILT_IN_INIT_TRAMPOLINE:
5741 return expand_builtin_init_trampoline (arglist);
5742 case BUILT_IN_ADJUST_TRAMPOLINE:
5743 return expand_builtin_adjust_trampoline (arglist);
5745 case BUILT_IN_FORK:
5746 case BUILT_IN_EXECL:
5747 case BUILT_IN_EXECV:
5748 case BUILT_IN_EXECLP:
5749 case BUILT_IN_EXECLE:
5750 case BUILT_IN_EXECVP:
5751 case BUILT_IN_EXECVE:
5752 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5753 if (target)
5754 return target;
5755 break;
5757 default: /* just do library call, if unknown builtin */
5758 break;
5761 /* The switch statement above can drop through to cause the function
5762 to be called normally. */
5763 return expand_call (exp, target, ignore);
5766 /* Determine whether a tree node represents a call to a built-in
5767 function. If the tree T is a call to a built-in function with
5768 the right number of arguments of the appropriate types, return
5769 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5770 Otherwise the return value is END_BUILTINS. */
5772 enum built_in_function
5773 builtin_mathfn_code (tree t)
5775 tree fndecl, arglist, parmlist;
5776 tree argtype, parmtype;
5778 if (TREE_CODE (t) != CALL_EXPR
5779 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5780 return END_BUILTINS;
5782 fndecl = get_callee_fndecl (t);
5783 if (fndecl == NULL_TREE
5784 || TREE_CODE (fndecl) != FUNCTION_DECL
5785 || ! DECL_BUILT_IN (fndecl)
5786 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5787 return END_BUILTINS;
5789 arglist = TREE_OPERAND (t, 1);
5790 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5791 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5793 /* If a function doesn't take a variable number of arguments,
5794 the last element in the list will have type `void'. */
5795 parmtype = TREE_VALUE (parmlist);
5796 if (VOID_TYPE_P (parmtype))
5798 if (arglist)
5799 return END_BUILTINS;
5800 return DECL_FUNCTION_CODE (fndecl);
5803 if (! arglist)
5804 return END_BUILTINS;
5806 argtype = TREE_TYPE (TREE_VALUE (arglist));
5808 if (SCALAR_FLOAT_TYPE_P (parmtype))
5810 if (! SCALAR_FLOAT_TYPE_P (argtype))
5811 return END_BUILTINS;
5813 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5815 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5816 return END_BUILTINS;
5818 else if (POINTER_TYPE_P (parmtype))
5820 if (! POINTER_TYPE_P (argtype))
5821 return END_BUILTINS;
5823 else if (INTEGRAL_TYPE_P (parmtype))
5825 if (! INTEGRAL_TYPE_P (argtype))
5826 return END_BUILTINS;
5828 else
5829 return END_BUILTINS;
5831 arglist = TREE_CHAIN (arglist);
5834 /* Variable-length argument list. */
5835 return DECL_FUNCTION_CODE (fndecl);
5838 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5839 constant. ARGLIST is the argument list of the call. */
5841 static tree
5842 fold_builtin_constant_p (tree arglist)
5844 if (arglist == 0)
5845 return 0;
5847 arglist = TREE_VALUE (arglist);
5849 /* We return 1 for a numeric type that's known to be a constant
5850 value at compile-time or for an aggregate type that's a
5851 literal constant. */
5852 STRIP_NOPS (arglist);
5854 /* If we know this is a constant, emit the constant of one. */
5855 if (CONSTANT_CLASS_P (arglist)
5856 || (TREE_CODE (arglist) == CONSTRUCTOR
5857 && TREE_CONSTANT (arglist))
5858 || (TREE_CODE (arglist) == ADDR_EXPR
5859 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5860 return integer_one_node;
5862 /* If this expression has side effects, show we don't know it to be a
5863 constant. Likewise if it's a pointer or aggregate type since in
5864 those case we only want literals, since those are only optimized
5865 when generating RTL, not later.
5866 And finally, if we are compiling an initializer, not code, we
5867 need to return a definite result now; there's not going to be any
5868 more optimization done. */
5869 if (TREE_SIDE_EFFECTS (arglist)
5870 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5871 || POINTER_TYPE_P (TREE_TYPE (arglist))
5872 || cfun == 0)
5873 return integer_zero_node;
5875 return 0;
5878 /* Fold a call to __builtin_expect, if we expect that a comparison against
5879 the argument will fold to a constant. In practice, this means a true
5880 constant or the address of a non-weak symbol. ARGLIST is the argument
5881 list of the call. */
5883 static tree
5884 fold_builtin_expect (tree arglist)
5886 tree arg, inner;
5888 if (arglist == 0)
5889 return 0;
5891 arg = TREE_VALUE (arglist);
5893 /* If the argument isn't invariant, then there's nothing we can do. */
5894 if (!TREE_INVARIANT (arg))
5895 return 0;
5897 /* If we're looking at an address of a weak decl, then do not fold. */
5898 inner = arg;
5899 STRIP_NOPS (inner);
5900 if (TREE_CODE (inner) == ADDR_EXPR)
5904 inner = TREE_OPERAND (inner, 0);
5906 while (TREE_CODE (inner) == COMPONENT_REF
5907 || TREE_CODE (inner) == ARRAY_REF);
5908 if (DECL_P (inner) && DECL_WEAK (inner))
5909 return 0;
5912 /* Otherwise, ARG already has the proper type for the return value. */
5913 return arg;
5916 /* Fold a call to __builtin_classify_type. */
5918 static tree
5919 fold_builtin_classify_type (tree arglist)
5921 if (arglist == 0)
5922 return build_int_cst (NULL_TREE, no_type_class);
5924 return build_int_cst (NULL_TREE,
5925 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
5928 /* Fold a call to __builtin_strlen. */
5930 static tree
5931 fold_builtin_strlen (tree arglist)
5933 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5934 return NULL_TREE;
5935 else
5937 tree len = c_strlen (TREE_VALUE (arglist), 0);
5939 if (len)
5941 /* Convert from the internal "sizetype" type to "size_t". */
5942 if (size_type_node)
5943 len = fold_convert (size_type_node, len);
5944 return len;
5947 return NULL_TREE;
5951 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5953 static tree
5954 fold_builtin_inf (tree type, int warn)
5956 REAL_VALUE_TYPE real;
5958 /* __builtin_inff is intended to be usable to define INFINITY on all
5959 targets. If an infinity is not available, INFINITY expands "to a
5960 positive constant of type float that overflows at translation
5961 time", footnote "In this case, using INFINITY will violate the
5962 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5963 Thus we pedwarn to ensure this constraint violation is
5964 diagnosed. */
5965 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5966 pedwarn ("target format does not support infinity");
5968 real_inf (&real);
5969 return build_real (type, real);
5972 /* Fold a call to __builtin_nan or __builtin_nans. */
5974 static tree
5975 fold_builtin_nan (tree arglist, tree type, int quiet)
5977 REAL_VALUE_TYPE real;
5978 const char *str;
5980 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5981 return 0;
5982 str = c_getstr (TREE_VALUE (arglist));
5983 if (!str)
5984 return 0;
5986 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5987 return 0;
5989 return build_real (type, real);
5992 /* Return true if the floating point expression T has an integer value.
5993 We also allow +Inf, -Inf and NaN to be considered integer values. */
5995 static bool
5996 integer_valued_real_p (tree t)
5998 switch (TREE_CODE (t))
6000 case FLOAT_EXPR:
6001 return true;
6003 case ABS_EXPR:
6004 case SAVE_EXPR:
6005 case NON_LVALUE_EXPR:
6006 return integer_valued_real_p (TREE_OPERAND (t, 0));
6008 case COMPOUND_EXPR:
6009 case MODIFY_EXPR:
6010 case BIND_EXPR:
6011 return integer_valued_real_p (TREE_OPERAND (t, 1));
6013 case PLUS_EXPR:
6014 case MINUS_EXPR:
6015 case MULT_EXPR:
6016 case MIN_EXPR:
6017 case MAX_EXPR:
6018 return integer_valued_real_p (TREE_OPERAND (t, 0))
6019 && integer_valued_real_p (TREE_OPERAND (t, 1));
6021 case COND_EXPR:
6022 return integer_valued_real_p (TREE_OPERAND (t, 1))
6023 && integer_valued_real_p (TREE_OPERAND (t, 2));
6025 case REAL_CST:
6026 if (! TREE_CONSTANT_OVERFLOW (t))
6028 REAL_VALUE_TYPE c, cint;
6030 c = TREE_REAL_CST (t);
6031 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6032 return real_identical (&c, &cint);
6035 case NOP_EXPR:
6037 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6038 if (TREE_CODE (type) == INTEGER_TYPE)
6039 return true;
6040 if (TREE_CODE (type) == REAL_TYPE)
6041 return integer_valued_real_p (TREE_OPERAND (t, 0));
6042 break;
6045 case CALL_EXPR:
6046 switch (builtin_mathfn_code (t))
6048 case BUILT_IN_CEIL:
6049 case BUILT_IN_CEILF:
6050 case BUILT_IN_CEILL:
6051 case BUILT_IN_FLOOR:
6052 case BUILT_IN_FLOORF:
6053 case BUILT_IN_FLOORL:
6054 case BUILT_IN_NEARBYINT:
6055 case BUILT_IN_NEARBYINTF:
6056 case BUILT_IN_NEARBYINTL:
6057 case BUILT_IN_RINT:
6058 case BUILT_IN_RINTF:
6059 case BUILT_IN_RINTL:
6060 case BUILT_IN_ROUND:
6061 case BUILT_IN_ROUNDF:
6062 case BUILT_IN_ROUNDL:
6063 case BUILT_IN_TRUNC:
6064 case BUILT_IN_TRUNCF:
6065 case BUILT_IN_TRUNCL:
6066 return true;
6068 default:
6069 break;
6071 break;
6073 default:
6074 break;
6076 return false;
6079 /* EXP is assumed to be builtin call where truncation can be propagated
6080 across (for instance floor((double)f) == (double)floorf (f).
6081 Do the transformation. */
6083 static tree
6084 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6086 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6087 tree arg;
6089 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6090 return 0;
6092 arg = TREE_VALUE (arglist);
6093 /* Integer rounding functions are idempotent. */
6094 if (fcode == builtin_mathfn_code (arg))
6095 return arg;
6097 /* If argument is already integer valued, and we don't need to worry
6098 about setting errno, there's no need to perform rounding. */
6099 if (! flag_errno_math && integer_valued_real_p (arg))
6100 return arg;
6102 if (optimize)
6104 tree arg0 = strip_float_extensions (arg);
6105 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6106 tree newtype = TREE_TYPE (arg0);
6107 tree decl;
6109 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6110 && (decl = mathfn_built_in (newtype, fcode)))
6112 arglist =
6113 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6114 return fold_convert (ftype,
6115 build_function_call_expr (decl, arglist));
6118 return 0;
6121 /* EXP is assumed to be builtin call which can narrow the FP type of
6122 the argument, for instance lround((double)f) -> lroundf (f). */
6124 static tree
6125 fold_fixed_mathfn (tree fndecl, tree arglist)
6127 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6128 tree arg;
6130 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6131 return 0;
6133 arg = TREE_VALUE (arglist);
6135 /* If argument is already integer valued, and we don't need to worry
6136 about setting errno, there's no need to perform rounding. */
6137 if (! flag_errno_math && integer_valued_real_p (arg))
6138 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg));
6140 if (optimize)
6142 tree ftype = TREE_TYPE (arg);
6143 tree arg0 = strip_float_extensions (arg);
6144 tree newtype = TREE_TYPE (arg0);
6145 tree decl;
6147 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6148 && (decl = mathfn_built_in (newtype, fcode)))
6150 arglist =
6151 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6152 return build_function_call_expr (decl, arglist);
6155 return 0;
6158 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6159 is the argument list and TYPE is the return type. Return
6160 NULL_TREE if no if no simplification can be made. */
6162 static tree
6163 fold_builtin_cabs (tree arglist, tree type)
6165 tree arg;
6167 if (!arglist || TREE_CHAIN (arglist))
6168 return NULL_TREE;
6170 arg = TREE_VALUE (arglist);
6171 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6172 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6173 return NULL_TREE;
6175 /* Evaluate cabs of a constant at compile-time. */
6176 if (flag_unsafe_math_optimizations
6177 && TREE_CODE (arg) == COMPLEX_CST
6178 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6179 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6180 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6181 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6183 REAL_VALUE_TYPE r, i;
6185 r = TREE_REAL_CST (TREE_REALPART (arg));
6186 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6188 real_arithmetic (&r, MULT_EXPR, &r, &r);
6189 real_arithmetic (&i, MULT_EXPR, &i, &i);
6190 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6191 if (real_sqrt (&r, TYPE_MODE (type), &r)
6192 || ! flag_trapping_math)
6193 return build_real (type, r);
6196 /* If either part is zero, cabs is fabs of the other. */
6197 if (TREE_CODE (arg) == COMPLEX_EXPR
6198 && real_zerop (TREE_OPERAND (arg, 0)))
6199 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6200 if (TREE_CODE (arg) == COMPLEX_EXPR
6201 && real_zerop (TREE_OPERAND (arg, 1)))
6202 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6204 /* Don't do this when optimizing for size. */
6205 if (flag_unsafe_math_optimizations
6206 && optimize && !optimize_size)
6208 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6210 if (sqrtfn != NULL_TREE)
6212 tree rpart, ipart, result, arglist;
6214 arg = builtin_save_expr (arg);
6216 rpart = fold (build1 (REALPART_EXPR, type, arg));
6217 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6219 rpart = builtin_save_expr (rpart);
6220 ipart = builtin_save_expr (ipart);
6222 result = fold (build2 (PLUS_EXPR, type,
6223 fold (build2 (MULT_EXPR, type,
6224 rpart, rpart)),
6225 fold (build2 (MULT_EXPR, type,
6226 ipart, ipart))));
6228 arglist = build_tree_list (NULL_TREE, result);
6229 return build_function_call_expr (sqrtfn, arglist);
6233 return NULL_TREE;
6236 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6237 NULL_TREE if no simplification can be made. */
6239 static tree
6240 fold_builtin_sqrt (tree arglist, tree type)
6243 enum built_in_function fcode;
6244 tree arg = TREE_VALUE (arglist);
6246 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6247 return NULL_TREE;
6249 /* Optimize sqrt of constant value. */
6250 if (TREE_CODE (arg) == REAL_CST
6251 && ! TREE_CONSTANT_OVERFLOW (arg))
6253 REAL_VALUE_TYPE r, x;
6255 x = TREE_REAL_CST (arg);
6256 if (real_sqrt (&r, TYPE_MODE (type), &x)
6257 || (!flag_trapping_math && !flag_errno_math))
6258 return build_real (type, r);
6261 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6262 fcode = builtin_mathfn_code (arg);
6263 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6265 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6266 arg = fold (build2 (MULT_EXPR, type,
6267 TREE_VALUE (TREE_OPERAND (arg, 1)),
6268 build_real (type, dconsthalf)));
6269 arglist = build_tree_list (NULL_TREE, arg);
6270 return build_function_call_expr (expfn, arglist);
6273 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6274 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6276 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6278 if (powfn)
6280 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6281 tree tree_root;
6282 /* The inner root was either sqrt or cbrt. */
6283 REAL_VALUE_TYPE dconstroot =
6284 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6286 /* Adjust for the outer root. */
6287 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6288 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6289 tree_root = build_real (type, dconstroot);
6290 arglist = tree_cons (NULL_TREE, arg0,
6291 build_tree_list (NULL_TREE, tree_root));
6292 return build_function_call_expr (powfn, arglist);
6296 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6297 if (flag_unsafe_math_optimizations
6298 && (fcode == BUILT_IN_POW
6299 || fcode == BUILT_IN_POWF
6300 || fcode == BUILT_IN_POWL))
6302 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6303 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6304 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6305 tree narg1;
6306 if (!tree_expr_nonnegative_p (arg0))
6307 arg0 = build1 (ABS_EXPR, type, arg0);
6308 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6309 build_real (type, dconsthalf)));
6310 arglist = tree_cons (NULL_TREE, arg0,
6311 build_tree_list (NULL_TREE, narg1));
6312 return build_function_call_expr (powfn, arglist);
6315 return NULL_TREE;
6318 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6319 NULL_TREE if no simplification can be made. */
6320 static tree
6321 fold_builtin_cbrt (tree arglist, tree type)
6323 tree arg = TREE_VALUE (arglist);
6324 const enum built_in_function fcode = builtin_mathfn_code (arg);
6326 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6327 return NULL_TREE;
6329 /* Optimize cbrt of constant value. */
6330 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6331 return arg;
6333 if (flag_unsafe_math_optimizations)
6335 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6336 if (BUILTIN_EXPONENT_P (fcode))
6338 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6339 const REAL_VALUE_TYPE third_trunc =
6340 real_value_truncate (TYPE_MODE (type), dconstthird);
6341 arg = fold (build2 (MULT_EXPR, type,
6342 TREE_VALUE (TREE_OPERAND (arg, 1)),
6343 build_real (type, third_trunc)));
6344 arglist = build_tree_list (NULL_TREE, arg);
6345 return build_function_call_expr (expfn, arglist);
6348 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6349 if (BUILTIN_SQRT_P (fcode))
6351 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6353 if (powfn)
6355 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6356 tree tree_root;
6357 REAL_VALUE_TYPE dconstroot = dconstthird;
6359 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6360 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6361 tree_root = build_real (type, dconstroot);
6362 arglist = tree_cons (NULL_TREE, arg0,
6363 build_tree_list (NULL_TREE, tree_root));
6364 return build_function_call_expr (powfn, arglist);
6368 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
6369 if (BUILTIN_CBRT_P (fcode))
6371 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6372 if (tree_expr_nonnegative_p (arg0))
6374 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6376 if (powfn)
6378 tree tree_root;
6379 REAL_VALUE_TYPE dconstroot;
6381 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
6382 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6383 tree_root = build_real (type, dconstroot);
6384 arglist = tree_cons (NULL_TREE, arg0,
6385 build_tree_list (NULL_TREE, tree_root));
6386 return build_function_call_expr (powfn, arglist);
6391 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
6392 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
6393 || fcode == BUILT_IN_POWL)
6395 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
6396 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6397 if (tree_expr_nonnegative_p (arg00))
6399 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6400 const REAL_VALUE_TYPE dconstroot
6401 = real_value_truncate (TYPE_MODE (type), dconstthird);
6402 tree narg01 = fold (build2 (MULT_EXPR, type, arg01,
6403 build_real (type, dconstroot)));
6404 arglist = tree_cons (NULL_TREE, arg00,
6405 build_tree_list (NULL_TREE, narg01));
6406 return build_function_call_expr (powfn, arglist);
6410 return NULL_TREE;
6413 /* Fold function call to builtin sin, sinf, or sinl. Return
6414 NULL_TREE if no simplification can be made. */
6415 static tree
6416 fold_builtin_sin (tree arglist)
6418 tree arg = TREE_VALUE (arglist);
6420 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6421 return NULL_TREE;
6423 /* Optimize sin (0.0) = 0.0. */
6424 if (real_zerop (arg))
6425 return arg;
6427 return NULL_TREE;
6430 /* Fold function call to builtin cos, cosf, or cosl. Return
6431 NULL_TREE if no simplification can be made. */
6432 static tree
6433 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6435 tree arg = TREE_VALUE (arglist);
6437 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6438 return NULL_TREE;
6440 /* Optimize cos (0.0) = 1.0. */
6441 if (real_zerop (arg))
6442 return build_real (type, dconst1);
6444 /* Optimize cos(-x) into cos (x). */
6445 if (TREE_CODE (arg) == NEGATE_EXPR)
6447 tree args = build_tree_list (NULL_TREE,
6448 TREE_OPERAND (arg, 0));
6449 return build_function_call_expr (fndecl, args);
6452 return NULL_TREE;
6455 /* Fold function call to builtin tan, tanf, or tanl. Return
6456 NULL_TREE if no simplification can be made. */
6457 static tree
6458 fold_builtin_tan (tree arglist)
6460 enum built_in_function fcode;
6461 tree arg = TREE_VALUE (arglist);
6463 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6464 return NULL_TREE;
6466 /* Optimize tan(0.0) = 0.0. */
6467 if (real_zerop (arg))
6468 return arg;
6470 /* Optimize tan(atan(x)) = x. */
6471 fcode = builtin_mathfn_code (arg);
6472 if (flag_unsafe_math_optimizations
6473 && (fcode == BUILT_IN_ATAN
6474 || fcode == BUILT_IN_ATANF
6475 || fcode == BUILT_IN_ATANL))
6476 return TREE_VALUE (TREE_OPERAND (arg, 1));
6478 return NULL_TREE;
6481 /* Fold function call to builtin atan, atanf, or atanl. Return
6482 NULL_TREE if no simplification can be made. */
6484 static tree
6485 fold_builtin_atan (tree arglist, tree type)
6488 tree arg = TREE_VALUE (arglist);
6490 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6491 return NULL_TREE;
6493 /* Optimize atan(0.0) = 0.0. */
6494 if (real_zerop (arg))
6495 return arg;
6497 /* Optimize atan(1.0) = pi/4. */
6498 if (real_onep (arg))
6500 REAL_VALUE_TYPE cst;
6502 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6503 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6504 return build_real (type, cst);
6507 return NULL_TREE;
6510 /* Fold function call to builtin trunc, truncf or truncl. Return
6511 NULL_TREE if no simplification can be made. */
6513 static tree
6514 fold_builtin_trunc (tree fndecl, tree arglist)
6516 tree arg;
6518 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6519 return 0;
6521 /* Optimize trunc of constant value. */
6522 arg = TREE_VALUE (arglist);
6523 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6525 REAL_VALUE_TYPE r, x;
6526 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6528 x = TREE_REAL_CST (arg);
6529 real_trunc (&r, TYPE_MODE (type), &x);
6530 return build_real (type, r);
6533 return fold_trunc_transparent_mathfn (fndecl, arglist);
6536 /* Fold function call to builtin floor, floorf or floorl. Return
6537 NULL_TREE if no simplification can be made. */
6539 static tree
6540 fold_builtin_floor (tree fndecl, tree arglist)
6542 tree arg;
6544 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6545 return 0;
6547 /* Optimize floor of constant value. */
6548 arg = TREE_VALUE (arglist);
6549 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6551 REAL_VALUE_TYPE x;
6553 x = TREE_REAL_CST (arg);
6554 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6556 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6557 REAL_VALUE_TYPE r;
6559 real_floor (&r, TYPE_MODE (type), &x);
6560 return build_real (type, r);
6564 return fold_trunc_transparent_mathfn (fndecl, arglist);
6567 /* Fold function call to builtin ceil, ceilf or ceill. Return
6568 NULL_TREE if no simplification can be made. */
6570 static tree
6571 fold_builtin_ceil (tree fndecl, tree arglist)
6573 tree arg;
6575 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6576 return 0;
6578 /* Optimize ceil of constant value. */
6579 arg = TREE_VALUE (arglist);
6580 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6582 REAL_VALUE_TYPE x;
6584 x = TREE_REAL_CST (arg);
6585 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6587 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6588 REAL_VALUE_TYPE r;
6590 real_ceil (&r, TYPE_MODE (type), &x);
6591 return build_real (type, r);
6595 return fold_trunc_transparent_mathfn (fndecl, arglist);
6598 /* Fold function call to builtin round, roundf or roundl. Return
6599 NULL_TREE if no simplification can be made. */
6601 static tree
6602 fold_builtin_round (tree fndecl, tree arglist)
6604 tree arg;
6606 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6607 return 0;
6609 /* Optimize round of constant value. */
6610 arg = TREE_VALUE (arglist);
6611 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6613 REAL_VALUE_TYPE x;
6615 x = TREE_REAL_CST (arg);
6616 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6618 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6619 REAL_VALUE_TYPE r;
6621 real_round (&r, TYPE_MODE (type), &x);
6622 return build_real (type, r);
6626 return fold_trunc_transparent_mathfn (fndecl, arglist);
6629 /* Fold function call to builtin lround, lroundf or lroundl (or the
6630 corresponding long long versions). Return NULL_TREE if no
6631 simplification can be made. */
6633 static tree
6634 fold_builtin_lround (tree fndecl, tree arglist)
6636 tree arg;
6638 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6639 return 0;
6641 /* Optimize lround of constant value. */
6642 arg = TREE_VALUE (arglist);
6643 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6645 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6647 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6649 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
6650 tree ftype = TREE_TYPE (arg), result;
6651 HOST_WIDE_INT hi, lo;
6652 REAL_VALUE_TYPE r;
6654 real_round (&r, TYPE_MODE (ftype), &x);
6655 REAL_VALUE_TO_INT (&lo, &hi, r);
6656 result = build_int_cst_wide (NULL_TREE, lo, hi);
6657 if (int_fits_type_p (result, itype))
6658 return fold_convert (itype, result);
6662 return fold_fixed_mathfn (fndecl, arglist);
6665 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6666 and their long and long long variants (i.e. ffsl and ffsll).
6667 Return NULL_TREE if no simplification can be made. */
6669 static tree
6670 fold_builtin_bitop (tree fndecl, tree arglist)
6672 tree arg;
6674 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6675 return NULL_TREE;
6677 /* Optimize for constant argument. */
6678 arg = TREE_VALUE (arglist);
6679 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6681 HOST_WIDE_INT hi, width, result;
6682 unsigned HOST_WIDE_INT lo;
6683 tree type;
6685 type = TREE_TYPE (arg);
6686 width = TYPE_PRECISION (type);
6687 lo = TREE_INT_CST_LOW (arg);
6689 /* Clear all the bits that are beyond the type's precision. */
6690 if (width > HOST_BITS_PER_WIDE_INT)
6692 hi = TREE_INT_CST_HIGH (arg);
6693 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6694 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6696 else
6698 hi = 0;
6699 if (width < HOST_BITS_PER_WIDE_INT)
6700 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6703 switch (DECL_FUNCTION_CODE (fndecl))
6705 case BUILT_IN_FFS:
6706 case BUILT_IN_FFSL:
6707 case BUILT_IN_FFSLL:
6708 if (lo != 0)
6709 result = exact_log2 (lo & -lo) + 1;
6710 else if (hi != 0)
6711 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6712 else
6713 result = 0;
6714 break;
6716 case BUILT_IN_CLZ:
6717 case BUILT_IN_CLZL:
6718 case BUILT_IN_CLZLL:
6719 if (hi != 0)
6720 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6721 else if (lo != 0)
6722 result = width - floor_log2 (lo) - 1;
6723 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6724 result = width;
6725 break;
6727 case BUILT_IN_CTZ:
6728 case BUILT_IN_CTZL:
6729 case BUILT_IN_CTZLL:
6730 if (lo != 0)
6731 result = exact_log2 (lo & -lo);
6732 else if (hi != 0)
6733 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6734 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6735 result = width;
6736 break;
6738 case BUILT_IN_POPCOUNT:
6739 case BUILT_IN_POPCOUNTL:
6740 case BUILT_IN_POPCOUNTLL:
6741 result = 0;
6742 while (lo)
6743 result++, lo &= lo - 1;
6744 while (hi)
6745 result++, hi &= hi - 1;
6746 break;
6748 case BUILT_IN_PARITY:
6749 case BUILT_IN_PARITYL:
6750 case BUILT_IN_PARITYLL:
6751 result = 0;
6752 while (lo)
6753 result++, lo &= lo - 1;
6754 while (hi)
6755 result++, hi &= hi - 1;
6756 result &= 1;
6757 break;
6759 default:
6760 gcc_unreachable ();
6763 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
6766 return NULL_TREE;
6769 /* Return true if EXPR is the real constant contained in VALUE. */
6771 static bool
6772 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6774 STRIP_NOPS (expr);
6776 return ((TREE_CODE (expr) == REAL_CST
6777 && ! TREE_CONSTANT_OVERFLOW (expr)
6778 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6779 || (TREE_CODE (expr) == COMPLEX_CST
6780 && real_dconstp (TREE_REALPART (expr), value)
6781 && real_zerop (TREE_IMAGPART (expr))));
6784 /* A subroutine of fold_builtin to fold the various logarithmic
6785 functions. EXP is the CALL_EXPR of a call to a builtin logN
6786 function. VALUE is the base of the logN function. */
6788 static tree
6789 fold_builtin_logarithm (tree fndecl, tree arglist,
6790 const REAL_VALUE_TYPE *value)
6792 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6794 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6795 tree arg = TREE_VALUE (arglist);
6796 const enum built_in_function fcode = builtin_mathfn_code (arg);
6798 /* Optimize logN(1.0) = 0.0. */
6799 if (real_onep (arg))
6800 return build_real (type, dconst0);
6802 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6803 exactly, then only do this if flag_unsafe_math_optimizations. */
6804 if (exact_real_truncate (TYPE_MODE (type), value)
6805 || flag_unsafe_math_optimizations)
6807 const REAL_VALUE_TYPE value_truncate =
6808 real_value_truncate (TYPE_MODE (type), *value);
6809 if (real_dconstp (arg, &value_truncate))
6810 return build_real (type, dconst1);
6813 /* Special case, optimize logN(expN(x)) = x. */
6814 if (flag_unsafe_math_optimizations
6815 && ((value == &dconste
6816 && (fcode == BUILT_IN_EXP
6817 || fcode == BUILT_IN_EXPF
6818 || fcode == BUILT_IN_EXPL))
6819 || (value == &dconst2
6820 && (fcode == BUILT_IN_EXP2
6821 || fcode == BUILT_IN_EXP2F
6822 || fcode == BUILT_IN_EXP2L))
6823 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6824 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6826 /* Optimize logN(func()) for various exponential functions. We
6827 want to determine the value "x" and the power "exponent" in
6828 order to transform logN(x**exponent) into exponent*logN(x). */
6829 if (flag_unsafe_math_optimizations)
6831 tree exponent = 0, x = 0;
6833 switch (fcode)
6835 case BUILT_IN_EXP:
6836 case BUILT_IN_EXPF:
6837 case BUILT_IN_EXPL:
6838 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6839 x = build_real (type,
6840 real_value_truncate (TYPE_MODE (type), dconste));
6841 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6842 break;
6843 case BUILT_IN_EXP2:
6844 case BUILT_IN_EXP2F:
6845 case BUILT_IN_EXP2L:
6846 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6847 x = build_real (type, dconst2);
6848 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6849 break;
6850 case BUILT_IN_EXP10:
6851 case BUILT_IN_EXP10F:
6852 case BUILT_IN_EXP10L:
6853 case BUILT_IN_POW10:
6854 case BUILT_IN_POW10F:
6855 case BUILT_IN_POW10L:
6856 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6857 x = build_real (type, dconst10);
6858 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6859 break;
6860 case BUILT_IN_SQRT:
6861 case BUILT_IN_SQRTF:
6862 case BUILT_IN_SQRTL:
6863 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6864 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6865 exponent = build_real (type, dconsthalf);
6866 break;
6867 case BUILT_IN_CBRT:
6868 case BUILT_IN_CBRTF:
6869 case BUILT_IN_CBRTL:
6870 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6871 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6872 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6873 dconstthird));
6874 break;
6875 case BUILT_IN_POW:
6876 case BUILT_IN_POWF:
6877 case BUILT_IN_POWL:
6878 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6879 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6880 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6881 break;
6882 default:
6883 break;
6886 /* Now perform the optimization. */
6887 if (x && exponent)
6889 tree logfn;
6890 arglist = build_tree_list (NULL_TREE, x);
6891 logfn = build_function_call_expr (fndecl, arglist);
6892 return fold (build2 (MULT_EXPR, type, exponent, logfn));
6897 return 0;
6900 /* Fold a builtin function call to pow, powf, or powl. Return
6901 NULL_TREE if no simplification can be made. */
6902 static tree
6903 fold_builtin_pow (tree fndecl, tree arglist, tree type)
6905 tree arg0 = TREE_VALUE (arglist);
6906 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6908 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6909 return NULL_TREE;
6911 /* Optimize pow(1.0,y) = 1.0. */
6912 if (real_onep (arg0))
6913 return omit_one_operand (type, build_real (type, dconst1), arg1);
6915 if (TREE_CODE (arg1) == REAL_CST
6916 && ! TREE_CONSTANT_OVERFLOW (arg1))
6918 REAL_VALUE_TYPE cint;
6919 REAL_VALUE_TYPE c;
6920 HOST_WIDE_INT n;
6922 c = TREE_REAL_CST (arg1);
6924 /* Optimize pow(x,0.0) = 1.0. */
6925 if (REAL_VALUES_EQUAL (c, dconst0))
6926 return omit_one_operand (type, build_real (type, dconst1),
6927 arg0);
6929 /* Optimize pow(x,1.0) = x. */
6930 if (REAL_VALUES_EQUAL (c, dconst1))
6931 return arg0;
6933 /* Optimize pow(x,-1.0) = 1.0/x. */
6934 if (REAL_VALUES_EQUAL (c, dconstm1))
6935 return fold (build2 (RDIV_EXPR, type,
6936 build_real (type, dconst1), arg0));
6938 /* Optimize pow(x,0.5) = sqrt(x). */
6939 if (flag_unsafe_math_optimizations
6940 && REAL_VALUES_EQUAL (c, dconsthalf))
6942 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6944 if (sqrtfn != NULL_TREE)
6946 tree arglist = build_tree_list (NULL_TREE, arg0);
6947 return build_function_call_expr (sqrtfn, arglist);
6951 /* Check for an integer exponent. */
6952 n = real_to_integer (&c);
6953 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
6954 if (real_identical (&c, &cint))
6956 /* Attempt to evaluate pow at compile-time. */
6957 if (TREE_CODE (arg0) == REAL_CST
6958 && ! TREE_CONSTANT_OVERFLOW (arg0))
6960 REAL_VALUE_TYPE x;
6961 bool inexact;
6963 x = TREE_REAL_CST (arg0);
6964 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6965 if (flag_unsafe_math_optimizations || !inexact)
6966 return build_real (type, x);
6969 /* Strip sign ops from even integer powers. */
6970 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
6972 tree narg0 = fold_strip_sign_ops (arg0);
6973 if (narg0)
6975 arglist = build_tree_list (NULL_TREE, arg1);
6976 arglist = tree_cons (NULL_TREE, narg0, arglist);
6977 return build_function_call_expr (fndecl, arglist);
6983 if (flag_unsafe_math_optimizations)
6985 const enum built_in_function fcode = builtin_mathfn_code (arg0);
6987 /* Optimize pow(expN(x),y) = expN(x*y). */
6988 if (BUILTIN_EXPONENT_P (fcode))
6990 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6991 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6992 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
6993 arglist = build_tree_list (NULL_TREE, arg);
6994 return build_function_call_expr (expfn, arglist);
6997 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6998 if (BUILTIN_SQRT_P (fcode))
7000 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7001 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7002 build_real (type, dconsthalf)));
7004 arglist = tree_cons (NULL_TREE, narg0,
7005 build_tree_list (NULL_TREE, narg1));
7006 return build_function_call_expr (fndecl, arglist);
7009 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7010 if (BUILTIN_CBRT_P (fcode))
7012 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7013 if (tree_expr_nonnegative_p (arg))
7015 const REAL_VALUE_TYPE dconstroot
7016 = real_value_truncate (TYPE_MODE (type), dconstthird);
7017 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7018 build_real (type, dconstroot)));
7019 arglist = tree_cons (NULL_TREE, arg,
7020 build_tree_list (NULL_TREE, narg1));
7021 return build_function_call_expr (fndecl, arglist);
7025 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7026 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7027 || fcode == BUILT_IN_POWL)
7029 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7030 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7031 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7032 arglist = tree_cons (NULL_TREE, arg00,
7033 build_tree_list (NULL_TREE, narg1));
7034 return build_function_call_expr (fndecl, arglist);
7038 return NULL_TREE;
7041 /* Fold a builtin function call to powi, powif, or powil. Return
7042 NULL_TREE if no simplification can be made. */
7043 static tree
7044 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7046 tree arg0 = TREE_VALUE (arglist);
7047 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7049 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7050 return NULL_TREE;
7052 /* Optimize pow(1.0,y) = 1.0. */
7053 if (real_onep (arg0))
7054 return omit_one_operand (type, build_real (type, dconst1), arg1);
7056 if (host_integerp (arg1, 0))
7058 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7060 /* Evaluate powi at compile-time. */
7061 if (TREE_CODE (arg0) == REAL_CST
7062 && ! TREE_CONSTANT_OVERFLOW (arg0))
7064 REAL_VALUE_TYPE x;
7065 x = TREE_REAL_CST (arg0);
7066 real_powi (&x, TYPE_MODE (type), &x, c);
7067 return build_real (type, x);
7070 /* Optimize pow(x,0) = 1.0. */
7071 if (c == 0)
7072 return omit_one_operand (type, build_real (type, dconst1),
7073 arg0);
7075 /* Optimize pow(x,1) = x. */
7076 if (c == 1)
7077 return arg0;
7079 /* Optimize pow(x,-1) = 1.0/x. */
7080 if (c == -1)
7081 return fold (build2 (RDIV_EXPR, type,
7082 build_real (type, dconst1), arg0));
7085 return NULL_TREE;
7088 /* A subroutine of fold_builtin to fold the various exponent
7089 functions. EXP is the CALL_EXPR of a call to a builtin function.
7090 VALUE is the value which will be raised to a power. */
7092 static tree
7093 fold_builtin_exponent (tree fndecl, tree arglist,
7094 const REAL_VALUE_TYPE *value)
7096 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7098 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7099 tree arg = TREE_VALUE (arglist);
7101 /* Optimize exp*(0.0) = 1.0. */
7102 if (real_zerop (arg))
7103 return build_real (type, dconst1);
7105 /* Optimize expN(1.0) = N. */
7106 if (real_onep (arg))
7108 REAL_VALUE_TYPE cst;
7110 real_convert (&cst, TYPE_MODE (type), value);
7111 return build_real (type, cst);
7114 /* Attempt to evaluate expN(integer) at compile-time. */
7115 if (flag_unsafe_math_optimizations
7116 && TREE_CODE (arg) == REAL_CST
7117 && ! TREE_CONSTANT_OVERFLOW (arg))
7119 REAL_VALUE_TYPE cint;
7120 REAL_VALUE_TYPE c;
7121 HOST_WIDE_INT n;
7123 c = TREE_REAL_CST (arg);
7124 n = real_to_integer (&c);
7125 real_from_integer (&cint, VOIDmode, n,
7126 n < 0 ? -1 : 0, 0);
7127 if (real_identical (&c, &cint))
7129 REAL_VALUE_TYPE x;
7131 real_powi (&x, TYPE_MODE (type), value, n);
7132 return build_real (type, x);
7136 /* Optimize expN(logN(x)) = x. */
7137 if (flag_unsafe_math_optimizations)
7139 const enum built_in_function fcode = builtin_mathfn_code (arg);
7141 if ((value == &dconste
7142 && (fcode == BUILT_IN_LOG
7143 || fcode == BUILT_IN_LOGF
7144 || fcode == BUILT_IN_LOGL))
7145 || (value == &dconst2
7146 && (fcode == BUILT_IN_LOG2
7147 || fcode == BUILT_IN_LOG2F
7148 || fcode == BUILT_IN_LOG2L))
7149 || (value == &dconst10
7150 && (fcode == BUILT_IN_LOG10
7151 || fcode == BUILT_IN_LOG10F
7152 || fcode == BUILT_IN_LOG10L)))
7153 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7157 return 0;
7160 /* Fold function call to builtin memcpy. Return
7161 NULL_TREE if no simplification can be made. */
7163 static tree
7164 fold_builtin_memcpy (tree fndecl, tree arglist)
7166 tree dest, src, len;
7168 if (!validate_arglist (arglist,
7169 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7170 return 0;
7172 dest = TREE_VALUE (arglist);
7173 src = TREE_VALUE (TREE_CHAIN (arglist));
7174 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7176 /* If the LEN parameter is zero, return DEST. */
7177 if (integer_zerop (len))
7178 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
7180 /* If SRC and DEST are the same (and not volatile), return DEST. */
7181 if (operand_equal_p (src, dest, 0))
7182 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
7184 return 0;
7187 /* Fold function call to builtin mempcpy. Return
7188 NULL_TREE if no simplification can be made. */
7190 static tree
7191 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7193 if (validate_arglist (arglist,
7194 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7196 tree dest = TREE_VALUE (arglist);
7197 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7198 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7200 /* If the LEN parameter is zero, return DEST. */
7201 if (integer_zerop (len))
7202 return omit_one_operand (type, dest, src);
7204 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7205 if (operand_equal_p (src, dest, 0))
7207 if (endp == 0)
7208 return omit_one_operand (type, dest, len);
7210 if (endp == 2)
7211 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7212 ssize_int (1)));
7214 len = fold_convert (TREE_TYPE (dest), len);
7215 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7216 return fold_convert (type, len);
7219 return 0;
7222 /* Fold function call to builtin memmove. Return
7223 NULL_TREE if no simplification can be made. */
7225 static tree
7226 fold_builtin_memmove (tree arglist, tree type)
7228 tree dest, src, len;
7230 if (!validate_arglist (arglist,
7231 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7232 return 0;
7234 dest = TREE_VALUE (arglist);
7235 src = TREE_VALUE (TREE_CHAIN (arglist));
7236 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7238 /* If the LEN parameter is zero, return DEST. */
7239 if (integer_zerop (len))
7240 return omit_one_operand (type, dest, src);
7242 /* If SRC and DEST are the same (and not volatile), return DEST. */
7243 if (operand_equal_p (src, dest, 0))
7244 return omit_one_operand (type, dest, len);
7246 return 0;
7249 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7250 the length of the string to be copied. Return NULL_TREE if no
7251 simplification can be made. */
7253 tree
7254 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
7256 tree dest, src, fn;
7258 if (!validate_arglist (arglist,
7259 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7260 return 0;
7262 dest = TREE_VALUE (arglist);
7263 src = TREE_VALUE (TREE_CHAIN (arglist));
7265 /* If SRC and DEST are the same (and not volatile), return DEST. */
7266 if (operand_equal_p (src, dest, 0))
7267 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
7269 if (optimize_size)
7270 return 0;
7272 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7273 if (!fn)
7274 return 0;
7276 if (!len)
7278 len = c_strlen (src, 1);
7279 if (! len || TREE_SIDE_EFFECTS (len))
7280 return 0;
7283 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7284 arglist = build_tree_list (NULL_TREE, len);
7285 arglist = tree_cons (NULL_TREE, src, arglist);
7286 arglist = tree_cons (NULL_TREE, dest, arglist);
7287 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7288 build_function_call_expr (fn, arglist));
7291 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7292 the length of the source string. Return NULL_TREE if no simplification
7293 can be made. */
7295 tree
7296 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
7298 tree dest, src, len, fn;
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 /* We can't compare slen with len as constants below if len is not a
7313 constant. */
7314 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7315 return 0;
7317 if (!slen)
7318 slen = c_strlen (src, 1);
7320 /* Now, we must be passed a constant src ptr parameter. */
7321 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7322 return 0;
7324 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7326 /* We do not support simplification of this case, though we do
7327 support it when expanding trees into RTL. */
7328 /* FIXME: generate a call to __builtin_memset. */
7329 if (tree_int_cst_lt (slen, len))
7330 return 0;
7332 /* OK transform into builtin memcpy. */
7333 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7334 if (!fn)
7335 return 0;
7336 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7337 build_function_call_expr (fn, arglist));
7340 /* Fold function call to builtin memcmp. Return
7341 NULL_TREE if no simplification can be made. */
7343 static tree
7344 fold_builtin_memcmp (tree arglist)
7346 tree arg1, arg2, len;
7347 const char *p1, *p2;
7349 if (!validate_arglist (arglist,
7350 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7351 return 0;
7353 arg1 = TREE_VALUE (arglist);
7354 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7355 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7357 /* If the LEN parameter is zero, return zero. */
7358 if (integer_zerop (len))
7359 return omit_two_operands (integer_type_node, integer_zero_node,
7360 arg1, arg2);
7362 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7363 if (operand_equal_p (arg1, arg2, 0))
7364 return omit_one_operand (integer_type_node, integer_zero_node, len);
7366 p1 = c_getstr (arg1);
7367 p2 = c_getstr (arg2);
7369 /* If all arguments are constant, and the value of len is not greater
7370 than the lengths of arg1 and arg2, evaluate at compile-time. */
7371 if (host_integerp (len, 1) && p1 && p2
7372 && compare_tree_int (len, strlen (p1) + 1) <= 0
7373 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7375 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7377 if (r > 0)
7378 return integer_one_node;
7379 else if (r < 0)
7380 return integer_minus_one_node;
7381 else
7382 return integer_zero_node;
7385 /* If len parameter is one, return an expression corresponding to
7386 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7387 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7389 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7390 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7391 tree ind1 = fold_convert (integer_type_node,
7392 build1 (INDIRECT_REF, cst_uchar_node,
7393 fold_convert (cst_uchar_ptr_node,
7394 arg1)));
7395 tree ind2 = fold_convert (integer_type_node,
7396 build1 (INDIRECT_REF, cst_uchar_node,
7397 fold_convert (cst_uchar_ptr_node,
7398 arg2)));
7399 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7402 return 0;
7405 /* Fold function call to builtin strcmp. Return
7406 NULL_TREE if no simplification can be made. */
7408 static tree
7409 fold_builtin_strcmp (tree arglist)
7411 tree arg1, arg2;
7412 const char *p1, *p2;
7414 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7415 return 0;
7417 arg1 = TREE_VALUE (arglist);
7418 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7420 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7421 if (operand_equal_p (arg1, arg2, 0))
7422 return integer_zero_node;
7424 p1 = c_getstr (arg1);
7425 p2 = c_getstr (arg2);
7427 if (p1 && p2)
7429 const int i = strcmp (p1, p2);
7430 if (i < 0)
7431 return integer_minus_one_node;
7432 else if (i > 0)
7433 return integer_one_node;
7434 else
7435 return integer_zero_node;
7438 /* If the second arg is "", return *(const unsigned char*)arg1. */
7439 if (p2 && *p2 == '\0')
7441 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7442 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7443 return fold_convert (integer_type_node,
7444 build1 (INDIRECT_REF, cst_uchar_node,
7445 fold_convert (cst_uchar_ptr_node,
7446 arg1)));
7449 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7450 if (p1 && *p1 == '\0')
7452 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7453 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7454 tree temp = fold_convert (integer_type_node,
7455 build1 (INDIRECT_REF, cst_uchar_node,
7456 fold_convert (cst_uchar_ptr_node,
7457 arg2)));
7458 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7461 return 0;
7464 /* Fold function call to builtin strncmp. Return
7465 NULL_TREE if no simplification can be made. */
7467 static tree
7468 fold_builtin_strncmp (tree arglist)
7470 tree arg1, arg2, len;
7471 const char *p1, *p2;
7473 if (!validate_arglist (arglist,
7474 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7475 return 0;
7477 arg1 = TREE_VALUE (arglist);
7478 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7479 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7481 /* If the LEN parameter is zero, return zero. */
7482 if (integer_zerop (len))
7483 return omit_two_operands (integer_type_node, integer_zero_node,
7484 arg1, arg2);
7486 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7487 if (operand_equal_p (arg1, arg2, 0))
7488 return omit_one_operand (integer_type_node, integer_zero_node, len);
7490 p1 = c_getstr (arg1);
7491 p2 = c_getstr (arg2);
7493 if (host_integerp (len, 1) && p1 && p2)
7495 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7496 if (i > 0)
7497 return integer_one_node;
7498 else if (i < 0)
7499 return integer_minus_one_node;
7500 else
7501 return integer_zero_node;
7504 /* If the second arg is "", and the length is greater than zero,
7505 return *(const unsigned char*)arg1. */
7506 if (p2 && *p2 == '\0'
7507 && TREE_CODE (len) == INTEGER_CST
7508 && tree_int_cst_sgn (len) == 1)
7510 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7511 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7512 return fold_convert (integer_type_node,
7513 build1 (INDIRECT_REF, cst_uchar_node,
7514 fold_convert (cst_uchar_ptr_node,
7515 arg1)));
7518 /* If the first arg is "", and the length is greater than zero,
7519 return -*(const unsigned char*)arg2. */
7520 if (p1 && *p1 == '\0'
7521 && TREE_CODE (len) == INTEGER_CST
7522 && tree_int_cst_sgn (len) == 1)
7524 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7525 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7526 tree temp = fold_convert (integer_type_node,
7527 build1 (INDIRECT_REF, cst_uchar_node,
7528 fold_convert (cst_uchar_ptr_node,
7529 arg2)));
7530 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7533 /* If len parameter is one, return an expression corresponding to
7534 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7535 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7537 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7538 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7539 tree ind1 = fold_convert (integer_type_node,
7540 build1 (INDIRECT_REF, cst_uchar_node,
7541 fold_convert (cst_uchar_ptr_node,
7542 arg1)));
7543 tree ind2 = fold_convert (integer_type_node,
7544 build1 (INDIRECT_REF, cst_uchar_node,
7545 fold_convert (cst_uchar_ptr_node,
7546 arg2)));
7547 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7550 return 0;
7553 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7554 NULL_TREE if no simplification can be made. */
7556 static tree
7557 fold_builtin_signbit (tree fndecl, tree arglist)
7559 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7560 tree arg, temp;
7562 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7563 return NULL_TREE;
7565 arg = TREE_VALUE (arglist);
7567 /* If ARG is a compile-time constant, determine the result. */
7568 if (TREE_CODE (arg) == REAL_CST
7569 && !TREE_CONSTANT_OVERFLOW (arg))
7571 REAL_VALUE_TYPE c;
7573 c = TREE_REAL_CST (arg);
7574 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7575 return fold_convert (type, temp);
7578 /* If ARG is non-negative, the result is always zero. */
7579 if (tree_expr_nonnegative_p (arg))
7580 return omit_one_operand (type, integer_zero_node, arg);
7582 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7583 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7584 return fold (build2 (LT_EXPR, type, arg,
7585 build_real (TREE_TYPE (arg), dconst0)));
7587 return NULL_TREE;
7590 /* Fold function call to builtin copysign, copysignf or copysignl.
7591 Return NULL_TREE if no simplification can be made. */
7593 static tree
7594 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7596 tree arg1, arg2, tem;
7598 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7599 return NULL_TREE;
7601 arg1 = TREE_VALUE (arglist);
7602 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7604 /* copysign(X,X) is X. */
7605 if (operand_equal_p (arg1, arg2, 0))
7606 return fold_convert (type, arg1);
7608 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7609 if (TREE_CODE (arg1) == REAL_CST
7610 && TREE_CODE (arg2) == REAL_CST
7611 && !TREE_CONSTANT_OVERFLOW (arg1)
7612 && !TREE_CONSTANT_OVERFLOW (arg2))
7614 REAL_VALUE_TYPE c1, c2;
7616 c1 = TREE_REAL_CST (arg1);
7617 c2 = TREE_REAL_CST (arg2);
7618 real_copysign (&c1, &c2);
7619 return build_real (type, c1);
7620 c1.sign = c2.sign;
7623 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7624 Remember to evaluate Y for side-effects. */
7625 if (tree_expr_nonnegative_p (arg2))
7626 return omit_one_operand (type,
7627 fold (build1 (ABS_EXPR, type, arg1)),
7628 arg2);
7630 /* Strip sign changing operations for the first argument. */
7631 tem = fold_strip_sign_ops (arg1);
7632 if (tem)
7634 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7635 return build_function_call_expr (fndecl, arglist);
7638 return NULL_TREE;
7641 /* Fold a call to builtin isascii. */
7643 static tree
7644 fold_builtin_isascii (tree arglist)
7646 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7647 return 0;
7648 else
7650 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7651 tree arg = TREE_VALUE (arglist);
7653 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7654 build_int_cst (NULL_TREE,
7655 ~ (unsigned HOST_WIDE_INT) 0x7f));
7656 arg = fold (build2 (EQ_EXPR, integer_type_node,
7657 arg, integer_zero_node));
7659 if (in_gimple_form && !TREE_CONSTANT (arg))
7660 return NULL_TREE;
7661 else
7662 return arg;
7666 /* Fold a call to builtin toascii. */
7668 static tree
7669 fold_builtin_toascii (tree arglist)
7671 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7672 return 0;
7673 else
7675 /* Transform toascii(c) -> (c & 0x7f). */
7676 tree arg = TREE_VALUE (arglist);
7678 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7679 build_int_cst (NULL_TREE, 0x7f)));
7683 /* Fold a call to builtin isdigit. */
7685 static tree
7686 fold_builtin_isdigit (tree arglist)
7688 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7689 return 0;
7690 else
7692 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7693 /* According to the C standard, isdigit is unaffected by locale.
7694 However, it definitely is affected by the target character set. */
7695 tree arg;
7696 unsigned HOST_WIDE_INT target_digit0
7697 = lang_hooks.to_target_charset ('0');
7699 if (target_digit0 == 0)
7700 return NULL_TREE;
7702 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
7703 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7704 build_int_cst (unsigned_type_node, target_digit0));
7705 arg = build2 (LE_EXPR, integer_type_node, arg,
7706 build_int_cst (unsigned_type_node, 9));
7707 arg = fold (arg);
7708 if (in_gimple_form && !TREE_CONSTANT (arg))
7709 return NULL_TREE;
7710 else
7711 return arg;
7715 /* Fold a call to fabs, fabsf or fabsl. */
7717 static tree
7718 fold_builtin_fabs (tree arglist, tree type)
7720 tree arg;
7722 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7723 return 0;
7725 arg = TREE_VALUE (arglist);
7726 arg = fold_convert (type, arg);
7727 if (TREE_CODE (arg) == REAL_CST)
7728 return fold_abs_const (arg, type);
7729 return fold (build1 (ABS_EXPR, type, arg));
7732 /* Fold a call to abs, labs, llabs or imaxabs. */
7734 static tree
7735 fold_builtin_abs (tree arglist, tree type)
7737 tree arg;
7739 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7740 return 0;
7742 arg = TREE_VALUE (arglist);
7743 arg = fold_convert (type, arg);
7744 if (TREE_CODE (arg) == INTEGER_CST)
7745 return fold_abs_const (arg, type);
7746 return fold (build1 (ABS_EXPR, type, arg));
7749 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7750 EXP is the CALL_EXPR for the call. */
7752 static tree
7753 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
7755 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7756 tree arg;
7757 REAL_VALUE_TYPE r;
7759 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7761 /* Check that we have exactly one argument. */
7762 if (arglist == 0)
7764 error ("too few arguments to function %qs",
7765 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7766 return error_mark_node;
7768 else if (TREE_CHAIN (arglist) != 0)
7770 error ("too many arguments to function %qs",
7771 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7772 return error_mark_node;
7774 else
7776 error ("non-floating-point argument to function %qs",
7777 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7778 return error_mark_node;
7782 arg = TREE_VALUE (arglist);
7783 switch (builtin_index)
7785 case BUILT_IN_ISINF:
7786 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7787 return omit_one_operand (type, integer_zero_node, arg);
7789 if (TREE_CODE (arg) == REAL_CST)
7791 r = TREE_REAL_CST (arg);
7792 if (real_isinf (&r))
7793 return real_compare (GT_EXPR, &r, &dconst0)
7794 ? integer_one_node : integer_minus_one_node;
7795 else
7796 return integer_zero_node;
7799 return NULL_TREE;
7801 case BUILT_IN_FINITE:
7802 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7803 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7804 return omit_one_operand (type, integer_zero_node, arg);
7806 if (TREE_CODE (arg) == REAL_CST)
7808 r = TREE_REAL_CST (arg);
7809 return real_isinf (&r) || real_isnan (&r)
7810 ? integer_zero_node : integer_one_node;
7813 return NULL_TREE;
7815 case BUILT_IN_ISNAN:
7816 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7817 return omit_one_operand (type, integer_zero_node, arg);
7819 if (TREE_CODE (arg) == REAL_CST)
7821 r = TREE_REAL_CST (arg);
7822 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7825 arg = builtin_save_expr (arg);
7826 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7828 default:
7829 gcc_unreachable ();
7833 /* Fold a call to an unordered comparison function such as
7834 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
7835 being called and ARGLIST is the argument list for the call.
7836 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7837 the opposite of the desired result. UNORDERED_CODE is used
7838 for modes that can hold NaNs and ORDERED_CODE is used for
7839 the rest. */
7841 static tree
7842 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
7843 enum tree_code unordered_code,
7844 enum tree_code ordered_code)
7846 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7847 enum tree_code code;
7848 tree arg0, arg1;
7849 tree type0, type1;
7850 enum tree_code code0, code1;
7851 tree cmp_type = NULL_TREE;
7853 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7855 /* Check that we have exactly two arguments. */
7856 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7858 error ("too few arguments to function %qs",
7859 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7860 return error_mark_node;
7862 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7864 error ("too many arguments to function %qs",
7865 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7866 return error_mark_node;
7870 arg0 = TREE_VALUE (arglist);
7871 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7873 type0 = TREE_TYPE (arg0);
7874 type1 = TREE_TYPE (arg1);
7876 code0 = TREE_CODE (type0);
7877 code1 = TREE_CODE (type1);
7879 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7880 /* Choose the wider of two real types. */
7881 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7882 ? type0 : type1;
7883 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7884 cmp_type = type0;
7885 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7886 cmp_type = type1;
7887 else
7889 error ("non-floating-point argument to function %qs",
7890 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7891 return error_mark_node;
7894 arg0 = fold_convert (cmp_type, arg0);
7895 arg1 = fold_convert (cmp_type, arg1);
7897 if (unordered_code == UNORDERED_EXPR)
7899 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7900 return omit_two_operands (type, integer_zero_node, arg0, arg1);
7901 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
7904 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
7905 : ordered_code;
7906 return fold (build1 (TRUTH_NOT_EXPR, type,
7907 fold (build2 (code, type, arg0, arg1))));
7910 /* Fold a call to one of the external complex multiply libcalls. */
7912 static tree
7913 fold_builtin_complex_mul (tree type, tree arglist)
7915 tree ar, ai, br, bi;
7917 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7918 REAL_TYPE, VOID_TYPE))
7919 return NULL;
7921 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7922 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7923 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7924 bi = TREE_VALUE (arglist);
7926 return fold_complex_mult_parts (type, ar, ai, br, bi);
7929 /* Fold a call to one of the external complex division libcalls. */
7931 static tree
7932 fold_builtin_complex_div (tree type, tree arglist)
7934 tree ar, ai, br, bi;
7936 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7937 REAL_TYPE, VOID_TYPE))
7938 return NULL;
7940 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7941 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7942 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7943 bi = TREE_VALUE (arglist);
7945 return fold_complex_div_parts (type, ar, ai, br, bi, RDIV_EXPR);
7948 /* Used by constant folding to simplify calls to builtin functions. EXP is
7949 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
7950 result of the function call is ignored. This function returns NULL_TREE
7951 if no simplification was possible. */
7953 static tree
7954 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
7956 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7957 enum built_in_function fcode;
7959 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7960 return targetm.fold_builtin (fndecl, arglist, ignore);
7962 fcode = DECL_FUNCTION_CODE (fndecl);
7963 switch (fcode)
7965 case BUILT_IN_FPUTS:
7966 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
7968 case BUILT_IN_FPUTS_UNLOCKED:
7969 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
7971 case BUILT_IN_STRSTR:
7972 return fold_builtin_strstr (arglist, type);
7974 case BUILT_IN_STRCAT:
7975 return fold_builtin_strcat (arglist);
7977 case BUILT_IN_STRNCAT:
7978 return fold_builtin_strncat (arglist);
7980 case BUILT_IN_STRSPN:
7981 return fold_builtin_strspn (arglist);
7983 case BUILT_IN_STRCSPN:
7984 return fold_builtin_strcspn (arglist);
7986 case BUILT_IN_STRCHR:
7987 case BUILT_IN_INDEX:
7988 return fold_builtin_strchr (arglist, type);
7990 case BUILT_IN_STRRCHR:
7991 case BUILT_IN_RINDEX:
7992 return fold_builtin_strrchr (arglist, type);
7994 case BUILT_IN_STRCPY:
7995 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
7997 case BUILT_IN_STRNCPY:
7998 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8000 case BUILT_IN_STRCMP:
8001 return fold_builtin_strcmp (arglist);
8003 case BUILT_IN_STRNCMP:
8004 return fold_builtin_strncmp (arglist);
8006 case BUILT_IN_STRPBRK:
8007 return fold_builtin_strpbrk (arglist, type);
8009 case BUILT_IN_BCMP:
8010 case BUILT_IN_MEMCMP:
8011 return fold_builtin_memcmp (arglist);
8013 case BUILT_IN_SPRINTF:
8014 return fold_builtin_sprintf (arglist, ignore);
8016 case BUILT_IN_CONSTANT_P:
8018 tree val;
8020 val = fold_builtin_constant_p (arglist);
8021 /* Gimplification will pull the CALL_EXPR for the builtin out of
8022 an if condition. When not optimizing, we'll not CSE it back.
8023 To avoid link error types of regressions, return false now. */
8024 if (!val && !optimize)
8025 val = integer_zero_node;
8027 return val;
8030 case BUILT_IN_EXPECT:
8031 return fold_builtin_expect (arglist);
8033 case BUILT_IN_CLASSIFY_TYPE:
8034 return fold_builtin_classify_type (arglist);
8036 case BUILT_IN_STRLEN:
8037 return fold_builtin_strlen (arglist);
8039 case BUILT_IN_FABS:
8040 case BUILT_IN_FABSF:
8041 case BUILT_IN_FABSL:
8042 return fold_builtin_fabs (arglist, type);
8044 case BUILT_IN_ABS:
8045 case BUILT_IN_LABS:
8046 case BUILT_IN_LLABS:
8047 case BUILT_IN_IMAXABS:
8048 return fold_builtin_abs (arglist, type);
8050 case BUILT_IN_CONJ:
8051 case BUILT_IN_CONJF:
8052 case BUILT_IN_CONJL:
8053 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8054 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
8055 break;
8057 case BUILT_IN_CREAL:
8058 case BUILT_IN_CREALF:
8059 case BUILT_IN_CREALL:
8060 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8061 return non_lvalue (fold (build1 (REALPART_EXPR, type,
8062 TREE_VALUE (arglist))));
8063 break;
8065 case BUILT_IN_CIMAG:
8066 case BUILT_IN_CIMAGF:
8067 case BUILT_IN_CIMAGL:
8068 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8069 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8070 TREE_VALUE (arglist))));
8071 break;
8073 case BUILT_IN_CABS:
8074 case BUILT_IN_CABSF:
8075 case BUILT_IN_CABSL:
8076 return fold_builtin_cabs (arglist, type);
8078 case BUILT_IN_SQRT:
8079 case BUILT_IN_SQRTF:
8080 case BUILT_IN_SQRTL:
8081 return fold_builtin_sqrt (arglist, type);
8083 case BUILT_IN_CBRT:
8084 case BUILT_IN_CBRTF:
8085 case BUILT_IN_CBRTL:
8086 return fold_builtin_cbrt (arglist, type);
8088 case BUILT_IN_SIN:
8089 case BUILT_IN_SINF:
8090 case BUILT_IN_SINL:
8091 return fold_builtin_sin (arglist);
8093 case BUILT_IN_COS:
8094 case BUILT_IN_COSF:
8095 case BUILT_IN_COSL:
8096 return fold_builtin_cos (arglist, type, fndecl);
8098 case BUILT_IN_EXP:
8099 case BUILT_IN_EXPF:
8100 case BUILT_IN_EXPL:
8101 return fold_builtin_exponent (fndecl, arglist, &dconste);
8103 case BUILT_IN_EXP2:
8104 case BUILT_IN_EXP2F:
8105 case BUILT_IN_EXP2L:
8106 return fold_builtin_exponent (fndecl, arglist, &dconst2);
8108 case BUILT_IN_EXP10:
8109 case BUILT_IN_EXP10F:
8110 case BUILT_IN_EXP10L:
8111 case BUILT_IN_POW10:
8112 case BUILT_IN_POW10F:
8113 case BUILT_IN_POW10L:
8114 return fold_builtin_exponent (fndecl, arglist, &dconst10);
8116 case BUILT_IN_LOG:
8117 case BUILT_IN_LOGF:
8118 case BUILT_IN_LOGL:
8119 return fold_builtin_logarithm (fndecl, arglist, &dconste);
8121 case BUILT_IN_LOG2:
8122 case BUILT_IN_LOG2F:
8123 case BUILT_IN_LOG2L:
8124 return fold_builtin_logarithm (fndecl, arglist, &dconst2);
8126 case BUILT_IN_LOG10:
8127 case BUILT_IN_LOG10F:
8128 case BUILT_IN_LOG10L:
8129 return fold_builtin_logarithm (fndecl, arglist, &dconst10);
8131 case BUILT_IN_TAN:
8132 case BUILT_IN_TANF:
8133 case BUILT_IN_TANL:
8134 return fold_builtin_tan (arglist);
8136 case BUILT_IN_ATAN:
8137 case BUILT_IN_ATANF:
8138 case BUILT_IN_ATANL:
8139 return fold_builtin_atan (arglist, type);
8141 case BUILT_IN_POW:
8142 case BUILT_IN_POWF:
8143 case BUILT_IN_POWL:
8144 return fold_builtin_pow (fndecl, arglist, type);
8146 case BUILT_IN_POWI:
8147 case BUILT_IN_POWIF:
8148 case BUILT_IN_POWIL:
8149 return fold_builtin_powi (fndecl, arglist, type);
8151 case BUILT_IN_INF:
8152 case BUILT_IN_INFF:
8153 case BUILT_IN_INFL:
8154 return fold_builtin_inf (type, true);
8156 case BUILT_IN_HUGE_VAL:
8157 case BUILT_IN_HUGE_VALF:
8158 case BUILT_IN_HUGE_VALL:
8159 return fold_builtin_inf (type, false);
8161 case BUILT_IN_NAN:
8162 case BUILT_IN_NANF:
8163 case BUILT_IN_NANL:
8164 return fold_builtin_nan (arglist, type, true);
8166 case BUILT_IN_NANS:
8167 case BUILT_IN_NANSF:
8168 case BUILT_IN_NANSL:
8169 return fold_builtin_nan (arglist, type, false);
8171 case BUILT_IN_FLOOR:
8172 case BUILT_IN_FLOORF:
8173 case BUILT_IN_FLOORL:
8174 return fold_builtin_floor (fndecl, arglist);
8176 case BUILT_IN_CEIL:
8177 case BUILT_IN_CEILF:
8178 case BUILT_IN_CEILL:
8179 return fold_builtin_ceil (fndecl, arglist);
8181 case BUILT_IN_TRUNC:
8182 case BUILT_IN_TRUNCF:
8183 case BUILT_IN_TRUNCL:
8184 return fold_builtin_trunc (fndecl, arglist);
8186 case BUILT_IN_ROUND:
8187 case BUILT_IN_ROUNDF:
8188 case BUILT_IN_ROUNDL:
8189 return fold_builtin_round (fndecl, arglist);
8191 case BUILT_IN_NEARBYINT:
8192 case BUILT_IN_NEARBYINTF:
8193 case BUILT_IN_NEARBYINTL:
8194 case BUILT_IN_RINT:
8195 case BUILT_IN_RINTF:
8196 case BUILT_IN_RINTL:
8197 return fold_trunc_transparent_mathfn (fndecl, arglist);
8199 case BUILT_IN_LROUND:
8200 case BUILT_IN_LROUNDF:
8201 case BUILT_IN_LROUNDL:
8202 case BUILT_IN_LLROUND:
8203 case BUILT_IN_LLROUNDF:
8204 case BUILT_IN_LLROUNDL:
8205 return fold_builtin_lround (fndecl, arglist);
8207 case BUILT_IN_LRINT:
8208 case BUILT_IN_LRINTF:
8209 case BUILT_IN_LRINTL:
8210 case BUILT_IN_LLRINT:
8211 case BUILT_IN_LLRINTF:
8212 case BUILT_IN_LLRINTL:
8213 return fold_fixed_mathfn (fndecl, arglist);
8215 case BUILT_IN_FFS:
8216 case BUILT_IN_FFSL:
8217 case BUILT_IN_FFSLL:
8218 case BUILT_IN_CLZ:
8219 case BUILT_IN_CLZL:
8220 case BUILT_IN_CLZLL:
8221 case BUILT_IN_CTZ:
8222 case BUILT_IN_CTZL:
8223 case BUILT_IN_CTZLL:
8224 case BUILT_IN_POPCOUNT:
8225 case BUILT_IN_POPCOUNTL:
8226 case BUILT_IN_POPCOUNTLL:
8227 case BUILT_IN_PARITY:
8228 case BUILT_IN_PARITYL:
8229 case BUILT_IN_PARITYLL:
8230 return fold_builtin_bitop (fndecl, arglist);
8232 case BUILT_IN_MEMCPY:
8233 return fold_builtin_memcpy (fndecl, arglist);
8235 case BUILT_IN_MEMPCPY:
8236 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8238 case BUILT_IN_MEMMOVE:
8239 return fold_builtin_memmove (arglist, type);
8241 case BUILT_IN_SIGNBIT:
8242 case BUILT_IN_SIGNBITF:
8243 case BUILT_IN_SIGNBITL:
8244 return fold_builtin_signbit (fndecl, arglist);
8246 case BUILT_IN_ISASCII:
8247 return fold_builtin_isascii (arglist);
8249 case BUILT_IN_TOASCII:
8250 return fold_builtin_toascii (arglist);
8252 case BUILT_IN_ISDIGIT:
8253 return fold_builtin_isdigit (arglist);
8255 case BUILT_IN_COPYSIGN:
8256 case BUILT_IN_COPYSIGNF:
8257 case BUILT_IN_COPYSIGNL:
8258 return fold_builtin_copysign (fndecl, arglist, type);
8260 case BUILT_IN_FINITE:
8261 case BUILT_IN_FINITEF:
8262 case BUILT_IN_FINITEL:
8263 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
8265 case BUILT_IN_ISINF:
8266 case BUILT_IN_ISINFF:
8267 case BUILT_IN_ISINFL:
8268 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
8270 case BUILT_IN_ISNAN:
8271 case BUILT_IN_ISNANF:
8272 case BUILT_IN_ISNANL:
8273 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
8275 case BUILT_IN_ISGREATER:
8276 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
8277 case BUILT_IN_ISGREATEREQUAL:
8278 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
8279 case BUILT_IN_ISLESS:
8280 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
8281 case BUILT_IN_ISLESSEQUAL:
8282 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
8283 case BUILT_IN_ISLESSGREATER:
8284 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
8285 case BUILT_IN_ISUNORDERED:
8286 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
8287 NOP_EXPR);
8289 /* We do the folding for va_start in the expander. */
8290 case BUILT_IN_VA_START:
8291 break;
8293 default:
8294 if (fcode >= BUILT_IN_COMPLEX_MUL_MIN
8295 && fcode <= BUILT_IN_COMPLEX_MUL_MAX)
8296 return fold_builtin_complex_mul (type, arglist);
8297 if (fcode >= BUILT_IN_COMPLEX_DIV_MIN
8298 && fcode <= BUILT_IN_COMPLEX_DIV_MAX)
8299 return fold_builtin_complex_div (type, arglist);
8300 break;
8303 return 0;
8306 /* A wrapper function for builtin folding that prevents warnings for
8307 "statement without effect" and the like, caused by removing the
8308 call node earlier than the warning is generated. */
8310 tree
8311 fold_builtin (tree fndecl, tree arglist, bool ignore)
8313 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
8314 if (exp)
8316 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8317 if (CONSTANT_CLASS_P (exp))
8318 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8319 TREE_NO_WARNING (exp) = 1;
8322 return exp;
8325 /* Conveniently construct a function call expression. */
8327 tree
8328 build_function_call_expr (tree fn, tree arglist)
8330 tree call_expr;
8332 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8333 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8334 call_expr, arglist, NULL_TREE);
8335 return fold (call_expr);
8338 /* This function validates the types of a function call argument list
8339 represented as a tree chain of parameters against a specified list
8340 of tree_codes. If the last specifier is a 0, that represents an
8341 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8343 static int
8344 validate_arglist (tree arglist, ...)
8346 enum tree_code code;
8347 int res = 0;
8348 va_list ap;
8350 va_start (ap, arglist);
8354 code = va_arg (ap, enum tree_code);
8355 switch (code)
8357 case 0:
8358 /* This signifies an ellipses, any further arguments are all ok. */
8359 res = 1;
8360 goto end;
8361 case VOID_TYPE:
8362 /* This signifies an endlink, if no arguments remain, return
8363 true, otherwise return false. */
8364 res = arglist == 0;
8365 goto end;
8366 default:
8367 /* If no parameters remain or the parameter's code does not
8368 match the specified code, return false. Otherwise continue
8369 checking any remaining arguments. */
8370 if (arglist == 0)
8371 goto end;
8372 if (code == POINTER_TYPE)
8374 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8375 goto end;
8377 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8378 goto end;
8379 break;
8381 arglist = TREE_CHAIN (arglist);
8383 while (1);
8385 /* We need gotos here since we can only have one VA_CLOSE in a
8386 function. */
8387 end: ;
8388 va_end (ap);
8390 return res;
8393 /* Default target-specific builtin expander that does nothing. */
8396 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8397 rtx target ATTRIBUTE_UNUSED,
8398 rtx subtarget ATTRIBUTE_UNUSED,
8399 enum machine_mode mode ATTRIBUTE_UNUSED,
8400 int ignore ATTRIBUTE_UNUSED)
8402 return NULL_RTX;
8405 /* Returns true is EXP represents data that would potentially reside
8406 in a readonly section. */
8408 static bool
8409 readonly_data_expr (tree exp)
8411 STRIP_NOPS (exp);
8413 if (TREE_CODE (exp) != ADDR_EXPR)
8414 return false;
8416 exp = get_base_address (TREE_OPERAND (exp, 0));
8417 if (!exp)
8418 return false;
8420 /* Make sure we call decl_readonly_section only for trees it
8421 can handle (since it returns true for everything it doesn't
8422 understand). */
8423 if (TREE_CODE (exp) == STRING_CST
8424 || TREE_CODE (exp) == CONSTRUCTOR
8425 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8426 return decl_readonly_section (exp, 0);
8427 else
8428 return false;
8431 /* Simplify a call to the strstr builtin.
8433 Return 0 if no simplification was possible, otherwise return the
8434 simplified form of the call as a tree.
8436 The simplified form may be a constant or other expression which
8437 computes the same value, but in a more efficient manner (including
8438 calls to other builtin functions).
8440 The call may contain arguments which need to be evaluated, but
8441 which are not useful to determine the result of the call. In
8442 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8443 COMPOUND_EXPR will be an argument which must be evaluated.
8444 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8445 COMPOUND_EXPR in the chain will contain the tree for the simplified
8446 form of the builtin function call. */
8448 static tree
8449 fold_builtin_strstr (tree arglist, tree type)
8451 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8452 return 0;
8453 else
8455 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8456 tree fn;
8457 const char *p1, *p2;
8459 p2 = c_getstr (s2);
8460 if (p2 == NULL)
8461 return 0;
8463 p1 = c_getstr (s1);
8464 if (p1 != NULL)
8466 const char *r = strstr (p1, p2);
8467 tree tem;
8469 if (r == NULL)
8470 return build_int_cst (TREE_TYPE (s1), 0);
8472 /* Return an offset into the constant string argument. */
8473 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8474 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8475 return fold_convert (type, tem);
8478 if (p2[0] == '\0')
8479 return s1;
8481 if (p2[1] != '\0')
8482 return 0;
8484 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8485 if (!fn)
8486 return 0;
8488 /* New argument list transforming strstr(s1, s2) to
8489 strchr(s1, s2[0]). */
8490 arglist = build_tree_list (NULL_TREE,
8491 build_int_cst (NULL_TREE, p2[0]));
8492 arglist = tree_cons (NULL_TREE, s1, arglist);
8493 return build_function_call_expr (fn, arglist);
8497 /* Simplify a call to the strchr builtin.
8499 Return 0 if no simplification was possible, otherwise return the
8500 simplified form of the call as a tree.
8502 The simplified form may be a constant or other expression which
8503 computes the same value, but in a more efficient manner (including
8504 calls to other builtin functions).
8506 The call may contain arguments which need to be evaluated, but
8507 which are not useful to determine the result of the call. In
8508 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8509 COMPOUND_EXPR will be an argument which must be evaluated.
8510 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8511 COMPOUND_EXPR in the chain will contain the tree for the simplified
8512 form of the builtin function call. */
8514 static tree
8515 fold_builtin_strchr (tree arglist, tree type)
8517 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8518 return 0;
8519 else
8521 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8522 const char *p1;
8524 if (TREE_CODE (s2) != INTEGER_CST)
8525 return 0;
8527 p1 = c_getstr (s1);
8528 if (p1 != NULL)
8530 char c;
8531 const char *r;
8532 tree tem;
8534 if (target_char_cast (s2, &c))
8535 return 0;
8537 r = strchr (p1, c);
8539 if (r == NULL)
8540 return build_int_cst (TREE_TYPE (s1), 0);
8542 /* Return an offset into the constant string argument. */
8543 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8544 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8545 return fold_convert (type, tem);
8547 return 0;
8551 /* Simplify a call to the strrchr builtin.
8553 Return 0 if no simplification was possible, otherwise return the
8554 simplified form of the call as a tree.
8556 The simplified form may be a constant or other expression which
8557 computes the same value, but in a more efficient manner (including
8558 calls to other builtin functions).
8560 The call may contain arguments which need to be evaluated, but
8561 which are not useful to determine the result of the call. In
8562 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8563 COMPOUND_EXPR will be an argument which must be evaluated.
8564 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8565 COMPOUND_EXPR in the chain will contain the tree for the simplified
8566 form of the builtin function call. */
8568 static tree
8569 fold_builtin_strrchr (tree arglist, tree type)
8571 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8572 return 0;
8573 else
8575 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8576 tree fn;
8577 const char *p1;
8579 if (TREE_CODE (s2) != INTEGER_CST)
8580 return 0;
8582 p1 = c_getstr (s1);
8583 if (p1 != NULL)
8585 char c;
8586 const char *r;
8587 tree tem;
8589 if (target_char_cast (s2, &c))
8590 return 0;
8592 r = strrchr (p1, c);
8594 if (r == NULL)
8595 return build_int_cst (TREE_TYPE (s1), 0);
8597 /* Return an offset into the constant string argument. */
8598 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8599 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8600 return fold_convert (type, tem);
8603 if (! integer_zerop (s2))
8604 return 0;
8606 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8607 if (!fn)
8608 return 0;
8610 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8611 return build_function_call_expr (fn, arglist);
8615 /* Simplify a call to the strpbrk builtin.
8617 Return 0 if no simplification was possible, otherwise return the
8618 simplified form of the call as a tree.
8620 The simplified form may be a constant or other expression which
8621 computes the same value, but in a more efficient manner (including
8622 calls to other builtin functions).
8624 The call may contain arguments which need to be evaluated, but
8625 which are not useful to determine the result of the call. In
8626 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8627 COMPOUND_EXPR will be an argument which must be evaluated.
8628 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8629 COMPOUND_EXPR in the chain will contain the tree for the simplified
8630 form of the builtin function call. */
8632 static tree
8633 fold_builtin_strpbrk (tree arglist, tree type)
8635 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8636 return 0;
8637 else
8639 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8640 tree fn;
8641 const char *p1, *p2;
8643 p2 = c_getstr (s2);
8644 if (p2 == NULL)
8645 return 0;
8647 p1 = c_getstr (s1);
8648 if (p1 != NULL)
8650 const char *r = strpbrk (p1, p2);
8651 tree tem;
8653 if (r == NULL)
8654 return build_int_cst (TREE_TYPE (s1), 0);
8656 /* Return an offset into the constant string argument. */
8657 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8658 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8659 return fold_convert (type, tem);
8662 if (p2[0] == '\0')
8663 /* strpbrk(x, "") == NULL.
8664 Evaluate and ignore s1 in case it had side-effects. */
8665 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8667 if (p2[1] != '\0')
8668 return 0; /* Really call strpbrk. */
8670 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8671 if (!fn)
8672 return 0;
8674 /* New argument list transforming strpbrk(s1, s2) to
8675 strchr(s1, s2[0]). */
8676 arglist = build_tree_list (NULL_TREE,
8677 build_int_cst (NULL_TREE, p2[0]));
8678 arglist = tree_cons (NULL_TREE, s1, arglist);
8679 return build_function_call_expr (fn, arglist);
8683 /* Simplify a call to the strcat builtin.
8685 Return 0 if no simplification was possible, otherwise return the
8686 simplified form of the call as a tree.
8688 The simplified form may be a constant or other expression which
8689 computes the same value, but in a more efficient manner (including
8690 calls to other builtin functions).
8692 The call may contain arguments which need to be evaluated, but
8693 which are not useful to determine the result of the call. In
8694 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8695 COMPOUND_EXPR will be an argument which must be evaluated.
8696 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8697 COMPOUND_EXPR in the chain will contain the tree for the simplified
8698 form of the builtin function call. */
8700 static tree
8701 fold_builtin_strcat (tree arglist)
8703 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8704 return 0;
8705 else
8707 tree dst = TREE_VALUE (arglist),
8708 src = TREE_VALUE (TREE_CHAIN (arglist));
8709 const char *p = c_getstr (src);
8711 /* If the string length is zero, return the dst parameter. */
8712 if (p && *p == '\0')
8713 return dst;
8715 return 0;
8719 /* Simplify a call to the strncat builtin.
8721 Return 0 if no simplification was possible, otherwise return the
8722 simplified form of the call as a tree.
8724 The simplified form may be a constant or other expression which
8725 computes the same value, but in a more efficient manner (including
8726 calls to other builtin functions).
8728 The call may contain arguments which need to be evaluated, but
8729 which are not useful to determine the result of the call. In
8730 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8731 COMPOUND_EXPR will be an argument which must be evaluated.
8732 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8733 COMPOUND_EXPR in the chain will contain the tree for the simplified
8734 form of the builtin function call. */
8736 static tree
8737 fold_builtin_strncat (tree arglist)
8739 if (!validate_arglist (arglist,
8740 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8741 return 0;
8742 else
8744 tree dst = TREE_VALUE (arglist);
8745 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8746 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8747 const char *p = c_getstr (src);
8749 /* If the requested length is zero, or the src parameter string
8750 length is zero, return the dst parameter. */
8751 if (integer_zerop (len) || (p && *p == '\0'))
8752 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8754 /* If the requested len is greater than or equal to the string
8755 length, call strcat. */
8756 if (TREE_CODE (len) == INTEGER_CST && p
8757 && compare_tree_int (len, strlen (p)) >= 0)
8759 tree newarglist
8760 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8761 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8763 /* If the replacement _DECL isn't initialized, don't do the
8764 transformation. */
8765 if (!fn)
8766 return 0;
8768 return build_function_call_expr (fn, newarglist);
8770 return 0;
8774 /* Simplify a call to the strspn builtin.
8776 Return 0 if no simplification was possible, otherwise return the
8777 simplified form of the call as a tree.
8779 The simplified form may be a constant or other expression which
8780 computes the same value, but in a more efficient manner (including
8781 calls to other builtin functions).
8783 The call may contain arguments which need to be evaluated, but
8784 which are not useful to determine the result of the call. In
8785 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8786 COMPOUND_EXPR will be an argument which must be evaluated.
8787 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8788 COMPOUND_EXPR in the chain will contain the tree for the simplified
8789 form of the builtin function call. */
8791 static tree
8792 fold_builtin_strspn (tree arglist)
8794 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8795 return 0;
8796 else
8798 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8799 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8801 /* If both arguments are constants, evaluate at compile-time. */
8802 if (p1 && p2)
8804 const size_t r = strspn (p1, p2);
8805 return size_int (r);
8808 /* If either argument is "", return 0. */
8809 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8810 /* Evaluate and ignore both arguments in case either one has
8811 side-effects. */
8812 return omit_two_operands (integer_type_node, integer_zero_node,
8813 s1, s2);
8814 return 0;
8818 /* Simplify a call to the strcspn builtin.
8820 Return 0 if no simplification was possible, otherwise return the
8821 simplified form of the call as a tree.
8823 The simplified form may be a constant or other expression which
8824 computes the same value, but in a more efficient manner (including
8825 calls to other builtin functions).
8827 The call may contain arguments which need to be evaluated, but
8828 which are not useful to determine the result of the call. In
8829 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8830 COMPOUND_EXPR will be an argument which must be evaluated.
8831 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8832 COMPOUND_EXPR in the chain will contain the tree for the simplified
8833 form of the builtin function call. */
8835 static tree
8836 fold_builtin_strcspn (tree arglist)
8838 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8839 return 0;
8840 else
8842 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8843 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8845 /* If both arguments are constants, evaluate at compile-time. */
8846 if (p1 && p2)
8848 const size_t r = strcspn (p1, p2);
8849 return size_int (r);
8852 /* If the first argument is "", return 0. */
8853 if (p1 && *p1 == '\0')
8855 /* Evaluate and ignore argument s2 in case it has
8856 side-effects. */
8857 return omit_one_operand (integer_type_node,
8858 integer_zero_node, s2);
8861 /* If the second argument is "", return __builtin_strlen(s1). */
8862 if (p2 && *p2 == '\0')
8864 tree newarglist = build_tree_list (NULL_TREE, s1),
8865 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8867 /* If the replacement _DECL isn't initialized, don't do the
8868 transformation. */
8869 if (!fn)
8870 return 0;
8872 return build_function_call_expr (fn, newarglist);
8874 return 0;
8878 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
8879 by the builtin will be ignored. UNLOCKED is true is true if this
8880 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
8881 the known length of the string. Return NULL_TREE if no simplification
8882 was possible. */
8884 tree
8885 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
8887 tree fn;
8888 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8889 : implicit_built_in_decls[BUILT_IN_FPUTC];
8890 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8891 : implicit_built_in_decls[BUILT_IN_FWRITE];
8893 /* If the return value is used, or the replacement _DECL isn't
8894 initialized, don't do the transformation. */
8895 if (!ignore || !fn_fputc || !fn_fwrite)
8896 return 0;
8898 /* Verify the arguments in the original call. */
8899 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8900 return 0;
8902 if (! len)
8903 len = c_strlen (TREE_VALUE (arglist), 0);
8905 /* Get the length of the string passed to fputs. If the length
8906 can't be determined, punt. */
8907 if (!len
8908 || TREE_CODE (len) != INTEGER_CST)
8909 return 0;
8911 switch (compare_tree_int (len, 1))
8913 case -1: /* length is 0, delete the call entirely . */
8914 return omit_one_operand (integer_type_node, integer_zero_node,
8915 TREE_VALUE (TREE_CHAIN (arglist)));
8917 case 0: /* length is 1, call fputc. */
8919 const char *p = c_getstr (TREE_VALUE (arglist));
8921 if (p != NULL)
8923 /* New argument list transforming fputs(string, stream) to
8924 fputc(string[0], stream). */
8925 arglist = build_tree_list (NULL_TREE,
8926 TREE_VALUE (TREE_CHAIN (arglist)));
8927 arglist = tree_cons (NULL_TREE,
8928 build_int_cst (NULL_TREE, p[0]),
8929 arglist);
8930 fn = fn_fputc;
8931 break;
8934 /* FALLTHROUGH */
8935 case 1: /* length is greater than 1, call fwrite. */
8937 tree string_arg;
8939 /* If optimizing for size keep fputs. */
8940 if (optimize_size)
8941 return 0;
8942 string_arg = TREE_VALUE (arglist);
8943 /* New argument list transforming fputs(string, stream) to
8944 fwrite(string, 1, len, stream). */
8945 arglist = build_tree_list (NULL_TREE,
8946 TREE_VALUE (TREE_CHAIN (arglist)));
8947 arglist = tree_cons (NULL_TREE, len, arglist);
8948 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8949 arglist = tree_cons (NULL_TREE, string_arg, arglist);
8950 fn = fn_fwrite;
8951 break;
8953 default:
8954 gcc_unreachable ();
8957 /* These optimizations are only performed when the result is ignored,
8958 hence there's no need to cast the result to integer_type_node. */
8959 return build_function_call_expr (fn, arglist);
8962 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8963 produced. False otherwise. This is done so that we don't output the error
8964 or warning twice or three times. */
8965 bool
8966 fold_builtin_next_arg (tree arglist)
8968 tree fntype = TREE_TYPE (current_function_decl);
8970 if (TYPE_ARG_TYPES (fntype) == 0
8971 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8972 == void_type_node))
8974 error ("%<va_start%> used in function with fixed args");
8975 return true;
8977 else if (!arglist)
8979 /* Evidently an out of date version of <stdarg.h>; can't validate
8980 va_start's second argument, but can still work as intended. */
8981 warning ("%<__builtin_next_arg%> called without an argument");
8982 return true;
8984 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8985 when we checked the arguments and if needed issued a warning. */
8986 else if (!TREE_CHAIN (arglist)
8987 || !integer_zerop (TREE_VALUE (arglist))
8988 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
8989 || TREE_CHAIN (TREE_CHAIN (arglist)))
8991 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8992 tree arg = TREE_VALUE (arglist);
8994 if (TREE_CHAIN (arglist))
8996 error ("%<va_start%> used with too many arguments");
8997 return true;
9000 /* Strip off all nops for the sake of the comparison. This
9001 is not quite the same as STRIP_NOPS. It does more.
9002 We must also strip off INDIRECT_EXPR for C++ reference
9003 parameters. */
9004 while (TREE_CODE (arg) == NOP_EXPR
9005 || TREE_CODE (arg) == CONVERT_EXPR
9006 || TREE_CODE (arg) == NON_LVALUE_EXPR
9007 || TREE_CODE (arg) == INDIRECT_REF)
9008 arg = TREE_OPERAND (arg, 0);
9009 if (arg != last_parm)
9011 /* FIXME: Sometimes with the tree optimizers we can get the
9012 not the last argument even though the user used the last
9013 argument. We just warn and set the arg to be the last
9014 argument so that we will get wrong-code because of
9015 it. */
9016 warning ("second parameter of %<va_start%> not last named argument");
9018 /* We want to verify the second parameter just once before the tree
9019 optimizers are run and then avoid keeping it in the tree,
9020 as otherwise we could warn even for correct code like:
9021 void foo (int i, ...)
9022 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9023 TREE_VALUE (arglist) = integer_zero_node;
9024 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9026 return false;
9030 /* Simplify a call to the sprintf builtin.
9032 Return 0 if no simplification was possible, otherwise return the
9033 simplified form of the call as a tree. If IGNORED is true, it means that
9034 the caller does not use the returned value of the function. */
9036 static tree
9037 fold_builtin_sprintf (tree arglist, int ignored)
9039 tree call, retval, dest, fmt;
9040 const char *fmt_str = NULL;
9042 /* Verify the required arguments in the original call. We deal with two
9043 types of sprintf() calls: 'sprintf (str, fmt)' and
9044 'sprintf (dest, "%s", orig)'. */
9045 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9046 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9047 VOID_TYPE))
9048 return NULL_TREE;
9050 /* Get the destination string and the format specifier. */
9051 dest = TREE_VALUE (arglist);
9052 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9054 /* Check whether the format is a literal string constant. */
9055 fmt_str = c_getstr (fmt);
9056 if (fmt_str == NULL)
9057 return NULL_TREE;
9059 call = NULL_TREE;
9060 retval = NULL_TREE;
9062 /* If the format doesn't contain % args or %%, use strcpy. */
9063 if (strchr (fmt_str, '%') == NULL)
9065 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9067 if (!fn)
9068 return NULL_TREE;
9070 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9071 'format' is known to contain no % formats. */
9072 arglist = build_tree_list (NULL_TREE, fmt);
9073 arglist = tree_cons (NULL_TREE, dest, arglist);
9074 call = build_function_call_expr (fn, arglist);
9075 if (!ignored)
9076 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9079 /* If the format is "%s", use strcpy if the result isn't used. */
9080 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9082 tree fn, orig;
9083 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9085 if (!fn)
9086 return NULL_TREE;
9088 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9089 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9090 arglist = build_tree_list (NULL_TREE, orig);
9091 arglist = tree_cons (NULL_TREE, dest, arglist);
9092 if (!ignored)
9094 retval = c_strlen (orig, 1);
9095 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9096 return NULL_TREE;
9098 call = build_function_call_expr (fn, arglist);
9101 if (call && retval)
9103 retval = convert
9104 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9105 retval);
9106 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
9108 else
9109 return call;