Merge trunk version 193672 into gupc branch.
[official-gcc.git] / gcc / builtins.c
blobb0cab51556ef9436f755874a0852194a5e77d58f
1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "realmpfr.h"
30 #include "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 "predict.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
52 #include "builtins.h"
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
57 #endif
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
60 struct target_builtins default_target_builtins;
61 #if SWITCHABLE_TARGET
62 struct target_builtins *this_target_builtins = &default_target_builtins;
63 #endif
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
72 #include "builtins.def"
74 #undef DEF_BUILTIN
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 builtin_info_type builtin_info;
80 static const char *c_getstr (tree);
81 static rtx c_readstr (const char *, enum machine_mode);
82 static int target_char_cast (tree, char *);
83 static rtx get_memory_rtx (tree, tree);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector (int, rtx);
88 #endif
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_next_arg (void);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx);
116 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
118 enum machine_mode, int);
119 static rtx expand_builtin_strcpy (tree, rtx);
120 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
121 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, bool);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static tree stabilize_va_list_loc (location_t, tree, int);
132 static rtx expand_builtin_expect (tree, rtx);
133 static tree fold_builtin_constant_p (tree);
134 static tree fold_builtin_expect (location_t, tree, tree);
135 static tree fold_builtin_classify_type (tree);
136 static tree fold_builtin_strlen (location_t, tree, tree);
137 static tree fold_builtin_inf (location_t, tree, int);
138 static tree fold_builtin_nan (tree, tree, int);
139 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
140 static bool validate_arg (const_tree, enum tree_code code);
141 static bool integer_valued_real_p (tree);
142 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
143 static bool readonly_data_expr (tree);
144 static rtx expand_builtin_fabs (tree, rtx, rtx);
145 static rtx expand_builtin_signbit (tree, rtx);
146 static tree fold_builtin_sqrt (location_t, tree, tree);
147 static tree fold_builtin_cbrt (location_t, tree, tree);
148 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_cos (location_t, tree, tree, tree);
151 static tree fold_builtin_cosh (location_t, tree, tree, tree);
152 static tree fold_builtin_tan (tree, tree);
153 static tree fold_builtin_trunc (location_t, tree, tree);
154 static tree fold_builtin_floor (location_t, tree, tree);
155 static tree fold_builtin_ceil (location_t, tree, tree);
156 static tree fold_builtin_round (location_t, tree, tree);
157 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
158 static tree fold_builtin_bitop (tree, tree);
159 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160 static tree fold_builtin_strchr (location_t, tree, tree, tree);
161 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163 static tree fold_builtin_strcmp (location_t, tree, tree);
164 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165 static tree fold_builtin_signbit (location_t, tree, tree);
166 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173 enum tree_code);
174 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175 static tree fold_builtin_0 (location_t, tree, bool);
176 static tree fold_builtin_1 (location_t, tree, tree, bool);
177 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180 static tree fold_builtin_varargs (location_t, tree, tree, bool);
182 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183 static tree fold_builtin_strstr (location_t, tree, tree, tree);
184 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185 static tree fold_builtin_strcat (location_t, tree, tree);
186 static tree fold_builtin_strncat (location_t, tree, tree, tree);
187 static tree fold_builtin_strspn (location_t, tree, tree);
188 static tree fold_builtin_strcspn (location_t, tree, tree);
189 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
190 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
192 static rtx expand_builtin_object_size (tree);
193 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194 enum built_in_function);
195 static void maybe_emit_chk_warning (tree, enum built_in_function);
196 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_free_warning (tree);
198 static tree fold_builtin_object_size (tree, tree);
199 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
204 enum built_in_function);
205 static bool init_target_chars (void);
207 static unsigned HOST_WIDE_INT target_newline;
208 static unsigned HOST_WIDE_INT target_percent;
209 static unsigned HOST_WIDE_INT target_c;
210 static unsigned HOST_WIDE_INT target_s;
211 static char target_percent_c[3];
212 static char target_percent_s[3];
213 static char target_percent_s_newline[4];
214 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
216 static tree do_mpfr_arg2 (tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_arg3 (tree, tree, tree, tree,
219 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
220 static tree do_mpfr_sincos (tree, tree, tree);
221 static tree do_mpfr_bessel_n (tree, tree, tree,
222 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_remquo (tree, tree, tree);
225 static tree do_mpfr_lgamma_r (tree, tree, tree);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
230 static bool
231 is_builtin_name (const char *name)
233 if (strncmp (name, "__builtin_", 10) == 0)
234 return true;
235 if (strncmp (name, "__sync_", 7) == 0)
236 return true;
237 if (strncmp (name, "__atomic_", 9) == 0)
238 return true;
239 return false;
243 /* Return true if DECL is a function symbol representing a built-in. */
245 bool
246 is_builtin_fn (tree decl)
248 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
252 /* Return true if NODE should be considered for inline expansion regardless
253 of the optimization level. This means whenever a function is invoked with
254 its "internal" name, which normally contains the prefix "__builtin". */
256 static bool
257 called_as_built_in (tree node)
259 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260 we want the name used to call the function, not the name it
261 will have. */
262 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
263 return is_builtin_name (name);
266 /* Compute values M and N such that M divides (address of EXP - N) and such
267 that N < M. If these numbers can be determined, store M in alignp and N in
268 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
269 *alignp and any bit-offset to *bitposp.
271 Note that the address (and thus the alignment) computed here is based
272 on the address to which a symbol resolves, whereas DECL_ALIGN is based
273 on the address at which an object is actually located. These two
274 addresses are not always the same. For example, on ARM targets,
275 the address &foo of a Thumb function foo() has the lowest bit set,
276 whereas foo() itself starts on an even address.
278 If ADDR_P is true we are taking the address of the memory reference EXP
279 and thus cannot rely on the access taking place. */
281 static bool
282 get_object_alignment_2 (tree exp, unsigned int *alignp,
283 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
285 HOST_WIDE_INT bitsize, bitpos;
286 tree offset;
287 enum machine_mode mode;
288 int unsignedp, volatilep;
289 unsigned int inner, align = BITS_PER_UNIT;
290 bool known_alignment = false;
292 /* Get the innermost object and the constant (bitpos) and possibly
293 variable (offset) offset of the access. */
294 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
295 &mode, &unsignedp, &volatilep, true);
297 /* Extract alignment information from the innermost object and
298 possibly adjust bitpos and offset. */
299 if (TREE_CODE (exp) == FUNCTION_DECL)
301 /* Function addresses can encode extra information besides their
302 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
303 allows the low bit to be used as a virtual bit, we know
304 that the address itself must be at least 2-byte aligned. */
305 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
306 align = 2 * BITS_PER_UNIT;
308 else if (TREE_CODE (exp) == LABEL_DECL)
310 else if (TREE_CODE (exp) == CONST_DECL)
312 /* The alignment of a CONST_DECL is determined by its initializer. */
313 exp = DECL_INITIAL (exp);
314 align = TYPE_ALIGN (TREE_TYPE (exp));
315 #ifdef CONSTANT_ALIGNMENT
316 if (CONSTANT_CLASS_P (exp))
317 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
318 #endif
319 known_alignment = true;
321 else if (DECL_P (exp))
323 align = DECL_ALIGN (exp);
324 known_alignment = true;
326 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
328 align = TYPE_ALIGN (TREE_TYPE (exp));
330 else if (TREE_CODE (exp) == INDIRECT_REF
331 || TREE_CODE (exp) == MEM_REF
332 || TREE_CODE (exp) == TARGET_MEM_REF)
334 tree addr = TREE_OPERAND (exp, 0);
335 unsigned ptr_align;
336 unsigned HOST_WIDE_INT ptr_bitpos;
338 if (TREE_CODE (addr) == BIT_AND_EXPR
339 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
341 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
342 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
343 align *= BITS_PER_UNIT;
344 addr = TREE_OPERAND (addr, 0);
347 known_alignment
348 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
349 align = MAX (ptr_align, align);
351 /* The alignment of the pointer operand in a TARGET_MEM_REF
352 has to take the variable offset parts into account. */
353 if (TREE_CODE (exp) == TARGET_MEM_REF)
355 if (TMR_INDEX (exp))
357 unsigned HOST_WIDE_INT step = 1;
358 if (TMR_STEP (exp))
359 step = TREE_INT_CST_LOW (TMR_STEP (exp));
360 align = MIN (align, (step & -step) * BITS_PER_UNIT);
362 if (TMR_INDEX2 (exp))
363 align = BITS_PER_UNIT;
364 known_alignment = false;
367 /* When EXP is an actual memory reference then we can use
368 TYPE_ALIGN of a pointer indirection to derive alignment.
369 Do so only if get_pointer_alignment_1 did not reveal absolute
370 alignment knowledge and if using that alignment would
371 improve the situation. */
372 if (!addr_p && !known_alignment
373 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
374 align = TYPE_ALIGN (TREE_TYPE (exp));
375 else
377 /* Else adjust bitpos accordingly. */
378 bitpos += ptr_bitpos;
379 if (TREE_CODE (exp) == MEM_REF
380 || TREE_CODE (exp) == TARGET_MEM_REF)
381 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
384 else if (TREE_CODE (exp) == STRING_CST)
386 /* STRING_CST are the only constant objects we allow to be not
387 wrapped inside a CONST_DECL. */
388 align = TYPE_ALIGN (TREE_TYPE (exp));
389 #ifdef CONSTANT_ALIGNMENT
390 if (CONSTANT_CLASS_P (exp))
391 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
392 #endif
393 known_alignment = true;
396 /* If there is a non-constant offset part extract the maximum
397 alignment that can prevail. */
398 inner = ~0U;
399 while (offset)
401 tree next_offset;
403 if (TREE_CODE (offset) == PLUS_EXPR)
405 next_offset = TREE_OPERAND (offset, 0);
406 offset = TREE_OPERAND (offset, 1);
408 else
409 next_offset = NULL;
410 if (host_integerp (offset, 1))
412 /* Any overflow in calculating offset_bits won't change
413 the alignment. */
414 unsigned offset_bits
415 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
417 if (offset_bits)
418 inner = MIN (inner, (offset_bits & -offset_bits));
420 else if (TREE_CODE (offset) == MULT_EXPR
421 && host_integerp (TREE_OPERAND (offset, 1), 1))
423 /* Any overflow in calculating offset_factor won't change
424 the alignment. */
425 unsigned offset_factor
426 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
427 * BITS_PER_UNIT);
429 if (offset_factor)
430 inner = MIN (inner, (offset_factor & -offset_factor));
432 else
434 inner = MIN (inner, BITS_PER_UNIT);
435 break;
437 offset = next_offset;
439 /* Alignment is innermost object alignment adjusted by the constant
440 and non-constant offset parts. */
441 align = MIN (align, inner);
443 *alignp = align;
444 *bitposp = bitpos & (*alignp - 1);
445 return known_alignment;
448 /* For a memory reference expression EXP compute values M and N such that M
449 divides (&EXP - N) and such that N < M. If these numbers can be determined,
450 store M in alignp and N in *BITPOSP and return true. Otherwise return false
451 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
453 bool
454 get_object_alignment_1 (tree exp, unsigned int *alignp,
455 unsigned HOST_WIDE_INT *bitposp)
457 return get_object_alignment_2 (exp, alignp, bitposp, false);
460 /* Return the alignment in bits of EXP, an object. */
462 unsigned int
463 get_object_alignment (tree exp)
465 unsigned HOST_WIDE_INT bitpos = 0;
466 unsigned int align;
468 get_object_alignment_1 (exp, &align, &bitpos);
470 /* align and bitpos now specify known low bits of the pointer.
471 ptr & (align - 1) == bitpos. */
473 if (bitpos != 0)
474 align = (bitpos & -bitpos);
475 return align;
478 /* For a pointer valued expression EXP compute values M and N such that M
479 divides (EXP - N) and such that N < M. If these numbers can be determined,
480 store M in alignp and N in *BITPOSP and return true. Return false if
481 the results are just a conservative approximation.
483 If EXP is not a pointer, false is returned too. */
485 bool
486 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
487 unsigned HOST_WIDE_INT *bitposp)
489 STRIP_NOPS (exp);
491 if (TREE_CODE (exp) == ADDR_EXPR)
492 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
493 alignp, bitposp, true);
494 else if (TREE_CODE (exp) == SSA_NAME
495 && POINTER_TYPE_P (TREE_TYPE (exp)))
497 unsigned int ptr_align, ptr_misalign;
498 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
500 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
502 *bitposp = ptr_misalign * BITS_PER_UNIT;
503 *alignp = ptr_align * BITS_PER_UNIT;
504 /* We cannot really tell whether this result is an approximation. */
505 return true;
507 else
509 *bitposp = 0;
510 *alignp = BITS_PER_UNIT;
511 return false;
514 else if (TREE_CODE (exp) == INTEGER_CST)
516 *alignp = BIGGEST_ALIGNMENT;
517 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
518 & (BIGGEST_ALIGNMENT - 1));
519 return true;
522 *bitposp = 0;
523 *alignp = BITS_PER_UNIT;
524 return false;
527 /* Return the alignment in bits of EXP, a pointer valued expression.
528 The alignment returned is, by default, the alignment of the thing that
529 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
531 Otherwise, look at the expression to see if we can do better, i.e., if the
532 expression is actually pointing at an object whose alignment is tighter. */
534 unsigned int
535 get_pointer_alignment (tree exp)
537 unsigned HOST_WIDE_INT bitpos = 0;
538 unsigned int align;
540 get_pointer_alignment_1 (exp, &align, &bitpos);
542 /* align and bitpos now specify known low bits of the pointer.
543 ptr & (align - 1) == bitpos. */
545 if (bitpos != 0)
546 align = (bitpos & -bitpos);
548 return align;
551 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
552 way, because it could contain a zero byte in the middle.
553 TREE_STRING_LENGTH is the size of the character array, not the string.
555 ONLY_VALUE should be nonzero if the result is not going to be emitted
556 into the instruction stream and zero if it is going to be expanded.
557 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
558 is returned, otherwise NULL, since
559 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
560 evaluate the side-effects.
562 The value returned is of type `ssizetype'.
564 Unfortunately, string_constant can't access the values of const char
565 arrays with initializers, so neither can we do so here. */
567 tree
568 c_strlen (tree src, int only_value)
570 tree offset_node;
571 HOST_WIDE_INT offset;
572 int max;
573 const char *ptr;
574 location_t loc;
576 STRIP_NOPS (src);
577 if (TREE_CODE (src) == COND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
580 tree len1, len2;
582 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
583 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
584 if (tree_int_cst_equal (len1, len2))
585 return len1;
588 if (TREE_CODE (src) == COMPOUND_EXPR
589 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
590 return c_strlen (TREE_OPERAND (src, 1), only_value);
592 loc = EXPR_LOC_OR_HERE (src);
594 src = string_constant (src, &offset_node);
595 if (src == 0)
596 return NULL_TREE;
598 max = TREE_STRING_LENGTH (src) - 1;
599 ptr = TREE_STRING_POINTER (src);
601 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
603 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
604 compute the offset to the following null if we don't know where to
605 start searching for it. */
606 int i;
608 for (i = 0; i < max; i++)
609 if (ptr[i] == 0)
610 return NULL_TREE;
612 /* We don't know the starting offset, but we do know that the string
613 has no internal zero bytes. We can assume that the offset falls
614 within the bounds of the string; otherwise, the programmer deserves
615 what he gets. Subtract the offset from the length of the string,
616 and return that. This would perhaps not be valid if we were dealing
617 with named arrays in addition to literal string constants. */
619 return size_diffop_loc (loc, size_int (max), offset_node);
622 /* We have a known offset into the string. Start searching there for
623 a null character if we can represent it as a single HOST_WIDE_INT. */
624 if (offset_node == 0)
625 offset = 0;
626 else if (! host_integerp (offset_node, 0))
627 offset = -1;
628 else
629 offset = tree_low_cst (offset_node, 0);
631 /* If the offset is known to be out of bounds, warn, and call strlen at
632 runtime. */
633 if (offset < 0 || offset > max)
635 /* Suppress multiple warnings for propagated constant strings. */
636 if (! TREE_NO_WARNING (src))
638 warning_at (loc, 0, "offset outside bounds of constant string");
639 TREE_NO_WARNING (src) = 1;
641 return NULL_TREE;
644 /* Use strlen to search for the first zero byte. Since any strings
645 constructed with build_string will have nulls appended, we win even
646 if we get handed something like (char[4])"abcd".
648 Since OFFSET is our starting index into the string, no further
649 calculation is needed. */
650 return ssize_int (strlen (ptr + offset));
653 /* Return a char pointer for a C string if it is a string constant
654 or sum of string constant and integer constant. */
656 static const char *
657 c_getstr (tree src)
659 tree offset_node;
661 src = string_constant (src, &offset_node);
662 if (src == 0)
663 return 0;
665 if (offset_node == 0)
666 return TREE_STRING_POINTER (src);
667 else if (!host_integerp (offset_node, 1)
668 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
669 return 0;
671 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
674 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
675 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
677 static rtx
678 c_readstr (const char *str, enum machine_mode mode)
680 HOST_WIDE_INT c[2];
681 HOST_WIDE_INT ch;
682 unsigned int i, j;
684 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
686 c[0] = 0;
687 c[1] = 0;
688 ch = 1;
689 for (i = 0; i < GET_MODE_SIZE (mode); i++)
691 j = i;
692 if (WORDS_BIG_ENDIAN)
693 j = GET_MODE_SIZE (mode) - i - 1;
694 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
695 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
696 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
697 j *= BITS_PER_UNIT;
698 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
700 if (ch)
701 ch = (unsigned char) str[i];
702 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
704 return immed_double_const (c[0], c[1], mode);
707 /* Cast a target constant CST to target CHAR and if that value fits into
708 host char type, return zero and put that value into variable pointed to by
709 P. */
711 static int
712 target_char_cast (tree cst, char *p)
714 unsigned HOST_WIDE_INT val, hostval;
716 if (TREE_CODE (cst) != INTEGER_CST
717 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
718 return 1;
720 val = TREE_INT_CST_LOW (cst);
721 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
722 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
724 hostval = val;
725 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
726 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
728 if (val != hostval)
729 return 1;
731 *p = hostval;
732 return 0;
735 /* Similar to save_expr, but assumes that arbitrary code is not executed
736 in between the multiple evaluations. In particular, we assume that a
737 non-addressable local variable will not be modified. */
739 static tree
740 builtin_save_expr (tree exp)
742 if (TREE_CODE (exp) == SSA_NAME
743 || (TREE_ADDRESSABLE (exp) == 0
744 && (TREE_CODE (exp) == PARM_DECL
745 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
746 return exp;
748 return save_expr (exp);
751 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
752 times to get the address of either a higher stack frame, or a return
753 address located within it (depending on FNDECL_CODE). */
755 static rtx
756 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
758 int i;
760 #ifdef INITIAL_FRAME_ADDRESS_RTX
761 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
762 #else
763 rtx tem;
765 /* For a zero count with __builtin_return_address, we don't care what
766 frame address we return, because target-specific definitions will
767 override us. Therefore frame pointer elimination is OK, and using
768 the soft frame pointer is OK.
770 For a nonzero count, or a zero count with __builtin_frame_address,
771 we require a stable offset from the current frame pointer to the
772 previous one, so we must use the hard frame pointer, and
773 we must disable frame pointer elimination. */
774 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
775 tem = frame_pointer_rtx;
776 else
778 tem = hard_frame_pointer_rtx;
780 /* Tell reload not to eliminate the frame pointer. */
781 crtl->accesses_prior_frames = 1;
783 #endif
785 /* Some machines need special handling before we can access
786 arbitrary frames. For example, on the SPARC, we must first flush
787 all register windows to the stack. */
788 #ifdef SETUP_FRAME_ADDRESSES
789 if (count > 0)
790 SETUP_FRAME_ADDRESSES ();
791 #endif
793 /* On the SPARC, the return address is not in the frame, it is in a
794 register. There is no way to access it off of the current frame
795 pointer, but it can be accessed off the previous frame pointer by
796 reading the value from the register window save area. */
797 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
798 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
799 count--;
800 #endif
802 /* Scan back COUNT frames to the specified frame. */
803 for (i = 0; i < count; i++)
805 /* Assume the dynamic chain pointer is in the word that the
806 frame address points to, unless otherwise specified. */
807 #ifdef DYNAMIC_CHAIN_ADDRESS
808 tem = DYNAMIC_CHAIN_ADDRESS (tem);
809 #endif
810 tem = memory_address (Pmode, tem);
811 tem = gen_frame_mem (Pmode, tem);
812 tem = copy_to_reg (tem);
815 /* For __builtin_frame_address, return what we've got. But, on
816 the SPARC for example, we may have to add a bias. */
817 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
818 #ifdef FRAME_ADDR_RTX
819 return FRAME_ADDR_RTX (tem);
820 #else
821 return tem;
822 #endif
824 /* For __builtin_return_address, get the return address from that frame. */
825 #ifdef RETURN_ADDR_RTX
826 tem = RETURN_ADDR_RTX (count, tem);
827 #else
828 tem = memory_address (Pmode,
829 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
830 tem = gen_frame_mem (Pmode, tem);
831 #endif
832 return tem;
835 /* Alias set used for setjmp buffer. */
836 static alias_set_type setjmp_alias_set = -1;
838 /* Construct the leading half of a __builtin_setjmp call. Control will
839 return to RECEIVER_LABEL. This is also called directly by the SJLJ
840 exception handling code. */
842 void
843 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
845 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
846 rtx stack_save;
847 rtx mem;
849 if (setjmp_alias_set == -1)
850 setjmp_alias_set = new_alias_set ();
852 buf_addr = convert_memory_address (Pmode, buf_addr);
854 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
856 /* We store the frame pointer and the address of receiver_label in
857 the buffer and use the rest of it for the stack save area, which
858 is machine-dependent. */
860 mem = gen_rtx_MEM (Pmode, buf_addr);
861 set_mem_alias_set (mem, setjmp_alias_set);
862 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
864 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
865 GET_MODE_SIZE (Pmode))),
866 set_mem_alias_set (mem, setjmp_alias_set);
868 emit_move_insn (validize_mem (mem),
869 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
871 stack_save = gen_rtx_MEM (sa_mode,
872 plus_constant (Pmode, buf_addr,
873 2 * GET_MODE_SIZE (Pmode)));
874 set_mem_alias_set (stack_save, setjmp_alias_set);
875 emit_stack_save (SAVE_NONLOCAL, &stack_save);
877 /* If there is further processing to do, do it. */
878 #ifdef HAVE_builtin_setjmp_setup
879 if (HAVE_builtin_setjmp_setup)
880 emit_insn (gen_builtin_setjmp_setup (buf_addr));
881 #endif
883 /* We have a nonlocal label. */
884 cfun->has_nonlocal_label = 1;
887 /* Construct the trailing part of a __builtin_setjmp call. This is
888 also called directly by the SJLJ exception handling code. */
890 void
891 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
893 rtx chain;
895 /* Clobber the FP when we get here, so we have to make sure it's
896 marked as used by this function. */
897 emit_use (hard_frame_pointer_rtx);
899 /* Mark the static chain as clobbered here so life information
900 doesn't get messed up for it. */
901 chain = targetm.calls.static_chain (current_function_decl, true);
902 if (chain && REG_P (chain))
903 emit_clobber (chain);
905 /* Now put in the code to restore the frame pointer, and argument
906 pointer, if needed. */
907 #ifdef HAVE_nonlocal_goto
908 if (! HAVE_nonlocal_goto)
909 #endif
911 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
912 /* This might change the hard frame pointer in ways that aren't
913 apparent to early optimization passes, so force a clobber. */
914 emit_clobber (hard_frame_pointer_rtx);
917 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
918 if (fixed_regs[ARG_POINTER_REGNUM])
920 #ifdef ELIMINABLE_REGS
921 size_t i;
922 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
924 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
925 if (elim_regs[i].from == ARG_POINTER_REGNUM
926 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
927 break;
929 if (i == ARRAY_SIZE (elim_regs))
930 #endif
932 /* Now restore our arg pointer from the address at which it
933 was saved in our stack frame. */
934 emit_move_insn (crtl->args.internal_arg_pointer,
935 copy_to_reg (get_arg_pointer_save_area ()));
938 #endif
940 #ifdef HAVE_builtin_setjmp_receiver
941 if (HAVE_builtin_setjmp_receiver)
942 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
943 else
944 #endif
945 #ifdef HAVE_nonlocal_goto_receiver
946 if (HAVE_nonlocal_goto_receiver)
947 emit_insn (gen_nonlocal_goto_receiver ());
948 else
949 #endif
950 { /* Nothing */ }
952 /* We must not allow the code we just generated to be reordered by
953 scheduling. Specifically, the update of the frame pointer must
954 happen immediately, not later. */
955 emit_insn (gen_blockage ());
958 /* __builtin_longjmp is passed a pointer to an array of five words (not
959 all will be used on all machines). It operates similarly to the C
960 library function of the same name, but is more efficient. Much of
961 the code below is copied from the handling of non-local gotos. */
963 static void
964 expand_builtin_longjmp (rtx buf_addr, rtx value)
966 rtx fp, lab, stack, insn, last;
967 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
969 /* DRAP is needed for stack realign if longjmp is expanded to current
970 function */
971 if (SUPPORTS_STACK_ALIGNMENT)
972 crtl->need_drap = true;
974 if (setjmp_alias_set == -1)
975 setjmp_alias_set = new_alias_set ();
977 buf_addr = convert_memory_address (Pmode, buf_addr);
979 buf_addr = force_reg (Pmode, buf_addr);
981 /* We require that the user must pass a second argument of 1, because
982 that is what builtin_setjmp will return. */
983 gcc_assert (value == const1_rtx);
985 last = get_last_insn ();
986 #ifdef HAVE_builtin_longjmp
987 if (HAVE_builtin_longjmp)
988 emit_insn (gen_builtin_longjmp (buf_addr));
989 else
990 #endif
992 fp = gen_rtx_MEM (Pmode, buf_addr);
993 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
994 GET_MODE_SIZE (Pmode)));
996 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
997 2 * GET_MODE_SIZE (Pmode)));
998 set_mem_alias_set (fp, setjmp_alias_set);
999 set_mem_alias_set (lab, setjmp_alias_set);
1000 set_mem_alias_set (stack, setjmp_alias_set);
1002 /* Pick up FP, label, and SP from the block and jump. This code is
1003 from expand_goto in stmt.c; see there for detailed comments. */
1004 #ifdef HAVE_nonlocal_goto
1005 if (HAVE_nonlocal_goto)
1006 /* We have to pass a value to the nonlocal_goto pattern that will
1007 get copied into the static_chain pointer, but it does not matter
1008 what that value is, because builtin_setjmp does not use it. */
1009 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1010 else
1011 #endif
1013 lab = copy_to_reg (lab);
1015 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1016 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1018 emit_move_insn (hard_frame_pointer_rtx, fp);
1019 emit_stack_restore (SAVE_NONLOCAL, stack);
1021 emit_use (hard_frame_pointer_rtx);
1022 emit_use (stack_pointer_rtx);
1023 emit_indirect_jump (lab);
1027 /* Search backwards and mark the jump insn as a non-local goto.
1028 Note that this precludes the use of __builtin_longjmp to a
1029 __builtin_setjmp target in the same function. However, we've
1030 already cautioned the user that these functions are for
1031 internal exception handling use only. */
1032 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1034 gcc_assert (insn != last);
1036 if (JUMP_P (insn))
1038 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1039 break;
1041 else if (CALL_P (insn))
1042 break;
1046 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1047 and the address of the save area. */
1049 static rtx
1050 expand_builtin_nonlocal_goto (tree exp)
1052 tree t_label, t_save_area;
1053 rtx r_label, r_save_area, r_fp, r_sp, insn;
1055 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1056 return NULL_RTX;
1058 t_label = CALL_EXPR_ARG (exp, 0);
1059 t_save_area = CALL_EXPR_ARG (exp, 1);
1061 r_label = expand_normal (t_label);
1062 r_label = convert_memory_address (Pmode, r_label);
1063 r_save_area = expand_normal (t_save_area);
1064 r_save_area = convert_memory_address (Pmode, r_save_area);
1065 /* Copy the address of the save location to a register just in case it was
1066 based on the frame pointer. */
1067 r_save_area = copy_to_reg (r_save_area);
1068 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1069 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1070 plus_constant (Pmode, r_save_area,
1071 GET_MODE_SIZE (Pmode)));
1073 crtl->has_nonlocal_goto = 1;
1075 #ifdef HAVE_nonlocal_goto
1076 /* ??? We no longer need to pass the static chain value, afaik. */
1077 if (HAVE_nonlocal_goto)
1078 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1079 else
1080 #endif
1082 r_label = copy_to_reg (r_label);
1084 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1085 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1087 /* Restore frame pointer for containing function. */
1088 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1089 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1091 /* USE of hard_frame_pointer_rtx added for consistency;
1092 not clear if really needed. */
1093 emit_use (hard_frame_pointer_rtx);
1094 emit_use (stack_pointer_rtx);
1096 /* If the architecture is using a GP register, we must
1097 conservatively assume that the target function makes use of it.
1098 The prologue of functions with nonlocal gotos must therefore
1099 initialize the GP register to the appropriate value, and we
1100 must then make sure that this value is live at the point
1101 of the jump. (Note that this doesn't necessarily apply
1102 to targets with a nonlocal_goto pattern; they are free
1103 to implement it in their own way. Note also that this is
1104 a no-op if the GP register is a global invariant.) */
1105 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1106 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1107 emit_use (pic_offset_table_rtx);
1109 emit_indirect_jump (r_label);
1112 /* Search backwards to the jump insn and mark it as a
1113 non-local goto. */
1114 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1116 if (JUMP_P (insn))
1118 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1119 break;
1121 else if (CALL_P (insn))
1122 break;
1125 return const0_rtx;
1128 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1129 (not all will be used on all machines) that was passed to __builtin_setjmp.
1130 It updates the stack pointer in that block to correspond to the current
1131 stack pointer. */
1133 static void
1134 expand_builtin_update_setjmp_buf (rtx buf_addr)
1136 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1137 rtx stack_save
1138 = gen_rtx_MEM (sa_mode,
1139 memory_address
1140 (sa_mode,
1141 plus_constant (Pmode, buf_addr,
1142 2 * GET_MODE_SIZE (Pmode))));
1144 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1147 /* Expand a call to __builtin_prefetch. For a target that does not support
1148 data prefetch, evaluate the memory address argument in case it has side
1149 effects. */
1151 static void
1152 expand_builtin_prefetch (tree exp)
1154 tree arg0, arg1, arg2;
1155 int nargs;
1156 rtx op0, op1, op2;
1158 if (!validate_arglist (exp, POINTER_TYPE, 0))
1159 return;
1161 arg0 = CALL_EXPR_ARG (exp, 0);
1163 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1164 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1165 locality). */
1166 nargs = call_expr_nargs (exp);
1167 if (nargs > 1)
1168 arg1 = CALL_EXPR_ARG (exp, 1);
1169 else
1170 arg1 = integer_zero_node;
1171 if (nargs > 2)
1172 arg2 = CALL_EXPR_ARG (exp, 2);
1173 else
1174 arg2 = integer_three_node;
1176 /* Argument 0 is an address. */
1177 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1179 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1180 if (TREE_CODE (arg1) != INTEGER_CST)
1182 error ("second argument to %<__builtin_prefetch%> must be a constant");
1183 arg1 = integer_zero_node;
1185 op1 = expand_normal (arg1);
1186 /* Argument 1 must be either zero or one. */
1187 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1189 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1190 " using zero");
1191 op1 = const0_rtx;
1194 /* Argument 2 (locality) must be a compile-time constant int. */
1195 if (TREE_CODE (arg2) != INTEGER_CST)
1197 error ("third argument to %<__builtin_prefetch%> must be a constant");
1198 arg2 = integer_zero_node;
1200 op2 = expand_normal (arg2);
1201 /* Argument 2 must be 0, 1, 2, or 3. */
1202 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1204 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1205 op2 = const0_rtx;
1208 #ifdef HAVE_prefetch
1209 if (HAVE_prefetch)
1211 struct expand_operand ops[3];
1213 create_address_operand (&ops[0], op0);
1214 create_integer_operand (&ops[1], INTVAL (op1));
1215 create_integer_operand (&ops[2], INTVAL (op2));
1216 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1217 return;
1219 #endif
1221 /* Don't do anything with direct references to volatile memory, but
1222 generate code to handle other side effects. */
1223 if (!MEM_P (op0) && side_effects_p (op0))
1224 emit_insn (op0);
1227 /* Get a MEM rtx for expression EXP which is the address of an operand
1228 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1229 the maximum length of the block of memory that might be accessed or
1230 NULL if unknown. */
1232 static rtx
1233 get_memory_rtx (tree exp, tree len)
1235 tree orig_exp = exp;
1236 rtx addr, mem;
1238 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1239 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1240 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1241 exp = TREE_OPERAND (exp, 0);
1243 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1244 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1246 /* Get an expression we can use to find the attributes to assign to MEM.
1247 First remove any nops. */
1248 while (CONVERT_EXPR_P (exp)
1249 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1250 exp = TREE_OPERAND (exp, 0);
1252 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1253 (as builtin stringops may alias with anything). */
1254 exp = fold_build2 (MEM_REF,
1255 build_array_type (char_type_node,
1256 build_range_type (sizetype,
1257 size_one_node, len)),
1258 exp, build_int_cst (ptr_type_node, 0));
1260 /* If the MEM_REF has no acceptable address, try to get the base object
1261 from the original address we got, and build an all-aliasing
1262 unknown-sized access to that one. */
1263 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1264 set_mem_attributes (mem, exp, 0);
1265 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1266 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1267 0))))
1269 exp = build_fold_addr_expr (exp);
1270 exp = fold_build2 (MEM_REF,
1271 build_array_type (char_type_node,
1272 build_range_type (sizetype,
1273 size_zero_node,
1274 NULL)),
1275 exp, build_int_cst (ptr_type_node, 0));
1276 set_mem_attributes (mem, exp, 0);
1278 set_mem_alias_set (mem, 0);
1279 return mem;
1282 /* Built-in functions to perform an untyped call and return. */
1284 #define apply_args_mode \
1285 (this_target_builtins->x_apply_args_mode)
1286 #define apply_result_mode \
1287 (this_target_builtins->x_apply_result_mode)
1289 /* Return the size required for the block returned by __builtin_apply_args,
1290 and initialize apply_args_mode. */
1292 static int
1293 apply_args_size (void)
1295 static int size = -1;
1296 int align;
1297 unsigned int regno;
1298 enum machine_mode mode;
1300 /* The values computed by this function never change. */
1301 if (size < 0)
1303 /* The first value is the incoming arg-pointer. */
1304 size = GET_MODE_SIZE (Pmode);
1306 /* The second value is the structure value address unless this is
1307 passed as an "invisible" first argument. */
1308 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1309 size += GET_MODE_SIZE (Pmode);
1311 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1312 if (FUNCTION_ARG_REGNO_P (regno))
1314 mode = targetm.calls.get_raw_arg_mode (regno);
1316 gcc_assert (mode != VOIDmode);
1318 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1319 if (size % align != 0)
1320 size = CEIL (size, align) * align;
1321 size += GET_MODE_SIZE (mode);
1322 apply_args_mode[regno] = mode;
1324 else
1326 apply_args_mode[regno] = VOIDmode;
1329 return size;
1332 /* Return the size required for the block returned by __builtin_apply,
1333 and initialize apply_result_mode. */
1335 static int
1336 apply_result_size (void)
1338 static int size = -1;
1339 int align, regno;
1340 enum machine_mode mode;
1342 /* The values computed by this function never change. */
1343 if (size < 0)
1345 size = 0;
1347 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1348 if (targetm.calls.function_value_regno_p (regno))
1350 mode = targetm.calls.get_raw_result_mode (regno);
1352 gcc_assert (mode != VOIDmode);
1354 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1355 if (size % align != 0)
1356 size = CEIL (size, align) * align;
1357 size += GET_MODE_SIZE (mode);
1358 apply_result_mode[regno] = mode;
1360 else
1361 apply_result_mode[regno] = VOIDmode;
1363 /* Allow targets that use untyped_call and untyped_return to override
1364 the size so that machine-specific information can be stored here. */
1365 #ifdef APPLY_RESULT_SIZE
1366 size = APPLY_RESULT_SIZE;
1367 #endif
1369 return size;
1372 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1373 /* Create a vector describing the result block RESULT. If SAVEP is true,
1374 the result block is used to save the values; otherwise it is used to
1375 restore the values. */
1377 static rtx
1378 result_vector (int savep, rtx result)
1380 int regno, size, align, nelts;
1381 enum machine_mode mode;
1382 rtx reg, mem;
1383 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1385 size = nelts = 0;
1386 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1387 if ((mode = apply_result_mode[regno]) != VOIDmode)
1389 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1390 if (size % align != 0)
1391 size = CEIL (size, align) * align;
1392 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1393 mem = adjust_address (result, mode, size);
1394 savevec[nelts++] = (savep
1395 ? gen_rtx_SET (VOIDmode, mem, reg)
1396 : gen_rtx_SET (VOIDmode, reg, mem));
1397 size += GET_MODE_SIZE (mode);
1399 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1401 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1403 /* Save the state required to perform an untyped call with the same
1404 arguments as were passed to the current function. */
1406 static rtx
1407 expand_builtin_apply_args_1 (void)
1409 rtx registers, tem;
1410 int size, align, regno;
1411 enum machine_mode mode;
1412 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1414 /* Create a block where the arg-pointer, structure value address,
1415 and argument registers can be saved. */
1416 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1418 /* Walk past the arg-pointer and structure value address. */
1419 size = GET_MODE_SIZE (Pmode);
1420 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1421 size += GET_MODE_SIZE (Pmode);
1423 /* Save each register used in calling a function to the block. */
1424 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1425 if ((mode = apply_args_mode[regno]) != VOIDmode)
1427 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1428 if (size % align != 0)
1429 size = CEIL (size, align) * align;
1431 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1433 emit_move_insn (adjust_address (registers, mode, size), tem);
1434 size += GET_MODE_SIZE (mode);
1437 /* Save the arg pointer to the block. */
1438 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1439 #ifdef STACK_GROWS_DOWNWARD
1440 /* We need the pointer as the caller actually passed them to us, not
1441 as we might have pretended they were passed. Make sure it's a valid
1442 operand, as emit_move_insn isn't expected to handle a PLUS. */
1444 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1445 NULL_RTX);
1446 #endif
1447 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1449 size = GET_MODE_SIZE (Pmode);
1451 /* Save the structure value address unless this is passed as an
1452 "invisible" first argument. */
1453 if (struct_incoming_value)
1455 emit_move_insn (adjust_address (registers, Pmode, size),
1456 copy_to_reg (struct_incoming_value));
1457 size += GET_MODE_SIZE (Pmode);
1460 /* Return the address of the block. */
1461 return copy_addr_to_reg (XEXP (registers, 0));
1464 /* __builtin_apply_args returns block of memory allocated on
1465 the stack into which is stored the arg pointer, structure
1466 value address, static chain, and all the registers that might
1467 possibly be used in performing a function call. The code is
1468 moved to the start of the function so the incoming values are
1469 saved. */
1471 static rtx
1472 expand_builtin_apply_args (void)
1474 /* Don't do __builtin_apply_args more than once in a function.
1475 Save the result of the first call and reuse it. */
1476 if (apply_args_value != 0)
1477 return apply_args_value;
1479 /* When this function is called, it means that registers must be
1480 saved on entry to this function. So we migrate the
1481 call to the first insn of this function. */
1482 rtx temp;
1483 rtx seq;
1485 start_sequence ();
1486 temp = expand_builtin_apply_args_1 ();
1487 seq = get_insns ();
1488 end_sequence ();
1490 apply_args_value = temp;
1492 /* Put the insns after the NOTE that starts the function.
1493 If this is inside a start_sequence, make the outer-level insn
1494 chain current, so the code is placed at the start of the
1495 function. If internal_arg_pointer is a non-virtual pseudo,
1496 it needs to be placed after the function that initializes
1497 that pseudo. */
1498 push_topmost_sequence ();
1499 if (REG_P (crtl->args.internal_arg_pointer)
1500 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1501 emit_insn_before (seq, parm_birth_insn);
1502 else
1503 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1504 pop_topmost_sequence ();
1505 return temp;
1509 /* Perform an untyped call and save the state required to perform an
1510 untyped return of whatever value was returned by the given function. */
1512 static rtx
1513 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1515 int size, align, regno;
1516 enum machine_mode mode;
1517 rtx incoming_args, result, reg, dest, src, call_insn;
1518 rtx old_stack_level = 0;
1519 rtx call_fusage = 0;
1520 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1522 arguments = convert_memory_address (Pmode, arguments);
1524 /* Create a block where the return registers can be saved. */
1525 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1527 /* Fetch the arg pointer from the ARGUMENTS block. */
1528 incoming_args = gen_reg_rtx (Pmode);
1529 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1530 #ifndef STACK_GROWS_DOWNWARD
1531 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1532 incoming_args, 0, OPTAB_LIB_WIDEN);
1533 #endif
1535 /* Push a new argument block and copy the arguments. Do not allow
1536 the (potential) memcpy call below to interfere with our stack
1537 manipulations. */
1538 do_pending_stack_adjust ();
1539 NO_DEFER_POP;
1541 /* Save the stack with nonlocal if available. */
1542 #ifdef HAVE_save_stack_nonlocal
1543 if (HAVE_save_stack_nonlocal)
1544 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1545 else
1546 #endif
1547 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1549 /* Allocate a block of memory onto the stack and copy the memory
1550 arguments to the outgoing arguments address. We can pass TRUE
1551 as the 4th argument because we just saved the stack pointer
1552 and will restore it right after the call. */
1553 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1555 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1556 may have already set current_function_calls_alloca to true.
1557 current_function_calls_alloca won't be set if argsize is zero,
1558 so we have to guarantee need_drap is true here. */
1559 if (SUPPORTS_STACK_ALIGNMENT)
1560 crtl->need_drap = true;
1562 dest = virtual_outgoing_args_rtx;
1563 #ifndef STACK_GROWS_DOWNWARD
1564 if (CONST_INT_P (argsize))
1565 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1566 else
1567 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1568 #endif
1569 dest = gen_rtx_MEM (BLKmode, dest);
1570 set_mem_align (dest, PARM_BOUNDARY);
1571 src = gen_rtx_MEM (BLKmode, incoming_args);
1572 set_mem_align (src, PARM_BOUNDARY);
1573 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1575 /* Refer to the argument block. */
1576 apply_args_size ();
1577 arguments = gen_rtx_MEM (BLKmode, arguments);
1578 set_mem_align (arguments, PARM_BOUNDARY);
1580 /* Walk past the arg-pointer and structure value address. */
1581 size = GET_MODE_SIZE (Pmode);
1582 if (struct_value)
1583 size += GET_MODE_SIZE (Pmode);
1585 /* Restore each of the registers previously saved. Make USE insns
1586 for each of these registers for use in making the call. */
1587 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1588 if ((mode = apply_args_mode[regno]) != VOIDmode)
1590 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1591 if (size % align != 0)
1592 size = CEIL (size, align) * align;
1593 reg = gen_rtx_REG (mode, regno);
1594 emit_move_insn (reg, adjust_address (arguments, mode, size));
1595 use_reg (&call_fusage, reg);
1596 size += GET_MODE_SIZE (mode);
1599 /* Restore the structure value address unless this is passed as an
1600 "invisible" first argument. */
1601 size = GET_MODE_SIZE (Pmode);
1602 if (struct_value)
1604 rtx value = gen_reg_rtx (Pmode);
1605 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1606 emit_move_insn (struct_value, value);
1607 if (REG_P (struct_value))
1608 use_reg (&call_fusage, struct_value);
1609 size += GET_MODE_SIZE (Pmode);
1612 /* All arguments and registers used for the call are set up by now! */
1613 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1615 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1616 and we don't want to load it into a register as an optimization,
1617 because prepare_call_address already did it if it should be done. */
1618 if (GET_CODE (function) != SYMBOL_REF)
1619 function = memory_address (FUNCTION_MODE, function);
1621 /* Generate the actual call instruction and save the return value. */
1622 #ifdef HAVE_untyped_call
1623 if (HAVE_untyped_call)
1624 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1625 result, result_vector (1, result)));
1626 else
1627 #endif
1628 #ifdef HAVE_call_value
1629 if (HAVE_call_value)
1631 rtx valreg = 0;
1633 /* Locate the unique return register. It is not possible to
1634 express a call that sets more than one return register using
1635 call_value; use untyped_call for that. In fact, untyped_call
1636 only needs to save the return registers in the given block. */
1637 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1638 if ((mode = apply_result_mode[regno]) != VOIDmode)
1640 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1642 valreg = gen_rtx_REG (mode, regno);
1645 emit_call_insn (GEN_CALL_VALUE (valreg,
1646 gen_rtx_MEM (FUNCTION_MODE, function),
1647 const0_rtx, NULL_RTX, const0_rtx));
1649 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1651 else
1652 #endif
1653 gcc_unreachable ();
1655 /* Find the CALL insn we just emitted, and attach the register usage
1656 information. */
1657 call_insn = last_call_insn ();
1658 add_function_usage_to (call_insn, call_fusage);
1660 /* Restore the stack. */
1661 #ifdef HAVE_save_stack_nonlocal
1662 if (HAVE_save_stack_nonlocal)
1663 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1664 else
1665 #endif
1666 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1667 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1669 OK_DEFER_POP;
1671 /* Return the address of the result block. */
1672 result = copy_addr_to_reg (XEXP (result, 0));
1673 return convert_memory_address (ptr_mode, result);
1676 /* Perform an untyped return. */
1678 static void
1679 expand_builtin_return (rtx result)
1681 int size, align, regno;
1682 enum machine_mode mode;
1683 rtx reg;
1684 rtx call_fusage = 0;
1686 result = convert_memory_address (Pmode, result);
1688 apply_result_size ();
1689 result = gen_rtx_MEM (BLKmode, result);
1691 #ifdef HAVE_untyped_return
1692 if (HAVE_untyped_return)
1694 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1695 emit_barrier ();
1696 return;
1698 #endif
1700 /* Restore the return value and note that each value is used. */
1701 size = 0;
1702 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1703 if ((mode = apply_result_mode[regno]) != VOIDmode)
1705 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1706 if (size % align != 0)
1707 size = CEIL (size, align) * align;
1708 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1709 emit_move_insn (reg, adjust_address (result, mode, size));
1711 push_to_sequence (call_fusage);
1712 emit_use (reg);
1713 call_fusage = get_insns ();
1714 end_sequence ();
1715 size += GET_MODE_SIZE (mode);
1718 /* Put the USE insns before the return. */
1719 emit_insn (call_fusage);
1721 /* Return whatever values was restored by jumping directly to the end
1722 of the function. */
1723 expand_naked_return ();
1726 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1728 static enum type_class
1729 type_to_class (tree type)
1731 switch (TREE_CODE (type))
1733 case VOID_TYPE: return void_type_class;
1734 case INTEGER_TYPE: return integer_type_class;
1735 case ENUMERAL_TYPE: return enumeral_type_class;
1736 case BOOLEAN_TYPE: return boolean_type_class;
1737 case POINTER_TYPE: return pointer_type_class;
1738 case REFERENCE_TYPE: return reference_type_class;
1739 case OFFSET_TYPE: return offset_type_class;
1740 case REAL_TYPE: return real_type_class;
1741 case COMPLEX_TYPE: return complex_type_class;
1742 case FUNCTION_TYPE: return function_type_class;
1743 case METHOD_TYPE: return method_type_class;
1744 case RECORD_TYPE: return record_type_class;
1745 case UNION_TYPE:
1746 case QUAL_UNION_TYPE: return union_type_class;
1747 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1748 ? string_type_class : array_type_class);
1749 case LANG_TYPE: return lang_type_class;
1750 default: return no_type_class;
1754 /* Expand a call EXP to __builtin_classify_type. */
1756 static rtx
1757 expand_builtin_classify_type (tree exp)
1759 if (call_expr_nargs (exp))
1760 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1761 return GEN_INT (no_type_class);
1764 /* This helper macro, meant to be used in mathfn_built_in below,
1765 determines which among a set of three builtin math functions is
1766 appropriate for a given type mode. The `F' and `L' cases are
1767 automatically generated from the `double' case. */
1768 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1769 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1770 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1771 fcodel = BUILT_IN_MATHFN##L ; break;
1772 /* Similar to above, but appends _R after any F/L suffix. */
1773 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1774 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1775 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1776 fcodel = BUILT_IN_MATHFN##L_R ; break;
1778 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1779 if available. If IMPLICIT is true use the implicit builtin declaration,
1780 otherwise use the explicit declaration. If we can't do the conversion,
1781 return zero. */
1783 static tree
1784 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1786 enum built_in_function fcode, fcodef, fcodel, fcode2;
1788 switch (fn)
1790 CASE_MATHFN (BUILT_IN_ACOS)
1791 CASE_MATHFN (BUILT_IN_ACOSH)
1792 CASE_MATHFN (BUILT_IN_ASIN)
1793 CASE_MATHFN (BUILT_IN_ASINH)
1794 CASE_MATHFN (BUILT_IN_ATAN)
1795 CASE_MATHFN (BUILT_IN_ATAN2)
1796 CASE_MATHFN (BUILT_IN_ATANH)
1797 CASE_MATHFN (BUILT_IN_CBRT)
1798 CASE_MATHFN (BUILT_IN_CEIL)
1799 CASE_MATHFN (BUILT_IN_CEXPI)
1800 CASE_MATHFN (BUILT_IN_COPYSIGN)
1801 CASE_MATHFN (BUILT_IN_COS)
1802 CASE_MATHFN (BUILT_IN_COSH)
1803 CASE_MATHFN (BUILT_IN_DREM)
1804 CASE_MATHFN (BUILT_IN_ERF)
1805 CASE_MATHFN (BUILT_IN_ERFC)
1806 CASE_MATHFN (BUILT_IN_EXP)
1807 CASE_MATHFN (BUILT_IN_EXP10)
1808 CASE_MATHFN (BUILT_IN_EXP2)
1809 CASE_MATHFN (BUILT_IN_EXPM1)
1810 CASE_MATHFN (BUILT_IN_FABS)
1811 CASE_MATHFN (BUILT_IN_FDIM)
1812 CASE_MATHFN (BUILT_IN_FLOOR)
1813 CASE_MATHFN (BUILT_IN_FMA)
1814 CASE_MATHFN (BUILT_IN_FMAX)
1815 CASE_MATHFN (BUILT_IN_FMIN)
1816 CASE_MATHFN (BUILT_IN_FMOD)
1817 CASE_MATHFN (BUILT_IN_FREXP)
1818 CASE_MATHFN (BUILT_IN_GAMMA)
1819 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1820 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1821 CASE_MATHFN (BUILT_IN_HYPOT)
1822 CASE_MATHFN (BUILT_IN_ILOGB)
1823 CASE_MATHFN (BUILT_IN_ICEIL)
1824 CASE_MATHFN (BUILT_IN_IFLOOR)
1825 CASE_MATHFN (BUILT_IN_INF)
1826 CASE_MATHFN (BUILT_IN_IRINT)
1827 CASE_MATHFN (BUILT_IN_IROUND)
1828 CASE_MATHFN (BUILT_IN_ISINF)
1829 CASE_MATHFN (BUILT_IN_J0)
1830 CASE_MATHFN (BUILT_IN_J1)
1831 CASE_MATHFN (BUILT_IN_JN)
1832 CASE_MATHFN (BUILT_IN_LCEIL)
1833 CASE_MATHFN (BUILT_IN_LDEXP)
1834 CASE_MATHFN (BUILT_IN_LFLOOR)
1835 CASE_MATHFN (BUILT_IN_LGAMMA)
1836 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1837 CASE_MATHFN (BUILT_IN_LLCEIL)
1838 CASE_MATHFN (BUILT_IN_LLFLOOR)
1839 CASE_MATHFN (BUILT_IN_LLRINT)
1840 CASE_MATHFN (BUILT_IN_LLROUND)
1841 CASE_MATHFN (BUILT_IN_LOG)
1842 CASE_MATHFN (BUILT_IN_LOG10)
1843 CASE_MATHFN (BUILT_IN_LOG1P)
1844 CASE_MATHFN (BUILT_IN_LOG2)
1845 CASE_MATHFN (BUILT_IN_LOGB)
1846 CASE_MATHFN (BUILT_IN_LRINT)
1847 CASE_MATHFN (BUILT_IN_LROUND)
1848 CASE_MATHFN (BUILT_IN_MODF)
1849 CASE_MATHFN (BUILT_IN_NAN)
1850 CASE_MATHFN (BUILT_IN_NANS)
1851 CASE_MATHFN (BUILT_IN_NEARBYINT)
1852 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1853 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1854 CASE_MATHFN (BUILT_IN_POW)
1855 CASE_MATHFN (BUILT_IN_POWI)
1856 CASE_MATHFN (BUILT_IN_POW10)
1857 CASE_MATHFN (BUILT_IN_REMAINDER)
1858 CASE_MATHFN (BUILT_IN_REMQUO)
1859 CASE_MATHFN (BUILT_IN_RINT)
1860 CASE_MATHFN (BUILT_IN_ROUND)
1861 CASE_MATHFN (BUILT_IN_SCALB)
1862 CASE_MATHFN (BUILT_IN_SCALBLN)
1863 CASE_MATHFN (BUILT_IN_SCALBN)
1864 CASE_MATHFN (BUILT_IN_SIGNBIT)
1865 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1866 CASE_MATHFN (BUILT_IN_SIN)
1867 CASE_MATHFN (BUILT_IN_SINCOS)
1868 CASE_MATHFN (BUILT_IN_SINH)
1869 CASE_MATHFN (BUILT_IN_SQRT)
1870 CASE_MATHFN (BUILT_IN_TAN)
1871 CASE_MATHFN (BUILT_IN_TANH)
1872 CASE_MATHFN (BUILT_IN_TGAMMA)
1873 CASE_MATHFN (BUILT_IN_TRUNC)
1874 CASE_MATHFN (BUILT_IN_Y0)
1875 CASE_MATHFN (BUILT_IN_Y1)
1876 CASE_MATHFN (BUILT_IN_YN)
1878 default:
1879 return NULL_TREE;
1882 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1883 fcode2 = fcode;
1884 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1885 fcode2 = fcodef;
1886 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1887 fcode2 = fcodel;
1888 else
1889 return NULL_TREE;
1891 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1892 return NULL_TREE;
1894 return builtin_decl_explicit (fcode2);
1897 /* Like mathfn_built_in_1(), but always use the implicit array. */
1899 tree
1900 mathfn_built_in (tree type, enum built_in_function fn)
1902 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1905 /* If errno must be maintained, expand the RTL to check if the result,
1906 TARGET, of a built-in function call, EXP, is NaN, and if so set
1907 errno to EDOM. */
1909 static void
1910 expand_errno_check (tree exp, rtx target)
1912 rtx lab = gen_label_rtx ();
1914 /* Test the result; if it is NaN, set errno=EDOM because
1915 the argument was not in the domain. */
1916 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1917 NULL_RTX, NULL_RTX, lab,
1918 /* The jump is very likely. */
1919 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1921 #ifdef TARGET_EDOM
1922 /* If this built-in doesn't throw an exception, set errno directly. */
1923 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1925 #ifdef GEN_ERRNO_RTX
1926 rtx errno_rtx = GEN_ERRNO_RTX;
1927 #else
1928 rtx errno_rtx
1929 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1930 #endif
1931 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1932 emit_label (lab);
1933 return;
1935 #endif
1937 /* Make sure the library call isn't expanded as a tail call. */
1938 CALL_EXPR_TAILCALL (exp) = 0;
1940 /* We can't set errno=EDOM directly; let the library call do it.
1941 Pop the arguments right away in case the call gets deleted. */
1942 NO_DEFER_POP;
1943 expand_call (exp, target, 0);
1944 OK_DEFER_POP;
1945 emit_label (lab);
1948 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1949 Return NULL_RTX if a normal call should be emitted rather than expanding
1950 the function in-line. EXP is the expression that is a call to the builtin
1951 function; if convenient, the result should be placed in TARGET.
1952 SUBTARGET may be used as the target for computing one of EXP's operands. */
1954 static rtx
1955 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1957 optab builtin_optab;
1958 rtx op0, insns;
1959 tree fndecl = get_callee_fndecl (exp);
1960 enum machine_mode mode;
1961 bool errno_set = false;
1962 tree arg;
1964 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1965 return NULL_RTX;
1967 arg = CALL_EXPR_ARG (exp, 0);
1969 switch (DECL_FUNCTION_CODE (fndecl))
1971 CASE_FLT_FN (BUILT_IN_SQRT):
1972 errno_set = ! tree_expr_nonnegative_p (arg);
1973 builtin_optab = sqrt_optab;
1974 break;
1975 CASE_FLT_FN (BUILT_IN_EXP):
1976 errno_set = true; builtin_optab = exp_optab; break;
1977 CASE_FLT_FN (BUILT_IN_EXP10):
1978 CASE_FLT_FN (BUILT_IN_POW10):
1979 errno_set = true; builtin_optab = exp10_optab; break;
1980 CASE_FLT_FN (BUILT_IN_EXP2):
1981 errno_set = true; builtin_optab = exp2_optab; break;
1982 CASE_FLT_FN (BUILT_IN_EXPM1):
1983 errno_set = true; builtin_optab = expm1_optab; break;
1984 CASE_FLT_FN (BUILT_IN_LOGB):
1985 errno_set = true; builtin_optab = logb_optab; break;
1986 CASE_FLT_FN (BUILT_IN_LOG):
1987 errno_set = true; builtin_optab = log_optab; break;
1988 CASE_FLT_FN (BUILT_IN_LOG10):
1989 errno_set = true; builtin_optab = log10_optab; break;
1990 CASE_FLT_FN (BUILT_IN_LOG2):
1991 errno_set = true; builtin_optab = log2_optab; break;
1992 CASE_FLT_FN (BUILT_IN_LOG1P):
1993 errno_set = true; builtin_optab = log1p_optab; break;
1994 CASE_FLT_FN (BUILT_IN_ASIN):
1995 builtin_optab = asin_optab; break;
1996 CASE_FLT_FN (BUILT_IN_ACOS):
1997 builtin_optab = acos_optab; break;
1998 CASE_FLT_FN (BUILT_IN_TAN):
1999 builtin_optab = tan_optab; break;
2000 CASE_FLT_FN (BUILT_IN_ATAN):
2001 builtin_optab = atan_optab; break;
2002 CASE_FLT_FN (BUILT_IN_FLOOR):
2003 builtin_optab = floor_optab; break;
2004 CASE_FLT_FN (BUILT_IN_CEIL):
2005 builtin_optab = ceil_optab; break;
2006 CASE_FLT_FN (BUILT_IN_TRUNC):
2007 builtin_optab = btrunc_optab; break;
2008 CASE_FLT_FN (BUILT_IN_ROUND):
2009 builtin_optab = round_optab; break;
2010 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2011 builtin_optab = nearbyint_optab;
2012 if (flag_trapping_math)
2013 break;
2014 /* Else fallthrough and expand as rint. */
2015 CASE_FLT_FN (BUILT_IN_RINT):
2016 builtin_optab = rint_optab; break;
2017 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2018 builtin_optab = significand_optab; break;
2019 default:
2020 gcc_unreachable ();
2023 /* Make a suitable register to place result in. */
2024 mode = TYPE_MODE (TREE_TYPE (exp));
2026 if (! flag_errno_math || ! HONOR_NANS (mode))
2027 errno_set = false;
2029 /* Before working hard, check whether the instruction is available. */
2030 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2031 && (!errno_set || !optimize_insn_for_size_p ()))
2033 target = gen_reg_rtx (mode);
2035 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2036 need to expand the argument again. This way, we will not perform
2037 side-effects more the once. */
2038 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2040 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2042 start_sequence ();
2044 /* Compute into TARGET.
2045 Set TARGET to wherever the result comes back. */
2046 target = expand_unop (mode, builtin_optab, op0, target, 0);
2048 if (target != 0)
2050 if (errno_set)
2051 expand_errno_check (exp, target);
2053 /* Output the entire sequence. */
2054 insns = get_insns ();
2055 end_sequence ();
2056 emit_insn (insns);
2057 return target;
2060 /* If we were unable to expand via the builtin, stop the sequence
2061 (without outputting the insns) and call to the library function
2062 with the stabilized argument list. */
2063 end_sequence ();
2066 return expand_call (exp, target, target == const0_rtx);
2069 /* Expand a call to the builtin binary math functions (pow and atan2).
2070 Return NULL_RTX if a normal call should be emitted rather than expanding the
2071 function in-line. EXP is the expression that is a call to the builtin
2072 function; if convenient, the result should be placed in TARGET.
2073 SUBTARGET may be used as the target for computing one of EXP's
2074 operands. */
2076 static rtx
2077 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2079 optab builtin_optab;
2080 rtx op0, op1, insns;
2081 int op1_type = REAL_TYPE;
2082 tree fndecl = get_callee_fndecl (exp);
2083 tree arg0, arg1;
2084 enum machine_mode mode;
2085 bool errno_set = true;
2087 switch (DECL_FUNCTION_CODE (fndecl))
2089 CASE_FLT_FN (BUILT_IN_SCALBN):
2090 CASE_FLT_FN (BUILT_IN_SCALBLN):
2091 CASE_FLT_FN (BUILT_IN_LDEXP):
2092 op1_type = INTEGER_TYPE;
2093 default:
2094 break;
2097 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2098 return NULL_RTX;
2100 arg0 = CALL_EXPR_ARG (exp, 0);
2101 arg1 = CALL_EXPR_ARG (exp, 1);
2103 switch (DECL_FUNCTION_CODE (fndecl))
2105 CASE_FLT_FN (BUILT_IN_POW):
2106 builtin_optab = pow_optab; break;
2107 CASE_FLT_FN (BUILT_IN_ATAN2):
2108 builtin_optab = atan2_optab; break;
2109 CASE_FLT_FN (BUILT_IN_SCALB):
2110 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2111 return 0;
2112 builtin_optab = scalb_optab; break;
2113 CASE_FLT_FN (BUILT_IN_SCALBN):
2114 CASE_FLT_FN (BUILT_IN_SCALBLN):
2115 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2116 return 0;
2117 /* Fall through... */
2118 CASE_FLT_FN (BUILT_IN_LDEXP):
2119 builtin_optab = ldexp_optab; break;
2120 CASE_FLT_FN (BUILT_IN_FMOD):
2121 builtin_optab = fmod_optab; break;
2122 CASE_FLT_FN (BUILT_IN_REMAINDER):
2123 CASE_FLT_FN (BUILT_IN_DREM):
2124 builtin_optab = remainder_optab; break;
2125 default:
2126 gcc_unreachable ();
2129 /* Make a suitable register to place result in. */
2130 mode = TYPE_MODE (TREE_TYPE (exp));
2132 /* Before working hard, check whether the instruction is available. */
2133 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2134 return NULL_RTX;
2136 target = gen_reg_rtx (mode);
2138 if (! flag_errno_math || ! HONOR_NANS (mode))
2139 errno_set = false;
2141 if (errno_set && optimize_insn_for_size_p ())
2142 return 0;
2144 /* Always stabilize the argument list. */
2145 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2146 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2148 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2149 op1 = expand_normal (arg1);
2151 start_sequence ();
2153 /* Compute into TARGET.
2154 Set TARGET to wherever the result comes back. */
2155 target = expand_binop (mode, builtin_optab, op0, op1,
2156 target, 0, OPTAB_DIRECT);
2158 /* If we were unable to expand via the builtin, stop the sequence
2159 (without outputting the insns) and call to the library function
2160 with the stabilized argument list. */
2161 if (target == 0)
2163 end_sequence ();
2164 return expand_call (exp, target, target == const0_rtx);
2167 if (errno_set)
2168 expand_errno_check (exp, target);
2170 /* Output the entire sequence. */
2171 insns = get_insns ();
2172 end_sequence ();
2173 emit_insn (insns);
2175 return target;
2178 /* Expand a call to the builtin trinary math functions (fma).
2179 Return NULL_RTX if a normal call should be emitted rather than expanding the
2180 function in-line. EXP is the expression that is a call to the builtin
2181 function; if convenient, the result should be placed in TARGET.
2182 SUBTARGET may be used as the target for computing one of EXP's
2183 operands. */
2185 static rtx
2186 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2188 optab builtin_optab;
2189 rtx op0, op1, op2, insns;
2190 tree fndecl = get_callee_fndecl (exp);
2191 tree arg0, arg1, arg2;
2192 enum machine_mode mode;
2194 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2195 return NULL_RTX;
2197 arg0 = CALL_EXPR_ARG (exp, 0);
2198 arg1 = CALL_EXPR_ARG (exp, 1);
2199 arg2 = CALL_EXPR_ARG (exp, 2);
2201 switch (DECL_FUNCTION_CODE (fndecl))
2203 CASE_FLT_FN (BUILT_IN_FMA):
2204 builtin_optab = fma_optab; break;
2205 default:
2206 gcc_unreachable ();
2209 /* Make a suitable register to place result in. */
2210 mode = TYPE_MODE (TREE_TYPE (exp));
2212 /* Before working hard, check whether the instruction is available. */
2213 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2214 return NULL_RTX;
2216 target = gen_reg_rtx (mode);
2218 /* Always stabilize the argument list. */
2219 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2220 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2221 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2223 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2224 op1 = expand_normal (arg1);
2225 op2 = expand_normal (arg2);
2227 start_sequence ();
2229 /* Compute into TARGET.
2230 Set TARGET to wherever the result comes back. */
2231 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2232 target, 0);
2234 /* If we were unable to expand via the builtin, stop the sequence
2235 (without outputting the insns) and call to the library function
2236 with the stabilized argument list. */
2237 if (target == 0)
2239 end_sequence ();
2240 return expand_call (exp, target, target == const0_rtx);
2243 /* Output the entire sequence. */
2244 insns = get_insns ();
2245 end_sequence ();
2246 emit_insn (insns);
2248 return target;
2251 /* Expand a call to the builtin sin and cos math functions.
2252 Return NULL_RTX if a normal call should be emitted rather than expanding the
2253 function in-line. EXP is the expression that is a call to the builtin
2254 function; if convenient, the result should be placed in TARGET.
2255 SUBTARGET may be used as the target for computing one of EXP's
2256 operands. */
2258 static rtx
2259 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2261 optab builtin_optab;
2262 rtx op0, insns;
2263 tree fndecl = get_callee_fndecl (exp);
2264 enum machine_mode mode;
2265 tree arg;
2267 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2268 return NULL_RTX;
2270 arg = CALL_EXPR_ARG (exp, 0);
2272 switch (DECL_FUNCTION_CODE (fndecl))
2274 CASE_FLT_FN (BUILT_IN_SIN):
2275 CASE_FLT_FN (BUILT_IN_COS):
2276 builtin_optab = sincos_optab; break;
2277 default:
2278 gcc_unreachable ();
2281 /* Make a suitable register to place result in. */
2282 mode = TYPE_MODE (TREE_TYPE (exp));
2284 /* Check if sincos insn is available, otherwise fallback
2285 to sin or cos insn. */
2286 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2287 switch (DECL_FUNCTION_CODE (fndecl))
2289 CASE_FLT_FN (BUILT_IN_SIN):
2290 builtin_optab = sin_optab; break;
2291 CASE_FLT_FN (BUILT_IN_COS):
2292 builtin_optab = cos_optab; break;
2293 default:
2294 gcc_unreachable ();
2297 /* Before working hard, check whether the instruction is available. */
2298 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2300 target = gen_reg_rtx (mode);
2302 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2303 need to expand the argument again. This way, we will not perform
2304 side-effects more the once. */
2305 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2307 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2309 start_sequence ();
2311 /* Compute into TARGET.
2312 Set TARGET to wherever the result comes back. */
2313 if (builtin_optab == sincos_optab)
2315 int result;
2317 switch (DECL_FUNCTION_CODE (fndecl))
2319 CASE_FLT_FN (BUILT_IN_SIN):
2320 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2321 break;
2322 CASE_FLT_FN (BUILT_IN_COS):
2323 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2324 break;
2325 default:
2326 gcc_unreachable ();
2328 gcc_assert (result);
2330 else
2332 target = expand_unop (mode, builtin_optab, op0, target, 0);
2335 if (target != 0)
2337 /* Output the entire sequence. */
2338 insns = get_insns ();
2339 end_sequence ();
2340 emit_insn (insns);
2341 return target;
2344 /* If we were unable to expand via the builtin, stop the sequence
2345 (without outputting the insns) and call to the library function
2346 with the stabilized argument list. */
2347 end_sequence ();
2350 target = expand_call (exp, target, target == const0_rtx);
2352 return target;
2355 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2356 return an RTL instruction code that implements the functionality.
2357 If that isn't possible or available return CODE_FOR_nothing. */
2359 static enum insn_code
2360 interclass_mathfn_icode (tree arg, tree fndecl)
2362 bool errno_set = false;
2363 optab builtin_optab = unknown_optab;
2364 enum machine_mode mode;
2366 switch (DECL_FUNCTION_CODE (fndecl))
2368 CASE_FLT_FN (BUILT_IN_ILOGB):
2369 errno_set = true; builtin_optab = ilogb_optab; break;
2370 CASE_FLT_FN (BUILT_IN_ISINF):
2371 builtin_optab = isinf_optab; break;
2372 case BUILT_IN_ISNORMAL:
2373 case BUILT_IN_ISFINITE:
2374 CASE_FLT_FN (BUILT_IN_FINITE):
2375 case BUILT_IN_FINITED32:
2376 case BUILT_IN_FINITED64:
2377 case BUILT_IN_FINITED128:
2378 case BUILT_IN_ISINFD32:
2379 case BUILT_IN_ISINFD64:
2380 case BUILT_IN_ISINFD128:
2381 /* These builtins have no optabs (yet). */
2382 break;
2383 default:
2384 gcc_unreachable ();
2387 /* There's no easy way to detect the case we need to set EDOM. */
2388 if (flag_errno_math && errno_set)
2389 return CODE_FOR_nothing;
2391 /* Optab mode depends on the mode of the input argument. */
2392 mode = TYPE_MODE (TREE_TYPE (arg));
2394 if (builtin_optab)
2395 return optab_handler (builtin_optab, mode);
2396 return CODE_FOR_nothing;
2399 /* Expand a call to one of the builtin math functions that operate on
2400 floating point argument and output an integer result (ilogb, isinf,
2401 isnan, etc).
2402 Return 0 if a normal call should be emitted rather than expanding the
2403 function in-line. EXP is the expression that is a call to the builtin
2404 function; if convenient, the result should be placed in TARGET. */
2406 static rtx
2407 expand_builtin_interclass_mathfn (tree exp, rtx target)
2409 enum insn_code icode = CODE_FOR_nothing;
2410 rtx op0;
2411 tree fndecl = get_callee_fndecl (exp);
2412 enum machine_mode mode;
2413 tree arg;
2415 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2416 return NULL_RTX;
2418 arg = CALL_EXPR_ARG (exp, 0);
2419 icode = interclass_mathfn_icode (arg, fndecl);
2420 mode = TYPE_MODE (TREE_TYPE (arg));
2422 if (icode != CODE_FOR_nothing)
2424 struct expand_operand ops[1];
2425 rtx last = get_last_insn ();
2426 tree orig_arg = arg;
2428 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2429 need to expand the argument again. This way, we will not perform
2430 side-effects more the once. */
2431 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2433 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2435 if (mode != GET_MODE (op0))
2436 op0 = convert_to_mode (mode, op0, 0);
2438 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2439 if (maybe_legitimize_operands (icode, 0, 1, ops)
2440 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2441 return ops[0].value;
2443 delete_insns_since (last);
2444 CALL_EXPR_ARG (exp, 0) = orig_arg;
2447 return NULL_RTX;
2450 /* Expand a call to the builtin sincos math function.
2451 Return NULL_RTX if a normal call should be emitted rather than expanding the
2452 function in-line. EXP is the expression that is a call to the builtin
2453 function. */
2455 static rtx
2456 expand_builtin_sincos (tree exp)
2458 rtx op0, op1, op2, target1, target2;
2459 enum machine_mode mode;
2460 tree arg, sinp, cosp;
2461 int result;
2462 location_t loc = EXPR_LOCATION (exp);
2463 tree alias_type, alias_off;
2465 if (!validate_arglist (exp, REAL_TYPE,
2466 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2467 return NULL_RTX;
2469 arg = CALL_EXPR_ARG (exp, 0);
2470 sinp = CALL_EXPR_ARG (exp, 1);
2471 cosp = CALL_EXPR_ARG (exp, 2);
2473 /* Make a suitable register to place result in. */
2474 mode = TYPE_MODE (TREE_TYPE (arg));
2476 /* Check if sincos insn is available, otherwise emit the call. */
2477 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2478 return NULL_RTX;
2480 target1 = gen_reg_rtx (mode);
2481 target2 = gen_reg_rtx (mode);
2483 op0 = expand_normal (arg);
2484 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2485 alias_off = build_int_cst (alias_type, 0);
2486 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2487 sinp, alias_off));
2488 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2489 cosp, alias_off));
2491 /* Compute into target1 and target2.
2492 Set TARGET to wherever the result comes back. */
2493 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2494 gcc_assert (result);
2496 /* Move target1 and target2 to the memory locations indicated
2497 by op1 and op2. */
2498 emit_move_insn (op1, target1);
2499 emit_move_insn (op2, target2);
2501 return const0_rtx;
2504 /* Expand a call to the internal cexpi builtin to the sincos math function.
2505 EXP is the expression that is a call to the builtin function; if convenient,
2506 the result should be placed in TARGET. */
2508 static rtx
2509 expand_builtin_cexpi (tree exp, rtx target)
2511 tree fndecl = get_callee_fndecl (exp);
2512 tree arg, type;
2513 enum machine_mode mode;
2514 rtx op0, op1, op2;
2515 location_t loc = EXPR_LOCATION (exp);
2517 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2518 return NULL_RTX;
2520 arg = CALL_EXPR_ARG (exp, 0);
2521 type = TREE_TYPE (arg);
2522 mode = TYPE_MODE (TREE_TYPE (arg));
2524 /* Try expanding via a sincos optab, fall back to emitting a libcall
2525 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2526 is only generated from sincos, cexp or if we have either of them. */
2527 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2529 op1 = gen_reg_rtx (mode);
2530 op2 = gen_reg_rtx (mode);
2532 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2534 /* Compute into op1 and op2. */
2535 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2537 else if (TARGET_HAS_SINCOS)
2539 tree call, fn = NULL_TREE;
2540 tree top1, top2;
2541 rtx op1a, op2a;
2543 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2544 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2545 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2546 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2547 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2548 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2549 else
2550 gcc_unreachable ();
2552 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2553 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2554 op1a = copy_addr_to_reg (XEXP (op1, 0));
2555 op2a = copy_addr_to_reg (XEXP (op2, 0));
2556 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2557 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2559 /* Make sure not to fold the sincos call again. */
2560 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2561 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2562 call, 3, arg, top1, top2));
2564 else
2566 tree call, fn = NULL_TREE, narg;
2567 tree ctype = build_complex_type (type);
2569 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2570 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2571 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2572 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2573 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2574 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2575 else
2576 gcc_unreachable ();
2578 /* If we don't have a decl for cexp create one. This is the
2579 friendliest fallback if the user calls __builtin_cexpi
2580 without full target C99 function support. */
2581 if (fn == NULL_TREE)
2583 tree fntype;
2584 const char *name = NULL;
2586 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2587 name = "cexpf";
2588 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2589 name = "cexp";
2590 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2591 name = "cexpl";
2593 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2594 fn = build_fn_decl (name, fntype);
2597 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2598 build_real (type, dconst0), arg);
2600 /* Make sure not to fold the cexp call again. */
2601 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2602 return expand_expr (build_call_nary (ctype, call, 1, narg),
2603 target, VOIDmode, EXPAND_NORMAL);
2606 /* Now build the proper return type. */
2607 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2608 make_tree (TREE_TYPE (arg), op2),
2609 make_tree (TREE_TYPE (arg), op1)),
2610 target, VOIDmode, EXPAND_NORMAL);
2613 /* Conveniently construct a function call expression. FNDECL names the
2614 function to be called, N is the number of arguments, and the "..."
2615 parameters are the argument expressions. Unlike build_call_exr
2616 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2618 static tree
2619 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2621 va_list ap;
2622 tree fntype = TREE_TYPE (fndecl);
2623 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2625 va_start (ap, n);
2626 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2627 va_end (ap);
2628 SET_EXPR_LOCATION (fn, loc);
2629 return fn;
2632 /* Expand a call to one of the builtin rounding functions gcc defines
2633 as an extension (lfloor and lceil). As these are gcc extensions we
2634 do not need to worry about setting errno to EDOM.
2635 If expanding via optab fails, lower expression to (int)(floor(x)).
2636 EXP is the expression that is a call to the builtin function;
2637 if convenient, the result should be placed in TARGET. */
2639 static rtx
2640 expand_builtin_int_roundingfn (tree exp, rtx target)
2642 convert_optab builtin_optab;
2643 rtx op0, insns, tmp;
2644 tree fndecl = get_callee_fndecl (exp);
2645 enum built_in_function fallback_fn;
2646 tree fallback_fndecl;
2647 enum machine_mode mode;
2648 tree arg;
2650 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2651 gcc_unreachable ();
2653 arg = CALL_EXPR_ARG (exp, 0);
2655 switch (DECL_FUNCTION_CODE (fndecl))
2657 CASE_FLT_FN (BUILT_IN_ICEIL):
2658 CASE_FLT_FN (BUILT_IN_LCEIL):
2659 CASE_FLT_FN (BUILT_IN_LLCEIL):
2660 builtin_optab = lceil_optab;
2661 fallback_fn = BUILT_IN_CEIL;
2662 break;
2664 CASE_FLT_FN (BUILT_IN_IFLOOR):
2665 CASE_FLT_FN (BUILT_IN_LFLOOR):
2666 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2667 builtin_optab = lfloor_optab;
2668 fallback_fn = BUILT_IN_FLOOR;
2669 break;
2671 default:
2672 gcc_unreachable ();
2675 /* Make a suitable register to place result in. */
2676 mode = TYPE_MODE (TREE_TYPE (exp));
2678 target = gen_reg_rtx (mode);
2680 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2681 need to expand the argument again. This way, we will not perform
2682 side-effects more the once. */
2683 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2685 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2687 start_sequence ();
2689 /* Compute into TARGET. */
2690 if (expand_sfix_optab (target, op0, builtin_optab))
2692 /* Output the entire sequence. */
2693 insns = get_insns ();
2694 end_sequence ();
2695 emit_insn (insns);
2696 return target;
2699 /* If we were unable to expand via the builtin, stop the sequence
2700 (without outputting the insns). */
2701 end_sequence ();
2703 /* Fall back to floating point rounding optab. */
2704 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2706 /* For non-C99 targets we may end up without a fallback fndecl here
2707 if the user called __builtin_lfloor directly. In this case emit
2708 a call to the floor/ceil variants nevertheless. This should result
2709 in the best user experience for not full C99 targets. */
2710 if (fallback_fndecl == NULL_TREE)
2712 tree fntype;
2713 const char *name = NULL;
2715 switch (DECL_FUNCTION_CODE (fndecl))
2717 case BUILT_IN_ICEIL:
2718 case BUILT_IN_LCEIL:
2719 case BUILT_IN_LLCEIL:
2720 name = "ceil";
2721 break;
2722 case BUILT_IN_ICEILF:
2723 case BUILT_IN_LCEILF:
2724 case BUILT_IN_LLCEILF:
2725 name = "ceilf";
2726 break;
2727 case BUILT_IN_ICEILL:
2728 case BUILT_IN_LCEILL:
2729 case BUILT_IN_LLCEILL:
2730 name = "ceill";
2731 break;
2732 case BUILT_IN_IFLOOR:
2733 case BUILT_IN_LFLOOR:
2734 case BUILT_IN_LLFLOOR:
2735 name = "floor";
2736 break;
2737 case BUILT_IN_IFLOORF:
2738 case BUILT_IN_LFLOORF:
2739 case BUILT_IN_LLFLOORF:
2740 name = "floorf";
2741 break;
2742 case BUILT_IN_IFLOORL:
2743 case BUILT_IN_LFLOORL:
2744 case BUILT_IN_LLFLOORL:
2745 name = "floorl";
2746 break;
2747 default:
2748 gcc_unreachable ();
2751 fntype = build_function_type_list (TREE_TYPE (arg),
2752 TREE_TYPE (arg), NULL_TREE);
2753 fallback_fndecl = build_fn_decl (name, fntype);
2756 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2758 tmp = expand_normal (exp);
2760 /* Truncate the result of floating point optab to integer
2761 via expand_fix (). */
2762 target = gen_reg_rtx (mode);
2763 expand_fix (target, tmp, 0);
2765 return target;
2768 /* Expand a call to one of the builtin math functions doing integer
2769 conversion (lrint).
2770 Return 0 if a normal call should be emitted rather than expanding the
2771 function in-line. EXP is the expression that is a call to the builtin
2772 function; if convenient, the result should be placed in TARGET. */
2774 static rtx
2775 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2777 convert_optab builtin_optab;
2778 rtx op0, insns;
2779 tree fndecl = get_callee_fndecl (exp);
2780 tree arg;
2781 enum machine_mode mode;
2782 enum built_in_function fallback_fn = BUILT_IN_NONE;
2784 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2785 gcc_unreachable ();
2787 arg = CALL_EXPR_ARG (exp, 0);
2789 switch (DECL_FUNCTION_CODE (fndecl))
2791 CASE_FLT_FN (BUILT_IN_IRINT):
2792 fallback_fn = BUILT_IN_LRINT;
2793 /* FALLTHRU */
2794 CASE_FLT_FN (BUILT_IN_LRINT):
2795 CASE_FLT_FN (BUILT_IN_LLRINT):
2796 builtin_optab = lrint_optab;
2797 break;
2799 CASE_FLT_FN (BUILT_IN_IROUND):
2800 fallback_fn = BUILT_IN_LROUND;
2801 /* FALLTHRU */
2802 CASE_FLT_FN (BUILT_IN_LROUND):
2803 CASE_FLT_FN (BUILT_IN_LLROUND):
2804 builtin_optab = lround_optab;
2805 break;
2807 default:
2808 gcc_unreachable ();
2811 /* There's no easy way to detect the case we need to set EDOM. */
2812 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2813 return NULL_RTX;
2815 /* Make a suitable register to place result in. */
2816 mode = TYPE_MODE (TREE_TYPE (exp));
2818 /* There's no easy way to detect the case we need to set EDOM. */
2819 if (!flag_errno_math)
2821 target = gen_reg_rtx (mode);
2823 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2824 need to expand the argument again. This way, we will not perform
2825 side-effects more the once. */
2826 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2828 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2830 start_sequence ();
2832 if (expand_sfix_optab (target, op0, builtin_optab))
2834 /* Output the entire sequence. */
2835 insns = get_insns ();
2836 end_sequence ();
2837 emit_insn (insns);
2838 return target;
2841 /* If we were unable to expand via the builtin, stop the sequence
2842 (without outputting the insns) and call to the library function
2843 with the stabilized argument list. */
2844 end_sequence ();
2847 if (fallback_fn != BUILT_IN_NONE)
2849 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2850 targets, (int) round (x) should never be transformed into
2851 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2852 a call to lround in the hope that the target provides at least some
2853 C99 functions. This should result in the best user experience for
2854 not full C99 targets. */
2855 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2856 fallback_fn, 0);
2858 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2859 fallback_fndecl, 1, arg);
2861 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2862 return convert_to_mode (mode, target, 0);
2865 target = expand_call (exp, target, target == const0_rtx);
2867 return target;
2870 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2871 a normal call should be emitted rather than expanding the function
2872 in-line. EXP is the expression that is a call to the builtin
2873 function; if convenient, the result should be placed in TARGET. */
2875 static rtx
2876 expand_builtin_powi (tree exp, rtx target)
2878 tree arg0, arg1;
2879 rtx op0, op1;
2880 enum machine_mode mode;
2881 enum machine_mode mode2;
2883 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2884 return NULL_RTX;
2886 arg0 = CALL_EXPR_ARG (exp, 0);
2887 arg1 = CALL_EXPR_ARG (exp, 1);
2888 mode = TYPE_MODE (TREE_TYPE (exp));
2890 /* Emit a libcall to libgcc. */
2892 /* Mode of the 2nd argument must match that of an int. */
2893 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2895 if (target == NULL_RTX)
2896 target = gen_reg_rtx (mode);
2898 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2899 if (GET_MODE (op0) != mode)
2900 op0 = convert_to_mode (mode, op0, 0);
2901 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2902 if (GET_MODE (op1) != mode2)
2903 op1 = convert_to_mode (mode2, op1, 0);
2905 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2906 target, LCT_CONST, mode, 2,
2907 op0, mode, op1, mode2);
2909 return target;
2912 /* Expand expression EXP which is a call to the strlen builtin. Return
2913 NULL_RTX if we failed the caller should emit a normal call, otherwise
2914 try to get the result in TARGET, if convenient. */
2916 static rtx
2917 expand_builtin_strlen (tree exp, rtx target,
2918 enum machine_mode target_mode)
2920 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2921 return NULL_RTX;
2922 else
2924 struct expand_operand ops[4];
2925 rtx pat;
2926 tree len;
2927 tree src = CALL_EXPR_ARG (exp, 0);
2928 rtx src_reg, before_strlen;
2929 enum machine_mode insn_mode = target_mode;
2930 enum insn_code icode = CODE_FOR_nothing;
2931 unsigned int align;
2933 /* If the length can be computed at compile-time, return it. */
2934 len = c_strlen (src, 0);
2935 if (len)
2936 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2938 /* If the length can be computed at compile-time and is constant
2939 integer, but there are side-effects in src, evaluate
2940 src for side-effects, then return len.
2941 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2942 can be optimized into: i++; x = 3; */
2943 len = c_strlen (src, 1);
2944 if (len && TREE_CODE (len) == INTEGER_CST)
2946 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2947 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2950 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2952 /* If SRC is not a pointer type, don't do this operation inline. */
2953 if (align == 0)
2954 return NULL_RTX;
2956 /* Bail out if we can't compute strlen in the right mode. */
2957 while (insn_mode != VOIDmode)
2959 icode = optab_handler (strlen_optab, insn_mode);
2960 if (icode != CODE_FOR_nothing)
2961 break;
2963 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2965 if (insn_mode == VOIDmode)
2966 return NULL_RTX;
2968 /* Make a place to hold the source address. We will not expand
2969 the actual source until we are sure that the expansion will
2970 not fail -- there are trees that cannot be expanded twice. */
2971 src_reg = gen_reg_rtx (Pmode);
2973 /* Mark the beginning of the strlen sequence so we can emit the
2974 source operand later. */
2975 before_strlen = get_last_insn ();
2977 create_output_operand (&ops[0], target, insn_mode);
2978 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2979 create_integer_operand (&ops[2], 0);
2980 create_integer_operand (&ops[3], align);
2981 if (!maybe_expand_insn (icode, 4, ops))
2982 return NULL_RTX;
2984 /* Now that we are assured of success, expand the source. */
2985 start_sequence ();
2986 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2987 if (pat != src_reg)
2989 #ifdef POINTERS_EXTEND_UNSIGNED
2990 if (GET_MODE (pat) != Pmode)
2991 pat = convert_to_mode (Pmode, pat,
2992 POINTERS_EXTEND_UNSIGNED);
2993 #endif
2994 emit_move_insn (src_reg, pat);
2996 pat = get_insns ();
2997 end_sequence ();
2999 if (before_strlen)
3000 emit_insn_after (pat, before_strlen);
3001 else
3002 emit_insn_before (pat, get_insns ());
3004 /* Return the value in the proper mode for this function. */
3005 if (GET_MODE (ops[0].value) == target_mode)
3006 target = ops[0].value;
3007 else if (target != 0)
3008 convert_move (target, ops[0].value, 0);
3009 else
3010 target = convert_to_mode (target_mode, ops[0].value, 0);
3012 return target;
3016 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3017 bytes from constant string DATA + OFFSET and return it as target
3018 constant. */
3020 static rtx
3021 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3022 enum machine_mode mode)
3024 const char *str = (const char *) data;
3026 gcc_assert (offset >= 0
3027 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3028 <= strlen (str) + 1));
3030 return c_readstr (str + offset, mode);
3033 /* Expand a call EXP to the memcpy builtin.
3034 Return NULL_RTX if we failed, the caller should emit a normal call,
3035 otherwise try to get the result in TARGET, if convenient (and in
3036 mode MODE if that's convenient). */
3038 static rtx
3039 expand_builtin_memcpy (tree exp, rtx target)
3041 if (!validate_arglist (exp,
3042 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3043 return NULL_RTX;
3044 else
3046 tree dest = CALL_EXPR_ARG (exp, 0);
3047 tree src = CALL_EXPR_ARG (exp, 1);
3048 tree len = CALL_EXPR_ARG (exp, 2);
3049 const char *src_str;
3050 unsigned int src_align = get_pointer_alignment (src);
3051 unsigned int dest_align = get_pointer_alignment (dest);
3052 rtx dest_mem, src_mem, dest_addr, len_rtx;
3053 HOST_WIDE_INT expected_size = -1;
3054 unsigned int expected_align = 0;
3056 /* If DEST is not a pointer type, call the normal function. */
3057 if (dest_align == 0)
3058 return NULL_RTX;
3060 /* If either SRC is not a pointer type, don't do this
3061 operation in-line. */
3062 if (src_align == 0)
3063 return NULL_RTX;
3065 if (currently_expanding_gimple_stmt)
3066 stringop_block_profile (currently_expanding_gimple_stmt,
3067 &expected_align, &expected_size);
3069 if (expected_align < dest_align)
3070 expected_align = dest_align;
3071 dest_mem = get_memory_rtx (dest, len);
3072 set_mem_align (dest_mem, dest_align);
3073 len_rtx = expand_normal (len);
3074 src_str = c_getstr (src);
3076 /* If SRC is a string constant and block move would be done
3077 by pieces, we can avoid loading the string from memory
3078 and only stored the computed constants. */
3079 if (src_str
3080 && CONST_INT_P (len_rtx)
3081 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3082 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3083 CONST_CAST (char *, src_str),
3084 dest_align, false))
3086 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3087 builtin_memcpy_read_str,
3088 CONST_CAST (char *, src_str),
3089 dest_align, false, 0);
3090 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3091 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3092 return dest_mem;
3095 src_mem = get_memory_rtx (src, len);
3096 set_mem_align (src_mem, src_align);
3098 /* Copy word part most expediently. */
3099 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3100 CALL_EXPR_TAILCALL (exp)
3101 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3102 expected_align, expected_size);
3104 if (dest_addr == 0)
3106 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3107 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3109 return dest_addr;
3113 /* Expand a call EXP to the mempcpy builtin.
3114 Return NULL_RTX if we failed; the caller should emit a normal call,
3115 otherwise try to get the result in TARGET, if convenient (and in
3116 mode MODE if that's convenient). If ENDP is 0 return the
3117 destination pointer, if ENDP is 1 return the end pointer ala
3118 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3119 stpcpy. */
3121 static rtx
3122 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3124 if (!validate_arglist (exp,
3125 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3126 return NULL_RTX;
3127 else
3129 tree dest = CALL_EXPR_ARG (exp, 0);
3130 tree src = CALL_EXPR_ARG (exp, 1);
3131 tree len = CALL_EXPR_ARG (exp, 2);
3132 return expand_builtin_mempcpy_args (dest, src, len,
3133 target, mode, /*endp=*/ 1);
3137 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3138 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3139 so that this can also be called without constructing an actual CALL_EXPR.
3140 The other arguments and return value are the same as for
3141 expand_builtin_mempcpy. */
3143 static rtx
3144 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3145 rtx target, enum machine_mode mode, int endp)
3147 /* If return value is ignored, transform mempcpy into memcpy. */
3148 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3150 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3151 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3152 dest, src, len);
3153 return expand_expr (result, target, mode, EXPAND_NORMAL);
3155 else
3157 const char *src_str;
3158 unsigned int src_align = get_pointer_alignment (src);
3159 unsigned int dest_align = get_pointer_alignment (dest);
3160 rtx dest_mem, src_mem, len_rtx;
3162 /* If either SRC or DEST is not a pointer type, don't do this
3163 operation in-line. */
3164 if (dest_align == 0 || src_align == 0)
3165 return NULL_RTX;
3167 /* If LEN is not constant, call the normal function. */
3168 if (! host_integerp (len, 1))
3169 return NULL_RTX;
3171 len_rtx = expand_normal (len);
3172 src_str = c_getstr (src);
3174 /* If SRC is a string constant and block move would be done
3175 by pieces, we can avoid loading the string from memory
3176 and only stored the computed constants. */
3177 if (src_str
3178 && CONST_INT_P (len_rtx)
3179 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3180 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3181 CONST_CAST (char *, src_str),
3182 dest_align, false))
3184 dest_mem = get_memory_rtx (dest, len);
3185 set_mem_align (dest_mem, dest_align);
3186 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3187 builtin_memcpy_read_str,
3188 CONST_CAST (char *, src_str),
3189 dest_align, false, endp);
3190 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3191 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3192 return dest_mem;
3195 if (CONST_INT_P (len_rtx)
3196 && can_move_by_pieces (INTVAL (len_rtx),
3197 MIN (dest_align, src_align)))
3199 dest_mem = get_memory_rtx (dest, len);
3200 set_mem_align (dest_mem, dest_align);
3201 src_mem = get_memory_rtx (src, len);
3202 set_mem_align (src_mem, src_align);
3203 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3204 MIN (dest_align, src_align), endp);
3205 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3206 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3207 return dest_mem;
3210 return NULL_RTX;
3214 #ifndef HAVE_movstr
3215 # define HAVE_movstr 0
3216 # define CODE_FOR_movstr CODE_FOR_nothing
3217 #endif
3219 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3220 we failed, the caller should emit a normal call, otherwise try to
3221 get the result in TARGET, if convenient. If ENDP is 0 return the
3222 destination pointer, if ENDP is 1 return the end pointer ala
3223 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3224 stpcpy. */
3226 static rtx
3227 expand_movstr (tree dest, tree src, rtx target, int endp)
3229 struct expand_operand ops[3];
3230 rtx dest_mem;
3231 rtx src_mem;
3233 if (!HAVE_movstr)
3234 return NULL_RTX;
3236 dest_mem = get_memory_rtx (dest, NULL);
3237 src_mem = get_memory_rtx (src, NULL);
3238 if (!endp)
3240 target = force_reg (Pmode, XEXP (dest_mem, 0));
3241 dest_mem = replace_equiv_address (dest_mem, target);
3244 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3245 create_fixed_operand (&ops[1], dest_mem);
3246 create_fixed_operand (&ops[2], src_mem);
3247 expand_insn (CODE_FOR_movstr, 3, ops);
3249 if (endp && target != const0_rtx)
3251 target = ops[0].value;
3252 /* movstr is supposed to set end to the address of the NUL
3253 terminator. If the caller requested a mempcpy-like return value,
3254 adjust it. */
3255 if (endp == 1)
3257 rtx tem = plus_constant (GET_MODE (target),
3258 gen_lowpart (GET_MODE (target), target), 1);
3259 emit_move_insn (target, force_operand (tem, NULL_RTX));
3262 return target;
3265 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3266 NULL_RTX if we failed the caller should emit a normal call, otherwise
3267 try to get the result in TARGET, if convenient (and in mode MODE if that's
3268 convenient). */
3270 static rtx
3271 expand_builtin_strcpy (tree exp, rtx target)
3273 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3275 tree dest = CALL_EXPR_ARG (exp, 0);
3276 tree src = CALL_EXPR_ARG (exp, 1);
3277 return expand_builtin_strcpy_args (dest, src, target);
3279 return NULL_RTX;
3282 /* Helper function to do the actual work for expand_builtin_strcpy. The
3283 arguments to the builtin_strcpy call DEST and SRC are broken out
3284 so that this can also be called without constructing an actual CALL_EXPR.
3285 The other arguments and return value are the same as for
3286 expand_builtin_strcpy. */
3288 static rtx
3289 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3291 return expand_movstr (dest, src, target, /*endp=*/0);
3294 /* Expand a call EXP to the stpcpy builtin.
3295 Return NULL_RTX if we failed the caller should emit a normal call,
3296 otherwise try to get the result in TARGET, if convenient (and in
3297 mode MODE if that's convenient). */
3299 static rtx
3300 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3302 tree dst, src;
3303 location_t loc = EXPR_LOCATION (exp);
3305 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3306 return NULL_RTX;
3308 dst = CALL_EXPR_ARG (exp, 0);
3309 src = CALL_EXPR_ARG (exp, 1);
3311 /* If return value is ignored, transform stpcpy into strcpy. */
3312 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3314 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3315 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3316 return expand_expr (result, target, mode, EXPAND_NORMAL);
3318 else
3320 tree len, lenp1;
3321 rtx ret;
3323 /* Ensure we get an actual string whose length can be evaluated at
3324 compile-time, not an expression containing a string. This is
3325 because the latter will potentially produce pessimized code
3326 when used to produce the return value. */
3327 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3328 return expand_movstr (dst, src, target, /*endp=*/2);
3330 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3331 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3332 target, mode, /*endp=*/2);
3334 if (ret)
3335 return ret;
3337 if (TREE_CODE (len) == INTEGER_CST)
3339 rtx len_rtx = expand_normal (len);
3341 if (CONST_INT_P (len_rtx))
3343 ret = expand_builtin_strcpy_args (dst, src, target);
3345 if (ret)
3347 if (! target)
3349 if (mode != VOIDmode)
3350 target = gen_reg_rtx (mode);
3351 else
3352 target = gen_reg_rtx (GET_MODE (ret));
3354 if (GET_MODE (target) != GET_MODE (ret))
3355 ret = gen_lowpart (GET_MODE (target), ret);
3357 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3358 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3359 gcc_assert (ret);
3361 return target;
3366 return expand_movstr (dst, src, target, /*endp=*/2);
3370 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3371 bytes from constant string DATA + OFFSET and return it as target
3372 constant. */
3375 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3376 enum machine_mode mode)
3378 const char *str = (const char *) data;
3380 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3381 return const0_rtx;
3383 return c_readstr (str + offset, mode);
3386 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3387 NULL_RTX if we failed the caller should emit a normal call. */
3389 static rtx
3390 expand_builtin_strncpy (tree exp, rtx target)
3392 location_t loc = EXPR_LOCATION (exp);
3394 if (validate_arglist (exp,
3395 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3397 tree dest = CALL_EXPR_ARG (exp, 0);
3398 tree src = CALL_EXPR_ARG (exp, 1);
3399 tree len = CALL_EXPR_ARG (exp, 2);
3400 tree slen = c_strlen (src, 1);
3402 /* We must be passed a constant len and src parameter. */
3403 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3404 return NULL_RTX;
3406 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3408 /* We're required to pad with trailing zeros if the requested
3409 len is greater than strlen(s2)+1. In that case try to
3410 use store_by_pieces, if it fails, punt. */
3411 if (tree_int_cst_lt (slen, len))
3413 unsigned int dest_align = get_pointer_alignment (dest);
3414 const char *p = c_getstr (src);
3415 rtx dest_mem;
3417 if (!p || dest_align == 0 || !host_integerp (len, 1)
3418 || !can_store_by_pieces (tree_low_cst (len, 1),
3419 builtin_strncpy_read_str,
3420 CONST_CAST (char *, p),
3421 dest_align, false))
3422 return NULL_RTX;
3424 dest_mem = get_memory_rtx (dest, len);
3425 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3426 builtin_strncpy_read_str,
3427 CONST_CAST (char *, p), dest_align, false, 0);
3428 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3429 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3430 return dest_mem;
3433 return NULL_RTX;
3436 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3437 bytes from constant string DATA + OFFSET and return it as target
3438 constant. */
3441 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3442 enum machine_mode mode)
3444 const char *c = (const char *) data;
3445 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3447 memset (p, *c, GET_MODE_SIZE (mode));
3449 return c_readstr (p, mode);
3452 /* Callback routine for store_by_pieces. Return the RTL of a register
3453 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3454 char value given in the RTL register data. For example, if mode is
3455 4 bytes wide, return the RTL for 0x01010101*data. */
3457 static rtx
3458 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3459 enum machine_mode mode)
3461 rtx target, coeff;
3462 size_t size;
3463 char *p;
3465 size = GET_MODE_SIZE (mode);
3466 if (size == 1)
3467 return (rtx) data;
3469 p = XALLOCAVEC (char, size);
3470 memset (p, 1, size);
3471 coeff = c_readstr (p, mode);
3473 target = convert_to_mode (mode, (rtx) data, 1);
3474 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3475 return force_reg (mode, target);
3478 /* Expand expression EXP, which is a call to the memset builtin. Return
3479 NULL_RTX if we failed the caller should emit a normal call, otherwise
3480 try to get the result in TARGET, if convenient (and in mode MODE if that's
3481 convenient). */
3483 static rtx
3484 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3486 if (!validate_arglist (exp,
3487 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3488 return NULL_RTX;
3489 else
3491 tree dest = CALL_EXPR_ARG (exp, 0);
3492 tree val = CALL_EXPR_ARG (exp, 1);
3493 tree len = CALL_EXPR_ARG (exp, 2);
3494 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3498 /* Helper function to do the actual work for expand_builtin_memset. The
3499 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3500 so that this can also be called without constructing an actual CALL_EXPR.
3501 The other arguments and return value are the same as for
3502 expand_builtin_memset. */
3504 static rtx
3505 expand_builtin_memset_args (tree dest, tree val, tree len,
3506 rtx target, enum machine_mode mode, tree orig_exp)
3508 tree fndecl, fn;
3509 enum built_in_function fcode;
3510 enum machine_mode val_mode;
3511 char c;
3512 unsigned int dest_align;
3513 rtx dest_mem, dest_addr, len_rtx;
3514 HOST_WIDE_INT expected_size = -1;
3515 unsigned int expected_align = 0;
3517 dest_align = get_pointer_alignment (dest);
3519 /* If DEST is not a pointer type, don't do this operation in-line. */
3520 if (dest_align == 0)
3521 return NULL_RTX;
3523 if (currently_expanding_gimple_stmt)
3524 stringop_block_profile (currently_expanding_gimple_stmt,
3525 &expected_align, &expected_size);
3527 if (expected_align < dest_align)
3528 expected_align = dest_align;
3530 /* If the LEN parameter is zero, return DEST. */
3531 if (integer_zerop (len))
3533 /* Evaluate and ignore VAL in case it has side-effects. */
3534 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3535 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3538 /* Stabilize the arguments in case we fail. */
3539 dest = builtin_save_expr (dest);
3540 val = builtin_save_expr (val);
3541 len = builtin_save_expr (len);
3543 len_rtx = expand_normal (len);
3544 dest_mem = get_memory_rtx (dest, len);
3545 val_mode = TYPE_MODE (unsigned_char_type_node);
3547 if (TREE_CODE (val) != INTEGER_CST)
3549 rtx val_rtx;
3551 val_rtx = expand_normal (val);
3552 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3554 /* Assume that we can memset by pieces if we can store
3555 * the coefficients by pieces (in the required modes).
3556 * We can't pass builtin_memset_gen_str as that emits RTL. */
3557 c = 1;
3558 if (host_integerp (len, 1)
3559 && can_store_by_pieces (tree_low_cst (len, 1),
3560 builtin_memset_read_str, &c, dest_align,
3561 true))
3563 val_rtx = force_reg (val_mode, val_rtx);
3564 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3565 builtin_memset_gen_str, val_rtx, dest_align,
3566 true, 0);
3568 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3569 dest_align, expected_align,
3570 expected_size))
3571 goto do_libcall;
3573 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3574 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3575 return dest_mem;
3578 if (target_char_cast (val, &c))
3579 goto do_libcall;
3581 if (c)
3583 if (host_integerp (len, 1)
3584 && can_store_by_pieces (tree_low_cst (len, 1),
3585 builtin_memset_read_str, &c, dest_align,
3586 true))
3587 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3588 builtin_memset_read_str, &c, dest_align, true, 0);
3589 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3590 gen_int_mode (c, val_mode),
3591 dest_align, expected_align,
3592 expected_size))
3593 goto do_libcall;
3595 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3596 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3597 return dest_mem;
3600 set_mem_align (dest_mem, dest_align);
3601 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3602 CALL_EXPR_TAILCALL (orig_exp)
3603 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3604 expected_align, expected_size);
3606 if (dest_addr == 0)
3608 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3609 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3612 return dest_addr;
3614 do_libcall:
3615 fndecl = get_callee_fndecl (orig_exp);
3616 fcode = DECL_FUNCTION_CODE (fndecl);
3617 if (fcode == BUILT_IN_MEMSET)
3618 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3619 dest, val, len);
3620 else if (fcode == BUILT_IN_BZERO)
3621 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3622 dest, len);
3623 else
3624 gcc_unreachable ();
3625 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3626 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3627 return expand_call (fn, target, target == const0_rtx);
3630 /* Expand expression EXP, which is a call to the bzero builtin. Return
3631 NULL_RTX if we failed the caller should emit a normal call. */
3633 static rtx
3634 expand_builtin_bzero (tree exp)
3636 tree dest, size;
3637 location_t loc = EXPR_LOCATION (exp);
3639 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3640 return NULL_RTX;
3642 dest = CALL_EXPR_ARG (exp, 0);
3643 size = CALL_EXPR_ARG (exp, 1);
3645 /* New argument list transforming bzero(ptr x, int y) to
3646 memset(ptr x, int 0, size_t y). This is done this way
3647 so that if it isn't expanded inline, we fallback to
3648 calling bzero instead of memset. */
3650 return expand_builtin_memset_args (dest, integer_zero_node,
3651 fold_convert_loc (loc,
3652 size_type_node, size),
3653 const0_rtx, VOIDmode, exp);
3656 /* Expand expression EXP, which is a call to the memcmp built-in function.
3657 Return NULL_RTX if we failed and the caller should emit a normal call,
3658 otherwise try to get the result in TARGET, if convenient (and in mode
3659 MODE, if that's convenient). */
3661 static rtx
3662 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3663 ATTRIBUTE_UNUSED enum machine_mode mode)
3665 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3667 if (!validate_arglist (exp,
3668 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3669 return NULL_RTX;
3671 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3672 implementing memcmp because it will stop if it encounters two
3673 zero bytes. */
3674 #if defined HAVE_cmpmemsi
3676 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3677 rtx result;
3678 rtx insn;
3679 tree arg1 = CALL_EXPR_ARG (exp, 0);
3680 tree arg2 = CALL_EXPR_ARG (exp, 1);
3681 tree len = CALL_EXPR_ARG (exp, 2);
3683 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3684 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3685 enum machine_mode insn_mode;
3687 if (HAVE_cmpmemsi)
3688 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3689 else
3690 return NULL_RTX;
3692 /* If we don't have POINTER_TYPE, call the function. */
3693 if (arg1_align == 0 || arg2_align == 0)
3694 return NULL_RTX;
3696 /* Make a place to write the result of the instruction. */
3697 result = target;
3698 if (! (result != 0
3699 && REG_P (result) && GET_MODE (result) == insn_mode
3700 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3701 result = gen_reg_rtx (insn_mode);
3703 arg1_rtx = get_memory_rtx (arg1, len);
3704 arg2_rtx = get_memory_rtx (arg2, len);
3705 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3707 /* Set MEM_SIZE as appropriate. */
3708 if (CONST_INT_P (arg3_rtx))
3710 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3711 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3714 if (HAVE_cmpmemsi)
3715 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3716 GEN_INT (MIN (arg1_align, arg2_align)));
3717 else
3718 gcc_unreachable ();
3720 if (insn)
3721 emit_insn (insn);
3722 else
3723 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3724 TYPE_MODE (integer_type_node), 3,
3725 XEXP (arg1_rtx, 0), Pmode,
3726 XEXP (arg2_rtx, 0), Pmode,
3727 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3728 TYPE_UNSIGNED (sizetype)),
3729 TYPE_MODE (sizetype));
3731 /* Return the value in the proper mode for this function. */
3732 mode = TYPE_MODE (TREE_TYPE (exp));
3733 if (GET_MODE (result) == mode)
3734 return result;
3735 else if (target != 0)
3737 convert_move (target, result, 0);
3738 return target;
3740 else
3741 return convert_to_mode (mode, result, 0);
3743 #endif /* HAVE_cmpmemsi. */
3745 return NULL_RTX;
3748 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3749 if we failed the caller should emit a normal call, otherwise try to get
3750 the result in TARGET, if convenient. */
3752 static rtx
3753 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3755 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3756 return NULL_RTX;
3758 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3759 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3760 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3762 rtx arg1_rtx, arg2_rtx;
3763 rtx result, insn = NULL_RTX;
3764 tree fndecl, fn;
3765 tree arg1 = CALL_EXPR_ARG (exp, 0);
3766 tree arg2 = CALL_EXPR_ARG (exp, 1);
3768 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3769 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3771 /* If we don't have POINTER_TYPE, call the function. */
3772 if (arg1_align == 0 || arg2_align == 0)
3773 return NULL_RTX;
3775 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3776 arg1 = builtin_save_expr (arg1);
3777 arg2 = builtin_save_expr (arg2);
3779 arg1_rtx = get_memory_rtx (arg1, NULL);
3780 arg2_rtx = get_memory_rtx (arg2, NULL);
3782 #ifdef HAVE_cmpstrsi
3783 /* Try to call cmpstrsi. */
3784 if (HAVE_cmpstrsi)
3786 enum machine_mode insn_mode
3787 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3789 /* Make a place to write the result of the instruction. */
3790 result = target;
3791 if (! (result != 0
3792 && REG_P (result) && GET_MODE (result) == insn_mode
3793 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3794 result = gen_reg_rtx (insn_mode);
3796 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3797 GEN_INT (MIN (arg1_align, arg2_align)));
3799 #endif
3800 #ifdef HAVE_cmpstrnsi
3801 /* Try to determine at least one length and call cmpstrnsi. */
3802 if (!insn && HAVE_cmpstrnsi)
3804 tree len;
3805 rtx arg3_rtx;
3807 enum machine_mode insn_mode
3808 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3809 tree len1 = c_strlen (arg1, 1);
3810 tree len2 = c_strlen (arg2, 1);
3812 if (len1)
3813 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3814 if (len2)
3815 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3817 /* If we don't have a constant length for the first, use the length
3818 of the second, if we know it. We don't require a constant for
3819 this case; some cost analysis could be done if both are available
3820 but neither is constant. For now, assume they're equally cheap,
3821 unless one has side effects. If both strings have constant lengths,
3822 use the smaller. */
3824 if (!len1)
3825 len = len2;
3826 else if (!len2)
3827 len = len1;
3828 else if (TREE_SIDE_EFFECTS (len1))
3829 len = len2;
3830 else if (TREE_SIDE_EFFECTS (len2))
3831 len = len1;
3832 else if (TREE_CODE (len1) != INTEGER_CST)
3833 len = len2;
3834 else if (TREE_CODE (len2) != INTEGER_CST)
3835 len = len1;
3836 else if (tree_int_cst_lt (len1, len2))
3837 len = len1;
3838 else
3839 len = len2;
3841 /* If both arguments have side effects, we cannot optimize. */
3842 if (!len || TREE_SIDE_EFFECTS (len))
3843 goto do_libcall;
3845 arg3_rtx = expand_normal (len);
3847 /* Make a place to write the result of the instruction. */
3848 result = target;
3849 if (! (result != 0
3850 && REG_P (result) && GET_MODE (result) == insn_mode
3851 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3852 result = gen_reg_rtx (insn_mode);
3854 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3855 GEN_INT (MIN (arg1_align, arg2_align)));
3857 #endif
3859 if (insn)
3861 enum machine_mode mode;
3862 emit_insn (insn);
3864 /* Return the value in the proper mode for this function. */
3865 mode = TYPE_MODE (TREE_TYPE (exp));
3866 if (GET_MODE (result) == mode)
3867 return result;
3868 if (target == 0)
3869 return convert_to_mode (mode, result, 0);
3870 convert_move (target, result, 0);
3871 return target;
3874 /* Expand the library call ourselves using a stabilized argument
3875 list to avoid re-evaluating the function's arguments twice. */
3876 #ifdef HAVE_cmpstrnsi
3877 do_libcall:
3878 #endif
3879 fndecl = get_callee_fndecl (exp);
3880 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3881 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3882 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3883 return expand_call (fn, target, target == const0_rtx);
3885 #endif
3886 return NULL_RTX;
3889 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3890 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3891 the result in TARGET, if convenient. */
3893 static rtx
3894 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3895 ATTRIBUTE_UNUSED enum machine_mode mode)
3897 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3899 if (!validate_arglist (exp,
3900 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3901 return NULL_RTX;
3903 /* If c_strlen can determine an expression for one of the string
3904 lengths, and it doesn't have side effects, then emit cmpstrnsi
3905 using length MIN(strlen(string)+1, arg3). */
3906 #ifdef HAVE_cmpstrnsi
3907 if (HAVE_cmpstrnsi)
3909 tree len, len1, len2;
3910 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3911 rtx result, insn;
3912 tree fndecl, fn;
3913 tree arg1 = CALL_EXPR_ARG (exp, 0);
3914 tree arg2 = CALL_EXPR_ARG (exp, 1);
3915 tree arg3 = CALL_EXPR_ARG (exp, 2);
3917 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3918 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3919 enum machine_mode insn_mode
3920 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3922 len1 = c_strlen (arg1, 1);
3923 len2 = c_strlen (arg2, 1);
3925 if (len1)
3926 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3927 if (len2)
3928 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3930 /* If we don't have a constant length for the first, use the length
3931 of the second, if we know it. We don't require a constant for
3932 this case; some cost analysis could be done if both are available
3933 but neither is constant. For now, assume they're equally cheap,
3934 unless one has side effects. If both strings have constant lengths,
3935 use the smaller. */
3937 if (!len1)
3938 len = len2;
3939 else if (!len2)
3940 len = len1;
3941 else if (TREE_SIDE_EFFECTS (len1))
3942 len = len2;
3943 else if (TREE_SIDE_EFFECTS (len2))
3944 len = len1;
3945 else if (TREE_CODE (len1) != INTEGER_CST)
3946 len = len2;
3947 else if (TREE_CODE (len2) != INTEGER_CST)
3948 len = len1;
3949 else if (tree_int_cst_lt (len1, len2))
3950 len = len1;
3951 else
3952 len = len2;
3954 /* If both arguments have side effects, we cannot optimize. */
3955 if (!len || TREE_SIDE_EFFECTS (len))
3956 return NULL_RTX;
3958 /* The actual new length parameter is MIN(len,arg3). */
3959 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3960 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3962 /* If we don't have POINTER_TYPE, call the function. */
3963 if (arg1_align == 0 || arg2_align == 0)
3964 return NULL_RTX;
3966 /* Make a place to write the result of the instruction. */
3967 result = target;
3968 if (! (result != 0
3969 && REG_P (result) && GET_MODE (result) == insn_mode
3970 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3971 result = gen_reg_rtx (insn_mode);
3973 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3974 arg1 = builtin_save_expr (arg1);
3975 arg2 = builtin_save_expr (arg2);
3976 len = builtin_save_expr (len);
3978 arg1_rtx = get_memory_rtx (arg1, len);
3979 arg2_rtx = get_memory_rtx (arg2, len);
3980 arg3_rtx = expand_normal (len);
3981 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3982 GEN_INT (MIN (arg1_align, arg2_align)));
3983 if (insn)
3985 emit_insn (insn);
3987 /* Return the value in the proper mode for this function. */
3988 mode = TYPE_MODE (TREE_TYPE (exp));
3989 if (GET_MODE (result) == mode)
3990 return result;
3991 if (target == 0)
3992 return convert_to_mode (mode, result, 0);
3993 convert_move (target, result, 0);
3994 return target;
3997 /* Expand the library call ourselves using a stabilized argument
3998 list to avoid re-evaluating the function's arguments twice. */
3999 fndecl = get_callee_fndecl (exp);
4000 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4001 arg1, arg2, len);
4002 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4003 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4004 return expand_call (fn, target, target == const0_rtx);
4006 #endif
4007 return NULL_RTX;
4010 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4011 if that's convenient. */
4014 expand_builtin_saveregs (void)
4016 rtx val, seq;
4018 /* Don't do __builtin_saveregs more than once in a function.
4019 Save the result of the first call and reuse it. */
4020 if (saveregs_value != 0)
4021 return saveregs_value;
4023 /* When this function is called, it means that registers must be
4024 saved on entry to this function. So we migrate the call to the
4025 first insn of this function. */
4027 start_sequence ();
4029 /* Do whatever the machine needs done in this case. */
4030 val = targetm.calls.expand_builtin_saveregs ();
4032 seq = get_insns ();
4033 end_sequence ();
4035 saveregs_value = val;
4037 /* Put the insns after the NOTE that starts the function. If this
4038 is inside a start_sequence, make the outer-level insn chain current, so
4039 the code is placed at the start of the function. */
4040 push_topmost_sequence ();
4041 emit_insn_after (seq, entry_of_function ());
4042 pop_topmost_sequence ();
4044 return val;
4047 /* Expand a call to __builtin_next_arg. */
4049 static rtx
4050 expand_builtin_next_arg (void)
4052 /* Checking arguments is already done in fold_builtin_next_arg
4053 that must be called before this function. */
4054 return expand_binop (ptr_mode, add_optab,
4055 crtl->args.internal_arg_pointer,
4056 crtl->args.arg_offset_rtx,
4057 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4060 /* Make it easier for the backends by protecting the valist argument
4061 from multiple evaluations. */
4063 static tree
4064 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4066 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4068 /* The current way of determining the type of valist is completely
4069 bogus. We should have the information on the va builtin instead. */
4070 if (!vatype)
4071 vatype = targetm.fn_abi_va_list (cfun->decl);
4073 if (TREE_CODE (vatype) == ARRAY_TYPE)
4075 if (TREE_SIDE_EFFECTS (valist))
4076 valist = save_expr (valist);
4078 /* For this case, the backends will be expecting a pointer to
4079 vatype, but it's possible we've actually been given an array
4080 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4081 So fix it. */
4082 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4084 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4085 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4088 else
4090 tree pt = build_pointer_type (vatype);
4092 if (! needs_lvalue)
4094 if (! TREE_SIDE_EFFECTS (valist))
4095 return valist;
4097 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4098 TREE_SIDE_EFFECTS (valist) = 1;
4101 if (TREE_SIDE_EFFECTS (valist))
4102 valist = save_expr (valist);
4103 valist = fold_build2_loc (loc, MEM_REF,
4104 vatype, valist, build_int_cst (pt, 0));
4107 return valist;
4110 /* The "standard" definition of va_list is void*. */
4112 tree
4113 std_build_builtin_va_list (void)
4115 return ptr_type_node;
4118 /* The "standard" abi va_list is va_list_type_node. */
4120 tree
4121 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4123 return va_list_type_node;
4126 /* The "standard" type of va_list is va_list_type_node. */
4128 tree
4129 std_canonical_va_list_type (tree type)
4131 tree wtype, htype;
4133 if (INDIRECT_REF_P (type))
4134 type = TREE_TYPE (type);
4135 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4136 type = TREE_TYPE (type);
4137 wtype = va_list_type_node;
4138 htype = type;
4139 /* Treat structure va_list types. */
4140 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4141 htype = TREE_TYPE (htype);
4142 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4144 /* If va_list is an array type, the argument may have decayed
4145 to a pointer type, e.g. by being passed to another function.
4146 In that case, unwrap both types so that we can compare the
4147 underlying records. */
4148 if (TREE_CODE (htype) == ARRAY_TYPE
4149 || POINTER_TYPE_P (htype))
4151 wtype = TREE_TYPE (wtype);
4152 htype = TREE_TYPE (htype);
4155 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4156 return va_list_type_node;
4158 return NULL_TREE;
4161 /* The "standard" implementation of va_start: just assign `nextarg' to
4162 the variable. */
4164 void
4165 std_expand_builtin_va_start (tree valist, rtx nextarg)
4167 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4168 convert_move (va_r, nextarg, 0);
4171 /* Expand EXP, a call to __builtin_va_start. */
4173 static rtx
4174 expand_builtin_va_start (tree exp)
4176 rtx nextarg;
4177 tree valist;
4178 location_t loc = EXPR_LOCATION (exp);
4180 if (call_expr_nargs (exp) < 2)
4182 error_at (loc, "too few arguments to function %<va_start%>");
4183 return const0_rtx;
4186 if (fold_builtin_next_arg (exp, true))
4187 return const0_rtx;
4189 nextarg = expand_builtin_next_arg ();
4190 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4192 if (targetm.expand_builtin_va_start)
4193 targetm.expand_builtin_va_start (valist, nextarg);
4194 else
4195 std_expand_builtin_va_start (valist, nextarg);
4197 return const0_rtx;
4200 /* The "standard" implementation of va_arg: read the value from the
4201 current (padded) address and increment by the (padded) size. */
4203 tree
4204 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4205 gimple_seq *post_p)
4207 tree addr, t, type_size, rounded_size, valist_tmp;
4208 unsigned HOST_WIDE_INT align, boundary;
4209 bool indirect;
4211 #ifdef ARGS_GROW_DOWNWARD
4212 /* All of the alignment and movement below is for args-grow-up machines.
4213 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4214 implement their own specialized gimplify_va_arg_expr routines. */
4215 gcc_unreachable ();
4216 #endif
4218 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4219 if (indirect)
4220 type = build_pointer_type (type);
4222 align = PARM_BOUNDARY / BITS_PER_UNIT;
4223 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4225 /* When we align parameter on stack for caller, if the parameter
4226 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4227 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4228 here with caller. */
4229 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4230 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4232 boundary /= BITS_PER_UNIT;
4234 /* Hoist the valist value into a temporary for the moment. */
4235 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4237 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4238 requires greater alignment, we must perform dynamic alignment. */
4239 if (boundary > align
4240 && !integer_zerop (TYPE_SIZE (type)))
4242 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4243 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4244 gimplify_and_add (t, pre_p);
4246 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4247 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4248 valist_tmp,
4249 build_int_cst (TREE_TYPE (valist), -boundary)));
4250 gimplify_and_add (t, pre_p);
4252 else
4253 boundary = align;
4255 /* If the actual alignment is less than the alignment of the type,
4256 adjust the type accordingly so that we don't assume strict alignment
4257 when dereferencing the pointer. */
4258 boundary *= BITS_PER_UNIT;
4259 if (boundary < TYPE_ALIGN (type))
4261 type = build_variant_type_copy (type);
4262 TYPE_ALIGN (type) = boundary;
4265 /* Compute the rounded size of the type. */
4266 type_size = size_in_bytes (type);
4267 rounded_size = round_up (type_size, align);
4269 /* Reduce rounded_size so it's sharable with the postqueue. */
4270 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4272 /* Get AP. */
4273 addr = valist_tmp;
4274 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4276 /* Small args are padded downward. */
4277 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4278 rounded_size, size_int (align));
4279 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4280 size_binop (MINUS_EXPR, rounded_size, type_size));
4281 addr = fold_build_pointer_plus (addr, t);
4284 /* Compute new value for AP. */
4285 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4286 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4287 gimplify_and_add (t, pre_p);
4289 addr = fold_convert (build_pointer_type (type), addr);
4291 if (indirect)
4292 addr = build_va_arg_indirect_ref (addr);
4294 return build_va_arg_indirect_ref (addr);
4297 /* Build an indirect-ref expression over the given TREE, which represents a
4298 piece of a va_arg() expansion. */
4299 tree
4300 build_va_arg_indirect_ref (tree addr)
4302 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4304 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4305 mf_mark (addr);
4307 return addr;
4310 /* Return a dummy expression of type TYPE in order to keep going after an
4311 error. */
4313 static tree
4314 dummy_object (tree type)
4316 tree t = build_int_cst (build_pointer_type (type), 0);
4317 return build2 (MEM_REF, type, t, t);
4320 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4321 builtin function, but a very special sort of operator. */
4323 enum gimplify_status
4324 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4326 tree promoted_type, have_va_type;
4327 tree valist = TREE_OPERAND (*expr_p, 0);
4328 tree type = TREE_TYPE (*expr_p);
4329 tree t;
4330 location_t loc = EXPR_LOCATION (*expr_p);
4332 /* Verify that valist is of the proper type. */
4333 have_va_type = TREE_TYPE (valist);
4334 if (have_va_type == error_mark_node)
4335 return GS_ERROR;
4336 have_va_type = targetm.canonical_va_list_type (have_va_type);
4338 if (have_va_type == NULL_TREE)
4340 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4341 return GS_ERROR;
4344 /* Generate a diagnostic for requesting data of a type that cannot
4345 be passed through `...' due to type promotion at the call site. */
4346 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4347 != type)
4349 static bool gave_help;
4350 bool warned;
4352 /* Unfortunately, this is merely undefined, rather than a constraint
4353 violation, so we cannot make this an error. If this call is never
4354 executed, the program is still strictly conforming. */
4355 warned = warning_at (loc, 0,
4356 "%qT is promoted to %qT when passed through %<...%>",
4357 type, promoted_type);
4358 if (!gave_help && warned)
4360 gave_help = true;
4361 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4362 promoted_type, type);
4365 /* We can, however, treat "undefined" any way we please.
4366 Call abort to encourage the user to fix the program. */
4367 if (warned)
4368 inform (loc, "if this code is reached, the program will abort");
4369 /* Before the abort, allow the evaluation of the va_list
4370 expression to exit or longjmp. */
4371 gimplify_and_add (valist, pre_p);
4372 t = build_call_expr_loc (loc,
4373 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4374 gimplify_and_add (t, pre_p);
4376 /* This is dead code, but go ahead and finish so that the
4377 mode of the result comes out right. */
4378 *expr_p = dummy_object (type);
4379 return GS_ALL_DONE;
4381 else
4383 /* Make it easier for the backends by protecting the valist argument
4384 from multiple evaluations. */
4385 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4387 /* For this case, the backends will be expecting a pointer to
4388 TREE_TYPE (abi), but it's possible we've
4389 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4390 So fix it. */
4391 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4393 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4394 valist = fold_convert_loc (loc, p1,
4395 build_fold_addr_expr_loc (loc, valist));
4398 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4400 else
4401 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4403 if (!targetm.gimplify_va_arg_expr)
4404 /* FIXME: Once most targets are converted we should merely
4405 assert this is non-null. */
4406 return GS_ALL_DONE;
4408 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4409 return GS_OK;
4413 /* Expand EXP, a call to __builtin_va_end. */
4415 static rtx
4416 expand_builtin_va_end (tree exp)
4418 tree valist = CALL_EXPR_ARG (exp, 0);
4420 /* Evaluate for side effects, if needed. I hate macros that don't
4421 do that. */
4422 if (TREE_SIDE_EFFECTS (valist))
4423 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4425 return const0_rtx;
4428 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4429 builtin rather than just as an assignment in stdarg.h because of the
4430 nastiness of array-type va_list types. */
4432 static rtx
4433 expand_builtin_va_copy (tree exp)
4435 tree dst, src, t;
4436 location_t loc = EXPR_LOCATION (exp);
4438 dst = CALL_EXPR_ARG (exp, 0);
4439 src = CALL_EXPR_ARG (exp, 1);
4441 dst = stabilize_va_list_loc (loc, dst, 1);
4442 src = stabilize_va_list_loc (loc, src, 0);
4444 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4446 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4448 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4449 TREE_SIDE_EFFECTS (t) = 1;
4450 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4452 else
4454 rtx dstb, srcb, size;
4456 /* Evaluate to pointers. */
4457 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4458 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4459 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4460 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4462 dstb = convert_memory_address (Pmode, dstb);
4463 srcb = convert_memory_address (Pmode, srcb);
4465 /* "Dereference" to BLKmode memories. */
4466 dstb = gen_rtx_MEM (BLKmode, dstb);
4467 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4468 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4469 srcb = gen_rtx_MEM (BLKmode, srcb);
4470 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4471 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4473 /* Copy. */
4474 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4477 return const0_rtx;
4480 /* Expand a call to one of the builtin functions __builtin_frame_address or
4481 __builtin_return_address. */
4483 static rtx
4484 expand_builtin_frame_address (tree fndecl, tree exp)
4486 /* The argument must be a nonnegative integer constant.
4487 It counts the number of frames to scan up the stack.
4488 The value is the return address saved in that frame. */
4489 if (call_expr_nargs (exp) == 0)
4490 /* Warning about missing arg was already issued. */
4491 return const0_rtx;
4492 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4494 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4495 error ("invalid argument to %<__builtin_frame_address%>");
4496 else
4497 error ("invalid argument to %<__builtin_return_address%>");
4498 return const0_rtx;
4500 else
4502 rtx tem
4503 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4504 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4506 /* Some ports cannot access arbitrary stack frames. */
4507 if (tem == NULL)
4509 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4510 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4511 else
4512 warning (0, "unsupported argument to %<__builtin_return_address%>");
4513 return const0_rtx;
4516 /* For __builtin_frame_address, return what we've got. */
4517 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4518 return tem;
4520 if (!REG_P (tem)
4521 && ! CONSTANT_P (tem))
4522 tem = copy_addr_to_reg (tem);
4523 return tem;
4527 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4528 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4529 is the same as for allocate_dynamic_stack_space. */
4531 static rtx
4532 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4534 rtx op0;
4535 rtx result;
4536 bool valid_arglist;
4537 unsigned int align;
4538 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4539 == BUILT_IN_ALLOCA_WITH_ALIGN);
4541 /* Emit normal call if we use mudflap. */
4542 if (flag_mudflap)
4543 return NULL_RTX;
4545 valid_arglist
4546 = (alloca_with_align
4547 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4548 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4550 if (!valid_arglist)
4551 return NULL_RTX;
4553 /* Compute the argument. */
4554 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4556 /* Compute the alignment. */
4557 align = (alloca_with_align
4558 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4559 : BIGGEST_ALIGNMENT);
4561 /* Allocate the desired space. */
4562 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4563 result = convert_memory_address (ptr_mode, result);
4565 return result;
4568 /* Expand a call to bswap builtin in EXP.
4569 Return NULL_RTX if a normal call should be emitted rather than expanding the
4570 function in-line. If convenient, the result should be placed in TARGET.
4571 SUBTARGET may be used as the target for computing one of EXP's operands. */
4573 static rtx
4574 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4575 rtx subtarget)
4577 tree arg;
4578 rtx op0;
4580 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4581 return NULL_RTX;
4583 arg = CALL_EXPR_ARG (exp, 0);
4584 op0 = expand_expr (arg,
4585 subtarget && GET_MODE (subtarget) == target_mode
4586 ? subtarget : NULL_RTX,
4587 target_mode, EXPAND_NORMAL);
4588 if (GET_MODE (op0) != target_mode)
4589 op0 = convert_to_mode (target_mode, op0, 1);
4591 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4593 gcc_assert (target);
4595 return convert_to_mode (target_mode, target, 1);
4598 /* Expand a call to a unary builtin in EXP.
4599 Return NULL_RTX if a normal call should be emitted rather than expanding the
4600 function in-line. If convenient, the result should be placed in TARGET.
4601 SUBTARGET may be used as the target for computing one of EXP's operands. */
4603 static rtx
4604 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4605 rtx subtarget, optab op_optab)
4607 rtx op0;
4609 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4610 return NULL_RTX;
4612 /* Compute the argument. */
4613 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4614 (subtarget
4615 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4616 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4617 VOIDmode, EXPAND_NORMAL);
4618 /* Compute op, into TARGET if possible.
4619 Set TARGET to wherever the result comes back. */
4620 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4621 op_optab, op0, target, op_optab != clrsb_optab);
4622 gcc_assert (target);
4624 return convert_to_mode (target_mode, target, 0);
4627 /* Expand a call to __builtin_expect. We just return our argument
4628 as the builtin_expect semantic should've been already executed by
4629 tree branch prediction pass. */
4631 static rtx
4632 expand_builtin_expect (tree exp, rtx target)
4634 tree arg;
4636 if (call_expr_nargs (exp) < 2)
4637 return const0_rtx;
4638 arg = CALL_EXPR_ARG (exp, 0);
4640 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4641 /* When guessing was done, the hints should be already stripped away. */
4642 gcc_assert (!flag_guess_branch_prob
4643 || optimize == 0 || seen_error ());
4644 return target;
4647 /* Expand a call to __builtin_assume_aligned. We just return our first
4648 argument as the builtin_assume_aligned semantic should've been already
4649 executed by CCP. */
4651 static rtx
4652 expand_builtin_assume_aligned (tree exp, rtx target)
4654 if (call_expr_nargs (exp) < 2)
4655 return const0_rtx;
4656 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4657 EXPAND_NORMAL);
4658 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4659 && (call_expr_nargs (exp) < 3
4660 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4661 return target;
4664 void
4665 expand_builtin_trap (void)
4667 #ifdef HAVE_trap
4668 if (HAVE_trap)
4670 rtx insn = emit_insn (gen_trap ());
4671 /* For trap insns when not accumulating outgoing args force
4672 REG_ARGS_SIZE note to prevent crossjumping of calls with
4673 different args sizes. */
4674 if (!ACCUMULATE_OUTGOING_ARGS)
4675 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4677 else
4678 #endif
4679 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4680 emit_barrier ();
4683 /* Expand a call to __builtin_unreachable. We do nothing except emit
4684 a barrier saying that control flow will not pass here.
4686 It is the responsibility of the program being compiled to ensure
4687 that control flow does never reach __builtin_unreachable. */
4688 static void
4689 expand_builtin_unreachable (void)
4691 emit_barrier ();
4694 /* Expand EXP, a call to fabs, fabsf or fabsl.
4695 Return NULL_RTX if a normal call should be emitted rather than expanding
4696 the function inline. If convenient, the result should be placed
4697 in TARGET. SUBTARGET may be used as the target for computing
4698 the operand. */
4700 static rtx
4701 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4703 enum machine_mode mode;
4704 tree arg;
4705 rtx op0;
4707 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4708 return NULL_RTX;
4710 arg = CALL_EXPR_ARG (exp, 0);
4711 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4712 mode = TYPE_MODE (TREE_TYPE (arg));
4713 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4714 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4717 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4718 Return NULL is a normal call should be emitted rather than expanding the
4719 function inline. If convenient, the result should be placed in TARGET.
4720 SUBTARGET may be used as the target for computing the operand. */
4722 static rtx
4723 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4725 rtx op0, op1;
4726 tree arg;
4728 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4729 return NULL_RTX;
4731 arg = CALL_EXPR_ARG (exp, 0);
4732 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4734 arg = CALL_EXPR_ARG (exp, 1);
4735 op1 = expand_normal (arg);
4737 return expand_copysign (op0, op1, target);
4740 /* Create a new constant string literal and return a char* pointer to it.
4741 The STRING_CST value is the LEN characters at STR. */
4742 tree
4743 build_string_literal (int len, const char *str)
4745 tree t, elem, index, type;
4747 t = build_string (len, str);
4748 elem = build_type_variant (char_type_node, 1, 0);
4749 index = build_index_type (size_int (len - 1));
4750 type = build_array_type (elem, index);
4751 TREE_TYPE (t) = type;
4752 TREE_CONSTANT (t) = 1;
4753 TREE_READONLY (t) = 1;
4754 TREE_STATIC (t) = 1;
4756 type = build_pointer_type (elem);
4757 t = build1 (ADDR_EXPR, type,
4758 build4 (ARRAY_REF, elem,
4759 t, integer_zero_node, NULL_TREE, NULL_TREE));
4760 return t;
4763 /* Expand a call to __builtin___clear_cache. */
4765 static rtx
4766 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4768 #ifndef HAVE_clear_cache
4769 #ifdef CLEAR_INSN_CACHE
4770 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4771 does something. Just do the default expansion to a call to
4772 __clear_cache(). */
4773 return NULL_RTX;
4774 #else
4775 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4776 does nothing. There is no need to call it. Do nothing. */
4777 return const0_rtx;
4778 #endif /* CLEAR_INSN_CACHE */
4779 #else
4780 /* We have a "clear_cache" insn, and it will handle everything. */
4781 tree begin, end;
4782 rtx begin_rtx, end_rtx;
4784 /* We must not expand to a library call. If we did, any
4785 fallback library function in libgcc that might contain a call to
4786 __builtin___clear_cache() would recurse infinitely. */
4787 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4789 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4790 return const0_rtx;
4793 if (HAVE_clear_cache)
4795 struct expand_operand ops[2];
4797 begin = CALL_EXPR_ARG (exp, 0);
4798 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4800 end = CALL_EXPR_ARG (exp, 1);
4801 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4803 create_address_operand (&ops[0], begin_rtx);
4804 create_address_operand (&ops[1], end_rtx);
4805 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4806 return const0_rtx;
4808 return const0_rtx;
4809 #endif /* HAVE_clear_cache */
4812 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4814 static rtx
4815 round_trampoline_addr (rtx tramp)
4817 rtx temp, addend, mask;
4819 /* If we don't need too much alignment, we'll have been guaranteed
4820 proper alignment by get_trampoline_type. */
4821 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4822 return tramp;
4824 /* Round address up to desired boundary. */
4825 temp = gen_reg_rtx (Pmode);
4826 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4827 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4829 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4830 temp, 0, OPTAB_LIB_WIDEN);
4831 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4832 temp, 0, OPTAB_LIB_WIDEN);
4834 return tramp;
4837 static rtx
4838 expand_builtin_init_trampoline (tree exp, bool onstack)
4840 tree t_tramp, t_func, t_chain;
4841 rtx m_tramp, r_tramp, r_chain, tmp;
4843 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4844 POINTER_TYPE, VOID_TYPE))
4845 return NULL_RTX;
4847 t_tramp = CALL_EXPR_ARG (exp, 0);
4848 t_func = CALL_EXPR_ARG (exp, 1);
4849 t_chain = CALL_EXPR_ARG (exp, 2);
4851 r_tramp = expand_normal (t_tramp);
4852 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4853 MEM_NOTRAP_P (m_tramp) = 1;
4855 /* If ONSTACK, the TRAMP argument should be the address of a field
4856 within the local function's FRAME decl. Either way, let's see if
4857 we can fill in the MEM_ATTRs for this memory. */
4858 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4859 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4860 true, 0);
4862 /* Creator of a heap trampoline is responsible for making sure the
4863 address is aligned to at least STACK_BOUNDARY. Normally malloc
4864 will ensure this anyhow. */
4865 tmp = round_trampoline_addr (r_tramp);
4866 if (tmp != r_tramp)
4868 m_tramp = change_address (m_tramp, BLKmode, tmp);
4869 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4870 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4873 /* The FUNC argument should be the address of the nested function.
4874 Extract the actual function decl to pass to the hook. */
4875 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4876 t_func = TREE_OPERAND (t_func, 0);
4877 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4879 r_chain = expand_normal (t_chain);
4881 /* Generate insns to initialize the trampoline. */
4882 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4884 if (onstack)
4886 trampolines_created = 1;
4888 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4889 "trampoline generated for nested function %qD", t_func);
4892 return const0_rtx;
4895 static rtx
4896 expand_builtin_adjust_trampoline (tree exp)
4898 rtx tramp;
4900 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4901 return NULL_RTX;
4903 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4904 tramp = round_trampoline_addr (tramp);
4905 if (targetm.calls.trampoline_adjust_address)
4906 tramp = targetm.calls.trampoline_adjust_address (tramp);
4908 return tramp;
4911 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4912 function. The function first checks whether the back end provides
4913 an insn to implement signbit for the respective mode. If not, it
4914 checks whether the floating point format of the value is such that
4915 the sign bit can be extracted. If that is not the case, the
4916 function returns NULL_RTX to indicate that a normal call should be
4917 emitted rather than expanding the function in-line. EXP is the
4918 expression that is a call to the builtin function; if convenient,
4919 the result should be placed in TARGET. */
4920 static rtx
4921 expand_builtin_signbit (tree exp, rtx target)
4923 const struct real_format *fmt;
4924 enum machine_mode fmode, imode, rmode;
4925 tree arg;
4926 int word, bitpos;
4927 enum insn_code icode;
4928 rtx temp;
4929 location_t loc = EXPR_LOCATION (exp);
4931 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4932 return NULL_RTX;
4934 arg = CALL_EXPR_ARG (exp, 0);
4935 fmode = TYPE_MODE (TREE_TYPE (arg));
4936 rmode = TYPE_MODE (TREE_TYPE (exp));
4937 fmt = REAL_MODE_FORMAT (fmode);
4939 arg = builtin_save_expr (arg);
4941 /* Expand the argument yielding a RTX expression. */
4942 temp = expand_normal (arg);
4944 /* Check if the back end provides an insn that handles signbit for the
4945 argument's mode. */
4946 icode = optab_handler (signbit_optab, fmode);
4947 if (icode != CODE_FOR_nothing)
4949 rtx last = get_last_insn ();
4950 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4951 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4952 return target;
4953 delete_insns_since (last);
4956 /* For floating point formats without a sign bit, implement signbit
4957 as "ARG < 0.0". */
4958 bitpos = fmt->signbit_ro;
4959 if (bitpos < 0)
4961 /* But we can't do this if the format supports signed zero. */
4962 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4963 return NULL_RTX;
4965 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4966 build_real (TREE_TYPE (arg), dconst0));
4967 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4970 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4972 imode = int_mode_for_mode (fmode);
4973 if (imode == BLKmode)
4974 return NULL_RTX;
4975 temp = gen_lowpart (imode, temp);
4977 else
4979 imode = word_mode;
4980 /* Handle targets with different FP word orders. */
4981 if (FLOAT_WORDS_BIG_ENDIAN)
4982 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4983 else
4984 word = bitpos / BITS_PER_WORD;
4985 temp = operand_subword_force (temp, word, fmode);
4986 bitpos = bitpos % BITS_PER_WORD;
4989 /* Force the intermediate word_mode (or narrower) result into a
4990 register. This avoids attempting to create paradoxical SUBREGs
4991 of floating point modes below. */
4992 temp = force_reg (imode, temp);
4994 /* If the bitpos is within the "result mode" lowpart, the operation
4995 can be implement with a single bitwise AND. Otherwise, we need
4996 a right shift and an AND. */
4998 if (bitpos < GET_MODE_BITSIZE (rmode))
5000 double_int mask = double_int_zero.set_bit (bitpos);
5002 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5003 temp = gen_lowpart (rmode, temp);
5004 temp = expand_binop (rmode, and_optab, temp,
5005 immed_double_int_const (mask, rmode),
5006 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5008 else
5010 /* Perform a logical right shift to place the signbit in the least
5011 significant bit, then truncate the result to the desired mode
5012 and mask just this bit. */
5013 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5014 temp = gen_lowpart (rmode, temp);
5015 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5016 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5019 return temp;
5022 /* Expand fork or exec calls. TARGET is the desired target of the
5023 call. EXP is the call. FN is the
5024 identificator of the actual function. IGNORE is nonzero if the
5025 value is to be ignored. */
5027 static rtx
5028 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5030 tree id, decl;
5031 tree call;
5033 /* If we are not profiling, just call the function. */
5034 if (!profile_arc_flag)
5035 return NULL_RTX;
5037 /* Otherwise call the wrapper. This should be equivalent for the rest of
5038 compiler, so the code does not diverge, and the wrapper may run the
5039 code necessary for keeping the profiling sane. */
5041 switch (DECL_FUNCTION_CODE (fn))
5043 case BUILT_IN_FORK:
5044 id = get_identifier ("__gcov_fork");
5045 break;
5047 case BUILT_IN_EXECL:
5048 id = get_identifier ("__gcov_execl");
5049 break;
5051 case BUILT_IN_EXECV:
5052 id = get_identifier ("__gcov_execv");
5053 break;
5055 case BUILT_IN_EXECLP:
5056 id = get_identifier ("__gcov_execlp");
5057 break;
5059 case BUILT_IN_EXECLE:
5060 id = get_identifier ("__gcov_execle");
5061 break;
5063 case BUILT_IN_EXECVP:
5064 id = get_identifier ("__gcov_execvp");
5065 break;
5067 case BUILT_IN_EXECVE:
5068 id = get_identifier ("__gcov_execve");
5069 break;
5071 default:
5072 gcc_unreachable ();
5075 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5076 FUNCTION_DECL, id, TREE_TYPE (fn));
5077 DECL_EXTERNAL (decl) = 1;
5078 TREE_PUBLIC (decl) = 1;
5079 DECL_ARTIFICIAL (decl) = 1;
5080 TREE_NOTHROW (decl) = 1;
5081 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5082 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5083 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5084 return expand_call (call, target, ignore);
5089 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5090 the pointer in these functions is void*, the tree optimizers may remove
5091 casts. The mode computed in expand_builtin isn't reliable either, due
5092 to __sync_bool_compare_and_swap.
5094 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5095 group of builtins. This gives us log2 of the mode size. */
5097 static inline enum machine_mode
5098 get_builtin_sync_mode (int fcode_diff)
5100 /* The size is not negotiable, so ask not to get BLKmode in return
5101 if the target indicates that a smaller size would be better. */
5102 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5105 /* Expand the memory expression LOC and return the appropriate memory operand
5106 for the builtin_sync operations. */
5108 static rtx
5109 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5111 rtx addr, mem;
5113 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5114 addr = convert_memory_address (Pmode, addr);
5116 /* Note that we explicitly do not want any alias information for this
5117 memory, so that we kill all other live memories. Otherwise we don't
5118 satisfy the full barrier semantics of the intrinsic. */
5119 mem = validize_mem (gen_rtx_MEM (mode, addr));
5121 /* The alignment needs to be at least according to that of the mode. */
5122 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5123 get_pointer_alignment (loc)));
5124 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5125 MEM_VOLATILE_P (mem) = 1;
5127 return mem;
5130 /* Make sure an argument is in the right mode.
5131 EXP is the tree argument.
5132 MODE is the mode it should be in. */
5134 static rtx
5135 expand_expr_force_mode (tree exp, enum machine_mode mode)
5137 rtx val;
5138 enum machine_mode old_mode;
5140 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5141 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5142 of CONST_INTs, where we know the old_mode only from the call argument. */
5144 old_mode = GET_MODE (val);
5145 if (old_mode == VOIDmode)
5146 old_mode = TYPE_MODE (TREE_TYPE (exp));
5147 val = convert_modes (mode, old_mode, val, 1);
5148 return val;
5152 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5153 EXP is the CALL_EXPR. CODE is the rtx code
5154 that corresponds to the arithmetic or logical operation from the name;
5155 an exception here is that NOT actually means NAND. TARGET is an optional
5156 place for us to store the results; AFTER is true if this is the
5157 fetch_and_xxx form. */
5159 static rtx
5160 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5161 enum rtx_code code, bool after,
5162 rtx target)
5164 rtx val, mem;
5165 location_t loc = EXPR_LOCATION (exp);
5167 if (code == NOT && warn_sync_nand)
5169 tree fndecl = get_callee_fndecl (exp);
5170 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5172 static bool warned_f_a_n, warned_n_a_f;
5174 switch (fcode)
5176 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5177 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5178 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5179 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5180 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5181 if (warned_f_a_n)
5182 break;
5184 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5185 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5186 warned_f_a_n = true;
5187 break;
5189 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5190 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5191 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5192 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5193 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5194 if (warned_n_a_f)
5195 break;
5197 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5198 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5199 warned_n_a_f = true;
5200 break;
5202 default:
5203 gcc_unreachable ();
5207 /* Expand the operands. */
5208 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5209 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5211 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5212 after);
5215 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5216 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5217 true if this is the boolean form. TARGET is a place for us to store the
5218 results; this is NOT optional if IS_BOOL is true. */
5220 static rtx
5221 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5222 bool is_bool, rtx target)
5224 rtx old_val, new_val, mem;
5225 rtx *pbool, *poval;
5227 /* Expand the operands. */
5228 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5229 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5230 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5232 pbool = poval = NULL;
5233 if (target != const0_rtx)
5235 if (is_bool)
5236 pbool = &target;
5237 else
5238 poval = &target;
5240 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5241 false, MEMMODEL_SEQ_CST,
5242 MEMMODEL_SEQ_CST))
5243 return NULL_RTX;
5245 return target;
5248 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5249 general form is actually an atomic exchange, and some targets only
5250 support a reduced form with the second argument being a constant 1.
5251 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5252 the results. */
5254 static rtx
5255 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5256 rtx target)
5258 rtx val, mem;
5260 /* Expand the operands. */
5261 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5262 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5264 return expand_sync_lock_test_and_set (target, mem, val);
5267 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5269 static void
5270 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5272 rtx mem;
5274 /* Expand the operands. */
5275 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5277 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5280 /* Given an integer representing an ``enum memmodel'', verify its
5281 correctness and return the memory model enum. */
5283 static enum memmodel
5284 get_memmodel (tree exp)
5286 rtx op;
5287 unsigned HOST_WIDE_INT val;
5289 /* If the parameter is not a constant, it's a run time value so we'll just
5290 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5291 if (TREE_CODE (exp) != INTEGER_CST)
5292 return MEMMODEL_SEQ_CST;
5294 op = expand_normal (exp);
5296 val = INTVAL (op);
5297 if (targetm.memmodel_check)
5298 val = targetm.memmodel_check (val);
5299 else if (val & ~MEMMODEL_MASK)
5301 warning (OPT_Winvalid_memory_model,
5302 "Unknown architecture specifier in memory model to builtin.");
5303 return MEMMODEL_SEQ_CST;
5306 if ((INTVAL(op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5308 warning (OPT_Winvalid_memory_model,
5309 "invalid memory model argument to builtin");
5310 return MEMMODEL_SEQ_CST;
5313 return (enum memmodel) val;
5316 /* Expand the __atomic_exchange intrinsic:
5317 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5318 EXP is the CALL_EXPR.
5319 TARGET is an optional place for us to store the results. */
5321 static rtx
5322 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5324 rtx val, mem;
5325 enum memmodel model;
5327 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5328 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5330 error ("invalid memory model for %<__atomic_exchange%>");
5331 return NULL_RTX;
5334 if (!flag_inline_atomics)
5335 return NULL_RTX;
5337 /* Expand the operands. */
5338 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5339 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5341 return expand_atomic_exchange (target, mem, val, model);
5344 /* Expand the __atomic_compare_exchange intrinsic:
5345 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5346 TYPE desired, BOOL weak,
5347 enum memmodel success,
5348 enum memmodel failure)
5349 EXP is the CALL_EXPR.
5350 TARGET is an optional place for us to store the results. */
5352 static rtx
5353 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5354 rtx target)
5356 rtx expect, desired, mem, oldval;
5357 enum memmodel success, failure;
5358 tree weak;
5359 bool is_weak;
5361 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5362 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5364 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5365 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5367 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5368 return NULL_RTX;
5371 if (failure > success)
5373 error ("failure memory model cannot be stronger than success "
5374 "memory model for %<__atomic_compare_exchange%>");
5375 return NULL_RTX;
5378 if (!flag_inline_atomics)
5379 return NULL_RTX;
5381 /* Expand the operands. */
5382 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5384 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5385 expect = convert_memory_address (Pmode, expect);
5386 expect = gen_rtx_MEM (mode, expect);
5387 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5389 weak = CALL_EXPR_ARG (exp, 3);
5390 is_weak = false;
5391 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5392 is_weak = true;
5394 oldval = expect;
5395 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5396 &oldval, mem, oldval, desired,
5397 is_weak, success, failure))
5398 return NULL_RTX;
5400 if (oldval != expect)
5401 emit_move_insn (expect, oldval);
5403 return target;
5406 /* Expand the __atomic_load intrinsic:
5407 TYPE __atomic_load (TYPE *object, enum memmodel)
5408 EXP is the CALL_EXPR.
5409 TARGET is an optional place for us to store the results. */
5411 static rtx
5412 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5414 rtx mem;
5415 enum memmodel model;
5417 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5418 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5419 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5421 error ("invalid memory model for %<__atomic_load%>");
5422 return NULL_RTX;
5425 if (!flag_inline_atomics)
5426 return NULL_RTX;
5428 /* Expand the operand. */
5429 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5431 return expand_atomic_load (target, mem, model);
5435 /* Expand the __atomic_store intrinsic:
5436 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5437 EXP is the CALL_EXPR.
5438 TARGET is an optional place for us to store the results. */
5440 static rtx
5441 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5443 rtx mem, val;
5444 enum memmodel model;
5446 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5447 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5448 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5449 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5451 error ("invalid memory model for %<__atomic_store%>");
5452 return NULL_RTX;
5455 if (!flag_inline_atomics)
5456 return NULL_RTX;
5458 /* Expand the operands. */
5459 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5460 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5462 return expand_atomic_store (mem, val, model, false);
5465 /* Expand the __atomic_fetch_XXX intrinsic:
5466 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5467 EXP is the CALL_EXPR.
5468 TARGET is an optional place for us to store the results.
5469 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5470 FETCH_AFTER is true if returning the result of the operation.
5471 FETCH_AFTER is false if returning the value before the operation.
5472 IGNORE is true if the result is not used.
5473 EXT_CALL is the correct builtin for an external call if this cannot be
5474 resolved to an instruction sequence. */
5476 static rtx
5477 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5478 enum rtx_code code, bool fetch_after,
5479 bool ignore, enum built_in_function ext_call)
5481 rtx val, mem, ret;
5482 enum memmodel model;
5483 tree fndecl;
5484 tree addr;
5486 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5488 /* Expand the operands. */
5489 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5490 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5492 /* Only try generating instructions if inlining is turned on. */
5493 if (flag_inline_atomics)
5495 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5496 if (ret)
5497 return ret;
5500 /* Return if a different routine isn't needed for the library call. */
5501 if (ext_call == BUILT_IN_NONE)
5502 return NULL_RTX;
5504 /* Change the call to the specified function. */
5505 fndecl = get_callee_fndecl (exp);
5506 addr = CALL_EXPR_FN (exp);
5507 STRIP_NOPS (addr);
5509 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5510 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5512 /* Expand the call here so we can emit trailing code. */
5513 ret = expand_call (exp, target, ignore);
5515 /* Replace the original function just in case it matters. */
5516 TREE_OPERAND (addr, 0) = fndecl;
5518 /* Then issue the arithmetic correction to return the right result. */
5519 if (!ignore)
5521 if (code == NOT)
5523 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5524 OPTAB_LIB_WIDEN);
5525 ret = expand_simple_unop (mode, NOT, ret, target, true);
5527 else
5528 ret = expand_simple_binop (mode, code, ret, val, target, true,
5529 OPTAB_LIB_WIDEN);
5531 return ret;
5535 #ifndef HAVE_atomic_clear
5536 # define HAVE_atomic_clear 0
5537 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5538 #endif
5540 /* Expand an atomic clear operation.
5541 void _atomic_clear (BOOL *obj, enum memmodel)
5542 EXP is the call expression. */
5544 static rtx
5545 expand_builtin_atomic_clear (tree exp)
5547 enum machine_mode mode;
5548 rtx mem, ret;
5549 enum memmodel model;
5551 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5552 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5553 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5555 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5556 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5558 error ("invalid memory model for %<__atomic_store%>");
5559 return const0_rtx;
5562 if (HAVE_atomic_clear)
5564 emit_insn (gen_atomic_clear (mem, model));
5565 return const0_rtx;
5568 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5569 Failing that, a store is issued by __atomic_store. The only way this can
5570 fail is if the bool type is larger than a word size. Unlikely, but
5571 handle it anyway for completeness. Assume a single threaded model since
5572 there is no atomic support in this case, and no barriers are required. */
5573 ret = expand_atomic_store (mem, const0_rtx, model, true);
5574 if (!ret)
5575 emit_move_insn (mem, const0_rtx);
5576 return const0_rtx;
5579 /* Expand an atomic test_and_set operation.
5580 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5581 EXP is the call expression. */
5583 static rtx
5584 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5586 rtx mem;
5587 enum memmodel model;
5588 enum machine_mode mode;
5590 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5591 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5592 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5594 return expand_atomic_test_and_set (target, mem, model);
5598 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5599 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5601 static tree
5602 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5604 int size;
5605 enum machine_mode mode;
5606 unsigned int mode_align, type_align;
5608 if (TREE_CODE (arg0) != INTEGER_CST)
5609 return NULL_TREE;
5611 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5612 mode = mode_for_size (size, MODE_INT, 0);
5613 mode_align = GET_MODE_ALIGNMENT (mode);
5615 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5616 type_align = mode_align;
5617 else
5619 tree ttype = TREE_TYPE (arg1);
5621 /* This function is usually invoked and folded immediately by the front
5622 end before anything else has a chance to look at it. The pointer
5623 parameter at this point is usually cast to a void *, so check for that
5624 and look past the cast. */
5625 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5626 && VOID_TYPE_P (TREE_TYPE (ttype)))
5627 arg1 = TREE_OPERAND (arg1, 0);
5629 ttype = TREE_TYPE (arg1);
5630 gcc_assert (POINTER_TYPE_P (ttype));
5632 /* Get the underlying type of the object. */
5633 ttype = TREE_TYPE (ttype);
5634 type_align = TYPE_ALIGN (ttype);
5637 /* If the object has smaller alignment, the the lock free routines cannot
5638 be used. */
5639 if (type_align < mode_align)
5640 return boolean_false_node;
5642 /* Check if a compare_and_swap pattern exists for the mode which represents
5643 the required size. The pattern is not allowed to fail, so the existence
5644 of the pattern indicates support is present. */
5645 if (can_compare_and_swap_p (mode, true))
5646 return boolean_true_node;
5647 else
5648 return boolean_false_node;
5651 /* Return true if the parameters to call EXP represent an object which will
5652 always generate lock free instructions. The first argument represents the
5653 size of the object, and the second parameter is a pointer to the object
5654 itself. If NULL is passed for the object, then the result is based on
5655 typical alignment for an object of the specified size. Otherwise return
5656 false. */
5658 static rtx
5659 expand_builtin_atomic_always_lock_free (tree exp)
5661 tree size;
5662 tree arg0 = CALL_EXPR_ARG (exp, 0);
5663 tree arg1 = CALL_EXPR_ARG (exp, 1);
5665 if (TREE_CODE (arg0) != INTEGER_CST)
5667 error ("non-constant argument 1 to __atomic_always_lock_free");
5668 return const0_rtx;
5671 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5672 if (size == boolean_true_node)
5673 return const1_rtx;
5674 return const0_rtx;
5677 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5678 is lock free on this architecture. */
5680 static tree
5681 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5683 if (!flag_inline_atomics)
5684 return NULL_TREE;
5686 /* If it isn't always lock free, don't generate a result. */
5687 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5688 return boolean_true_node;
5690 return NULL_TREE;
5693 /* Return true if the parameters to call EXP represent an object which will
5694 always generate lock free instructions. The first argument represents the
5695 size of the object, and the second parameter is a pointer to the object
5696 itself. If NULL is passed for the object, then the result is based on
5697 typical alignment for an object of the specified size. Otherwise return
5698 NULL*/
5700 static rtx
5701 expand_builtin_atomic_is_lock_free (tree exp)
5703 tree size;
5704 tree arg0 = CALL_EXPR_ARG (exp, 0);
5705 tree arg1 = CALL_EXPR_ARG (exp, 1);
5707 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5709 error ("non-integer argument 1 to __atomic_is_lock_free");
5710 return NULL_RTX;
5713 if (!flag_inline_atomics)
5714 return NULL_RTX;
5716 /* If the value is known at compile time, return the RTX for it. */
5717 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5718 if (size == boolean_true_node)
5719 return const1_rtx;
5721 return NULL_RTX;
5724 /* Expand the __atomic_thread_fence intrinsic:
5725 void __atomic_thread_fence (enum memmodel)
5726 EXP is the CALL_EXPR. */
5728 static void
5729 expand_builtin_atomic_thread_fence (tree exp)
5731 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5732 expand_mem_thread_fence (model);
5735 /* Expand the __atomic_signal_fence intrinsic:
5736 void __atomic_signal_fence (enum memmodel)
5737 EXP is the CALL_EXPR. */
5739 static void
5740 expand_builtin_atomic_signal_fence (tree exp)
5742 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5743 expand_mem_signal_fence (model);
5746 /* Expand the __sync_synchronize intrinsic. */
5748 static void
5749 expand_builtin_sync_synchronize (void)
5751 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5754 static rtx
5755 expand_builtin_thread_pointer (tree exp, rtx target)
5757 enum insn_code icode;
5758 if (!validate_arglist (exp, VOID_TYPE))
5759 return const0_rtx;
5760 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5761 if (icode != CODE_FOR_nothing)
5763 struct expand_operand op;
5764 if (!REG_P (target) || GET_MODE (target) != Pmode)
5765 target = gen_reg_rtx (Pmode);
5766 create_output_operand (&op, target, Pmode);
5767 expand_insn (icode, 1, &op);
5768 return target;
5770 error ("__builtin_thread_pointer is not supported on this target");
5771 return const0_rtx;
5774 static void
5775 expand_builtin_set_thread_pointer (tree exp)
5777 enum insn_code icode;
5778 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5779 return;
5780 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5781 if (icode != CODE_FOR_nothing)
5783 struct expand_operand op;
5784 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5785 Pmode, EXPAND_NORMAL);
5786 create_input_operand (&op, val, Pmode);
5787 expand_insn (icode, 1, &op);
5788 return;
5790 error ("__builtin_set_thread_pointer is not supported on this target");
5794 /* Expand an expression EXP that calls a built-in function,
5795 with result going to TARGET if that's convenient
5796 (and in mode MODE if that's convenient).
5797 SUBTARGET may be used as the target for computing one of EXP's operands.
5798 IGNORE is nonzero if the value is to be ignored. */
5801 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5802 int ignore)
5804 tree fndecl = get_callee_fndecl (exp);
5805 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5806 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5807 int flags;
5809 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5810 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5812 /* When not optimizing, generate calls to library functions for a certain
5813 set of builtins. */
5814 if (!optimize
5815 && !called_as_built_in (fndecl)
5816 && fcode != BUILT_IN_ALLOCA
5817 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5818 && fcode != BUILT_IN_FREE)
5819 return expand_call (exp, target, ignore);
5821 /* The built-in function expanders test for target == const0_rtx
5822 to determine whether the function's result will be ignored. */
5823 if (ignore)
5824 target = const0_rtx;
5826 /* If the result of a pure or const built-in function is ignored, and
5827 none of its arguments are volatile, we can avoid expanding the
5828 built-in call and just evaluate the arguments for side-effects. */
5829 if (target == const0_rtx
5830 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5831 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5833 bool volatilep = false;
5834 tree arg;
5835 call_expr_arg_iterator iter;
5837 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5838 if (TREE_THIS_VOLATILE (arg))
5840 volatilep = true;
5841 break;
5844 if (! volatilep)
5846 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5847 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5848 return const0_rtx;
5852 switch (fcode)
5854 CASE_FLT_FN (BUILT_IN_FABS):
5855 target = expand_builtin_fabs (exp, target, subtarget);
5856 if (target)
5857 return target;
5858 break;
5860 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5861 target = expand_builtin_copysign (exp, target, subtarget);
5862 if (target)
5863 return target;
5864 break;
5866 /* Just do a normal library call if we were unable to fold
5867 the values. */
5868 CASE_FLT_FN (BUILT_IN_CABS):
5869 break;
5871 CASE_FLT_FN (BUILT_IN_EXP):
5872 CASE_FLT_FN (BUILT_IN_EXP10):
5873 CASE_FLT_FN (BUILT_IN_POW10):
5874 CASE_FLT_FN (BUILT_IN_EXP2):
5875 CASE_FLT_FN (BUILT_IN_EXPM1):
5876 CASE_FLT_FN (BUILT_IN_LOGB):
5877 CASE_FLT_FN (BUILT_IN_LOG):
5878 CASE_FLT_FN (BUILT_IN_LOG10):
5879 CASE_FLT_FN (BUILT_IN_LOG2):
5880 CASE_FLT_FN (BUILT_IN_LOG1P):
5881 CASE_FLT_FN (BUILT_IN_TAN):
5882 CASE_FLT_FN (BUILT_IN_ASIN):
5883 CASE_FLT_FN (BUILT_IN_ACOS):
5884 CASE_FLT_FN (BUILT_IN_ATAN):
5885 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5886 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5887 because of possible accuracy problems. */
5888 if (! flag_unsafe_math_optimizations)
5889 break;
5890 CASE_FLT_FN (BUILT_IN_SQRT):
5891 CASE_FLT_FN (BUILT_IN_FLOOR):
5892 CASE_FLT_FN (BUILT_IN_CEIL):
5893 CASE_FLT_FN (BUILT_IN_TRUNC):
5894 CASE_FLT_FN (BUILT_IN_ROUND):
5895 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5896 CASE_FLT_FN (BUILT_IN_RINT):
5897 target = expand_builtin_mathfn (exp, target, subtarget);
5898 if (target)
5899 return target;
5900 break;
5902 CASE_FLT_FN (BUILT_IN_FMA):
5903 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5904 if (target)
5905 return target;
5906 break;
5908 CASE_FLT_FN (BUILT_IN_ILOGB):
5909 if (! flag_unsafe_math_optimizations)
5910 break;
5911 CASE_FLT_FN (BUILT_IN_ISINF):
5912 CASE_FLT_FN (BUILT_IN_FINITE):
5913 case BUILT_IN_ISFINITE:
5914 case BUILT_IN_ISNORMAL:
5915 target = expand_builtin_interclass_mathfn (exp, target);
5916 if (target)
5917 return target;
5918 break;
5920 CASE_FLT_FN (BUILT_IN_ICEIL):
5921 CASE_FLT_FN (BUILT_IN_LCEIL):
5922 CASE_FLT_FN (BUILT_IN_LLCEIL):
5923 CASE_FLT_FN (BUILT_IN_LFLOOR):
5924 CASE_FLT_FN (BUILT_IN_IFLOOR):
5925 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5926 target = expand_builtin_int_roundingfn (exp, target);
5927 if (target)
5928 return target;
5929 break;
5931 CASE_FLT_FN (BUILT_IN_IRINT):
5932 CASE_FLT_FN (BUILT_IN_LRINT):
5933 CASE_FLT_FN (BUILT_IN_LLRINT):
5934 CASE_FLT_FN (BUILT_IN_IROUND):
5935 CASE_FLT_FN (BUILT_IN_LROUND):
5936 CASE_FLT_FN (BUILT_IN_LLROUND):
5937 target = expand_builtin_int_roundingfn_2 (exp, target);
5938 if (target)
5939 return target;
5940 break;
5942 CASE_FLT_FN (BUILT_IN_POWI):
5943 target = expand_builtin_powi (exp, target);
5944 if (target)
5945 return target;
5946 break;
5948 CASE_FLT_FN (BUILT_IN_ATAN2):
5949 CASE_FLT_FN (BUILT_IN_LDEXP):
5950 CASE_FLT_FN (BUILT_IN_SCALB):
5951 CASE_FLT_FN (BUILT_IN_SCALBN):
5952 CASE_FLT_FN (BUILT_IN_SCALBLN):
5953 if (! flag_unsafe_math_optimizations)
5954 break;
5956 CASE_FLT_FN (BUILT_IN_FMOD):
5957 CASE_FLT_FN (BUILT_IN_REMAINDER):
5958 CASE_FLT_FN (BUILT_IN_DREM):
5959 CASE_FLT_FN (BUILT_IN_POW):
5960 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5961 if (target)
5962 return target;
5963 break;
5965 CASE_FLT_FN (BUILT_IN_CEXPI):
5966 target = expand_builtin_cexpi (exp, target);
5967 gcc_assert (target);
5968 return target;
5970 CASE_FLT_FN (BUILT_IN_SIN):
5971 CASE_FLT_FN (BUILT_IN_COS):
5972 if (! flag_unsafe_math_optimizations)
5973 break;
5974 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5975 if (target)
5976 return target;
5977 break;
5979 CASE_FLT_FN (BUILT_IN_SINCOS):
5980 if (! flag_unsafe_math_optimizations)
5981 break;
5982 target = expand_builtin_sincos (exp);
5983 if (target)
5984 return target;
5985 break;
5987 case BUILT_IN_APPLY_ARGS:
5988 return expand_builtin_apply_args ();
5990 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5991 FUNCTION with a copy of the parameters described by
5992 ARGUMENTS, and ARGSIZE. It returns a block of memory
5993 allocated on the stack into which is stored all the registers
5994 that might possibly be used for returning the result of a
5995 function. ARGUMENTS is the value returned by
5996 __builtin_apply_args. ARGSIZE is the number of bytes of
5997 arguments that must be copied. ??? How should this value be
5998 computed? We'll also need a safe worst case value for varargs
5999 functions. */
6000 case BUILT_IN_APPLY:
6001 if (!validate_arglist (exp, POINTER_TYPE,
6002 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6003 && !validate_arglist (exp, REFERENCE_TYPE,
6004 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6005 return const0_rtx;
6006 else
6008 rtx ops[3];
6010 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6011 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6012 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6014 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6017 /* __builtin_return (RESULT) causes the function to return the
6018 value described by RESULT. RESULT is address of the block of
6019 memory returned by __builtin_apply. */
6020 case BUILT_IN_RETURN:
6021 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6022 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6023 return const0_rtx;
6025 case BUILT_IN_SAVEREGS:
6026 return expand_builtin_saveregs ();
6028 case BUILT_IN_VA_ARG_PACK:
6029 /* All valid uses of __builtin_va_arg_pack () are removed during
6030 inlining. */
6031 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6032 return const0_rtx;
6034 case BUILT_IN_VA_ARG_PACK_LEN:
6035 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6036 inlining. */
6037 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6038 return const0_rtx;
6040 /* Return the address of the first anonymous stack arg. */
6041 case BUILT_IN_NEXT_ARG:
6042 if (fold_builtin_next_arg (exp, false))
6043 return const0_rtx;
6044 return expand_builtin_next_arg ();
6046 case BUILT_IN_CLEAR_CACHE:
6047 target = expand_builtin___clear_cache (exp);
6048 if (target)
6049 return target;
6050 break;
6052 case BUILT_IN_CLASSIFY_TYPE:
6053 return expand_builtin_classify_type (exp);
6055 case BUILT_IN_CONSTANT_P:
6056 return const0_rtx;
6058 case BUILT_IN_FRAME_ADDRESS:
6059 case BUILT_IN_RETURN_ADDRESS:
6060 return expand_builtin_frame_address (fndecl, exp);
6062 /* Returns the address of the area where the structure is returned.
6063 0 otherwise. */
6064 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6065 if (call_expr_nargs (exp) != 0
6066 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6067 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6068 return const0_rtx;
6069 else
6070 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6072 case BUILT_IN_ALLOCA:
6073 case BUILT_IN_ALLOCA_WITH_ALIGN:
6074 /* If the allocation stems from the declaration of a variable-sized
6075 object, it cannot accumulate. */
6076 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6077 if (target)
6078 return target;
6079 break;
6081 case BUILT_IN_STACK_SAVE:
6082 return expand_stack_save ();
6084 case BUILT_IN_STACK_RESTORE:
6085 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6086 return const0_rtx;
6088 case BUILT_IN_BSWAP16:
6089 case BUILT_IN_BSWAP32:
6090 case BUILT_IN_BSWAP64:
6091 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6092 if (target)
6093 return target;
6094 break;
6096 CASE_INT_FN (BUILT_IN_FFS):
6097 case BUILT_IN_FFSIMAX:
6098 target = expand_builtin_unop (target_mode, exp, target,
6099 subtarget, ffs_optab);
6100 if (target)
6101 return target;
6102 break;
6104 CASE_INT_FN (BUILT_IN_CLZ):
6105 case BUILT_IN_CLZIMAX:
6106 target = expand_builtin_unop (target_mode, exp, target,
6107 subtarget, clz_optab);
6108 if (target)
6109 return target;
6110 break;
6112 CASE_INT_FN (BUILT_IN_CTZ):
6113 case BUILT_IN_CTZIMAX:
6114 target = expand_builtin_unop (target_mode, exp, target,
6115 subtarget, ctz_optab);
6116 if (target)
6117 return target;
6118 break;
6120 CASE_INT_FN (BUILT_IN_CLRSB):
6121 case BUILT_IN_CLRSBIMAX:
6122 target = expand_builtin_unop (target_mode, exp, target,
6123 subtarget, clrsb_optab);
6124 if (target)
6125 return target;
6126 break;
6128 CASE_INT_FN (BUILT_IN_POPCOUNT):
6129 case BUILT_IN_POPCOUNTIMAX:
6130 target = expand_builtin_unop (target_mode, exp, target,
6131 subtarget, popcount_optab);
6132 if (target)
6133 return target;
6134 break;
6136 CASE_INT_FN (BUILT_IN_PARITY):
6137 case BUILT_IN_PARITYIMAX:
6138 target = expand_builtin_unop (target_mode, exp, target,
6139 subtarget, parity_optab);
6140 if (target)
6141 return target;
6142 break;
6144 case BUILT_IN_STRLEN:
6145 target = expand_builtin_strlen (exp, target, target_mode);
6146 if (target)
6147 return target;
6148 break;
6150 case BUILT_IN_STRCPY:
6151 target = expand_builtin_strcpy (exp, target);
6152 if (target)
6153 return target;
6154 break;
6156 case BUILT_IN_STRNCPY:
6157 target = expand_builtin_strncpy (exp, target);
6158 if (target)
6159 return target;
6160 break;
6162 case BUILT_IN_STPCPY:
6163 target = expand_builtin_stpcpy (exp, target, mode);
6164 if (target)
6165 return target;
6166 break;
6168 case BUILT_IN_MEMCPY:
6169 target = expand_builtin_memcpy (exp, target);
6170 if (target)
6171 return target;
6172 break;
6174 case BUILT_IN_MEMPCPY:
6175 target = expand_builtin_mempcpy (exp, target, mode);
6176 if (target)
6177 return target;
6178 break;
6180 case BUILT_IN_MEMSET:
6181 target = expand_builtin_memset (exp, target, mode);
6182 if (target)
6183 return target;
6184 break;
6186 case BUILT_IN_BZERO:
6187 target = expand_builtin_bzero (exp);
6188 if (target)
6189 return target;
6190 break;
6192 case BUILT_IN_STRCMP:
6193 target = expand_builtin_strcmp (exp, target);
6194 if (target)
6195 return target;
6196 break;
6198 case BUILT_IN_STRNCMP:
6199 target = expand_builtin_strncmp (exp, target, mode);
6200 if (target)
6201 return target;
6202 break;
6204 case BUILT_IN_BCMP:
6205 case BUILT_IN_MEMCMP:
6206 target = expand_builtin_memcmp (exp, target, mode);
6207 if (target)
6208 return target;
6209 break;
6211 case BUILT_IN_SETJMP:
6212 /* This should have been lowered to the builtins below. */
6213 gcc_unreachable ();
6215 case BUILT_IN_SETJMP_SETUP:
6216 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6217 and the receiver label. */
6218 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6220 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6221 VOIDmode, EXPAND_NORMAL);
6222 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6223 rtx label_r = label_rtx (label);
6225 /* This is copied from the handling of non-local gotos. */
6226 expand_builtin_setjmp_setup (buf_addr, label_r);
6227 nonlocal_goto_handler_labels
6228 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6229 nonlocal_goto_handler_labels);
6230 /* ??? Do not let expand_label treat us as such since we would
6231 not want to be both on the list of non-local labels and on
6232 the list of forced labels. */
6233 FORCED_LABEL (label) = 0;
6234 return const0_rtx;
6236 break;
6238 case BUILT_IN_SETJMP_DISPATCHER:
6239 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6240 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6242 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6243 rtx label_r = label_rtx (label);
6245 /* Remove the dispatcher label from the list of non-local labels
6246 since the receiver labels have been added to it above. */
6247 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6248 return const0_rtx;
6250 break;
6252 case BUILT_IN_SETJMP_RECEIVER:
6253 /* __builtin_setjmp_receiver is passed the receiver label. */
6254 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6256 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6257 rtx label_r = label_rtx (label);
6259 expand_builtin_setjmp_receiver (label_r);
6260 return const0_rtx;
6262 break;
6264 /* __builtin_longjmp is passed a pointer to an array of five words.
6265 It's similar to the C library longjmp function but works with
6266 __builtin_setjmp above. */
6267 case BUILT_IN_LONGJMP:
6268 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6270 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6271 VOIDmode, EXPAND_NORMAL);
6272 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6274 if (value != const1_rtx)
6276 error ("%<__builtin_longjmp%> second argument must be 1");
6277 return const0_rtx;
6280 expand_builtin_longjmp (buf_addr, value);
6281 return const0_rtx;
6283 break;
6285 case BUILT_IN_NONLOCAL_GOTO:
6286 target = expand_builtin_nonlocal_goto (exp);
6287 if (target)
6288 return target;
6289 break;
6291 /* This updates the setjmp buffer that is its argument with the value
6292 of the current stack pointer. */
6293 case BUILT_IN_UPDATE_SETJMP_BUF:
6294 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6296 rtx buf_addr
6297 = expand_normal (CALL_EXPR_ARG (exp, 0));
6299 expand_builtin_update_setjmp_buf (buf_addr);
6300 return const0_rtx;
6302 break;
6304 case BUILT_IN_TRAP:
6305 expand_builtin_trap ();
6306 return const0_rtx;
6308 case BUILT_IN_UNREACHABLE:
6309 expand_builtin_unreachable ();
6310 return const0_rtx;
6312 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6313 case BUILT_IN_SIGNBITD32:
6314 case BUILT_IN_SIGNBITD64:
6315 case BUILT_IN_SIGNBITD128:
6316 target = expand_builtin_signbit (exp, target);
6317 if (target)
6318 return target;
6319 break;
6321 /* Various hooks for the DWARF 2 __throw routine. */
6322 case BUILT_IN_UNWIND_INIT:
6323 expand_builtin_unwind_init ();
6324 return const0_rtx;
6325 case BUILT_IN_DWARF_CFA:
6326 return virtual_cfa_rtx;
6327 #ifdef DWARF2_UNWIND_INFO
6328 case BUILT_IN_DWARF_SP_COLUMN:
6329 return expand_builtin_dwarf_sp_column ();
6330 case BUILT_IN_INIT_DWARF_REG_SIZES:
6331 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6332 return const0_rtx;
6333 #endif
6334 case BUILT_IN_FROB_RETURN_ADDR:
6335 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6336 case BUILT_IN_EXTRACT_RETURN_ADDR:
6337 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6338 case BUILT_IN_EH_RETURN:
6339 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6340 CALL_EXPR_ARG (exp, 1));
6341 return const0_rtx;
6342 #ifdef EH_RETURN_DATA_REGNO
6343 case BUILT_IN_EH_RETURN_DATA_REGNO:
6344 return expand_builtin_eh_return_data_regno (exp);
6345 #endif
6346 case BUILT_IN_EXTEND_POINTER:
6347 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6348 case BUILT_IN_EH_POINTER:
6349 return expand_builtin_eh_pointer (exp);
6350 case BUILT_IN_EH_FILTER:
6351 return expand_builtin_eh_filter (exp);
6352 case BUILT_IN_EH_COPY_VALUES:
6353 return expand_builtin_eh_copy_values (exp);
6355 case BUILT_IN_VA_START:
6356 return expand_builtin_va_start (exp);
6357 case BUILT_IN_VA_END:
6358 return expand_builtin_va_end (exp);
6359 case BUILT_IN_VA_COPY:
6360 return expand_builtin_va_copy (exp);
6361 case BUILT_IN_EXPECT:
6362 return expand_builtin_expect (exp, target);
6363 case BUILT_IN_ASSUME_ALIGNED:
6364 return expand_builtin_assume_aligned (exp, target);
6365 case BUILT_IN_PREFETCH:
6366 expand_builtin_prefetch (exp);
6367 return const0_rtx;
6369 case BUILT_IN_INIT_TRAMPOLINE:
6370 return expand_builtin_init_trampoline (exp, true);
6371 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6372 return expand_builtin_init_trampoline (exp, false);
6373 case BUILT_IN_ADJUST_TRAMPOLINE:
6374 return expand_builtin_adjust_trampoline (exp);
6376 case BUILT_IN_FORK:
6377 case BUILT_IN_EXECL:
6378 case BUILT_IN_EXECV:
6379 case BUILT_IN_EXECLP:
6380 case BUILT_IN_EXECLE:
6381 case BUILT_IN_EXECVP:
6382 case BUILT_IN_EXECVE:
6383 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6384 if (target)
6385 return target;
6386 break;
6388 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6389 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6390 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6391 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6392 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6393 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6394 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6395 if (target)
6396 return target;
6397 break;
6399 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6400 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6401 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6402 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6403 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6405 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6406 if (target)
6407 return target;
6408 break;
6410 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6411 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6412 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6413 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6414 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6415 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6416 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6417 if (target)
6418 return target;
6419 break;
6421 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6422 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6423 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6424 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6425 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6426 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6427 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6428 if (target)
6429 return target;
6430 break;
6432 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6433 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6434 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6435 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6436 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6437 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6438 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6439 if (target)
6440 return target;
6441 break;
6443 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6444 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6445 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6446 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6447 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6448 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6449 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6450 if (target)
6451 return target;
6452 break;
6454 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6455 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6456 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6457 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6458 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6459 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6460 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6461 if (target)
6462 return target;
6463 break;
6465 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6466 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6467 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6468 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6469 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6470 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6471 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6472 if (target)
6473 return target;
6474 break;
6476 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6477 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6478 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6479 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6480 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6481 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6482 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6483 if (target)
6484 return target;
6485 break;
6487 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6488 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6489 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6490 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6491 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6492 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6493 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6494 if (target)
6495 return target;
6496 break;
6498 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6499 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6500 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6501 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6502 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6503 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6504 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6505 if (target)
6506 return target;
6507 break;
6509 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6510 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6511 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6512 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6513 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6515 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6516 if (target)
6517 return target;
6518 break;
6520 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6521 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6522 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6523 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6524 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6525 if (mode == VOIDmode)
6526 mode = TYPE_MODE (boolean_type_node);
6527 if (!target || !register_operand (target, mode))
6528 target = gen_reg_rtx (mode);
6530 mode = get_builtin_sync_mode
6531 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6532 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6533 if (target)
6534 return target;
6535 break;
6537 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6538 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6539 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6540 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6541 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6542 mode = get_builtin_sync_mode
6543 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6544 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6545 if (target)
6546 return target;
6547 break;
6549 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6550 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6551 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6552 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6553 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6554 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6555 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6556 if (target)
6557 return target;
6558 break;
6560 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6561 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6562 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6563 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6564 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6565 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6566 expand_builtin_sync_lock_release (mode, exp);
6567 return const0_rtx;
6569 case BUILT_IN_SYNC_SYNCHRONIZE:
6570 expand_builtin_sync_synchronize ();
6571 return const0_rtx;
6573 case BUILT_IN_ATOMIC_EXCHANGE_1:
6574 case BUILT_IN_ATOMIC_EXCHANGE_2:
6575 case BUILT_IN_ATOMIC_EXCHANGE_4:
6576 case BUILT_IN_ATOMIC_EXCHANGE_8:
6577 case BUILT_IN_ATOMIC_EXCHANGE_16:
6578 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6579 target = expand_builtin_atomic_exchange (mode, exp, target);
6580 if (target)
6581 return target;
6582 break;
6584 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6585 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6586 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6587 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6588 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6590 unsigned int nargs, z;
6591 vec<tree, va_gc> *vec;
6593 mode =
6594 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6595 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6596 if (target)
6597 return target;
6599 /* If this is turned into an external library call, the weak parameter
6600 must be dropped to match the expected parameter list. */
6601 nargs = call_expr_nargs (exp);
6602 vec_alloc (vec, nargs - 1);
6603 for (z = 0; z < 3; z++)
6604 vec->quick_push (CALL_EXPR_ARG (exp, z));
6605 /* Skip the boolean weak parameter. */
6606 for (z = 4; z < 6; z++)
6607 vec->quick_push (CALL_EXPR_ARG (exp, z));
6608 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6609 break;
6612 case BUILT_IN_ATOMIC_LOAD_1:
6613 case BUILT_IN_ATOMIC_LOAD_2:
6614 case BUILT_IN_ATOMIC_LOAD_4:
6615 case BUILT_IN_ATOMIC_LOAD_8:
6616 case BUILT_IN_ATOMIC_LOAD_16:
6617 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6618 target = expand_builtin_atomic_load (mode, exp, target);
6619 if (target)
6620 return target;
6621 break;
6623 case BUILT_IN_ATOMIC_STORE_1:
6624 case BUILT_IN_ATOMIC_STORE_2:
6625 case BUILT_IN_ATOMIC_STORE_4:
6626 case BUILT_IN_ATOMIC_STORE_8:
6627 case BUILT_IN_ATOMIC_STORE_16:
6628 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6629 target = expand_builtin_atomic_store (mode, exp);
6630 if (target)
6631 return const0_rtx;
6632 break;
6634 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6635 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6636 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6637 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6638 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6640 enum built_in_function lib;
6641 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6642 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6643 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6644 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6645 ignore, lib);
6646 if (target)
6647 return target;
6648 break;
6650 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6651 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6652 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6653 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6654 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6656 enum built_in_function lib;
6657 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6658 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6659 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6660 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6661 ignore, lib);
6662 if (target)
6663 return target;
6664 break;
6666 case BUILT_IN_ATOMIC_AND_FETCH_1:
6667 case BUILT_IN_ATOMIC_AND_FETCH_2:
6668 case BUILT_IN_ATOMIC_AND_FETCH_4:
6669 case BUILT_IN_ATOMIC_AND_FETCH_8:
6670 case BUILT_IN_ATOMIC_AND_FETCH_16:
6672 enum built_in_function lib;
6673 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6674 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6675 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6676 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6677 ignore, lib);
6678 if (target)
6679 return target;
6680 break;
6682 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6683 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6684 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6685 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6686 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6688 enum built_in_function lib;
6689 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6690 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6691 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6692 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6693 ignore, lib);
6694 if (target)
6695 return target;
6696 break;
6698 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6699 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6700 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6701 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6702 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6704 enum built_in_function lib;
6705 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6706 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6707 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6708 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6709 ignore, lib);
6710 if (target)
6711 return target;
6712 break;
6714 case BUILT_IN_ATOMIC_OR_FETCH_1:
6715 case BUILT_IN_ATOMIC_OR_FETCH_2:
6716 case BUILT_IN_ATOMIC_OR_FETCH_4:
6717 case BUILT_IN_ATOMIC_OR_FETCH_8:
6718 case BUILT_IN_ATOMIC_OR_FETCH_16:
6720 enum built_in_function lib;
6721 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6722 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6723 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6724 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6725 ignore, lib);
6726 if (target)
6727 return target;
6728 break;
6730 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6731 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6732 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6733 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6734 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6735 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6736 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6737 ignore, BUILT_IN_NONE);
6738 if (target)
6739 return target;
6740 break;
6742 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6743 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6744 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6745 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6746 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6747 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6748 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6749 ignore, BUILT_IN_NONE);
6750 if (target)
6751 return target;
6752 break;
6754 case BUILT_IN_ATOMIC_FETCH_AND_1:
6755 case BUILT_IN_ATOMIC_FETCH_AND_2:
6756 case BUILT_IN_ATOMIC_FETCH_AND_4:
6757 case BUILT_IN_ATOMIC_FETCH_AND_8:
6758 case BUILT_IN_ATOMIC_FETCH_AND_16:
6759 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6760 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6761 ignore, BUILT_IN_NONE);
6762 if (target)
6763 return target;
6764 break;
6766 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6767 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6768 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6769 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6770 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6771 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6772 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6773 ignore, BUILT_IN_NONE);
6774 if (target)
6775 return target;
6776 break;
6778 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6779 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6780 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6781 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6782 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6783 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6784 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6785 ignore, BUILT_IN_NONE);
6786 if (target)
6787 return target;
6788 break;
6790 case BUILT_IN_ATOMIC_FETCH_OR_1:
6791 case BUILT_IN_ATOMIC_FETCH_OR_2:
6792 case BUILT_IN_ATOMIC_FETCH_OR_4:
6793 case BUILT_IN_ATOMIC_FETCH_OR_8:
6794 case BUILT_IN_ATOMIC_FETCH_OR_16:
6795 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6796 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6797 ignore, BUILT_IN_NONE);
6798 if (target)
6799 return target;
6800 break;
6802 case BUILT_IN_ATOMIC_TEST_AND_SET:
6803 return expand_builtin_atomic_test_and_set (exp, target);
6805 case BUILT_IN_ATOMIC_CLEAR:
6806 return expand_builtin_atomic_clear (exp);
6808 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6809 return expand_builtin_atomic_always_lock_free (exp);
6811 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6812 target = expand_builtin_atomic_is_lock_free (exp);
6813 if (target)
6814 return target;
6815 break;
6817 case BUILT_IN_ATOMIC_THREAD_FENCE:
6818 expand_builtin_atomic_thread_fence (exp);
6819 return const0_rtx;
6821 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6822 expand_builtin_atomic_signal_fence (exp);
6823 return const0_rtx;
6825 case BUILT_IN_OBJECT_SIZE:
6826 return expand_builtin_object_size (exp);
6828 case BUILT_IN_MEMCPY_CHK:
6829 case BUILT_IN_MEMPCPY_CHK:
6830 case BUILT_IN_MEMMOVE_CHK:
6831 case BUILT_IN_MEMSET_CHK:
6832 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6833 if (target)
6834 return target;
6835 break;
6837 case BUILT_IN_STRCPY_CHK:
6838 case BUILT_IN_STPCPY_CHK:
6839 case BUILT_IN_STRNCPY_CHK:
6840 case BUILT_IN_STPNCPY_CHK:
6841 case BUILT_IN_STRCAT_CHK:
6842 case BUILT_IN_STRNCAT_CHK:
6843 case BUILT_IN_SNPRINTF_CHK:
6844 case BUILT_IN_VSNPRINTF_CHK:
6845 maybe_emit_chk_warning (exp, fcode);
6846 break;
6848 case BUILT_IN_SPRINTF_CHK:
6849 case BUILT_IN_VSPRINTF_CHK:
6850 maybe_emit_sprintf_chk_warning (exp, fcode);
6851 break;
6853 case BUILT_IN_FREE:
6854 if (warn_free_nonheap_object)
6855 maybe_emit_free_warning (exp);
6856 break;
6858 case BUILT_IN_THREAD_POINTER:
6859 return expand_builtin_thread_pointer (exp, target);
6861 case BUILT_IN_SET_THREAD_POINTER:
6862 expand_builtin_set_thread_pointer (exp);
6863 return const0_rtx;
6865 default: /* just do library call, if unknown builtin */
6866 break;
6869 /* The switch statement above can drop through to cause the function
6870 to be called normally. */
6871 return expand_call (exp, target, ignore);
6874 /* Determine whether a tree node represents a call to a built-in
6875 function. If the tree T is a call to a built-in function with
6876 the right number of arguments of the appropriate types, return
6877 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6878 Otherwise the return value is END_BUILTINS. */
6880 enum built_in_function
6881 builtin_mathfn_code (const_tree t)
6883 const_tree fndecl, arg, parmlist;
6884 const_tree argtype, parmtype;
6885 const_call_expr_arg_iterator iter;
6887 if (TREE_CODE (t) != CALL_EXPR
6888 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6889 return END_BUILTINS;
6891 fndecl = get_callee_fndecl (t);
6892 if (fndecl == NULL_TREE
6893 || TREE_CODE (fndecl) != FUNCTION_DECL
6894 || ! DECL_BUILT_IN (fndecl)
6895 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6896 return END_BUILTINS;
6898 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6899 init_const_call_expr_arg_iterator (t, &iter);
6900 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6902 /* If a function doesn't take a variable number of arguments,
6903 the last element in the list will have type `void'. */
6904 parmtype = TREE_VALUE (parmlist);
6905 if (VOID_TYPE_P (parmtype))
6907 if (more_const_call_expr_args_p (&iter))
6908 return END_BUILTINS;
6909 return DECL_FUNCTION_CODE (fndecl);
6912 if (! more_const_call_expr_args_p (&iter))
6913 return END_BUILTINS;
6915 arg = next_const_call_expr_arg (&iter);
6916 argtype = TREE_TYPE (arg);
6918 if (SCALAR_FLOAT_TYPE_P (parmtype))
6920 if (! SCALAR_FLOAT_TYPE_P (argtype))
6921 return END_BUILTINS;
6923 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6925 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6926 return END_BUILTINS;
6928 else if (POINTER_TYPE_P (parmtype))
6930 if (! POINTER_TYPE_P (argtype))
6931 return END_BUILTINS;
6933 else if (INTEGRAL_TYPE_P (parmtype))
6935 if (! INTEGRAL_TYPE_P (argtype))
6936 return END_BUILTINS;
6938 else
6939 return END_BUILTINS;
6942 /* Variable-length argument list. */
6943 return DECL_FUNCTION_CODE (fndecl);
6946 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6947 evaluate to a constant. */
6949 static tree
6950 fold_builtin_constant_p (tree arg)
6952 /* We return 1 for a numeric type that's known to be a constant
6953 value at compile-time or for an aggregate type that's a
6954 literal constant. */
6955 STRIP_NOPS (arg);
6957 /* If we know this is a constant, emit the constant of one. */
6958 if (CONSTANT_CLASS_P (arg)
6959 || (TREE_CODE (arg) == CONSTRUCTOR
6960 && TREE_CONSTANT (arg)))
6961 return integer_one_node;
6962 if (TREE_CODE (arg) == ADDR_EXPR)
6964 tree op = TREE_OPERAND (arg, 0);
6965 if (TREE_CODE (op) == STRING_CST
6966 || (TREE_CODE (op) == ARRAY_REF
6967 && integer_zerop (TREE_OPERAND (op, 1))
6968 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6969 return integer_one_node;
6972 /* If this expression has side effects, show we don't know it to be a
6973 constant. Likewise if it's a pointer or aggregate type since in
6974 those case we only want literals, since those are only optimized
6975 when generating RTL, not later.
6976 And finally, if we are compiling an initializer, not code, we
6977 need to return a definite result now; there's not going to be any
6978 more optimization done. */
6979 if (TREE_SIDE_EFFECTS (arg)
6980 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6981 || POINTER_TYPE_P (TREE_TYPE (arg))
6982 || cfun == 0
6983 || folding_initializer)
6984 return integer_zero_node;
6986 return NULL_TREE;
6989 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6990 return it as a truthvalue. */
6992 static tree
6993 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6995 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6997 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6998 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6999 ret_type = TREE_TYPE (TREE_TYPE (fn));
7000 pred_type = TREE_VALUE (arg_types);
7001 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7003 pred = fold_convert_loc (loc, pred_type, pred);
7004 expected = fold_convert_loc (loc, expected_type, expected);
7005 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
7007 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7008 build_int_cst (ret_type, 0));
7011 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7012 NULL_TREE if no simplification is possible. */
7014 static tree
7015 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7017 tree inner, fndecl, inner_arg0;
7018 enum tree_code code;
7020 /* Distribute the expected value over short-circuiting operators.
7021 See through the cast from truthvalue_type_node to long. */
7022 inner_arg0 = arg0;
7023 while (TREE_CODE (inner_arg0) == NOP_EXPR
7024 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7025 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7026 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7028 /* If this is a builtin_expect within a builtin_expect keep the
7029 inner one. See through a comparison against a constant. It
7030 might have been added to create a thruthvalue. */
7031 inner = inner_arg0;
7033 if (COMPARISON_CLASS_P (inner)
7034 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7035 inner = TREE_OPERAND (inner, 0);
7037 if (TREE_CODE (inner) == CALL_EXPR
7038 && (fndecl = get_callee_fndecl (inner))
7039 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7040 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7041 return arg0;
7043 inner = inner_arg0;
7044 code = TREE_CODE (inner);
7045 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7047 tree op0 = TREE_OPERAND (inner, 0);
7048 tree op1 = TREE_OPERAND (inner, 1);
7050 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7051 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7052 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7054 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7057 /* If the argument isn't invariant then there's nothing else we can do. */
7058 if (!TREE_CONSTANT (inner_arg0))
7059 return NULL_TREE;
7061 /* If we expect that a comparison against the argument will fold to
7062 a constant return the constant. In practice, this means a true
7063 constant or the address of a non-weak symbol. */
7064 inner = inner_arg0;
7065 STRIP_NOPS (inner);
7066 if (TREE_CODE (inner) == ADDR_EXPR)
7070 inner = TREE_OPERAND (inner, 0);
7072 while (TREE_CODE (inner) == COMPONENT_REF
7073 || TREE_CODE (inner) == ARRAY_REF);
7074 if ((TREE_CODE (inner) == VAR_DECL
7075 || TREE_CODE (inner) == FUNCTION_DECL)
7076 && DECL_WEAK (inner))
7077 return NULL_TREE;
7080 /* Otherwise, ARG0 already has the proper type for the return value. */
7081 return arg0;
7084 /* Fold a call to __builtin_classify_type with argument ARG. */
7086 static tree
7087 fold_builtin_classify_type (tree arg)
7089 if (arg == 0)
7090 return build_int_cst (integer_type_node, no_type_class);
7092 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7095 /* Fold a call to __builtin_strlen with argument ARG. */
7097 static tree
7098 fold_builtin_strlen (location_t loc, tree type, tree arg)
7100 if (!validate_arg (arg, POINTER_TYPE))
7101 return NULL_TREE;
7102 else
7104 tree len = c_strlen (arg, 0);
7106 if (len)
7107 return fold_convert_loc (loc, type, len);
7109 return NULL_TREE;
7113 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7115 static tree
7116 fold_builtin_inf (location_t loc, tree type, int warn)
7118 REAL_VALUE_TYPE real;
7120 /* __builtin_inff is intended to be usable to define INFINITY on all
7121 targets. If an infinity is not available, INFINITY expands "to a
7122 positive constant of type float that overflows at translation
7123 time", footnote "In this case, using INFINITY will violate the
7124 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7125 Thus we pedwarn to ensure this constraint violation is
7126 diagnosed. */
7127 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7128 pedwarn (loc, 0, "target format does not support infinity");
7130 real_inf (&real);
7131 return build_real (type, real);
7134 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7136 static tree
7137 fold_builtin_nan (tree arg, tree type, int quiet)
7139 REAL_VALUE_TYPE real;
7140 const char *str;
7142 if (!validate_arg (arg, POINTER_TYPE))
7143 return NULL_TREE;
7144 str = c_getstr (arg);
7145 if (!str)
7146 return NULL_TREE;
7148 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7149 return NULL_TREE;
7151 return build_real (type, real);
7154 /* Return true if the floating point expression T has an integer value.
7155 We also allow +Inf, -Inf and NaN to be considered integer values. */
7157 static bool
7158 integer_valued_real_p (tree t)
7160 switch (TREE_CODE (t))
7162 case FLOAT_EXPR:
7163 return true;
7165 case ABS_EXPR:
7166 case SAVE_EXPR:
7167 return integer_valued_real_p (TREE_OPERAND (t, 0));
7169 case COMPOUND_EXPR:
7170 case MODIFY_EXPR:
7171 case BIND_EXPR:
7172 return integer_valued_real_p (TREE_OPERAND (t, 1));
7174 case PLUS_EXPR:
7175 case MINUS_EXPR:
7176 case MULT_EXPR:
7177 case MIN_EXPR:
7178 case MAX_EXPR:
7179 return integer_valued_real_p (TREE_OPERAND (t, 0))
7180 && integer_valued_real_p (TREE_OPERAND (t, 1));
7182 case COND_EXPR:
7183 return integer_valued_real_p (TREE_OPERAND (t, 1))
7184 && integer_valued_real_p (TREE_OPERAND (t, 2));
7186 case REAL_CST:
7187 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7189 case NOP_EXPR:
7191 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7192 if (TREE_CODE (type) == INTEGER_TYPE)
7193 return true;
7194 if (TREE_CODE (type) == REAL_TYPE)
7195 return integer_valued_real_p (TREE_OPERAND (t, 0));
7196 break;
7199 case CALL_EXPR:
7200 switch (builtin_mathfn_code (t))
7202 CASE_FLT_FN (BUILT_IN_CEIL):
7203 CASE_FLT_FN (BUILT_IN_FLOOR):
7204 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7205 CASE_FLT_FN (BUILT_IN_RINT):
7206 CASE_FLT_FN (BUILT_IN_ROUND):
7207 CASE_FLT_FN (BUILT_IN_TRUNC):
7208 return true;
7210 CASE_FLT_FN (BUILT_IN_FMIN):
7211 CASE_FLT_FN (BUILT_IN_FMAX):
7212 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7213 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7215 default:
7216 break;
7218 break;
7220 default:
7221 break;
7223 return false;
7226 /* FNDECL is assumed to be a builtin where truncation can be propagated
7227 across (for instance floor((double)f) == (double)floorf (f).
7228 Do the transformation for a call with argument ARG. */
7230 static tree
7231 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7233 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7235 if (!validate_arg (arg, REAL_TYPE))
7236 return NULL_TREE;
7238 /* Integer rounding functions are idempotent. */
7239 if (fcode == builtin_mathfn_code (arg))
7240 return arg;
7242 /* If argument is already integer valued, and we don't need to worry
7243 about setting errno, there's no need to perform rounding. */
7244 if (! flag_errno_math && integer_valued_real_p (arg))
7245 return arg;
7247 if (optimize)
7249 tree arg0 = strip_float_extensions (arg);
7250 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7251 tree newtype = TREE_TYPE (arg0);
7252 tree decl;
7254 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7255 && (decl = mathfn_built_in (newtype, fcode)))
7256 return fold_convert_loc (loc, ftype,
7257 build_call_expr_loc (loc, decl, 1,
7258 fold_convert_loc (loc,
7259 newtype,
7260 arg0)));
7262 return NULL_TREE;
7265 /* FNDECL is assumed to be builtin which can narrow the FP type of
7266 the argument, for instance lround((double)f) -> lroundf (f).
7267 Do the transformation for a call with argument ARG. */
7269 static tree
7270 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7272 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7274 if (!validate_arg (arg, REAL_TYPE))
7275 return NULL_TREE;
7277 /* If argument is already integer valued, and we don't need to worry
7278 about setting errno, there's no need to perform rounding. */
7279 if (! flag_errno_math && integer_valued_real_p (arg))
7280 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7281 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7283 if (optimize)
7285 tree ftype = TREE_TYPE (arg);
7286 tree arg0 = strip_float_extensions (arg);
7287 tree newtype = TREE_TYPE (arg0);
7288 tree decl;
7290 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7291 && (decl = mathfn_built_in (newtype, fcode)))
7292 return build_call_expr_loc (loc, decl, 1,
7293 fold_convert_loc (loc, newtype, arg0));
7296 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7297 sizeof (int) == sizeof (long). */
7298 if (TYPE_PRECISION (integer_type_node)
7299 == TYPE_PRECISION (long_integer_type_node))
7301 tree newfn = NULL_TREE;
7302 switch (fcode)
7304 CASE_FLT_FN (BUILT_IN_ICEIL):
7305 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7306 break;
7308 CASE_FLT_FN (BUILT_IN_IFLOOR):
7309 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7310 break;
7312 CASE_FLT_FN (BUILT_IN_IROUND):
7313 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7314 break;
7316 CASE_FLT_FN (BUILT_IN_IRINT):
7317 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7318 break;
7320 default:
7321 break;
7324 if (newfn)
7326 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7327 return fold_convert_loc (loc,
7328 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7332 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7333 sizeof (long long) == sizeof (long). */
7334 if (TYPE_PRECISION (long_long_integer_type_node)
7335 == TYPE_PRECISION (long_integer_type_node))
7337 tree newfn = NULL_TREE;
7338 switch (fcode)
7340 CASE_FLT_FN (BUILT_IN_LLCEIL):
7341 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7342 break;
7344 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7345 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7346 break;
7348 CASE_FLT_FN (BUILT_IN_LLROUND):
7349 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7350 break;
7352 CASE_FLT_FN (BUILT_IN_LLRINT):
7353 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7354 break;
7356 default:
7357 break;
7360 if (newfn)
7362 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7363 return fold_convert_loc (loc,
7364 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7368 return NULL_TREE;
7371 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7372 return type. Return NULL_TREE if no simplification can be made. */
7374 static tree
7375 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7377 tree res;
7379 if (!validate_arg (arg, COMPLEX_TYPE)
7380 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7381 return NULL_TREE;
7383 /* Calculate the result when the argument is a constant. */
7384 if (TREE_CODE (arg) == COMPLEX_CST
7385 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7386 type, mpfr_hypot)))
7387 return res;
7389 if (TREE_CODE (arg) == COMPLEX_EXPR)
7391 tree real = TREE_OPERAND (arg, 0);
7392 tree imag = TREE_OPERAND (arg, 1);
7394 /* If either part is zero, cabs is fabs of the other. */
7395 if (real_zerop (real))
7396 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7397 if (real_zerop (imag))
7398 return fold_build1_loc (loc, ABS_EXPR, type, real);
7400 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7401 if (flag_unsafe_math_optimizations
7402 && operand_equal_p (real, imag, OEP_PURE_SAME))
7404 const REAL_VALUE_TYPE sqrt2_trunc
7405 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7406 STRIP_NOPS (real);
7407 return fold_build2_loc (loc, MULT_EXPR, type,
7408 fold_build1_loc (loc, ABS_EXPR, type, real),
7409 build_real (type, sqrt2_trunc));
7413 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7414 if (TREE_CODE (arg) == NEGATE_EXPR
7415 || TREE_CODE (arg) == CONJ_EXPR)
7416 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7418 /* Don't do this when optimizing for size. */
7419 if (flag_unsafe_math_optimizations
7420 && optimize && optimize_function_for_speed_p (cfun))
7422 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7424 if (sqrtfn != NULL_TREE)
7426 tree rpart, ipart, result;
7428 arg = builtin_save_expr (arg);
7430 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7431 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7433 rpart = builtin_save_expr (rpart);
7434 ipart = builtin_save_expr (ipart);
7436 result = fold_build2_loc (loc, PLUS_EXPR, type,
7437 fold_build2_loc (loc, MULT_EXPR, type,
7438 rpart, rpart),
7439 fold_build2_loc (loc, MULT_EXPR, type,
7440 ipart, ipart));
7442 return build_call_expr_loc (loc, sqrtfn, 1, result);
7446 return NULL_TREE;
7449 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7450 complex tree type of the result. If NEG is true, the imaginary
7451 zero is negative. */
7453 static tree
7454 build_complex_cproj (tree type, bool neg)
7456 REAL_VALUE_TYPE rinf, rzero = dconst0;
7458 real_inf (&rinf);
7459 rzero.sign = neg;
7460 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7461 build_real (TREE_TYPE (type), rzero));
7464 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7465 return type. Return NULL_TREE if no simplification can be made. */
7467 static tree
7468 fold_builtin_cproj (location_t loc, tree arg, tree type)
7470 if (!validate_arg (arg, COMPLEX_TYPE)
7471 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7472 return NULL_TREE;
7474 /* If there are no infinities, return arg. */
7475 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7476 return non_lvalue_loc (loc, arg);
7478 /* Calculate the result when the argument is a constant. */
7479 if (TREE_CODE (arg) == COMPLEX_CST)
7481 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7482 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7484 if (real_isinf (real) || real_isinf (imag))
7485 return build_complex_cproj (type, imag->sign);
7486 else
7487 return arg;
7489 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7491 tree real = TREE_OPERAND (arg, 0);
7492 tree imag = TREE_OPERAND (arg, 1);
7494 STRIP_NOPS (real);
7495 STRIP_NOPS (imag);
7497 /* If the real part is inf and the imag part is known to be
7498 nonnegative, return (inf + 0i). Remember side-effects are
7499 possible in the imag part. */
7500 if (TREE_CODE (real) == REAL_CST
7501 && real_isinf (TREE_REAL_CST_PTR (real))
7502 && tree_expr_nonnegative_p (imag))
7503 return omit_one_operand_loc (loc, type,
7504 build_complex_cproj (type, false),
7505 arg);
7507 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7508 Remember side-effects are possible in the real part. */
7509 if (TREE_CODE (imag) == REAL_CST
7510 && real_isinf (TREE_REAL_CST_PTR (imag)))
7511 return
7512 omit_one_operand_loc (loc, type,
7513 build_complex_cproj (type, TREE_REAL_CST_PTR
7514 (imag)->sign), arg);
7517 return NULL_TREE;
7520 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7521 Return NULL_TREE if no simplification can be made. */
7523 static tree
7524 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7527 enum built_in_function fcode;
7528 tree res;
7530 if (!validate_arg (arg, REAL_TYPE))
7531 return NULL_TREE;
7533 /* Calculate the result when the argument is a constant. */
7534 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7535 return res;
7537 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7538 fcode = builtin_mathfn_code (arg);
7539 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7541 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7542 arg = fold_build2_loc (loc, MULT_EXPR, type,
7543 CALL_EXPR_ARG (arg, 0),
7544 build_real (type, dconsthalf));
7545 return build_call_expr_loc (loc, expfn, 1, arg);
7548 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7549 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7551 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7553 if (powfn)
7555 tree arg0 = CALL_EXPR_ARG (arg, 0);
7556 tree tree_root;
7557 /* The inner root was either sqrt or cbrt. */
7558 /* This was a conditional expression but it triggered a bug
7559 in Sun C 5.5. */
7560 REAL_VALUE_TYPE dconstroot;
7561 if (BUILTIN_SQRT_P (fcode))
7562 dconstroot = dconsthalf;
7563 else
7564 dconstroot = dconst_third ();
7566 /* Adjust for the outer root. */
7567 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7568 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7569 tree_root = build_real (type, dconstroot);
7570 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7574 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7575 if (flag_unsafe_math_optimizations
7576 && (fcode == BUILT_IN_POW
7577 || fcode == BUILT_IN_POWF
7578 || fcode == BUILT_IN_POWL))
7580 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7581 tree arg0 = CALL_EXPR_ARG (arg, 0);
7582 tree arg1 = CALL_EXPR_ARG (arg, 1);
7583 tree narg1;
7584 if (!tree_expr_nonnegative_p (arg0))
7585 arg0 = build1 (ABS_EXPR, type, arg0);
7586 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7587 build_real (type, dconsthalf));
7588 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7591 return NULL_TREE;
7594 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7595 Return NULL_TREE if no simplification can be made. */
7597 static tree
7598 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7600 const enum built_in_function fcode = builtin_mathfn_code (arg);
7601 tree res;
7603 if (!validate_arg (arg, REAL_TYPE))
7604 return NULL_TREE;
7606 /* Calculate the result when the argument is a constant. */
7607 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7608 return res;
7610 if (flag_unsafe_math_optimizations)
7612 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7613 if (BUILTIN_EXPONENT_P (fcode))
7615 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7616 const REAL_VALUE_TYPE third_trunc =
7617 real_value_truncate (TYPE_MODE (type), dconst_third ());
7618 arg = fold_build2_loc (loc, MULT_EXPR, type,
7619 CALL_EXPR_ARG (arg, 0),
7620 build_real (type, third_trunc));
7621 return build_call_expr_loc (loc, expfn, 1, arg);
7624 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7625 if (BUILTIN_SQRT_P (fcode))
7627 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7629 if (powfn)
7631 tree arg0 = CALL_EXPR_ARG (arg, 0);
7632 tree tree_root;
7633 REAL_VALUE_TYPE dconstroot = dconst_third ();
7635 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7636 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7637 tree_root = build_real (type, dconstroot);
7638 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7642 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7643 if (BUILTIN_CBRT_P (fcode))
7645 tree arg0 = CALL_EXPR_ARG (arg, 0);
7646 if (tree_expr_nonnegative_p (arg0))
7648 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7650 if (powfn)
7652 tree tree_root;
7653 REAL_VALUE_TYPE dconstroot;
7655 real_arithmetic (&dconstroot, MULT_EXPR,
7656 dconst_third_ptr (), dconst_third_ptr ());
7657 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7658 tree_root = build_real (type, dconstroot);
7659 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7664 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7665 if (fcode == BUILT_IN_POW
7666 || fcode == BUILT_IN_POWF
7667 || fcode == BUILT_IN_POWL)
7669 tree arg00 = CALL_EXPR_ARG (arg, 0);
7670 tree arg01 = CALL_EXPR_ARG (arg, 1);
7671 if (tree_expr_nonnegative_p (arg00))
7673 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7674 const REAL_VALUE_TYPE dconstroot
7675 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7676 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7677 build_real (type, dconstroot));
7678 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7682 return NULL_TREE;
7685 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7686 TYPE is the type of the return value. Return NULL_TREE if no
7687 simplification can be made. */
7689 static tree
7690 fold_builtin_cos (location_t loc,
7691 tree arg, tree type, tree fndecl)
7693 tree res, narg;
7695 if (!validate_arg (arg, REAL_TYPE))
7696 return NULL_TREE;
7698 /* Calculate the result when the argument is a constant. */
7699 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7700 return res;
7702 /* Optimize cos(-x) into cos (x). */
7703 if ((narg = fold_strip_sign_ops (arg)))
7704 return build_call_expr_loc (loc, fndecl, 1, narg);
7706 return NULL_TREE;
7709 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7710 Return NULL_TREE if no simplification can be made. */
7712 static tree
7713 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7715 if (validate_arg (arg, REAL_TYPE))
7717 tree res, narg;
7719 /* Calculate the result when the argument is a constant. */
7720 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7721 return res;
7723 /* Optimize cosh(-x) into cosh (x). */
7724 if ((narg = fold_strip_sign_ops (arg)))
7725 return build_call_expr_loc (loc, fndecl, 1, narg);
7728 return NULL_TREE;
7731 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7732 argument ARG. TYPE is the type of the return value. Return
7733 NULL_TREE if no simplification can be made. */
7735 static tree
7736 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7737 bool hyper)
7739 if (validate_arg (arg, COMPLEX_TYPE)
7740 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7742 tree tmp;
7744 /* Calculate the result when the argument is a constant. */
7745 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7746 return tmp;
7748 /* Optimize fn(-x) into fn(x). */
7749 if ((tmp = fold_strip_sign_ops (arg)))
7750 return build_call_expr_loc (loc, fndecl, 1, tmp);
7753 return NULL_TREE;
7756 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7757 Return NULL_TREE if no simplification can be made. */
7759 static tree
7760 fold_builtin_tan (tree arg, tree type)
7762 enum built_in_function fcode;
7763 tree res;
7765 if (!validate_arg (arg, REAL_TYPE))
7766 return NULL_TREE;
7768 /* Calculate the result when the argument is a constant. */
7769 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7770 return res;
7772 /* Optimize tan(atan(x)) = x. */
7773 fcode = builtin_mathfn_code (arg);
7774 if (flag_unsafe_math_optimizations
7775 && (fcode == BUILT_IN_ATAN
7776 || fcode == BUILT_IN_ATANF
7777 || fcode == BUILT_IN_ATANL))
7778 return CALL_EXPR_ARG (arg, 0);
7780 return NULL_TREE;
7783 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7784 NULL_TREE if no simplification can be made. */
7786 static tree
7787 fold_builtin_sincos (location_t loc,
7788 tree arg0, tree arg1, tree arg2)
7790 tree type;
7791 tree res, fn, call;
7793 if (!validate_arg (arg0, REAL_TYPE)
7794 || !validate_arg (arg1, POINTER_TYPE)
7795 || !validate_arg (arg2, POINTER_TYPE))
7796 return NULL_TREE;
7798 type = TREE_TYPE (arg0);
7800 /* Calculate the result when the argument is a constant. */
7801 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7802 return res;
7804 /* Canonicalize sincos to cexpi. */
7805 if (!TARGET_C99_FUNCTIONS)
7806 return NULL_TREE;
7807 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7808 if (!fn)
7809 return NULL_TREE;
7811 call = build_call_expr_loc (loc, fn, 1, arg0);
7812 call = builtin_save_expr (call);
7814 return build2 (COMPOUND_EXPR, void_type_node,
7815 build2 (MODIFY_EXPR, void_type_node,
7816 build_fold_indirect_ref_loc (loc, arg1),
7817 build1 (IMAGPART_EXPR, type, call)),
7818 build2 (MODIFY_EXPR, void_type_node,
7819 build_fold_indirect_ref_loc (loc, arg2),
7820 build1 (REALPART_EXPR, type, call)));
7823 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7824 NULL_TREE if no simplification can be made. */
7826 static tree
7827 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7829 tree rtype;
7830 tree realp, imagp, ifn;
7831 tree res;
7833 if (!validate_arg (arg0, COMPLEX_TYPE)
7834 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7835 return NULL_TREE;
7837 /* Calculate the result when the argument is a constant. */
7838 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7839 return res;
7841 rtype = TREE_TYPE (TREE_TYPE (arg0));
7843 /* In case we can figure out the real part of arg0 and it is constant zero
7844 fold to cexpi. */
7845 if (!TARGET_C99_FUNCTIONS)
7846 return NULL_TREE;
7847 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7848 if (!ifn)
7849 return NULL_TREE;
7851 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7852 && real_zerop (realp))
7854 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7855 return build_call_expr_loc (loc, ifn, 1, narg);
7858 /* In case we can easily decompose real and imaginary parts split cexp
7859 to exp (r) * cexpi (i). */
7860 if (flag_unsafe_math_optimizations
7861 && realp)
7863 tree rfn, rcall, icall;
7865 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7866 if (!rfn)
7867 return NULL_TREE;
7869 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7870 if (!imagp)
7871 return NULL_TREE;
7873 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7874 icall = builtin_save_expr (icall);
7875 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7876 rcall = builtin_save_expr (rcall);
7877 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7878 fold_build2_loc (loc, MULT_EXPR, rtype,
7879 rcall,
7880 fold_build1_loc (loc, REALPART_EXPR,
7881 rtype, icall)),
7882 fold_build2_loc (loc, MULT_EXPR, rtype,
7883 rcall,
7884 fold_build1_loc (loc, IMAGPART_EXPR,
7885 rtype, icall)));
7888 return NULL_TREE;
7891 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7892 Return NULL_TREE if no simplification can be made. */
7894 static tree
7895 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7897 if (!validate_arg (arg, REAL_TYPE))
7898 return NULL_TREE;
7900 /* Optimize trunc of constant value. */
7901 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7903 REAL_VALUE_TYPE r, x;
7904 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7906 x = TREE_REAL_CST (arg);
7907 real_trunc (&r, TYPE_MODE (type), &x);
7908 return build_real (type, r);
7911 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7914 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7915 Return NULL_TREE if no simplification can be made. */
7917 static tree
7918 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7920 if (!validate_arg (arg, REAL_TYPE))
7921 return NULL_TREE;
7923 /* Optimize floor of constant value. */
7924 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7926 REAL_VALUE_TYPE x;
7928 x = TREE_REAL_CST (arg);
7929 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7931 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7932 REAL_VALUE_TYPE r;
7934 real_floor (&r, TYPE_MODE (type), &x);
7935 return build_real (type, r);
7939 /* Fold floor (x) where x is nonnegative to trunc (x). */
7940 if (tree_expr_nonnegative_p (arg))
7942 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7943 if (truncfn)
7944 return build_call_expr_loc (loc, truncfn, 1, arg);
7947 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7950 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7951 Return NULL_TREE if no simplification can be made. */
7953 static tree
7954 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7956 if (!validate_arg (arg, REAL_TYPE))
7957 return NULL_TREE;
7959 /* Optimize ceil of constant value. */
7960 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7962 REAL_VALUE_TYPE x;
7964 x = TREE_REAL_CST (arg);
7965 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7967 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7968 REAL_VALUE_TYPE r;
7970 real_ceil (&r, TYPE_MODE (type), &x);
7971 return build_real (type, r);
7975 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7978 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7979 Return NULL_TREE if no simplification can be made. */
7981 static tree
7982 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7984 if (!validate_arg (arg, REAL_TYPE))
7985 return NULL_TREE;
7987 /* Optimize round of constant value. */
7988 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7990 REAL_VALUE_TYPE x;
7992 x = TREE_REAL_CST (arg);
7993 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7995 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7996 REAL_VALUE_TYPE r;
7998 real_round (&r, TYPE_MODE (type), &x);
7999 return build_real (type, r);
8003 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8006 /* Fold function call to builtin lround, lroundf or lroundl (or the
8007 corresponding long long versions) and other rounding functions. ARG
8008 is the argument to the call. Return NULL_TREE if no simplification
8009 can be made. */
8011 static tree
8012 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8014 if (!validate_arg (arg, REAL_TYPE))
8015 return NULL_TREE;
8017 /* Optimize lround of constant value. */
8018 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8020 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8022 if (real_isfinite (&x))
8024 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8025 tree ftype = TREE_TYPE (arg);
8026 double_int val;
8027 REAL_VALUE_TYPE r;
8029 switch (DECL_FUNCTION_CODE (fndecl))
8031 CASE_FLT_FN (BUILT_IN_IFLOOR):
8032 CASE_FLT_FN (BUILT_IN_LFLOOR):
8033 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8034 real_floor (&r, TYPE_MODE (ftype), &x);
8035 break;
8037 CASE_FLT_FN (BUILT_IN_ICEIL):
8038 CASE_FLT_FN (BUILT_IN_LCEIL):
8039 CASE_FLT_FN (BUILT_IN_LLCEIL):
8040 real_ceil (&r, TYPE_MODE (ftype), &x);
8041 break;
8043 CASE_FLT_FN (BUILT_IN_IROUND):
8044 CASE_FLT_FN (BUILT_IN_LROUND):
8045 CASE_FLT_FN (BUILT_IN_LLROUND):
8046 real_round (&r, TYPE_MODE (ftype), &x);
8047 break;
8049 default:
8050 gcc_unreachable ();
8053 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8054 if (double_int_fits_to_tree_p (itype, val))
8055 return double_int_to_tree (itype, val);
8059 switch (DECL_FUNCTION_CODE (fndecl))
8061 CASE_FLT_FN (BUILT_IN_LFLOOR):
8062 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8063 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8064 if (tree_expr_nonnegative_p (arg))
8065 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8066 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8067 break;
8068 default:;
8071 return fold_fixed_mathfn (loc, fndecl, arg);
8074 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8075 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8076 the argument to the call. Return NULL_TREE if no simplification can
8077 be made. */
8079 static tree
8080 fold_builtin_bitop (tree fndecl, tree arg)
8082 if (!validate_arg (arg, INTEGER_TYPE))
8083 return NULL_TREE;
8085 /* Optimize for constant argument. */
8086 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8088 HOST_WIDE_INT hi, width, result;
8089 unsigned HOST_WIDE_INT lo;
8090 tree type;
8092 type = TREE_TYPE (arg);
8093 width = TYPE_PRECISION (type);
8094 lo = TREE_INT_CST_LOW (arg);
8096 /* Clear all the bits that are beyond the type's precision. */
8097 if (width > HOST_BITS_PER_WIDE_INT)
8099 hi = TREE_INT_CST_HIGH (arg);
8100 if (width < HOST_BITS_PER_DOUBLE_INT)
8101 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8102 << (width - HOST_BITS_PER_WIDE_INT));
8104 else
8106 hi = 0;
8107 if (width < HOST_BITS_PER_WIDE_INT)
8108 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8111 switch (DECL_FUNCTION_CODE (fndecl))
8113 CASE_INT_FN (BUILT_IN_FFS):
8114 if (lo != 0)
8115 result = ffs_hwi (lo);
8116 else if (hi != 0)
8117 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8118 else
8119 result = 0;
8120 break;
8122 CASE_INT_FN (BUILT_IN_CLZ):
8123 if (hi != 0)
8124 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8125 else if (lo != 0)
8126 result = width - floor_log2 (lo) - 1;
8127 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8128 result = width;
8129 break;
8131 CASE_INT_FN (BUILT_IN_CTZ):
8132 if (lo != 0)
8133 result = ctz_hwi (lo);
8134 else if (hi != 0)
8135 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8136 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8137 result = width;
8138 break;
8140 CASE_INT_FN (BUILT_IN_CLRSB):
8141 if (width > HOST_BITS_PER_WIDE_INT
8142 && (hi & ((unsigned HOST_WIDE_INT) 1
8143 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8145 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8146 << (width - HOST_BITS_PER_WIDE_INT - 1));
8147 lo = ~lo;
8149 else if (width <= HOST_BITS_PER_WIDE_INT
8150 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8151 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8152 if (hi != 0)
8153 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8154 else if (lo != 0)
8155 result = width - floor_log2 (lo) - 2;
8156 else
8157 result = width - 1;
8158 break;
8160 CASE_INT_FN (BUILT_IN_POPCOUNT):
8161 result = 0;
8162 while (lo)
8163 result++, lo &= lo - 1;
8164 while (hi)
8165 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8166 break;
8168 CASE_INT_FN (BUILT_IN_PARITY):
8169 result = 0;
8170 while (lo)
8171 result++, lo &= lo - 1;
8172 while (hi)
8173 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8174 result &= 1;
8175 break;
8177 default:
8178 gcc_unreachable ();
8181 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8184 return NULL_TREE;
8187 /* Fold function call to builtin_bswap and the short, long and long long
8188 variants. Return NULL_TREE if no simplification can be made. */
8189 static tree
8190 fold_builtin_bswap (tree fndecl, tree arg)
8192 if (! validate_arg (arg, INTEGER_TYPE))
8193 return NULL_TREE;
8195 /* Optimize constant value. */
8196 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8198 HOST_WIDE_INT hi, width, r_hi = 0;
8199 unsigned HOST_WIDE_INT lo, r_lo = 0;
8200 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8202 width = TYPE_PRECISION (type);
8203 lo = TREE_INT_CST_LOW (arg);
8204 hi = TREE_INT_CST_HIGH (arg);
8206 switch (DECL_FUNCTION_CODE (fndecl))
8208 case BUILT_IN_BSWAP16:
8209 case BUILT_IN_BSWAP32:
8210 case BUILT_IN_BSWAP64:
8212 int s;
8214 for (s = 0; s < width; s += 8)
8216 int d = width - s - 8;
8217 unsigned HOST_WIDE_INT byte;
8219 if (s < HOST_BITS_PER_WIDE_INT)
8220 byte = (lo >> s) & 0xff;
8221 else
8222 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8224 if (d < HOST_BITS_PER_WIDE_INT)
8225 r_lo |= byte << d;
8226 else
8227 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8231 break;
8233 default:
8234 gcc_unreachable ();
8237 if (width < HOST_BITS_PER_WIDE_INT)
8238 return build_int_cst (type, r_lo);
8239 else
8240 return build_int_cst_wide (type, r_lo, r_hi);
8243 return NULL_TREE;
8246 /* A subroutine of fold_builtin to fold the various logarithmic
8247 functions. Return NULL_TREE if no simplification can me made.
8248 FUNC is the corresponding MPFR logarithm function. */
8250 static tree
8251 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8252 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8254 if (validate_arg (arg, REAL_TYPE))
8256 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8257 tree res;
8258 const enum built_in_function fcode = builtin_mathfn_code (arg);
8260 /* Calculate the result when the argument is a constant. */
8261 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8262 return res;
8264 /* Special case, optimize logN(expN(x)) = x. */
8265 if (flag_unsafe_math_optimizations
8266 && ((func == mpfr_log
8267 && (fcode == BUILT_IN_EXP
8268 || fcode == BUILT_IN_EXPF
8269 || fcode == BUILT_IN_EXPL))
8270 || (func == mpfr_log2
8271 && (fcode == BUILT_IN_EXP2
8272 || fcode == BUILT_IN_EXP2F
8273 || fcode == BUILT_IN_EXP2L))
8274 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8275 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8277 /* Optimize logN(func()) for various exponential functions. We
8278 want to determine the value "x" and the power "exponent" in
8279 order to transform logN(x**exponent) into exponent*logN(x). */
8280 if (flag_unsafe_math_optimizations)
8282 tree exponent = 0, x = 0;
8284 switch (fcode)
8286 CASE_FLT_FN (BUILT_IN_EXP):
8287 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8288 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8289 dconst_e ()));
8290 exponent = CALL_EXPR_ARG (arg, 0);
8291 break;
8292 CASE_FLT_FN (BUILT_IN_EXP2):
8293 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8294 x = build_real (type, dconst2);
8295 exponent = CALL_EXPR_ARG (arg, 0);
8296 break;
8297 CASE_FLT_FN (BUILT_IN_EXP10):
8298 CASE_FLT_FN (BUILT_IN_POW10):
8299 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8301 REAL_VALUE_TYPE dconst10;
8302 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8303 x = build_real (type, dconst10);
8305 exponent = CALL_EXPR_ARG (arg, 0);
8306 break;
8307 CASE_FLT_FN (BUILT_IN_SQRT):
8308 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8309 x = CALL_EXPR_ARG (arg, 0);
8310 exponent = build_real (type, dconsthalf);
8311 break;
8312 CASE_FLT_FN (BUILT_IN_CBRT):
8313 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8314 x = CALL_EXPR_ARG (arg, 0);
8315 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8316 dconst_third ()));
8317 break;
8318 CASE_FLT_FN (BUILT_IN_POW):
8319 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8320 x = CALL_EXPR_ARG (arg, 0);
8321 exponent = CALL_EXPR_ARG (arg, 1);
8322 break;
8323 default:
8324 break;
8327 /* Now perform the optimization. */
8328 if (x && exponent)
8330 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8331 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8336 return NULL_TREE;
8339 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8340 NULL_TREE if no simplification can be made. */
8342 static tree
8343 fold_builtin_hypot (location_t loc, tree fndecl,
8344 tree arg0, tree arg1, tree type)
8346 tree res, narg0, narg1;
8348 if (!validate_arg (arg0, REAL_TYPE)
8349 || !validate_arg (arg1, REAL_TYPE))
8350 return NULL_TREE;
8352 /* Calculate the result when the argument is a constant. */
8353 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8354 return res;
8356 /* If either argument to hypot has a negate or abs, strip that off.
8357 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8358 narg0 = fold_strip_sign_ops (arg0);
8359 narg1 = fold_strip_sign_ops (arg1);
8360 if (narg0 || narg1)
8362 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8363 narg1 ? narg1 : arg1);
8366 /* If either argument is zero, hypot is fabs of the other. */
8367 if (real_zerop (arg0))
8368 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8369 else if (real_zerop (arg1))
8370 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8372 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8373 if (flag_unsafe_math_optimizations
8374 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8376 const REAL_VALUE_TYPE sqrt2_trunc
8377 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8378 return fold_build2_loc (loc, MULT_EXPR, type,
8379 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8380 build_real (type, sqrt2_trunc));
8383 return NULL_TREE;
8387 /* Fold a builtin function call to pow, powf, or powl. Return
8388 NULL_TREE if no simplification can be made. */
8389 static tree
8390 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8392 tree res;
8394 if (!validate_arg (arg0, REAL_TYPE)
8395 || !validate_arg (arg1, REAL_TYPE))
8396 return NULL_TREE;
8398 /* Calculate the result when the argument is a constant. */
8399 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8400 return res;
8402 /* Optimize pow(1.0,y) = 1.0. */
8403 if (real_onep (arg0))
8404 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8406 if (TREE_CODE (arg1) == REAL_CST
8407 && !TREE_OVERFLOW (arg1))
8409 REAL_VALUE_TYPE cint;
8410 REAL_VALUE_TYPE c;
8411 HOST_WIDE_INT n;
8413 c = TREE_REAL_CST (arg1);
8415 /* Optimize pow(x,0.0) = 1.0. */
8416 if (REAL_VALUES_EQUAL (c, dconst0))
8417 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8418 arg0);
8420 /* Optimize pow(x,1.0) = x. */
8421 if (REAL_VALUES_EQUAL (c, dconst1))
8422 return arg0;
8424 /* Optimize pow(x,-1.0) = 1.0/x. */
8425 if (REAL_VALUES_EQUAL (c, dconstm1))
8426 return fold_build2_loc (loc, RDIV_EXPR, type,
8427 build_real (type, dconst1), arg0);
8429 /* Optimize pow(x,0.5) = sqrt(x). */
8430 if (flag_unsafe_math_optimizations
8431 && REAL_VALUES_EQUAL (c, dconsthalf))
8433 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8435 if (sqrtfn != NULL_TREE)
8436 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8439 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8440 if (flag_unsafe_math_optimizations)
8442 const REAL_VALUE_TYPE dconstroot
8443 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8445 if (REAL_VALUES_EQUAL (c, dconstroot))
8447 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8448 if (cbrtfn != NULL_TREE)
8449 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8453 /* Check for an integer exponent. */
8454 n = real_to_integer (&c);
8455 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8456 if (real_identical (&c, &cint))
8458 /* Attempt to evaluate pow at compile-time, unless this should
8459 raise an exception. */
8460 if (TREE_CODE (arg0) == REAL_CST
8461 && !TREE_OVERFLOW (arg0)
8462 && (n > 0
8463 || (!flag_trapping_math && !flag_errno_math)
8464 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8466 REAL_VALUE_TYPE x;
8467 bool inexact;
8469 x = TREE_REAL_CST (arg0);
8470 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8471 if (flag_unsafe_math_optimizations || !inexact)
8472 return build_real (type, x);
8475 /* Strip sign ops from even integer powers. */
8476 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8478 tree narg0 = fold_strip_sign_ops (arg0);
8479 if (narg0)
8480 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8485 if (flag_unsafe_math_optimizations)
8487 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8489 /* Optimize pow(expN(x),y) = expN(x*y). */
8490 if (BUILTIN_EXPONENT_P (fcode))
8492 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8493 tree arg = CALL_EXPR_ARG (arg0, 0);
8494 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8495 return build_call_expr_loc (loc, expfn, 1, arg);
8498 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8499 if (BUILTIN_SQRT_P (fcode))
8501 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8502 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8503 build_real (type, dconsthalf));
8504 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8507 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8508 if (BUILTIN_CBRT_P (fcode))
8510 tree arg = CALL_EXPR_ARG (arg0, 0);
8511 if (tree_expr_nonnegative_p (arg))
8513 const REAL_VALUE_TYPE dconstroot
8514 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8515 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8516 build_real (type, dconstroot));
8517 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8521 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8522 if (fcode == BUILT_IN_POW
8523 || fcode == BUILT_IN_POWF
8524 || fcode == BUILT_IN_POWL)
8526 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8527 if (tree_expr_nonnegative_p (arg00))
8529 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8530 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8531 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8536 return NULL_TREE;
8539 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8540 Return NULL_TREE if no simplification can be made. */
8541 static tree
8542 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8543 tree arg0, tree arg1, tree type)
8545 if (!validate_arg (arg0, REAL_TYPE)
8546 || !validate_arg (arg1, INTEGER_TYPE))
8547 return NULL_TREE;
8549 /* Optimize pow(1.0,y) = 1.0. */
8550 if (real_onep (arg0))
8551 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8553 if (host_integerp (arg1, 0))
8555 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8557 /* Evaluate powi at compile-time. */
8558 if (TREE_CODE (arg0) == REAL_CST
8559 && !TREE_OVERFLOW (arg0))
8561 REAL_VALUE_TYPE x;
8562 x = TREE_REAL_CST (arg0);
8563 real_powi (&x, TYPE_MODE (type), &x, c);
8564 return build_real (type, x);
8567 /* Optimize pow(x,0) = 1.0. */
8568 if (c == 0)
8569 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8570 arg0);
8572 /* Optimize pow(x,1) = x. */
8573 if (c == 1)
8574 return arg0;
8576 /* Optimize pow(x,-1) = 1.0/x. */
8577 if (c == -1)
8578 return fold_build2_loc (loc, RDIV_EXPR, type,
8579 build_real (type, dconst1), arg0);
8582 return NULL_TREE;
8585 /* A subroutine of fold_builtin to fold the various exponent
8586 functions. Return NULL_TREE if no simplification can be made.
8587 FUNC is the corresponding MPFR exponent function. */
8589 static tree
8590 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8591 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8593 if (validate_arg (arg, REAL_TYPE))
8595 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8596 tree res;
8598 /* Calculate the result when the argument is a constant. */
8599 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8600 return res;
8602 /* Optimize expN(logN(x)) = x. */
8603 if (flag_unsafe_math_optimizations)
8605 const enum built_in_function fcode = builtin_mathfn_code (arg);
8607 if ((func == mpfr_exp
8608 && (fcode == BUILT_IN_LOG
8609 || fcode == BUILT_IN_LOGF
8610 || fcode == BUILT_IN_LOGL))
8611 || (func == mpfr_exp2
8612 && (fcode == BUILT_IN_LOG2
8613 || fcode == BUILT_IN_LOG2F
8614 || fcode == BUILT_IN_LOG2L))
8615 || (func == mpfr_exp10
8616 && (fcode == BUILT_IN_LOG10
8617 || fcode == BUILT_IN_LOG10F
8618 || fcode == BUILT_IN_LOG10L)))
8619 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8623 return NULL_TREE;
8626 /* Return true if VAR is a VAR_DECL or a component thereof. */
8628 static bool
8629 var_decl_component_p (tree var)
8631 tree inner = var;
8632 while (handled_component_p (inner))
8633 inner = TREE_OPERAND (inner, 0);
8634 return SSA_VAR_P (inner);
8637 /* Fold function call to builtin memset. Return
8638 NULL_TREE if no simplification can be made. */
8640 static tree
8641 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8642 tree type, bool ignore)
8644 tree var, ret, etype;
8645 unsigned HOST_WIDE_INT length, cval;
8647 if (! validate_arg (dest, POINTER_TYPE)
8648 || ! validate_arg (c, INTEGER_TYPE)
8649 || ! validate_arg (len, INTEGER_TYPE))
8650 return NULL_TREE;
8652 if (! host_integerp (len, 1))
8653 return NULL_TREE;
8655 /* If the LEN parameter is zero, return DEST. */
8656 if (integer_zerop (len))
8657 return omit_one_operand_loc (loc, type, dest, c);
8659 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8660 return NULL_TREE;
8662 var = dest;
8663 STRIP_NOPS (var);
8664 if (TREE_CODE (var) != ADDR_EXPR)
8665 return NULL_TREE;
8667 var = TREE_OPERAND (var, 0);
8668 if (TREE_THIS_VOLATILE (var))
8669 return NULL_TREE;
8671 etype = TREE_TYPE (var);
8672 if (TREE_CODE (etype) == ARRAY_TYPE)
8673 etype = TREE_TYPE (etype);
8675 if (!INTEGRAL_TYPE_P (etype)
8676 && !POINTER_TYPE_P (etype))
8677 return NULL_TREE;
8679 if (! var_decl_component_p (var))
8680 return NULL_TREE;
8682 length = tree_low_cst (len, 1);
8683 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8684 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8685 return NULL_TREE;
8687 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8688 return NULL_TREE;
8690 if (integer_zerop (c))
8691 cval = 0;
8692 else
8694 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8695 return NULL_TREE;
8697 cval = TREE_INT_CST_LOW (c);
8698 cval &= 0xff;
8699 cval |= cval << 8;
8700 cval |= cval << 16;
8701 cval |= (cval << 31) << 1;
8704 ret = build_int_cst_type (etype, cval);
8705 var = build_fold_indirect_ref_loc (loc,
8706 fold_convert_loc (loc,
8707 build_pointer_type (etype),
8708 dest));
8709 ret = build2 (MODIFY_EXPR, etype, var, ret);
8710 if (ignore)
8711 return ret;
8713 return omit_one_operand_loc (loc, type, dest, ret);
8716 /* Fold function call to builtin memset. Return
8717 NULL_TREE if no simplification can be made. */
8719 static tree
8720 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8722 if (! validate_arg (dest, POINTER_TYPE)
8723 || ! validate_arg (size, INTEGER_TYPE))
8724 return NULL_TREE;
8726 if (!ignore)
8727 return NULL_TREE;
8729 /* New argument list transforming bzero(ptr x, int y) to
8730 memset(ptr x, int 0, size_t y). This is done this way
8731 so that if it isn't expanded inline, we fallback to
8732 calling bzero instead of memset. */
8734 return fold_builtin_memset (loc, dest, integer_zero_node,
8735 fold_convert_loc (loc, size_type_node, size),
8736 void_type_node, ignore);
8739 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8740 NULL_TREE if no simplification can be made.
8741 If ENDP is 0, return DEST (like memcpy).
8742 If ENDP is 1, return DEST+LEN (like mempcpy).
8743 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8744 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8745 (memmove). */
8747 static tree
8748 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8749 tree len, tree type, bool ignore, int endp)
8751 tree destvar, srcvar, expr;
8753 if (! validate_arg (dest, POINTER_TYPE)
8754 || ! validate_arg (src, POINTER_TYPE)
8755 || ! validate_arg (len, INTEGER_TYPE))
8756 return NULL_TREE;
8758 /* If the LEN parameter is zero, return DEST. */
8759 if (integer_zerop (len))
8760 return omit_one_operand_loc (loc, type, dest, src);
8762 /* If SRC and DEST are the same (and not volatile), return
8763 DEST{,+LEN,+LEN-1}. */
8764 if (operand_equal_p (src, dest, 0))
8765 expr = len;
8766 else
8768 tree srctype, desttype;
8769 unsigned int src_align, dest_align;
8770 tree off0;
8772 if (endp == 3)
8774 src_align = get_pointer_alignment (src);
8775 dest_align = get_pointer_alignment (dest);
8777 /* Both DEST and SRC must be pointer types.
8778 ??? This is what old code did. Is the testing for pointer types
8779 really mandatory?
8781 If either SRC is readonly or length is 1, we can use memcpy. */
8782 if (!dest_align || !src_align)
8783 return NULL_TREE;
8784 if (readonly_data_expr (src)
8785 || (host_integerp (len, 1)
8786 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8787 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8789 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8790 if (!fn)
8791 return NULL_TREE;
8792 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8795 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8796 if (TREE_CODE (src) == ADDR_EXPR
8797 && TREE_CODE (dest) == ADDR_EXPR)
8799 tree src_base, dest_base, fn;
8800 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8801 HOST_WIDE_INT size = -1;
8802 HOST_WIDE_INT maxsize = -1;
8804 srcvar = TREE_OPERAND (src, 0);
8805 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8806 &size, &maxsize);
8807 destvar = TREE_OPERAND (dest, 0);
8808 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8809 &size, &maxsize);
8810 if (host_integerp (len, 1))
8811 maxsize = tree_low_cst (len, 1);
8812 else
8813 maxsize = -1;
8814 src_offset /= BITS_PER_UNIT;
8815 dest_offset /= BITS_PER_UNIT;
8816 if (SSA_VAR_P (src_base)
8817 && SSA_VAR_P (dest_base))
8819 if (operand_equal_p (src_base, dest_base, 0)
8820 && ranges_overlap_p (src_offset, maxsize,
8821 dest_offset, maxsize))
8822 return NULL_TREE;
8824 else if (TREE_CODE (src_base) == MEM_REF
8825 && TREE_CODE (dest_base) == MEM_REF)
8827 double_int off;
8828 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8829 TREE_OPERAND (dest_base, 0), 0))
8830 return NULL_TREE;
8831 off = mem_ref_offset (src_base) +
8832 double_int::from_shwi (src_offset);
8833 if (!off.fits_shwi ())
8834 return NULL_TREE;
8835 src_offset = off.low;
8836 off = mem_ref_offset (dest_base) +
8837 double_int::from_shwi (dest_offset);
8838 if (!off.fits_shwi ())
8839 return NULL_TREE;
8840 dest_offset = off.low;
8841 if (ranges_overlap_p (src_offset, maxsize,
8842 dest_offset, maxsize))
8843 return NULL_TREE;
8845 else
8846 return NULL_TREE;
8848 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8849 if (!fn)
8850 return NULL_TREE;
8851 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8854 /* If the destination and source do not alias optimize into
8855 memcpy as well. */
8856 if ((is_gimple_min_invariant (dest)
8857 || TREE_CODE (dest) == SSA_NAME)
8858 && (is_gimple_min_invariant (src)
8859 || TREE_CODE (src) == SSA_NAME))
8861 ao_ref destr, srcr;
8862 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8863 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8864 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8866 tree fn;
8867 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8868 if (!fn)
8869 return NULL_TREE;
8870 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8874 return NULL_TREE;
8877 if (!host_integerp (len, 0))
8878 return NULL_TREE;
8879 /* FIXME:
8880 This logic lose for arguments like (type *)malloc (sizeof (type)),
8881 since we strip the casts of up to VOID return value from malloc.
8882 Perhaps we ought to inherit type from non-VOID argument here? */
8883 STRIP_NOPS (src);
8884 STRIP_NOPS (dest);
8885 if (!POINTER_TYPE_P (TREE_TYPE (src))
8886 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8887 return NULL_TREE;
8888 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8889 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8891 tree tem = TREE_OPERAND (src, 0);
8892 STRIP_NOPS (tem);
8893 if (tem != TREE_OPERAND (src, 0))
8894 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8896 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8898 tree tem = TREE_OPERAND (dest, 0);
8899 STRIP_NOPS (tem);
8900 if (tem != TREE_OPERAND (dest, 0))
8901 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8903 srctype = TREE_TYPE (TREE_TYPE (src));
8904 if (TREE_CODE (srctype) == ARRAY_TYPE
8905 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8907 srctype = TREE_TYPE (srctype);
8908 STRIP_NOPS (src);
8909 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8911 desttype = TREE_TYPE (TREE_TYPE (dest));
8912 if (TREE_CODE (desttype) == ARRAY_TYPE
8913 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8915 desttype = TREE_TYPE (desttype);
8916 STRIP_NOPS (dest);
8917 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8919 if (TREE_ADDRESSABLE (srctype)
8920 || TREE_ADDRESSABLE (desttype))
8921 return NULL_TREE;
8923 src_align = get_pointer_alignment (src);
8924 dest_align = get_pointer_alignment (dest);
8925 if (dest_align < TYPE_ALIGN (desttype)
8926 || src_align < TYPE_ALIGN (srctype))
8927 return NULL_TREE;
8929 if (!ignore)
8930 dest = builtin_save_expr (dest);
8932 /* Build accesses at offset zero with a ref-all character type. */
8933 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8934 ptr_mode, true), 0);
8936 destvar = dest;
8937 STRIP_NOPS (destvar);
8938 if (TREE_CODE (destvar) == ADDR_EXPR
8939 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8940 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8941 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8942 else
8943 destvar = NULL_TREE;
8945 srcvar = src;
8946 STRIP_NOPS (srcvar);
8947 if (TREE_CODE (srcvar) == ADDR_EXPR
8948 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8949 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8951 if (!destvar
8952 || src_align >= TYPE_ALIGN (desttype))
8953 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8954 srcvar, off0);
8955 else if (!STRICT_ALIGNMENT)
8957 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8958 src_align);
8959 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8961 else
8962 srcvar = NULL_TREE;
8964 else
8965 srcvar = NULL_TREE;
8967 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8968 return NULL_TREE;
8970 if (srcvar == NULL_TREE)
8972 STRIP_NOPS (src);
8973 if (src_align >= TYPE_ALIGN (desttype))
8974 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8975 else
8977 if (STRICT_ALIGNMENT)
8978 return NULL_TREE;
8979 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8980 src_align);
8981 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8984 else if (destvar == NULL_TREE)
8986 STRIP_NOPS (dest);
8987 if (dest_align >= TYPE_ALIGN (srctype))
8988 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8989 else
8991 if (STRICT_ALIGNMENT)
8992 return NULL_TREE;
8993 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8994 dest_align);
8995 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8999 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9002 if (ignore)
9003 return expr;
9005 if (endp == 0 || endp == 3)
9006 return omit_one_operand_loc (loc, type, dest, expr);
9008 if (expr == len)
9009 expr = NULL_TREE;
9011 if (endp == 2)
9012 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9013 ssize_int (1));
9015 dest = fold_build_pointer_plus_loc (loc, dest, len);
9016 dest = fold_convert_loc (loc, type, dest);
9017 if (expr)
9018 dest = omit_one_operand_loc (loc, type, dest, expr);
9019 return dest;
9022 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9023 If LEN is not NULL, it represents the length of the string to be
9024 copied. Return NULL_TREE if no simplification can be made. */
9026 tree
9027 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9029 tree fn;
9031 if (!validate_arg (dest, POINTER_TYPE)
9032 || !validate_arg (src, POINTER_TYPE))
9033 return NULL_TREE;
9035 /* If SRC and DEST are the same (and not volatile), return DEST. */
9036 if (operand_equal_p (src, dest, 0))
9037 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9039 if (optimize_function_for_size_p (cfun))
9040 return NULL_TREE;
9042 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9043 if (!fn)
9044 return NULL_TREE;
9046 if (!len)
9048 len = c_strlen (src, 1);
9049 if (! len || TREE_SIDE_EFFECTS (len))
9050 return NULL_TREE;
9053 len = fold_convert_loc (loc, size_type_node, len);
9054 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9055 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9056 build_call_expr_loc (loc, fn, 3, dest, src, len));
9059 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9060 Return NULL_TREE if no simplification can be made. */
9062 static tree
9063 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9065 tree fn, len, lenp1, call, type;
9067 if (!validate_arg (dest, POINTER_TYPE)
9068 || !validate_arg (src, POINTER_TYPE))
9069 return NULL_TREE;
9071 len = c_strlen (src, 1);
9072 if (!len
9073 || TREE_CODE (len) != INTEGER_CST)
9074 return NULL_TREE;
9076 if (optimize_function_for_size_p (cfun)
9077 /* If length is zero it's small enough. */
9078 && !integer_zerop (len))
9079 return NULL_TREE;
9081 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9082 if (!fn)
9083 return NULL_TREE;
9085 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9086 fold_convert_loc (loc, size_type_node, len),
9087 build_int_cst (size_type_node, 1));
9088 /* We use dest twice in building our expression. Save it from
9089 multiple expansions. */
9090 dest = builtin_save_expr (dest);
9091 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9093 type = TREE_TYPE (TREE_TYPE (fndecl));
9094 dest = fold_build_pointer_plus_loc (loc, dest, len);
9095 dest = fold_convert_loc (loc, type, dest);
9096 dest = omit_one_operand_loc (loc, type, dest, call);
9097 return dest;
9100 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9101 If SLEN is not NULL, it represents the length of the source string.
9102 Return NULL_TREE if no simplification can be made. */
9104 tree
9105 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9106 tree src, tree len, tree slen)
9108 tree fn;
9110 if (!validate_arg (dest, POINTER_TYPE)
9111 || !validate_arg (src, POINTER_TYPE)
9112 || !validate_arg (len, INTEGER_TYPE))
9113 return NULL_TREE;
9115 /* If the LEN parameter is zero, return DEST. */
9116 if (integer_zerop (len))
9117 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9119 /* We can't compare slen with len as constants below if len is not a
9120 constant. */
9121 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9122 return NULL_TREE;
9124 if (!slen)
9125 slen = c_strlen (src, 1);
9127 /* Now, we must be passed a constant src ptr parameter. */
9128 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9129 return NULL_TREE;
9131 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9133 /* We do not support simplification of this case, though we do
9134 support it when expanding trees into RTL. */
9135 /* FIXME: generate a call to __builtin_memset. */
9136 if (tree_int_cst_lt (slen, len))
9137 return NULL_TREE;
9139 /* OK transform into builtin memcpy. */
9140 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9141 if (!fn)
9142 return NULL_TREE;
9144 len = fold_convert_loc (loc, size_type_node, len);
9145 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9146 build_call_expr_loc (loc, fn, 3, dest, src, len));
9149 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9150 arguments to the call, and TYPE is its return type.
9151 Return NULL_TREE if no simplification can be made. */
9153 static tree
9154 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9156 if (!validate_arg (arg1, POINTER_TYPE)
9157 || !validate_arg (arg2, INTEGER_TYPE)
9158 || !validate_arg (len, INTEGER_TYPE))
9159 return NULL_TREE;
9160 else
9162 const char *p1;
9164 if (TREE_CODE (arg2) != INTEGER_CST
9165 || !host_integerp (len, 1))
9166 return NULL_TREE;
9168 p1 = c_getstr (arg1);
9169 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9171 char c;
9172 const char *r;
9173 tree tem;
9175 if (target_char_cast (arg2, &c))
9176 return NULL_TREE;
9178 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9180 if (r == NULL)
9181 return build_int_cst (TREE_TYPE (arg1), 0);
9183 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9184 return fold_convert_loc (loc, type, tem);
9186 return NULL_TREE;
9190 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9191 Return NULL_TREE if no simplification can be made. */
9193 static tree
9194 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9196 const char *p1, *p2;
9198 if (!validate_arg (arg1, POINTER_TYPE)
9199 || !validate_arg (arg2, POINTER_TYPE)
9200 || !validate_arg (len, INTEGER_TYPE))
9201 return NULL_TREE;
9203 /* If the LEN parameter is zero, return zero. */
9204 if (integer_zerop (len))
9205 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9206 arg1, arg2);
9208 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9209 if (operand_equal_p (arg1, arg2, 0))
9210 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9212 p1 = c_getstr (arg1);
9213 p2 = c_getstr (arg2);
9215 /* If all arguments are constant, and the value of len is not greater
9216 than the lengths of arg1 and arg2, evaluate at compile-time. */
9217 if (host_integerp (len, 1) && p1 && p2
9218 && compare_tree_int (len, strlen (p1) + 1) <= 0
9219 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9221 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9223 if (r > 0)
9224 return integer_one_node;
9225 else if (r < 0)
9226 return integer_minus_one_node;
9227 else
9228 return integer_zero_node;
9231 /* If len parameter is one, return an expression corresponding to
9232 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9233 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9235 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9236 tree cst_uchar_ptr_node
9237 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9239 tree ind1
9240 = fold_convert_loc (loc, integer_type_node,
9241 build1 (INDIRECT_REF, cst_uchar_node,
9242 fold_convert_loc (loc,
9243 cst_uchar_ptr_node,
9244 arg1)));
9245 tree ind2
9246 = fold_convert_loc (loc, integer_type_node,
9247 build1 (INDIRECT_REF, cst_uchar_node,
9248 fold_convert_loc (loc,
9249 cst_uchar_ptr_node,
9250 arg2)));
9251 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9254 return NULL_TREE;
9257 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9258 Return NULL_TREE if no simplification can be made. */
9260 static tree
9261 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9263 const char *p1, *p2;
9265 if (!validate_arg (arg1, POINTER_TYPE)
9266 || !validate_arg (arg2, POINTER_TYPE))
9267 return NULL_TREE;
9269 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9270 if (operand_equal_p (arg1, arg2, 0))
9271 return integer_zero_node;
9273 p1 = c_getstr (arg1);
9274 p2 = c_getstr (arg2);
9276 if (p1 && p2)
9278 const int i = strcmp (p1, p2);
9279 if (i < 0)
9280 return integer_minus_one_node;
9281 else if (i > 0)
9282 return integer_one_node;
9283 else
9284 return integer_zero_node;
9287 /* If the second arg is "", return *(const unsigned char*)arg1. */
9288 if (p2 && *p2 == '\0')
9290 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9291 tree cst_uchar_ptr_node
9292 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9294 return fold_convert_loc (loc, integer_type_node,
9295 build1 (INDIRECT_REF, cst_uchar_node,
9296 fold_convert_loc (loc,
9297 cst_uchar_ptr_node,
9298 arg1)));
9301 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9302 if (p1 && *p1 == '\0')
9304 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9305 tree cst_uchar_ptr_node
9306 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9308 tree temp
9309 = fold_convert_loc (loc, integer_type_node,
9310 build1 (INDIRECT_REF, cst_uchar_node,
9311 fold_convert_loc (loc,
9312 cst_uchar_ptr_node,
9313 arg2)));
9314 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9317 return NULL_TREE;
9320 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9321 Return NULL_TREE if no simplification can be made. */
9323 static tree
9324 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9326 const char *p1, *p2;
9328 if (!validate_arg (arg1, POINTER_TYPE)
9329 || !validate_arg (arg2, POINTER_TYPE)
9330 || !validate_arg (len, INTEGER_TYPE))
9331 return NULL_TREE;
9333 /* If the LEN parameter is zero, return zero. */
9334 if (integer_zerop (len))
9335 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9336 arg1, arg2);
9338 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9339 if (operand_equal_p (arg1, arg2, 0))
9340 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9342 p1 = c_getstr (arg1);
9343 p2 = c_getstr (arg2);
9345 if (host_integerp (len, 1) && p1 && p2)
9347 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9348 if (i > 0)
9349 return integer_one_node;
9350 else if (i < 0)
9351 return integer_minus_one_node;
9352 else
9353 return integer_zero_node;
9356 /* If the second arg is "", and the length is greater than zero,
9357 return *(const unsigned char*)arg1. */
9358 if (p2 && *p2 == '\0'
9359 && TREE_CODE (len) == INTEGER_CST
9360 && tree_int_cst_sgn (len) == 1)
9362 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9363 tree cst_uchar_ptr_node
9364 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9366 return fold_convert_loc (loc, integer_type_node,
9367 build1 (INDIRECT_REF, cst_uchar_node,
9368 fold_convert_loc (loc,
9369 cst_uchar_ptr_node,
9370 arg1)));
9373 /* If the first arg is "", and the length is greater than zero,
9374 return -*(const unsigned char*)arg2. */
9375 if (p1 && *p1 == '\0'
9376 && TREE_CODE (len) == INTEGER_CST
9377 && tree_int_cst_sgn (len) == 1)
9379 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9380 tree cst_uchar_ptr_node
9381 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9383 tree temp = fold_convert_loc (loc, integer_type_node,
9384 build1 (INDIRECT_REF, cst_uchar_node,
9385 fold_convert_loc (loc,
9386 cst_uchar_ptr_node,
9387 arg2)));
9388 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9391 /* If len parameter is one, return an expression corresponding to
9392 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9393 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9395 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9396 tree cst_uchar_ptr_node
9397 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9399 tree ind1 = fold_convert_loc (loc, integer_type_node,
9400 build1 (INDIRECT_REF, cst_uchar_node,
9401 fold_convert_loc (loc,
9402 cst_uchar_ptr_node,
9403 arg1)));
9404 tree ind2 = fold_convert_loc (loc, integer_type_node,
9405 build1 (INDIRECT_REF, cst_uchar_node,
9406 fold_convert_loc (loc,
9407 cst_uchar_ptr_node,
9408 arg2)));
9409 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9412 return NULL_TREE;
9415 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9416 ARG. Return NULL_TREE if no simplification can be made. */
9418 static tree
9419 fold_builtin_signbit (location_t loc, tree arg, tree type)
9421 if (!validate_arg (arg, REAL_TYPE))
9422 return NULL_TREE;
9424 /* If ARG is a compile-time constant, determine the result. */
9425 if (TREE_CODE (arg) == REAL_CST
9426 && !TREE_OVERFLOW (arg))
9428 REAL_VALUE_TYPE c;
9430 c = TREE_REAL_CST (arg);
9431 return (REAL_VALUE_NEGATIVE (c)
9432 ? build_one_cst (type)
9433 : build_zero_cst (type));
9436 /* If ARG is non-negative, the result is always zero. */
9437 if (tree_expr_nonnegative_p (arg))
9438 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9440 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9441 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9442 return fold_convert (type,
9443 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9444 build_real (TREE_TYPE (arg), dconst0)));
9446 return NULL_TREE;
9449 /* Fold function call to builtin copysign, copysignf or copysignl with
9450 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9451 be made. */
9453 static tree
9454 fold_builtin_copysign (location_t loc, tree fndecl,
9455 tree arg1, tree arg2, tree type)
9457 tree tem;
9459 if (!validate_arg (arg1, REAL_TYPE)
9460 || !validate_arg (arg2, REAL_TYPE))
9461 return NULL_TREE;
9463 /* copysign(X,X) is X. */
9464 if (operand_equal_p (arg1, arg2, 0))
9465 return fold_convert_loc (loc, type, arg1);
9467 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9468 if (TREE_CODE (arg1) == REAL_CST
9469 && TREE_CODE (arg2) == REAL_CST
9470 && !TREE_OVERFLOW (arg1)
9471 && !TREE_OVERFLOW (arg2))
9473 REAL_VALUE_TYPE c1, c2;
9475 c1 = TREE_REAL_CST (arg1);
9476 c2 = TREE_REAL_CST (arg2);
9477 /* c1.sign := c2.sign. */
9478 real_copysign (&c1, &c2);
9479 return build_real (type, c1);
9482 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9483 Remember to evaluate Y for side-effects. */
9484 if (tree_expr_nonnegative_p (arg2))
9485 return omit_one_operand_loc (loc, type,
9486 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9487 arg2);
9489 /* Strip sign changing operations for the first argument. */
9490 tem = fold_strip_sign_ops (arg1);
9491 if (tem)
9492 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9494 return NULL_TREE;
9497 /* Fold a call to builtin isascii with argument ARG. */
9499 static tree
9500 fold_builtin_isascii (location_t loc, tree arg)
9502 if (!validate_arg (arg, INTEGER_TYPE))
9503 return NULL_TREE;
9504 else
9506 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9507 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9508 build_int_cst (integer_type_node,
9509 ~ (unsigned HOST_WIDE_INT) 0x7f));
9510 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9511 arg, integer_zero_node);
9515 /* Fold a call to builtin toascii with argument ARG. */
9517 static tree
9518 fold_builtin_toascii (location_t loc, tree arg)
9520 if (!validate_arg (arg, INTEGER_TYPE))
9521 return NULL_TREE;
9523 /* Transform toascii(c) -> (c & 0x7f). */
9524 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9525 build_int_cst (integer_type_node, 0x7f));
9528 /* Fold a call to builtin isdigit with argument ARG. */
9530 static tree
9531 fold_builtin_isdigit (location_t loc, tree arg)
9533 if (!validate_arg (arg, INTEGER_TYPE))
9534 return NULL_TREE;
9535 else
9537 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9538 /* According to the C standard, isdigit is unaffected by locale.
9539 However, it definitely is affected by the target character set. */
9540 unsigned HOST_WIDE_INT target_digit0
9541 = lang_hooks.to_target_charset ('0');
9543 if (target_digit0 == 0)
9544 return NULL_TREE;
9546 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9547 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9548 build_int_cst (unsigned_type_node, target_digit0));
9549 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9550 build_int_cst (unsigned_type_node, 9));
9554 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9556 static tree
9557 fold_builtin_fabs (location_t loc, tree arg, tree type)
9559 if (!validate_arg (arg, REAL_TYPE))
9560 return NULL_TREE;
9562 arg = fold_convert_loc (loc, type, arg);
9563 if (TREE_CODE (arg) == REAL_CST)
9564 return fold_abs_const (arg, type);
9565 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9568 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9570 static tree
9571 fold_builtin_abs (location_t loc, tree arg, tree type)
9573 if (!validate_arg (arg, INTEGER_TYPE))
9574 return NULL_TREE;
9576 arg = fold_convert_loc (loc, type, arg);
9577 if (TREE_CODE (arg) == INTEGER_CST)
9578 return fold_abs_const (arg, type);
9579 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9582 /* Fold a fma operation with arguments ARG[012]. */
9584 tree
9585 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9586 tree type, tree arg0, tree arg1, tree arg2)
9588 if (TREE_CODE (arg0) == REAL_CST
9589 && TREE_CODE (arg1) == REAL_CST
9590 && TREE_CODE (arg2) == REAL_CST)
9591 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9593 return NULL_TREE;
9596 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9598 static tree
9599 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9601 if (validate_arg (arg0, REAL_TYPE)
9602 && validate_arg(arg1, REAL_TYPE)
9603 && validate_arg(arg2, REAL_TYPE))
9605 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9606 if (tem)
9607 return tem;
9609 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9610 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9611 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9613 return NULL_TREE;
9616 /* Fold a call to builtin fmin or fmax. */
9618 static tree
9619 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9620 tree type, bool max)
9622 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9624 /* Calculate the result when the argument is a constant. */
9625 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9627 if (res)
9628 return res;
9630 /* If either argument is NaN, return the other one. Avoid the
9631 transformation if we get (and honor) a signalling NaN. Using
9632 omit_one_operand() ensures we create a non-lvalue. */
9633 if (TREE_CODE (arg0) == REAL_CST
9634 && real_isnan (&TREE_REAL_CST (arg0))
9635 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9636 || ! TREE_REAL_CST (arg0).signalling))
9637 return omit_one_operand_loc (loc, type, arg1, arg0);
9638 if (TREE_CODE (arg1) == REAL_CST
9639 && real_isnan (&TREE_REAL_CST (arg1))
9640 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9641 || ! TREE_REAL_CST (arg1).signalling))
9642 return omit_one_operand_loc (loc, type, arg0, arg1);
9644 /* Transform fmin/fmax(x,x) -> x. */
9645 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9646 return omit_one_operand_loc (loc, type, arg0, arg1);
9648 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9649 functions to return the numeric arg if the other one is NaN.
9650 These tree codes don't honor that, so only transform if
9651 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9652 handled, so we don't have to worry about it either. */
9653 if (flag_finite_math_only)
9654 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9655 fold_convert_loc (loc, type, arg0),
9656 fold_convert_loc (loc, type, arg1));
9658 return NULL_TREE;
9661 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9663 static tree
9664 fold_builtin_carg (location_t loc, tree arg, tree type)
9666 if (validate_arg (arg, COMPLEX_TYPE)
9667 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9669 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9671 if (atan2_fn)
9673 tree new_arg = builtin_save_expr (arg);
9674 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9675 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9676 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9680 return NULL_TREE;
9683 /* Fold a call to builtin logb/ilogb. */
9685 static tree
9686 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9688 if (! validate_arg (arg, REAL_TYPE))
9689 return NULL_TREE;
9691 STRIP_NOPS (arg);
9693 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9695 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9697 switch (value->cl)
9699 case rvc_nan:
9700 case rvc_inf:
9701 /* If arg is Inf or NaN and we're logb, return it. */
9702 if (TREE_CODE (rettype) == REAL_TYPE)
9703 return fold_convert_loc (loc, rettype, arg);
9704 /* Fall through... */
9705 case rvc_zero:
9706 /* Zero may set errno and/or raise an exception for logb, also
9707 for ilogb we don't know FP_ILOGB0. */
9708 return NULL_TREE;
9709 case rvc_normal:
9710 /* For normal numbers, proceed iff radix == 2. In GCC,
9711 normalized significands are in the range [0.5, 1.0). We
9712 want the exponent as if they were [1.0, 2.0) so get the
9713 exponent and subtract 1. */
9714 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9715 return fold_convert_loc (loc, rettype,
9716 build_int_cst (integer_type_node,
9717 REAL_EXP (value)-1));
9718 break;
9722 return NULL_TREE;
9725 /* Fold a call to builtin significand, if radix == 2. */
9727 static tree
9728 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9730 if (! validate_arg (arg, REAL_TYPE))
9731 return NULL_TREE;
9733 STRIP_NOPS (arg);
9735 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9737 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9739 switch (value->cl)
9741 case rvc_zero:
9742 case rvc_nan:
9743 case rvc_inf:
9744 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9745 return fold_convert_loc (loc, rettype, arg);
9746 case rvc_normal:
9747 /* For normal numbers, proceed iff radix == 2. */
9748 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9750 REAL_VALUE_TYPE result = *value;
9751 /* In GCC, normalized significands are in the range [0.5,
9752 1.0). We want them to be [1.0, 2.0) so set the
9753 exponent to 1. */
9754 SET_REAL_EXP (&result, 1);
9755 return build_real (rettype, result);
9757 break;
9761 return NULL_TREE;
9764 /* Fold a call to builtin frexp, we can assume the base is 2. */
9766 static tree
9767 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9769 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9770 return NULL_TREE;
9772 STRIP_NOPS (arg0);
9774 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9775 return NULL_TREE;
9777 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9779 /* Proceed if a valid pointer type was passed in. */
9780 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9782 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9783 tree frac, exp;
9785 switch (value->cl)
9787 case rvc_zero:
9788 /* For +-0, return (*exp = 0, +-0). */
9789 exp = integer_zero_node;
9790 frac = arg0;
9791 break;
9792 case rvc_nan:
9793 case rvc_inf:
9794 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9795 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9796 case rvc_normal:
9798 /* Since the frexp function always expects base 2, and in
9799 GCC normalized significands are already in the range
9800 [0.5, 1.0), we have exactly what frexp wants. */
9801 REAL_VALUE_TYPE frac_rvt = *value;
9802 SET_REAL_EXP (&frac_rvt, 0);
9803 frac = build_real (rettype, frac_rvt);
9804 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9806 break;
9807 default:
9808 gcc_unreachable ();
9811 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9812 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9813 TREE_SIDE_EFFECTS (arg1) = 1;
9814 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9817 return NULL_TREE;
9820 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9821 then we can assume the base is two. If it's false, then we have to
9822 check the mode of the TYPE parameter in certain cases. */
9824 static tree
9825 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9826 tree type, bool ldexp)
9828 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9830 STRIP_NOPS (arg0);
9831 STRIP_NOPS (arg1);
9833 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9834 if (real_zerop (arg0) || integer_zerop (arg1)
9835 || (TREE_CODE (arg0) == REAL_CST
9836 && !real_isfinite (&TREE_REAL_CST (arg0))))
9837 return omit_one_operand_loc (loc, type, arg0, arg1);
9839 /* If both arguments are constant, then try to evaluate it. */
9840 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9841 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9842 && host_integerp (arg1, 0))
9844 /* Bound the maximum adjustment to twice the range of the
9845 mode's valid exponents. Use abs to ensure the range is
9846 positive as a sanity check. */
9847 const long max_exp_adj = 2 *
9848 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9849 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9851 /* Get the user-requested adjustment. */
9852 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9854 /* The requested adjustment must be inside this range. This
9855 is a preliminary cap to avoid things like overflow, we
9856 may still fail to compute the result for other reasons. */
9857 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9859 REAL_VALUE_TYPE initial_result;
9861 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9863 /* Ensure we didn't overflow. */
9864 if (! real_isinf (&initial_result))
9866 const REAL_VALUE_TYPE trunc_result
9867 = real_value_truncate (TYPE_MODE (type), initial_result);
9869 /* Only proceed if the target mode can hold the
9870 resulting value. */
9871 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9872 return build_real (type, trunc_result);
9878 return NULL_TREE;
9881 /* Fold a call to builtin modf. */
9883 static tree
9884 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9886 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9887 return NULL_TREE;
9889 STRIP_NOPS (arg0);
9891 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9892 return NULL_TREE;
9894 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9896 /* Proceed if a valid pointer type was passed in. */
9897 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9899 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9900 REAL_VALUE_TYPE trunc, frac;
9902 switch (value->cl)
9904 case rvc_nan:
9905 case rvc_zero:
9906 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9907 trunc = frac = *value;
9908 break;
9909 case rvc_inf:
9910 /* For +-Inf, return (*arg1 = arg0, +-0). */
9911 frac = dconst0;
9912 frac.sign = value->sign;
9913 trunc = *value;
9914 break;
9915 case rvc_normal:
9916 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9917 real_trunc (&trunc, VOIDmode, value);
9918 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9919 /* If the original number was negative and already
9920 integral, then the fractional part is -0.0. */
9921 if (value->sign && frac.cl == rvc_zero)
9922 frac.sign = value->sign;
9923 break;
9926 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9927 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9928 build_real (rettype, trunc));
9929 TREE_SIDE_EFFECTS (arg1) = 1;
9930 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9931 build_real (rettype, frac));
9934 return NULL_TREE;
9937 /* Given a location LOC, an interclass builtin function decl FNDECL
9938 and its single argument ARG, return an folded expression computing
9939 the same, or NULL_TREE if we either couldn't or didn't want to fold
9940 (the latter happen if there's an RTL instruction available). */
9942 static tree
9943 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9945 enum machine_mode mode;
9947 if (!validate_arg (arg, REAL_TYPE))
9948 return NULL_TREE;
9950 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9951 return NULL_TREE;
9953 mode = TYPE_MODE (TREE_TYPE (arg));
9955 /* If there is no optab, try generic code. */
9956 switch (DECL_FUNCTION_CODE (fndecl))
9958 tree result;
9960 CASE_FLT_FN (BUILT_IN_ISINF):
9962 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9963 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9964 tree const type = TREE_TYPE (arg);
9965 REAL_VALUE_TYPE r;
9966 char buf[128];
9968 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9969 real_from_string (&r, buf);
9970 result = build_call_expr (isgr_fn, 2,
9971 fold_build1_loc (loc, ABS_EXPR, type, arg),
9972 build_real (type, r));
9973 return result;
9975 CASE_FLT_FN (BUILT_IN_FINITE):
9976 case BUILT_IN_ISFINITE:
9978 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9979 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9980 tree const type = TREE_TYPE (arg);
9981 REAL_VALUE_TYPE r;
9982 char buf[128];
9984 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9985 real_from_string (&r, buf);
9986 result = build_call_expr (isle_fn, 2,
9987 fold_build1_loc (loc, ABS_EXPR, type, arg),
9988 build_real (type, r));
9989 /*result = fold_build2_loc (loc, UNGT_EXPR,
9990 TREE_TYPE (TREE_TYPE (fndecl)),
9991 fold_build1_loc (loc, ABS_EXPR, type, arg),
9992 build_real (type, r));
9993 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9994 TREE_TYPE (TREE_TYPE (fndecl)),
9995 result);*/
9996 return result;
9998 case BUILT_IN_ISNORMAL:
10000 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10001 islessequal(fabs(x),DBL_MAX). */
10002 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10003 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10004 tree const type = TREE_TYPE (arg);
10005 REAL_VALUE_TYPE rmax, rmin;
10006 char buf[128];
10008 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10009 real_from_string (&rmax, buf);
10010 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10011 real_from_string (&rmin, buf);
10012 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10013 result = build_call_expr (isle_fn, 2, arg,
10014 build_real (type, rmax));
10015 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10016 build_call_expr (isge_fn, 2, arg,
10017 build_real (type, rmin)));
10018 return result;
10020 default:
10021 break;
10024 return NULL_TREE;
10027 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10028 ARG is the argument for the call. */
10030 static tree
10031 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10033 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10034 REAL_VALUE_TYPE r;
10036 if (!validate_arg (arg, REAL_TYPE))
10037 return NULL_TREE;
10039 switch (builtin_index)
10041 case BUILT_IN_ISINF:
10042 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10043 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10045 if (TREE_CODE (arg) == REAL_CST)
10047 r = TREE_REAL_CST (arg);
10048 if (real_isinf (&r))
10049 return real_compare (GT_EXPR, &r, &dconst0)
10050 ? integer_one_node : integer_minus_one_node;
10051 else
10052 return integer_zero_node;
10055 return NULL_TREE;
10057 case BUILT_IN_ISINF_SIGN:
10059 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10060 /* In a boolean context, GCC will fold the inner COND_EXPR to
10061 1. So e.g. "if (isinf_sign(x))" would be folded to just
10062 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10063 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10064 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10065 tree tmp = NULL_TREE;
10067 arg = builtin_save_expr (arg);
10069 if (signbit_fn && isinf_fn)
10071 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10072 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10074 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10075 signbit_call, integer_zero_node);
10076 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10077 isinf_call, integer_zero_node);
10079 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10080 integer_minus_one_node, integer_one_node);
10081 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10082 isinf_call, tmp,
10083 integer_zero_node);
10086 return tmp;
10089 case BUILT_IN_ISFINITE:
10090 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10091 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10092 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10094 if (TREE_CODE (arg) == REAL_CST)
10096 r = TREE_REAL_CST (arg);
10097 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10100 return NULL_TREE;
10102 case BUILT_IN_ISNAN:
10103 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10104 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10106 if (TREE_CODE (arg) == REAL_CST)
10108 r = TREE_REAL_CST (arg);
10109 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10112 arg = builtin_save_expr (arg);
10113 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10115 default:
10116 gcc_unreachable ();
10120 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10121 This builtin will generate code to return the appropriate floating
10122 point classification depending on the value of the floating point
10123 number passed in. The possible return values must be supplied as
10124 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10125 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10126 one floating point argument which is "type generic". */
10128 static tree
10129 fold_builtin_fpclassify (location_t loc, tree exp)
10131 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10132 arg, type, res, tmp;
10133 enum machine_mode mode;
10134 REAL_VALUE_TYPE r;
10135 char buf[128];
10137 /* Verify the required arguments in the original call. */
10138 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10139 INTEGER_TYPE, INTEGER_TYPE,
10140 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10141 return NULL_TREE;
10143 fp_nan = CALL_EXPR_ARG (exp, 0);
10144 fp_infinite = CALL_EXPR_ARG (exp, 1);
10145 fp_normal = CALL_EXPR_ARG (exp, 2);
10146 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10147 fp_zero = CALL_EXPR_ARG (exp, 4);
10148 arg = CALL_EXPR_ARG (exp, 5);
10149 type = TREE_TYPE (arg);
10150 mode = TYPE_MODE (type);
10151 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10153 /* fpclassify(x) ->
10154 isnan(x) ? FP_NAN :
10155 (fabs(x) == Inf ? FP_INFINITE :
10156 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10157 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10159 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10160 build_real (type, dconst0));
10161 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10162 tmp, fp_zero, fp_subnormal);
10164 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10165 real_from_string (&r, buf);
10166 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10167 arg, build_real (type, r));
10168 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10170 if (HONOR_INFINITIES (mode))
10172 real_inf (&r);
10173 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10174 build_real (type, r));
10175 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10176 fp_infinite, res);
10179 if (HONOR_NANS (mode))
10181 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10182 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10185 return res;
10188 /* Fold a call to an unordered comparison function such as
10189 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10190 being called and ARG0 and ARG1 are the arguments for the call.
10191 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10192 the opposite of the desired result. UNORDERED_CODE is used
10193 for modes that can hold NaNs and ORDERED_CODE is used for
10194 the rest. */
10196 static tree
10197 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10198 enum tree_code unordered_code,
10199 enum tree_code ordered_code)
10201 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10202 enum tree_code code;
10203 tree type0, type1;
10204 enum tree_code code0, code1;
10205 tree cmp_type = NULL_TREE;
10207 type0 = TREE_TYPE (arg0);
10208 type1 = TREE_TYPE (arg1);
10210 code0 = TREE_CODE (type0);
10211 code1 = TREE_CODE (type1);
10213 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10214 /* Choose the wider of two real types. */
10215 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10216 ? type0 : type1;
10217 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10218 cmp_type = type0;
10219 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10220 cmp_type = type1;
10222 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10223 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10225 if (unordered_code == UNORDERED_EXPR)
10227 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10228 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10229 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10232 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10233 : ordered_code;
10234 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10235 fold_build2_loc (loc, code, type, arg0, arg1));
10238 /* Fold a call to built-in function FNDECL with 0 arguments.
10239 IGNORE is true if the result of the function call is ignored. This
10240 function returns NULL_TREE if no simplification was possible. */
10242 static tree
10243 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10245 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10246 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10247 switch (fcode)
10249 CASE_FLT_FN (BUILT_IN_INF):
10250 case BUILT_IN_INFD32:
10251 case BUILT_IN_INFD64:
10252 case BUILT_IN_INFD128:
10253 return fold_builtin_inf (loc, type, true);
10255 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10256 return fold_builtin_inf (loc, type, false);
10258 case BUILT_IN_CLASSIFY_TYPE:
10259 return fold_builtin_classify_type (NULL_TREE);
10261 default:
10262 break;
10264 return NULL_TREE;
10267 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10268 IGNORE is true if the result of the function call is ignored. This
10269 function returns NULL_TREE if no simplification was possible. */
10271 static tree
10272 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10274 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10275 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10276 switch (fcode)
10278 case BUILT_IN_CONSTANT_P:
10280 tree val = fold_builtin_constant_p (arg0);
10282 /* Gimplification will pull the CALL_EXPR for the builtin out of
10283 an if condition. When not optimizing, we'll not CSE it back.
10284 To avoid link error types of regressions, return false now. */
10285 if (!val && !optimize)
10286 val = integer_zero_node;
10288 return val;
10291 case BUILT_IN_CLASSIFY_TYPE:
10292 return fold_builtin_classify_type (arg0);
10294 case BUILT_IN_STRLEN:
10295 return fold_builtin_strlen (loc, type, arg0);
10297 CASE_FLT_FN (BUILT_IN_FABS):
10298 return fold_builtin_fabs (loc, arg0, type);
10300 case BUILT_IN_ABS:
10301 case BUILT_IN_LABS:
10302 case BUILT_IN_LLABS:
10303 case BUILT_IN_IMAXABS:
10304 return fold_builtin_abs (loc, arg0, type);
10306 CASE_FLT_FN (BUILT_IN_CONJ):
10307 if (validate_arg (arg0, COMPLEX_TYPE)
10308 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10309 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10310 break;
10312 CASE_FLT_FN (BUILT_IN_CREAL):
10313 if (validate_arg (arg0, COMPLEX_TYPE)
10314 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10315 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10316 break;
10318 CASE_FLT_FN (BUILT_IN_CIMAG):
10319 if (validate_arg (arg0, COMPLEX_TYPE)
10320 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10321 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10322 break;
10324 CASE_FLT_FN (BUILT_IN_CCOS):
10325 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10327 CASE_FLT_FN (BUILT_IN_CCOSH):
10328 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10330 CASE_FLT_FN (BUILT_IN_CPROJ):
10331 return fold_builtin_cproj(loc, arg0, type);
10333 CASE_FLT_FN (BUILT_IN_CSIN):
10334 if (validate_arg (arg0, COMPLEX_TYPE)
10335 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10336 return do_mpc_arg1 (arg0, type, mpc_sin);
10337 break;
10339 CASE_FLT_FN (BUILT_IN_CSINH):
10340 if (validate_arg (arg0, COMPLEX_TYPE)
10341 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10342 return do_mpc_arg1 (arg0, type, mpc_sinh);
10343 break;
10345 CASE_FLT_FN (BUILT_IN_CTAN):
10346 if (validate_arg (arg0, COMPLEX_TYPE)
10347 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10348 return do_mpc_arg1 (arg0, type, mpc_tan);
10349 break;
10351 CASE_FLT_FN (BUILT_IN_CTANH):
10352 if (validate_arg (arg0, COMPLEX_TYPE)
10353 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10354 return do_mpc_arg1 (arg0, type, mpc_tanh);
10355 break;
10357 CASE_FLT_FN (BUILT_IN_CLOG):
10358 if (validate_arg (arg0, COMPLEX_TYPE)
10359 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10360 return do_mpc_arg1 (arg0, type, mpc_log);
10361 break;
10363 CASE_FLT_FN (BUILT_IN_CSQRT):
10364 if (validate_arg (arg0, COMPLEX_TYPE)
10365 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10366 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10367 break;
10369 CASE_FLT_FN (BUILT_IN_CASIN):
10370 if (validate_arg (arg0, COMPLEX_TYPE)
10371 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10372 return do_mpc_arg1 (arg0, type, mpc_asin);
10373 break;
10375 CASE_FLT_FN (BUILT_IN_CACOS):
10376 if (validate_arg (arg0, COMPLEX_TYPE)
10377 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10378 return do_mpc_arg1 (arg0, type, mpc_acos);
10379 break;
10381 CASE_FLT_FN (BUILT_IN_CATAN):
10382 if (validate_arg (arg0, COMPLEX_TYPE)
10383 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10384 return do_mpc_arg1 (arg0, type, mpc_atan);
10385 break;
10387 CASE_FLT_FN (BUILT_IN_CASINH):
10388 if (validate_arg (arg0, COMPLEX_TYPE)
10389 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10390 return do_mpc_arg1 (arg0, type, mpc_asinh);
10391 break;
10393 CASE_FLT_FN (BUILT_IN_CACOSH):
10394 if (validate_arg (arg0, COMPLEX_TYPE)
10395 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10396 return do_mpc_arg1 (arg0, type, mpc_acosh);
10397 break;
10399 CASE_FLT_FN (BUILT_IN_CATANH):
10400 if (validate_arg (arg0, COMPLEX_TYPE)
10401 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10402 return do_mpc_arg1 (arg0, type, mpc_atanh);
10403 break;
10405 CASE_FLT_FN (BUILT_IN_CABS):
10406 return fold_builtin_cabs (loc, arg0, type, fndecl);
10408 CASE_FLT_FN (BUILT_IN_CARG):
10409 return fold_builtin_carg (loc, arg0, type);
10411 CASE_FLT_FN (BUILT_IN_SQRT):
10412 return fold_builtin_sqrt (loc, arg0, type);
10414 CASE_FLT_FN (BUILT_IN_CBRT):
10415 return fold_builtin_cbrt (loc, arg0, type);
10417 CASE_FLT_FN (BUILT_IN_ASIN):
10418 if (validate_arg (arg0, REAL_TYPE))
10419 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10420 &dconstm1, &dconst1, true);
10421 break;
10423 CASE_FLT_FN (BUILT_IN_ACOS):
10424 if (validate_arg (arg0, REAL_TYPE))
10425 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10426 &dconstm1, &dconst1, true);
10427 break;
10429 CASE_FLT_FN (BUILT_IN_ATAN):
10430 if (validate_arg (arg0, REAL_TYPE))
10431 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10432 break;
10434 CASE_FLT_FN (BUILT_IN_ASINH):
10435 if (validate_arg (arg0, REAL_TYPE))
10436 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10437 break;
10439 CASE_FLT_FN (BUILT_IN_ACOSH):
10440 if (validate_arg (arg0, REAL_TYPE))
10441 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10442 &dconst1, NULL, true);
10443 break;
10445 CASE_FLT_FN (BUILT_IN_ATANH):
10446 if (validate_arg (arg0, REAL_TYPE))
10447 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10448 &dconstm1, &dconst1, false);
10449 break;
10451 CASE_FLT_FN (BUILT_IN_SIN):
10452 if (validate_arg (arg0, REAL_TYPE))
10453 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10454 break;
10456 CASE_FLT_FN (BUILT_IN_COS):
10457 return fold_builtin_cos (loc, arg0, type, fndecl);
10459 CASE_FLT_FN (BUILT_IN_TAN):
10460 return fold_builtin_tan (arg0, type);
10462 CASE_FLT_FN (BUILT_IN_CEXP):
10463 return fold_builtin_cexp (loc, arg0, type);
10465 CASE_FLT_FN (BUILT_IN_CEXPI):
10466 if (validate_arg (arg0, REAL_TYPE))
10467 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10468 break;
10470 CASE_FLT_FN (BUILT_IN_SINH):
10471 if (validate_arg (arg0, REAL_TYPE))
10472 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10473 break;
10475 CASE_FLT_FN (BUILT_IN_COSH):
10476 return fold_builtin_cosh (loc, arg0, type, fndecl);
10478 CASE_FLT_FN (BUILT_IN_TANH):
10479 if (validate_arg (arg0, REAL_TYPE))
10480 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10481 break;
10483 CASE_FLT_FN (BUILT_IN_ERF):
10484 if (validate_arg (arg0, REAL_TYPE))
10485 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10486 break;
10488 CASE_FLT_FN (BUILT_IN_ERFC):
10489 if (validate_arg (arg0, REAL_TYPE))
10490 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10491 break;
10493 CASE_FLT_FN (BUILT_IN_TGAMMA):
10494 if (validate_arg (arg0, REAL_TYPE))
10495 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10496 break;
10498 CASE_FLT_FN (BUILT_IN_EXP):
10499 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10501 CASE_FLT_FN (BUILT_IN_EXP2):
10502 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10504 CASE_FLT_FN (BUILT_IN_EXP10):
10505 CASE_FLT_FN (BUILT_IN_POW10):
10506 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10508 CASE_FLT_FN (BUILT_IN_EXPM1):
10509 if (validate_arg (arg0, REAL_TYPE))
10510 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10511 break;
10513 CASE_FLT_FN (BUILT_IN_LOG):
10514 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10516 CASE_FLT_FN (BUILT_IN_LOG2):
10517 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10519 CASE_FLT_FN (BUILT_IN_LOG10):
10520 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10522 CASE_FLT_FN (BUILT_IN_LOG1P):
10523 if (validate_arg (arg0, REAL_TYPE))
10524 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10525 &dconstm1, NULL, false);
10526 break;
10528 CASE_FLT_FN (BUILT_IN_J0):
10529 if (validate_arg (arg0, REAL_TYPE))
10530 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10531 NULL, NULL, 0);
10532 break;
10534 CASE_FLT_FN (BUILT_IN_J1):
10535 if (validate_arg (arg0, REAL_TYPE))
10536 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10537 NULL, NULL, 0);
10538 break;
10540 CASE_FLT_FN (BUILT_IN_Y0):
10541 if (validate_arg (arg0, REAL_TYPE))
10542 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10543 &dconst0, NULL, false);
10544 break;
10546 CASE_FLT_FN (BUILT_IN_Y1):
10547 if (validate_arg (arg0, REAL_TYPE))
10548 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10549 &dconst0, NULL, false);
10550 break;
10552 CASE_FLT_FN (BUILT_IN_NAN):
10553 case BUILT_IN_NAND32:
10554 case BUILT_IN_NAND64:
10555 case BUILT_IN_NAND128:
10556 return fold_builtin_nan (arg0, type, true);
10558 CASE_FLT_FN (BUILT_IN_NANS):
10559 return fold_builtin_nan (arg0, type, false);
10561 CASE_FLT_FN (BUILT_IN_FLOOR):
10562 return fold_builtin_floor (loc, fndecl, arg0);
10564 CASE_FLT_FN (BUILT_IN_CEIL):
10565 return fold_builtin_ceil (loc, fndecl, arg0);
10567 CASE_FLT_FN (BUILT_IN_TRUNC):
10568 return fold_builtin_trunc (loc, fndecl, arg0);
10570 CASE_FLT_FN (BUILT_IN_ROUND):
10571 return fold_builtin_round (loc, fndecl, arg0);
10573 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10574 CASE_FLT_FN (BUILT_IN_RINT):
10575 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10577 CASE_FLT_FN (BUILT_IN_ICEIL):
10578 CASE_FLT_FN (BUILT_IN_LCEIL):
10579 CASE_FLT_FN (BUILT_IN_LLCEIL):
10580 CASE_FLT_FN (BUILT_IN_LFLOOR):
10581 CASE_FLT_FN (BUILT_IN_IFLOOR):
10582 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10583 CASE_FLT_FN (BUILT_IN_IROUND):
10584 CASE_FLT_FN (BUILT_IN_LROUND):
10585 CASE_FLT_FN (BUILT_IN_LLROUND):
10586 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10588 CASE_FLT_FN (BUILT_IN_IRINT):
10589 CASE_FLT_FN (BUILT_IN_LRINT):
10590 CASE_FLT_FN (BUILT_IN_LLRINT):
10591 return fold_fixed_mathfn (loc, fndecl, arg0);
10593 case BUILT_IN_BSWAP16:
10594 case BUILT_IN_BSWAP32:
10595 case BUILT_IN_BSWAP64:
10596 return fold_builtin_bswap (fndecl, arg0);
10598 CASE_INT_FN (BUILT_IN_FFS):
10599 CASE_INT_FN (BUILT_IN_CLZ):
10600 CASE_INT_FN (BUILT_IN_CTZ):
10601 CASE_INT_FN (BUILT_IN_CLRSB):
10602 CASE_INT_FN (BUILT_IN_POPCOUNT):
10603 CASE_INT_FN (BUILT_IN_PARITY):
10604 return fold_builtin_bitop (fndecl, arg0);
10606 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10607 return fold_builtin_signbit (loc, arg0, type);
10609 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10610 return fold_builtin_significand (loc, arg0, type);
10612 CASE_FLT_FN (BUILT_IN_ILOGB):
10613 CASE_FLT_FN (BUILT_IN_LOGB):
10614 return fold_builtin_logb (loc, arg0, type);
10616 case BUILT_IN_ISASCII:
10617 return fold_builtin_isascii (loc, arg0);
10619 case BUILT_IN_TOASCII:
10620 return fold_builtin_toascii (loc, arg0);
10622 case BUILT_IN_ISDIGIT:
10623 return fold_builtin_isdigit (loc, arg0);
10625 CASE_FLT_FN (BUILT_IN_FINITE):
10626 case BUILT_IN_FINITED32:
10627 case BUILT_IN_FINITED64:
10628 case BUILT_IN_FINITED128:
10629 case BUILT_IN_ISFINITE:
10631 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10632 if (ret)
10633 return ret;
10634 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10637 CASE_FLT_FN (BUILT_IN_ISINF):
10638 case BUILT_IN_ISINFD32:
10639 case BUILT_IN_ISINFD64:
10640 case BUILT_IN_ISINFD128:
10642 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10643 if (ret)
10644 return ret;
10645 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10648 case BUILT_IN_ISNORMAL:
10649 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10651 case BUILT_IN_ISINF_SIGN:
10652 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10654 CASE_FLT_FN (BUILT_IN_ISNAN):
10655 case BUILT_IN_ISNAND32:
10656 case BUILT_IN_ISNAND64:
10657 case BUILT_IN_ISNAND128:
10658 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10660 case BUILT_IN_PRINTF:
10661 case BUILT_IN_PRINTF_UNLOCKED:
10662 case BUILT_IN_VPRINTF:
10663 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10665 case BUILT_IN_FREE:
10666 if (integer_zerop (arg0))
10667 return build_empty_stmt (loc);
10668 break;
10670 default:
10671 break;
10674 return NULL_TREE;
10678 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10679 IGNORE is true if the result of the function call is ignored. This
10680 function returns NULL_TREE if no simplification was possible. */
10682 static tree
10683 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10685 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10686 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10688 switch (fcode)
10690 CASE_FLT_FN (BUILT_IN_JN):
10691 if (validate_arg (arg0, INTEGER_TYPE)
10692 && validate_arg (arg1, REAL_TYPE))
10693 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10694 break;
10696 CASE_FLT_FN (BUILT_IN_YN):
10697 if (validate_arg (arg0, INTEGER_TYPE)
10698 && validate_arg (arg1, REAL_TYPE))
10699 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10700 &dconst0, false);
10701 break;
10703 CASE_FLT_FN (BUILT_IN_DREM):
10704 CASE_FLT_FN (BUILT_IN_REMAINDER):
10705 if (validate_arg (arg0, REAL_TYPE)
10706 && validate_arg(arg1, REAL_TYPE))
10707 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10708 break;
10710 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10711 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10712 if (validate_arg (arg0, REAL_TYPE)
10713 && validate_arg(arg1, POINTER_TYPE))
10714 return do_mpfr_lgamma_r (arg0, arg1, type);
10715 break;
10717 CASE_FLT_FN (BUILT_IN_ATAN2):
10718 if (validate_arg (arg0, REAL_TYPE)
10719 && validate_arg(arg1, REAL_TYPE))
10720 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10721 break;
10723 CASE_FLT_FN (BUILT_IN_FDIM):
10724 if (validate_arg (arg0, REAL_TYPE)
10725 && validate_arg(arg1, REAL_TYPE))
10726 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10727 break;
10729 CASE_FLT_FN (BUILT_IN_HYPOT):
10730 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10732 CASE_FLT_FN (BUILT_IN_CPOW):
10733 if (validate_arg (arg0, COMPLEX_TYPE)
10734 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10735 && validate_arg (arg1, COMPLEX_TYPE)
10736 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10737 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10738 break;
10740 CASE_FLT_FN (BUILT_IN_LDEXP):
10741 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10742 CASE_FLT_FN (BUILT_IN_SCALBN):
10743 CASE_FLT_FN (BUILT_IN_SCALBLN):
10744 return fold_builtin_load_exponent (loc, arg0, arg1,
10745 type, /*ldexp=*/false);
10747 CASE_FLT_FN (BUILT_IN_FREXP):
10748 return fold_builtin_frexp (loc, arg0, arg1, type);
10750 CASE_FLT_FN (BUILT_IN_MODF):
10751 return fold_builtin_modf (loc, arg0, arg1, type);
10753 case BUILT_IN_BZERO:
10754 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10756 case BUILT_IN_FPUTS:
10757 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10759 case BUILT_IN_FPUTS_UNLOCKED:
10760 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10762 case BUILT_IN_STRSTR:
10763 return fold_builtin_strstr (loc, arg0, arg1, type);
10765 case BUILT_IN_STRCAT:
10766 return fold_builtin_strcat (loc, arg0, arg1);
10768 case BUILT_IN_STRSPN:
10769 return fold_builtin_strspn (loc, arg0, arg1);
10771 case BUILT_IN_STRCSPN:
10772 return fold_builtin_strcspn (loc, arg0, arg1);
10774 case BUILT_IN_STRCHR:
10775 case BUILT_IN_INDEX:
10776 return fold_builtin_strchr (loc, arg0, arg1, type);
10778 case BUILT_IN_STRRCHR:
10779 case BUILT_IN_RINDEX:
10780 return fold_builtin_strrchr (loc, arg0, arg1, type);
10782 case BUILT_IN_STRCPY:
10783 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10785 case BUILT_IN_STPCPY:
10786 if (ignore)
10788 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10789 if (!fn)
10790 break;
10792 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10794 else
10795 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10796 break;
10798 case BUILT_IN_STRCMP:
10799 return fold_builtin_strcmp (loc, arg0, arg1);
10801 case BUILT_IN_STRPBRK:
10802 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10804 case BUILT_IN_EXPECT:
10805 return fold_builtin_expect (loc, arg0, arg1);
10807 CASE_FLT_FN (BUILT_IN_POW):
10808 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10810 CASE_FLT_FN (BUILT_IN_POWI):
10811 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10813 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10814 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10816 CASE_FLT_FN (BUILT_IN_FMIN):
10817 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10819 CASE_FLT_FN (BUILT_IN_FMAX):
10820 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10822 case BUILT_IN_ISGREATER:
10823 return fold_builtin_unordered_cmp (loc, fndecl,
10824 arg0, arg1, UNLE_EXPR, LE_EXPR);
10825 case BUILT_IN_ISGREATEREQUAL:
10826 return fold_builtin_unordered_cmp (loc, fndecl,
10827 arg0, arg1, UNLT_EXPR, LT_EXPR);
10828 case BUILT_IN_ISLESS:
10829 return fold_builtin_unordered_cmp (loc, fndecl,
10830 arg0, arg1, UNGE_EXPR, GE_EXPR);
10831 case BUILT_IN_ISLESSEQUAL:
10832 return fold_builtin_unordered_cmp (loc, fndecl,
10833 arg0, arg1, UNGT_EXPR, GT_EXPR);
10834 case BUILT_IN_ISLESSGREATER:
10835 return fold_builtin_unordered_cmp (loc, fndecl,
10836 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10837 case BUILT_IN_ISUNORDERED:
10838 return fold_builtin_unordered_cmp (loc, fndecl,
10839 arg0, arg1, UNORDERED_EXPR,
10840 NOP_EXPR);
10842 /* We do the folding for va_start in the expander. */
10843 case BUILT_IN_VA_START:
10844 break;
10846 case BUILT_IN_SPRINTF:
10847 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10849 case BUILT_IN_OBJECT_SIZE:
10850 return fold_builtin_object_size (arg0, arg1);
10852 case BUILT_IN_PRINTF:
10853 case BUILT_IN_PRINTF_UNLOCKED:
10854 case BUILT_IN_VPRINTF:
10855 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10857 case BUILT_IN_PRINTF_CHK:
10858 case BUILT_IN_VPRINTF_CHK:
10859 if (!validate_arg (arg0, INTEGER_TYPE)
10860 || TREE_SIDE_EFFECTS (arg0))
10861 return NULL_TREE;
10862 else
10863 return fold_builtin_printf (loc, fndecl,
10864 arg1, NULL_TREE, ignore, fcode);
10865 break;
10867 case BUILT_IN_FPRINTF:
10868 case BUILT_IN_FPRINTF_UNLOCKED:
10869 case BUILT_IN_VFPRINTF:
10870 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10871 ignore, fcode);
10873 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10874 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10876 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10877 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10879 default:
10880 break;
10882 return NULL_TREE;
10885 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10886 and ARG2. IGNORE is true if the result of the function call is ignored.
10887 This function returns NULL_TREE if no simplification was possible. */
10889 static tree
10890 fold_builtin_3 (location_t loc, tree fndecl,
10891 tree arg0, tree arg1, tree arg2, bool ignore)
10893 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10894 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10895 switch (fcode)
10898 CASE_FLT_FN (BUILT_IN_SINCOS):
10899 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10901 CASE_FLT_FN (BUILT_IN_FMA):
10902 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10903 break;
10905 CASE_FLT_FN (BUILT_IN_REMQUO):
10906 if (validate_arg (arg0, REAL_TYPE)
10907 && validate_arg(arg1, REAL_TYPE)
10908 && validate_arg(arg2, POINTER_TYPE))
10909 return do_mpfr_remquo (arg0, arg1, arg2);
10910 break;
10912 case BUILT_IN_MEMSET:
10913 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10915 case BUILT_IN_BCOPY:
10916 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10917 void_type_node, true, /*endp=*/3);
10919 case BUILT_IN_MEMCPY:
10920 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10921 type, ignore, /*endp=*/0);
10923 case BUILT_IN_MEMPCPY:
10924 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10925 type, ignore, /*endp=*/1);
10927 case BUILT_IN_MEMMOVE:
10928 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10929 type, ignore, /*endp=*/3);
10931 case BUILT_IN_STRNCAT:
10932 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10934 case BUILT_IN_STRNCPY:
10935 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10937 case BUILT_IN_STRNCMP:
10938 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10940 case BUILT_IN_MEMCHR:
10941 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10943 case BUILT_IN_BCMP:
10944 case BUILT_IN_MEMCMP:
10945 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10947 case BUILT_IN_SPRINTF:
10948 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10950 case BUILT_IN_SNPRINTF:
10951 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10953 case BUILT_IN_STRCPY_CHK:
10954 case BUILT_IN_STPCPY_CHK:
10955 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10956 ignore, fcode);
10958 case BUILT_IN_STRCAT_CHK:
10959 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10961 case BUILT_IN_PRINTF_CHK:
10962 case BUILT_IN_VPRINTF_CHK:
10963 if (!validate_arg (arg0, INTEGER_TYPE)
10964 || TREE_SIDE_EFFECTS (arg0))
10965 return NULL_TREE;
10966 else
10967 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10968 break;
10970 case BUILT_IN_FPRINTF:
10971 case BUILT_IN_FPRINTF_UNLOCKED:
10972 case BUILT_IN_VFPRINTF:
10973 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10974 ignore, fcode);
10976 case BUILT_IN_FPRINTF_CHK:
10977 case BUILT_IN_VFPRINTF_CHK:
10978 if (!validate_arg (arg1, INTEGER_TYPE)
10979 || TREE_SIDE_EFFECTS (arg1))
10980 return NULL_TREE;
10981 else
10982 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10983 ignore, fcode);
10985 default:
10986 break;
10988 return NULL_TREE;
10991 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10992 ARG2, and ARG3. IGNORE is true if the result of the function call is
10993 ignored. This function returns NULL_TREE if no simplification was
10994 possible. */
10996 static tree
10997 fold_builtin_4 (location_t loc, tree fndecl,
10998 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11000 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11002 switch (fcode)
11004 case BUILT_IN_MEMCPY_CHK:
11005 case BUILT_IN_MEMPCPY_CHK:
11006 case BUILT_IN_MEMMOVE_CHK:
11007 case BUILT_IN_MEMSET_CHK:
11008 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11009 NULL_TREE, ignore,
11010 DECL_FUNCTION_CODE (fndecl));
11012 case BUILT_IN_STRNCPY_CHK:
11013 case BUILT_IN_STPNCPY_CHK:
11014 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11015 ignore, fcode);
11017 case BUILT_IN_STRNCAT_CHK:
11018 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11020 case BUILT_IN_SNPRINTF:
11021 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11023 case BUILT_IN_FPRINTF_CHK:
11024 case BUILT_IN_VFPRINTF_CHK:
11025 if (!validate_arg (arg1, INTEGER_TYPE)
11026 || TREE_SIDE_EFFECTS (arg1))
11027 return NULL_TREE;
11028 else
11029 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11030 ignore, fcode);
11031 break;
11033 default:
11034 break;
11036 return NULL_TREE;
11039 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11040 arguments, where NARGS <= 4. IGNORE is true if the result of the
11041 function call is ignored. This function returns NULL_TREE if no
11042 simplification was possible. Note that this only folds builtins with
11043 fixed argument patterns. Foldings that do varargs-to-varargs
11044 transformations, or that match calls with more than 4 arguments,
11045 need to be handled with fold_builtin_varargs instead. */
11047 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11049 static tree
11050 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11052 tree ret = NULL_TREE;
11054 switch (nargs)
11056 case 0:
11057 ret = fold_builtin_0 (loc, fndecl, ignore);
11058 break;
11059 case 1:
11060 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11061 break;
11062 case 2:
11063 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11064 break;
11065 case 3:
11066 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11067 break;
11068 case 4:
11069 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11070 ignore);
11071 break;
11072 default:
11073 break;
11075 if (ret)
11077 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11078 SET_EXPR_LOCATION (ret, loc);
11079 TREE_NO_WARNING (ret) = 1;
11080 return ret;
11082 return NULL_TREE;
11085 /* Builtins with folding operations that operate on "..." arguments
11086 need special handling; we need to store the arguments in a convenient
11087 data structure before attempting any folding. Fortunately there are
11088 only a few builtins that fall into this category. FNDECL is the
11089 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11090 result of the function call is ignored. */
11092 static tree
11093 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11094 bool ignore ATTRIBUTE_UNUSED)
11096 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11097 tree ret = NULL_TREE;
11099 switch (fcode)
11101 case BUILT_IN_SPRINTF_CHK:
11102 case BUILT_IN_VSPRINTF_CHK:
11103 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11104 break;
11106 case BUILT_IN_SNPRINTF_CHK:
11107 case BUILT_IN_VSNPRINTF_CHK:
11108 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11109 break;
11111 case BUILT_IN_FPCLASSIFY:
11112 ret = fold_builtin_fpclassify (loc, exp);
11113 break;
11115 default:
11116 break;
11118 if (ret)
11120 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11121 SET_EXPR_LOCATION (ret, loc);
11122 TREE_NO_WARNING (ret) = 1;
11123 return ret;
11125 return NULL_TREE;
11128 /* Return true if FNDECL shouldn't be folded right now.
11129 If a built-in function has an inline attribute always_inline
11130 wrapper, defer folding it after always_inline functions have
11131 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11132 might not be performed. */
11134 bool
11135 avoid_folding_inline_builtin (tree fndecl)
11137 return (DECL_DECLARED_INLINE_P (fndecl)
11138 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11139 && cfun
11140 && !cfun->always_inline_functions_inlined
11141 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11144 /* A wrapper function for builtin folding that prevents warnings for
11145 "statement without effect" and the like, caused by removing the
11146 call node earlier than the warning is generated. */
11148 tree
11149 fold_call_expr (location_t loc, tree exp, bool ignore)
11151 tree ret = NULL_TREE;
11152 tree fndecl = get_callee_fndecl (exp);
11153 if (fndecl
11154 && TREE_CODE (fndecl) == FUNCTION_DECL
11155 && DECL_BUILT_IN (fndecl)
11156 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11157 yet. Defer folding until we see all the arguments
11158 (after inlining). */
11159 && !CALL_EXPR_VA_ARG_PACK (exp))
11161 int nargs = call_expr_nargs (exp);
11163 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11164 instead last argument is __builtin_va_arg_pack (). Defer folding
11165 even in that case, until arguments are finalized. */
11166 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11168 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11169 if (fndecl2
11170 && TREE_CODE (fndecl2) == FUNCTION_DECL
11171 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11172 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11173 return NULL_TREE;
11176 if (avoid_folding_inline_builtin (fndecl))
11177 return NULL_TREE;
11179 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11180 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11181 CALL_EXPR_ARGP (exp), ignore);
11182 else
11184 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11186 tree *args = CALL_EXPR_ARGP (exp);
11187 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11189 if (!ret)
11190 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11191 if (ret)
11192 return ret;
11195 return NULL_TREE;
11198 /* Conveniently construct a function call expression. FNDECL names the
11199 function to be called and N arguments are passed in the array
11200 ARGARRAY. */
11202 tree
11203 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11205 tree fntype = TREE_TYPE (fndecl);
11206 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11208 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11211 /* Conveniently construct a function call expression. FNDECL names the
11212 function to be called and the arguments are passed in the vector
11213 VEC. */
11215 tree
11216 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11218 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11219 vec_safe_address (vec));
11223 /* Conveniently construct a function call expression. FNDECL names the
11224 function to be called, N is the number of arguments, and the "..."
11225 parameters are the argument expressions. */
11227 tree
11228 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11230 va_list ap;
11231 tree *argarray = XALLOCAVEC (tree, n);
11232 int i;
11234 va_start (ap, n);
11235 for (i = 0; i < n; i++)
11236 argarray[i] = va_arg (ap, tree);
11237 va_end (ap);
11238 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11241 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11242 varargs macros aren't supported by all bootstrap compilers. */
11244 tree
11245 build_call_expr (tree fndecl, int n, ...)
11247 va_list ap;
11248 tree *argarray = XALLOCAVEC (tree, n);
11249 int i;
11251 va_start (ap, n);
11252 for (i = 0; i < n; i++)
11253 argarray[i] = va_arg (ap, tree);
11254 va_end (ap);
11255 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11258 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11259 N arguments are passed in the array ARGARRAY. */
11261 tree
11262 fold_builtin_call_array (location_t loc, tree type,
11263 tree fn,
11264 int n,
11265 tree *argarray)
11267 tree ret = NULL_TREE;
11268 tree exp;
11270 if (TREE_CODE (fn) == ADDR_EXPR)
11272 tree fndecl = TREE_OPERAND (fn, 0);
11273 if (TREE_CODE (fndecl) == FUNCTION_DECL
11274 && DECL_BUILT_IN (fndecl))
11276 /* If last argument is __builtin_va_arg_pack (), arguments to this
11277 function are not finalized yet. Defer folding until they are. */
11278 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11280 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11281 if (fndecl2
11282 && TREE_CODE (fndecl2) == FUNCTION_DECL
11283 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11284 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11285 return build_call_array_loc (loc, type, fn, n, argarray);
11287 if (avoid_folding_inline_builtin (fndecl))
11288 return build_call_array_loc (loc, type, fn, n, argarray);
11289 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11291 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11292 if (ret)
11293 return ret;
11295 return build_call_array_loc (loc, type, fn, n, argarray);
11297 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11299 /* First try the transformations that don't require consing up
11300 an exp. */
11301 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11302 if (ret)
11303 return ret;
11306 /* If we got this far, we need to build an exp. */
11307 exp = build_call_array_loc (loc, type, fn, n, argarray);
11308 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11309 return ret ? ret : exp;
11313 return build_call_array_loc (loc, type, fn, n, argarray);
11316 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11317 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11318 of arguments in ARGS to be omitted. OLDNARGS is the number of
11319 elements in ARGS. */
11321 static tree
11322 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11323 int skip, tree fndecl, int n, va_list newargs)
11325 int nargs = oldnargs - skip + n;
11326 tree *buffer;
11328 if (n > 0)
11330 int i, j;
11332 buffer = XALLOCAVEC (tree, nargs);
11333 for (i = 0; i < n; i++)
11334 buffer[i] = va_arg (newargs, tree);
11335 for (j = skip; j < oldnargs; j++, i++)
11336 buffer[i] = args[j];
11338 else
11339 buffer = args + skip;
11341 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11344 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11345 list ARGS along with N new arguments specified as the "..."
11346 parameters. SKIP is the number of arguments in ARGS to be omitted.
11347 OLDNARGS is the number of elements in ARGS. */
11349 static tree
11350 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11351 int skip, tree fndecl, int n, ...)
11353 va_list ap;
11354 tree t;
11356 va_start (ap, n);
11357 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11358 va_end (ap);
11360 return t;
11363 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11364 along with N new arguments specified as the "..." parameters. SKIP
11365 is the number of arguments in EXP to be omitted. This function is used
11366 to do varargs-to-varargs transformations. */
11368 static tree
11369 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11371 va_list ap;
11372 tree t;
11374 va_start (ap, n);
11375 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11376 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11377 va_end (ap);
11379 return t;
11382 /* Validate a single argument ARG against a tree code CODE representing
11383 a type. */
11385 static bool
11386 validate_arg (const_tree arg, enum tree_code code)
11388 if (!arg)
11389 return false;
11390 else if (code == POINTER_TYPE)
11391 return POINTER_TYPE_P (TREE_TYPE (arg));
11392 else if (code == INTEGER_TYPE)
11393 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11394 return code == TREE_CODE (TREE_TYPE (arg));
11397 /* This function validates the types of a function call argument list
11398 against a specified list of tree_codes. If the last specifier is a 0,
11399 that represents an ellipses, otherwise the last specifier must be a
11400 VOID_TYPE.
11402 This is the GIMPLE version of validate_arglist. Eventually we want to
11403 completely convert builtins.c to work from GIMPLEs and the tree based
11404 validate_arglist will then be removed. */
11406 bool
11407 validate_gimple_arglist (const_gimple call, ...)
11409 enum tree_code code;
11410 bool res = 0;
11411 va_list ap;
11412 const_tree arg;
11413 size_t i;
11415 va_start (ap, call);
11416 i = 0;
11420 code = (enum tree_code) va_arg (ap, int);
11421 switch (code)
11423 case 0:
11424 /* This signifies an ellipses, any further arguments are all ok. */
11425 res = true;
11426 goto end;
11427 case VOID_TYPE:
11428 /* This signifies an endlink, if no arguments remain, return
11429 true, otherwise return false. */
11430 res = (i == gimple_call_num_args (call));
11431 goto end;
11432 default:
11433 /* If no parameters remain or the parameter's code does not
11434 match the specified code, return false. Otherwise continue
11435 checking any remaining arguments. */
11436 arg = gimple_call_arg (call, i++);
11437 if (!validate_arg (arg, code))
11438 goto end;
11439 break;
11442 while (1);
11444 /* We need gotos here since we can only have one VA_CLOSE in a
11445 function. */
11446 end: ;
11447 va_end (ap);
11449 return res;
11452 /* This function validates the types of a function call argument list
11453 against a specified list of tree_codes. If the last specifier is a 0,
11454 that represents an ellipses, otherwise the last specifier must be a
11455 VOID_TYPE. */
11457 bool
11458 validate_arglist (const_tree callexpr, ...)
11460 enum tree_code code;
11461 bool res = 0;
11462 va_list ap;
11463 const_call_expr_arg_iterator iter;
11464 const_tree arg;
11466 va_start (ap, callexpr);
11467 init_const_call_expr_arg_iterator (callexpr, &iter);
11471 code = (enum tree_code) va_arg (ap, int);
11472 switch (code)
11474 case 0:
11475 /* This signifies an ellipses, any further arguments are all ok. */
11476 res = true;
11477 goto end;
11478 case VOID_TYPE:
11479 /* This signifies an endlink, if no arguments remain, return
11480 true, otherwise return false. */
11481 res = !more_const_call_expr_args_p (&iter);
11482 goto end;
11483 default:
11484 /* If no parameters remain or the parameter's code does not
11485 match the specified code, return false. Otherwise continue
11486 checking any remaining arguments. */
11487 arg = next_const_call_expr_arg (&iter);
11488 if (!validate_arg (arg, code))
11489 goto end;
11490 break;
11493 while (1);
11495 /* We need gotos here since we can only have one VA_CLOSE in a
11496 function. */
11497 end: ;
11498 va_end (ap);
11500 return res;
11503 /* Default target-specific builtin expander that does nothing. */
11506 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11507 rtx target ATTRIBUTE_UNUSED,
11508 rtx subtarget ATTRIBUTE_UNUSED,
11509 enum machine_mode mode ATTRIBUTE_UNUSED,
11510 int ignore ATTRIBUTE_UNUSED)
11512 return NULL_RTX;
11515 /* Returns true is EXP represents data that would potentially reside
11516 in a readonly section. */
11518 static bool
11519 readonly_data_expr (tree exp)
11521 STRIP_NOPS (exp);
11523 if (TREE_CODE (exp) != ADDR_EXPR)
11524 return false;
11526 exp = get_base_address (TREE_OPERAND (exp, 0));
11527 if (!exp)
11528 return false;
11530 /* Make sure we call decl_readonly_section only for trees it
11531 can handle (since it returns true for everything it doesn't
11532 understand). */
11533 if (TREE_CODE (exp) == STRING_CST
11534 || TREE_CODE (exp) == CONSTRUCTOR
11535 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11536 return decl_readonly_section (exp, 0);
11537 else
11538 return false;
11541 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11542 to the call, and TYPE is its return type.
11544 Return NULL_TREE if no simplification was possible, otherwise return the
11545 simplified form of the call as a tree.
11547 The simplified form may be a constant or other expression which
11548 computes the same value, but in a more efficient manner (including
11549 calls to other builtin functions).
11551 The call may contain arguments which need to be evaluated, but
11552 which are not useful to determine the result of the call. In
11553 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11554 COMPOUND_EXPR will be an argument which must be evaluated.
11555 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11556 COMPOUND_EXPR in the chain will contain the tree for the simplified
11557 form of the builtin function call. */
11559 static tree
11560 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11562 if (!validate_arg (s1, POINTER_TYPE)
11563 || !validate_arg (s2, POINTER_TYPE))
11564 return NULL_TREE;
11565 else
11567 tree fn;
11568 const char *p1, *p2;
11570 p2 = c_getstr (s2);
11571 if (p2 == NULL)
11572 return NULL_TREE;
11574 p1 = c_getstr (s1);
11575 if (p1 != NULL)
11577 const char *r = strstr (p1, p2);
11578 tree tem;
11580 if (r == NULL)
11581 return build_int_cst (TREE_TYPE (s1), 0);
11583 /* Return an offset into the constant string argument. */
11584 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11585 return fold_convert_loc (loc, type, tem);
11588 /* The argument is const char *, and the result is char *, so we need
11589 a type conversion here to avoid a warning. */
11590 if (p2[0] == '\0')
11591 return fold_convert_loc (loc, type, s1);
11593 if (p2[1] != '\0')
11594 return NULL_TREE;
11596 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11597 if (!fn)
11598 return NULL_TREE;
11600 /* New argument list transforming strstr(s1, s2) to
11601 strchr(s1, s2[0]). */
11602 return build_call_expr_loc (loc, fn, 2, s1,
11603 build_int_cst (integer_type_node, p2[0]));
11607 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11608 the call, and TYPE is its return type.
11610 Return NULL_TREE if no simplification was possible, otherwise return the
11611 simplified form of the call as a tree.
11613 The simplified form may be a constant or other expression which
11614 computes the same value, but in a more efficient manner (including
11615 calls to other builtin functions).
11617 The call may contain arguments which need to be evaluated, but
11618 which are not useful to determine the result of the call. In
11619 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11620 COMPOUND_EXPR will be an argument which must be evaluated.
11621 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11622 COMPOUND_EXPR in the chain will contain the tree for the simplified
11623 form of the builtin function call. */
11625 static tree
11626 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11628 if (!validate_arg (s1, POINTER_TYPE)
11629 || !validate_arg (s2, INTEGER_TYPE))
11630 return NULL_TREE;
11631 else
11633 const char *p1;
11635 if (TREE_CODE (s2) != INTEGER_CST)
11636 return NULL_TREE;
11638 p1 = c_getstr (s1);
11639 if (p1 != NULL)
11641 char c;
11642 const char *r;
11643 tree tem;
11645 if (target_char_cast (s2, &c))
11646 return NULL_TREE;
11648 r = strchr (p1, c);
11650 if (r == NULL)
11651 return build_int_cst (TREE_TYPE (s1), 0);
11653 /* Return an offset into the constant string argument. */
11654 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11655 return fold_convert_loc (loc, type, tem);
11657 return NULL_TREE;
11661 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11662 the call, and TYPE is its return type.
11664 Return NULL_TREE if no simplification was possible, otherwise return the
11665 simplified form of the call as a tree.
11667 The simplified form may be a constant or other expression which
11668 computes the same value, but in a more efficient manner (including
11669 calls to other builtin functions).
11671 The call may contain arguments which need to be evaluated, but
11672 which are not useful to determine the result of the call. In
11673 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11674 COMPOUND_EXPR will be an argument which must be evaluated.
11675 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11676 COMPOUND_EXPR in the chain will contain the tree for the simplified
11677 form of the builtin function call. */
11679 static tree
11680 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11682 if (!validate_arg (s1, POINTER_TYPE)
11683 || !validate_arg (s2, INTEGER_TYPE))
11684 return NULL_TREE;
11685 else
11687 tree fn;
11688 const char *p1;
11690 if (TREE_CODE (s2) != INTEGER_CST)
11691 return NULL_TREE;
11693 p1 = c_getstr (s1);
11694 if (p1 != NULL)
11696 char c;
11697 const char *r;
11698 tree tem;
11700 if (target_char_cast (s2, &c))
11701 return NULL_TREE;
11703 r = strrchr (p1, c);
11705 if (r == NULL)
11706 return build_int_cst (TREE_TYPE (s1), 0);
11708 /* Return an offset into the constant string argument. */
11709 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11710 return fold_convert_loc (loc, type, tem);
11713 if (! integer_zerop (s2))
11714 return NULL_TREE;
11716 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11717 if (!fn)
11718 return NULL_TREE;
11720 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11721 return build_call_expr_loc (loc, fn, 2, s1, s2);
11725 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11726 to the call, and TYPE is its return type.
11728 Return NULL_TREE if no simplification was possible, otherwise return the
11729 simplified form of the call as a tree.
11731 The simplified form may be a constant or other expression which
11732 computes the same value, but in a more efficient manner (including
11733 calls to other builtin functions).
11735 The call may contain arguments which need to be evaluated, but
11736 which are not useful to determine the result of the call. In
11737 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11738 COMPOUND_EXPR will be an argument which must be evaluated.
11739 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11740 COMPOUND_EXPR in the chain will contain the tree for the simplified
11741 form of the builtin function call. */
11743 static tree
11744 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11746 if (!validate_arg (s1, POINTER_TYPE)
11747 || !validate_arg (s2, POINTER_TYPE))
11748 return NULL_TREE;
11749 else
11751 tree fn;
11752 const char *p1, *p2;
11754 p2 = c_getstr (s2);
11755 if (p2 == NULL)
11756 return NULL_TREE;
11758 p1 = c_getstr (s1);
11759 if (p1 != NULL)
11761 const char *r = strpbrk (p1, p2);
11762 tree tem;
11764 if (r == NULL)
11765 return build_int_cst (TREE_TYPE (s1), 0);
11767 /* Return an offset into the constant string argument. */
11768 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11769 return fold_convert_loc (loc, type, tem);
11772 if (p2[0] == '\0')
11773 /* strpbrk(x, "") == NULL.
11774 Evaluate and ignore s1 in case it had side-effects. */
11775 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11777 if (p2[1] != '\0')
11778 return NULL_TREE; /* Really call strpbrk. */
11780 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11781 if (!fn)
11782 return NULL_TREE;
11784 /* New argument list transforming strpbrk(s1, s2) to
11785 strchr(s1, s2[0]). */
11786 return build_call_expr_loc (loc, fn, 2, s1,
11787 build_int_cst (integer_type_node, p2[0]));
11791 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11792 to the call.
11794 Return NULL_TREE if no simplification was possible, otherwise return the
11795 simplified form of the call as a tree.
11797 The simplified form may be a constant or other expression which
11798 computes the same value, but in a more efficient manner (including
11799 calls to other builtin functions).
11801 The call may contain arguments which need to be evaluated, but
11802 which are not useful to determine the result of the call. In
11803 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11804 COMPOUND_EXPR will be an argument which must be evaluated.
11805 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11806 COMPOUND_EXPR in the chain will contain the tree for the simplified
11807 form of the builtin function call. */
11809 static tree
11810 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11812 if (!validate_arg (dst, POINTER_TYPE)
11813 || !validate_arg (src, POINTER_TYPE))
11814 return NULL_TREE;
11815 else
11817 const char *p = c_getstr (src);
11819 /* If the string length is zero, return the dst parameter. */
11820 if (p && *p == '\0')
11821 return dst;
11823 if (optimize_insn_for_speed_p ())
11825 /* See if we can store by pieces into (dst + strlen(dst)). */
11826 tree newdst, call;
11827 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11828 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11830 if (!strlen_fn || !strcpy_fn)
11831 return NULL_TREE;
11833 /* If we don't have a movstr we don't want to emit an strcpy
11834 call. We have to do that if the length of the source string
11835 isn't computable (in that case we can use memcpy probably
11836 later expanding to a sequence of mov instructions). If we
11837 have movstr instructions we can emit strcpy calls. */
11838 if (!HAVE_movstr)
11840 tree len = c_strlen (src, 1);
11841 if (! len || TREE_SIDE_EFFECTS (len))
11842 return NULL_TREE;
11845 /* Stabilize the argument list. */
11846 dst = builtin_save_expr (dst);
11848 /* Create strlen (dst). */
11849 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11850 /* Create (dst p+ strlen (dst)). */
11852 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11853 newdst = builtin_save_expr (newdst);
11855 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11856 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11858 return NULL_TREE;
11862 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11863 arguments to the call.
11865 Return NULL_TREE if no simplification was possible, otherwise return the
11866 simplified form of the call as a tree.
11868 The simplified form may be a constant or other expression which
11869 computes the same value, but in a more efficient manner (including
11870 calls to other builtin functions).
11872 The call may contain arguments which need to be evaluated, but
11873 which are not useful to determine the result of the call. In
11874 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11875 COMPOUND_EXPR will be an argument which must be evaluated.
11876 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11877 COMPOUND_EXPR in the chain will contain the tree for the simplified
11878 form of the builtin function call. */
11880 static tree
11881 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11883 if (!validate_arg (dst, POINTER_TYPE)
11884 || !validate_arg (src, POINTER_TYPE)
11885 || !validate_arg (len, INTEGER_TYPE))
11886 return NULL_TREE;
11887 else
11889 const char *p = c_getstr (src);
11891 /* If the requested length is zero, or the src parameter string
11892 length is zero, return the dst parameter. */
11893 if (integer_zerop (len) || (p && *p == '\0'))
11894 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11896 /* If the requested len is greater than or equal to the string
11897 length, call strcat. */
11898 if (TREE_CODE (len) == INTEGER_CST && p
11899 && compare_tree_int (len, strlen (p)) >= 0)
11901 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11903 /* If the replacement _DECL isn't initialized, don't do the
11904 transformation. */
11905 if (!fn)
11906 return NULL_TREE;
11908 return build_call_expr_loc (loc, fn, 2, dst, src);
11910 return NULL_TREE;
11914 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11915 to the call.
11917 Return NULL_TREE if no simplification was possible, otherwise return the
11918 simplified form of the call as a tree.
11920 The simplified form may be a constant or other expression which
11921 computes the same value, but in a more efficient manner (including
11922 calls to other builtin functions).
11924 The call may contain arguments which need to be evaluated, but
11925 which are not useful to determine the result of the call. In
11926 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11927 COMPOUND_EXPR will be an argument which must be evaluated.
11928 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11929 COMPOUND_EXPR in the chain will contain the tree for the simplified
11930 form of the builtin function call. */
11932 static tree
11933 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11935 if (!validate_arg (s1, POINTER_TYPE)
11936 || !validate_arg (s2, POINTER_TYPE))
11937 return NULL_TREE;
11938 else
11940 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11942 /* If both arguments are constants, evaluate at compile-time. */
11943 if (p1 && p2)
11945 const size_t r = strspn (p1, p2);
11946 return build_int_cst (size_type_node, r);
11949 /* If either argument is "", return NULL_TREE. */
11950 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11951 /* Evaluate and ignore both arguments in case either one has
11952 side-effects. */
11953 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11954 s1, s2);
11955 return NULL_TREE;
11959 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11960 to the call.
11962 Return NULL_TREE if no simplification was possible, otherwise return the
11963 simplified form of the call as a tree.
11965 The simplified form may be a constant or other expression which
11966 computes the same value, but in a more efficient manner (including
11967 calls to other builtin functions).
11969 The call may contain arguments which need to be evaluated, but
11970 which are not useful to determine the result of the call. In
11971 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11972 COMPOUND_EXPR will be an argument which must be evaluated.
11973 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11974 COMPOUND_EXPR in the chain will contain the tree for the simplified
11975 form of the builtin function call. */
11977 static tree
11978 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11980 if (!validate_arg (s1, POINTER_TYPE)
11981 || !validate_arg (s2, POINTER_TYPE))
11982 return NULL_TREE;
11983 else
11985 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11987 /* If both arguments are constants, evaluate at compile-time. */
11988 if (p1 && p2)
11990 const size_t r = strcspn (p1, p2);
11991 return build_int_cst (size_type_node, r);
11994 /* If the first argument is "", return NULL_TREE. */
11995 if (p1 && *p1 == '\0')
11997 /* Evaluate and ignore argument s2 in case it has
11998 side-effects. */
11999 return omit_one_operand_loc (loc, size_type_node,
12000 size_zero_node, s2);
12003 /* If the second argument is "", return __builtin_strlen(s1). */
12004 if (p2 && *p2 == '\0')
12006 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
12008 /* If the replacement _DECL isn't initialized, don't do the
12009 transformation. */
12010 if (!fn)
12011 return NULL_TREE;
12013 return build_call_expr_loc (loc, fn, 1, s1);
12015 return NULL_TREE;
12019 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
12020 to the call. IGNORE is true if the value returned
12021 by the builtin will be ignored. UNLOCKED is true is true if this
12022 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
12023 the known length of the string. Return NULL_TREE if no simplification
12024 was possible. */
12026 tree
12027 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12028 bool ignore, bool unlocked, tree len)
12030 /* If we're using an unlocked function, assume the other unlocked
12031 functions exist explicitly. */
12032 tree const fn_fputc = (unlocked
12033 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12034 : builtin_decl_implicit (BUILT_IN_FPUTC));
12035 tree const fn_fwrite = (unlocked
12036 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12037 : builtin_decl_implicit (BUILT_IN_FWRITE));
12039 /* If the return value is used, don't do the transformation. */
12040 if (!ignore)
12041 return NULL_TREE;
12043 /* Verify the arguments in the original call. */
12044 if (!validate_arg (arg0, POINTER_TYPE)
12045 || !validate_arg (arg1, POINTER_TYPE))
12046 return NULL_TREE;
12048 if (! len)
12049 len = c_strlen (arg0, 0);
12051 /* Get the length of the string passed to fputs. If the length
12052 can't be determined, punt. */
12053 if (!len
12054 || TREE_CODE (len) != INTEGER_CST)
12055 return NULL_TREE;
12057 switch (compare_tree_int (len, 1))
12059 case -1: /* length is 0, delete the call entirely . */
12060 return omit_one_operand_loc (loc, integer_type_node,
12061 integer_zero_node, arg1);;
12063 case 0: /* length is 1, call fputc. */
12065 const char *p = c_getstr (arg0);
12067 if (p != NULL)
12069 if (fn_fputc)
12070 return build_call_expr_loc (loc, fn_fputc, 2,
12071 build_int_cst
12072 (integer_type_node, p[0]), arg1);
12073 else
12074 return NULL_TREE;
12077 /* FALLTHROUGH */
12078 case 1: /* length is greater than 1, call fwrite. */
12080 /* If optimizing for size keep fputs. */
12081 if (optimize_function_for_size_p (cfun))
12082 return NULL_TREE;
12083 /* New argument list transforming fputs(string, stream) to
12084 fwrite(string, 1, len, stream). */
12085 if (fn_fwrite)
12086 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12087 size_one_node, len, arg1);
12088 else
12089 return NULL_TREE;
12091 default:
12092 gcc_unreachable ();
12094 return NULL_TREE;
12097 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12098 produced. False otherwise. This is done so that we don't output the error
12099 or warning twice or three times. */
12101 bool
12102 fold_builtin_next_arg (tree exp, bool va_start_p)
12104 tree fntype = TREE_TYPE (current_function_decl);
12105 int nargs = call_expr_nargs (exp);
12106 tree arg;
12107 /* There is good chance the current input_location points inside the
12108 definition of the va_start macro (perhaps on the token for
12109 builtin) in a system header, so warnings will not be emitted.
12110 Use the location in real source code. */
12111 source_location current_location =
12112 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12113 NULL);
12115 if (!stdarg_p (fntype))
12117 error ("%<va_start%> used in function with fixed args");
12118 return true;
12121 if (va_start_p)
12123 if (va_start_p && (nargs != 2))
12125 error ("wrong number of arguments to function %<va_start%>");
12126 return true;
12128 arg = CALL_EXPR_ARG (exp, 1);
12130 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12131 when we checked the arguments and if needed issued a warning. */
12132 else
12134 if (nargs == 0)
12136 /* Evidently an out of date version of <stdarg.h>; can't validate
12137 va_start's second argument, but can still work as intended. */
12138 warning_at (current_location,
12139 OPT_Wvarargs,
12140 "%<__builtin_next_arg%> called without an argument");
12141 return true;
12143 else if (nargs > 1)
12145 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12146 return true;
12148 arg = CALL_EXPR_ARG (exp, 0);
12151 if (TREE_CODE (arg) == SSA_NAME)
12152 arg = SSA_NAME_VAR (arg);
12154 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12155 or __builtin_next_arg (0) the first time we see it, after checking
12156 the arguments and if needed issuing a warning. */
12157 if (!integer_zerop (arg))
12159 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12161 /* Strip off all nops for the sake of the comparison. This
12162 is not quite the same as STRIP_NOPS. It does more.
12163 We must also strip off INDIRECT_EXPR for C++ reference
12164 parameters. */
12165 while (CONVERT_EXPR_P (arg)
12166 || TREE_CODE (arg) == INDIRECT_REF)
12167 arg = TREE_OPERAND (arg, 0);
12168 if (arg != last_parm)
12170 /* FIXME: Sometimes with the tree optimizers we can get the
12171 not the last argument even though the user used the last
12172 argument. We just warn and set the arg to be the last
12173 argument so that we will get wrong-code because of
12174 it. */
12175 warning_at (current_location,
12176 OPT_Wvarargs,
12177 "second parameter of %<va_start%> not last named argument");
12180 /* Undefined by C99 7.15.1.4p4 (va_start):
12181 "If the parameter parmN is declared with the register storage
12182 class, with a function or array type, or with a type that is
12183 not compatible with the type that results after application of
12184 the default argument promotions, the behavior is undefined."
12186 else if (DECL_REGISTER (arg))
12188 warning_at (current_location,
12189 OPT_Wvarargs,
12190 "undefined behaviour when second parameter of "
12191 "%<va_start%> is declared with %<register%> storage");
12194 /* We want to verify the second parameter just once before the tree
12195 optimizers are run and then avoid keeping it in the tree,
12196 as otherwise we could warn even for correct code like:
12197 void foo (int i, ...)
12198 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12199 if (va_start_p)
12200 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12201 else
12202 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12204 return false;
12208 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12209 ORIG may be null if this is a 2-argument call. We don't attempt to
12210 simplify calls with more than 3 arguments.
12212 Return NULL_TREE if no simplification was possible, otherwise return the
12213 simplified form of the call as a tree. If IGNORED is true, it means that
12214 the caller does not use the returned value of the function. */
12216 static tree
12217 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12218 tree orig, int ignored)
12220 tree call, retval;
12221 const char *fmt_str = NULL;
12223 /* Verify the required arguments in the original call. We deal with two
12224 types of sprintf() calls: 'sprintf (str, fmt)' and
12225 'sprintf (dest, "%s", orig)'. */
12226 if (!validate_arg (dest, POINTER_TYPE)
12227 || !validate_arg (fmt, POINTER_TYPE))
12228 return NULL_TREE;
12229 if (orig && !validate_arg (orig, POINTER_TYPE))
12230 return NULL_TREE;
12232 /* Check whether the format is a literal string constant. */
12233 fmt_str = c_getstr (fmt);
12234 if (fmt_str == NULL)
12235 return NULL_TREE;
12237 call = NULL_TREE;
12238 retval = NULL_TREE;
12240 if (!init_target_chars ())
12241 return NULL_TREE;
12243 /* If the format doesn't contain % args or %%, use strcpy. */
12244 if (strchr (fmt_str, target_percent) == NULL)
12246 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12248 if (!fn)
12249 return NULL_TREE;
12251 /* Don't optimize sprintf (buf, "abc", ptr++). */
12252 if (orig)
12253 return NULL_TREE;
12255 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12256 'format' is known to contain no % formats. */
12257 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12258 if (!ignored)
12259 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12262 /* If the format is "%s", use strcpy if the result isn't used. */
12263 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12265 tree fn;
12266 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12268 if (!fn)
12269 return NULL_TREE;
12271 /* Don't crash on sprintf (str1, "%s"). */
12272 if (!orig)
12273 return NULL_TREE;
12275 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12276 if (!ignored)
12278 retval = c_strlen (orig, 1);
12279 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12280 return NULL_TREE;
12282 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12285 if (call && retval)
12287 retval = fold_convert_loc
12288 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12289 retval);
12290 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12292 else
12293 return call;
12296 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12297 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12298 attempt to simplify calls with more than 4 arguments.
12300 Return NULL_TREE if no simplification was possible, otherwise return the
12301 simplified form of the call as a tree. If IGNORED is true, it means that
12302 the caller does not use the returned value of the function. */
12304 static tree
12305 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12306 tree orig, int ignored)
12308 tree call, retval;
12309 const char *fmt_str = NULL;
12310 unsigned HOST_WIDE_INT destlen;
12312 /* Verify the required arguments in the original call. We deal with two
12313 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12314 'snprintf (dest, cst, "%s", orig)'. */
12315 if (!validate_arg (dest, POINTER_TYPE)
12316 || !validate_arg (destsize, INTEGER_TYPE)
12317 || !validate_arg (fmt, POINTER_TYPE))
12318 return NULL_TREE;
12319 if (orig && !validate_arg (orig, POINTER_TYPE))
12320 return NULL_TREE;
12322 if (!host_integerp (destsize, 1))
12323 return NULL_TREE;
12325 /* Check whether the format is a literal string constant. */
12326 fmt_str = c_getstr (fmt);
12327 if (fmt_str == NULL)
12328 return NULL_TREE;
12330 call = NULL_TREE;
12331 retval = NULL_TREE;
12333 if (!init_target_chars ())
12334 return NULL_TREE;
12336 destlen = tree_low_cst (destsize, 1);
12338 /* If the format doesn't contain % args or %%, use strcpy. */
12339 if (strchr (fmt_str, target_percent) == NULL)
12341 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12342 size_t len = strlen (fmt_str);
12344 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12345 if (orig)
12346 return NULL_TREE;
12348 /* We could expand this as
12349 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12350 or to
12351 memcpy (str, fmt_with_nul_at_cstm1, cst);
12352 but in the former case that might increase code size
12353 and in the latter case grow .rodata section too much.
12354 So punt for now. */
12355 if (len >= destlen)
12356 return NULL_TREE;
12358 if (!fn)
12359 return NULL_TREE;
12361 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12362 'format' is known to contain no % formats and
12363 strlen (fmt) < cst. */
12364 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12366 if (!ignored)
12367 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12370 /* If the format is "%s", use strcpy if the result isn't used. */
12371 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12373 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12374 unsigned HOST_WIDE_INT origlen;
12376 /* Don't crash on snprintf (str1, cst, "%s"). */
12377 if (!orig)
12378 return NULL_TREE;
12380 retval = c_strlen (orig, 1);
12381 if (!retval || !host_integerp (retval, 1))
12382 return NULL_TREE;
12384 origlen = tree_low_cst (retval, 1);
12385 /* We could expand this as
12386 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12387 or to
12388 memcpy (str1, str2_with_nul_at_cstm1, cst);
12389 but in the former case that might increase code size
12390 and in the latter case grow .rodata section too much.
12391 So punt for now. */
12392 if (origlen >= destlen)
12393 return NULL_TREE;
12395 /* Convert snprintf (str1, cst, "%s", str2) into
12396 strcpy (str1, str2) if strlen (str2) < cst. */
12397 if (!fn)
12398 return NULL_TREE;
12400 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12402 if (ignored)
12403 retval = NULL_TREE;
12406 if (call && retval)
12408 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12409 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12410 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12412 else
12413 return call;
12416 /* Expand a call EXP to __builtin_object_size. */
12419 expand_builtin_object_size (tree exp)
12421 tree ost;
12422 int object_size_type;
12423 tree fndecl = get_callee_fndecl (exp);
12425 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12427 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12428 exp, fndecl);
12429 expand_builtin_trap ();
12430 return const0_rtx;
12433 ost = CALL_EXPR_ARG (exp, 1);
12434 STRIP_NOPS (ost);
12436 if (TREE_CODE (ost) != INTEGER_CST
12437 || tree_int_cst_sgn (ost) < 0
12438 || compare_tree_int (ost, 3) > 0)
12440 error ("%Klast argument of %D is not integer constant between 0 and 3",
12441 exp, fndecl);
12442 expand_builtin_trap ();
12443 return const0_rtx;
12446 object_size_type = tree_low_cst (ost, 0);
12448 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12451 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12452 FCODE is the BUILT_IN_* to use.
12453 Return NULL_RTX if we failed; the caller should emit a normal call,
12454 otherwise try to get the result in TARGET, if convenient (and in
12455 mode MODE if that's convenient). */
12457 static rtx
12458 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12459 enum built_in_function fcode)
12461 tree dest, src, len, size;
12463 if (!validate_arglist (exp,
12464 POINTER_TYPE,
12465 fcode == BUILT_IN_MEMSET_CHK
12466 ? INTEGER_TYPE : POINTER_TYPE,
12467 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12468 return NULL_RTX;
12470 dest = CALL_EXPR_ARG (exp, 0);
12471 src = CALL_EXPR_ARG (exp, 1);
12472 len = CALL_EXPR_ARG (exp, 2);
12473 size = CALL_EXPR_ARG (exp, 3);
12475 if (! host_integerp (size, 1))
12476 return NULL_RTX;
12478 if (host_integerp (len, 1) || integer_all_onesp (size))
12480 tree fn;
12482 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12484 warning_at (tree_nonartificial_location (exp),
12485 0, "%Kcall to %D will always overflow destination buffer",
12486 exp, get_callee_fndecl (exp));
12487 return NULL_RTX;
12490 fn = NULL_TREE;
12491 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12492 mem{cpy,pcpy,move,set} is available. */
12493 switch (fcode)
12495 case BUILT_IN_MEMCPY_CHK:
12496 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12497 break;
12498 case BUILT_IN_MEMPCPY_CHK:
12499 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12500 break;
12501 case BUILT_IN_MEMMOVE_CHK:
12502 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12503 break;
12504 case BUILT_IN_MEMSET_CHK:
12505 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12506 break;
12507 default:
12508 break;
12511 if (! fn)
12512 return NULL_RTX;
12514 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12515 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12516 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12517 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12519 else if (fcode == BUILT_IN_MEMSET_CHK)
12520 return NULL_RTX;
12521 else
12523 unsigned int dest_align = get_pointer_alignment (dest);
12525 /* If DEST is not a pointer type, call the normal function. */
12526 if (dest_align == 0)
12527 return NULL_RTX;
12529 /* If SRC and DEST are the same (and not volatile), do nothing. */
12530 if (operand_equal_p (src, dest, 0))
12532 tree expr;
12534 if (fcode != BUILT_IN_MEMPCPY_CHK)
12536 /* Evaluate and ignore LEN in case it has side-effects. */
12537 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12538 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12541 expr = fold_build_pointer_plus (dest, len);
12542 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12545 /* __memmove_chk special case. */
12546 if (fcode == BUILT_IN_MEMMOVE_CHK)
12548 unsigned int src_align = get_pointer_alignment (src);
12550 if (src_align == 0)
12551 return NULL_RTX;
12553 /* If src is categorized for a readonly section we can use
12554 normal __memcpy_chk. */
12555 if (readonly_data_expr (src))
12557 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12558 if (!fn)
12559 return NULL_RTX;
12560 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12561 dest, src, len, size);
12562 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12563 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12564 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12567 return NULL_RTX;
12571 /* Emit warning if a buffer overflow is detected at compile time. */
12573 static void
12574 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12576 int is_strlen = 0;
12577 tree len, size;
12578 location_t loc = tree_nonartificial_location (exp);
12580 switch (fcode)
12582 case BUILT_IN_STRCPY_CHK:
12583 case BUILT_IN_STPCPY_CHK:
12584 /* For __strcat_chk the warning will be emitted only if overflowing
12585 by at least strlen (dest) + 1 bytes. */
12586 case BUILT_IN_STRCAT_CHK:
12587 len = CALL_EXPR_ARG (exp, 1);
12588 size = CALL_EXPR_ARG (exp, 2);
12589 is_strlen = 1;
12590 break;
12591 case BUILT_IN_STRNCAT_CHK:
12592 case BUILT_IN_STRNCPY_CHK:
12593 case BUILT_IN_STPNCPY_CHK:
12594 len = CALL_EXPR_ARG (exp, 2);
12595 size = CALL_EXPR_ARG (exp, 3);
12596 break;
12597 case BUILT_IN_SNPRINTF_CHK:
12598 case BUILT_IN_VSNPRINTF_CHK:
12599 len = CALL_EXPR_ARG (exp, 1);
12600 size = CALL_EXPR_ARG (exp, 3);
12601 break;
12602 default:
12603 gcc_unreachable ();
12606 if (!len || !size)
12607 return;
12609 if (! host_integerp (size, 1) || integer_all_onesp (size))
12610 return;
12612 if (is_strlen)
12614 len = c_strlen (len, 1);
12615 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12616 return;
12618 else if (fcode == BUILT_IN_STRNCAT_CHK)
12620 tree src = CALL_EXPR_ARG (exp, 1);
12621 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12622 return;
12623 src = c_strlen (src, 1);
12624 if (! src || ! host_integerp (src, 1))
12626 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12627 exp, get_callee_fndecl (exp));
12628 return;
12630 else if (tree_int_cst_lt (src, size))
12631 return;
12633 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12634 return;
12636 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12637 exp, get_callee_fndecl (exp));
12640 /* Emit warning if a buffer overflow is detected at compile time
12641 in __sprintf_chk/__vsprintf_chk calls. */
12643 static void
12644 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12646 tree size, len, fmt;
12647 const char *fmt_str;
12648 int nargs = call_expr_nargs (exp);
12650 /* Verify the required arguments in the original call. */
12652 if (nargs < 4)
12653 return;
12654 size = CALL_EXPR_ARG (exp, 2);
12655 fmt = CALL_EXPR_ARG (exp, 3);
12657 if (! host_integerp (size, 1) || integer_all_onesp (size))
12658 return;
12660 /* Check whether the format is a literal string constant. */
12661 fmt_str = c_getstr (fmt);
12662 if (fmt_str == NULL)
12663 return;
12665 if (!init_target_chars ())
12666 return;
12668 /* If the format doesn't contain % args or %%, we know its size. */
12669 if (strchr (fmt_str, target_percent) == 0)
12670 len = build_int_cstu (size_type_node, strlen (fmt_str));
12671 /* If the format is "%s" and first ... argument is a string literal,
12672 we know it too. */
12673 else if (fcode == BUILT_IN_SPRINTF_CHK
12674 && strcmp (fmt_str, target_percent_s) == 0)
12676 tree arg;
12678 if (nargs < 5)
12679 return;
12680 arg = CALL_EXPR_ARG (exp, 4);
12681 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12682 return;
12684 len = c_strlen (arg, 1);
12685 if (!len || ! host_integerp (len, 1))
12686 return;
12688 else
12689 return;
12691 if (! tree_int_cst_lt (len, size))
12692 warning_at (tree_nonartificial_location (exp),
12693 0, "%Kcall to %D will always overflow destination buffer",
12694 exp, get_callee_fndecl (exp));
12697 /* Emit warning if a free is called with address of a variable. */
12699 static void
12700 maybe_emit_free_warning (tree exp)
12702 tree arg = CALL_EXPR_ARG (exp, 0);
12704 STRIP_NOPS (arg);
12705 if (TREE_CODE (arg) != ADDR_EXPR)
12706 return;
12708 arg = get_base_address (TREE_OPERAND (arg, 0));
12709 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12710 return;
12712 if (SSA_VAR_P (arg))
12713 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12714 "%Kattempt to free a non-heap object %qD", exp, arg);
12715 else
12716 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12717 "%Kattempt to free a non-heap object", exp);
12720 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12721 if possible. */
12723 tree
12724 fold_builtin_object_size (tree ptr, tree ost)
12726 unsigned HOST_WIDE_INT bytes;
12727 int object_size_type;
12729 if (!validate_arg (ptr, POINTER_TYPE)
12730 || !validate_arg (ost, INTEGER_TYPE))
12731 return NULL_TREE;
12733 STRIP_NOPS (ost);
12735 if (TREE_CODE (ost) != INTEGER_CST
12736 || tree_int_cst_sgn (ost) < 0
12737 || compare_tree_int (ost, 3) > 0)
12738 return NULL_TREE;
12740 object_size_type = tree_low_cst (ost, 0);
12742 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12743 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12744 and (size_t) 0 for types 2 and 3. */
12745 if (TREE_SIDE_EFFECTS (ptr))
12746 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12748 if (TREE_CODE (ptr) == ADDR_EXPR)
12750 bytes = compute_builtin_object_size (ptr, object_size_type);
12751 if (double_int_fits_to_tree_p (size_type_node,
12752 double_int::from_uhwi (bytes)))
12753 return build_int_cstu (size_type_node, bytes);
12755 else if (TREE_CODE (ptr) == SSA_NAME)
12757 /* If object size is not known yet, delay folding until
12758 later. Maybe subsequent passes will help determining
12759 it. */
12760 bytes = compute_builtin_object_size (ptr, object_size_type);
12761 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12762 && double_int_fits_to_tree_p (size_type_node,
12763 double_int::from_uhwi (bytes)))
12764 return build_int_cstu (size_type_node, bytes);
12767 return NULL_TREE;
12770 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12771 DEST, SRC, LEN, and SIZE are the arguments to the call.
12772 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12773 code of the builtin. If MAXLEN is not NULL, it is maximum length
12774 passed as third argument. */
12776 tree
12777 fold_builtin_memory_chk (location_t loc, tree fndecl,
12778 tree dest, tree src, tree len, tree size,
12779 tree maxlen, bool ignore,
12780 enum built_in_function fcode)
12782 tree fn;
12784 if (!validate_arg (dest, POINTER_TYPE)
12785 || !validate_arg (src,
12786 (fcode == BUILT_IN_MEMSET_CHK
12787 ? INTEGER_TYPE : POINTER_TYPE))
12788 || !validate_arg (len, INTEGER_TYPE)
12789 || !validate_arg (size, INTEGER_TYPE))
12790 return NULL_TREE;
12792 /* If SRC and DEST are the same (and not volatile), return DEST
12793 (resp. DEST+LEN for __mempcpy_chk). */
12794 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12796 if (fcode != BUILT_IN_MEMPCPY_CHK)
12797 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12798 dest, len);
12799 else
12801 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12802 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12806 if (! host_integerp (size, 1))
12807 return NULL_TREE;
12809 if (! integer_all_onesp (size))
12811 if (! host_integerp (len, 1))
12813 /* If LEN is not constant, try MAXLEN too.
12814 For MAXLEN only allow optimizing into non-_ocs function
12815 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12816 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12818 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12820 /* (void) __mempcpy_chk () can be optimized into
12821 (void) __memcpy_chk (). */
12822 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12823 if (!fn)
12824 return NULL_TREE;
12826 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12828 return NULL_TREE;
12831 else
12832 maxlen = len;
12834 if (tree_int_cst_lt (size, maxlen))
12835 return NULL_TREE;
12838 fn = NULL_TREE;
12839 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12840 mem{cpy,pcpy,move,set} is available. */
12841 switch (fcode)
12843 case BUILT_IN_MEMCPY_CHK:
12844 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12845 break;
12846 case BUILT_IN_MEMPCPY_CHK:
12847 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12848 break;
12849 case BUILT_IN_MEMMOVE_CHK:
12850 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12851 break;
12852 case BUILT_IN_MEMSET_CHK:
12853 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12854 break;
12855 default:
12856 break;
12859 if (!fn)
12860 return NULL_TREE;
12862 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12865 /* Fold a call to the __st[rp]cpy_chk builtin.
12866 DEST, SRC, and SIZE are the arguments to the call.
12867 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12868 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12869 strings passed as second argument. */
12871 tree
12872 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12873 tree src, tree size,
12874 tree maxlen, bool ignore,
12875 enum built_in_function fcode)
12877 tree len, fn;
12879 if (!validate_arg (dest, POINTER_TYPE)
12880 || !validate_arg (src, POINTER_TYPE)
12881 || !validate_arg (size, INTEGER_TYPE))
12882 return NULL_TREE;
12884 /* If SRC and DEST are the same (and not volatile), return DEST. */
12885 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12886 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12888 if (! host_integerp (size, 1))
12889 return NULL_TREE;
12891 if (! integer_all_onesp (size))
12893 len = c_strlen (src, 1);
12894 if (! len || ! host_integerp (len, 1))
12896 /* If LEN is not constant, try MAXLEN too.
12897 For MAXLEN only allow optimizing into non-_ocs function
12898 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12899 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12901 if (fcode == BUILT_IN_STPCPY_CHK)
12903 if (! ignore)
12904 return NULL_TREE;
12906 /* If return value of __stpcpy_chk is ignored,
12907 optimize into __strcpy_chk. */
12908 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12909 if (!fn)
12910 return NULL_TREE;
12912 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12915 if (! len || TREE_SIDE_EFFECTS (len))
12916 return NULL_TREE;
12918 /* If c_strlen returned something, but not a constant,
12919 transform __strcpy_chk into __memcpy_chk. */
12920 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12921 if (!fn)
12922 return NULL_TREE;
12924 len = fold_convert_loc (loc, size_type_node, len);
12925 len = size_binop_loc (loc, PLUS_EXPR, len,
12926 build_int_cst (size_type_node, 1));
12927 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12928 build_call_expr_loc (loc, fn, 4,
12929 dest, src, len, size));
12932 else
12933 maxlen = len;
12935 if (! tree_int_cst_lt (maxlen, size))
12936 return NULL_TREE;
12939 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12940 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12941 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12942 if (!fn)
12943 return NULL_TREE;
12945 return build_call_expr_loc (loc, fn, 2, dest, src);
12948 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12949 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12950 length passed as third argument. IGNORE is true if return value can be
12951 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12953 tree
12954 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12955 tree len, tree size, tree maxlen, bool ignore,
12956 enum built_in_function fcode)
12958 tree fn;
12960 if (!validate_arg (dest, POINTER_TYPE)
12961 || !validate_arg (src, POINTER_TYPE)
12962 || !validate_arg (len, INTEGER_TYPE)
12963 || !validate_arg (size, INTEGER_TYPE))
12964 return NULL_TREE;
12966 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12968 /* If return value of __stpncpy_chk is ignored,
12969 optimize into __strncpy_chk. */
12970 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12971 if (fn)
12972 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12975 if (! host_integerp (size, 1))
12976 return NULL_TREE;
12978 if (! integer_all_onesp (size))
12980 if (! host_integerp (len, 1))
12982 /* If LEN is not constant, try MAXLEN too.
12983 For MAXLEN only allow optimizing into non-_ocs function
12984 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12985 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12986 return NULL_TREE;
12988 else
12989 maxlen = len;
12991 if (tree_int_cst_lt (size, maxlen))
12992 return NULL_TREE;
12995 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12996 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12997 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12998 if (!fn)
12999 return NULL_TREE;
13001 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13004 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
13005 are the arguments to the call. */
13007 static tree
13008 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
13009 tree src, tree size)
13011 tree fn;
13012 const char *p;
13014 if (!validate_arg (dest, POINTER_TYPE)
13015 || !validate_arg (src, POINTER_TYPE)
13016 || !validate_arg (size, INTEGER_TYPE))
13017 return NULL_TREE;
13019 p = c_getstr (src);
13020 /* If the SRC parameter is "", return DEST. */
13021 if (p && *p == '\0')
13022 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13024 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
13025 return NULL_TREE;
13027 /* If __builtin_strcat_chk is used, assume strcat is available. */
13028 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
13029 if (!fn)
13030 return NULL_TREE;
13032 return build_call_expr_loc (loc, fn, 2, dest, src);
13035 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13036 LEN, and SIZE. */
13038 static tree
13039 fold_builtin_strncat_chk (location_t loc, tree fndecl,
13040 tree dest, tree src, tree len, tree size)
13042 tree fn;
13043 const char *p;
13045 if (!validate_arg (dest, POINTER_TYPE)
13046 || !validate_arg (src, POINTER_TYPE)
13047 || !validate_arg (size, INTEGER_TYPE)
13048 || !validate_arg (size, INTEGER_TYPE))
13049 return NULL_TREE;
13051 p = c_getstr (src);
13052 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13053 if (p && *p == '\0')
13054 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13055 else if (integer_zerop (len))
13056 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13058 if (! host_integerp (size, 1))
13059 return NULL_TREE;
13061 if (! integer_all_onesp (size))
13063 tree src_len = c_strlen (src, 1);
13064 if (src_len
13065 && host_integerp (src_len, 1)
13066 && host_integerp (len, 1)
13067 && ! tree_int_cst_lt (len, src_len))
13069 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13070 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13071 if (!fn)
13072 return NULL_TREE;
13074 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13076 return NULL_TREE;
13079 /* If __builtin_strncat_chk is used, assume strncat is available. */
13080 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13081 if (!fn)
13082 return NULL_TREE;
13084 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13087 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13088 Return NULL_TREE if a normal call should be emitted rather than
13089 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13090 or BUILT_IN_VSPRINTF_CHK. */
13092 static tree
13093 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13094 enum built_in_function fcode)
13096 tree dest, size, len, fn, fmt, flag;
13097 const char *fmt_str;
13099 /* Verify the required arguments in the original call. */
13100 if (nargs < 4)
13101 return NULL_TREE;
13102 dest = args[0];
13103 if (!validate_arg (dest, POINTER_TYPE))
13104 return NULL_TREE;
13105 flag = args[1];
13106 if (!validate_arg (flag, INTEGER_TYPE))
13107 return NULL_TREE;
13108 size = args[2];
13109 if (!validate_arg (size, INTEGER_TYPE))
13110 return NULL_TREE;
13111 fmt = args[3];
13112 if (!validate_arg (fmt, POINTER_TYPE))
13113 return NULL_TREE;
13115 if (! host_integerp (size, 1))
13116 return NULL_TREE;
13118 len = NULL_TREE;
13120 if (!init_target_chars ())
13121 return NULL_TREE;
13123 /* Check whether the format is a literal string constant. */
13124 fmt_str = c_getstr (fmt);
13125 if (fmt_str != NULL)
13127 /* If the format doesn't contain % args or %%, we know the size. */
13128 if (strchr (fmt_str, target_percent) == 0)
13130 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13131 len = build_int_cstu (size_type_node, strlen (fmt_str));
13133 /* If the format is "%s" and first ... argument is a string literal,
13134 we know the size too. */
13135 else if (fcode == BUILT_IN_SPRINTF_CHK
13136 && strcmp (fmt_str, target_percent_s) == 0)
13138 tree arg;
13140 if (nargs == 5)
13142 arg = args[4];
13143 if (validate_arg (arg, POINTER_TYPE))
13145 len = c_strlen (arg, 1);
13146 if (! len || ! host_integerp (len, 1))
13147 len = NULL_TREE;
13153 if (! integer_all_onesp (size))
13155 if (! len || ! tree_int_cst_lt (len, size))
13156 return NULL_TREE;
13159 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13160 or if format doesn't contain % chars or is "%s". */
13161 if (! integer_zerop (flag))
13163 if (fmt_str == NULL)
13164 return NULL_TREE;
13165 if (strchr (fmt_str, target_percent) != NULL
13166 && strcmp (fmt_str, target_percent_s))
13167 return NULL_TREE;
13170 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13171 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13172 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13173 if (!fn)
13174 return NULL_TREE;
13176 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13179 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13180 a normal call should be emitted rather than expanding the function
13181 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13183 static tree
13184 fold_builtin_sprintf_chk (location_t loc, tree exp,
13185 enum built_in_function fcode)
13187 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13188 CALL_EXPR_ARGP (exp), fcode);
13191 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13192 NULL_TREE if a normal call should be emitted rather than expanding
13193 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13194 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13195 passed as second argument. */
13197 static tree
13198 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13199 tree maxlen, enum built_in_function fcode)
13201 tree dest, size, len, fn, fmt, flag;
13202 const char *fmt_str;
13204 /* Verify the required arguments in the original call. */
13205 if (nargs < 5)
13206 return NULL_TREE;
13207 dest = args[0];
13208 if (!validate_arg (dest, POINTER_TYPE))
13209 return NULL_TREE;
13210 len = args[1];
13211 if (!validate_arg (len, INTEGER_TYPE))
13212 return NULL_TREE;
13213 flag = args[2];
13214 if (!validate_arg (flag, INTEGER_TYPE))
13215 return NULL_TREE;
13216 size = args[3];
13217 if (!validate_arg (size, INTEGER_TYPE))
13218 return NULL_TREE;
13219 fmt = args[4];
13220 if (!validate_arg (fmt, POINTER_TYPE))
13221 return NULL_TREE;
13223 if (! host_integerp (size, 1))
13224 return NULL_TREE;
13226 if (! integer_all_onesp (size))
13228 if (! host_integerp (len, 1))
13230 /* If LEN is not constant, try MAXLEN too.
13231 For MAXLEN only allow optimizing into non-_ocs function
13232 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13233 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13234 return NULL_TREE;
13236 else
13237 maxlen = len;
13239 if (tree_int_cst_lt (size, maxlen))
13240 return NULL_TREE;
13243 if (!init_target_chars ())
13244 return NULL_TREE;
13246 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13247 or if format doesn't contain % chars or is "%s". */
13248 if (! integer_zerop (flag))
13250 fmt_str = c_getstr (fmt);
13251 if (fmt_str == NULL)
13252 return NULL_TREE;
13253 if (strchr (fmt_str, target_percent) != NULL
13254 && strcmp (fmt_str, target_percent_s))
13255 return NULL_TREE;
13258 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13259 available. */
13260 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13261 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13262 if (!fn)
13263 return NULL_TREE;
13265 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13268 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13269 a normal call should be emitted rather than expanding the function
13270 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13271 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13272 passed as second argument. */
13274 tree
13275 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13276 enum built_in_function fcode)
13278 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13279 CALL_EXPR_ARGP (exp), maxlen, fcode);
13282 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13283 FMT and ARG are the arguments to the call; we don't fold cases with
13284 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13286 Return NULL_TREE if no simplification was possible, otherwise return the
13287 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13288 code of the function to be simplified. */
13290 static tree
13291 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13292 tree arg, bool ignore,
13293 enum built_in_function fcode)
13295 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13296 const char *fmt_str = NULL;
13298 /* If the return value is used, don't do the transformation. */
13299 if (! ignore)
13300 return NULL_TREE;
13302 /* Verify the required arguments in the original call. */
13303 if (!validate_arg (fmt, POINTER_TYPE))
13304 return NULL_TREE;
13306 /* Check whether the format is a literal string constant. */
13307 fmt_str = c_getstr (fmt);
13308 if (fmt_str == NULL)
13309 return NULL_TREE;
13311 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13313 /* If we're using an unlocked function, assume the other
13314 unlocked functions exist explicitly. */
13315 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13316 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13318 else
13320 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13321 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13324 if (!init_target_chars ())
13325 return NULL_TREE;
13327 if (strcmp (fmt_str, target_percent_s) == 0
13328 || strchr (fmt_str, target_percent) == NULL)
13330 const char *str;
13332 if (strcmp (fmt_str, target_percent_s) == 0)
13334 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13335 return NULL_TREE;
13337 if (!arg || !validate_arg (arg, POINTER_TYPE))
13338 return NULL_TREE;
13340 str = c_getstr (arg);
13341 if (str == NULL)
13342 return NULL_TREE;
13344 else
13346 /* The format specifier doesn't contain any '%' characters. */
13347 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13348 && arg)
13349 return NULL_TREE;
13350 str = fmt_str;
13353 /* If the string was "", printf does nothing. */
13354 if (str[0] == '\0')
13355 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13357 /* If the string has length of 1, call putchar. */
13358 if (str[1] == '\0')
13360 /* Given printf("c"), (where c is any one character,)
13361 convert "c"[0] to an int and pass that to the replacement
13362 function. */
13363 newarg = build_int_cst (integer_type_node, str[0]);
13364 if (fn_putchar)
13365 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13367 else
13369 /* If the string was "string\n", call puts("string"). */
13370 size_t len = strlen (str);
13371 if ((unsigned char)str[len - 1] == target_newline
13372 && (size_t) (int) len == len
13373 && (int) len > 0)
13375 char *newstr;
13376 tree offset_node, string_cst;
13378 /* Create a NUL-terminated string that's one char shorter
13379 than the original, stripping off the trailing '\n'. */
13380 newarg = build_string_literal (len, str);
13381 string_cst = string_constant (newarg, &offset_node);
13382 gcc_checking_assert (string_cst
13383 && (TREE_STRING_LENGTH (string_cst)
13384 == (int) len)
13385 && integer_zerop (offset_node)
13386 && (unsigned char)
13387 TREE_STRING_POINTER (string_cst)[len - 1]
13388 == target_newline);
13389 /* build_string_literal creates a new STRING_CST,
13390 modify it in place to avoid double copying. */
13391 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13392 newstr[len - 1] = '\0';
13393 if (fn_puts)
13394 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13396 else
13397 /* We'd like to arrange to call fputs(string,stdout) here,
13398 but we need stdout and don't have a way to get it yet. */
13399 return NULL_TREE;
13403 /* The other optimizations can be done only on the non-va_list variants. */
13404 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13405 return NULL_TREE;
13407 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13408 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13410 if (!arg || !validate_arg (arg, POINTER_TYPE))
13411 return NULL_TREE;
13412 if (fn_puts)
13413 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13416 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13417 else if (strcmp (fmt_str, target_percent_c) == 0)
13419 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13420 return NULL_TREE;
13421 if (fn_putchar)
13422 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13425 if (!call)
13426 return NULL_TREE;
13428 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13431 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13432 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13433 more than 3 arguments, and ARG may be null in the 2-argument case.
13435 Return NULL_TREE if no simplification was possible, otherwise return the
13436 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13437 code of the function to be simplified. */
13439 static tree
13440 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13441 tree fmt, tree arg, bool ignore,
13442 enum built_in_function fcode)
13444 tree fn_fputc, fn_fputs, call = NULL_TREE;
13445 const char *fmt_str = NULL;
13447 /* If the return value is used, don't do the transformation. */
13448 if (! ignore)
13449 return NULL_TREE;
13451 /* Verify the required arguments in the original call. */
13452 if (!validate_arg (fp, POINTER_TYPE))
13453 return NULL_TREE;
13454 if (!validate_arg (fmt, POINTER_TYPE))
13455 return NULL_TREE;
13457 /* Check whether the format is a literal string constant. */
13458 fmt_str = c_getstr (fmt);
13459 if (fmt_str == NULL)
13460 return NULL_TREE;
13462 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13464 /* If we're using an unlocked function, assume the other
13465 unlocked functions exist explicitly. */
13466 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13467 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13469 else
13471 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13472 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13475 if (!init_target_chars ())
13476 return NULL_TREE;
13478 /* If the format doesn't contain % args or %%, use strcpy. */
13479 if (strchr (fmt_str, target_percent) == NULL)
13481 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13482 && arg)
13483 return NULL_TREE;
13485 /* If the format specifier was "", fprintf does nothing. */
13486 if (fmt_str[0] == '\0')
13488 /* If FP has side-effects, just wait until gimplification is
13489 done. */
13490 if (TREE_SIDE_EFFECTS (fp))
13491 return NULL_TREE;
13493 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13496 /* When "string" doesn't contain %, replace all cases of
13497 fprintf (fp, string) with fputs (string, fp). The fputs
13498 builtin will take care of special cases like length == 1. */
13499 if (fn_fputs)
13500 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13503 /* The other optimizations can be done only on the non-va_list variants. */
13504 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13505 return NULL_TREE;
13507 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13508 else if (strcmp (fmt_str, target_percent_s) == 0)
13510 if (!arg || !validate_arg (arg, POINTER_TYPE))
13511 return NULL_TREE;
13512 if (fn_fputs)
13513 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13516 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13517 else if (strcmp (fmt_str, target_percent_c) == 0)
13519 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13520 return NULL_TREE;
13521 if (fn_fputc)
13522 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13525 if (!call)
13526 return NULL_TREE;
13527 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13530 /* Initialize format string characters in the target charset. */
13532 static bool
13533 init_target_chars (void)
13535 static bool init;
13536 if (!init)
13538 target_newline = lang_hooks.to_target_charset ('\n');
13539 target_percent = lang_hooks.to_target_charset ('%');
13540 target_c = lang_hooks.to_target_charset ('c');
13541 target_s = lang_hooks.to_target_charset ('s');
13542 if (target_newline == 0 || target_percent == 0 || target_c == 0
13543 || target_s == 0)
13544 return false;
13546 target_percent_c[0] = target_percent;
13547 target_percent_c[1] = target_c;
13548 target_percent_c[2] = '\0';
13550 target_percent_s[0] = target_percent;
13551 target_percent_s[1] = target_s;
13552 target_percent_s[2] = '\0';
13554 target_percent_s_newline[0] = target_percent;
13555 target_percent_s_newline[1] = target_s;
13556 target_percent_s_newline[2] = target_newline;
13557 target_percent_s_newline[3] = '\0';
13559 init = true;
13561 return true;
13564 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13565 and no overflow/underflow occurred. INEXACT is true if M was not
13566 exactly calculated. TYPE is the tree type for the result. This
13567 function assumes that you cleared the MPFR flags and then
13568 calculated M to see if anything subsequently set a flag prior to
13569 entering this function. Return NULL_TREE if any checks fail. */
13571 static tree
13572 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13574 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13575 overflow/underflow occurred. If -frounding-math, proceed iff the
13576 result of calling FUNC was exact. */
13577 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13578 && (!flag_rounding_math || !inexact))
13580 REAL_VALUE_TYPE rr;
13582 real_from_mpfr (&rr, m, type, GMP_RNDN);
13583 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13584 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13585 but the mpft_t is not, then we underflowed in the
13586 conversion. */
13587 if (real_isfinite (&rr)
13588 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13590 REAL_VALUE_TYPE rmode;
13592 real_convert (&rmode, TYPE_MODE (type), &rr);
13593 /* Proceed iff the specified mode can hold the value. */
13594 if (real_identical (&rmode, &rr))
13595 return build_real (type, rmode);
13598 return NULL_TREE;
13601 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13602 number and no overflow/underflow occurred. INEXACT is true if M
13603 was not exactly calculated. TYPE is the tree type for the result.
13604 This function assumes that you cleared the MPFR flags and then
13605 calculated M to see if anything subsequently set a flag prior to
13606 entering this function. Return NULL_TREE if any checks fail, if
13607 FORCE_CONVERT is true, then bypass the checks. */
13609 static tree
13610 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13612 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13613 overflow/underflow occurred. If -frounding-math, proceed iff the
13614 result of calling FUNC was exact. */
13615 if (force_convert
13616 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13617 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13618 && (!flag_rounding_math || !inexact)))
13620 REAL_VALUE_TYPE re, im;
13622 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13623 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13624 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13625 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13626 but the mpft_t is not, then we underflowed in the
13627 conversion. */
13628 if (force_convert
13629 || (real_isfinite (&re) && real_isfinite (&im)
13630 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13631 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13633 REAL_VALUE_TYPE re_mode, im_mode;
13635 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13636 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13637 /* Proceed iff the specified mode can hold the value. */
13638 if (force_convert
13639 || (real_identical (&re_mode, &re)
13640 && real_identical (&im_mode, &im)))
13641 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13642 build_real (TREE_TYPE (type), im_mode));
13645 return NULL_TREE;
13648 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13649 FUNC on it and return the resulting value as a tree with type TYPE.
13650 If MIN and/or MAX are not NULL, then the supplied ARG must be
13651 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13652 acceptable values, otherwise they are not. The mpfr precision is
13653 set to the precision of TYPE. We assume that function FUNC returns
13654 zero if the result could be calculated exactly within the requested
13655 precision. */
13657 static tree
13658 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13659 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13660 bool inclusive)
13662 tree result = NULL_TREE;
13664 STRIP_NOPS (arg);
13666 /* To proceed, MPFR must exactly represent the target floating point
13667 format, which only happens when the target base equals two. */
13668 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13669 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13671 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13673 if (real_isfinite (ra)
13674 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13675 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13677 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13678 const int prec = fmt->p;
13679 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13680 int inexact;
13681 mpfr_t m;
13683 mpfr_init2 (m, prec);
13684 mpfr_from_real (m, ra, GMP_RNDN);
13685 mpfr_clear_flags ();
13686 inexact = func (m, m, rnd);
13687 result = do_mpfr_ckconv (m, type, inexact);
13688 mpfr_clear (m);
13692 return result;
13695 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13696 FUNC on it and return the resulting value as a tree with type TYPE.
13697 The mpfr precision is set to the precision of TYPE. We assume that
13698 function FUNC returns zero if the result could be calculated
13699 exactly within the requested precision. */
13701 static tree
13702 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13703 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13705 tree result = NULL_TREE;
13707 STRIP_NOPS (arg1);
13708 STRIP_NOPS (arg2);
13710 /* To proceed, MPFR must exactly represent the target floating point
13711 format, which only happens when the target base equals two. */
13712 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13713 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13714 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13716 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13717 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13719 if (real_isfinite (ra1) && real_isfinite (ra2))
13721 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13722 const int prec = fmt->p;
13723 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13724 int inexact;
13725 mpfr_t m1, m2;
13727 mpfr_inits2 (prec, m1, m2, NULL);
13728 mpfr_from_real (m1, ra1, GMP_RNDN);
13729 mpfr_from_real (m2, ra2, GMP_RNDN);
13730 mpfr_clear_flags ();
13731 inexact = func (m1, m1, m2, rnd);
13732 result = do_mpfr_ckconv (m1, type, inexact);
13733 mpfr_clears (m1, m2, NULL);
13737 return result;
13740 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13741 FUNC on it and return the resulting value as a tree with type TYPE.
13742 The mpfr precision is set to the precision of TYPE. We assume that
13743 function FUNC returns zero if the result could be calculated
13744 exactly within the requested precision. */
13746 static tree
13747 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13748 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13750 tree result = NULL_TREE;
13752 STRIP_NOPS (arg1);
13753 STRIP_NOPS (arg2);
13754 STRIP_NOPS (arg3);
13756 /* To proceed, MPFR must exactly represent the target floating point
13757 format, which only happens when the target base equals two. */
13758 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13759 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13760 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13761 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13763 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13764 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13765 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13767 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13769 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13770 const int prec = fmt->p;
13771 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13772 int inexact;
13773 mpfr_t m1, m2, m3;
13775 mpfr_inits2 (prec, m1, m2, m3, NULL);
13776 mpfr_from_real (m1, ra1, GMP_RNDN);
13777 mpfr_from_real (m2, ra2, GMP_RNDN);
13778 mpfr_from_real (m3, ra3, GMP_RNDN);
13779 mpfr_clear_flags ();
13780 inexact = func (m1, m1, m2, m3, rnd);
13781 result = do_mpfr_ckconv (m1, type, inexact);
13782 mpfr_clears (m1, m2, m3, NULL);
13786 return result;
13789 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13790 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13791 If ARG_SINP and ARG_COSP are NULL then the result is returned
13792 as a complex value.
13793 The type is taken from the type of ARG and is used for setting the
13794 precision of the calculation and results. */
13796 static tree
13797 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13799 tree const type = TREE_TYPE (arg);
13800 tree result = NULL_TREE;
13802 STRIP_NOPS (arg);
13804 /* To proceed, MPFR must exactly represent the target floating point
13805 format, which only happens when the target base equals two. */
13806 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13807 && TREE_CODE (arg) == REAL_CST
13808 && !TREE_OVERFLOW (arg))
13810 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13812 if (real_isfinite (ra))
13814 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13815 const int prec = fmt->p;
13816 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13817 tree result_s, result_c;
13818 int inexact;
13819 mpfr_t m, ms, mc;
13821 mpfr_inits2 (prec, m, ms, mc, NULL);
13822 mpfr_from_real (m, ra, GMP_RNDN);
13823 mpfr_clear_flags ();
13824 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13825 result_s = do_mpfr_ckconv (ms, type, inexact);
13826 result_c = do_mpfr_ckconv (mc, type, inexact);
13827 mpfr_clears (m, ms, mc, NULL);
13828 if (result_s && result_c)
13830 /* If we are to return in a complex value do so. */
13831 if (!arg_sinp && !arg_cosp)
13832 return build_complex (build_complex_type (type),
13833 result_c, result_s);
13835 /* Dereference the sin/cos pointer arguments. */
13836 arg_sinp = build_fold_indirect_ref (arg_sinp);
13837 arg_cosp = build_fold_indirect_ref (arg_cosp);
13838 /* Proceed if valid pointer type were passed in. */
13839 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13840 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13842 /* Set the values. */
13843 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13844 result_s);
13845 TREE_SIDE_EFFECTS (result_s) = 1;
13846 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13847 result_c);
13848 TREE_SIDE_EFFECTS (result_c) = 1;
13849 /* Combine the assignments into a compound expr. */
13850 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13851 result_s, result_c));
13856 return result;
13859 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13860 two-argument mpfr order N Bessel function FUNC on them and return
13861 the resulting value as a tree with type TYPE. The mpfr precision
13862 is set to the precision of TYPE. We assume that function FUNC
13863 returns zero if the result could be calculated exactly within the
13864 requested precision. */
13865 static tree
13866 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13867 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13868 const REAL_VALUE_TYPE *min, bool inclusive)
13870 tree result = NULL_TREE;
13872 STRIP_NOPS (arg1);
13873 STRIP_NOPS (arg2);
13875 /* To proceed, MPFR must exactly represent the target floating point
13876 format, which only happens when the target base equals two. */
13877 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13878 && host_integerp (arg1, 0)
13879 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13881 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13882 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13884 if (n == (long)n
13885 && real_isfinite (ra)
13886 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13888 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13889 const int prec = fmt->p;
13890 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13891 int inexact;
13892 mpfr_t m;
13894 mpfr_init2 (m, prec);
13895 mpfr_from_real (m, ra, GMP_RNDN);
13896 mpfr_clear_flags ();
13897 inexact = func (m, n, m, rnd);
13898 result = do_mpfr_ckconv (m, type, inexact);
13899 mpfr_clear (m);
13903 return result;
13906 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13907 the pointer *(ARG_QUO) and return the result. The type is taken
13908 from the type of ARG0 and is used for setting the precision of the
13909 calculation and results. */
13911 static tree
13912 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13914 tree const type = TREE_TYPE (arg0);
13915 tree result = NULL_TREE;
13917 STRIP_NOPS (arg0);
13918 STRIP_NOPS (arg1);
13920 /* To proceed, MPFR must exactly represent the target floating point
13921 format, which only happens when the target base equals two. */
13922 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13923 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13924 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13926 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13927 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13929 if (real_isfinite (ra0) && real_isfinite (ra1))
13931 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13932 const int prec = fmt->p;
13933 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13934 tree result_rem;
13935 long integer_quo;
13936 mpfr_t m0, m1;
13938 mpfr_inits2 (prec, m0, m1, NULL);
13939 mpfr_from_real (m0, ra0, GMP_RNDN);
13940 mpfr_from_real (m1, ra1, GMP_RNDN);
13941 mpfr_clear_flags ();
13942 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13943 /* Remquo is independent of the rounding mode, so pass
13944 inexact=0 to do_mpfr_ckconv(). */
13945 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13946 mpfr_clears (m0, m1, NULL);
13947 if (result_rem)
13949 /* MPFR calculates quo in the host's long so it may
13950 return more bits in quo than the target int can hold
13951 if sizeof(host long) > sizeof(target int). This can
13952 happen even for native compilers in LP64 mode. In
13953 these cases, modulo the quo value with the largest
13954 number that the target int can hold while leaving one
13955 bit for the sign. */
13956 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13957 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13959 /* Dereference the quo pointer argument. */
13960 arg_quo = build_fold_indirect_ref (arg_quo);
13961 /* Proceed iff a valid pointer type was passed in. */
13962 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13964 /* Set the value. */
13965 tree result_quo
13966 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13967 build_int_cst (TREE_TYPE (arg_quo),
13968 integer_quo));
13969 TREE_SIDE_EFFECTS (result_quo) = 1;
13970 /* Combine the quo assignment with the rem. */
13971 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13972 result_quo, result_rem));
13977 return result;
13980 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13981 resulting value as a tree with type TYPE. The mpfr precision is
13982 set to the precision of TYPE. We assume that this mpfr function
13983 returns zero if the result could be calculated exactly within the
13984 requested precision. In addition, the integer pointer represented
13985 by ARG_SG will be dereferenced and set to the appropriate signgam
13986 (-1,1) value. */
13988 static tree
13989 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13991 tree result = NULL_TREE;
13993 STRIP_NOPS (arg);
13995 /* To proceed, MPFR must exactly represent the target floating point
13996 format, which only happens when the target base equals two. Also
13997 verify ARG is a constant and that ARG_SG is an int pointer. */
13998 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13999 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
14000 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
14001 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
14003 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
14005 /* In addition to NaN and Inf, the argument cannot be zero or a
14006 negative integer. */
14007 if (real_isfinite (ra)
14008 && ra->cl != rvc_zero
14009 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
14011 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14012 const int prec = fmt->p;
14013 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14014 int inexact, sg;
14015 mpfr_t m;
14016 tree result_lg;
14018 mpfr_init2 (m, prec);
14019 mpfr_from_real (m, ra, GMP_RNDN);
14020 mpfr_clear_flags ();
14021 inexact = mpfr_lgamma (m, &sg, m, rnd);
14022 result_lg = do_mpfr_ckconv (m, type, inexact);
14023 mpfr_clear (m);
14024 if (result_lg)
14026 tree result_sg;
14028 /* Dereference the arg_sg pointer argument. */
14029 arg_sg = build_fold_indirect_ref (arg_sg);
14030 /* Assign the signgam value into *arg_sg. */
14031 result_sg = fold_build2 (MODIFY_EXPR,
14032 TREE_TYPE (arg_sg), arg_sg,
14033 build_int_cst (TREE_TYPE (arg_sg), sg));
14034 TREE_SIDE_EFFECTS (result_sg) = 1;
14035 /* Combine the signgam assignment with the lgamma result. */
14036 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14037 result_sg, result_lg));
14042 return result;
14045 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14046 function FUNC on it and return the resulting value as a tree with
14047 type TYPE. The mpfr precision is set to the precision of TYPE. We
14048 assume that function FUNC returns zero if the result could be
14049 calculated exactly within the requested precision. */
14051 static tree
14052 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14054 tree result = NULL_TREE;
14056 STRIP_NOPS (arg);
14058 /* To proceed, MPFR must exactly represent the target floating point
14059 format, which only happens when the target base equals two. */
14060 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14061 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14062 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14064 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14065 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14067 if (real_isfinite (re) && real_isfinite (im))
14069 const struct real_format *const fmt =
14070 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14071 const int prec = fmt->p;
14072 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14073 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14074 int inexact;
14075 mpc_t m;
14077 mpc_init2 (m, prec);
14078 mpfr_from_real (mpc_realref(m), re, rnd);
14079 mpfr_from_real (mpc_imagref(m), im, rnd);
14080 mpfr_clear_flags ();
14081 inexact = func (m, m, crnd);
14082 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14083 mpc_clear (m);
14087 return result;
14090 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14091 mpc function FUNC on it and return the resulting value as a tree
14092 with type TYPE. The mpfr precision is set to the precision of
14093 TYPE. We assume that function FUNC returns zero if the result
14094 could be calculated exactly within the requested precision. If
14095 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14096 in the arguments and/or results. */
14098 tree
14099 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14100 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14102 tree result = NULL_TREE;
14104 STRIP_NOPS (arg0);
14105 STRIP_NOPS (arg1);
14107 /* To proceed, MPFR must exactly represent the target floating point
14108 format, which only happens when the target base equals two. */
14109 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14110 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14111 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14112 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14113 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14115 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14116 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14117 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14118 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14120 if (do_nonfinite
14121 || (real_isfinite (re0) && real_isfinite (im0)
14122 && real_isfinite (re1) && real_isfinite (im1)))
14124 const struct real_format *const fmt =
14125 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14126 const int prec = fmt->p;
14127 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14128 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14129 int inexact;
14130 mpc_t m0, m1;
14132 mpc_init2 (m0, prec);
14133 mpc_init2 (m1, prec);
14134 mpfr_from_real (mpc_realref(m0), re0, rnd);
14135 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14136 mpfr_from_real (mpc_realref(m1), re1, rnd);
14137 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14138 mpfr_clear_flags ();
14139 inexact = func (m0, m0, m1, crnd);
14140 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14141 mpc_clear (m0);
14142 mpc_clear (m1);
14146 return result;
14149 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14150 a normal call should be emitted rather than expanding the function
14151 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14153 static tree
14154 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14156 int nargs = gimple_call_num_args (stmt);
14158 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14159 (nargs > 0
14160 ? gimple_call_arg_ptr (stmt, 0)
14161 : &error_mark_node), fcode);
14164 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14165 a normal call should be emitted rather than expanding the function
14166 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14167 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14168 passed as second argument. */
14170 tree
14171 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14172 enum built_in_function fcode)
14174 int nargs = gimple_call_num_args (stmt);
14176 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14177 (nargs > 0
14178 ? gimple_call_arg_ptr (stmt, 0)
14179 : &error_mark_node), maxlen, fcode);
14182 /* Builtins with folding operations that operate on "..." arguments
14183 need special handling; we need to store the arguments in a convenient
14184 data structure before attempting any folding. Fortunately there are
14185 only a few builtins that fall into this category. FNDECL is the
14186 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14187 result of the function call is ignored. */
14189 static tree
14190 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14191 bool ignore ATTRIBUTE_UNUSED)
14193 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14194 tree ret = NULL_TREE;
14196 switch (fcode)
14198 case BUILT_IN_SPRINTF_CHK:
14199 case BUILT_IN_VSPRINTF_CHK:
14200 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14201 break;
14203 case BUILT_IN_SNPRINTF_CHK:
14204 case BUILT_IN_VSNPRINTF_CHK:
14205 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14207 default:
14208 break;
14210 if (ret)
14212 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14213 TREE_NO_WARNING (ret) = 1;
14214 return ret;
14216 return NULL_TREE;
14219 /* A wrapper function for builtin folding that prevents warnings for
14220 "statement without effect" and the like, caused by removing the
14221 call node earlier than the warning is generated. */
14223 tree
14224 fold_call_stmt (gimple stmt, bool ignore)
14226 tree ret = NULL_TREE;
14227 tree fndecl = gimple_call_fndecl (stmt);
14228 location_t loc = gimple_location (stmt);
14229 if (fndecl
14230 && TREE_CODE (fndecl) == FUNCTION_DECL
14231 && DECL_BUILT_IN (fndecl)
14232 && !gimple_call_va_arg_pack_p (stmt))
14234 int nargs = gimple_call_num_args (stmt);
14235 tree *args = (nargs > 0
14236 ? gimple_call_arg_ptr (stmt, 0)
14237 : &error_mark_node);
14239 if (avoid_folding_inline_builtin (fndecl))
14240 return NULL_TREE;
14241 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14243 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14245 else
14247 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14248 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14249 if (!ret)
14250 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14251 if (ret)
14253 /* Propagate location information from original call to
14254 expansion of builtin. Otherwise things like
14255 maybe_emit_chk_warning, that operate on the expansion
14256 of a builtin, will use the wrong location information. */
14257 if (gimple_has_location (stmt))
14259 tree realret = ret;
14260 if (TREE_CODE (ret) == NOP_EXPR)
14261 realret = TREE_OPERAND (ret, 0);
14262 if (CAN_HAVE_LOCATION_P (realret)
14263 && !EXPR_HAS_LOCATION (realret))
14264 SET_EXPR_LOCATION (realret, loc);
14265 return realret;
14267 return ret;
14271 return NULL_TREE;
14274 /* Look up the function in builtin_decl that corresponds to DECL
14275 and set ASMSPEC as its user assembler name. DECL must be a
14276 function decl that declares a builtin. */
14278 void
14279 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14281 tree builtin;
14282 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14283 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14284 && asmspec != 0);
14286 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14287 set_user_assembler_name (builtin, asmspec);
14288 switch (DECL_FUNCTION_CODE (decl))
14290 case BUILT_IN_MEMCPY:
14291 init_block_move_fn (asmspec);
14292 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14293 break;
14294 case BUILT_IN_MEMSET:
14295 init_block_clear_fn (asmspec);
14296 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14297 break;
14298 case BUILT_IN_MEMMOVE:
14299 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14300 break;
14301 case BUILT_IN_MEMCMP:
14302 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14303 break;
14304 case BUILT_IN_ABORT:
14305 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14306 break;
14307 case BUILT_IN_FFS:
14308 if (INT_TYPE_SIZE < BITS_PER_WORD)
14310 set_user_assembler_libfunc ("ffs", asmspec);
14311 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14312 MODE_INT, 0), "ffs");
14314 break;
14315 default:
14316 break;
14320 /* Return true if DECL is a builtin that expands to a constant or similarly
14321 simple code. */
14322 bool
14323 is_simple_builtin (tree decl)
14325 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14326 switch (DECL_FUNCTION_CODE (decl))
14328 /* Builtins that expand to constants. */
14329 case BUILT_IN_CONSTANT_P:
14330 case BUILT_IN_EXPECT:
14331 case BUILT_IN_OBJECT_SIZE:
14332 case BUILT_IN_UNREACHABLE:
14333 /* Simple register moves or loads from stack. */
14334 case BUILT_IN_ASSUME_ALIGNED:
14335 case BUILT_IN_RETURN_ADDRESS:
14336 case BUILT_IN_EXTRACT_RETURN_ADDR:
14337 case BUILT_IN_FROB_RETURN_ADDR:
14338 case BUILT_IN_RETURN:
14339 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14340 case BUILT_IN_FRAME_ADDRESS:
14341 case BUILT_IN_VA_END:
14342 case BUILT_IN_STACK_SAVE:
14343 case BUILT_IN_STACK_RESTORE:
14344 /* Exception state returns or moves registers around. */
14345 case BUILT_IN_EH_FILTER:
14346 case BUILT_IN_EH_POINTER:
14347 case BUILT_IN_EH_COPY_VALUES:
14348 return true;
14350 default:
14351 return false;
14354 return false;
14357 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14358 most probably expanded inline into reasonably simple code. This is a
14359 superset of is_simple_builtin. */
14360 bool
14361 is_inexpensive_builtin (tree decl)
14363 if (!decl)
14364 return false;
14365 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14366 return true;
14367 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14368 switch (DECL_FUNCTION_CODE (decl))
14370 case BUILT_IN_ABS:
14371 case BUILT_IN_ALLOCA:
14372 case BUILT_IN_ALLOCA_WITH_ALIGN:
14373 case BUILT_IN_BSWAP16:
14374 case BUILT_IN_BSWAP32:
14375 case BUILT_IN_BSWAP64:
14376 case BUILT_IN_CLZ:
14377 case BUILT_IN_CLZIMAX:
14378 case BUILT_IN_CLZL:
14379 case BUILT_IN_CLZLL:
14380 case BUILT_IN_CTZ:
14381 case BUILT_IN_CTZIMAX:
14382 case BUILT_IN_CTZL:
14383 case BUILT_IN_CTZLL:
14384 case BUILT_IN_FFS:
14385 case BUILT_IN_FFSIMAX:
14386 case BUILT_IN_FFSL:
14387 case BUILT_IN_FFSLL:
14388 case BUILT_IN_IMAXABS:
14389 case BUILT_IN_FINITE:
14390 case BUILT_IN_FINITEF:
14391 case BUILT_IN_FINITEL:
14392 case BUILT_IN_FINITED32:
14393 case BUILT_IN_FINITED64:
14394 case BUILT_IN_FINITED128:
14395 case BUILT_IN_FPCLASSIFY:
14396 case BUILT_IN_ISFINITE:
14397 case BUILT_IN_ISINF_SIGN:
14398 case BUILT_IN_ISINF:
14399 case BUILT_IN_ISINFF:
14400 case BUILT_IN_ISINFL:
14401 case BUILT_IN_ISINFD32:
14402 case BUILT_IN_ISINFD64:
14403 case BUILT_IN_ISINFD128:
14404 case BUILT_IN_ISNAN:
14405 case BUILT_IN_ISNANF:
14406 case BUILT_IN_ISNANL:
14407 case BUILT_IN_ISNAND32:
14408 case BUILT_IN_ISNAND64:
14409 case BUILT_IN_ISNAND128:
14410 case BUILT_IN_ISNORMAL:
14411 case BUILT_IN_ISGREATER:
14412 case BUILT_IN_ISGREATEREQUAL:
14413 case BUILT_IN_ISLESS:
14414 case BUILT_IN_ISLESSEQUAL:
14415 case BUILT_IN_ISLESSGREATER:
14416 case BUILT_IN_ISUNORDERED:
14417 case BUILT_IN_VA_ARG_PACK:
14418 case BUILT_IN_VA_ARG_PACK_LEN:
14419 case BUILT_IN_VA_COPY:
14420 case BUILT_IN_TRAP:
14421 case BUILT_IN_SAVEREGS:
14422 case BUILT_IN_POPCOUNTL:
14423 case BUILT_IN_POPCOUNTLL:
14424 case BUILT_IN_POPCOUNTIMAX:
14425 case BUILT_IN_POPCOUNT:
14426 case BUILT_IN_PARITYL:
14427 case BUILT_IN_PARITYLL:
14428 case BUILT_IN_PARITYIMAX:
14429 case BUILT_IN_PARITY:
14430 case BUILT_IN_LABS:
14431 case BUILT_IN_LLABS:
14432 case BUILT_IN_PREFETCH:
14433 return true;
14435 default:
14436 return is_simple_builtin (decl);
14439 return false;