PR libffi/23935
[official-gcc.git] / gcc / builtins.c
blob5bcba14d7c277365ef6a6ff804c5fb219c5cb841
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 #ifndef PAD_VARARGS_DOWN
52 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
53 #endif
55 /* Define the names of the builtin function types and codes. */
56 const char *const built_in_class_names[4]
57 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
59 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
60 const char * built_in_names[(int) END_BUILTINS] =
62 #include "builtins.def"
64 #undef DEF_BUILTIN
66 /* Setup an array of _DECL trees, make sure each element is
67 initialized to NULL_TREE. */
68 tree built_in_decls[(int) END_BUILTINS];
69 /* Declarations used when constructing the builtin implicitly in the compiler.
70 It may be NULL_TREE when this is invalid (for instance runtime is not
71 required to implement the function call in all cases). */
72 tree implicit_built_in_decls[(int) END_BUILTINS];
74 static int get_pointer_alignment (tree, unsigned int);
75 static const char *c_getstr (tree);
76 static rtx c_readstr (const char *, enum machine_mode);
77 static int target_char_cast (tree, char *);
78 static rtx get_memory_rtx (tree, tree);
79 static int apply_args_size (void);
80 static int apply_result_size (void);
81 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
82 static rtx result_vector (int, rtx);
83 #endif
84 static rtx expand_builtin_setjmp (tree, rtx);
85 static void expand_builtin_update_setjmp_buf (rtx);
86 static void expand_builtin_prefetch (tree);
87 static rtx expand_builtin_apply_args (void);
88 static rtx expand_builtin_apply_args_1 (void);
89 static rtx expand_builtin_apply (rtx, rtx, rtx);
90 static void expand_builtin_return (rtx);
91 static enum type_class type_to_class (tree);
92 static rtx expand_builtin_classify_type (tree);
93 static void expand_errno_check (tree, rtx);
94 static rtx expand_builtin_mathfn (tree, rtx, rtx);
95 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
96 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
97 static rtx expand_builtin_sincos (tree);
98 static rtx expand_builtin_int_roundingfn (tree, rtx, rtx);
99 static rtx expand_builtin_args_info (tree);
100 static rtx expand_builtin_next_arg (void);
101 static rtx expand_builtin_va_start (tree);
102 static rtx expand_builtin_va_end (tree);
103 static rtx expand_builtin_va_copy (tree);
104 static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode);
105 static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
106 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
107 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
108 static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
109 static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
110 static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
111 static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
113 static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
114 static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode, tree);
115 static rtx expand_builtin_bcopy (tree);
116 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
117 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
118 static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
120 static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode);
121 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
122 static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree);
123 static rtx expand_builtin_bzero (tree);
124 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode);
127 static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode);
128 static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode);
129 static rtx expand_builtin_alloca (tree, rtx);
130 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
131 static rtx expand_builtin_frame_address (tree, tree);
132 static rtx expand_builtin_fputs (tree, rtx, bool);
133 static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool);
134 static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool);
135 static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode);
136 static tree stabilize_va_list (tree, int);
137 static rtx expand_builtin_expect (tree, rtx);
138 static tree fold_builtin_constant_p (tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (tree);
141 static tree fold_builtin_inf (tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static int validate_arglist (tree, ...);
144 static bool integer_valued_real_p (tree);
145 static tree fold_trunc_transparent_mathfn (tree, tree);
146 static bool readonly_data_expr (tree);
147 static rtx expand_builtin_fabs (tree, rtx, rtx);
148 static rtx expand_builtin_signbit (tree, rtx);
149 static tree fold_builtin_sqrt (tree, tree);
150 static tree fold_builtin_cbrt (tree, tree);
151 static tree fold_builtin_pow (tree, tree, tree);
152 static tree fold_builtin_powi (tree, tree, tree);
153 static tree fold_builtin_sin (tree);
154 static tree fold_builtin_cos (tree, tree, tree);
155 static tree fold_builtin_tan (tree);
156 static tree fold_builtin_atan (tree, tree);
157 static tree fold_builtin_trunc (tree, tree);
158 static tree fold_builtin_floor (tree, tree);
159 static tree fold_builtin_ceil (tree, tree);
160 static tree fold_builtin_round (tree, tree);
161 static tree fold_builtin_int_roundingfn (tree, tree);
162 static tree fold_builtin_bitop (tree, tree);
163 static tree fold_builtin_memory_op (tree, tree, bool, int);
164 static tree fold_builtin_strchr (tree, tree);
165 static tree fold_builtin_memcmp (tree);
166 static tree fold_builtin_strcmp (tree);
167 static tree fold_builtin_strncmp (tree);
168 static tree fold_builtin_signbit (tree, tree);
169 static tree fold_builtin_copysign (tree, tree, tree);
170 static tree fold_builtin_isascii (tree);
171 static tree fold_builtin_toascii (tree);
172 static tree fold_builtin_isdigit (tree);
173 static tree fold_builtin_fabs (tree, tree);
174 static tree fold_builtin_abs (tree, tree);
175 static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code,
176 enum tree_code);
177 static tree fold_builtin_1 (tree, tree, bool);
179 static tree fold_builtin_strpbrk (tree, tree);
180 static tree fold_builtin_strstr (tree, tree);
181 static tree fold_builtin_strrchr (tree, tree);
182 static tree fold_builtin_strcat (tree);
183 static tree fold_builtin_strncat (tree);
184 static tree fold_builtin_strspn (tree);
185 static tree fold_builtin_strcspn (tree);
186 static tree fold_builtin_sprintf (tree, int);
188 static rtx expand_builtin_object_size (tree);
189 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
190 enum built_in_function);
191 static void maybe_emit_chk_warning (tree, enum built_in_function);
192 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
193 static tree fold_builtin_object_size (tree);
194 static tree fold_builtin_strcat_chk (tree, tree);
195 static tree fold_builtin_strncat_chk (tree, tree);
196 static tree fold_builtin_sprintf_chk (tree, enum built_in_function);
197 static tree fold_builtin_printf (tree, tree, bool, enum built_in_function);
198 static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function);
199 static bool init_target_chars (void);
201 static unsigned HOST_WIDE_INT target_newline;
202 static unsigned HOST_WIDE_INT target_percent;
203 static unsigned HOST_WIDE_INT target_c;
204 static unsigned HOST_WIDE_INT target_s;
205 static char target_percent_c[3];
206 static char target_percent_s[3];
207 static char target_percent_s_newline[4];
209 /* Return true if NODE should be considered for inline expansion regardless
210 of the optimization level. This means whenever a function is invoked with
211 its "internal" name, which normally contains the prefix "__builtin". */
213 static bool called_as_built_in (tree node)
215 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
216 if (strncmp (name, "__builtin_", 10) == 0)
217 return true;
218 if (strncmp (name, "__sync_", 7) == 0)
219 return true;
220 return false;
223 /* Return the alignment in bits of EXP, a pointer valued expression.
224 But don't return more than MAX_ALIGN no matter what.
225 The alignment returned is, by default, the alignment of the thing that
226 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
228 Otherwise, look at the expression to see if we can do better, i.e., if the
229 expression is actually pointing at an object whose alignment is tighter. */
231 static int
232 get_pointer_alignment (tree exp, unsigned int max_align)
234 unsigned int align, inner;
236 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
237 return 0;
239 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
240 align = MIN (align, max_align);
242 while (1)
244 switch (TREE_CODE (exp))
246 case NOP_EXPR:
247 case CONVERT_EXPR:
248 case NON_LVALUE_EXPR:
249 exp = TREE_OPERAND (exp, 0);
250 if (! POINTER_TYPE_P (TREE_TYPE (exp)))
251 return align;
253 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
254 align = MIN (inner, max_align);
255 break;
257 case PLUS_EXPR:
258 /* If sum of pointer + int, restrict our maximum alignment to that
259 imposed by the integer. If not, we can't do any better than
260 ALIGN. */
261 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
262 return align;
264 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1))
265 & (max_align / BITS_PER_UNIT - 1))
266 != 0)
267 max_align >>= 1;
269 exp = TREE_OPERAND (exp, 0);
270 break;
272 case ADDR_EXPR:
273 /* See what we are pointing at and look at its alignment. */
274 exp = TREE_OPERAND (exp, 0);
275 inner = max_align;
276 if (handled_component_p (exp))
278 HOST_WIDE_INT bitsize, bitpos;
279 tree offset;
280 enum machine_mode mode;
281 int unsignedp, volatilep;
283 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
284 &mode, &unsignedp, &volatilep, true);
285 if (bitpos)
286 inner = MIN (inner, (unsigned) (bitpos & -bitpos));
287 if (offset && TREE_CODE (offset) == PLUS_EXPR
288 && host_integerp (TREE_OPERAND (offset, 1), 1))
290 /* Any overflow in calculating offset_bits won't change
291 the alignment. */
292 unsigned offset_bits
293 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
294 * BITS_PER_UNIT);
296 if (offset_bits)
297 inner = MIN (inner, (offset_bits & -offset_bits));
298 offset = TREE_OPERAND (offset, 0);
300 if (offset && TREE_CODE (offset) == MULT_EXPR
301 && host_integerp (TREE_OPERAND (offset, 1), 1))
303 /* Any overflow in calculating offset_factor won't change
304 the alignment. */
305 unsigned offset_factor
306 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
307 * BITS_PER_UNIT);
309 if (offset_factor)
310 inner = MIN (inner, (offset_factor & -offset_factor));
312 else if (offset)
313 inner = MIN (inner, BITS_PER_UNIT);
315 if (TREE_CODE (exp) == FUNCTION_DECL)
316 align = FUNCTION_BOUNDARY;
317 else if (DECL_P (exp))
318 align = MIN (inner, DECL_ALIGN (exp));
319 #ifdef CONSTANT_ALIGNMENT
320 else if (CONSTANT_CLASS_P (exp))
321 align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align));
322 #endif
323 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR
324 || TREE_CODE (exp) == INDIRECT_REF)
325 align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner);
326 else
327 align = MIN (align, inner);
328 return MIN (align, max_align);
330 default:
331 return align;
336 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
337 way, because it could contain a zero byte in the middle.
338 TREE_STRING_LENGTH is the size of the character array, not the string.
340 ONLY_VALUE should be nonzero if the result is not going to be emitted
341 into the instruction stream and zero if it is going to be expanded.
342 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
343 is returned, otherwise NULL, since
344 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
345 evaluate the side-effects.
347 The value returned is of type `ssizetype'.
349 Unfortunately, string_constant can't access the values of const char
350 arrays with initializers, so neither can we do so here. */
352 tree
353 c_strlen (tree src, int only_value)
355 tree offset_node;
356 HOST_WIDE_INT offset;
357 int max;
358 const char *ptr;
360 STRIP_NOPS (src);
361 if (TREE_CODE (src) == COND_EXPR
362 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
364 tree len1, len2;
366 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
367 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
368 if (tree_int_cst_equal (len1, len2))
369 return len1;
372 if (TREE_CODE (src) == COMPOUND_EXPR
373 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
374 return c_strlen (TREE_OPERAND (src, 1), only_value);
376 src = string_constant (src, &offset_node);
377 if (src == 0)
378 return 0;
380 max = TREE_STRING_LENGTH (src) - 1;
381 ptr = TREE_STRING_POINTER (src);
383 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
385 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
386 compute the offset to the following null if we don't know where to
387 start searching for it. */
388 int i;
390 for (i = 0; i < max; i++)
391 if (ptr[i] == 0)
392 return 0;
394 /* We don't know the starting offset, but we do know that the string
395 has no internal zero bytes. We can assume that the offset falls
396 within the bounds of the string; otherwise, the programmer deserves
397 what he gets. Subtract the offset from the length of the string,
398 and return that. This would perhaps not be valid if we were dealing
399 with named arrays in addition to literal string constants. */
401 return size_diffop (size_int (max), offset_node);
404 /* We have a known offset into the string. Start searching there for
405 a null character if we can represent it as a single HOST_WIDE_INT. */
406 if (offset_node == 0)
407 offset = 0;
408 else if (! host_integerp (offset_node, 0))
409 offset = -1;
410 else
411 offset = tree_low_cst (offset_node, 0);
413 /* If the offset is known to be out of bounds, warn, and call strlen at
414 runtime. */
415 if (offset < 0 || offset > max)
417 warning (0, "offset outside bounds of constant string");
418 return 0;
421 /* Use strlen to search for the first zero byte. Since any strings
422 constructed with build_string will have nulls appended, we win even
423 if we get handed something like (char[4])"abcd".
425 Since OFFSET is our starting index into the string, no further
426 calculation is needed. */
427 return ssize_int (strlen (ptr + offset));
430 /* Return a char pointer for a C string if it is a string constant
431 or sum of string constant and integer constant. */
433 static const char *
434 c_getstr (tree src)
436 tree offset_node;
438 src = string_constant (src, &offset_node);
439 if (src == 0)
440 return 0;
442 if (offset_node == 0)
443 return TREE_STRING_POINTER (src);
444 else if (!host_integerp (offset_node, 1)
445 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
446 return 0;
448 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
451 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
452 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
454 static rtx
455 c_readstr (const char *str, enum machine_mode mode)
457 HOST_WIDE_INT c[2];
458 HOST_WIDE_INT ch;
459 unsigned int i, j;
461 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
463 c[0] = 0;
464 c[1] = 0;
465 ch = 1;
466 for (i = 0; i < GET_MODE_SIZE (mode); i++)
468 j = i;
469 if (WORDS_BIG_ENDIAN)
470 j = GET_MODE_SIZE (mode) - i - 1;
471 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
472 && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
473 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
474 j *= BITS_PER_UNIT;
475 gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT);
477 if (ch)
478 ch = (unsigned char) str[i];
479 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
481 return immed_double_const (c[0], c[1], mode);
484 /* Cast a target constant CST to target CHAR and if that value fits into
485 host char type, return zero and put that value into variable pointed to by
486 P. */
488 static int
489 target_char_cast (tree cst, char *p)
491 unsigned HOST_WIDE_INT val, hostval;
493 if (!host_integerp (cst, 1)
494 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
495 return 1;
497 val = tree_low_cst (cst, 1);
498 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
499 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
501 hostval = val;
502 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
503 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
505 if (val != hostval)
506 return 1;
508 *p = hostval;
509 return 0;
512 /* Similar to save_expr, but assumes that arbitrary code is not executed
513 in between the multiple evaluations. In particular, we assume that a
514 non-addressable local variable will not be modified. */
516 static tree
517 builtin_save_expr (tree exp)
519 if (TREE_ADDRESSABLE (exp) == 0
520 && (TREE_CODE (exp) == PARM_DECL
521 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))
522 return exp;
524 return save_expr (exp);
527 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
528 times to get the address of either a higher stack frame, or a return
529 address located within it (depending on FNDECL_CODE). */
531 static rtx
532 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
534 int i;
536 #ifdef INITIAL_FRAME_ADDRESS_RTX
537 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
538 #else
539 rtx tem;
541 /* For a zero count with __builtin_return_address, we don't care what
542 frame address we return, because target-specific definitions will
543 override us. Therefore frame pointer elimination is OK, and using
544 the soft frame pointer is OK.
546 For a non-zero count, or a zero count with __builtin_frame_address,
547 we require a stable offset from the current frame pointer to the
548 previous one, so we must use the hard frame pointer, and
549 we must disable frame pointer elimination. */
550 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
551 tem = frame_pointer_rtx;
552 else
554 tem = hard_frame_pointer_rtx;
556 /* Tell reload not to eliminate the frame pointer. */
557 current_function_accesses_prior_frames = 1;
559 #endif
561 /* Some machines need special handling before we can access
562 arbitrary frames. For example, on the sparc, we must first flush
563 all register windows to the stack. */
564 #ifdef SETUP_FRAME_ADDRESSES
565 if (count > 0)
566 SETUP_FRAME_ADDRESSES ();
567 #endif
569 /* On the sparc, the return address is not in the frame, it is in a
570 register. There is no way to access it off of the current frame
571 pointer, but it can be accessed off the previous frame pointer by
572 reading the value from the register window save area. */
573 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
574 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
575 count--;
576 #endif
578 /* Scan back COUNT frames to the specified frame. */
579 for (i = 0; i < count; i++)
581 /* Assume the dynamic chain pointer is in the word that the
582 frame address points to, unless otherwise specified. */
583 #ifdef DYNAMIC_CHAIN_ADDRESS
584 tem = DYNAMIC_CHAIN_ADDRESS (tem);
585 #endif
586 tem = memory_address (Pmode, tem);
587 tem = gen_frame_mem (Pmode, tem);
588 tem = copy_to_reg (tem);
591 /* For __builtin_frame_address, return what we've got. */
592 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
593 return tem;
595 /* For __builtin_return_address, Get the return address from that
596 frame. */
597 #ifdef RETURN_ADDR_RTX
598 tem = RETURN_ADDR_RTX (count, tem);
599 #else
600 tem = memory_address (Pmode,
601 plus_constant (tem, GET_MODE_SIZE (Pmode)));
602 tem = gen_frame_mem (Pmode, tem);
603 #endif
604 return tem;
607 /* Alias set used for setjmp buffer. */
608 static HOST_WIDE_INT setjmp_alias_set = -1;
610 /* Construct the leading half of a __builtin_setjmp call. Control will
611 return to RECEIVER_LABEL. This is used directly by sjlj exception
612 handling code. */
614 void
615 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
617 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
618 rtx stack_save;
619 rtx mem;
621 if (setjmp_alias_set == -1)
622 setjmp_alias_set = new_alias_set ();
624 buf_addr = convert_memory_address (Pmode, buf_addr);
626 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
628 /* We store the frame pointer and the address of receiver_label in
629 the buffer and use the rest of it for the stack save area, which
630 is machine-dependent. */
632 mem = gen_rtx_MEM (Pmode, buf_addr);
633 set_mem_alias_set (mem, setjmp_alias_set);
634 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
636 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
637 set_mem_alias_set (mem, setjmp_alias_set);
639 emit_move_insn (validize_mem (mem),
640 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
642 stack_save = gen_rtx_MEM (sa_mode,
643 plus_constant (buf_addr,
644 2 * GET_MODE_SIZE (Pmode)));
645 set_mem_alias_set (stack_save, setjmp_alias_set);
646 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
648 /* If there is further processing to do, do it. */
649 #ifdef HAVE_builtin_setjmp_setup
650 if (HAVE_builtin_setjmp_setup)
651 emit_insn (gen_builtin_setjmp_setup (buf_addr));
652 #endif
654 /* Tell optimize_save_area_alloca that extra work is going to
655 need to go on during alloca. */
656 current_function_calls_setjmp = 1;
658 /* Set this so all the registers get saved in our frame; we need to be
659 able to copy the saved values for any registers from frames we unwind. */
660 current_function_has_nonlocal_label = 1;
663 /* Construct the trailing part of a __builtin_setjmp call.
664 This is used directly by sjlj exception handling code. */
666 void
667 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
669 /* Clobber the FP when we get here, so we have to make sure it's
670 marked as used by this function. */
671 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
673 /* Mark the static chain as clobbered here so life information
674 doesn't get messed up for it. */
675 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
677 /* Now put in the code to restore the frame pointer, and argument
678 pointer, if needed. */
679 #ifdef HAVE_nonlocal_goto
680 if (! HAVE_nonlocal_goto)
681 #endif
682 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
684 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
685 if (fixed_regs[ARG_POINTER_REGNUM])
687 #ifdef ELIMINABLE_REGS
688 size_t i;
689 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
691 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
692 if (elim_regs[i].from == ARG_POINTER_REGNUM
693 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
694 break;
696 if (i == ARRAY_SIZE (elim_regs))
697 #endif
699 /* Now restore our arg pointer from the address at which it
700 was saved in our stack frame. */
701 emit_move_insn (virtual_incoming_args_rtx,
702 copy_to_reg (get_arg_pointer_save_area (cfun)));
705 #endif
707 #ifdef HAVE_builtin_setjmp_receiver
708 if (HAVE_builtin_setjmp_receiver)
709 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
710 else
711 #endif
712 #ifdef HAVE_nonlocal_goto_receiver
713 if (HAVE_nonlocal_goto_receiver)
714 emit_insn (gen_nonlocal_goto_receiver ());
715 else
716 #endif
717 { /* Nothing */ }
719 /* @@@ This is a kludge. Not all machine descriptions define a blockage
720 insn, but we must not allow the code we just generated to be reordered
721 by scheduling. Specifically, the update of the frame pointer must
722 happen immediately, not later. So emit an ASM_INPUT to act as blockage
723 insn. */
724 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
727 /* __builtin_setjmp is passed a pointer to an array of five words (not
728 all will be used on all machines). It operates similarly to the C
729 library function of the same name, but is more efficient. Much of
730 the code below (and for longjmp) is copied from the handling of
731 non-local gotos.
733 NOTE: This is intended for use by GNAT and the exception handling
734 scheme in the compiler and will only work in the method used by
735 them. */
737 static rtx
738 expand_builtin_setjmp (tree arglist, rtx target)
740 rtx buf_addr, next_lab, cont_lab;
742 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
743 return NULL_RTX;
745 if (target == 0 || !REG_P (target)
746 || REGNO (target) < FIRST_PSEUDO_REGISTER)
747 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
749 buf_addr = expand_normal (TREE_VALUE (arglist));
751 next_lab = gen_label_rtx ();
752 cont_lab = gen_label_rtx ();
754 expand_builtin_setjmp_setup (buf_addr, next_lab);
756 /* Set TARGET to zero and branch to the continue label. Use emit_jump to
757 ensure that pending stack adjustments are flushed. */
758 emit_move_insn (target, const0_rtx);
759 emit_jump (cont_lab);
761 emit_label (next_lab);
763 /* Because setjmp and longjmp are not represented in the CFG, a cfgcleanup
764 may find that the basic block starting with NEXT_LAB is unreachable.
765 The whole block, along with NEXT_LAB, would be removed (see PR26983).
766 Make sure that never happens. */
767 LABEL_PRESERVE_P (next_lab) = 1;
769 expand_builtin_setjmp_receiver (next_lab);
771 /* Set TARGET to one. */
772 emit_move_insn (target, const1_rtx);
773 emit_label (cont_lab);
775 /* Tell flow about the strange goings on. Putting `next_lab' on
776 `nonlocal_goto_handler_labels' to indicates that function
777 calls may traverse the arc back to this label. */
779 current_function_has_nonlocal_label = 1;
780 nonlocal_goto_handler_labels
781 = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels);
783 return target;
786 /* __builtin_longjmp is passed a pointer to an array of five words (not
787 all will be used on all machines). It operates similarly to the C
788 library function of the same name, but is more efficient. Much of
789 the code below is copied from the handling of non-local gotos.
791 NOTE: This is intended for use by GNAT and the exception handling
792 scheme in the compiler and will only work in the method used by
793 them. */
795 static void
796 expand_builtin_longjmp (rtx buf_addr, rtx value)
798 rtx fp, lab, stack, insn, last;
799 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
801 if (setjmp_alias_set == -1)
802 setjmp_alias_set = new_alias_set ();
804 buf_addr = convert_memory_address (Pmode, buf_addr);
806 buf_addr = force_reg (Pmode, buf_addr);
808 /* We used to store value in static_chain_rtx, but that fails if pointers
809 are smaller than integers. We instead require that the user must pass
810 a second argument of 1, because that is what builtin_setjmp will
811 return. This also makes EH slightly more efficient, since we are no
812 longer copying around a value that we don't care about. */
813 gcc_assert (value == const1_rtx);
815 last = get_last_insn ();
816 #ifdef HAVE_builtin_longjmp
817 if (HAVE_builtin_longjmp)
818 emit_insn (gen_builtin_longjmp (buf_addr));
819 else
820 #endif
822 fp = gen_rtx_MEM (Pmode, buf_addr);
823 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
824 GET_MODE_SIZE (Pmode)));
826 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
827 2 * GET_MODE_SIZE (Pmode)));
828 set_mem_alias_set (fp, setjmp_alias_set);
829 set_mem_alias_set (lab, setjmp_alias_set);
830 set_mem_alias_set (stack, setjmp_alias_set);
832 /* Pick up FP, label, and SP from the block and jump. This code is
833 from expand_goto in stmt.c; see there for detailed comments. */
834 #ifdef HAVE_nonlocal_goto
835 if (HAVE_nonlocal_goto)
836 /* We have to pass a value to the nonlocal_goto pattern that will
837 get copied into the static_chain pointer, but it does not matter
838 what that value is, because builtin_setjmp does not use it. */
839 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
840 else
841 #endif
843 lab = copy_to_reg (lab);
845 emit_insn (gen_rtx_CLOBBER (VOIDmode,
846 gen_rtx_MEM (BLKmode,
847 gen_rtx_SCRATCH (VOIDmode))));
848 emit_insn (gen_rtx_CLOBBER (VOIDmode,
849 gen_rtx_MEM (BLKmode,
850 hard_frame_pointer_rtx)));
852 emit_move_insn (hard_frame_pointer_rtx, fp);
853 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
855 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
856 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
857 emit_indirect_jump (lab);
861 /* Search backwards and mark the jump insn as a non-local goto.
862 Note that this precludes the use of __builtin_longjmp to a
863 __builtin_setjmp target in the same function. However, we've
864 already cautioned the user that these functions are for
865 internal exception handling use only. */
866 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
868 gcc_assert (insn != last);
870 if (JUMP_P (insn))
872 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx,
873 REG_NOTES (insn));
874 break;
876 else if (CALL_P (insn))
877 break;
881 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
882 and the address of the save area. */
884 static rtx
885 expand_builtin_nonlocal_goto (tree arglist)
887 tree t_label, t_save_area;
888 rtx r_label, r_save_area, r_fp, r_sp, insn;
890 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
891 return NULL_RTX;
893 t_label = TREE_VALUE (arglist);
894 arglist = TREE_CHAIN (arglist);
895 t_save_area = TREE_VALUE (arglist);
897 r_label = expand_normal (t_label);
898 r_label = convert_memory_address (Pmode, r_label);
899 r_save_area = expand_normal (t_save_area);
900 r_save_area = convert_memory_address (Pmode, r_save_area);
901 r_fp = gen_rtx_MEM (Pmode, r_save_area);
902 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
903 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
905 current_function_has_nonlocal_goto = 1;
907 #ifdef HAVE_nonlocal_goto
908 /* ??? We no longer need to pass the static chain value, afaik. */
909 if (HAVE_nonlocal_goto)
910 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
911 else
912 #endif
914 r_label = copy_to_reg (r_label);
916 emit_insn (gen_rtx_CLOBBER (VOIDmode,
917 gen_rtx_MEM (BLKmode,
918 gen_rtx_SCRATCH (VOIDmode))));
920 emit_insn (gen_rtx_CLOBBER (VOIDmode,
921 gen_rtx_MEM (BLKmode,
922 hard_frame_pointer_rtx)));
924 /* Restore frame pointer for containing function.
925 This sets the actual hard register used for the frame pointer
926 to the location of the function's incoming static chain info.
927 The non-local goto handler will then adjust it to contain the
928 proper value and reload the argument pointer, if needed. */
929 emit_move_insn (hard_frame_pointer_rtx, r_fp);
930 emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX);
932 /* USE of hard_frame_pointer_rtx added for consistency;
933 not clear if really needed. */
934 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
935 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
936 emit_indirect_jump (r_label);
939 /* Search backwards to the jump insn and mark it as a
940 non-local goto. */
941 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
943 if (JUMP_P (insn))
945 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
946 const0_rtx, REG_NOTES (insn));
947 break;
949 else if (CALL_P (insn))
950 break;
953 return const0_rtx;
956 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
957 (not all will be used on all machines) that was passed to __builtin_setjmp.
958 It updates the stack pointer in that block to correspond to the current
959 stack pointer. */
961 static void
962 expand_builtin_update_setjmp_buf (rtx buf_addr)
964 enum machine_mode sa_mode = Pmode;
965 rtx stack_save;
968 #ifdef HAVE_save_stack_nonlocal
969 if (HAVE_save_stack_nonlocal)
970 sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
971 #endif
972 #ifdef STACK_SAVEAREA_MODE
973 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
974 #endif
976 stack_save
977 = gen_rtx_MEM (sa_mode,
978 memory_address
979 (sa_mode,
980 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
982 #ifdef HAVE_setjmp
983 if (HAVE_setjmp)
984 emit_insn (gen_setjmp ());
985 #endif
987 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
990 /* Expand a call to __builtin_prefetch. For a target that does not support
991 data prefetch, evaluate the memory address argument in case it has side
992 effects. */
994 static void
995 expand_builtin_prefetch (tree arglist)
997 tree arg0, arg1, arg2;
998 rtx op0, op1, op2;
1000 if (!validate_arglist (arglist, POINTER_TYPE, 0))
1001 return;
1003 arg0 = TREE_VALUE (arglist);
1004 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1005 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1006 locality). */
1007 if (TREE_CHAIN (arglist))
1009 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
1010 if (TREE_CHAIN (TREE_CHAIN (arglist)))
1011 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1012 else
1013 arg2 = build_int_cst (NULL_TREE, 3);
1015 else
1017 arg1 = integer_zero_node;
1018 arg2 = build_int_cst (NULL_TREE, 3);
1021 /* Argument 0 is an address. */
1022 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1024 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1025 if (TREE_CODE (arg1) != INTEGER_CST)
1027 error ("second argument to %<__builtin_prefetch%> must be a constant");
1028 arg1 = integer_zero_node;
1030 op1 = expand_normal (arg1);
1031 /* Argument 1 must be either zero or one. */
1032 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1034 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1035 " using zero");
1036 op1 = const0_rtx;
1039 /* Argument 2 (locality) must be a compile-time constant int. */
1040 if (TREE_CODE (arg2) != INTEGER_CST)
1042 error ("third argument to %<__builtin_prefetch%> must be a constant");
1043 arg2 = integer_zero_node;
1045 op2 = expand_normal (arg2);
1046 /* Argument 2 must be 0, 1, 2, or 3. */
1047 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1049 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1050 op2 = const0_rtx;
1053 #ifdef HAVE_prefetch
1054 if (HAVE_prefetch)
1056 if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate)
1057 (op0,
1058 insn_data[(int) CODE_FOR_prefetch].operand[0].mode))
1059 || (GET_MODE (op0) != Pmode))
1061 op0 = convert_memory_address (Pmode, op0);
1062 op0 = force_reg (Pmode, op0);
1064 emit_insn (gen_prefetch (op0, op1, op2));
1066 #endif
1068 /* Don't do anything with direct references to volatile memory, but
1069 generate code to handle other side effects. */
1070 if (!MEM_P (op0) && side_effects_p (op0))
1071 emit_insn (op0);
1074 /* Get a MEM rtx for expression EXP which is the address of an operand
1075 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1076 the maximum length of the block of memory that might be accessed or
1077 NULL if unknown. */
1079 static rtx
1080 get_memory_rtx (tree exp, tree len)
1082 rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1083 rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1085 /* Get an expression we can use to find the attributes to assign to MEM.
1086 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1087 we can. First remove any nops. */
1088 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
1089 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1090 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1091 exp = TREE_OPERAND (exp, 0);
1093 if (TREE_CODE (exp) == ADDR_EXPR)
1094 exp = TREE_OPERAND (exp, 0);
1095 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1096 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1097 else
1098 exp = NULL;
1100 /* Honor attributes derived from exp, except for the alias set
1101 (as builtin stringops may alias with anything) and the size
1102 (as stringops may access multiple array elements). */
1103 if (exp)
1105 set_mem_attributes (mem, exp, 0);
1107 /* Allow the string and memory builtins to overflow from one
1108 field into another, see http://gcc.gnu.org/PR23561.
1109 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1110 memory accessed by the string or memory builtin will fit
1111 within the field. */
1112 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1114 tree mem_expr = MEM_EXPR (mem);
1115 HOST_WIDE_INT offset = -1, length = -1;
1116 tree inner = exp;
1118 while (TREE_CODE (inner) == ARRAY_REF
1119 || TREE_CODE (inner) == NOP_EXPR
1120 || TREE_CODE (inner) == CONVERT_EXPR
1121 || TREE_CODE (inner) == NON_LVALUE_EXPR
1122 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1123 || TREE_CODE (inner) == SAVE_EXPR)
1124 inner = TREE_OPERAND (inner, 0);
1126 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1128 if (MEM_OFFSET (mem)
1129 && GET_CODE (MEM_OFFSET (mem)) == CONST_INT)
1130 offset = INTVAL (MEM_OFFSET (mem));
1132 if (offset >= 0 && len && host_integerp (len, 0))
1133 length = tree_low_cst (len, 0);
1135 while (TREE_CODE (inner) == COMPONENT_REF)
1137 tree field = TREE_OPERAND (inner, 1);
1138 gcc_assert (! DECL_BIT_FIELD (field));
1139 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1140 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1142 if (length >= 0
1143 && TYPE_SIZE_UNIT (TREE_TYPE (inner))
1144 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0))
1146 HOST_WIDE_INT size
1147 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0);
1148 /* If we can prove the memory starting at XEXP (mem, 0)
1149 and ending at XEXP (mem, 0) + LENGTH will fit into
1150 this field, we can keep that COMPONENT_REF in MEM_EXPR. */
1151 if (offset <= size
1152 && length <= size
1153 && offset + length <= size)
1154 break;
1157 if (offset >= 0
1158 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1159 offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0)
1160 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1161 / BITS_PER_UNIT;
1162 else
1164 offset = -1;
1165 length = -1;
1168 mem_expr = TREE_OPERAND (mem_expr, 0);
1169 inner = TREE_OPERAND (inner, 0);
1172 if (mem_expr == NULL)
1173 offset = -1;
1174 if (mem_expr != MEM_EXPR (mem))
1176 set_mem_expr (mem, mem_expr);
1177 set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX);
1180 set_mem_alias_set (mem, 0);
1181 set_mem_size (mem, NULL_RTX);
1184 return mem;
1187 /* Built-in functions to perform an untyped call and return. */
1189 /* For each register that may be used for calling a function, this
1190 gives a mode used to copy the register's value. VOIDmode indicates
1191 the register is not used for calling a function. If the machine
1192 has register windows, this gives only the outbound registers.
1193 INCOMING_REGNO gives the corresponding inbound register. */
1194 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
1196 /* For each register that may be used for returning values, this gives
1197 a mode used to copy the register's value. VOIDmode indicates the
1198 register is not used for returning values. If the machine has
1199 register windows, this gives only the outbound registers.
1200 INCOMING_REGNO gives the corresponding inbound register. */
1201 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
1203 /* For each register that may be used for calling a function, this
1204 gives the offset of that register into the block returned by
1205 __builtin_apply_args. 0 indicates that the register is not
1206 used for calling a function. */
1207 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
1209 /* Return the size required for the block returned by __builtin_apply_args,
1210 and initialize apply_args_mode. */
1212 static int
1213 apply_args_size (void)
1215 static int size = -1;
1216 int align;
1217 unsigned int regno;
1218 enum machine_mode mode;
1220 /* The values computed by this function never change. */
1221 if (size < 0)
1223 /* The first value is the incoming arg-pointer. */
1224 size = GET_MODE_SIZE (Pmode);
1226 /* The second value is the structure value address unless this is
1227 passed as an "invisible" first argument. */
1228 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1229 size += GET_MODE_SIZE (Pmode);
1231 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1232 if (FUNCTION_ARG_REGNO_P (regno))
1234 mode = reg_raw_mode[regno];
1236 gcc_assert (mode != VOIDmode);
1238 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1239 if (size % align != 0)
1240 size = CEIL (size, align) * align;
1241 apply_args_reg_offset[regno] = size;
1242 size += GET_MODE_SIZE (mode);
1243 apply_args_mode[regno] = mode;
1245 else
1247 apply_args_mode[regno] = VOIDmode;
1248 apply_args_reg_offset[regno] = 0;
1251 return size;
1254 /* Return the size required for the block returned by __builtin_apply,
1255 and initialize apply_result_mode. */
1257 static int
1258 apply_result_size (void)
1260 static int size = -1;
1261 int align, regno;
1262 enum machine_mode mode;
1264 /* The values computed by this function never change. */
1265 if (size < 0)
1267 size = 0;
1269 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1270 if (FUNCTION_VALUE_REGNO_P (regno))
1272 mode = reg_raw_mode[regno];
1274 gcc_assert (mode != VOIDmode);
1276 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1277 if (size % align != 0)
1278 size = CEIL (size, align) * align;
1279 size += GET_MODE_SIZE (mode);
1280 apply_result_mode[regno] = mode;
1282 else
1283 apply_result_mode[regno] = VOIDmode;
1285 /* Allow targets that use untyped_call and untyped_return to override
1286 the size so that machine-specific information can be stored here. */
1287 #ifdef APPLY_RESULT_SIZE
1288 size = APPLY_RESULT_SIZE;
1289 #endif
1291 return size;
1294 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1295 /* Create a vector describing the result block RESULT. If SAVEP is true,
1296 the result block is used to save the values; otherwise it is used to
1297 restore the values. */
1299 static rtx
1300 result_vector (int savep, rtx result)
1302 int regno, size, align, nelts;
1303 enum machine_mode mode;
1304 rtx reg, mem;
1305 rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
1307 size = nelts = 0;
1308 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1309 if ((mode = apply_result_mode[regno]) != VOIDmode)
1311 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1312 if (size % align != 0)
1313 size = CEIL (size, align) * align;
1314 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1315 mem = adjust_address (result, mode, size);
1316 savevec[nelts++] = (savep
1317 ? gen_rtx_SET (VOIDmode, mem, reg)
1318 : gen_rtx_SET (VOIDmode, reg, mem));
1319 size += GET_MODE_SIZE (mode);
1321 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1323 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1325 /* Save the state required to perform an untyped call with the same
1326 arguments as were passed to the current function. */
1328 static rtx
1329 expand_builtin_apply_args_1 (void)
1331 rtx registers, tem;
1332 int size, align, regno;
1333 enum machine_mode mode;
1334 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1336 /* Create a block where the arg-pointer, structure value address,
1337 and argument registers can be saved. */
1338 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1340 /* Walk past the arg-pointer and structure value address. */
1341 size = GET_MODE_SIZE (Pmode);
1342 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1343 size += GET_MODE_SIZE (Pmode);
1345 /* Save each register used in calling a function to the block. */
1346 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1347 if ((mode = apply_args_mode[regno]) != VOIDmode)
1349 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1350 if (size % align != 0)
1351 size = CEIL (size, align) * align;
1353 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1355 emit_move_insn (adjust_address (registers, mode, size), tem);
1356 size += GET_MODE_SIZE (mode);
1359 /* Save the arg pointer to the block. */
1360 tem = copy_to_reg (virtual_incoming_args_rtx);
1361 #ifdef STACK_GROWS_DOWNWARD
1362 /* We need the pointer as the caller actually passed them to us, not
1363 as we might have pretended they were passed. Make sure it's a valid
1364 operand, as emit_move_insn isn't expected to handle a PLUS. */
1366 = force_operand (plus_constant (tem, current_function_pretend_args_size),
1367 NULL_RTX);
1368 #endif
1369 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1371 size = GET_MODE_SIZE (Pmode);
1373 /* Save the structure value address unless this is passed as an
1374 "invisible" first argument. */
1375 if (struct_incoming_value)
1377 emit_move_insn (adjust_address (registers, Pmode, size),
1378 copy_to_reg (struct_incoming_value));
1379 size += GET_MODE_SIZE (Pmode);
1382 /* Return the address of the block. */
1383 return copy_addr_to_reg (XEXP (registers, 0));
1386 /* __builtin_apply_args returns block of memory allocated on
1387 the stack into which is stored the arg pointer, structure
1388 value address, static chain, and all the registers that might
1389 possibly be used in performing a function call. The code is
1390 moved to the start of the function so the incoming values are
1391 saved. */
1393 static rtx
1394 expand_builtin_apply_args (void)
1396 /* Don't do __builtin_apply_args more than once in a function.
1397 Save the result of the first call and reuse it. */
1398 if (apply_args_value != 0)
1399 return apply_args_value;
1401 /* When this function is called, it means that registers must be
1402 saved on entry to this function. So we migrate the
1403 call to the first insn of this function. */
1404 rtx temp;
1405 rtx seq;
1407 start_sequence ();
1408 temp = expand_builtin_apply_args_1 ();
1409 seq = get_insns ();
1410 end_sequence ();
1412 apply_args_value = temp;
1414 /* Put the insns after the NOTE that starts the function.
1415 If this is inside a start_sequence, make the outer-level insn
1416 chain current, so the code is placed at the start of the
1417 function. */
1418 push_topmost_sequence ();
1419 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1420 pop_topmost_sequence ();
1421 return temp;
1425 /* Perform an untyped call and save the state required to perform an
1426 untyped return of whatever value was returned by the given function. */
1428 static rtx
1429 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1431 int size, align, regno;
1432 enum machine_mode mode;
1433 rtx incoming_args, result, reg, dest, src, call_insn;
1434 rtx old_stack_level = 0;
1435 rtx call_fusage = 0;
1436 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1438 arguments = convert_memory_address (Pmode, arguments);
1440 /* Create a block where the return registers can be saved. */
1441 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1443 /* Fetch the arg pointer from the ARGUMENTS block. */
1444 incoming_args = gen_reg_rtx (Pmode);
1445 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1446 #ifndef STACK_GROWS_DOWNWARD
1447 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1448 incoming_args, 0, OPTAB_LIB_WIDEN);
1449 #endif
1451 /* Push a new argument block and copy the arguments. Do not allow
1452 the (potential) memcpy call below to interfere with our stack
1453 manipulations. */
1454 do_pending_stack_adjust ();
1455 NO_DEFER_POP;
1457 /* Save the stack with nonlocal if available. */
1458 #ifdef HAVE_save_stack_nonlocal
1459 if (HAVE_save_stack_nonlocal)
1460 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
1461 else
1462 #endif
1463 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1465 /* Allocate a block of memory onto the stack and copy the memory
1466 arguments to the outgoing arguments address. */
1467 allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
1468 dest = virtual_outgoing_args_rtx;
1469 #ifndef STACK_GROWS_DOWNWARD
1470 if (GET_CODE (argsize) == CONST_INT)
1471 dest = plus_constant (dest, -INTVAL (argsize));
1472 else
1473 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1474 #endif
1475 dest = gen_rtx_MEM (BLKmode, dest);
1476 set_mem_align (dest, PARM_BOUNDARY);
1477 src = gen_rtx_MEM (BLKmode, incoming_args);
1478 set_mem_align (src, PARM_BOUNDARY);
1479 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1481 /* Refer to the argument block. */
1482 apply_args_size ();
1483 arguments = gen_rtx_MEM (BLKmode, arguments);
1484 set_mem_align (arguments, PARM_BOUNDARY);
1486 /* Walk past the arg-pointer and structure value address. */
1487 size = GET_MODE_SIZE (Pmode);
1488 if (struct_value)
1489 size += GET_MODE_SIZE (Pmode);
1491 /* Restore each of the registers previously saved. Make USE insns
1492 for each of these registers for use in making the call. */
1493 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1494 if ((mode = apply_args_mode[regno]) != VOIDmode)
1496 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1497 if (size % align != 0)
1498 size = CEIL (size, align) * align;
1499 reg = gen_rtx_REG (mode, regno);
1500 emit_move_insn (reg, adjust_address (arguments, mode, size));
1501 use_reg (&call_fusage, reg);
1502 size += GET_MODE_SIZE (mode);
1505 /* Restore the structure value address unless this is passed as an
1506 "invisible" first argument. */
1507 size = GET_MODE_SIZE (Pmode);
1508 if (struct_value)
1510 rtx value = gen_reg_rtx (Pmode);
1511 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1512 emit_move_insn (struct_value, value);
1513 if (REG_P (struct_value))
1514 use_reg (&call_fusage, struct_value);
1515 size += GET_MODE_SIZE (Pmode);
1518 /* All arguments and registers used for the call are set up by now! */
1519 function = prepare_call_address (function, NULL, &call_fusage, 0, 0);
1521 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1522 and we don't want to load it into a register as an optimization,
1523 because prepare_call_address already did it if it should be done. */
1524 if (GET_CODE (function) != SYMBOL_REF)
1525 function = memory_address (FUNCTION_MODE, function);
1527 /* Generate the actual call instruction and save the return value. */
1528 #ifdef HAVE_untyped_call
1529 if (HAVE_untyped_call)
1530 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1531 result, result_vector (1, result)));
1532 else
1533 #endif
1534 #ifdef HAVE_call_value
1535 if (HAVE_call_value)
1537 rtx valreg = 0;
1539 /* Locate the unique return register. It is not possible to
1540 express a call that sets more than one return register using
1541 call_value; use untyped_call for that. In fact, untyped_call
1542 only needs to save the return registers in the given block. */
1543 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1544 if ((mode = apply_result_mode[regno]) != VOIDmode)
1546 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1548 valreg = gen_rtx_REG (mode, regno);
1551 emit_call_insn (GEN_CALL_VALUE (valreg,
1552 gen_rtx_MEM (FUNCTION_MODE, function),
1553 const0_rtx, NULL_RTX, const0_rtx));
1555 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1557 else
1558 #endif
1559 gcc_unreachable ();
1561 /* Find the CALL insn we just emitted, and attach the register usage
1562 information. */
1563 call_insn = last_call_insn ();
1564 add_function_usage_to (call_insn, call_fusage);
1566 /* Restore the stack. */
1567 #ifdef HAVE_save_stack_nonlocal
1568 if (HAVE_save_stack_nonlocal)
1569 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1570 else
1571 #endif
1572 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1574 OK_DEFER_POP;
1576 /* Return the address of the result block. */
1577 result = copy_addr_to_reg (XEXP (result, 0));
1578 return convert_memory_address (ptr_mode, result);
1581 /* Perform an untyped return. */
1583 static void
1584 expand_builtin_return (rtx result)
1586 int size, align, regno;
1587 enum machine_mode mode;
1588 rtx reg;
1589 rtx call_fusage = 0;
1591 result = convert_memory_address (Pmode, result);
1593 apply_result_size ();
1594 result = gen_rtx_MEM (BLKmode, result);
1596 #ifdef HAVE_untyped_return
1597 if (HAVE_untyped_return)
1599 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1600 emit_barrier ();
1601 return;
1603 #endif
1605 /* Restore the return value and note that each value is used. */
1606 size = 0;
1607 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1608 if ((mode = apply_result_mode[regno]) != VOIDmode)
1610 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1611 if (size % align != 0)
1612 size = CEIL (size, align) * align;
1613 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1614 emit_move_insn (reg, adjust_address (result, mode, size));
1616 push_to_sequence (call_fusage);
1617 emit_insn (gen_rtx_USE (VOIDmode, reg));
1618 call_fusage = get_insns ();
1619 end_sequence ();
1620 size += GET_MODE_SIZE (mode);
1623 /* Put the USE insns before the return. */
1624 emit_insn (call_fusage);
1626 /* Return whatever values was restored by jumping directly to the end
1627 of the function. */
1628 expand_naked_return ();
1631 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1633 static enum type_class
1634 type_to_class (tree type)
1636 switch (TREE_CODE (type))
1638 case VOID_TYPE: return void_type_class;
1639 case INTEGER_TYPE: return integer_type_class;
1640 case ENUMERAL_TYPE: return enumeral_type_class;
1641 case BOOLEAN_TYPE: return boolean_type_class;
1642 case POINTER_TYPE: return pointer_type_class;
1643 case REFERENCE_TYPE: return reference_type_class;
1644 case OFFSET_TYPE: return offset_type_class;
1645 case REAL_TYPE: return real_type_class;
1646 case COMPLEX_TYPE: return complex_type_class;
1647 case FUNCTION_TYPE: return function_type_class;
1648 case METHOD_TYPE: return method_type_class;
1649 case RECORD_TYPE: return record_type_class;
1650 case UNION_TYPE:
1651 case QUAL_UNION_TYPE: return union_type_class;
1652 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1653 ? string_type_class : array_type_class);
1654 case LANG_TYPE: return lang_type_class;
1655 default: return no_type_class;
1659 /* Expand a call to __builtin_classify_type with arguments found in
1660 ARGLIST. */
1662 static rtx
1663 expand_builtin_classify_type (tree arglist)
1665 if (arglist != 0)
1666 return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
1667 return GEN_INT (no_type_class);
1670 /* This helper macro, meant to be used in mathfn_built_in below,
1671 determines which among a set of three builtin math functions is
1672 appropriate for a given type mode. The `F' and `L' cases are
1673 automatically generated from the `double' case. */
1674 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1675 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1676 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1677 fcodel = BUILT_IN_MATHFN##L ; break;
1679 /* Return mathematic function equivalent to FN but operating directly
1680 on TYPE, if available. If we can't do the conversion, return zero. */
1681 tree
1682 mathfn_built_in (tree type, enum built_in_function fn)
1684 enum built_in_function fcode, fcodef, fcodel;
1686 switch (fn)
1688 CASE_MATHFN (BUILT_IN_ACOS)
1689 CASE_MATHFN (BUILT_IN_ACOSH)
1690 CASE_MATHFN (BUILT_IN_ASIN)
1691 CASE_MATHFN (BUILT_IN_ASINH)
1692 CASE_MATHFN (BUILT_IN_ATAN)
1693 CASE_MATHFN (BUILT_IN_ATAN2)
1694 CASE_MATHFN (BUILT_IN_ATANH)
1695 CASE_MATHFN (BUILT_IN_CBRT)
1696 CASE_MATHFN (BUILT_IN_CEIL)
1697 CASE_MATHFN (BUILT_IN_COPYSIGN)
1698 CASE_MATHFN (BUILT_IN_COS)
1699 CASE_MATHFN (BUILT_IN_COSH)
1700 CASE_MATHFN (BUILT_IN_DREM)
1701 CASE_MATHFN (BUILT_IN_ERF)
1702 CASE_MATHFN (BUILT_IN_ERFC)
1703 CASE_MATHFN (BUILT_IN_EXP)
1704 CASE_MATHFN (BUILT_IN_EXP10)
1705 CASE_MATHFN (BUILT_IN_EXP2)
1706 CASE_MATHFN (BUILT_IN_EXPM1)
1707 CASE_MATHFN (BUILT_IN_FABS)
1708 CASE_MATHFN (BUILT_IN_FDIM)
1709 CASE_MATHFN (BUILT_IN_FLOOR)
1710 CASE_MATHFN (BUILT_IN_FMA)
1711 CASE_MATHFN (BUILT_IN_FMAX)
1712 CASE_MATHFN (BUILT_IN_FMIN)
1713 CASE_MATHFN (BUILT_IN_FMOD)
1714 CASE_MATHFN (BUILT_IN_FREXP)
1715 CASE_MATHFN (BUILT_IN_GAMMA)
1716 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1717 CASE_MATHFN (BUILT_IN_HYPOT)
1718 CASE_MATHFN (BUILT_IN_ILOGB)
1719 CASE_MATHFN (BUILT_IN_INF)
1720 CASE_MATHFN (BUILT_IN_J0)
1721 CASE_MATHFN (BUILT_IN_J1)
1722 CASE_MATHFN (BUILT_IN_JN)
1723 CASE_MATHFN (BUILT_IN_LCEIL)
1724 CASE_MATHFN (BUILT_IN_LDEXP)
1725 CASE_MATHFN (BUILT_IN_LFLOOR)
1726 CASE_MATHFN (BUILT_IN_LGAMMA)
1727 CASE_MATHFN (BUILT_IN_LLCEIL)
1728 CASE_MATHFN (BUILT_IN_LLFLOOR)
1729 CASE_MATHFN (BUILT_IN_LLRINT)
1730 CASE_MATHFN (BUILT_IN_LLROUND)
1731 CASE_MATHFN (BUILT_IN_LOG)
1732 CASE_MATHFN (BUILT_IN_LOG10)
1733 CASE_MATHFN (BUILT_IN_LOG1P)
1734 CASE_MATHFN (BUILT_IN_LOG2)
1735 CASE_MATHFN (BUILT_IN_LOGB)
1736 CASE_MATHFN (BUILT_IN_LRINT)
1737 CASE_MATHFN (BUILT_IN_LROUND)
1738 CASE_MATHFN (BUILT_IN_MODF)
1739 CASE_MATHFN (BUILT_IN_NAN)
1740 CASE_MATHFN (BUILT_IN_NANS)
1741 CASE_MATHFN (BUILT_IN_NEARBYINT)
1742 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1743 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1744 CASE_MATHFN (BUILT_IN_POW)
1745 CASE_MATHFN (BUILT_IN_POWI)
1746 CASE_MATHFN (BUILT_IN_POW10)
1747 CASE_MATHFN (BUILT_IN_REMAINDER)
1748 CASE_MATHFN (BUILT_IN_REMQUO)
1749 CASE_MATHFN (BUILT_IN_RINT)
1750 CASE_MATHFN (BUILT_IN_ROUND)
1751 CASE_MATHFN (BUILT_IN_SCALB)
1752 CASE_MATHFN (BUILT_IN_SCALBLN)
1753 CASE_MATHFN (BUILT_IN_SCALBN)
1754 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1755 CASE_MATHFN (BUILT_IN_SIN)
1756 CASE_MATHFN (BUILT_IN_SINCOS)
1757 CASE_MATHFN (BUILT_IN_SINH)
1758 CASE_MATHFN (BUILT_IN_SQRT)
1759 CASE_MATHFN (BUILT_IN_TAN)
1760 CASE_MATHFN (BUILT_IN_TANH)
1761 CASE_MATHFN (BUILT_IN_TGAMMA)
1762 CASE_MATHFN (BUILT_IN_TRUNC)
1763 CASE_MATHFN (BUILT_IN_Y0)
1764 CASE_MATHFN (BUILT_IN_Y1)
1765 CASE_MATHFN (BUILT_IN_YN)
1767 default:
1768 return 0;
1771 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1772 return implicit_built_in_decls[fcode];
1773 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1774 return implicit_built_in_decls[fcodef];
1775 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1776 return implicit_built_in_decls[fcodel];
1777 else
1778 return 0;
1781 /* If errno must be maintained, expand the RTL to check if the result,
1782 TARGET, of a built-in function call, EXP, is NaN, and if so set
1783 errno to EDOM. */
1785 static void
1786 expand_errno_check (tree exp, rtx target)
1788 rtx lab = gen_label_rtx ();
1790 /* Test the result; if it is NaN, set errno=EDOM because
1791 the argument was not in the domain. */
1792 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1793 0, lab);
1795 #ifdef TARGET_EDOM
1796 /* If this built-in doesn't throw an exception, set errno directly. */
1797 if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)))
1799 #ifdef GEN_ERRNO_RTX
1800 rtx errno_rtx = GEN_ERRNO_RTX;
1801 #else
1802 rtx errno_rtx
1803 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1804 #endif
1805 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1806 emit_label (lab);
1807 return;
1809 #endif
1811 /* We can't set errno=EDOM directly; let the library call do it.
1812 Pop the arguments right away in case the call gets deleted. */
1813 NO_DEFER_POP;
1814 expand_call (exp, target, 0);
1815 OK_DEFER_POP;
1816 emit_label (lab);
1820 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1821 Return 0 if a normal call should be emitted rather than expanding the
1822 function in-line. EXP is the expression that is a call to the builtin
1823 function; if convenient, the result should be placed in TARGET.
1824 SUBTARGET may be used as the target for computing one of EXP's operands. */
1826 static rtx
1827 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1829 optab builtin_optab;
1830 rtx op0, insns, before_call;
1831 tree fndecl = get_callee_fndecl (exp);
1832 tree arglist = TREE_OPERAND (exp, 1);
1833 enum machine_mode mode;
1834 bool errno_set = false;
1835 tree arg, narg;
1837 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
1838 return 0;
1840 arg = TREE_VALUE (arglist);
1842 switch (DECL_FUNCTION_CODE (fndecl))
1844 CASE_FLT_FN (BUILT_IN_SQRT):
1845 errno_set = ! tree_expr_nonnegative_p (arg);
1846 builtin_optab = sqrt_optab;
1847 break;
1848 CASE_FLT_FN (BUILT_IN_EXP):
1849 errno_set = true; builtin_optab = exp_optab; break;
1850 CASE_FLT_FN (BUILT_IN_EXP10):
1851 CASE_FLT_FN (BUILT_IN_POW10):
1852 errno_set = true; builtin_optab = exp10_optab; break;
1853 CASE_FLT_FN (BUILT_IN_EXP2):
1854 errno_set = true; builtin_optab = exp2_optab; break;
1855 CASE_FLT_FN (BUILT_IN_EXPM1):
1856 errno_set = true; builtin_optab = expm1_optab; break;
1857 CASE_FLT_FN (BUILT_IN_LOGB):
1858 errno_set = true; builtin_optab = logb_optab; break;
1859 CASE_FLT_FN (BUILT_IN_ILOGB):
1860 errno_set = true; builtin_optab = ilogb_optab; break;
1861 CASE_FLT_FN (BUILT_IN_LOG):
1862 errno_set = true; builtin_optab = log_optab; break;
1863 CASE_FLT_FN (BUILT_IN_LOG10):
1864 errno_set = true; builtin_optab = log10_optab; break;
1865 CASE_FLT_FN (BUILT_IN_LOG2):
1866 errno_set = true; builtin_optab = log2_optab; break;
1867 CASE_FLT_FN (BUILT_IN_LOG1P):
1868 errno_set = true; builtin_optab = log1p_optab; break;
1869 CASE_FLT_FN (BUILT_IN_ASIN):
1870 builtin_optab = asin_optab; break;
1871 CASE_FLT_FN (BUILT_IN_ACOS):
1872 builtin_optab = acos_optab; break;
1873 CASE_FLT_FN (BUILT_IN_TAN):
1874 builtin_optab = tan_optab; break;
1875 CASE_FLT_FN (BUILT_IN_ATAN):
1876 builtin_optab = atan_optab; break;
1877 CASE_FLT_FN (BUILT_IN_FLOOR):
1878 builtin_optab = floor_optab; break;
1879 CASE_FLT_FN (BUILT_IN_CEIL):
1880 builtin_optab = ceil_optab; break;
1881 CASE_FLT_FN (BUILT_IN_TRUNC):
1882 builtin_optab = btrunc_optab; break;
1883 CASE_FLT_FN (BUILT_IN_ROUND):
1884 builtin_optab = round_optab; break;
1885 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1886 builtin_optab = nearbyint_optab; break;
1887 CASE_FLT_FN (BUILT_IN_RINT):
1888 builtin_optab = rint_optab; break;
1889 CASE_FLT_FN (BUILT_IN_LRINT):
1890 CASE_FLT_FN (BUILT_IN_LLRINT):
1891 builtin_optab = lrint_optab; break;
1892 default:
1893 gcc_unreachable ();
1896 /* Make a suitable register to place result in. */
1897 mode = TYPE_MODE (TREE_TYPE (exp));
1899 if (! flag_errno_math || ! HONOR_NANS (mode))
1900 errno_set = false;
1902 /* Before working hard, check whether the instruction is available. */
1903 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1905 target = gen_reg_rtx (mode);
1907 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
1908 need to expand the argument again. This way, we will not perform
1909 side-effects more the once. */
1910 narg = builtin_save_expr (arg);
1911 if (narg != arg)
1913 arg = narg;
1914 arglist = build_tree_list (NULL_TREE, arg);
1915 exp = build_function_call_expr (fndecl, arglist);
1918 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
1920 start_sequence ();
1922 /* Compute into TARGET.
1923 Set TARGET to wherever the result comes back. */
1924 target = expand_unop (mode, builtin_optab, op0, target, 0);
1926 if (target != 0)
1928 if (errno_set)
1929 expand_errno_check (exp, target);
1931 /* Output the entire sequence. */
1932 insns = get_insns ();
1933 end_sequence ();
1934 emit_insn (insns);
1935 return target;
1938 /* If we were unable to expand via the builtin, stop the sequence
1939 (without outputting the insns) and call to the library function
1940 with the stabilized argument list. */
1941 end_sequence ();
1944 before_call = get_last_insn ();
1946 target = expand_call (exp, target, target == const0_rtx);
1948 /* If this is a sqrt operation and we don't care about errno, try to
1949 attach a REG_EQUAL note with a SQRT rtx to the emitted libcall.
1950 This allows the semantics of the libcall to be visible to the RTL
1951 optimizers. */
1952 if (builtin_optab == sqrt_optab && !errno_set)
1954 /* Search backwards through the insns emitted by expand_call looking
1955 for the instruction with the REG_RETVAL note. */
1956 rtx last = get_last_insn ();
1957 while (last != before_call)
1959 if (find_reg_note (last, REG_RETVAL, NULL))
1961 rtx note = find_reg_note (last, REG_EQUAL, NULL);
1962 /* Check that the REQ_EQUAL note is an EXPR_LIST with
1963 two elements, i.e. symbol_ref(sqrt) and the operand. */
1964 if (note
1965 && GET_CODE (note) == EXPR_LIST
1966 && GET_CODE (XEXP (note, 0)) == EXPR_LIST
1967 && XEXP (XEXP (note, 0), 1) != NULL_RTX
1968 && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX)
1970 rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0);
1971 /* Check operand is a register with expected mode. */
1972 if (operand
1973 && REG_P (operand)
1974 && GET_MODE (operand) == mode)
1976 /* Replace the REG_EQUAL note with a SQRT rtx. */
1977 rtx equiv = gen_rtx_SQRT (mode, operand);
1978 set_unique_reg_note (last, REG_EQUAL, equiv);
1981 break;
1983 last = PREV_INSN (last);
1987 return target;
1990 /* Expand a call to the builtin binary math functions (pow and atan2).
1991 Return 0 if a normal call should be emitted rather than expanding the
1992 function in-line. EXP is the expression that is a call to the builtin
1993 function; if convenient, the result should be placed in TARGET.
1994 SUBTARGET may be used as the target for computing one of EXP's
1995 operands. */
1997 static rtx
1998 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2000 optab builtin_optab;
2001 rtx op0, op1, insns;
2002 int op1_type = REAL_TYPE;
2003 tree fndecl = get_callee_fndecl (exp);
2004 tree arglist = TREE_OPERAND (exp, 1);
2005 tree arg0, arg1, temp, narg;
2006 enum machine_mode mode;
2007 bool errno_set = true;
2008 bool stable = true;
2010 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP)
2011 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF)
2012 || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL))
2013 op1_type = INTEGER_TYPE;
2015 if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE))
2016 return 0;
2018 arg0 = TREE_VALUE (arglist);
2019 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2021 switch (DECL_FUNCTION_CODE (fndecl))
2023 CASE_FLT_FN (BUILT_IN_POW):
2024 builtin_optab = pow_optab; break;
2025 CASE_FLT_FN (BUILT_IN_ATAN2):
2026 builtin_optab = atan2_optab; break;
2027 CASE_FLT_FN (BUILT_IN_LDEXP):
2028 builtin_optab = ldexp_optab; break;
2029 CASE_FLT_FN (BUILT_IN_FMOD):
2030 builtin_optab = fmod_optab; break;
2031 CASE_FLT_FN (BUILT_IN_DREM):
2032 builtin_optab = drem_optab; break;
2033 default:
2034 gcc_unreachable ();
2037 /* Make a suitable register to place result in. */
2038 mode = TYPE_MODE (TREE_TYPE (exp));
2040 /* Before working hard, check whether the instruction is available. */
2041 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2042 return 0;
2044 target = gen_reg_rtx (mode);
2046 if (! flag_errno_math || ! HONOR_NANS (mode))
2047 errno_set = false;
2049 /* Always stabilize the argument list. */
2050 narg = builtin_save_expr (arg1);
2051 if (narg != arg1)
2053 arg1 = narg;
2054 temp = build_tree_list (NULL_TREE, narg);
2055 stable = false;
2057 else
2058 temp = TREE_CHAIN (arglist);
2060 narg = builtin_save_expr (arg0);
2061 if (narg != arg0)
2063 arg0 = narg;
2064 arglist = tree_cons (NULL_TREE, narg, temp);
2065 stable = false;
2067 else if (! stable)
2068 arglist = tree_cons (NULL_TREE, arg0, temp);
2070 if (! stable)
2071 exp = build_function_call_expr (fndecl, arglist);
2073 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2074 op1 = expand_normal (arg1);
2076 start_sequence ();
2078 /* Compute into TARGET.
2079 Set TARGET to wherever the result comes back. */
2080 target = expand_binop (mode, builtin_optab, op0, op1,
2081 target, 0, OPTAB_DIRECT);
2083 /* If we were unable to expand via the builtin, stop the sequence
2084 (without outputting the insns) and call to the library function
2085 with the stabilized argument list. */
2086 if (target == 0)
2088 end_sequence ();
2089 return expand_call (exp, target, target == const0_rtx);
2092 if (errno_set)
2093 expand_errno_check (exp, target);
2095 /* Output the entire sequence. */
2096 insns = get_insns ();
2097 end_sequence ();
2098 emit_insn (insns);
2100 return target;
2103 /* Expand a call to the builtin sin and cos math functions.
2104 Return 0 if a normal call should be emitted rather than expanding the
2105 function in-line. EXP is the expression that is a call to the builtin
2106 function; if convenient, the result should be placed in TARGET.
2107 SUBTARGET may be used as the target for computing one of EXP's
2108 operands. */
2110 static rtx
2111 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2113 optab builtin_optab;
2114 rtx op0, insns;
2115 tree fndecl = get_callee_fndecl (exp);
2116 tree arglist = TREE_OPERAND (exp, 1);
2117 enum machine_mode mode;
2118 tree arg, narg;
2120 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2121 return 0;
2123 arg = TREE_VALUE (arglist);
2125 switch (DECL_FUNCTION_CODE (fndecl))
2127 CASE_FLT_FN (BUILT_IN_SIN):
2128 CASE_FLT_FN (BUILT_IN_COS):
2129 builtin_optab = sincos_optab; break;
2130 default:
2131 gcc_unreachable ();
2134 /* Make a suitable register to place result in. */
2135 mode = TYPE_MODE (TREE_TYPE (exp));
2137 /* Check if sincos insn is available, otherwise fallback
2138 to sin or cos insn. */
2139 if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) {
2140 switch (DECL_FUNCTION_CODE (fndecl))
2142 CASE_FLT_FN (BUILT_IN_SIN):
2143 builtin_optab = sin_optab; break;
2144 CASE_FLT_FN (BUILT_IN_COS):
2145 builtin_optab = cos_optab; break;
2146 default:
2147 gcc_unreachable ();
2151 /* Before working hard, check whether the instruction is available. */
2152 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2154 target = gen_reg_rtx (mode);
2156 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2157 need to expand the argument again. This way, we will not perform
2158 side-effects more the once. */
2159 narg = save_expr (arg);
2160 if (narg != arg)
2162 arg = narg;
2163 arglist = build_tree_list (NULL_TREE, arg);
2164 exp = build_function_call_expr (fndecl, arglist);
2167 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2169 start_sequence ();
2171 /* Compute into TARGET.
2172 Set TARGET to wherever the result comes back. */
2173 if (builtin_optab == sincos_optab)
2175 int result;
2177 switch (DECL_FUNCTION_CODE (fndecl))
2179 CASE_FLT_FN (BUILT_IN_SIN):
2180 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2181 break;
2182 CASE_FLT_FN (BUILT_IN_COS):
2183 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2184 break;
2185 default:
2186 gcc_unreachable ();
2188 gcc_assert (result);
2190 else
2192 target = expand_unop (mode, builtin_optab, op0, target, 0);
2195 if (target != 0)
2197 /* Output the entire sequence. */
2198 insns = get_insns ();
2199 end_sequence ();
2200 emit_insn (insns);
2201 return target;
2204 /* If we were unable to expand via the builtin, stop the sequence
2205 (without outputting the insns) and call to the library function
2206 with the stabilized argument list. */
2207 end_sequence ();
2210 target = expand_call (exp, target, target == const0_rtx);
2212 return target;
2215 /* Expand a call to the builtin sincos math function.
2216 Return 0 if a normal call should be emitted rather than expanding the
2217 function in-line. EXP is the expression that is a call to the builtin
2218 function. */
2220 static rtx
2221 expand_builtin_sincos (tree exp)
2223 rtx op0, op1, op2, target1, target2;
2224 tree arglist = TREE_OPERAND (exp, 1);
2225 enum machine_mode mode;
2226 tree arg, sinp, cosp;
2227 int result;
2229 if (!validate_arglist (arglist, REAL_TYPE,
2230 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2231 return 0;
2233 arg = TREE_VALUE (arglist);
2234 sinp = TREE_VALUE (TREE_CHAIN (arglist));
2235 cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2237 /* Make a suitable register to place result in. */
2238 mode = TYPE_MODE (TREE_TYPE (arg));
2240 /* Check if sincos insn is available, otherwise emit the call. */
2241 if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
2242 return NULL_RTX;
2244 target1 = gen_reg_rtx (mode);
2245 target2 = gen_reg_rtx (mode);
2247 op0 = expand_normal (arg);
2248 op1 = expand_normal (build_fold_indirect_ref (sinp));
2249 op2 = expand_normal (build_fold_indirect_ref (cosp));
2251 /* Compute into target1 and target2.
2252 Set TARGET to wherever the result comes back. */
2253 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2254 gcc_assert (result);
2256 /* Move target1 and target2 to the memory locations indicated
2257 by op1 and op2. */
2258 emit_move_insn (op1, target1);
2259 emit_move_insn (op2, target2);
2261 return const0_rtx;
2264 /* Expand a call to one of the builtin rounding functions (lfloor).
2265 If expanding via optab fails, lower expression to (int)(floor(x)).
2266 EXP is the expression that is a call to the builtin function;
2267 if convenient, the result should be placed in TARGET. SUBTARGET may
2268 be used as the target for computing one of EXP's operands. */
2270 static rtx
2271 expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget)
2273 optab builtin_optab;
2274 rtx op0, insns, tmp;
2275 tree fndecl = get_callee_fndecl (exp);
2276 tree arglist = TREE_OPERAND (exp, 1);
2277 enum built_in_function fallback_fn;
2278 tree fallback_fndecl;
2279 enum machine_mode mode;
2280 tree arg, narg;
2282 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
2283 gcc_unreachable ();
2285 arg = TREE_VALUE (arglist);
2287 switch (DECL_FUNCTION_CODE (fndecl))
2289 CASE_FLT_FN (BUILT_IN_LCEIL):
2290 CASE_FLT_FN (BUILT_IN_LLCEIL):
2291 builtin_optab = lceil_optab;
2292 fallback_fn = BUILT_IN_CEIL;
2293 break;
2295 CASE_FLT_FN (BUILT_IN_LFLOOR):
2296 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2297 builtin_optab = lfloor_optab;
2298 fallback_fn = BUILT_IN_FLOOR;
2299 break;
2301 default:
2302 gcc_unreachable ();
2305 /* Make a suitable register to place result in. */
2306 mode = TYPE_MODE (TREE_TYPE (exp));
2308 /* Before working hard, check whether the instruction is available. */
2309 if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2311 target = gen_reg_rtx (mode);
2313 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2314 need to expand the argument again. This way, we will not perform
2315 side-effects more the once. */
2316 narg = builtin_save_expr (arg);
2317 if (narg != arg)
2319 arg = narg;
2320 arglist = build_tree_list (NULL_TREE, arg);
2321 exp = build_function_call_expr (fndecl, arglist);
2324 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
2326 start_sequence ();
2328 /* Compute into TARGET.
2329 Set TARGET to wherever the result comes back. */
2330 target = expand_unop (mode, builtin_optab, op0, target, 0);
2332 if (target != 0)
2334 /* Output the entire sequence. */
2335 insns = get_insns ();
2336 end_sequence ();
2337 emit_insn (insns);
2338 return target;
2341 /* If we were unable to expand via the builtin, stop the sequence
2342 (without outputting the insns). */
2343 end_sequence ();
2346 /* Fall back to floating point rounding optab. */
2347 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2348 /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS.
2349 ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */
2350 gcc_assert (fallback_fndecl != NULL_TREE);
2351 exp = build_function_call_expr (fallback_fndecl, arglist);
2353 tmp = expand_normal (exp);
2355 /* Truncate the result of floating point optab to integer
2356 via expand_fix (). */
2357 target = gen_reg_rtx (mode);
2358 expand_fix (target, tmp, 0);
2360 return target;
2363 /* To evaluate powi(x,n), the floating point value x raised to the
2364 constant integer exponent n, we use a hybrid algorithm that
2365 combines the "window method" with look-up tables. For an
2366 introduction to exponentiation algorithms and "addition chains",
2367 see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
2368 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
2369 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
2370 Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */
2372 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
2373 multiplications to inline before calling the system library's pow
2374 function. powi(x,n) requires at worst 2*bits(n)-2 multiplications,
2375 so this default never requires calling pow, powf or powl. */
2377 #ifndef POWI_MAX_MULTS
2378 #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2)
2379 #endif
2381 /* The size of the "optimal power tree" lookup table. All
2382 exponents less than this value are simply looked up in the
2383 powi_table below. This threshold is also used to size the
2384 cache of pseudo registers that hold intermediate results. */
2385 #define POWI_TABLE_SIZE 256
2387 /* The size, in bits of the window, used in the "window method"
2388 exponentiation algorithm. This is equivalent to a radix of
2389 (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */
2390 #define POWI_WINDOW_SIZE 3
2392 /* The following table is an efficient representation of an
2393 "optimal power tree". For each value, i, the corresponding
2394 value, j, in the table states than an optimal evaluation
2395 sequence for calculating pow(x,i) can be found by evaluating
2396 pow(x,j)*pow(x,i-j). An optimal power tree for the first
2397 100 integers is given in Knuth's "Seminumerical algorithms". */
2399 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2401 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2402 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2403 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2404 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2405 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2406 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2407 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2408 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2409 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2410 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2411 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2412 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2413 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2414 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2415 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2416 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2417 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2418 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2419 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2420 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2421 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2422 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2423 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2424 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2425 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2426 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2427 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2428 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2429 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2430 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2431 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2432 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2436 /* Return the number of multiplications required to calculate
2437 powi(x,n) where n is less than POWI_TABLE_SIZE. This is a
2438 subroutine of powi_cost. CACHE is an array indicating
2439 which exponents have already been calculated. */
2441 static int
2442 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
2444 /* If we've already calculated this exponent, then this evaluation
2445 doesn't require any additional multiplications. */
2446 if (cache[n])
2447 return 0;
2449 cache[n] = true;
2450 return powi_lookup_cost (n - powi_table[n], cache)
2451 + powi_lookup_cost (powi_table[n], cache) + 1;
2454 /* Return the number of multiplications required to calculate
2455 powi(x,n) for an arbitrary x, given the exponent N. This
2456 function needs to be kept in sync with expand_powi below. */
2458 static int
2459 powi_cost (HOST_WIDE_INT n)
2461 bool cache[POWI_TABLE_SIZE];
2462 unsigned HOST_WIDE_INT digit;
2463 unsigned HOST_WIDE_INT val;
2464 int result;
2466 if (n == 0)
2467 return 0;
2469 /* Ignore the reciprocal when calculating the cost. */
2470 val = (n < 0) ? -n : n;
2472 /* Initialize the exponent cache. */
2473 memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
2474 cache[1] = true;
2476 result = 0;
2478 while (val >= POWI_TABLE_SIZE)
2480 if (val & 1)
2482 digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
2483 result += powi_lookup_cost (digit, cache)
2484 + POWI_WINDOW_SIZE + 1;
2485 val >>= POWI_WINDOW_SIZE;
2487 else
2489 val >>= 1;
2490 result++;
2494 return result + powi_lookup_cost (val, cache);
2497 /* Recursive subroutine of expand_powi. This function takes the array,
2498 CACHE, of already calculated exponents and an exponent N and returns
2499 an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */
2501 static rtx
2502 expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache)
2504 unsigned HOST_WIDE_INT digit;
2505 rtx target, result;
2506 rtx op0, op1;
2508 if (n < POWI_TABLE_SIZE)
2510 if (cache[n])
2511 return cache[n];
2513 target = gen_reg_rtx (mode);
2514 cache[n] = target;
2516 op0 = expand_powi_1 (mode, n - powi_table[n], cache);
2517 op1 = expand_powi_1 (mode, powi_table[n], cache);
2519 else if (n & 1)
2521 target = gen_reg_rtx (mode);
2522 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2523 op0 = expand_powi_1 (mode, n - digit, cache);
2524 op1 = expand_powi_1 (mode, digit, cache);
2526 else
2528 target = gen_reg_rtx (mode);
2529 op0 = expand_powi_1 (mode, n >> 1, cache);
2530 op1 = op0;
2533 result = expand_mult (mode, op0, op1, target, 0);
2534 if (result != target)
2535 emit_move_insn (target, result);
2536 return target;
2539 /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the
2540 floating point operand in mode MODE, and N is the exponent. This
2541 function needs to be kept in sync with powi_cost above. */
2543 static rtx
2544 expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n)
2546 unsigned HOST_WIDE_INT val;
2547 rtx cache[POWI_TABLE_SIZE];
2548 rtx result;
2550 if (n == 0)
2551 return CONST1_RTX (mode);
2553 val = (n < 0) ? -n : n;
2555 memset (cache, 0, sizeof (cache));
2556 cache[1] = x;
2558 result = expand_powi_1 (mode, (n < 0) ? -n : n, cache);
2560 /* If the original exponent was negative, reciprocate the result. */
2561 if (n < 0)
2562 result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode),
2563 result, NULL_RTX, 0, OPTAB_LIB_WIDEN);
2565 return result;
2568 /* Expand a call to the pow built-in mathematical function. Return 0 if
2569 a normal call should be emitted rather than expanding the function
2570 in-line. EXP is the expression that is a call to the builtin
2571 function; if convenient, the result should be placed in TARGET. */
2573 static rtx
2574 expand_builtin_pow (tree exp, rtx target, rtx subtarget)
2576 tree arglist = TREE_OPERAND (exp, 1);
2577 tree arg0, arg1;
2579 if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2580 return 0;
2582 arg0 = TREE_VALUE (arglist);
2583 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2585 if (TREE_CODE (arg1) == REAL_CST
2586 && ! TREE_CONSTANT_OVERFLOW (arg1))
2588 REAL_VALUE_TYPE cint;
2589 REAL_VALUE_TYPE c;
2590 HOST_WIDE_INT n;
2592 c = TREE_REAL_CST (arg1);
2593 n = real_to_integer (&c);
2594 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
2595 if (real_identical (&c, &cint))
2597 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2598 Otherwise, check the number of multiplications required.
2599 Note that pow never sets errno for an integer exponent. */
2600 if ((n >= -1 && n <= 2)
2601 || (flag_unsafe_math_optimizations
2602 && ! optimize_size
2603 && powi_cost (n) <= POWI_MAX_MULTS))
2605 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2606 rtx op = expand_expr (arg0, subtarget, VOIDmode, 0);
2607 op = force_reg (mode, op);
2608 return expand_powi (op, mode, n);
2613 if (! flag_unsafe_math_optimizations)
2614 return NULL_RTX;
2615 return expand_builtin_mathfn_2 (exp, target, subtarget);
2618 /* Expand a call to the powi built-in mathematical function. Return 0 if
2619 a normal call should be emitted rather than expanding the function
2620 in-line. EXP is the expression that is a call to the builtin
2621 function; if convenient, the result should be placed in TARGET. */
2623 static rtx
2624 expand_builtin_powi (tree exp, rtx target, rtx subtarget)
2626 tree arglist = TREE_OPERAND (exp, 1);
2627 tree arg0, arg1;
2628 rtx op0, op1;
2629 enum machine_mode mode;
2630 enum machine_mode mode2;
2632 if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2633 return 0;
2635 arg0 = TREE_VALUE (arglist);
2636 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
2637 mode = TYPE_MODE (TREE_TYPE (exp));
2639 /* Handle constant power. */
2641 if (TREE_CODE (arg1) == INTEGER_CST
2642 && ! TREE_CONSTANT_OVERFLOW (arg1))
2644 HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1);
2646 /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
2647 Otherwise, check the number of multiplications required. */
2648 if ((TREE_INT_CST_HIGH (arg1) == 0
2649 || TREE_INT_CST_HIGH (arg1) == -1)
2650 && ((n >= -1 && n <= 2)
2651 || (! optimize_size
2652 && powi_cost (n) <= POWI_MAX_MULTS)))
2654 op0 = expand_expr (arg0, subtarget, VOIDmode, 0);
2655 op0 = force_reg (mode, op0);
2656 return expand_powi (op0, mode, n);
2660 /* Emit a libcall to libgcc. */
2662 /* Mode of the 2nd argument must match that of an int. */
2663 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2665 if (target == NULL_RTX)
2666 target = gen_reg_rtx (mode);
2668 op0 = expand_expr (arg0, subtarget, mode, 0);
2669 if (GET_MODE (op0) != mode)
2670 op0 = convert_to_mode (mode, op0, 0);
2671 op1 = expand_expr (arg1, 0, mode2, 0);
2672 if (GET_MODE (op1) != mode2)
2673 op1 = convert_to_mode (mode2, op1, 0);
2675 target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc,
2676 target, LCT_CONST_MAKE_BLOCK, mode, 2,
2677 op0, mode, op1, mode2);
2679 return target;
2682 /* Expand expression EXP which is a call to the strlen builtin. Return 0
2683 if we failed the caller should emit a normal call, otherwise
2684 try to get the result in TARGET, if convenient. */
2686 static rtx
2687 expand_builtin_strlen (tree arglist, rtx target,
2688 enum machine_mode target_mode)
2690 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
2691 return 0;
2692 else
2694 rtx pat;
2695 tree len, src = TREE_VALUE (arglist);
2696 rtx result, src_reg, char_rtx, before_strlen;
2697 enum machine_mode insn_mode = target_mode, char_mode;
2698 enum insn_code icode = CODE_FOR_nothing;
2699 int align;
2701 /* If the length can be computed at compile-time, return it. */
2702 len = c_strlen (src, 0);
2703 if (len)
2704 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2706 /* If the length can be computed at compile-time and is constant
2707 integer, but there are side-effects in src, evaluate
2708 src for side-effects, then return len.
2709 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2710 can be optimized into: i++; x = 3; */
2711 len = c_strlen (src, 1);
2712 if (len && TREE_CODE (len) == INTEGER_CST)
2714 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2715 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2718 align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
2720 /* If SRC is not a pointer type, don't do this operation inline. */
2721 if (align == 0)
2722 return 0;
2724 /* Bail out if we can't compute strlen in the right mode. */
2725 while (insn_mode != VOIDmode)
2727 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
2728 if (icode != CODE_FOR_nothing)
2729 break;
2731 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2733 if (insn_mode == VOIDmode)
2734 return 0;
2736 /* Make a place to write the result of the instruction. */
2737 result = target;
2738 if (! (result != 0
2739 && REG_P (result)
2740 && GET_MODE (result) == insn_mode
2741 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
2742 result = gen_reg_rtx (insn_mode);
2744 /* Make a place to hold the source address. We will not expand
2745 the actual source until we are sure that the expansion will
2746 not fail -- there are trees that cannot be expanded twice. */
2747 src_reg = gen_reg_rtx (Pmode);
2749 /* Mark the beginning of the strlen sequence so we can emit the
2750 source operand later. */
2751 before_strlen = get_last_insn ();
2753 char_rtx = const0_rtx;
2754 char_mode = insn_data[(int) icode].operand[2].mode;
2755 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
2756 char_mode))
2757 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
2759 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
2760 char_rtx, GEN_INT (align));
2761 if (! pat)
2762 return 0;
2763 emit_insn (pat);
2765 /* Now that we are assured of success, expand the source. */
2766 start_sequence ();
2767 pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL);
2768 if (pat != src_reg)
2769 emit_move_insn (src_reg, pat);
2770 pat = get_insns ();
2771 end_sequence ();
2773 if (before_strlen)
2774 emit_insn_after (pat, before_strlen);
2775 else
2776 emit_insn_before (pat, get_insns ());
2778 /* Return the value in the proper mode for this function. */
2779 if (GET_MODE (result) == target_mode)
2780 target = result;
2781 else if (target != 0)
2782 convert_move (target, result, 0);
2783 else
2784 target = convert_to_mode (target_mode, result, 0);
2786 return target;
2790 /* Expand a call to the strstr builtin. Return 0 if we failed the
2791 caller should emit a normal call, otherwise try to get the result
2792 in TARGET, if convenient (and in mode MODE if that's convenient). */
2794 static rtx
2795 expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode)
2797 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2799 tree result = fold_builtin_strstr (arglist, type);
2800 if (result)
2801 return expand_expr (result, target, mode, EXPAND_NORMAL);
2803 return 0;
2806 /* Expand a call to the strchr builtin. Return 0 if we failed the
2807 caller should emit a normal call, otherwise try to get the result
2808 in TARGET, if convenient (and in mode MODE if that's convenient). */
2810 static rtx
2811 expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2813 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2815 tree result = fold_builtin_strchr (arglist, type);
2816 if (result)
2817 return expand_expr (result, target, mode, EXPAND_NORMAL);
2819 /* FIXME: Should use strchrM optab so that ports can optimize this. */
2821 return 0;
2824 /* Expand a call to the strrchr builtin. Return 0 if we failed the
2825 caller should emit a normal call, otherwise try to get the result
2826 in TARGET, if convenient (and in mode MODE if that's convenient). */
2828 static rtx
2829 expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode)
2831 if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2833 tree result = fold_builtin_strrchr (arglist, type);
2834 if (result)
2835 return expand_expr (result, target, mode, EXPAND_NORMAL);
2837 return 0;
2840 /* Expand a call to the strpbrk builtin. Return 0 if we failed the
2841 caller should emit a normal call, otherwise try to get the result
2842 in TARGET, if convenient (and in mode MODE if that's convenient). */
2844 static rtx
2845 expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode)
2847 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2849 tree result = fold_builtin_strpbrk (arglist, type);
2850 if (result)
2851 return expand_expr (result, target, mode, EXPAND_NORMAL);
2853 return 0;
2856 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
2857 bytes from constant string DATA + OFFSET and return it as target
2858 constant. */
2860 static rtx
2861 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
2862 enum machine_mode mode)
2864 const char *str = (const char *) data;
2866 gcc_assert (offset >= 0
2867 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
2868 <= strlen (str) + 1));
2870 return c_readstr (str + offset, mode);
2873 /* Expand a call to the memcpy builtin, with arguments in ARGLIST.
2874 Return 0 if we failed, the caller should emit a normal call,
2875 otherwise try to get the result in TARGET, if convenient (and in
2876 mode MODE if that's convenient). */
2877 static rtx
2878 expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
2880 tree fndecl = get_callee_fndecl (exp);
2881 tree arglist = TREE_OPERAND (exp, 1);
2882 if (!validate_arglist (arglist,
2883 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2884 return 0;
2885 else
2887 tree dest = TREE_VALUE (arglist);
2888 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2889 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2890 const char *src_str;
2891 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2892 unsigned int dest_align
2893 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2894 rtx dest_mem, src_mem, dest_addr, len_rtx;
2895 tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)),
2896 false, /*endp=*/0);
2898 if (result)
2900 while (TREE_CODE (result) == COMPOUND_EXPR)
2902 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
2903 EXPAND_NORMAL);
2904 result = TREE_OPERAND (result, 1);
2906 return expand_expr (result, target, mode, EXPAND_NORMAL);
2909 /* If DEST is not a pointer type, call the normal function. */
2910 if (dest_align == 0)
2911 return 0;
2913 /* If either SRC is not a pointer type, don't do this
2914 operation in-line. */
2915 if (src_align == 0)
2916 return 0;
2918 dest_mem = get_memory_rtx (dest, len);
2919 set_mem_align (dest_mem, dest_align);
2920 len_rtx = expand_normal (len);
2921 src_str = c_getstr (src);
2923 /* If SRC is a string constant and block move would be done
2924 by pieces, we can avoid loading the string from memory
2925 and only stored the computed constants. */
2926 if (src_str
2927 && GET_CODE (len_rtx) == CONST_INT
2928 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
2929 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
2930 (void *) src_str, dest_align))
2932 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
2933 builtin_memcpy_read_str,
2934 (void *) src_str, dest_align, 0);
2935 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2936 dest_mem = convert_memory_address (ptr_mode, dest_mem);
2937 return dest_mem;
2940 src_mem = get_memory_rtx (src, len);
2941 set_mem_align (src_mem, src_align);
2943 /* Copy word part most expediently. */
2944 dest_addr = emit_block_move (dest_mem, src_mem, len_rtx,
2945 CALL_EXPR_TAILCALL (exp)
2946 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
2948 if (dest_addr == 0)
2950 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
2951 dest_addr = convert_memory_address (ptr_mode, dest_addr);
2953 return dest_addr;
2957 /* Expand a call to the mempcpy builtin, with arguments in ARGLIST.
2958 Return 0 if we failed; the caller should emit a normal call,
2959 otherwise try to get the result in TARGET, if convenient (and in
2960 mode MODE if that's convenient). If ENDP is 0 return the
2961 destination pointer, if ENDP is 1 return the end pointer ala
2962 mempcpy, and if ENDP is 2 return the end pointer minus one ala
2963 stpcpy. */
2965 static rtx
2966 expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
2967 int endp)
2969 if (!validate_arglist (arglist,
2970 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2971 return 0;
2972 /* If return value is ignored, transform mempcpy into memcpy. */
2973 else if (target == const0_rtx)
2975 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
2977 if (!fn)
2978 return 0;
2980 return expand_expr (build_function_call_expr (fn, arglist),
2981 target, mode, EXPAND_NORMAL);
2983 else
2985 tree dest = TREE_VALUE (arglist);
2986 tree src = TREE_VALUE (TREE_CHAIN (arglist));
2987 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
2988 const char *src_str;
2989 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
2990 unsigned int dest_align
2991 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
2992 rtx dest_mem, src_mem, len_rtx;
2993 tree result = fold_builtin_memory_op (arglist, type, false, endp);
2995 if (result)
2997 while (TREE_CODE (result) == COMPOUND_EXPR)
2999 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3000 EXPAND_NORMAL);
3001 result = TREE_OPERAND (result, 1);
3003 return expand_expr (result, target, mode, EXPAND_NORMAL);
3006 /* If either SRC or DEST is not a pointer type, don't do this
3007 operation in-line. */
3008 if (dest_align == 0 || src_align == 0)
3009 return 0;
3011 /* If LEN is not constant, call the normal function. */
3012 if (! host_integerp (len, 1))
3013 return 0;
3015 len_rtx = expand_normal (len);
3016 src_str = c_getstr (src);
3018 /* If SRC is a string constant and block move would be done
3019 by pieces, we can avoid loading the string from memory
3020 and only stored the computed constants. */
3021 if (src_str
3022 && GET_CODE (len_rtx) == CONST_INT
3023 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3024 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3025 (void *) src_str, dest_align))
3027 dest_mem = get_memory_rtx (dest, len);
3028 set_mem_align (dest_mem, dest_align);
3029 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3030 builtin_memcpy_read_str,
3031 (void *) src_str, dest_align, endp);
3032 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3033 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3034 return dest_mem;
3037 if (GET_CODE (len_rtx) == CONST_INT
3038 && can_move_by_pieces (INTVAL (len_rtx),
3039 MIN (dest_align, src_align)))
3041 dest_mem = get_memory_rtx (dest, len);
3042 set_mem_align (dest_mem, dest_align);
3043 src_mem = get_memory_rtx (src, len);
3044 set_mem_align (src_mem, src_align);
3045 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3046 MIN (dest_align, src_align), endp);
3047 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3048 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3049 return dest_mem;
3052 return 0;
3056 /* Expand expression EXP, which is a call to the memmove builtin. Return 0
3057 if we failed; the caller should emit a normal call. */
3059 static rtx
3060 expand_builtin_memmove (tree arglist, tree type, rtx target,
3061 enum machine_mode mode, tree orig_exp)
3063 if (!validate_arglist (arglist,
3064 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3065 return 0;
3066 else
3068 tree dest = TREE_VALUE (arglist);
3069 tree src = TREE_VALUE (TREE_CHAIN (arglist));
3070 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3072 unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
3073 unsigned int dest_align
3074 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3075 tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3);
3077 if (result)
3079 while (TREE_CODE (result) == COMPOUND_EXPR)
3081 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3082 EXPAND_NORMAL);
3083 result = TREE_OPERAND (result, 1);
3085 return expand_expr (result, target, mode, EXPAND_NORMAL);
3088 /* If DEST is not a pointer type, call the normal function. */
3089 if (dest_align == 0)
3090 return 0;
3092 /* If either SRC is not a pointer type, don't do this
3093 operation in-line. */
3094 if (src_align == 0)
3095 return 0;
3097 /* If src is categorized for a readonly section we can use
3098 normal memcpy. */
3099 if (readonly_data_expr (src))
3101 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3102 if (!fn)
3103 return 0;
3104 fn = build_function_call_expr (fn, arglist);
3105 if (TREE_CODE (fn) == CALL_EXPR)
3106 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3107 return expand_expr (fn, target, mode, EXPAND_NORMAL);
3110 /* If length is 1 and we can expand memcpy call inline,
3111 it is ok to use memcpy as well. */
3112 if (integer_onep (len))
3114 rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
3115 /*endp=*/0);
3116 if (ret)
3117 return ret;
3120 /* Otherwise, call the normal function. */
3121 return 0;
3125 /* Expand expression EXP, which is a call to the bcopy builtin. Return 0
3126 if we failed the caller should emit a normal call. */
3128 static rtx
3129 expand_builtin_bcopy (tree exp)
3131 tree arglist = TREE_OPERAND (exp, 1);
3132 tree type = TREE_TYPE (exp);
3133 tree src, dest, size, newarglist;
3135 if (!validate_arglist (arglist,
3136 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3137 return NULL_RTX;
3139 src = TREE_VALUE (arglist);
3140 dest = TREE_VALUE (TREE_CHAIN (arglist));
3141 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3143 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
3144 memmove(ptr y, ptr x, size_t z). This is done this way
3145 so that if it isn't expanded inline, we fallback to
3146 calling bcopy instead of memmove. */
3148 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3149 newarglist = tree_cons (NULL_TREE, src, newarglist);
3150 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3152 return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode, exp);
3155 #ifndef HAVE_movstr
3156 # define HAVE_movstr 0
3157 # define CODE_FOR_movstr CODE_FOR_nothing
3158 #endif
3160 /* Expand into a movstr instruction, if one is available. Return 0 if
3161 we failed, the caller should emit a normal call, otherwise try to
3162 get the result in TARGET, if convenient. If ENDP is 0 return the
3163 destination pointer, if ENDP is 1 return the end pointer ala
3164 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3165 stpcpy. */
3167 static rtx
3168 expand_movstr (tree dest, tree src, rtx target, int endp)
3170 rtx end;
3171 rtx dest_mem;
3172 rtx src_mem;
3173 rtx insn;
3174 const struct insn_data * data;
3176 if (!HAVE_movstr)
3177 return 0;
3179 dest_mem = get_memory_rtx (dest, NULL);
3180 src_mem = get_memory_rtx (src, NULL);
3181 if (!endp)
3183 target = force_reg (Pmode, XEXP (dest_mem, 0));
3184 dest_mem = replace_equiv_address (dest_mem, target);
3185 end = gen_reg_rtx (Pmode);
3187 else
3189 if (target == 0 || target == const0_rtx)
3191 end = gen_reg_rtx (Pmode);
3192 if (target == 0)
3193 target = end;
3195 else
3196 end = target;
3199 data = insn_data + CODE_FOR_movstr;
3201 if (data->operand[0].mode != VOIDmode)
3202 end = gen_lowpart (data->operand[0].mode, end);
3204 insn = data->genfun (end, dest_mem, src_mem);
3206 gcc_assert (insn);
3208 emit_insn (insn);
3210 /* movstr is supposed to set end to the address of the NUL
3211 terminator. If the caller requested a mempcpy-like return value,
3212 adjust it. */
3213 if (endp == 1 && target != const0_rtx)
3215 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1);
3216 emit_move_insn (target, force_operand (tem, NULL_RTX));
3219 return target;
3222 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
3223 if we failed the caller should emit a normal call, otherwise try to get
3224 the result in TARGET, if convenient (and in mode MODE if that's
3225 convenient). */
3227 static rtx
3228 expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
3230 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3232 tree result = fold_builtin_strcpy (fndecl, arglist, 0);
3233 if (result)
3235 while (TREE_CODE (result) == COMPOUND_EXPR)
3237 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3238 EXPAND_NORMAL);
3239 result = TREE_OPERAND (result, 1);
3241 return expand_expr (result, target, mode, EXPAND_NORMAL);
3244 return expand_movstr (TREE_VALUE (arglist),
3245 TREE_VALUE (TREE_CHAIN (arglist)),
3246 target, /*endp=*/0);
3248 return 0;
3251 /* Expand a call to the stpcpy builtin, with arguments in ARGLIST.
3252 Return 0 if we failed the caller should emit a normal call,
3253 otherwise try to get the result in TARGET, if convenient (and in
3254 mode MODE if that's convenient). */
3256 static rtx
3257 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3259 tree arglist = TREE_OPERAND (exp, 1);
3260 /* If return value is ignored, transform stpcpy into strcpy. */
3261 if (target == const0_rtx)
3263 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3264 if (!fn)
3265 return 0;
3267 return expand_expr (build_function_call_expr (fn, arglist),
3268 target, mode, EXPAND_NORMAL);
3271 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3272 return 0;
3273 else
3275 tree dst, src, len, lenp1;
3276 tree narglist;
3277 rtx ret;
3279 /* Ensure we get an actual string whose length can be evaluated at
3280 compile-time, not an expression containing a string. This is
3281 because the latter will potentially produce pessimized code
3282 when used to produce the return value. */
3283 src = TREE_VALUE (TREE_CHAIN (arglist));
3284 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3285 return expand_movstr (TREE_VALUE (arglist),
3286 TREE_VALUE (TREE_CHAIN (arglist)),
3287 target, /*endp=*/2);
3289 dst = TREE_VALUE (arglist);
3290 lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1));
3291 narglist = build_tree_list (NULL_TREE, lenp1);
3292 narglist = tree_cons (NULL_TREE, src, narglist);
3293 narglist = tree_cons (NULL_TREE, dst, narglist);
3294 ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
3295 target, mode, /*endp=*/2);
3297 if (ret)
3298 return ret;
3300 if (TREE_CODE (len) == INTEGER_CST)
3302 rtx len_rtx = expand_normal (len);
3304 if (GET_CODE (len_rtx) == CONST_INT)
3306 ret = expand_builtin_strcpy (get_callee_fndecl (exp),
3307 arglist, target, mode);
3309 if (ret)
3311 if (! target)
3313 if (mode != VOIDmode)
3314 target = gen_reg_rtx (mode);
3315 else
3316 target = gen_reg_rtx (GET_MODE (ret));
3318 if (GET_MODE (target) != GET_MODE (ret))
3319 ret = gen_lowpart (GET_MODE (target), ret);
3321 ret = plus_constant (ret, INTVAL (len_rtx));
3322 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3323 gcc_assert (ret);
3325 return target;
3330 return expand_movstr (TREE_VALUE (arglist),
3331 TREE_VALUE (TREE_CHAIN (arglist)),
3332 target, /*endp=*/2);
3336 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3337 bytes from constant string DATA + OFFSET and return it as target
3338 constant. */
3340 static rtx
3341 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3342 enum machine_mode mode)
3344 const char *str = (const char *) data;
3346 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3347 return const0_rtx;
3349 return c_readstr (str + offset, mode);
3352 /* Expand expression EXP, which is a call to the strncpy builtin. Return 0
3353 if we failed the caller should emit a normal call. */
3355 static rtx
3356 expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode)
3358 tree fndecl = get_callee_fndecl (exp);
3359 tree arglist = TREE_OPERAND (exp, 1);
3360 if (validate_arglist (arglist,
3361 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3363 tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1);
3364 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3365 tree result = fold_builtin_strncpy (fndecl, arglist, slen);
3367 if (result)
3369 while (TREE_CODE (result) == COMPOUND_EXPR)
3371 expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
3372 EXPAND_NORMAL);
3373 result = TREE_OPERAND (result, 1);
3375 return expand_expr (result, target, mode, EXPAND_NORMAL);
3378 /* We must be passed a constant len and src parameter. */
3379 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3380 return 0;
3382 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
3384 /* We're required to pad with trailing zeros if the requested
3385 len is greater than strlen(s2)+1. In that case try to
3386 use store_by_pieces, if it fails, punt. */
3387 if (tree_int_cst_lt (slen, len))
3389 tree dest = TREE_VALUE (arglist);
3390 unsigned int dest_align
3391 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3392 const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist)));
3393 rtx dest_mem;
3395 if (!p || dest_align == 0 || !host_integerp (len, 1)
3396 || !can_store_by_pieces (tree_low_cst (len, 1),
3397 builtin_strncpy_read_str,
3398 (void *) p, dest_align))
3399 return 0;
3401 dest_mem = get_memory_rtx (dest, len);
3402 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3403 builtin_strncpy_read_str,
3404 (void *) p, dest_align, 0);
3405 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3406 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3407 return dest_mem;
3410 return 0;
3413 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3414 bytes from constant string DATA + OFFSET and return it as target
3415 constant. */
3417 static rtx
3418 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3419 enum machine_mode mode)
3421 const char *c = (const char *) data;
3422 char *p = alloca (GET_MODE_SIZE (mode));
3424 memset (p, *c, GET_MODE_SIZE (mode));
3426 return c_readstr (p, mode);
3429 /* Callback routine for store_by_pieces. Return the RTL of a register
3430 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3431 char value given in the RTL register data. For example, if mode is
3432 4 bytes wide, return the RTL for 0x01010101*data. */
3434 static rtx
3435 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3436 enum machine_mode mode)
3438 rtx target, coeff;
3439 size_t size;
3440 char *p;
3442 size = GET_MODE_SIZE (mode);
3443 if (size == 1)
3444 return (rtx) data;
3446 p = alloca (size);
3447 memset (p, 1, size);
3448 coeff = c_readstr (p, mode);
3450 target = convert_to_mode (mode, (rtx) data, 1);
3451 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3452 return force_reg (mode, target);
3455 /* Expand expression EXP, which is a call to the memset builtin. Return 0
3456 if we failed the caller should emit a normal call, otherwise try to get
3457 the result in TARGET, if convenient (and in mode MODE if that's
3458 convenient). */
3460 static rtx
3461 expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode,
3462 tree orig_exp)
3464 if (!validate_arglist (arglist,
3465 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3466 return 0;
3467 else
3469 tree dest = TREE_VALUE (arglist);
3470 tree val = TREE_VALUE (TREE_CHAIN (arglist));
3471 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3472 tree fndecl, fn;
3473 enum built_in_function fcode;
3474 char c;
3475 unsigned int dest_align;
3476 rtx dest_mem, dest_addr, len_rtx;
3478 dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
3480 /* If DEST is not a pointer type, don't do this
3481 operation in-line. */
3482 if (dest_align == 0)
3483 return 0;
3485 /* If the LEN parameter is zero, return DEST. */
3486 if (integer_zerop (len))
3488 /* Evaluate and ignore VAL in case it has side-effects. */
3489 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3490 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3493 /* Stabilize the arguments in case we fail. */
3494 dest = builtin_save_expr (dest);
3495 val = builtin_save_expr (val);
3496 len = builtin_save_expr (len);
3498 len_rtx = expand_normal (len);
3499 dest_mem = get_memory_rtx (dest, len);
3501 if (TREE_CODE (val) != INTEGER_CST)
3503 rtx val_rtx;
3505 val_rtx = expand_normal (val);
3506 val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node),
3507 val_rtx, 0);
3509 /* Assume that we can memset by pieces if we can store the
3510 * the coefficients by pieces (in the required modes).
3511 * We can't pass builtin_memset_gen_str as that emits RTL. */
3512 c = 1;
3513 if (host_integerp (len, 1)
3514 && !(optimize_size && tree_low_cst (len, 1) > 1)
3515 && can_store_by_pieces (tree_low_cst (len, 1),
3516 builtin_memset_read_str, &c, dest_align))
3518 val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node),
3519 val_rtx);
3520 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3521 builtin_memset_gen_str, val_rtx, dest_align, 0);
3523 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3524 dest_align))
3525 goto do_libcall;
3527 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3528 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3529 return dest_mem;
3532 if (target_char_cast (val, &c))
3533 goto do_libcall;
3535 if (c)
3537 if (host_integerp (len, 1)
3538 && !(optimize_size && tree_low_cst (len, 1) > 1)
3539 && can_store_by_pieces (tree_low_cst (len, 1),
3540 builtin_memset_read_str, &c, dest_align))
3541 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3542 builtin_memset_read_str, &c, dest_align, 0);
3543 else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c),
3544 dest_align))
3545 goto do_libcall;
3547 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3548 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3549 return dest_mem;
3552 set_mem_align (dest_mem, dest_align);
3553 dest_addr = clear_storage (dest_mem, len_rtx,
3554 CALL_EXPR_TAILCALL (orig_exp)
3555 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL);
3557 if (dest_addr == 0)
3559 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3560 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3563 return dest_addr;
3565 do_libcall:
3566 fndecl = get_callee_fndecl (orig_exp);
3567 fcode = DECL_FUNCTION_CODE (fndecl);
3568 gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO);
3569 arglist = build_tree_list (NULL_TREE, len);
3570 if (fcode == BUILT_IN_MEMSET)
3571 arglist = tree_cons (NULL_TREE, val, arglist);
3572 arglist = tree_cons (NULL_TREE, dest, arglist);
3573 fn = build_function_call_expr (fndecl, arglist);
3574 if (TREE_CODE (fn) == CALL_EXPR)
3575 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3576 return expand_call (fn, target, target == const0_rtx);
3580 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
3581 if we failed the caller should emit a normal call. */
3583 static rtx
3584 expand_builtin_bzero (tree exp)
3586 tree arglist = TREE_OPERAND (exp, 1);
3587 tree dest, size, newarglist;
3589 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3590 return NULL_RTX;
3592 dest = TREE_VALUE (arglist);
3593 size = TREE_VALUE (TREE_CHAIN (arglist));
3595 /* New argument list transforming bzero(ptr x, int y) to
3596 memset(ptr x, int 0, size_t y). This is done this way
3597 so that if it isn't expanded inline, we fallback to
3598 calling bzero instead of memset. */
3600 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
3601 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
3602 newarglist = tree_cons (NULL_TREE, dest, newarglist);
3604 return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp);
3607 /* Expand expression EXP, which is a call to the memcmp built-in function.
3608 ARGLIST is the argument list for this call. Return 0 if we failed and the
3609 caller should emit a normal call, otherwise try to get the result in
3610 TARGET, if convenient (and in mode MODE, if that's convenient). */
3612 static rtx
3613 expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target,
3614 enum machine_mode mode)
3616 if (!validate_arglist (arglist,
3617 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3618 return 0;
3619 else
3621 tree result = fold_builtin_memcmp (arglist);
3622 if (result)
3623 return expand_expr (result, target, mode, EXPAND_NORMAL);
3626 #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi
3628 tree arg1 = TREE_VALUE (arglist);
3629 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3630 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3631 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3632 rtx result;
3633 rtx insn;
3635 int arg1_align
3636 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3637 int arg2_align
3638 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3639 enum machine_mode insn_mode;
3641 #ifdef HAVE_cmpmemsi
3642 if (HAVE_cmpmemsi)
3643 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3644 else
3645 #endif
3646 #ifdef HAVE_cmpstrnsi
3647 if (HAVE_cmpstrnsi)
3648 insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3649 else
3650 #endif
3651 return 0;
3653 /* If we don't have POINTER_TYPE, call the function. */
3654 if (arg1_align == 0 || arg2_align == 0)
3655 return 0;
3657 /* Make a place to write the result of the instruction. */
3658 result = target;
3659 if (! (result != 0
3660 && REG_P (result) && GET_MODE (result) == insn_mode
3661 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3662 result = gen_reg_rtx (insn_mode);
3664 arg1_rtx = get_memory_rtx (arg1, len);
3665 arg2_rtx = get_memory_rtx (arg2, len);
3666 arg3_rtx = expand_normal (len);
3668 /* Set MEM_SIZE as appropriate. */
3669 if (GET_CODE (arg3_rtx) == CONST_INT)
3671 set_mem_size (arg1_rtx, arg3_rtx);
3672 set_mem_size (arg2_rtx, arg3_rtx);
3675 #ifdef HAVE_cmpmemsi
3676 if (HAVE_cmpmemsi)
3677 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3678 GEN_INT (MIN (arg1_align, arg2_align)));
3679 else
3680 #endif
3681 #ifdef HAVE_cmpstrnsi
3682 if (HAVE_cmpstrnsi)
3683 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3684 GEN_INT (MIN (arg1_align, arg2_align)));
3685 else
3686 #endif
3687 gcc_unreachable ();
3689 if (insn)
3690 emit_insn (insn);
3691 else
3692 emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK,
3693 TYPE_MODE (integer_type_node), 3,
3694 XEXP (arg1_rtx, 0), Pmode,
3695 XEXP (arg2_rtx, 0), Pmode,
3696 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3697 TYPE_UNSIGNED (sizetype)),
3698 TYPE_MODE (sizetype));
3700 /* Return the value in the proper mode for this function. */
3701 mode = TYPE_MODE (TREE_TYPE (exp));
3702 if (GET_MODE (result) == mode)
3703 return result;
3704 else if (target != 0)
3706 convert_move (target, result, 0);
3707 return target;
3709 else
3710 return convert_to_mode (mode, result, 0);
3712 #endif
3714 return 0;
3717 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
3718 if we failed the caller should emit a normal call, otherwise try to get
3719 the result in TARGET, if convenient. */
3721 static rtx
3722 expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode)
3724 tree arglist = TREE_OPERAND (exp, 1);
3726 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3727 return 0;
3728 else
3730 tree result = fold_builtin_strcmp (arglist);
3731 if (result)
3732 return expand_expr (result, target, mode, EXPAND_NORMAL);
3735 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3736 if (cmpstr_optab[SImode] != CODE_FOR_nothing
3737 || cmpstrn_optab[SImode] != CODE_FOR_nothing)
3739 rtx arg1_rtx, arg2_rtx;
3740 rtx result, insn = NULL_RTX;
3741 tree fndecl, fn;
3743 tree arg1 = TREE_VALUE (arglist);
3744 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3745 int arg1_align
3746 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3747 int arg2_align
3748 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3750 /* If we don't have POINTER_TYPE, call the function. */
3751 if (arg1_align == 0 || arg2_align == 0)
3752 return 0;
3754 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3755 arg1 = builtin_save_expr (arg1);
3756 arg2 = builtin_save_expr (arg2);
3758 arg1_rtx = get_memory_rtx (arg1, NULL);
3759 arg2_rtx = get_memory_rtx (arg2, NULL);
3761 #ifdef HAVE_cmpstrsi
3762 /* Try to call cmpstrsi. */
3763 if (HAVE_cmpstrsi)
3765 enum machine_mode insn_mode
3766 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3768 /* Make a place to write the result of the instruction. */
3769 result = target;
3770 if (! (result != 0
3771 && REG_P (result) && GET_MODE (result) == insn_mode
3772 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3773 result = gen_reg_rtx (insn_mode);
3775 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3776 GEN_INT (MIN (arg1_align, arg2_align)));
3778 #endif
3779 #ifdef HAVE_cmpstrnsi
3780 /* Try to determine at least one length and call cmpstrnsi. */
3781 if (!insn && HAVE_cmpstrnsi)
3783 tree len;
3784 rtx arg3_rtx;
3786 enum machine_mode insn_mode
3787 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3788 tree len1 = c_strlen (arg1, 1);
3789 tree len2 = c_strlen (arg2, 1);
3791 if (len1)
3792 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3793 if (len2)
3794 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3796 /* If we don't have a constant length for the first, use the length
3797 of the second, if we know it. We don't require a constant for
3798 this case; some cost analysis could be done if both are available
3799 but neither is constant. For now, assume they're equally cheap,
3800 unless one has side effects. If both strings have constant lengths,
3801 use the smaller. */
3803 if (!len1)
3804 len = len2;
3805 else if (!len2)
3806 len = len1;
3807 else if (TREE_SIDE_EFFECTS (len1))
3808 len = len2;
3809 else if (TREE_SIDE_EFFECTS (len2))
3810 len = len1;
3811 else if (TREE_CODE (len1) != INTEGER_CST)
3812 len = len2;
3813 else if (TREE_CODE (len2) != INTEGER_CST)
3814 len = len1;
3815 else if (tree_int_cst_lt (len1, len2))
3816 len = len1;
3817 else
3818 len = len2;
3820 /* If both arguments have side effects, we cannot optimize. */
3821 if (!len || TREE_SIDE_EFFECTS (len))
3822 goto do_libcall;
3824 arg3_rtx = expand_normal (len);
3826 /* Make a place to write the result of the instruction. */
3827 result = target;
3828 if (! (result != 0
3829 && REG_P (result) && GET_MODE (result) == insn_mode
3830 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3831 result = gen_reg_rtx (insn_mode);
3833 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3834 GEN_INT (MIN (arg1_align, arg2_align)));
3836 #endif
3838 if (insn)
3840 emit_insn (insn);
3842 /* Return the value in the proper mode for this function. */
3843 mode = TYPE_MODE (TREE_TYPE (exp));
3844 if (GET_MODE (result) == mode)
3845 return result;
3846 if (target == 0)
3847 return convert_to_mode (mode, result, 0);
3848 convert_move (target, result, 0);
3849 return target;
3852 /* Expand the library call ourselves using a stabilized argument
3853 list to avoid re-evaluating the function's arguments twice. */
3854 #ifdef HAVE_cmpstrnsi
3855 do_libcall:
3856 #endif
3857 arglist = build_tree_list (NULL_TREE, arg2);
3858 arglist = tree_cons (NULL_TREE, arg1, arglist);
3859 fndecl = get_callee_fndecl (exp);
3860 fn = build_function_call_expr (fndecl, arglist);
3861 if (TREE_CODE (fn) == CALL_EXPR)
3862 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3863 return expand_call (fn, target, target == const0_rtx);
3865 #endif
3866 return 0;
3869 /* Expand expression EXP, which is a call to the strncmp builtin. Return 0
3870 if we failed the caller should emit a normal call, otherwise try to get
3871 the result in TARGET, if convenient. */
3873 static rtx
3874 expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode)
3876 tree arglist = TREE_OPERAND (exp, 1);
3878 if (!validate_arglist (arglist,
3879 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3880 return 0;
3881 else
3883 tree result = fold_builtin_strncmp (arglist);
3884 if (result)
3885 return expand_expr (result, target, mode, EXPAND_NORMAL);
3888 /* If c_strlen can determine an expression for one of the string
3889 lengths, and it doesn't have side effects, then emit cmpstrnsi
3890 using length MIN(strlen(string)+1, arg3). */
3891 #ifdef HAVE_cmpstrnsi
3892 if (HAVE_cmpstrnsi)
3894 tree arg1 = TREE_VALUE (arglist);
3895 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
3896 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3897 tree len, len1, len2;
3898 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3899 rtx result, insn;
3900 tree fndecl, fn;
3902 int arg1_align
3903 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3904 int arg2_align
3905 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
3906 enum machine_mode insn_mode
3907 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3909 len1 = c_strlen (arg1, 1);
3910 len2 = c_strlen (arg2, 1);
3912 if (len1)
3913 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3914 if (len2)
3915 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3917 /* If we don't have a constant length for the first, use the length
3918 of the second, if we know it. We don't require a constant for
3919 this case; some cost analysis could be done if both are available
3920 but neither is constant. For now, assume they're equally cheap,
3921 unless one has side effects. If both strings have constant lengths,
3922 use the smaller. */
3924 if (!len1)
3925 len = len2;
3926 else if (!len2)
3927 len = len1;
3928 else if (TREE_SIDE_EFFECTS (len1))
3929 len = len2;
3930 else if (TREE_SIDE_EFFECTS (len2))
3931 len = len1;
3932 else if (TREE_CODE (len1) != INTEGER_CST)
3933 len = len2;
3934 else if (TREE_CODE (len2) != INTEGER_CST)
3935 len = len1;
3936 else if (tree_int_cst_lt (len1, len2))
3937 len = len1;
3938 else
3939 len = len2;
3941 /* If both arguments have side effects, we cannot optimize. */
3942 if (!len || TREE_SIDE_EFFECTS (len))
3943 return 0;
3945 /* The actual new length parameter is MIN(len,arg3). */
3946 len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len,
3947 fold_convert (TREE_TYPE (len), arg3));
3949 /* If we don't have POINTER_TYPE, call the function. */
3950 if (arg1_align == 0 || arg2_align == 0)
3951 return 0;
3953 /* Make a place to write the result of the instruction. */
3954 result = target;
3955 if (! (result != 0
3956 && REG_P (result) && GET_MODE (result) == insn_mode
3957 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3958 result = gen_reg_rtx (insn_mode);
3960 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3961 arg1 = builtin_save_expr (arg1);
3962 arg2 = builtin_save_expr (arg2);
3963 len = builtin_save_expr (len);
3965 arg1_rtx = get_memory_rtx (arg1, len);
3966 arg2_rtx = get_memory_rtx (arg2, len);
3967 arg3_rtx = expand_normal (len);
3968 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3969 GEN_INT (MIN (arg1_align, arg2_align)));
3970 if (insn)
3972 emit_insn (insn);
3974 /* Return the value in the proper mode for this function. */
3975 mode = TYPE_MODE (TREE_TYPE (exp));
3976 if (GET_MODE (result) == mode)
3977 return result;
3978 if (target == 0)
3979 return convert_to_mode (mode, result, 0);
3980 convert_move (target, result, 0);
3981 return target;
3984 /* Expand the library call ourselves using a stabilized argument
3985 list to avoid re-evaluating the function's arguments twice. */
3986 arglist = build_tree_list (NULL_TREE, len);
3987 arglist = tree_cons (NULL_TREE, arg2, arglist);
3988 arglist = tree_cons (NULL_TREE, arg1, arglist);
3989 fndecl = get_callee_fndecl (exp);
3990 fn = build_function_call_expr (fndecl, arglist);
3991 if (TREE_CODE (fn) == CALL_EXPR)
3992 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3993 return expand_call (fn, target, target == const0_rtx);
3995 #endif
3996 return 0;
3999 /* Expand expression EXP, which is a call to the strcat builtin.
4000 Return 0 if we failed the caller should emit a normal call,
4001 otherwise try to get the result in TARGET, if convenient. */
4003 static rtx
4004 expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode)
4006 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4007 return 0;
4008 else
4010 tree dst = TREE_VALUE (arglist),
4011 src = TREE_VALUE (TREE_CHAIN (arglist));
4012 const char *p = c_getstr (src);
4014 /* If the string length is zero, return the dst parameter. */
4015 if (p && *p == '\0')
4016 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4018 if (!optimize_size)
4020 /* See if we can store by pieces into (dst + strlen(dst)). */
4021 tree newsrc, newdst,
4022 strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
4023 rtx insns;
4025 /* Stabilize the argument list. */
4026 newsrc = builtin_save_expr (src);
4027 if (newsrc != src)
4028 arglist = build_tree_list (NULL_TREE, newsrc);
4029 else
4030 arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */
4032 dst = builtin_save_expr (dst);
4034 start_sequence ();
4036 /* Create strlen (dst). */
4037 newdst =
4038 build_function_call_expr (strlen_fn,
4039 build_tree_list (NULL_TREE, dst));
4040 /* Create (dst + (cast) strlen (dst)). */
4041 newdst = fold_convert (TREE_TYPE (dst), newdst);
4042 newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst);
4044 newdst = builtin_save_expr (newdst);
4045 arglist = tree_cons (NULL_TREE, newdst, arglist);
4047 if (!expand_builtin_strcpy (fndecl, arglist, target, mode))
4049 end_sequence (); /* Stop sequence. */
4050 return 0;
4053 /* Output the entire sequence. */
4054 insns = get_insns ();
4055 end_sequence ();
4056 emit_insn (insns);
4058 return expand_expr (dst, target, mode, EXPAND_NORMAL);
4061 return 0;
4065 /* Expand expression EXP, which is a call to the strncat builtin.
4066 Return 0 if we failed the caller should emit a normal call,
4067 otherwise try to get the result in TARGET, if convenient. */
4069 static rtx
4070 expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode)
4072 if (validate_arglist (arglist,
4073 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4075 tree result = fold_builtin_strncat (arglist);
4076 if (result)
4077 return expand_expr (result, target, mode, EXPAND_NORMAL);
4079 return 0;
4082 /* Expand expression EXP, which is a call to the strspn builtin.
4083 Return 0 if we failed the caller should emit a normal call,
4084 otherwise try to get the result in TARGET, if convenient. */
4086 static rtx
4087 expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode)
4089 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4091 tree result = fold_builtin_strspn (arglist);
4092 if (result)
4093 return expand_expr (result, target, mode, EXPAND_NORMAL);
4095 return 0;
4098 /* Expand expression EXP, which is a call to the strcspn builtin.
4099 Return 0 if we failed the caller should emit a normal call,
4100 otherwise try to get the result in TARGET, if convenient. */
4102 static rtx
4103 expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode)
4105 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4107 tree result = fold_builtin_strcspn (arglist);
4108 if (result)
4109 return expand_expr (result, target, mode, EXPAND_NORMAL);
4111 return 0;
4114 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4115 if that's convenient. */
4118 expand_builtin_saveregs (void)
4120 rtx val, seq;
4122 /* Don't do __builtin_saveregs more than once in a function.
4123 Save the result of the first call and reuse it. */
4124 if (saveregs_value != 0)
4125 return saveregs_value;
4127 /* When this function is called, it means that registers must be
4128 saved on entry to this function. So we migrate the call to the
4129 first insn of this function. */
4131 start_sequence ();
4133 /* Do whatever the machine needs done in this case. */
4134 val = targetm.calls.expand_builtin_saveregs ();
4136 seq = get_insns ();
4137 end_sequence ();
4139 saveregs_value = val;
4141 /* Put the insns after the NOTE that starts the function. If this
4142 is inside a start_sequence, make the outer-level insn chain current, so
4143 the code is placed at the start of the function. */
4144 push_topmost_sequence ();
4145 emit_insn_after (seq, entry_of_function ());
4146 pop_topmost_sequence ();
4148 return val;
4151 /* __builtin_args_info (N) returns word N of the arg space info
4152 for the current function. The number and meanings of words
4153 is controlled by the definition of CUMULATIVE_ARGS. */
4155 static rtx
4156 expand_builtin_args_info (tree arglist)
4158 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
4159 int *word_ptr = (int *) &current_function_args_info;
4161 gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0);
4163 if (arglist != 0)
4165 if (!host_integerp (TREE_VALUE (arglist), 0))
4166 error ("argument of %<__builtin_args_info%> must be constant");
4167 else
4169 HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0);
4171 if (wordnum < 0 || wordnum >= nwords)
4172 error ("argument of %<__builtin_args_info%> out of range");
4173 else
4174 return GEN_INT (word_ptr[wordnum]);
4177 else
4178 error ("missing argument in %<__builtin_args_info%>");
4180 return const0_rtx;
4183 /* Expand a call to __builtin_next_arg. */
4185 static rtx
4186 expand_builtin_next_arg (void)
4188 /* Checking arguments is already done in fold_builtin_next_arg
4189 that must be called before this function. */
4190 return expand_binop (Pmode, add_optab,
4191 current_function_internal_arg_pointer,
4192 current_function_arg_offset_rtx,
4193 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4196 /* Make it easier for the backends by protecting the valist argument
4197 from multiple evaluations. */
4199 static tree
4200 stabilize_va_list (tree valist, int needs_lvalue)
4202 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4204 if (TREE_SIDE_EFFECTS (valist))
4205 valist = save_expr (valist);
4207 /* For this case, the backends will be expecting a pointer to
4208 TREE_TYPE (va_list_type_node), but it's possible we've
4209 actually been given an array (an actual va_list_type_node).
4210 So fix it. */
4211 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4213 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4214 valist = build_fold_addr_expr_with_type (valist, p1);
4217 else
4219 tree pt;
4221 if (! needs_lvalue)
4223 if (! TREE_SIDE_EFFECTS (valist))
4224 return valist;
4226 pt = build_pointer_type (va_list_type_node);
4227 valist = fold_build1 (ADDR_EXPR, pt, valist);
4228 TREE_SIDE_EFFECTS (valist) = 1;
4231 if (TREE_SIDE_EFFECTS (valist))
4232 valist = save_expr (valist);
4233 valist = build_fold_indirect_ref (valist);
4236 return valist;
4239 /* The "standard" definition of va_list is void*. */
4241 tree
4242 std_build_builtin_va_list (void)
4244 return ptr_type_node;
4247 /* The "standard" implementation of va_start: just assign `nextarg' to
4248 the variable. */
4250 void
4251 std_expand_builtin_va_start (tree valist, rtx nextarg)
4253 tree t;
4255 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
4256 make_tree (ptr_type_node, nextarg));
4257 TREE_SIDE_EFFECTS (t) = 1;
4259 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4262 /* Expand ARGLIST, from a call to __builtin_va_start. */
4264 static rtx
4265 expand_builtin_va_start (tree arglist)
4267 rtx nextarg;
4268 tree chain, valist;
4270 chain = TREE_CHAIN (arglist);
4272 if (!chain)
4274 error ("too few arguments to function %<va_start%>");
4275 return const0_rtx;
4278 if (fold_builtin_next_arg (chain))
4279 return const0_rtx;
4281 nextarg = expand_builtin_next_arg ();
4282 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
4284 #ifdef EXPAND_BUILTIN_VA_START
4285 EXPAND_BUILTIN_VA_START (valist, nextarg);
4286 #else
4287 std_expand_builtin_va_start (valist, nextarg);
4288 #endif
4290 return const0_rtx;
4293 /* The "standard" implementation of va_arg: read the value from the
4294 current (padded) address and increment by the (padded) size. */
4296 tree
4297 std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4299 tree addr, t, type_size, rounded_size, valist_tmp;
4300 unsigned HOST_WIDE_INT align, boundary;
4301 bool indirect;
4303 #ifdef ARGS_GROW_DOWNWARD
4304 /* All of the alignment and movement below is for args-grow-up machines.
4305 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4306 implement their own specialized gimplify_va_arg_expr routines. */
4307 gcc_unreachable ();
4308 #endif
4310 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4311 if (indirect)
4312 type = build_pointer_type (type);
4314 align = PARM_BOUNDARY / BITS_PER_UNIT;
4315 boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT;
4317 /* Hoist the valist value into a temporary for the moment. */
4318 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4320 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4321 requires greater alignment, we must perform dynamic alignment. */
4322 if (boundary > align
4323 && !integer_zerop (TYPE_SIZE (type)))
4325 t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1));
4326 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4327 build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t));
4328 gimplify_and_add (t, pre_p);
4330 t = fold_convert (TREE_TYPE (valist), size_int (-boundary));
4331 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4332 build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t));
4333 gimplify_and_add (t, pre_p);
4335 else
4336 boundary = align;
4338 /* If the actual alignment is less than the alignment of the type,
4339 adjust the type accordingly so that we don't assume strict alignment
4340 when deferencing the pointer. */
4341 boundary *= BITS_PER_UNIT;
4342 if (boundary < TYPE_ALIGN (type))
4344 type = build_variant_type_copy (type);
4345 TYPE_ALIGN (type) = boundary;
4348 /* Compute the rounded size of the type. */
4349 type_size = size_in_bytes (type);
4350 rounded_size = round_up (type_size, align);
4352 /* Reduce rounded_size so it's sharable with the postqueue. */
4353 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4355 /* Get AP. */
4356 addr = valist_tmp;
4357 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4359 /* Small args are padded downward. */
4360 t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align));
4361 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4362 size_binop (MINUS_EXPR, rounded_size, type_size));
4363 t = fold_convert (TREE_TYPE (addr), t);
4364 addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t);
4367 /* Compute new value for AP. */
4368 t = fold_convert (TREE_TYPE (valist), rounded_size);
4369 t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t);
4370 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4371 gimplify_and_add (t, pre_p);
4373 addr = fold_convert (build_pointer_type (type), addr);
4375 if (indirect)
4376 addr = build_va_arg_indirect_ref (addr);
4378 return build_va_arg_indirect_ref (addr);
4381 /* Build an indirect-ref expression over the given TREE, which represents a
4382 piece of a va_arg() expansion. */
4383 tree
4384 build_va_arg_indirect_ref (tree addr)
4386 addr = build_fold_indirect_ref (addr);
4388 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4389 mf_mark (addr);
4391 return addr;
4394 /* Return a dummy expression of type TYPE in order to keep going after an
4395 error. */
4397 static tree
4398 dummy_object (tree type)
4400 tree t = build_int_cst (build_pointer_type (type), 0);
4401 return build1 (INDIRECT_REF, type, t);
4404 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4405 builtin function, but a very special sort of operator. */
4407 enum gimplify_status
4408 gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p)
4410 tree promoted_type, want_va_type, have_va_type;
4411 tree valist = TREE_OPERAND (*expr_p, 0);
4412 tree type = TREE_TYPE (*expr_p);
4413 tree t;
4415 /* Verify that valist is of the proper type. */
4416 want_va_type = va_list_type_node;
4417 have_va_type = TREE_TYPE (valist);
4419 if (have_va_type == error_mark_node)
4420 return GS_ERROR;
4422 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
4424 /* If va_list is an array type, the argument may have decayed
4425 to a pointer type, e.g. by being passed to another function.
4426 In that case, unwrap both types so that we can compare the
4427 underlying records. */
4428 if (TREE_CODE (have_va_type) == ARRAY_TYPE
4429 || POINTER_TYPE_P (have_va_type))
4431 want_va_type = TREE_TYPE (want_va_type);
4432 have_va_type = TREE_TYPE (have_va_type);
4436 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
4438 error ("first argument to %<va_arg%> not of type %<va_list%>");
4439 return GS_ERROR;
4442 /* Generate a diagnostic for requesting data of a type that cannot
4443 be passed through `...' due to type promotion at the call site. */
4444 else if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4445 != type)
4447 static bool gave_help;
4449 /* Unfortunately, this is merely undefined, rather than a constraint
4450 violation, so we cannot make this an error. If this call is never
4451 executed, the program is still strictly conforming. */
4452 warning (0, "%qT is promoted to %qT when passed through %<...%>",
4453 type, promoted_type);
4454 if (! gave_help)
4456 gave_help = true;
4457 warning (0, "(so you should pass %qT not %qT to %<va_arg%>)",
4458 promoted_type, type);
4461 /* We can, however, treat "undefined" any way we please.
4462 Call abort to encourage the user to fix the program. */
4463 inform ("if this code is reached, the program will abort");
4464 t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
4465 NULL);
4466 append_to_statement_list (t, pre_p);
4468 /* This is dead code, but go ahead and finish so that the
4469 mode of the result comes out right. */
4470 *expr_p = dummy_object (type);
4471 return GS_ALL_DONE;
4473 else
4475 /* Make it easier for the backends by protecting the valist argument
4476 from multiple evaluations. */
4477 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4479 /* For this case, the backends will be expecting a pointer to
4480 TREE_TYPE (va_list_type_node), but it's possible we've
4481 actually been given an array (an actual va_list_type_node).
4482 So fix it. */
4483 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4485 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
4486 valist = build_fold_addr_expr_with_type (valist, p1);
4488 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4490 else
4491 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4493 if (!targetm.gimplify_va_arg_expr)
4494 /* FIXME:Once most targets are converted we should merely
4495 assert this is non-null. */
4496 return GS_ALL_DONE;
4498 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4499 return GS_OK;
4503 /* Expand ARGLIST, from a call to __builtin_va_end. */
4505 static rtx
4506 expand_builtin_va_end (tree arglist)
4508 tree valist = TREE_VALUE (arglist);
4510 /* Evaluate for side effects, if needed. I hate macros that don't
4511 do that. */
4512 if (TREE_SIDE_EFFECTS (valist))
4513 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4515 return const0_rtx;
4518 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
4519 builtin rather than just as an assignment in stdarg.h because of the
4520 nastiness of array-type va_list types. */
4522 static rtx
4523 expand_builtin_va_copy (tree arglist)
4525 tree dst, src, t;
4527 dst = TREE_VALUE (arglist);
4528 src = TREE_VALUE (TREE_CHAIN (arglist));
4530 dst = stabilize_va_list (dst, 1);
4531 src = stabilize_va_list (src, 0);
4533 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
4535 t = build2 (MODIFY_EXPR, va_list_type_node, dst, src);
4536 TREE_SIDE_EFFECTS (t) = 1;
4537 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4539 else
4541 rtx dstb, srcb, size;
4543 /* Evaluate to pointers. */
4544 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4545 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4546 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
4547 VOIDmode, EXPAND_NORMAL);
4549 dstb = convert_memory_address (Pmode, dstb);
4550 srcb = convert_memory_address (Pmode, srcb);
4552 /* "Dereference" to BLKmode memories. */
4553 dstb = gen_rtx_MEM (BLKmode, dstb);
4554 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4555 set_mem_align (dstb, TYPE_ALIGN (va_list_type_node));
4556 srcb = gen_rtx_MEM (BLKmode, srcb);
4557 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4558 set_mem_align (srcb, TYPE_ALIGN (va_list_type_node));
4560 /* Copy. */
4561 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4564 return const0_rtx;
4567 /* Expand a call to one of the builtin functions __builtin_frame_address or
4568 __builtin_return_address. */
4570 static rtx
4571 expand_builtin_frame_address (tree fndecl, tree arglist)
4573 /* The argument must be a nonnegative integer constant.
4574 It counts the number of frames to scan up the stack.
4575 The value is the return address saved in that frame. */
4576 if (arglist == 0)
4577 /* Warning about missing arg was already issued. */
4578 return const0_rtx;
4579 else if (! host_integerp (TREE_VALUE (arglist), 1))
4581 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4582 error ("invalid argument to %<__builtin_frame_address%>");
4583 else
4584 error ("invalid argument to %<__builtin_return_address%>");
4585 return const0_rtx;
4587 else
4589 rtx tem
4590 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4591 tree_low_cst (TREE_VALUE (arglist), 1));
4593 /* Some ports cannot access arbitrary stack frames. */
4594 if (tem == NULL)
4596 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4597 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4598 else
4599 warning (0, "unsupported argument to %<__builtin_return_address%>");
4600 return const0_rtx;
4603 /* For __builtin_frame_address, return what we've got. */
4604 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4605 return tem;
4607 if (!REG_P (tem)
4608 && ! CONSTANT_P (tem))
4609 tem = copy_to_mode_reg (Pmode, tem);
4610 return tem;
4614 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
4615 we failed and the caller should emit a normal call, otherwise try to get
4616 the result in TARGET, if convenient. */
4618 static rtx
4619 expand_builtin_alloca (tree arglist, rtx target)
4621 rtx op0;
4622 rtx result;
4624 /* In -fmudflap-instrumented code, alloca() and __builtin_alloca()
4625 should always expand to function calls. These can be intercepted
4626 in libmudflap. */
4627 if (flag_mudflap)
4628 return 0;
4630 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4631 return 0;
4633 /* Compute the argument. */
4634 op0 = expand_normal (TREE_VALUE (arglist));
4636 /* Allocate the desired space. */
4637 result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
4638 result = convert_memory_address (ptr_mode, result);
4640 return result;
4643 /* Expand a call to a unary builtin. The arguments are in ARGLIST.
4644 Return 0 if a normal call should be emitted rather than expanding the
4645 function in-line. If convenient, the result should be placed in TARGET.
4646 SUBTARGET may be used as the target for computing one of EXP's operands. */
4648 static rtx
4649 expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target,
4650 rtx subtarget, optab op_optab)
4652 rtx op0;
4653 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
4654 return 0;
4656 /* Compute the argument. */
4657 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
4658 /* Compute op, into TARGET if possible.
4659 Set TARGET to wherever the result comes back. */
4660 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
4661 op_optab, op0, target, 1);
4662 gcc_assert (target);
4664 return convert_to_mode (target_mode, target, 0);
4667 /* If the string passed to fputs is a constant and is one character
4668 long, we attempt to transform this call into __builtin_fputc(). */
4670 static rtx
4671 expand_builtin_fputs (tree arglist, rtx target, bool unlocked)
4673 /* Verify the arguments in the original call. */
4674 if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4676 tree result = fold_builtin_fputs (arglist, (target == const0_rtx),
4677 unlocked, NULL_TREE);
4678 if (result)
4679 return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
4681 return 0;
4684 /* Expand a call to __builtin_expect. We return our argument and emit a
4685 NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in
4686 a non-jump context. */
4688 static rtx
4689 expand_builtin_expect (tree arglist, rtx target)
4691 tree exp, c;
4692 rtx note, rtx_c;
4694 if (arglist == NULL_TREE
4695 || TREE_CHAIN (arglist) == NULL_TREE)
4696 return const0_rtx;
4697 exp = TREE_VALUE (arglist);
4698 c = TREE_VALUE (TREE_CHAIN (arglist));
4700 if (TREE_CODE (c) != INTEGER_CST)
4702 error ("second argument to %<__builtin_expect%> must be a constant");
4703 c = integer_zero_node;
4706 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
4708 /* Don't bother with expected value notes for integral constants. */
4709 if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT)
4711 /* We do need to force this into a register so that we can be
4712 moderately sure to be able to correctly interpret the branch
4713 condition later. */
4714 target = force_reg (GET_MODE (target), target);
4716 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
4718 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
4719 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
4722 return target;
4725 /* Like expand_builtin_expect, except do this in a jump context. This is
4726 called from do_jump if the conditional is a __builtin_expect. Return either
4727 a list of insns to emit the jump or NULL if we cannot optimize
4728 __builtin_expect. We need to optimize this at jump time so that machines
4729 like the PowerPC don't turn the test into a SCC operation, and then jump
4730 based on the test being 0/1. */
4733 expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label)
4735 tree arglist = TREE_OPERAND (exp, 1);
4736 tree arg0 = TREE_VALUE (arglist);
4737 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4738 rtx ret = NULL_RTX;
4740 /* Only handle __builtin_expect (test, 0) and
4741 __builtin_expect (test, 1). */
4742 if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
4743 && (integer_zerop (arg1) || integer_onep (arg1)))
4745 rtx insn, drop_through_label, temp;
4747 /* Expand the jump insns. */
4748 start_sequence ();
4749 do_jump (arg0, if_false_label, if_true_label);
4750 ret = get_insns ();
4752 drop_through_label = get_last_insn ();
4753 if (drop_through_label && NOTE_P (drop_through_label))
4754 drop_through_label = prev_nonnote_insn (drop_through_label);
4755 if (drop_through_label && !LABEL_P (drop_through_label))
4756 drop_through_label = NULL_RTX;
4757 end_sequence ();
4759 if (! if_true_label)
4760 if_true_label = drop_through_label;
4761 if (! if_false_label)
4762 if_false_label = drop_through_label;
4764 /* Go through and add the expect's to each of the conditional jumps. */
4765 insn = ret;
4766 while (insn != NULL_RTX)
4768 rtx next = NEXT_INSN (insn);
4770 if (JUMP_P (insn) && any_condjump_p (insn))
4772 rtx ifelse = SET_SRC (pc_set (insn));
4773 rtx then_dest = XEXP (ifelse, 1);
4774 rtx else_dest = XEXP (ifelse, 2);
4775 int taken = -1;
4777 /* First check if we recognize any of the labels. */
4778 if (GET_CODE (then_dest) == LABEL_REF
4779 && XEXP (then_dest, 0) == if_true_label)
4780 taken = 1;
4781 else if (GET_CODE (then_dest) == LABEL_REF
4782 && XEXP (then_dest, 0) == if_false_label)
4783 taken = 0;
4784 else if (GET_CODE (else_dest) == LABEL_REF
4785 && XEXP (else_dest, 0) == if_false_label)
4786 taken = 1;
4787 else if (GET_CODE (else_dest) == LABEL_REF
4788 && XEXP (else_dest, 0) == if_true_label)
4789 taken = 0;
4790 /* Otherwise check where we drop through. */
4791 else if (else_dest == pc_rtx)
4793 if (next && NOTE_P (next))
4794 next = next_nonnote_insn (next);
4796 if (next && JUMP_P (next)
4797 && any_uncondjump_p (next))
4798 temp = XEXP (SET_SRC (pc_set (next)), 0);
4799 else
4800 temp = next;
4802 /* TEMP is either a CODE_LABEL, NULL_RTX or something
4803 else that can't possibly match either target label. */
4804 if (temp == if_false_label)
4805 taken = 1;
4806 else if (temp == if_true_label)
4807 taken = 0;
4809 else if (then_dest == pc_rtx)
4811 if (next && NOTE_P (next))
4812 next = next_nonnote_insn (next);
4814 if (next && JUMP_P (next)
4815 && any_uncondjump_p (next))
4816 temp = XEXP (SET_SRC (pc_set (next)), 0);
4817 else
4818 temp = next;
4820 if (temp == if_false_label)
4821 taken = 0;
4822 else if (temp == if_true_label)
4823 taken = 1;
4826 if (taken != -1)
4828 /* If the test is expected to fail, reverse the
4829 probabilities. */
4830 if (integer_zerop (arg1))
4831 taken = 1 - taken;
4832 predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken);
4836 insn = next;
4840 return ret;
4843 void
4844 expand_builtin_trap (void)
4846 #ifdef HAVE_trap
4847 if (HAVE_trap)
4848 emit_insn (gen_trap ());
4849 else
4850 #endif
4851 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4852 emit_barrier ();
4855 /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST.
4856 Return 0 if a normal call should be emitted rather than expanding
4857 the function inline. If convenient, the result should be placed
4858 in TARGET. SUBTARGET may be used as the target for computing
4859 the operand. */
4861 static rtx
4862 expand_builtin_fabs (tree arglist, rtx target, rtx subtarget)
4864 enum machine_mode mode;
4865 tree arg;
4866 rtx op0;
4868 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
4869 return 0;
4871 arg = TREE_VALUE (arglist);
4872 mode = TYPE_MODE (TREE_TYPE (arg));
4873 op0 = expand_expr (arg, subtarget, VOIDmode, 0);
4874 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4877 /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST.
4878 Return NULL is a normal call should be emitted rather than expanding the
4879 function inline. If convenient, the result should be placed in TARGET.
4880 SUBTARGET may be used as the target for computing the operand. */
4882 static rtx
4883 expand_builtin_copysign (tree arglist, rtx target, rtx subtarget)
4885 rtx op0, op1;
4886 tree arg;
4888 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4889 return 0;
4891 arg = TREE_VALUE (arglist);
4892 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4894 arg = TREE_VALUE (TREE_CHAIN (arglist));
4895 op1 = expand_normal (arg);
4897 return expand_copysign (op0, op1, target);
4900 /* Create a new constant string literal and return a char* pointer to it.
4901 The STRING_CST value is the LEN characters at STR. */
4902 tree
4903 build_string_literal (int len, const char *str)
4905 tree t, elem, index, type;
4907 t = build_string (len, str);
4908 elem = build_type_variant (char_type_node, 1, 0);
4909 index = build_index_type (build_int_cst (NULL_TREE, len - 1));
4910 type = build_array_type (elem, index);
4911 TREE_TYPE (t) = type;
4912 TREE_CONSTANT (t) = 1;
4913 TREE_INVARIANT (t) = 1;
4914 TREE_READONLY (t) = 1;
4915 TREE_STATIC (t) = 1;
4917 type = build_pointer_type (type);
4918 t = build1 (ADDR_EXPR, type, t);
4920 type = build_pointer_type (elem);
4921 t = build1 (NOP_EXPR, type, t);
4922 return t;
4925 /* Expand EXP, a call to printf or printf_unlocked.
4926 Return 0 if a normal call should be emitted rather than transforming
4927 the function inline. If convenient, the result should be placed in
4928 TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked
4929 call. */
4930 static rtx
4931 expand_builtin_printf (tree exp, rtx target, enum machine_mode mode,
4932 bool unlocked)
4934 tree arglist = TREE_OPERAND (exp, 1);
4935 /* If we're using an unlocked function, assume the other unlocked
4936 functions exist explicitly. */
4937 tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]
4938 : implicit_built_in_decls[BUILT_IN_PUTCHAR];
4939 tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED]
4940 : implicit_built_in_decls[BUILT_IN_PUTS];
4941 const char *fmt_str;
4942 tree fn, fmt, arg;
4944 /* If the return value is used, don't do the transformation. */
4945 if (target != const0_rtx)
4946 return 0;
4948 /* Verify the required arguments in the original call. */
4949 if (! arglist)
4950 return 0;
4951 fmt = TREE_VALUE (arglist);
4952 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
4953 return 0;
4954 arglist = TREE_CHAIN (arglist);
4956 /* Check whether the format is a literal string constant. */
4957 fmt_str = c_getstr (fmt);
4958 if (fmt_str == NULL)
4959 return 0;
4961 if (!init_target_chars())
4962 return 0;
4964 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4965 if (strcmp (fmt_str, target_percent_s_newline) == 0)
4967 if (! arglist
4968 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
4969 || TREE_CHAIN (arglist))
4970 return 0;
4971 fn = fn_puts;
4973 /* If the format specifier was "%c", call __builtin_putchar(arg). */
4974 else if (strcmp (fmt_str, target_percent_c) == 0)
4976 if (! arglist
4977 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
4978 || TREE_CHAIN (arglist))
4979 return 0;
4980 fn = fn_putchar;
4982 else
4984 /* We can't handle anything else with % args or %% ... yet. */
4985 if (strchr (fmt_str, target_percent))
4986 return 0;
4988 if (arglist)
4989 return 0;
4991 /* If the format specifier was "", printf does nothing. */
4992 if (fmt_str[0] == '\0')
4993 return const0_rtx;
4994 /* If the format specifier has length of 1, call putchar. */
4995 if (fmt_str[1] == '\0')
4997 /* Given printf("c"), (where c is any one character,)
4998 convert "c"[0] to an int and pass that to the replacement
4999 function. */
5000 arg = build_int_cst (NULL_TREE, fmt_str[0]);
5001 arglist = build_tree_list (NULL_TREE, arg);
5002 fn = fn_putchar;
5004 else
5006 /* If the format specifier was "string\n", call puts("string"). */
5007 size_t len = strlen (fmt_str);
5008 if ((unsigned char)fmt_str[len - 1] == target_newline)
5010 /* Create a NUL-terminated string that's one char shorter
5011 than the original, stripping off the trailing '\n'. */
5012 char *newstr = alloca (len);
5013 memcpy (newstr, fmt_str, len - 1);
5014 newstr[len - 1] = 0;
5016 arg = build_string_literal (len, newstr);
5017 arglist = build_tree_list (NULL_TREE, arg);
5018 fn = fn_puts;
5020 else
5021 /* We'd like to arrange to call fputs(string,stdout) here,
5022 but we need stdout and don't have a way to get it yet. */
5023 return 0;
5027 if (!fn)
5028 return 0;
5029 fn = build_function_call_expr (fn, arglist);
5030 if (TREE_CODE (fn) == CALL_EXPR)
5031 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5032 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5035 /* Expand EXP, a call to fprintf or fprintf_unlocked.
5036 Return 0 if a normal call should be emitted rather than transforming
5037 the function inline. If convenient, the result should be placed in
5038 TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked
5039 call. */
5040 static rtx
5041 expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode,
5042 bool unlocked)
5044 tree arglist = TREE_OPERAND (exp, 1);
5045 /* If we're using an unlocked function, assume the other unlocked
5046 functions exist explicitly. */
5047 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
5048 : implicit_built_in_decls[BUILT_IN_FPUTC];
5049 tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED]
5050 : implicit_built_in_decls[BUILT_IN_FPUTS];
5051 const char *fmt_str;
5052 tree fn, fmt, fp, arg;
5054 /* If the return value is used, don't do the transformation. */
5055 if (target != const0_rtx)
5056 return 0;
5058 /* Verify the required arguments in the original call. */
5059 if (! arglist)
5060 return 0;
5061 fp = TREE_VALUE (arglist);
5062 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
5063 return 0;
5064 arglist = TREE_CHAIN (arglist);
5065 if (! arglist)
5066 return 0;
5067 fmt = TREE_VALUE (arglist);
5068 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5069 return 0;
5070 arglist = TREE_CHAIN (arglist);
5072 /* Check whether the format is a literal string constant. */
5073 fmt_str = c_getstr (fmt);
5074 if (fmt_str == NULL)
5075 return 0;
5077 if (!init_target_chars())
5078 return 0;
5080 /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */
5081 if (strcmp (fmt_str, target_percent_s) == 0)
5083 if (! arglist
5084 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
5085 || TREE_CHAIN (arglist))
5086 return 0;
5087 arg = TREE_VALUE (arglist);
5088 arglist = build_tree_list (NULL_TREE, fp);
5089 arglist = tree_cons (NULL_TREE, arg, arglist);
5090 fn = fn_fputs;
5092 /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */
5093 else if (strcmp (fmt_str, target_percent_c) == 0)
5095 if (! arglist
5096 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
5097 || TREE_CHAIN (arglist))
5098 return 0;
5099 arg = TREE_VALUE (arglist);
5100 arglist = build_tree_list (NULL_TREE, fp);
5101 arglist = tree_cons (NULL_TREE, arg, arglist);
5102 fn = fn_fputc;
5104 else
5106 /* We can't handle anything else with % args or %% ... yet. */
5107 if (strchr (fmt_str, target_percent))
5108 return 0;
5110 if (arglist)
5111 return 0;
5113 /* If the format specifier was "", fprintf does nothing. */
5114 if (fmt_str[0] == '\0')
5116 /* Evaluate and ignore FILE* argument for side-effects. */
5117 expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5118 return const0_rtx;
5121 /* When "string" doesn't contain %, replace all cases of
5122 fprintf(stream,string) with fputs(string,stream). The fputs
5123 builtin will take care of special cases like length == 1. */
5124 arglist = build_tree_list (NULL_TREE, fp);
5125 arglist = tree_cons (NULL_TREE, fmt, arglist);
5126 fn = fn_fputs;
5129 if (!fn)
5130 return 0;
5131 fn = build_function_call_expr (fn, arglist);
5132 if (TREE_CODE (fn) == CALL_EXPR)
5133 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
5134 return expand_expr (fn, target, mode, EXPAND_NORMAL);
5137 /* Expand a call to sprintf with argument list ARGLIST. Return 0 if
5138 a normal call should be emitted rather than expanding the function
5139 inline. If convenient, the result should be placed in TARGET with
5140 mode MODE. */
5142 static rtx
5143 expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode)
5145 tree orig_arglist, dest, fmt;
5146 const char *fmt_str;
5148 orig_arglist = arglist;
5150 /* Verify the required arguments in the original call. */
5151 if (! arglist)
5152 return 0;
5153 dest = TREE_VALUE (arglist);
5154 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
5155 return 0;
5156 arglist = TREE_CHAIN (arglist);
5157 if (! arglist)
5158 return 0;
5159 fmt = TREE_VALUE (arglist);
5160 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
5161 return 0;
5162 arglist = TREE_CHAIN (arglist);
5164 /* Check whether the format is a literal string constant. */
5165 fmt_str = c_getstr (fmt);
5166 if (fmt_str == NULL)
5167 return 0;
5169 if (!init_target_chars())
5170 return 0;
5172 /* If the format doesn't contain % args or %%, use strcpy. */
5173 if (strchr (fmt_str, target_percent) == 0)
5175 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5176 tree exp;
5178 if (arglist || ! fn)
5179 return 0;
5180 expand_expr (build_function_call_expr (fn, orig_arglist),
5181 const0_rtx, VOIDmode, EXPAND_NORMAL);
5182 if (target == const0_rtx)
5183 return const0_rtx;
5184 exp = build_int_cst (NULL_TREE, strlen (fmt_str));
5185 return expand_expr (exp, target, mode, EXPAND_NORMAL);
5187 /* If the format is "%s", use strcpy if the result isn't used. */
5188 else if (strcmp (fmt_str, target_percent_s) == 0)
5190 tree fn, arg, len;
5191 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
5193 if (! fn)
5194 return 0;
5196 if (! arglist || TREE_CHAIN (arglist))
5197 return 0;
5198 arg = TREE_VALUE (arglist);
5199 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
5200 return 0;
5202 if (target != const0_rtx)
5204 len = c_strlen (arg, 1);
5205 if (! len || TREE_CODE (len) != INTEGER_CST)
5206 return 0;
5208 else
5209 len = NULL_TREE;
5211 arglist = build_tree_list (NULL_TREE, arg);
5212 arglist = tree_cons (NULL_TREE, dest, arglist);
5213 expand_expr (build_function_call_expr (fn, arglist),
5214 const0_rtx, VOIDmode, EXPAND_NORMAL);
5216 if (target == const0_rtx)
5217 return const0_rtx;
5218 return expand_expr (len, target, mode, EXPAND_NORMAL);
5221 return 0;
5224 /* Expand a call to either the entry or exit function profiler. */
5226 static rtx
5227 expand_builtin_profile_func (bool exitp)
5229 rtx this, which;
5231 this = DECL_RTL (current_function_decl);
5232 gcc_assert (MEM_P (this));
5233 this = XEXP (this, 0);
5235 if (exitp)
5236 which = profile_function_exit_libfunc;
5237 else
5238 which = profile_function_entry_libfunc;
5240 emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode,
5241 expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
5243 Pmode);
5245 return const0_rtx;
5248 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5250 static rtx
5251 round_trampoline_addr (rtx tramp)
5253 rtx temp, addend, mask;
5255 /* If we don't need too much alignment, we'll have been guaranteed
5256 proper alignment by get_trampoline_type. */
5257 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5258 return tramp;
5260 /* Round address up to desired boundary. */
5261 temp = gen_reg_rtx (Pmode);
5262 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
5263 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5265 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5266 temp, 0, OPTAB_LIB_WIDEN);
5267 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5268 temp, 0, OPTAB_LIB_WIDEN);
5270 return tramp;
5273 static rtx
5274 expand_builtin_init_trampoline (tree arglist)
5276 tree t_tramp, t_func, t_chain;
5277 rtx r_tramp, r_func, r_chain;
5278 #ifdef TRAMPOLINE_TEMPLATE
5279 rtx blktramp;
5280 #endif
5282 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE,
5283 POINTER_TYPE, VOID_TYPE))
5284 return NULL_RTX;
5286 t_tramp = TREE_VALUE (arglist);
5287 arglist = TREE_CHAIN (arglist);
5288 t_func = TREE_VALUE (arglist);
5289 arglist = TREE_CHAIN (arglist);
5290 t_chain = TREE_VALUE (arglist);
5292 r_tramp = expand_normal (t_tramp);
5293 r_func = expand_normal (t_func);
5294 r_chain = expand_normal (t_chain);
5296 /* Generate insns to initialize the trampoline. */
5297 r_tramp = round_trampoline_addr (r_tramp);
5298 #ifdef TRAMPOLINE_TEMPLATE
5299 blktramp = gen_rtx_MEM (BLKmode, r_tramp);
5300 set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT);
5301 emit_block_move (blktramp, assemble_trampoline_template (),
5302 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
5303 #endif
5304 trampolines_created = 1;
5305 INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain);
5307 return const0_rtx;
5310 static rtx
5311 expand_builtin_adjust_trampoline (tree arglist)
5313 rtx tramp;
5315 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5316 return NULL_RTX;
5318 tramp = expand_normal (TREE_VALUE (arglist));
5319 tramp = round_trampoline_addr (tramp);
5320 #ifdef TRAMPOLINE_ADJUST_ADDRESS
5321 TRAMPOLINE_ADJUST_ADDRESS (tramp);
5322 #endif
5324 return tramp;
5327 /* Expand a call to the built-in signbit, signbitf or signbitl function.
5328 Return NULL_RTX if a normal call should be emitted rather than expanding
5329 the function in-line. EXP is the expression that is a call to the builtin
5330 function; if convenient, the result should be placed in TARGET. */
5332 static rtx
5333 expand_builtin_signbit (tree exp, rtx target)
5335 const struct real_format *fmt;
5336 enum machine_mode fmode, imode, rmode;
5337 HOST_WIDE_INT hi, lo;
5338 tree arg, arglist;
5339 int word, bitpos;
5340 rtx temp;
5342 arglist = TREE_OPERAND (exp, 1);
5343 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
5344 return 0;
5346 arg = TREE_VALUE (arglist);
5347 fmode = TYPE_MODE (TREE_TYPE (arg));
5348 rmode = TYPE_MODE (TREE_TYPE (exp));
5349 fmt = REAL_MODE_FORMAT (fmode);
5351 /* For floating point formats without a sign bit, implement signbit
5352 as "ARG < 0.0". */
5353 bitpos = fmt->signbit_ro;
5354 if (bitpos < 0)
5356 /* But we can't do this if the format supports signed zero. */
5357 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5358 return 0;
5360 arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg,
5361 build_real (TREE_TYPE (arg), dconst0));
5362 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5365 temp = expand_normal (arg);
5366 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5368 imode = int_mode_for_mode (fmode);
5369 if (imode == BLKmode)
5370 return 0;
5371 temp = gen_lowpart (imode, temp);
5373 else
5375 imode = word_mode;
5376 /* Handle targets with different FP word orders. */
5377 if (FLOAT_WORDS_BIG_ENDIAN)
5378 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5379 else
5380 word = bitpos / BITS_PER_WORD;
5381 temp = operand_subword_force (temp, word, fmode);
5382 bitpos = bitpos % BITS_PER_WORD;
5385 /* Force the intermediate word_mode (or narrower) result into a
5386 register. This avoids attempting to create paradoxical SUBREGs
5387 of floating point modes below. */
5388 temp = force_reg (imode, temp);
5390 /* If the bitpos is within the "result mode" lowpart, the operation
5391 can be implement with a single bitwise AND. Otherwise, we need
5392 a right shift and an AND. */
5394 if (bitpos < GET_MODE_BITSIZE (rmode))
5396 if (bitpos < HOST_BITS_PER_WIDE_INT)
5398 hi = 0;
5399 lo = (HOST_WIDE_INT) 1 << bitpos;
5401 else
5403 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
5404 lo = 0;
5407 if (imode != rmode)
5408 temp = gen_lowpart (rmode, temp);
5409 temp = expand_binop (rmode, and_optab, temp,
5410 immed_double_const (lo, hi, rmode),
5411 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5413 else
5415 /* Perform a logical right shift to place the signbit in the least
5416 significant bit, then truncate the result to the desired mode
5417 and mask just this bit. */
5418 temp = expand_shift (RSHIFT_EXPR, imode, temp,
5419 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
5420 temp = gen_lowpart (rmode, temp);
5421 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5422 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5425 return temp;
5428 /* Expand fork or exec calls. TARGET is the desired target of the
5429 call. ARGLIST is the list of arguments of the call. FN is the
5430 identificator of the actual function. IGNORE is nonzero if the
5431 value is to be ignored. */
5433 static rtx
5434 expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore)
5436 tree id, decl;
5437 tree call;
5439 /* If we are not profiling, just call the function. */
5440 if (!profile_arc_flag)
5441 return NULL_RTX;
5443 /* Otherwise call the wrapper. This should be equivalent for the rest of
5444 compiler, so the code does not diverge, and the wrapper may run the
5445 code necessary for keeping the profiling sane. */
5447 switch (DECL_FUNCTION_CODE (fn))
5449 case BUILT_IN_FORK:
5450 id = get_identifier ("__gcov_fork");
5451 break;
5453 case BUILT_IN_EXECL:
5454 id = get_identifier ("__gcov_execl");
5455 break;
5457 case BUILT_IN_EXECV:
5458 id = get_identifier ("__gcov_execv");
5459 break;
5461 case BUILT_IN_EXECLP:
5462 id = get_identifier ("__gcov_execlp");
5463 break;
5465 case BUILT_IN_EXECLE:
5466 id = get_identifier ("__gcov_execle");
5467 break;
5469 case BUILT_IN_EXECVP:
5470 id = get_identifier ("__gcov_execvp");
5471 break;
5473 case BUILT_IN_EXECVE:
5474 id = get_identifier ("__gcov_execve");
5475 break;
5477 default:
5478 gcc_unreachable ();
5481 decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn));
5482 DECL_EXTERNAL (decl) = 1;
5483 TREE_PUBLIC (decl) = 1;
5484 DECL_ARTIFICIAL (decl) = 1;
5485 TREE_NOTHROW (decl) = 1;
5486 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5487 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5488 call = build_function_call_expr (decl, arglist);
5490 return expand_call (call, target, ignore);
5494 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5495 the pointer in these functions is void*, the tree optimizers may remove
5496 casts. The mode computed in expand_builtin isn't reliable either, due
5497 to __sync_bool_compare_and_swap.
5499 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5500 group of builtins. This gives us log2 of the mode size. */
5502 static inline enum machine_mode
5503 get_builtin_sync_mode (int fcode_diff)
5505 /* The size is not negotiable, so ask not to get BLKmode in return
5506 if the target indicates that a smaller size would be better. */
5507 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5510 /* Expand the memory expression LOC and return the appropriate memory operand
5511 for the builtin_sync operations. */
5513 static rtx
5514 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5516 rtx addr, mem;
5518 addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM);
5520 /* Note that we explicitly do not want any alias information for this
5521 memory, so that we kill all other live memories. Otherwise we don't
5522 satisfy the full barrier semantics of the intrinsic. */
5523 mem = validize_mem (gen_rtx_MEM (mode, addr));
5525 set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT));
5526 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5527 MEM_VOLATILE_P (mem) = 1;
5529 return mem;
5532 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5533 ARGLIST is the operands list to the function. CODE is the rtx code
5534 that corresponds to the arithmetic or logical operation from the name;
5535 an exception here is that NOT actually means NAND. TARGET is an optional
5536 place for us to store the results; AFTER is true if this is the
5537 fetch_and_xxx form. IGNORE is true if we don't actually care about
5538 the result of the operation at all. */
5540 static rtx
5541 expand_builtin_sync_operation (enum machine_mode mode, tree arglist,
5542 enum rtx_code code, bool after,
5543 rtx target, bool ignore)
5545 rtx val, mem;
5547 /* Expand the operands. */
5548 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5550 arglist = TREE_CHAIN (arglist);
5551 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5553 if (ignore)
5554 return expand_sync_operation (mem, val, code);
5555 else
5556 return expand_sync_fetch_operation (mem, val, code, after, target);
5559 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5560 intrinsics. ARGLIST is the operands list to the function. IS_BOOL is
5561 true if this is the boolean form. TARGET is a place for us to store the
5562 results; this is NOT optional if IS_BOOL is true. */
5564 static rtx
5565 expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist,
5566 bool is_bool, rtx target)
5568 rtx old_val, new_val, mem;
5570 /* Expand the operands. */
5571 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5573 arglist = TREE_CHAIN (arglist);
5574 old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5576 arglist = TREE_CHAIN (arglist);
5577 new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5579 if (is_bool)
5580 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5581 else
5582 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5585 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5586 general form is actually an atomic exchange, and some targets only
5587 support a reduced form with the second argument being a constant 1.
5588 ARGLIST is the operands list to the function; TARGET is an optional
5589 place for us to store the results. */
5591 static rtx
5592 expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist,
5593 rtx target)
5595 rtx val, mem;
5597 /* Expand the operands. */
5598 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5600 arglist = TREE_CHAIN (arglist);
5601 val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL);
5603 return expand_sync_lock_test_and_set (mem, val, target);
5606 /* Expand the __sync_synchronize intrinsic. */
5608 static void
5609 expand_builtin_synchronize (void)
5611 tree x;
5613 #ifdef HAVE_memory_barrier
5614 if (HAVE_memory_barrier)
5616 emit_insn (gen_memory_barrier ());
5617 return;
5619 #endif
5621 /* If no explicit memory barrier instruction is available, create an
5622 empty asm stmt with a memory clobber. */
5623 x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL,
5624 tree_cons (NULL, build_string (6, "memory"), NULL));
5625 ASM_VOLATILE_P (x) = 1;
5626 expand_asm_expr (x);
5629 /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list
5630 to the function. */
5632 static void
5633 expand_builtin_lock_release (enum machine_mode mode, tree arglist)
5635 enum insn_code icode;
5636 rtx mem, insn;
5637 rtx val = const0_rtx;
5639 /* Expand the operands. */
5640 mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode);
5642 /* If there is an explicit operation in the md file, use it. */
5643 icode = sync_lock_release[mode];
5644 if (icode != CODE_FOR_nothing)
5646 if (!insn_data[icode].operand[1].predicate (val, mode))
5647 val = force_reg (mode, val);
5649 insn = GEN_FCN (icode) (mem, val);
5650 if (insn)
5652 emit_insn (insn);
5653 return;
5657 /* Otherwise we can implement this operation by emitting a barrier
5658 followed by a store of zero. */
5659 expand_builtin_synchronize ();
5660 emit_move_insn (mem, val);
5663 /* Expand an expression EXP that calls a built-in function,
5664 with result going to TARGET if that's convenient
5665 (and in mode MODE if that's convenient).
5666 SUBTARGET may be used as the target for computing one of EXP's operands.
5667 IGNORE is nonzero if the value is to be ignored. */
5670 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5671 int ignore)
5673 tree fndecl = get_callee_fndecl (exp);
5674 tree arglist = TREE_OPERAND (exp, 1);
5675 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5676 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5678 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5679 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5681 /* When not optimizing, generate calls to library functions for a certain
5682 set of builtins. */
5683 if (!optimize
5684 && !called_as_built_in (fndecl)
5685 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5686 && fcode != BUILT_IN_ALLOCA)
5687 return expand_call (exp, target, ignore);
5689 /* The built-in function expanders test for target == const0_rtx
5690 to determine whether the function's result will be ignored. */
5691 if (ignore)
5692 target = const0_rtx;
5694 /* If the result of a pure or const built-in function is ignored, and
5695 none of its arguments are volatile, we can avoid expanding the
5696 built-in call and just evaluate the arguments for side-effects. */
5697 if (target == const0_rtx
5698 && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl)))
5700 bool volatilep = false;
5701 tree arg;
5703 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5704 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
5706 volatilep = true;
5707 break;
5710 if (! volatilep)
5712 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
5713 expand_expr (TREE_VALUE (arg), const0_rtx,
5714 VOIDmode, EXPAND_NORMAL);
5715 return const0_rtx;
5719 switch (fcode)
5721 CASE_FLT_FN (BUILT_IN_FABS):
5722 target = expand_builtin_fabs (arglist, target, subtarget);
5723 if (target)
5724 return target;
5725 break;
5727 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5728 target = expand_builtin_copysign (arglist, target, subtarget);
5729 if (target)
5730 return target;
5731 break;
5733 /* Just do a normal library call if we were unable to fold
5734 the values. */
5735 CASE_FLT_FN (BUILT_IN_CABS):
5736 break;
5738 CASE_FLT_FN (BUILT_IN_EXP):
5739 CASE_FLT_FN (BUILT_IN_EXP10):
5740 CASE_FLT_FN (BUILT_IN_POW10):
5741 CASE_FLT_FN (BUILT_IN_EXP2):
5742 CASE_FLT_FN (BUILT_IN_EXPM1):
5743 CASE_FLT_FN (BUILT_IN_LOGB):
5744 CASE_FLT_FN (BUILT_IN_ILOGB):
5745 CASE_FLT_FN (BUILT_IN_LOG):
5746 CASE_FLT_FN (BUILT_IN_LOG10):
5747 CASE_FLT_FN (BUILT_IN_LOG2):
5748 CASE_FLT_FN (BUILT_IN_LOG1P):
5749 CASE_FLT_FN (BUILT_IN_TAN):
5750 CASE_FLT_FN (BUILT_IN_ASIN):
5751 CASE_FLT_FN (BUILT_IN_ACOS):
5752 CASE_FLT_FN (BUILT_IN_ATAN):
5753 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5754 because of possible accuracy problems. */
5755 if (! flag_unsafe_math_optimizations)
5756 break;
5757 CASE_FLT_FN (BUILT_IN_SQRT):
5758 CASE_FLT_FN (BUILT_IN_FLOOR):
5759 CASE_FLT_FN (BUILT_IN_CEIL):
5760 CASE_FLT_FN (BUILT_IN_TRUNC):
5761 CASE_FLT_FN (BUILT_IN_ROUND):
5762 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5763 CASE_FLT_FN (BUILT_IN_RINT):
5764 CASE_FLT_FN (BUILT_IN_LRINT):
5765 CASE_FLT_FN (BUILT_IN_LLRINT):
5766 target = expand_builtin_mathfn (exp, target, subtarget);
5767 if (target)
5768 return target;
5769 break;
5771 CASE_FLT_FN (BUILT_IN_LCEIL):
5772 CASE_FLT_FN (BUILT_IN_LLCEIL):
5773 CASE_FLT_FN (BUILT_IN_LFLOOR):
5774 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5775 target = expand_builtin_int_roundingfn (exp, target, subtarget);
5776 if (target)
5777 return target;
5778 break;
5780 CASE_FLT_FN (BUILT_IN_POW):
5781 target = expand_builtin_pow (exp, target, subtarget);
5782 if (target)
5783 return target;
5784 break;
5786 CASE_FLT_FN (BUILT_IN_POWI):
5787 target = expand_builtin_powi (exp, target, subtarget);
5788 if (target)
5789 return target;
5790 break;
5792 CASE_FLT_FN (BUILT_IN_ATAN2):
5793 CASE_FLT_FN (BUILT_IN_LDEXP):
5794 CASE_FLT_FN (BUILT_IN_FMOD):
5795 CASE_FLT_FN (BUILT_IN_DREM):
5796 if (! flag_unsafe_math_optimizations)
5797 break;
5798 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5799 if (target)
5800 return target;
5801 break;
5803 CASE_FLT_FN (BUILT_IN_SIN):
5804 CASE_FLT_FN (BUILT_IN_COS):
5805 if (! flag_unsafe_math_optimizations)
5806 break;
5807 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5808 if (target)
5809 return target;
5810 break;
5812 CASE_FLT_FN (BUILT_IN_SINCOS):
5813 if (! flag_unsafe_math_optimizations)
5814 break;
5815 target = expand_builtin_sincos (exp);
5816 if (target)
5817 return target;
5818 break;
5820 case BUILT_IN_APPLY_ARGS:
5821 return expand_builtin_apply_args ();
5823 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5824 FUNCTION with a copy of the parameters described by
5825 ARGUMENTS, and ARGSIZE. It returns a block of memory
5826 allocated on the stack into which is stored all the registers
5827 that might possibly be used for returning the result of a
5828 function. ARGUMENTS is the value returned by
5829 __builtin_apply_args. ARGSIZE is the number of bytes of
5830 arguments that must be copied. ??? How should this value be
5831 computed? We'll also need a safe worst case value for varargs
5832 functions. */
5833 case BUILT_IN_APPLY:
5834 if (!validate_arglist (arglist, POINTER_TYPE,
5835 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5836 && !validate_arglist (arglist, REFERENCE_TYPE,
5837 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5838 return const0_rtx;
5839 else
5841 int i;
5842 tree t;
5843 rtx ops[3];
5845 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
5846 ops[i] = expand_normal (TREE_VALUE (t));
5848 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5851 /* __builtin_return (RESULT) causes the function to return the
5852 value described by RESULT. RESULT is address of the block of
5853 memory returned by __builtin_apply. */
5854 case BUILT_IN_RETURN:
5855 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
5856 expand_builtin_return (expand_normal (TREE_VALUE (arglist)));
5857 return const0_rtx;
5859 case BUILT_IN_SAVEREGS:
5860 return expand_builtin_saveregs ();
5862 case BUILT_IN_ARGS_INFO:
5863 return expand_builtin_args_info (arglist);
5865 /* Return the address of the first anonymous stack arg. */
5866 case BUILT_IN_NEXT_ARG:
5867 if (fold_builtin_next_arg (arglist))
5868 return const0_rtx;
5869 return expand_builtin_next_arg ();
5871 case BUILT_IN_CLASSIFY_TYPE:
5872 return expand_builtin_classify_type (arglist);
5874 case BUILT_IN_CONSTANT_P:
5875 return const0_rtx;
5877 case BUILT_IN_FRAME_ADDRESS:
5878 case BUILT_IN_RETURN_ADDRESS:
5879 return expand_builtin_frame_address (fndecl, arglist);
5881 /* Returns the address of the area where the structure is returned.
5882 0 otherwise. */
5883 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5884 if (arglist != 0
5885 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5886 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5887 return const0_rtx;
5888 else
5889 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5891 case BUILT_IN_ALLOCA:
5892 target = expand_builtin_alloca (arglist, target);
5893 if (target)
5894 return target;
5895 break;
5897 case BUILT_IN_STACK_SAVE:
5898 return expand_stack_save ();
5900 case BUILT_IN_STACK_RESTORE:
5901 expand_stack_restore (TREE_VALUE (arglist));
5902 return const0_rtx;
5904 CASE_INT_FN (BUILT_IN_FFS):
5905 case BUILT_IN_FFSIMAX:
5906 target = expand_builtin_unop (target_mode, arglist, target,
5907 subtarget, ffs_optab);
5908 if (target)
5909 return target;
5910 break;
5912 CASE_INT_FN (BUILT_IN_CLZ):
5913 case BUILT_IN_CLZIMAX:
5914 target = expand_builtin_unop (target_mode, arglist, target,
5915 subtarget, clz_optab);
5916 if (target)
5917 return target;
5918 break;
5920 CASE_INT_FN (BUILT_IN_CTZ):
5921 case BUILT_IN_CTZIMAX:
5922 target = expand_builtin_unop (target_mode, arglist, target,
5923 subtarget, ctz_optab);
5924 if (target)
5925 return target;
5926 break;
5928 CASE_INT_FN (BUILT_IN_POPCOUNT):
5929 case BUILT_IN_POPCOUNTIMAX:
5930 target = expand_builtin_unop (target_mode, arglist, target,
5931 subtarget, popcount_optab);
5932 if (target)
5933 return target;
5934 break;
5936 CASE_INT_FN (BUILT_IN_PARITY):
5937 case BUILT_IN_PARITYIMAX:
5938 target = expand_builtin_unop (target_mode, arglist, target,
5939 subtarget, parity_optab);
5940 if (target)
5941 return target;
5942 break;
5944 case BUILT_IN_STRLEN:
5945 target = expand_builtin_strlen (arglist, target, target_mode);
5946 if (target)
5947 return target;
5948 break;
5950 case BUILT_IN_STRCPY:
5951 target = expand_builtin_strcpy (fndecl, arglist, target, mode);
5952 if (target)
5953 return target;
5954 break;
5956 case BUILT_IN_STRNCPY:
5957 target = expand_builtin_strncpy (exp, target, mode);
5958 if (target)
5959 return target;
5960 break;
5962 case BUILT_IN_STPCPY:
5963 target = expand_builtin_stpcpy (exp, target, mode);
5964 if (target)
5965 return target;
5966 break;
5968 case BUILT_IN_STRCAT:
5969 target = expand_builtin_strcat (fndecl, arglist, target, mode);
5970 if (target)
5971 return target;
5972 break;
5974 case BUILT_IN_STRNCAT:
5975 target = expand_builtin_strncat (arglist, target, mode);
5976 if (target)
5977 return target;
5978 break;
5980 case BUILT_IN_STRSPN:
5981 target = expand_builtin_strspn (arglist, target, mode);
5982 if (target)
5983 return target;
5984 break;
5986 case BUILT_IN_STRCSPN:
5987 target = expand_builtin_strcspn (arglist, target, mode);
5988 if (target)
5989 return target;
5990 break;
5992 case BUILT_IN_STRSTR:
5993 target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode);
5994 if (target)
5995 return target;
5996 break;
5998 case BUILT_IN_STRPBRK:
5999 target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode);
6000 if (target)
6001 return target;
6002 break;
6004 case BUILT_IN_INDEX:
6005 case BUILT_IN_STRCHR:
6006 target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode);
6007 if (target)
6008 return target;
6009 break;
6011 case BUILT_IN_RINDEX:
6012 case BUILT_IN_STRRCHR:
6013 target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode);
6014 if (target)
6015 return target;
6016 break;
6018 case BUILT_IN_MEMCPY:
6019 target = expand_builtin_memcpy (exp, target, mode);
6020 if (target)
6021 return target;
6022 break;
6024 case BUILT_IN_MEMPCPY:
6025 target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
6026 if (target)
6027 return target;
6028 break;
6030 case BUILT_IN_MEMMOVE:
6031 target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target,
6032 mode, exp);
6033 if (target)
6034 return target;
6035 break;
6037 case BUILT_IN_BCOPY:
6038 target = expand_builtin_bcopy (exp);
6039 if (target)
6040 return target;
6041 break;
6043 case BUILT_IN_MEMSET:
6044 target = expand_builtin_memset (arglist, target, mode, exp);
6045 if (target)
6046 return target;
6047 break;
6049 case BUILT_IN_BZERO:
6050 target = expand_builtin_bzero (exp);
6051 if (target)
6052 return target;
6053 break;
6055 case BUILT_IN_STRCMP:
6056 target = expand_builtin_strcmp (exp, target, mode);
6057 if (target)
6058 return target;
6059 break;
6061 case BUILT_IN_STRNCMP:
6062 target = expand_builtin_strncmp (exp, target, mode);
6063 if (target)
6064 return target;
6065 break;
6067 case BUILT_IN_BCMP:
6068 case BUILT_IN_MEMCMP:
6069 target = expand_builtin_memcmp (exp, arglist, target, mode);
6070 if (target)
6071 return target;
6072 break;
6074 case BUILT_IN_SETJMP:
6075 target = expand_builtin_setjmp (arglist, target);
6076 if (target)
6077 return target;
6078 break;
6080 /* __builtin_longjmp is passed a pointer to an array of five words.
6081 It's similar to the C library longjmp function but works with
6082 __builtin_setjmp above. */
6083 case BUILT_IN_LONGJMP:
6084 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6085 break;
6086 else
6088 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
6089 VOIDmode, EXPAND_NORMAL);
6090 rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist)));
6092 if (value != const1_rtx)
6094 error ("%<__builtin_longjmp%> second argument must be 1");
6095 return const0_rtx;
6098 expand_builtin_longjmp (buf_addr, value);
6099 return const0_rtx;
6102 case BUILT_IN_NONLOCAL_GOTO:
6103 target = expand_builtin_nonlocal_goto (arglist);
6104 if (target)
6105 return target;
6106 break;
6108 /* This updates the setjmp buffer that is its argument with the value
6109 of the current stack pointer. */
6110 case BUILT_IN_UPDATE_SETJMP_BUF:
6111 if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6113 rtx buf_addr
6114 = expand_normal (TREE_VALUE (arglist));
6116 expand_builtin_update_setjmp_buf (buf_addr);
6117 return const0_rtx;
6119 break;
6121 case BUILT_IN_TRAP:
6122 expand_builtin_trap ();
6123 return const0_rtx;
6125 case BUILT_IN_PRINTF:
6126 target = expand_builtin_printf (exp, target, mode, false);
6127 if (target)
6128 return target;
6129 break;
6131 case BUILT_IN_PRINTF_UNLOCKED:
6132 target = expand_builtin_printf (exp, target, mode, true);
6133 if (target)
6134 return target;
6135 break;
6137 case BUILT_IN_FPUTS:
6138 target = expand_builtin_fputs (arglist, target, false);
6139 if (target)
6140 return target;
6141 break;
6142 case BUILT_IN_FPUTS_UNLOCKED:
6143 target = expand_builtin_fputs (arglist, target, true);
6144 if (target)
6145 return target;
6146 break;
6148 case BUILT_IN_FPRINTF:
6149 target = expand_builtin_fprintf (exp, target, mode, false);
6150 if (target)
6151 return target;
6152 break;
6154 case BUILT_IN_FPRINTF_UNLOCKED:
6155 target = expand_builtin_fprintf (exp, target, mode, true);
6156 if (target)
6157 return target;
6158 break;
6160 case BUILT_IN_SPRINTF:
6161 target = expand_builtin_sprintf (arglist, target, mode);
6162 if (target)
6163 return target;
6164 break;
6166 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6167 target = expand_builtin_signbit (exp, target);
6168 if (target)
6169 return target;
6170 break;
6172 /* Various hooks for the DWARF 2 __throw routine. */
6173 case BUILT_IN_UNWIND_INIT:
6174 expand_builtin_unwind_init ();
6175 return const0_rtx;
6176 case BUILT_IN_DWARF_CFA:
6177 return virtual_cfa_rtx;
6178 #ifdef DWARF2_UNWIND_INFO
6179 case BUILT_IN_DWARF_SP_COLUMN:
6180 return expand_builtin_dwarf_sp_column ();
6181 case BUILT_IN_INIT_DWARF_REG_SIZES:
6182 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
6183 return const0_rtx;
6184 #endif
6185 case BUILT_IN_FROB_RETURN_ADDR:
6186 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
6187 case BUILT_IN_EXTRACT_RETURN_ADDR:
6188 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
6189 case BUILT_IN_EH_RETURN:
6190 expand_builtin_eh_return (TREE_VALUE (arglist),
6191 TREE_VALUE (TREE_CHAIN (arglist)));
6192 return const0_rtx;
6193 #ifdef EH_RETURN_DATA_REGNO
6194 case BUILT_IN_EH_RETURN_DATA_REGNO:
6195 return expand_builtin_eh_return_data_regno (arglist);
6196 #endif
6197 case BUILT_IN_EXTEND_POINTER:
6198 return expand_builtin_extend_pointer (TREE_VALUE (arglist));
6200 case BUILT_IN_VA_START:
6201 case BUILT_IN_STDARG_START:
6202 return expand_builtin_va_start (arglist);
6203 case BUILT_IN_VA_END:
6204 return expand_builtin_va_end (arglist);
6205 case BUILT_IN_VA_COPY:
6206 return expand_builtin_va_copy (arglist);
6207 case BUILT_IN_EXPECT:
6208 return expand_builtin_expect (arglist, target);
6209 case BUILT_IN_PREFETCH:
6210 expand_builtin_prefetch (arglist);
6211 return const0_rtx;
6213 case BUILT_IN_PROFILE_FUNC_ENTER:
6214 return expand_builtin_profile_func (false);
6215 case BUILT_IN_PROFILE_FUNC_EXIT:
6216 return expand_builtin_profile_func (true);
6218 case BUILT_IN_INIT_TRAMPOLINE:
6219 return expand_builtin_init_trampoline (arglist);
6220 case BUILT_IN_ADJUST_TRAMPOLINE:
6221 return expand_builtin_adjust_trampoline (arglist);
6223 case BUILT_IN_FORK:
6224 case BUILT_IN_EXECL:
6225 case BUILT_IN_EXECV:
6226 case BUILT_IN_EXECLP:
6227 case BUILT_IN_EXECLE:
6228 case BUILT_IN_EXECVP:
6229 case BUILT_IN_EXECVE:
6230 target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore);
6231 if (target)
6232 return target;
6233 break;
6235 case BUILT_IN_FETCH_AND_ADD_1:
6236 case BUILT_IN_FETCH_AND_ADD_2:
6237 case BUILT_IN_FETCH_AND_ADD_4:
6238 case BUILT_IN_FETCH_AND_ADD_8:
6239 case BUILT_IN_FETCH_AND_ADD_16:
6240 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1);
6241 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6242 false, target, ignore);
6243 if (target)
6244 return target;
6245 break;
6247 case BUILT_IN_FETCH_AND_SUB_1:
6248 case BUILT_IN_FETCH_AND_SUB_2:
6249 case BUILT_IN_FETCH_AND_SUB_4:
6250 case BUILT_IN_FETCH_AND_SUB_8:
6251 case BUILT_IN_FETCH_AND_SUB_16:
6252 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1);
6253 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6254 false, target, ignore);
6255 if (target)
6256 return target;
6257 break;
6259 case BUILT_IN_FETCH_AND_OR_1:
6260 case BUILT_IN_FETCH_AND_OR_2:
6261 case BUILT_IN_FETCH_AND_OR_4:
6262 case BUILT_IN_FETCH_AND_OR_8:
6263 case BUILT_IN_FETCH_AND_OR_16:
6264 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1);
6265 target = expand_builtin_sync_operation (mode, arglist, IOR,
6266 false, target, ignore);
6267 if (target)
6268 return target;
6269 break;
6271 case BUILT_IN_FETCH_AND_AND_1:
6272 case BUILT_IN_FETCH_AND_AND_2:
6273 case BUILT_IN_FETCH_AND_AND_4:
6274 case BUILT_IN_FETCH_AND_AND_8:
6275 case BUILT_IN_FETCH_AND_AND_16:
6276 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1);
6277 target = expand_builtin_sync_operation (mode, arglist, AND,
6278 false, target, ignore);
6279 if (target)
6280 return target;
6281 break;
6283 case BUILT_IN_FETCH_AND_XOR_1:
6284 case BUILT_IN_FETCH_AND_XOR_2:
6285 case BUILT_IN_FETCH_AND_XOR_4:
6286 case BUILT_IN_FETCH_AND_XOR_8:
6287 case BUILT_IN_FETCH_AND_XOR_16:
6288 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1);
6289 target = expand_builtin_sync_operation (mode, arglist, XOR,
6290 false, target, ignore);
6291 if (target)
6292 return target;
6293 break;
6295 case BUILT_IN_FETCH_AND_NAND_1:
6296 case BUILT_IN_FETCH_AND_NAND_2:
6297 case BUILT_IN_FETCH_AND_NAND_4:
6298 case BUILT_IN_FETCH_AND_NAND_8:
6299 case BUILT_IN_FETCH_AND_NAND_16:
6300 mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1);
6301 target = expand_builtin_sync_operation (mode, arglist, NOT,
6302 false, target, ignore);
6303 if (target)
6304 return target;
6305 break;
6307 case BUILT_IN_ADD_AND_FETCH_1:
6308 case BUILT_IN_ADD_AND_FETCH_2:
6309 case BUILT_IN_ADD_AND_FETCH_4:
6310 case BUILT_IN_ADD_AND_FETCH_8:
6311 case BUILT_IN_ADD_AND_FETCH_16:
6312 mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1);
6313 target = expand_builtin_sync_operation (mode, arglist, PLUS,
6314 true, target, ignore);
6315 if (target)
6316 return target;
6317 break;
6319 case BUILT_IN_SUB_AND_FETCH_1:
6320 case BUILT_IN_SUB_AND_FETCH_2:
6321 case BUILT_IN_SUB_AND_FETCH_4:
6322 case BUILT_IN_SUB_AND_FETCH_8:
6323 case BUILT_IN_SUB_AND_FETCH_16:
6324 mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1);
6325 target = expand_builtin_sync_operation (mode, arglist, MINUS,
6326 true, target, ignore);
6327 if (target)
6328 return target;
6329 break;
6331 case BUILT_IN_OR_AND_FETCH_1:
6332 case BUILT_IN_OR_AND_FETCH_2:
6333 case BUILT_IN_OR_AND_FETCH_4:
6334 case BUILT_IN_OR_AND_FETCH_8:
6335 case BUILT_IN_OR_AND_FETCH_16:
6336 mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1);
6337 target = expand_builtin_sync_operation (mode, arglist, IOR,
6338 true, target, ignore);
6339 if (target)
6340 return target;
6341 break;
6343 case BUILT_IN_AND_AND_FETCH_1:
6344 case BUILT_IN_AND_AND_FETCH_2:
6345 case BUILT_IN_AND_AND_FETCH_4:
6346 case BUILT_IN_AND_AND_FETCH_8:
6347 case BUILT_IN_AND_AND_FETCH_16:
6348 mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1);
6349 target = expand_builtin_sync_operation (mode, arglist, AND,
6350 true, target, ignore);
6351 if (target)
6352 return target;
6353 break;
6355 case BUILT_IN_XOR_AND_FETCH_1:
6356 case BUILT_IN_XOR_AND_FETCH_2:
6357 case BUILT_IN_XOR_AND_FETCH_4:
6358 case BUILT_IN_XOR_AND_FETCH_8:
6359 case BUILT_IN_XOR_AND_FETCH_16:
6360 mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1);
6361 target = expand_builtin_sync_operation (mode, arglist, XOR,
6362 true, target, ignore);
6363 if (target)
6364 return target;
6365 break;
6367 case BUILT_IN_NAND_AND_FETCH_1:
6368 case BUILT_IN_NAND_AND_FETCH_2:
6369 case BUILT_IN_NAND_AND_FETCH_4:
6370 case BUILT_IN_NAND_AND_FETCH_8:
6371 case BUILT_IN_NAND_AND_FETCH_16:
6372 mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1);
6373 target = expand_builtin_sync_operation (mode, arglist, NOT,
6374 true, target, ignore);
6375 if (target)
6376 return target;
6377 break;
6379 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1:
6380 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2:
6381 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4:
6382 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8:
6383 case BUILT_IN_BOOL_COMPARE_AND_SWAP_16:
6384 if (mode == VOIDmode)
6385 mode = TYPE_MODE (boolean_type_node);
6386 if (!target || !register_operand (target, mode))
6387 target = gen_reg_rtx (mode);
6389 mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1);
6390 target = expand_builtin_compare_and_swap (mode, arglist, true, target);
6391 if (target)
6392 return target;
6393 break;
6395 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
6396 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
6397 case BUILT_IN_VAL_COMPARE_AND_SWAP_4:
6398 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
6399 case BUILT_IN_VAL_COMPARE_AND_SWAP_16:
6400 mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1);
6401 target = expand_builtin_compare_and_swap (mode, arglist, false, target);
6402 if (target)
6403 return target;
6404 break;
6406 case BUILT_IN_LOCK_TEST_AND_SET_1:
6407 case BUILT_IN_LOCK_TEST_AND_SET_2:
6408 case BUILT_IN_LOCK_TEST_AND_SET_4:
6409 case BUILT_IN_LOCK_TEST_AND_SET_8:
6410 case BUILT_IN_LOCK_TEST_AND_SET_16:
6411 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1);
6412 target = expand_builtin_lock_test_and_set (mode, arglist, target);
6413 if (target)
6414 return target;
6415 break;
6417 case BUILT_IN_LOCK_RELEASE_1:
6418 case BUILT_IN_LOCK_RELEASE_2:
6419 case BUILT_IN_LOCK_RELEASE_4:
6420 case BUILT_IN_LOCK_RELEASE_8:
6421 case BUILT_IN_LOCK_RELEASE_16:
6422 mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1);
6423 expand_builtin_lock_release (mode, arglist);
6424 return const0_rtx;
6426 case BUILT_IN_SYNCHRONIZE:
6427 expand_builtin_synchronize ();
6428 return const0_rtx;
6430 case BUILT_IN_OBJECT_SIZE:
6431 return expand_builtin_object_size (exp);
6433 case BUILT_IN_MEMCPY_CHK:
6434 case BUILT_IN_MEMPCPY_CHK:
6435 case BUILT_IN_MEMMOVE_CHK:
6436 case BUILT_IN_MEMSET_CHK:
6437 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6438 if (target)
6439 return target;
6440 break;
6442 case BUILT_IN_STRCPY_CHK:
6443 case BUILT_IN_STPCPY_CHK:
6444 case BUILT_IN_STRNCPY_CHK:
6445 case BUILT_IN_STRCAT_CHK:
6446 case BUILT_IN_SNPRINTF_CHK:
6447 case BUILT_IN_VSNPRINTF_CHK:
6448 maybe_emit_chk_warning (exp, fcode);
6449 break;
6451 case BUILT_IN_SPRINTF_CHK:
6452 case BUILT_IN_VSPRINTF_CHK:
6453 maybe_emit_sprintf_chk_warning (exp, fcode);
6454 break;
6456 default: /* just do library call, if unknown builtin */
6457 break;
6460 /* The switch statement above can drop through to cause the function
6461 to be called normally. */
6462 return expand_call (exp, target, ignore);
6465 /* Determine whether a tree node represents a call to a built-in
6466 function. If the tree T is a call to a built-in function with
6467 the right number of arguments of the appropriate types, return
6468 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6469 Otherwise the return value is END_BUILTINS. */
6471 enum built_in_function
6472 builtin_mathfn_code (tree t)
6474 tree fndecl, arglist, parmlist;
6475 tree argtype, parmtype;
6477 if (TREE_CODE (t) != CALL_EXPR
6478 || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR)
6479 return END_BUILTINS;
6481 fndecl = get_callee_fndecl (t);
6482 if (fndecl == NULL_TREE
6483 || TREE_CODE (fndecl) != FUNCTION_DECL
6484 || ! DECL_BUILT_IN (fndecl)
6485 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6486 return END_BUILTINS;
6488 arglist = TREE_OPERAND (t, 1);
6489 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6490 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6492 /* If a function doesn't take a variable number of arguments,
6493 the last element in the list will have type `void'. */
6494 parmtype = TREE_VALUE (parmlist);
6495 if (VOID_TYPE_P (parmtype))
6497 if (arglist)
6498 return END_BUILTINS;
6499 return DECL_FUNCTION_CODE (fndecl);
6502 if (! arglist)
6503 return END_BUILTINS;
6505 argtype = TREE_TYPE (TREE_VALUE (arglist));
6507 if (SCALAR_FLOAT_TYPE_P (parmtype))
6509 if (! SCALAR_FLOAT_TYPE_P (argtype))
6510 return END_BUILTINS;
6512 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6514 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6515 return END_BUILTINS;
6517 else if (POINTER_TYPE_P (parmtype))
6519 if (! POINTER_TYPE_P (argtype))
6520 return END_BUILTINS;
6522 else if (INTEGRAL_TYPE_P (parmtype))
6524 if (! INTEGRAL_TYPE_P (argtype))
6525 return END_BUILTINS;
6527 else
6528 return END_BUILTINS;
6530 arglist = TREE_CHAIN (arglist);
6533 /* Variable-length argument list. */
6534 return DECL_FUNCTION_CODE (fndecl);
6537 /* Fold a call to __builtin_constant_p, if we know it will evaluate to a
6538 constant. ARGLIST is the argument list of the call. */
6540 static tree
6541 fold_builtin_constant_p (tree arglist)
6543 if (arglist == 0)
6544 return 0;
6546 arglist = TREE_VALUE (arglist);
6548 /* We return 1 for a numeric type that's known to be a constant
6549 value at compile-time or for an aggregate type that's a
6550 literal constant. */
6551 STRIP_NOPS (arglist);
6553 /* If we know this is a constant, emit the constant of one. */
6554 if (CONSTANT_CLASS_P (arglist)
6555 || (TREE_CODE (arglist) == CONSTRUCTOR
6556 && TREE_CONSTANT (arglist)))
6557 return integer_one_node;
6558 if (TREE_CODE (arglist) == ADDR_EXPR)
6560 tree op = TREE_OPERAND (arglist, 0);
6561 if (TREE_CODE (op) == STRING_CST
6562 || (TREE_CODE (op) == ARRAY_REF
6563 && integer_zerop (TREE_OPERAND (op, 1))
6564 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6565 return integer_one_node;
6568 /* If this expression has side effects, show we don't know it to be a
6569 constant. Likewise if it's a pointer or aggregate type since in
6570 those case we only want literals, since those are only optimized
6571 when generating RTL, not later.
6572 And finally, if we are compiling an initializer, not code, we
6573 need to return a definite result now; there's not going to be any
6574 more optimization done. */
6575 if (TREE_SIDE_EFFECTS (arglist)
6576 || AGGREGATE_TYPE_P (TREE_TYPE (arglist))
6577 || POINTER_TYPE_P (TREE_TYPE (arglist))
6578 || cfun == 0
6579 || folding_initializer)
6580 return integer_zero_node;
6582 return 0;
6585 /* Fold a call to __builtin_expect, if we expect that a comparison against
6586 the argument will fold to a constant. In practice, this means a true
6587 constant or the address of a non-weak symbol. ARGLIST is the argument
6588 list of the call. */
6590 static tree
6591 fold_builtin_expect (tree arglist)
6593 tree arg, inner;
6595 if (arglist == 0)
6596 return 0;
6598 arg = TREE_VALUE (arglist);
6600 /* If the argument isn't invariant, then there's nothing we can do. */
6601 if (!TREE_INVARIANT (arg))
6602 return 0;
6604 /* If we're looking at an address of a weak decl, then do not fold. */
6605 inner = arg;
6606 STRIP_NOPS (inner);
6607 if (TREE_CODE (inner) == ADDR_EXPR)
6611 inner = TREE_OPERAND (inner, 0);
6613 while (TREE_CODE (inner) == COMPONENT_REF
6614 || TREE_CODE (inner) == ARRAY_REF);
6615 if (DECL_P (inner) && DECL_WEAK (inner))
6616 return 0;
6619 /* Otherwise, ARG already has the proper type for the return value. */
6620 return arg;
6623 /* Fold a call to __builtin_classify_type. */
6625 static tree
6626 fold_builtin_classify_type (tree arglist)
6628 if (arglist == 0)
6629 return build_int_cst (NULL_TREE, no_type_class);
6631 return build_int_cst (NULL_TREE,
6632 type_to_class (TREE_TYPE (TREE_VALUE (arglist))));
6635 /* Fold a call to __builtin_strlen. */
6637 static tree
6638 fold_builtin_strlen (tree arglist)
6640 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6641 return NULL_TREE;
6642 else
6644 tree len = c_strlen (TREE_VALUE (arglist), 0);
6646 if (len)
6648 /* Convert from the internal "sizetype" type to "size_t". */
6649 if (size_type_node)
6650 len = fold_convert (size_type_node, len);
6651 return len;
6654 return NULL_TREE;
6658 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6660 static tree
6661 fold_builtin_inf (tree type, int warn)
6663 REAL_VALUE_TYPE real;
6665 /* __builtin_inff is intended to be usable to define INFINITY on all
6666 targets. If an infinity is not available, INFINITY expands "to a
6667 positive constant of type float that overflows at translation
6668 time", footnote "In this case, using INFINITY will violate the
6669 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6670 Thus we pedwarn to ensure this constraint violation is
6671 diagnosed. */
6672 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6673 pedwarn ("target format does not support infinity");
6675 real_inf (&real);
6676 return build_real (type, real);
6679 /* Fold a call to __builtin_nan or __builtin_nans. */
6681 static tree
6682 fold_builtin_nan (tree arglist, tree type, int quiet)
6684 REAL_VALUE_TYPE real;
6685 const char *str;
6687 if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE))
6688 return 0;
6689 str = c_getstr (TREE_VALUE (arglist));
6690 if (!str)
6691 return 0;
6693 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6694 return 0;
6696 return build_real (type, real);
6699 /* Return true if the floating point expression T has an integer value.
6700 We also allow +Inf, -Inf and NaN to be considered integer values. */
6702 static bool
6703 integer_valued_real_p (tree t)
6705 switch (TREE_CODE (t))
6707 case FLOAT_EXPR:
6708 return true;
6710 case ABS_EXPR:
6711 case SAVE_EXPR:
6712 case NON_LVALUE_EXPR:
6713 return integer_valued_real_p (TREE_OPERAND (t, 0));
6715 case COMPOUND_EXPR:
6716 case MODIFY_EXPR:
6717 case BIND_EXPR:
6718 return integer_valued_real_p (TREE_OPERAND (t, 1));
6720 case PLUS_EXPR:
6721 case MINUS_EXPR:
6722 case MULT_EXPR:
6723 case MIN_EXPR:
6724 case MAX_EXPR:
6725 return integer_valued_real_p (TREE_OPERAND (t, 0))
6726 && integer_valued_real_p (TREE_OPERAND (t, 1));
6728 case COND_EXPR:
6729 return integer_valued_real_p (TREE_OPERAND (t, 1))
6730 && integer_valued_real_p (TREE_OPERAND (t, 2));
6732 case REAL_CST:
6733 if (! TREE_CONSTANT_OVERFLOW (t))
6735 REAL_VALUE_TYPE c, cint;
6737 c = TREE_REAL_CST (t);
6738 real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c);
6739 return real_identical (&c, &cint);
6741 break;
6743 case NOP_EXPR:
6745 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6746 if (TREE_CODE (type) == INTEGER_TYPE)
6747 return true;
6748 if (TREE_CODE (type) == REAL_TYPE)
6749 return integer_valued_real_p (TREE_OPERAND (t, 0));
6750 break;
6753 case CALL_EXPR:
6754 switch (builtin_mathfn_code (t))
6756 CASE_FLT_FN (BUILT_IN_CEIL):
6757 CASE_FLT_FN (BUILT_IN_FLOOR):
6758 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6759 CASE_FLT_FN (BUILT_IN_RINT):
6760 CASE_FLT_FN (BUILT_IN_ROUND):
6761 CASE_FLT_FN (BUILT_IN_TRUNC):
6762 return true;
6764 default:
6765 break;
6767 break;
6769 default:
6770 break;
6772 return false;
6775 /* EXP is assumed to be builtin call where truncation can be propagated
6776 across (for instance floor((double)f) == (double)floorf (f).
6777 Do the transformation. */
6779 static tree
6780 fold_trunc_transparent_mathfn (tree fndecl, tree arglist)
6782 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6783 tree arg;
6785 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6786 return 0;
6788 arg = TREE_VALUE (arglist);
6789 /* Integer rounding functions are idempotent. */
6790 if (fcode == builtin_mathfn_code (arg))
6791 return arg;
6793 /* If argument is already integer valued, and we don't need to worry
6794 about setting errno, there's no need to perform rounding. */
6795 if (! flag_errno_math && integer_valued_real_p (arg))
6796 return arg;
6798 if (optimize)
6800 tree arg0 = strip_float_extensions (arg);
6801 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6802 tree newtype = TREE_TYPE (arg0);
6803 tree decl;
6805 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6806 && (decl = mathfn_built_in (newtype, fcode)))
6808 arglist =
6809 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6810 return fold_convert (ftype,
6811 build_function_call_expr (decl, arglist));
6814 return 0;
6817 /* EXP is assumed to be builtin call which can narrow the FP type of
6818 the argument, for instance lround((double)f) -> lroundf (f). */
6820 static tree
6821 fold_fixed_mathfn (tree fndecl, tree arglist)
6823 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6824 tree arg;
6826 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6827 return 0;
6829 arg = TREE_VALUE (arglist);
6831 /* If argument is already integer valued, and we don't need to worry
6832 about setting errno, there's no need to perform rounding. */
6833 if (! flag_errno_math && integer_valued_real_p (arg))
6834 return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg);
6836 if (optimize)
6838 tree ftype = TREE_TYPE (arg);
6839 tree arg0 = strip_float_extensions (arg);
6840 tree newtype = TREE_TYPE (arg0);
6841 tree decl;
6843 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6844 && (decl = mathfn_built_in (newtype, fcode)))
6846 arglist =
6847 build_tree_list (NULL_TREE, fold_convert (newtype, arg0));
6848 return build_function_call_expr (decl, arglist);
6852 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6853 sizeof (long long) == sizeof (long). */
6854 if (TYPE_PRECISION (long_long_integer_type_node)
6855 == TYPE_PRECISION (long_integer_type_node))
6857 tree newfn = NULL_TREE;
6858 switch (fcode)
6860 CASE_FLT_FN (BUILT_IN_LLCEIL):
6861 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6862 break;
6864 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6865 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6866 break;
6868 CASE_FLT_FN (BUILT_IN_LLROUND):
6869 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6870 break;
6872 CASE_FLT_FN (BUILT_IN_LLRINT):
6873 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6874 break;
6876 default:
6877 break;
6880 if (newfn)
6882 tree newcall = build_function_call_expr (newfn, arglist);
6883 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6887 return 0;
6890 /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST
6891 is the argument list, TYPE is the return type and FNDECL is the
6892 original function DECL. Return NULL_TREE if no if no simplification
6893 can be made. */
6895 static tree
6896 fold_builtin_cabs (tree arglist, tree type, tree fndecl)
6898 tree arg;
6900 if (!arglist || TREE_CHAIN (arglist))
6901 return NULL_TREE;
6903 arg = TREE_VALUE (arglist);
6904 if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
6905 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6906 return NULL_TREE;
6908 /* Evaluate cabs of a constant at compile-time. */
6909 if (flag_unsafe_math_optimizations
6910 && TREE_CODE (arg) == COMPLEX_CST
6911 && TREE_CODE (TREE_REALPART (arg)) == REAL_CST
6912 && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST
6913 && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg))
6914 && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg)))
6916 REAL_VALUE_TYPE r, i;
6918 r = TREE_REAL_CST (TREE_REALPART (arg));
6919 i = TREE_REAL_CST (TREE_IMAGPART (arg));
6921 real_arithmetic (&r, MULT_EXPR, &r, &r);
6922 real_arithmetic (&i, MULT_EXPR, &i, &i);
6923 real_arithmetic (&r, PLUS_EXPR, &r, &i);
6924 if (real_sqrt (&r, TYPE_MODE (type), &r)
6925 || ! flag_trapping_math)
6926 return build_real (type, r);
6929 /* If either part is zero, cabs is fabs of the other. */
6930 if (TREE_CODE (arg) == COMPLEX_EXPR
6931 && real_zerop (TREE_OPERAND (arg, 0)))
6932 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1));
6933 if (TREE_CODE (arg) == COMPLEX_EXPR
6934 && real_zerop (TREE_OPERAND (arg, 1)))
6935 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0));
6937 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6938 if (TREE_CODE (arg) == NEGATE_EXPR
6939 || TREE_CODE (arg) == CONJ_EXPR)
6941 tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0));
6942 return build_function_call_expr (fndecl, arglist);
6945 /* Don't do this when optimizing for size. */
6946 if (flag_unsafe_math_optimizations
6947 && optimize && !optimize_size)
6949 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6951 if (sqrtfn != NULL_TREE)
6953 tree rpart, ipart, result, arglist;
6955 arg = builtin_save_expr (arg);
6957 rpart = fold_build1 (REALPART_EXPR, type, arg);
6958 ipart = fold_build1 (IMAGPART_EXPR, type, arg);
6960 rpart = builtin_save_expr (rpart);
6961 ipart = builtin_save_expr (ipart);
6963 result = fold_build2 (PLUS_EXPR, type,
6964 fold_build2 (MULT_EXPR, type,
6965 rpart, rpart),
6966 fold_build2 (MULT_EXPR, type,
6967 ipart, ipart));
6969 arglist = build_tree_list (NULL_TREE, result);
6970 return build_function_call_expr (sqrtfn, arglist);
6974 return NULL_TREE;
6977 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return
6978 NULL_TREE if no simplification can be made. */
6980 static tree
6981 fold_builtin_sqrt (tree arglist, tree type)
6984 enum built_in_function fcode;
6985 tree arg = TREE_VALUE (arglist);
6987 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
6988 return NULL_TREE;
6990 /* Optimize sqrt of constant value. */
6991 if (TREE_CODE (arg) == REAL_CST
6992 && ! TREE_CONSTANT_OVERFLOW (arg))
6994 REAL_VALUE_TYPE r, x;
6996 x = TREE_REAL_CST (arg);
6997 if (real_sqrt (&r, TYPE_MODE (type), &x)
6998 || (!flag_trapping_math && !flag_errno_math))
6999 return build_real (type, r);
7002 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7003 fcode = builtin_mathfn_code (arg);
7004 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7006 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7007 arg = fold_build2 (MULT_EXPR, type,
7008 TREE_VALUE (TREE_OPERAND (arg, 1)),
7009 build_real (type, dconsthalf));
7010 arglist = build_tree_list (NULL_TREE, arg);
7011 return build_function_call_expr (expfn, arglist);
7014 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7015 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7017 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7019 if (powfn)
7021 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7022 tree tree_root;
7023 /* The inner root was either sqrt or cbrt. */
7024 REAL_VALUE_TYPE dconstroot =
7025 BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
7027 /* Adjust for the outer root. */
7028 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7029 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7030 tree_root = build_real (type, dconstroot);
7031 arglist = tree_cons (NULL_TREE, arg0,
7032 build_tree_list (NULL_TREE, tree_root));
7033 return build_function_call_expr (powfn, arglist);
7037 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7038 if (flag_unsafe_math_optimizations
7039 && (fcode == BUILT_IN_POW
7040 || fcode == BUILT_IN_POWF
7041 || fcode == BUILT_IN_POWL))
7043 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7044 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7045 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7046 tree narg1;
7047 if (!tree_expr_nonnegative_p (arg0))
7048 arg0 = build1 (ABS_EXPR, type, arg0);
7049 narg1 = fold_build2 (MULT_EXPR, type, arg1,
7050 build_real (type, dconsthalf));
7051 arglist = tree_cons (NULL_TREE, arg0,
7052 build_tree_list (NULL_TREE, narg1));
7053 return build_function_call_expr (powfn, arglist);
7056 return NULL_TREE;
7059 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return
7060 NULL_TREE if no simplification can be made. */
7061 static tree
7062 fold_builtin_cbrt (tree arglist, tree type)
7064 tree arg = TREE_VALUE (arglist);
7065 const enum built_in_function fcode = builtin_mathfn_code (arg);
7067 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7068 return NULL_TREE;
7070 /* Optimize cbrt of constant value. */
7071 if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
7072 return arg;
7074 if (flag_unsafe_math_optimizations)
7076 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7077 if (BUILTIN_EXPONENT_P (fcode))
7079 tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7080 const REAL_VALUE_TYPE third_trunc =
7081 real_value_truncate (TYPE_MODE (type), dconstthird);
7082 arg = fold_build2 (MULT_EXPR, type,
7083 TREE_VALUE (TREE_OPERAND (arg, 1)),
7084 build_real (type, third_trunc));
7085 arglist = build_tree_list (NULL_TREE, arg);
7086 return build_function_call_expr (expfn, arglist);
7089 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7090 if (BUILTIN_SQRT_P (fcode))
7092 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7094 if (powfn)
7096 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7097 tree tree_root;
7098 REAL_VALUE_TYPE dconstroot = dconstthird;
7100 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7101 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7102 tree_root = build_real (type, dconstroot);
7103 arglist = tree_cons (NULL_TREE, arg0,
7104 build_tree_list (NULL_TREE, tree_root));
7105 return build_function_call_expr (powfn, arglist);
7109 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7110 if (BUILTIN_CBRT_P (fcode))
7112 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
7113 if (tree_expr_nonnegative_p (arg0))
7115 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7117 if (powfn)
7119 tree tree_root;
7120 REAL_VALUE_TYPE dconstroot;
7122 real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird);
7123 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7124 tree_root = build_real (type, dconstroot);
7125 arglist = tree_cons (NULL_TREE, arg0,
7126 build_tree_list (NULL_TREE, tree_root));
7127 return build_function_call_expr (powfn, arglist);
7132 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7133 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7134 || fcode == BUILT_IN_POWL)
7136 tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1));
7137 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7138 if (tree_expr_nonnegative_p (arg00))
7140 tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
7141 const REAL_VALUE_TYPE dconstroot
7142 = real_value_truncate (TYPE_MODE (type), dconstthird);
7143 tree narg01 = fold_build2 (MULT_EXPR, type, arg01,
7144 build_real (type, dconstroot));
7145 arglist = tree_cons (NULL_TREE, arg00,
7146 build_tree_list (NULL_TREE, narg01));
7147 return build_function_call_expr (powfn, arglist);
7151 return NULL_TREE;
7154 /* Fold function call to builtin sin, sinf, or sinl. Return
7155 NULL_TREE if no simplification can be made. */
7156 static tree
7157 fold_builtin_sin (tree arglist)
7159 tree arg = TREE_VALUE (arglist);
7161 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7162 return NULL_TREE;
7164 /* Optimize sin (0.0) = 0.0. */
7165 if (real_zerop (arg))
7166 return arg;
7168 return NULL_TREE;
7171 /* Fold function call to builtin cos, cosf, or cosl. Return
7172 NULL_TREE if no simplification can be made. */
7173 static tree
7174 fold_builtin_cos (tree arglist, tree type, tree fndecl)
7176 tree arg = TREE_VALUE (arglist);
7178 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7179 return NULL_TREE;
7181 /* Optimize cos (0.0) = 1.0. */
7182 if (real_zerop (arg))
7183 return build_real (type, dconst1);
7185 /* Optimize cos(-x) into cos (x). */
7186 if (TREE_CODE (arg) == NEGATE_EXPR)
7188 tree args = build_tree_list (NULL_TREE,
7189 TREE_OPERAND (arg, 0));
7190 return build_function_call_expr (fndecl, args);
7193 return NULL_TREE;
7196 /* Fold function call to builtin tan, tanf, or tanl. Return
7197 NULL_TREE if no simplification can be made. */
7198 static tree
7199 fold_builtin_tan (tree arglist)
7201 enum built_in_function fcode;
7202 tree arg = TREE_VALUE (arglist);
7204 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7205 return NULL_TREE;
7207 /* Optimize tan(0.0) = 0.0. */
7208 if (real_zerop (arg))
7209 return arg;
7211 /* Optimize tan(atan(x)) = x. */
7212 fcode = builtin_mathfn_code (arg);
7213 if (flag_unsafe_math_optimizations
7214 && (fcode == BUILT_IN_ATAN
7215 || fcode == BUILT_IN_ATANF
7216 || fcode == BUILT_IN_ATANL))
7217 return TREE_VALUE (TREE_OPERAND (arg, 1));
7219 return NULL_TREE;
7222 /* Fold function call to builtin atan, atanf, or atanl. Return
7223 NULL_TREE if no simplification can be made. */
7225 static tree
7226 fold_builtin_atan (tree arglist, tree type)
7229 tree arg = TREE_VALUE (arglist);
7231 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7232 return NULL_TREE;
7234 /* Optimize atan(0.0) = 0.0. */
7235 if (real_zerop (arg))
7236 return arg;
7238 /* Optimize atan(1.0) = pi/4. */
7239 if (real_onep (arg))
7241 REAL_VALUE_TYPE cst;
7243 real_convert (&cst, TYPE_MODE (type), &dconstpi);
7244 SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2);
7245 return build_real (type, cst);
7248 return NULL_TREE;
7251 /* Fold function call to builtin trunc, truncf or truncl. Return
7252 NULL_TREE if no simplification can be made. */
7254 static tree
7255 fold_builtin_trunc (tree fndecl, tree arglist)
7257 tree arg;
7259 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7260 return 0;
7262 /* Optimize trunc of constant value. */
7263 arg = TREE_VALUE (arglist);
7264 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7266 REAL_VALUE_TYPE r, x;
7267 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7269 x = TREE_REAL_CST (arg);
7270 real_trunc (&r, TYPE_MODE (type), &x);
7271 return build_real (type, r);
7274 return fold_trunc_transparent_mathfn (fndecl, arglist);
7277 /* Fold function call to builtin floor, floorf or floorl. Return
7278 NULL_TREE if no simplification can be made. */
7280 static tree
7281 fold_builtin_floor (tree fndecl, tree arglist)
7283 tree arg;
7285 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7286 return 0;
7288 /* Optimize floor of constant value. */
7289 arg = TREE_VALUE (arglist);
7290 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7292 REAL_VALUE_TYPE x;
7294 x = TREE_REAL_CST (arg);
7295 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7297 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7298 REAL_VALUE_TYPE r;
7300 real_floor (&r, TYPE_MODE (type), &x);
7301 return build_real (type, r);
7305 return fold_trunc_transparent_mathfn (fndecl, arglist);
7308 /* Fold function call to builtin ceil, ceilf or ceill. Return
7309 NULL_TREE if no simplification can be made. */
7311 static tree
7312 fold_builtin_ceil (tree fndecl, tree arglist)
7314 tree arg;
7316 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7317 return 0;
7319 /* Optimize ceil of constant value. */
7320 arg = TREE_VALUE (arglist);
7321 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7323 REAL_VALUE_TYPE x;
7325 x = TREE_REAL_CST (arg);
7326 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7328 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7329 REAL_VALUE_TYPE r;
7331 real_ceil (&r, TYPE_MODE (type), &x);
7332 return build_real (type, r);
7336 return fold_trunc_transparent_mathfn (fndecl, arglist);
7339 /* Fold function call to builtin round, roundf or roundl. Return
7340 NULL_TREE if no simplification can be made. */
7342 static tree
7343 fold_builtin_round (tree fndecl, tree arglist)
7345 tree arg;
7347 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7348 return 0;
7350 /* Optimize round of constant value. */
7351 arg = TREE_VALUE (arglist);
7352 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7354 REAL_VALUE_TYPE x;
7356 x = TREE_REAL_CST (arg);
7357 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7359 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7360 REAL_VALUE_TYPE r;
7362 real_round (&r, TYPE_MODE (type), &x);
7363 return build_real (type, r);
7367 return fold_trunc_transparent_mathfn (fndecl, arglist);
7370 /* Fold function call to builtin lround, lroundf or lroundl (or the
7371 corresponding long long versions) and other rounding functions.
7372 Return NULL_TREE if no simplification can be made. */
7374 static tree
7375 fold_builtin_int_roundingfn (tree fndecl, tree arglist)
7377 tree arg;
7379 if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7380 return 0;
7382 /* Optimize lround of constant value. */
7383 arg = TREE_VALUE (arglist);
7384 if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7386 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7388 if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x))
7390 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7391 tree ftype = TREE_TYPE (arg), result;
7392 HOST_WIDE_INT hi, lo;
7393 REAL_VALUE_TYPE r;
7395 switch (DECL_FUNCTION_CODE (fndecl))
7397 CASE_FLT_FN (BUILT_IN_LFLOOR):
7398 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7399 real_floor (&r, TYPE_MODE (ftype), &x);
7400 break;
7402 CASE_FLT_FN (BUILT_IN_LCEIL):
7403 CASE_FLT_FN (BUILT_IN_LLCEIL):
7404 real_ceil (&r, TYPE_MODE (ftype), &x);
7405 break;
7407 CASE_FLT_FN (BUILT_IN_LROUND):
7408 CASE_FLT_FN (BUILT_IN_LLROUND):
7409 real_round (&r, TYPE_MODE (ftype), &x);
7410 break;
7412 default:
7413 gcc_unreachable ();
7416 REAL_VALUE_TO_INT (&lo, &hi, r);
7417 result = build_int_cst_wide (NULL_TREE, lo, hi);
7418 if (int_fits_type_p (result, itype))
7419 return fold_convert (itype, result);
7423 return fold_fixed_mathfn (fndecl, arglist);
7426 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7427 and their long and long long variants (i.e. ffsl and ffsll).
7428 Return NULL_TREE if no simplification can be made. */
7430 static tree
7431 fold_builtin_bitop (tree fndecl, tree arglist)
7433 tree arg;
7435 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
7436 return NULL_TREE;
7438 /* Optimize for constant argument. */
7439 arg = TREE_VALUE (arglist);
7440 if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg))
7442 HOST_WIDE_INT hi, width, result;
7443 unsigned HOST_WIDE_INT lo;
7444 tree type;
7446 type = TREE_TYPE (arg);
7447 width = TYPE_PRECISION (type);
7448 lo = TREE_INT_CST_LOW (arg);
7450 /* Clear all the bits that are beyond the type's precision. */
7451 if (width > HOST_BITS_PER_WIDE_INT)
7453 hi = TREE_INT_CST_HIGH (arg);
7454 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7455 hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT));
7457 else
7459 hi = 0;
7460 if (width < HOST_BITS_PER_WIDE_INT)
7461 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7464 switch (DECL_FUNCTION_CODE (fndecl))
7466 CASE_INT_FN (BUILT_IN_FFS):
7467 if (lo != 0)
7468 result = exact_log2 (lo & -lo) + 1;
7469 else if (hi != 0)
7470 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1;
7471 else
7472 result = 0;
7473 break;
7475 CASE_INT_FN (BUILT_IN_CLZ):
7476 if (hi != 0)
7477 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7478 else if (lo != 0)
7479 result = width - floor_log2 (lo) - 1;
7480 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7481 result = width;
7482 break;
7484 CASE_INT_FN (BUILT_IN_CTZ):
7485 if (lo != 0)
7486 result = exact_log2 (lo & -lo);
7487 else if (hi != 0)
7488 result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi);
7489 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7490 result = width;
7491 break;
7493 CASE_INT_FN (BUILT_IN_POPCOUNT):
7494 result = 0;
7495 while (lo)
7496 result++, lo &= lo - 1;
7497 while (hi)
7498 result++, hi &= hi - 1;
7499 break;
7501 CASE_INT_FN (BUILT_IN_PARITY):
7502 result = 0;
7503 while (lo)
7504 result++, lo &= lo - 1;
7505 while (hi)
7506 result++, hi &= hi - 1;
7507 result &= 1;
7508 break;
7510 default:
7511 gcc_unreachable ();
7514 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7517 return NULL_TREE;
7520 /* Return true if EXPR is the real constant contained in VALUE. */
7522 static bool
7523 real_dconstp (tree expr, const REAL_VALUE_TYPE *value)
7525 STRIP_NOPS (expr);
7527 return ((TREE_CODE (expr) == REAL_CST
7528 && ! TREE_CONSTANT_OVERFLOW (expr)
7529 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value))
7530 || (TREE_CODE (expr) == COMPLEX_CST
7531 && real_dconstp (TREE_REALPART (expr), value)
7532 && real_zerop (TREE_IMAGPART (expr))));
7535 /* A subroutine of fold_builtin to fold the various logarithmic
7536 functions. EXP is the CALL_EXPR of a call to a builtin logN
7537 function. VALUE is the base of the logN function. */
7539 static tree
7540 fold_builtin_logarithm (tree fndecl, tree arglist,
7541 const REAL_VALUE_TYPE *value)
7543 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7545 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7546 tree arg = TREE_VALUE (arglist);
7547 const enum built_in_function fcode = builtin_mathfn_code (arg);
7549 /* Optimize logN(1.0) = 0.0. */
7550 if (real_onep (arg))
7551 return build_real (type, dconst0);
7553 /* Optimize logN(N) = 1.0. If N can't be truncated to MODE
7554 exactly, then only do this if flag_unsafe_math_optimizations. */
7555 if (exact_real_truncate (TYPE_MODE (type), value)
7556 || flag_unsafe_math_optimizations)
7558 const REAL_VALUE_TYPE value_truncate =
7559 real_value_truncate (TYPE_MODE (type), *value);
7560 if (real_dconstp (arg, &value_truncate))
7561 return build_real (type, dconst1);
7564 /* Special case, optimize logN(expN(x)) = x. */
7565 if (flag_unsafe_math_optimizations
7566 && ((value == &dconste
7567 && (fcode == BUILT_IN_EXP
7568 || fcode == BUILT_IN_EXPF
7569 || fcode == BUILT_IN_EXPL))
7570 || (value == &dconst2
7571 && (fcode == BUILT_IN_EXP2
7572 || fcode == BUILT_IN_EXP2F
7573 || fcode == BUILT_IN_EXP2L))
7574 || (value == &dconst10 && (BUILTIN_EXP10_P (fcode)))))
7575 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7577 /* Optimize logN(func()) for various exponential functions. We
7578 want to determine the value "x" and the power "exponent" in
7579 order to transform logN(x**exponent) into exponent*logN(x). */
7580 if (flag_unsafe_math_optimizations)
7582 tree exponent = 0, x = 0;
7584 switch (fcode)
7586 CASE_FLT_FN (BUILT_IN_EXP):
7587 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7588 x = build_real (type,
7589 real_value_truncate (TYPE_MODE (type), dconste));
7590 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7591 break;
7592 CASE_FLT_FN (BUILT_IN_EXP2):
7593 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7594 x = build_real (type, dconst2);
7595 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7596 break;
7597 CASE_FLT_FN (BUILT_IN_EXP10):
7598 CASE_FLT_FN (BUILT_IN_POW10):
7599 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7600 x = build_real (type, dconst10);
7601 exponent = TREE_VALUE (TREE_OPERAND (arg, 1));
7602 break;
7603 CASE_FLT_FN (BUILT_IN_SQRT):
7604 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7605 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7606 exponent = build_real (type, dconsthalf);
7607 break;
7608 CASE_FLT_FN (BUILT_IN_CBRT):
7609 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7610 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7611 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7612 dconstthird));
7613 break;
7614 CASE_FLT_FN (BUILT_IN_POW):
7615 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7616 x = TREE_VALUE (TREE_OPERAND (arg, 1));
7617 exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
7618 break;
7619 default:
7620 break;
7623 /* Now perform the optimization. */
7624 if (x && exponent)
7626 tree logfn;
7627 arglist = build_tree_list (NULL_TREE, x);
7628 logfn = build_function_call_expr (fndecl, arglist);
7629 return fold_build2 (MULT_EXPR, type, exponent, logfn);
7634 return 0;
7637 /* Fold a builtin function call to pow, powf, or powl. Return
7638 NULL_TREE if no simplification can be made. */
7639 static tree
7640 fold_builtin_pow (tree fndecl, tree arglist, tree type)
7642 tree arg0 = TREE_VALUE (arglist);
7643 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7645 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
7646 return NULL_TREE;
7648 /* Optimize pow(1.0,y) = 1.0. */
7649 if (real_onep (arg0))
7650 return omit_one_operand (type, build_real (type, dconst1), arg1);
7652 if (TREE_CODE (arg1) == REAL_CST
7653 && ! TREE_CONSTANT_OVERFLOW (arg1))
7655 REAL_VALUE_TYPE cint;
7656 REAL_VALUE_TYPE c;
7657 HOST_WIDE_INT n;
7659 c = TREE_REAL_CST (arg1);
7661 /* Optimize pow(x,0.0) = 1.0. */
7662 if (REAL_VALUES_EQUAL (c, dconst0))
7663 return omit_one_operand (type, build_real (type, dconst1),
7664 arg0);
7666 /* Optimize pow(x,1.0) = x. */
7667 if (REAL_VALUES_EQUAL (c, dconst1))
7668 return arg0;
7670 /* Optimize pow(x,-1.0) = 1.0/x. */
7671 if (REAL_VALUES_EQUAL (c, dconstm1))
7672 return fold_build2 (RDIV_EXPR, type,
7673 build_real (type, dconst1), arg0);
7675 /* Optimize pow(x,0.5) = sqrt(x). */
7676 if (flag_unsafe_math_optimizations
7677 && REAL_VALUES_EQUAL (c, dconsthalf))
7679 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7681 if (sqrtfn != NULL_TREE)
7683 tree arglist = build_tree_list (NULL_TREE, arg0);
7684 return build_function_call_expr (sqrtfn, arglist);
7688 /* Check for an integer exponent. */
7689 n = real_to_integer (&c);
7690 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7691 if (real_identical (&c, &cint))
7693 /* Attempt to evaluate pow at compile-time. */
7694 if (TREE_CODE (arg0) == REAL_CST
7695 && ! TREE_CONSTANT_OVERFLOW (arg0))
7697 REAL_VALUE_TYPE x;
7698 bool inexact;
7700 x = TREE_REAL_CST (arg0);
7701 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7702 if (flag_unsafe_math_optimizations || !inexact)
7703 return build_real (type, x);
7706 /* Strip sign ops from even integer powers. */
7707 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7709 tree narg0 = fold_strip_sign_ops (arg0);
7710 if (narg0)
7712 arglist = build_tree_list (NULL_TREE, arg1);
7713 arglist = tree_cons (NULL_TREE, narg0, arglist);
7714 return build_function_call_expr (fndecl, arglist);
7720 if (flag_unsafe_math_optimizations)
7722 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7724 /* Optimize pow(expN(x),y) = expN(x*y). */
7725 if (BUILTIN_EXPONENT_P (fcode))
7727 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7728 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7729 arg = fold_build2 (MULT_EXPR, type, arg, arg1);
7730 arglist = build_tree_list (NULL_TREE, arg);
7731 return build_function_call_expr (expfn, arglist);
7734 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7735 if (BUILTIN_SQRT_P (fcode))
7737 tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7738 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7739 build_real (type, dconsthalf));
7741 arglist = tree_cons (NULL_TREE, narg0,
7742 build_tree_list (NULL_TREE, narg1));
7743 return build_function_call_expr (fndecl, arglist);
7746 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7747 if (BUILTIN_CBRT_P (fcode))
7749 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
7750 if (tree_expr_nonnegative_p (arg))
7752 const REAL_VALUE_TYPE dconstroot
7753 = real_value_truncate (TYPE_MODE (type), dconstthird);
7754 tree narg1 = fold_build2 (MULT_EXPR, type, arg1,
7755 build_real (type, dconstroot));
7756 arglist = tree_cons (NULL_TREE, arg,
7757 build_tree_list (NULL_TREE, narg1));
7758 return build_function_call_expr (fndecl, arglist);
7762 /* Optimize pow(pow(x,y),z) = pow(x,y*z). */
7763 if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF
7764 || fcode == BUILT_IN_POWL)
7766 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7767 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7768 tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1);
7769 arglist = tree_cons (NULL_TREE, arg00,
7770 build_tree_list (NULL_TREE, narg1));
7771 return build_function_call_expr (fndecl, arglist);
7775 return NULL_TREE;
7778 /* Fold a builtin function call to powi, powif, or powil. Return
7779 NULL_TREE if no simplification can be made. */
7780 static tree
7781 fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
7783 tree arg0 = TREE_VALUE (arglist);
7784 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
7786 if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
7787 return NULL_TREE;
7789 /* Optimize pow(1.0,y) = 1.0. */
7790 if (real_onep (arg0))
7791 return omit_one_operand (type, build_real (type, dconst1), arg1);
7793 if (host_integerp (arg1, 0))
7795 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7797 /* Evaluate powi at compile-time. */
7798 if (TREE_CODE (arg0) == REAL_CST
7799 && ! TREE_CONSTANT_OVERFLOW (arg0))
7801 REAL_VALUE_TYPE x;
7802 x = TREE_REAL_CST (arg0);
7803 real_powi (&x, TYPE_MODE (type), &x, c);
7804 return build_real (type, x);
7807 /* Optimize pow(x,0) = 1.0. */
7808 if (c == 0)
7809 return omit_one_operand (type, build_real (type, dconst1),
7810 arg0);
7812 /* Optimize pow(x,1) = x. */
7813 if (c == 1)
7814 return arg0;
7816 /* Optimize pow(x,-1) = 1.0/x. */
7817 if (c == -1)
7818 return fold_build2 (RDIV_EXPR, type,
7819 build_real (type, dconst1), arg0);
7822 return NULL_TREE;
7825 /* A subroutine of fold_builtin to fold the various exponent
7826 functions. EXP is the CALL_EXPR of a call to a builtin function.
7827 VALUE is the value which will be raised to a power. */
7829 static tree
7830 fold_builtin_exponent (tree fndecl, tree arglist,
7831 const REAL_VALUE_TYPE *value)
7833 if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
7835 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7836 tree arg = TREE_VALUE (arglist);
7838 /* Optimize exp*(0.0) = 1.0. */
7839 if (real_zerop (arg))
7840 return build_real (type, dconst1);
7842 /* Optimize expN(1.0) = N. */
7843 if (real_onep (arg))
7845 REAL_VALUE_TYPE cst;
7847 real_convert (&cst, TYPE_MODE (type), value);
7848 return build_real (type, cst);
7851 /* Attempt to evaluate expN(integer) at compile-time. */
7852 if (flag_unsafe_math_optimizations
7853 && TREE_CODE (arg) == REAL_CST
7854 && ! TREE_CONSTANT_OVERFLOW (arg))
7856 REAL_VALUE_TYPE cint;
7857 REAL_VALUE_TYPE c;
7858 HOST_WIDE_INT n;
7860 c = TREE_REAL_CST (arg);
7861 n = real_to_integer (&c);
7862 real_from_integer (&cint, VOIDmode, n,
7863 n < 0 ? -1 : 0, 0);
7864 if (real_identical (&c, &cint))
7866 REAL_VALUE_TYPE x;
7868 real_powi (&x, TYPE_MODE (type), value, n);
7869 return build_real (type, x);
7873 /* Optimize expN(logN(x)) = x. */
7874 if (flag_unsafe_math_optimizations)
7876 const enum built_in_function fcode = builtin_mathfn_code (arg);
7878 if ((value == &dconste
7879 && (fcode == BUILT_IN_LOG
7880 || fcode == BUILT_IN_LOGF
7881 || fcode == BUILT_IN_LOGL))
7882 || (value == &dconst2
7883 && (fcode == BUILT_IN_LOG2
7884 || fcode == BUILT_IN_LOG2F
7885 || fcode == BUILT_IN_LOG2L))
7886 || (value == &dconst10
7887 && (fcode == BUILT_IN_LOG10
7888 || fcode == BUILT_IN_LOG10F
7889 || fcode == BUILT_IN_LOG10L)))
7890 return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1)));
7894 return 0;
7897 /* Fold function call to builtin memset. Return
7898 NULL_TREE if no simplification can be made. */
7900 static tree
7901 fold_builtin_memset (tree arglist, tree type, bool ignore)
7903 tree dest, c, len, var, ret;
7904 unsigned HOST_WIDE_INT length, cval;
7906 if (!validate_arglist (arglist,
7907 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
7908 return 0;
7910 dest = TREE_VALUE (arglist);
7911 c = TREE_VALUE (TREE_CHAIN (arglist));
7912 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
7914 if (! host_integerp (len, 1))
7915 return 0;
7917 /* If the LEN parameter is zero, return DEST. */
7918 if (integer_zerop (len))
7919 return omit_one_operand (type, dest, c);
7921 if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
7922 return 0;
7924 var = dest;
7925 STRIP_NOPS (var);
7926 if (TREE_CODE (var) != ADDR_EXPR)
7927 return 0;
7929 var = TREE_OPERAND (var, 0);
7930 if (TREE_THIS_VOLATILE (var))
7931 return 0;
7933 if (!INTEGRAL_TYPE_P (TREE_TYPE (var))
7934 && !POINTER_TYPE_P (TREE_TYPE (var)))
7935 return 0;
7937 length = tree_low_cst (len, 1);
7938 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length
7939 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
7940 < (int) length)
7941 return 0;
7943 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
7944 return 0;
7946 if (integer_zerop (c))
7947 cval = 0;
7948 else
7950 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
7951 return 0;
7953 cval = tree_low_cst (c, 1);
7954 cval &= 0xff;
7955 cval |= cval << 8;
7956 cval |= cval << 16;
7957 cval |= (cval << 31) << 1;
7960 ret = build_int_cst_type (TREE_TYPE (var), cval);
7961 ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret);
7962 if (ignore)
7963 return ret;
7965 return omit_one_operand (type, dest, ret);
7968 /* Fold function call to builtin memset. Return
7969 NULL_TREE if no simplification can be made. */
7971 static tree
7972 fold_builtin_bzero (tree arglist, bool ignore)
7974 tree dest, size, newarglist;
7976 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7977 return 0;
7979 if (!ignore)
7980 return 0;
7982 dest = TREE_VALUE (arglist);
7983 size = TREE_VALUE (TREE_CHAIN (arglist));
7985 /* New argument list transforming bzero(ptr x, int y) to
7986 memset(ptr x, int 0, size_t y). This is done this way
7987 so that if it isn't expanded inline, we fallback to
7988 calling bzero instead of memset. */
7990 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
7991 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
7992 newarglist = tree_cons (NULL_TREE, dest, newarglist);
7993 return fold_builtin_memset (newarglist, void_type_node, ignore);
7996 /* Fold function call to builtin mem{{,p}cpy,move}. Return
7997 NULL_TREE if no simplification can be made.
7998 If ENDP is 0, return DEST (like memcpy).
7999 If ENDP is 1, return DEST+LEN (like mempcpy).
8000 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8001 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8002 (memmove). */
8004 static tree
8005 fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp)
8007 tree dest, src, len, destvar, srcvar, expr;
8008 unsigned HOST_WIDE_INT length;
8010 if (! validate_arglist (arglist,
8011 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8012 return 0;
8014 dest = TREE_VALUE (arglist);
8015 src = TREE_VALUE (TREE_CHAIN (arglist));
8016 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8018 /* If the LEN parameter is zero, return DEST. */
8019 if (integer_zerop (len))
8020 return omit_one_operand (type, dest, src);
8022 /* If SRC and DEST are the same (and not volatile), return
8023 DEST{,+LEN,+LEN-1}. */
8024 if (operand_equal_p (src, dest, 0))
8025 expr = len;
8026 else
8028 if (! host_integerp (len, 1))
8029 return 0;
8031 if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src))
8032 return 0;
8034 destvar = dest;
8035 STRIP_NOPS (destvar);
8036 if (TREE_CODE (destvar) != ADDR_EXPR)
8037 return 0;
8039 destvar = TREE_OPERAND (destvar, 0);
8040 if (TREE_THIS_VOLATILE (destvar))
8041 return 0;
8043 if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8044 && !POINTER_TYPE_P (TREE_TYPE (destvar))
8045 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar)))
8046 return 0;
8048 srcvar = src;
8049 STRIP_NOPS (srcvar);
8050 if (TREE_CODE (srcvar) != ADDR_EXPR)
8051 return 0;
8053 srcvar = TREE_OPERAND (srcvar, 0);
8054 if (TREE_THIS_VOLATILE (srcvar))
8055 return 0;
8057 if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8058 && !POINTER_TYPE_P (TREE_TYPE (srcvar))
8059 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar)))
8060 return 0;
8062 length = tree_low_cst (len, 1);
8063 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length
8064 || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8065 < (int) length
8066 || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length
8067 || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT
8068 < (int) length)
8069 return 0;
8071 if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar))
8072 || POINTER_TYPE_P (TREE_TYPE (srcvar)))
8073 && (INTEGRAL_TYPE_P (TREE_TYPE (destvar))
8074 || POINTER_TYPE_P (TREE_TYPE (destvar))))
8075 expr = fold_convert (TREE_TYPE (destvar), srcvar);
8076 else
8077 expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar);
8078 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr);
8081 if (ignore)
8082 return expr;
8084 if (endp == 0 || endp == 3)
8085 return omit_one_operand (type, dest, expr);
8087 if (expr == len)
8088 expr = 0;
8090 if (endp == 2)
8091 len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len,
8092 ssize_int (1));
8094 len = fold_convert (TREE_TYPE (dest), len);
8095 dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
8096 dest = fold_convert (type, dest);
8097 if (expr)
8098 dest = omit_one_operand (type, dest, expr);
8099 return dest;
8102 /* Fold function call to builtin bcopy. Return NULL_TREE if no
8103 simplification can be made. */
8105 static tree
8106 fold_builtin_bcopy (tree arglist, bool ignore)
8108 tree src, dest, size, newarglist;
8110 if (!validate_arglist (arglist,
8111 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8112 return 0;
8114 if (! ignore)
8115 return 0;
8117 src = TREE_VALUE (arglist);
8118 dest = TREE_VALUE (TREE_CHAIN (arglist));
8119 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8121 /* New argument list transforming bcopy(ptr x, ptr y, int z) to
8122 memmove(ptr y, ptr x, size_t z). This is done this way
8123 so that if it isn't expanded inline, we fallback to
8124 calling bcopy instead of memmove. */
8126 newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size));
8127 newarglist = tree_cons (NULL_TREE, src, newarglist);
8128 newarglist = tree_cons (NULL_TREE, dest, newarglist);
8130 return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3);
8133 /* Fold function call to builtin strcpy. If LEN is not NULL, it represents
8134 the length of the string to be copied. Return NULL_TREE if no
8135 simplification can be made. */
8137 tree
8138 fold_builtin_strcpy (tree fndecl, tree arglist, tree len)
8140 tree dest, src, fn;
8142 if (!validate_arglist (arglist,
8143 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8144 return 0;
8146 dest = TREE_VALUE (arglist);
8147 src = TREE_VALUE (TREE_CHAIN (arglist));
8149 /* If SRC and DEST are the same (and not volatile), return DEST. */
8150 if (operand_equal_p (src, dest, 0))
8151 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
8153 if (optimize_size)
8154 return 0;
8156 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8157 if (!fn)
8158 return 0;
8160 if (!len)
8162 len = c_strlen (src, 1);
8163 if (! len || TREE_SIDE_EFFECTS (len))
8164 return 0;
8167 len = size_binop (PLUS_EXPR, len, ssize_int (1));
8168 arglist = build_tree_list (NULL_TREE, len);
8169 arglist = tree_cons (NULL_TREE, src, arglist);
8170 arglist = tree_cons (NULL_TREE, dest, arglist);
8171 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8172 build_function_call_expr (fn, arglist));
8175 /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents
8176 the length of the source string. Return NULL_TREE if no simplification
8177 can be made. */
8179 tree
8180 fold_builtin_strncpy (tree fndecl, tree arglist, tree slen)
8182 tree dest, src, len, fn;
8184 if (!validate_arglist (arglist,
8185 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8186 return 0;
8188 dest = TREE_VALUE (arglist);
8189 src = TREE_VALUE (TREE_CHAIN (arglist));
8190 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8192 /* If the LEN parameter is zero, return DEST. */
8193 if (integer_zerop (len))
8194 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8196 /* We can't compare slen with len as constants below if len is not a
8197 constant. */
8198 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8199 return 0;
8201 if (!slen)
8202 slen = c_strlen (src, 1);
8204 /* Now, we must be passed a constant src ptr parameter. */
8205 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8206 return 0;
8208 slen = size_binop (PLUS_EXPR, slen, ssize_int (1));
8210 /* We do not support simplification of this case, though we do
8211 support it when expanding trees into RTL. */
8212 /* FIXME: generate a call to __builtin_memset. */
8213 if (tree_int_cst_lt (slen, len))
8214 return 0;
8216 /* OK transform into builtin memcpy. */
8217 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8218 if (!fn)
8219 return 0;
8220 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
8221 build_function_call_expr (fn, arglist));
8224 /* Fold function call to builtin memcmp. Return
8225 NULL_TREE if no simplification can be made. */
8227 static tree
8228 fold_builtin_memcmp (tree arglist)
8230 tree arg1, arg2, len;
8231 const char *p1, *p2;
8233 if (!validate_arglist (arglist,
8234 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8235 return 0;
8237 arg1 = TREE_VALUE (arglist);
8238 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8239 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8241 /* If the LEN parameter is zero, return zero. */
8242 if (integer_zerop (len))
8243 return omit_two_operands (integer_type_node, integer_zero_node,
8244 arg1, arg2);
8246 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8247 if (operand_equal_p (arg1, arg2, 0))
8248 return omit_one_operand (integer_type_node, integer_zero_node, len);
8250 p1 = c_getstr (arg1);
8251 p2 = c_getstr (arg2);
8253 /* If all arguments are constant, and the value of len is not greater
8254 than the lengths of arg1 and arg2, evaluate at compile-time. */
8255 if (host_integerp (len, 1) && p1 && p2
8256 && compare_tree_int (len, strlen (p1) + 1) <= 0
8257 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8259 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8261 if (r > 0)
8262 return integer_one_node;
8263 else if (r < 0)
8264 return integer_minus_one_node;
8265 else
8266 return integer_zero_node;
8269 /* If len parameter is one, return an expression corresponding to
8270 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8271 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8273 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8274 tree cst_uchar_ptr_node
8275 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8277 tree ind1 = fold_convert (integer_type_node,
8278 build1 (INDIRECT_REF, cst_uchar_node,
8279 fold_convert (cst_uchar_ptr_node,
8280 arg1)));
8281 tree ind2 = fold_convert (integer_type_node,
8282 build1 (INDIRECT_REF, cst_uchar_node,
8283 fold_convert (cst_uchar_ptr_node,
8284 arg2)));
8285 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8288 return 0;
8291 /* Fold function call to builtin strcmp. Return
8292 NULL_TREE if no simplification can be made. */
8294 static tree
8295 fold_builtin_strcmp (tree arglist)
8297 tree arg1, arg2;
8298 const char *p1, *p2;
8300 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
8301 return 0;
8303 arg1 = TREE_VALUE (arglist);
8304 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8306 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8307 if (operand_equal_p (arg1, arg2, 0))
8308 return integer_zero_node;
8310 p1 = c_getstr (arg1);
8311 p2 = c_getstr (arg2);
8313 if (p1 && p2)
8315 const int i = strcmp (p1, p2);
8316 if (i < 0)
8317 return integer_minus_one_node;
8318 else if (i > 0)
8319 return integer_one_node;
8320 else
8321 return integer_zero_node;
8324 /* If the second arg is "", return *(const unsigned char*)arg1. */
8325 if (p2 && *p2 == '\0')
8327 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8328 tree cst_uchar_ptr_node
8329 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8331 return fold_convert (integer_type_node,
8332 build1 (INDIRECT_REF, cst_uchar_node,
8333 fold_convert (cst_uchar_ptr_node,
8334 arg1)));
8337 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8338 if (p1 && *p1 == '\0')
8340 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8341 tree cst_uchar_ptr_node
8342 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8344 tree temp = fold_convert (integer_type_node,
8345 build1 (INDIRECT_REF, cst_uchar_node,
8346 fold_convert (cst_uchar_ptr_node,
8347 arg2)));
8348 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8351 return 0;
8354 /* Fold function call to builtin strncmp. Return
8355 NULL_TREE if no simplification can be made. */
8357 static tree
8358 fold_builtin_strncmp (tree arglist)
8360 tree arg1, arg2, len;
8361 const char *p1, *p2;
8363 if (!validate_arglist (arglist,
8364 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
8365 return 0;
8367 arg1 = TREE_VALUE (arglist);
8368 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8369 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
8371 /* If the LEN parameter is zero, return zero. */
8372 if (integer_zerop (len))
8373 return omit_two_operands (integer_type_node, integer_zero_node,
8374 arg1, arg2);
8376 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8377 if (operand_equal_p (arg1, arg2, 0))
8378 return omit_one_operand (integer_type_node, integer_zero_node, len);
8380 p1 = c_getstr (arg1);
8381 p2 = c_getstr (arg2);
8383 if (host_integerp (len, 1) && p1 && p2)
8385 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8386 if (i > 0)
8387 return integer_one_node;
8388 else if (i < 0)
8389 return integer_minus_one_node;
8390 else
8391 return integer_zero_node;
8394 /* If the second arg is "", and the length is greater than zero,
8395 return *(const unsigned char*)arg1. */
8396 if (p2 && *p2 == '\0'
8397 && TREE_CODE (len) == INTEGER_CST
8398 && tree_int_cst_sgn (len) == 1)
8400 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8401 tree cst_uchar_ptr_node
8402 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8404 return fold_convert (integer_type_node,
8405 build1 (INDIRECT_REF, cst_uchar_node,
8406 fold_convert (cst_uchar_ptr_node,
8407 arg1)));
8410 /* If the first arg is "", and the length is greater than zero,
8411 return -*(const unsigned char*)arg2. */
8412 if (p1 && *p1 == '\0'
8413 && TREE_CODE (len) == INTEGER_CST
8414 && tree_int_cst_sgn (len) == 1)
8416 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8417 tree cst_uchar_ptr_node
8418 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8420 tree temp = fold_convert (integer_type_node,
8421 build1 (INDIRECT_REF, cst_uchar_node,
8422 fold_convert (cst_uchar_ptr_node,
8423 arg2)));
8424 return fold_build1 (NEGATE_EXPR, integer_type_node, temp);
8427 /* If len parameter is one, return an expression corresponding to
8428 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8429 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8431 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8432 tree cst_uchar_ptr_node
8433 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8435 tree ind1 = fold_convert (integer_type_node,
8436 build1 (INDIRECT_REF, cst_uchar_node,
8437 fold_convert (cst_uchar_ptr_node,
8438 arg1)));
8439 tree ind2 = fold_convert (integer_type_node,
8440 build1 (INDIRECT_REF, cst_uchar_node,
8441 fold_convert (cst_uchar_ptr_node,
8442 arg2)));
8443 return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2);
8446 return 0;
8449 /* Fold function call to builtin signbit, signbitf or signbitl. Return
8450 NULL_TREE if no simplification can be made. */
8452 static tree
8453 fold_builtin_signbit (tree fndecl, tree arglist)
8455 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8456 tree arg, temp;
8458 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8459 return NULL_TREE;
8461 arg = TREE_VALUE (arglist);
8463 /* If ARG is a compile-time constant, determine the result. */
8464 if (TREE_CODE (arg) == REAL_CST
8465 && !TREE_CONSTANT_OVERFLOW (arg))
8467 REAL_VALUE_TYPE c;
8469 c = TREE_REAL_CST (arg);
8470 temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node;
8471 return fold_convert (type, temp);
8474 /* If ARG is non-negative, the result is always zero. */
8475 if (tree_expr_nonnegative_p (arg))
8476 return omit_one_operand (type, integer_zero_node, arg);
8478 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8479 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8480 return fold_build2 (LT_EXPR, type, arg,
8481 build_real (TREE_TYPE (arg), dconst0));
8483 return NULL_TREE;
8486 /* Fold function call to builtin copysign, copysignf or copysignl.
8487 Return NULL_TREE if no simplification can be made. */
8489 static tree
8490 fold_builtin_copysign (tree fndecl, tree arglist, tree type)
8492 tree arg1, arg2, tem;
8494 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8495 return NULL_TREE;
8497 arg1 = TREE_VALUE (arglist);
8498 arg2 = TREE_VALUE (TREE_CHAIN (arglist));
8500 /* copysign(X,X) is X. */
8501 if (operand_equal_p (arg1, arg2, 0))
8502 return fold_convert (type, arg1);
8504 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8505 if (TREE_CODE (arg1) == REAL_CST
8506 && TREE_CODE (arg2) == REAL_CST
8507 && !TREE_CONSTANT_OVERFLOW (arg1)
8508 && !TREE_CONSTANT_OVERFLOW (arg2))
8510 REAL_VALUE_TYPE c1, c2;
8512 c1 = TREE_REAL_CST (arg1);
8513 c2 = TREE_REAL_CST (arg2);
8514 /* c1.sign := c2.sign. */
8515 real_copysign (&c1, &c2);
8516 return build_real (type, c1);
8519 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8520 Remember to evaluate Y for side-effects. */
8521 if (tree_expr_nonnegative_p (arg2))
8522 return omit_one_operand (type,
8523 fold_build1 (ABS_EXPR, type, arg1),
8524 arg2);
8526 /* Strip sign changing operations for the first argument. */
8527 tem = fold_strip_sign_ops (arg1);
8528 if (tem)
8530 arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist));
8531 return build_function_call_expr (fndecl, arglist);
8534 return NULL_TREE;
8537 /* Fold a call to builtin isascii. */
8539 static tree
8540 fold_builtin_isascii (tree arglist)
8542 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8543 return 0;
8544 else
8546 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8547 tree arg = TREE_VALUE (arglist);
8549 arg = build2 (BIT_AND_EXPR, integer_type_node, arg,
8550 build_int_cst (NULL_TREE,
8551 ~ (unsigned HOST_WIDE_INT) 0x7f));
8552 arg = fold_build2 (EQ_EXPR, integer_type_node,
8553 arg, integer_zero_node);
8555 if (in_gimple_form && !TREE_CONSTANT (arg))
8556 return NULL_TREE;
8557 else
8558 return arg;
8562 /* Fold a call to builtin toascii. */
8564 static tree
8565 fold_builtin_toascii (tree arglist)
8567 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8568 return 0;
8569 else
8571 /* Transform toascii(c) -> (c & 0x7f). */
8572 tree arg = TREE_VALUE (arglist);
8574 return fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8575 build_int_cst (NULL_TREE, 0x7f));
8579 /* Fold a call to builtin isdigit. */
8581 static tree
8582 fold_builtin_isdigit (tree arglist)
8584 if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8585 return 0;
8586 else
8588 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8589 /* According to the C standard, isdigit is unaffected by locale.
8590 However, it definitely is affected by the target character set. */
8591 tree arg;
8592 unsigned HOST_WIDE_INT target_digit0
8593 = lang_hooks.to_target_charset ('0');
8595 if (target_digit0 == 0)
8596 return NULL_TREE;
8598 arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist));
8599 arg = build2 (MINUS_EXPR, unsigned_type_node, arg,
8600 build_int_cst (unsigned_type_node, target_digit0));
8601 arg = fold_build2 (LE_EXPR, integer_type_node, arg,
8602 build_int_cst (unsigned_type_node, 9));
8603 if (in_gimple_form && !TREE_CONSTANT (arg))
8604 return NULL_TREE;
8605 else
8606 return arg;
8610 /* Fold a call to fabs, fabsf or fabsl. */
8612 static tree
8613 fold_builtin_fabs (tree arglist, tree type)
8615 tree arg;
8617 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8618 return 0;
8620 arg = TREE_VALUE (arglist);
8621 arg = fold_convert (type, arg);
8622 if (TREE_CODE (arg) == REAL_CST)
8623 return fold_abs_const (arg, type);
8624 return fold_build1 (ABS_EXPR, type, arg);
8627 /* Fold a call to abs, labs, llabs or imaxabs. */
8629 static tree
8630 fold_builtin_abs (tree arglist, tree type)
8632 tree arg;
8634 if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE))
8635 return 0;
8637 arg = TREE_VALUE (arglist);
8638 arg = fold_convert (type, arg);
8639 if (TREE_CODE (arg) == INTEGER_CST)
8640 return fold_abs_const (arg, type);
8641 return fold_build1 (ABS_EXPR, type, arg);
8644 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8645 EXP is the CALL_EXPR for the call. */
8647 static tree
8648 fold_builtin_classify (tree fndecl, tree arglist, int builtin_index)
8650 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8651 tree arg;
8652 REAL_VALUE_TYPE r;
8654 if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
8656 /* Check that we have exactly one argument. */
8657 if (arglist == 0)
8659 error ("too few arguments to function %qs",
8660 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8661 return error_mark_node;
8663 else if (TREE_CHAIN (arglist) != 0)
8665 error ("too many arguments to function %qs",
8666 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8667 return error_mark_node;
8669 else
8671 error ("non-floating-point argument to function %qs",
8672 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8673 return error_mark_node;
8677 arg = TREE_VALUE (arglist);
8678 switch (builtin_index)
8680 case BUILT_IN_ISINF:
8681 if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8682 return omit_one_operand (type, integer_zero_node, arg);
8684 if (TREE_CODE (arg) == REAL_CST)
8686 r = TREE_REAL_CST (arg);
8687 if (real_isinf (&r))
8688 return real_compare (GT_EXPR, &r, &dconst0)
8689 ? integer_one_node : integer_minus_one_node;
8690 else
8691 return integer_zero_node;
8694 return NULL_TREE;
8696 case BUILT_IN_FINITE:
8697 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))
8698 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
8699 return omit_one_operand (type, integer_zero_node, arg);
8701 if (TREE_CODE (arg) == REAL_CST)
8703 r = TREE_REAL_CST (arg);
8704 return real_isinf (&r) || real_isnan (&r)
8705 ? integer_zero_node : integer_one_node;
8708 return NULL_TREE;
8710 case BUILT_IN_ISNAN:
8711 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))))
8712 return omit_one_operand (type, integer_zero_node, arg);
8714 if (TREE_CODE (arg) == REAL_CST)
8716 r = TREE_REAL_CST (arg);
8717 return real_isnan (&r) ? integer_one_node : integer_zero_node;
8720 arg = builtin_save_expr (arg);
8721 return fold_build2 (UNORDERED_EXPR, type, arg, arg);
8723 default:
8724 gcc_unreachable ();
8728 /* Fold a call to an unordered comparison function such as
8729 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8730 being called and ARGLIST is the argument list for the call.
8731 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8732 the opposite of the desired result. UNORDERED_CODE is used
8733 for modes that can hold NaNs and ORDERED_CODE is used for
8734 the rest. */
8736 static tree
8737 fold_builtin_unordered_cmp (tree fndecl, tree arglist,
8738 enum tree_code unordered_code,
8739 enum tree_code ordered_code)
8741 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8742 enum tree_code code;
8743 tree arg0, arg1;
8744 tree type0, type1;
8745 enum tree_code code0, code1;
8746 tree cmp_type = NULL_TREE;
8748 if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE))
8750 /* Check that we have exactly two arguments. */
8751 if (arglist == 0 || TREE_CHAIN (arglist) == 0)
8753 error ("too few arguments to function %qs",
8754 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8755 return error_mark_node;
8757 else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0)
8759 error ("too many arguments to function %qs",
8760 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8761 return error_mark_node;
8765 arg0 = TREE_VALUE (arglist);
8766 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
8768 type0 = TREE_TYPE (arg0);
8769 type1 = TREE_TYPE (arg1);
8771 code0 = TREE_CODE (type0);
8772 code1 = TREE_CODE (type1);
8774 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8775 /* Choose the wider of two real types. */
8776 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8777 ? type0 : type1;
8778 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8779 cmp_type = type0;
8780 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8781 cmp_type = type1;
8782 else
8784 error ("non-floating-point argument to function %qs",
8785 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
8786 return error_mark_node;
8789 arg0 = fold_convert (cmp_type, arg0);
8790 arg1 = fold_convert (cmp_type, arg1);
8792 if (unordered_code == UNORDERED_EXPR)
8794 if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8795 return omit_two_operands (type, integer_zero_node, arg0, arg1);
8796 return fold_build2 (UNORDERED_EXPR, type, arg0, arg1);
8799 code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
8800 : ordered_code;
8801 return fold_build1 (TRUTH_NOT_EXPR, type,
8802 fold_build2 (code, type, arg0, arg1));
8805 /* Used by constant folding to simplify calls to builtin functions. EXP is
8806 the CALL_EXPR of a call to a builtin function. IGNORE is true if the
8807 result of the function call is ignored. This function returns NULL_TREE
8808 if no simplification was possible. */
8810 static tree
8811 fold_builtin_1 (tree fndecl, tree arglist, bool ignore)
8813 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8814 enum built_in_function fcode;
8816 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
8817 return targetm.fold_builtin (fndecl, arglist, ignore);
8819 fcode = DECL_FUNCTION_CODE (fndecl);
8820 switch (fcode)
8822 case BUILT_IN_FPUTS:
8823 return fold_builtin_fputs (arglist, ignore, false, NULL_TREE);
8825 case BUILT_IN_FPUTS_UNLOCKED:
8826 return fold_builtin_fputs (arglist, ignore, true, NULL_TREE);
8828 case BUILT_IN_STRSTR:
8829 return fold_builtin_strstr (arglist, type);
8831 case BUILT_IN_STRCAT:
8832 return fold_builtin_strcat (arglist);
8834 case BUILT_IN_STRNCAT:
8835 return fold_builtin_strncat (arglist);
8837 case BUILT_IN_STRSPN:
8838 return fold_builtin_strspn (arglist);
8840 case BUILT_IN_STRCSPN:
8841 return fold_builtin_strcspn (arglist);
8843 case BUILT_IN_STRCHR:
8844 case BUILT_IN_INDEX:
8845 return fold_builtin_strchr (arglist, type);
8847 case BUILT_IN_STRRCHR:
8848 case BUILT_IN_RINDEX:
8849 return fold_builtin_strrchr (arglist, type);
8851 case BUILT_IN_STRCPY:
8852 return fold_builtin_strcpy (fndecl, arglist, NULL_TREE);
8854 case BUILT_IN_STRNCPY:
8855 return fold_builtin_strncpy (fndecl, arglist, NULL_TREE);
8857 case BUILT_IN_STRCMP:
8858 return fold_builtin_strcmp (arglist);
8860 case BUILT_IN_STRNCMP:
8861 return fold_builtin_strncmp (arglist);
8863 case BUILT_IN_STRPBRK:
8864 return fold_builtin_strpbrk (arglist, type);
8866 case BUILT_IN_BCMP:
8867 case BUILT_IN_MEMCMP:
8868 return fold_builtin_memcmp (arglist);
8870 case BUILT_IN_SPRINTF:
8871 return fold_builtin_sprintf (arglist, ignore);
8873 case BUILT_IN_CONSTANT_P:
8875 tree val;
8877 val = fold_builtin_constant_p (arglist);
8878 /* Gimplification will pull the CALL_EXPR for the builtin out of
8879 an if condition. When not optimizing, we'll not CSE it back.
8880 To avoid link error types of regressions, return false now. */
8881 if (!val && !optimize)
8882 val = integer_zero_node;
8884 return val;
8887 case BUILT_IN_EXPECT:
8888 return fold_builtin_expect (arglist);
8890 case BUILT_IN_CLASSIFY_TYPE:
8891 return fold_builtin_classify_type (arglist);
8893 case BUILT_IN_STRLEN:
8894 return fold_builtin_strlen (arglist);
8896 CASE_FLT_FN (BUILT_IN_FABS):
8897 return fold_builtin_fabs (arglist, type);
8899 case BUILT_IN_ABS:
8900 case BUILT_IN_LABS:
8901 case BUILT_IN_LLABS:
8902 case BUILT_IN_IMAXABS:
8903 return fold_builtin_abs (arglist, type);
8905 CASE_FLT_FN (BUILT_IN_CONJ):
8906 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8907 return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist));
8908 break;
8910 CASE_FLT_FN (BUILT_IN_CREAL):
8911 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8912 return non_lvalue (fold_build1 (REALPART_EXPR, type,
8913 TREE_VALUE (arglist)));
8914 break;
8916 CASE_FLT_FN (BUILT_IN_CIMAG):
8917 if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE))
8918 return non_lvalue (fold_build1 (IMAGPART_EXPR, type,
8919 TREE_VALUE (arglist)));
8920 break;
8922 CASE_FLT_FN (BUILT_IN_CABS):
8923 return fold_builtin_cabs (arglist, type, fndecl);
8925 CASE_FLT_FN (BUILT_IN_SQRT):
8926 return fold_builtin_sqrt (arglist, type);
8928 CASE_FLT_FN (BUILT_IN_CBRT):
8929 return fold_builtin_cbrt (arglist, type);
8931 CASE_FLT_FN (BUILT_IN_SIN):
8932 return fold_builtin_sin (arglist);
8934 CASE_FLT_FN (BUILT_IN_COS):
8935 return fold_builtin_cos (arglist, type, fndecl);
8937 CASE_FLT_FN (BUILT_IN_EXP):
8938 return fold_builtin_exponent (fndecl, arglist, &dconste);
8940 CASE_FLT_FN (BUILT_IN_EXP2):
8941 return fold_builtin_exponent (fndecl, arglist, &dconst2);
8943 CASE_FLT_FN (BUILT_IN_EXP10):
8944 CASE_FLT_FN (BUILT_IN_POW10):
8945 return fold_builtin_exponent (fndecl, arglist, &dconst10);
8947 CASE_FLT_FN (BUILT_IN_LOG):
8948 return fold_builtin_logarithm (fndecl, arglist, &dconste);
8950 CASE_FLT_FN (BUILT_IN_LOG2):
8951 return fold_builtin_logarithm (fndecl, arglist, &dconst2);
8953 CASE_FLT_FN (BUILT_IN_LOG10):
8954 return fold_builtin_logarithm (fndecl, arglist, &dconst10);
8956 CASE_FLT_FN (BUILT_IN_TAN):
8957 return fold_builtin_tan (arglist);
8959 CASE_FLT_FN (BUILT_IN_ATAN):
8960 return fold_builtin_atan (arglist, type);
8962 CASE_FLT_FN (BUILT_IN_POW):
8963 return fold_builtin_pow (fndecl, arglist, type);
8965 CASE_FLT_FN (BUILT_IN_POWI):
8966 return fold_builtin_powi (fndecl, arglist, type);
8968 CASE_FLT_FN (BUILT_IN_INF):
8969 case BUILT_IN_INFD32:
8970 case BUILT_IN_INFD64:
8971 case BUILT_IN_INFD128:
8972 return fold_builtin_inf (type, true);
8974 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
8975 return fold_builtin_inf (type, false);
8977 CASE_FLT_FN (BUILT_IN_NAN):
8978 case BUILT_IN_NAND32:
8979 case BUILT_IN_NAND64:
8980 case BUILT_IN_NAND128:
8981 return fold_builtin_nan (arglist, type, true);
8983 CASE_FLT_FN (BUILT_IN_NANS):
8984 return fold_builtin_nan (arglist, type, false);
8986 CASE_FLT_FN (BUILT_IN_FLOOR):
8987 return fold_builtin_floor (fndecl, arglist);
8989 CASE_FLT_FN (BUILT_IN_CEIL):
8990 return fold_builtin_ceil (fndecl, arglist);
8992 CASE_FLT_FN (BUILT_IN_TRUNC):
8993 return fold_builtin_trunc (fndecl, arglist);
8995 CASE_FLT_FN (BUILT_IN_ROUND):
8996 return fold_builtin_round (fndecl, arglist);
8998 CASE_FLT_FN (BUILT_IN_NEARBYINT):
8999 CASE_FLT_FN (BUILT_IN_RINT):
9000 return fold_trunc_transparent_mathfn (fndecl, arglist);
9002 CASE_FLT_FN (BUILT_IN_LCEIL):
9003 CASE_FLT_FN (BUILT_IN_LLCEIL):
9004 CASE_FLT_FN (BUILT_IN_LFLOOR):
9005 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9006 CASE_FLT_FN (BUILT_IN_LROUND):
9007 CASE_FLT_FN (BUILT_IN_LLROUND):
9008 return fold_builtin_int_roundingfn (fndecl, arglist);
9010 CASE_FLT_FN (BUILT_IN_LRINT):
9011 CASE_FLT_FN (BUILT_IN_LLRINT):
9012 return fold_fixed_mathfn (fndecl, arglist);
9014 CASE_INT_FN (BUILT_IN_FFS):
9015 CASE_INT_FN (BUILT_IN_CLZ):
9016 CASE_INT_FN (BUILT_IN_CTZ):
9017 CASE_INT_FN (BUILT_IN_POPCOUNT):
9018 CASE_INT_FN (BUILT_IN_PARITY):
9019 return fold_builtin_bitop (fndecl, arglist);
9021 case BUILT_IN_MEMSET:
9022 return fold_builtin_memset (arglist, type, ignore);
9024 case BUILT_IN_MEMCPY:
9025 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0);
9027 case BUILT_IN_MEMPCPY:
9028 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1);
9030 case BUILT_IN_MEMMOVE:
9031 return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3);
9033 case BUILT_IN_BZERO:
9034 return fold_builtin_bzero (arglist, ignore);
9036 case BUILT_IN_BCOPY:
9037 return fold_builtin_bcopy (arglist, ignore);
9039 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9040 return fold_builtin_signbit (fndecl, arglist);
9042 case BUILT_IN_ISASCII:
9043 return fold_builtin_isascii (arglist);
9045 case BUILT_IN_TOASCII:
9046 return fold_builtin_toascii (arglist);
9048 case BUILT_IN_ISDIGIT:
9049 return fold_builtin_isdigit (arglist);
9051 CASE_FLT_FN (BUILT_IN_COPYSIGN):
9052 return fold_builtin_copysign (fndecl, arglist, type);
9054 CASE_FLT_FN (BUILT_IN_FINITE):
9055 case BUILT_IN_FINITED32:
9056 case BUILT_IN_FINITED64:
9057 case BUILT_IN_FINITED128:
9058 return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE);
9060 CASE_FLT_FN (BUILT_IN_ISINF):
9061 case BUILT_IN_ISINFD32:
9062 case BUILT_IN_ISINFD64:
9063 case BUILT_IN_ISINFD128:
9064 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF);
9066 CASE_FLT_FN (BUILT_IN_ISNAN):
9067 case BUILT_IN_ISNAND32:
9068 case BUILT_IN_ISNAND64:
9069 case BUILT_IN_ISNAND128:
9070 return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN);
9072 case BUILT_IN_ISGREATER:
9073 return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR);
9074 case BUILT_IN_ISGREATEREQUAL:
9075 return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR);
9076 case BUILT_IN_ISLESS:
9077 return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR);
9078 case BUILT_IN_ISLESSEQUAL:
9079 return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR);
9080 case BUILT_IN_ISLESSGREATER:
9081 return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR);
9082 case BUILT_IN_ISUNORDERED:
9083 return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR,
9084 NOP_EXPR);
9086 /* We do the folding for va_start in the expander. */
9087 case BUILT_IN_VA_START:
9088 break;
9090 case BUILT_IN_OBJECT_SIZE:
9091 return fold_builtin_object_size (arglist);
9092 case BUILT_IN_MEMCPY_CHK:
9093 case BUILT_IN_MEMPCPY_CHK:
9094 case BUILT_IN_MEMMOVE_CHK:
9095 case BUILT_IN_MEMSET_CHK:
9096 return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore,
9097 DECL_FUNCTION_CODE (fndecl));
9098 case BUILT_IN_STRCPY_CHK:
9099 case BUILT_IN_STPCPY_CHK:
9100 return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore,
9101 DECL_FUNCTION_CODE (fndecl));
9102 case BUILT_IN_STRNCPY_CHK:
9103 return fold_builtin_strncpy_chk (arglist, NULL_TREE);
9104 case BUILT_IN_STRCAT_CHK:
9105 return fold_builtin_strcat_chk (fndecl, arglist);
9106 case BUILT_IN_STRNCAT_CHK:
9107 return fold_builtin_strncat_chk (fndecl, arglist);
9108 case BUILT_IN_SPRINTF_CHK:
9109 case BUILT_IN_VSPRINTF_CHK:
9110 return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl));
9111 case BUILT_IN_SNPRINTF_CHK:
9112 case BUILT_IN_VSNPRINTF_CHK:
9113 return fold_builtin_snprintf_chk (arglist, NULL_TREE,
9114 DECL_FUNCTION_CODE (fndecl));
9116 case BUILT_IN_PRINTF:
9117 case BUILT_IN_PRINTF_UNLOCKED:
9118 case BUILT_IN_VPRINTF:
9119 case BUILT_IN_PRINTF_CHK:
9120 case BUILT_IN_VPRINTF_CHK:
9121 return fold_builtin_printf (fndecl, arglist, ignore,
9122 DECL_FUNCTION_CODE (fndecl));
9124 case BUILT_IN_FPRINTF:
9125 case BUILT_IN_FPRINTF_UNLOCKED:
9126 case BUILT_IN_VFPRINTF:
9127 case BUILT_IN_FPRINTF_CHK:
9128 case BUILT_IN_VFPRINTF_CHK:
9129 return fold_builtin_fprintf (fndecl, arglist, ignore,
9130 DECL_FUNCTION_CODE (fndecl));
9132 default:
9133 break;
9136 return 0;
9139 /* A wrapper function for builtin folding that prevents warnings for
9140 "statement without effect" and the like, caused by removing the
9141 call node earlier than the warning is generated. */
9143 tree
9144 fold_builtin (tree fndecl, tree arglist, bool ignore)
9146 tree exp = fold_builtin_1 (fndecl, arglist, ignore);
9147 if (exp)
9149 exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp);
9150 TREE_NO_WARNING (exp) = 1;
9153 return exp;
9156 /* Conveniently construct a function call expression. */
9158 tree
9159 build_function_call_expr (tree fn, tree arglist)
9161 tree call_expr;
9163 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
9164 return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
9165 call_expr, arglist, NULL_TREE);
9168 /* This function validates the types of a function call argument list
9169 represented as a tree chain of parameters against a specified list
9170 of tree_codes. If the last specifier is a 0, that represents an
9171 ellipses, otherwise the last specifier must be a VOID_TYPE. */
9173 static int
9174 validate_arglist (tree arglist, ...)
9176 enum tree_code code;
9177 int res = 0;
9178 va_list ap;
9180 va_start (ap, arglist);
9184 code = va_arg (ap, enum tree_code);
9185 switch (code)
9187 case 0:
9188 /* This signifies an ellipses, any further arguments are all ok. */
9189 res = 1;
9190 goto end;
9191 case VOID_TYPE:
9192 /* This signifies an endlink, if no arguments remain, return
9193 true, otherwise return false. */
9194 res = arglist == 0;
9195 goto end;
9196 default:
9197 /* If no parameters remain or the parameter's code does not
9198 match the specified code, return false. Otherwise continue
9199 checking any remaining arguments. */
9200 if (arglist == 0)
9201 goto end;
9202 if (code == POINTER_TYPE)
9204 if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))))
9205 goto end;
9207 else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))))
9208 goto end;
9209 break;
9211 arglist = TREE_CHAIN (arglist);
9213 while (1);
9215 /* We need gotos here since we can only have one VA_CLOSE in a
9216 function. */
9217 end: ;
9218 va_end (ap);
9220 return res;
9223 /* Default target-specific builtin expander that does nothing. */
9226 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9227 rtx target ATTRIBUTE_UNUSED,
9228 rtx subtarget ATTRIBUTE_UNUSED,
9229 enum machine_mode mode ATTRIBUTE_UNUSED,
9230 int ignore ATTRIBUTE_UNUSED)
9232 return NULL_RTX;
9235 /* Returns true is EXP represents data that would potentially reside
9236 in a readonly section. */
9238 static bool
9239 readonly_data_expr (tree exp)
9241 STRIP_NOPS (exp);
9243 if (TREE_CODE (exp) != ADDR_EXPR)
9244 return false;
9246 exp = get_base_address (TREE_OPERAND (exp, 0));
9247 if (!exp)
9248 return false;
9250 /* Make sure we call decl_readonly_section only for trees it
9251 can handle (since it returns true for everything it doesn't
9252 understand). */
9253 if (TREE_CODE (exp) == STRING_CST
9254 || TREE_CODE (exp) == CONSTRUCTOR
9255 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
9256 return decl_readonly_section (exp, 0);
9257 else
9258 return false;
9261 /* Simplify a call to the strstr builtin.
9263 Return 0 if no simplification was possible, otherwise return the
9264 simplified form of the call as a tree.
9266 The simplified form may be a constant or other expression which
9267 computes the same value, but in a more efficient manner (including
9268 calls to other builtin functions).
9270 The call may contain arguments which need to be evaluated, but
9271 which are not useful to determine the result of the call. In
9272 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9273 COMPOUND_EXPR will be an argument which must be evaluated.
9274 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9275 COMPOUND_EXPR in the chain will contain the tree for the simplified
9276 form of the builtin function call. */
9278 static tree
9279 fold_builtin_strstr (tree arglist, tree type)
9281 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9282 return 0;
9283 else
9285 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9286 tree fn;
9287 const char *p1, *p2;
9289 p2 = c_getstr (s2);
9290 if (p2 == NULL)
9291 return 0;
9293 p1 = c_getstr (s1);
9294 if (p1 != NULL)
9296 const char *r = strstr (p1, p2);
9297 tree tem;
9299 if (r == NULL)
9300 return build_int_cst (TREE_TYPE (s1), 0);
9302 /* Return an offset into the constant string argument. */
9303 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9304 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9305 return fold_convert (type, tem);
9308 /* The argument is const char *, and the result is char *, so we need
9309 a type conversion here to avoid a warning. */
9310 if (p2[0] == '\0')
9311 return fold_convert (type, s1);
9313 if (p2[1] != '\0')
9314 return 0;
9316 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9317 if (!fn)
9318 return 0;
9320 /* New argument list transforming strstr(s1, s2) to
9321 strchr(s1, s2[0]). */
9322 arglist = build_tree_list (NULL_TREE,
9323 build_int_cst (NULL_TREE, p2[0]));
9324 arglist = tree_cons (NULL_TREE, s1, arglist);
9325 return build_function_call_expr (fn, arglist);
9329 /* Simplify a call to the strchr builtin.
9331 Return 0 if no simplification was possible, otherwise return the
9332 simplified form of the call as a tree.
9334 The simplified form may be a constant or other expression which
9335 computes the same value, but in a more efficient manner (including
9336 calls to other builtin functions).
9338 The call may contain arguments which need to be evaluated, but
9339 which are not useful to determine the result of the call. In
9340 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9341 COMPOUND_EXPR will be an argument which must be evaluated.
9342 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9343 COMPOUND_EXPR in the chain will contain the tree for the simplified
9344 form of the builtin function call. */
9346 static tree
9347 fold_builtin_strchr (tree arglist, tree type)
9349 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9350 return 0;
9351 else
9353 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9354 const char *p1;
9356 if (TREE_CODE (s2) != INTEGER_CST)
9357 return 0;
9359 p1 = c_getstr (s1);
9360 if (p1 != NULL)
9362 char c;
9363 const char *r;
9364 tree tem;
9366 if (target_char_cast (s2, &c))
9367 return 0;
9369 r = strchr (p1, c);
9371 if (r == NULL)
9372 return build_int_cst (TREE_TYPE (s1), 0);
9374 /* Return an offset into the constant string argument. */
9375 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9376 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9377 return fold_convert (type, tem);
9379 return 0;
9383 /* Simplify a call to the strrchr builtin.
9385 Return 0 if no simplification was possible, otherwise return the
9386 simplified form of the call as a tree.
9388 The simplified form may be a constant or other expression which
9389 computes the same value, but in a more efficient manner (including
9390 calls to other builtin functions).
9392 The call may contain arguments which need to be evaluated, but
9393 which are not useful to determine the result of the call. In
9394 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9395 COMPOUND_EXPR will be an argument which must be evaluated.
9396 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9397 COMPOUND_EXPR in the chain will contain the tree for the simplified
9398 form of the builtin function call. */
9400 static tree
9401 fold_builtin_strrchr (tree arglist, tree type)
9403 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9404 return 0;
9405 else
9407 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9408 tree fn;
9409 const char *p1;
9411 if (TREE_CODE (s2) != INTEGER_CST)
9412 return 0;
9414 p1 = c_getstr (s1);
9415 if (p1 != NULL)
9417 char c;
9418 const char *r;
9419 tree tem;
9421 if (target_char_cast (s2, &c))
9422 return 0;
9424 r = strrchr (p1, c);
9426 if (r == NULL)
9427 return build_int_cst (TREE_TYPE (s1), 0);
9429 /* Return an offset into the constant string argument. */
9430 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9431 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9432 return fold_convert (type, tem);
9435 if (! integer_zerop (s2))
9436 return 0;
9438 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9439 if (!fn)
9440 return 0;
9442 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9443 return build_function_call_expr (fn, arglist);
9447 /* Simplify a call to the strpbrk builtin.
9449 Return 0 if no simplification was possible, otherwise return the
9450 simplified form of the call as a tree.
9452 The simplified form may be a constant or other expression which
9453 computes the same value, but in a more efficient manner (including
9454 calls to other builtin functions).
9456 The call may contain arguments which need to be evaluated, but
9457 which are not useful to determine the result of the call. In
9458 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9459 COMPOUND_EXPR will be an argument which must be evaluated.
9460 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9461 COMPOUND_EXPR in the chain will contain the tree for the simplified
9462 form of the builtin function call. */
9464 static tree
9465 fold_builtin_strpbrk (tree arglist, tree type)
9467 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9468 return 0;
9469 else
9471 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9472 tree fn;
9473 const char *p1, *p2;
9475 p2 = c_getstr (s2);
9476 if (p2 == NULL)
9477 return 0;
9479 p1 = c_getstr (s1);
9480 if (p1 != NULL)
9482 const char *r = strpbrk (p1, p2);
9483 tree tem;
9485 if (r == NULL)
9486 return build_int_cst (TREE_TYPE (s1), 0);
9488 /* Return an offset into the constant string argument. */
9489 tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1),
9490 s1, build_int_cst (TREE_TYPE (s1), r - p1));
9491 return fold_convert (type, tem);
9494 if (p2[0] == '\0')
9495 /* strpbrk(x, "") == NULL.
9496 Evaluate and ignore s1 in case it had side-effects. */
9497 return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1);
9499 if (p2[1] != '\0')
9500 return 0; /* Really call strpbrk. */
9502 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
9503 if (!fn)
9504 return 0;
9506 /* New argument list transforming strpbrk(s1, s2) to
9507 strchr(s1, s2[0]). */
9508 arglist = build_tree_list (NULL_TREE,
9509 build_int_cst (NULL_TREE, p2[0]));
9510 arglist = tree_cons (NULL_TREE, s1, arglist);
9511 return build_function_call_expr (fn, arglist);
9515 /* Simplify a call to the strcat builtin.
9517 Return 0 if no simplification was possible, otherwise return the
9518 simplified form of the call as a tree.
9520 The simplified form may be a constant or other expression which
9521 computes the same value, but in a more efficient manner (including
9522 calls to other builtin functions).
9524 The call may contain arguments which need to be evaluated, but
9525 which are not useful to determine the result of the call. In
9526 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9527 COMPOUND_EXPR will be an argument which must be evaluated.
9528 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9529 COMPOUND_EXPR in the chain will contain the tree for the simplified
9530 form of the builtin function call. */
9532 static tree
9533 fold_builtin_strcat (tree arglist)
9535 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9536 return 0;
9537 else
9539 tree dst = TREE_VALUE (arglist),
9540 src = TREE_VALUE (TREE_CHAIN (arglist));
9541 const char *p = c_getstr (src);
9543 /* If the string length is zero, return the dst parameter. */
9544 if (p && *p == '\0')
9545 return dst;
9547 return 0;
9551 /* Simplify a call to the strncat builtin.
9553 Return 0 if no simplification was possible, otherwise return the
9554 simplified form of the call as a tree.
9556 The simplified form may be a constant or other expression which
9557 computes the same value, but in a more efficient manner (including
9558 calls to other builtin functions).
9560 The call may contain arguments which need to be evaluated, but
9561 which are not useful to determine the result of the call. In
9562 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9563 COMPOUND_EXPR will be an argument which must be evaluated.
9564 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9565 COMPOUND_EXPR in the chain will contain the tree for the simplified
9566 form of the builtin function call. */
9568 static tree
9569 fold_builtin_strncat (tree arglist)
9571 if (!validate_arglist (arglist,
9572 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9573 return 0;
9574 else
9576 tree dst = TREE_VALUE (arglist);
9577 tree src = TREE_VALUE (TREE_CHAIN (arglist));
9578 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9579 const char *p = c_getstr (src);
9581 /* If the requested length is zero, or the src parameter string
9582 length is zero, return the dst parameter. */
9583 if (integer_zerop (len) || (p && *p == '\0'))
9584 return omit_two_operands (TREE_TYPE (dst), dst, src, len);
9586 /* If the requested len is greater than or equal to the string
9587 length, call strcat. */
9588 if (TREE_CODE (len) == INTEGER_CST && p
9589 && compare_tree_int (len, strlen (p)) >= 0)
9591 tree newarglist
9592 = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src));
9593 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
9595 /* If the replacement _DECL isn't initialized, don't do the
9596 transformation. */
9597 if (!fn)
9598 return 0;
9600 return build_function_call_expr (fn, newarglist);
9602 return 0;
9606 /* Simplify a call to the strspn builtin.
9608 Return 0 if no simplification was possible, otherwise return the
9609 simplified form of the call as a tree.
9611 The simplified form may be a constant or other expression which
9612 computes the same value, but in a more efficient manner (including
9613 calls to other builtin functions).
9615 The call may contain arguments which need to be evaluated, but
9616 which are not useful to determine the result of the call. In
9617 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9618 COMPOUND_EXPR will be an argument which must be evaluated.
9619 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9620 COMPOUND_EXPR in the chain will contain the tree for the simplified
9621 form of the builtin function call. */
9623 static tree
9624 fold_builtin_strspn (tree arglist)
9626 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9627 return 0;
9628 else
9630 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9631 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9633 /* If both arguments are constants, evaluate at compile-time. */
9634 if (p1 && p2)
9636 const size_t r = strspn (p1, p2);
9637 return size_int (r);
9640 /* If either argument is "", return 0. */
9641 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9642 /* Evaluate and ignore both arguments in case either one has
9643 side-effects. */
9644 return omit_two_operands (integer_type_node, integer_zero_node,
9645 s1, s2);
9646 return 0;
9650 /* Simplify a call to the strcspn builtin.
9652 Return 0 if no simplification was possible, otherwise return the
9653 simplified form of the call as a tree.
9655 The simplified form may be a constant or other expression which
9656 computes the same value, but in a more efficient manner (including
9657 calls to other builtin functions).
9659 The call may contain arguments which need to be evaluated, but
9660 which are not useful to determine the result of the call. In
9661 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9662 COMPOUND_EXPR will be an argument which must be evaluated.
9663 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9664 COMPOUND_EXPR in the chain will contain the tree for the simplified
9665 form of the builtin function call. */
9667 static tree
9668 fold_builtin_strcspn (tree arglist)
9670 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9671 return 0;
9672 else
9674 tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
9675 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9677 /* If both arguments are constants, evaluate at compile-time. */
9678 if (p1 && p2)
9680 const size_t r = strcspn (p1, p2);
9681 return size_int (r);
9684 /* If the first argument is "", return 0. */
9685 if (p1 && *p1 == '\0')
9687 /* Evaluate and ignore argument s2 in case it has
9688 side-effects. */
9689 return omit_one_operand (integer_type_node,
9690 integer_zero_node, s2);
9693 /* If the second argument is "", return __builtin_strlen(s1). */
9694 if (p2 && *p2 == '\0')
9696 tree newarglist = build_tree_list (NULL_TREE, s1),
9697 fn = implicit_built_in_decls[BUILT_IN_STRLEN];
9699 /* If the replacement _DECL isn't initialized, don't do the
9700 transformation. */
9701 if (!fn)
9702 return 0;
9704 return build_function_call_expr (fn, newarglist);
9706 return 0;
9710 /* Fold a call to the fputs builtin. IGNORE is true if the value returned
9711 by the builtin will be ignored. UNLOCKED is true is true if this
9712 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
9713 the known length of the string. Return NULL_TREE if no simplification
9714 was possible. */
9716 tree
9717 fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len)
9719 tree fn;
9720 /* If we're using an unlocked function, assume the other unlocked
9721 functions exist explicitly. */
9722 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
9723 : implicit_built_in_decls[BUILT_IN_FPUTC];
9724 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
9725 : implicit_built_in_decls[BUILT_IN_FWRITE];
9727 /* If the return value is used, don't do the transformation. */
9728 if (!ignore)
9729 return 0;
9731 /* Verify the arguments in the original call. */
9732 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
9733 return 0;
9735 if (! len)
9736 len = c_strlen (TREE_VALUE (arglist), 0);
9738 /* Get the length of the string passed to fputs. If the length
9739 can't be determined, punt. */
9740 if (!len
9741 || TREE_CODE (len) != INTEGER_CST)
9742 return 0;
9744 switch (compare_tree_int (len, 1))
9746 case -1: /* length is 0, delete the call entirely . */
9747 return omit_one_operand (integer_type_node, integer_zero_node,
9748 TREE_VALUE (TREE_CHAIN (arglist)));
9750 case 0: /* length is 1, call fputc. */
9752 const char *p = c_getstr (TREE_VALUE (arglist));
9754 if (p != NULL)
9756 /* New argument list transforming fputs(string, stream) to
9757 fputc(string[0], stream). */
9758 arglist = build_tree_list (NULL_TREE,
9759 TREE_VALUE (TREE_CHAIN (arglist)));
9760 arglist = tree_cons (NULL_TREE,
9761 build_int_cst (NULL_TREE, p[0]),
9762 arglist);
9763 fn = fn_fputc;
9764 break;
9767 /* FALLTHROUGH */
9768 case 1: /* length is greater than 1, call fwrite. */
9770 tree string_arg;
9772 /* If optimizing for size keep fputs. */
9773 if (optimize_size)
9774 return 0;
9775 string_arg = TREE_VALUE (arglist);
9776 /* New argument list transforming fputs(string, stream) to
9777 fwrite(string, 1, len, stream). */
9778 arglist = build_tree_list (NULL_TREE,
9779 TREE_VALUE (TREE_CHAIN (arglist)));
9780 arglist = tree_cons (NULL_TREE, len, arglist);
9781 arglist = tree_cons (NULL_TREE, size_one_node, arglist);
9782 arglist = tree_cons (NULL_TREE, string_arg, arglist);
9783 fn = fn_fwrite;
9784 break;
9786 default:
9787 gcc_unreachable ();
9790 /* If the replacement _DECL isn't initialized, don't do the
9791 transformation. */
9792 if (!fn)
9793 return 0;
9795 /* These optimizations are only performed when the result is ignored,
9796 hence there's no need to cast the result to integer_type_node. */
9797 return build_function_call_expr (fn, arglist);
9800 /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error
9801 produced. False otherwise. This is done so that we don't output the error
9802 or warning twice or three times. */
9803 bool
9804 fold_builtin_next_arg (tree arglist)
9806 tree fntype = TREE_TYPE (current_function_decl);
9808 if (TYPE_ARG_TYPES (fntype) == 0
9809 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
9810 == void_type_node))
9812 error ("%<va_start%> used in function with fixed args");
9813 return true;
9815 else if (!arglist)
9817 /* Evidently an out of date version of <stdarg.h>; can't validate
9818 va_start's second argument, but can still work as intended. */
9819 warning (0, "%<__builtin_next_arg%> called without an argument");
9820 return true;
9822 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9823 when we checked the arguments and if needed issued a warning. */
9824 else if (!TREE_CHAIN (arglist)
9825 || !integer_zerop (TREE_VALUE (arglist))
9826 || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist)))
9827 || TREE_CHAIN (TREE_CHAIN (arglist)))
9829 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9830 tree arg = TREE_VALUE (arglist);
9832 if (TREE_CHAIN (arglist))
9834 error ("%<va_start%> used with too many arguments");
9835 return true;
9838 /* Strip off all nops for the sake of the comparison. This
9839 is not quite the same as STRIP_NOPS. It does more.
9840 We must also strip off INDIRECT_EXPR for C++ reference
9841 parameters. */
9842 while (TREE_CODE (arg) == NOP_EXPR
9843 || TREE_CODE (arg) == CONVERT_EXPR
9844 || TREE_CODE (arg) == NON_LVALUE_EXPR
9845 || TREE_CODE (arg) == INDIRECT_REF)
9846 arg = TREE_OPERAND (arg, 0);
9847 if (arg != last_parm)
9849 /* FIXME: Sometimes with the tree optimizers we can get the
9850 not the last argument even though the user used the last
9851 argument. We just warn and set the arg to be the last
9852 argument so that we will get wrong-code because of
9853 it. */
9854 warning (0, "second parameter of %<va_start%> not last named argument");
9856 /* We want to verify the second parameter just once before the tree
9857 optimizers are run and then avoid keeping it in the tree,
9858 as otherwise we could warn even for correct code like:
9859 void foo (int i, ...)
9860 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9861 TREE_VALUE (arglist) = integer_zero_node;
9862 TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node);
9864 return false;
9868 /* Simplify a call to the sprintf builtin.
9870 Return 0 if no simplification was possible, otherwise return the
9871 simplified form of the call as a tree. If IGNORED is true, it means that
9872 the caller does not use the returned value of the function. */
9874 static tree
9875 fold_builtin_sprintf (tree arglist, int ignored)
9877 tree call, retval, dest, fmt;
9878 const char *fmt_str = NULL;
9880 /* Verify the required arguments in the original call. We deal with two
9881 types of sprintf() calls: 'sprintf (str, fmt)' and
9882 'sprintf (dest, "%s", orig)'. */
9883 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
9884 && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
9885 VOID_TYPE))
9886 return NULL_TREE;
9888 /* Get the destination string and the format specifier. */
9889 dest = TREE_VALUE (arglist);
9890 fmt = TREE_VALUE (TREE_CHAIN (arglist));
9892 /* Check whether the format is a literal string constant. */
9893 fmt_str = c_getstr (fmt);
9894 if (fmt_str == NULL)
9895 return NULL_TREE;
9897 call = NULL_TREE;
9898 retval = NULL_TREE;
9900 if (!init_target_chars())
9901 return 0;
9903 /* If the format doesn't contain % args or %%, use strcpy. */
9904 if (strchr (fmt_str, target_percent) == NULL)
9906 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9908 if (!fn)
9909 return NULL_TREE;
9911 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
9912 'format' is known to contain no % formats. */
9913 arglist = build_tree_list (NULL_TREE, fmt);
9914 arglist = tree_cons (NULL_TREE, dest, arglist);
9915 call = build_function_call_expr (fn, arglist);
9916 if (!ignored)
9917 retval = build_int_cst (NULL_TREE, strlen (fmt_str));
9920 /* If the format is "%s", use strcpy if the result isn't used. */
9921 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
9923 tree fn, orig;
9924 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
9926 if (!fn)
9927 return NULL_TREE;
9929 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
9930 orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
9931 arglist = build_tree_list (NULL_TREE, orig);
9932 arglist = tree_cons (NULL_TREE, dest, arglist);
9933 if (!ignored)
9935 retval = c_strlen (orig, 1);
9936 if (!retval || TREE_CODE (retval) != INTEGER_CST)
9937 return NULL_TREE;
9939 call = build_function_call_expr (fn, arglist);
9942 if (call && retval)
9944 retval = fold_convert
9945 (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
9946 retval);
9947 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
9949 else
9950 return call;
9953 /* Expand a call to __builtin_object_size. */
9956 expand_builtin_object_size (tree exp)
9958 tree ost;
9959 int object_size_type;
9960 tree fndecl = get_callee_fndecl (exp);
9961 tree arglist = TREE_OPERAND (exp, 1);
9962 location_t locus = EXPR_LOCATION (exp);
9964 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9966 error ("%Hfirst argument of %D must be a pointer, second integer constant",
9967 &locus, fndecl);
9968 expand_builtin_trap ();
9969 return const0_rtx;
9972 ost = TREE_VALUE (TREE_CHAIN (arglist));
9973 STRIP_NOPS (ost);
9975 if (TREE_CODE (ost) != INTEGER_CST
9976 || tree_int_cst_sgn (ost) < 0
9977 || compare_tree_int (ost, 3) > 0)
9979 error ("%Hlast argument of %D is not integer constant between 0 and 3",
9980 &locus, fndecl);
9981 expand_builtin_trap ();
9982 return const0_rtx;
9985 object_size_type = tree_low_cst (ost, 0);
9987 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9990 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9991 FCODE is the BUILT_IN_* to use.
9992 Return 0 if we failed; the caller should emit a normal call,
9993 otherwise try to get the result in TARGET, if convenient (and in
9994 mode MODE if that's convenient). */
9996 static rtx
9997 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
9998 enum built_in_function fcode)
10000 tree arglist = TREE_OPERAND (exp, 1);
10001 tree dest, src, len, size;
10003 if (!validate_arglist (arglist,
10004 POINTER_TYPE,
10005 fcode == BUILT_IN_MEMSET_CHK
10006 ? INTEGER_TYPE : POINTER_TYPE,
10007 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10008 return 0;
10010 dest = TREE_VALUE (arglist);
10011 src = TREE_VALUE (TREE_CHAIN (arglist));
10012 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10013 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10015 if (! host_integerp (size, 1))
10016 return 0;
10018 if (host_integerp (len, 1) || integer_all_onesp (size))
10020 tree fn;
10022 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
10024 location_t locus = EXPR_LOCATION (exp);
10025 warning (0, "%Hcall to %D will always overflow destination buffer",
10026 &locus, get_callee_fndecl (exp));
10027 return 0;
10030 arglist = build_tree_list (NULL_TREE, len);
10031 arglist = tree_cons (NULL_TREE, src, arglist);
10032 arglist = tree_cons (NULL_TREE, dest, arglist);
10034 fn = NULL_TREE;
10035 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10036 mem{cpy,pcpy,move,set} is available. */
10037 switch (fcode)
10039 case BUILT_IN_MEMCPY_CHK:
10040 fn = built_in_decls[BUILT_IN_MEMCPY];
10041 break;
10042 case BUILT_IN_MEMPCPY_CHK:
10043 fn = built_in_decls[BUILT_IN_MEMPCPY];
10044 break;
10045 case BUILT_IN_MEMMOVE_CHK:
10046 fn = built_in_decls[BUILT_IN_MEMMOVE];
10047 break;
10048 case BUILT_IN_MEMSET_CHK:
10049 fn = built_in_decls[BUILT_IN_MEMSET];
10050 break;
10051 default:
10052 break;
10055 if (! fn)
10056 return 0;
10058 fn = build_function_call_expr (fn, arglist);
10059 if (TREE_CODE (fn) == CALL_EXPR)
10060 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10061 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10063 else if (fcode == BUILT_IN_MEMSET_CHK)
10064 return 0;
10065 else
10067 unsigned int dest_align
10068 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
10070 /* If DEST is not a pointer type, call the normal function. */
10071 if (dest_align == 0)
10072 return 0;
10074 /* If SRC and DEST are the same (and not volatile), do nothing. */
10075 if (operand_equal_p (src, dest, 0))
10077 tree expr;
10079 if (fcode != BUILT_IN_MEMPCPY_CHK)
10081 /* Evaluate and ignore LEN in case it has side-effects. */
10082 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10083 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10086 len = fold_convert (TREE_TYPE (dest), len);
10087 expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len);
10088 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10091 /* __memmove_chk special case. */
10092 if (fcode == BUILT_IN_MEMMOVE_CHK)
10094 unsigned int src_align
10095 = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
10097 if (src_align == 0)
10098 return 0;
10100 /* If src is categorized for a readonly section we can use
10101 normal __memcpy_chk. */
10102 if (readonly_data_expr (src))
10104 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10105 if (!fn)
10106 return 0;
10107 fn = build_function_call_expr (fn, arglist);
10108 if (TREE_CODE (fn) == CALL_EXPR)
10109 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10110 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10113 return 0;
10117 /* Emit warning if a buffer overflow is detected at compile time. */
10119 static void
10120 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10122 int arg_mask, is_strlen = 0;
10123 tree arglist = TREE_OPERAND (exp, 1), a;
10124 tree len, size;
10125 location_t locus;
10127 switch (fcode)
10129 case BUILT_IN_STRCPY_CHK:
10130 case BUILT_IN_STPCPY_CHK:
10131 /* For __strcat_chk the warning will be emitted only if overflowing
10132 by at least strlen (dest) + 1 bytes. */
10133 case BUILT_IN_STRCAT_CHK:
10134 arg_mask = 6;
10135 is_strlen = 1;
10136 break;
10137 case BUILT_IN_STRNCPY_CHK:
10138 arg_mask = 12;
10139 break;
10140 case BUILT_IN_SNPRINTF_CHK:
10141 case BUILT_IN_VSNPRINTF_CHK:
10142 arg_mask = 10;
10143 break;
10144 default:
10145 gcc_unreachable ();
10148 len = NULL_TREE;
10149 size = NULL_TREE;
10150 for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1)
10151 if (arg_mask & 1)
10153 if (len)
10154 size = a;
10155 else
10156 len = a;
10159 if (!len || !size)
10160 return;
10162 len = TREE_VALUE (len);
10163 size = TREE_VALUE (size);
10165 if (! host_integerp (size, 1) || integer_all_onesp (size))
10166 return;
10168 if (is_strlen)
10170 len = c_strlen (len, 1);
10171 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
10172 return;
10174 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
10175 return;
10177 locus = EXPR_LOCATION (exp);
10178 warning (0, "%Hcall to %D will always overflow destination buffer",
10179 &locus, get_callee_fndecl (exp));
10182 /* Emit warning if a buffer overflow is detected at compile time
10183 in __sprintf_chk/__vsprintf_chk calls. */
10185 static void
10186 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10188 tree arglist = TREE_OPERAND (exp, 1);
10189 tree dest, size, len, fmt, flag;
10190 const char *fmt_str;
10192 /* Verify the required arguments in the original call. */
10193 if (! arglist)
10194 return;
10195 dest = TREE_VALUE (arglist);
10196 arglist = TREE_CHAIN (arglist);
10197 if (! arglist)
10198 return;
10199 flag = TREE_VALUE (arglist);
10200 arglist = TREE_CHAIN (arglist);
10201 if (! arglist)
10202 return;
10203 size = TREE_VALUE (arglist);
10204 arglist = TREE_CHAIN (arglist);
10205 if (! arglist)
10206 return;
10207 fmt = TREE_VALUE (arglist);
10208 arglist = TREE_CHAIN (arglist);
10210 if (! host_integerp (size, 1) || integer_all_onesp (size))
10211 return;
10213 /* Check whether the format is a literal string constant. */
10214 fmt_str = c_getstr (fmt);
10215 if (fmt_str == NULL)
10216 return;
10218 if (!init_target_chars())
10219 return;
10221 /* If the format doesn't contain % args or %%, we know its size. */
10222 if (strchr (fmt_str, target_percent) == 0)
10223 len = build_int_cstu (size_type_node, strlen (fmt_str));
10224 /* If the format is "%s" and first ... argument is a string literal,
10225 we know it too. */
10226 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10228 tree arg;
10230 if (! arglist)
10231 return;
10232 arg = TREE_VALUE (arglist);
10233 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10234 return;
10236 len = c_strlen (arg, 1);
10237 if (!len || ! host_integerp (len, 1))
10238 return;
10240 else
10241 return;
10243 if (! tree_int_cst_lt (len, size))
10245 location_t locus = EXPR_LOCATION (exp);
10246 warning (0, "%Hcall to %D will always overflow destination buffer",
10247 &locus, get_callee_fndecl (exp));
10251 /* Fold a call to __builtin_object_size, if possible. */
10253 tree
10254 fold_builtin_object_size (tree arglist)
10256 tree ptr, ost, ret = 0;
10257 int object_size_type;
10259 if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10260 return 0;
10262 ptr = TREE_VALUE (arglist);
10263 ost = TREE_VALUE (TREE_CHAIN (arglist));
10264 STRIP_NOPS (ost);
10266 if (TREE_CODE (ost) != INTEGER_CST
10267 || tree_int_cst_sgn (ost) < 0
10268 || compare_tree_int (ost, 3) > 0)
10269 return 0;
10271 object_size_type = tree_low_cst (ost, 0);
10273 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10274 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10275 and (size_t) 0 for types 2 and 3. */
10276 if (TREE_SIDE_EFFECTS (ptr))
10277 return fold_convert (size_type_node,
10278 object_size_type < 2
10279 ? integer_minus_one_node : integer_zero_node);
10281 if (TREE_CODE (ptr) == ADDR_EXPR)
10282 ret = build_int_cstu (size_type_node,
10283 compute_builtin_object_size (ptr, object_size_type));
10285 else if (TREE_CODE (ptr) == SSA_NAME)
10287 unsigned HOST_WIDE_INT bytes;
10289 /* If object size is not known yet, delay folding until
10290 later. Maybe subsequent passes will help determining
10291 it. */
10292 bytes = compute_builtin_object_size (ptr, object_size_type);
10293 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2
10294 ? -1 : 0))
10295 ret = build_int_cstu (size_type_node, bytes);
10298 if (ret)
10300 ret = force_fit_type (ret, -1, false, false);
10301 if (TREE_CONSTANT_OVERFLOW (ret))
10302 ret = 0;
10305 return ret;
10308 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10309 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10310 code of the builtin. If MAXLEN is not NULL, it is maximum length
10311 passed as third argument. */
10313 tree
10314 fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10315 enum built_in_function fcode)
10317 tree dest, src, len, size, fn;
10319 if (!validate_arglist (arglist,
10320 POINTER_TYPE,
10321 fcode == BUILT_IN_MEMSET_CHK
10322 ? INTEGER_TYPE : POINTER_TYPE,
10323 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10324 return 0;
10326 dest = TREE_VALUE (arglist);
10327 /* Actually val for __memset_chk, but it doesn't matter. */
10328 src = TREE_VALUE (TREE_CHAIN (arglist));
10329 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10330 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10332 /* If SRC and DEST are the same (and not volatile), return DEST
10333 (resp. DEST+LEN for __mempcpy_chk). */
10334 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
10336 if (fcode != BUILT_IN_MEMPCPY_CHK)
10337 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10338 else
10340 tree temp = fold_convert (TREE_TYPE (dest), len);
10341 temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp);
10342 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp);
10346 if (! host_integerp (size, 1))
10347 return 0;
10349 if (! integer_all_onesp (size))
10351 if (! host_integerp (len, 1))
10353 /* If LEN is not constant, try MAXLEN too.
10354 For MAXLEN only allow optimizing into non-_ocs function
10355 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10356 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10358 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
10360 /* (void) __mempcpy_chk () can be optimized into
10361 (void) __memcpy_chk (). */
10362 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10363 if (!fn)
10364 return 0;
10366 return build_function_call_expr (fn, arglist);
10368 return 0;
10371 else
10372 maxlen = len;
10374 if (tree_int_cst_lt (size, maxlen))
10375 return 0;
10378 arglist = build_tree_list (NULL_TREE, len);
10379 arglist = tree_cons (NULL_TREE, src, arglist);
10380 arglist = tree_cons (NULL_TREE, dest, arglist);
10382 fn = NULL_TREE;
10383 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10384 mem{cpy,pcpy,move,set} is available. */
10385 switch (fcode)
10387 case BUILT_IN_MEMCPY_CHK:
10388 fn = built_in_decls[BUILT_IN_MEMCPY];
10389 break;
10390 case BUILT_IN_MEMPCPY_CHK:
10391 fn = built_in_decls[BUILT_IN_MEMPCPY];
10392 break;
10393 case BUILT_IN_MEMMOVE_CHK:
10394 fn = built_in_decls[BUILT_IN_MEMMOVE];
10395 break;
10396 case BUILT_IN_MEMSET_CHK:
10397 fn = built_in_decls[BUILT_IN_MEMSET];
10398 break;
10399 default:
10400 break;
10403 if (!fn)
10404 return 0;
10406 return build_function_call_expr (fn, arglist);
10409 /* Fold a call to the __st[rp]cpy_chk builtin.
10410 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
10411 code of the builtin. If MAXLEN is not NULL, it is maximum length of
10412 strings passed as second argument. */
10414 tree
10415 fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore,
10416 enum built_in_function fcode)
10418 tree dest, src, size, len, fn;
10420 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10421 VOID_TYPE))
10422 return 0;
10424 dest = TREE_VALUE (arglist);
10425 src = TREE_VALUE (TREE_CHAIN (arglist));
10426 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10428 /* If SRC and DEST are the same (and not volatile), return DEST. */
10429 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
10430 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest);
10432 if (! host_integerp (size, 1))
10433 return 0;
10435 if (! integer_all_onesp (size))
10437 len = c_strlen (src, 1);
10438 if (! len || ! host_integerp (len, 1))
10440 /* If LEN is not constant, try MAXLEN too.
10441 For MAXLEN only allow optimizing into non-_ocs function
10442 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10443 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10445 if (fcode == BUILT_IN_STPCPY_CHK)
10447 if (! ignore)
10448 return 0;
10450 /* If return value of __stpcpy_chk is ignored,
10451 optimize into __strcpy_chk. */
10452 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
10453 if (!fn)
10454 return 0;
10456 return build_function_call_expr (fn, arglist);
10459 if (! len || TREE_SIDE_EFFECTS (len))
10460 return 0;
10462 /* If c_strlen returned something, but not a constant,
10463 transform __strcpy_chk into __memcpy_chk. */
10464 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
10465 if (!fn)
10466 return 0;
10468 len = size_binop (PLUS_EXPR, len, ssize_int (1));
10469 arglist = build_tree_list (NULL_TREE, size);
10470 arglist = tree_cons (NULL_TREE, len, arglist);
10471 arglist = tree_cons (NULL_TREE, src, arglist);
10472 arglist = tree_cons (NULL_TREE, dest, arglist);
10473 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)),
10474 build_function_call_expr (fn, arglist));
10477 else
10478 maxlen = len;
10480 if (! tree_int_cst_lt (maxlen, size))
10481 return 0;
10484 arglist = build_tree_list (NULL_TREE, src);
10485 arglist = tree_cons (NULL_TREE, dest, arglist);
10487 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
10488 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
10489 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
10490 if (!fn)
10491 return 0;
10493 return build_function_call_expr (fn, arglist);
10496 /* Fold a call to the __strncpy_chk builtin.
10497 If MAXLEN is not NULL, it is maximum length passed as third argument. */
10499 tree
10500 fold_builtin_strncpy_chk (tree arglist, tree maxlen)
10502 tree dest, src, size, len, fn;
10504 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10505 INTEGER_TYPE, VOID_TYPE))
10506 return 0;
10508 dest = TREE_VALUE (arglist);
10509 src = TREE_VALUE (TREE_CHAIN (arglist));
10510 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10511 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10513 if (! host_integerp (size, 1))
10514 return 0;
10516 if (! integer_all_onesp (size))
10518 if (! host_integerp (len, 1))
10520 /* If LEN is not constant, try MAXLEN too.
10521 For MAXLEN only allow optimizing into non-_ocs function
10522 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10523 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10524 return 0;
10526 else
10527 maxlen = len;
10529 if (tree_int_cst_lt (size, maxlen))
10530 return 0;
10533 arglist = build_tree_list (NULL_TREE, len);
10534 arglist = tree_cons (NULL_TREE, src, arglist);
10535 arglist = tree_cons (NULL_TREE, dest, arglist);
10537 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
10538 fn = built_in_decls[BUILT_IN_STRNCPY];
10539 if (!fn)
10540 return 0;
10542 return build_function_call_expr (fn, arglist);
10545 /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */
10547 static tree
10548 fold_builtin_strcat_chk (tree fndecl, tree arglist)
10550 tree dest, src, size, fn;
10551 const char *p;
10553 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10554 VOID_TYPE))
10555 return 0;
10557 dest = TREE_VALUE (arglist);
10558 src = TREE_VALUE (TREE_CHAIN (arglist));
10559 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10561 p = c_getstr (src);
10562 /* If the SRC parameter is "", return DEST. */
10563 if (p && *p == '\0')
10564 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10566 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
10567 return 0;
10569 arglist = build_tree_list (NULL_TREE, src);
10570 arglist = tree_cons (NULL_TREE, dest, arglist);
10572 /* If __builtin_strcat_chk is used, assume strcat is available. */
10573 fn = built_in_decls[BUILT_IN_STRCAT];
10574 if (!fn)
10575 return 0;
10577 return build_function_call_expr (fn, arglist);
10580 /* Fold a call to the __strncat_chk builtin EXP. */
10582 static tree
10583 fold_builtin_strncat_chk (tree fndecl, tree arglist)
10585 tree dest, src, size, len, fn;
10586 const char *p;
10588 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE,
10589 INTEGER_TYPE, VOID_TYPE))
10590 return 0;
10592 dest = TREE_VALUE (arglist);
10593 src = TREE_VALUE (TREE_CHAIN (arglist));
10594 len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
10595 size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
10597 p = c_getstr (src);
10598 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
10599 if (p && *p == '\0')
10600 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
10601 else if (integer_zerop (len))
10602 return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
10604 if (! host_integerp (size, 1))
10605 return 0;
10607 if (! integer_all_onesp (size))
10609 tree src_len = c_strlen (src, 1);
10610 if (src_len
10611 && host_integerp (src_len, 1)
10612 && host_integerp (len, 1)
10613 && ! tree_int_cst_lt (len, src_len))
10615 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
10616 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
10617 if (!fn)
10618 return 0;
10620 arglist = build_tree_list (NULL_TREE, size);
10621 arglist = tree_cons (NULL_TREE, src, arglist);
10622 arglist = tree_cons (NULL_TREE, dest, arglist);
10623 return build_function_call_expr (fn, arglist);
10625 return 0;
10628 arglist = build_tree_list (NULL_TREE, len);
10629 arglist = tree_cons (NULL_TREE, src, arglist);
10630 arglist = tree_cons (NULL_TREE, dest, arglist);
10632 /* If __builtin_strncat_chk is used, assume strncat is available. */
10633 fn = built_in_decls[BUILT_IN_STRNCAT];
10634 if (!fn)
10635 return 0;
10637 return build_function_call_expr (fn, arglist);
10640 /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if
10641 a normal call should be emitted rather than expanding the function
10642 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
10644 static tree
10645 fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode)
10647 tree dest, size, len, fn, fmt, flag;
10648 const char *fmt_str;
10650 /* Verify the required arguments in the original call. */
10651 if (! arglist)
10652 return 0;
10653 dest = TREE_VALUE (arglist);
10654 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10655 return 0;
10656 arglist = TREE_CHAIN (arglist);
10657 if (! arglist)
10658 return 0;
10659 flag = TREE_VALUE (arglist);
10660 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE)
10661 return 0;
10662 arglist = TREE_CHAIN (arglist);
10663 if (! arglist)
10664 return 0;
10665 size = TREE_VALUE (arglist);
10666 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10667 return 0;
10668 arglist = TREE_CHAIN (arglist);
10669 if (! arglist)
10670 return 0;
10671 fmt = TREE_VALUE (arglist);
10672 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10673 return 0;
10674 arglist = TREE_CHAIN (arglist);
10676 if (! host_integerp (size, 1))
10677 return 0;
10679 len = NULL_TREE;
10681 if (!init_target_chars())
10682 return 0;
10684 /* Check whether the format is a literal string constant. */
10685 fmt_str = c_getstr (fmt);
10686 if (fmt_str != NULL)
10688 /* If the format doesn't contain % args or %%, we know the size. */
10689 if (strchr (fmt_str, target_percent) == 0)
10691 if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE)
10692 len = build_int_cstu (size_type_node, strlen (fmt_str));
10694 /* If the format is "%s" and first ... argument is a string literal,
10695 we know the size too. */
10696 else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0)
10698 tree arg;
10700 if (arglist && !TREE_CHAIN (arglist))
10702 arg = TREE_VALUE (arglist);
10703 if (POINTER_TYPE_P (TREE_TYPE (arg)))
10705 len = c_strlen (arg, 1);
10706 if (! len || ! host_integerp (len, 1))
10707 len = NULL_TREE;
10713 if (! integer_all_onesp (size))
10715 if (! len || ! tree_int_cst_lt (len, size))
10716 return 0;
10719 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
10720 or if format doesn't contain % chars or is "%s". */
10721 if (! integer_zerop (flag))
10723 if (fmt_str == NULL)
10724 return 0;
10725 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10726 return 0;
10729 arglist = tree_cons (NULL_TREE, fmt, arglist);
10730 arglist = tree_cons (NULL_TREE, dest, arglist);
10732 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
10733 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
10734 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
10735 if (!fn)
10736 return 0;
10738 return build_function_call_expr (fn, arglist);
10741 /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if
10742 a normal call should be emitted rather than expanding the function
10743 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
10744 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
10745 passed as second argument. */
10747 tree
10748 fold_builtin_snprintf_chk (tree arglist, tree maxlen,
10749 enum built_in_function fcode)
10751 tree dest, size, len, fn, fmt, flag;
10752 const char *fmt_str;
10754 /* Verify the required arguments in the original call. */
10755 if (! arglist)
10756 return 0;
10757 dest = TREE_VALUE (arglist);
10758 if (! POINTER_TYPE_P (TREE_TYPE (dest)))
10759 return 0;
10760 arglist = TREE_CHAIN (arglist);
10761 if (! arglist)
10762 return 0;
10763 len = TREE_VALUE (arglist);
10764 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10765 return 0;
10766 arglist = TREE_CHAIN (arglist);
10767 if (! arglist)
10768 return 0;
10769 flag = TREE_VALUE (arglist);
10770 if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE)
10771 return 0;
10772 arglist = TREE_CHAIN (arglist);
10773 if (! arglist)
10774 return 0;
10775 size = TREE_VALUE (arglist);
10776 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE)
10777 return 0;
10778 arglist = TREE_CHAIN (arglist);
10779 if (! arglist)
10780 return 0;
10781 fmt = TREE_VALUE (arglist);
10782 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10783 return 0;
10784 arglist = TREE_CHAIN (arglist);
10786 if (! host_integerp (size, 1))
10787 return 0;
10789 if (! integer_all_onesp (size))
10791 if (! host_integerp (len, 1))
10793 /* If LEN is not constant, try MAXLEN too.
10794 For MAXLEN only allow optimizing into non-_ocs function
10795 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
10796 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
10797 return 0;
10799 else
10800 maxlen = len;
10802 if (tree_int_cst_lt (size, maxlen))
10803 return 0;
10806 if (!init_target_chars())
10807 return 0;
10809 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
10810 or if format doesn't contain % chars or is "%s". */
10811 if (! integer_zerop (flag))
10813 fmt_str = c_getstr (fmt);
10814 if (fmt_str == NULL)
10815 return 0;
10816 if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s))
10817 return 0;
10820 arglist = tree_cons (NULL_TREE, fmt, arglist);
10821 arglist = tree_cons (NULL_TREE, len, arglist);
10822 arglist = tree_cons (NULL_TREE, dest, arglist);
10824 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
10825 available. */
10826 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
10827 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
10828 if (!fn)
10829 return 0;
10831 return build_function_call_expr (fn, arglist);
10834 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
10836 Return 0 if no simplification was possible, otherwise return the
10837 simplified form of the call as a tree. FCODE is the BUILT_IN_*
10838 code of the function to be simplified. */
10840 static tree
10841 fold_builtin_printf (tree fndecl, tree arglist, bool ignore,
10842 enum built_in_function fcode)
10844 tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call;
10845 const char *fmt_str = NULL;
10847 /* If the return value is used, don't do the transformation. */
10848 if (! ignore)
10849 return 0;
10851 /* Verify the required arguments in the original call. */
10852 if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK)
10854 tree flag;
10856 if (! arglist)
10857 return 0;
10858 flag = TREE_VALUE (arglist);
10859 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
10860 || TREE_SIDE_EFFECTS (flag))
10861 return 0;
10862 arglist = TREE_CHAIN (arglist);
10865 if (! arglist)
10866 return 0;
10867 fmt = TREE_VALUE (arglist);
10868 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
10869 return 0;
10870 arglist = TREE_CHAIN (arglist);
10872 /* Check whether the format is a literal string constant. */
10873 fmt_str = c_getstr (fmt);
10874 if (fmt_str == NULL)
10875 return NULL_TREE;
10877 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
10879 /* If we're using an unlocked function, assume the other
10880 unlocked functions exist explicitly. */
10881 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
10882 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
10884 else
10886 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
10887 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
10890 if (!init_target_chars())
10891 return 0;
10893 if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL)
10895 const char *str;
10897 if (strcmp (fmt_str, target_percent_s) == 0)
10899 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
10900 return 0;
10902 if (! arglist
10903 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
10904 || TREE_CHAIN (arglist))
10905 return 0;
10907 str = c_getstr (TREE_VALUE (arglist));
10908 if (str == NULL)
10909 return 0;
10911 else
10913 /* The format specifier doesn't contain any '%' characters. */
10914 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
10915 && arglist)
10916 return 0;
10917 str = fmt_str;
10920 /* If the string was "", printf does nothing. */
10921 if (str[0] == '\0')
10922 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
10924 /* If the string has length of 1, call putchar. */
10925 if (str[1] == '\0')
10927 /* Given printf("c"), (where c is any one character,)
10928 convert "c"[0] to an int and pass that to the replacement
10929 function. */
10930 arg = build_int_cst (NULL_TREE, str[0]);
10931 arglist = build_tree_list (NULL_TREE, arg);
10932 fn = fn_putchar;
10934 else
10936 /* If the string was "string\n", call puts("string"). */
10937 size_t len = strlen (str);
10938 if ((unsigned char)str[len - 1] == target_newline)
10940 /* Create a NUL-terminated string that's one char shorter
10941 than the original, stripping off the trailing '\n'. */
10942 char *newstr = alloca (len);
10943 memcpy (newstr, str, len - 1);
10944 newstr[len - 1] = 0;
10946 arg = build_string_literal (len, newstr);
10947 arglist = build_tree_list (NULL_TREE, arg);
10948 fn = fn_puts;
10950 else
10951 /* We'd like to arrange to call fputs(string,stdout) here,
10952 but we need stdout and don't have a way to get it yet. */
10953 return 0;
10957 /* The other optimizations can be done only on the non-va_list variants. */
10958 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
10959 return 0;
10961 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
10962 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
10964 if (! arglist
10965 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
10966 || TREE_CHAIN (arglist))
10967 return 0;
10968 fn = fn_puts;
10971 /* If the format specifier was "%c", call __builtin_putchar(arg). */
10972 else if (strcmp (fmt_str, target_percent_c) == 0)
10974 if (! arglist
10975 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
10976 || TREE_CHAIN (arglist))
10977 return 0;
10978 fn = fn_putchar;
10981 if (!fn)
10982 return 0;
10984 call = build_function_call_expr (fn, arglist);
10985 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
10988 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
10990 Return 0 if no simplification was possible, otherwise return the
10991 simplified form of the call as a tree. FCODE is the BUILT_IN_*
10992 code of the function to be simplified. */
10994 static tree
10995 fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore,
10996 enum built_in_function fcode)
10998 tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call;
10999 const char *fmt_str = NULL;
11001 /* If the return value is used, don't do the transformation. */
11002 if (! ignore)
11003 return 0;
11005 /* Verify the required arguments in the original call. */
11006 if (! arglist)
11007 return 0;
11008 fp = TREE_VALUE (arglist);
11009 if (! POINTER_TYPE_P (TREE_TYPE (fp)))
11010 return 0;
11011 arglist = TREE_CHAIN (arglist);
11013 if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK)
11015 tree flag;
11017 if (! arglist)
11018 return 0;
11019 flag = TREE_VALUE (arglist);
11020 if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE
11021 || TREE_SIDE_EFFECTS (flag))
11022 return 0;
11023 arglist = TREE_CHAIN (arglist);
11026 if (! arglist)
11027 return 0;
11028 fmt = TREE_VALUE (arglist);
11029 if (! POINTER_TYPE_P (TREE_TYPE (fmt)))
11030 return 0;
11031 arglist = TREE_CHAIN (arglist);
11033 /* Check whether the format is a literal string constant. */
11034 fmt_str = c_getstr (fmt);
11035 if (fmt_str == NULL)
11036 return NULL_TREE;
11038 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11040 /* If we're using an unlocked function, assume the other
11041 unlocked functions exist explicitly. */
11042 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
11043 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
11045 else
11047 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
11048 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
11051 if (!init_target_chars())
11052 return 0;
11054 /* If the format doesn't contain % args or %%, use strcpy. */
11055 if (strchr (fmt_str, target_percent) == NULL)
11057 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11058 && arglist)
11059 return 0;
11061 /* If the format specifier was "", fprintf does nothing. */
11062 if (fmt_str[0] == '\0')
11064 /* If FP has side-effects, just wait until gimplification is
11065 done. */
11066 if (TREE_SIDE_EFFECTS (fp))
11067 return 0;
11069 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11072 /* When "string" doesn't contain %, replace all cases of
11073 fprintf (fp, string) with fputs (string, fp). The fputs
11074 builtin will take care of special cases like length == 1. */
11075 arglist = build_tree_list (NULL_TREE, fp);
11076 arglist = tree_cons (NULL_TREE, fmt, arglist);
11077 fn = fn_fputs;
11080 /* The other optimizations can be done only on the non-va_list variants. */
11081 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11082 return 0;
11084 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11085 else if (strcmp (fmt_str, target_percent_s) == 0)
11087 if (! arglist
11088 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
11089 || TREE_CHAIN (arglist))
11090 return 0;
11091 arg = TREE_VALUE (arglist);
11092 arglist = build_tree_list (NULL_TREE, fp);
11093 arglist = tree_cons (NULL_TREE, arg, arglist);
11094 fn = fn_fputs;
11097 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11098 else if (strcmp (fmt_str, target_percent_c) == 0)
11100 if (! arglist
11101 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE
11102 || TREE_CHAIN (arglist))
11103 return 0;
11104 arg = TREE_VALUE (arglist);
11105 arglist = build_tree_list (NULL_TREE, fp);
11106 arglist = tree_cons (NULL_TREE, arg, arglist);
11107 fn = fn_fputc;
11110 if (!fn)
11111 return 0;
11113 call = build_function_call_expr (fn, arglist);
11114 return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call);
11117 /* Initialize format string characters in the target charset. */
11119 static bool
11120 init_target_chars (void)
11122 static bool init;
11123 if (!init)
11125 target_newline = lang_hooks.to_target_charset ('\n');
11126 target_percent = lang_hooks.to_target_charset ('%');
11127 target_c = lang_hooks.to_target_charset ('c');
11128 target_s = lang_hooks.to_target_charset ('s');
11129 if (target_newline == 0 || target_percent == 0 || target_c == 0
11130 || target_s == 0)
11131 return false;
11133 target_percent_c[0] = target_percent;
11134 target_percent_c[1] = target_c;
11135 target_percent_c[2] = '\0';
11137 target_percent_s[0] = target_percent;
11138 target_percent_s[1] = target_s;
11139 target_percent_s[2] = '\0';
11141 target_percent_s_newline[0] = target_percent;
11142 target_percent_s_newline[1] = target_s;
11143 target_percent_s_newline[2] = target_newline;
11144 target_percent_s_newline[3] = '\0';
11146 init = true;
11148 return true;