2005-03-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / builtins.c
blob23bb4298884ae96eb87ea5ef7bf4b13896134911
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_SUM);
983 rtx mem;
985 addr = convert_memory_address (Pmode, addr);
987 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
989 /* Get an expression we can use to find the attributes to assign to MEM.
990 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
991 we can. First remove any nops. */
992 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
993 || TREE_CODE (exp) == NON_LVALUE_EXPR)
994 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
995 exp = TREE_OPERAND (exp, 0);
997 if (TREE_CODE (exp) == ADDR_EXPR)
998 exp = TREE_OPERAND (exp, 0);
999 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1000 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1001 else
1002 exp = NULL;
1004 /* Honor attributes derived from exp, except for the alias set
1005 (as builtin stringops may alias with anything) and the size
1006 (as stringops may access multiple array elements). */
1007 if (exp)
1009 set_mem_attributes (mem, exp, 0);
1010 set_mem_alias_set (mem, 0);
1011 set_mem_size (mem, NULL_RTX);
1014 return mem;
1017 /* Built-in functions to perform an untyped call and return. */
1019 /* For each register that may be used for calling a function, this
1020 gives a mode used to copy the register's value. VOIDmode indicates
1021 the register is not used for calling a function. If the machine
1022 has register windows, this gives only the outbound registers.
1023 INCOMING_REGNO gives the corresponding inbound register. */
1024 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1026 /* For each register that may be used for returning values, this gives
1027 a mode used to copy the register's value. VOIDmode indicates the
1028 register is not used for returning values. If the machine has
1029 register windows, this gives only the outbound registers.
1030 INCOMING_REGNO gives the corresponding inbound register. */
1031 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1033 /* For each register that may be used for calling a function, this
1034 gives the offset of that register into the block returned by
1035 __builtin_apply_args. 0 indicates that the register is not
1036 used for calling a function. */
1037 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1039 /* Return the size required for the block returned by __builtin_apply_args,
1040 and initialize apply_args_mode. */
1042 static int
1043 apply_args_size (void)
1045 static int size = -1;
1046 int align;
1047 unsigned int regno;
1048 enum machine_mode mode;
1050 /* The values computed by this function never change. */
1051 if (size < 0)
1053 /* The first value is the incoming arg-pointer. */
1054 size = GET_MODE_SIZE (Pmode);
1056 /* The second value is the structure value address unless this is
1057 passed as an "invisible" first argument. */
1058 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1059 size += GET_MODE_SIZE (Pmode);
1061 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1062 if (FUNCTION_ARG_REGNO_P (regno))
1064 mode = reg_raw_mode[regno];
1066 gcc_assert (mode != VOIDmode);
1068 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1069 if (size % align != 0)
1070 size = CEIL (size, align) * align;
1071 apply_args_reg_offset[regno] = size;
1072 size += GET_MODE_SIZE (mode);
1073 apply_args_mode[regno] = mode;
1075 else
1077 apply_args_mode[regno] = VOIDmode;
1078 apply_args_reg_offset[regno] = 0;
1081 return size;
1084 /* Return the size required for the block returned by __builtin_apply,
1085 and initialize apply_result_mode. */
1087 static int
1088 apply_result_size (void)
1090 static int size = -1;
1091 int align, regno;
1092 enum machine_mode mode;
1094 /* The values computed by this function never change. */
1095 if (size < 0)
1097 size = 0;
1099 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1100 if (FUNCTION_VALUE_REGNO_P (regno))
1102 mode = reg_raw_mode[regno];
1104 gcc_assert (mode != VOIDmode);
1106 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1107 if (size % align != 0)
1108 size = CEIL (size, align) * align;
1109 size += GET_MODE_SIZE (mode);
1110 apply_result_mode[regno] = mode;
1112 else
1113 apply_result_mode[regno] = VOIDmode;
1115 /* Allow targets that use untyped_call and untyped_return to override
1116 the size so that machine-specific information can be stored here. */
1117 #ifdef APPLY_RESULT_SIZE
1118 size = APPLY_RESULT_SIZE;
1119 #endif
1121 return size;
1124 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1125 /* Create a vector describing the result block RESULT. If SAVEP is true,
1126 the result block is used to save the values; otherwise it is used to
1127 restore the values. */
1129 static rtx
1130 result_vector (int savep, rtx result)
1132 int regno, size, align, nelts;
1133 enum machine_mode mode;
1134 rtx reg, mem;
1135 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1137 size = nelts = 0;
1138 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1139 if ((mode = apply_result_mode[regno]) != VOIDmode)
1141 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1142 if (size % align != 0)
1143 size = CEIL (size, align) * align;
1144 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1145 mem = adjust_address (result, mode, size);
1146 savevec[nelts++] = (savep
1147 ? gen_rtx_SET (VOIDmode, mem, reg)
1148 : gen_rtx_SET (VOIDmode, reg, mem));
1149 size += GET_MODE_SIZE (mode);
1151 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1153 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1155 /* Save the state required to perform an untyped call with the same
1156 arguments as were passed to the current function. */
1158 static rtx
1159 expand_builtin_apply_args_1 (void)
1161 rtx registers, tem;
1162 int size, align, regno;
1163 enum machine_mode mode;
1164 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1166 /* Create a block where the arg-pointer, structure value address,
1167 and argument registers can be saved. */
1168 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1170 /* Walk past the arg-pointer and structure value address. */
1171 size = GET_MODE_SIZE (Pmode);
1172 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1173 size += GET_MODE_SIZE (Pmode);
1175 /* Save each register used in calling a function to the block. */
1176 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1177 if ((mode = apply_args_mode[regno]) != VOIDmode)
1179 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1180 if (size % align != 0)
1181 size = CEIL (size, align) * align;
1183 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1185 emit_move_insn (adjust_address (registers, mode, size), tem);
1186 size += GET_MODE_SIZE (mode);
1189 /* Save the arg pointer to the block. */
1190 tem = copy_to_reg (virtual_incoming_args_rtx);
1191 #ifdef STACK_GROWS_DOWNWARD
1192 /* We need the pointer as the caller actually passed them to us, not
1193 as we might have pretended they were passed. Make sure it's a valid
1194 operand, as emit_move_insn isn't expected to handle a PLUS. */
1196 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1197 NULL_RTX);
1198 #endif
1199 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1201 size = GET_MODE_SIZE (Pmode);
1203 /* Save the structure value address unless this is passed as an
1204 "invisible" first argument. */
1205 if (struct_incoming_value)
1207 emit_move_insn (adjust_address (registers, Pmode, size),
1208 copy_to_reg (struct_incoming_value));
1209 size += GET_MODE_SIZE (Pmode);
1212 /* Return the address of the block. */
1213 return copy_addr_to_reg (XEXP (registers, 0));
1216 /* __builtin_apply_args returns block of memory allocated on
1217 the stack into which is stored the arg pointer, structure
1218 value address, static chain, and all the registers that might
1219 possibly be used in performing a function call. The code is
1220 moved to the start of the function so the incoming values are
1221 saved. */
1223 static rtx
1224 expand_builtin_apply_args (void)
1226 /* Don't do __builtin_apply_args more than once in a function.
1227 Save the result of the first call and reuse it. */
1228 if (apply_args_value != 0)
1229 return apply_args_value;
1231 /* When this function is called, it means that registers must be
1232 saved on entry to this function. So we migrate the
1233 call to the first insn of this function. */
1234 rtx temp;
1235 rtx seq;
1237 start_sequence ();
1238 temp = expand_builtin_apply_args_1 ();
1239 seq = get_insns ();
1240 end_sequence ();
1242 apply_args_value = temp;
1244 /* Put the insns after the NOTE that starts the function.
1245 If this is inside a start_sequence, make the outer-level insn
1246 chain current, so the code is placed at the start of the
1247 function. */
1248 push_topmost_sequence ();
1249 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1250 pop_topmost_sequence ();
1251 return temp;
1255 /* Perform an untyped call and save the state required to perform an
1256 untyped return of whatever value was returned by the given function. */
1258 static rtx
1259 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1261 int size, align, regno;
1262 enum machine_mode mode;
1263 rtx incoming_args, result, reg, dest, src, call_insn;
1264 rtx old_stack_level = 0;
1265 rtx call_fusage = 0;
1266 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1268 arguments = convert_memory_address (Pmode, arguments);
1270 /* Create a block where the return registers can be saved. */
1271 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1273 /* Fetch the arg pointer from the ARGUMENTS block. */
1274 incoming_args = gen_reg_rtx (Pmode);
1275 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1276 #ifndef STACK_GROWS_DOWNWARD
1277 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1278 incoming_args, 0, OPTAB_LIB_WIDEN);
1279 #endif
1281 /* Push a new argument block and copy the arguments. Do not allow
1282 the (potential) memcpy call below to interfere with our stack
1283 manipulations. */
1284 do_pending_stack_adjust ();
1285 NO_DEFER_POP;
1287 /* Save the stack with nonlocal if available. */
1288 #ifdef HAVE_save_stack_nonlocal
1289 if (HAVE_save_stack_nonlocal)
1290 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1291 else
1292 #endif
1293 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1295 /* Allocate a block of memory onto the stack and copy the memory
1296 arguments to the outgoing arguments address. */
1297 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1298 dest = virtual_outgoing_args_rtx;
1299 #ifndef STACK_GROWS_DOWNWARD
1300 if (GET_CODE (argsize) == CONST_INT)
1301 dest = plus_constant (dest, -INTVAL (argsize));
1302 else
1303 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1304 #endif
1305 dest = gen_rtx_MEM (BLKmode, dest);
1306 set_mem_align (dest, PARM_BOUNDARY);
1307 src = gen_rtx_MEM (BLKmode, incoming_args);
1308 set_mem_align (src, PARM_BOUNDARY);
1309 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1311 /* Refer to the argument block. */
1312 apply_args_size ();
1313 arguments = gen_rtx_MEM (BLKmode, arguments);
1314 set_mem_align (arguments, PARM_BOUNDARY);
1316 /* Walk past the arg-pointer and structure value address. */
1317 size = GET_MODE_SIZE (Pmode);
1318 if (struct_value)
1319 size += GET_MODE_SIZE (Pmode);
1321 /* Restore each of the registers previously saved. Make USE insns
1322 for each of these registers for use in making the call. */
1323 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1324 if ((mode = apply_args_mode[regno]) != VOIDmode)
1326 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1327 if (size % align != 0)
1328 size = CEIL (size, align) * align;
1329 reg = gen_rtx_REG (mode, regno);
1330 emit_move_insn (reg, adjust_address (arguments, mode, size));
1331 use_reg (&call_fusage, reg);
1332 size += GET_MODE_SIZE (mode);
1335 /* Restore the structure value address unless this is passed as an
1336 "invisible" first argument. */
1337 size = GET_MODE_SIZE (Pmode);
1338 if (struct_value)
1340 rtx value = gen_reg_rtx (Pmode);
1341 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1342 emit_move_insn (struct_value, value);
1343 if (REG_P (struct_value))
1344 use_reg (&call_fusage, struct_value);
1345 size += GET_MODE_SIZE (Pmode);
1348 /* All arguments and registers used for the call are set up by now! */
1349 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1351 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1352 and we don't want to load it into a register as an optimization,
1353 because prepare_call_address already did it if it should be done. */
1354 if (GET_CODE (function) != SYMBOL_REF)
1355 function = memory_address (FUNCTION_MODE, function);
1357 /* Generate the actual call instruction and save the return value. */
1358 #ifdef HAVE_untyped_call
1359 if (HAVE_untyped_call)
1360 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1361 result, result_vector (1, result)));
1362 else
1363 #endif
1364 #ifdef HAVE_call_value
1365 if (HAVE_call_value)
1367 rtx valreg = 0;
1369 /* Locate the unique return register. It is not possible to
1370 express a call that sets more than one return register using
1371 call_value; use untyped_call for that. In fact, untyped_call
1372 only needs to save the return registers in the given block. */
1373 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1374 if ((mode = apply_result_mode[regno]) != VOIDmode)
1376 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1378 valreg = gen_rtx_REG (mode, regno);
1381 emit_call_insn (GEN_CALL_VALUE (valreg,
1382 gen_rtx_MEM (FUNCTION_MODE, function),
1383 const0_rtx, NULL_RTX, const0_rtx));
1385 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1387 else
1388 #endif
1389 gcc_unreachable ();
1391 /* Find the CALL insn we just emitted, and attach the register usage
1392 information. */
1393 call_insn = last_call_insn ();
1394 add_function_usage_to (call_insn, call_fusage);
1396 /* Restore the stack. */
1397 #ifdef HAVE_save_stack_nonlocal
1398 if (HAVE_save_stack_nonlocal)
1399 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1400 else
1401 #endif
1402 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1404 OK_DEFER_POP;
1406 /* Return the address of the result block. */
1407 result = copy_addr_to_reg (XEXP (result, 0));
1408 return convert_memory_address (ptr_mode, result);
1411 /* Perform an untyped return. */
1413 static void
1414 expand_builtin_return (rtx result)
1416 int size, align, regno;
1417 enum machine_mode mode;
1418 rtx reg;
1419 rtx call_fusage = 0;
1421 result = convert_memory_address (Pmode, result);
1423 apply_result_size ();
1424 result = gen_rtx_MEM (BLKmode, result);
1426 #ifdef HAVE_untyped_return
1427 if (HAVE_untyped_return)
1429 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1430 emit_barrier ();
1431 return;
1433 #endif
1435 /* Restore the return value and note that each value is used. */
1436 size = 0;
1437 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1438 if ((mode = apply_result_mode[regno]) != VOIDmode)
1440 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1441 if (size % align != 0)
1442 size = CEIL (size, align) * align;
1443 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1444 emit_move_insn (reg, adjust_address (result, mode, size));
1446 push_to_sequence (call_fusage);
1447 emit_insn (gen_rtx_USE (VOIDmode, reg));
1448 call_fusage = get_insns ();
1449 end_sequence ();
1450 size += GET_MODE_SIZE (mode);
1453 /* Put the USE insns before the return. */
1454 emit_insn (call_fusage);
1456 /* Return whatever values was restored by jumping directly to the end
1457 of the function. */
1458 expand_naked_return ();
1461 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1463 static enum type_class
1464 type_to_class (tree type)
1466 switch (TREE_CODE (type))
1468 case VOID_TYPE: return void_type_class;
1469 case INTEGER_TYPE: return integer_type_class;
1470 case CHAR_TYPE: return char_type_class;
1471 case ENUMERAL_TYPE: return enumeral_type_class;
1472 case BOOLEAN_TYPE: return boolean_type_class;
1473 case POINTER_TYPE: return pointer_type_class;
1474 case REFERENCE_TYPE: return reference_type_class;
1475 case OFFSET_TYPE: return offset_type_class;
1476 case REAL_TYPE: return real_type_class;
1477 case COMPLEX_TYPE: return complex_type_class;
1478 case FUNCTION_TYPE: return function_type_class;
1479 case METHOD_TYPE: return method_type_class;
1480 case RECORD_TYPE: return record_type_class;
1481 case UNION_TYPE:
1482 case QUAL_UNION_TYPE: return union_type_class;
1483 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1484 ? string_type_class : array_type_class);
1485 case FILE_TYPE: return file_type_class;
1486 case LANG_TYPE: return lang_type_class;
1487 default: return no_type_class;
1491 /* Expand a call to __builtin_classify_type with arguments found in
1492 ARGLIST. */
1494 static rtx
1495 expand_builtin_classify_type (tree arglist)
1497 if (arglist != 0)
1498 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1499 return GEN_INT (no_type_class);
1502 /* This helper macro, meant to be used in mathfn_built_in below,
1503 determines which among a set of three builtin math functions is
1504 appropriate for a given type mode. The `F' and `L' cases are
1505 automatically generated from the `double' case. */
1506 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1507 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1508 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1509 fcodel = BUILT_IN_MATHFN##L ; break;
1511 /* Return mathematic function equivalent to FN but operating directly
1512 on TYPE, if available. If we can't do the conversion, return zero. */
1513 tree
1514 mathfn_built_in (tree type, enum built_in_function fn)
1516 enum built_in_function fcode, fcodef, fcodel;
1518 switch (fn)
1520 CASE_MATHFN (BUILT_IN_ACOS)
1521 CASE_MATHFN (BUILT_IN_ACOSH)
1522 CASE_MATHFN (BUILT_IN_ASIN)
1523 CASE_MATHFN (BUILT_IN_ASINH)
1524 CASE_MATHFN (BUILT_IN_ATAN)
1525 CASE_MATHFN (BUILT_IN_ATAN2)
1526 CASE_MATHFN (BUILT_IN_ATANH)
1527 CASE_MATHFN (BUILT_IN_CBRT)
1528 CASE_MATHFN (BUILT_IN_CEIL)
1529 CASE_MATHFN (BUILT_IN_COPYSIGN)
1530 CASE_MATHFN (BUILT_IN_COS)
1531 CASE_MATHFN (BUILT_IN_COSH)
1532 CASE_MATHFN (BUILT_IN_DREM)
1533 CASE_MATHFN (BUILT_IN_ERF)
1534 CASE_MATHFN (BUILT_IN_ERFC)
1535 CASE_MATHFN (BUILT_IN_EXP)
1536 CASE_MATHFN (BUILT_IN_EXP10)
1537 CASE_MATHFN (BUILT_IN_EXP2)
1538 CASE_MATHFN (BUILT_IN_EXPM1)
1539 CASE_MATHFN (BUILT_IN_FABS)
1540 CASE_MATHFN (BUILT_IN_FDIM)
1541 CASE_MATHFN (BUILT_IN_FLOOR)
1542 CASE_MATHFN (BUILT_IN_FMA)
1543 CASE_MATHFN (BUILT_IN_FMAX)
1544 CASE_MATHFN (BUILT_IN_FMIN)
1545 CASE_MATHFN (BUILT_IN_FMOD)
1546 CASE_MATHFN (BUILT_IN_FREXP)
1547 CASE_MATHFN (BUILT_IN_GAMMA)
1548 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1549 CASE_MATHFN (BUILT_IN_HYPOT)
1550 CASE_MATHFN (BUILT_IN_ILOGB)
1551 CASE_MATHFN (BUILT_IN_INF)
1552 CASE_MATHFN (BUILT_IN_J0)
1553 CASE_MATHFN (BUILT_IN_J1)
1554 CASE_MATHFN (BUILT_IN_JN)
1555 CASE_MATHFN (BUILT_IN_LDEXP)
1556 CASE_MATHFN (BUILT_IN_LGAMMA)
1557 CASE_MATHFN (BUILT_IN_LLRINT)
1558 CASE_MATHFN (BUILT_IN_LLROUND)
1559 CASE_MATHFN (BUILT_IN_LOG)
1560 CASE_MATHFN (BUILT_IN_LOG10)
1561 CASE_MATHFN (BUILT_IN_LOG1P)
1562 CASE_MATHFN (BUILT_IN_LOG2)
1563 CASE_MATHFN (BUILT_IN_LOGB)
1564 CASE_MATHFN (BUILT_IN_LRINT)
1565 CASE_MATHFN (BUILT_IN_LROUND)
1566 CASE_MATHFN (BUILT_IN_MODF)
1567 CASE_MATHFN (BUILT_IN_NAN)
1568 CASE_MATHFN (BUILT_IN_NANS)
1569 CASE_MATHFN (BUILT_IN_NEARBYINT)
1570 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1571 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1572 CASE_MATHFN (BUILT_IN_POW)
1573 CASE_MATHFN (BUILT_IN_POWI)
1574 CASE_MATHFN (BUILT_IN_POW10)
1575 CASE_MATHFN (BUILT_IN_REMAINDER)
1576 CASE_MATHFN (BUILT_IN_REMQUO)
1577 CASE_MATHFN (BUILT_IN_RINT)
1578 CASE_MATHFN (BUILT_IN_ROUND)
1579 CASE_MATHFN (BUILT_IN_SCALB)
1580 CASE_MATHFN (BUILT_IN_SCALBLN)
1581 CASE_MATHFN (BUILT_IN_SCALBN)
1582 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1583 CASE_MATHFN (BUILT_IN_SIN)
1584 CASE_MATHFN (BUILT_IN_SINCOS)
1585 CASE_MATHFN (BUILT_IN_SINH)
1586 CASE_MATHFN (BUILT_IN_SQRT)
1587 CASE_MATHFN (BUILT_IN_TAN)
1588 CASE_MATHFN (BUILT_IN_TANH)
1589 CASE_MATHFN (BUILT_IN_TGAMMA)
1590 CASE_MATHFN (BUILT_IN_TRUNC)
1591 CASE_MATHFN (BUILT_IN_Y0)
1592 CASE_MATHFN (BUILT_IN_Y1)
1593 CASE_MATHFN (BUILT_IN_YN)
1595 default:
1596 return 0;
1599 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1600 return implicit_built_in_decls[fcode];
1601 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1602 return implicit_built_in_decls[fcodef];
1603 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1604 return implicit_built_in_decls[fcodel];
1605 else
1606 return 0;
1609 /* If errno must be maintained, expand the RTL to check if the result,
1610 TARGET, of a built-in function call, EXP, is NaN, and if so set
1611 errno to EDOM. */
1613 static void
1614 expand_errno_check (tree exp, rtx target)
1616 rtx lab = gen_label_rtx ();
1618 /* Test the result; if it is NaN, set errno=EDOM because
1619 the argument was not in the domain. */
1620 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1621 0, lab);
1623 #ifdef TARGET_EDOM
1624 /* If this built-in doesn't throw an exception, set errno directly. */
1625 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1627 #ifdef GEN_ERRNO_RTX
1628 rtx errno_rtx = GEN_ERRNO_RTX;
1629 #else
1630 rtx errno_rtx
1631 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1632 #endif
1633 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1634 emit_label (lab);
1635 return;
1637 #endif
1639 /* We can't set errno=EDOM directly; let the library call do it.
1640 Pop the arguments right away in case the call gets deleted. */
1641 NO_DEFER_POP;
1642 expand_call (exp, target, 0);
1643 OK_DEFER_POP;
1644 emit_label (lab);
1648 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1649 Return 0 if a normal call should be emitted rather than expanding the
1650 function in-line. EXP is the expression that is a call to the builtin
1651 function; if convenient, the result should be placed in TARGET.
1652 SUBTARGET may be used as the target for computing one of EXP's operands. */
1654 static rtx
1655 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1657 optab builtin_optab;
1658 rtx op0, insns, before_call;
1659 tree fndecl = get_callee_fndecl (exp);
1660 tree arglist = TREE_OPERAND (exp, 1);
1661 enum machine_mode mode;
1662 bool errno_set = false;
1663 tree arg, narg;
1665 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1666 return 0;
1668 arg = TREE_VALUE (arglist);
1670 switch (DECL_FUNCTION_CODE (fndecl))
1672 case BUILT_IN_SQRT:
1673 case BUILT_IN_SQRTF:
1674 case BUILT_IN_SQRTL:
1675 errno_set = ! tree_expr_nonnegative_p (arg);
1676 builtin_optab = sqrt_optab;
1677 break;
1678 case BUILT_IN_EXP:
1679 case BUILT_IN_EXPF:
1680 case BUILT_IN_EXPL:
1681 errno_set = true; builtin_optab = exp_optab; break;
1682 case BUILT_IN_EXP10:
1683 case BUILT_IN_EXP10F:
1684 case BUILT_IN_EXP10L:
1685 case BUILT_IN_POW10:
1686 case BUILT_IN_POW10F:
1687 case BUILT_IN_POW10L:
1688 errno_set = true; builtin_optab = exp10_optab; break;
1689 case BUILT_IN_EXP2:
1690 case BUILT_IN_EXP2F:
1691 case BUILT_IN_EXP2L:
1692 errno_set = true; builtin_optab = exp2_optab; break;
1693 case BUILT_IN_EXPM1:
1694 case BUILT_IN_EXPM1F:
1695 case BUILT_IN_EXPM1L:
1696 errno_set = true; builtin_optab = expm1_optab; break;
1697 case BUILT_IN_LOGB:
1698 case BUILT_IN_LOGBF:
1699 case BUILT_IN_LOGBL:
1700 errno_set = true; builtin_optab = logb_optab; break;
1701 case BUILT_IN_ILOGB:
1702 case BUILT_IN_ILOGBF:
1703 case BUILT_IN_ILOGBL:
1704 errno_set = true; builtin_optab = ilogb_optab; break;
1705 case BUILT_IN_LOG:
1706 case BUILT_IN_LOGF:
1707 case BUILT_IN_LOGL:
1708 errno_set = true; builtin_optab = log_optab; break;
1709 case BUILT_IN_LOG10:
1710 case BUILT_IN_LOG10F:
1711 case BUILT_IN_LOG10L:
1712 errno_set = true; builtin_optab = log10_optab; break;
1713 case BUILT_IN_LOG2:
1714 case BUILT_IN_LOG2F:
1715 case BUILT_IN_LOG2L:
1716 errno_set = true; builtin_optab = log2_optab; break;
1717 case BUILT_IN_LOG1P:
1718 case BUILT_IN_LOG1PF:
1719 case BUILT_IN_LOG1PL:
1720 errno_set = true; builtin_optab = log1p_optab; break;
1721 case BUILT_IN_ASIN:
1722 case BUILT_IN_ASINF:
1723 case BUILT_IN_ASINL:
1724 builtin_optab = asin_optab; break;
1725 case BUILT_IN_ACOS:
1726 case BUILT_IN_ACOSF:
1727 case BUILT_IN_ACOSL:
1728 builtin_optab = acos_optab; break;
1729 case BUILT_IN_TAN:
1730 case BUILT_IN_TANF:
1731 case BUILT_IN_TANL:
1732 builtin_optab = tan_optab; break;
1733 case BUILT_IN_ATAN:
1734 case BUILT_IN_ATANF:
1735 case BUILT_IN_ATANL:
1736 builtin_optab = atan_optab; break;
1737 case BUILT_IN_FLOOR:
1738 case BUILT_IN_FLOORF:
1739 case BUILT_IN_FLOORL:
1740 builtin_optab = floor_optab; break;
1741 case BUILT_IN_CEIL:
1742 case BUILT_IN_CEILF:
1743 case BUILT_IN_CEILL:
1744 builtin_optab = ceil_optab; break;
1745 case BUILT_IN_TRUNC:
1746 case BUILT_IN_TRUNCF:
1747 case BUILT_IN_TRUNCL:
1748 builtin_optab = btrunc_optab; break;
1749 case BUILT_IN_ROUND:
1750 case BUILT_IN_ROUNDF:
1751 case BUILT_IN_ROUNDL:
1752 builtin_optab = round_optab; break;
1753 case BUILT_IN_NEARBYINT:
1754 case BUILT_IN_NEARBYINTF:
1755 case BUILT_IN_NEARBYINTL:
1756 builtin_optab = nearbyint_optab; break;
1757 case BUILT_IN_RINT:
1758 case BUILT_IN_RINTF:
1759 case BUILT_IN_RINTL:
1760 builtin_optab = rint_optab; break;
1761 case BUILT_IN_LRINT:
1762 case BUILT_IN_LRINTF:
1763 case BUILT_IN_LRINTL:
1764 case BUILT_IN_LLRINT:
1765 case BUILT_IN_LLRINTF:
1766 case BUILT_IN_LLRINTL:
1767 builtin_optab = lrint_optab; break;
1768 default:
1769 gcc_unreachable ();
1772 /* Make a suitable register to place result in. */
1773 mode = TYPE_MODE (TREE_TYPE (exp));
1775 if (! flag_errno_math || ! HONOR_NANS (mode))
1776 errno_set = false;
1778 /* Before working hard, check whether the instruction is available. */
1779 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1781 target = gen_reg_rtx (mode);
1783 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1784 need to expand the argument again. This way, we will not perform
1785 side-effects more the once. */
1786 narg = builtin_save_expr (arg);
1787 if (narg != arg)
1789 arg = narg;
1790 arglist = build_tree_list (NULL_TREE, arg);
1791 exp = build_function_call_expr (fndecl, arglist);
1794 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1796 start_sequence ();
1798 /* Compute into TARGET.
1799 Set TARGET to wherever the result comes back. */
1800 target = expand_unop (mode, builtin_optab, op0, target, 0);
1802 if (target != 0)
1804 if (errno_set)
1805 expand_errno_check (exp, target);
1807 /* Output the entire sequence. */
1808 insns = get_insns ();
1809 end_sequence ();
1810 emit_insn (insns);
1811 return target;
1814 /* If we were unable to expand via the builtin, stop the sequence
1815 (without outputting the insns) and call to the library function
1816 with the stabilized argument list. */
1817 end_sequence ();
1820 before_call = get_last_insn ();
1822 target = expand_call (exp, target, target == const0_rtx);
1824 /* If this is a sqrt operation and we don't care about errno, try to
1825 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1826 This allows the semantics of the libcall to be visible to the RTL
1827 optimizers. */
1828 if (builtin_optab == sqrt_optab && !errno_set)
1830 /* Search backwards through the insns emitted by expand_call looking
1831 for the instruction with the REG_RETVAL note. */
1832 rtx last = get_last_insn ();
1833 while (last != before_call)
1835 if (find_reg_note (last, REG_RETVAL, NULL))
1837 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1838 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1839 two elements, i.e. symbol_ref(sqrt) and the operand. */
1840 if (note
1841 && GET_CODE (note) == EXPR_LIST
1842 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1843 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1844 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1846 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1847 /* Check operand is a register with expected mode. */
1848 if (operand
1849 && REG_P (operand)
1850 && GET_MODE (operand) == mode)
1852 /* Replace the REG_EQUAL note with a SQRT rtx. */
1853 rtx equiv = gen_rtx_SQRT (mode, operand);
1854 set_unique_reg_note (last, REG_EQUAL, equiv);
1857 break;
1859 last = PREV_INSN (last);
1863 return target;
1866 /* Expand a call to the builtin binary math functions (pow and atan2).
1867 Return 0 if a normal call should be emitted rather than expanding the
1868 function in-line. EXP is the expression that is a call to the builtin
1869 function; if convenient, the result should be placed in TARGET.
1870 SUBTARGET may be used as the target for computing one of EXP's
1871 operands. */
1873 static rtx
1874 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
1876 optab builtin_optab;
1877 rtx op0, op1, insns;
1878 int op1_type = REAL_TYPE;
1879 tree fndecl = get_callee_fndecl (exp);
1880 tree arglist = TREE_OPERAND (exp, 1);
1881 tree arg0, arg1, temp, narg;
1882 enum machine_mode mode;
1883 bool errno_set = true;
1884 bool stable = true;
1886 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
1887 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
1888 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
1889 op1_type = INTEGER_TYPE;
1891 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
1892 return 0;
1894 arg0 = TREE_VALUE (arglist);
1895 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1897 switch (DECL_FUNCTION_CODE (fndecl))
1899 case BUILT_IN_POW:
1900 case BUILT_IN_POWF:
1901 case BUILT_IN_POWL:
1902 builtin_optab = pow_optab; break;
1903 case BUILT_IN_ATAN2:
1904 case BUILT_IN_ATAN2F:
1905 case BUILT_IN_ATAN2L:
1906 builtin_optab = atan2_optab; break;
1907 case BUILT_IN_LDEXP:
1908 case BUILT_IN_LDEXPF:
1909 case BUILT_IN_LDEXPL:
1910 builtin_optab = ldexp_optab; break;
1911 case BUILT_IN_FMOD:
1912 case BUILT_IN_FMODF:
1913 case BUILT_IN_FMODL:
1914 builtin_optab = fmod_optab; break;
1915 case BUILT_IN_DREM:
1916 case BUILT_IN_DREMF:
1917 case BUILT_IN_DREML:
1918 builtin_optab = drem_optab; break;
1919 default:
1920 gcc_unreachable ();
1923 /* Make a suitable register to place result in. */
1924 mode = TYPE_MODE (TREE_TYPE (exp));
1926 /* Before working hard, check whether the instruction is available. */
1927 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
1928 return 0;
1930 target = gen_reg_rtx (mode);
1932 if (! flag_errno_math || ! HONOR_NANS (mode))
1933 errno_set = false;
1935 /* Always stabilize the argument list. */
1936 narg = builtin_save_expr (arg1);
1937 if (narg != arg1)
1939 arg1 = narg;
1940 temp = build_tree_list (NULL_TREE, narg);
1941 stable = false;
1943 else
1944 temp = TREE_CHAIN (arglist);
1946 narg = builtin_save_expr (arg0);
1947 if (narg != arg0)
1949 arg0 = narg;
1950 arglist = tree_cons (NULL_TREE, narg, temp);
1951 stable = false;
1953 else if (! stable)
1954 arglist = tree_cons (NULL_TREE, arg0, temp);
1956 if (! stable)
1957 exp = build_function_call_expr (fndecl, arglist);
1959 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
1960 op1 = expand_expr (arg1, 0, VOIDmode, 0);
1962 start_sequence ();
1964 /* Compute into TARGET.
1965 Set TARGET to wherever the result comes back. */
1966 target = expand_binop (mode, builtin_optab, op0, op1,
1967 target, 0, OPTAB_DIRECT);
1969 /* If we were unable to expand via the builtin, stop the sequence
1970 (without outputting the insns) and call to the library function
1971 with the stabilized argument list. */
1972 if (target == 0)
1974 end_sequence ();
1975 return expand_call (exp, target, target == const0_rtx);
1978 if (errno_set)
1979 expand_errno_check (exp, target);
1981 /* Output the entire sequence. */
1982 insns = get_insns ();
1983 end_sequence ();
1984 emit_insn (insns);
1986 return target;
1989 /* Expand a call to the builtin sin and cos math functions.
1990 Return 0 if a normal call should be emitted rather than expanding the
1991 function in-line. EXP is the expression that is a call to the builtin
1992 function; if convenient, the result should be placed in TARGET.
1993 SUBTARGET may be used as the target for computing one of EXP's
1994 operands. */
1996 static rtx
1997 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
1999 optab builtin_optab;
2000 rtx op0, insns;
2001 tree fndecl = get_callee_fndecl (exp);
2002 tree arglist = TREE_OPERAND (exp, 1);
2003 enum machine_mode mode;
2004 bool errno_set = false;
2005 tree arg, narg;
2007 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2008 return 0;
2010 arg = TREE_VALUE (arglist);
2012 switch (DECL_FUNCTION_CODE (fndecl))
2014 case BUILT_IN_SIN:
2015 case BUILT_IN_SINF:
2016 case BUILT_IN_SINL:
2017 case BUILT_IN_COS:
2018 case BUILT_IN_COSF:
2019 case BUILT_IN_COSL:
2020 builtin_optab = sincos_optab; break;
2021 default:
2022 gcc_unreachable ();
2025 /* Make a suitable register to place result in. */
2026 mode = TYPE_MODE (TREE_TYPE (exp));
2028 if (! flag_errno_math || ! HONOR_NANS (mode))
2029 errno_set = false;
2031 /* Check if sincos insn is available, otherwise fallback
2032 to sin or cos insn. */
2033 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2034 switch (DECL_FUNCTION_CODE (fndecl))
2036 case BUILT_IN_SIN:
2037 case BUILT_IN_SINF:
2038 case BUILT_IN_SINL:
2039 builtin_optab = sin_optab; break;
2040 case BUILT_IN_COS:
2041 case BUILT_IN_COSF:
2042 case BUILT_IN_COSL:
2043 builtin_optab = cos_optab; break;
2044 default:
2045 gcc_unreachable ();
2049 /* Before working hard, check whether the instruction is available. */
2050 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2052 target = gen_reg_rtx (mode);
2054 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2055 need to expand the argument again. This way, we will not perform
2056 side-effects more the once. */
2057 narg = save_expr (arg);
2058 if (narg != arg)
2060 arg = narg;
2061 arglist = build_tree_list (NULL_TREE, arg);
2062 exp = build_function_call_expr (fndecl, arglist);
2065 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2067 start_sequence ();
2069 /* Compute into TARGET.
2070 Set TARGET to wherever the result comes back. */
2071 if (builtin_optab == sincos_optab)
2073 int result;
2075 switch (DECL_FUNCTION_CODE (fndecl))
2077 case BUILT_IN_SIN:
2078 case BUILT_IN_SINF:
2079 case BUILT_IN_SINL:
2080 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2081 break;
2082 case BUILT_IN_COS:
2083 case BUILT_IN_COSF:
2084 case BUILT_IN_COSL:
2085 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2086 break;
2087 default:
2088 gcc_unreachable ();
2090 gcc_assert (result);
2092 else
2094 target = expand_unop (mode, builtin_optab, op0, target, 0);
2097 if (target != 0)
2099 if (errno_set)
2100 expand_errno_check (exp, target);
2102 /* Output the entire sequence. */
2103 insns = get_insns ();
2104 end_sequence ();
2105 emit_insn (insns);
2106 return target;
2109 /* If we were unable to expand via the builtin, stop the sequence
2110 (without outputting the insns) and call to the library function
2111 with the stabilized argument list. */
2112 end_sequence ();
2115 target = expand_call (exp, target, target == const0_rtx);
2117 return target;
2120 /* To evaluate powi(x,n), the floating point value x raised to the
2121 constant integer exponent n, we use a hybrid algorithm that
2122 combines the "window method" with look-up tables. For an
2123 introduction to exponentiation algorithms and "addition chains",
2124 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2125 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2126 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2127 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2129 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2130 multiplications to inline before calling the system library's pow
2131 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2132 so this default never requires calling pow, powf or powl. */
2134 #ifndef POWI_MAX_MULTS
2135 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2136 #endif
2138 /* The size of the "optimal power tree" lookup table. All
2139 exponents less than this value are simply looked up in the
2140 powi_table below. This threshold is also used to size the
2141 cache of pseudo registers that hold intermediate results. */
2142 #define POWI_TABLE_SIZE 256
2144 /* The size, in bits of the window, used in the "window method"
2145 exponentiation algorithm. This is equivalent to a radix of
2146 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2147 #define POWI_WINDOW_SIZE 3
2149 /* The following table is an efficient representation of an
2150 "optimal power tree". For each value, i, the corresponding
2151 value, j, in the table states than an optimal evaluation
2152 sequence for calculating pow(x,i) can be found by evaluating
2153 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2154 100 integers is given in Knuth's "Seminumerical algorithms". */
2156 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2158 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2159 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2160 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2161 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2162 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2163 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2164 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2165 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2166 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2167 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2168 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2169 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2170 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2171 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2172 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2173 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2174 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2175 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2176 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2177 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2178 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2179 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2180 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2181 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2182 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2183 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2184 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2185 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2186 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2187 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2188 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2189 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2193 /* Return the number of multiplications required to calculate
2194 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2195 subroutine of powi_cost. CACHE is an array indicating
2196 which exponents have already been calculated. */
2198 static int
2199 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2201 /* If we've already calculated this exponent, then this evaluation
2202 doesn't require any additional multiplications. */
2203 if (cache[n])
2204 return 0;
2206 cache[n] = true;
2207 return powi_lookup_cost (n - powi_table[n], cache)
2208 + powi_lookup_cost (powi_table[n], cache) + 1;
2211 /* Return the number of multiplications required to calculate
2212 powi(x,n) for an arbitrary x, given the exponent N. This
2213 function needs to be kept in sync with expand_powi below. */
2215 static int
2216 powi_cost (HOST_WIDE_INT n)
2218 bool cache[POWI_TABLE_SIZE];
2219 unsigned HOST_WIDE_INT digit;
2220 unsigned HOST_WIDE_INT val;
2221 int result;
2223 if (n == 0)
2224 return 0;
2226 /* Ignore the reciprocal when calculating the cost. */
2227 val = (n < 0) ? -n : n;
2229 /* Initialize the exponent cache. */
2230 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2231 cache[1] = true;
2233 result = 0;
2235 while (val >= POWI_TABLE_SIZE)
2237 if (val & 1)
2239 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2240 result += powi_lookup_cost (digit, cache)
2241 + POWI_WINDOW_SIZE + 1;
2242 val >>= POWI_WINDOW_SIZE;
2244 else
2246 val >>= 1;
2247 result++;
2251 return result + powi_lookup_cost (val, cache);
2254 /* Recursive subroutine of expand_powi. This function takes the array,
2255 CACHE, of already calculated exponents and an exponent N and returns
2256 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2258 static rtx
2259 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2261 unsigned HOST_WIDE_INT digit;
2262 rtx target, result;
2263 rtx op0, op1;
2265 if (n < POWI_TABLE_SIZE)
2267 if (cache[n])
2268 return cache[n];
2270 target = gen_reg_rtx (mode);
2271 cache[n] = target;
2273 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2274 op1 = expand_powi_1 (mode, powi_table[n], cache);
2276 else if (n & 1)
2278 target = gen_reg_rtx (mode);
2279 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2280 op0 = expand_powi_1 (mode, n - digit, cache);
2281 op1 = expand_powi_1 (mode, digit, cache);
2283 else
2285 target = gen_reg_rtx (mode);
2286 op0 = expand_powi_1 (mode, n >> 1, cache);
2287 op1 = op0;
2290 result = expand_mult (mode, op0, op1, target, 0);
2291 if (result != target)
2292 emit_move_insn (target, result);
2293 return target;
2296 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2297 floating point operand in mode MODE, and N is the exponent. This
2298 function needs to be kept in sync with powi_cost above. */
2300 static rtx
2301 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2303 unsigned HOST_WIDE_INT val;
2304 rtx cache[POWI_TABLE_SIZE];
2305 rtx result;
2307 if (n == 0)
2308 return CONST1_RTX (mode);
2310 val = (n < 0) ? -n : n;
2312 memset (cache, 0, sizeof (cache));
2313 cache[1] = x;
2315 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2317 /* If the original exponent was negative, reciprocate the result. */
2318 if (n < 0)
2319 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2320 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2322 return result;
2325 /* Expand a call to the pow built-in mathematical function. Return 0 if
2326 a normal call should be emitted rather than expanding the function
2327 in-line. EXP is the expression that is a call to the builtin
2328 function; if convenient, the result should be placed in TARGET. */
2330 static rtx
2331 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2333 tree arglist = TREE_OPERAND (exp, 1);
2334 tree arg0, arg1;
2336 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2337 return 0;
2339 arg0 = TREE_VALUE (arglist);
2340 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2342 if (TREE_CODE (arg1) == REAL_CST
2343 && ! TREE_CONSTANT_OVERFLOW (arg1))
2345 REAL_VALUE_TYPE cint;
2346 REAL_VALUE_TYPE c;
2347 HOST_WIDE_INT n;
2349 c = TREE_REAL_CST (arg1);
2350 n = real_to_integer (&c);
2351 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2352 if (real_identical (&c, &cint))
2354 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2355 Otherwise, check the number of multiplications required.
2356 Note that pow never sets errno for an integer exponent. */
2357 if ((n >= -1 && n <= 2)
2358 || (flag_unsafe_math_optimizations
2359 && ! optimize_size
2360 && powi_cost (n) <= POWI_MAX_MULTS))
2362 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2363 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2364 op = force_reg (mode, op);
2365 return expand_powi (op, mode, n);
2370 if (! flag_unsafe_math_optimizations)
2371 return NULL_RTX;
2372 return expand_builtin_mathfn_2 (exp, target, subtarget);
2375 /* Expand a call to the powi built-in mathematical function. Return 0 if
2376 a normal call should be emitted rather than expanding the function
2377 in-line. EXP is the expression that is a call to the builtin
2378 function; if convenient, the result should be placed in TARGET. */
2380 static rtx
2381 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2383 tree arglist = TREE_OPERAND (exp, 1);
2384 tree arg0, arg1;
2385 rtx op0, op1;
2386 enum machine_mode mode;
2388 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2389 return 0;
2391 arg0 = TREE_VALUE (arglist);
2392 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2393 mode = TYPE_MODE (TREE_TYPE (exp));
2395 /* Handle constant power. */
2397 if (TREE_CODE (arg1) == INTEGER_CST
2398 && ! TREE_CONSTANT_OVERFLOW (arg1))
2400 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2402 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2403 Otherwise, check the number of multiplications required. */
2404 if ((TREE_INT_CST_HIGH (arg1) == 0
2405 || TREE_INT_CST_HIGH (arg1) == -1)
2406 && ((n >= -1 && n <= 2)
2407 || (! optimize_size
2408 && powi_cost (n) <= POWI_MAX_MULTS)))
2410 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2411 op0 = force_reg (mode, op0);
2412 return expand_powi (op0, mode, n);
2416 /* Emit a libcall to libgcc. */
2418 if (target == NULL_RTX)
2419 target = gen_reg_rtx (mode);
2421 op0 = expand_expr (arg0, subtarget, mode, 0);
2422 if (GET_MODE (op0) != mode)
2423 op0 = convert_to_mode (mode, op0, 0);
2424 op1 = expand_expr (arg1, 0, word_mode, 0);
2425 if (GET_MODE (op1) != word_mode)
2426 op1 = convert_to_mode (word_mode, op1, 0);
2428 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2429 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2430 op0, mode, op1, word_mode);
2432 return target;
2435 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2436 if we failed the caller should emit a normal call, otherwise
2437 try to get the result in TARGET, if convenient. */
2439 static rtx
2440 expand_builtin_strlen (tree arglist, rtx target,
2441 enum machine_mode target_mode)
2443 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2444 return 0;
2445 else
2447 rtx pat;
2448 tree len, src = TREE_VALUE (arglist);
2449 rtx result, src_reg, char_rtx, before_strlen;
2450 enum machine_mode insn_mode = target_mode, char_mode;
2451 enum insn_code icode = CODE_FOR_nothing;
2452 int align;
2454 /* If the length can be computed at compile-time, return it. */
2455 len = c_strlen (src, 0);
2456 if (len)
2457 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2459 /* If the length can be computed at compile-time and is constant
2460 integer, but there are side-effects in src, evaluate
2461 src for side-effects, then return len.
2462 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2463 can be optimized into: i++; x = 3; */
2464 len = c_strlen (src, 1);
2465 if (len && TREE_CODE (len) == INTEGER_CST)
2467 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2468 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2471 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2473 /* If SRC is not a pointer type, don't do this operation inline. */
2474 if (align == 0)
2475 return 0;
2477 /* Bail out if we can't compute strlen in the right mode. */
2478 while (insn_mode != VOIDmode)
2480 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2481 if (icode != CODE_FOR_nothing)
2482 break;
2484 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2486 if (insn_mode == VOIDmode)
2487 return 0;
2489 /* Make a place to write the result of the instruction. */
2490 result = target;
2491 if (! (result != 0
2492 && REG_P (result)
2493 && GET_MODE (result) == insn_mode
2494 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2495 result = gen_reg_rtx (insn_mode);
2497 /* Make a place to hold the source address. We will not expand
2498 the actual source until we are sure that the expansion will
2499 not fail -- there are trees that cannot be expanded twice. */
2500 src_reg = gen_reg_rtx (Pmode);
2502 /* Mark the beginning of the strlen sequence so we can emit the
2503 source operand later. */
2504 before_strlen = get_last_insn ();
2506 char_rtx = const0_rtx;
2507 char_mode = insn_data[(int) icode].operand[2].mode;
2508 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2509 char_mode))
2510 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2512 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2513 char_rtx, GEN_INT (align));
2514 if (! pat)
2515 return 0;
2516 emit_insn (pat);
2518 /* Now that we are assured of success, expand the source. */
2519 start_sequence ();
2520 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2521 if (pat != src_reg)
2522 emit_move_insn (src_reg, pat);
2523 pat = get_insns ();
2524 end_sequence ();
2526 if (before_strlen)
2527 emit_insn_after (pat, before_strlen);
2528 else
2529 emit_insn_before (pat, get_insns ());
2531 /* Return the value in the proper mode for this function. */
2532 if (GET_MODE (result) == target_mode)
2533 target = result;
2534 else if (target != 0)
2535 convert_move (target, result, 0);
2536 else
2537 target = convert_to_mode (target_mode, result, 0);
2539 return target;
2543 /* Expand a call to the strstr builtin. Return 0 if we failed the
2544 caller should emit a normal call, otherwise try to get the result
2545 in TARGET, if convenient (and in mode MODE if that's convenient). */
2547 static rtx
2548 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2550 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2552 tree result = fold_builtin_strstr (arglist, type);
2553 if (result)
2554 return expand_expr (result, target, mode, EXPAND_NORMAL);
2556 return 0;
2559 /* Expand a call to the strchr builtin. Return 0 if we failed the
2560 caller should emit a normal call, otherwise try to get the result
2561 in TARGET, if convenient (and in mode MODE if that's convenient). */
2563 static rtx
2564 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2566 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2568 tree result = fold_builtin_strchr (arglist, type);
2569 if (result)
2570 return expand_expr (result, target, mode, EXPAND_NORMAL);
2572 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2574 return 0;
2577 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2578 caller should emit a normal call, otherwise try to get the result
2579 in TARGET, if convenient (and in mode MODE if that's convenient). */
2581 static rtx
2582 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2584 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2586 tree result = fold_builtin_strrchr (arglist, type);
2587 if (result)
2588 return expand_expr (result, target, mode, EXPAND_NORMAL);
2590 return 0;
2593 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2594 caller should emit a normal call, otherwise try to get the result
2595 in TARGET, if convenient (and in mode MODE if that's convenient). */
2597 static rtx
2598 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2600 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2602 tree result = fold_builtin_strpbrk (arglist, type);
2603 if (result)
2604 return expand_expr (result, target, mode, EXPAND_NORMAL);
2606 return 0;
2609 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2610 bytes from constant string DATA + OFFSET and return it as target
2611 constant. */
2613 static rtx
2614 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2615 enum machine_mode mode)
2617 const char *str = (const char *) data;
2619 gcc_assert (offset >= 0
2620 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2621 <= strlen (str) + 1));
2623 return c_readstr (str + offset, mode);
2626 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2627 Return 0 if we failed, the caller should emit a normal call,
2628 otherwise try to get the result in TARGET, if convenient (and in
2629 mode MODE if that's convenient). */
2630 static rtx
2631 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2633 tree fndecl = get_callee_fndecl (exp);
2634 tree arglist = TREE_OPERAND (exp, 1);
2635 if (!validate_arglist (arglist,
2636 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2637 return 0;
2638 else
2640 tree dest = TREE_VALUE (arglist);
2641 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2642 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2643 const char *src_str;
2644 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2645 unsigned int dest_align
2646 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2647 rtx dest_mem, src_mem, dest_addr, len_rtx;
2648 tree result = fold_builtin_memcpy (fndecl, arglist);
2650 if (result)
2651 return expand_expr (result, target, mode, EXPAND_NORMAL);
2653 /* If DEST is not a pointer type, call the normal function. */
2654 if (dest_align == 0)
2655 return 0;
2657 /* If either SRC is not a pointer type, don't do this
2658 operation in-line. */
2659 if (src_align == 0)
2660 return 0;
2662 dest_mem = get_memory_rtx (dest);
2663 set_mem_align (dest_mem, dest_align);
2664 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2665 src_str = c_getstr (src);
2667 /* If SRC is a string constant and block move would be done
2668 by pieces, we can avoid loading the string from memory
2669 and only stored the computed constants. */
2670 if (src_str
2671 && GET_CODE (len_rtx) == CONST_INT
2672 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2673 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2674 (void *) src_str, dest_align))
2676 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2677 builtin_memcpy_read_str,
2678 (void *) src_str, dest_align, 0);
2679 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2680 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2681 return dest_mem;
2684 src_mem = get_memory_rtx (src);
2685 set_mem_align (src_mem, src_align);
2687 /* Copy word part most expediently. */
2688 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2689 BLOCK_OP_NORMAL);
2691 if (dest_addr == 0)
2693 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2694 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2696 return dest_addr;
2700 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2701 Return 0 if we failed the caller should emit a normal call,
2702 otherwise try to get the result in TARGET, if convenient (and in
2703 mode MODE if that's convenient). If ENDP is 0 return the
2704 destination pointer, if ENDP is 1 return the end pointer ala
2705 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2706 stpcpy. */
2708 static rtx
2709 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2710 int endp)
2712 if (!validate_arglist (arglist,
2713 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2714 return 0;
2715 /* If return value is ignored, transform mempcpy into memcpy. */
2716 else if (target == const0_rtx)
2718 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2720 if (!fn)
2721 return 0;
2723 return expand_expr (build_function_call_expr (fn, arglist),
2724 target, mode, EXPAND_NORMAL);
2726 else
2728 tree dest = TREE_VALUE (arglist);
2729 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2730 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2731 const char *src_str;
2732 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2733 unsigned int dest_align
2734 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2735 rtx dest_mem, src_mem, len_rtx;
2736 tree result = fold_builtin_mempcpy (arglist, type, endp);
2738 if (result)
2739 return expand_expr (result, target, mode, EXPAND_NORMAL);
2741 /* If either SRC or DEST is not a pointer type, don't do this
2742 operation in-line. */
2743 if (dest_align == 0 || src_align == 0)
2744 return 0;
2746 /* If LEN is not constant, call the normal function. */
2747 if (! host_integerp (len, 1))
2748 return 0;
2750 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
2751 src_str = c_getstr (src);
2753 /* If SRC is a string constant and block move would be done
2754 by pieces, we can avoid loading the string from memory
2755 and only stored the computed constants. */
2756 if (src_str
2757 && GET_CODE (len_rtx) == CONST_INT
2758 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2759 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2760 (void *) src_str, dest_align))
2762 dest_mem = get_memory_rtx (dest);
2763 set_mem_align (dest_mem, dest_align);
2764 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2765 builtin_memcpy_read_str,
2766 (void *) src_str, dest_align, endp);
2767 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2768 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2769 return dest_mem;
2772 if (GET_CODE (len_rtx) == CONST_INT
2773 && can_move_by_pieces (INTVAL (len_rtx),
2774 MIN (dest_align, src_align)))
2776 dest_mem = get_memory_rtx (dest);
2777 set_mem_align (dest_mem, dest_align);
2778 src_mem = get_memory_rtx (src);
2779 set_mem_align (src_mem, src_align);
2780 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
2781 MIN (dest_align, src_align), endp);
2782 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2783 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2784 return dest_mem;
2787 return 0;
2791 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
2792 if we failed the caller should emit a normal call. */
2794 static rtx
2795 expand_builtin_memmove (tree arglist, tree type, rtx target,
2796 enum machine_mode mode)
2798 if (!validate_arglist (arglist,
2799 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2800 return 0;
2801 else
2803 tree dest = TREE_VALUE (arglist);
2804 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2805 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2807 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2808 unsigned int dest_align
2809 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2810 tree result = fold_builtin_memmove (arglist, type);
2812 if (result)
2813 return expand_expr (result, target, mode, EXPAND_NORMAL);
2815 /* If DEST is not a pointer type, call the normal function. */
2816 if (dest_align == 0)
2817 return 0;
2819 /* If either SRC is not a pointer type, don't do this
2820 operation in-line. */
2821 if (src_align == 0)
2822 return 0;
2824 /* If src is categorized for a readonly section we can use
2825 normal memcpy. */
2826 if (readonly_data_expr (src))
2828 tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2829 if (!fn)
2830 return 0;
2831 return expand_expr (build_function_call_expr (fn, arglist),
2832 target, mode, EXPAND_NORMAL);
2835 /* If length is 1 and we can expand memcpy call inline,
2836 it is ok to use memcpy as well. */
2837 if (integer_onep (len))
2839 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
2840 /*endp=*/0);
2841 if (ret)
2842 return ret;
2845 /* Otherwise, call the normal function. */
2846 return 0;
2850 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
2851 if we failed the caller should emit a normal call. */
2853 static rtx
2854 expand_builtin_bcopy (tree arglist, tree type)
2856 tree src, dest, size, newarglist;
2858 if (!validate_arglist (arglist,
2859 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2860 return NULL_RTX;
2862 src = TREE_VALUE (arglist);
2863 dest = TREE_VALUE (TREE_CHAIN (arglist));
2864 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2866 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
2867 memmove(ptr y, ptr x, size_t z). This is done this way
2868 so that if it isn't expanded inline, we fallback to
2869 calling bcopy instead of memmove. */
2871 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
2872 newarglist = tree_cons (NULL_TREE, src, newarglist);
2873 newarglist = tree_cons (NULL_TREE, dest, newarglist);
2875 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode);
2878 #ifndef HAVE_movstr
2879 # define HAVE_movstr 0
2880 # define CODE_FOR_movstr CODE_FOR_nothing
2881 #endif
2883 /* Expand into a movstr instruction, if one is available. Return 0 if
2884 we failed, the caller should emit a normal call, otherwise try to
2885 get the result in TARGET, if convenient. If ENDP is 0 return the
2886 destination pointer, if ENDP is 1 return the end pointer ala
2887 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2888 stpcpy. */
2890 static rtx
2891 expand_movstr (tree dest, tree src, rtx target, int endp)
2893 rtx end;
2894 rtx dest_mem;
2895 rtx src_mem;
2896 rtx insn;
2897 const struct insn_data * data;
2899 if (!HAVE_movstr)
2900 return 0;
2902 dest_mem = get_memory_rtx (dest);
2903 src_mem = get_memory_rtx (src);
2904 if (!endp)
2906 target = force_reg (Pmode, XEXP (dest_mem, 0));
2907 dest_mem = replace_equiv_address (dest_mem, target);
2908 end = gen_reg_rtx (Pmode);
2910 else
2912 if (target == 0 || target == const0_rtx)
2914 end = gen_reg_rtx (Pmode);
2915 if (target == 0)
2916 target = end;
2918 else
2919 end = target;
2922 data = insn_data + CODE_FOR_movstr;
2924 if (data->operand[0].mode != VOIDmode)
2925 end = gen_lowpart (data->operand[0].mode, end);
2927 insn = data->genfun (end, dest_mem, src_mem);
2929 gcc_assert (insn);
2931 emit_insn (insn);
2933 /* movstr is supposed to set end to the address of the NUL
2934 terminator. If the caller requested a mempcpy-like return value,
2935 adjust it. */
2936 if (endp == 1 && target != const0_rtx)
2938 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
2939 emit_move_insn (target, force_operand (tem, NULL_RTX));
2942 return target;
2945 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
2946 if we failed the caller should emit a normal call, otherwise try to get
2947 the result in TARGET, if convenient (and in mode MODE if that's
2948 convenient). */
2950 static rtx
2951 expand_builtin_strcpy (tree exp, rtx target, enum machine_mode mode)
2953 tree fndecl = get_callee_fndecl (exp);
2954 tree arglist = TREE_OPERAND (exp, 1);
2955 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2957 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
2958 if (result)
2959 return expand_expr (result, target, mode, EXPAND_NORMAL);
2961 return expand_movstr (TREE_VALUE (arglist),
2962 TREE_VALUE (TREE_CHAIN (arglist)),
2963 target, /*endp=*/0);
2965 return 0;
2968 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
2969 Return 0 if we failed the caller should emit a normal call,
2970 otherwise try to get the result in TARGET, if convenient (and in
2971 mode MODE if that's convenient). */
2973 static rtx
2974 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
2976 tree arglist = TREE_OPERAND (exp, 1);
2977 /* If return value is ignored, transform stpcpy into strcpy. */
2978 if (target == const0_rtx)
2980 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
2981 if (!fn)
2982 return 0;
2984 return expand_expr (build_function_call_expr (fn, arglist),
2985 target, mode, EXPAND_NORMAL);
2988 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2989 return 0;
2990 else
2992 tree dst, src, len, lenp1;
2993 tree narglist;
2994 rtx ret;
2996 /* Ensure we get an actual string whose length can be evaluated at
2997 compile-time, not an expression containing a string. This is
2998 because the latter will potentially produce pessimized code
2999 when used to produce the return value. */
3000 src = TREE_VALUE (TREE_CHAIN (arglist));
3001 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3002 return expand_movstr (TREE_VALUE (arglist),
3003 TREE_VALUE (TREE_CHAIN (arglist)),
3004 target, /*endp=*/2);
3006 dst = TREE_VALUE (arglist);
3007 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3008 narglist = build_tree_list (NULL_TREE, lenp1);
3009 narglist = tree_cons (NULL_TREE, src, narglist);
3010 narglist = tree_cons (NULL_TREE, dst, narglist);
3011 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3012 target, mode, /*endp=*/2);
3014 if (ret)
3015 return ret;
3017 if (TREE_CODE (len) == INTEGER_CST)
3019 rtx len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3021 if (GET_CODE (len_rtx) == CONST_INT)
3023 ret = expand_builtin_strcpy (exp, target, mode);
3025 if (ret)
3027 if (! target)
3029 if (mode != VOIDmode)
3030 target = gen_reg_rtx (mode);
3031 else
3032 target = gen_reg_rtx (GET_MODE (ret));
3034 if (GET_MODE (target) != GET_MODE (ret))
3035 ret = gen_lowpart (GET_MODE (target), ret);
3037 ret = plus_constant (ret, INTVAL (len_rtx));
3038 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3039 gcc_assert (ret);
3041 return target;
3046 return expand_movstr (TREE_VALUE (arglist),
3047 TREE_VALUE (TREE_CHAIN (arglist)),
3048 target, /*endp=*/2);
3052 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3053 bytes from constant string DATA + OFFSET and return it as target
3054 constant. */
3056 static rtx
3057 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3058 enum machine_mode mode)
3060 const char *str = (const char *) data;
3062 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3063 return const0_rtx;
3065 return c_readstr (str + offset, mode);
3068 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3069 if we failed the caller should emit a normal call. */
3071 static rtx
3072 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3074 tree fndecl = get_callee_fndecl (exp);
3075 tree arglist = TREE_OPERAND (exp, 1);
3076 if (validate_arglist (arglist,
3077 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3079 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3080 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3081 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3083 if (result)
3084 return expand_expr (result, target, mode, EXPAND_NORMAL);
3086 /* We must be passed a constant len and src parameter. */
3087 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3088 return 0;
3090 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3092 /* We're required to pad with trailing zeros if the requested
3093 len is greater than strlen(s2)+1. In that case try to
3094 use store_by_pieces, if it fails, punt. */
3095 if (tree_int_cst_lt (slen, len))
3097 tree dest = TREE_VALUE (arglist);
3098 unsigned int dest_align
3099 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3100 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3101 rtx dest_mem;
3103 if (!p || dest_align == 0 || !host_integerp (len, 1)
3104 || !can_store_by_pieces (tree_low_cst (len, 1),
3105 builtin_strncpy_read_str,
3106 (void *) p, dest_align))
3107 return 0;
3109 dest_mem = get_memory_rtx (dest);
3110 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3111 builtin_strncpy_read_str,
3112 (void *) p, dest_align, 0);
3113 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3114 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3115 return dest_mem;
3118 return 0;
3121 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3122 bytes from constant string DATA + OFFSET and return it as target
3123 constant. */
3125 static rtx
3126 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3127 enum machine_mode mode)
3129 const char *c = (const char *) data;
3130 char *p = alloca (GET_MODE_SIZE (mode));
3132 memset (p, *c, GET_MODE_SIZE (mode));
3134 return c_readstr (p, mode);
3137 /* Callback routine for store_by_pieces. Return the RTL of a register
3138 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3139 char value given in the RTL register data. For example, if mode is
3140 4 bytes wide, return the RTL for 0x01010101*data. */
3142 static rtx
3143 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3144 enum machine_mode mode)
3146 rtx target, coeff;
3147 size_t size;
3148 char *p;
3150 size = GET_MODE_SIZE (mode);
3151 if (size == 1)
3152 return (rtx) data;
3154 p = alloca (size);
3155 memset (p, 1, size);
3156 coeff = c_readstr (p, mode);
3158 target = convert_to_mode (mode, (rtx) data, 1);
3159 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3160 return force_reg (mode, target);
3163 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3164 if we failed the caller should emit a normal call, otherwise try to get
3165 the result in TARGET, if convenient (and in mode MODE if that's
3166 convenient). */
3168 static rtx
3169 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode)
3171 if (!validate_arglist (arglist,
3172 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3173 return 0;
3174 else
3176 tree dest = TREE_VALUE (arglist);
3177 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3178 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3179 char c;
3181 unsigned int dest_align
3182 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3183 rtx dest_mem, dest_addr, len_rtx;
3185 /* If DEST is not a pointer type, don't do this
3186 operation in-line. */
3187 if (dest_align == 0)
3188 return 0;
3190 /* If the LEN parameter is zero, return DEST. */
3191 if (integer_zerop (len))
3193 /* Evaluate and ignore VAL in case it has side-effects. */
3194 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3195 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3198 if (TREE_CODE (val) != INTEGER_CST)
3200 rtx val_rtx;
3202 if (!host_integerp (len, 1))
3203 return 0;
3205 if (optimize_size && tree_low_cst (len, 1) > 1)
3206 return 0;
3208 /* Assume that we can memset by pieces if we can store the
3209 * the coefficients by pieces (in the required modes).
3210 * We can't pass builtin_memset_gen_str as that emits RTL. */
3211 c = 1;
3212 if (!can_store_by_pieces (tree_low_cst (len, 1),
3213 builtin_memset_read_str,
3214 &c, dest_align))
3215 return 0;
3217 val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val));
3218 val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0);
3219 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3220 val_rtx);
3221 dest_mem = get_memory_rtx (dest);
3222 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3223 builtin_memset_gen_str,
3224 val_rtx, dest_align, 0);
3225 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3226 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3227 return dest_mem;
3230 if (target_char_cast (val, &c))
3231 return 0;
3233 if (c)
3235 if (!host_integerp (len, 1))
3236 return 0;
3237 if (!can_store_by_pieces (tree_low_cst (len, 1),
3238 builtin_memset_read_str, &c,
3239 dest_align))
3240 return 0;
3242 dest_mem = get_memory_rtx (dest);
3243 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3244 builtin_memset_read_str,
3245 &c, dest_align, 0);
3246 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3247 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3248 return dest_mem;
3251 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3253 dest_mem = get_memory_rtx (dest);
3254 set_mem_align (dest_mem, dest_align);
3255 dest_addr = clear_storage (dest_mem, len_rtx);
3257 if (dest_addr == 0)
3259 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3260 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3263 return dest_addr;
3267 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3268 if we failed the caller should emit a normal call. */
3270 static rtx
3271 expand_builtin_bzero (tree arglist)
3273 tree dest, size, newarglist;
3275 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3276 return NULL_RTX;
3278 dest = TREE_VALUE (arglist);
3279 size = TREE_VALUE (TREE_CHAIN (arglist));
3281 /* New argument list transforming bzero(ptr x, int y) to
3282 memset(ptr x, int 0, size_t y). This is done this way
3283 so that if it isn't expanded inline, we fallback to
3284 calling bzero instead of memset. */
3286 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3287 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3288 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3290 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode);
3293 /* Expand expression EXP, which is a call to the memcmp built-in function.
3294 ARGLIST is the argument list for this call. Return 0 if we failed and the
3295 caller should emit a normal call, otherwise try to get the result in
3296 TARGET, if convenient (and in mode MODE, if that's convenient). */
3298 static rtx
3299 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3300 enum machine_mode mode)
3302 if (!validate_arglist (arglist,
3303 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3304 return 0;
3305 else
3307 tree result = fold_builtin_memcmp (arglist);
3308 if (result)
3309 return expand_expr (result, target, mode, EXPAND_NORMAL);
3312 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi
3314 tree arg1 = TREE_VALUE (arglist);
3315 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3316 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3317 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3318 rtx result;
3319 rtx insn;
3321 int arg1_align
3322 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3323 int arg2_align
3324 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3325 enum machine_mode insn_mode;
3327 #ifdef HAVE_cmpmemsi
3328 if (HAVE_cmpmemsi)
3329 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3330 else
3331 #endif
3332 #ifdef HAVE_cmpstrsi
3333 if (HAVE_cmpstrsi)
3334 insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3335 else
3336 #endif
3337 return 0;
3339 /* If we don't have POINTER_TYPE, call the function. */
3340 if (arg1_align == 0 || arg2_align == 0)
3341 return 0;
3343 /* Make a place to write the result of the instruction. */
3344 result = target;
3345 if (! (result != 0
3346 && REG_P (result) && GET_MODE (result) == insn_mode
3347 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3348 result = gen_reg_rtx (insn_mode);
3350 arg1_rtx = get_memory_rtx (arg1);
3351 arg2_rtx = get_memory_rtx (arg2);
3352 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3354 /* Set MEM_SIZE as appropriate. */
3355 if (GET_CODE (arg3_rtx) == CONST_INT)
3357 set_mem_size (arg1_rtx, arg3_rtx);
3358 set_mem_size (arg2_rtx, arg3_rtx);
3361 #ifdef HAVE_cmpmemsi
3362 if (HAVE_cmpmemsi)
3363 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3364 GEN_INT (MIN (arg1_align, arg2_align)));
3365 else
3366 #endif
3367 #ifdef HAVE_cmpstrsi
3368 if (HAVE_cmpstrsi)
3369 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3370 GEN_INT (MIN (arg1_align, arg2_align)));
3371 else
3372 #endif
3373 gcc_unreachable ();
3375 if (insn)
3376 emit_insn (insn);
3377 else
3378 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3379 TYPE_MODE (integer_type_node), 3,
3380 XEXP (arg1_rtx, 0), Pmode,
3381 XEXP (arg2_rtx, 0), Pmode,
3382 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3383 TYPE_UNSIGNED (sizetype)),
3384 TYPE_MODE (sizetype));
3386 /* Return the value in the proper mode for this function. */
3387 mode = TYPE_MODE (TREE_TYPE (exp));
3388 if (GET_MODE (result) == mode)
3389 return result;
3390 else if (target != 0)
3392 convert_move (target, result, 0);
3393 return target;
3395 else
3396 return convert_to_mode (mode, result, 0);
3398 #endif
3400 return 0;
3403 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3404 if we failed the caller should emit a normal call, otherwise try to get
3405 the result in TARGET, if convenient. */
3407 static rtx
3408 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3410 tree arglist = TREE_OPERAND (exp, 1);
3412 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3413 return 0;
3414 else
3416 tree result = fold_builtin_strcmp (arglist);
3417 if (result)
3418 return expand_expr (result, target, mode, EXPAND_NORMAL);
3421 #ifdef HAVE_cmpstrsi
3422 if (HAVE_cmpstrsi)
3424 tree arg1 = TREE_VALUE (arglist);
3425 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3426 tree len, len1, len2;
3427 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3428 rtx result, insn;
3429 tree fndecl;
3431 int arg1_align
3432 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3433 int arg2_align
3434 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3435 enum machine_mode insn_mode
3436 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3438 len1 = c_strlen (arg1, 1);
3439 len2 = c_strlen (arg2, 1);
3441 if (len1)
3442 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3443 if (len2)
3444 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3446 /* If we don't have a constant length for the first, use the length
3447 of the second, if we know it. We don't require a constant for
3448 this case; some cost analysis could be done if both are available
3449 but neither is constant. For now, assume they're equally cheap,
3450 unless one has side effects. If both strings have constant lengths,
3451 use the smaller. */
3453 if (!len1)
3454 len = len2;
3455 else if (!len2)
3456 len = len1;
3457 else if (TREE_SIDE_EFFECTS (len1))
3458 len = len2;
3459 else if (TREE_SIDE_EFFECTS (len2))
3460 len = len1;
3461 else if (TREE_CODE (len1) != INTEGER_CST)
3462 len = len2;
3463 else if (TREE_CODE (len2) != INTEGER_CST)
3464 len = len1;
3465 else if (tree_int_cst_lt (len1, len2))
3466 len = len1;
3467 else
3468 len = len2;
3470 /* If both arguments have side effects, we cannot optimize. */
3471 if (!len || TREE_SIDE_EFFECTS (len))
3472 return 0;
3474 /* If we don't have POINTER_TYPE, call the function. */
3475 if (arg1_align == 0 || arg2_align == 0)
3476 return 0;
3478 /* Make a place to write the result of the instruction. */
3479 result = target;
3480 if (! (result != 0
3481 && REG_P (result) && GET_MODE (result) == insn_mode
3482 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3483 result = gen_reg_rtx (insn_mode);
3485 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3486 arg1 = builtin_save_expr (arg1);
3487 arg2 = builtin_save_expr (arg2);
3489 arg1_rtx = get_memory_rtx (arg1);
3490 arg2_rtx = get_memory_rtx (arg2);
3491 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3492 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3493 GEN_INT (MIN (arg1_align, arg2_align)));
3494 if (insn)
3496 emit_insn (insn);
3498 /* Return the value in the proper mode for this function. */
3499 mode = TYPE_MODE (TREE_TYPE (exp));
3500 if (GET_MODE (result) == mode)
3501 return result;
3502 if (target == 0)
3503 return convert_to_mode (mode, result, 0);
3504 convert_move (target, result, 0);
3505 return target;
3508 /* Expand the library call ourselves using a stabilized argument
3509 list to avoid re-evaluating the function's arguments twice. */
3510 arglist = build_tree_list (NULL_TREE, arg2);
3511 arglist = tree_cons (NULL_TREE, arg1, arglist);
3512 fndecl = get_callee_fndecl (exp);
3513 exp = build_function_call_expr (fndecl, arglist);
3514 return expand_call (exp, target, target == const0_rtx);
3516 #endif
3517 return 0;
3520 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3521 if we failed the caller should emit a normal call, otherwise try to get
3522 the result in TARGET, if convenient. */
3524 static rtx
3525 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3527 tree arglist = TREE_OPERAND (exp, 1);
3529 if (!validate_arglist (arglist,
3530 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3531 return 0;
3532 else
3534 tree result = fold_builtin_strncmp (arglist);
3535 if (result)
3536 return expand_expr (result, target, mode, EXPAND_NORMAL);
3539 /* If c_strlen can determine an expression for one of the string
3540 lengths, and it doesn't have side effects, then emit cmpstrsi
3541 using length MIN(strlen(string)+1, arg3). */
3542 #ifdef HAVE_cmpstrsi
3543 if (HAVE_cmpstrsi)
3545 tree arg1 = TREE_VALUE (arglist);
3546 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3547 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3548 tree len, len1, len2;
3549 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3550 rtx result, insn;
3551 tree fndecl;
3553 int arg1_align
3554 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3555 int arg2_align
3556 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3557 enum machine_mode insn_mode
3558 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3560 len1 = c_strlen (arg1, 1);
3561 len2 = c_strlen (arg2, 1);
3563 if (len1)
3564 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3565 if (len2)
3566 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3568 /* If we don't have a constant length for the first, use the length
3569 of the second, if we know it. We don't require a constant for
3570 this case; some cost analysis could be done if both are available
3571 but neither is constant. For now, assume they're equally cheap,
3572 unless one has side effects. If both strings have constant lengths,
3573 use the smaller. */
3575 if (!len1)
3576 len = len2;
3577 else if (!len2)
3578 len = len1;
3579 else if (TREE_SIDE_EFFECTS (len1))
3580 len = len2;
3581 else if (TREE_SIDE_EFFECTS (len2))
3582 len = len1;
3583 else if (TREE_CODE (len1) != INTEGER_CST)
3584 len = len2;
3585 else if (TREE_CODE (len2) != INTEGER_CST)
3586 len = len1;
3587 else if (tree_int_cst_lt (len1, len2))
3588 len = len1;
3589 else
3590 len = len2;
3592 /* If both arguments have side effects, we cannot optimize. */
3593 if (!len || TREE_SIDE_EFFECTS (len))
3594 return 0;
3596 /* The actual new length parameter is MIN(len,arg3). */
3597 len = fold (build2 (MIN_EXPR, TREE_TYPE (len), len,
3598 fold_convert (TREE_TYPE (len), arg3)));
3600 /* If we don't have POINTER_TYPE, call the function. */
3601 if (arg1_align == 0 || arg2_align == 0)
3602 return 0;
3604 /* Make a place to write the result of the instruction. */
3605 result = target;
3606 if (! (result != 0
3607 && REG_P (result) && GET_MODE (result) == insn_mode
3608 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3609 result = gen_reg_rtx (insn_mode);
3611 /* Stabilize the arguments in case gen_cmpstrsi fails. */
3612 arg1 = builtin_save_expr (arg1);
3613 arg2 = builtin_save_expr (arg2);
3614 len = builtin_save_expr (len);
3616 arg1_rtx = get_memory_rtx (arg1);
3617 arg2_rtx = get_memory_rtx (arg2);
3618 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
3619 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3620 GEN_INT (MIN (arg1_align, arg2_align)));
3621 if (insn)
3623 emit_insn (insn);
3625 /* Return the value in the proper mode for this function. */
3626 mode = TYPE_MODE (TREE_TYPE (exp));
3627 if (GET_MODE (result) == mode)
3628 return result;
3629 if (target == 0)
3630 return convert_to_mode (mode, result, 0);
3631 convert_move (target, result, 0);
3632 return target;
3635 /* Expand the library call ourselves using a stabilized argument
3636 list to avoid re-evaluating the function's arguments twice. */
3637 arglist = build_tree_list (NULL_TREE, len);
3638 arglist = tree_cons (NULL_TREE, arg2, arglist);
3639 arglist = tree_cons (NULL_TREE, arg1, arglist);
3640 fndecl = get_callee_fndecl (exp);
3641 exp = build_function_call_expr (fndecl, arglist);
3642 return expand_call (exp, target, target == const0_rtx);
3644 #endif
3645 return 0;
3648 /* Expand expression EXP, which is a call to the strcat builtin.
3649 Return 0 if we failed the caller should emit a normal call,
3650 otherwise try to get the result in TARGET, if convenient. */
3652 static rtx
3653 expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
3655 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3656 return 0;
3657 else
3659 tree dst = TREE_VALUE (arglist),
3660 src = TREE_VALUE (TREE_CHAIN (arglist));
3661 const char *p = c_getstr (src);
3663 if (p)
3665 /* If the string length is zero, return the dst parameter. */
3666 if (*p == '\0')
3667 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3668 else if (!optimize_size)
3670 /* Otherwise if !optimize_size, see if we can store by
3671 pieces into (dst + strlen(dst)). */
3672 tree newdst, arglist,
3673 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
3675 /* This is the length argument. */
3676 arglist = build_tree_list (NULL_TREE,
3677 fold (size_binop (PLUS_EXPR,
3678 c_strlen (src, 0),
3679 ssize_int (1))));
3680 /* Prepend src argument. */
3681 arglist = tree_cons (NULL_TREE, src, arglist);
3683 /* We're going to use dst more than once. */
3684 dst = builtin_save_expr (dst);
3686 /* Create strlen (dst). */
3687 newdst =
3688 fold (build_function_call_expr (strlen_fn,
3689 build_tree_list (NULL_TREE,
3690 dst)));
3691 /* Create (dst + strlen (dst)). */
3692 newdst = fold (build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst));
3694 /* Prepend the new dst argument. */
3695 arglist = tree_cons (NULL_TREE, newdst, arglist);
3697 /* We don't want to get turned into a memcpy if the
3698 target is const0_rtx, i.e. when the return value
3699 isn't used. That would produce pessimized code so
3700 pass in a target of zero, it should never actually be
3701 used. If this was successful return the original
3702 dst, not the result of mempcpy. */
3703 if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
3704 return expand_expr (dst, target, mode, EXPAND_NORMAL);
3705 else
3706 return 0;
3710 return 0;
3714 /* Expand expression EXP, which is a call to the strncat builtin.
3715 Return 0 if we failed the caller should emit a normal call,
3716 otherwise try to get the result in TARGET, if convenient. */
3718 static rtx
3719 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
3721 if (validate_arglist (arglist,
3722 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3724 tree result = fold_builtin_strncat (arglist);
3725 if (result)
3726 return expand_expr (result, target, mode, EXPAND_NORMAL);
3728 return 0;
3731 /* Expand expression EXP, which is a call to the strspn builtin.
3732 Return 0 if we failed the caller should emit a normal call,
3733 otherwise try to get the result in TARGET, if convenient. */
3735 static rtx
3736 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
3738 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3740 tree result = fold_builtin_strspn (arglist);
3741 if (result)
3742 return expand_expr (result, target, mode, EXPAND_NORMAL);
3744 return 0;
3747 /* Expand expression EXP, which is a call to the strcspn builtin.
3748 Return 0 if we failed the caller should emit a normal call,
3749 otherwise try to get the result in TARGET, if convenient. */
3751 static rtx
3752 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
3754 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3756 tree result = fold_builtin_strcspn (arglist);
3757 if (result)
3758 return expand_expr (result, target, mode, EXPAND_NORMAL);
3760 return 0;
3763 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3764 if that's convenient. */
3767 expand_builtin_saveregs (void)
3769 rtx val, seq;
3771 /* Don't do __builtin_saveregs more than once in a function.
3772 Save the result of the first call and reuse it. */
3773 if (saveregs_value != 0)
3774 return saveregs_value;
3776 /* When this function is called, it means that registers must be
3777 saved on entry to this function. So we migrate the call to the
3778 first insn of this function. */
3780 start_sequence ();
3782 /* Do whatever the machine needs done in this case. */
3783 val = targetm.calls.expand_builtin_saveregs ();
3785 seq = get_insns ();
3786 end_sequence ();
3788 saveregs_value = val;
3790 /* Put the insns after the NOTE that starts the function. If this
3791 is inside a start_sequence, make the outer-level insn chain current, so
3792 the code is placed at the start of the function. */
3793 push_topmost_sequence ();
3794 emit_insn_after (seq, entry_of_function ());
3795 pop_topmost_sequence ();
3797 return val;
3800 /* __builtin_args_info (N) returns word N of the arg space info
3801 for the current function. The number and meanings of words
3802 is controlled by the definition of CUMULATIVE_ARGS. */
3804 static rtx
3805 expand_builtin_args_info (tree arglist)
3807 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
3808 int *word_ptr = (int *) &current_function_args_info;
3810 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
3812 if (arglist != 0)
3814 if (!host_integerp (TREE_VALUE (arglist), 0))
3815 error ("argument of %<__builtin_args_info%> must be constant");
3816 else
3818 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
3820 if (wordnum < 0 || wordnum >= nwords)
3821 error ("argument of %<__builtin_args_info%> out of range");
3822 else
3823 return GEN_INT (word_ptr[wordnum]);
3826 else
3827 error ("missing argument in %<__builtin_args_info%>");
3829 return const0_rtx;
3832 /* Expand a call to __builtin_next_arg. */
3834 static rtx
3835 expand_builtin_next_arg (void)
3837 /* Checking arguments is already done in fold_builtin_next_arg
3838 that must be called before this function. */
3839 return expand_binop (Pmode, add_optab,
3840 current_function_internal_arg_pointer,
3841 current_function_arg_offset_rtx,
3842 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3845 /* Make it easier for the backends by protecting the valist argument
3846 from multiple evaluations. */
3848 static tree
3849 stabilize_va_list (tree valist, int needs_lvalue)
3851 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3853 if (TREE_SIDE_EFFECTS (valist))
3854 valist = save_expr (valist);
3856 /* For this case, the backends will be expecting a pointer to
3857 TREE_TYPE (va_list_type_node), but it's possible we've
3858 actually been given an array (an actual va_list_type_node).
3859 So fix it. */
3860 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
3862 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
3863 valist = build_fold_addr_expr_with_type (valist, p1);
3866 else
3868 tree pt;
3870 if (! needs_lvalue)
3872 if (! TREE_SIDE_EFFECTS (valist))
3873 return valist;
3875 pt = build_pointer_type (va_list_type_node);
3876 valist = fold (build1 (ADDR_EXPR, pt, valist));
3877 TREE_SIDE_EFFECTS (valist) = 1;
3880 if (TREE_SIDE_EFFECTS (valist))
3881 valist = save_expr (valist);
3882 valist = build_fold_indirect_ref (valist);
3885 return valist;
3888 /* The "standard" definition of va_list is void*. */
3890 tree
3891 std_build_builtin_va_list (void)
3893 return ptr_type_node;
3896 /* The "standard" implementation of va_start: just assign `nextarg' to
3897 the variable. */
3899 void
3900 std_expand_builtin_va_start (tree valist, rtx nextarg)
3902 tree t;
3904 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
3905 make_tree (ptr_type_node, nextarg));
3906 TREE_SIDE_EFFECTS (t) = 1;
3908 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3911 /* Expand ARGLIST, from a call to __builtin_va_start. */
3913 static rtx
3914 expand_builtin_va_start (tree arglist)
3916 rtx nextarg;
3917 tree chain, valist;
3919 chain = TREE_CHAIN (arglist);
3921 if (!chain)
3923 error ("too few arguments to function %<va_start%>");
3924 return const0_rtx;
3927 if (fold_builtin_next_arg (chain))
3928 return const0_rtx;
3930 nextarg = expand_builtin_next_arg ();
3931 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
3933 #ifdef EXPAND_BUILTIN_VA_START
3934 EXPAND_BUILTIN_VA_START (valist, nextarg);
3935 #else
3936 std_expand_builtin_va_start (valist, nextarg);
3937 #endif
3939 return const0_rtx;
3942 /* The "standard" implementation of va_arg: read the value from the
3943 current (padded) address and increment by the (padded) size. */
3945 tree
3946 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
3948 tree addr, t, type_size, rounded_size, valist_tmp;
3949 unsigned HOST_WIDE_INT align, boundary;
3950 bool indirect;
3952 #ifdef ARGS_GROW_DOWNWARD
3953 /* All of the alignment and movement below is for args-grow-up machines.
3954 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
3955 implement their own specialized gimplify_va_arg_expr routines. */
3956 gcc_unreachable ();
3957 #endif
3959 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3960 if (indirect)
3961 type = build_pointer_type (type);
3963 align = PARM_BOUNDARY / BITS_PER_UNIT;
3964 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
3966 /* Hoist the valist value into a temporary for the moment. */
3967 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
3969 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
3970 requires greater alignment, we must perform dynamic alignment. */
3971 if (boundary > align)
3973 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
3974 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3975 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
3976 gimplify_and_add (t, pre_p);
3978 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
3979 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
3980 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
3981 gimplify_and_add (t, pre_p);
3983 else
3984 boundary = align;
3986 /* If the actual alignment is less than the alignment of the type,
3987 adjust the type accordingly so that we don't assume strict alignment
3988 when deferencing the pointer. */
3989 boundary *= BITS_PER_UNIT;
3990 if (boundary < TYPE_ALIGN (type))
3992 type = build_variant_type_copy (type);
3993 TYPE_ALIGN (type) = boundary;
3996 /* Compute the rounded size of the type. */
3997 type_size = size_in_bytes (type);
3998 rounded_size = round_up (type_size, align);
4000 /* Reduce rounded_size so it's sharable with the postqueue. */
4001 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4003 /* Get AP. */
4004 addr = valist_tmp;
4005 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4007 /* Small args are padded downward. */
4008 t = fold (build2 (GT_EXPR, sizetype, rounded_size, size_int (align)));
4009 t = fold (build3 (COND_EXPR, sizetype, t, size_zero_node,
4010 size_binop (MINUS_EXPR, rounded_size, type_size)));
4011 t = fold_convert (TREE_TYPE (addr), t);
4012 addr = fold (build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t));
4015 /* Compute new value for AP. */
4016 t = fold_convert (TREE_TYPE (valist), rounded_size);
4017 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4018 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4019 gimplify_and_add (t, pre_p);
4021 addr = fold_convert (build_pointer_type (type), addr);
4023 if (indirect)
4024 addr = build_va_arg_indirect_ref (addr);
4026 return build_va_arg_indirect_ref (addr);
4029 /* Build an indirect-ref expression over the given TREE, which represents a
4030 piece of a va_arg() expansion. */
4031 tree
4032 build_va_arg_indirect_ref (tree addr)
4034 addr = build_fold_indirect_ref (addr);
4036 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4037 mf_mark (addr);
4039 return addr;
4042 /* Return a dummy expression of type TYPE in order to keep going after an
4043 error. */
4045 static tree
4046 dummy_object (tree type)
4048 tree t = convert (build_pointer_type (type), null_pointer_node);
4049 return build1 (INDIRECT_REF, type, t);
4052 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4053 builtin function, but a very special sort of operator. */
4055 enum gimplify_status
4056 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4058 tree promoted_type, want_va_type, have_va_type;
4059 tree valist = TREE_OPERAND (*expr_p, 0);
4060 tree type = TREE_TYPE (*expr_p);
4061 tree t;
4063 /* Verify that valist is of the proper type. */
4064 want_va_type = va_list_type_node;
4065 have_va_type = TREE_TYPE (valist);
4067 if (have_va_type == error_mark_node)
4068 return GS_ERROR;
4070 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4072 /* If va_list is an array type, the argument may have decayed
4073 to a pointer type, e.g. by being passed to another function.
4074 In that case, unwrap both types so that we can compare the
4075 underlying records. */
4076 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4077 || POINTER_TYPE_P (have_va_type))
4079 want_va_type = TREE_TYPE (want_va_type);
4080 have_va_type = TREE_TYPE (have_va_type);
4084 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4086 error ("first argument to %<va_arg%> not of type %<va_list%>");
4087 return GS_ERROR;
4090 /* Generate a diagnostic for requesting data of a type that cannot
4091 be passed through `...' due to type promotion at the call site. */
4092 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4093 != type)
4095 static bool gave_help;
4097 /* Unfortunately, this is merely undefined, rather than a constraint
4098 violation, so we cannot make this an error. If this call is never
4099 executed, the program is still strictly conforming. */
4100 warning ("%qT is promoted to %qT when passed through %<...%>",
4101 type, promoted_type);
4102 if (! gave_help)
4104 gave_help = true;
4105 warning ("(so you should pass %qT not %qT to %<va_arg%>)",
4106 promoted_type, type);
4109 /* We can, however, treat "undefined" any way we please.
4110 Call abort to encourage the user to fix the program. */
4111 inform ("if this code is reached, the program will abort");
4112 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4113 NULL);
4114 append_to_statement_list (t, pre_p);
4116 /* This is dead code, but go ahead and finish so that the
4117 mode of the result comes out right. */
4118 *expr_p = dummy_object (type);
4119 return GS_ALL_DONE;
4121 else
4123 /* Make it easier for the backends by protecting the valist argument
4124 from multiple evaluations. */
4125 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4127 /* For this case, the backends will be expecting a pointer to
4128 TREE_TYPE (va_list_type_node), but it's possible we've
4129 actually been given an array (an actual va_list_type_node).
4130 So fix it. */
4131 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4133 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4134 valist = build_fold_addr_expr_with_type (valist, p1);
4136 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4138 else
4139 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4141 if (!targetm.gimplify_va_arg_expr)
4142 /* Once most targets are converted this should abort. */
4143 return GS_ALL_DONE;
4145 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4146 return GS_OK;
4150 /* Expand ARGLIST, from a call to __builtin_va_end. */
4152 static rtx
4153 expand_builtin_va_end (tree arglist)
4155 tree valist = TREE_VALUE (arglist);
4157 /* Evaluate for side effects, if needed. I hate macros that don't
4158 do that. */
4159 if (TREE_SIDE_EFFECTS (valist))
4160 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4162 return const0_rtx;
4165 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4166 builtin rather than just as an assignment in stdarg.h because of the
4167 nastiness of array-type va_list types. */
4169 static rtx
4170 expand_builtin_va_copy (tree arglist)
4172 tree dst, src, t;
4174 dst = TREE_VALUE (arglist);
4175 src = TREE_VALUE (TREE_CHAIN (arglist));
4177 dst = stabilize_va_list (dst, 1);
4178 src = stabilize_va_list (src, 0);
4180 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4182 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4183 TREE_SIDE_EFFECTS (t) = 1;
4184 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4186 else
4188 rtx dstb, srcb, size;
4190 /* Evaluate to pointers. */
4191 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4192 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4193 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4194 VOIDmode, EXPAND_NORMAL);
4196 dstb = convert_memory_address (Pmode, dstb);
4197 srcb = convert_memory_address (Pmode, srcb);
4199 /* "Dereference" to BLKmode memories. */
4200 dstb = gen_rtx_MEM (BLKmode, dstb);
4201 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4202 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4203 srcb = gen_rtx_MEM (BLKmode, srcb);
4204 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4205 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4207 /* Copy. */
4208 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4211 return const0_rtx;
4214 /* Expand a call to one of the builtin functions __builtin_frame_address or
4215 __builtin_return_address. */
4217 static rtx
4218 expand_builtin_frame_address (tree fndecl, tree arglist)
4220 /* The argument must be a nonnegative integer constant.
4221 It counts the number of frames to scan up the stack.
4222 The value is the return address saved in that frame. */
4223 if (arglist == 0)
4224 /* Warning about missing arg was already issued. */
4225 return const0_rtx;
4226 else if (! host_integerp (TREE_VALUE (arglist), 1))
4228 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4229 error ("invalid argument to %<__builtin_frame_address%>");
4230 else
4231 error ("invalid argument to %<__builtin_return_address%>");
4232 return const0_rtx;
4234 else
4236 rtx tem
4237 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4238 tree_low_cst (TREE_VALUE (arglist), 1));
4240 /* Some ports cannot access arbitrary stack frames. */
4241 if (tem == NULL)
4243 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4244 warning ("unsupported argument to %<__builtin_frame_address%>");
4245 else
4246 warning ("unsupported argument to %<__builtin_return_address%>");
4247 return const0_rtx;
4250 /* For __builtin_frame_address, return what we've got. */
4251 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4252 return tem;
4254 if (!REG_P (tem)
4255 && ! CONSTANT_P (tem))
4256 tem = copy_to_mode_reg (Pmode, tem);
4257 return tem;
4261 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4262 we failed and the caller should emit a normal call, otherwise try to get
4263 the result in TARGET, if convenient. */
4265 static rtx
4266 expand_builtin_alloca (tree arglist, rtx target)
4268 rtx op0;
4269 rtx result;
4271 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4272 should always expand to function calls. These can be intercepted
4273 in libmudflap. */
4274 if (flag_mudflap)
4275 return 0;
4277 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4278 return 0;
4280 /* Compute the argument. */
4281 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4283 /* Allocate the desired space. */
4284 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4285 result = convert_memory_address (ptr_mode, result);
4287 return result;
4290 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4291 Return 0 if a normal call should be emitted rather than expanding the
4292 function in-line. If convenient, the result should be placed in TARGET.
4293 SUBTARGET may be used as the target for computing one of EXP's operands. */
4295 static rtx
4296 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4297 rtx subtarget, optab op_optab)
4299 rtx op0;
4300 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4301 return 0;
4303 /* Compute the argument. */
4304 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4305 /* Compute op, into TARGET if possible.
4306 Set TARGET to wherever the result comes back. */
4307 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4308 op_optab, op0, target, 1);
4309 gcc_assert (target);
4311 return convert_to_mode (target_mode, target, 0);
4314 /* If the string passed to fputs is a constant and is one character
4315 long, we attempt to transform this call into __builtin_fputc(). */
4317 static rtx
4318 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4320 /* Verify the arguments in the original call. */
4321 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4323 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4324 unlocked, NULL_TREE);
4325 if (result)
4326 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4328 return 0;
4331 /* Expand a call to __builtin_expect. We return our argument and emit a
4332 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4333 a non-jump context. */
4335 static rtx
4336 expand_builtin_expect (tree arglist, rtx target)
4338 tree exp, c;
4339 rtx note, rtx_c;
4341 if (arglist == NULL_TREE
4342 || TREE_CHAIN (arglist) == NULL_TREE)
4343 return const0_rtx;
4344 exp = TREE_VALUE (arglist);
4345 c = TREE_VALUE (TREE_CHAIN (arglist));
4347 if (TREE_CODE (c) != INTEGER_CST)
4349 error ("second argument to %<__builtin_expect%> must be a constant");
4350 c = integer_zero_node;
4353 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4355 /* Don't bother with expected value notes for integral constants. */
4356 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4358 /* We do need to force this into a register so that we can be
4359 moderately sure to be able to correctly interpret the branch
4360 condition later. */
4361 target = force_reg (GET_MODE (target), target);
4363 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4365 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4366 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4369 return target;
4372 /* Like expand_builtin_expect, except do this in a jump context. This is
4373 called from do_jump if the conditional is a __builtin_expect. Return either
4374 a list of insns to emit the jump or NULL if we cannot optimize
4375 __builtin_expect. We need to optimize this at jump time so that machines
4376 like the PowerPC don't turn the test into a SCC operation, and then jump
4377 based on the test being 0/1. */
4380 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4382 tree arglist = TREE_OPERAND (exp, 1);
4383 tree arg0 = TREE_VALUE (arglist);
4384 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4385 rtx ret = NULL_RTX;
4387 /* Only handle __builtin_expect (test, 0) and
4388 __builtin_expect (test, 1). */
4389 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4390 && (integer_zerop (arg1) || integer_onep (arg1)))
4392 rtx insn, drop_through_label, temp;
4394 /* Expand the jump insns. */
4395 start_sequence ();
4396 do_jump (arg0, if_false_label, if_true_label);
4397 ret = get_insns ();
4399 drop_through_label = get_last_insn ();
4400 if (drop_through_label && NOTE_P (drop_through_label))
4401 drop_through_label = prev_nonnote_insn (drop_through_label);
4402 if (drop_through_label && !LABEL_P (drop_through_label))
4403 drop_through_label = NULL_RTX;
4404 end_sequence ();
4406 if (! if_true_label)
4407 if_true_label = drop_through_label;
4408 if (! if_false_label)
4409 if_false_label = drop_through_label;
4411 /* Go through and add the expect's to each of the conditional jumps. */
4412 insn = ret;
4413 while (insn != NULL_RTX)
4415 rtx next = NEXT_INSN (insn);
4417 if (JUMP_P (insn) && any_condjump_p (insn))
4419 rtx ifelse = SET_SRC (pc_set (insn));
4420 rtx then_dest = XEXP (ifelse, 1);
4421 rtx else_dest = XEXP (ifelse, 2);
4422 int taken = -1;
4424 /* First check if we recognize any of the labels. */
4425 if (GET_CODE (then_dest) == LABEL_REF
4426 && XEXP (then_dest, 0) == if_true_label)
4427 taken = 1;
4428 else if (GET_CODE (then_dest) == LABEL_REF
4429 && XEXP (then_dest, 0) == if_false_label)
4430 taken = 0;
4431 else if (GET_CODE (else_dest) == LABEL_REF
4432 && XEXP (else_dest, 0) == if_false_label)
4433 taken = 1;
4434 else if (GET_CODE (else_dest) == LABEL_REF
4435 && XEXP (else_dest, 0) == if_true_label)
4436 taken = 0;
4437 /* Otherwise check where we drop through. */
4438 else if (else_dest == pc_rtx)
4440 if (next && NOTE_P (next))
4441 next = next_nonnote_insn (next);
4443 if (next && JUMP_P (next)
4444 && any_uncondjump_p (next))
4445 temp = XEXP (SET_SRC (pc_set (next)), 0);
4446 else
4447 temp = next;
4449 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4450 else that can't possibly match either target label. */
4451 if (temp == if_false_label)
4452 taken = 1;
4453 else if (temp == if_true_label)
4454 taken = 0;
4456 else if (then_dest == pc_rtx)
4458 if (next && NOTE_P (next))
4459 next = next_nonnote_insn (next);
4461 if (next && JUMP_P (next)
4462 && any_uncondjump_p (next))
4463 temp = XEXP (SET_SRC (pc_set (next)), 0);
4464 else
4465 temp = next;
4467 if (temp == if_false_label)
4468 taken = 0;
4469 else if (temp == if_true_label)
4470 taken = 1;
4473 if (taken != -1)
4475 /* If the test is expected to fail, reverse the
4476 probabilities. */
4477 if (integer_zerop (arg1))
4478 taken = 1 - taken;
4479 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4483 insn = next;
4487 return ret;
4490 static void
4491 expand_builtin_trap (void)
4493 #ifdef HAVE_trap
4494 if (HAVE_trap)
4495 emit_insn (gen_trap ());
4496 else
4497 #endif
4498 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4499 emit_barrier ();
4502 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4503 Return 0 if a normal call should be emitted rather than expanding
4504 the function inline. If convenient, the result should be placed
4505 in TARGET. SUBTARGET may be used as the target for computing
4506 the operand. */
4508 static rtx
4509 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4511 enum machine_mode mode;
4512 tree arg;
4513 rtx op0;
4515 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4516 return 0;
4518 arg = TREE_VALUE (arglist);
4519 mode = TYPE_MODE (TREE_TYPE (arg));
4520 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4521 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4524 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4525 Return NULL is a normal call should be emitted rather than expanding the
4526 function inline. If convenient, the result should be placed in TARGET.
4527 SUBTARGET may be used as the target for computing the operand. */
4529 static rtx
4530 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4532 rtx op0, op1;
4533 tree arg;
4535 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4536 return 0;
4538 arg = TREE_VALUE (arglist);
4539 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4541 arg = TREE_VALUE (TREE_CHAIN (arglist));
4542 op1 = expand_expr (arg, NULL, VOIDmode, 0);
4544 return expand_copysign (op0, op1, target);
4547 /* Create a new constant string literal and return a char* pointer to it.
4548 The STRING_CST value is the LEN characters at STR. */
4549 static tree
4550 build_string_literal (int len, const char *str)
4552 tree t, elem, index, type;
4554 t = build_string (len, str);
4555 elem = build_type_variant (char_type_node, 1, 0);
4556 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4557 type = build_array_type (elem, index);
4558 TREE_TYPE (t) = type;
4559 TREE_CONSTANT (t) = 1;
4560 TREE_INVARIANT (t) = 1;
4561 TREE_READONLY (t) = 1;
4562 TREE_STATIC (t) = 1;
4564 type = build_pointer_type (type);
4565 t = build1 (ADDR_EXPR, type, t);
4567 type = build_pointer_type (elem);
4568 t = build1 (NOP_EXPR, type, t);
4569 return t;
4572 /* Expand a call to printf or printf_unlocked with argument list ARGLIST.
4573 Return 0 if a normal call should be emitted rather than transforming
4574 the function inline. If convenient, the result should be placed in
4575 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4576 call. */
4577 static rtx
4578 expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode,
4579 bool unlocked)
4581 tree fn_putchar = unlocked
4582 ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4583 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4584 tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4585 : implicit_built_in_decls[BUILT_IN_PUTS];
4586 const char *fmt_str;
4587 tree fn, fmt, arg;
4589 /* If the return value is used, don't do the transformation. */
4590 if (target != const0_rtx)
4591 return 0;
4593 /* Verify the required arguments in the original call. */
4594 if (! arglist)
4595 return 0;
4596 fmt = TREE_VALUE (arglist);
4597 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4598 return 0;
4599 arglist = TREE_CHAIN (arglist);
4601 /* Check whether the format is a literal string constant. */
4602 fmt_str = c_getstr (fmt);
4603 if (fmt_str == NULL)
4604 return 0;
4606 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4607 if (strcmp (fmt_str, "%s\n") == 0)
4609 if (! arglist
4610 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4611 || TREE_CHAIN (arglist))
4612 return 0;
4613 fn = fn_puts;
4615 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4616 else if (strcmp (fmt_str, "%c") == 0)
4618 if (! arglist
4619 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4620 || TREE_CHAIN (arglist))
4621 return 0;
4622 fn = fn_putchar;
4624 else
4626 /* We can't handle anything else with % args or %% ... yet. */
4627 if (strchr (fmt_str, '%'))
4628 return 0;
4630 if (arglist)
4631 return 0;
4633 /* If the format specifier was "", printf does nothing. */
4634 if (fmt_str[0] == '\0')
4635 return const0_rtx;
4636 /* If the format specifier has length of 1, call putchar. */
4637 if (fmt_str[1] == '\0')
4639 /* Given printf("c"), (where c is any one character,)
4640 convert "c"[0] to an int and pass that to the replacement
4641 function. */
4642 arg = build_int_cst (NULL_TREE, fmt_str[0]);
4643 arglist = build_tree_list (NULL_TREE, arg);
4644 fn = fn_putchar;
4646 else
4648 /* If the format specifier was "string\n", call puts("string"). */
4649 size_t len = strlen (fmt_str);
4650 if (fmt_str[len - 1] == '\n')
4652 /* Create a NUL-terminated string that's one char shorter
4653 than the original, stripping off the trailing '\n'. */
4654 char *newstr = alloca (len);
4655 memcpy (newstr, fmt_str, len - 1);
4656 newstr[len - 1] = 0;
4658 arg = build_string_literal (len, newstr);
4659 arglist = build_tree_list (NULL_TREE, arg);
4660 fn = fn_puts;
4662 else
4663 /* We'd like to arrange to call fputs(string,stdout) here,
4664 but we need stdout and don't have a way to get it yet. */
4665 return 0;
4669 if (!fn)
4670 return 0;
4671 return expand_expr (build_function_call_expr (fn, arglist),
4672 target, mode, EXPAND_NORMAL);
4675 /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST.
4676 Return 0 if a normal call should be emitted rather than transforming
4677 the function inline. If convenient, the result should be placed in
4678 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
4679 call. */
4680 static rtx
4681 expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode,
4682 bool unlocked)
4684 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
4685 : implicit_built_in_decls[BUILT_IN_FPUTC];
4686 tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
4687 : implicit_built_in_decls[BUILT_IN_FPUTS];
4688 const char *fmt_str;
4689 tree fn, fmt, fp, arg;
4691 /* If the return value is used, don't do the transformation. */
4692 if (target != const0_rtx)
4693 return 0;
4695 /* Verify the required arguments in the original call. */
4696 if (! arglist)
4697 return 0;
4698 fp = TREE_VALUE (arglist);
4699 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
4700 return 0;
4701 arglist = TREE_CHAIN (arglist);
4702 if (! arglist)
4703 return 0;
4704 fmt = TREE_VALUE (arglist);
4705 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4706 return 0;
4707 arglist = TREE_CHAIN (arglist);
4709 /* Check whether the format is a literal string constant. */
4710 fmt_str = c_getstr (fmt);
4711 if (fmt_str == NULL)
4712 return 0;
4714 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
4715 if (strcmp (fmt_str, "%s") == 0)
4717 if (! arglist
4718 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4719 || TREE_CHAIN (arglist))
4720 return 0;
4721 arg = TREE_VALUE (arglist);
4722 arglist = build_tree_list (NULL_TREE, fp);
4723 arglist = tree_cons (NULL_TREE, arg, arglist);
4724 fn = fn_fputs;
4726 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
4727 else if (strcmp (fmt_str, "%c") == 0)
4729 if (! arglist
4730 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4731 || TREE_CHAIN (arglist))
4732 return 0;
4733 arg = TREE_VALUE (arglist);
4734 arglist = build_tree_list (NULL_TREE, fp);
4735 arglist = tree_cons (NULL_TREE, arg, arglist);
4736 fn = fn_fputc;
4738 else
4740 /* We can't handle anything else with % args or %% ... yet. */
4741 if (strchr (fmt_str, '%'))
4742 return 0;
4744 if (arglist)
4745 return 0;
4747 /* If the format specifier was "", fprintf does nothing. */
4748 if (fmt_str[0] == '\0')
4750 /* Evaluate and ignore FILE* argument for side-effects. */
4751 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4752 return const0_rtx;
4755 /* When "string" doesn't contain %, replace all cases of
4756 fprintf(stream,string) with fputs(string,stream). The fputs
4757 builtin will take care of special cases like length == 1. */
4758 arglist = build_tree_list (NULL_TREE, fp);
4759 arglist = tree_cons (NULL_TREE, fmt, arglist);
4760 fn = fn_fputs;
4763 if (!fn)
4764 return 0;
4765 return expand_expr (build_function_call_expr (fn, arglist),
4766 target, mode, EXPAND_NORMAL);
4769 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
4770 a normal call should be emitted rather than expanding the function
4771 inline. If convenient, the result should be placed in TARGET with
4772 mode MODE. */
4774 static rtx
4775 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
4777 tree orig_arglist, dest, fmt;
4778 const char *fmt_str;
4780 orig_arglist = arglist;
4782 /* Verify the required arguments in the original call. */
4783 if (! arglist)
4784 return 0;
4785 dest = TREE_VALUE (arglist);
4786 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
4787 return 0;
4788 arglist = TREE_CHAIN (arglist);
4789 if (! arglist)
4790 return 0;
4791 fmt = TREE_VALUE (arglist);
4792 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4793 return 0;
4794 arglist = TREE_CHAIN (arglist);
4796 /* Check whether the format is a literal string constant. */
4797 fmt_str = c_getstr (fmt);
4798 if (fmt_str == NULL)
4799 return 0;
4801 /* If the format doesn't contain % args or %%, use strcpy. */
4802 if (strchr (fmt_str, '%') == 0)
4804 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4805 tree exp;
4807 if (arglist || ! fn)
4808 return 0;
4809 expand_expr (build_function_call_expr (fn, orig_arglist),
4810 const0_rtx, VOIDmode, EXPAND_NORMAL);
4811 if (target == const0_rtx)
4812 return const0_rtx;
4813 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
4814 return expand_expr (exp, target, mode, EXPAND_NORMAL);
4816 /* If the format is "%s", use strcpy if the result isn't used. */
4817 else if (strcmp (fmt_str, "%s") == 0)
4819 tree fn, arg, len;
4820 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
4822 if (! fn)
4823 return 0;
4825 if (! arglist || TREE_CHAIN (arglist))
4826 return 0;
4827 arg = TREE_VALUE (arglist);
4828 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
4829 return 0;
4831 if (target != const0_rtx)
4833 len = c_strlen (arg, 1);
4834 if (! len || TREE_CODE (len) != INTEGER_CST)
4835 return 0;
4837 else
4838 len = NULL_TREE;
4840 arglist = build_tree_list (NULL_TREE, arg);
4841 arglist = tree_cons (NULL_TREE, dest, arglist);
4842 expand_expr (build_function_call_expr (fn, arglist),
4843 const0_rtx, VOIDmode, EXPAND_NORMAL);
4845 if (target == const0_rtx)
4846 return const0_rtx;
4847 return expand_expr (len, target, mode, EXPAND_NORMAL);
4850 return 0;
4853 /* Expand a call to either the entry or exit function profiler. */
4855 static rtx
4856 expand_builtin_profile_func (bool exitp)
4858 rtx this, which;
4860 this = DECL_RTL (current_function_decl);
4861 gcc_assert (MEM_P (this));
4862 this = XEXP (this, 0);
4864 if (exitp)
4865 which = profile_function_exit_libfunc;
4866 else
4867 which = profile_function_entry_libfunc;
4869 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
4870 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
4872 Pmode);
4874 return const0_rtx;
4877 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4879 static rtx
4880 round_trampoline_addr (rtx tramp)
4882 rtx temp, addend, mask;
4884 /* If we don't need too much alignment, we'll have been guaranteed
4885 proper alignment by get_trampoline_type. */
4886 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4887 return tramp;
4889 /* Round address up to desired boundary. */
4890 temp = gen_reg_rtx (Pmode);
4891 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4892 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4894 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4895 temp, 0, OPTAB_LIB_WIDEN);
4896 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4897 temp, 0, OPTAB_LIB_WIDEN);
4899 return tramp;
4902 static rtx
4903 expand_builtin_init_trampoline (tree arglist)
4905 tree t_tramp, t_func, t_chain;
4906 rtx r_tramp, r_func, r_chain;
4907 #ifdef TRAMPOLINE_TEMPLATE
4908 rtx blktramp;
4909 #endif
4911 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
4912 POINTER_TYPE, VOID_TYPE))
4913 return NULL_RTX;
4915 t_tramp = TREE_VALUE (arglist);
4916 arglist = TREE_CHAIN (arglist);
4917 t_func = TREE_VALUE (arglist);
4918 arglist = TREE_CHAIN (arglist);
4919 t_chain = TREE_VALUE (arglist);
4921 r_tramp = expand_expr (t_tramp, NULL_RTX, VOIDmode, 0);
4922 r_func = expand_expr (t_func, NULL_RTX, VOIDmode, 0);
4923 r_chain = expand_expr (t_chain, NULL_RTX, VOIDmode, 0);
4925 /* Generate insns to initialize the trampoline. */
4926 r_tramp = round_trampoline_addr (r_tramp);
4927 #ifdef TRAMPOLINE_TEMPLATE
4928 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
4929 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
4930 emit_block_move (blktramp, assemble_trampoline_template (),
4931 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
4932 #endif
4933 trampolines_created = 1;
4934 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
4936 return const0_rtx;
4939 static rtx
4940 expand_builtin_adjust_trampoline (tree arglist)
4942 rtx tramp;
4944 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
4945 return NULL_RTX;
4947 tramp = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
4948 tramp = round_trampoline_addr (tramp);
4949 #ifdef TRAMPOLINE_ADJUST_ADDRESS
4950 TRAMPOLINE_ADJUST_ADDRESS (tramp);
4951 #endif
4953 return tramp;
4956 /* Expand a call to the built-in signbit, signbitf or signbitl function.
4957 Return NULL_RTX if a normal call should be emitted rather than expanding
4958 the function in-line. EXP is the expression that is a call to the builtin
4959 function; if convenient, the result should be placed in TARGET. */
4961 static rtx
4962 expand_builtin_signbit (tree exp, rtx target)
4964 const struct real_format *fmt;
4965 enum machine_mode fmode, imode, rmode;
4966 HOST_WIDE_INT hi, lo;
4967 tree arg, arglist;
4968 int word, bitpos;
4969 rtx temp;
4971 arglist = TREE_OPERAND (exp, 1);
4972 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4973 return 0;
4975 arg = TREE_VALUE (arglist);
4976 fmode = TYPE_MODE (TREE_TYPE (arg));
4977 rmode = TYPE_MODE (TREE_TYPE (exp));
4978 fmt = REAL_MODE_FORMAT (fmode);
4980 /* For floating point formats without a sign bit, implement signbit
4981 as "ARG < 0.0". */
4982 bitpos = fmt->signbit_ro;
4983 if (bitpos < 0)
4985 /* But we can't do this if the format supports signed zero. */
4986 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4987 return 0;
4989 arg = fold (build2 (LT_EXPR, TREE_TYPE (exp), arg,
4990 build_real (TREE_TYPE (arg), dconst0)));
4991 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4994 temp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
4995 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4997 imode = int_mode_for_mode (fmode);
4998 if (imode == BLKmode)
4999 return 0;
5000 temp = gen_lowpart (imode, temp);
5002 else
5004 imode = word_mode;
5005 /* Handle targets with different FP word orders. */
5006 if (FLOAT_WORDS_BIG_ENDIAN)
5007 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5008 else
5009 word = bitpos / BITS_PER_WORD;
5010 temp = operand_subword_force (temp, word, fmode);
5011 bitpos = bitpos % BITS_PER_WORD;
5014 /* Force the intermediate word_mode (or narrower) result into a
5015 register. This avoids attempting to create paradoxical SUBREGs
5016 of floating point modes below. */
5017 temp = force_reg (imode, temp);
5019 /* If the bitpos is within the "result mode" lowpart, the operation
5020 can be implement with a single bitwise AND. Otherwise, we need
5021 a right shift and an AND. */
5023 if (bitpos < GET_MODE_BITSIZE (rmode))
5025 if (bitpos < HOST_BITS_PER_WIDE_INT)
5027 hi = 0;
5028 lo = (HOST_WIDE_INT) 1 << bitpos;
5030 else
5032 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5033 lo = 0;
5036 if (imode != rmode)
5037 temp = gen_lowpart (rmode, temp);
5038 temp = expand_binop (rmode, and_optab, temp,
5039 immed_double_const (lo, hi, rmode),
5040 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5042 else
5044 /* Perform a logical right shift to place the signbit in the least
5045 significant bit, then truncate the result to the desired mode
5046 and mask just this bit. */
5047 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5048 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5049 temp = gen_lowpart (rmode, temp);
5050 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5051 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5054 return temp;
5057 /* Expand fork or exec calls. TARGET is the desired target of the
5058 call. ARGLIST is the list of arguments of the call. FN is the
5059 identificator of the actual function. IGNORE is nonzero if the
5060 value is to be ignored. */
5062 static rtx
5063 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5065 tree id, decl;
5066 tree call;
5068 /* If we are not profiling, just call the function. */
5069 if (!profile_arc_flag)
5070 return NULL_RTX;
5072 /* Otherwise call the wrapper. This should be equivalent for the rest of
5073 compiler, so the code does not diverge, and the wrapper may run the
5074 code necessary for keeping the profiling sane. */
5076 switch (DECL_FUNCTION_CODE (fn))
5078 case BUILT_IN_FORK:
5079 id = get_identifier ("__gcov_fork");
5080 break;
5082 case BUILT_IN_EXECL:
5083 id = get_identifier ("__gcov_execl");
5084 break;
5086 case BUILT_IN_EXECV:
5087 id = get_identifier ("__gcov_execv");
5088 break;
5090 case BUILT_IN_EXECLP:
5091 id = get_identifier ("__gcov_execlp");
5092 break;
5094 case BUILT_IN_EXECLE:
5095 id = get_identifier ("__gcov_execle");
5096 break;
5098 case BUILT_IN_EXECVP:
5099 id = get_identifier ("__gcov_execvp");
5100 break;
5102 case BUILT_IN_EXECVE:
5103 id = get_identifier ("__gcov_execve");
5104 break;
5106 default:
5107 gcc_unreachable ();
5110 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5111 DECL_EXTERNAL (decl) = 1;
5112 TREE_PUBLIC (decl) = 1;
5113 DECL_ARTIFICIAL (decl) = 1;
5114 TREE_NOTHROW (decl) = 1;
5115 call = build_function_call_expr (decl, arglist);
5117 return expand_call (call, target, ignore);
5120 /* Expand an expression EXP that calls a built-in function,
5121 with result going to TARGET if that's convenient
5122 (and in mode MODE if that's convenient).
5123 SUBTARGET may be used as the target for computing one of EXP's operands.
5124 IGNORE is nonzero if the value is to be ignored. */
5127 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5128 int ignore)
5130 tree fndecl = get_callee_fndecl (exp);
5131 tree arglist = TREE_OPERAND (exp, 1);
5132 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5133 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5135 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5136 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5138 /* When not optimizing, generate calls to library functions for a certain
5139 set of builtins. */
5140 if (!optimize
5141 && !CALLED_AS_BUILT_IN (fndecl)
5142 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5143 && fcode != BUILT_IN_ALLOCA)
5144 return expand_call (exp, target, ignore);
5146 /* The built-in function expanders test for target == const0_rtx
5147 to determine whether the function's result will be ignored. */
5148 if (ignore)
5149 target = const0_rtx;
5151 /* If the result of a pure or const built-in function is ignored, and
5152 none of its arguments are volatile, we can avoid expanding the
5153 built-in call and just evaluate the arguments for side-effects. */
5154 if (target == const0_rtx
5155 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5157 bool volatilep = false;
5158 tree arg;
5160 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5161 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5163 volatilep = true;
5164 break;
5167 if (! volatilep)
5169 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5170 expand_expr (TREE_VALUE (arg), const0_rtx,
5171 VOIDmode, EXPAND_NORMAL);
5172 return const0_rtx;
5176 switch (fcode)
5178 case BUILT_IN_FABS:
5179 case BUILT_IN_FABSF:
5180 case BUILT_IN_FABSL:
5181 target = expand_builtin_fabs (arglist, target, subtarget);
5182 if (target)
5183 return target;
5184 break;
5186 case BUILT_IN_COPYSIGN:
5187 case BUILT_IN_COPYSIGNF:
5188 case BUILT_IN_COPYSIGNL:
5189 target = expand_builtin_copysign (arglist, target, subtarget);
5190 if (target)
5191 return target;
5192 break;
5194 /* Just do a normal library call if we were unable to fold
5195 the values. */
5196 case BUILT_IN_CABS:
5197 case BUILT_IN_CABSF:
5198 case BUILT_IN_CABSL:
5199 break;
5201 case BUILT_IN_EXP:
5202 case BUILT_IN_EXPF:
5203 case BUILT_IN_EXPL:
5204 case BUILT_IN_EXP10:
5205 case BUILT_IN_EXP10F:
5206 case BUILT_IN_EXP10L:
5207 case BUILT_IN_POW10:
5208 case BUILT_IN_POW10F:
5209 case BUILT_IN_POW10L:
5210 case BUILT_IN_EXP2:
5211 case BUILT_IN_EXP2F:
5212 case BUILT_IN_EXP2L:
5213 case BUILT_IN_EXPM1:
5214 case BUILT_IN_EXPM1F:
5215 case BUILT_IN_EXPM1L:
5216 case BUILT_IN_LOGB:
5217 case BUILT_IN_LOGBF:
5218 case BUILT_IN_LOGBL:
5219 case BUILT_IN_ILOGB:
5220 case BUILT_IN_ILOGBF:
5221 case BUILT_IN_ILOGBL:
5222 case BUILT_IN_LOG:
5223 case BUILT_IN_LOGF:
5224 case BUILT_IN_LOGL:
5225 case BUILT_IN_LOG10:
5226 case BUILT_IN_LOG10F:
5227 case BUILT_IN_LOG10L:
5228 case BUILT_IN_LOG2:
5229 case BUILT_IN_LOG2F:
5230 case BUILT_IN_LOG2L:
5231 case BUILT_IN_LOG1P:
5232 case BUILT_IN_LOG1PF:
5233 case BUILT_IN_LOG1PL:
5234 case BUILT_IN_TAN:
5235 case BUILT_IN_TANF:
5236 case BUILT_IN_TANL:
5237 case BUILT_IN_ASIN:
5238 case BUILT_IN_ASINF:
5239 case BUILT_IN_ASINL:
5240 case BUILT_IN_ACOS:
5241 case BUILT_IN_ACOSF:
5242 case BUILT_IN_ACOSL:
5243 case BUILT_IN_ATAN:
5244 case BUILT_IN_ATANF:
5245 case BUILT_IN_ATANL:
5246 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5247 because of possible accuracy problems. */
5248 if (! flag_unsafe_math_optimizations)
5249 break;
5250 case BUILT_IN_SQRT:
5251 case BUILT_IN_SQRTF:
5252 case BUILT_IN_SQRTL:
5253 case BUILT_IN_FLOOR:
5254 case BUILT_IN_FLOORF:
5255 case BUILT_IN_FLOORL:
5256 case BUILT_IN_CEIL:
5257 case BUILT_IN_CEILF:
5258 case BUILT_IN_CEILL:
5259 case BUILT_IN_TRUNC:
5260 case BUILT_IN_TRUNCF:
5261 case BUILT_IN_TRUNCL:
5262 case BUILT_IN_ROUND:
5263 case BUILT_IN_ROUNDF:
5264 case BUILT_IN_ROUNDL:
5265 case BUILT_IN_NEARBYINT:
5266 case BUILT_IN_NEARBYINTF:
5267 case BUILT_IN_NEARBYINTL:
5268 case BUILT_IN_RINT:
5269 case BUILT_IN_RINTF:
5270 case BUILT_IN_RINTL:
5271 case BUILT_IN_LRINT:
5272 case BUILT_IN_LRINTF:
5273 case BUILT_IN_LRINTL:
5274 case BUILT_IN_LLRINT:
5275 case BUILT_IN_LLRINTF:
5276 case BUILT_IN_LLRINTL:
5277 target = expand_builtin_mathfn (exp, target, subtarget);
5278 if (target)
5279 return target;
5280 break;
5282 case BUILT_IN_POW:
5283 case BUILT_IN_POWF:
5284 case BUILT_IN_POWL:
5285 target = expand_builtin_pow (exp, target, subtarget);
5286 if (target)
5287 return target;
5288 break;
5290 case BUILT_IN_POWI:
5291 case BUILT_IN_POWIF:
5292 case BUILT_IN_POWIL:
5293 target = expand_builtin_powi (exp, target, subtarget);
5294 if (target)
5295 return target;
5296 break;
5298 case BUILT_IN_ATAN2:
5299 case BUILT_IN_ATAN2F:
5300 case BUILT_IN_ATAN2L:
5301 case BUILT_IN_LDEXP:
5302 case BUILT_IN_LDEXPF:
5303 case BUILT_IN_LDEXPL:
5304 case BUILT_IN_FMOD:
5305 case BUILT_IN_FMODF:
5306 case BUILT_IN_FMODL:
5307 case BUILT_IN_DREM:
5308 case BUILT_IN_DREMF:
5309 case BUILT_IN_DREML:
5310 if (! flag_unsafe_math_optimizations)
5311 break;
5312 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5313 if (target)
5314 return target;
5315 break;
5317 case BUILT_IN_SIN:
5318 case BUILT_IN_SINF:
5319 case BUILT_IN_SINL:
5320 case BUILT_IN_COS:
5321 case BUILT_IN_COSF:
5322 case BUILT_IN_COSL:
5323 if (! flag_unsafe_math_optimizations)
5324 break;
5325 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5326 if (target)
5327 return target;
5328 break;
5330 case BUILT_IN_APPLY_ARGS:
5331 return expand_builtin_apply_args ();
5333 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5334 FUNCTION with a copy of the parameters described by
5335 ARGUMENTS, and ARGSIZE. It returns a block of memory
5336 allocated on the stack into which is stored all the registers
5337 that might possibly be used for returning the result of a
5338 function. ARGUMENTS is the value returned by
5339 __builtin_apply_args. ARGSIZE is the number of bytes of
5340 arguments that must be copied. ??? How should this value be
5341 computed? We'll also need a safe worst case value for varargs
5342 functions. */
5343 case BUILT_IN_APPLY:
5344 if (!validate_arglist (arglist, POINTER_TYPE,
5345 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5346 && !validate_arglist (arglist, REFERENCE_TYPE,
5347 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5348 return const0_rtx;
5349 else
5351 int i;
5352 tree t;
5353 rtx ops[3];
5355 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5356 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
5358 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5361 /* __builtin_return (RESULT) causes the function to return the
5362 value described by RESULT. RESULT is address of the block of
5363 memory returned by __builtin_apply. */
5364 case BUILT_IN_RETURN:
5365 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5366 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
5367 NULL_RTX, VOIDmode, 0));
5368 return const0_rtx;
5370 case BUILT_IN_SAVEREGS:
5371 return expand_builtin_saveregs ();
5373 case BUILT_IN_ARGS_INFO:
5374 return expand_builtin_args_info (arglist);
5376 /* Return the address of the first anonymous stack arg. */
5377 case BUILT_IN_NEXT_ARG:
5378 if (fold_builtin_next_arg (arglist))
5379 return const0_rtx;
5380 return expand_builtin_next_arg ();
5382 case BUILT_IN_CLASSIFY_TYPE:
5383 return expand_builtin_classify_type (arglist);
5385 case BUILT_IN_CONSTANT_P:
5386 return const0_rtx;
5388 case BUILT_IN_FRAME_ADDRESS:
5389 case BUILT_IN_RETURN_ADDRESS:
5390 return expand_builtin_frame_address (fndecl, arglist);
5392 /* Returns the address of the area where the structure is returned.
5393 0 otherwise. */
5394 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5395 if (arglist != 0
5396 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5397 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5398 return const0_rtx;
5399 else
5400 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5402 case BUILT_IN_ALLOCA:
5403 target = expand_builtin_alloca (arglist, target);
5404 if (target)
5405 return target;
5406 break;
5408 case BUILT_IN_STACK_SAVE:
5409 return expand_stack_save ();
5411 case BUILT_IN_STACK_RESTORE:
5412 expand_stack_restore (TREE_VALUE (arglist));
5413 return const0_rtx;
5415 case BUILT_IN_FFS:
5416 case BUILT_IN_FFSL:
5417 case BUILT_IN_FFSLL:
5418 case BUILT_IN_FFSIMAX:
5419 target = expand_builtin_unop (target_mode, arglist, target,
5420 subtarget, ffs_optab);
5421 if (target)
5422 return target;
5423 break;
5425 case BUILT_IN_CLZ:
5426 case BUILT_IN_CLZL:
5427 case BUILT_IN_CLZLL:
5428 case BUILT_IN_CLZIMAX:
5429 target = expand_builtin_unop (target_mode, arglist, target,
5430 subtarget, clz_optab);
5431 if (target)
5432 return target;
5433 break;
5435 case BUILT_IN_CTZ:
5436 case BUILT_IN_CTZL:
5437 case BUILT_IN_CTZLL:
5438 case BUILT_IN_CTZIMAX:
5439 target = expand_builtin_unop (target_mode, arglist, target,
5440 subtarget, ctz_optab);
5441 if (target)
5442 return target;
5443 break;
5445 case BUILT_IN_POPCOUNT:
5446 case BUILT_IN_POPCOUNTL:
5447 case BUILT_IN_POPCOUNTLL:
5448 case BUILT_IN_POPCOUNTIMAX:
5449 target = expand_builtin_unop (target_mode, arglist, target,
5450 subtarget, popcount_optab);
5451 if (target)
5452 return target;
5453 break;
5455 case BUILT_IN_PARITY:
5456 case BUILT_IN_PARITYL:
5457 case BUILT_IN_PARITYLL:
5458 case BUILT_IN_PARITYIMAX:
5459 target = expand_builtin_unop (target_mode, arglist, target,
5460 subtarget, parity_optab);
5461 if (target)
5462 return target;
5463 break;
5465 case BUILT_IN_STRLEN:
5466 target = expand_builtin_strlen (arglist, target, target_mode);
5467 if (target)
5468 return target;
5469 break;
5471 case BUILT_IN_STRCPY:
5472 target = expand_builtin_strcpy (exp, target, mode);
5473 if (target)
5474 return target;
5475 break;
5477 case BUILT_IN_STRNCPY:
5478 target = expand_builtin_strncpy (exp, target, mode);
5479 if (target)
5480 return target;
5481 break;
5483 case BUILT_IN_STPCPY:
5484 target = expand_builtin_stpcpy (exp, target, mode);
5485 if (target)
5486 return target;
5487 break;
5489 case BUILT_IN_STRCAT:
5490 target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
5491 if (target)
5492 return target;
5493 break;
5495 case BUILT_IN_STRNCAT:
5496 target = expand_builtin_strncat (arglist, target, mode);
5497 if (target)
5498 return target;
5499 break;
5501 case BUILT_IN_STRSPN:
5502 target = expand_builtin_strspn (arglist, target, mode);
5503 if (target)
5504 return target;
5505 break;
5507 case BUILT_IN_STRCSPN:
5508 target = expand_builtin_strcspn (arglist, target, mode);
5509 if (target)
5510 return target;
5511 break;
5513 case BUILT_IN_STRSTR:
5514 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5515 if (target)
5516 return target;
5517 break;
5519 case BUILT_IN_STRPBRK:
5520 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
5521 if (target)
5522 return target;
5523 break;
5525 case BUILT_IN_INDEX:
5526 case BUILT_IN_STRCHR:
5527 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
5528 if (target)
5529 return target;
5530 break;
5532 case BUILT_IN_RINDEX:
5533 case BUILT_IN_STRRCHR:
5534 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
5535 if (target)
5536 return target;
5537 break;
5539 case BUILT_IN_MEMCPY:
5540 target = expand_builtin_memcpy (exp, target, mode);
5541 if (target)
5542 return target;
5543 break;
5545 case BUILT_IN_MEMPCPY:
5546 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
5547 if (target)
5548 return target;
5549 break;
5551 case BUILT_IN_MEMMOVE:
5552 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, mode);
5553 if (target)
5554 return target;
5555 break;
5557 case BUILT_IN_BCOPY:
5558 target = expand_builtin_bcopy (arglist, TREE_TYPE (exp));
5559 if (target)
5560 return target;
5561 break;
5563 case BUILT_IN_MEMSET:
5564 target = expand_builtin_memset (arglist, target, mode);
5565 if (target)
5566 return target;
5567 break;
5569 case BUILT_IN_BZERO:
5570 target = expand_builtin_bzero (arglist);
5571 if (target)
5572 return target;
5573 break;
5575 case BUILT_IN_STRCMP:
5576 target = expand_builtin_strcmp (exp, target, mode);
5577 if (target)
5578 return target;
5579 break;
5581 case BUILT_IN_STRNCMP:
5582 target = expand_builtin_strncmp (exp, target, mode);
5583 if (target)
5584 return target;
5585 break;
5587 case BUILT_IN_BCMP:
5588 case BUILT_IN_MEMCMP:
5589 target = expand_builtin_memcmp (exp, arglist, target, mode);
5590 if (target)
5591 return target;
5592 break;
5594 case BUILT_IN_SETJMP:
5595 target = expand_builtin_setjmp (arglist, target);
5596 if (target)
5597 return target;
5598 break;
5600 /* __builtin_longjmp is passed a pointer to an array of five words.
5601 It's similar to the C library longjmp function but works with
5602 __builtin_setjmp above. */
5603 case BUILT_IN_LONGJMP:
5604 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5605 break;
5606 else
5608 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
5609 VOIDmode, 0);
5610 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
5611 NULL_RTX, VOIDmode, 0);
5613 if (value != const1_rtx)
5615 error ("%<__builtin_longjmp%> second argument must be 1");
5616 return const0_rtx;
5619 expand_builtin_longjmp (buf_addr, value);
5620 return const0_rtx;
5623 case BUILT_IN_NONLOCAL_GOTO:
5624 target = expand_builtin_nonlocal_goto (arglist);
5625 if (target)
5626 return target;
5627 break;
5629 /* This updates the setjmp buffer that is its argument with the value
5630 of the current stack pointer. */
5631 case BUILT_IN_UPDATE_SETJMP_BUF:
5632 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5634 rtx buf_addr
5635 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
5637 expand_builtin_update_setjmp_buf (buf_addr);
5638 return const0_rtx;
5640 break;
5642 case BUILT_IN_TRAP:
5643 expand_builtin_trap ();
5644 return const0_rtx;
5646 case BUILT_IN_PRINTF:
5647 target = expand_builtin_printf (arglist, target, mode, false);
5648 if (target)
5649 return target;
5650 break;
5652 case BUILT_IN_PRINTF_UNLOCKED:
5653 target = expand_builtin_printf (arglist, target, mode, true);
5654 if (target)
5655 return target;
5656 break;
5658 case BUILT_IN_FPUTS:
5659 target = expand_builtin_fputs (arglist, target, false);
5660 if (target)
5661 return target;
5662 break;
5663 case BUILT_IN_FPUTS_UNLOCKED:
5664 target = expand_builtin_fputs (arglist, target, true);
5665 if (target)
5666 return target;
5667 break;
5669 case BUILT_IN_FPRINTF:
5670 target = expand_builtin_fprintf (arglist, target, mode, false);
5671 if (target)
5672 return target;
5673 break;
5675 case BUILT_IN_FPRINTF_UNLOCKED:
5676 target = expand_builtin_fprintf (arglist, target, mode, true);
5677 if (target)
5678 return target;
5679 break;
5681 case BUILT_IN_SPRINTF:
5682 target = expand_builtin_sprintf (arglist, target, mode);
5683 if (target)
5684 return target;
5685 break;
5687 case BUILT_IN_SIGNBIT:
5688 case BUILT_IN_SIGNBITF:
5689 case BUILT_IN_SIGNBITL:
5690 target = expand_builtin_signbit (exp, target);
5691 if (target)
5692 return target;
5693 break;
5695 /* Various hooks for the DWARF 2 __throw routine. */
5696 case BUILT_IN_UNWIND_INIT:
5697 expand_builtin_unwind_init ();
5698 return const0_rtx;
5699 case BUILT_IN_DWARF_CFA:
5700 return virtual_cfa_rtx;
5701 #ifdef DWARF2_UNWIND_INFO
5702 case BUILT_IN_DWARF_SP_COLUMN:
5703 return expand_builtin_dwarf_sp_column ();
5704 case BUILT_IN_INIT_DWARF_REG_SIZES:
5705 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
5706 return const0_rtx;
5707 #endif
5708 case BUILT_IN_FROB_RETURN_ADDR:
5709 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
5710 case BUILT_IN_EXTRACT_RETURN_ADDR:
5711 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
5712 case BUILT_IN_EH_RETURN:
5713 expand_builtin_eh_return (TREE_VALUE (arglist),
5714 TREE_VALUE (TREE_CHAIN (arglist)));
5715 return const0_rtx;
5716 #ifdef EH_RETURN_DATA_REGNO
5717 case BUILT_IN_EH_RETURN_DATA_REGNO:
5718 return expand_builtin_eh_return_data_regno (arglist);
5719 #endif
5720 case BUILT_IN_EXTEND_POINTER:
5721 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
5723 case BUILT_IN_VA_START:
5724 case BUILT_IN_STDARG_START:
5725 return expand_builtin_va_start (arglist);
5726 case BUILT_IN_VA_END:
5727 return expand_builtin_va_end (arglist);
5728 case BUILT_IN_VA_COPY:
5729 return expand_builtin_va_copy (arglist);
5730 case BUILT_IN_EXPECT:
5731 return expand_builtin_expect (arglist, target);
5732 case BUILT_IN_PREFETCH:
5733 expand_builtin_prefetch (arglist);
5734 return const0_rtx;
5736 case BUILT_IN_PROFILE_FUNC_ENTER:
5737 return expand_builtin_profile_func (false);
5738 case BUILT_IN_PROFILE_FUNC_EXIT:
5739 return expand_builtin_profile_func (true);
5741 case BUILT_IN_INIT_TRAMPOLINE:
5742 return expand_builtin_init_trampoline (arglist);
5743 case BUILT_IN_ADJUST_TRAMPOLINE:
5744 return expand_builtin_adjust_trampoline (arglist);
5746 case BUILT_IN_FORK:
5747 case BUILT_IN_EXECL:
5748 case BUILT_IN_EXECV:
5749 case BUILT_IN_EXECLP:
5750 case BUILT_IN_EXECLE:
5751 case BUILT_IN_EXECVP:
5752 case BUILT_IN_EXECVE:
5753 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
5754 if (target)
5755 return target;
5756 break;
5758 default: /* just do library call, if unknown builtin */
5759 break;
5762 /* The switch statement above can drop through to cause the function
5763 to be called normally. */
5764 return expand_call (exp, target, ignore);
5767 /* Determine whether a tree node represents a call to a built-in
5768 function. If the tree T is a call to a built-in function with
5769 the right number of arguments of the appropriate types, return
5770 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
5771 Otherwise the return value is END_BUILTINS. */
5773 enum built_in_function
5774 builtin_mathfn_code (tree t)
5776 tree fndecl, arglist, parmlist;
5777 tree argtype, parmtype;
5779 if (TREE_CODE (t) != CALL_EXPR
5780 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
5781 return END_BUILTINS;
5783 fndecl = get_callee_fndecl (t);
5784 if (fndecl == NULL_TREE
5785 || TREE_CODE (fndecl) != FUNCTION_DECL
5786 || ! DECL_BUILT_IN (fndecl)
5787 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5788 return END_BUILTINS;
5790 arglist = TREE_OPERAND (t, 1);
5791 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5792 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
5794 /* If a function doesn't take a variable number of arguments,
5795 the last element in the list will have type `void'. */
5796 parmtype = TREE_VALUE (parmlist);
5797 if (VOID_TYPE_P (parmtype))
5799 if (arglist)
5800 return END_BUILTINS;
5801 return DECL_FUNCTION_CODE (fndecl);
5804 if (! arglist)
5805 return END_BUILTINS;
5807 argtype = TREE_TYPE (TREE_VALUE (arglist));
5809 if (SCALAR_FLOAT_TYPE_P (parmtype))
5811 if (! SCALAR_FLOAT_TYPE_P (argtype))
5812 return END_BUILTINS;
5814 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
5816 if (! COMPLEX_FLOAT_TYPE_P (argtype))
5817 return END_BUILTINS;
5819 else if (POINTER_TYPE_P (parmtype))
5821 if (! POINTER_TYPE_P (argtype))
5822 return END_BUILTINS;
5824 else if (INTEGRAL_TYPE_P (parmtype))
5826 if (! INTEGRAL_TYPE_P (argtype))
5827 return END_BUILTINS;
5829 else
5830 return END_BUILTINS;
5832 arglist = TREE_CHAIN (arglist);
5835 /* Variable-length argument list. */
5836 return DECL_FUNCTION_CODE (fndecl);
5839 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
5840 constant. ARGLIST is the argument list of the call. */
5842 static tree
5843 fold_builtin_constant_p (tree arglist)
5845 if (arglist == 0)
5846 return 0;
5848 arglist = TREE_VALUE (arglist);
5850 /* We return 1 for a numeric type that's known to be a constant
5851 value at compile-time or for an aggregate type that's a
5852 literal constant. */
5853 STRIP_NOPS (arglist);
5855 /* If we know this is a constant, emit the constant of one. */
5856 if (CONSTANT_CLASS_P (arglist)
5857 || (TREE_CODE (arglist) == CONSTRUCTOR
5858 && TREE_CONSTANT (arglist))
5859 || (TREE_CODE (arglist) == ADDR_EXPR
5860 && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST))
5861 return integer_one_node;
5863 /* If this expression has side effects, show we don't know it to be a
5864 constant. Likewise if it's a pointer or aggregate type since in
5865 those case we only want literals, since those are only optimized
5866 when generating RTL, not later.
5867 And finally, if we are compiling an initializer, not code, we
5868 need to return a definite result now; there's not going to be any
5869 more optimization done. */
5870 if (TREE_SIDE_EFFECTS (arglist)
5871 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
5872 || POINTER_TYPE_P (TREE_TYPE (arglist))
5873 || cfun == 0)
5874 return integer_zero_node;
5876 return 0;
5879 /* Fold a call to __builtin_expect, if we expect that a comparison against
5880 the argument will fold to a constant. In practice, this means a true
5881 constant or the address of a non-weak symbol. ARGLIST is the argument
5882 list of the call. */
5884 static tree
5885 fold_builtin_expect (tree arglist)
5887 tree arg, inner;
5889 if (arglist == 0)
5890 return 0;
5892 arg = TREE_VALUE (arglist);
5894 /* If the argument isn't invariant, then there's nothing we can do. */
5895 if (!TREE_INVARIANT (arg))
5896 return 0;
5898 /* If we're looking at an address of a weak decl, then do not fold. */
5899 inner = arg;
5900 STRIP_NOPS (inner);
5901 if (TREE_CODE (inner) == ADDR_EXPR)
5905 inner = TREE_OPERAND (inner, 0);
5907 while (TREE_CODE (inner) == COMPONENT_REF
5908 || TREE_CODE (inner) == ARRAY_REF);
5909 if (DECL_P (inner) && DECL_WEAK (inner))
5910 return 0;
5913 /* Otherwise, ARG already has the proper type for the return value. */
5914 return arg;
5917 /* Fold a call to __builtin_classify_type. */
5919 static tree
5920 fold_builtin_classify_type (tree arglist)
5922 if (arglist == 0)
5923 return build_int_cst (NULL_TREE, no_type_class);
5925 return build_int_cst (NULL_TREE,
5926 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
5929 /* Fold a call to __builtin_strlen. */
5931 static tree
5932 fold_builtin_strlen (tree arglist)
5934 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5935 return NULL_TREE;
5936 else
5938 tree len = c_strlen (TREE_VALUE (arglist), 0);
5940 if (len)
5942 /* Convert from the internal "sizetype" type to "size_t". */
5943 if (size_type_node)
5944 len = fold_convert (size_type_node, len);
5945 return len;
5948 return NULL_TREE;
5952 /* Fold a call to __builtin_inf or __builtin_huge_val. */
5954 static tree
5955 fold_builtin_inf (tree type, int warn)
5957 REAL_VALUE_TYPE real;
5959 /* __builtin_inff is intended to be usable to define INFINITY on all
5960 targets. If an infinity is not available, INFINITY expands "to a
5961 positive constant of type float that overflows at translation
5962 time", footnote "In this case, using INFINITY will violate the
5963 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
5964 Thus we pedwarn to ensure this constraint violation is
5965 diagnosed. */
5966 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
5967 pedwarn ("target format does not support infinity");
5969 real_inf (&real);
5970 return build_real (type, real);
5973 /* Fold a call to __builtin_nan or __builtin_nans. */
5975 static tree
5976 fold_builtin_nan (tree arglist, tree type, int quiet)
5978 REAL_VALUE_TYPE real;
5979 const char *str;
5981 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5982 return 0;
5983 str = c_getstr (TREE_VALUE (arglist));
5984 if (!str)
5985 return 0;
5987 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
5988 return 0;
5990 return build_real (type, real);
5993 /* Return true if the floating point expression T has an integer value.
5994 We also allow +Inf, -Inf and NaN to be considered integer values. */
5996 static bool
5997 integer_valued_real_p (tree t)
5999 switch (TREE_CODE (t))
6001 case FLOAT_EXPR:
6002 return true;
6004 case ABS_EXPR:
6005 case SAVE_EXPR:
6006 case NON_LVALUE_EXPR:
6007 return integer_valued_real_p (TREE_OPERAND (t, 0));
6009 case COMPOUND_EXPR:
6010 case MODIFY_EXPR:
6011 case BIND_EXPR:
6012 return integer_valued_real_p (TREE_OPERAND (t, 1));
6014 case PLUS_EXPR:
6015 case MINUS_EXPR:
6016 case MULT_EXPR:
6017 case MIN_EXPR:
6018 case MAX_EXPR:
6019 return integer_valued_real_p (TREE_OPERAND (t, 0))
6020 && integer_valued_real_p (TREE_OPERAND (t, 1));
6022 case COND_EXPR:
6023 return integer_valued_real_p (TREE_OPERAND (t, 1))
6024 && integer_valued_real_p (TREE_OPERAND (t, 2));
6026 case REAL_CST:
6027 if (! TREE_CONSTANT_OVERFLOW (t))
6029 REAL_VALUE_TYPE c, cint;
6031 c = TREE_REAL_CST (t);
6032 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6033 return real_identical (&c, &cint);
6036 case NOP_EXPR:
6038 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6039 if (TREE_CODE (type) == INTEGER_TYPE)
6040 return true;
6041 if (TREE_CODE (type) == REAL_TYPE)
6042 return integer_valued_real_p (TREE_OPERAND (t, 0));
6043 break;
6046 case CALL_EXPR:
6047 switch (builtin_mathfn_code (t))
6049 case BUILT_IN_CEIL:
6050 case BUILT_IN_CEILF:
6051 case BUILT_IN_CEILL:
6052 case BUILT_IN_FLOOR:
6053 case BUILT_IN_FLOORF:
6054 case BUILT_IN_FLOORL:
6055 case BUILT_IN_NEARBYINT:
6056 case BUILT_IN_NEARBYINTF:
6057 case BUILT_IN_NEARBYINTL:
6058 case BUILT_IN_RINT:
6059 case BUILT_IN_RINTF:
6060 case BUILT_IN_RINTL:
6061 case BUILT_IN_ROUND:
6062 case BUILT_IN_ROUNDF:
6063 case BUILT_IN_ROUNDL:
6064 case BUILT_IN_TRUNC:
6065 case BUILT_IN_TRUNCF:
6066 case BUILT_IN_TRUNCL:
6067 return true;
6069 default:
6070 break;
6072 break;
6074 default:
6075 break;
6077 return false;
6080 /* EXP is assumed to be builtin call where truncation can be propagated
6081 across (for instance floor((double)f) == (double)floorf (f).
6082 Do the transformation. */
6084 static tree
6085 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6087 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6088 tree arg;
6090 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6091 return 0;
6093 arg = TREE_VALUE (arglist);
6094 /* Integer rounding functions are idempotent. */
6095 if (fcode == builtin_mathfn_code (arg))
6096 return arg;
6098 /* If argument is already integer valued, and we don't need to worry
6099 about setting errno, there's no need to perform rounding. */
6100 if (! flag_errno_math && integer_valued_real_p (arg))
6101 return arg;
6103 if (optimize)
6105 tree arg0 = strip_float_extensions (arg);
6106 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6107 tree newtype = TREE_TYPE (arg0);
6108 tree decl;
6110 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6111 && (decl = mathfn_built_in (newtype, fcode)))
6113 arglist =
6114 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6115 return fold_convert (ftype,
6116 build_function_call_expr (decl, arglist));
6119 return 0;
6122 /* EXP is assumed to be builtin call which can narrow the FP type of
6123 the argument, for instance lround((double)f) -> lroundf (f). */
6125 static tree
6126 fold_fixed_mathfn (tree fndecl, tree arglist)
6128 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6129 tree arg;
6131 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6132 return 0;
6134 arg = TREE_VALUE (arglist);
6136 /* If argument is already integer valued, and we don't need to worry
6137 about setting errno, there's no need to perform rounding. */
6138 if (! flag_errno_math && integer_valued_real_p (arg))
6139 return fold (build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg));
6141 if (optimize)
6143 tree ftype = TREE_TYPE (arg);
6144 tree arg0 = strip_float_extensions (arg);
6145 tree newtype = TREE_TYPE (arg0);
6146 tree decl;
6148 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6149 && (decl = mathfn_built_in (newtype, fcode)))
6151 arglist =
6152 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6153 return build_function_call_expr (decl, arglist);
6156 return 0;
6159 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6160 is the argument list and TYPE is the return type. Return
6161 NULL_TREE if no if no simplification can be made. */
6163 static tree
6164 fold_builtin_cabs (tree arglist, tree type)
6166 tree arg;
6168 if (!arglist || TREE_CHAIN (arglist))
6169 return NULL_TREE;
6171 arg = TREE_VALUE (arglist);
6172 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6173 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6174 return NULL_TREE;
6176 /* Evaluate cabs of a constant at compile-time. */
6177 if (flag_unsafe_math_optimizations
6178 && TREE_CODE (arg) == COMPLEX_CST
6179 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6180 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6181 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6182 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6184 REAL_VALUE_TYPE r, i;
6186 r = TREE_REAL_CST (TREE_REALPART (arg));
6187 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6189 real_arithmetic (&r, MULT_EXPR, &r, &r);
6190 real_arithmetic (&i, MULT_EXPR, &i, &i);
6191 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6192 if (real_sqrt (&r, TYPE_MODE (type), &r)
6193 || ! flag_trapping_math)
6194 return build_real (type, r);
6197 /* If either part is zero, cabs is fabs of the other. */
6198 if (TREE_CODE (arg) == COMPLEX_EXPR
6199 && real_zerop (TREE_OPERAND (arg, 0)))
6200 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)));
6201 if (TREE_CODE (arg) == COMPLEX_EXPR
6202 && real_zerop (TREE_OPERAND (arg, 1)))
6203 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)));
6205 /* Don't do this when optimizing for size. */
6206 if (flag_unsafe_math_optimizations
6207 && optimize && !optimize_size)
6209 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6211 if (sqrtfn != NULL_TREE)
6213 tree rpart, ipart, result, arglist;
6215 arg = builtin_save_expr (arg);
6217 rpart = fold (build1 (REALPART_EXPR, type, arg));
6218 ipart = fold (build1 (IMAGPART_EXPR, type, arg));
6220 rpart = builtin_save_expr (rpart);
6221 ipart = builtin_save_expr (ipart);
6223 result = fold (build2 (PLUS_EXPR, type,
6224 fold (build2 (MULT_EXPR, type,
6225 rpart, rpart)),
6226 fold (build2 (MULT_EXPR, type,
6227 ipart, ipart))));
6229 arglist = build_tree_list (NULL_TREE, result);
6230 return build_function_call_expr (sqrtfn, arglist);
6234 return NULL_TREE;
6237 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6238 NULL_TREE if no simplification can be made. */
6240 static tree
6241 fold_builtin_sqrt (tree arglist, tree type)
6244 enum built_in_function fcode;
6245 tree arg = TREE_VALUE (arglist);
6247 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6248 return NULL_TREE;
6250 /* Optimize sqrt of constant value. */
6251 if (TREE_CODE (arg) == REAL_CST
6252 && ! TREE_CONSTANT_OVERFLOW (arg))
6254 REAL_VALUE_TYPE r, x;
6256 x = TREE_REAL_CST (arg);
6257 if (real_sqrt (&r, TYPE_MODE (type), &x)
6258 || (!flag_trapping_math && !flag_errno_math))
6259 return build_real (type, r);
6262 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6263 fcode = builtin_mathfn_code (arg);
6264 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6266 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6267 arg = fold (build2 (MULT_EXPR, type,
6268 TREE_VALUE (TREE_OPERAND (arg, 1)),
6269 build_real (type, dconsthalf)));
6270 arglist = build_tree_list (NULL_TREE, arg);
6271 return build_function_call_expr (expfn, arglist);
6274 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6275 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6277 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6279 if (powfn)
6281 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6282 tree tree_root;
6283 /* The inner root was either sqrt or cbrt. */
6284 REAL_VALUE_TYPE dconstroot =
6285 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
6287 /* Adjust for the outer root. */
6288 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6289 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6290 tree_root = build_real (type, dconstroot);
6291 arglist = tree_cons (NULL_TREE, arg0,
6292 build_tree_list (NULL_TREE, tree_root));
6293 return build_function_call_expr (powfn, arglist);
6297 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6298 if (flag_unsafe_math_optimizations
6299 && (fcode == BUILT_IN_POW
6300 || fcode == BUILT_IN_POWF
6301 || fcode == BUILT_IN_POWL))
6303 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6304 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6305 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6306 tree narg1;
6307 if (!tree_expr_nonnegative_p (arg0))
6308 arg0 = build1 (ABS_EXPR, type, arg0);
6309 narg1 = fold (build2 (MULT_EXPR, type, arg1,
6310 build_real (type, dconsthalf)));
6311 arglist = tree_cons (NULL_TREE, arg0,
6312 build_tree_list (NULL_TREE, narg1));
6313 return build_function_call_expr (powfn, arglist);
6316 return NULL_TREE;
6319 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
6320 NULL_TREE if no simplification can be made. */
6321 static tree
6322 fold_builtin_cbrt (tree arglist, tree type)
6324 tree arg = TREE_VALUE (arglist);
6325 const enum built_in_function fcode = builtin_mathfn_code (arg);
6327 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6328 return NULL_TREE;
6330 /* Optimize cbrt of constant value. */
6331 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
6332 return arg;
6334 if (flag_unsafe_math_optimizations)
6336 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6337 if (BUILTIN_EXPONENT_P (fcode))
6339 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6340 const REAL_VALUE_TYPE third_trunc =
6341 real_value_truncate (TYPE_MODE (type), dconstthird);
6342 arg = fold (build2 (MULT_EXPR, type,
6343 TREE_VALUE (TREE_OPERAND (arg, 1)),
6344 build_real (type, third_trunc)));
6345 arglist = build_tree_list (NULL_TREE, arg);
6346 return build_function_call_expr (expfn, arglist);
6349 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6350 if (BUILTIN_SQRT_P (fcode))
6352 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6354 if (powfn)
6356 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6357 tree tree_root;
6358 REAL_VALUE_TYPE dconstroot = dconstthird;
6360 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6361 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6362 tree_root = build_real (type, dconstroot);
6363 arglist = tree_cons (NULL_TREE, arg0,
6364 build_tree_list (NULL_TREE, tree_root));
6365 return build_function_call_expr (powfn, arglist);
6369 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
6370 if (BUILTIN_CBRT_P (fcode))
6372 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
6373 if (tree_expr_nonnegative_p (arg0))
6375 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6377 if (powfn)
6379 tree tree_root;
6380 REAL_VALUE_TYPE dconstroot;
6382 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
6383 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6384 tree_root = build_real (type, dconstroot);
6385 arglist = tree_cons (NULL_TREE, arg0,
6386 build_tree_list (NULL_TREE, tree_root));
6387 return build_function_call_expr (powfn, arglist);
6392 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
6393 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
6394 || fcode == BUILT_IN_POWL)
6396 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
6397 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6398 if (tree_expr_nonnegative_p (arg00))
6400 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
6401 const REAL_VALUE_TYPE dconstroot
6402 = real_value_truncate (TYPE_MODE (type), dconstthird);
6403 tree narg01 = fold (build2 (MULT_EXPR, type, arg01,
6404 build_real (type, dconstroot)));
6405 arglist = tree_cons (NULL_TREE, arg00,
6406 build_tree_list (NULL_TREE, narg01));
6407 return build_function_call_expr (powfn, arglist);
6411 return NULL_TREE;
6414 /* Fold function call to builtin sin, sinf, or sinl. Return
6415 NULL_TREE if no simplification can be made. */
6416 static tree
6417 fold_builtin_sin (tree arglist)
6419 tree arg = TREE_VALUE (arglist);
6421 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6422 return NULL_TREE;
6424 /* Optimize sin (0.0) = 0.0. */
6425 if (real_zerop (arg))
6426 return arg;
6428 return NULL_TREE;
6431 /* Fold function call to builtin cos, cosf, or cosl. Return
6432 NULL_TREE if no simplification can be made. */
6433 static tree
6434 fold_builtin_cos (tree arglist, tree type, tree fndecl)
6436 tree arg = TREE_VALUE (arglist);
6438 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6439 return NULL_TREE;
6441 /* Optimize cos (0.0) = 1.0. */
6442 if (real_zerop (arg))
6443 return build_real (type, dconst1);
6445 /* Optimize cos(-x) into cos (x). */
6446 if (TREE_CODE (arg) == NEGATE_EXPR)
6448 tree args = build_tree_list (NULL_TREE,
6449 TREE_OPERAND (arg, 0));
6450 return build_function_call_expr (fndecl, args);
6453 return NULL_TREE;
6456 /* Fold function call to builtin tan, tanf, or tanl. Return
6457 NULL_TREE if no simplification can be made. */
6458 static tree
6459 fold_builtin_tan (tree arglist)
6461 enum built_in_function fcode;
6462 tree arg = TREE_VALUE (arglist);
6464 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6465 return NULL_TREE;
6467 /* Optimize tan(0.0) = 0.0. */
6468 if (real_zerop (arg))
6469 return arg;
6471 /* Optimize tan(atan(x)) = x. */
6472 fcode = builtin_mathfn_code (arg);
6473 if (flag_unsafe_math_optimizations
6474 && (fcode == BUILT_IN_ATAN
6475 || fcode == BUILT_IN_ATANF
6476 || fcode == BUILT_IN_ATANL))
6477 return TREE_VALUE (TREE_OPERAND (arg, 1));
6479 return NULL_TREE;
6482 /* Fold function call to builtin atan, atanf, or atanl. Return
6483 NULL_TREE if no simplification can be made. */
6485 static tree
6486 fold_builtin_atan (tree arglist, tree type)
6489 tree arg = TREE_VALUE (arglist);
6491 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6492 return NULL_TREE;
6494 /* Optimize atan(0.0) = 0.0. */
6495 if (real_zerop (arg))
6496 return arg;
6498 /* Optimize atan(1.0) = pi/4. */
6499 if (real_onep (arg))
6501 REAL_VALUE_TYPE cst;
6503 real_convert (&cst, TYPE_MODE (type), &dconstpi);
6504 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
6505 return build_real (type, cst);
6508 return NULL_TREE;
6511 /* Fold function call to builtin trunc, truncf or truncl. Return
6512 NULL_TREE if no simplification can be made. */
6514 static tree
6515 fold_builtin_trunc (tree fndecl, tree arglist)
6517 tree arg;
6519 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6520 return 0;
6522 /* Optimize trunc of constant value. */
6523 arg = TREE_VALUE (arglist);
6524 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6526 REAL_VALUE_TYPE r, x;
6527 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6529 x = TREE_REAL_CST (arg);
6530 real_trunc (&r, TYPE_MODE (type), &x);
6531 return build_real (type, r);
6534 return fold_trunc_transparent_mathfn (fndecl, arglist);
6537 /* Fold function call to builtin floor, floorf or floorl. Return
6538 NULL_TREE if no simplification can be made. */
6540 static tree
6541 fold_builtin_floor (tree fndecl, tree arglist)
6543 tree arg;
6545 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6546 return 0;
6548 /* Optimize floor of constant value. */
6549 arg = TREE_VALUE (arglist);
6550 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6552 REAL_VALUE_TYPE x;
6554 x = TREE_REAL_CST (arg);
6555 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6557 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6558 REAL_VALUE_TYPE r;
6560 real_floor (&r, TYPE_MODE (type), &x);
6561 return build_real (type, r);
6565 return fold_trunc_transparent_mathfn (fndecl, arglist);
6568 /* Fold function call to builtin ceil, ceilf or ceill. Return
6569 NULL_TREE if no simplification can be made. */
6571 static tree
6572 fold_builtin_ceil (tree fndecl, tree arglist)
6574 tree arg;
6576 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6577 return 0;
6579 /* Optimize ceil of constant value. */
6580 arg = TREE_VALUE (arglist);
6581 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6583 REAL_VALUE_TYPE x;
6585 x = TREE_REAL_CST (arg);
6586 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6588 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6589 REAL_VALUE_TYPE r;
6591 real_ceil (&r, TYPE_MODE (type), &x);
6592 return build_real (type, r);
6596 return fold_trunc_transparent_mathfn (fndecl, arglist);
6599 /* Fold function call to builtin round, roundf or roundl. Return
6600 NULL_TREE if no simplification can be made. */
6602 static tree
6603 fold_builtin_round (tree fndecl, tree arglist)
6605 tree arg;
6607 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6608 return 0;
6610 /* Optimize round of constant value. */
6611 arg = TREE_VALUE (arglist);
6612 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6614 REAL_VALUE_TYPE x;
6616 x = TREE_REAL_CST (arg);
6617 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
6619 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6620 REAL_VALUE_TYPE r;
6622 real_round (&r, TYPE_MODE (type), &x);
6623 return build_real (type, r);
6627 return fold_trunc_transparent_mathfn (fndecl, arglist);
6630 /* Fold function call to builtin lround, lroundf or lroundl (or the
6631 corresponding long long versions). Return NULL_TREE if no
6632 simplification can be made. */
6634 static tree
6635 fold_builtin_lround (tree fndecl, tree arglist)
6637 tree arg;
6639 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6640 return 0;
6642 /* Optimize lround of constant value. */
6643 arg = TREE_VALUE (arglist);
6644 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6646 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
6648 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
6650 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
6651 tree ftype = TREE_TYPE (arg), result;
6652 HOST_WIDE_INT hi, lo;
6653 REAL_VALUE_TYPE r;
6655 real_round (&r, TYPE_MODE (ftype), &x);
6656 REAL_VALUE_TO_INT (&lo, &hi, r);
6657 result = build_int_cst_wide (NULL_TREE, lo, hi);
6658 if (int_fits_type_p (result, itype))
6659 return fold_convert (itype, result);
6663 return fold_fixed_mathfn (fndecl, arglist);
6666 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
6667 and their long and long long variants (i.e. ffsl and ffsll).
6668 Return NULL_TREE if no simplification can be made. */
6670 static tree
6671 fold_builtin_bitop (tree fndecl, tree arglist)
6673 tree arg;
6675 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
6676 return NULL_TREE;
6678 /* Optimize for constant argument. */
6679 arg = TREE_VALUE (arglist);
6680 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
6682 HOST_WIDE_INT hi, width, result;
6683 unsigned HOST_WIDE_INT lo;
6684 tree type;
6686 type = TREE_TYPE (arg);
6687 width = TYPE_PRECISION (type);
6688 lo = TREE_INT_CST_LOW (arg);
6690 /* Clear all the bits that are beyond the type's precision. */
6691 if (width > HOST_BITS_PER_WIDE_INT)
6693 hi = TREE_INT_CST_HIGH (arg);
6694 if (width < 2 * HOST_BITS_PER_WIDE_INT)
6695 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
6697 else
6699 hi = 0;
6700 if (width < HOST_BITS_PER_WIDE_INT)
6701 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
6704 switch (DECL_FUNCTION_CODE (fndecl))
6706 case BUILT_IN_FFS:
6707 case BUILT_IN_FFSL:
6708 case BUILT_IN_FFSLL:
6709 if (lo != 0)
6710 result = exact_log2 (lo & -lo) + 1;
6711 else if (hi != 0)
6712 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
6713 else
6714 result = 0;
6715 break;
6717 case BUILT_IN_CLZ:
6718 case BUILT_IN_CLZL:
6719 case BUILT_IN_CLZLL:
6720 if (hi != 0)
6721 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
6722 else if (lo != 0)
6723 result = width - floor_log2 (lo) - 1;
6724 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6725 result = width;
6726 break;
6728 case BUILT_IN_CTZ:
6729 case BUILT_IN_CTZL:
6730 case BUILT_IN_CTZLL:
6731 if (lo != 0)
6732 result = exact_log2 (lo & -lo);
6733 else if (hi != 0)
6734 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
6735 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
6736 result = width;
6737 break;
6739 case BUILT_IN_POPCOUNT:
6740 case BUILT_IN_POPCOUNTL:
6741 case BUILT_IN_POPCOUNTLL:
6742 result = 0;
6743 while (lo)
6744 result++, lo &= lo - 1;
6745 while (hi)
6746 result++, hi &= hi - 1;
6747 break;
6749 case BUILT_IN_PARITY:
6750 case BUILT_IN_PARITYL:
6751 case BUILT_IN_PARITYLL:
6752 result = 0;
6753 while (lo)
6754 result++, lo &= lo - 1;
6755 while (hi)
6756 result++, hi &= hi - 1;
6757 result &= 1;
6758 break;
6760 default:
6761 gcc_unreachable ();
6764 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
6767 return NULL_TREE;
6770 /* Return true if EXPR is the real constant contained in VALUE. */
6772 static bool
6773 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
6775 STRIP_NOPS (expr);
6777 return ((TREE_CODE (expr) == REAL_CST
6778 && ! TREE_CONSTANT_OVERFLOW (expr)
6779 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
6780 || (TREE_CODE (expr) == COMPLEX_CST
6781 && real_dconstp (TREE_REALPART (expr), value)
6782 && real_zerop (TREE_IMAGPART (expr))));
6785 /* A subroutine of fold_builtin to fold the various logarithmic
6786 functions. EXP is the CALL_EXPR of a call to a builtin logN
6787 function. VALUE is the base of the logN function. */
6789 static tree
6790 fold_builtin_logarithm (tree fndecl, tree arglist,
6791 const REAL_VALUE_TYPE *value)
6793 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6795 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6796 tree arg = TREE_VALUE (arglist);
6797 const enum built_in_function fcode = builtin_mathfn_code (arg);
6799 /* Optimize logN(1.0) = 0.0. */
6800 if (real_onep (arg))
6801 return build_real (type, dconst0);
6803 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
6804 exactly, then only do this if flag_unsafe_math_optimizations. */
6805 if (exact_real_truncate (TYPE_MODE (type), value)
6806 || flag_unsafe_math_optimizations)
6808 const REAL_VALUE_TYPE value_truncate =
6809 real_value_truncate (TYPE_MODE (type), *value);
6810 if (real_dconstp (arg, &value_truncate))
6811 return build_real (type, dconst1);
6814 /* Special case, optimize logN(expN(x)) = x. */
6815 if (flag_unsafe_math_optimizations
6816 && ((value == &dconste
6817 && (fcode == BUILT_IN_EXP
6818 || fcode == BUILT_IN_EXPF
6819 || fcode == BUILT_IN_EXPL))
6820 || (value == &dconst2
6821 && (fcode == BUILT_IN_EXP2
6822 || fcode == BUILT_IN_EXP2F
6823 || fcode == BUILT_IN_EXP2L))
6824 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
6825 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
6827 /* Optimize logN(func()) for various exponential functions. We
6828 want to determine the value "x" and the power "exponent" in
6829 order to transform logN(x**exponent) into exponent*logN(x). */
6830 if (flag_unsafe_math_optimizations)
6832 tree exponent = 0, x = 0;
6834 switch (fcode)
6836 case BUILT_IN_EXP:
6837 case BUILT_IN_EXPF:
6838 case BUILT_IN_EXPL:
6839 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
6840 x = build_real (type,
6841 real_value_truncate (TYPE_MODE (type), dconste));
6842 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6843 break;
6844 case BUILT_IN_EXP2:
6845 case BUILT_IN_EXP2F:
6846 case BUILT_IN_EXP2L:
6847 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
6848 x = build_real (type, dconst2);
6849 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6850 break;
6851 case BUILT_IN_EXP10:
6852 case BUILT_IN_EXP10F:
6853 case BUILT_IN_EXP10L:
6854 case BUILT_IN_POW10:
6855 case BUILT_IN_POW10F:
6856 case BUILT_IN_POW10L:
6857 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
6858 x = build_real (type, dconst10);
6859 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
6860 break;
6861 case BUILT_IN_SQRT:
6862 case BUILT_IN_SQRTF:
6863 case BUILT_IN_SQRTL:
6864 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
6865 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6866 exponent = build_real (type, dconsthalf);
6867 break;
6868 case BUILT_IN_CBRT:
6869 case BUILT_IN_CBRTF:
6870 case BUILT_IN_CBRTL:
6871 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
6872 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6873 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
6874 dconstthird));
6875 break;
6876 case BUILT_IN_POW:
6877 case BUILT_IN_POWF:
6878 case BUILT_IN_POWL:
6879 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
6880 x = TREE_VALUE (TREE_OPERAND (arg, 1));
6881 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
6882 break;
6883 default:
6884 break;
6887 /* Now perform the optimization. */
6888 if (x && exponent)
6890 tree logfn;
6891 arglist = build_tree_list (NULL_TREE, x);
6892 logfn = build_function_call_expr (fndecl, arglist);
6893 return fold (build2 (MULT_EXPR, type, exponent, logfn));
6898 return 0;
6901 /* Fold a builtin function call to pow, powf, or powl. Return
6902 NULL_TREE if no simplification can be made. */
6903 static tree
6904 fold_builtin_pow (tree fndecl, tree arglist, tree type)
6906 tree arg0 = TREE_VALUE (arglist);
6907 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6909 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6910 return NULL_TREE;
6912 /* Optimize pow(1.0,y) = 1.0. */
6913 if (real_onep (arg0))
6914 return omit_one_operand (type, build_real (type, dconst1), arg1);
6916 if (TREE_CODE (arg1) == REAL_CST
6917 && ! TREE_CONSTANT_OVERFLOW (arg1))
6919 REAL_VALUE_TYPE cint;
6920 REAL_VALUE_TYPE c;
6921 HOST_WIDE_INT n;
6923 c = TREE_REAL_CST (arg1);
6925 /* Optimize pow(x,0.0) = 1.0. */
6926 if (REAL_VALUES_EQUAL (c, dconst0))
6927 return omit_one_operand (type, build_real (type, dconst1),
6928 arg0);
6930 /* Optimize pow(x,1.0) = x. */
6931 if (REAL_VALUES_EQUAL (c, dconst1))
6932 return arg0;
6934 /* Optimize pow(x,-1.0) = 1.0/x. */
6935 if (REAL_VALUES_EQUAL (c, dconstm1))
6936 return fold (build2 (RDIV_EXPR, type,
6937 build_real (type, dconst1), arg0));
6939 /* Optimize pow(x,0.5) = sqrt(x). */
6940 if (flag_unsafe_math_optimizations
6941 && REAL_VALUES_EQUAL (c, dconsthalf))
6943 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6945 if (sqrtfn != NULL_TREE)
6947 tree arglist = build_tree_list (NULL_TREE, arg0);
6948 return build_function_call_expr (sqrtfn, arglist);
6952 /* Check for an integer exponent. */
6953 n = real_to_integer (&c);
6954 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
6955 if (real_identical (&c, &cint))
6957 /* Attempt to evaluate pow at compile-time. */
6958 if (TREE_CODE (arg0) == REAL_CST
6959 && ! TREE_CONSTANT_OVERFLOW (arg0))
6961 REAL_VALUE_TYPE x;
6962 bool inexact;
6964 x = TREE_REAL_CST (arg0);
6965 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
6966 if (flag_unsafe_math_optimizations || !inexact)
6967 return build_real (type, x);
6970 /* Strip sign ops from even integer powers. */
6971 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
6973 tree narg0 = fold_strip_sign_ops (arg0);
6974 if (narg0)
6976 arglist = build_tree_list (NULL_TREE, arg1);
6977 arglist = tree_cons (NULL_TREE, narg0, arglist);
6978 return build_function_call_expr (fndecl, arglist);
6984 if (flag_unsafe_math_optimizations)
6986 const enum built_in_function fcode = builtin_mathfn_code (arg0);
6988 /* Optimize pow(expN(x),y) = expN(x*y). */
6989 if (BUILTIN_EXPONENT_P (fcode))
6991 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6992 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
6993 arg = fold (build2 (MULT_EXPR, type, arg, arg1));
6994 arglist = build_tree_list (NULL_TREE, arg);
6995 return build_function_call_expr (expfn, arglist);
6998 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
6999 if (BUILTIN_SQRT_P (fcode))
7001 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7002 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7003 build_real (type, dconsthalf)));
7005 arglist = tree_cons (NULL_TREE, narg0,
7006 build_tree_list (NULL_TREE, narg1));
7007 return build_function_call_expr (fndecl, arglist);
7010 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7011 if (BUILTIN_CBRT_P (fcode))
7013 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7014 if (tree_expr_nonnegative_p (arg))
7016 const REAL_VALUE_TYPE dconstroot
7017 = real_value_truncate (TYPE_MODE (type), dconstthird);
7018 tree narg1 = fold (build2 (MULT_EXPR, type, arg1,
7019 build_real (type, dconstroot)));
7020 arglist = tree_cons (NULL_TREE, arg,
7021 build_tree_list (NULL_TREE, narg1));
7022 return build_function_call_expr (fndecl, arglist);
7026 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7027 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7028 || fcode == BUILT_IN_POWL)
7030 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7031 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7032 tree narg1 = fold (build2 (MULT_EXPR, type, arg01, arg1));
7033 arglist = tree_cons (NULL_TREE, arg00,
7034 build_tree_list (NULL_TREE, narg1));
7035 return build_function_call_expr (fndecl, arglist);
7039 return NULL_TREE;
7042 /* Fold a builtin function call to powi, powif, or powil. Return
7043 NULL_TREE if no simplification can be made. */
7044 static tree
7045 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7047 tree arg0 = TREE_VALUE (arglist);
7048 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7050 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7051 return NULL_TREE;
7053 /* Optimize pow(1.0,y) = 1.0. */
7054 if (real_onep (arg0))
7055 return omit_one_operand (type, build_real (type, dconst1), arg1);
7057 if (host_integerp (arg1, 0))
7059 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7061 /* Evaluate powi at compile-time. */
7062 if (TREE_CODE (arg0) == REAL_CST
7063 && ! TREE_CONSTANT_OVERFLOW (arg0))
7065 REAL_VALUE_TYPE x;
7066 x = TREE_REAL_CST (arg0);
7067 real_powi (&x, TYPE_MODE (type), &x, c);
7068 return build_real (type, x);
7071 /* Optimize pow(x,0) = 1.0. */
7072 if (c == 0)
7073 return omit_one_operand (type, build_real (type, dconst1),
7074 arg0);
7076 /* Optimize pow(x,1) = x. */
7077 if (c == 1)
7078 return arg0;
7080 /* Optimize pow(x,-1) = 1.0/x. */
7081 if (c == -1)
7082 return fold (build2 (RDIV_EXPR, type,
7083 build_real (type, dconst1), arg0));
7086 return NULL_TREE;
7089 /* A subroutine of fold_builtin to fold the various exponent
7090 functions. EXP is the CALL_EXPR of a call to a builtin function.
7091 VALUE is the value which will be raised to a power. */
7093 static tree
7094 fold_builtin_exponent (tree fndecl, tree arglist,
7095 const REAL_VALUE_TYPE *value)
7097 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7099 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7100 tree arg = TREE_VALUE (arglist);
7102 /* Optimize exp*(0.0) = 1.0. */
7103 if (real_zerop (arg))
7104 return build_real (type, dconst1);
7106 /* Optimize expN(1.0) = N. */
7107 if (real_onep (arg))
7109 REAL_VALUE_TYPE cst;
7111 real_convert (&cst, TYPE_MODE (type), value);
7112 return build_real (type, cst);
7115 /* Attempt to evaluate expN(integer) at compile-time. */
7116 if (flag_unsafe_math_optimizations
7117 && TREE_CODE (arg) == REAL_CST
7118 && ! TREE_CONSTANT_OVERFLOW (arg))
7120 REAL_VALUE_TYPE cint;
7121 REAL_VALUE_TYPE c;
7122 HOST_WIDE_INT n;
7124 c = TREE_REAL_CST (arg);
7125 n = real_to_integer (&c);
7126 real_from_integer (&cint, VOIDmode, n,
7127 n < 0 ? -1 : 0, 0);
7128 if (real_identical (&c, &cint))
7130 REAL_VALUE_TYPE x;
7132 real_powi (&x, TYPE_MODE (type), value, n);
7133 return build_real (type, x);
7137 /* Optimize expN(logN(x)) = x. */
7138 if (flag_unsafe_math_optimizations)
7140 const enum built_in_function fcode = builtin_mathfn_code (arg);
7142 if ((value == &dconste
7143 && (fcode == BUILT_IN_LOG
7144 || fcode == BUILT_IN_LOGF
7145 || fcode == BUILT_IN_LOGL))
7146 || (value == &dconst2
7147 && (fcode == BUILT_IN_LOG2
7148 || fcode == BUILT_IN_LOG2F
7149 || fcode == BUILT_IN_LOG2L))
7150 || (value == &dconst10
7151 && (fcode == BUILT_IN_LOG10
7152 || fcode == BUILT_IN_LOG10F
7153 || fcode == BUILT_IN_LOG10L)))
7154 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7158 return 0;
7161 /* Fold function call to builtin memcpy. Return
7162 NULL_TREE if no simplification can be made. */
7164 static tree
7165 fold_builtin_memcpy (tree fndecl, tree arglist)
7167 tree dest, src, len;
7169 if (!validate_arglist (arglist,
7170 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7171 return 0;
7173 dest = TREE_VALUE (arglist);
7174 src = TREE_VALUE (TREE_CHAIN (arglist));
7175 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7177 /* If the LEN parameter is zero, return DEST. */
7178 if (integer_zerop (len))
7179 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
7181 /* If SRC and DEST are the same (and not volatile), return DEST. */
7182 if (operand_equal_p (src, dest, 0))
7183 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
7185 return 0;
7188 /* Fold function call to builtin mempcpy. Return
7189 NULL_TREE if no simplification can be made. */
7191 static tree
7192 fold_builtin_mempcpy (tree arglist, tree type, int endp)
7194 if (validate_arglist (arglist,
7195 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7197 tree dest = TREE_VALUE (arglist);
7198 tree src = TREE_VALUE (TREE_CHAIN (arglist));
7199 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7201 /* If the LEN parameter is zero, return DEST. */
7202 if (integer_zerop (len))
7203 return omit_one_operand (type, dest, src);
7205 /* If SRC and DEST are the same (and not volatile), return DEST+LEN. */
7206 if (operand_equal_p (src, dest, 0))
7208 if (endp == 0)
7209 return omit_one_operand (type, dest, len);
7211 if (endp == 2)
7212 len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
7213 ssize_int (1)));
7215 len = fold_convert (TREE_TYPE (dest), len);
7216 len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
7217 return fold_convert (type, len);
7220 return 0;
7223 /* Fold function call to builtin memmove. Return
7224 NULL_TREE if no simplification can be made. */
7226 static tree
7227 fold_builtin_memmove (tree arglist, tree type)
7229 tree dest, src, len;
7231 if (!validate_arglist (arglist,
7232 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7233 return 0;
7235 dest = TREE_VALUE (arglist);
7236 src = TREE_VALUE (TREE_CHAIN (arglist));
7237 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7239 /* If the LEN parameter is zero, return DEST. */
7240 if (integer_zerop (len))
7241 return omit_one_operand (type, dest, src);
7243 /* If SRC and DEST are the same (and not volatile), return DEST. */
7244 if (operand_equal_p (src, dest, 0))
7245 return omit_one_operand (type, dest, len);
7247 return 0;
7250 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
7251 the length of the string to be copied. Return NULL_TREE if no
7252 simplification can be made. */
7254 tree
7255 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
7257 tree dest, src, fn;
7259 if (!validate_arglist (arglist,
7260 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7261 return 0;
7263 dest = TREE_VALUE (arglist);
7264 src = TREE_VALUE (TREE_CHAIN (arglist));
7266 /* If SRC and DEST are the same (and not volatile), return DEST. */
7267 if (operand_equal_p (src, dest, 0))
7268 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
7270 if (optimize_size)
7271 return 0;
7273 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7274 if (!fn)
7275 return 0;
7277 if (!len)
7279 len = c_strlen (src, 1);
7280 if (! len || TREE_SIDE_EFFECTS (len))
7281 return 0;
7284 len = size_binop (PLUS_EXPR, len, ssize_int (1));
7285 arglist = build_tree_list (NULL_TREE, len);
7286 arglist = tree_cons (NULL_TREE, src, arglist);
7287 arglist = tree_cons (NULL_TREE, dest, arglist);
7288 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7289 build_function_call_expr (fn, arglist));
7292 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
7293 the length of the source string. Return NULL_TREE if no simplification
7294 can be made. */
7296 tree
7297 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
7299 tree dest, src, len, fn;
7301 if (!validate_arglist (arglist,
7302 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7303 return 0;
7305 dest = TREE_VALUE (arglist);
7306 src = TREE_VALUE (TREE_CHAIN (arglist));
7307 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7309 /* If the LEN parameter is zero, return DEST. */
7310 if (integer_zerop (len))
7311 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
7313 /* We can't compare slen with len as constants below if len is not a
7314 constant. */
7315 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
7316 return 0;
7318 if (!slen)
7319 slen = c_strlen (src, 1);
7321 /* Now, we must be passed a constant src ptr parameter. */
7322 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
7323 return 0;
7325 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
7327 /* We do not support simplification of this case, though we do
7328 support it when expanding trees into RTL. */
7329 /* FIXME: generate a call to __builtin_memset. */
7330 if (tree_int_cst_lt (slen, len))
7331 return 0;
7333 /* OK transform into builtin memcpy. */
7334 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
7335 if (!fn)
7336 return 0;
7337 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
7338 build_function_call_expr (fn, arglist));
7341 /* Fold function call to builtin memcmp. Return
7342 NULL_TREE if no simplification can be made. */
7344 static tree
7345 fold_builtin_memcmp (tree arglist)
7347 tree arg1, arg2, len;
7348 const char *p1, *p2;
7350 if (!validate_arglist (arglist,
7351 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7352 return 0;
7354 arg1 = TREE_VALUE (arglist);
7355 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7356 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7358 /* If the LEN parameter is zero, return zero. */
7359 if (integer_zerop (len))
7360 return omit_two_operands (integer_type_node, integer_zero_node,
7361 arg1, arg2);
7363 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7364 if (operand_equal_p (arg1, arg2, 0))
7365 return omit_one_operand (integer_type_node, integer_zero_node, len);
7367 p1 = c_getstr (arg1);
7368 p2 = c_getstr (arg2);
7370 /* If all arguments are constant, and the value of len is not greater
7371 than the lengths of arg1 and arg2, evaluate at compile-time. */
7372 if (host_integerp (len, 1) && p1 && p2
7373 && compare_tree_int (len, strlen (p1) + 1) <= 0
7374 && compare_tree_int (len, strlen (p2) + 1) <= 0)
7376 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
7378 if (r > 0)
7379 return integer_one_node;
7380 else if (r < 0)
7381 return integer_minus_one_node;
7382 else
7383 return integer_zero_node;
7386 /* If len parameter is one, return an expression corresponding to
7387 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7388 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7390 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7391 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7392 tree ind1 = fold_convert (integer_type_node,
7393 build1 (INDIRECT_REF, cst_uchar_node,
7394 fold_convert (cst_uchar_ptr_node,
7395 arg1)));
7396 tree ind2 = fold_convert (integer_type_node,
7397 build1 (INDIRECT_REF, cst_uchar_node,
7398 fold_convert (cst_uchar_ptr_node,
7399 arg2)));
7400 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7403 return 0;
7406 /* Fold function call to builtin strcmp. Return
7407 NULL_TREE if no simplification can be made. */
7409 static tree
7410 fold_builtin_strcmp (tree arglist)
7412 tree arg1, arg2;
7413 const char *p1, *p2;
7415 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7416 return 0;
7418 arg1 = TREE_VALUE (arglist);
7419 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7421 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7422 if (operand_equal_p (arg1, arg2, 0))
7423 return integer_zero_node;
7425 p1 = c_getstr (arg1);
7426 p2 = c_getstr (arg2);
7428 if (p1 && p2)
7430 const int i = strcmp (p1, p2);
7431 if (i < 0)
7432 return integer_minus_one_node;
7433 else if (i > 0)
7434 return integer_one_node;
7435 else
7436 return integer_zero_node;
7439 /* If the second arg is "", return *(const unsigned char*)arg1. */
7440 if (p2 && *p2 == '\0')
7442 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7443 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7444 return fold_convert (integer_type_node,
7445 build1 (INDIRECT_REF, cst_uchar_node,
7446 fold_convert (cst_uchar_ptr_node,
7447 arg1)));
7450 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7451 if (p1 && *p1 == '\0')
7453 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7454 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7455 tree temp = fold_convert (integer_type_node,
7456 build1 (INDIRECT_REF, cst_uchar_node,
7457 fold_convert (cst_uchar_ptr_node,
7458 arg2)));
7459 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7462 return 0;
7465 /* Fold function call to builtin strncmp. Return
7466 NULL_TREE if no simplification can be made. */
7468 static tree
7469 fold_builtin_strncmp (tree arglist)
7471 tree arg1, arg2, len;
7472 const char *p1, *p2;
7474 if (!validate_arglist (arglist,
7475 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7476 return 0;
7478 arg1 = TREE_VALUE (arglist);
7479 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7480 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7482 /* If the LEN parameter is zero, return zero. */
7483 if (integer_zerop (len))
7484 return omit_two_operands (integer_type_node, integer_zero_node,
7485 arg1, arg2);
7487 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7488 if (operand_equal_p (arg1, arg2, 0))
7489 return omit_one_operand (integer_type_node, integer_zero_node, len);
7491 p1 = c_getstr (arg1);
7492 p2 = c_getstr (arg2);
7494 if (host_integerp (len, 1) && p1 && p2)
7496 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
7497 if (i > 0)
7498 return integer_one_node;
7499 else if (i < 0)
7500 return integer_minus_one_node;
7501 else
7502 return integer_zero_node;
7505 /* If the second arg is "", and the length is greater than zero,
7506 return *(const unsigned char*)arg1. */
7507 if (p2 && *p2 == '\0'
7508 && TREE_CODE (len) == INTEGER_CST
7509 && tree_int_cst_sgn (len) == 1)
7511 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7512 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7513 return fold_convert (integer_type_node,
7514 build1 (INDIRECT_REF, cst_uchar_node,
7515 fold_convert (cst_uchar_ptr_node,
7516 arg1)));
7519 /* If the first arg is "", and the length is greater than zero,
7520 return -*(const unsigned char*)arg2. */
7521 if (p1 && *p1 == '\0'
7522 && TREE_CODE (len) == INTEGER_CST
7523 && tree_int_cst_sgn (len) == 1)
7525 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7526 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7527 tree temp = fold_convert (integer_type_node,
7528 build1 (INDIRECT_REF, cst_uchar_node,
7529 fold_convert (cst_uchar_ptr_node,
7530 arg2)));
7531 return fold (build1 (NEGATE_EXPR, integer_type_node, temp));
7534 /* If len parameter is one, return an expression corresponding to
7535 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7536 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
7538 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
7539 tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node);
7540 tree ind1 = fold_convert (integer_type_node,
7541 build1 (INDIRECT_REF, cst_uchar_node,
7542 fold_convert (cst_uchar_ptr_node,
7543 arg1)));
7544 tree ind2 = fold_convert (integer_type_node,
7545 build1 (INDIRECT_REF, cst_uchar_node,
7546 fold_convert (cst_uchar_ptr_node,
7547 arg2)));
7548 return fold (build2 (MINUS_EXPR, integer_type_node, ind1, ind2));
7551 return 0;
7554 /* Fold function call to builtin signbit, signbitf or signbitl. Return
7555 NULL_TREE if no simplification can be made. */
7557 static tree
7558 fold_builtin_signbit (tree fndecl, tree arglist)
7560 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7561 tree arg, temp;
7563 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7564 return NULL_TREE;
7566 arg = TREE_VALUE (arglist);
7568 /* If ARG is a compile-time constant, determine the result. */
7569 if (TREE_CODE (arg) == REAL_CST
7570 && !TREE_CONSTANT_OVERFLOW (arg))
7572 REAL_VALUE_TYPE c;
7574 c = TREE_REAL_CST (arg);
7575 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
7576 return fold_convert (type, temp);
7579 /* If ARG is non-negative, the result is always zero. */
7580 if (tree_expr_nonnegative_p (arg))
7581 return omit_one_operand (type, integer_zero_node, arg);
7583 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
7584 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
7585 return fold (build2 (LT_EXPR, type, arg,
7586 build_real (TREE_TYPE (arg), dconst0)));
7588 return NULL_TREE;
7591 /* Fold function call to builtin copysign, copysignf or copysignl.
7592 Return NULL_TREE if no simplification can be made. */
7594 static tree
7595 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
7597 tree arg1, arg2, tem;
7599 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7600 return NULL_TREE;
7602 arg1 = TREE_VALUE (arglist);
7603 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
7605 /* copysign(X,X) is X. */
7606 if (operand_equal_p (arg1, arg2, 0))
7607 return fold_convert (type, arg1);
7609 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
7610 if (TREE_CODE (arg1) == REAL_CST
7611 && TREE_CODE (arg2) == REAL_CST
7612 && !TREE_CONSTANT_OVERFLOW (arg1)
7613 && !TREE_CONSTANT_OVERFLOW (arg2))
7615 REAL_VALUE_TYPE c1, c2;
7617 c1 = TREE_REAL_CST (arg1);
7618 c2 = TREE_REAL_CST (arg2);
7619 real_copysign (&c1, &c2);
7620 return build_real (type, c1);
7621 c1.sign = c2.sign;
7624 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
7625 Remember to evaluate Y for side-effects. */
7626 if (tree_expr_nonnegative_p (arg2))
7627 return omit_one_operand (type,
7628 fold (build1 (ABS_EXPR, type, arg1)),
7629 arg2);
7631 /* Strip sign changing operations for the first argument. */
7632 tem = fold_strip_sign_ops (arg1);
7633 if (tem)
7635 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
7636 return build_function_call_expr (fndecl, arglist);
7639 return NULL_TREE;
7642 /* Fold a call to builtin isascii. */
7644 static tree
7645 fold_builtin_isascii (tree arglist)
7647 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7648 return 0;
7649 else
7651 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7652 tree arg = TREE_VALUE (arglist);
7654 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
7655 build_int_cst (NULL_TREE,
7656 ~ (unsigned HOST_WIDE_INT) 0x7f));
7657 arg = fold (build2 (EQ_EXPR, integer_type_node,
7658 arg, integer_zero_node));
7660 if (in_gimple_form && !TREE_CONSTANT (arg))
7661 return NULL_TREE;
7662 else
7663 return arg;
7667 /* Fold a call to builtin toascii. */
7669 static tree
7670 fold_builtin_toascii (tree arglist)
7672 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7673 return 0;
7674 else
7676 /* Transform toascii(c) -> (c & 0x7f). */
7677 tree arg = TREE_VALUE (arglist);
7679 return fold (build2 (BIT_AND_EXPR, integer_type_node, arg,
7680 build_int_cst (NULL_TREE, 0x7f)));
7684 /* Fold a call to builtin isdigit. */
7686 static tree
7687 fold_builtin_isdigit (tree arglist)
7689 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7690 return 0;
7691 else
7693 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7694 /* According to the C standard, isdigit is unaffected by locale.
7695 However, it definitely is affected by the target character set. */
7696 tree arg;
7697 unsigned HOST_WIDE_INT target_digit0
7698 = lang_hooks.to_target_charset ('0');
7700 if (target_digit0 == 0)
7701 return NULL_TREE;
7703 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
7704 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
7705 build_int_cst (unsigned_type_node, target_digit0));
7706 arg = build2 (LE_EXPR, integer_type_node, arg,
7707 build_int_cst (unsigned_type_node, 9));
7708 arg = fold (arg);
7709 if (in_gimple_form && !TREE_CONSTANT (arg))
7710 return NULL_TREE;
7711 else
7712 return arg;
7716 /* Fold a call to fabs, fabsf or fabsl. */
7718 static tree
7719 fold_builtin_fabs (tree arglist, tree type)
7721 tree arg;
7723 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7724 return 0;
7726 arg = TREE_VALUE (arglist);
7727 arg = fold_convert (type, arg);
7728 if (TREE_CODE (arg) == REAL_CST)
7729 return fold_abs_const (arg, type);
7730 return fold (build1 (ABS_EXPR, type, arg));
7733 /* Fold a call to abs, labs, llabs or imaxabs. */
7735 static tree
7736 fold_builtin_abs (tree arglist, tree type)
7738 tree arg;
7740 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7741 return 0;
7743 arg = TREE_VALUE (arglist);
7744 arg = fold_convert (type, arg);
7745 if (TREE_CODE (arg) == INTEGER_CST)
7746 return fold_abs_const (arg, type);
7747 return fold (build1 (ABS_EXPR, type, arg));
7750 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
7751 EXP is the CALL_EXPR for the call. */
7753 static tree
7754 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
7756 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7757 tree arg;
7758 REAL_VALUE_TYPE r;
7760 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7762 /* Check that we have exactly one argument. */
7763 if (arglist == 0)
7765 error ("too few arguments to function %qs",
7766 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7767 return error_mark_node;
7769 else if (TREE_CHAIN (arglist) != 0)
7771 error ("too many arguments to function %qs",
7772 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7773 return error_mark_node;
7775 else
7777 error ("non-floating-point argument to function %qs",
7778 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7779 return error_mark_node;
7783 arg = TREE_VALUE (arglist);
7784 switch (builtin_index)
7786 case BUILT_IN_ISINF:
7787 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7788 return omit_one_operand (type, integer_zero_node, arg);
7790 if (TREE_CODE (arg) == REAL_CST)
7792 r = TREE_REAL_CST (arg);
7793 if (real_isinf (&r))
7794 return real_compare (GT_EXPR, &r, &dconst0)
7795 ? integer_one_node : integer_minus_one_node;
7796 else
7797 return integer_zero_node;
7800 return NULL_TREE;
7802 case BUILT_IN_FINITE:
7803 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
7804 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
7805 return omit_one_operand (type, integer_zero_node, arg);
7807 if (TREE_CODE (arg) == REAL_CST)
7809 r = TREE_REAL_CST (arg);
7810 return real_isinf (&r) || real_isnan (&r)
7811 ? integer_zero_node : integer_one_node;
7814 return NULL_TREE;
7816 case BUILT_IN_ISNAN:
7817 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
7818 return omit_one_operand (type, integer_zero_node, arg);
7820 if (TREE_CODE (arg) == REAL_CST)
7822 r = TREE_REAL_CST (arg);
7823 return real_isnan (&r) ? integer_one_node : integer_zero_node;
7826 arg = builtin_save_expr (arg);
7827 return fold (build2 (UNORDERED_EXPR, type, arg, arg));
7829 default:
7830 gcc_unreachable ();
7834 /* Fold a call to an unordered comparison function such as
7835 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
7836 being called and ARGLIST is the argument list for the call.
7837 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
7838 the opposite of the desired result. UNORDERED_CODE is used
7839 for modes that can hold NaNs and ORDERED_CODE is used for
7840 the rest. */
7842 static tree
7843 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
7844 enum tree_code unordered_code,
7845 enum tree_code ordered_code)
7847 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7848 enum tree_code code;
7849 tree arg0, arg1;
7850 tree type0, type1;
7851 enum tree_code code0, code1;
7852 tree cmp_type = NULL_TREE;
7854 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7856 /* Check that we have exactly two arguments. */
7857 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
7859 error ("too few arguments to function %qs",
7860 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7861 return error_mark_node;
7863 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
7865 error ("too many arguments to function %qs",
7866 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7867 return error_mark_node;
7871 arg0 = TREE_VALUE (arglist);
7872 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7874 type0 = TREE_TYPE (arg0);
7875 type1 = TREE_TYPE (arg1);
7877 code0 = TREE_CODE (type0);
7878 code1 = TREE_CODE (type1);
7880 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
7881 /* Choose the wider of two real types. */
7882 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
7883 ? type0 : type1;
7884 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
7885 cmp_type = type0;
7886 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
7887 cmp_type = type1;
7888 else
7890 error ("non-floating-point argument to function %qs",
7891 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
7892 return error_mark_node;
7895 arg0 = fold_convert (cmp_type, arg0);
7896 arg1 = fold_convert (cmp_type, arg1);
7898 if (unordered_code == UNORDERED_EXPR)
7900 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7901 return omit_two_operands (type, integer_zero_node, arg0, arg1);
7902 return fold (build2 (UNORDERED_EXPR, type, arg0, arg1));
7905 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
7906 : ordered_code;
7907 return fold (build1 (TRUTH_NOT_EXPR, type,
7908 fold (build2 (code, type, arg0, arg1))));
7911 /* Fold a call to one of the external complex multiply libcalls. */
7913 static tree
7914 fold_builtin_complex_mul (tree type, tree arglist)
7916 tree ar, ai, br, bi;
7918 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7919 REAL_TYPE, VOID_TYPE))
7920 return NULL;
7922 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7923 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7924 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7925 bi = TREE_VALUE (arglist);
7927 return fold_complex_mult_parts (type, ar, ai, br, bi);
7930 /* Fold a call to one of the external complex division libcalls. */
7932 static tree
7933 fold_builtin_complex_div (tree type, tree arglist)
7935 tree ar, ai, br, bi;
7937 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, REAL_TYPE,
7938 REAL_TYPE, VOID_TYPE))
7939 return NULL;
7941 ar = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7942 ai = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7943 br = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
7944 bi = TREE_VALUE (arglist);
7946 return fold_complex_div_parts (type, ar, ai, br, bi, RDIV_EXPR);
7949 /* Used by constant folding to simplify calls to builtin functions. EXP is
7950 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
7951 result of the function call is ignored. This function returns NULL_TREE
7952 if no simplification was possible. */
7954 static tree
7955 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
7957 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7958 enum built_in_function fcode;
7960 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7961 return targetm.fold_builtin (fndecl, arglist, ignore);
7963 fcode = DECL_FUNCTION_CODE (fndecl);
7964 switch (fcode)
7966 case BUILT_IN_FPUTS:
7967 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
7969 case BUILT_IN_FPUTS_UNLOCKED:
7970 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
7972 case BUILT_IN_STRSTR:
7973 return fold_builtin_strstr (arglist, type);
7975 case BUILT_IN_STRCAT:
7976 return fold_builtin_strcat (arglist);
7978 case BUILT_IN_STRNCAT:
7979 return fold_builtin_strncat (arglist);
7981 case BUILT_IN_STRSPN:
7982 return fold_builtin_strspn (arglist);
7984 case BUILT_IN_STRCSPN:
7985 return fold_builtin_strcspn (arglist);
7987 case BUILT_IN_STRCHR:
7988 case BUILT_IN_INDEX:
7989 return fold_builtin_strchr (arglist, type);
7991 case BUILT_IN_STRRCHR:
7992 case BUILT_IN_RINDEX:
7993 return fold_builtin_strrchr (arglist, type);
7995 case BUILT_IN_STRCPY:
7996 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
7998 case BUILT_IN_STRNCPY:
7999 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8001 case BUILT_IN_STRCMP:
8002 return fold_builtin_strcmp (arglist);
8004 case BUILT_IN_STRNCMP:
8005 return fold_builtin_strncmp (arglist);
8007 case BUILT_IN_STRPBRK:
8008 return fold_builtin_strpbrk (arglist, type);
8010 case BUILT_IN_BCMP:
8011 case BUILT_IN_MEMCMP:
8012 return fold_builtin_memcmp (arglist);
8014 case BUILT_IN_SPRINTF:
8015 return fold_builtin_sprintf (arglist, ignore);
8017 case BUILT_IN_CONSTANT_P:
8019 tree val;
8021 val = fold_builtin_constant_p (arglist);
8022 /* Gimplification will pull the CALL_EXPR for the builtin out of
8023 an if condition. When not optimizing, we'll not CSE it back.
8024 To avoid link error types of regressions, return false now. */
8025 if (!val && !optimize)
8026 val = integer_zero_node;
8028 return val;
8031 case BUILT_IN_EXPECT:
8032 return fold_builtin_expect (arglist);
8034 case BUILT_IN_CLASSIFY_TYPE:
8035 return fold_builtin_classify_type (arglist);
8037 case BUILT_IN_STRLEN:
8038 return fold_builtin_strlen (arglist);
8040 case BUILT_IN_FABS:
8041 case BUILT_IN_FABSF:
8042 case BUILT_IN_FABSL:
8043 return fold_builtin_fabs (arglist, type);
8045 case BUILT_IN_ABS:
8046 case BUILT_IN_LABS:
8047 case BUILT_IN_LLABS:
8048 case BUILT_IN_IMAXABS:
8049 return fold_builtin_abs (arglist, type);
8051 case BUILT_IN_CONJ:
8052 case BUILT_IN_CONJF:
8053 case BUILT_IN_CONJL:
8054 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8055 return fold (build1 (CONJ_EXPR, type, TREE_VALUE (arglist)));
8056 break;
8058 case BUILT_IN_CREAL:
8059 case BUILT_IN_CREALF:
8060 case BUILT_IN_CREALL:
8061 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8062 return non_lvalue (fold (build1 (REALPART_EXPR, type,
8063 TREE_VALUE (arglist))));
8064 break;
8066 case BUILT_IN_CIMAG:
8067 case BUILT_IN_CIMAGF:
8068 case BUILT_IN_CIMAGL:
8069 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8070 return non_lvalue (fold (build1 (IMAGPART_EXPR, type,
8071 TREE_VALUE (arglist))));
8072 break;
8074 case BUILT_IN_CABS:
8075 case BUILT_IN_CABSF:
8076 case BUILT_IN_CABSL:
8077 return fold_builtin_cabs (arglist, type);
8079 case BUILT_IN_SQRT:
8080 case BUILT_IN_SQRTF:
8081 case BUILT_IN_SQRTL:
8082 return fold_builtin_sqrt (arglist, type);
8084 case BUILT_IN_CBRT:
8085 case BUILT_IN_CBRTF:
8086 case BUILT_IN_CBRTL:
8087 return fold_builtin_cbrt (arglist, type);
8089 case BUILT_IN_SIN:
8090 case BUILT_IN_SINF:
8091 case BUILT_IN_SINL:
8092 return fold_builtin_sin (arglist);
8094 case BUILT_IN_COS:
8095 case BUILT_IN_COSF:
8096 case BUILT_IN_COSL:
8097 return fold_builtin_cos (arglist, type, fndecl);
8099 case BUILT_IN_EXP:
8100 case BUILT_IN_EXPF:
8101 case BUILT_IN_EXPL:
8102 return fold_builtin_exponent (fndecl, arglist, &dconste);
8104 case BUILT_IN_EXP2:
8105 case BUILT_IN_EXP2F:
8106 case BUILT_IN_EXP2L:
8107 return fold_builtin_exponent (fndecl, arglist, &dconst2);
8109 case BUILT_IN_EXP10:
8110 case BUILT_IN_EXP10F:
8111 case BUILT_IN_EXP10L:
8112 case BUILT_IN_POW10:
8113 case BUILT_IN_POW10F:
8114 case BUILT_IN_POW10L:
8115 return fold_builtin_exponent (fndecl, arglist, &dconst10);
8117 case BUILT_IN_LOG:
8118 case BUILT_IN_LOGF:
8119 case BUILT_IN_LOGL:
8120 return fold_builtin_logarithm (fndecl, arglist, &dconste);
8122 case BUILT_IN_LOG2:
8123 case BUILT_IN_LOG2F:
8124 case BUILT_IN_LOG2L:
8125 return fold_builtin_logarithm (fndecl, arglist, &dconst2);
8127 case BUILT_IN_LOG10:
8128 case BUILT_IN_LOG10F:
8129 case BUILT_IN_LOG10L:
8130 return fold_builtin_logarithm (fndecl, arglist, &dconst10);
8132 case BUILT_IN_TAN:
8133 case BUILT_IN_TANF:
8134 case BUILT_IN_TANL:
8135 return fold_builtin_tan (arglist);
8137 case BUILT_IN_ATAN:
8138 case BUILT_IN_ATANF:
8139 case BUILT_IN_ATANL:
8140 return fold_builtin_atan (arglist, type);
8142 case BUILT_IN_POW:
8143 case BUILT_IN_POWF:
8144 case BUILT_IN_POWL:
8145 return fold_builtin_pow (fndecl, arglist, type);
8147 case BUILT_IN_POWI:
8148 case BUILT_IN_POWIF:
8149 case BUILT_IN_POWIL:
8150 return fold_builtin_powi (fndecl, arglist, type);
8152 case BUILT_IN_INF:
8153 case BUILT_IN_INFF:
8154 case BUILT_IN_INFL:
8155 return fold_builtin_inf (type, true);
8157 case BUILT_IN_HUGE_VAL:
8158 case BUILT_IN_HUGE_VALF:
8159 case BUILT_IN_HUGE_VALL:
8160 return fold_builtin_inf (type, false);
8162 case BUILT_IN_NAN:
8163 case BUILT_IN_NANF:
8164 case BUILT_IN_NANL:
8165 return fold_builtin_nan (arglist, type, true);
8167 case BUILT_IN_NANS:
8168 case BUILT_IN_NANSF:
8169 case BUILT_IN_NANSL:
8170 return fold_builtin_nan (arglist, type, false);
8172 case BUILT_IN_FLOOR:
8173 case BUILT_IN_FLOORF:
8174 case BUILT_IN_FLOORL:
8175 return fold_builtin_floor (fndecl, arglist);
8177 case BUILT_IN_CEIL:
8178 case BUILT_IN_CEILF:
8179 case BUILT_IN_CEILL:
8180 return fold_builtin_ceil (fndecl, arglist);
8182 case BUILT_IN_TRUNC:
8183 case BUILT_IN_TRUNCF:
8184 case BUILT_IN_TRUNCL:
8185 return fold_builtin_trunc (fndecl, arglist);
8187 case BUILT_IN_ROUND:
8188 case BUILT_IN_ROUNDF:
8189 case BUILT_IN_ROUNDL:
8190 return fold_builtin_round (fndecl, arglist);
8192 case BUILT_IN_NEARBYINT:
8193 case BUILT_IN_NEARBYINTF:
8194 case BUILT_IN_NEARBYINTL:
8195 case BUILT_IN_RINT:
8196 case BUILT_IN_RINTF:
8197 case BUILT_IN_RINTL:
8198 return fold_trunc_transparent_mathfn (fndecl, arglist);
8200 case BUILT_IN_LROUND:
8201 case BUILT_IN_LROUNDF:
8202 case BUILT_IN_LROUNDL:
8203 case BUILT_IN_LLROUND:
8204 case BUILT_IN_LLROUNDF:
8205 case BUILT_IN_LLROUNDL:
8206 return fold_builtin_lround (fndecl, arglist);
8208 case BUILT_IN_LRINT:
8209 case BUILT_IN_LRINTF:
8210 case BUILT_IN_LRINTL:
8211 case BUILT_IN_LLRINT:
8212 case BUILT_IN_LLRINTF:
8213 case BUILT_IN_LLRINTL:
8214 return fold_fixed_mathfn (fndecl, arglist);
8216 case BUILT_IN_FFS:
8217 case BUILT_IN_FFSL:
8218 case BUILT_IN_FFSLL:
8219 case BUILT_IN_CLZ:
8220 case BUILT_IN_CLZL:
8221 case BUILT_IN_CLZLL:
8222 case BUILT_IN_CTZ:
8223 case BUILT_IN_CTZL:
8224 case BUILT_IN_CTZLL:
8225 case BUILT_IN_POPCOUNT:
8226 case BUILT_IN_POPCOUNTL:
8227 case BUILT_IN_POPCOUNTLL:
8228 case BUILT_IN_PARITY:
8229 case BUILT_IN_PARITYL:
8230 case BUILT_IN_PARITYLL:
8231 return fold_builtin_bitop (fndecl, arglist);
8233 case BUILT_IN_MEMCPY:
8234 return fold_builtin_memcpy (fndecl, arglist);
8236 case BUILT_IN_MEMPCPY:
8237 return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
8239 case BUILT_IN_MEMMOVE:
8240 return fold_builtin_memmove (arglist, type);
8242 case BUILT_IN_SIGNBIT:
8243 case BUILT_IN_SIGNBITF:
8244 case BUILT_IN_SIGNBITL:
8245 return fold_builtin_signbit (fndecl, arglist);
8247 case BUILT_IN_ISASCII:
8248 return fold_builtin_isascii (arglist);
8250 case BUILT_IN_TOASCII:
8251 return fold_builtin_toascii (arglist);
8253 case BUILT_IN_ISDIGIT:
8254 return fold_builtin_isdigit (arglist);
8256 case BUILT_IN_COPYSIGN:
8257 case BUILT_IN_COPYSIGNF:
8258 case BUILT_IN_COPYSIGNL:
8259 return fold_builtin_copysign (fndecl, arglist, type);
8261 case BUILT_IN_FINITE:
8262 case BUILT_IN_FINITEF:
8263 case BUILT_IN_FINITEL:
8264 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
8266 case BUILT_IN_ISINF:
8267 case BUILT_IN_ISINFF:
8268 case BUILT_IN_ISINFL:
8269 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
8271 case BUILT_IN_ISNAN:
8272 case BUILT_IN_ISNANF:
8273 case BUILT_IN_ISNANL:
8274 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
8276 case BUILT_IN_ISGREATER:
8277 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
8278 case BUILT_IN_ISGREATEREQUAL:
8279 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
8280 case BUILT_IN_ISLESS:
8281 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
8282 case BUILT_IN_ISLESSEQUAL:
8283 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
8284 case BUILT_IN_ISLESSGREATER:
8285 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
8286 case BUILT_IN_ISUNORDERED:
8287 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
8288 NOP_EXPR);
8290 /* We do the folding for va_start in the expander. */
8291 case BUILT_IN_VA_START:
8292 break;
8294 default:
8295 if (fcode >= BUILT_IN_COMPLEX_MUL_MIN
8296 && fcode <= BUILT_IN_COMPLEX_MUL_MAX)
8297 return fold_builtin_complex_mul (type, arglist);
8298 if (fcode >= BUILT_IN_COMPLEX_DIV_MIN
8299 && fcode <= BUILT_IN_COMPLEX_DIV_MAX)
8300 return fold_builtin_complex_div (type, arglist);
8301 break;
8304 return 0;
8307 /* A wrapper function for builtin folding that prevents warnings for
8308 "statement without effect" and the like, caused by removing the
8309 call node earlier than the warning is generated. */
8311 tree
8312 fold_builtin (tree fndecl, tree arglist, bool ignore)
8314 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
8315 if (exp)
8317 /* ??? Don't clobber shared nodes such as integer_zero_node. */
8318 if (CONSTANT_CLASS_P (exp))
8319 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
8320 TREE_NO_WARNING (exp) = 1;
8323 return exp;
8326 /* Conveniently construct a function call expression. */
8328 tree
8329 build_function_call_expr (tree fn, tree arglist)
8331 tree call_expr;
8333 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
8334 call_expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
8335 call_expr, arglist, NULL_TREE);
8336 return fold (call_expr);
8339 /* This function validates the types of a function call argument list
8340 represented as a tree chain of parameters against a specified list
8341 of tree_codes. If the last specifier is a 0, that represents an
8342 ellipses, otherwise the last specifier must be a VOID_TYPE. */
8344 static int
8345 validate_arglist (tree arglist, ...)
8347 enum tree_code code;
8348 int res = 0;
8349 va_list ap;
8351 va_start (ap, arglist);
8355 code = va_arg (ap, enum tree_code);
8356 switch (code)
8358 case 0:
8359 /* This signifies an ellipses, any further arguments are all ok. */
8360 res = 1;
8361 goto end;
8362 case VOID_TYPE:
8363 /* This signifies an endlink, if no arguments remain, return
8364 true, otherwise return false. */
8365 res = arglist == 0;
8366 goto end;
8367 default:
8368 /* If no parameters remain or the parameter's code does not
8369 match the specified code, return false. Otherwise continue
8370 checking any remaining arguments. */
8371 if (arglist == 0)
8372 goto end;
8373 if (code == POINTER_TYPE)
8375 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
8376 goto end;
8378 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
8379 goto end;
8380 break;
8382 arglist = TREE_CHAIN (arglist);
8384 while (1);
8386 /* We need gotos here since we can only have one VA_CLOSE in a
8387 function. */
8388 end: ;
8389 va_end (ap);
8391 return res;
8394 /* Default target-specific builtin expander that does nothing. */
8397 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
8398 rtx target ATTRIBUTE_UNUSED,
8399 rtx subtarget ATTRIBUTE_UNUSED,
8400 enum machine_mode mode ATTRIBUTE_UNUSED,
8401 int ignore ATTRIBUTE_UNUSED)
8403 return NULL_RTX;
8406 /* Returns true is EXP represents data that would potentially reside
8407 in a readonly section. */
8409 static bool
8410 readonly_data_expr (tree exp)
8412 STRIP_NOPS (exp);
8414 if (TREE_CODE (exp) != ADDR_EXPR)
8415 return false;
8417 exp = get_base_address (TREE_OPERAND (exp, 0));
8418 if (!exp)
8419 return false;
8421 /* Make sure we call decl_readonly_section only for trees it
8422 can handle (since it returns true for everything it doesn't
8423 understand). */
8424 if (TREE_CODE (exp) == STRING_CST
8425 || TREE_CODE (exp) == CONSTRUCTOR
8426 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
8427 return decl_readonly_section (exp, 0);
8428 else
8429 return false;
8432 /* Simplify a call to the strstr builtin.
8434 Return 0 if no simplification was possible, otherwise return the
8435 simplified form of the call as a tree.
8437 The simplified form may be a constant or other expression which
8438 computes the same value, but in a more efficient manner (including
8439 calls to other builtin functions).
8441 The call may contain arguments which need to be evaluated, but
8442 which are not useful to determine the result of the call. In
8443 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8444 COMPOUND_EXPR will be an argument which must be evaluated.
8445 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8446 COMPOUND_EXPR in the chain will contain the tree for the simplified
8447 form of the builtin function call. */
8449 static tree
8450 fold_builtin_strstr (tree arglist, tree type)
8452 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8453 return 0;
8454 else
8456 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8457 tree fn;
8458 const char *p1, *p2;
8460 p2 = c_getstr (s2);
8461 if (p2 == NULL)
8462 return 0;
8464 p1 = c_getstr (s1);
8465 if (p1 != NULL)
8467 const char *r = strstr (p1, p2);
8468 tree tem;
8470 if (r == NULL)
8471 return build_int_cst (TREE_TYPE (s1), 0);
8473 /* Return an offset into the constant string argument. */
8474 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8475 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8476 return fold_convert (type, tem);
8479 if (p2[0] == '\0')
8480 return s1;
8482 if (p2[1] != '\0')
8483 return 0;
8485 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8486 if (!fn)
8487 return 0;
8489 /* New argument list transforming strstr(s1, s2) to
8490 strchr(s1, s2[0]). */
8491 arglist = build_tree_list (NULL_TREE,
8492 build_int_cst (NULL_TREE, p2[0]));
8493 arglist = tree_cons (NULL_TREE, s1, arglist);
8494 return build_function_call_expr (fn, arglist);
8498 /* Simplify a call to the strchr builtin.
8500 Return 0 if no simplification was possible, otherwise return the
8501 simplified form of the call as a tree.
8503 The simplified form may be a constant or other expression which
8504 computes the same value, but in a more efficient manner (including
8505 calls to other builtin functions).
8507 The call may contain arguments which need to be evaluated, but
8508 which are not useful to determine the result of the call. In
8509 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8510 COMPOUND_EXPR will be an argument which must be evaluated.
8511 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8512 COMPOUND_EXPR in the chain will contain the tree for the simplified
8513 form of the builtin function call. */
8515 static tree
8516 fold_builtin_strchr (tree arglist, tree type)
8518 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8519 return 0;
8520 else
8522 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8523 const char *p1;
8525 if (TREE_CODE (s2) != INTEGER_CST)
8526 return 0;
8528 p1 = c_getstr (s1);
8529 if (p1 != NULL)
8531 char c;
8532 const char *r;
8533 tree tem;
8535 if (target_char_cast (s2, &c))
8536 return 0;
8538 r = strchr (p1, c);
8540 if (r == NULL)
8541 return build_int_cst (TREE_TYPE (s1), 0);
8543 /* Return an offset into the constant string argument. */
8544 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8545 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8546 return fold_convert (type, tem);
8548 return 0;
8552 /* Simplify a call to the strrchr builtin.
8554 Return 0 if no simplification was possible, otherwise return the
8555 simplified form of the call as a tree.
8557 The simplified form may be a constant or other expression which
8558 computes the same value, but in a more efficient manner (including
8559 calls to other builtin functions).
8561 The call may contain arguments which need to be evaluated, but
8562 which are not useful to determine the result of the call. In
8563 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8564 COMPOUND_EXPR will be an argument which must be evaluated.
8565 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8566 COMPOUND_EXPR in the chain will contain the tree for the simplified
8567 form of the builtin function call. */
8569 static tree
8570 fold_builtin_strrchr (tree arglist, tree type)
8572 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8573 return 0;
8574 else
8576 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8577 tree fn;
8578 const char *p1;
8580 if (TREE_CODE (s2) != INTEGER_CST)
8581 return 0;
8583 p1 = c_getstr (s1);
8584 if (p1 != NULL)
8586 char c;
8587 const char *r;
8588 tree tem;
8590 if (target_char_cast (s2, &c))
8591 return 0;
8593 r = strrchr (p1, c);
8595 if (r == NULL)
8596 return build_int_cst (TREE_TYPE (s1), 0);
8598 /* Return an offset into the constant string argument. */
8599 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8600 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8601 return fold_convert (type, tem);
8604 if (! integer_zerop (s2))
8605 return 0;
8607 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8608 if (!fn)
8609 return 0;
8611 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
8612 return build_function_call_expr (fn, arglist);
8616 /* Simplify a call to the strpbrk builtin.
8618 Return 0 if no simplification was possible, otherwise return the
8619 simplified form of the call as a tree.
8621 The simplified form may be a constant or other expression which
8622 computes the same value, but in a more efficient manner (including
8623 calls to other builtin functions).
8625 The call may contain arguments which need to be evaluated, but
8626 which are not useful to determine the result of the call. In
8627 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8628 COMPOUND_EXPR will be an argument which must be evaluated.
8629 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8630 COMPOUND_EXPR in the chain will contain the tree for the simplified
8631 form of the builtin function call. */
8633 static tree
8634 fold_builtin_strpbrk (tree arglist, tree type)
8636 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8637 return 0;
8638 else
8640 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8641 tree fn;
8642 const char *p1, *p2;
8644 p2 = c_getstr (s2);
8645 if (p2 == NULL)
8646 return 0;
8648 p1 = c_getstr (s1);
8649 if (p1 != NULL)
8651 const char *r = strpbrk (p1, p2);
8652 tree tem;
8654 if (r == NULL)
8655 return build_int_cst (TREE_TYPE (s1), 0);
8657 /* Return an offset into the constant string argument. */
8658 tem = fold (build2 (PLUS_EXPR, TREE_TYPE (s1),
8659 s1, build_int_cst (TREE_TYPE (s1), r - p1)));
8660 return fold_convert (type, tem);
8663 if (p2[0] == '\0')
8664 /* strpbrk(x, "") == NULL.
8665 Evaluate and ignore s1 in case it had side-effects. */
8666 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
8668 if (p2[1] != '\0')
8669 return 0; /* Really call strpbrk. */
8671 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
8672 if (!fn)
8673 return 0;
8675 /* New argument list transforming strpbrk(s1, s2) to
8676 strchr(s1, s2[0]). */
8677 arglist = build_tree_list (NULL_TREE,
8678 build_int_cst (NULL_TREE, p2[0]));
8679 arglist = tree_cons (NULL_TREE, s1, arglist);
8680 return build_function_call_expr (fn, arglist);
8684 /* Simplify a call to the strcat builtin.
8686 Return 0 if no simplification was possible, otherwise return the
8687 simplified form of the call as a tree.
8689 The simplified form may be a constant or other expression which
8690 computes the same value, but in a more efficient manner (including
8691 calls to other builtin functions).
8693 The call may contain arguments which need to be evaluated, but
8694 which are not useful to determine the result of the call. In
8695 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8696 COMPOUND_EXPR will be an argument which must be evaluated.
8697 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8698 COMPOUND_EXPR in the chain will contain the tree for the simplified
8699 form of the builtin function call. */
8701 static tree
8702 fold_builtin_strcat (tree arglist)
8704 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8705 return 0;
8706 else
8708 tree dst = TREE_VALUE (arglist),
8709 src = TREE_VALUE (TREE_CHAIN (arglist));
8710 const char *p = c_getstr (src);
8712 /* If the string length is zero, return the dst parameter. */
8713 if (p && *p == '\0')
8714 return dst;
8716 return 0;
8720 /* Simplify a call to the strncat builtin.
8722 Return 0 if no simplification was possible, otherwise return the
8723 simplified form of the call as a tree.
8725 The simplified form may be a constant or other expression which
8726 computes the same value, but in a more efficient manner (including
8727 calls to other builtin functions).
8729 The call may contain arguments which need to be evaluated, but
8730 which are not useful to determine the result of the call. In
8731 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8732 COMPOUND_EXPR will be an argument which must be evaluated.
8733 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8734 COMPOUND_EXPR in the chain will contain the tree for the simplified
8735 form of the builtin function call. */
8737 static tree
8738 fold_builtin_strncat (tree arglist)
8740 if (!validate_arglist (arglist,
8741 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8742 return 0;
8743 else
8745 tree dst = TREE_VALUE (arglist);
8746 tree src = TREE_VALUE (TREE_CHAIN (arglist));
8747 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8748 const char *p = c_getstr (src);
8750 /* If the requested length is zero, or the src parameter string
8751 length is zero, return the dst parameter. */
8752 if (integer_zerop (len) || (p && *p == '\0'))
8753 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
8755 /* If the requested len is greater than or equal to the string
8756 length, call strcat. */
8757 if (TREE_CODE (len) == INTEGER_CST && p
8758 && compare_tree_int (len, strlen (p)) >= 0)
8760 tree newarglist
8761 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
8762 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
8764 /* If the replacement _DECL isn't initialized, don't do the
8765 transformation. */
8766 if (!fn)
8767 return 0;
8769 return build_function_call_expr (fn, newarglist);
8771 return 0;
8775 /* Simplify a call to the strspn builtin.
8777 Return 0 if no simplification was possible, otherwise return the
8778 simplified form of the call as a tree.
8780 The simplified form may be a constant or other expression which
8781 computes the same value, but in a more efficient manner (including
8782 calls to other builtin functions).
8784 The call may contain arguments which need to be evaluated, but
8785 which are not useful to determine the result of the call. In
8786 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8787 COMPOUND_EXPR will be an argument which must be evaluated.
8788 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8789 COMPOUND_EXPR in the chain will contain the tree for the simplified
8790 form of the builtin function call. */
8792 static tree
8793 fold_builtin_strspn (tree arglist)
8795 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8796 return 0;
8797 else
8799 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8800 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8802 /* If both arguments are constants, evaluate at compile-time. */
8803 if (p1 && p2)
8805 const size_t r = strspn (p1, p2);
8806 return size_int (r);
8809 /* If either argument is "", return 0. */
8810 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
8811 /* Evaluate and ignore both arguments in case either one has
8812 side-effects. */
8813 return omit_two_operands (integer_type_node, integer_zero_node,
8814 s1, s2);
8815 return 0;
8819 /* Simplify a call to the strcspn builtin.
8821 Return 0 if no simplification was possible, otherwise return the
8822 simplified form of the call as a tree.
8824 The simplified form may be a constant or other expression which
8825 computes the same value, but in a more efficient manner (including
8826 calls to other builtin functions).
8828 The call may contain arguments which need to be evaluated, but
8829 which are not useful to determine the result of the call. In
8830 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8831 COMPOUND_EXPR will be an argument which must be evaluated.
8832 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8833 COMPOUND_EXPR in the chain will contain the tree for the simplified
8834 form of the builtin function call. */
8836 static tree
8837 fold_builtin_strcspn (tree arglist)
8839 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8840 return 0;
8841 else
8843 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
8844 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
8846 /* If both arguments are constants, evaluate at compile-time. */
8847 if (p1 && p2)
8849 const size_t r = strcspn (p1, p2);
8850 return size_int (r);
8853 /* If the first argument is "", return 0. */
8854 if (p1 && *p1 == '\0')
8856 /* Evaluate and ignore argument s2 in case it has
8857 side-effects. */
8858 return omit_one_operand (integer_type_node,
8859 integer_zero_node, s2);
8862 /* If the second argument is "", return __builtin_strlen(s1). */
8863 if (p2 && *p2 == '\0')
8865 tree newarglist = build_tree_list (NULL_TREE, s1),
8866 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
8868 /* If the replacement _DECL isn't initialized, don't do the
8869 transformation. */
8870 if (!fn)
8871 return 0;
8873 return build_function_call_expr (fn, newarglist);
8875 return 0;
8879 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
8880 by the builtin will be ignored. UNLOCKED is true is true if this
8881 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
8882 the known length of the string. Return NULL_TREE if no simplification
8883 was possible. */
8885 tree
8886 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
8888 tree fn;
8889 tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
8890 : implicit_built_in_decls[BUILT_IN_FPUTC];
8891 tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
8892 : implicit_built_in_decls[BUILT_IN_FWRITE];
8894 /* If the return value is used, or the replacement _DECL isn't
8895 initialized, don't do the transformation. */
8896 if (!ignore || !fn_fputc || !fn_fwrite)
8897 return 0;
8899 /* Verify the arguments in the original call. */
8900 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8901 return 0;
8903 if (! len)
8904 len = c_strlen (TREE_VALUE (arglist), 0);
8906 /* Get the length of the string passed to fputs. If the length
8907 can't be determined, punt. */
8908 if (!len
8909 || TREE_CODE (len) != INTEGER_CST)
8910 return 0;
8912 switch (compare_tree_int (len, 1))
8914 case -1: /* length is 0, delete the call entirely . */
8915 return omit_one_operand (integer_type_node, integer_zero_node,
8916 TREE_VALUE (TREE_CHAIN (arglist)));
8918 case 0: /* length is 1, call fputc. */
8920 const char *p = c_getstr (TREE_VALUE (arglist));
8922 if (p != NULL)
8924 /* New argument list transforming fputs(string, stream) to
8925 fputc(string[0], stream). */
8926 arglist = build_tree_list (NULL_TREE,
8927 TREE_VALUE (TREE_CHAIN (arglist)));
8928 arglist = tree_cons (NULL_TREE,
8929 build_int_cst (NULL_TREE, p[0]),
8930 arglist);
8931 fn = fn_fputc;
8932 break;
8935 /* FALLTHROUGH */
8936 case 1: /* length is greater than 1, call fwrite. */
8938 tree string_arg;
8940 /* If optimizing for size keep fputs. */
8941 if (optimize_size)
8942 return 0;
8943 string_arg = TREE_VALUE (arglist);
8944 /* New argument list transforming fputs(string, stream) to
8945 fwrite(string, 1, len, stream). */
8946 arglist = build_tree_list (NULL_TREE,
8947 TREE_VALUE (TREE_CHAIN (arglist)));
8948 arglist = tree_cons (NULL_TREE, len, arglist);
8949 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
8950 arglist = tree_cons (NULL_TREE, string_arg, arglist);
8951 fn = fn_fwrite;
8952 break;
8954 default:
8955 gcc_unreachable ();
8958 /* These optimizations are only performed when the result is ignored,
8959 hence there's no need to cast the result to integer_type_node. */
8960 return build_function_call_expr (fn, arglist);
8963 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
8964 produced. False otherwise. This is done so that we don't output the error
8965 or warning twice or three times. */
8966 bool
8967 fold_builtin_next_arg (tree arglist)
8969 tree fntype = TREE_TYPE (current_function_decl);
8971 if (TYPE_ARG_TYPES (fntype) == 0
8972 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
8973 == void_type_node))
8975 error ("%<va_start%> used in function with fixed args");
8976 return true;
8978 else if (!arglist)
8980 /* Evidently an out of date version of <stdarg.h>; can't validate
8981 va_start's second argument, but can still work as intended. */
8982 warning ("%<__builtin_next_arg%> called without an argument");
8983 return true;
8985 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
8986 when we checked the arguments and if needed issued a warning. */
8987 else if (!TREE_CHAIN (arglist)
8988 || !integer_zerop (TREE_VALUE (arglist))
8989 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
8990 || TREE_CHAIN (TREE_CHAIN (arglist)))
8992 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
8993 tree arg = TREE_VALUE (arglist);
8995 if (TREE_CHAIN (arglist))
8997 error ("%<va_start%> used with too many arguments");
8998 return true;
9001 /* Strip off all nops for the sake of the comparison. This
9002 is not quite the same as STRIP_NOPS. It does more.
9003 We must also strip off INDIRECT_EXPR for C++ reference
9004 parameters. */
9005 while (TREE_CODE (arg) == NOP_EXPR
9006 || TREE_CODE (arg) == CONVERT_EXPR
9007 || TREE_CODE (arg) == NON_LVALUE_EXPR
9008 || TREE_CODE (arg) == INDIRECT_REF)
9009 arg = TREE_OPERAND (arg, 0);
9010 if (arg != last_parm)
9012 /* FIXME: Sometimes with the tree optimizers we can get the
9013 not the last argument even though the user used the last
9014 argument. We just warn and set the arg to be the last
9015 argument so that we will get wrong-code because of
9016 it. */
9017 warning ("second parameter of %<va_start%> not last named argument");
9019 /* We want to verify the second parameter just once before the tree
9020 optimizers are run and then avoid keeping it in the tree,
9021 as otherwise we could warn even for correct code like:
9022 void foo (int i, ...)
9023 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9024 TREE_VALUE (arglist) = integer_zero_node;
9025 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9027 return false;
9031 /* Simplify a call to the sprintf builtin.
9033 Return 0 if no simplification was possible, otherwise return the
9034 simplified form of the call as a tree. If IGNORED is true, it means that
9035 the caller does not use the returned value of the function. */
9037 static tree
9038 fold_builtin_sprintf (tree arglist, int ignored)
9040 tree call, retval, dest, fmt;
9041 const char *fmt_str = NULL;
9043 /* Verify the required arguments in the original call. We deal with two
9044 types of sprintf() calls: 'sprintf (str, fmt)' and
9045 'sprintf (dest, "%s", orig)'. */
9046 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9047 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9048 VOID_TYPE))
9049 return NULL_TREE;
9051 /* Get the destination string and the format specifier. */
9052 dest = TREE_VALUE (arglist);
9053 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9055 /* Check whether the format is a literal string constant. */
9056 fmt_str = c_getstr (fmt);
9057 if (fmt_str == NULL)
9058 return NULL_TREE;
9060 call = NULL_TREE;
9061 retval = NULL_TREE;
9063 /* If the format doesn't contain % args or %%, use strcpy. */
9064 if (strchr (fmt_str, '%') == NULL)
9066 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9068 if (!fn)
9069 return NULL_TREE;
9071 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9072 'format' is known to contain no % formats. */
9073 arglist = build_tree_list (NULL_TREE, fmt);
9074 arglist = tree_cons (NULL_TREE, dest, arglist);
9075 call = build_function_call_expr (fn, arglist);
9076 if (!ignored)
9077 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9080 /* If the format is "%s", use strcpy if the result isn't used. */
9081 else if (fmt_str && strcmp (fmt_str, "%s") == 0)
9083 tree fn, orig;
9084 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9086 if (!fn)
9087 return NULL_TREE;
9089 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9090 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9091 arglist = build_tree_list (NULL_TREE, orig);
9092 arglist = tree_cons (NULL_TREE, dest, arglist);
9093 if (!ignored)
9095 retval = c_strlen (orig, 1);
9096 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9097 return NULL_TREE;
9099 call = build_function_call_expr (fn, arglist);
9102 if (call && retval)
9104 retval = convert
9105 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9106 retval);
9107 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
9109 else
9110 return call;