2012-12-14 Steve Ellcey <sellcey@mips.com>
[official-gcc.git] / gcc / builtins.c
blobfb7b537ca2c4e487c2b91ef83497957dc7caa66c
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. Similarly, we must block
955 (frame-related) register values to be used across this code. */
956 emit_insn (gen_blockage ());
959 /* __builtin_longjmp is passed a pointer to an array of five words (not
960 all will be used on all machines). It operates similarly to the C
961 library function of the same name, but is more efficient. Much of
962 the code below is copied from the handling of non-local gotos. */
964 static void
965 expand_builtin_longjmp (rtx buf_addr, rtx value)
967 rtx fp, lab, stack, insn, last;
968 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
970 /* DRAP is needed for stack realign if longjmp is expanded to current
971 function */
972 if (SUPPORTS_STACK_ALIGNMENT)
973 crtl->need_drap = true;
975 if (setjmp_alias_set == -1)
976 setjmp_alias_set = new_alias_set ();
978 buf_addr = convert_memory_address (Pmode, buf_addr);
980 buf_addr = force_reg (Pmode, buf_addr);
982 /* We require that the user must pass a second argument of 1, because
983 that is what builtin_setjmp will return. */
984 gcc_assert (value == const1_rtx);
986 last = get_last_insn ();
987 #ifdef HAVE_builtin_longjmp
988 if (HAVE_builtin_longjmp)
989 emit_insn (gen_builtin_longjmp (buf_addr));
990 else
991 #endif
993 fp = gen_rtx_MEM (Pmode, buf_addr);
994 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
995 GET_MODE_SIZE (Pmode)));
997 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
998 2 * GET_MODE_SIZE (Pmode)));
999 set_mem_alias_set (fp, setjmp_alias_set);
1000 set_mem_alias_set (lab, setjmp_alias_set);
1001 set_mem_alias_set (stack, setjmp_alias_set);
1003 /* Pick up FP, label, and SP from the block and jump. This code is
1004 from expand_goto in stmt.c; see there for detailed comments. */
1005 #ifdef HAVE_nonlocal_goto
1006 if (HAVE_nonlocal_goto)
1007 /* We have to pass a value to the nonlocal_goto pattern that will
1008 get copied into the static_chain pointer, but it does not matter
1009 what that value is, because builtin_setjmp does not use it. */
1010 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1011 else
1012 #endif
1014 lab = copy_to_reg (lab);
1016 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1017 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1019 emit_move_insn (hard_frame_pointer_rtx, fp);
1020 emit_stack_restore (SAVE_NONLOCAL, stack);
1022 emit_use (hard_frame_pointer_rtx);
1023 emit_use (stack_pointer_rtx);
1024 emit_indirect_jump (lab);
1028 /* Search backwards and mark the jump insn as a non-local goto.
1029 Note that this precludes the use of __builtin_longjmp to a
1030 __builtin_setjmp target in the same function. However, we've
1031 already cautioned the user that these functions are for
1032 internal exception handling use only. */
1033 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1035 gcc_assert (insn != last);
1037 if (JUMP_P (insn))
1039 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1040 break;
1042 else if (CALL_P (insn))
1043 break;
1047 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1048 and the address of the save area. */
1050 static rtx
1051 expand_builtin_nonlocal_goto (tree exp)
1053 tree t_label, t_save_area;
1054 rtx r_label, r_save_area, r_fp, r_sp, insn;
1056 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1057 return NULL_RTX;
1059 t_label = CALL_EXPR_ARG (exp, 0);
1060 t_save_area = CALL_EXPR_ARG (exp, 1);
1062 r_label = expand_normal (t_label);
1063 r_label = convert_memory_address (Pmode, r_label);
1064 r_save_area = expand_normal (t_save_area);
1065 r_save_area = convert_memory_address (Pmode, r_save_area);
1066 /* Copy the address of the save location to a register just in case it was
1067 based on the frame pointer. */
1068 r_save_area = copy_to_reg (r_save_area);
1069 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1070 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1071 plus_constant (Pmode, r_save_area,
1072 GET_MODE_SIZE (Pmode)));
1074 crtl->has_nonlocal_goto = 1;
1076 #ifdef HAVE_nonlocal_goto
1077 /* ??? We no longer need to pass the static chain value, afaik. */
1078 if (HAVE_nonlocal_goto)
1079 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1080 else
1081 #endif
1083 r_label = copy_to_reg (r_label);
1085 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1086 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1088 /* Restore frame pointer for containing function. */
1089 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1090 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1092 /* USE of hard_frame_pointer_rtx added for consistency;
1093 not clear if really needed. */
1094 emit_use (hard_frame_pointer_rtx);
1095 emit_use (stack_pointer_rtx);
1097 /* If the architecture is using a GP register, we must
1098 conservatively assume that the target function makes use of it.
1099 The prologue of functions with nonlocal gotos must therefore
1100 initialize the GP register to the appropriate value, and we
1101 must then make sure that this value is live at the point
1102 of the jump. (Note that this doesn't necessarily apply
1103 to targets with a nonlocal_goto pattern; they are free
1104 to implement it in their own way. Note also that this is
1105 a no-op if the GP register is a global invariant.) */
1106 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1107 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1108 emit_use (pic_offset_table_rtx);
1110 emit_indirect_jump (r_label);
1113 /* Search backwards to the jump insn and mark it as a
1114 non-local goto. */
1115 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1117 if (JUMP_P (insn))
1119 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1120 break;
1122 else if (CALL_P (insn))
1123 break;
1126 return const0_rtx;
1129 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1130 (not all will be used on all machines) that was passed to __builtin_setjmp.
1131 It updates the stack pointer in that block to correspond to the current
1132 stack pointer. */
1134 static void
1135 expand_builtin_update_setjmp_buf (rtx buf_addr)
1137 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1138 rtx stack_save
1139 = gen_rtx_MEM (sa_mode,
1140 memory_address
1141 (sa_mode,
1142 plus_constant (Pmode, buf_addr,
1143 2 * GET_MODE_SIZE (Pmode))));
1145 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1148 /* Expand a call to __builtin_prefetch. For a target that does not support
1149 data prefetch, evaluate the memory address argument in case it has side
1150 effects. */
1152 static void
1153 expand_builtin_prefetch (tree exp)
1155 tree arg0, arg1, arg2;
1156 int nargs;
1157 rtx op0, op1, op2;
1159 if (!validate_arglist (exp, POINTER_TYPE, 0))
1160 return;
1162 arg0 = CALL_EXPR_ARG (exp, 0);
1164 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1165 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1166 locality). */
1167 nargs = call_expr_nargs (exp);
1168 if (nargs > 1)
1169 arg1 = CALL_EXPR_ARG (exp, 1);
1170 else
1171 arg1 = integer_zero_node;
1172 if (nargs > 2)
1173 arg2 = CALL_EXPR_ARG (exp, 2);
1174 else
1175 arg2 = integer_three_node;
1177 /* Argument 0 is an address. */
1178 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1180 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1181 if (TREE_CODE (arg1) != INTEGER_CST)
1183 error ("second argument to %<__builtin_prefetch%> must be a constant");
1184 arg1 = integer_zero_node;
1186 op1 = expand_normal (arg1);
1187 /* Argument 1 must be either zero or one. */
1188 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1190 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1191 " using zero");
1192 op1 = const0_rtx;
1195 /* Argument 2 (locality) must be a compile-time constant int. */
1196 if (TREE_CODE (arg2) != INTEGER_CST)
1198 error ("third argument to %<__builtin_prefetch%> must be a constant");
1199 arg2 = integer_zero_node;
1201 op2 = expand_normal (arg2);
1202 /* Argument 2 must be 0, 1, 2, or 3. */
1203 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1205 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1206 op2 = const0_rtx;
1209 #ifdef HAVE_prefetch
1210 if (HAVE_prefetch)
1212 struct expand_operand ops[3];
1214 create_address_operand (&ops[0], op0);
1215 create_integer_operand (&ops[1], INTVAL (op1));
1216 create_integer_operand (&ops[2], INTVAL (op2));
1217 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1218 return;
1220 #endif
1222 /* Don't do anything with direct references to volatile memory, but
1223 generate code to handle other side effects. */
1224 if (!MEM_P (op0) && side_effects_p (op0))
1225 emit_insn (op0);
1228 /* Get a MEM rtx for expression EXP which is the address of an operand
1229 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1230 the maximum length of the block of memory that might be accessed or
1231 NULL if unknown. */
1233 static rtx
1234 get_memory_rtx (tree exp, tree len)
1236 tree orig_exp = exp;
1237 rtx addr, mem;
1239 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1240 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1241 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1242 exp = TREE_OPERAND (exp, 0);
1244 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1245 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1247 /* Get an expression we can use to find the attributes to assign to MEM.
1248 First remove any nops. */
1249 while (CONVERT_EXPR_P (exp)
1250 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1251 exp = TREE_OPERAND (exp, 0);
1253 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1254 (as builtin stringops may alias with anything). */
1255 exp = fold_build2 (MEM_REF,
1256 build_array_type (char_type_node,
1257 build_range_type (sizetype,
1258 size_one_node, len)),
1259 exp, build_int_cst (ptr_type_node, 0));
1261 /* If the MEM_REF has no acceptable address, try to get the base object
1262 from the original address we got, and build an all-aliasing
1263 unknown-sized access to that one. */
1264 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1265 set_mem_attributes (mem, exp, 0);
1266 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1267 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1268 0))))
1270 exp = build_fold_addr_expr (exp);
1271 exp = fold_build2 (MEM_REF,
1272 build_array_type (char_type_node,
1273 build_range_type (sizetype,
1274 size_zero_node,
1275 NULL)),
1276 exp, build_int_cst (ptr_type_node, 0));
1277 set_mem_attributes (mem, exp, 0);
1279 set_mem_alias_set (mem, 0);
1280 return mem;
1283 /* Built-in functions to perform an untyped call and return. */
1285 #define apply_args_mode \
1286 (this_target_builtins->x_apply_args_mode)
1287 #define apply_result_mode \
1288 (this_target_builtins->x_apply_result_mode)
1290 /* Return the size required for the block returned by __builtin_apply_args,
1291 and initialize apply_args_mode. */
1293 static int
1294 apply_args_size (void)
1296 static int size = -1;
1297 int align;
1298 unsigned int regno;
1299 enum machine_mode mode;
1301 /* The values computed by this function never change. */
1302 if (size < 0)
1304 /* The first value is the incoming arg-pointer. */
1305 size = GET_MODE_SIZE (Pmode);
1307 /* The second value is the structure value address unless this is
1308 passed as an "invisible" first argument. */
1309 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1310 size += GET_MODE_SIZE (Pmode);
1312 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1313 if (FUNCTION_ARG_REGNO_P (regno))
1315 mode = targetm.calls.get_raw_arg_mode (regno);
1317 gcc_assert (mode != VOIDmode);
1319 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1320 if (size % align != 0)
1321 size = CEIL (size, align) * align;
1322 size += GET_MODE_SIZE (mode);
1323 apply_args_mode[regno] = mode;
1325 else
1327 apply_args_mode[regno] = VOIDmode;
1330 return size;
1333 /* Return the size required for the block returned by __builtin_apply,
1334 and initialize apply_result_mode. */
1336 static int
1337 apply_result_size (void)
1339 static int size = -1;
1340 int align, regno;
1341 enum machine_mode mode;
1343 /* The values computed by this function never change. */
1344 if (size < 0)
1346 size = 0;
1348 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1349 if (targetm.calls.function_value_regno_p (regno))
1351 mode = targetm.calls.get_raw_result_mode (regno);
1353 gcc_assert (mode != VOIDmode);
1355 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1356 if (size % align != 0)
1357 size = CEIL (size, align) * align;
1358 size += GET_MODE_SIZE (mode);
1359 apply_result_mode[regno] = mode;
1361 else
1362 apply_result_mode[regno] = VOIDmode;
1364 /* Allow targets that use untyped_call and untyped_return to override
1365 the size so that machine-specific information can be stored here. */
1366 #ifdef APPLY_RESULT_SIZE
1367 size = APPLY_RESULT_SIZE;
1368 #endif
1370 return size;
1373 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1374 /* Create a vector describing the result block RESULT. If SAVEP is true,
1375 the result block is used to save the values; otherwise it is used to
1376 restore the values. */
1378 static rtx
1379 result_vector (int savep, rtx result)
1381 int regno, size, align, nelts;
1382 enum machine_mode mode;
1383 rtx reg, mem;
1384 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1386 size = nelts = 0;
1387 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1388 if ((mode = apply_result_mode[regno]) != VOIDmode)
1390 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1391 if (size % align != 0)
1392 size = CEIL (size, align) * align;
1393 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1394 mem = adjust_address (result, mode, size);
1395 savevec[nelts++] = (savep
1396 ? gen_rtx_SET (VOIDmode, mem, reg)
1397 : gen_rtx_SET (VOIDmode, reg, mem));
1398 size += GET_MODE_SIZE (mode);
1400 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1402 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1404 /* Save the state required to perform an untyped call with the same
1405 arguments as were passed to the current function. */
1407 static rtx
1408 expand_builtin_apply_args_1 (void)
1410 rtx registers, tem;
1411 int size, align, regno;
1412 enum machine_mode mode;
1413 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1415 /* Create a block where the arg-pointer, structure value address,
1416 and argument registers can be saved. */
1417 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1419 /* Walk past the arg-pointer and structure value address. */
1420 size = GET_MODE_SIZE (Pmode);
1421 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1422 size += GET_MODE_SIZE (Pmode);
1424 /* Save each register used in calling a function to the block. */
1425 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1426 if ((mode = apply_args_mode[regno]) != VOIDmode)
1428 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1429 if (size % align != 0)
1430 size = CEIL (size, align) * align;
1432 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1434 emit_move_insn (adjust_address (registers, mode, size), tem);
1435 size += GET_MODE_SIZE (mode);
1438 /* Save the arg pointer to the block. */
1439 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1440 #ifdef STACK_GROWS_DOWNWARD
1441 /* We need the pointer as the caller actually passed them to us, not
1442 as we might have pretended they were passed. Make sure it's a valid
1443 operand, as emit_move_insn isn't expected to handle a PLUS. */
1445 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1446 NULL_RTX);
1447 #endif
1448 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1450 size = GET_MODE_SIZE (Pmode);
1452 /* Save the structure value address unless this is passed as an
1453 "invisible" first argument. */
1454 if (struct_incoming_value)
1456 emit_move_insn (adjust_address (registers, Pmode, size),
1457 copy_to_reg (struct_incoming_value));
1458 size += GET_MODE_SIZE (Pmode);
1461 /* Return the address of the block. */
1462 return copy_addr_to_reg (XEXP (registers, 0));
1465 /* __builtin_apply_args returns block of memory allocated on
1466 the stack into which is stored the arg pointer, structure
1467 value address, static chain, and all the registers that might
1468 possibly be used in performing a function call. The code is
1469 moved to the start of the function so the incoming values are
1470 saved. */
1472 static rtx
1473 expand_builtin_apply_args (void)
1475 /* Don't do __builtin_apply_args more than once in a function.
1476 Save the result of the first call and reuse it. */
1477 if (apply_args_value != 0)
1478 return apply_args_value;
1480 /* When this function is called, it means that registers must be
1481 saved on entry to this function. So we migrate the
1482 call to the first insn of this function. */
1483 rtx temp;
1484 rtx seq;
1486 start_sequence ();
1487 temp = expand_builtin_apply_args_1 ();
1488 seq = get_insns ();
1489 end_sequence ();
1491 apply_args_value = temp;
1493 /* Put the insns after the NOTE that starts the function.
1494 If this is inside a start_sequence, make the outer-level insn
1495 chain current, so the code is placed at the start of the
1496 function. If internal_arg_pointer is a non-virtual pseudo,
1497 it needs to be placed after the function that initializes
1498 that pseudo. */
1499 push_topmost_sequence ();
1500 if (REG_P (crtl->args.internal_arg_pointer)
1501 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1502 emit_insn_before (seq, parm_birth_insn);
1503 else
1504 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1505 pop_topmost_sequence ();
1506 return temp;
1510 /* Perform an untyped call and save the state required to perform an
1511 untyped return of whatever value was returned by the given function. */
1513 static rtx
1514 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1516 int size, align, regno;
1517 enum machine_mode mode;
1518 rtx incoming_args, result, reg, dest, src, call_insn;
1519 rtx old_stack_level = 0;
1520 rtx call_fusage = 0;
1521 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1523 arguments = convert_memory_address (Pmode, arguments);
1525 /* Create a block where the return registers can be saved. */
1526 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1528 /* Fetch the arg pointer from the ARGUMENTS block. */
1529 incoming_args = gen_reg_rtx (Pmode);
1530 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1531 #ifndef STACK_GROWS_DOWNWARD
1532 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1533 incoming_args, 0, OPTAB_LIB_WIDEN);
1534 #endif
1536 /* Push a new argument block and copy the arguments. Do not allow
1537 the (potential) memcpy call below to interfere with our stack
1538 manipulations. */
1539 do_pending_stack_adjust ();
1540 NO_DEFER_POP;
1542 /* Save the stack with nonlocal if available. */
1543 #ifdef HAVE_save_stack_nonlocal
1544 if (HAVE_save_stack_nonlocal)
1545 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1546 else
1547 #endif
1548 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1550 /* Allocate a block of memory onto the stack and copy the memory
1551 arguments to the outgoing arguments address. We can pass TRUE
1552 as the 4th argument because we just saved the stack pointer
1553 and will restore it right after the call. */
1554 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1556 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1557 may have already set current_function_calls_alloca to true.
1558 current_function_calls_alloca won't be set if argsize is zero,
1559 so we have to guarantee need_drap is true here. */
1560 if (SUPPORTS_STACK_ALIGNMENT)
1561 crtl->need_drap = true;
1563 dest = virtual_outgoing_args_rtx;
1564 #ifndef STACK_GROWS_DOWNWARD
1565 if (CONST_INT_P (argsize))
1566 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1567 else
1568 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1569 #endif
1570 dest = gen_rtx_MEM (BLKmode, dest);
1571 set_mem_align (dest, PARM_BOUNDARY);
1572 src = gen_rtx_MEM (BLKmode, incoming_args);
1573 set_mem_align (src, PARM_BOUNDARY);
1574 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1576 /* Refer to the argument block. */
1577 apply_args_size ();
1578 arguments = gen_rtx_MEM (BLKmode, arguments);
1579 set_mem_align (arguments, PARM_BOUNDARY);
1581 /* Walk past the arg-pointer and structure value address. */
1582 size = GET_MODE_SIZE (Pmode);
1583 if (struct_value)
1584 size += GET_MODE_SIZE (Pmode);
1586 /* Restore each of the registers previously saved. Make USE insns
1587 for each of these registers for use in making the call. */
1588 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1589 if ((mode = apply_args_mode[regno]) != VOIDmode)
1591 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1592 if (size % align != 0)
1593 size = CEIL (size, align) * align;
1594 reg = gen_rtx_REG (mode, regno);
1595 emit_move_insn (reg, adjust_address (arguments, mode, size));
1596 use_reg (&call_fusage, reg);
1597 size += GET_MODE_SIZE (mode);
1600 /* Restore the structure value address unless this is passed as an
1601 "invisible" first argument. */
1602 size = GET_MODE_SIZE (Pmode);
1603 if (struct_value)
1605 rtx value = gen_reg_rtx (Pmode);
1606 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1607 emit_move_insn (struct_value, value);
1608 if (REG_P (struct_value))
1609 use_reg (&call_fusage, struct_value);
1610 size += GET_MODE_SIZE (Pmode);
1613 /* All arguments and registers used for the call are set up by now! */
1614 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1616 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1617 and we don't want to load it into a register as an optimization,
1618 because prepare_call_address already did it if it should be done. */
1619 if (GET_CODE (function) != SYMBOL_REF)
1620 function = memory_address (FUNCTION_MODE, function);
1622 /* Generate the actual call instruction and save the return value. */
1623 #ifdef HAVE_untyped_call
1624 if (HAVE_untyped_call)
1625 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1626 result, result_vector (1, result)));
1627 else
1628 #endif
1629 #ifdef HAVE_call_value
1630 if (HAVE_call_value)
1632 rtx valreg = 0;
1634 /* Locate the unique return register. It is not possible to
1635 express a call that sets more than one return register using
1636 call_value; use untyped_call for that. In fact, untyped_call
1637 only needs to save the return registers in the given block. */
1638 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1639 if ((mode = apply_result_mode[regno]) != VOIDmode)
1641 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1643 valreg = gen_rtx_REG (mode, regno);
1646 emit_call_insn (GEN_CALL_VALUE (valreg,
1647 gen_rtx_MEM (FUNCTION_MODE, function),
1648 const0_rtx, NULL_RTX, const0_rtx));
1650 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1652 else
1653 #endif
1654 gcc_unreachable ();
1656 /* Find the CALL insn we just emitted, and attach the register usage
1657 information. */
1658 call_insn = last_call_insn ();
1659 add_function_usage_to (call_insn, call_fusage);
1661 /* Restore the stack. */
1662 #ifdef HAVE_save_stack_nonlocal
1663 if (HAVE_save_stack_nonlocal)
1664 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1665 else
1666 #endif
1667 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1668 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1670 OK_DEFER_POP;
1672 /* Return the address of the result block. */
1673 result = copy_addr_to_reg (XEXP (result, 0));
1674 return convert_memory_address (ptr_mode, result);
1677 /* Perform an untyped return. */
1679 static void
1680 expand_builtin_return (rtx result)
1682 int size, align, regno;
1683 enum machine_mode mode;
1684 rtx reg;
1685 rtx call_fusage = 0;
1687 result = convert_memory_address (Pmode, result);
1689 apply_result_size ();
1690 result = gen_rtx_MEM (BLKmode, result);
1692 #ifdef HAVE_untyped_return
1693 if (HAVE_untyped_return)
1695 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1696 emit_barrier ();
1697 return;
1699 #endif
1701 /* Restore the return value and note that each value is used. */
1702 size = 0;
1703 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1704 if ((mode = apply_result_mode[regno]) != VOIDmode)
1706 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1707 if (size % align != 0)
1708 size = CEIL (size, align) * align;
1709 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1710 emit_move_insn (reg, adjust_address (result, mode, size));
1712 push_to_sequence (call_fusage);
1713 emit_use (reg);
1714 call_fusage = get_insns ();
1715 end_sequence ();
1716 size += GET_MODE_SIZE (mode);
1719 /* Put the USE insns before the return. */
1720 emit_insn (call_fusage);
1722 /* Return whatever values was restored by jumping directly to the end
1723 of the function. */
1724 expand_naked_return ();
1727 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1729 static enum type_class
1730 type_to_class (tree type)
1732 switch (TREE_CODE (type))
1734 case VOID_TYPE: return void_type_class;
1735 case INTEGER_TYPE: return integer_type_class;
1736 case ENUMERAL_TYPE: return enumeral_type_class;
1737 case BOOLEAN_TYPE: return boolean_type_class;
1738 case POINTER_TYPE: return pointer_type_class;
1739 case REFERENCE_TYPE: return reference_type_class;
1740 case OFFSET_TYPE: return offset_type_class;
1741 case REAL_TYPE: return real_type_class;
1742 case COMPLEX_TYPE: return complex_type_class;
1743 case FUNCTION_TYPE: return function_type_class;
1744 case METHOD_TYPE: return method_type_class;
1745 case RECORD_TYPE: return record_type_class;
1746 case UNION_TYPE:
1747 case QUAL_UNION_TYPE: return union_type_class;
1748 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1749 ? string_type_class : array_type_class);
1750 case LANG_TYPE: return lang_type_class;
1751 default: return no_type_class;
1755 /* Expand a call EXP to __builtin_classify_type. */
1757 static rtx
1758 expand_builtin_classify_type (tree exp)
1760 if (call_expr_nargs (exp))
1761 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1762 return GEN_INT (no_type_class);
1765 /* This helper macro, meant to be used in mathfn_built_in below,
1766 determines which among a set of three builtin math functions is
1767 appropriate for a given type mode. The `F' and `L' cases are
1768 automatically generated from the `double' case. */
1769 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1770 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1771 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1772 fcodel = BUILT_IN_MATHFN##L ; break;
1773 /* Similar to above, but appends _R after any F/L suffix. */
1774 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1775 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1776 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1777 fcodel = BUILT_IN_MATHFN##L_R ; break;
1779 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1780 if available. If IMPLICIT is true use the implicit builtin declaration,
1781 otherwise use the explicit declaration. If we can't do the conversion,
1782 return zero. */
1784 static tree
1785 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1787 enum built_in_function fcode, fcodef, fcodel, fcode2;
1789 switch (fn)
1791 CASE_MATHFN (BUILT_IN_ACOS)
1792 CASE_MATHFN (BUILT_IN_ACOSH)
1793 CASE_MATHFN (BUILT_IN_ASIN)
1794 CASE_MATHFN (BUILT_IN_ASINH)
1795 CASE_MATHFN (BUILT_IN_ATAN)
1796 CASE_MATHFN (BUILT_IN_ATAN2)
1797 CASE_MATHFN (BUILT_IN_ATANH)
1798 CASE_MATHFN (BUILT_IN_CBRT)
1799 CASE_MATHFN (BUILT_IN_CEIL)
1800 CASE_MATHFN (BUILT_IN_CEXPI)
1801 CASE_MATHFN (BUILT_IN_COPYSIGN)
1802 CASE_MATHFN (BUILT_IN_COS)
1803 CASE_MATHFN (BUILT_IN_COSH)
1804 CASE_MATHFN (BUILT_IN_DREM)
1805 CASE_MATHFN (BUILT_IN_ERF)
1806 CASE_MATHFN (BUILT_IN_ERFC)
1807 CASE_MATHFN (BUILT_IN_EXP)
1808 CASE_MATHFN (BUILT_IN_EXP10)
1809 CASE_MATHFN (BUILT_IN_EXP2)
1810 CASE_MATHFN (BUILT_IN_EXPM1)
1811 CASE_MATHFN (BUILT_IN_FABS)
1812 CASE_MATHFN (BUILT_IN_FDIM)
1813 CASE_MATHFN (BUILT_IN_FLOOR)
1814 CASE_MATHFN (BUILT_IN_FMA)
1815 CASE_MATHFN (BUILT_IN_FMAX)
1816 CASE_MATHFN (BUILT_IN_FMIN)
1817 CASE_MATHFN (BUILT_IN_FMOD)
1818 CASE_MATHFN (BUILT_IN_FREXP)
1819 CASE_MATHFN (BUILT_IN_GAMMA)
1820 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1821 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1822 CASE_MATHFN (BUILT_IN_HYPOT)
1823 CASE_MATHFN (BUILT_IN_ILOGB)
1824 CASE_MATHFN (BUILT_IN_ICEIL)
1825 CASE_MATHFN (BUILT_IN_IFLOOR)
1826 CASE_MATHFN (BUILT_IN_INF)
1827 CASE_MATHFN (BUILT_IN_IRINT)
1828 CASE_MATHFN (BUILT_IN_IROUND)
1829 CASE_MATHFN (BUILT_IN_ISINF)
1830 CASE_MATHFN (BUILT_IN_J0)
1831 CASE_MATHFN (BUILT_IN_J1)
1832 CASE_MATHFN (BUILT_IN_JN)
1833 CASE_MATHFN (BUILT_IN_LCEIL)
1834 CASE_MATHFN (BUILT_IN_LDEXP)
1835 CASE_MATHFN (BUILT_IN_LFLOOR)
1836 CASE_MATHFN (BUILT_IN_LGAMMA)
1837 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1838 CASE_MATHFN (BUILT_IN_LLCEIL)
1839 CASE_MATHFN (BUILT_IN_LLFLOOR)
1840 CASE_MATHFN (BUILT_IN_LLRINT)
1841 CASE_MATHFN (BUILT_IN_LLROUND)
1842 CASE_MATHFN (BUILT_IN_LOG)
1843 CASE_MATHFN (BUILT_IN_LOG10)
1844 CASE_MATHFN (BUILT_IN_LOG1P)
1845 CASE_MATHFN (BUILT_IN_LOG2)
1846 CASE_MATHFN (BUILT_IN_LOGB)
1847 CASE_MATHFN (BUILT_IN_LRINT)
1848 CASE_MATHFN (BUILT_IN_LROUND)
1849 CASE_MATHFN (BUILT_IN_MODF)
1850 CASE_MATHFN (BUILT_IN_NAN)
1851 CASE_MATHFN (BUILT_IN_NANS)
1852 CASE_MATHFN (BUILT_IN_NEARBYINT)
1853 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1854 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1855 CASE_MATHFN (BUILT_IN_POW)
1856 CASE_MATHFN (BUILT_IN_POWI)
1857 CASE_MATHFN (BUILT_IN_POW10)
1858 CASE_MATHFN (BUILT_IN_REMAINDER)
1859 CASE_MATHFN (BUILT_IN_REMQUO)
1860 CASE_MATHFN (BUILT_IN_RINT)
1861 CASE_MATHFN (BUILT_IN_ROUND)
1862 CASE_MATHFN (BUILT_IN_SCALB)
1863 CASE_MATHFN (BUILT_IN_SCALBLN)
1864 CASE_MATHFN (BUILT_IN_SCALBN)
1865 CASE_MATHFN (BUILT_IN_SIGNBIT)
1866 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1867 CASE_MATHFN (BUILT_IN_SIN)
1868 CASE_MATHFN (BUILT_IN_SINCOS)
1869 CASE_MATHFN (BUILT_IN_SINH)
1870 CASE_MATHFN (BUILT_IN_SQRT)
1871 CASE_MATHFN (BUILT_IN_TAN)
1872 CASE_MATHFN (BUILT_IN_TANH)
1873 CASE_MATHFN (BUILT_IN_TGAMMA)
1874 CASE_MATHFN (BUILT_IN_TRUNC)
1875 CASE_MATHFN (BUILT_IN_Y0)
1876 CASE_MATHFN (BUILT_IN_Y1)
1877 CASE_MATHFN (BUILT_IN_YN)
1879 default:
1880 return NULL_TREE;
1883 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1884 fcode2 = fcode;
1885 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1886 fcode2 = fcodef;
1887 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1888 fcode2 = fcodel;
1889 else
1890 return NULL_TREE;
1892 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1893 return NULL_TREE;
1895 return builtin_decl_explicit (fcode2);
1898 /* Like mathfn_built_in_1(), but always use the implicit array. */
1900 tree
1901 mathfn_built_in (tree type, enum built_in_function fn)
1903 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1906 /* If errno must be maintained, expand the RTL to check if the result,
1907 TARGET, of a built-in function call, EXP, is NaN, and if so set
1908 errno to EDOM. */
1910 static void
1911 expand_errno_check (tree exp, rtx target)
1913 rtx lab = gen_label_rtx ();
1915 /* Test the result; if it is NaN, set errno=EDOM because
1916 the argument was not in the domain. */
1917 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1918 NULL_RTX, NULL_RTX, lab,
1919 /* The jump is very likely. */
1920 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1922 #ifdef TARGET_EDOM
1923 /* If this built-in doesn't throw an exception, set errno directly. */
1924 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1926 #ifdef GEN_ERRNO_RTX
1927 rtx errno_rtx = GEN_ERRNO_RTX;
1928 #else
1929 rtx errno_rtx
1930 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1931 #endif
1932 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1933 emit_label (lab);
1934 return;
1936 #endif
1938 /* Make sure the library call isn't expanded as a tail call. */
1939 CALL_EXPR_TAILCALL (exp) = 0;
1941 /* We can't set errno=EDOM directly; let the library call do it.
1942 Pop the arguments right away in case the call gets deleted. */
1943 NO_DEFER_POP;
1944 expand_call (exp, target, 0);
1945 OK_DEFER_POP;
1946 emit_label (lab);
1949 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1950 Return NULL_RTX if a normal call should be emitted rather than expanding
1951 the function in-line. EXP is the expression that is a call to the builtin
1952 function; if convenient, the result should be placed in TARGET.
1953 SUBTARGET may be used as the target for computing one of EXP's operands. */
1955 static rtx
1956 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1958 optab builtin_optab;
1959 rtx op0, insns;
1960 tree fndecl = get_callee_fndecl (exp);
1961 enum machine_mode mode;
1962 bool errno_set = false;
1963 tree arg;
1965 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1966 return NULL_RTX;
1968 arg = CALL_EXPR_ARG (exp, 0);
1970 switch (DECL_FUNCTION_CODE (fndecl))
1972 CASE_FLT_FN (BUILT_IN_SQRT):
1973 errno_set = ! tree_expr_nonnegative_p (arg);
1974 builtin_optab = sqrt_optab;
1975 break;
1976 CASE_FLT_FN (BUILT_IN_EXP):
1977 errno_set = true; builtin_optab = exp_optab; break;
1978 CASE_FLT_FN (BUILT_IN_EXP10):
1979 CASE_FLT_FN (BUILT_IN_POW10):
1980 errno_set = true; builtin_optab = exp10_optab; break;
1981 CASE_FLT_FN (BUILT_IN_EXP2):
1982 errno_set = true; builtin_optab = exp2_optab; break;
1983 CASE_FLT_FN (BUILT_IN_EXPM1):
1984 errno_set = true; builtin_optab = expm1_optab; break;
1985 CASE_FLT_FN (BUILT_IN_LOGB):
1986 errno_set = true; builtin_optab = logb_optab; break;
1987 CASE_FLT_FN (BUILT_IN_LOG):
1988 errno_set = true; builtin_optab = log_optab; break;
1989 CASE_FLT_FN (BUILT_IN_LOG10):
1990 errno_set = true; builtin_optab = log10_optab; break;
1991 CASE_FLT_FN (BUILT_IN_LOG2):
1992 errno_set = true; builtin_optab = log2_optab; break;
1993 CASE_FLT_FN (BUILT_IN_LOG1P):
1994 errno_set = true; builtin_optab = log1p_optab; break;
1995 CASE_FLT_FN (BUILT_IN_ASIN):
1996 builtin_optab = asin_optab; break;
1997 CASE_FLT_FN (BUILT_IN_ACOS):
1998 builtin_optab = acos_optab; break;
1999 CASE_FLT_FN (BUILT_IN_TAN):
2000 builtin_optab = tan_optab; break;
2001 CASE_FLT_FN (BUILT_IN_ATAN):
2002 builtin_optab = atan_optab; break;
2003 CASE_FLT_FN (BUILT_IN_FLOOR):
2004 builtin_optab = floor_optab; break;
2005 CASE_FLT_FN (BUILT_IN_CEIL):
2006 builtin_optab = ceil_optab; break;
2007 CASE_FLT_FN (BUILT_IN_TRUNC):
2008 builtin_optab = btrunc_optab; break;
2009 CASE_FLT_FN (BUILT_IN_ROUND):
2010 builtin_optab = round_optab; break;
2011 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2012 builtin_optab = nearbyint_optab;
2013 if (flag_trapping_math)
2014 break;
2015 /* Else fallthrough and expand as rint. */
2016 CASE_FLT_FN (BUILT_IN_RINT):
2017 builtin_optab = rint_optab; break;
2018 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2019 builtin_optab = significand_optab; break;
2020 default:
2021 gcc_unreachable ();
2024 /* Make a suitable register to place result in. */
2025 mode = TYPE_MODE (TREE_TYPE (exp));
2027 if (! flag_errno_math || ! HONOR_NANS (mode))
2028 errno_set = false;
2030 /* Before working hard, check whether the instruction is available. */
2031 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2032 && (!errno_set || !optimize_insn_for_size_p ()))
2034 target = gen_reg_rtx (mode);
2036 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2037 need to expand the argument again. This way, we will not perform
2038 side-effects more the once. */
2039 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2041 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2043 start_sequence ();
2045 /* Compute into TARGET.
2046 Set TARGET to wherever the result comes back. */
2047 target = expand_unop (mode, builtin_optab, op0, target, 0);
2049 if (target != 0)
2051 if (errno_set)
2052 expand_errno_check (exp, target);
2054 /* Output the entire sequence. */
2055 insns = get_insns ();
2056 end_sequence ();
2057 emit_insn (insns);
2058 return target;
2061 /* If we were unable to expand via the builtin, stop the sequence
2062 (without outputting the insns) and call to the library function
2063 with the stabilized argument list. */
2064 end_sequence ();
2067 return expand_call (exp, target, target == const0_rtx);
2070 /* Expand a call to the builtin binary math functions (pow and atan2).
2071 Return NULL_RTX if a normal call should be emitted rather than expanding the
2072 function in-line. EXP is the expression that is a call to the builtin
2073 function; if convenient, the result should be placed in TARGET.
2074 SUBTARGET may be used as the target for computing one of EXP's
2075 operands. */
2077 static rtx
2078 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2080 optab builtin_optab;
2081 rtx op0, op1, insns;
2082 int op1_type = REAL_TYPE;
2083 tree fndecl = get_callee_fndecl (exp);
2084 tree arg0, arg1;
2085 enum machine_mode mode;
2086 bool errno_set = true;
2088 switch (DECL_FUNCTION_CODE (fndecl))
2090 CASE_FLT_FN (BUILT_IN_SCALBN):
2091 CASE_FLT_FN (BUILT_IN_SCALBLN):
2092 CASE_FLT_FN (BUILT_IN_LDEXP):
2093 op1_type = INTEGER_TYPE;
2094 default:
2095 break;
2098 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2099 return NULL_RTX;
2101 arg0 = CALL_EXPR_ARG (exp, 0);
2102 arg1 = CALL_EXPR_ARG (exp, 1);
2104 switch (DECL_FUNCTION_CODE (fndecl))
2106 CASE_FLT_FN (BUILT_IN_POW):
2107 builtin_optab = pow_optab; break;
2108 CASE_FLT_FN (BUILT_IN_ATAN2):
2109 builtin_optab = atan2_optab; break;
2110 CASE_FLT_FN (BUILT_IN_SCALB):
2111 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2112 return 0;
2113 builtin_optab = scalb_optab; break;
2114 CASE_FLT_FN (BUILT_IN_SCALBN):
2115 CASE_FLT_FN (BUILT_IN_SCALBLN):
2116 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2117 return 0;
2118 /* Fall through... */
2119 CASE_FLT_FN (BUILT_IN_LDEXP):
2120 builtin_optab = ldexp_optab; break;
2121 CASE_FLT_FN (BUILT_IN_FMOD):
2122 builtin_optab = fmod_optab; break;
2123 CASE_FLT_FN (BUILT_IN_REMAINDER):
2124 CASE_FLT_FN (BUILT_IN_DREM):
2125 builtin_optab = remainder_optab; break;
2126 default:
2127 gcc_unreachable ();
2130 /* Make a suitable register to place result in. */
2131 mode = TYPE_MODE (TREE_TYPE (exp));
2133 /* Before working hard, check whether the instruction is available. */
2134 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2135 return NULL_RTX;
2137 target = gen_reg_rtx (mode);
2139 if (! flag_errno_math || ! HONOR_NANS (mode))
2140 errno_set = false;
2142 if (errno_set && optimize_insn_for_size_p ())
2143 return 0;
2145 /* Always stabilize the argument list. */
2146 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2147 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2149 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2150 op1 = expand_normal (arg1);
2152 start_sequence ();
2154 /* Compute into TARGET.
2155 Set TARGET to wherever the result comes back. */
2156 target = expand_binop (mode, builtin_optab, op0, op1,
2157 target, 0, OPTAB_DIRECT);
2159 /* If we were unable to expand via the builtin, stop the sequence
2160 (without outputting the insns) and call to the library function
2161 with the stabilized argument list. */
2162 if (target == 0)
2164 end_sequence ();
2165 return expand_call (exp, target, target == const0_rtx);
2168 if (errno_set)
2169 expand_errno_check (exp, target);
2171 /* Output the entire sequence. */
2172 insns = get_insns ();
2173 end_sequence ();
2174 emit_insn (insns);
2176 return target;
2179 /* Expand a call to the builtin trinary math functions (fma).
2180 Return NULL_RTX if a normal call should be emitted rather than expanding the
2181 function in-line. EXP is the expression that is a call to the builtin
2182 function; if convenient, the result should be placed in TARGET.
2183 SUBTARGET may be used as the target for computing one of EXP's
2184 operands. */
2186 static rtx
2187 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2189 optab builtin_optab;
2190 rtx op0, op1, op2, insns;
2191 tree fndecl = get_callee_fndecl (exp);
2192 tree arg0, arg1, arg2;
2193 enum machine_mode mode;
2195 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2196 return NULL_RTX;
2198 arg0 = CALL_EXPR_ARG (exp, 0);
2199 arg1 = CALL_EXPR_ARG (exp, 1);
2200 arg2 = CALL_EXPR_ARG (exp, 2);
2202 switch (DECL_FUNCTION_CODE (fndecl))
2204 CASE_FLT_FN (BUILT_IN_FMA):
2205 builtin_optab = fma_optab; break;
2206 default:
2207 gcc_unreachable ();
2210 /* Make a suitable register to place result in. */
2211 mode = TYPE_MODE (TREE_TYPE (exp));
2213 /* Before working hard, check whether the instruction is available. */
2214 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2215 return NULL_RTX;
2217 target = gen_reg_rtx (mode);
2219 /* Always stabilize the argument list. */
2220 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2221 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2222 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2224 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2225 op1 = expand_normal (arg1);
2226 op2 = expand_normal (arg2);
2228 start_sequence ();
2230 /* Compute into TARGET.
2231 Set TARGET to wherever the result comes back. */
2232 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2233 target, 0);
2235 /* If we were unable to expand via the builtin, stop the sequence
2236 (without outputting the insns) and call to the library function
2237 with the stabilized argument list. */
2238 if (target == 0)
2240 end_sequence ();
2241 return expand_call (exp, target, target == const0_rtx);
2244 /* Output the entire sequence. */
2245 insns = get_insns ();
2246 end_sequence ();
2247 emit_insn (insns);
2249 return target;
2252 /* Expand a call to the builtin sin and cos math functions.
2253 Return NULL_RTX if a normal call should be emitted rather than expanding the
2254 function in-line. EXP is the expression that is a call to the builtin
2255 function; if convenient, the result should be placed in TARGET.
2256 SUBTARGET may be used as the target for computing one of EXP's
2257 operands. */
2259 static rtx
2260 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2262 optab builtin_optab;
2263 rtx op0, insns;
2264 tree fndecl = get_callee_fndecl (exp);
2265 enum machine_mode mode;
2266 tree arg;
2268 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2269 return NULL_RTX;
2271 arg = CALL_EXPR_ARG (exp, 0);
2273 switch (DECL_FUNCTION_CODE (fndecl))
2275 CASE_FLT_FN (BUILT_IN_SIN):
2276 CASE_FLT_FN (BUILT_IN_COS):
2277 builtin_optab = sincos_optab; break;
2278 default:
2279 gcc_unreachable ();
2282 /* Make a suitable register to place result in. */
2283 mode = TYPE_MODE (TREE_TYPE (exp));
2285 /* Check if sincos insn is available, otherwise fallback
2286 to sin or cos insn. */
2287 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2288 switch (DECL_FUNCTION_CODE (fndecl))
2290 CASE_FLT_FN (BUILT_IN_SIN):
2291 builtin_optab = sin_optab; break;
2292 CASE_FLT_FN (BUILT_IN_COS):
2293 builtin_optab = cos_optab; break;
2294 default:
2295 gcc_unreachable ();
2298 /* Before working hard, check whether the instruction is available. */
2299 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2301 target = gen_reg_rtx (mode);
2303 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2304 need to expand the argument again. This way, we will not perform
2305 side-effects more the once. */
2306 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2308 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2310 start_sequence ();
2312 /* Compute into TARGET.
2313 Set TARGET to wherever the result comes back. */
2314 if (builtin_optab == sincos_optab)
2316 int result;
2318 switch (DECL_FUNCTION_CODE (fndecl))
2320 CASE_FLT_FN (BUILT_IN_SIN):
2321 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2322 break;
2323 CASE_FLT_FN (BUILT_IN_COS):
2324 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2325 break;
2326 default:
2327 gcc_unreachable ();
2329 gcc_assert (result);
2331 else
2333 target = expand_unop (mode, builtin_optab, op0, target, 0);
2336 if (target != 0)
2338 /* Output the entire sequence. */
2339 insns = get_insns ();
2340 end_sequence ();
2341 emit_insn (insns);
2342 return target;
2345 /* If we were unable to expand via the builtin, stop the sequence
2346 (without outputting the insns) and call to the library function
2347 with the stabilized argument list. */
2348 end_sequence ();
2351 target = expand_call (exp, target, target == const0_rtx);
2353 return target;
2356 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2357 return an RTL instruction code that implements the functionality.
2358 If that isn't possible or available return CODE_FOR_nothing. */
2360 static enum insn_code
2361 interclass_mathfn_icode (tree arg, tree fndecl)
2363 bool errno_set = false;
2364 optab builtin_optab = unknown_optab;
2365 enum machine_mode mode;
2367 switch (DECL_FUNCTION_CODE (fndecl))
2369 CASE_FLT_FN (BUILT_IN_ILOGB):
2370 errno_set = true; builtin_optab = ilogb_optab; break;
2371 CASE_FLT_FN (BUILT_IN_ISINF):
2372 builtin_optab = isinf_optab; break;
2373 case BUILT_IN_ISNORMAL:
2374 case BUILT_IN_ISFINITE:
2375 CASE_FLT_FN (BUILT_IN_FINITE):
2376 case BUILT_IN_FINITED32:
2377 case BUILT_IN_FINITED64:
2378 case BUILT_IN_FINITED128:
2379 case BUILT_IN_ISINFD32:
2380 case BUILT_IN_ISINFD64:
2381 case BUILT_IN_ISINFD128:
2382 /* These builtins have no optabs (yet). */
2383 break;
2384 default:
2385 gcc_unreachable ();
2388 /* There's no easy way to detect the case we need to set EDOM. */
2389 if (flag_errno_math && errno_set)
2390 return CODE_FOR_nothing;
2392 /* Optab mode depends on the mode of the input argument. */
2393 mode = TYPE_MODE (TREE_TYPE (arg));
2395 if (builtin_optab)
2396 return optab_handler (builtin_optab, mode);
2397 return CODE_FOR_nothing;
2400 /* Expand a call to one of the builtin math functions that operate on
2401 floating point argument and output an integer result (ilogb, isinf,
2402 isnan, etc).
2403 Return 0 if a normal call should be emitted rather than expanding the
2404 function in-line. EXP is the expression that is a call to the builtin
2405 function; if convenient, the result should be placed in TARGET. */
2407 static rtx
2408 expand_builtin_interclass_mathfn (tree exp, rtx target)
2410 enum insn_code icode = CODE_FOR_nothing;
2411 rtx op0;
2412 tree fndecl = get_callee_fndecl (exp);
2413 enum machine_mode mode;
2414 tree arg;
2416 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2417 return NULL_RTX;
2419 arg = CALL_EXPR_ARG (exp, 0);
2420 icode = interclass_mathfn_icode (arg, fndecl);
2421 mode = TYPE_MODE (TREE_TYPE (arg));
2423 if (icode != CODE_FOR_nothing)
2425 struct expand_operand ops[1];
2426 rtx last = get_last_insn ();
2427 tree orig_arg = arg;
2429 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2430 need to expand the argument again. This way, we will not perform
2431 side-effects more the once. */
2432 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2434 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2436 if (mode != GET_MODE (op0))
2437 op0 = convert_to_mode (mode, op0, 0);
2439 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2440 if (maybe_legitimize_operands (icode, 0, 1, ops)
2441 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2442 return ops[0].value;
2444 delete_insns_since (last);
2445 CALL_EXPR_ARG (exp, 0) = orig_arg;
2448 return NULL_RTX;
2451 /* Expand a call to the builtin sincos math function.
2452 Return NULL_RTX if a normal call should be emitted rather than expanding the
2453 function in-line. EXP is the expression that is a call to the builtin
2454 function. */
2456 static rtx
2457 expand_builtin_sincos (tree exp)
2459 rtx op0, op1, op2, target1, target2;
2460 enum machine_mode mode;
2461 tree arg, sinp, cosp;
2462 int result;
2463 location_t loc = EXPR_LOCATION (exp);
2464 tree alias_type, alias_off;
2466 if (!validate_arglist (exp, REAL_TYPE,
2467 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2468 return NULL_RTX;
2470 arg = CALL_EXPR_ARG (exp, 0);
2471 sinp = CALL_EXPR_ARG (exp, 1);
2472 cosp = CALL_EXPR_ARG (exp, 2);
2474 /* Make a suitable register to place result in. */
2475 mode = TYPE_MODE (TREE_TYPE (arg));
2477 /* Check if sincos insn is available, otherwise emit the call. */
2478 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2479 return NULL_RTX;
2481 target1 = gen_reg_rtx (mode);
2482 target2 = gen_reg_rtx (mode);
2484 op0 = expand_normal (arg);
2485 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2486 alias_off = build_int_cst (alias_type, 0);
2487 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2488 sinp, alias_off));
2489 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2490 cosp, alias_off));
2492 /* Compute into target1 and target2.
2493 Set TARGET to wherever the result comes back. */
2494 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2495 gcc_assert (result);
2497 /* Move target1 and target2 to the memory locations indicated
2498 by op1 and op2. */
2499 emit_move_insn (op1, target1);
2500 emit_move_insn (op2, target2);
2502 return const0_rtx;
2505 /* Expand a call to the internal cexpi builtin to the sincos math function.
2506 EXP is the expression that is a call to the builtin function; if convenient,
2507 the result should be placed in TARGET. */
2509 static rtx
2510 expand_builtin_cexpi (tree exp, rtx target)
2512 tree fndecl = get_callee_fndecl (exp);
2513 tree arg, type;
2514 enum machine_mode mode;
2515 rtx op0, op1, op2;
2516 location_t loc = EXPR_LOCATION (exp);
2518 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2519 return NULL_RTX;
2521 arg = CALL_EXPR_ARG (exp, 0);
2522 type = TREE_TYPE (arg);
2523 mode = TYPE_MODE (TREE_TYPE (arg));
2525 /* Try expanding via a sincos optab, fall back to emitting a libcall
2526 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2527 is only generated from sincos, cexp or if we have either of them. */
2528 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2530 op1 = gen_reg_rtx (mode);
2531 op2 = gen_reg_rtx (mode);
2533 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2535 /* Compute into op1 and op2. */
2536 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2538 else if (TARGET_HAS_SINCOS)
2540 tree call, fn = NULL_TREE;
2541 tree top1, top2;
2542 rtx op1a, op2a;
2544 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2545 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2546 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2547 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2548 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2549 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2550 else
2551 gcc_unreachable ();
2553 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2554 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2555 op1a = copy_addr_to_reg (XEXP (op1, 0));
2556 op2a = copy_addr_to_reg (XEXP (op2, 0));
2557 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2558 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2560 /* Make sure not to fold the sincos call again. */
2561 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2562 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2563 call, 3, arg, top1, top2));
2565 else
2567 tree call, fn = NULL_TREE, narg;
2568 tree ctype = build_complex_type (type);
2570 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2571 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2572 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2573 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2574 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2575 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2576 else
2577 gcc_unreachable ();
2579 /* If we don't have a decl for cexp create one. This is the
2580 friendliest fallback if the user calls __builtin_cexpi
2581 without full target C99 function support. */
2582 if (fn == NULL_TREE)
2584 tree fntype;
2585 const char *name = NULL;
2587 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2588 name = "cexpf";
2589 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2590 name = "cexp";
2591 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2592 name = "cexpl";
2594 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2595 fn = build_fn_decl (name, fntype);
2598 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2599 build_real (type, dconst0), arg);
2601 /* Make sure not to fold the cexp call again. */
2602 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2603 return expand_expr (build_call_nary (ctype, call, 1, narg),
2604 target, VOIDmode, EXPAND_NORMAL);
2607 /* Now build the proper return type. */
2608 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2609 make_tree (TREE_TYPE (arg), op2),
2610 make_tree (TREE_TYPE (arg), op1)),
2611 target, VOIDmode, EXPAND_NORMAL);
2614 /* Conveniently construct a function call expression. FNDECL names the
2615 function to be called, N is the number of arguments, and the "..."
2616 parameters are the argument expressions. Unlike build_call_exr
2617 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2619 static tree
2620 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2622 va_list ap;
2623 tree fntype = TREE_TYPE (fndecl);
2624 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2626 va_start (ap, n);
2627 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2628 va_end (ap);
2629 SET_EXPR_LOCATION (fn, loc);
2630 return fn;
2633 /* Expand a call to one of the builtin rounding functions gcc defines
2634 as an extension (lfloor and lceil). As these are gcc extensions we
2635 do not need to worry about setting errno to EDOM.
2636 If expanding via optab fails, lower expression to (int)(floor(x)).
2637 EXP is the expression that is a call to the builtin function;
2638 if convenient, the result should be placed in TARGET. */
2640 static rtx
2641 expand_builtin_int_roundingfn (tree exp, rtx target)
2643 convert_optab builtin_optab;
2644 rtx op0, insns, tmp;
2645 tree fndecl = get_callee_fndecl (exp);
2646 enum built_in_function fallback_fn;
2647 tree fallback_fndecl;
2648 enum machine_mode mode;
2649 tree arg;
2651 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2652 gcc_unreachable ();
2654 arg = CALL_EXPR_ARG (exp, 0);
2656 switch (DECL_FUNCTION_CODE (fndecl))
2658 CASE_FLT_FN (BUILT_IN_ICEIL):
2659 CASE_FLT_FN (BUILT_IN_LCEIL):
2660 CASE_FLT_FN (BUILT_IN_LLCEIL):
2661 builtin_optab = lceil_optab;
2662 fallback_fn = BUILT_IN_CEIL;
2663 break;
2665 CASE_FLT_FN (BUILT_IN_IFLOOR):
2666 CASE_FLT_FN (BUILT_IN_LFLOOR):
2667 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2668 builtin_optab = lfloor_optab;
2669 fallback_fn = BUILT_IN_FLOOR;
2670 break;
2672 default:
2673 gcc_unreachable ();
2676 /* Make a suitable register to place result in. */
2677 mode = TYPE_MODE (TREE_TYPE (exp));
2679 target = gen_reg_rtx (mode);
2681 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2682 need to expand the argument again. This way, we will not perform
2683 side-effects more the once. */
2684 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2686 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2688 start_sequence ();
2690 /* Compute into TARGET. */
2691 if (expand_sfix_optab (target, op0, builtin_optab))
2693 /* Output the entire sequence. */
2694 insns = get_insns ();
2695 end_sequence ();
2696 emit_insn (insns);
2697 return target;
2700 /* If we were unable to expand via the builtin, stop the sequence
2701 (without outputting the insns). */
2702 end_sequence ();
2704 /* Fall back to floating point rounding optab. */
2705 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2707 /* For non-C99 targets we may end up without a fallback fndecl here
2708 if the user called __builtin_lfloor directly. In this case emit
2709 a call to the floor/ceil variants nevertheless. This should result
2710 in the best user experience for not full C99 targets. */
2711 if (fallback_fndecl == NULL_TREE)
2713 tree fntype;
2714 const char *name = NULL;
2716 switch (DECL_FUNCTION_CODE (fndecl))
2718 case BUILT_IN_ICEIL:
2719 case BUILT_IN_LCEIL:
2720 case BUILT_IN_LLCEIL:
2721 name = "ceil";
2722 break;
2723 case BUILT_IN_ICEILF:
2724 case BUILT_IN_LCEILF:
2725 case BUILT_IN_LLCEILF:
2726 name = "ceilf";
2727 break;
2728 case BUILT_IN_ICEILL:
2729 case BUILT_IN_LCEILL:
2730 case BUILT_IN_LLCEILL:
2731 name = "ceill";
2732 break;
2733 case BUILT_IN_IFLOOR:
2734 case BUILT_IN_LFLOOR:
2735 case BUILT_IN_LLFLOOR:
2736 name = "floor";
2737 break;
2738 case BUILT_IN_IFLOORF:
2739 case BUILT_IN_LFLOORF:
2740 case BUILT_IN_LLFLOORF:
2741 name = "floorf";
2742 break;
2743 case BUILT_IN_IFLOORL:
2744 case BUILT_IN_LFLOORL:
2745 case BUILT_IN_LLFLOORL:
2746 name = "floorl";
2747 break;
2748 default:
2749 gcc_unreachable ();
2752 fntype = build_function_type_list (TREE_TYPE (arg),
2753 TREE_TYPE (arg), NULL_TREE);
2754 fallback_fndecl = build_fn_decl (name, fntype);
2757 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2759 tmp = expand_normal (exp);
2761 /* Truncate the result of floating point optab to integer
2762 via expand_fix (). */
2763 target = gen_reg_rtx (mode);
2764 expand_fix (target, tmp, 0);
2766 return target;
2769 /* Expand a call to one of the builtin math functions doing integer
2770 conversion (lrint).
2771 Return 0 if a normal call should be emitted rather than expanding the
2772 function in-line. EXP is the expression that is a call to the builtin
2773 function; if convenient, the result should be placed in TARGET. */
2775 static rtx
2776 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2778 convert_optab builtin_optab;
2779 rtx op0, insns;
2780 tree fndecl = get_callee_fndecl (exp);
2781 tree arg;
2782 enum machine_mode mode;
2783 enum built_in_function fallback_fn = BUILT_IN_NONE;
2785 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2786 gcc_unreachable ();
2788 arg = CALL_EXPR_ARG (exp, 0);
2790 switch (DECL_FUNCTION_CODE (fndecl))
2792 CASE_FLT_FN (BUILT_IN_IRINT):
2793 fallback_fn = BUILT_IN_LRINT;
2794 /* FALLTHRU */
2795 CASE_FLT_FN (BUILT_IN_LRINT):
2796 CASE_FLT_FN (BUILT_IN_LLRINT):
2797 builtin_optab = lrint_optab;
2798 break;
2800 CASE_FLT_FN (BUILT_IN_IROUND):
2801 fallback_fn = BUILT_IN_LROUND;
2802 /* FALLTHRU */
2803 CASE_FLT_FN (BUILT_IN_LROUND):
2804 CASE_FLT_FN (BUILT_IN_LLROUND):
2805 builtin_optab = lround_optab;
2806 break;
2808 default:
2809 gcc_unreachable ();
2812 /* There's no easy way to detect the case we need to set EDOM. */
2813 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2814 return NULL_RTX;
2816 /* Make a suitable register to place result in. */
2817 mode = TYPE_MODE (TREE_TYPE (exp));
2819 /* There's no easy way to detect the case we need to set EDOM. */
2820 if (!flag_errno_math)
2822 target = gen_reg_rtx (mode);
2824 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2825 need to expand the argument again. This way, we will not perform
2826 side-effects more the once. */
2827 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2829 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2831 start_sequence ();
2833 if (expand_sfix_optab (target, op0, builtin_optab))
2835 /* Output the entire sequence. */
2836 insns = get_insns ();
2837 end_sequence ();
2838 emit_insn (insns);
2839 return target;
2842 /* If we were unable to expand via the builtin, stop the sequence
2843 (without outputting the insns) and call to the library function
2844 with the stabilized argument list. */
2845 end_sequence ();
2848 if (fallback_fn != BUILT_IN_NONE)
2850 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2851 targets, (int) round (x) should never be transformed into
2852 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2853 a call to lround in the hope that the target provides at least some
2854 C99 functions. This should result in the best user experience for
2855 not full C99 targets. */
2856 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2857 fallback_fn, 0);
2859 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2860 fallback_fndecl, 1, arg);
2862 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2863 return convert_to_mode (mode, target, 0);
2866 target = expand_call (exp, target, target == const0_rtx);
2868 return target;
2871 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2872 a normal call should be emitted rather than expanding the function
2873 in-line. EXP is the expression that is a call to the builtin
2874 function; if convenient, the result should be placed in TARGET. */
2876 static rtx
2877 expand_builtin_powi (tree exp, rtx target)
2879 tree arg0, arg1;
2880 rtx op0, op1;
2881 enum machine_mode mode;
2882 enum machine_mode mode2;
2884 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2885 return NULL_RTX;
2887 arg0 = CALL_EXPR_ARG (exp, 0);
2888 arg1 = CALL_EXPR_ARG (exp, 1);
2889 mode = TYPE_MODE (TREE_TYPE (exp));
2891 /* Emit a libcall to libgcc. */
2893 /* Mode of the 2nd argument must match that of an int. */
2894 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2896 if (target == NULL_RTX)
2897 target = gen_reg_rtx (mode);
2899 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2900 if (GET_MODE (op0) != mode)
2901 op0 = convert_to_mode (mode, op0, 0);
2902 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2903 if (GET_MODE (op1) != mode2)
2904 op1 = convert_to_mode (mode2, op1, 0);
2906 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2907 target, LCT_CONST, mode, 2,
2908 op0, mode, op1, mode2);
2910 return target;
2913 /* Expand expression EXP which is a call to the strlen builtin. Return
2914 NULL_RTX if we failed the caller should emit a normal call, otherwise
2915 try to get the result in TARGET, if convenient. */
2917 static rtx
2918 expand_builtin_strlen (tree exp, rtx target,
2919 enum machine_mode target_mode)
2921 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2922 return NULL_RTX;
2923 else
2925 struct expand_operand ops[4];
2926 rtx pat;
2927 tree len;
2928 tree src = CALL_EXPR_ARG (exp, 0);
2929 rtx src_reg, before_strlen;
2930 enum machine_mode insn_mode = target_mode;
2931 enum insn_code icode = CODE_FOR_nothing;
2932 unsigned int align;
2934 /* If the length can be computed at compile-time, return it. */
2935 len = c_strlen (src, 0);
2936 if (len)
2937 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2939 /* If the length can be computed at compile-time and is constant
2940 integer, but there are side-effects in src, evaluate
2941 src for side-effects, then return len.
2942 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2943 can be optimized into: i++; x = 3; */
2944 len = c_strlen (src, 1);
2945 if (len && TREE_CODE (len) == INTEGER_CST)
2947 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2948 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2951 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2953 /* If SRC is not a pointer type, don't do this operation inline. */
2954 if (align == 0)
2955 return NULL_RTX;
2957 /* Bail out if we can't compute strlen in the right mode. */
2958 while (insn_mode != VOIDmode)
2960 icode = optab_handler (strlen_optab, insn_mode);
2961 if (icode != CODE_FOR_nothing)
2962 break;
2964 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2966 if (insn_mode == VOIDmode)
2967 return NULL_RTX;
2969 /* Make a place to hold the source address. We will not expand
2970 the actual source until we are sure that the expansion will
2971 not fail -- there are trees that cannot be expanded twice. */
2972 src_reg = gen_reg_rtx (Pmode);
2974 /* Mark the beginning of the strlen sequence so we can emit the
2975 source operand later. */
2976 before_strlen = get_last_insn ();
2978 create_output_operand (&ops[0], target, insn_mode);
2979 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2980 create_integer_operand (&ops[2], 0);
2981 create_integer_operand (&ops[3], align);
2982 if (!maybe_expand_insn (icode, 4, ops))
2983 return NULL_RTX;
2985 /* Now that we are assured of success, expand the source. */
2986 start_sequence ();
2987 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2988 if (pat != src_reg)
2990 #ifdef POINTERS_EXTEND_UNSIGNED
2991 if (GET_MODE (pat) != Pmode)
2992 pat = convert_to_mode (Pmode, pat,
2993 POINTERS_EXTEND_UNSIGNED);
2994 #endif
2995 emit_move_insn (src_reg, pat);
2997 pat = get_insns ();
2998 end_sequence ();
3000 if (before_strlen)
3001 emit_insn_after (pat, before_strlen);
3002 else
3003 emit_insn_before (pat, get_insns ());
3005 /* Return the value in the proper mode for this function. */
3006 if (GET_MODE (ops[0].value) == target_mode)
3007 target = ops[0].value;
3008 else if (target != 0)
3009 convert_move (target, ops[0].value, 0);
3010 else
3011 target = convert_to_mode (target_mode, ops[0].value, 0);
3013 return target;
3017 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3018 bytes from constant string DATA + OFFSET and return it as target
3019 constant. */
3021 static rtx
3022 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3023 enum machine_mode mode)
3025 const char *str = (const char *) data;
3027 gcc_assert (offset >= 0
3028 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3029 <= strlen (str) + 1));
3031 return c_readstr (str + offset, mode);
3034 /* Expand a call EXP to the memcpy builtin.
3035 Return NULL_RTX if we failed, the caller should emit a normal call,
3036 otherwise try to get the result in TARGET, if convenient (and in
3037 mode MODE if that's convenient). */
3039 static rtx
3040 expand_builtin_memcpy (tree exp, rtx target)
3042 if (!validate_arglist (exp,
3043 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3044 return NULL_RTX;
3045 else
3047 tree dest = CALL_EXPR_ARG (exp, 0);
3048 tree src = CALL_EXPR_ARG (exp, 1);
3049 tree len = CALL_EXPR_ARG (exp, 2);
3050 const char *src_str;
3051 unsigned int src_align = get_pointer_alignment (src);
3052 unsigned int dest_align = get_pointer_alignment (dest);
3053 rtx dest_mem, src_mem, dest_addr, len_rtx;
3054 HOST_WIDE_INT expected_size = -1;
3055 unsigned int expected_align = 0;
3057 /* If DEST is not a pointer type, call the normal function. */
3058 if (dest_align == 0)
3059 return NULL_RTX;
3061 /* If either SRC is not a pointer type, don't do this
3062 operation in-line. */
3063 if (src_align == 0)
3064 return NULL_RTX;
3066 if (currently_expanding_gimple_stmt)
3067 stringop_block_profile (currently_expanding_gimple_stmt,
3068 &expected_align, &expected_size);
3070 if (expected_align < dest_align)
3071 expected_align = dest_align;
3072 dest_mem = get_memory_rtx (dest, len);
3073 set_mem_align (dest_mem, dest_align);
3074 len_rtx = expand_normal (len);
3075 src_str = c_getstr (src);
3077 /* If SRC is a string constant and block move would be done
3078 by pieces, we can avoid loading the string from memory
3079 and only stored the computed constants. */
3080 if (src_str
3081 && CONST_INT_P (len_rtx)
3082 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3083 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3084 CONST_CAST (char *, src_str),
3085 dest_align, false))
3087 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3088 builtin_memcpy_read_str,
3089 CONST_CAST (char *, src_str),
3090 dest_align, false, 0);
3091 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3092 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3093 return dest_mem;
3096 src_mem = get_memory_rtx (src, len);
3097 set_mem_align (src_mem, src_align);
3099 /* Copy word part most expediently. */
3100 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3101 CALL_EXPR_TAILCALL (exp)
3102 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3103 expected_align, expected_size);
3105 if (dest_addr == 0)
3107 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3108 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3110 return dest_addr;
3114 /* Expand a call EXP to the mempcpy builtin.
3115 Return NULL_RTX if we failed; the caller should emit a normal call,
3116 otherwise try to get the result in TARGET, if convenient (and in
3117 mode MODE if that's convenient). If ENDP is 0 return the
3118 destination pointer, if ENDP is 1 return the end pointer ala
3119 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3120 stpcpy. */
3122 static rtx
3123 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3125 if (!validate_arglist (exp,
3126 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3127 return NULL_RTX;
3128 else
3130 tree dest = CALL_EXPR_ARG (exp, 0);
3131 tree src = CALL_EXPR_ARG (exp, 1);
3132 tree len = CALL_EXPR_ARG (exp, 2);
3133 return expand_builtin_mempcpy_args (dest, src, len,
3134 target, mode, /*endp=*/ 1);
3138 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3139 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3140 so that this can also be called without constructing an actual CALL_EXPR.
3141 The other arguments and return value are the same as for
3142 expand_builtin_mempcpy. */
3144 static rtx
3145 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3146 rtx target, enum machine_mode mode, int endp)
3148 /* If return value is ignored, transform mempcpy into memcpy. */
3149 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3151 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3152 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3153 dest, src, len);
3154 return expand_expr (result, target, mode, EXPAND_NORMAL);
3156 else
3158 const char *src_str;
3159 unsigned int src_align = get_pointer_alignment (src);
3160 unsigned int dest_align = get_pointer_alignment (dest);
3161 rtx dest_mem, src_mem, len_rtx;
3163 /* If either SRC or DEST is not a pointer type, don't do this
3164 operation in-line. */
3165 if (dest_align == 0 || src_align == 0)
3166 return NULL_RTX;
3168 /* If LEN is not constant, call the normal function. */
3169 if (! host_integerp (len, 1))
3170 return NULL_RTX;
3172 len_rtx = expand_normal (len);
3173 src_str = c_getstr (src);
3175 /* If SRC is a string constant and block move would be done
3176 by pieces, we can avoid loading the string from memory
3177 and only stored the computed constants. */
3178 if (src_str
3179 && CONST_INT_P (len_rtx)
3180 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3181 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3182 CONST_CAST (char *, src_str),
3183 dest_align, false))
3185 dest_mem = get_memory_rtx (dest, len);
3186 set_mem_align (dest_mem, dest_align);
3187 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3188 builtin_memcpy_read_str,
3189 CONST_CAST (char *, src_str),
3190 dest_align, false, endp);
3191 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3192 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3193 return dest_mem;
3196 if (CONST_INT_P (len_rtx)
3197 && can_move_by_pieces (INTVAL (len_rtx),
3198 MIN (dest_align, src_align)))
3200 dest_mem = get_memory_rtx (dest, len);
3201 set_mem_align (dest_mem, dest_align);
3202 src_mem = get_memory_rtx (src, len);
3203 set_mem_align (src_mem, src_align);
3204 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3205 MIN (dest_align, src_align), endp);
3206 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3207 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3208 return dest_mem;
3211 return NULL_RTX;
3215 #ifndef HAVE_movstr
3216 # define HAVE_movstr 0
3217 # define CODE_FOR_movstr CODE_FOR_nothing
3218 #endif
3220 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3221 we failed, the caller should emit a normal call, otherwise try to
3222 get the result in TARGET, if convenient. If ENDP is 0 return the
3223 destination pointer, if ENDP is 1 return the end pointer ala
3224 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3225 stpcpy. */
3227 static rtx
3228 expand_movstr (tree dest, tree src, rtx target, int endp)
3230 struct expand_operand ops[3];
3231 rtx dest_mem;
3232 rtx src_mem;
3234 if (!HAVE_movstr)
3235 return NULL_RTX;
3237 dest_mem = get_memory_rtx (dest, NULL);
3238 src_mem = get_memory_rtx (src, NULL);
3239 if (!endp)
3241 target = force_reg (Pmode, XEXP (dest_mem, 0));
3242 dest_mem = replace_equiv_address (dest_mem, target);
3245 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3246 create_fixed_operand (&ops[1], dest_mem);
3247 create_fixed_operand (&ops[2], src_mem);
3248 expand_insn (CODE_FOR_movstr, 3, ops);
3250 if (endp && target != const0_rtx)
3252 target = ops[0].value;
3253 /* movstr is supposed to set end to the address of the NUL
3254 terminator. If the caller requested a mempcpy-like return value,
3255 adjust it. */
3256 if (endp == 1)
3258 rtx tem = plus_constant (GET_MODE (target),
3259 gen_lowpart (GET_MODE (target), target), 1);
3260 emit_move_insn (target, force_operand (tem, NULL_RTX));
3263 return target;
3266 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3267 NULL_RTX if we failed the caller should emit a normal call, otherwise
3268 try to get the result in TARGET, if convenient (and in mode MODE if that's
3269 convenient). */
3271 static rtx
3272 expand_builtin_strcpy (tree exp, rtx target)
3274 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3276 tree dest = CALL_EXPR_ARG (exp, 0);
3277 tree src = CALL_EXPR_ARG (exp, 1);
3278 return expand_builtin_strcpy_args (dest, src, target);
3280 return NULL_RTX;
3283 /* Helper function to do the actual work for expand_builtin_strcpy. The
3284 arguments to the builtin_strcpy call DEST and SRC are broken out
3285 so that this can also be called without constructing an actual CALL_EXPR.
3286 The other arguments and return value are the same as for
3287 expand_builtin_strcpy. */
3289 static rtx
3290 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3292 return expand_movstr (dest, src, target, /*endp=*/0);
3295 /* Expand a call EXP to the stpcpy builtin.
3296 Return NULL_RTX if we failed the caller should emit a normal call,
3297 otherwise try to get the result in TARGET, if convenient (and in
3298 mode MODE if that's convenient). */
3300 static rtx
3301 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3303 tree dst, src;
3304 location_t loc = EXPR_LOCATION (exp);
3306 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3307 return NULL_RTX;
3309 dst = CALL_EXPR_ARG (exp, 0);
3310 src = CALL_EXPR_ARG (exp, 1);
3312 /* If return value is ignored, transform stpcpy into strcpy. */
3313 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3315 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3316 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3317 return expand_expr (result, target, mode, EXPAND_NORMAL);
3319 else
3321 tree len, lenp1;
3322 rtx ret;
3324 /* Ensure we get an actual string whose length can be evaluated at
3325 compile-time, not an expression containing a string. This is
3326 because the latter will potentially produce pessimized code
3327 when used to produce the return value. */
3328 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3329 return expand_movstr (dst, src, target, /*endp=*/2);
3331 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3332 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3333 target, mode, /*endp=*/2);
3335 if (ret)
3336 return ret;
3338 if (TREE_CODE (len) == INTEGER_CST)
3340 rtx len_rtx = expand_normal (len);
3342 if (CONST_INT_P (len_rtx))
3344 ret = expand_builtin_strcpy_args (dst, src, target);
3346 if (ret)
3348 if (! target)
3350 if (mode != VOIDmode)
3351 target = gen_reg_rtx (mode);
3352 else
3353 target = gen_reg_rtx (GET_MODE (ret));
3355 if (GET_MODE (target) != GET_MODE (ret))
3356 ret = gen_lowpart (GET_MODE (target), ret);
3358 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3359 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3360 gcc_assert (ret);
3362 return target;
3367 return expand_movstr (dst, src, target, /*endp=*/2);
3371 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3372 bytes from constant string DATA + OFFSET and return it as target
3373 constant. */
3376 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3377 enum machine_mode mode)
3379 const char *str = (const char *) data;
3381 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3382 return const0_rtx;
3384 return c_readstr (str + offset, mode);
3387 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3388 NULL_RTX if we failed the caller should emit a normal call. */
3390 static rtx
3391 expand_builtin_strncpy (tree exp, rtx target)
3393 location_t loc = EXPR_LOCATION (exp);
3395 if (validate_arglist (exp,
3396 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3398 tree dest = CALL_EXPR_ARG (exp, 0);
3399 tree src = CALL_EXPR_ARG (exp, 1);
3400 tree len = CALL_EXPR_ARG (exp, 2);
3401 tree slen = c_strlen (src, 1);
3403 /* We must be passed a constant len and src parameter. */
3404 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3405 return NULL_RTX;
3407 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3409 /* We're required to pad with trailing zeros if the requested
3410 len is greater than strlen(s2)+1. In that case try to
3411 use store_by_pieces, if it fails, punt. */
3412 if (tree_int_cst_lt (slen, len))
3414 unsigned int dest_align = get_pointer_alignment (dest);
3415 const char *p = c_getstr (src);
3416 rtx dest_mem;
3418 if (!p || dest_align == 0 || !host_integerp (len, 1)
3419 || !can_store_by_pieces (tree_low_cst (len, 1),
3420 builtin_strncpy_read_str,
3421 CONST_CAST (char *, p),
3422 dest_align, false))
3423 return NULL_RTX;
3425 dest_mem = get_memory_rtx (dest, len);
3426 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3427 builtin_strncpy_read_str,
3428 CONST_CAST (char *, p), dest_align, false, 0);
3429 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3430 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3431 return dest_mem;
3434 return NULL_RTX;
3437 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3438 bytes from constant string DATA + OFFSET and return it as target
3439 constant. */
3442 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3443 enum machine_mode mode)
3445 const char *c = (const char *) data;
3446 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3448 memset (p, *c, GET_MODE_SIZE (mode));
3450 return c_readstr (p, mode);
3453 /* Callback routine for store_by_pieces. Return the RTL of a register
3454 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3455 char value given in the RTL register data. For example, if mode is
3456 4 bytes wide, return the RTL for 0x01010101*data. */
3458 static rtx
3459 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3460 enum machine_mode mode)
3462 rtx target, coeff;
3463 size_t size;
3464 char *p;
3466 size = GET_MODE_SIZE (mode);
3467 if (size == 1)
3468 return (rtx) data;
3470 p = XALLOCAVEC (char, size);
3471 memset (p, 1, size);
3472 coeff = c_readstr (p, mode);
3474 target = convert_to_mode (mode, (rtx) data, 1);
3475 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3476 return force_reg (mode, target);
3479 /* Expand expression EXP, which is a call to the memset builtin. Return
3480 NULL_RTX if we failed the caller should emit a normal call, otherwise
3481 try to get the result in TARGET, if convenient (and in mode MODE if that's
3482 convenient). */
3484 static rtx
3485 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3487 if (!validate_arglist (exp,
3488 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3489 return NULL_RTX;
3490 else
3492 tree dest = CALL_EXPR_ARG (exp, 0);
3493 tree val = CALL_EXPR_ARG (exp, 1);
3494 tree len = CALL_EXPR_ARG (exp, 2);
3495 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3499 /* Helper function to do the actual work for expand_builtin_memset. The
3500 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3501 so that this can also be called without constructing an actual CALL_EXPR.
3502 The other arguments and return value are the same as for
3503 expand_builtin_memset. */
3505 static rtx
3506 expand_builtin_memset_args (tree dest, tree val, tree len,
3507 rtx target, enum machine_mode mode, tree orig_exp)
3509 tree fndecl, fn;
3510 enum built_in_function fcode;
3511 enum machine_mode val_mode;
3512 char c;
3513 unsigned int dest_align;
3514 rtx dest_mem, dest_addr, len_rtx;
3515 HOST_WIDE_INT expected_size = -1;
3516 unsigned int expected_align = 0;
3518 dest_align = get_pointer_alignment (dest);
3520 /* If DEST is not a pointer type, don't do this operation in-line. */
3521 if (dest_align == 0)
3522 return NULL_RTX;
3524 if (currently_expanding_gimple_stmt)
3525 stringop_block_profile (currently_expanding_gimple_stmt,
3526 &expected_align, &expected_size);
3528 if (expected_align < dest_align)
3529 expected_align = dest_align;
3531 /* If the LEN parameter is zero, return DEST. */
3532 if (integer_zerop (len))
3534 /* Evaluate and ignore VAL in case it has side-effects. */
3535 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3536 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3539 /* Stabilize the arguments in case we fail. */
3540 dest = builtin_save_expr (dest);
3541 val = builtin_save_expr (val);
3542 len = builtin_save_expr (len);
3544 len_rtx = expand_normal (len);
3545 dest_mem = get_memory_rtx (dest, len);
3546 val_mode = TYPE_MODE (unsigned_char_type_node);
3548 if (TREE_CODE (val) != INTEGER_CST)
3550 rtx val_rtx;
3552 val_rtx = expand_normal (val);
3553 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3555 /* Assume that we can memset by pieces if we can store
3556 * the coefficients by pieces (in the required modes).
3557 * We can't pass builtin_memset_gen_str as that emits RTL. */
3558 c = 1;
3559 if (host_integerp (len, 1)
3560 && can_store_by_pieces (tree_low_cst (len, 1),
3561 builtin_memset_read_str, &c, dest_align,
3562 true))
3564 val_rtx = force_reg (val_mode, val_rtx);
3565 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3566 builtin_memset_gen_str, val_rtx, dest_align,
3567 true, 0);
3569 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3570 dest_align, expected_align,
3571 expected_size))
3572 goto do_libcall;
3574 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3575 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3576 return dest_mem;
3579 if (target_char_cast (val, &c))
3580 goto do_libcall;
3582 if (c)
3584 if (host_integerp (len, 1)
3585 && can_store_by_pieces (tree_low_cst (len, 1),
3586 builtin_memset_read_str, &c, dest_align,
3587 true))
3588 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3589 builtin_memset_read_str, &c, dest_align, true, 0);
3590 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3591 gen_int_mode (c, val_mode),
3592 dest_align, expected_align,
3593 expected_size))
3594 goto do_libcall;
3596 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3597 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3598 return dest_mem;
3601 set_mem_align (dest_mem, dest_align);
3602 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3603 CALL_EXPR_TAILCALL (orig_exp)
3604 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3605 expected_align, expected_size);
3607 if (dest_addr == 0)
3609 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3610 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3613 return dest_addr;
3615 do_libcall:
3616 fndecl = get_callee_fndecl (orig_exp);
3617 fcode = DECL_FUNCTION_CODE (fndecl);
3618 if (fcode == BUILT_IN_MEMSET)
3619 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3620 dest, val, len);
3621 else if (fcode == BUILT_IN_BZERO)
3622 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3623 dest, len);
3624 else
3625 gcc_unreachable ();
3626 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3627 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3628 return expand_call (fn, target, target == const0_rtx);
3631 /* Expand expression EXP, which is a call to the bzero builtin. Return
3632 NULL_RTX if we failed the caller should emit a normal call. */
3634 static rtx
3635 expand_builtin_bzero (tree exp)
3637 tree dest, size;
3638 location_t loc = EXPR_LOCATION (exp);
3640 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3641 return NULL_RTX;
3643 dest = CALL_EXPR_ARG (exp, 0);
3644 size = CALL_EXPR_ARG (exp, 1);
3646 /* New argument list transforming bzero(ptr x, int y) to
3647 memset(ptr x, int 0, size_t y). This is done this way
3648 so that if it isn't expanded inline, we fallback to
3649 calling bzero instead of memset. */
3651 return expand_builtin_memset_args (dest, integer_zero_node,
3652 fold_convert_loc (loc,
3653 size_type_node, size),
3654 const0_rtx, VOIDmode, exp);
3657 /* Expand expression EXP, which is a call to the memcmp built-in function.
3658 Return NULL_RTX if we failed and the caller should emit a normal call,
3659 otherwise try to get the result in TARGET, if convenient (and in mode
3660 MODE, if that's convenient). */
3662 static rtx
3663 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3664 ATTRIBUTE_UNUSED enum machine_mode mode)
3666 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3668 if (!validate_arglist (exp,
3669 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3670 return NULL_RTX;
3672 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3673 implementing memcmp because it will stop if it encounters two
3674 zero bytes. */
3675 #if defined HAVE_cmpmemsi
3677 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3678 rtx result;
3679 rtx insn;
3680 tree arg1 = CALL_EXPR_ARG (exp, 0);
3681 tree arg2 = CALL_EXPR_ARG (exp, 1);
3682 tree len = CALL_EXPR_ARG (exp, 2);
3684 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3685 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3686 enum machine_mode insn_mode;
3688 if (HAVE_cmpmemsi)
3689 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3690 else
3691 return NULL_RTX;
3693 /* If we don't have POINTER_TYPE, call the function. */
3694 if (arg1_align == 0 || arg2_align == 0)
3695 return NULL_RTX;
3697 /* Make a place to write the result of the instruction. */
3698 result = target;
3699 if (! (result != 0
3700 && REG_P (result) && GET_MODE (result) == insn_mode
3701 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3702 result = gen_reg_rtx (insn_mode);
3704 arg1_rtx = get_memory_rtx (arg1, len);
3705 arg2_rtx = get_memory_rtx (arg2, len);
3706 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3708 /* Set MEM_SIZE as appropriate. */
3709 if (CONST_INT_P (arg3_rtx))
3711 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3712 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3715 if (HAVE_cmpmemsi)
3716 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3717 GEN_INT (MIN (arg1_align, arg2_align)));
3718 else
3719 gcc_unreachable ();
3721 if (insn)
3722 emit_insn (insn);
3723 else
3724 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3725 TYPE_MODE (integer_type_node), 3,
3726 XEXP (arg1_rtx, 0), Pmode,
3727 XEXP (arg2_rtx, 0), Pmode,
3728 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3729 TYPE_UNSIGNED (sizetype)),
3730 TYPE_MODE (sizetype));
3732 /* Return the value in the proper mode for this function. */
3733 mode = TYPE_MODE (TREE_TYPE (exp));
3734 if (GET_MODE (result) == mode)
3735 return result;
3736 else if (target != 0)
3738 convert_move (target, result, 0);
3739 return target;
3741 else
3742 return convert_to_mode (mode, result, 0);
3744 #endif /* HAVE_cmpmemsi. */
3746 return NULL_RTX;
3749 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3750 if we failed the caller should emit a normal call, otherwise try to get
3751 the result in TARGET, if convenient. */
3753 static rtx
3754 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3756 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3757 return NULL_RTX;
3759 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3760 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3761 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3763 rtx arg1_rtx, arg2_rtx;
3764 rtx result, insn = NULL_RTX;
3765 tree fndecl, fn;
3766 tree arg1 = CALL_EXPR_ARG (exp, 0);
3767 tree arg2 = CALL_EXPR_ARG (exp, 1);
3769 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3770 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3772 /* If we don't have POINTER_TYPE, call the function. */
3773 if (arg1_align == 0 || arg2_align == 0)
3774 return NULL_RTX;
3776 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3777 arg1 = builtin_save_expr (arg1);
3778 arg2 = builtin_save_expr (arg2);
3780 arg1_rtx = get_memory_rtx (arg1, NULL);
3781 arg2_rtx = get_memory_rtx (arg2, NULL);
3783 #ifdef HAVE_cmpstrsi
3784 /* Try to call cmpstrsi. */
3785 if (HAVE_cmpstrsi)
3787 enum machine_mode insn_mode
3788 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3790 /* Make a place to write the result of the instruction. */
3791 result = target;
3792 if (! (result != 0
3793 && REG_P (result) && GET_MODE (result) == insn_mode
3794 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3795 result = gen_reg_rtx (insn_mode);
3797 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3798 GEN_INT (MIN (arg1_align, arg2_align)));
3800 #endif
3801 #ifdef HAVE_cmpstrnsi
3802 /* Try to determine at least one length and call cmpstrnsi. */
3803 if (!insn && HAVE_cmpstrnsi)
3805 tree len;
3806 rtx arg3_rtx;
3808 enum machine_mode insn_mode
3809 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3810 tree len1 = c_strlen (arg1, 1);
3811 tree len2 = c_strlen (arg2, 1);
3813 if (len1)
3814 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3815 if (len2)
3816 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3818 /* If we don't have a constant length for the first, use the length
3819 of the second, if we know it. We don't require a constant for
3820 this case; some cost analysis could be done if both are available
3821 but neither is constant. For now, assume they're equally cheap,
3822 unless one has side effects. If both strings have constant lengths,
3823 use the smaller. */
3825 if (!len1)
3826 len = len2;
3827 else if (!len2)
3828 len = len1;
3829 else if (TREE_SIDE_EFFECTS (len1))
3830 len = len2;
3831 else if (TREE_SIDE_EFFECTS (len2))
3832 len = len1;
3833 else if (TREE_CODE (len1) != INTEGER_CST)
3834 len = len2;
3835 else if (TREE_CODE (len2) != INTEGER_CST)
3836 len = len1;
3837 else if (tree_int_cst_lt (len1, len2))
3838 len = len1;
3839 else
3840 len = len2;
3842 /* If both arguments have side effects, we cannot optimize. */
3843 if (!len || TREE_SIDE_EFFECTS (len))
3844 goto do_libcall;
3846 arg3_rtx = expand_normal (len);
3848 /* Make a place to write the result of the instruction. */
3849 result = target;
3850 if (! (result != 0
3851 && REG_P (result) && GET_MODE (result) == insn_mode
3852 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3853 result = gen_reg_rtx (insn_mode);
3855 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3856 GEN_INT (MIN (arg1_align, arg2_align)));
3858 #endif
3860 if (insn)
3862 enum machine_mode mode;
3863 emit_insn (insn);
3865 /* Return the value in the proper mode for this function. */
3866 mode = TYPE_MODE (TREE_TYPE (exp));
3867 if (GET_MODE (result) == mode)
3868 return result;
3869 if (target == 0)
3870 return convert_to_mode (mode, result, 0);
3871 convert_move (target, result, 0);
3872 return target;
3875 /* Expand the library call ourselves using a stabilized argument
3876 list to avoid re-evaluating the function's arguments twice. */
3877 #ifdef HAVE_cmpstrnsi
3878 do_libcall:
3879 #endif
3880 fndecl = get_callee_fndecl (exp);
3881 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3882 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3883 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3884 return expand_call (fn, target, target == const0_rtx);
3886 #endif
3887 return NULL_RTX;
3890 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3891 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3892 the result in TARGET, if convenient. */
3894 static rtx
3895 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3896 ATTRIBUTE_UNUSED enum machine_mode mode)
3898 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3900 if (!validate_arglist (exp,
3901 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3902 return NULL_RTX;
3904 /* If c_strlen can determine an expression for one of the string
3905 lengths, and it doesn't have side effects, then emit cmpstrnsi
3906 using length MIN(strlen(string)+1, arg3). */
3907 #ifdef HAVE_cmpstrnsi
3908 if (HAVE_cmpstrnsi)
3910 tree len, len1, len2;
3911 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3912 rtx result, insn;
3913 tree fndecl, fn;
3914 tree arg1 = CALL_EXPR_ARG (exp, 0);
3915 tree arg2 = CALL_EXPR_ARG (exp, 1);
3916 tree arg3 = CALL_EXPR_ARG (exp, 2);
3918 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3919 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3920 enum machine_mode insn_mode
3921 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3923 len1 = c_strlen (arg1, 1);
3924 len2 = c_strlen (arg2, 1);
3926 if (len1)
3927 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3928 if (len2)
3929 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3931 /* If we don't have a constant length for the first, use the length
3932 of the second, if we know it. We don't require a constant for
3933 this case; some cost analysis could be done if both are available
3934 but neither is constant. For now, assume they're equally cheap,
3935 unless one has side effects. If both strings have constant lengths,
3936 use the smaller. */
3938 if (!len1)
3939 len = len2;
3940 else if (!len2)
3941 len = len1;
3942 else if (TREE_SIDE_EFFECTS (len1))
3943 len = len2;
3944 else if (TREE_SIDE_EFFECTS (len2))
3945 len = len1;
3946 else if (TREE_CODE (len1) != INTEGER_CST)
3947 len = len2;
3948 else if (TREE_CODE (len2) != INTEGER_CST)
3949 len = len1;
3950 else if (tree_int_cst_lt (len1, len2))
3951 len = len1;
3952 else
3953 len = len2;
3955 /* If both arguments have side effects, we cannot optimize. */
3956 if (!len || TREE_SIDE_EFFECTS (len))
3957 return NULL_RTX;
3959 /* The actual new length parameter is MIN(len,arg3). */
3960 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3961 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3963 /* If we don't have POINTER_TYPE, call the function. */
3964 if (arg1_align == 0 || arg2_align == 0)
3965 return NULL_RTX;
3967 /* Make a place to write the result of the instruction. */
3968 result = target;
3969 if (! (result != 0
3970 && REG_P (result) && GET_MODE (result) == insn_mode
3971 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3972 result = gen_reg_rtx (insn_mode);
3974 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3975 arg1 = builtin_save_expr (arg1);
3976 arg2 = builtin_save_expr (arg2);
3977 len = builtin_save_expr (len);
3979 arg1_rtx = get_memory_rtx (arg1, len);
3980 arg2_rtx = get_memory_rtx (arg2, len);
3981 arg3_rtx = expand_normal (len);
3982 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3983 GEN_INT (MIN (arg1_align, arg2_align)));
3984 if (insn)
3986 emit_insn (insn);
3988 /* Return the value in the proper mode for this function. */
3989 mode = TYPE_MODE (TREE_TYPE (exp));
3990 if (GET_MODE (result) == mode)
3991 return result;
3992 if (target == 0)
3993 return convert_to_mode (mode, result, 0);
3994 convert_move (target, result, 0);
3995 return target;
3998 /* Expand the library call ourselves using a stabilized argument
3999 list to avoid re-evaluating the function's arguments twice. */
4000 fndecl = get_callee_fndecl (exp);
4001 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4002 arg1, arg2, len);
4003 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4004 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4005 return expand_call (fn, target, target == const0_rtx);
4007 #endif
4008 return NULL_RTX;
4011 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4012 if that's convenient. */
4015 expand_builtin_saveregs (void)
4017 rtx val, seq;
4019 /* Don't do __builtin_saveregs more than once in a function.
4020 Save the result of the first call and reuse it. */
4021 if (saveregs_value != 0)
4022 return saveregs_value;
4024 /* When this function is called, it means that registers must be
4025 saved on entry to this function. So we migrate the call to the
4026 first insn of this function. */
4028 start_sequence ();
4030 /* Do whatever the machine needs done in this case. */
4031 val = targetm.calls.expand_builtin_saveregs ();
4033 seq = get_insns ();
4034 end_sequence ();
4036 saveregs_value = val;
4038 /* Put the insns after the NOTE that starts the function. If this
4039 is inside a start_sequence, make the outer-level insn chain current, so
4040 the code is placed at the start of the function. */
4041 push_topmost_sequence ();
4042 emit_insn_after (seq, entry_of_function ());
4043 pop_topmost_sequence ();
4045 return val;
4048 /* Expand a call to __builtin_next_arg. */
4050 static rtx
4051 expand_builtin_next_arg (void)
4053 /* Checking arguments is already done in fold_builtin_next_arg
4054 that must be called before this function. */
4055 return expand_binop (ptr_mode, add_optab,
4056 crtl->args.internal_arg_pointer,
4057 crtl->args.arg_offset_rtx,
4058 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4061 /* Make it easier for the backends by protecting the valist argument
4062 from multiple evaluations. */
4064 static tree
4065 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4067 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4069 /* The current way of determining the type of valist is completely
4070 bogus. We should have the information on the va builtin instead. */
4071 if (!vatype)
4072 vatype = targetm.fn_abi_va_list (cfun->decl);
4074 if (TREE_CODE (vatype) == ARRAY_TYPE)
4076 if (TREE_SIDE_EFFECTS (valist))
4077 valist = save_expr (valist);
4079 /* For this case, the backends will be expecting a pointer to
4080 vatype, but it's possible we've actually been given an array
4081 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4082 So fix it. */
4083 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4085 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4086 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4089 else
4091 tree pt = build_pointer_type (vatype);
4093 if (! needs_lvalue)
4095 if (! TREE_SIDE_EFFECTS (valist))
4096 return valist;
4098 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4099 TREE_SIDE_EFFECTS (valist) = 1;
4102 if (TREE_SIDE_EFFECTS (valist))
4103 valist = save_expr (valist);
4104 valist = fold_build2_loc (loc, MEM_REF,
4105 vatype, valist, build_int_cst (pt, 0));
4108 return valist;
4111 /* The "standard" definition of va_list is void*. */
4113 tree
4114 std_build_builtin_va_list (void)
4116 return ptr_type_node;
4119 /* The "standard" abi va_list is va_list_type_node. */
4121 tree
4122 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4124 return va_list_type_node;
4127 /* The "standard" type of va_list is va_list_type_node. */
4129 tree
4130 std_canonical_va_list_type (tree type)
4132 tree wtype, htype;
4134 if (INDIRECT_REF_P (type))
4135 type = TREE_TYPE (type);
4136 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4137 type = TREE_TYPE (type);
4138 wtype = va_list_type_node;
4139 htype = type;
4140 /* Treat structure va_list types. */
4141 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4142 htype = TREE_TYPE (htype);
4143 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4145 /* If va_list is an array type, the argument may have decayed
4146 to a pointer type, e.g. by being passed to another function.
4147 In that case, unwrap both types so that we can compare the
4148 underlying records. */
4149 if (TREE_CODE (htype) == ARRAY_TYPE
4150 || POINTER_TYPE_P (htype))
4152 wtype = TREE_TYPE (wtype);
4153 htype = TREE_TYPE (htype);
4156 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4157 return va_list_type_node;
4159 return NULL_TREE;
4162 /* The "standard" implementation of va_start: just assign `nextarg' to
4163 the variable. */
4165 void
4166 std_expand_builtin_va_start (tree valist, rtx nextarg)
4168 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4169 convert_move (va_r, nextarg, 0);
4172 /* Expand EXP, a call to __builtin_va_start. */
4174 static rtx
4175 expand_builtin_va_start (tree exp)
4177 rtx nextarg;
4178 tree valist;
4179 location_t loc = EXPR_LOCATION (exp);
4181 if (call_expr_nargs (exp) < 2)
4183 error_at (loc, "too few arguments to function %<va_start%>");
4184 return const0_rtx;
4187 if (fold_builtin_next_arg (exp, true))
4188 return const0_rtx;
4190 nextarg = expand_builtin_next_arg ();
4191 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4193 if (targetm.expand_builtin_va_start)
4194 targetm.expand_builtin_va_start (valist, nextarg);
4195 else
4196 std_expand_builtin_va_start (valist, nextarg);
4198 return const0_rtx;
4201 /* The "standard" implementation of va_arg: read the value from the
4202 current (padded) address and increment by the (padded) size. */
4204 tree
4205 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4206 gimple_seq *post_p)
4208 tree addr, t, type_size, rounded_size, valist_tmp;
4209 unsigned HOST_WIDE_INT align, boundary;
4210 bool indirect;
4212 #ifdef ARGS_GROW_DOWNWARD
4213 /* All of the alignment and movement below is for args-grow-up machines.
4214 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4215 implement their own specialized gimplify_va_arg_expr routines. */
4216 gcc_unreachable ();
4217 #endif
4219 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4220 if (indirect)
4221 type = build_pointer_type (type);
4223 align = PARM_BOUNDARY / BITS_PER_UNIT;
4224 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4226 /* When we align parameter on stack for caller, if the parameter
4227 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4228 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4229 here with caller. */
4230 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4231 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4233 boundary /= BITS_PER_UNIT;
4235 /* Hoist the valist value into a temporary for the moment. */
4236 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4238 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4239 requires greater alignment, we must perform dynamic alignment. */
4240 if (boundary > align
4241 && !integer_zerop (TYPE_SIZE (type)))
4243 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4244 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4245 gimplify_and_add (t, pre_p);
4247 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4248 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4249 valist_tmp,
4250 build_int_cst (TREE_TYPE (valist), -boundary)));
4251 gimplify_and_add (t, pre_p);
4253 else
4254 boundary = align;
4256 /* If the actual alignment is less than the alignment of the type,
4257 adjust the type accordingly so that we don't assume strict alignment
4258 when dereferencing the pointer. */
4259 boundary *= BITS_PER_UNIT;
4260 if (boundary < TYPE_ALIGN (type))
4262 type = build_variant_type_copy (type);
4263 TYPE_ALIGN (type) = boundary;
4266 /* Compute the rounded size of the type. */
4267 type_size = size_in_bytes (type);
4268 rounded_size = round_up (type_size, align);
4270 /* Reduce rounded_size so it's sharable with the postqueue. */
4271 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4273 /* Get AP. */
4274 addr = valist_tmp;
4275 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4277 /* Small args are padded downward. */
4278 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4279 rounded_size, size_int (align));
4280 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4281 size_binop (MINUS_EXPR, rounded_size, type_size));
4282 addr = fold_build_pointer_plus (addr, t);
4285 /* Compute new value for AP. */
4286 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4287 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4288 gimplify_and_add (t, pre_p);
4290 addr = fold_convert (build_pointer_type (type), addr);
4292 if (indirect)
4293 addr = build_va_arg_indirect_ref (addr);
4295 return build_va_arg_indirect_ref (addr);
4298 /* Build an indirect-ref expression over the given TREE, which represents a
4299 piece of a va_arg() expansion. */
4300 tree
4301 build_va_arg_indirect_ref (tree addr)
4303 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4305 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4306 mf_mark (addr);
4308 return addr;
4311 /* Return a dummy expression of type TYPE in order to keep going after an
4312 error. */
4314 static tree
4315 dummy_object (tree type)
4317 tree t = build_int_cst (build_pointer_type (type), 0);
4318 return build2 (MEM_REF, type, t, t);
4321 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4322 builtin function, but a very special sort of operator. */
4324 enum gimplify_status
4325 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4327 tree promoted_type, have_va_type;
4328 tree valist = TREE_OPERAND (*expr_p, 0);
4329 tree type = TREE_TYPE (*expr_p);
4330 tree t;
4331 location_t loc = EXPR_LOCATION (*expr_p);
4333 /* Verify that valist is of the proper type. */
4334 have_va_type = TREE_TYPE (valist);
4335 if (have_va_type == error_mark_node)
4336 return GS_ERROR;
4337 have_va_type = targetm.canonical_va_list_type (have_va_type);
4339 if (have_va_type == NULL_TREE)
4341 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4342 return GS_ERROR;
4345 /* Generate a diagnostic for requesting data of a type that cannot
4346 be passed through `...' due to type promotion at the call site. */
4347 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4348 != type)
4350 static bool gave_help;
4351 bool warned;
4353 /* Unfortunately, this is merely undefined, rather than a constraint
4354 violation, so we cannot make this an error. If this call is never
4355 executed, the program is still strictly conforming. */
4356 warned = warning_at (loc, 0,
4357 "%qT is promoted to %qT when passed through %<...%>",
4358 type, promoted_type);
4359 if (!gave_help && warned)
4361 gave_help = true;
4362 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4363 promoted_type, type);
4366 /* We can, however, treat "undefined" any way we please.
4367 Call abort to encourage the user to fix the program. */
4368 if (warned)
4369 inform (loc, "if this code is reached, the program will abort");
4370 /* Before the abort, allow the evaluation of the va_list
4371 expression to exit or longjmp. */
4372 gimplify_and_add (valist, pre_p);
4373 t = build_call_expr_loc (loc,
4374 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4375 gimplify_and_add (t, pre_p);
4377 /* This is dead code, but go ahead and finish so that the
4378 mode of the result comes out right. */
4379 *expr_p = dummy_object (type);
4380 return GS_ALL_DONE;
4382 else
4384 /* Make it easier for the backends by protecting the valist argument
4385 from multiple evaluations. */
4386 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4388 /* For this case, the backends will be expecting a pointer to
4389 TREE_TYPE (abi), but it's possible we've
4390 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4391 So fix it. */
4392 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4394 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4395 valist = fold_convert_loc (loc, p1,
4396 build_fold_addr_expr_loc (loc, valist));
4399 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4401 else
4402 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4404 if (!targetm.gimplify_va_arg_expr)
4405 /* FIXME: Once most targets are converted we should merely
4406 assert this is non-null. */
4407 return GS_ALL_DONE;
4409 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4410 return GS_OK;
4414 /* Expand EXP, a call to __builtin_va_end. */
4416 static rtx
4417 expand_builtin_va_end (tree exp)
4419 tree valist = CALL_EXPR_ARG (exp, 0);
4421 /* Evaluate for side effects, if needed. I hate macros that don't
4422 do that. */
4423 if (TREE_SIDE_EFFECTS (valist))
4424 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4426 return const0_rtx;
4429 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4430 builtin rather than just as an assignment in stdarg.h because of the
4431 nastiness of array-type va_list types. */
4433 static rtx
4434 expand_builtin_va_copy (tree exp)
4436 tree dst, src, t;
4437 location_t loc = EXPR_LOCATION (exp);
4439 dst = CALL_EXPR_ARG (exp, 0);
4440 src = CALL_EXPR_ARG (exp, 1);
4442 dst = stabilize_va_list_loc (loc, dst, 1);
4443 src = stabilize_va_list_loc (loc, src, 0);
4445 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4447 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4449 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4450 TREE_SIDE_EFFECTS (t) = 1;
4451 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4453 else
4455 rtx dstb, srcb, size;
4457 /* Evaluate to pointers. */
4458 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4459 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4460 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4461 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4463 dstb = convert_memory_address (Pmode, dstb);
4464 srcb = convert_memory_address (Pmode, srcb);
4466 /* "Dereference" to BLKmode memories. */
4467 dstb = gen_rtx_MEM (BLKmode, dstb);
4468 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4469 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4470 srcb = gen_rtx_MEM (BLKmode, srcb);
4471 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4472 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4474 /* Copy. */
4475 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4478 return const0_rtx;
4481 /* Expand a call to one of the builtin functions __builtin_frame_address or
4482 __builtin_return_address. */
4484 static rtx
4485 expand_builtin_frame_address (tree fndecl, tree exp)
4487 /* The argument must be a nonnegative integer constant.
4488 It counts the number of frames to scan up the stack.
4489 The value is the return address saved in that frame. */
4490 if (call_expr_nargs (exp) == 0)
4491 /* Warning about missing arg was already issued. */
4492 return const0_rtx;
4493 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4495 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4496 error ("invalid argument to %<__builtin_frame_address%>");
4497 else
4498 error ("invalid argument to %<__builtin_return_address%>");
4499 return const0_rtx;
4501 else
4503 rtx tem
4504 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4505 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4507 /* Some ports cannot access arbitrary stack frames. */
4508 if (tem == NULL)
4510 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4511 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4512 else
4513 warning (0, "unsupported argument to %<__builtin_return_address%>");
4514 return const0_rtx;
4517 /* For __builtin_frame_address, return what we've got. */
4518 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4519 return tem;
4521 if (!REG_P (tem)
4522 && ! CONSTANT_P (tem))
4523 tem = copy_addr_to_reg (tem);
4524 return tem;
4528 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4529 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4530 is the same as for allocate_dynamic_stack_space. */
4532 static rtx
4533 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4535 rtx op0;
4536 rtx result;
4537 bool valid_arglist;
4538 unsigned int align;
4539 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4540 == BUILT_IN_ALLOCA_WITH_ALIGN);
4542 /* Emit normal call if we use mudflap. */
4543 if (flag_mudflap)
4544 return NULL_RTX;
4546 valid_arglist
4547 = (alloca_with_align
4548 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4549 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4551 if (!valid_arglist)
4552 return NULL_RTX;
4554 /* Compute the argument. */
4555 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4557 /* Compute the alignment. */
4558 align = (alloca_with_align
4559 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4560 : BIGGEST_ALIGNMENT);
4562 /* Allocate the desired space. */
4563 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4564 result = convert_memory_address (ptr_mode, result);
4566 return result;
4569 /* Expand a call to bswap builtin in EXP.
4570 Return NULL_RTX if a normal call should be emitted rather than expanding the
4571 function in-line. If convenient, the result should be placed in TARGET.
4572 SUBTARGET may be used as the target for computing one of EXP's operands. */
4574 static rtx
4575 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4576 rtx subtarget)
4578 tree arg;
4579 rtx op0;
4581 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4582 return NULL_RTX;
4584 arg = CALL_EXPR_ARG (exp, 0);
4585 op0 = expand_expr (arg,
4586 subtarget && GET_MODE (subtarget) == target_mode
4587 ? subtarget : NULL_RTX,
4588 target_mode, EXPAND_NORMAL);
4589 if (GET_MODE (op0) != target_mode)
4590 op0 = convert_to_mode (target_mode, op0, 1);
4592 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4594 gcc_assert (target);
4596 return convert_to_mode (target_mode, target, 1);
4599 /* Expand a call to a unary builtin in EXP.
4600 Return NULL_RTX if a normal call should be emitted rather than expanding the
4601 function in-line. If convenient, the result should be placed in TARGET.
4602 SUBTARGET may be used as the target for computing one of EXP's operands. */
4604 static rtx
4605 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4606 rtx subtarget, optab op_optab)
4608 rtx op0;
4610 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4611 return NULL_RTX;
4613 /* Compute the argument. */
4614 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4615 (subtarget
4616 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4617 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4618 VOIDmode, EXPAND_NORMAL);
4619 /* Compute op, into TARGET if possible.
4620 Set TARGET to wherever the result comes back. */
4621 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4622 op_optab, op0, target, op_optab != clrsb_optab);
4623 gcc_assert (target);
4625 return convert_to_mode (target_mode, target, 0);
4628 /* Expand a call to __builtin_expect. We just return our argument
4629 as the builtin_expect semantic should've been already executed by
4630 tree branch prediction pass. */
4632 static rtx
4633 expand_builtin_expect (tree exp, rtx target)
4635 tree arg;
4637 if (call_expr_nargs (exp) < 2)
4638 return const0_rtx;
4639 arg = CALL_EXPR_ARG (exp, 0);
4641 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4642 /* When guessing was done, the hints should be already stripped away. */
4643 gcc_assert (!flag_guess_branch_prob
4644 || optimize == 0 || seen_error ());
4645 return target;
4648 /* Expand a call to __builtin_assume_aligned. We just return our first
4649 argument as the builtin_assume_aligned semantic should've been already
4650 executed by CCP. */
4652 static rtx
4653 expand_builtin_assume_aligned (tree exp, rtx target)
4655 if (call_expr_nargs (exp) < 2)
4656 return const0_rtx;
4657 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4658 EXPAND_NORMAL);
4659 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4660 && (call_expr_nargs (exp) < 3
4661 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4662 return target;
4665 void
4666 expand_builtin_trap (void)
4668 #ifdef HAVE_trap
4669 if (HAVE_trap)
4671 rtx insn = emit_insn (gen_trap ());
4672 /* For trap insns when not accumulating outgoing args force
4673 REG_ARGS_SIZE note to prevent crossjumping of calls with
4674 different args sizes. */
4675 if (!ACCUMULATE_OUTGOING_ARGS)
4676 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4678 else
4679 #endif
4680 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4681 emit_barrier ();
4684 /* Expand a call to __builtin_unreachable. We do nothing except emit
4685 a barrier saying that control flow will not pass here.
4687 It is the responsibility of the program being compiled to ensure
4688 that control flow does never reach __builtin_unreachable. */
4689 static void
4690 expand_builtin_unreachable (void)
4692 emit_barrier ();
4695 /* Expand EXP, a call to fabs, fabsf or fabsl.
4696 Return NULL_RTX if a normal call should be emitted rather than expanding
4697 the function inline. If convenient, the result should be placed
4698 in TARGET. SUBTARGET may be used as the target for computing
4699 the operand. */
4701 static rtx
4702 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4704 enum machine_mode mode;
4705 tree arg;
4706 rtx op0;
4708 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4709 return NULL_RTX;
4711 arg = CALL_EXPR_ARG (exp, 0);
4712 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4713 mode = TYPE_MODE (TREE_TYPE (arg));
4714 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4715 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4718 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4719 Return NULL is a normal call should be emitted rather than expanding the
4720 function inline. If convenient, the result should be placed in TARGET.
4721 SUBTARGET may be used as the target for computing the operand. */
4723 static rtx
4724 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4726 rtx op0, op1;
4727 tree arg;
4729 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4730 return NULL_RTX;
4732 arg = CALL_EXPR_ARG (exp, 0);
4733 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4735 arg = CALL_EXPR_ARG (exp, 1);
4736 op1 = expand_normal (arg);
4738 return expand_copysign (op0, op1, target);
4741 /* Create a new constant string literal and return a char* pointer to it.
4742 The STRING_CST value is the LEN characters at STR. */
4743 tree
4744 build_string_literal (int len, const char *str)
4746 tree t, elem, index, type;
4748 t = build_string (len, str);
4749 elem = build_type_variant (char_type_node, 1, 0);
4750 index = build_index_type (size_int (len - 1));
4751 type = build_array_type (elem, index);
4752 TREE_TYPE (t) = type;
4753 TREE_CONSTANT (t) = 1;
4754 TREE_READONLY (t) = 1;
4755 TREE_STATIC (t) = 1;
4757 type = build_pointer_type (elem);
4758 t = build1 (ADDR_EXPR, type,
4759 build4 (ARRAY_REF, elem,
4760 t, integer_zero_node, NULL_TREE, NULL_TREE));
4761 return t;
4764 /* Expand a call to __builtin___clear_cache. */
4766 static rtx
4767 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4769 #ifndef HAVE_clear_cache
4770 #ifdef CLEAR_INSN_CACHE
4771 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4772 does something. Just do the default expansion to a call to
4773 __clear_cache(). */
4774 return NULL_RTX;
4775 #else
4776 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4777 does nothing. There is no need to call it. Do nothing. */
4778 return const0_rtx;
4779 #endif /* CLEAR_INSN_CACHE */
4780 #else
4781 /* We have a "clear_cache" insn, and it will handle everything. */
4782 tree begin, end;
4783 rtx begin_rtx, end_rtx;
4785 /* We must not expand to a library call. If we did, any
4786 fallback library function in libgcc that might contain a call to
4787 __builtin___clear_cache() would recurse infinitely. */
4788 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4790 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4791 return const0_rtx;
4794 if (HAVE_clear_cache)
4796 struct expand_operand ops[2];
4798 begin = CALL_EXPR_ARG (exp, 0);
4799 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4801 end = CALL_EXPR_ARG (exp, 1);
4802 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4804 create_address_operand (&ops[0], begin_rtx);
4805 create_address_operand (&ops[1], end_rtx);
4806 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4807 return const0_rtx;
4809 return const0_rtx;
4810 #endif /* HAVE_clear_cache */
4813 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4815 static rtx
4816 round_trampoline_addr (rtx tramp)
4818 rtx temp, addend, mask;
4820 /* If we don't need too much alignment, we'll have been guaranteed
4821 proper alignment by get_trampoline_type. */
4822 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4823 return tramp;
4825 /* Round address up to desired boundary. */
4826 temp = gen_reg_rtx (Pmode);
4827 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4828 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4830 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4831 temp, 0, OPTAB_LIB_WIDEN);
4832 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4833 temp, 0, OPTAB_LIB_WIDEN);
4835 return tramp;
4838 static rtx
4839 expand_builtin_init_trampoline (tree exp, bool onstack)
4841 tree t_tramp, t_func, t_chain;
4842 rtx m_tramp, r_tramp, r_chain, tmp;
4844 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4845 POINTER_TYPE, VOID_TYPE))
4846 return NULL_RTX;
4848 t_tramp = CALL_EXPR_ARG (exp, 0);
4849 t_func = CALL_EXPR_ARG (exp, 1);
4850 t_chain = CALL_EXPR_ARG (exp, 2);
4852 r_tramp = expand_normal (t_tramp);
4853 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4854 MEM_NOTRAP_P (m_tramp) = 1;
4856 /* If ONSTACK, the TRAMP argument should be the address of a field
4857 within the local function's FRAME decl. Either way, let's see if
4858 we can fill in the MEM_ATTRs for this memory. */
4859 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4860 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4861 true, 0);
4863 /* Creator of a heap trampoline is responsible for making sure the
4864 address is aligned to at least STACK_BOUNDARY. Normally malloc
4865 will ensure this anyhow. */
4866 tmp = round_trampoline_addr (r_tramp);
4867 if (tmp != r_tramp)
4869 m_tramp = change_address (m_tramp, BLKmode, tmp);
4870 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4871 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4874 /* The FUNC argument should be the address of the nested function.
4875 Extract the actual function decl to pass to the hook. */
4876 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4877 t_func = TREE_OPERAND (t_func, 0);
4878 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4880 r_chain = expand_normal (t_chain);
4882 /* Generate insns to initialize the trampoline. */
4883 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4885 if (onstack)
4887 trampolines_created = 1;
4889 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4890 "trampoline generated for nested function %qD", t_func);
4893 return const0_rtx;
4896 static rtx
4897 expand_builtin_adjust_trampoline (tree exp)
4899 rtx tramp;
4901 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4902 return NULL_RTX;
4904 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4905 tramp = round_trampoline_addr (tramp);
4906 if (targetm.calls.trampoline_adjust_address)
4907 tramp = targetm.calls.trampoline_adjust_address (tramp);
4909 return tramp;
4912 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4913 function. The function first checks whether the back end provides
4914 an insn to implement signbit for the respective mode. If not, it
4915 checks whether the floating point format of the value is such that
4916 the sign bit can be extracted. If that is not the case, the
4917 function returns NULL_RTX to indicate that a normal call should be
4918 emitted rather than expanding the function in-line. EXP is the
4919 expression that is a call to the builtin function; if convenient,
4920 the result should be placed in TARGET. */
4921 static rtx
4922 expand_builtin_signbit (tree exp, rtx target)
4924 const struct real_format *fmt;
4925 enum machine_mode fmode, imode, rmode;
4926 tree arg;
4927 int word, bitpos;
4928 enum insn_code icode;
4929 rtx temp;
4930 location_t loc = EXPR_LOCATION (exp);
4932 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4933 return NULL_RTX;
4935 arg = CALL_EXPR_ARG (exp, 0);
4936 fmode = TYPE_MODE (TREE_TYPE (arg));
4937 rmode = TYPE_MODE (TREE_TYPE (exp));
4938 fmt = REAL_MODE_FORMAT (fmode);
4940 arg = builtin_save_expr (arg);
4942 /* Expand the argument yielding a RTX expression. */
4943 temp = expand_normal (arg);
4945 /* Check if the back end provides an insn that handles signbit for the
4946 argument's mode. */
4947 icode = optab_handler (signbit_optab, fmode);
4948 if (icode != CODE_FOR_nothing)
4950 rtx last = get_last_insn ();
4951 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4952 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4953 return target;
4954 delete_insns_since (last);
4957 /* For floating point formats without a sign bit, implement signbit
4958 as "ARG < 0.0". */
4959 bitpos = fmt->signbit_ro;
4960 if (bitpos < 0)
4962 /* But we can't do this if the format supports signed zero. */
4963 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4964 return NULL_RTX;
4966 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4967 build_real (TREE_TYPE (arg), dconst0));
4968 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4971 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4973 imode = int_mode_for_mode (fmode);
4974 if (imode == BLKmode)
4975 return NULL_RTX;
4976 temp = gen_lowpart (imode, temp);
4978 else
4980 imode = word_mode;
4981 /* Handle targets with different FP word orders. */
4982 if (FLOAT_WORDS_BIG_ENDIAN)
4983 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4984 else
4985 word = bitpos / BITS_PER_WORD;
4986 temp = operand_subword_force (temp, word, fmode);
4987 bitpos = bitpos % BITS_PER_WORD;
4990 /* Force the intermediate word_mode (or narrower) result into a
4991 register. This avoids attempting to create paradoxical SUBREGs
4992 of floating point modes below. */
4993 temp = force_reg (imode, temp);
4995 /* If the bitpos is within the "result mode" lowpart, the operation
4996 can be implement with a single bitwise AND. Otherwise, we need
4997 a right shift and an AND. */
4999 if (bitpos < GET_MODE_BITSIZE (rmode))
5001 double_int mask = double_int_zero.set_bit (bitpos);
5003 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5004 temp = gen_lowpart (rmode, temp);
5005 temp = expand_binop (rmode, and_optab, temp,
5006 immed_double_int_const (mask, rmode),
5007 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5009 else
5011 /* Perform a logical right shift to place the signbit in the least
5012 significant bit, then truncate the result to the desired mode
5013 and mask just this bit. */
5014 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5015 temp = gen_lowpart (rmode, temp);
5016 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5017 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5020 return temp;
5023 /* Expand fork or exec calls. TARGET is the desired target of the
5024 call. EXP is the call. FN is the
5025 identificator of the actual function. IGNORE is nonzero if the
5026 value is to be ignored. */
5028 static rtx
5029 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5031 tree id, decl;
5032 tree call;
5034 /* If we are not profiling, just call the function. */
5035 if (!profile_arc_flag)
5036 return NULL_RTX;
5038 /* Otherwise call the wrapper. This should be equivalent for the rest of
5039 compiler, so the code does not diverge, and the wrapper may run the
5040 code necessary for keeping the profiling sane. */
5042 switch (DECL_FUNCTION_CODE (fn))
5044 case BUILT_IN_FORK:
5045 id = get_identifier ("__gcov_fork");
5046 break;
5048 case BUILT_IN_EXECL:
5049 id = get_identifier ("__gcov_execl");
5050 break;
5052 case BUILT_IN_EXECV:
5053 id = get_identifier ("__gcov_execv");
5054 break;
5056 case BUILT_IN_EXECLP:
5057 id = get_identifier ("__gcov_execlp");
5058 break;
5060 case BUILT_IN_EXECLE:
5061 id = get_identifier ("__gcov_execle");
5062 break;
5064 case BUILT_IN_EXECVP:
5065 id = get_identifier ("__gcov_execvp");
5066 break;
5068 case BUILT_IN_EXECVE:
5069 id = get_identifier ("__gcov_execve");
5070 break;
5072 default:
5073 gcc_unreachable ();
5076 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5077 FUNCTION_DECL, id, TREE_TYPE (fn));
5078 DECL_EXTERNAL (decl) = 1;
5079 TREE_PUBLIC (decl) = 1;
5080 DECL_ARTIFICIAL (decl) = 1;
5081 TREE_NOTHROW (decl) = 1;
5082 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5083 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5084 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5085 return expand_call (call, target, ignore);
5090 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5091 the pointer in these functions is void*, the tree optimizers may remove
5092 casts. The mode computed in expand_builtin isn't reliable either, due
5093 to __sync_bool_compare_and_swap.
5095 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5096 group of builtins. This gives us log2 of the mode size. */
5098 static inline enum machine_mode
5099 get_builtin_sync_mode (int fcode_diff)
5101 /* The size is not negotiable, so ask not to get BLKmode in return
5102 if the target indicates that a smaller size would be better. */
5103 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5106 /* Expand the memory expression LOC and return the appropriate memory operand
5107 for the builtin_sync operations. */
5109 static rtx
5110 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5112 rtx addr, mem;
5114 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5115 addr = convert_memory_address (Pmode, addr);
5117 /* Note that we explicitly do not want any alias information for this
5118 memory, so that we kill all other live memories. Otherwise we don't
5119 satisfy the full barrier semantics of the intrinsic. */
5120 mem = validize_mem (gen_rtx_MEM (mode, addr));
5122 /* The alignment needs to be at least according to that of the mode. */
5123 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5124 get_pointer_alignment (loc)));
5125 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5126 MEM_VOLATILE_P (mem) = 1;
5128 return mem;
5131 /* Make sure an argument is in the right mode.
5132 EXP is the tree argument.
5133 MODE is the mode it should be in. */
5135 static rtx
5136 expand_expr_force_mode (tree exp, enum machine_mode mode)
5138 rtx val;
5139 enum machine_mode old_mode;
5141 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5142 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5143 of CONST_INTs, where we know the old_mode only from the call argument. */
5145 old_mode = GET_MODE (val);
5146 if (old_mode == VOIDmode)
5147 old_mode = TYPE_MODE (TREE_TYPE (exp));
5148 val = convert_modes (mode, old_mode, val, 1);
5149 return val;
5153 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5154 EXP is the CALL_EXPR. CODE is the rtx code
5155 that corresponds to the arithmetic or logical operation from the name;
5156 an exception here is that NOT actually means NAND. TARGET is an optional
5157 place for us to store the results; AFTER is true if this is the
5158 fetch_and_xxx form. */
5160 static rtx
5161 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5162 enum rtx_code code, bool after,
5163 rtx target)
5165 rtx val, mem;
5166 location_t loc = EXPR_LOCATION (exp);
5168 if (code == NOT && warn_sync_nand)
5170 tree fndecl = get_callee_fndecl (exp);
5171 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5173 static bool warned_f_a_n, warned_n_a_f;
5175 switch (fcode)
5177 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5178 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5179 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5180 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5181 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5182 if (warned_f_a_n)
5183 break;
5185 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5186 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5187 warned_f_a_n = true;
5188 break;
5190 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5191 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5192 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5193 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5194 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5195 if (warned_n_a_f)
5196 break;
5198 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5199 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5200 warned_n_a_f = true;
5201 break;
5203 default:
5204 gcc_unreachable ();
5208 /* Expand the operands. */
5209 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5210 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5212 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5213 after);
5216 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5217 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5218 true if this is the boolean form. TARGET is a place for us to store the
5219 results; this is NOT optional if IS_BOOL is true. */
5221 static rtx
5222 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5223 bool is_bool, rtx target)
5225 rtx old_val, new_val, mem;
5226 rtx *pbool, *poval;
5228 /* Expand the operands. */
5229 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5230 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5231 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5233 pbool = poval = NULL;
5234 if (target != const0_rtx)
5236 if (is_bool)
5237 pbool = &target;
5238 else
5239 poval = &target;
5241 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5242 false, MEMMODEL_SEQ_CST,
5243 MEMMODEL_SEQ_CST))
5244 return NULL_RTX;
5246 return target;
5249 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5250 general form is actually an atomic exchange, and some targets only
5251 support a reduced form with the second argument being a constant 1.
5252 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5253 the results. */
5255 static rtx
5256 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5257 rtx target)
5259 rtx val, mem;
5261 /* Expand the operands. */
5262 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5263 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5265 return expand_sync_lock_test_and_set (target, mem, val);
5268 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5270 static void
5271 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5273 rtx mem;
5275 /* Expand the operands. */
5276 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5278 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5281 /* Given an integer representing an ``enum memmodel'', verify its
5282 correctness and return the memory model enum. */
5284 static enum memmodel
5285 get_memmodel (tree exp)
5287 rtx op;
5288 unsigned HOST_WIDE_INT val;
5290 /* If the parameter is not a constant, it's a run time value so we'll just
5291 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5292 if (TREE_CODE (exp) != INTEGER_CST)
5293 return MEMMODEL_SEQ_CST;
5295 op = expand_normal (exp);
5297 val = INTVAL (op);
5298 if (targetm.memmodel_check)
5299 val = targetm.memmodel_check (val);
5300 else if (val & ~MEMMODEL_MASK)
5302 warning (OPT_Winvalid_memory_model,
5303 "Unknown architecture specifier in memory model to builtin.");
5304 return MEMMODEL_SEQ_CST;
5307 if ((INTVAL(op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5309 warning (OPT_Winvalid_memory_model,
5310 "invalid memory model argument to builtin");
5311 return MEMMODEL_SEQ_CST;
5314 return (enum memmodel) val;
5317 /* Expand the __atomic_exchange intrinsic:
5318 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5319 EXP is the CALL_EXPR.
5320 TARGET is an optional place for us to store the results. */
5322 static rtx
5323 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5325 rtx val, mem;
5326 enum memmodel model;
5328 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5329 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5331 error ("invalid memory model for %<__atomic_exchange%>");
5332 return NULL_RTX;
5335 if (!flag_inline_atomics)
5336 return NULL_RTX;
5338 /* Expand the operands. */
5339 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5340 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5342 return expand_atomic_exchange (target, mem, val, model);
5345 /* Expand the __atomic_compare_exchange intrinsic:
5346 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5347 TYPE desired, BOOL weak,
5348 enum memmodel success,
5349 enum memmodel failure)
5350 EXP is the CALL_EXPR.
5351 TARGET is an optional place for us to store the results. */
5353 static rtx
5354 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5355 rtx target)
5357 rtx expect, desired, mem, oldval;
5358 enum memmodel success, failure;
5359 tree weak;
5360 bool is_weak;
5362 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5363 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5365 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5366 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5368 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5369 return NULL_RTX;
5372 if (failure > success)
5374 error ("failure memory model cannot be stronger than success "
5375 "memory model for %<__atomic_compare_exchange%>");
5376 return NULL_RTX;
5379 if (!flag_inline_atomics)
5380 return NULL_RTX;
5382 /* Expand the operands. */
5383 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5385 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5386 expect = convert_memory_address (Pmode, expect);
5387 expect = gen_rtx_MEM (mode, expect);
5388 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5390 weak = CALL_EXPR_ARG (exp, 3);
5391 is_weak = false;
5392 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5393 is_weak = true;
5395 oldval = expect;
5396 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5397 &oldval, mem, oldval, desired,
5398 is_weak, success, failure))
5399 return NULL_RTX;
5401 if (oldval != expect)
5402 emit_move_insn (expect, oldval);
5404 return target;
5407 /* Expand the __atomic_load intrinsic:
5408 TYPE __atomic_load (TYPE *object, enum memmodel)
5409 EXP is the CALL_EXPR.
5410 TARGET is an optional place for us to store the results. */
5412 static rtx
5413 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5415 rtx mem;
5416 enum memmodel model;
5418 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5419 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5420 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5422 error ("invalid memory model for %<__atomic_load%>");
5423 return NULL_RTX;
5426 if (!flag_inline_atomics)
5427 return NULL_RTX;
5429 /* Expand the operand. */
5430 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5432 return expand_atomic_load (target, mem, model);
5436 /* Expand the __atomic_store intrinsic:
5437 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5438 EXP is the CALL_EXPR.
5439 TARGET is an optional place for us to store the results. */
5441 static rtx
5442 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5444 rtx mem, val;
5445 enum memmodel model;
5447 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5448 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5449 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5450 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5452 error ("invalid memory model for %<__atomic_store%>");
5453 return NULL_RTX;
5456 if (!flag_inline_atomics)
5457 return NULL_RTX;
5459 /* Expand the operands. */
5460 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5461 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5463 return expand_atomic_store (mem, val, model, false);
5466 /* Expand the __atomic_fetch_XXX intrinsic:
5467 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5468 EXP is the CALL_EXPR.
5469 TARGET is an optional place for us to store the results.
5470 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5471 FETCH_AFTER is true if returning the result of the operation.
5472 FETCH_AFTER is false if returning the value before the operation.
5473 IGNORE is true if the result is not used.
5474 EXT_CALL is the correct builtin for an external call if this cannot be
5475 resolved to an instruction sequence. */
5477 static rtx
5478 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5479 enum rtx_code code, bool fetch_after,
5480 bool ignore, enum built_in_function ext_call)
5482 rtx val, mem, ret;
5483 enum memmodel model;
5484 tree fndecl;
5485 tree addr;
5487 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5489 /* Expand the operands. */
5490 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5491 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5493 /* Only try generating instructions if inlining is turned on. */
5494 if (flag_inline_atomics)
5496 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5497 if (ret)
5498 return ret;
5501 /* Return if a different routine isn't needed for the library call. */
5502 if (ext_call == BUILT_IN_NONE)
5503 return NULL_RTX;
5505 /* Change the call to the specified function. */
5506 fndecl = get_callee_fndecl (exp);
5507 addr = CALL_EXPR_FN (exp);
5508 STRIP_NOPS (addr);
5510 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5511 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5513 /* Expand the call here so we can emit trailing code. */
5514 ret = expand_call (exp, target, ignore);
5516 /* Replace the original function just in case it matters. */
5517 TREE_OPERAND (addr, 0) = fndecl;
5519 /* Then issue the arithmetic correction to return the right result. */
5520 if (!ignore)
5522 if (code == NOT)
5524 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5525 OPTAB_LIB_WIDEN);
5526 ret = expand_simple_unop (mode, NOT, ret, target, true);
5528 else
5529 ret = expand_simple_binop (mode, code, ret, val, target, true,
5530 OPTAB_LIB_WIDEN);
5532 return ret;
5536 #ifndef HAVE_atomic_clear
5537 # define HAVE_atomic_clear 0
5538 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5539 #endif
5541 /* Expand an atomic clear operation.
5542 void _atomic_clear (BOOL *obj, enum memmodel)
5543 EXP is the call expression. */
5545 static rtx
5546 expand_builtin_atomic_clear (tree exp)
5548 enum machine_mode mode;
5549 rtx mem, ret;
5550 enum memmodel model;
5552 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5553 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5554 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5556 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5557 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5559 error ("invalid memory model for %<__atomic_store%>");
5560 return const0_rtx;
5563 if (HAVE_atomic_clear)
5565 emit_insn (gen_atomic_clear (mem, model));
5566 return const0_rtx;
5569 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5570 Failing that, a store is issued by __atomic_store. The only way this can
5571 fail is if the bool type is larger than a word size. Unlikely, but
5572 handle it anyway for completeness. Assume a single threaded model since
5573 there is no atomic support in this case, and no barriers are required. */
5574 ret = expand_atomic_store (mem, const0_rtx, model, true);
5575 if (!ret)
5576 emit_move_insn (mem, const0_rtx);
5577 return const0_rtx;
5580 /* Expand an atomic test_and_set operation.
5581 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5582 EXP is the call expression. */
5584 static rtx
5585 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5587 rtx mem;
5588 enum memmodel model;
5589 enum machine_mode mode;
5591 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5592 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5593 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5595 return expand_atomic_test_and_set (target, mem, model);
5599 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5600 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5602 static tree
5603 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5605 int size;
5606 enum machine_mode mode;
5607 unsigned int mode_align, type_align;
5609 if (TREE_CODE (arg0) != INTEGER_CST)
5610 return NULL_TREE;
5612 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5613 mode = mode_for_size (size, MODE_INT, 0);
5614 mode_align = GET_MODE_ALIGNMENT (mode);
5616 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5617 type_align = mode_align;
5618 else
5620 tree ttype = TREE_TYPE (arg1);
5622 /* This function is usually invoked and folded immediately by the front
5623 end before anything else has a chance to look at it. The pointer
5624 parameter at this point is usually cast to a void *, so check for that
5625 and look past the cast. */
5626 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5627 && VOID_TYPE_P (TREE_TYPE (ttype)))
5628 arg1 = TREE_OPERAND (arg1, 0);
5630 ttype = TREE_TYPE (arg1);
5631 gcc_assert (POINTER_TYPE_P (ttype));
5633 /* Get the underlying type of the object. */
5634 ttype = TREE_TYPE (ttype);
5635 type_align = TYPE_ALIGN (ttype);
5638 /* If the object has smaller alignment, the the lock free routines cannot
5639 be used. */
5640 if (type_align < mode_align)
5641 return boolean_false_node;
5643 /* Check if a compare_and_swap pattern exists for the mode which represents
5644 the required size. The pattern is not allowed to fail, so the existence
5645 of the pattern indicates support is present. */
5646 if (can_compare_and_swap_p (mode, true))
5647 return boolean_true_node;
5648 else
5649 return boolean_false_node;
5652 /* Return true if the parameters to call EXP represent an object which will
5653 always generate lock free instructions. The first argument represents the
5654 size of the object, and the second parameter is a pointer to the object
5655 itself. If NULL is passed for the object, then the result is based on
5656 typical alignment for an object of the specified size. Otherwise return
5657 false. */
5659 static rtx
5660 expand_builtin_atomic_always_lock_free (tree exp)
5662 tree size;
5663 tree arg0 = CALL_EXPR_ARG (exp, 0);
5664 tree arg1 = CALL_EXPR_ARG (exp, 1);
5666 if (TREE_CODE (arg0) != INTEGER_CST)
5668 error ("non-constant argument 1 to __atomic_always_lock_free");
5669 return const0_rtx;
5672 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5673 if (size == boolean_true_node)
5674 return const1_rtx;
5675 return const0_rtx;
5678 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5679 is lock free on this architecture. */
5681 static tree
5682 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5684 if (!flag_inline_atomics)
5685 return NULL_TREE;
5687 /* If it isn't always lock free, don't generate a result. */
5688 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5689 return boolean_true_node;
5691 return NULL_TREE;
5694 /* Return true if the parameters to call EXP represent an object which will
5695 always generate lock free instructions. The first argument represents the
5696 size of the object, and the second parameter is a pointer to the object
5697 itself. If NULL is passed for the object, then the result is based on
5698 typical alignment for an object of the specified size. Otherwise return
5699 NULL*/
5701 static rtx
5702 expand_builtin_atomic_is_lock_free (tree exp)
5704 tree size;
5705 tree arg0 = CALL_EXPR_ARG (exp, 0);
5706 tree arg1 = CALL_EXPR_ARG (exp, 1);
5708 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5710 error ("non-integer argument 1 to __atomic_is_lock_free");
5711 return NULL_RTX;
5714 if (!flag_inline_atomics)
5715 return NULL_RTX;
5717 /* If the value is known at compile time, return the RTX for it. */
5718 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5719 if (size == boolean_true_node)
5720 return const1_rtx;
5722 return NULL_RTX;
5725 /* Expand the __atomic_thread_fence intrinsic:
5726 void __atomic_thread_fence (enum memmodel)
5727 EXP is the CALL_EXPR. */
5729 static void
5730 expand_builtin_atomic_thread_fence (tree exp)
5732 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5733 expand_mem_thread_fence (model);
5736 /* Expand the __atomic_signal_fence intrinsic:
5737 void __atomic_signal_fence (enum memmodel)
5738 EXP is the CALL_EXPR. */
5740 static void
5741 expand_builtin_atomic_signal_fence (tree exp)
5743 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5744 expand_mem_signal_fence (model);
5747 /* Expand the __sync_synchronize intrinsic. */
5749 static void
5750 expand_builtin_sync_synchronize (void)
5752 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5755 static rtx
5756 expand_builtin_thread_pointer (tree exp, rtx target)
5758 enum insn_code icode;
5759 if (!validate_arglist (exp, VOID_TYPE))
5760 return const0_rtx;
5761 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5762 if (icode != CODE_FOR_nothing)
5764 struct expand_operand op;
5765 if (!REG_P (target) || GET_MODE (target) != Pmode)
5766 target = gen_reg_rtx (Pmode);
5767 create_output_operand (&op, target, Pmode);
5768 expand_insn (icode, 1, &op);
5769 return target;
5771 error ("__builtin_thread_pointer is not supported on this target");
5772 return const0_rtx;
5775 static void
5776 expand_builtin_set_thread_pointer (tree exp)
5778 enum insn_code icode;
5779 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5780 return;
5781 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5782 if (icode != CODE_FOR_nothing)
5784 struct expand_operand op;
5785 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5786 Pmode, EXPAND_NORMAL);
5787 create_input_operand (&op, val, Pmode);
5788 expand_insn (icode, 1, &op);
5789 return;
5791 error ("__builtin_set_thread_pointer is not supported on this target");
5795 /* Expand an expression EXP that calls a built-in function,
5796 with result going to TARGET if that's convenient
5797 (and in mode MODE if that's convenient).
5798 SUBTARGET may be used as the target for computing one of EXP's operands.
5799 IGNORE is nonzero if the value is to be ignored. */
5802 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5803 int ignore)
5805 tree fndecl = get_callee_fndecl (exp);
5806 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5807 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5808 int flags;
5810 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5811 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5813 /* When not optimizing, generate calls to library functions for a certain
5814 set of builtins. */
5815 if (!optimize
5816 && !called_as_built_in (fndecl)
5817 && fcode != BUILT_IN_ALLOCA
5818 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5819 && fcode != BUILT_IN_FREE)
5820 return expand_call (exp, target, ignore);
5822 /* The built-in function expanders test for target == const0_rtx
5823 to determine whether the function's result will be ignored. */
5824 if (ignore)
5825 target = const0_rtx;
5827 /* If the result of a pure or const built-in function is ignored, and
5828 none of its arguments are volatile, we can avoid expanding the
5829 built-in call and just evaluate the arguments for side-effects. */
5830 if (target == const0_rtx
5831 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5832 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5834 bool volatilep = false;
5835 tree arg;
5836 call_expr_arg_iterator iter;
5838 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5839 if (TREE_THIS_VOLATILE (arg))
5841 volatilep = true;
5842 break;
5845 if (! volatilep)
5847 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5848 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5849 return const0_rtx;
5853 switch (fcode)
5855 CASE_FLT_FN (BUILT_IN_FABS):
5856 target = expand_builtin_fabs (exp, target, subtarget);
5857 if (target)
5858 return target;
5859 break;
5861 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5862 target = expand_builtin_copysign (exp, target, subtarget);
5863 if (target)
5864 return target;
5865 break;
5867 /* Just do a normal library call if we were unable to fold
5868 the values. */
5869 CASE_FLT_FN (BUILT_IN_CABS):
5870 break;
5872 CASE_FLT_FN (BUILT_IN_EXP):
5873 CASE_FLT_FN (BUILT_IN_EXP10):
5874 CASE_FLT_FN (BUILT_IN_POW10):
5875 CASE_FLT_FN (BUILT_IN_EXP2):
5876 CASE_FLT_FN (BUILT_IN_EXPM1):
5877 CASE_FLT_FN (BUILT_IN_LOGB):
5878 CASE_FLT_FN (BUILT_IN_LOG):
5879 CASE_FLT_FN (BUILT_IN_LOG10):
5880 CASE_FLT_FN (BUILT_IN_LOG2):
5881 CASE_FLT_FN (BUILT_IN_LOG1P):
5882 CASE_FLT_FN (BUILT_IN_TAN):
5883 CASE_FLT_FN (BUILT_IN_ASIN):
5884 CASE_FLT_FN (BUILT_IN_ACOS):
5885 CASE_FLT_FN (BUILT_IN_ATAN):
5886 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5887 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5888 because of possible accuracy problems. */
5889 if (! flag_unsafe_math_optimizations)
5890 break;
5891 CASE_FLT_FN (BUILT_IN_SQRT):
5892 CASE_FLT_FN (BUILT_IN_FLOOR):
5893 CASE_FLT_FN (BUILT_IN_CEIL):
5894 CASE_FLT_FN (BUILT_IN_TRUNC):
5895 CASE_FLT_FN (BUILT_IN_ROUND):
5896 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5897 CASE_FLT_FN (BUILT_IN_RINT):
5898 target = expand_builtin_mathfn (exp, target, subtarget);
5899 if (target)
5900 return target;
5901 break;
5903 CASE_FLT_FN (BUILT_IN_FMA):
5904 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5905 if (target)
5906 return target;
5907 break;
5909 CASE_FLT_FN (BUILT_IN_ILOGB):
5910 if (! flag_unsafe_math_optimizations)
5911 break;
5912 CASE_FLT_FN (BUILT_IN_ISINF):
5913 CASE_FLT_FN (BUILT_IN_FINITE):
5914 case BUILT_IN_ISFINITE:
5915 case BUILT_IN_ISNORMAL:
5916 target = expand_builtin_interclass_mathfn (exp, target);
5917 if (target)
5918 return target;
5919 break;
5921 CASE_FLT_FN (BUILT_IN_ICEIL):
5922 CASE_FLT_FN (BUILT_IN_LCEIL):
5923 CASE_FLT_FN (BUILT_IN_LLCEIL):
5924 CASE_FLT_FN (BUILT_IN_LFLOOR):
5925 CASE_FLT_FN (BUILT_IN_IFLOOR):
5926 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5927 target = expand_builtin_int_roundingfn (exp, target);
5928 if (target)
5929 return target;
5930 break;
5932 CASE_FLT_FN (BUILT_IN_IRINT):
5933 CASE_FLT_FN (BUILT_IN_LRINT):
5934 CASE_FLT_FN (BUILT_IN_LLRINT):
5935 CASE_FLT_FN (BUILT_IN_IROUND):
5936 CASE_FLT_FN (BUILT_IN_LROUND):
5937 CASE_FLT_FN (BUILT_IN_LLROUND):
5938 target = expand_builtin_int_roundingfn_2 (exp, target);
5939 if (target)
5940 return target;
5941 break;
5943 CASE_FLT_FN (BUILT_IN_POWI):
5944 target = expand_builtin_powi (exp, target);
5945 if (target)
5946 return target;
5947 break;
5949 CASE_FLT_FN (BUILT_IN_ATAN2):
5950 CASE_FLT_FN (BUILT_IN_LDEXP):
5951 CASE_FLT_FN (BUILT_IN_SCALB):
5952 CASE_FLT_FN (BUILT_IN_SCALBN):
5953 CASE_FLT_FN (BUILT_IN_SCALBLN):
5954 if (! flag_unsafe_math_optimizations)
5955 break;
5957 CASE_FLT_FN (BUILT_IN_FMOD):
5958 CASE_FLT_FN (BUILT_IN_REMAINDER):
5959 CASE_FLT_FN (BUILT_IN_DREM):
5960 CASE_FLT_FN (BUILT_IN_POW):
5961 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5962 if (target)
5963 return target;
5964 break;
5966 CASE_FLT_FN (BUILT_IN_CEXPI):
5967 target = expand_builtin_cexpi (exp, target);
5968 gcc_assert (target);
5969 return target;
5971 CASE_FLT_FN (BUILT_IN_SIN):
5972 CASE_FLT_FN (BUILT_IN_COS):
5973 if (! flag_unsafe_math_optimizations)
5974 break;
5975 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5976 if (target)
5977 return target;
5978 break;
5980 CASE_FLT_FN (BUILT_IN_SINCOS):
5981 if (! flag_unsafe_math_optimizations)
5982 break;
5983 target = expand_builtin_sincos (exp);
5984 if (target)
5985 return target;
5986 break;
5988 case BUILT_IN_APPLY_ARGS:
5989 return expand_builtin_apply_args ();
5991 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5992 FUNCTION with a copy of the parameters described by
5993 ARGUMENTS, and ARGSIZE. It returns a block of memory
5994 allocated on the stack into which is stored all the registers
5995 that might possibly be used for returning the result of a
5996 function. ARGUMENTS is the value returned by
5997 __builtin_apply_args. ARGSIZE is the number of bytes of
5998 arguments that must be copied. ??? How should this value be
5999 computed? We'll also need a safe worst case value for varargs
6000 functions. */
6001 case BUILT_IN_APPLY:
6002 if (!validate_arglist (exp, POINTER_TYPE,
6003 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6004 && !validate_arglist (exp, REFERENCE_TYPE,
6005 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6006 return const0_rtx;
6007 else
6009 rtx ops[3];
6011 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6012 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6013 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6015 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6018 /* __builtin_return (RESULT) causes the function to return the
6019 value described by RESULT. RESULT is address of the block of
6020 memory returned by __builtin_apply. */
6021 case BUILT_IN_RETURN:
6022 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6023 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6024 return const0_rtx;
6026 case BUILT_IN_SAVEREGS:
6027 return expand_builtin_saveregs ();
6029 case BUILT_IN_VA_ARG_PACK:
6030 /* All valid uses of __builtin_va_arg_pack () are removed during
6031 inlining. */
6032 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6033 return const0_rtx;
6035 case BUILT_IN_VA_ARG_PACK_LEN:
6036 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6037 inlining. */
6038 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6039 return const0_rtx;
6041 /* Return the address of the first anonymous stack arg. */
6042 case BUILT_IN_NEXT_ARG:
6043 if (fold_builtin_next_arg (exp, false))
6044 return const0_rtx;
6045 return expand_builtin_next_arg ();
6047 case BUILT_IN_CLEAR_CACHE:
6048 target = expand_builtin___clear_cache (exp);
6049 if (target)
6050 return target;
6051 break;
6053 case BUILT_IN_CLASSIFY_TYPE:
6054 return expand_builtin_classify_type (exp);
6056 case BUILT_IN_CONSTANT_P:
6057 return const0_rtx;
6059 case BUILT_IN_FRAME_ADDRESS:
6060 case BUILT_IN_RETURN_ADDRESS:
6061 return expand_builtin_frame_address (fndecl, exp);
6063 /* Returns the address of the area where the structure is returned.
6064 0 otherwise. */
6065 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6066 if (call_expr_nargs (exp) != 0
6067 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6068 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6069 return const0_rtx;
6070 else
6071 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6073 case BUILT_IN_ALLOCA:
6074 case BUILT_IN_ALLOCA_WITH_ALIGN:
6075 /* If the allocation stems from the declaration of a variable-sized
6076 object, it cannot accumulate. */
6077 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6078 if (target)
6079 return target;
6080 break;
6082 case BUILT_IN_STACK_SAVE:
6083 return expand_stack_save ();
6085 case BUILT_IN_STACK_RESTORE:
6086 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6087 return const0_rtx;
6089 case BUILT_IN_BSWAP16:
6090 case BUILT_IN_BSWAP32:
6091 case BUILT_IN_BSWAP64:
6092 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6093 if (target)
6094 return target;
6095 break;
6097 CASE_INT_FN (BUILT_IN_FFS):
6098 case BUILT_IN_FFSIMAX:
6099 target = expand_builtin_unop (target_mode, exp, target,
6100 subtarget, ffs_optab);
6101 if (target)
6102 return target;
6103 break;
6105 CASE_INT_FN (BUILT_IN_CLZ):
6106 case BUILT_IN_CLZIMAX:
6107 target = expand_builtin_unop (target_mode, exp, target,
6108 subtarget, clz_optab);
6109 if (target)
6110 return target;
6111 break;
6113 CASE_INT_FN (BUILT_IN_CTZ):
6114 case BUILT_IN_CTZIMAX:
6115 target = expand_builtin_unop (target_mode, exp, target,
6116 subtarget, ctz_optab);
6117 if (target)
6118 return target;
6119 break;
6121 CASE_INT_FN (BUILT_IN_CLRSB):
6122 case BUILT_IN_CLRSBIMAX:
6123 target = expand_builtin_unop (target_mode, exp, target,
6124 subtarget, clrsb_optab);
6125 if (target)
6126 return target;
6127 break;
6129 CASE_INT_FN (BUILT_IN_POPCOUNT):
6130 case BUILT_IN_POPCOUNTIMAX:
6131 target = expand_builtin_unop (target_mode, exp, target,
6132 subtarget, popcount_optab);
6133 if (target)
6134 return target;
6135 break;
6137 CASE_INT_FN (BUILT_IN_PARITY):
6138 case BUILT_IN_PARITYIMAX:
6139 target = expand_builtin_unop (target_mode, exp, target,
6140 subtarget, parity_optab);
6141 if (target)
6142 return target;
6143 break;
6145 case BUILT_IN_STRLEN:
6146 target = expand_builtin_strlen (exp, target, target_mode);
6147 if (target)
6148 return target;
6149 break;
6151 case BUILT_IN_STRCPY:
6152 target = expand_builtin_strcpy (exp, target);
6153 if (target)
6154 return target;
6155 break;
6157 case BUILT_IN_STRNCPY:
6158 target = expand_builtin_strncpy (exp, target);
6159 if (target)
6160 return target;
6161 break;
6163 case BUILT_IN_STPCPY:
6164 target = expand_builtin_stpcpy (exp, target, mode);
6165 if (target)
6166 return target;
6167 break;
6169 case BUILT_IN_MEMCPY:
6170 target = expand_builtin_memcpy (exp, target);
6171 if (target)
6172 return target;
6173 break;
6175 case BUILT_IN_MEMPCPY:
6176 target = expand_builtin_mempcpy (exp, target, mode);
6177 if (target)
6178 return target;
6179 break;
6181 case BUILT_IN_MEMSET:
6182 target = expand_builtin_memset (exp, target, mode);
6183 if (target)
6184 return target;
6185 break;
6187 case BUILT_IN_BZERO:
6188 target = expand_builtin_bzero (exp);
6189 if (target)
6190 return target;
6191 break;
6193 case BUILT_IN_STRCMP:
6194 target = expand_builtin_strcmp (exp, target);
6195 if (target)
6196 return target;
6197 break;
6199 case BUILT_IN_STRNCMP:
6200 target = expand_builtin_strncmp (exp, target, mode);
6201 if (target)
6202 return target;
6203 break;
6205 case BUILT_IN_BCMP:
6206 case BUILT_IN_MEMCMP:
6207 target = expand_builtin_memcmp (exp, target, mode);
6208 if (target)
6209 return target;
6210 break;
6212 case BUILT_IN_SETJMP:
6213 /* This should have been lowered to the builtins below. */
6214 gcc_unreachable ();
6216 case BUILT_IN_SETJMP_SETUP:
6217 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6218 and the receiver label. */
6219 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6221 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6222 VOIDmode, EXPAND_NORMAL);
6223 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6224 rtx label_r = label_rtx (label);
6226 /* This is copied from the handling of non-local gotos. */
6227 expand_builtin_setjmp_setup (buf_addr, label_r);
6228 nonlocal_goto_handler_labels
6229 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6230 nonlocal_goto_handler_labels);
6231 /* ??? Do not let expand_label treat us as such since we would
6232 not want to be both on the list of non-local labels and on
6233 the list of forced labels. */
6234 FORCED_LABEL (label) = 0;
6235 return const0_rtx;
6237 break;
6239 case BUILT_IN_SETJMP_DISPATCHER:
6240 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6241 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6243 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6244 rtx label_r = label_rtx (label);
6246 /* Remove the dispatcher label from the list of non-local labels
6247 since the receiver labels have been added to it above. */
6248 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6249 return const0_rtx;
6251 break;
6253 case BUILT_IN_SETJMP_RECEIVER:
6254 /* __builtin_setjmp_receiver is passed the receiver label. */
6255 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6257 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6258 rtx label_r = label_rtx (label);
6260 expand_builtin_setjmp_receiver (label_r);
6261 return const0_rtx;
6263 break;
6265 /* __builtin_longjmp is passed a pointer to an array of five words.
6266 It's similar to the C library longjmp function but works with
6267 __builtin_setjmp above. */
6268 case BUILT_IN_LONGJMP:
6269 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6271 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6272 VOIDmode, EXPAND_NORMAL);
6273 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6275 if (value != const1_rtx)
6277 error ("%<__builtin_longjmp%> second argument must be 1");
6278 return const0_rtx;
6281 expand_builtin_longjmp (buf_addr, value);
6282 return const0_rtx;
6284 break;
6286 case BUILT_IN_NONLOCAL_GOTO:
6287 target = expand_builtin_nonlocal_goto (exp);
6288 if (target)
6289 return target;
6290 break;
6292 /* This updates the setjmp buffer that is its argument with the value
6293 of the current stack pointer. */
6294 case BUILT_IN_UPDATE_SETJMP_BUF:
6295 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6297 rtx buf_addr
6298 = expand_normal (CALL_EXPR_ARG (exp, 0));
6300 expand_builtin_update_setjmp_buf (buf_addr);
6301 return const0_rtx;
6303 break;
6305 case BUILT_IN_TRAP:
6306 expand_builtin_trap ();
6307 return const0_rtx;
6309 case BUILT_IN_UNREACHABLE:
6310 expand_builtin_unreachable ();
6311 return const0_rtx;
6313 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6314 case BUILT_IN_SIGNBITD32:
6315 case BUILT_IN_SIGNBITD64:
6316 case BUILT_IN_SIGNBITD128:
6317 target = expand_builtin_signbit (exp, target);
6318 if (target)
6319 return target;
6320 break;
6322 /* Various hooks for the DWARF 2 __throw routine. */
6323 case BUILT_IN_UNWIND_INIT:
6324 expand_builtin_unwind_init ();
6325 return const0_rtx;
6326 case BUILT_IN_DWARF_CFA:
6327 return virtual_cfa_rtx;
6328 #ifdef DWARF2_UNWIND_INFO
6329 case BUILT_IN_DWARF_SP_COLUMN:
6330 return expand_builtin_dwarf_sp_column ();
6331 case BUILT_IN_INIT_DWARF_REG_SIZES:
6332 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6333 return const0_rtx;
6334 #endif
6335 case BUILT_IN_FROB_RETURN_ADDR:
6336 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6337 case BUILT_IN_EXTRACT_RETURN_ADDR:
6338 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6339 case BUILT_IN_EH_RETURN:
6340 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6341 CALL_EXPR_ARG (exp, 1));
6342 return const0_rtx;
6343 #ifdef EH_RETURN_DATA_REGNO
6344 case BUILT_IN_EH_RETURN_DATA_REGNO:
6345 return expand_builtin_eh_return_data_regno (exp);
6346 #endif
6347 case BUILT_IN_EXTEND_POINTER:
6348 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6349 case BUILT_IN_EH_POINTER:
6350 return expand_builtin_eh_pointer (exp);
6351 case BUILT_IN_EH_FILTER:
6352 return expand_builtin_eh_filter (exp);
6353 case BUILT_IN_EH_COPY_VALUES:
6354 return expand_builtin_eh_copy_values (exp);
6356 case BUILT_IN_VA_START:
6357 return expand_builtin_va_start (exp);
6358 case BUILT_IN_VA_END:
6359 return expand_builtin_va_end (exp);
6360 case BUILT_IN_VA_COPY:
6361 return expand_builtin_va_copy (exp);
6362 case BUILT_IN_EXPECT:
6363 return expand_builtin_expect (exp, target);
6364 case BUILT_IN_ASSUME_ALIGNED:
6365 return expand_builtin_assume_aligned (exp, target);
6366 case BUILT_IN_PREFETCH:
6367 expand_builtin_prefetch (exp);
6368 return const0_rtx;
6370 case BUILT_IN_INIT_TRAMPOLINE:
6371 return expand_builtin_init_trampoline (exp, true);
6372 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6373 return expand_builtin_init_trampoline (exp, false);
6374 case BUILT_IN_ADJUST_TRAMPOLINE:
6375 return expand_builtin_adjust_trampoline (exp);
6377 case BUILT_IN_FORK:
6378 case BUILT_IN_EXECL:
6379 case BUILT_IN_EXECV:
6380 case BUILT_IN_EXECLP:
6381 case BUILT_IN_EXECLE:
6382 case BUILT_IN_EXECVP:
6383 case BUILT_IN_EXECVE:
6384 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6385 if (target)
6386 return target;
6387 break;
6389 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6390 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6391 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6392 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6393 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6394 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6395 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6396 if (target)
6397 return target;
6398 break;
6400 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6401 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6402 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6403 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6404 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6405 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6406 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6407 if (target)
6408 return target;
6409 break;
6411 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6412 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6413 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6414 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6415 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6416 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6417 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6418 if (target)
6419 return target;
6420 break;
6422 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6423 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6424 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6425 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6426 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6427 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6428 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6429 if (target)
6430 return target;
6431 break;
6433 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6434 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6435 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6436 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6437 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6438 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6439 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6440 if (target)
6441 return target;
6442 break;
6444 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6445 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6446 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6447 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6448 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6449 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6450 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6451 if (target)
6452 return target;
6453 break;
6455 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6456 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6457 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6458 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6459 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6460 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6461 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6462 if (target)
6463 return target;
6464 break;
6466 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6467 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6468 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6469 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6470 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6471 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6472 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6473 if (target)
6474 return target;
6475 break;
6477 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6478 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6479 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6480 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6481 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6482 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6483 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6484 if (target)
6485 return target;
6486 break;
6488 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6489 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6490 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6491 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6492 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6493 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6494 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6495 if (target)
6496 return target;
6497 break;
6499 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6500 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6501 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6502 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6503 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6504 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6505 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6506 if (target)
6507 return target;
6508 break;
6510 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6511 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6512 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6513 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6514 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6515 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6516 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6517 if (target)
6518 return target;
6519 break;
6521 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6522 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6523 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6524 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6525 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6526 if (mode == VOIDmode)
6527 mode = TYPE_MODE (boolean_type_node);
6528 if (!target || !register_operand (target, mode))
6529 target = gen_reg_rtx (mode);
6531 mode = get_builtin_sync_mode
6532 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6533 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6534 if (target)
6535 return target;
6536 break;
6538 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6539 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6540 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6541 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6542 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6543 mode = get_builtin_sync_mode
6544 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6545 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6546 if (target)
6547 return target;
6548 break;
6550 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6551 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6552 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6553 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6554 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6555 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6556 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6557 if (target)
6558 return target;
6559 break;
6561 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6562 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6563 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6564 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6565 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6566 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6567 expand_builtin_sync_lock_release (mode, exp);
6568 return const0_rtx;
6570 case BUILT_IN_SYNC_SYNCHRONIZE:
6571 expand_builtin_sync_synchronize ();
6572 return const0_rtx;
6574 case BUILT_IN_ATOMIC_EXCHANGE_1:
6575 case BUILT_IN_ATOMIC_EXCHANGE_2:
6576 case BUILT_IN_ATOMIC_EXCHANGE_4:
6577 case BUILT_IN_ATOMIC_EXCHANGE_8:
6578 case BUILT_IN_ATOMIC_EXCHANGE_16:
6579 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6580 target = expand_builtin_atomic_exchange (mode, exp, target);
6581 if (target)
6582 return target;
6583 break;
6585 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6586 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6587 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6588 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6589 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6591 unsigned int nargs, z;
6592 vec<tree, va_gc> *vec;
6594 mode =
6595 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6596 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6597 if (target)
6598 return target;
6600 /* If this is turned into an external library call, the weak parameter
6601 must be dropped to match the expected parameter list. */
6602 nargs = call_expr_nargs (exp);
6603 vec_alloc (vec, nargs - 1);
6604 for (z = 0; z < 3; z++)
6605 vec->quick_push (CALL_EXPR_ARG (exp, z));
6606 /* Skip the boolean weak parameter. */
6607 for (z = 4; z < 6; z++)
6608 vec->quick_push (CALL_EXPR_ARG (exp, z));
6609 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6610 break;
6613 case BUILT_IN_ATOMIC_LOAD_1:
6614 case BUILT_IN_ATOMIC_LOAD_2:
6615 case BUILT_IN_ATOMIC_LOAD_4:
6616 case BUILT_IN_ATOMIC_LOAD_8:
6617 case BUILT_IN_ATOMIC_LOAD_16:
6618 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6619 target = expand_builtin_atomic_load (mode, exp, target);
6620 if (target)
6621 return target;
6622 break;
6624 case BUILT_IN_ATOMIC_STORE_1:
6625 case BUILT_IN_ATOMIC_STORE_2:
6626 case BUILT_IN_ATOMIC_STORE_4:
6627 case BUILT_IN_ATOMIC_STORE_8:
6628 case BUILT_IN_ATOMIC_STORE_16:
6629 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6630 target = expand_builtin_atomic_store (mode, exp);
6631 if (target)
6632 return const0_rtx;
6633 break;
6635 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6636 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6637 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6638 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6639 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6641 enum built_in_function lib;
6642 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6643 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6644 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6645 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6646 ignore, lib);
6647 if (target)
6648 return target;
6649 break;
6651 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6652 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6653 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6654 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6655 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6657 enum built_in_function lib;
6658 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6659 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6660 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6661 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6662 ignore, lib);
6663 if (target)
6664 return target;
6665 break;
6667 case BUILT_IN_ATOMIC_AND_FETCH_1:
6668 case BUILT_IN_ATOMIC_AND_FETCH_2:
6669 case BUILT_IN_ATOMIC_AND_FETCH_4:
6670 case BUILT_IN_ATOMIC_AND_FETCH_8:
6671 case BUILT_IN_ATOMIC_AND_FETCH_16:
6673 enum built_in_function lib;
6674 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6675 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6676 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6677 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6678 ignore, lib);
6679 if (target)
6680 return target;
6681 break;
6683 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6684 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6685 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6686 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6687 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6689 enum built_in_function lib;
6690 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6691 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6692 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6693 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6694 ignore, lib);
6695 if (target)
6696 return target;
6697 break;
6699 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6700 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6701 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6702 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6703 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6705 enum built_in_function lib;
6706 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6707 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6708 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6709 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6710 ignore, lib);
6711 if (target)
6712 return target;
6713 break;
6715 case BUILT_IN_ATOMIC_OR_FETCH_1:
6716 case BUILT_IN_ATOMIC_OR_FETCH_2:
6717 case BUILT_IN_ATOMIC_OR_FETCH_4:
6718 case BUILT_IN_ATOMIC_OR_FETCH_8:
6719 case BUILT_IN_ATOMIC_OR_FETCH_16:
6721 enum built_in_function lib;
6722 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6723 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6724 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6725 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6726 ignore, lib);
6727 if (target)
6728 return target;
6729 break;
6731 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6732 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6733 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6734 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6735 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6736 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6737 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6738 ignore, BUILT_IN_NONE);
6739 if (target)
6740 return target;
6741 break;
6743 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6744 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6745 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6746 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6747 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6748 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6749 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6750 ignore, BUILT_IN_NONE);
6751 if (target)
6752 return target;
6753 break;
6755 case BUILT_IN_ATOMIC_FETCH_AND_1:
6756 case BUILT_IN_ATOMIC_FETCH_AND_2:
6757 case BUILT_IN_ATOMIC_FETCH_AND_4:
6758 case BUILT_IN_ATOMIC_FETCH_AND_8:
6759 case BUILT_IN_ATOMIC_FETCH_AND_16:
6760 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6761 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6762 ignore, BUILT_IN_NONE);
6763 if (target)
6764 return target;
6765 break;
6767 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6768 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6769 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6770 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6771 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6772 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6773 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6774 ignore, BUILT_IN_NONE);
6775 if (target)
6776 return target;
6777 break;
6779 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6780 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6781 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6782 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6783 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6784 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6785 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6786 ignore, BUILT_IN_NONE);
6787 if (target)
6788 return target;
6789 break;
6791 case BUILT_IN_ATOMIC_FETCH_OR_1:
6792 case BUILT_IN_ATOMIC_FETCH_OR_2:
6793 case BUILT_IN_ATOMIC_FETCH_OR_4:
6794 case BUILT_IN_ATOMIC_FETCH_OR_8:
6795 case BUILT_IN_ATOMIC_FETCH_OR_16:
6796 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6797 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6798 ignore, BUILT_IN_NONE);
6799 if (target)
6800 return target;
6801 break;
6803 case BUILT_IN_ATOMIC_TEST_AND_SET:
6804 return expand_builtin_atomic_test_and_set (exp, target);
6806 case BUILT_IN_ATOMIC_CLEAR:
6807 return expand_builtin_atomic_clear (exp);
6809 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6810 return expand_builtin_atomic_always_lock_free (exp);
6812 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6813 target = expand_builtin_atomic_is_lock_free (exp);
6814 if (target)
6815 return target;
6816 break;
6818 case BUILT_IN_ATOMIC_THREAD_FENCE:
6819 expand_builtin_atomic_thread_fence (exp);
6820 return const0_rtx;
6822 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6823 expand_builtin_atomic_signal_fence (exp);
6824 return const0_rtx;
6826 case BUILT_IN_OBJECT_SIZE:
6827 return expand_builtin_object_size (exp);
6829 case BUILT_IN_MEMCPY_CHK:
6830 case BUILT_IN_MEMPCPY_CHK:
6831 case BUILT_IN_MEMMOVE_CHK:
6832 case BUILT_IN_MEMSET_CHK:
6833 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6834 if (target)
6835 return target;
6836 break;
6838 case BUILT_IN_STRCPY_CHK:
6839 case BUILT_IN_STPCPY_CHK:
6840 case BUILT_IN_STRNCPY_CHK:
6841 case BUILT_IN_STPNCPY_CHK:
6842 case BUILT_IN_STRCAT_CHK:
6843 case BUILT_IN_STRNCAT_CHK:
6844 case BUILT_IN_SNPRINTF_CHK:
6845 case BUILT_IN_VSNPRINTF_CHK:
6846 maybe_emit_chk_warning (exp, fcode);
6847 break;
6849 case BUILT_IN_SPRINTF_CHK:
6850 case BUILT_IN_VSPRINTF_CHK:
6851 maybe_emit_sprintf_chk_warning (exp, fcode);
6852 break;
6854 case BUILT_IN_FREE:
6855 if (warn_free_nonheap_object)
6856 maybe_emit_free_warning (exp);
6857 break;
6859 case BUILT_IN_THREAD_POINTER:
6860 return expand_builtin_thread_pointer (exp, target);
6862 case BUILT_IN_SET_THREAD_POINTER:
6863 expand_builtin_set_thread_pointer (exp);
6864 return const0_rtx;
6866 default: /* just do library call, if unknown builtin */
6867 break;
6870 /* The switch statement above can drop through to cause the function
6871 to be called normally. */
6872 return expand_call (exp, target, ignore);
6875 /* Determine whether a tree node represents a call to a built-in
6876 function. If the tree T is a call to a built-in function with
6877 the right number of arguments of the appropriate types, return
6878 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6879 Otherwise the return value is END_BUILTINS. */
6881 enum built_in_function
6882 builtin_mathfn_code (const_tree t)
6884 const_tree fndecl, arg, parmlist;
6885 const_tree argtype, parmtype;
6886 const_call_expr_arg_iterator iter;
6888 if (TREE_CODE (t) != CALL_EXPR
6889 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6890 return END_BUILTINS;
6892 fndecl = get_callee_fndecl (t);
6893 if (fndecl == NULL_TREE
6894 || TREE_CODE (fndecl) != FUNCTION_DECL
6895 || ! DECL_BUILT_IN (fndecl)
6896 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6897 return END_BUILTINS;
6899 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6900 init_const_call_expr_arg_iterator (t, &iter);
6901 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6903 /* If a function doesn't take a variable number of arguments,
6904 the last element in the list will have type `void'. */
6905 parmtype = TREE_VALUE (parmlist);
6906 if (VOID_TYPE_P (parmtype))
6908 if (more_const_call_expr_args_p (&iter))
6909 return END_BUILTINS;
6910 return DECL_FUNCTION_CODE (fndecl);
6913 if (! more_const_call_expr_args_p (&iter))
6914 return END_BUILTINS;
6916 arg = next_const_call_expr_arg (&iter);
6917 argtype = TREE_TYPE (arg);
6919 if (SCALAR_FLOAT_TYPE_P (parmtype))
6921 if (! SCALAR_FLOAT_TYPE_P (argtype))
6922 return END_BUILTINS;
6924 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6926 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6927 return END_BUILTINS;
6929 else if (POINTER_TYPE_P (parmtype))
6931 if (! POINTER_TYPE_P (argtype))
6932 return END_BUILTINS;
6934 else if (INTEGRAL_TYPE_P (parmtype))
6936 if (! INTEGRAL_TYPE_P (argtype))
6937 return END_BUILTINS;
6939 else
6940 return END_BUILTINS;
6943 /* Variable-length argument list. */
6944 return DECL_FUNCTION_CODE (fndecl);
6947 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6948 evaluate to a constant. */
6950 static tree
6951 fold_builtin_constant_p (tree arg)
6953 /* We return 1 for a numeric type that's known to be a constant
6954 value at compile-time or for an aggregate type that's a
6955 literal constant. */
6956 STRIP_NOPS (arg);
6958 /* If we know this is a constant, emit the constant of one. */
6959 if (CONSTANT_CLASS_P (arg)
6960 || (TREE_CODE (arg) == CONSTRUCTOR
6961 && TREE_CONSTANT (arg)))
6962 return integer_one_node;
6963 if (TREE_CODE (arg) == ADDR_EXPR)
6965 tree op = TREE_OPERAND (arg, 0);
6966 if (TREE_CODE (op) == STRING_CST
6967 || (TREE_CODE (op) == ARRAY_REF
6968 && integer_zerop (TREE_OPERAND (op, 1))
6969 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6970 return integer_one_node;
6973 /* If this expression has side effects, show we don't know it to be a
6974 constant. Likewise if it's a pointer or aggregate type since in
6975 those case we only want literals, since those are only optimized
6976 when generating RTL, not later.
6977 And finally, if we are compiling an initializer, not code, we
6978 need to return a definite result now; there's not going to be any
6979 more optimization done. */
6980 if (TREE_SIDE_EFFECTS (arg)
6981 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6982 || POINTER_TYPE_P (TREE_TYPE (arg))
6983 || cfun == 0
6984 || folding_initializer)
6985 return integer_zero_node;
6987 return NULL_TREE;
6990 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6991 return it as a truthvalue. */
6993 static tree
6994 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6996 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6998 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6999 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7000 ret_type = TREE_TYPE (TREE_TYPE (fn));
7001 pred_type = TREE_VALUE (arg_types);
7002 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7004 pred = fold_convert_loc (loc, pred_type, pred);
7005 expected = fold_convert_loc (loc, expected_type, expected);
7006 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
7008 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7009 build_int_cst (ret_type, 0));
7012 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7013 NULL_TREE if no simplification is possible. */
7015 static tree
7016 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7018 tree inner, fndecl, inner_arg0;
7019 enum tree_code code;
7021 /* Distribute the expected value over short-circuiting operators.
7022 See through the cast from truthvalue_type_node to long. */
7023 inner_arg0 = arg0;
7024 while (TREE_CODE (inner_arg0) == NOP_EXPR
7025 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7026 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7027 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7029 /* If this is a builtin_expect within a builtin_expect keep the
7030 inner one. See through a comparison against a constant. It
7031 might have been added to create a thruthvalue. */
7032 inner = inner_arg0;
7034 if (COMPARISON_CLASS_P (inner)
7035 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7036 inner = TREE_OPERAND (inner, 0);
7038 if (TREE_CODE (inner) == CALL_EXPR
7039 && (fndecl = get_callee_fndecl (inner))
7040 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7041 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7042 return arg0;
7044 inner = inner_arg0;
7045 code = TREE_CODE (inner);
7046 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7048 tree op0 = TREE_OPERAND (inner, 0);
7049 tree op1 = TREE_OPERAND (inner, 1);
7051 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7052 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7053 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7055 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7058 /* If the argument isn't invariant then there's nothing else we can do. */
7059 if (!TREE_CONSTANT (inner_arg0))
7060 return NULL_TREE;
7062 /* If we expect that a comparison against the argument will fold to
7063 a constant return the constant. In practice, this means a true
7064 constant or the address of a non-weak symbol. */
7065 inner = inner_arg0;
7066 STRIP_NOPS (inner);
7067 if (TREE_CODE (inner) == ADDR_EXPR)
7071 inner = TREE_OPERAND (inner, 0);
7073 while (TREE_CODE (inner) == COMPONENT_REF
7074 || TREE_CODE (inner) == ARRAY_REF);
7075 if ((TREE_CODE (inner) == VAR_DECL
7076 || TREE_CODE (inner) == FUNCTION_DECL)
7077 && DECL_WEAK (inner))
7078 return NULL_TREE;
7081 /* Otherwise, ARG0 already has the proper type for the return value. */
7082 return arg0;
7085 /* Fold a call to __builtin_classify_type with argument ARG. */
7087 static tree
7088 fold_builtin_classify_type (tree arg)
7090 if (arg == 0)
7091 return build_int_cst (integer_type_node, no_type_class);
7093 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7096 /* Fold a call to __builtin_strlen with argument ARG. */
7098 static tree
7099 fold_builtin_strlen (location_t loc, tree type, tree arg)
7101 if (!validate_arg (arg, POINTER_TYPE))
7102 return NULL_TREE;
7103 else
7105 tree len = c_strlen (arg, 0);
7107 if (len)
7108 return fold_convert_loc (loc, type, len);
7110 return NULL_TREE;
7114 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7116 static tree
7117 fold_builtin_inf (location_t loc, tree type, int warn)
7119 REAL_VALUE_TYPE real;
7121 /* __builtin_inff is intended to be usable to define INFINITY on all
7122 targets. If an infinity is not available, INFINITY expands "to a
7123 positive constant of type float that overflows at translation
7124 time", footnote "In this case, using INFINITY will violate the
7125 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7126 Thus we pedwarn to ensure this constraint violation is
7127 diagnosed. */
7128 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7129 pedwarn (loc, 0, "target format does not support infinity");
7131 real_inf (&real);
7132 return build_real (type, real);
7135 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7137 static tree
7138 fold_builtin_nan (tree arg, tree type, int quiet)
7140 REAL_VALUE_TYPE real;
7141 const char *str;
7143 if (!validate_arg (arg, POINTER_TYPE))
7144 return NULL_TREE;
7145 str = c_getstr (arg);
7146 if (!str)
7147 return NULL_TREE;
7149 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7150 return NULL_TREE;
7152 return build_real (type, real);
7155 /* Return true if the floating point expression T has an integer value.
7156 We also allow +Inf, -Inf and NaN to be considered integer values. */
7158 static bool
7159 integer_valued_real_p (tree t)
7161 switch (TREE_CODE (t))
7163 case FLOAT_EXPR:
7164 return true;
7166 case ABS_EXPR:
7167 case SAVE_EXPR:
7168 return integer_valued_real_p (TREE_OPERAND (t, 0));
7170 case COMPOUND_EXPR:
7171 case MODIFY_EXPR:
7172 case BIND_EXPR:
7173 return integer_valued_real_p (TREE_OPERAND (t, 1));
7175 case PLUS_EXPR:
7176 case MINUS_EXPR:
7177 case MULT_EXPR:
7178 case MIN_EXPR:
7179 case MAX_EXPR:
7180 return integer_valued_real_p (TREE_OPERAND (t, 0))
7181 && integer_valued_real_p (TREE_OPERAND (t, 1));
7183 case COND_EXPR:
7184 return integer_valued_real_p (TREE_OPERAND (t, 1))
7185 && integer_valued_real_p (TREE_OPERAND (t, 2));
7187 case REAL_CST:
7188 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7190 case NOP_EXPR:
7192 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7193 if (TREE_CODE (type) == INTEGER_TYPE)
7194 return true;
7195 if (TREE_CODE (type) == REAL_TYPE)
7196 return integer_valued_real_p (TREE_OPERAND (t, 0));
7197 break;
7200 case CALL_EXPR:
7201 switch (builtin_mathfn_code (t))
7203 CASE_FLT_FN (BUILT_IN_CEIL):
7204 CASE_FLT_FN (BUILT_IN_FLOOR):
7205 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7206 CASE_FLT_FN (BUILT_IN_RINT):
7207 CASE_FLT_FN (BUILT_IN_ROUND):
7208 CASE_FLT_FN (BUILT_IN_TRUNC):
7209 return true;
7211 CASE_FLT_FN (BUILT_IN_FMIN):
7212 CASE_FLT_FN (BUILT_IN_FMAX):
7213 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7214 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7216 default:
7217 break;
7219 break;
7221 default:
7222 break;
7224 return false;
7227 /* FNDECL is assumed to be a builtin where truncation can be propagated
7228 across (for instance floor((double)f) == (double)floorf (f).
7229 Do the transformation for a call with argument ARG. */
7231 static tree
7232 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7234 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7236 if (!validate_arg (arg, REAL_TYPE))
7237 return NULL_TREE;
7239 /* Integer rounding functions are idempotent. */
7240 if (fcode == builtin_mathfn_code (arg))
7241 return arg;
7243 /* If argument is already integer valued, and we don't need to worry
7244 about setting errno, there's no need to perform rounding. */
7245 if (! flag_errno_math && integer_valued_real_p (arg))
7246 return arg;
7248 if (optimize)
7250 tree arg0 = strip_float_extensions (arg);
7251 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7252 tree newtype = TREE_TYPE (arg0);
7253 tree decl;
7255 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7256 && (decl = mathfn_built_in (newtype, fcode)))
7257 return fold_convert_loc (loc, ftype,
7258 build_call_expr_loc (loc, decl, 1,
7259 fold_convert_loc (loc,
7260 newtype,
7261 arg0)));
7263 return NULL_TREE;
7266 /* FNDECL is assumed to be builtin which can narrow the FP type of
7267 the argument, for instance lround((double)f) -> lroundf (f).
7268 Do the transformation for a call with argument ARG. */
7270 static tree
7271 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7273 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7275 if (!validate_arg (arg, REAL_TYPE))
7276 return NULL_TREE;
7278 /* If argument is already integer valued, and we don't need to worry
7279 about setting errno, there's no need to perform rounding. */
7280 if (! flag_errno_math && integer_valued_real_p (arg))
7281 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7282 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7284 if (optimize)
7286 tree ftype = TREE_TYPE (arg);
7287 tree arg0 = strip_float_extensions (arg);
7288 tree newtype = TREE_TYPE (arg0);
7289 tree decl;
7291 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7292 && (decl = mathfn_built_in (newtype, fcode)))
7293 return build_call_expr_loc (loc, decl, 1,
7294 fold_convert_loc (loc, newtype, arg0));
7297 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7298 sizeof (int) == sizeof (long). */
7299 if (TYPE_PRECISION (integer_type_node)
7300 == TYPE_PRECISION (long_integer_type_node))
7302 tree newfn = NULL_TREE;
7303 switch (fcode)
7305 CASE_FLT_FN (BUILT_IN_ICEIL):
7306 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7307 break;
7309 CASE_FLT_FN (BUILT_IN_IFLOOR):
7310 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7311 break;
7313 CASE_FLT_FN (BUILT_IN_IROUND):
7314 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7315 break;
7317 CASE_FLT_FN (BUILT_IN_IRINT):
7318 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7319 break;
7321 default:
7322 break;
7325 if (newfn)
7327 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7328 return fold_convert_loc (loc,
7329 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7333 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7334 sizeof (long long) == sizeof (long). */
7335 if (TYPE_PRECISION (long_long_integer_type_node)
7336 == TYPE_PRECISION (long_integer_type_node))
7338 tree newfn = NULL_TREE;
7339 switch (fcode)
7341 CASE_FLT_FN (BUILT_IN_LLCEIL):
7342 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7343 break;
7345 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7346 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7347 break;
7349 CASE_FLT_FN (BUILT_IN_LLROUND):
7350 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7351 break;
7353 CASE_FLT_FN (BUILT_IN_LLRINT):
7354 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7355 break;
7357 default:
7358 break;
7361 if (newfn)
7363 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7364 return fold_convert_loc (loc,
7365 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7369 return NULL_TREE;
7372 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7373 return type. Return NULL_TREE if no simplification can be made. */
7375 static tree
7376 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7378 tree res;
7380 if (!validate_arg (arg, COMPLEX_TYPE)
7381 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7382 return NULL_TREE;
7384 /* Calculate the result when the argument is a constant. */
7385 if (TREE_CODE (arg) == COMPLEX_CST
7386 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7387 type, mpfr_hypot)))
7388 return res;
7390 if (TREE_CODE (arg) == COMPLEX_EXPR)
7392 tree real = TREE_OPERAND (arg, 0);
7393 tree imag = TREE_OPERAND (arg, 1);
7395 /* If either part is zero, cabs is fabs of the other. */
7396 if (real_zerop (real))
7397 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7398 if (real_zerop (imag))
7399 return fold_build1_loc (loc, ABS_EXPR, type, real);
7401 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7402 if (flag_unsafe_math_optimizations
7403 && operand_equal_p (real, imag, OEP_PURE_SAME))
7405 const REAL_VALUE_TYPE sqrt2_trunc
7406 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7407 STRIP_NOPS (real);
7408 return fold_build2_loc (loc, MULT_EXPR, type,
7409 fold_build1_loc (loc, ABS_EXPR, type, real),
7410 build_real (type, sqrt2_trunc));
7414 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7415 if (TREE_CODE (arg) == NEGATE_EXPR
7416 || TREE_CODE (arg) == CONJ_EXPR)
7417 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7419 /* Don't do this when optimizing for size. */
7420 if (flag_unsafe_math_optimizations
7421 && optimize && optimize_function_for_speed_p (cfun))
7423 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7425 if (sqrtfn != NULL_TREE)
7427 tree rpart, ipart, result;
7429 arg = builtin_save_expr (arg);
7431 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7432 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7434 rpart = builtin_save_expr (rpart);
7435 ipart = builtin_save_expr (ipart);
7437 result = fold_build2_loc (loc, PLUS_EXPR, type,
7438 fold_build2_loc (loc, MULT_EXPR, type,
7439 rpart, rpart),
7440 fold_build2_loc (loc, MULT_EXPR, type,
7441 ipart, ipart));
7443 return build_call_expr_loc (loc, sqrtfn, 1, result);
7447 return NULL_TREE;
7450 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7451 complex tree type of the result. If NEG is true, the imaginary
7452 zero is negative. */
7454 static tree
7455 build_complex_cproj (tree type, bool neg)
7457 REAL_VALUE_TYPE rinf, rzero = dconst0;
7459 real_inf (&rinf);
7460 rzero.sign = neg;
7461 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7462 build_real (TREE_TYPE (type), rzero));
7465 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7466 return type. Return NULL_TREE if no simplification can be made. */
7468 static tree
7469 fold_builtin_cproj (location_t loc, tree arg, tree type)
7471 if (!validate_arg (arg, COMPLEX_TYPE)
7472 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7473 return NULL_TREE;
7475 /* If there are no infinities, return arg. */
7476 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7477 return non_lvalue_loc (loc, arg);
7479 /* Calculate the result when the argument is a constant. */
7480 if (TREE_CODE (arg) == COMPLEX_CST)
7482 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7483 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7485 if (real_isinf (real) || real_isinf (imag))
7486 return build_complex_cproj (type, imag->sign);
7487 else
7488 return arg;
7490 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7492 tree real = TREE_OPERAND (arg, 0);
7493 tree imag = TREE_OPERAND (arg, 1);
7495 STRIP_NOPS (real);
7496 STRIP_NOPS (imag);
7498 /* If the real part is inf and the imag part is known to be
7499 nonnegative, return (inf + 0i). Remember side-effects are
7500 possible in the imag part. */
7501 if (TREE_CODE (real) == REAL_CST
7502 && real_isinf (TREE_REAL_CST_PTR (real))
7503 && tree_expr_nonnegative_p (imag))
7504 return omit_one_operand_loc (loc, type,
7505 build_complex_cproj (type, false),
7506 arg);
7508 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7509 Remember side-effects are possible in the real part. */
7510 if (TREE_CODE (imag) == REAL_CST
7511 && real_isinf (TREE_REAL_CST_PTR (imag)))
7512 return
7513 omit_one_operand_loc (loc, type,
7514 build_complex_cproj (type, TREE_REAL_CST_PTR
7515 (imag)->sign), arg);
7518 return NULL_TREE;
7521 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7522 Return NULL_TREE if no simplification can be made. */
7524 static tree
7525 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7528 enum built_in_function fcode;
7529 tree res;
7531 if (!validate_arg (arg, REAL_TYPE))
7532 return NULL_TREE;
7534 /* Calculate the result when the argument is a constant. */
7535 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7536 return res;
7538 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7539 fcode = builtin_mathfn_code (arg);
7540 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7542 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7543 arg = fold_build2_loc (loc, MULT_EXPR, type,
7544 CALL_EXPR_ARG (arg, 0),
7545 build_real (type, dconsthalf));
7546 return build_call_expr_loc (loc, expfn, 1, arg);
7549 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7550 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7552 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7554 if (powfn)
7556 tree arg0 = CALL_EXPR_ARG (arg, 0);
7557 tree tree_root;
7558 /* The inner root was either sqrt or cbrt. */
7559 /* This was a conditional expression but it triggered a bug
7560 in Sun C 5.5. */
7561 REAL_VALUE_TYPE dconstroot;
7562 if (BUILTIN_SQRT_P (fcode))
7563 dconstroot = dconsthalf;
7564 else
7565 dconstroot = dconst_third ();
7567 /* Adjust for the outer root. */
7568 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7569 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7570 tree_root = build_real (type, dconstroot);
7571 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7575 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7576 if (flag_unsafe_math_optimizations
7577 && (fcode == BUILT_IN_POW
7578 || fcode == BUILT_IN_POWF
7579 || fcode == BUILT_IN_POWL))
7581 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7582 tree arg0 = CALL_EXPR_ARG (arg, 0);
7583 tree arg1 = CALL_EXPR_ARG (arg, 1);
7584 tree narg1;
7585 if (!tree_expr_nonnegative_p (arg0))
7586 arg0 = build1 (ABS_EXPR, type, arg0);
7587 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7588 build_real (type, dconsthalf));
7589 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7592 return NULL_TREE;
7595 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7596 Return NULL_TREE if no simplification can be made. */
7598 static tree
7599 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7601 const enum built_in_function fcode = builtin_mathfn_code (arg);
7602 tree res;
7604 if (!validate_arg (arg, REAL_TYPE))
7605 return NULL_TREE;
7607 /* Calculate the result when the argument is a constant. */
7608 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7609 return res;
7611 if (flag_unsafe_math_optimizations)
7613 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7614 if (BUILTIN_EXPONENT_P (fcode))
7616 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7617 const REAL_VALUE_TYPE third_trunc =
7618 real_value_truncate (TYPE_MODE (type), dconst_third ());
7619 arg = fold_build2_loc (loc, MULT_EXPR, type,
7620 CALL_EXPR_ARG (arg, 0),
7621 build_real (type, third_trunc));
7622 return build_call_expr_loc (loc, expfn, 1, arg);
7625 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7626 if (BUILTIN_SQRT_P (fcode))
7628 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7630 if (powfn)
7632 tree arg0 = CALL_EXPR_ARG (arg, 0);
7633 tree tree_root;
7634 REAL_VALUE_TYPE dconstroot = dconst_third ();
7636 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7637 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7638 tree_root = build_real (type, dconstroot);
7639 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7643 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7644 if (BUILTIN_CBRT_P (fcode))
7646 tree arg0 = CALL_EXPR_ARG (arg, 0);
7647 if (tree_expr_nonnegative_p (arg0))
7649 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7651 if (powfn)
7653 tree tree_root;
7654 REAL_VALUE_TYPE dconstroot;
7656 real_arithmetic (&dconstroot, MULT_EXPR,
7657 dconst_third_ptr (), dconst_third_ptr ());
7658 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7659 tree_root = build_real (type, dconstroot);
7660 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7665 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7666 if (fcode == BUILT_IN_POW
7667 || fcode == BUILT_IN_POWF
7668 || fcode == BUILT_IN_POWL)
7670 tree arg00 = CALL_EXPR_ARG (arg, 0);
7671 tree arg01 = CALL_EXPR_ARG (arg, 1);
7672 if (tree_expr_nonnegative_p (arg00))
7674 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7675 const REAL_VALUE_TYPE dconstroot
7676 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7677 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7678 build_real (type, dconstroot));
7679 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7683 return NULL_TREE;
7686 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7687 TYPE is the type of the return value. Return NULL_TREE if no
7688 simplification can be made. */
7690 static tree
7691 fold_builtin_cos (location_t loc,
7692 tree arg, tree type, tree fndecl)
7694 tree res, narg;
7696 if (!validate_arg (arg, REAL_TYPE))
7697 return NULL_TREE;
7699 /* Calculate the result when the argument is a constant. */
7700 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7701 return res;
7703 /* Optimize cos(-x) into cos (x). */
7704 if ((narg = fold_strip_sign_ops (arg)))
7705 return build_call_expr_loc (loc, fndecl, 1, narg);
7707 return NULL_TREE;
7710 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7711 Return NULL_TREE if no simplification can be made. */
7713 static tree
7714 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7716 if (validate_arg (arg, REAL_TYPE))
7718 tree res, narg;
7720 /* Calculate the result when the argument is a constant. */
7721 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7722 return res;
7724 /* Optimize cosh(-x) into cosh (x). */
7725 if ((narg = fold_strip_sign_ops (arg)))
7726 return build_call_expr_loc (loc, fndecl, 1, narg);
7729 return NULL_TREE;
7732 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7733 argument ARG. TYPE is the type of the return value. Return
7734 NULL_TREE if no simplification can be made. */
7736 static tree
7737 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7738 bool hyper)
7740 if (validate_arg (arg, COMPLEX_TYPE)
7741 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7743 tree tmp;
7745 /* Calculate the result when the argument is a constant. */
7746 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7747 return tmp;
7749 /* Optimize fn(-x) into fn(x). */
7750 if ((tmp = fold_strip_sign_ops (arg)))
7751 return build_call_expr_loc (loc, fndecl, 1, tmp);
7754 return NULL_TREE;
7757 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7758 Return NULL_TREE if no simplification can be made. */
7760 static tree
7761 fold_builtin_tan (tree arg, tree type)
7763 enum built_in_function fcode;
7764 tree res;
7766 if (!validate_arg (arg, REAL_TYPE))
7767 return NULL_TREE;
7769 /* Calculate the result when the argument is a constant. */
7770 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7771 return res;
7773 /* Optimize tan(atan(x)) = x. */
7774 fcode = builtin_mathfn_code (arg);
7775 if (flag_unsafe_math_optimizations
7776 && (fcode == BUILT_IN_ATAN
7777 || fcode == BUILT_IN_ATANF
7778 || fcode == BUILT_IN_ATANL))
7779 return CALL_EXPR_ARG (arg, 0);
7781 return NULL_TREE;
7784 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7785 NULL_TREE if no simplification can be made. */
7787 static tree
7788 fold_builtin_sincos (location_t loc,
7789 tree arg0, tree arg1, tree arg2)
7791 tree type;
7792 tree res, fn, call;
7794 if (!validate_arg (arg0, REAL_TYPE)
7795 || !validate_arg (arg1, POINTER_TYPE)
7796 || !validate_arg (arg2, POINTER_TYPE))
7797 return NULL_TREE;
7799 type = TREE_TYPE (arg0);
7801 /* Calculate the result when the argument is a constant. */
7802 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7803 return res;
7805 /* Canonicalize sincos to cexpi. */
7806 if (!TARGET_C99_FUNCTIONS)
7807 return NULL_TREE;
7808 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7809 if (!fn)
7810 return NULL_TREE;
7812 call = build_call_expr_loc (loc, fn, 1, arg0);
7813 call = builtin_save_expr (call);
7815 return build2 (COMPOUND_EXPR, void_type_node,
7816 build2 (MODIFY_EXPR, void_type_node,
7817 build_fold_indirect_ref_loc (loc, arg1),
7818 build1 (IMAGPART_EXPR, type, call)),
7819 build2 (MODIFY_EXPR, void_type_node,
7820 build_fold_indirect_ref_loc (loc, arg2),
7821 build1 (REALPART_EXPR, type, call)));
7824 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7825 NULL_TREE if no simplification can be made. */
7827 static tree
7828 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7830 tree rtype;
7831 tree realp, imagp, ifn;
7832 tree res;
7834 if (!validate_arg (arg0, COMPLEX_TYPE)
7835 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7836 return NULL_TREE;
7838 /* Calculate the result when the argument is a constant. */
7839 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7840 return res;
7842 rtype = TREE_TYPE (TREE_TYPE (arg0));
7844 /* In case we can figure out the real part of arg0 and it is constant zero
7845 fold to cexpi. */
7846 if (!TARGET_C99_FUNCTIONS)
7847 return NULL_TREE;
7848 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7849 if (!ifn)
7850 return NULL_TREE;
7852 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7853 && real_zerop (realp))
7855 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7856 return build_call_expr_loc (loc, ifn, 1, narg);
7859 /* In case we can easily decompose real and imaginary parts split cexp
7860 to exp (r) * cexpi (i). */
7861 if (flag_unsafe_math_optimizations
7862 && realp)
7864 tree rfn, rcall, icall;
7866 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7867 if (!rfn)
7868 return NULL_TREE;
7870 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7871 if (!imagp)
7872 return NULL_TREE;
7874 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7875 icall = builtin_save_expr (icall);
7876 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7877 rcall = builtin_save_expr (rcall);
7878 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7879 fold_build2_loc (loc, MULT_EXPR, rtype,
7880 rcall,
7881 fold_build1_loc (loc, REALPART_EXPR,
7882 rtype, icall)),
7883 fold_build2_loc (loc, MULT_EXPR, rtype,
7884 rcall,
7885 fold_build1_loc (loc, IMAGPART_EXPR,
7886 rtype, icall)));
7889 return NULL_TREE;
7892 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7893 Return NULL_TREE if no simplification can be made. */
7895 static tree
7896 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7898 if (!validate_arg (arg, REAL_TYPE))
7899 return NULL_TREE;
7901 /* Optimize trunc of constant value. */
7902 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7904 REAL_VALUE_TYPE r, x;
7905 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7907 x = TREE_REAL_CST (arg);
7908 real_trunc (&r, TYPE_MODE (type), &x);
7909 return build_real (type, r);
7912 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7915 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7916 Return NULL_TREE if no simplification can be made. */
7918 static tree
7919 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7921 if (!validate_arg (arg, REAL_TYPE))
7922 return NULL_TREE;
7924 /* Optimize floor of constant value. */
7925 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7927 REAL_VALUE_TYPE x;
7929 x = TREE_REAL_CST (arg);
7930 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7932 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7933 REAL_VALUE_TYPE r;
7935 real_floor (&r, TYPE_MODE (type), &x);
7936 return build_real (type, r);
7940 /* Fold floor (x) where x is nonnegative to trunc (x). */
7941 if (tree_expr_nonnegative_p (arg))
7943 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7944 if (truncfn)
7945 return build_call_expr_loc (loc, truncfn, 1, arg);
7948 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7951 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7952 Return NULL_TREE if no simplification can be made. */
7954 static tree
7955 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7957 if (!validate_arg (arg, REAL_TYPE))
7958 return NULL_TREE;
7960 /* Optimize ceil of constant value. */
7961 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7963 REAL_VALUE_TYPE x;
7965 x = TREE_REAL_CST (arg);
7966 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7968 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7969 REAL_VALUE_TYPE r;
7971 real_ceil (&r, TYPE_MODE (type), &x);
7972 return build_real (type, r);
7976 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7979 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7980 Return NULL_TREE if no simplification can be made. */
7982 static tree
7983 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7985 if (!validate_arg (arg, REAL_TYPE))
7986 return NULL_TREE;
7988 /* Optimize round of constant value. */
7989 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7991 REAL_VALUE_TYPE x;
7993 x = TREE_REAL_CST (arg);
7994 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7996 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7997 REAL_VALUE_TYPE r;
7999 real_round (&r, TYPE_MODE (type), &x);
8000 return build_real (type, r);
8004 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8007 /* Fold function call to builtin lround, lroundf or lroundl (or the
8008 corresponding long long versions) and other rounding functions. ARG
8009 is the argument to the call. Return NULL_TREE if no simplification
8010 can be made. */
8012 static tree
8013 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8015 if (!validate_arg (arg, REAL_TYPE))
8016 return NULL_TREE;
8018 /* Optimize lround of constant value. */
8019 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8021 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8023 if (real_isfinite (&x))
8025 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8026 tree ftype = TREE_TYPE (arg);
8027 double_int val;
8028 REAL_VALUE_TYPE r;
8030 switch (DECL_FUNCTION_CODE (fndecl))
8032 CASE_FLT_FN (BUILT_IN_IFLOOR):
8033 CASE_FLT_FN (BUILT_IN_LFLOOR):
8034 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8035 real_floor (&r, TYPE_MODE (ftype), &x);
8036 break;
8038 CASE_FLT_FN (BUILT_IN_ICEIL):
8039 CASE_FLT_FN (BUILT_IN_LCEIL):
8040 CASE_FLT_FN (BUILT_IN_LLCEIL):
8041 real_ceil (&r, TYPE_MODE (ftype), &x);
8042 break;
8044 CASE_FLT_FN (BUILT_IN_IROUND):
8045 CASE_FLT_FN (BUILT_IN_LROUND):
8046 CASE_FLT_FN (BUILT_IN_LLROUND):
8047 real_round (&r, TYPE_MODE (ftype), &x);
8048 break;
8050 default:
8051 gcc_unreachable ();
8054 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8055 if (double_int_fits_to_tree_p (itype, val))
8056 return double_int_to_tree (itype, val);
8060 switch (DECL_FUNCTION_CODE (fndecl))
8062 CASE_FLT_FN (BUILT_IN_LFLOOR):
8063 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8064 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8065 if (tree_expr_nonnegative_p (arg))
8066 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8067 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8068 break;
8069 default:;
8072 return fold_fixed_mathfn (loc, fndecl, arg);
8075 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8076 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8077 the argument to the call. Return NULL_TREE if no simplification can
8078 be made. */
8080 static tree
8081 fold_builtin_bitop (tree fndecl, tree arg)
8083 if (!validate_arg (arg, INTEGER_TYPE))
8084 return NULL_TREE;
8086 /* Optimize for constant argument. */
8087 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8089 HOST_WIDE_INT hi, width, result;
8090 unsigned HOST_WIDE_INT lo;
8091 tree type;
8093 type = TREE_TYPE (arg);
8094 width = TYPE_PRECISION (type);
8095 lo = TREE_INT_CST_LOW (arg);
8097 /* Clear all the bits that are beyond the type's precision. */
8098 if (width > HOST_BITS_PER_WIDE_INT)
8100 hi = TREE_INT_CST_HIGH (arg);
8101 if (width < HOST_BITS_PER_DOUBLE_INT)
8102 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8103 << (width - HOST_BITS_PER_WIDE_INT));
8105 else
8107 hi = 0;
8108 if (width < HOST_BITS_PER_WIDE_INT)
8109 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8112 switch (DECL_FUNCTION_CODE (fndecl))
8114 CASE_INT_FN (BUILT_IN_FFS):
8115 if (lo != 0)
8116 result = ffs_hwi (lo);
8117 else if (hi != 0)
8118 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8119 else
8120 result = 0;
8121 break;
8123 CASE_INT_FN (BUILT_IN_CLZ):
8124 if (hi != 0)
8125 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8126 else if (lo != 0)
8127 result = width - floor_log2 (lo) - 1;
8128 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8129 result = width;
8130 break;
8132 CASE_INT_FN (BUILT_IN_CTZ):
8133 if (lo != 0)
8134 result = ctz_hwi (lo);
8135 else if (hi != 0)
8136 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8137 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8138 result = width;
8139 break;
8141 CASE_INT_FN (BUILT_IN_CLRSB):
8142 if (width > HOST_BITS_PER_WIDE_INT
8143 && (hi & ((unsigned HOST_WIDE_INT) 1
8144 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8146 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8147 << (width - HOST_BITS_PER_WIDE_INT - 1));
8148 lo = ~lo;
8150 else if (width <= HOST_BITS_PER_WIDE_INT
8151 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8152 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8153 if (hi != 0)
8154 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8155 else if (lo != 0)
8156 result = width - floor_log2 (lo) - 2;
8157 else
8158 result = width - 1;
8159 break;
8161 CASE_INT_FN (BUILT_IN_POPCOUNT):
8162 result = 0;
8163 while (lo)
8164 result++, lo &= lo - 1;
8165 while (hi)
8166 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8167 break;
8169 CASE_INT_FN (BUILT_IN_PARITY):
8170 result = 0;
8171 while (lo)
8172 result++, lo &= lo - 1;
8173 while (hi)
8174 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8175 result &= 1;
8176 break;
8178 default:
8179 gcc_unreachable ();
8182 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8185 return NULL_TREE;
8188 /* Fold function call to builtin_bswap and the short, long and long long
8189 variants. Return NULL_TREE if no simplification can be made. */
8190 static tree
8191 fold_builtin_bswap (tree fndecl, tree arg)
8193 if (! validate_arg (arg, INTEGER_TYPE))
8194 return NULL_TREE;
8196 /* Optimize constant value. */
8197 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8199 HOST_WIDE_INT hi, width, r_hi = 0;
8200 unsigned HOST_WIDE_INT lo, r_lo = 0;
8201 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8203 width = TYPE_PRECISION (type);
8204 lo = TREE_INT_CST_LOW (arg);
8205 hi = TREE_INT_CST_HIGH (arg);
8207 switch (DECL_FUNCTION_CODE (fndecl))
8209 case BUILT_IN_BSWAP16:
8210 case BUILT_IN_BSWAP32:
8211 case BUILT_IN_BSWAP64:
8213 int s;
8215 for (s = 0; s < width; s += 8)
8217 int d = width - s - 8;
8218 unsigned HOST_WIDE_INT byte;
8220 if (s < HOST_BITS_PER_WIDE_INT)
8221 byte = (lo >> s) & 0xff;
8222 else
8223 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8225 if (d < HOST_BITS_PER_WIDE_INT)
8226 r_lo |= byte << d;
8227 else
8228 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8232 break;
8234 default:
8235 gcc_unreachable ();
8238 if (width < HOST_BITS_PER_WIDE_INT)
8239 return build_int_cst (type, r_lo);
8240 else
8241 return build_int_cst_wide (type, r_lo, r_hi);
8244 return NULL_TREE;
8247 /* A subroutine of fold_builtin to fold the various logarithmic
8248 functions. Return NULL_TREE if no simplification can me made.
8249 FUNC is the corresponding MPFR logarithm function. */
8251 static tree
8252 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8253 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8255 if (validate_arg (arg, REAL_TYPE))
8257 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8258 tree res;
8259 const enum built_in_function fcode = builtin_mathfn_code (arg);
8261 /* Calculate the result when the argument is a constant. */
8262 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8263 return res;
8265 /* Special case, optimize logN(expN(x)) = x. */
8266 if (flag_unsafe_math_optimizations
8267 && ((func == mpfr_log
8268 && (fcode == BUILT_IN_EXP
8269 || fcode == BUILT_IN_EXPF
8270 || fcode == BUILT_IN_EXPL))
8271 || (func == mpfr_log2
8272 && (fcode == BUILT_IN_EXP2
8273 || fcode == BUILT_IN_EXP2F
8274 || fcode == BUILT_IN_EXP2L))
8275 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8276 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8278 /* Optimize logN(func()) for various exponential functions. We
8279 want to determine the value "x" and the power "exponent" in
8280 order to transform logN(x**exponent) into exponent*logN(x). */
8281 if (flag_unsafe_math_optimizations)
8283 tree exponent = 0, x = 0;
8285 switch (fcode)
8287 CASE_FLT_FN (BUILT_IN_EXP):
8288 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8289 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8290 dconst_e ()));
8291 exponent = CALL_EXPR_ARG (arg, 0);
8292 break;
8293 CASE_FLT_FN (BUILT_IN_EXP2):
8294 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8295 x = build_real (type, dconst2);
8296 exponent = CALL_EXPR_ARG (arg, 0);
8297 break;
8298 CASE_FLT_FN (BUILT_IN_EXP10):
8299 CASE_FLT_FN (BUILT_IN_POW10):
8300 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8302 REAL_VALUE_TYPE dconst10;
8303 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8304 x = build_real (type, dconst10);
8306 exponent = CALL_EXPR_ARG (arg, 0);
8307 break;
8308 CASE_FLT_FN (BUILT_IN_SQRT):
8309 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8310 x = CALL_EXPR_ARG (arg, 0);
8311 exponent = build_real (type, dconsthalf);
8312 break;
8313 CASE_FLT_FN (BUILT_IN_CBRT):
8314 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8315 x = CALL_EXPR_ARG (arg, 0);
8316 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8317 dconst_third ()));
8318 break;
8319 CASE_FLT_FN (BUILT_IN_POW):
8320 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8321 x = CALL_EXPR_ARG (arg, 0);
8322 exponent = CALL_EXPR_ARG (arg, 1);
8323 break;
8324 default:
8325 break;
8328 /* Now perform the optimization. */
8329 if (x && exponent)
8331 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8332 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8337 return NULL_TREE;
8340 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8341 NULL_TREE if no simplification can be made. */
8343 static tree
8344 fold_builtin_hypot (location_t loc, tree fndecl,
8345 tree arg0, tree arg1, tree type)
8347 tree res, narg0, narg1;
8349 if (!validate_arg (arg0, REAL_TYPE)
8350 || !validate_arg (arg1, REAL_TYPE))
8351 return NULL_TREE;
8353 /* Calculate the result when the argument is a constant. */
8354 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8355 return res;
8357 /* If either argument to hypot has a negate or abs, strip that off.
8358 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8359 narg0 = fold_strip_sign_ops (arg0);
8360 narg1 = fold_strip_sign_ops (arg1);
8361 if (narg0 || narg1)
8363 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8364 narg1 ? narg1 : arg1);
8367 /* If either argument is zero, hypot is fabs of the other. */
8368 if (real_zerop (arg0))
8369 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8370 else if (real_zerop (arg1))
8371 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8373 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8374 if (flag_unsafe_math_optimizations
8375 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8377 const REAL_VALUE_TYPE sqrt2_trunc
8378 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8379 return fold_build2_loc (loc, MULT_EXPR, type,
8380 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8381 build_real (type, sqrt2_trunc));
8384 return NULL_TREE;
8388 /* Fold a builtin function call to pow, powf, or powl. Return
8389 NULL_TREE if no simplification can be made. */
8390 static tree
8391 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8393 tree res;
8395 if (!validate_arg (arg0, REAL_TYPE)
8396 || !validate_arg (arg1, REAL_TYPE))
8397 return NULL_TREE;
8399 /* Calculate the result when the argument is a constant. */
8400 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8401 return res;
8403 /* Optimize pow(1.0,y) = 1.0. */
8404 if (real_onep (arg0))
8405 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8407 if (TREE_CODE (arg1) == REAL_CST
8408 && !TREE_OVERFLOW (arg1))
8410 REAL_VALUE_TYPE cint;
8411 REAL_VALUE_TYPE c;
8412 HOST_WIDE_INT n;
8414 c = TREE_REAL_CST (arg1);
8416 /* Optimize pow(x,0.0) = 1.0. */
8417 if (REAL_VALUES_EQUAL (c, dconst0))
8418 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8419 arg0);
8421 /* Optimize pow(x,1.0) = x. */
8422 if (REAL_VALUES_EQUAL (c, dconst1))
8423 return arg0;
8425 /* Optimize pow(x,-1.0) = 1.0/x. */
8426 if (REAL_VALUES_EQUAL (c, dconstm1))
8427 return fold_build2_loc (loc, RDIV_EXPR, type,
8428 build_real (type, dconst1), arg0);
8430 /* Optimize pow(x,0.5) = sqrt(x). */
8431 if (flag_unsafe_math_optimizations
8432 && REAL_VALUES_EQUAL (c, dconsthalf))
8434 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8436 if (sqrtfn != NULL_TREE)
8437 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8440 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8441 if (flag_unsafe_math_optimizations)
8443 const REAL_VALUE_TYPE dconstroot
8444 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8446 if (REAL_VALUES_EQUAL (c, dconstroot))
8448 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8449 if (cbrtfn != NULL_TREE)
8450 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8454 /* Check for an integer exponent. */
8455 n = real_to_integer (&c);
8456 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8457 if (real_identical (&c, &cint))
8459 /* Attempt to evaluate pow at compile-time, unless this should
8460 raise an exception. */
8461 if (TREE_CODE (arg0) == REAL_CST
8462 && !TREE_OVERFLOW (arg0)
8463 && (n > 0
8464 || (!flag_trapping_math && !flag_errno_math)
8465 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8467 REAL_VALUE_TYPE x;
8468 bool inexact;
8470 x = TREE_REAL_CST (arg0);
8471 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8472 if (flag_unsafe_math_optimizations || !inexact)
8473 return build_real (type, x);
8476 /* Strip sign ops from even integer powers. */
8477 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8479 tree narg0 = fold_strip_sign_ops (arg0);
8480 if (narg0)
8481 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8486 if (flag_unsafe_math_optimizations)
8488 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8490 /* Optimize pow(expN(x),y) = expN(x*y). */
8491 if (BUILTIN_EXPONENT_P (fcode))
8493 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8494 tree arg = CALL_EXPR_ARG (arg0, 0);
8495 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8496 return build_call_expr_loc (loc, expfn, 1, arg);
8499 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8500 if (BUILTIN_SQRT_P (fcode))
8502 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8503 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8504 build_real (type, dconsthalf));
8505 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8508 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8509 if (BUILTIN_CBRT_P (fcode))
8511 tree arg = CALL_EXPR_ARG (arg0, 0);
8512 if (tree_expr_nonnegative_p (arg))
8514 const REAL_VALUE_TYPE dconstroot
8515 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8516 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8517 build_real (type, dconstroot));
8518 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8522 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8523 if (fcode == BUILT_IN_POW
8524 || fcode == BUILT_IN_POWF
8525 || fcode == BUILT_IN_POWL)
8527 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8528 if (tree_expr_nonnegative_p (arg00))
8530 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8531 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8532 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8537 return NULL_TREE;
8540 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8541 Return NULL_TREE if no simplification can be made. */
8542 static tree
8543 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8544 tree arg0, tree arg1, tree type)
8546 if (!validate_arg (arg0, REAL_TYPE)
8547 || !validate_arg (arg1, INTEGER_TYPE))
8548 return NULL_TREE;
8550 /* Optimize pow(1.0,y) = 1.0. */
8551 if (real_onep (arg0))
8552 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8554 if (host_integerp (arg1, 0))
8556 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8558 /* Evaluate powi at compile-time. */
8559 if (TREE_CODE (arg0) == REAL_CST
8560 && !TREE_OVERFLOW (arg0))
8562 REAL_VALUE_TYPE x;
8563 x = TREE_REAL_CST (arg0);
8564 real_powi (&x, TYPE_MODE (type), &x, c);
8565 return build_real (type, x);
8568 /* Optimize pow(x,0) = 1.0. */
8569 if (c == 0)
8570 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8571 arg0);
8573 /* Optimize pow(x,1) = x. */
8574 if (c == 1)
8575 return arg0;
8577 /* Optimize pow(x,-1) = 1.0/x. */
8578 if (c == -1)
8579 return fold_build2_loc (loc, RDIV_EXPR, type,
8580 build_real (type, dconst1), arg0);
8583 return NULL_TREE;
8586 /* A subroutine of fold_builtin to fold the various exponent
8587 functions. Return NULL_TREE if no simplification can be made.
8588 FUNC is the corresponding MPFR exponent function. */
8590 static tree
8591 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8592 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8594 if (validate_arg (arg, REAL_TYPE))
8596 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8597 tree res;
8599 /* Calculate the result when the argument is a constant. */
8600 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8601 return res;
8603 /* Optimize expN(logN(x)) = x. */
8604 if (flag_unsafe_math_optimizations)
8606 const enum built_in_function fcode = builtin_mathfn_code (arg);
8608 if ((func == mpfr_exp
8609 && (fcode == BUILT_IN_LOG
8610 || fcode == BUILT_IN_LOGF
8611 || fcode == BUILT_IN_LOGL))
8612 || (func == mpfr_exp2
8613 && (fcode == BUILT_IN_LOG2
8614 || fcode == BUILT_IN_LOG2F
8615 || fcode == BUILT_IN_LOG2L))
8616 || (func == mpfr_exp10
8617 && (fcode == BUILT_IN_LOG10
8618 || fcode == BUILT_IN_LOG10F
8619 || fcode == BUILT_IN_LOG10L)))
8620 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8624 return NULL_TREE;
8627 /* Return true if VAR is a VAR_DECL or a component thereof. */
8629 static bool
8630 var_decl_component_p (tree var)
8632 tree inner = var;
8633 while (handled_component_p (inner))
8634 inner = TREE_OPERAND (inner, 0);
8635 return SSA_VAR_P (inner);
8638 /* Fold function call to builtin memset. Return
8639 NULL_TREE if no simplification can be made. */
8641 static tree
8642 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8643 tree type, bool ignore)
8645 tree var, ret, etype;
8646 unsigned HOST_WIDE_INT length, cval;
8648 if (! validate_arg (dest, POINTER_TYPE)
8649 || ! validate_arg (c, INTEGER_TYPE)
8650 || ! validate_arg (len, INTEGER_TYPE))
8651 return NULL_TREE;
8653 if (! host_integerp (len, 1))
8654 return NULL_TREE;
8656 /* If the LEN parameter is zero, return DEST. */
8657 if (integer_zerop (len))
8658 return omit_one_operand_loc (loc, type, dest, c);
8660 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8661 return NULL_TREE;
8663 var = dest;
8664 STRIP_NOPS (var);
8665 if (TREE_CODE (var) != ADDR_EXPR)
8666 return NULL_TREE;
8668 var = TREE_OPERAND (var, 0);
8669 if (TREE_THIS_VOLATILE (var))
8670 return NULL_TREE;
8672 etype = TREE_TYPE (var);
8673 if (TREE_CODE (etype) == ARRAY_TYPE)
8674 etype = TREE_TYPE (etype);
8676 if (!INTEGRAL_TYPE_P (etype)
8677 && !POINTER_TYPE_P (etype))
8678 return NULL_TREE;
8680 if (! var_decl_component_p (var))
8681 return NULL_TREE;
8683 length = tree_low_cst (len, 1);
8684 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8685 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8686 return NULL_TREE;
8688 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8689 return NULL_TREE;
8691 if (integer_zerop (c))
8692 cval = 0;
8693 else
8695 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8696 return NULL_TREE;
8698 cval = TREE_INT_CST_LOW (c);
8699 cval &= 0xff;
8700 cval |= cval << 8;
8701 cval |= cval << 16;
8702 cval |= (cval << 31) << 1;
8705 ret = build_int_cst_type (etype, cval);
8706 var = build_fold_indirect_ref_loc (loc,
8707 fold_convert_loc (loc,
8708 build_pointer_type (etype),
8709 dest));
8710 ret = build2 (MODIFY_EXPR, etype, var, ret);
8711 if (ignore)
8712 return ret;
8714 return omit_one_operand_loc (loc, type, dest, ret);
8717 /* Fold function call to builtin memset. Return
8718 NULL_TREE if no simplification can be made. */
8720 static tree
8721 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8723 if (! validate_arg (dest, POINTER_TYPE)
8724 || ! validate_arg (size, INTEGER_TYPE))
8725 return NULL_TREE;
8727 if (!ignore)
8728 return NULL_TREE;
8730 /* New argument list transforming bzero(ptr x, int y) to
8731 memset(ptr x, int 0, size_t y). This is done this way
8732 so that if it isn't expanded inline, we fallback to
8733 calling bzero instead of memset. */
8735 return fold_builtin_memset (loc, dest, integer_zero_node,
8736 fold_convert_loc (loc, size_type_node, size),
8737 void_type_node, ignore);
8740 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8741 NULL_TREE if no simplification can be made.
8742 If ENDP is 0, return DEST (like memcpy).
8743 If ENDP is 1, return DEST+LEN (like mempcpy).
8744 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8745 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8746 (memmove). */
8748 static tree
8749 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8750 tree len, tree type, bool ignore, int endp)
8752 tree destvar, srcvar, expr;
8754 if (! validate_arg (dest, POINTER_TYPE)
8755 || ! validate_arg (src, POINTER_TYPE)
8756 || ! validate_arg (len, INTEGER_TYPE))
8757 return NULL_TREE;
8759 /* If the LEN parameter is zero, return DEST. */
8760 if (integer_zerop (len))
8761 return omit_one_operand_loc (loc, type, dest, src);
8763 /* If SRC and DEST are the same (and not volatile), return
8764 DEST{,+LEN,+LEN-1}. */
8765 if (operand_equal_p (src, dest, 0))
8766 expr = len;
8767 else
8769 tree srctype, desttype;
8770 unsigned int src_align, dest_align;
8771 tree off0;
8773 if (endp == 3)
8775 src_align = get_pointer_alignment (src);
8776 dest_align = get_pointer_alignment (dest);
8778 /* Both DEST and SRC must be pointer types.
8779 ??? This is what old code did. Is the testing for pointer types
8780 really mandatory?
8782 If either SRC is readonly or length is 1, we can use memcpy. */
8783 if (!dest_align || !src_align)
8784 return NULL_TREE;
8785 if (readonly_data_expr (src)
8786 || (host_integerp (len, 1)
8787 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8788 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8790 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8791 if (!fn)
8792 return NULL_TREE;
8793 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8796 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8797 if (TREE_CODE (src) == ADDR_EXPR
8798 && TREE_CODE (dest) == ADDR_EXPR)
8800 tree src_base, dest_base, fn;
8801 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8802 HOST_WIDE_INT size = -1;
8803 HOST_WIDE_INT maxsize = -1;
8805 srcvar = TREE_OPERAND (src, 0);
8806 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8807 &size, &maxsize);
8808 destvar = TREE_OPERAND (dest, 0);
8809 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8810 &size, &maxsize);
8811 if (host_integerp (len, 1))
8812 maxsize = tree_low_cst (len, 1);
8813 else
8814 maxsize = -1;
8815 src_offset /= BITS_PER_UNIT;
8816 dest_offset /= BITS_PER_UNIT;
8817 if (SSA_VAR_P (src_base)
8818 && SSA_VAR_P (dest_base))
8820 if (operand_equal_p (src_base, dest_base, 0)
8821 && ranges_overlap_p (src_offset, maxsize,
8822 dest_offset, maxsize))
8823 return NULL_TREE;
8825 else if (TREE_CODE (src_base) == MEM_REF
8826 && TREE_CODE (dest_base) == MEM_REF)
8828 double_int off;
8829 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8830 TREE_OPERAND (dest_base, 0), 0))
8831 return NULL_TREE;
8832 off = mem_ref_offset (src_base) +
8833 double_int::from_shwi (src_offset);
8834 if (!off.fits_shwi ())
8835 return NULL_TREE;
8836 src_offset = off.low;
8837 off = mem_ref_offset (dest_base) +
8838 double_int::from_shwi (dest_offset);
8839 if (!off.fits_shwi ())
8840 return NULL_TREE;
8841 dest_offset = off.low;
8842 if (ranges_overlap_p (src_offset, maxsize,
8843 dest_offset, maxsize))
8844 return NULL_TREE;
8846 else
8847 return NULL_TREE;
8849 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8850 if (!fn)
8851 return NULL_TREE;
8852 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8855 /* If the destination and source do not alias optimize into
8856 memcpy as well. */
8857 if ((is_gimple_min_invariant (dest)
8858 || TREE_CODE (dest) == SSA_NAME)
8859 && (is_gimple_min_invariant (src)
8860 || TREE_CODE (src) == SSA_NAME))
8862 ao_ref destr, srcr;
8863 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8864 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8865 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8867 tree fn;
8868 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8869 if (!fn)
8870 return NULL_TREE;
8871 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8875 return NULL_TREE;
8878 if (!host_integerp (len, 0))
8879 return NULL_TREE;
8880 /* FIXME:
8881 This logic lose for arguments like (type *)malloc (sizeof (type)),
8882 since we strip the casts of up to VOID return value from malloc.
8883 Perhaps we ought to inherit type from non-VOID argument here? */
8884 STRIP_NOPS (src);
8885 STRIP_NOPS (dest);
8886 if (!POINTER_TYPE_P (TREE_TYPE (src))
8887 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8888 return NULL_TREE;
8889 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8890 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8892 tree tem = TREE_OPERAND (src, 0);
8893 STRIP_NOPS (tem);
8894 if (tem != TREE_OPERAND (src, 0))
8895 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8897 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8899 tree tem = TREE_OPERAND (dest, 0);
8900 STRIP_NOPS (tem);
8901 if (tem != TREE_OPERAND (dest, 0))
8902 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8904 srctype = TREE_TYPE (TREE_TYPE (src));
8905 if (TREE_CODE (srctype) == ARRAY_TYPE
8906 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8908 srctype = TREE_TYPE (srctype);
8909 STRIP_NOPS (src);
8910 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8912 desttype = TREE_TYPE (TREE_TYPE (dest));
8913 if (TREE_CODE (desttype) == ARRAY_TYPE
8914 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8916 desttype = TREE_TYPE (desttype);
8917 STRIP_NOPS (dest);
8918 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8920 if (TREE_ADDRESSABLE (srctype)
8921 || TREE_ADDRESSABLE (desttype))
8922 return NULL_TREE;
8924 src_align = get_pointer_alignment (src);
8925 dest_align = get_pointer_alignment (dest);
8926 if (dest_align < TYPE_ALIGN (desttype)
8927 || src_align < TYPE_ALIGN (srctype))
8928 return NULL_TREE;
8930 if (!ignore)
8931 dest = builtin_save_expr (dest);
8933 /* Build accesses at offset zero with a ref-all character type. */
8934 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8935 ptr_mode, true), 0);
8937 destvar = dest;
8938 STRIP_NOPS (destvar);
8939 if (TREE_CODE (destvar) == ADDR_EXPR
8940 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8941 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8942 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8943 else
8944 destvar = NULL_TREE;
8946 srcvar = src;
8947 STRIP_NOPS (srcvar);
8948 if (TREE_CODE (srcvar) == ADDR_EXPR
8949 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8950 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8952 if (!destvar
8953 || src_align >= TYPE_ALIGN (desttype))
8954 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8955 srcvar, off0);
8956 else if (!STRICT_ALIGNMENT)
8958 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8959 src_align);
8960 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8962 else
8963 srcvar = NULL_TREE;
8965 else
8966 srcvar = NULL_TREE;
8968 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8969 return NULL_TREE;
8971 if (srcvar == NULL_TREE)
8973 STRIP_NOPS (src);
8974 if (src_align >= TYPE_ALIGN (desttype))
8975 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8976 else
8978 if (STRICT_ALIGNMENT)
8979 return NULL_TREE;
8980 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8981 src_align);
8982 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8985 else if (destvar == NULL_TREE)
8987 STRIP_NOPS (dest);
8988 if (dest_align >= TYPE_ALIGN (srctype))
8989 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8990 else
8992 if (STRICT_ALIGNMENT)
8993 return NULL_TREE;
8994 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8995 dest_align);
8996 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
9000 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9003 if (ignore)
9004 return expr;
9006 if (endp == 0 || endp == 3)
9007 return omit_one_operand_loc (loc, type, dest, expr);
9009 if (expr == len)
9010 expr = NULL_TREE;
9012 if (endp == 2)
9013 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9014 ssize_int (1));
9016 dest = fold_build_pointer_plus_loc (loc, dest, len);
9017 dest = fold_convert_loc (loc, type, dest);
9018 if (expr)
9019 dest = omit_one_operand_loc (loc, type, dest, expr);
9020 return dest;
9023 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9024 If LEN is not NULL, it represents the length of the string to be
9025 copied. Return NULL_TREE if no simplification can be made. */
9027 tree
9028 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9030 tree fn;
9032 if (!validate_arg (dest, POINTER_TYPE)
9033 || !validate_arg (src, POINTER_TYPE))
9034 return NULL_TREE;
9036 /* If SRC and DEST are the same (and not volatile), return DEST. */
9037 if (operand_equal_p (src, dest, 0))
9038 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9040 if (optimize_function_for_size_p (cfun))
9041 return NULL_TREE;
9043 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9044 if (!fn)
9045 return NULL_TREE;
9047 if (!len)
9049 len = c_strlen (src, 1);
9050 if (! len || TREE_SIDE_EFFECTS (len))
9051 return NULL_TREE;
9054 len = fold_convert_loc (loc, size_type_node, len);
9055 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9056 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9057 build_call_expr_loc (loc, fn, 3, dest, src, len));
9060 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9061 Return NULL_TREE if no simplification can be made. */
9063 static tree
9064 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9066 tree fn, len, lenp1, call, type;
9068 if (!validate_arg (dest, POINTER_TYPE)
9069 || !validate_arg (src, POINTER_TYPE))
9070 return NULL_TREE;
9072 len = c_strlen (src, 1);
9073 if (!len
9074 || TREE_CODE (len) != INTEGER_CST)
9075 return NULL_TREE;
9077 if (optimize_function_for_size_p (cfun)
9078 /* If length is zero it's small enough. */
9079 && !integer_zerop (len))
9080 return NULL_TREE;
9082 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9083 if (!fn)
9084 return NULL_TREE;
9086 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9087 fold_convert_loc (loc, size_type_node, len),
9088 build_int_cst (size_type_node, 1));
9089 /* We use dest twice in building our expression. Save it from
9090 multiple expansions. */
9091 dest = builtin_save_expr (dest);
9092 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9094 type = TREE_TYPE (TREE_TYPE (fndecl));
9095 dest = fold_build_pointer_plus_loc (loc, dest, len);
9096 dest = fold_convert_loc (loc, type, dest);
9097 dest = omit_one_operand_loc (loc, type, dest, call);
9098 return dest;
9101 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9102 If SLEN is not NULL, it represents the length of the source string.
9103 Return NULL_TREE if no simplification can be made. */
9105 tree
9106 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9107 tree src, tree len, tree slen)
9109 tree fn;
9111 if (!validate_arg (dest, POINTER_TYPE)
9112 || !validate_arg (src, POINTER_TYPE)
9113 || !validate_arg (len, INTEGER_TYPE))
9114 return NULL_TREE;
9116 /* If the LEN parameter is zero, return DEST. */
9117 if (integer_zerop (len))
9118 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9120 /* We can't compare slen with len as constants below if len is not a
9121 constant. */
9122 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9123 return NULL_TREE;
9125 if (!slen)
9126 slen = c_strlen (src, 1);
9128 /* Now, we must be passed a constant src ptr parameter. */
9129 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9130 return NULL_TREE;
9132 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9134 /* We do not support simplification of this case, though we do
9135 support it when expanding trees into RTL. */
9136 /* FIXME: generate a call to __builtin_memset. */
9137 if (tree_int_cst_lt (slen, len))
9138 return NULL_TREE;
9140 /* OK transform into builtin memcpy. */
9141 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9142 if (!fn)
9143 return NULL_TREE;
9145 len = fold_convert_loc (loc, size_type_node, len);
9146 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9147 build_call_expr_loc (loc, fn, 3, dest, src, len));
9150 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9151 arguments to the call, and TYPE is its return type.
9152 Return NULL_TREE if no simplification can be made. */
9154 static tree
9155 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9157 if (!validate_arg (arg1, POINTER_TYPE)
9158 || !validate_arg (arg2, INTEGER_TYPE)
9159 || !validate_arg (len, INTEGER_TYPE))
9160 return NULL_TREE;
9161 else
9163 const char *p1;
9165 if (TREE_CODE (arg2) != INTEGER_CST
9166 || !host_integerp (len, 1))
9167 return NULL_TREE;
9169 p1 = c_getstr (arg1);
9170 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9172 char c;
9173 const char *r;
9174 tree tem;
9176 if (target_char_cast (arg2, &c))
9177 return NULL_TREE;
9179 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9181 if (r == NULL)
9182 return build_int_cst (TREE_TYPE (arg1), 0);
9184 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9185 return fold_convert_loc (loc, type, tem);
9187 return NULL_TREE;
9191 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9192 Return NULL_TREE if no simplification can be made. */
9194 static tree
9195 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9197 const char *p1, *p2;
9199 if (!validate_arg (arg1, POINTER_TYPE)
9200 || !validate_arg (arg2, POINTER_TYPE)
9201 || !validate_arg (len, INTEGER_TYPE))
9202 return NULL_TREE;
9204 /* If the LEN parameter is zero, return zero. */
9205 if (integer_zerop (len))
9206 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9207 arg1, arg2);
9209 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9210 if (operand_equal_p (arg1, arg2, 0))
9211 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9213 p1 = c_getstr (arg1);
9214 p2 = c_getstr (arg2);
9216 /* If all arguments are constant, and the value of len is not greater
9217 than the lengths of arg1 and arg2, evaluate at compile-time. */
9218 if (host_integerp (len, 1) && p1 && p2
9219 && compare_tree_int (len, strlen (p1) + 1) <= 0
9220 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9222 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9224 if (r > 0)
9225 return integer_one_node;
9226 else if (r < 0)
9227 return integer_minus_one_node;
9228 else
9229 return integer_zero_node;
9232 /* If len parameter is one, return an expression corresponding to
9233 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9234 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9236 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9237 tree cst_uchar_ptr_node
9238 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9240 tree ind1
9241 = fold_convert_loc (loc, integer_type_node,
9242 build1 (INDIRECT_REF, cst_uchar_node,
9243 fold_convert_loc (loc,
9244 cst_uchar_ptr_node,
9245 arg1)));
9246 tree ind2
9247 = fold_convert_loc (loc, integer_type_node,
9248 build1 (INDIRECT_REF, cst_uchar_node,
9249 fold_convert_loc (loc,
9250 cst_uchar_ptr_node,
9251 arg2)));
9252 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9255 return NULL_TREE;
9258 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9259 Return NULL_TREE if no simplification can be made. */
9261 static tree
9262 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9264 const char *p1, *p2;
9266 if (!validate_arg (arg1, POINTER_TYPE)
9267 || !validate_arg (arg2, POINTER_TYPE))
9268 return NULL_TREE;
9270 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9271 if (operand_equal_p (arg1, arg2, 0))
9272 return integer_zero_node;
9274 p1 = c_getstr (arg1);
9275 p2 = c_getstr (arg2);
9277 if (p1 && p2)
9279 const int i = strcmp (p1, p2);
9280 if (i < 0)
9281 return integer_minus_one_node;
9282 else if (i > 0)
9283 return integer_one_node;
9284 else
9285 return integer_zero_node;
9288 /* If the second arg is "", return *(const unsigned char*)arg1. */
9289 if (p2 && *p2 == '\0')
9291 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9292 tree cst_uchar_ptr_node
9293 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9295 return fold_convert_loc (loc, integer_type_node,
9296 build1 (INDIRECT_REF, cst_uchar_node,
9297 fold_convert_loc (loc,
9298 cst_uchar_ptr_node,
9299 arg1)));
9302 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9303 if (p1 && *p1 == '\0')
9305 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9306 tree cst_uchar_ptr_node
9307 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9309 tree temp
9310 = fold_convert_loc (loc, integer_type_node,
9311 build1 (INDIRECT_REF, cst_uchar_node,
9312 fold_convert_loc (loc,
9313 cst_uchar_ptr_node,
9314 arg2)));
9315 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9318 return NULL_TREE;
9321 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9322 Return NULL_TREE if no simplification can be made. */
9324 static tree
9325 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9327 const char *p1, *p2;
9329 if (!validate_arg (arg1, POINTER_TYPE)
9330 || !validate_arg (arg2, POINTER_TYPE)
9331 || !validate_arg (len, INTEGER_TYPE))
9332 return NULL_TREE;
9334 /* If the LEN parameter is zero, return zero. */
9335 if (integer_zerop (len))
9336 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9337 arg1, arg2);
9339 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9340 if (operand_equal_p (arg1, arg2, 0))
9341 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9343 p1 = c_getstr (arg1);
9344 p2 = c_getstr (arg2);
9346 if (host_integerp (len, 1) && p1 && p2)
9348 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9349 if (i > 0)
9350 return integer_one_node;
9351 else if (i < 0)
9352 return integer_minus_one_node;
9353 else
9354 return integer_zero_node;
9357 /* If the second arg is "", and the length is greater than zero,
9358 return *(const unsigned char*)arg1. */
9359 if (p2 && *p2 == '\0'
9360 && TREE_CODE (len) == INTEGER_CST
9361 && tree_int_cst_sgn (len) == 1)
9363 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9364 tree cst_uchar_ptr_node
9365 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9367 return fold_convert_loc (loc, integer_type_node,
9368 build1 (INDIRECT_REF, cst_uchar_node,
9369 fold_convert_loc (loc,
9370 cst_uchar_ptr_node,
9371 arg1)));
9374 /* If the first arg is "", and the length is greater than zero,
9375 return -*(const unsigned char*)arg2. */
9376 if (p1 && *p1 == '\0'
9377 && TREE_CODE (len) == INTEGER_CST
9378 && tree_int_cst_sgn (len) == 1)
9380 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9381 tree cst_uchar_ptr_node
9382 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9384 tree temp = fold_convert_loc (loc, integer_type_node,
9385 build1 (INDIRECT_REF, cst_uchar_node,
9386 fold_convert_loc (loc,
9387 cst_uchar_ptr_node,
9388 arg2)));
9389 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9392 /* If len parameter is one, return an expression corresponding to
9393 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9394 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9396 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9397 tree cst_uchar_ptr_node
9398 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9400 tree ind1 = fold_convert_loc (loc, integer_type_node,
9401 build1 (INDIRECT_REF, cst_uchar_node,
9402 fold_convert_loc (loc,
9403 cst_uchar_ptr_node,
9404 arg1)));
9405 tree ind2 = fold_convert_loc (loc, integer_type_node,
9406 build1 (INDIRECT_REF, cst_uchar_node,
9407 fold_convert_loc (loc,
9408 cst_uchar_ptr_node,
9409 arg2)));
9410 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9413 return NULL_TREE;
9416 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9417 ARG. Return NULL_TREE if no simplification can be made. */
9419 static tree
9420 fold_builtin_signbit (location_t loc, tree arg, tree type)
9422 if (!validate_arg (arg, REAL_TYPE))
9423 return NULL_TREE;
9425 /* If ARG is a compile-time constant, determine the result. */
9426 if (TREE_CODE (arg) == REAL_CST
9427 && !TREE_OVERFLOW (arg))
9429 REAL_VALUE_TYPE c;
9431 c = TREE_REAL_CST (arg);
9432 return (REAL_VALUE_NEGATIVE (c)
9433 ? build_one_cst (type)
9434 : build_zero_cst (type));
9437 /* If ARG is non-negative, the result is always zero. */
9438 if (tree_expr_nonnegative_p (arg))
9439 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9441 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9442 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9443 return fold_convert (type,
9444 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9445 build_real (TREE_TYPE (arg), dconst0)));
9447 return NULL_TREE;
9450 /* Fold function call to builtin copysign, copysignf or copysignl with
9451 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9452 be made. */
9454 static tree
9455 fold_builtin_copysign (location_t loc, tree fndecl,
9456 tree arg1, tree arg2, tree type)
9458 tree tem;
9460 if (!validate_arg (arg1, REAL_TYPE)
9461 || !validate_arg (arg2, REAL_TYPE))
9462 return NULL_TREE;
9464 /* copysign(X,X) is X. */
9465 if (operand_equal_p (arg1, arg2, 0))
9466 return fold_convert_loc (loc, type, arg1);
9468 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9469 if (TREE_CODE (arg1) == REAL_CST
9470 && TREE_CODE (arg2) == REAL_CST
9471 && !TREE_OVERFLOW (arg1)
9472 && !TREE_OVERFLOW (arg2))
9474 REAL_VALUE_TYPE c1, c2;
9476 c1 = TREE_REAL_CST (arg1);
9477 c2 = TREE_REAL_CST (arg2);
9478 /* c1.sign := c2.sign. */
9479 real_copysign (&c1, &c2);
9480 return build_real (type, c1);
9483 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9484 Remember to evaluate Y for side-effects. */
9485 if (tree_expr_nonnegative_p (arg2))
9486 return omit_one_operand_loc (loc, type,
9487 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9488 arg2);
9490 /* Strip sign changing operations for the first argument. */
9491 tem = fold_strip_sign_ops (arg1);
9492 if (tem)
9493 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9495 return NULL_TREE;
9498 /* Fold a call to builtin isascii with argument ARG. */
9500 static tree
9501 fold_builtin_isascii (location_t loc, tree arg)
9503 if (!validate_arg (arg, INTEGER_TYPE))
9504 return NULL_TREE;
9505 else
9507 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9508 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9509 build_int_cst (integer_type_node,
9510 ~ (unsigned HOST_WIDE_INT) 0x7f));
9511 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9512 arg, integer_zero_node);
9516 /* Fold a call to builtin toascii with argument ARG. */
9518 static tree
9519 fold_builtin_toascii (location_t loc, tree arg)
9521 if (!validate_arg (arg, INTEGER_TYPE))
9522 return NULL_TREE;
9524 /* Transform toascii(c) -> (c & 0x7f). */
9525 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9526 build_int_cst (integer_type_node, 0x7f));
9529 /* Fold a call to builtin isdigit with argument ARG. */
9531 static tree
9532 fold_builtin_isdigit (location_t loc, tree arg)
9534 if (!validate_arg (arg, INTEGER_TYPE))
9535 return NULL_TREE;
9536 else
9538 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9539 /* According to the C standard, isdigit is unaffected by locale.
9540 However, it definitely is affected by the target character set. */
9541 unsigned HOST_WIDE_INT target_digit0
9542 = lang_hooks.to_target_charset ('0');
9544 if (target_digit0 == 0)
9545 return NULL_TREE;
9547 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9548 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9549 build_int_cst (unsigned_type_node, target_digit0));
9550 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9551 build_int_cst (unsigned_type_node, 9));
9555 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9557 static tree
9558 fold_builtin_fabs (location_t loc, tree arg, tree type)
9560 if (!validate_arg (arg, REAL_TYPE))
9561 return NULL_TREE;
9563 arg = fold_convert_loc (loc, type, arg);
9564 if (TREE_CODE (arg) == REAL_CST)
9565 return fold_abs_const (arg, type);
9566 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9569 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9571 static tree
9572 fold_builtin_abs (location_t loc, tree arg, tree type)
9574 if (!validate_arg (arg, INTEGER_TYPE))
9575 return NULL_TREE;
9577 arg = fold_convert_loc (loc, type, arg);
9578 if (TREE_CODE (arg) == INTEGER_CST)
9579 return fold_abs_const (arg, type);
9580 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9583 /* Fold a fma operation with arguments ARG[012]. */
9585 tree
9586 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9587 tree type, tree arg0, tree arg1, tree arg2)
9589 if (TREE_CODE (arg0) == REAL_CST
9590 && TREE_CODE (arg1) == REAL_CST
9591 && TREE_CODE (arg2) == REAL_CST)
9592 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9594 return NULL_TREE;
9597 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9599 static tree
9600 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9602 if (validate_arg (arg0, REAL_TYPE)
9603 && validate_arg(arg1, REAL_TYPE)
9604 && validate_arg(arg2, REAL_TYPE))
9606 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9607 if (tem)
9608 return tem;
9610 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9611 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9612 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9614 return NULL_TREE;
9617 /* Fold a call to builtin fmin or fmax. */
9619 static tree
9620 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9621 tree type, bool max)
9623 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9625 /* Calculate the result when the argument is a constant. */
9626 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9628 if (res)
9629 return res;
9631 /* If either argument is NaN, return the other one. Avoid the
9632 transformation if we get (and honor) a signalling NaN. Using
9633 omit_one_operand() ensures we create a non-lvalue. */
9634 if (TREE_CODE (arg0) == REAL_CST
9635 && real_isnan (&TREE_REAL_CST (arg0))
9636 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9637 || ! TREE_REAL_CST (arg0).signalling))
9638 return omit_one_operand_loc (loc, type, arg1, arg0);
9639 if (TREE_CODE (arg1) == REAL_CST
9640 && real_isnan (&TREE_REAL_CST (arg1))
9641 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9642 || ! TREE_REAL_CST (arg1).signalling))
9643 return omit_one_operand_loc (loc, type, arg0, arg1);
9645 /* Transform fmin/fmax(x,x) -> x. */
9646 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9647 return omit_one_operand_loc (loc, type, arg0, arg1);
9649 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9650 functions to return the numeric arg if the other one is NaN.
9651 These tree codes don't honor that, so only transform if
9652 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9653 handled, so we don't have to worry about it either. */
9654 if (flag_finite_math_only)
9655 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9656 fold_convert_loc (loc, type, arg0),
9657 fold_convert_loc (loc, type, arg1));
9659 return NULL_TREE;
9662 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9664 static tree
9665 fold_builtin_carg (location_t loc, tree arg, tree type)
9667 if (validate_arg (arg, COMPLEX_TYPE)
9668 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9670 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9672 if (atan2_fn)
9674 tree new_arg = builtin_save_expr (arg);
9675 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9676 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9677 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9681 return NULL_TREE;
9684 /* Fold a call to builtin logb/ilogb. */
9686 static tree
9687 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9689 if (! validate_arg (arg, REAL_TYPE))
9690 return NULL_TREE;
9692 STRIP_NOPS (arg);
9694 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9696 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9698 switch (value->cl)
9700 case rvc_nan:
9701 case rvc_inf:
9702 /* If arg is Inf or NaN and we're logb, return it. */
9703 if (TREE_CODE (rettype) == REAL_TYPE)
9704 return fold_convert_loc (loc, rettype, arg);
9705 /* Fall through... */
9706 case rvc_zero:
9707 /* Zero may set errno and/or raise an exception for logb, also
9708 for ilogb we don't know FP_ILOGB0. */
9709 return NULL_TREE;
9710 case rvc_normal:
9711 /* For normal numbers, proceed iff radix == 2. In GCC,
9712 normalized significands are in the range [0.5, 1.0). We
9713 want the exponent as if they were [1.0, 2.0) so get the
9714 exponent and subtract 1. */
9715 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9716 return fold_convert_loc (loc, rettype,
9717 build_int_cst (integer_type_node,
9718 REAL_EXP (value)-1));
9719 break;
9723 return NULL_TREE;
9726 /* Fold a call to builtin significand, if radix == 2. */
9728 static tree
9729 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9731 if (! validate_arg (arg, REAL_TYPE))
9732 return NULL_TREE;
9734 STRIP_NOPS (arg);
9736 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9738 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9740 switch (value->cl)
9742 case rvc_zero:
9743 case rvc_nan:
9744 case rvc_inf:
9745 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9746 return fold_convert_loc (loc, rettype, arg);
9747 case rvc_normal:
9748 /* For normal numbers, proceed iff radix == 2. */
9749 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9751 REAL_VALUE_TYPE result = *value;
9752 /* In GCC, normalized significands are in the range [0.5,
9753 1.0). We want them to be [1.0, 2.0) so set the
9754 exponent to 1. */
9755 SET_REAL_EXP (&result, 1);
9756 return build_real (rettype, result);
9758 break;
9762 return NULL_TREE;
9765 /* Fold a call to builtin frexp, we can assume the base is 2. */
9767 static tree
9768 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9770 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9771 return NULL_TREE;
9773 STRIP_NOPS (arg0);
9775 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9776 return NULL_TREE;
9778 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9780 /* Proceed if a valid pointer type was passed in. */
9781 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9783 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9784 tree frac, exp;
9786 switch (value->cl)
9788 case rvc_zero:
9789 /* For +-0, return (*exp = 0, +-0). */
9790 exp = integer_zero_node;
9791 frac = arg0;
9792 break;
9793 case rvc_nan:
9794 case rvc_inf:
9795 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9796 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9797 case rvc_normal:
9799 /* Since the frexp function always expects base 2, and in
9800 GCC normalized significands are already in the range
9801 [0.5, 1.0), we have exactly what frexp wants. */
9802 REAL_VALUE_TYPE frac_rvt = *value;
9803 SET_REAL_EXP (&frac_rvt, 0);
9804 frac = build_real (rettype, frac_rvt);
9805 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9807 break;
9808 default:
9809 gcc_unreachable ();
9812 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9813 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9814 TREE_SIDE_EFFECTS (arg1) = 1;
9815 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9818 return NULL_TREE;
9821 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9822 then we can assume the base is two. If it's false, then we have to
9823 check the mode of the TYPE parameter in certain cases. */
9825 static tree
9826 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9827 tree type, bool ldexp)
9829 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9831 STRIP_NOPS (arg0);
9832 STRIP_NOPS (arg1);
9834 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9835 if (real_zerop (arg0) || integer_zerop (arg1)
9836 || (TREE_CODE (arg0) == REAL_CST
9837 && !real_isfinite (&TREE_REAL_CST (arg0))))
9838 return omit_one_operand_loc (loc, type, arg0, arg1);
9840 /* If both arguments are constant, then try to evaluate it. */
9841 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9842 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9843 && host_integerp (arg1, 0))
9845 /* Bound the maximum adjustment to twice the range of the
9846 mode's valid exponents. Use abs to ensure the range is
9847 positive as a sanity check. */
9848 const long max_exp_adj = 2 *
9849 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9850 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9852 /* Get the user-requested adjustment. */
9853 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9855 /* The requested adjustment must be inside this range. This
9856 is a preliminary cap to avoid things like overflow, we
9857 may still fail to compute the result for other reasons. */
9858 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9860 REAL_VALUE_TYPE initial_result;
9862 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9864 /* Ensure we didn't overflow. */
9865 if (! real_isinf (&initial_result))
9867 const REAL_VALUE_TYPE trunc_result
9868 = real_value_truncate (TYPE_MODE (type), initial_result);
9870 /* Only proceed if the target mode can hold the
9871 resulting value. */
9872 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9873 return build_real (type, trunc_result);
9879 return NULL_TREE;
9882 /* Fold a call to builtin modf. */
9884 static tree
9885 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9887 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9888 return NULL_TREE;
9890 STRIP_NOPS (arg0);
9892 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9893 return NULL_TREE;
9895 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9897 /* Proceed if a valid pointer type was passed in. */
9898 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9900 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9901 REAL_VALUE_TYPE trunc, frac;
9903 switch (value->cl)
9905 case rvc_nan:
9906 case rvc_zero:
9907 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9908 trunc = frac = *value;
9909 break;
9910 case rvc_inf:
9911 /* For +-Inf, return (*arg1 = arg0, +-0). */
9912 frac = dconst0;
9913 frac.sign = value->sign;
9914 trunc = *value;
9915 break;
9916 case rvc_normal:
9917 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9918 real_trunc (&trunc, VOIDmode, value);
9919 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9920 /* If the original number was negative and already
9921 integral, then the fractional part is -0.0. */
9922 if (value->sign && frac.cl == rvc_zero)
9923 frac.sign = value->sign;
9924 break;
9927 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9928 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9929 build_real (rettype, trunc));
9930 TREE_SIDE_EFFECTS (arg1) = 1;
9931 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9932 build_real (rettype, frac));
9935 return NULL_TREE;
9938 /* Given a location LOC, an interclass builtin function decl FNDECL
9939 and its single argument ARG, return an folded expression computing
9940 the same, or NULL_TREE if we either couldn't or didn't want to fold
9941 (the latter happen if there's an RTL instruction available). */
9943 static tree
9944 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9946 enum machine_mode mode;
9948 if (!validate_arg (arg, REAL_TYPE))
9949 return NULL_TREE;
9951 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9952 return NULL_TREE;
9954 mode = TYPE_MODE (TREE_TYPE (arg));
9956 /* If there is no optab, try generic code. */
9957 switch (DECL_FUNCTION_CODE (fndecl))
9959 tree result;
9961 CASE_FLT_FN (BUILT_IN_ISINF):
9963 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9964 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9965 tree const type = TREE_TYPE (arg);
9966 REAL_VALUE_TYPE r;
9967 char buf[128];
9969 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9970 real_from_string (&r, buf);
9971 result = build_call_expr (isgr_fn, 2,
9972 fold_build1_loc (loc, ABS_EXPR, type, arg),
9973 build_real (type, r));
9974 return result;
9976 CASE_FLT_FN (BUILT_IN_FINITE):
9977 case BUILT_IN_ISFINITE:
9979 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9980 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9981 tree const type = TREE_TYPE (arg);
9982 REAL_VALUE_TYPE r;
9983 char buf[128];
9985 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9986 real_from_string (&r, buf);
9987 result = build_call_expr (isle_fn, 2,
9988 fold_build1_loc (loc, ABS_EXPR, type, arg),
9989 build_real (type, r));
9990 /*result = fold_build2_loc (loc, UNGT_EXPR,
9991 TREE_TYPE (TREE_TYPE (fndecl)),
9992 fold_build1_loc (loc, ABS_EXPR, type, arg),
9993 build_real (type, r));
9994 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9995 TREE_TYPE (TREE_TYPE (fndecl)),
9996 result);*/
9997 return result;
9999 case BUILT_IN_ISNORMAL:
10001 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10002 islessequal(fabs(x),DBL_MAX). */
10003 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10004 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10005 tree const type = TREE_TYPE (arg);
10006 REAL_VALUE_TYPE rmax, rmin;
10007 char buf[128];
10009 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10010 real_from_string (&rmax, buf);
10011 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10012 real_from_string (&rmin, buf);
10013 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10014 result = build_call_expr (isle_fn, 2, arg,
10015 build_real (type, rmax));
10016 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10017 build_call_expr (isge_fn, 2, arg,
10018 build_real (type, rmin)));
10019 return result;
10021 default:
10022 break;
10025 return NULL_TREE;
10028 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10029 ARG is the argument for the call. */
10031 static tree
10032 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10034 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10035 REAL_VALUE_TYPE r;
10037 if (!validate_arg (arg, REAL_TYPE))
10038 return NULL_TREE;
10040 switch (builtin_index)
10042 case BUILT_IN_ISINF:
10043 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10044 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10046 if (TREE_CODE (arg) == REAL_CST)
10048 r = TREE_REAL_CST (arg);
10049 if (real_isinf (&r))
10050 return real_compare (GT_EXPR, &r, &dconst0)
10051 ? integer_one_node : integer_minus_one_node;
10052 else
10053 return integer_zero_node;
10056 return NULL_TREE;
10058 case BUILT_IN_ISINF_SIGN:
10060 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10061 /* In a boolean context, GCC will fold the inner COND_EXPR to
10062 1. So e.g. "if (isinf_sign(x))" would be folded to just
10063 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10064 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10065 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10066 tree tmp = NULL_TREE;
10068 arg = builtin_save_expr (arg);
10070 if (signbit_fn && isinf_fn)
10072 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10073 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10075 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10076 signbit_call, integer_zero_node);
10077 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10078 isinf_call, integer_zero_node);
10080 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10081 integer_minus_one_node, integer_one_node);
10082 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10083 isinf_call, tmp,
10084 integer_zero_node);
10087 return tmp;
10090 case BUILT_IN_ISFINITE:
10091 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10092 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10093 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10095 if (TREE_CODE (arg) == REAL_CST)
10097 r = TREE_REAL_CST (arg);
10098 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10101 return NULL_TREE;
10103 case BUILT_IN_ISNAN:
10104 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10105 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10107 if (TREE_CODE (arg) == REAL_CST)
10109 r = TREE_REAL_CST (arg);
10110 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10113 arg = builtin_save_expr (arg);
10114 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10116 default:
10117 gcc_unreachable ();
10121 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10122 This builtin will generate code to return the appropriate floating
10123 point classification depending on the value of the floating point
10124 number passed in. The possible return values must be supplied as
10125 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10126 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10127 one floating point argument which is "type generic". */
10129 static tree
10130 fold_builtin_fpclassify (location_t loc, tree exp)
10132 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10133 arg, type, res, tmp;
10134 enum machine_mode mode;
10135 REAL_VALUE_TYPE r;
10136 char buf[128];
10138 /* Verify the required arguments in the original call. */
10139 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10140 INTEGER_TYPE, INTEGER_TYPE,
10141 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10142 return NULL_TREE;
10144 fp_nan = CALL_EXPR_ARG (exp, 0);
10145 fp_infinite = CALL_EXPR_ARG (exp, 1);
10146 fp_normal = CALL_EXPR_ARG (exp, 2);
10147 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10148 fp_zero = CALL_EXPR_ARG (exp, 4);
10149 arg = CALL_EXPR_ARG (exp, 5);
10150 type = TREE_TYPE (arg);
10151 mode = TYPE_MODE (type);
10152 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10154 /* fpclassify(x) ->
10155 isnan(x) ? FP_NAN :
10156 (fabs(x) == Inf ? FP_INFINITE :
10157 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10158 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10160 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10161 build_real (type, dconst0));
10162 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10163 tmp, fp_zero, fp_subnormal);
10165 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10166 real_from_string (&r, buf);
10167 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10168 arg, build_real (type, r));
10169 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10171 if (HONOR_INFINITIES (mode))
10173 real_inf (&r);
10174 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10175 build_real (type, r));
10176 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10177 fp_infinite, res);
10180 if (HONOR_NANS (mode))
10182 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10183 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10186 return res;
10189 /* Fold a call to an unordered comparison function such as
10190 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10191 being called and ARG0 and ARG1 are the arguments for the call.
10192 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10193 the opposite of the desired result. UNORDERED_CODE is used
10194 for modes that can hold NaNs and ORDERED_CODE is used for
10195 the rest. */
10197 static tree
10198 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10199 enum tree_code unordered_code,
10200 enum tree_code ordered_code)
10202 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10203 enum tree_code code;
10204 tree type0, type1;
10205 enum tree_code code0, code1;
10206 tree cmp_type = NULL_TREE;
10208 type0 = TREE_TYPE (arg0);
10209 type1 = TREE_TYPE (arg1);
10211 code0 = TREE_CODE (type0);
10212 code1 = TREE_CODE (type1);
10214 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10215 /* Choose the wider of two real types. */
10216 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10217 ? type0 : type1;
10218 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10219 cmp_type = type0;
10220 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10221 cmp_type = type1;
10223 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10224 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10226 if (unordered_code == UNORDERED_EXPR)
10228 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10229 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10230 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10233 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10234 : ordered_code;
10235 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10236 fold_build2_loc (loc, code, type, arg0, arg1));
10239 /* Fold a call to built-in function FNDECL with 0 arguments.
10240 IGNORE is true if the result of the function call is ignored. This
10241 function returns NULL_TREE if no simplification was possible. */
10243 static tree
10244 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10246 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10247 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10248 switch (fcode)
10250 CASE_FLT_FN (BUILT_IN_INF):
10251 case BUILT_IN_INFD32:
10252 case BUILT_IN_INFD64:
10253 case BUILT_IN_INFD128:
10254 return fold_builtin_inf (loc, type, true);
10256 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10257 return fold_builtin_inf (loc, type, false);
10259 case BUILT_IN_CLASSIFY_TYPE:
10260 return fold_builtin_classify_type (NULL_TREE);
10262 default:
10263 break;
10265 return NULL_TREE;
10268 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10269 IGNORE is true if the result of the function call is ignored. This
10270 function returns NULL_TREE if no simplification was possible. */
10272 static tree
10273 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10275 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10276 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10277 switch (fcode)
10279 case BUILT_IN_CONSTANT_P:
10281 tree val = fold_builtin_constant_p (arg0);
10283 /* Gimplification will pull the CALL_EXPR for the builtin out of
10284 an if condition. When not optimizing, we'll not CSE it back.
10285 To avoid link error types of regressions, return false now. */
10286 if (!val && !optimize)
10287 val = integer_zero_node;
10289 return val;
10292 case BUILT_IN_CLASSIFY_TYPE:
10293 return fold_builtin_classify_type (arg0);
10295 case BUILT_IN_STRLEN:
10296 return fold_builtin_strlen (loc, type, arg0);
10298 CASE_FLT_FN (BUILT_IN_FABS):
10299 return fold_builtin_fabs (loc, arg0, type);
10301 case BUILT_IN_ABS:
10302 case BUILT_IN_LABS:
10303 case BUILT_IN_LLABS:
10304 case BUILT_IN_IMAXABS:
10305 return fold_builtin_abs (loc, arg0, type);
10307 CASE_FLT_FN (BUILT_IN_CONJ):
10308 if (validate_arg (arg0, COMPLEX_TYPE)
10309 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10310 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10311 break;
10313 CASE_FLT_FN (BUILT_IN_CREAL):
10314 if (validate_arg (arg0, COMPLEX_TYPE)
10315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10316 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10317 break;
10319 CASE_FLT_FN (BUILT_IN_CIMAG):
10320 if (validate_arg (arg0, COMPLEX_TYPE)
10321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10322 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10323 break;
10325 CASE_FLT_FN (BUILT_IN_CCOS):
10326 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10328 CASE_FLT_FN (BUILT_IN_CCOSH):
10329 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10331 CASE_FLT_FN (BUILT_IN_CPROJ):
10332 return fold_builtin_cproj(loc, arg0, type);
10334 CASE_FLT_FN (BUILT_IN_CSIN):
10335 if (validate_arg (arg0, COMPLEX_TYPE)
10336 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10337 return do_mpc_arg1 (arg0, type, mpc_sin);
10338 break;
10340 CASE_FLT_FN (BUILT_IN_CSINH):
10341 if (validate_arg (arg0, COMPLEX_TYPE)
10342 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10343 return do_mpc_arg1 (arg0, type, mpc_sinh);
10344 break;
10346 CASE_FLT_FN (BUILT_IN_CTAN):
10347 if (validate_arg (arg0, COMPLEX_TYPE)
10348 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10349 return do_mpc_arg1 (arg0, type, mpc_tan);
10350 break;
10352 CASE_FLT_FN (BUILT_IN_CTANH):
10353 if (validate_arg (arg0, COMPLEX_TYPE)
10354 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10355 return do_mpc_arg1 (arg0, type, mpc_tanh);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_CLOG):
10359 if (validate_arg (arg0, COMPLEX_TYPE)
10360 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10361 return do_mpc_arg1 (arg0, type, mpc_log);
10362 break;
10364 CASE_FLT_FN (BUILT_IN_CSQRT):
10365 if (validate_arg (arg0, COMPLEX_TYPE)
10366 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10367 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10368 break;
10370 CASE_FLT_FN (BUILT_IN_CASIN):
10371 if (validate_arg (arg0, COMPLEX_TYPE)
10372 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10373 return do_mpc_arg1 (arg0, type, mpc_asin);
10374 break;
10376 CASE_FLT_FN (BUILT_IN_CACOS):
10377 if (validate_arg (arg0, COMPLEX_TYPE)
10378 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10379 return do_mpc_arg1 (arg0, type, mpc_acos);
10380 break;
10382 CASE_FLT_FN (BUILT_IN_CATAN):
10383 if (validate_arg (arg0, COMPLEX_TYPE)
10384 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10385 return do_mpc_arg1 (arg0, type, mpc_atan);
10386 break;
10388 CASE_FLT_FN (BUILT_IN_CASINH):
10389 if (validate_arg (arg0, COMPLEX_TYPE)
10390 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10391 return do_mpc_arg1 (arg0, type, mpc_asinh);
10392 break;
10394 CASE_FLT_FN (BUILT_IN_CACOSH):
10395 if (validate_arg (arg0, COMPLEX_TYPE)
10396 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10397 return do_mpc_arg1 (arg0, type, mpc_acosh);
10398 break;
10400 CASE_FLT_FN (BUILT_IN_CATANH):
10401 if (validate_arg (arg0, COMPLEX_TYPE)
10402 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10403 return do_mpc_arg1 (arg0, type, mpc_atanh);
10404 break;
10406 CASE_FLT_FN (BUILT_IN_CABS):
10407 return fold_builtin_cabs (loc, arg0, type, fndecl);
10409 CASE_FLT_FN (BUILT_IN_CARG):
10410 return fold_builtin_carg (loc, arg0, type);
10412 CASE_FLT_FN (BUILT_IN_SQRT):
10413 return fold_builtin_sqrt (loc, arg0, type);
10415 CASE_FLT_FN (BUILT_IN_CBRT):
10416 return fold_builtin_cbrt (loc, arg0, type);
10418 CASE_FLT_FN (BUILT_IN_ASIN):
10419 if (validate_arg (arg0, REAL_TYPE))
10420 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10421 &dconstm1, &dconst1, true);
10422 break;
10424 CASE_FLT_FN (BUILT_IN_ACOS):
10425 if (validate_arg (arg0, REAL_TYPE))
10426 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10427 &dconstm1, &dconst1, true);
10428 break;
10430 CASE_FLT_FN (BUILT_IN_ATAN):
10431 if (validate_arg (arg0, REAL_TYPE))
10432 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10433 break;
10435 CASE_FLT_FN (BUILT_IN_ASINH):
10436 if (validate_arg (arg0, REAL_TYPE))
10437 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10438 break;
10440 CASE_FLT_FN (BUILT_IN_ACOSH):
10441 if (validate_arg (arg0, REAL_TYPE))
10442 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10443 &dconst1, NULL, true);
10444 break;
10446 CASE_FLT_FN (BUILT_IN_ATANH):
10447 if (validate_arg (arg0, REAL_TYPE))
10448 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10449 &dconstm1, &dconst1, false);
10450 break;
10452 CASE_FLT_FN (BUILT_IN_SIN):
10453 if (validate_arg (arg0, REAL_TYPE))
10454 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10455 break;
10457 CASE_FLT_FN (BUILT_IN_COS):
10458 return fold_builtin_cos (loc, arg0, type, fndecl);
10460 CASE_FLT_FN (BUILT_IN_TAN):
10461 return fold_builtin_tan (arg0, type);
10463 CASE_FLT_FN (BUILT_IN_CEXP):
10464 return fold_builtin_cexp (loc, arg0, type);
10466 CASE_FLT_FN (BUILT_IN_CEXPI):
10467 if (validate_arg (arg0, REAL_TYPE))
10468 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10469 break;
10471 CASE_FLT_FN (BUILT_IN_SINH):
10472 if (validate_arg (arg0, REAL_TYPE))
10473 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10474 break;
10476 CASE_FLT_FN (BUILT_IN_COSH):
10477 return fold_builtin_cosh (loc, arg0, type, fndecl);
10479 CASE_FLT_FN (BUILT_IN_TANH):
10480 if (validate_arg (arg0, REAL_TYPE))
10481 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10482 break;
10484 CASE_FLT_FN (BUILT_IN_ERF):
10485 if (validate_arg (arg0, REAL_TYPE))
10486 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10487 break;
10489 CASE_FLT_FN (BUILT_IN_ERFC):
10490 if (validate_arg (arg0, REAL_TYPE))
10491 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10492 break;
10494 CASE_FLT_FN (BUILT_IN_TGAMMA):
10495 if (validate_arg (arg0, REAL_TYPE))
10496 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10497 break;
10499 CASE_FLT_FN (BUILT_IN_EXP):
10500 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10502 CASE_FLT_FN (BUILT_IN_EXP2):
10503 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10505 CASE_FLT_FN (BUILT_IN_EXP10):
10506 CASE_FLT_FN (BUILT_IN_POW10):
10507 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10509 CASE_FLT_FN (BUILT_IN_EXPM1):
10510 if (validate_arg (arg0, REAL_TYPE))
10511 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10512 break;
10514 CASE_FLT_FN (BUILT_IN_LOG):
10515 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10517 CASE_FLT_FN (BUILT_IN_LOG2):
10518 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10520 CASE_FLT_FN (BUILT_IN_LOG10):
10521 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10523 CASE_FLT_FN (BUILT_IN_LOG1P):
10524 if (validate_arg (arg0, REAL_TYPE))
10525 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10526 &dconstm1, NULL, false);
10527 break;
10529 CASE_FLT_FN (BUILT_IN_J0):
10530 if (validate_arg (arg0, REAL_TYPE))
10531 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10532 NULL, NULL, 0);
10533 break;
10535 CASE_FLT_FN (BUILT_IN_J1):
10536 if (validate_arg (arg0, REAL_TYPE))
10537 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10538 NULL, NULL, 0);
10539 break;
10541 CASE_FLT_FN (BUILT_IN_Y0):
10542 if (validate_arg (arg0, REAL_TYPE))
10543 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10544 &dconst0, NULL, false);
10545 break;
10547 CASE_FLT_FN (BUILT_IN_Y1):
10548 if (validate_arg (arg0, REAL_TYPE))
10549 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10550 &dconst0, NULL, false);
10551 break;
10553 CASE_FLT_FN (BUILT_IN_NAN):
10554 case BUILT_IN_NAND32:
10555 case BUILT_IN_NAND64:
10556 case BUILT_IN_NAND128:
10557 return fold_builtin_nan (arg0, type, true);
10559 CASE_FLT_FN (BUILT_IN_NANS):
10560 return fold_builtin_nan (arg0, type, false);
10562 CASE_FLT_FN (BUILT_IN_FLOOR):
10563 return fold_builtin_floor (loc, fndecl, arg0);
10565 CASE_FLT_FN (BUILT_IN_CEIL):
10566 return fold_builtin_ceil (loc, fndecl, arg0);
10568 CASE_FLT_FN (BUILT_IN_TRUNC):
10569 return fold_builtin_trunc (loc, fndecl, arg0);
10571 CASE_FLT_FN (BUILT_IN_ROUND):
10572 return fold_builtin_round (loc, fndecl, arg0);
10574 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10575 CASE_FLT_FN (BUILT_IN_RINT):
10576 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10578 CASE_FLT_FN (BUILT_IN_ICEIL):
10579 CASE_FLT_FN (BUILT_IN_LCEIL):
10580 CASE_FLT_FN (BUILT_IN_LLCEIL):
10581 CASE_FLT_FN (BUILT_IN_LFLOOR):
10582 CASE_FLT_FN (BUILT_IN_IFLOOR):
10583 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10584 CASE_FLT_FN (BUILT_IN_IROUND):
10585 CASE_FLT_FN (BUILT_IN_LROUND):
10586 CASE_FLT_FN (BUILT_IN_LLROUND):
10587 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10589 CASE_FLT_FN (BUILT_IN_IRINT):
10590 CASE_FLT_FN (BUILT_IN_LRINT):
10591 CASE_FLT_FN (BUILT_IN_LLRINT):
10592 return fold_fixed_mathfn (loc, fndecl, arg0);
10594 case BUILT_IN_BSWAP16:
10595 case BUILT_IN_BSWAP32:
10596 case BUILT_IN_BSWAP64:
10597 return fold_builtin_bswap (fndecl, arg0);
10599 CASE_INT_FN (BUILT_IN_FFS):
10600 CASE_INT_FN (BUILT_IN_CLZ):
10601 CASE_INT_FN (BUILT_IN_CTZ):
10602 CASE_INT_FN (BUILT_IN_CLRSB):
10603 CASE_INT_FN (BUILT_IN_POPCOUNT):
10604 CASE_INT_FN (BUILT_IN_PARITY):
10605 return fold_builtin_bitop (fndecl, arg0);
10607 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10608 return fold_builtin_signbit (loc, arg0, type);
10610 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10611 return fold_builtin_significand (loc, arg0, type);
10613 CASE_FLT_FN (BUILT_IN_ILOGB):
10614 CASE_FLT_FN (BUILT_IN_LOGB):
10615 return fold_builtin_logb (loc, arg0, type);
10617 case BUILT_IN_ISASCII:
10618 return fold_builtin_isascii (loc, arg0);
10620 case BUILT_IN_TOASCII:
10621 return fold_builtin_toascii (loc, arg0);
10623 case BUILT_IN_ISDIGIT:
10624 return fold_builtin_isdigit (loc, arg0);
10626 CASE_FLT_FN (BUILT_IN_FINITE):
10627 case BUILT_IN_FINITED32:
10628 case BUILT_IN_FINITED64:
10629 case BUILT_IN_FINITED128:
10630 case BUILT_IN_ISFINITE:
10632 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10633 if (ret)
10634 return ret;
10635 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10638 CASE_FLT_FN (BUILT_IN_ISINF):
10639 case BUILT_IN_ISINFD32:
10640 case BUILT_IN_ISINFD64:
10641 case BUILT_IN_ISINFD128:
10643 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10644 if (ret)
10645 return ret;
10646 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10649 case BUILT_IN_ISNORMAL:
10650 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10652 case BUILT_IN_ISINF_SIGN:
10653 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10655 CASE_FLT_FN (BUILT_IN_ISNAN):
10656 case BUILT_IN_ISNAND32:
10657 case BUILT_IN_ISNAND64:
10658 case BUILT_IN_ISNAND128:
10659 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10661 case BUILT_IN_PRINTF:
10662 case BUILT_IN_PRINTF_UNLOCKED:
10663 case BUILT_IN_VPRINTF:
10664 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10666 case BUILT_IN_FREE:
10667 if (integer_zerop (arg0))
10668 return build_empty_stmt (loc);
10669 break;
10671 default:
10672 break;
10675 return NULL_TREE;
10679 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10680 IGNORE is true if the result of the function call is ignored. This
10681 function returns NULL_TREE if no simplification was possible. */
10683 static tree
10684 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10686 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10687 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10689 switch (fcode)
10691 CASE_FLT_FN (BUILT_IN_JN):
10692 if (validate_arg (arg0, INTEGER_TYPE)
10693 && validate_arg (arg1, REAL_TYPE))
10694 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10695 break;
10697 CASE_FLT_FN (BUILT_IN_YN):
10698 if (validate_arg (arg0, INTEGER_TYPE)
10699 && validate_arg (arg1, REAL_TYPE))
10700 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10701 &dconst0, false);
10702 break;
10704 CASE_FLT_FN (BUILT_IN_DREM):
10705 CASE_FLT_FN (BUILT_IN_REMAINDER):
10706 if (validate_arg (arg0, REAL_TYPE)
10707 && validate_arg(arg1, REAL_TYPE))
10708 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10709 break;
10711 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10712 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10713 if (validate_arg (arg0, REAL_TYPE)
10714 && validate_arg(arg1, POINTER_TYPE))
10715 return do_mpfr_lgamma_r (arg0, arg1, type);
10716 break;
10718 CASE_FLT_FN (BUILT_IN_ATAN2):
10719 if (validate_arg (arg0, REAL_TYPE)
10720 && validate_arg(arg1, REAL_TYPE))
10721 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10722 break;
10724 CASE_FLT_FN (BUILT_IN_FDIM):
10725 if (validate_arg (arg0, REAL_TYPE)
10726 && validate_arg(arg1, REAL_TYPE))
10727 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10728 break;
10730 CASE_FLT_FN (BUILT_IN_HYPOT):
10731 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10733 CASE_FLT_FN (BUILT_IN_CPOW):
10734 if (validate_arg (arg0, COMPLEX_TYPE)
10735 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10736 && validate_arg (arg1, COMPLEX_TYPE)
10737 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10738 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10739 break;
10741 CASE_FLT_FN (BUILT_IN_LDEXP):
10742 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10743 CASE_FLT_FN (BUILT_IN_SCALBN):
10744 CASE_FLT_FN (BUILT_IN_SCALBLN):
10745 return fold_builtin_load_exponent (loc, arg0, arg1,
10746 type, /*ldexp=*/false);
10748 CASE_FLT_FN (BUILT_IN_FREXP):
10749 return fold_builtin_frexp (loc, arg0, arg1, type);
10751 CASE_FLT_FN (BUILT_IN_MODF):
10752 return fold_builtin_modf (loc, arg0, arg1, type);
10754 case BUILT_IN_BZERO:
10755 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10757 case BUILT_IN_FPUTS:
10758 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10760 case BUILT_IN_FPUTS_UNLOCKED:
10761 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10763 case BUILT_IN_STRSTR:
10764 return fold_builtin_strstr (loc, arg0, arg1, type);
10766 case BUILT_IN_STRCAT:
10767 return fold_builtin_strcat (loc, arg0, arg1);
10769 case BUILT_IN_STRSPN:
10770 return fold_builtin_strspn (loc, arg0, arg1);
10772 case BUILT_IN_STRCSPN:
10773 return fold_builtin_strcspn (loc, arg0, arg1);
10775 case BUILT_IN_STRCHR:
10776 case BUILT_IN_INDEX:
10777 return fold_builtin_strchr (loc, arg0, arg1, type);
10779 case BUILT_IN_STRRCHR:
10780 case BUILT_IN_RINDEX:
10781 return fold_builtin_strrchr (loc, arg0, arg1, type);
10783 case BUILT_IN_STRCPY:
10784 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10786 case BUILT_IN_STPCPY:
10787 if (ignore)
10789 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10790 if (!fn)
10791 break;
10793 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10795 else
10796 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10797 break;
10799 case BUILT_IN_STRCMP:
10800 return fold_builtin_strcmp (loc, arg0, arg1);
10802 case BUILT_IN_STRPBRK:
10803 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10805 case BUILT_IN_EXPECT:
10806 return fold_builtin_expect (loc, arg0, arg1);
10808 CASE_FLT_FN (BUILT_IN_POW):
10809 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10811 CASE_FLT_FN (BUILT_IN_POWI):
10812 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10814 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10815 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10817 CASE_FLT_FN (BUILT_IN_FMIN):
10818 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10820 CASE_FLT_FN (BUILT_IN_FMAX):
10821 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10823 case BUILT_IN_ISGREATER:
10824 return fold_builtin_unordered_cmp (loc, fndecl,
10825 arg0, arg1, UNLE_EXPR, LE_EXPR);
10826 case BUILT_IN_ISGREATEREQUAL:
10827 return fold_builtin_unordered_cmp (loc, fndecl,
10828 arg0, arg1, UNLT_EXPR, LT_EXPR);
10829 case BUILT_IN_ISLESS:
10830 return fold_builtin_unordered_cmp (loc, fndecl,
10831 arg0, arg1, UNGE_EXPR, GE_EXPR);
10832 case BUILT_IN_ISLESSEQUAL:
10833 return fold_builtin_unordered_cmp (loc, fndecl,
10834 arg0, arg1, UNGT_EXPR, GT_EXPR);
10835 case BUILT_IN_ISLESSGREATER:
10836 return fold_builtin_unordered_cmp (loc, fndecl,
10837 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10838 case BUILT_IN_ISUNORDERED:
10839 return fold_builtin_unordered_cmp (loc, fndecl,
10840 arg0, arg1, UNORDERED_EXPR,
10841 NOP_EXPR);
10843 /* We do the folding for va_start in the expander. */
10844 case BUILT_IN_VA_START:
10845 break;
10847 case BUILT_IN_SPRINTF:
10848 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10850 case BUILT_IN_OBJECT_SIZE:
10851 return fold_builtin_object_size (arg0, arg1);
10853 case BUILT_IN_PRINTF:
10854 case BUILT_IN_PRINTF_UNLOCKED:
10855 case BUILT_IN_VPRINTF:
10856 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10858 case BUILT_IN_PRINTF_CHK:
10859 case BUILT_IN_VPRINTF_CHK:
10860 if (!validate_arg (arg0, INTEGER_TYPE)
10861 || TREE_SIDE_EFFECTS (arg0))
10862 return NULL_TREE;
10863 else
10864 return fold_builtin_printf (loc, fndecl,
10865 arg1, NULL_TREE, ignore, fcode);
10866 break;
10868 case BUILT_IN_FPRINTF:
10869 case BUILT_IN_FPRINTF_UNLOCKED:
10870 case BUILT_IN_VFPRINTF:
10871 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10872 ignore, fcode);
10874 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10875 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10877 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10878 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10880 default:
10881 break;
10883 return NULL_TREE;
10886 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10887 and ARG2. IGNORE is true if the result of the function call is ignored.
10888 This function returns NULL_TREE if no simplification was possible. */
10890 static tree
10891 fold_builtin_3 (location_t loc, tree fndecl,
10892 tree arg0, tree arg1, tree arg2, bool ignore)
10894 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10895 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10896 switch (fcode)
10899 CASE_FLT_FN (BUILT_IN_SINCOS):
10900 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10902 CASE_FLT_FN (BUILT_IN_FMA):
10903 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10904 break;
10906 CASE_FLT_FN (BUILT_IN_REMQUO):
10907 if (validate_arg (arg0, REAL_TYPE)
10908 && validate_arg(arg1, REAL_TYPE)
10909 && validate_arg(arg2, POINTER_TYPE))
10910 return do_mpfr_remquo (arg0, arg1, arg2);
10911 break;
10913 case BUILT_IN_MEMSET:
10914 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10916 case BUILT_IN_BCOPY:
10917 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10918 void_type_node, true, /*endp=*/3);
10920 case BUILT_IN_MEMCPY:
10921 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10922 type, ignore, /*endp=*/0);
10924 case BUILT_IN_MEMPCPY:
10925 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10926 type, ignore, /*endp=*/1);
10928 case BUILT_IN_MEMMOVE:
10929 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10930 type, ignore, /*endp=*/3);
10932 case BUILT_IN_STRNCAT:
10933 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10935 case BUILT_IN_STRNCPY:
10936 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10938 case BUILT_IN_STRNCMP:
10939 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10941 case BUILT_IN_MEMCHR:
10942 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10944 case BUILT_IN_BCMP:
10945 case BUILT_IN_MEMCMP:
10946 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10948 case BUILT_IN_SPRINTF:
10949 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10951 case BUILT_IN_SNPRINTF:
10952 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10954 case BUILT_IN_STRCPY_CHK:
10955 case BUILT_IN_STPCPY_CHK:
10956 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10957 ignore, fcode);
10959 case BUILT_IN_STRCAT_CHK:
10960 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10962 case BUILT_IN_PRINTF_CHK:
10963 case BUILT_IN_VPRINTF_CHK:
10964 if (!validate_arg (arg0, INTEGER_TYPE)
10965 || TREE_SIDE_EFFECTS (arg0))
10966 return NULL_TREE;
10967 else
10968 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10969 break;
10971 case BUILT_IN_FPRINTF:
10972 case BUILT_IN_FPRINTF_UNLOCKED:
10973 case BUILT_IN_VFPRINTF:
10974 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10975 ignore, fcode);
10977 case BUILT_IN_FPRINTF_CHK:
10978 case BUILT_IN_VFPRINTF_CHK:
10979 if (!validate_arg (arg1, INTEGER_TYPE)
10980 || TREE_SIDE_EFFECTS (arg1))
10981 return NULL_TREE;
10982 else
10983 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10984 ignore, fcode);
10986 default:
10987 break;
10989 return NULL_TREE;
10992 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10993 ARG2, and ARG3. IGNORE is true if the result of the function call is
10994 ignored. This function returns NULL_TREE if no simplification was
10995 possible. */
10997 static tree
10998 fold_builtin_4 (location_t loc, tree fndecl,
10999 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11001 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11003 switch (fcode)
11005 case BUILT_IN_MEMCPY_CHK:
11006 case BUILT_IN_MEMPCPY_CHK:
11007 case BUILT_IN_MEMMOVE_CHK:
11008 case BUILT_IN_MEMSET_CHK:
11009 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11010 NULL_TREE, ignore,
11011 DECL_FUNCTION_CODE (fndecl));
11013 case BUILT_IN_STRNCPY_CHK:
11014 case BUILT_IN_STPNCPY_CHK:
11015 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11016 ignore, fcode);
11018 case BUILT_IN_STRNCAT_CHK:
11019 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11021 case BUILT_IN_SNPRINTF:
11022 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11024 case BUILT_IN_FPRINTF_CHK:
11025 case BUILT_IN_VFPRINTF_CHK:
11026 if (!validate_arg (arg1, INTEGER_TYPE)
11027 || TREE_SIDE_EFFECTS (arg1))
11028 return NULL_TREE;
11029 else
11030 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11031 ignore, fcode);
11032 break;
11034 default:
11035 break;
11037 return NULL_TREE;
11040 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11041 arguments, where NARGS <= 4. IGNORE is true if the result of the
11042 function call is ignored. This function returns NULL_TREE if no
11043 simplification was possible. Note that this only folds builtins with
11044 fixed argument patterns. Foldings that do varargs-to-varargs
11045 transformations, or that match calls with more than 4 arguments,
11046 need to be handled with fold_builtin_varargs instead. */
11048 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11050 static tree
11051 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11053 tree ret = NULL_TREE;
11055 switch (nargs)
11057 case 0:
11058 ret = fold_builtin_0 (loc, fndecl, ignore);
11059 break;
11060 case 1:
11061 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11062 break;
11063 case 2:
11064 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11065 break;
11066 case 3:
11067 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11068 break;
11069 case 4:
11070 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11071 ignore);
11072 break;
11073 default:
11074 break;
11076 if (ret)
11078 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11079 SET_EXPR_LOCATION (ret, loc);
11080 TREE_NO_WARNING (ret) = 1;
11081 return ret;
11083 return NULL_TREE;
11086 /* Builtins with folding operations that operate on "..." arguments
11087 need special handling; we need to store the arguments in a convenient
11088 data structure before attempting any folding. Fortunately there are
11089 only a few builtins that fall into this category. FNDECL is the
11090 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11091 result of the function call is ignored. */
11093 static tree
11094 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11095 bool ignore ATTRIBUTE_UNUSED)
11097 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11098 tree ret = NULL_TREE;
11100 switch (fcode)
11102 case BUILT_IN_SPRINTF_CHK:
11103 case BUILT_IN_VSPRINTF_CHK:
11104 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11105 break;
11107 case BUILT_IN_SNPRINTF_CHK:
11108 case BUILT_IN_VSNPRINTF_CHK:
11109 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11110 break;
11112 case BUILT_IN_FPCLASSIFY:
11113 ret = fold_builtin_fpclassify (loc, exp);
11114 break;
11116 default:
11117 break;
11119 if (ret)
11121 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11122 SET_EXPR_LOCATION (ret, loc);
11123 TREE_NO_WARNING (ret) = 1;
11124 return ret;
11126 return NULL_TREE;
11129 /* Return true if FNDECL shouldn't be folded right now.
11130 If a built-in function has an inline attribute always_inline
11131 wrapper, defer folding it after always_inline functions have
11132 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11133 might not be performed. */
11135 bool
11136 avoid_folding_inline_builtin (tree fndecl)
11138 return (DECL_DECLARED_INLINE_P (fndecl)
11139 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11140 && cfun
11141 && !cfun->always_inline_functions_inlined
11142 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11145 /* A wrapper function for builtin folding that prevents warnings for
11146 "statement without effect" and the like, caused by removing the
11147 call node earlier than the warning is generated. */
11149 tree
11150 fold_call_expr (location_t loc, tree exp, bool ignore)
11152 tree ret = NULL_TREE;
11153 tree fndecl = get_callee_fndecl (exp);
11154 if (fndecl
11155 && TREE_CODE (fndecl) == FUNCTION_DECL
11156 && DECL_BUILT_IN (fndecl)
11157 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11158 yet. Defer folding until we see all the arguments
11159 (after inlining). */
11160 && !CALL_EXPR_VA_ARG_PACK (exp))
11162 int nargs = call_expr_nargs (exp);
11164 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11165 instead last argument is __builtin_va_arg_pack (). Defer folding
11166 even in that case, until arguments are finalized. */
11167 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11169 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11170 if (fndecl2
11171 && TREE_CODE (fndecl2) == FUNCTION_DECL
11172 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11173 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11174 return NULL_TREE;
11177 if (avoid_folding_inline_builtin (fndecl))
11178 return NULL_TREE;
11180 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11181 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11182 CALL_EXPR_ARGP (exp), ignore);
11183 else
11185 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11187 tree *args = CALL_EXPR_ARGP (exp);
11188 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11190 if (!ret)
11191 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11192 if (ret)
11193 return ret;
11196 return NULL_TREE;
11199 /* Conveniently construct a function call expression. FNDECL names the
11200 function to be called and N arguments are passed in the array
11201 ARGARRAY. */
11203 tree
11204 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11206 tree fntype = TREE_TYPE (fndecl);
11207 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11209 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11212 /* Conveniently construct a function call expression. FNDECL names the
11213 function to be called and the arguments are passed in the vector
11214 VEC. */
11216 tree
11217 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11219 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11220 vec_safe_address (vec));
11224 /* Conveniently construct a function call expression. FNDECL names the
11225 function to be called, N is the number of arguments, and the "..."
11226 parameters are the argument expressions. */
11228 tree
11229 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11231 va_list ap;
11232 tree *argarray = XALLOCAVEC (tree, n);
11233 int i;
11235 va_start (ap, n);
11236 for (i = 0; i < n; i++)
11237 argarray[i] = va_arg (ap, tree);
11238 va_end (ap);
11239 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11242 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11243 varargs macros aren't supported by all bootstrap compilers. */
11245 tree
11246 build_call_expr (tree fndecl, int n, ...)
11248 va_list ap;
11249 tree *argarray = XALLOCAVEC (tree, n);
11250 int i;
11252 va_start (ap, n);
11253 for (i = 0; i < n; i++)
11254 argarray[i] = va_arg (ap, tree);
11255 va_end (ap);
11256 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11259 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11260 N arguments are passed in the array ARGARRAY. */
11262 tree
11263 fold_builtin_call_array (location_t loc, tree type,
11264 tree fn,
11265 int n,
11266 tree *argarray)
11268 tree ret = NULL_TREE;
11269 tree exp;
11271 if (TREE_CODE (fn) == ADDR_EXPR)
11273 tree fndecl = TREE_OPERAND (fn, 0);
11274 if (TREE_CODE (fndecl) == FUNCTION_DECL
11275 && DECL_BUILT_IN (fndecl))
11277 /* If last argument is __builtin_va_arg_pack (), arguments to this
11278 function are not finalized yet. Defer folding until they are. */
11279 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11281 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11282 if (fndecl2
11283 && TREE_CODE (fndecl2) == FUNCTION_DECL
11284 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11285 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11286 return build_call_array_loc (loc, type, fn, n, argarray);
11288 if (avoid_folding_inline_builtin (fndecl))
11289 return build_call_array_loc (loc, type, fn, n, argarray);
11290 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11292 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11293 if (ret)
11294 return ret;
11296 return build_call_array_loc (loc, type, fn, n, argarray);
11298 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11300 /* First try the transformations that don't require consing up
11301 an exp. */
11302 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11303 if (ret)
11304 return ret;
11307 /* If we got this far, we need to build an exp. */
11308 exp = build_call_array_loc (loc, type, fn, n, argarray);
11309 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11310 return ret ? ret : exp;
11314 return build_call_array_loc (loc, type, fn, n, argarray);
11317 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11318 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11319 of arguments in ARGS to be omitted. OLDNARGS is the number of
11320 elements in ARGS. */
11322 static tree
11323 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11324 int skip, tree fndecl, int n, va_list newargs)
11326 int nargs = oldnargs - skip + n;
11327 tree *buffer;
11329 if (n > 0)
11331 int i, j;
11333 buffer = XALLOCAVEC (tree, nargs);
11334 for (i = 0; i < n; i++)
11335 buffer[i] = va_arg (newargs, tree);
11336 for (j = skip; j < oldnargs; j++, i++)
11337 buffer[i] = args[j];
11339 else
11340 buffer = args + skip;
11342 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11345 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11346 list ARGS along with N new arguments specified as the "..."
11347 parameters. SKIP is the number of arguments in ARGS to be omitted.
11348 OLDNARGS is the number of elements in ARGS. */
11350 static tree
11351 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11352 int skip, tree fndecl, int n, ...)
11354 va_list ap;
11355 tree t;
11357 va_start (ap, n);
11358 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11359 va_end (ap);
11361 return t;
11364 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11365 along with N new arguments specified as the "..." parameters. SKIP
11366 is the number of arguments in EXP to be omitted. This function is used
11367 to do varargs-to-varargs transformations. */
11369 static tree
11370 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11372 va_list ap;
11373 tree t;
11375 va_start (ap, n);
11376 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11377 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11378 va_end (ap);
11380 return t;
11383 /* Validate a single argument ARG against a tree code CODE representing
11384 a type. */
11386 static bool
11387 validate_arg (const_tree arg, enum tree_code code)
11389 if (!arg)
11390 return false;
11391 else if (code == POINTER_TYPE)
11392 return POINTER_TYPE_P (TREE_TYPE (arg));
11393 else if (code == INTEGER_TYPE)
11394 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11395 return code == TREE_CODE (TREE_TYPE (arg));
11398 /* This function validates the types of a function call argument list
11399 against a specified list of tree_codes. If the last specifier is a 0,
11400 that represents an ellipses, otherwise the last specifier must be a
11401 VOID_TYPE.
11403 This is the GIMPLE version of validate_arglist. Eventually we want to
11404 completely convert builtins.c to work from GIMPLEs and the tree based
11405 validate_arglist will then be removed. */
11407 bool
11408 validate_gimple_arglist (const_gimple call, ...)
11410 enum tree_code code;
11411 bool res = 0;
11412 va_list ap;
11413 const_tree arg;
11414 size_t i;
11416 va_start (ap, call);
11417 i = 0;
11421 code = (enum tree_code) va_arg (ap, int);
11422 switch (code)
11424 case 0:
11425 /* This signifies an ellipses, any further arguments are all ok. */
11426 res = true;
11427 goto end;
11428 case VOID_TYPE:
11429 /* This signifies an endlink, if no arguments remain, return
11430 true, otherwise return false. */
11431 res = (i == gimple_call_num_args (call));
11432 goto end;
11433 default:
11434 /* If no parameters remain or the parameter's code does not
11435 match the specified code, return false. Otherwise continue
11436 checking any remaining arguments. */
11437 arg = gimple_call_arg (call, i++);
11438 if (!validate_arg (arg, code))
11439 goto end;
11440 break;
11443 while (1);
11445 /* We need gotos here since we can only have one VA_CLOSE in a
11446 function. */
11447 end: ;
11448 va_end (ap);
11450 return res;
11453 /* This function validates the types of a function call argument list
11454 against a specified list of tree_codes. If the last specifier is a 0,
11455 that represents an ellipses, otherwise the last specifier must be a
11456 VOID_TYPE. */
11458 bool
11459 validate_arglist (const_tree callexpr, ...)
11461 enum tree_code code;
11462 bool res = 0;
11463 va_list ap;
11464 const_call_expr_arg_iterator iter;
11465 const_tree arg;
11467 va_start (ap, callexpr);
11468 init_const_call_expr_arg_iterator (callexpr, &iter);
11472 code = (enum tree_code) va_arg (ap, int);
11473 switch (code)
11475 case 0:
11476 /* This signifies an ellipses, any further arguments are all ok. */
11477 res = true;
11478 goto end;
11479 case VOID_TYPE:
11480 /* This signifies an endlink, if no arguments remain, return
11481 true, otherwise return false. */
11482 res = !more_const_call_expr_args_p (&iter);
11483 goto end;
11484 default:
11485 /* If no parameters remain or the parameter's code does not
11486 match the specified code, return false. Otherwise continue
11487 checking any remaining arguments. */
11488 arg = next_const_call_expr_arg (&iter);
11489 if (!validate_arg (arg, code))
11490 goto end;
11491 break;
11494 while (1);
11496 /* We need gotos here since we can only have one VA_CLOSE in a
11497 function. */
11498 end: ;
11499 va_end (ap);
11501 return res;
11504 /* Default target-specific builtin expander that does nothing. */
11507 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11508 rtx target ATTRIBUTE_UNUSED,
11509 rtx subtarget ATTRIBUTE_UNUSED,
11510 enum machine_mode mode ATTRIBUTE_UNUSED,
11511 int ignore ATTRIBUTE_UNUSED)
11513 return NULL_RTX;
11516 /* Returns true is EXP represents data that would potentially reside
11517 in a readonly section. */
11519 static bool
11520 readonly_data_expr (tree exp)
11522 STRIP_NOPS (exp);
11524 if (TREE_CODE (exp) != ADDR_EXPR)
11525 return false;
11527 exp = get_base_address (TREE_OPERAND (exp, 0));
11528 if (!exp)
11529 return false;
11531 /* Make sure we call decl_readonly_section only for trees it
11532 can handle (since it returns true for everything it doesn't
11533 understand). */
11534 if (TREE_CODE (exp) == STRING_CST
11535 || TREE_CODE (exp) == CONSTRUCTOR
11536 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11537 return decl_readonly_section (exp, 0);
11538 else
11539 return false;
11542 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11543 to the call, and TYPE is its return type.
11545 Return NULL_TREE if no simplification was possible, otherwise return the
11546 simplified form of the call as a tree.
11548 The simplified form may be a constant or other expression which
11549 computes the same value, but in a more efficient manner (including
11550 calls to other builtin functions).
11552 The call may contain arguments which need to be evaluated, but
11553 which are not useful to determine the result of the call. In
11554 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11555 COMPOUND_EXPR will be an argument which must be evaluated.
11556 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11557 COMPOUND_EXPR in the chain will contain the tree for the simplified
11558 form of the builtin function call. */
11560 static tree
11561 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11563 if (!validate_arg (s1, POINTER_TYPE)
11564 || !validate_arg (s2, POINTER_TYPE))
11565 return NULL_TREE;
11566 else
11568 tree fn;
11569 const char *p1, *p2;
11571 p2 = c_getstr (s2);
11572 if (p2 == NULL)
11573 return NULL_TREE;
11575 p1 = c_getstr (s1);
11576 if (p1 != NULL)
11578 const char *r = strstr (p1, p2);
11579 tree tem;
11581 if (r == NULL)
11582 return build_int_cst (TREE_TYPE (s1), 0);
11584 /* Return an offset into the constant string argument. */
11585 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11586 return fold_convert_loc (loc, type, tem);
11589 /* The argument is const char *, and the result is char *, so we need
11590 a type conversion here to avoid a warning. */
11591 if (p2[0] == '\0')
11592 return fold_convert_loc (loc, type, s1);
11594 if (p2[1] != '\0')
11595 return NULL_TREE;
11597 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11598 if (!fn)
11599 return NULL_TREE;
11601 /* New argument list transforming strstr(s1, s2) to
11602 strchr(s1, s2[0]). */
11603 return build_call_expr_loc (loc, fn, 2, s1,
11604 build_int_cst (integer_type_node, p2[0]));
11608 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11609 the call, and TYPE is its return type.
11611 Return NULL_TREE if no simplification was possible, otherwise return the
11612 simplified form of the call as a tree.
11614 The simplified form may be a constant or other expression which
11615 computes the same value, but in a more efficient manner (including
11616 calls to other builtin functions).
11618 The call may contain arguments which need to be evaluated, but
11619 which are not useful to determine the result of the call. In
11620 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11621 COMPOUND_EXPR will be an argument which must be evaluated.
11622 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11623 COMPOUND_EXPR in the chain will contain the tree for the simplified
11624 form of the builtin function call. */
11626 static tree
11627 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11629 if (!validate_arg (s1, POINTER_TYPE)
11630 || !validate_arg (s2, INTEGER_TYPE))
11631 return NULL_TREE;
11632 else
11634 const char *p1;
11636 if (TREE_CODE (s2) != INTEGER_CST)
11637 return NULL_TREE;
11639 p1 = c_getstr (s1);
11640 if (p1 != NULL)
11642 char c;
11643 const char *r;
11644 tree tem;
11646 if (target_char_cast (s2, &c))
11647 return NULL_TREE;
11649 r = strchr (p1, c);
11651 if (r == NULL)
11652 return build_int_cst (TREE_TYPE (s1), 0);
11654 /* Return an offset into the constant string argument. */
11655 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11656 return fold_convert_loc (loc, type, tem);
11658 return NULL_TREE;
11662 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11663 the call, and TYPE is its return type.
11665 Return NULL_TREE if no simplification was possible, otherwise return the
11666 simplified form of the call as a tree.
11668 The simplified form may be a constant or other expression which
11669 computes the same value, but in a more efficient manner (including
11670 calls to other builtin functions).
11672 The call may contain arguments which need to be evaluated, but
11673 which are not useful to determine the result of the call. In
11674 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11675 COMPOUND_EXPR will be an argument which must be evaluated.
11676 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11677 COMPOUND_EXPR in the chain will contain the tree for the simplified
11678 form of the builtin function call. */
11680 static tree
11681 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11683 if (!validate_arg (s1, POINTER_TYPE)
11684 || !validate_arg (s2, INTEGER_TYPE))
11685 return NULL_TREE;
11686 else
11688 tree fn;
11689 const char *p1;
11691 if (TREE_CODE (s2) != INTEGER_CST)
11692 return NULL_TREE;
11694 p1 = c_getstr (s1);
11695 if (p1 != NULL)
11697 char c;
11698 const char *r;
11699 tree tem;
11701 if (target_char_cast (s2, &c))
11702 return NULL_TREE;
11704 r = strrchr (p1, c);
11706 if (r == NULL)
11707 return build_int_cst (TREE_TYPE (s1), 0);
11709 /* Return an offset into the constant string argument. */
11710 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11711 return fold_convert_loc (loc, type, tem);
11714 if (! integer_zerop (s2))
11715 return NULL_TREE;
11717 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11718 if (!fn)
11719 return NULL_TREE;
11721 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11722 return build_call_expr_loc (loc, fn, 2, s1, s2);
11726 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11727 to the call, and TYPE is its return type.
11729 Return NULL_TREE if no simplification was possible, otherwise return the
11730 simplified form of the call as a tree.
11732 The simplified form may be a constant or other expression which
11733 computes the same value, but in a more efficient manner (including
11734 calls to other builtin functions).
11736 The call may contain arguments which need to be evaluated, but
11737 which are not useful to determine the result of the call. In
11738 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11739 COMPOUND_EXPR will be an argument which must be evaluated.
11740 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11741 COMPOUND_EXPR in the chain will contain the tree for the simplified
11742 form of the builtin function call. */
11744 static tree
11745 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11747 if (!validate_arg (s1, POINTER_TYPE)
11748 || !validate_arg (s2, POINTER_TYPE))
11749 return NULL_TREE;
11750 else
11752 tree fn;
11753 const char *p1, *p2;
11755 p2 = c_getstr (s2);
11756 if (p2 == NULL)
11757 return NULL_TREE;
11759 p1 = c_getstr (s1);
11760 if (p1 != NULL)
11762 const char *r = strpbrk (p1, p2);
11763 tree tem;
11765 if (r == NULL)
11766 return build_int_cst (TREE_TYPE (s1), 0);
11768 /* Return an offset into the constant string argument. */
11769 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11770 return fold_convert_loc (loc, type, tem);
11773 if (p2[0] == '\0')
11774 /* strpbrk(x, "") == NULL.
11775 Evaluate and ignore s1 in case it had side-effects. */
11776 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11778 if (p2[1] != '\0')
11779 return NULL_TREE; /* Really call strpbrk. */
11781 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11782 if (!fn)
11783 return NULL_TREE;
11785 /* New argument list transforming strpbrk(s1, s2) to
11786 strchr(s1, s2[0]). */
11787 return build_call_expr_loc (loc, fn, 2, s1,
11788 build_int_cst (integer_type_node, p2[0]));
11792 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11793 to the call.
11795 Return NULL_TREE if no simplification was possible, otherwise return the
11796 simplified form of the call as a tree.
11798 The simplified form may be a constant or other expression which
11799 computes the same value, but in a more efficient manner (including
11800 calls to other builtin functions).
11802 The call may contain arguments which need to be evaluated, but
11803 which are not useful to determine the result of the call. In
11804 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11805 COMPOUND_EXPR will be an argument which must be evaluated.
11806 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11807 COMPOUND_EXPR in the chain will contain the tree for the simplified
11808 form of the builtin function call. */
11810 static tree
11811 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11813 if (!validate_arg (dst, POINTER_TYPE)
11814 || !validate_arg (src, POINTER_TYPE))
11815 return NULL_TREE;
11816 else
11818 const char *p = c_getstr (src);
11820 /* If the string length is zero, return the dst parameter. */
11821 if (p && *p == '\0')
11822 return dst;
11824 if (optimize_insn_for_speed_p ())
11826 /* See if we can store by pieces into (dst + strlen(dst)). */
11827 tree newdst, call;
11828 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11829 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11831 if (!strlen_fn || !strcpy_fn)
11832 return NULL_TREE;
11834 /* If we don't have a movstr we don't want to emit an strcpy
11835 call. We have to do that if the length of the source string
11836 isn't computable (in that case we can use memcpy probably
11837 later expanding to a sequence of mov instructions). If we
11838 have movstr instructions we can emit strcpy calls. */
11839 if (!HAVE_movstr)
11841 tree len = c_strlen (src, 1);
11842 if (! len || TREE_SIDE_EFFECTS (len))
11843 return NULL_TREE;
11846 /* Stabilize the argument list. */
11847 dst = builtin_save_expr (dst);
11849 /* Create strlen (dst). */
11850 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11851 /* Create (dst p+ strlen (dst)). */
11853 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11854 newdst = builtin_save_expr (newdst);
11856 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11857 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11859 return NULL_TREE;
11863 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11864 arguments to the call.
11866 Return NULL_TREE if no simplification was possible, otherwise return the
11867 simplified form of the call as a tree.
11869 The simplified form may be a constant or other expression which
11870 computes the same value, but in a more efficient manner (including
11871 calls to other builtin functions).
11873 The call may contain arguments which need to be evaluated, but
11874 which are not useful to determine the result of the call. In
11875 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11876 COMPOUND_EXPR will be an argument which must be evaluated.
11877 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11878 COMPOUND_EXPR in the chain will contain the tree for the simplified
11879 form of the builtin function call. */
11881 static tree
11882 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11884 if (!validate_arg (dst, POINTER_TYPE)
11885 || !validate_arg (src, POINTER_TYPE)
11886 || !validate_arg (len, INTEGER_TYPE))
11887 return NULL_TREE;
11888 else
11890 const char *p = c_getstr (src);
11892 /* If the requested length is zero, or the src parameter string
11893 length is zero, return the dst parameter. */
11894 if (integer_zerop (len) || (p && *p == '\0'))
11895 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11897 /* If the requested len is greater than or equal to the string
11898 length, call strcat. */
11899 if (TREE_CODE (len) == INTEGER_CST && p
11900 && compare_tree_int (len, strlen (p)) >= 0)
11902 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11904 /* If the replacement _DECL isn't initialized, don't do the
11905 transformation. */
11906 if (!fn)
11907 return NULL_TREE;
11909 return build_call_expr_loc (loc, fn, 2, dst, src);
11911 return NULL_TREE;
11915 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11916 to the call.
11918 Return NULL_TREE if no simplification was possible, otherwise return the
11919 simplified form of the call as a tree.
11921 The simplified form may be a constant or other expression which
11922 computes the same value, but in a more efficient manner (including
11923 calls to other builtin functions).
11925 The call may contain arguments which need to be evaluated, but
11926 which are not useful to determine the result of the call. In
11927 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11928 COMPOUND_EXPR will be an argument which must be evaluated.
11929 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11930 COMPOUND_EXPR in the chain will contain the tree for the simplified
11931 form of the builtin function call. */
11933 static tree
11934 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11936 if (!validate_arg (s1, POINTER_TYPE)
11937 || !validate_arg (s2, POINTER_TYPE))
11938 return NULL_TREE;
11939 else
11941 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11943 /* If both arguments are constants, evaluate at compile-time. */
11944 if (p1 && p2)
11946 const size_t r = strspn (p1, p2);
11947 return build_int_cst (size_type_node, r);
11950 /* If either argument is "", return NULL_TREE. */
11951 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11952 /* Evaluate and ignore both arguments in case either one has
11953 side-effects. */
11954 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11955 s1, s2);
11956 return NULL_TREE;
11960 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11961 to the call.
11963 Return NULL_TREE if no simplification was possible, otherwise return the
11964 simplified form of the call as a tree.
11966 The simplified form may be a constant or other expression which
11967 computes the same value, but in a more efficient manner (including
11968 calls to other builtin functions).
11970 The call may contain arguments which need to be evaluated, but
11971 which are not useful to determine the result of the call. In
11972 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11973 COMPOUND_EXPR will be an argument which must be evaluated.
11974 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11975 COMPOUND_EXPR in the chain will contain the tree for the simplified
11976 form of the builtin function call. */
11978 static tree
11979 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11981 if (!validate_arg (s1, POINTER_TYPE)
11982 || !validate_arg (s2, POINTER_TYPE))
11983 return NULL_TREE;
11984 else
11986 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11988 /* If both arguments are constants, evaluate at compile-time. */
11989 if (p1 && p2)
11991 const size_t r = strcspn (p1, p2);
11992 return build_int_cst (size_type_node, r);
11995 /* If the first argument is "", return NULL_TREE. */
11996 if (p1 && *p1 == '\0')
11998 /* Evaluate and ignore argument s2 in case it has
11999 side-effects. */
12000 return omit_one_operand_loc (loc, size_type_node,
12001 size_zero_node, s2);
12004 /* If the second argument is "", return __builtin_strlen(s1). */
12005 if (p2 && *p2 == '\0')
12007 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
12009 /* If the replacement _DECL isn't initialized, don't do the
12010 transformation. */
12011 if (!fn)
12012 return NULL_TREE;
12014 return build_call_expr_loc (loc, fn, 1, s1);
12016 return NULL_TREE;
12020 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
12021 to the call. IGNORE is true if the value returned
12022 by the builtin will be ignored. UNLOCKED is true is true if this
12023 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
12024 the known length of the string. Return NULL_TREE if no simplification
12025 was possible. */
12027 tree
12028 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
12029 bool ignore, bool unlocked, tree len)
12031 /* If we're using an unlocked function, assume the other unlocked
12032 functions exist explicitly. */
12033 tree const fn_fputc = (unlocked
12034 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
12035 : builtin_decl_implicit (BUILT_IN_FPUTC));
12036 tree const fn_fwrite = (unlocked
12037 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12038 : builtin_decl_implicit (BUILT_IN_FWRITE));
12040 /* If the return value is used, don't do the transformation. */
12041 if (!ignore)
12042 return NULL_TREE;
12044 /* Verify the arguments in the original call. */
12045 if (!validate_arg (arg0, POINTER_TYPE)
12046 || !validate_arg (arg1, POINTER_TYPE))
12047 return NULL_TREE;
12049 if (! len)
12050 len = c_strlen (arg0, 0);
12052 /* Get the length of the string passed to fputs. If the length
12053 can't be determined, punt. */
12054 if (!len
12055 || TREE_CODE (len) != INTEGER_CST)
12056 return NULL_TREE;
12058 switch (compare_tree_int (len, 1))
12060 case -1: /* length is 0, delete the call entirely . */
12061 return omit_one_operand_loc (loc, integer_type_node,
12062 integer_zero_node, arg1);;
12064 case 0: /* length is 1, call fputc. */
12066 const char *p = c_getstr (arg0);
12068 if (p != NULL)
12070 if (fn_fputc)
12071 return build_call_expr_loc (loc, fn_fputc, 2,
12072 build_int_cst
12073 (integer_type_node, p[0]), arg1);
12074 else
12075 return NULL_TREE;
12078 /* FALLTHROUGH */
12079 case 1: /* length is greater than 1, call fwrite. */
12081 /* If optimizing for size keep fputs. */
12082 if (optimize_function_for_size_p (cfun))
12083 return NULL_TREE;
12084 /* New argument list transforming fputs(string, stream) to
12085 fwrite(string, 1, len, stream). */
12086 if (fn_fwrite)
12087 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12088 size_one_node, len, arg1);
12089 else
12090 return NULL_TREE;
12092 default:
12093 gcc_unreachable ();
12095 return NULL_TREE;
12098 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12099 produced. False otherwise. This is done so that we don't output the error
12100 or warning twice or three times. */
12102 bool
12103 fold_builtin_next_arg (tree exp, bool va_start_p)
12105 tree fntype = TREE_TYPE (current_function_decl);
12106 int nargs = call_expr_nargs (exp);
12107 tree arg;
12108 /* There is good chance the current input_location points inside the
12109 definition of the va_start macro (perhaps on the token for
12110 builtin) in a system header, so warnings will not be emitted.
12111 Use the location in real source code. */
12112 source_location current_location =
12113 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12114 NULL);
12116 if (!stdarg_p (fntype))
12118 error ("%<va_start%> used in function with fixed args");
12119 return true;
12122 if (va_start_p)
12124 if (va_start_p && (nargs != 2))
12126 error ("wrong number of arguments to function %<va_start%>");
12127 return true;
12129 arg = CALL_EXPR_ARG (exp, 1);
12131 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12132 when we checked the arguments and if needed issued a warning. */
12133 else
12135 if (nargs == 0)
12137 /* Evidently an out of date version of <stdarg.h>; can't validate
12138 va_start's second argument, but can still work as intended. */
12139 warning_at (current_location,
12140 OPT_Wvarargs,
12141 "%<__builtin_next_arg%> called without an argument");
12142 return true;
12144 else if (nargs > 1)
12146 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12147 return true;
12149 arg = CALL_EXPR_ARG (exp, 0);
12152 if (TREE_CODE (arg) == SSA_NAME)
12153 arg = SSA_NAME_VAR (arg);
12155 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12156 or __builtin_next_arg (0) the first time we see it, after checking
12157 the arguments and if needed issuing a warning. */
12158 if (!integer_zerop (arg))
12160 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12162 /* Strip off all nops for the sake of the comparison. This
12163 is not quite the same as STRIP_NOPS. It does more.
12164 We must also strip off INDIRECT_EXPR for C++ reference
12165 parameters. */
12166 while (CONVERT_EXPR_P (arg)
12167 || TREE_CODE (arg) == INDIRECT_REF)
12168 arg = TREE_OPERAND (arg, 0);
12169 if (arg != last_parm)
12171 /* FIXME: Sometimes with the tree optimizers we can get the
12172 not the last argument even though the user used the last
12173 argument. We just warn and set the arg to be the last
12174 argument so that we will get wrong-code because of
12175 it. */
12176 warning_at (current_location,
12177 OPT_Wvarargs,
12178 "second parameter of %<va_start%> not last named argument");
12181 /* Undefined by C99 7.15.1.4p4 (va_start):
12182 "If the parameter parmN is declared with the register storage
12183 class, with a function or array type, or with a type that is
12184 not compatible with the type that results after application of
12185 the default argument promotions, the behavior is undefined."
12187 else if (DECL_REGISTER (arg))
12189 warning_at (current_location,
12190 OPT_Wvarargs,
12191 "undefined behaviour when second parameter of "
12192 "%<va_start%> is declared with %<register%> storage");
12195 /* We want to verify the second parameter just once before the tree
12196 optimizers are run and then avoid keeping it in the tree,
12197 as otherwise we could warn even for correct code like:
12198 void foo (int i, ...)
12199 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12200 if (va_start_p)
12201 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12202 else
12203 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12205 return false;
12209 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12210 ORIG may be null if this is a 2-argument call. We don't attempt to
12211 simplify calls with more than 3 arguments.
12213 Return NULL_TREE if no simplification was possible, otherwise return the
12214 simplified form of the call as a tree. If IGNORED is true, it means that
12215 the caller does not use the returned value of the function. */
12217 static tree
12218 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12219 tree orig, int ignored)
12221 tree call, retval;
12222 const char *fmt_str = NULL;
12224 /* Verify the required arguments in the original call. We deal with two
12225 types of sprintf() calls: 'sprintf (str, fmt)' and
12226 'sprintf (dest, "%s", orig)'. */
12227 if (!validate_arg (dest, POINTER_TYPE)
12228 || !validate_arg (fmt, POINTER_TYPE))
12229 return NULL_TREE;
12230 if (orig && !validate_arg (orig, POINTER_TYPE))
12231 return NULL_TREE;
12233 /* Check whether the format is a literal string constant. */
12234 fmt_str = c_getstr (fmt);
12235 if (fmt_str == NULL)
12236 return NULL_TREE;
12238 call = NULL_TREE;
12239 retval = NULL_TREE;
12241 if (!init_target_chars ())
12242 return NULL_TREE;
12244 /* If the format doesn't contain % args or %%, use strcpy. */
12245 if (strchr (fmt_str, target_percent) == NULL)
12247 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12249 if (!fn)
12250 return NULL_TREE;
12252 /* Don't optimize sprintf (buf, "abc", ptr++). */
12253 if (orig)
12254 return NULL_TREE;
12256 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12257 'format' is known to contain no % formats. */
12258 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12259 if (!ignored)
12260 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12263 /* If the format is "%s", use strcpy if the result isn't used. */
12264 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12266 tree fn;
12267 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12269 if (!fn)
12270 return NULL_TREE;
12272 /* Don't crash on sprintf (str1, "%s"). */
12273 if (!orig)
12274 return NULL_TREE;
12276 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12277 if (!ignored)
12279 retval = c_strlen (orig, 1);
12280 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12281 return NULL_TREE;
12283 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12286 if (call && retval)
12288 retval = fold_convert_loc
12289 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12290 retval);
12291 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12293 else
12294 return call;
12297 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12298 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12299 attempt to simplify calls with more than 4 arguments.
12301 Return NULL_TREE if no simplification was possible, otherwise return the
12302 simplified form of the call as a tree. If IGNORED is true, it means that
12303 the caller does not use the returned value of the function. */
12305 static tree
12306 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12307 tree orig, int ignored)
12309 tree call, retval;
12310 const char *fmt_str = NULL;
12311 unsigned HOST_WIDE_INT destlen;
12313 /* Verify the required arguments in the original call. We deal with two
12314 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12315 'snprintf (dest, cst, "%s", orig)'. */
12316 if (!validate_arg (dest, POINTER_TYPE)
12317 || !validate_arg (destsize, INTEGER_TYPE)
12318 || !validate_arg (fmt, POINTER_TYPE))
12319 return NULL_TREE;
12320 if (orig && !validate_arg (orig, POINTER_TYPE))
12321 return NULL_TREE;
12323 if (!host_integerp (destsize, 1))
12324 return NULL_TREE;
12326 /* Check whether the format is a literal string constant. */
12327 fmt_str = c_getstr (fmt);
12328 if (fmt_str == NULL)
12329 return NULL_TREE;
12331 call = NULL_TREE;
12332 retval = NULL_TREE;
12334 if (!init_target_chars ())
12335 return NULL_TREE;
12337 destlen = tree_low_cst (destsize, 1);
12339 /* If the format doesn't contain % args or %%, use strcpy. */
12340 if (strchr (fmt_str, target_percent) == NULL)
12342 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12343 size_t len = strlen (fmt_str);
12345 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12346 if (orig)
12347 return NULL_TREE;
12349 /* We could expand this as
12350 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12351 or to
12352 memcpy (str, fmt_with_nul_at_cstm1, cst);
12353 but in the former case that might increase code size
12354 and in the latter case grow .rodata section too much.
12355 So punt for now. */
12356 if (len >= destlen)
12357 return NULL_TREE;
12359 if (!fn)
12360 return NULL_TREE;
12362 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12363 'format' is known to contain no % formats and
12364 strlen (fmt) < cst. */
12365 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12367 if (!ignored)
12368 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12371 /* If the format is "%s", use strcpy if the result isn't used. */
12372 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12374 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12375 unsigned HOST_WIDE_INT origlen;
12377 /* Don't crash on snprintf (str1, cst, "%s"). */
12378 if (!orig)
12379 return NULL_TREE;
12381 retval = c_strlen (orig, 1);
12382 if (!retval || !host_integerp (retval, 1))
12383 return NULL_TREE;
12385 origlen = tree_low_cst (retval, 1);
12386 /* We could expand this as
12387 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12388 or to
12389 memcpy (str1, str2_with_nul_at_cstm1, cst);
12390 but in the former case that might increase code size
12391 and in the latter case grow .rodata section too much.
12392 So punt for now. */
12393 if (origlen >= destlen)
12394 return NULL_TREE;
12396 /* Convert snprintf (str1, cst, "%s", str2) into
12397 strcpy (str1, str2) if strlen (str2) < cst. */
12398 if (!fn)
12399 return NULL_TREE;
12401 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12403 if (ignored)
12404 retval = NULL_TREE;
12407 if (call && retval)
12409 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12410 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12411 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12413 else
12414 return call;
12417 /* Expand a call EXP to __builtin_object_size. */
12420 expand_builtin_object_size (tree exp)
12422 tree ost;
12423 int object_size_type;
12424 tree fndecl = get_callee_fndecl (exp);
12426 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12428 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12429 exp, fndecl);
12430 expand_builtin_trap ();
12431 return const0_rtx;
12434 ost = CALL_EXPR_ARG (exp, 1);
12435 STRIP_NOPS (ost);
12437 if (TREE_CODE (ost) != INTEGER_CST
12438 || tree_int_cst_sgn (ost) < 0
12439 || compare_tree_int (ost, 3) > 0)
12441 error ("%Klast argument of %D is not integer constant between 0 and 3",
12442 exp, fndecl);
12443 expand_builtin_trap ();
12444 return const0_rtx;
12447 object_size_type = tree_low_cst (ost, 0);
12449 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12452 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12453 FCODE is the BUILT_IN_* to use.
12454 Return NULL_RTX if we failed; the caller should emit a normal call,
12455 otherwise try to get the result in TARGET, if convenient (and in
12456 mode MODE if that's convenient). */
12458 static rtx
12459 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12460 enum built_in_function fcode)
12462 tree dest, src, len, size;
12464 if (!validate_arglist (exp,
12465 POINTER_TYPE,
12466 fcode == BUILT_IN_MEMSET_CHK
12467 ? INTEGER_TYPE : POINTER_TYPE,
12468 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12469 return NULL_RTX;
12471 dest = CALL_EXPR_ARG (exp, 0);
12472 src = CALL_EXPR_ARG (exp, 1);
12473 len = CALL_EXPR_ARG (exp, 2);
12474 size = CALL_EXPR_ARG (exp, 3);
12476 if (! host_integerp (size, 1))
12477 return NULL_RTX;
12479 if (host_integerp (len, 1) || integer_all_onesp (size))
12481 tree fn;
12483 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12485 warning_at (tree_nonartificial_location (exp),
12486 0, "%Kcall to %D will always overflow destination buffer",
12487 exp, get_callee_fndecl (exp));
12488 return NULL_RTX;
12491 fn = NULL_TREE;
12492 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12493 mem{cpy,pcpy,move,set} is available. */
12494 switch (fcode)
12496 case BUILT_IN_MEMCPY_CHK:
12497 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12498 break;
12499 case BUILT_IN_MEMPCPY_CHK:
12500 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12501 break;
12502 case BUILT_IN_MEMMOVE_CHK:
12503 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12504 break;
12505 case BUILT_IN_MEMSET_CHK:
12506 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12507 break;
12508 default:
12509 break;
12512 if (! fn)
12513 return NULL_RTX;
12515 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12516 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12517 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12518 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12520 else if (fcode == BUILT_IN_MEMSET_CHK)
12521 return NULL_RTX;
12522 else
12524 unsigned int dest_align = get_pointer_alignment (dest);
12526 /* If DEST is not a pointer type, call the normal function. */
12527 if (dest_align == 0)
12528 return NULL_RTX;
12530 /* If SRC and DEST are the same (and not volatile), do nothing. */
12531 if (operand_equal_p (src, dest, 0))
12533 tree expr;
12535 if (fcode != BUILT_IN_MEMPCPY_CHK)
12537 /* Evaluate and ignore LEN in case it has side-effects. */
12538 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12539 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12542 expr = fold_build_pointer_plus (dest, len);
12543 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12546 /* __memmove_chk special case. */
12547 if (fcode == BUILT_IN_MEMMOVE_CHK)
12549 unsigned int src_align = get_pointer_alignment (src);
12551 if (src_align == 0)
12552 return NULL_RTX;
12554 /* If src is categorized for a readonly section we can use
12555 normal __memcpy_chk. */
12556 if (readonly_data_expr (src))
12558 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12559 if (!fn)
12560 return NULL_RTX;
12561 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12562 dest, src, len, size);
12563 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12564 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12565 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12568 return NULL_RTX;
12572 /* Emit warning if a buffer overflow is detected at compile time. */
12574 static void
12575 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12577 int is_strlen = 0;
12578 tree len, size;
12579 location_t loc = tree_nonartificial_location (exp);
12581 switch (fcode)
12583 case BUILT_IN_STRCPY_CHK:
12584 case BUILT_IN_STPCPY_CHK:
12585 /* For __strcat_chk the warning will be emitted only if overflowing
12586 by at least strlen (dest) + 1 bytes. */
12587 case BUILT_IN_STRCAT_CHK:
12588 len = CALL_EXPR_ARG (exp, 1);
12589 size = CALL_EXPR_ARG (exp, 2);
12590 is_strlen = 1;
12591 break;
12592 case BUILT_IN_STRNCAT_CHK:
12593 case BUILT_IN_STRNCPY_CHK:
12594 case BUILT_IN_STPNCPY_CHK:
12595 len = CALL_EXPR_ARG (exp, 2);
12596 size = CALL_EXPR_ARG (exp, 3);
12597 break;
12598 case BUILT_IN_SNPRINTF_CHK:
12599 case BUILT_IN_VSNPRINTF_CHK:
12600 len = CALL_EXPR_ARG (exp, 1);
12601 size = CALL_EXPR_ARG (exp, 3);
12602 break;
12603 default:
12604 gcc_unreachable ();
12607 if (!len || !size)
12608 return;
12610 if (! host_integerp (size, 1) || integer_all_onesp (size))
12611 return;
12613 if (is_strlen)
12615 len = c_strlen (len, 1);
12616 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12617 return;
12619 else if (fcode == BUILT_IN_STRNCAT_CHK)
12621 tree src = CALL_EXPR_ARG (exp, 1);
12622 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12623 return;
12624 src = c_strlen (src, 1);
12625 if (! src || ! host_integerp (src, 1))
12627 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12628 exp, get_callee_fndecl (exp));
12629 return;
12631 else if (tree_int_cst_lt (src, size))
12632 return;
12634 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12635 return;
12637 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12638 exp, get_callee_fndecl (exp));
12641 /* Emit warning if a buffer overflow is detected at compile time
12642 in __sprintf_chk/__vsprintf_chk calls. */
12644 static void
12645 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12647 tree size, len, fmt;
12648 const char *fmt_str;
12649 int nargs = call_expr_nargs (exp);
12651 /* Verify the required arguments in the original call. */
12653 if (nargs < 4)
12654 return;
12655 size = CALL_EXPR_ARG (exp, 2);
12656 fmt = CALL_EXPR_ARG (exp, 3);
12658 if (! host_integerp (size, 1) || integer_all_onesp (size))
12659 return;
12661 /* Check whether the format is a literal string constant. */
12662 fmt_str = c_getstr (fmt);
12663 if (fmt_str == NULL)
12664 return;
12666 if (!init_target_chars ())
12667 return;
12669 /* If the format doesn't contain % args or %%, we know its size. */
12670 if (strchr (fmt_str, target_percent) == 0)
12671 len = build_int_cstu (size_type_node, strlen (fmt_str));
12672 /* If the format is "%s" and first ... argument is a string literal,
12673 we know it too. */
12674 else if (fcode == BUILT_IN_SPRINTF_CHK
12675 && strcmp (fmt_str, target_percent_s) == 0)
12677 tree arg;
12679 if (nargs < 5)
12680 return;
12681 arg = CALL_EXPR_ARG (exp, 4);
12682 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12683 return;
12685 len = c_strlen (arg, 1);
12686 if (!len || ! host_integerp (len, 1))
12687 return;
12689 else
12690 return;
12692 if (! tree_int_cst_lt (len, size))
12693 warning_at (tree_nonartificial_location (exp),
12694 0, "%Kcall to %D will always overflow destination buffer",
12695 exp, get_callee_fndecl (exp));
12698 /* Emit warning if a free is called with address of a variable. */
12700 static void
12701 maybe_emit_free_warning (tree exp)
12703 tree arg = CALL_EXPR_ARG (exp, 0);
12705 STRIP_NOPS (arg);
12706 if (TREE_CODE (arg) != ADDR_EXPR)
12707 return;
12709 arg = get_base_address (TREE_OPERAND (arg, 0));
12710 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12711 return;
12713 if (SSA_VAR_P (arg))
12714 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12715 "%Kattempt to free a non-heap object %qD", exp, arg);
12716 else
12717 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12718 "%Kattempt to free a non-heap object", exp);
12721 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12722 if possible. */
12724 tree
12725 fold_builtin_object_size (tree ptr, tree ost)
12727 unsigned HOST_WIDE_INT bytes;
12728 int object_size_type;
12730 if (!validate_arg (ptr, POINTER_TYPE)
12731 || !validate_arg (ost, INTEGER_TYPE))
12732 return NULL_TREE;
12734 STRIP_NOPS (ost);
12736 if (TREE_CODE (ost) != INTEGER_CST
12737 || tree_int_cst_sgn (ost) < 0
12738 || compare_tree_int (ost, 3) > 0)
12739 return NULL_TREE;
12741 object_size_type = tree_low_cst (ost, 0);
12743 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12744 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12745 and (size_t) 0 for types 2 and 3. */
12746 if (TREE_SIDE_EFFECTS (ptr))
12747 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12749 if (TREE_CODE (ptr) == ADDR_EXPR)
12751 bytes = compute_builtin_object_size (ptr, object_size_type);
12752 if (double_int_fits_to_tree_p (size_type_node,
12753 double_int::from_uhwi (bytes)))
12754 return build_int_cstu (size_type_node, bytes);
12756 else if (TREE_CODE (ptr) == SSA_NAME)
12758 /* If object size is not known yet, delay folding until
12759 later. Maybe subsequent passes will help determining
12760 it. */
12761 bytes = compute_builtin_object_size (ptr, object_size_type);
12762 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12763 && double_int_fits_to_tree_p (size_type_node,
12764 double_int::from_uhwi (bytes)))
12765 return build_int_cstu (size_type_node, bytes);
12768 return NULL_TREE;
12771 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12772 DEST, SRC, LEN, and SIZE are the arguments to the call.
12773 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12774 code of the builtin. If MAXLEN is not NULL, it is maximum length
12775 passed as third argument. */
12777 tree
12778 fold_builtin_memory_chk (location_t loc, tree fndecl,
12779 tree dest, tree src, tree len, tree size,
12780 tree maxlen, bool ignore,
12781 enum built_in_function fcode)
12783 tree fn;
12785 if (!validate_arg (dest, POINTER_TYPE)
12786 || !validate_arg (src,
12787 (fcode == BUILT_IN_MEMSET_CHK
12788 ? INTEGER_TYPE : POINTER_TYPE))
12789 || !validate_arg (len, INTEGER_TYPE)
12790 || !validate_arg (size, INTEGER_TYPE))
12791 return NULL_TREE;
12793 /* If SRC and DEST are the same (and not volatile), return DEST
12794 (resp. DEST+LEN for __mempcpy_chk). */
12795 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12797 if (fcode != BUILT_IN_MEMPCPY_CHK)
12798 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12799 dest, len);
12800 else
12802 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12803 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12807 if (! host_integerp (size, 1))
12808 return NULL_TREE;
12810 if (! integer_all_onesp (size))
12812 if (! host_integerp (len, 1))
12814 /* If LEN is not constant, try MAXLEN too.
12815 For MAXLEN only allow optimizing into non-_ocs function
12816 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12817 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12819 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12821 /* (void) __mempcpy_chk () can be optimized into
12822 (void) __memcpy_chk (). */
12823 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12824 if (!fn)
12825 return NULL_TREE;
12827 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12829 return NULL_TREE;
12832 else
12833 maxlen = len;
12835 if (tree_int_cst_lt (size, maxlen))
12836 return NULL_TREE;
12839 fn = NULL_TREE;
12840 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12841 mem{cpy,pcpy,move,set} is available. */
12842 switch (fcode)
12844 case BUILT_IN_MEMCPY_CHK:
12845 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12846 break;
12847 case BUILT_IN_MEMPCPY_CHK:
12848 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12849 break;
12850 case BUILT_IN_MEMMOVE_CHK:
12851 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12852 break;
12853 case BUILT_IN_MEMSET_CHK:
12854 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12855 break;
12856 default:
12857 break;
12860 if (!fn)
12861 return NULL_TREE;
12863 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12866 /* Fold a call to the __st[rp]cpy_chk builtin.
12867 DEST, SRC, and SIZE are the arguments to the call.
12868 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12869 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12870 strings passed as second argument. */
12872 tree
12873 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12874 tree src, tree size,
12875 tree maxlen, bool ignore,
12876 enum built_in_function fcode)
12878 tree len, fn;
12880 if (!validate_arg (dest, POINTER_TYPE)
12881 || !validate_arg (src, POINTER_TYPE)
12882 || !validate_arg (size, INTEGER_TYPE))
12883 return NULL_TREE;
12885 /* If SRC and DEST are the same (and not volatile), return DEST. */
12886 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12887 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12889 if (! host_integerp (size, 1))
12890 return NULL_TREE;
12892 if (! integer_all_onesp (size))
12894 len = c_strlen (src, 1);
12895 if (! len || ! host_integerp (len, 1))
12897 /* If LEN is not constant, try MAXLEN too.
12898 For MAXLEN only allow optimizing into non-_ocs function
12899 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12900 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12902 if (fcode == BUILT_IN_STPCPY_CHK)
12904 if (! ignore)
12905 return NULL_TREE;
12907 /* If return value of __stpcpy_chk is ignored,
12908 optimize into __strcpy_chk. */
12909 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12910 if (!fn)
12911 return NULL_TREE;
12913 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12916 if (! len || TREE_SIDE_EFFECTS (len))
12917 return NULL_TREE;
12919 /* If c_strlen returned something, but not a constant,
12920 transform __strcpy_chk into __memcpy_chk. */
12921 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12922 if (!fn)
12923 return NULL_TREE;
12925 len = fold_convert_loc (loc, size_type_node, len);
12926 len = size_binop_loc (loc, PLUS_EXPR, len,
12927 build_int_cst (size_type_node, 1));
12928 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12929 build_call_expr_loc (loc, fn, 4,
12930 dest, src, len, size));
12933 else
12934 maxlen = len;
12936 if (! tree_int_cst_lt (maxlen, size))
12937 return NULL_TREE;
12940 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12941 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12942 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12943 if (!fn)
12944 return NULL_TREE;
12946 return build_call_expr_loc (loc, fn, 2, dest, src);
12949 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12950 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12951 length passed as third argument. IGNORE is true if return value can be
12952 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12954 tree
12955 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12956 tree len, tree size, tree maxlen, bool ignore,
12957 enum built_in_function fcode)
12959 tree fn;
12961 if (!validate_arg (dest, POINTER_TYPE)
12962 || !validate_arg (src, POINTER_TYPE)
12963 || !validate_arg (len, INTEGER_TYPE)
12964 || !validate_arg (size, INTEGER_TYPE))
12965 return NULL_TREE;
12967 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12969 /* If return value of __stpncpy_chk is ignored,
12970 optimize into __strncpy_chk. */
12971 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12972 if (fn)
12973 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12976 if (! host_integerp (size, 1))
12977 return NULL_TREE;
12979 if (! integer_all_onesp (size))
12981 if (! host_integerp (len, 1))
12983 /* If LEN is not constant, try MAXLEN too.
12984 For MAXLEN only allow optimizing into non-_ocs function
12985 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12986 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12987 return NULL_TREE;
12989 else
12990 maxlen = len;
12992 if (tree_int_cst_lt (size, maxlen))
12993 return NULL_TREE;
12996 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12997 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12998 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12999 if (!fn)
13000 return NULL_TREE;
13002 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13005 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
13006 are the arguments to the call. */
13008 static tree
13009 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
13010 tree src, tree size)
13012 tree fn;
13013 const char *p;
13015 if (!validate_arg (dest, POINTER_TYPE)
13016 || !validate_arg (src, POINTER_TYPE)
13017 || !validate_arg (size, INTEGER_TYPE))
13018 return NULL_TREE;
13020 p = c_getstr (src);
13021 /* If the SRC parameter is "", return DEST. */
13022 if (p && *p == '\0')
13023 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13025 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
13026 return NULL_TREE;
13028 /* If __builtin_strcat_chk is used, assume strcat is available. */
13029 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
13030 if (!fn)
13031 return NULL_TREE;
13033 return build_call_expr_loc (loc, fn, 2, dest, src);
13036 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13037 LEN, and SIZE. */
13039 static tree
13040 fold_builtin_strncat_chk (location_t loc, tree fndecl,
13041 tree dest, tree src, tree len, tree size)
13043 tree fn;
13044 const char *p;
13046 if (!validate_arg (dest, POINTER_TYPE)
13047 || !validate_arg (src, POINTER_TYPE)
13048 || !validate_arg (size, INTEGER_TYPE)
13049 || !validate_arg (size, INTEGER_TYPE))
13050 return NULL_TREE;
13052 p = c_getstr (src);
13053 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13054 if (p && *p == '\0')
13055 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13056 else if (integer_zerop (len))
13057 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13059 if (! host_integerp (size, 1))
13060 return NULL_TREE;
13062 if (! integer_all_onesp (size))
13064 tree src_len = c_strlen (src, 1);
13065 if (src_len
13066 && host_integerp (src_len, 1)
13067 && host_integerp (len, 1)
13068 && ! tree_int_cst_lt (len, src_len))
13070 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13071 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13072 if (!fn)
13073 return NULL_TREE;
13075 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13077 return NULL_TREE;
13080 /* If __builtin_strncat_chk is used, assume strncat is available. */
13081 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13082 if (!fn)
13083 return NULL_TREE;
13085 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13088 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13089 Return NULL_TREE if a normal call should be emitted rather than
13090 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13091 or BUILT_IN_VSPRINTF_CHK. */
13093 static tree
13094 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13095 enum built_in_function fcode)
13097 tree dest, size, len, fn, fmt, flag;
13098 const char *fmt_str;
13100 /* Verify the required arguments in the original call. */
13101 if (nargs < 4)
13102 return NULL_TREE;
13103 dest = args[0];
13104 if (!validate_arg (dest, POINTER_TYPE))
13105 return NULL_TREE;
13106 flag = args[1];
13107 if (!validate_arg (flag, INTEGER_TYPE))
13108 return NULL_TREE;
13109 size = args[2];
13110 if (!validate_arg (size, INTEGER_TYPE))
13111 return NULL_TREE;
13112 fmt = args[3];
13113 if (!validate_arg (fmt, POINTER_TYPE))
13114 return NULL_TREE;
13116 if (! host_integerp (size, 1))
13117 return NULL_TREE;
13119 len = NULL_TREE;
13121 if (!init_target_chars ())
13122 return NULL_TREE;
13124 /* Check whether the format is a literal string constant. */
13125 fmt_str = c_getstr (fmt);
13126 if (fmt_str != NULL)
13128 /* If the format doesn't contain % args or %%, we know the size. */
13129 if (strchr (fmt_str, target_percent) == 0)
13131 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13132 len = build_int_cstu (size_type_node, strlen (fmt_str));
13134 /* If the format is "%s" and first ... argument is a string literal,
13135 we know the size too. */
13136 else if (fcode == BUILT_IN_SPRINTF_CHK
13137 && strcmp (fmt_str, target_percent_s) == 0)
13139 tree arg;
13141 if (nargs == 5)
13143 arg = args[4];
13144 if (validate_arg (arg, POINTER_TYPE))
13146 len = c_strlen (arg, 1);
13147 if (! len || ! host_integerp (len, 1))
13148 len = NULL_TREE;
13154 if (! integer_all_onesp (size))
13156 if (! len || ! tree_int_cst_lt (len, size))
13157 return NULL_TREE;
13160 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13161 or if format doesn't contain % chars or is "%s". */
13162 if (! integer_zerop (flag))
13164 if (fmt_str == NULL)
13165 return NULL_TREE;
13166 if (strchr (fmt_str, target_percent) != NULL
13167 && strcmp (fmt_str, target_percent_s))
13168 return NULL_TREE;
13171 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13172 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13173 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13174 if (!fn)
13175 return NULL_TREE;
13177 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13180 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13181 a normal call should be emitted rather than expanding the function
13182 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13184 static tree
13185 fold_builtin_sprintf_chk (location_t loc, tree exp,
13186 enum built_in_function fcode)
13188 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13189 CALL_EXPR_ARGP (exp), fcode);
13192 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13193 NULL_TREE if a normal call should be emitted rather than expanding
13194 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13195 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13196 passed as second argument. */
13198 static tree
13199 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13200 tree maxlen, enum built_in_function fcode)
13202 tree dest, size, len, fn, fmt, flag;
13203 const char *fmt_str;
13205 /* Verify the required arguments in the original call. */
13206 if (nargs < 5)
13207 return NULL_TREE;
13208 dest = args[0];
13209 if (!validate_arg (dest, POINTER_TYPE))
13210 return NULL_TREE;
13211 len = args[1];
13212 if (!validate_arg (len, INTEGER_TYPE))
13213 return NULL_TREE;
13214 flag = args[2];
13215 if (!validate_arg (flag, INTEGER_TYPE))
13216 return NULL_TREE;
13217 size = args[3];
13218 if (!validate_arg (size, INTEGER_TYPE))
13219 return NULL_TREE;
13220 fmt = args[4];
13221 if (!validate_arg (fmt, POINTER_TYPE))
13222 return NULL_TREE;
13224 if (! host_integerp (size, 1))
13225 return NULL_TREE;
13227 if (! integer_all_onesp (size))
13229 if (! host_integerp (len, 1))
13231 /* If LEN is not constant, try MAXLEN too.
13232 For MAXLEN only allow optimizing into non-_ocs function
13233 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13234 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13235 return NULL_TREE;
13237 else
13238 maxlen = len;
13240 if (tree_int_cst_lt (size, maxlen))
13241 return NULL_TREE;
13244 if (!init_target_chars ())
13245 return NULL_TREE;
13247 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13248 or if format doesn't contain % chars or is "%s". */
13249 if (! integer_zerop (flag))
13251 fmt_str = c_getstr (fmt);
13252 if (fmt_str == NULL)
13253 return NULL_TREE;
13254 if (strchr (fmt_str, target_percent) != NULL
13255 && strcmp (fmt_str, target_percent_s))
13256 return NULL_TREE;
13259 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13260 available. */
13261 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13262 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13263 if (!fn)
13264 return NULL_TREE;
13266 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13269 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13270 a normal call should be emitted rather than expanding the function
13271 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13272 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13273 passed as second argument. */
13275 tree
13276 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13277 enum built_in_function fcode)
13279 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13280 CALL_EXPR_ARGP (exp), maxlen, fcode);
13283 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13284 FMT and ARG are the arguments to the call; we don't fold cases with
13285 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13287 Return NULL_TREE if no simplification was possible, otherwise return the
13288 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13289 code of the function to be simplified. */
13291 static tree
13292 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13293 tree arg, bool ignore,
13294 enum built_in_function fcode)
13296 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13297 const char *fmt_str = NULL;
13299 /* If the return value is used, don't do the transformation. */
13300 if (! ignore)
13301 return NULL_TREE;
13303 /* Verify the required arguments in the original call. */
13304 if (!validate_arg (fmt, POINTER_TYPE))
13305 return NULL_TREE;
13307 /* Check whether the format is a literal string constant. */
13308 fmt_str = c_getstr (fmt);
13309 if (fmt_str == NULL)
13310 return NULL_TREE;
13312 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13314 /* If we're using an unlocked function, assume the other
13315 unlocked functions exist explicitly. */
13316 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13317 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13319 else
13321 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13322 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13325 if (!init_target_chars ())
13326 return NULL_TREE;
13328 if (strcmp (fmt_str, target_percent_s) == 0
13329 || strchr (fmt_str, target_percent) == NULL)
13331 const char *str;
13333 if (strcmp (fmt_str, target_percent_s) == 0)
13335 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13336 return NULL_TREE;
13338 if (!arg || !validate_arg (arg, POINTER_TYPE))
13339 return NULL_TREE;
13341 str = c_getstr (arg);
13342 if (str == NULL)
13343 return NULL_TREE;
13345 else
13347 /* The format specifier doesn't contain any '%' characters. */
13348 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13349 && arg)
13350 return NULL_TREE;
13351 str = fmt_str;
13354 /* If the string was "", printf does nothing. */
13355 if (str[0] == '\0')
13356 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13358 /* If the string has length of 1, call putchar. */
13359 if (str[1] == '\0')
13361 /* Given printf("c"), (where c is any one character,)
13362 convert "c"[0] to an int and pass that to the replacement
13363 function. */
13364 newarg = build_int_cst (integer_type_node, str[0]);
13365 if (fn_putchar)
13366 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13368 else
13370 /* If the string was "string\n", call puts("string"). */
13371 size_t len = strlen (str);
13372 if ((unsigned char)str[len - 1] == target_newline
13373 && (size_t) (int) len == len
13374 && (int) len > 0)
13376 char *newstr;
13377 tree offset_node, string_cst;
13379 /* Create a NUL-terminated string that's one char shorter
13380 than the original, stripping off the trailing '\n'. */
13381 newarg = build_string_literal (len, str);
13382 string_cst = string_constant (newarg, &offset_node);
13383 gcc_checking_assert (string_cst
13384 && (TREE_STRING_LENGTH (string_cst)
13385 == (int) len)
13386 && integer_zerop (offset_node)
13387 && (unsigned char)
13388 TREE_STRING_POINTER (string_cst)[len - 1]
13389 == target_newline);
13390 /* build_string_literal creates a new STRING_CST,
13391 modify it in place to avoid double copying. */
13392 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13393 newstr[len - 1] = '\0';
13394 if (fn_puts)
13395 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13397 else
13398 /* We'd like to arrange to call fputs(string,stdout) here,
13399 but we need stdout and don't have a way to get it yet. */
13400 return NULL_TREE;
13404 /* The other optimizations can be done only on the non-va_list variants. */
13405 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13406 return NULL_TREE;
13408 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13409 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13411 if (!arg || !validate_arg (arg, POINTER_TYPE))
13412 return NULL_TREE;
13413 if (fn_puts)
13414 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13417 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13418 else if (strcmp (fmt_str, target_percent_c) == 0)
13420 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13421 return NULL_TREE;
13422 if (fn_putchar)
13423 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13426 if (!call)
13427 return NULL_TREE;
13429 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13432 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13433 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13434 more than 3 arguments, and ARG may be null in the 2-argument case.
13436 Return NULL_TREE if no simplification was possible, otherwise return the
13437 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13438 code of the function to be simplified. */
13440 static tree
13441 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13442 tree fmt, tree arg, bool ignore,
13443 enum built_in_function fcode)
13445 tree fn_fputc, fn_fputs, call = NULL_TREE;
13446 const char *fmt_str = NULL;
13448 /* If the return value is used, don't do the transformation. */
13449 if (! ignore)
13450 return NULL_TREE;
13452 /* Verify the required arguments in the original call. */
13453 if (!validate_arg (fp, POINTER_TYPE))
13454 return NULL_TREE;
13455 if (!validate_arg (fmt, POINTER_TYPE))
13456 return NULL_TREE;
13458 /* Check whether the format is a literal string constant. */
13459 fmt_str = c_getstr (fmt);
13460 if (fmt_str == NULL)
13461 return NULL_TREE;
13463 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13465 /* If we're using an unlocked function, assume the other
13466 unlocked functions exist explicitly. */
13467 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13468 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13470 else
13472 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13473 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13476 if (!init_target_chars ())
13477 return NULL_TREE;
13479 /* If the format doesn't contain % args or %%, use strcpy. */
13480 if (strchr (fmt_str, target_percent) == NULL)
13482 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13483 && arg)
13484 return NULL_TREE;
13486 /* If the format specifier was "", fprintf does nothing. */
13487 if (fmt_str[0] == '\0')
13489 /* If FP has side-effects, just wait until gimplification is
13490 done. */
13491 if (TREE_SIDE_EFFECTS (fp))
13492 return NULL_TREE;
13494 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13497 /* When "string" doesn't contain %, replace all cases of
13498 fprintf (fp, string) with fputs (string, fp). The fputs
13499 builtin will take care of special cases like length == 1. */
13500 if (fn_fputs)
13501 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13504 /* The other optimizations can be done only on the non-va_list variants. */
13505 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13506 return NULL_TREE;
13508 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13509 else if (strcmp (fmt_str, target_percent_s) == 0)
13511 if (!arg || !validate_arg (arg, POINTER_TYPE))
13512 return NULL_TREE;
13513 if (fn_fputs)
13514 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13517 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13518 else if (strcmp (fmt_str, target_percent_c) == 0)
13520 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13521 return NULL_TREE;
13522 if (fn_fputc)
13523 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13526 if (!call)
13527 return NULL_TREE;
13528 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13531 /* Initialize format string characters in the target charset. */
13533 static bool
13534 init_target_chars (void)
13536 static bool init;
13537 if (!init)
13539 target_newline = lang_hooks.to_target_charset ('\n');
13540 target_percent = lang_hooks.to_target_charset ('%');
13541 target_c = lang_hooks.to_target_charset ('c');
13542 target_s = lang_hooks.to_target_charset ('s');
13543 if (target_newline == 0 || target_percent == 0 || target_c == 0
13544 || target_s == 0)
13545 return false;
13547 target_percent_c[0] = target_percent;
13548 target_percent_c[1] = target_c;
13549 target_percent_c[2] = '\0';
13551 target_percent_s[0] = target_percent;
13552 target_percent_s[1] = target_s;
13553 target_percent_s[2] = '\0';
13555 target_percent_s_newline[0] = target_percent;
13556 target_percent_s_newline[1] = target_s;
13557 target_percent_s_newline[2] = target_newline;
13558 target_percent_s_newline[3] = '\0';
13560 init = true;
13562 return true;
13565 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13566 and no overflow/underflow occurred. INEXACT is true if M was not
13567 exactly calculated. TYPE is the tree type for the result. This
13568 function assumes that you cleared the MPFR flags and then
13569 calculated M to see if anything subsequently set a flag prior to
13570 entering this function. Return NULL_TREE if any checks fail. */
13572 static tree
13573 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13575 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13576 overflow/underflow occurred. If -frounding-math, proceed iff the
13577 result of calling FUNC was exact. */
13578 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13579 && (!flag_rounding_math || !inexact))
13581 REAL_VALUE_TYPE rr;
13583 real_from_mpfr (&rr, m, type, GMP_RNDN);
13584 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13585 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13586 but the mpft_t is not, then we underflowed in the
13587 conversion. */
13588 if (real_isfinite (&rr)
13589 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13591 REAL_VALUE_TYPE rmode;
13593 real_convert (&rmode, TYPE_MODE (type), &rr);
13594 /* Proceed iff the specified mode can hold the value. */
13595 if (real_identical (&rmode, &rr))
13596 return build_real (type, rmode);
13599 return NULL_TREE;
13602 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13603 number and no overflow/underflow occurred. INEXACT is true if M
13604 was not exactly calculated. TYPE is the tree type for the result.
13605 This function assumes that you cleared the MPFR flags and then
13606 calculated M to see if anything subsequently set a flag prior to
13607 entering this function. Return NULL_TREE if any checks fail, if
13608 FORCE_CONVERT is true, then bypass the checks. */
13610 static tree
13611 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13613 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13614 overflow/underflow occurred. If -frounding-math, proceed iff the
13615 result of calling FUNC was exact. */
13616 if (force_convert
13617 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13618 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13619 && (!flag_rounding_math || !inexact)))
13621 REAL_VALUE_TYPE re, im;
13623 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13624 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13625 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13626 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13627 but the mpft_t is not, then we underflowed in the
13628 conversion. */
13629 if (force_convert
13630 || (real_isfinite (&re) && real_isfinite (&im)
13631 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13632 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13634 REAL_VALUE_TYPE re_mode, im_mode;
13636 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13637 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13638 /* Proceed iff the specified mode can hold the value. */
13639 if (force_convert
13640 || (real_identical (&re_mode, &re)
13641 && real_identical (&im_mode, &im)))
13642 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13643 build_real (TREE_TYPE (type), im_mode));
13646 return NULL_TREE;
13649 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13650 FUNC on it and return the resulting value as a tree with type TYPE.
13651 If MIN and/or MAX are not NULL, then the supplied ARG must be
13652 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13653 acceptable values, otherwise they are not. The mpfr precision is
13654 set to the precision of TYPE. We assume that function FUNC returns
13655 zero if the result could be calculated exactly within the requested
13656 precision. */
13658 static tree
13659 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13660 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13661 bool inclusive)
13663 tree result = NULL_TREE;
13665 STRIP_NOPS (arg);
13667 /* To proceed, MPFR must exactly represent the target floating point
13668 format, which only happens when the target base equals two. */
13669 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13670 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13672 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13674 if (real_isfinite (ra)
13675 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13676 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13678 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13679 const int prec = fmt->p;
13680 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13681 int inexact;
13682 mpfr_t m;
13684 mpfr_init2 (m, prec);
13685 mpfr_from_real (m, ra, GMP_RNDN);
13686 mpfr_clear_flags ();
13687 inexact = func (m, m, rnd);
13688 result = do_mpfr_ckconv (m, type, inexact);
13689 mpfr_clear (m);
13693 return result;
13696 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13697 FUNC on it and return the resulting value as a tree with type TYPE.
13698 The mpfr precision is set to the precision of TYPE. We assume that
13699 function FUNC returns zero if the result could be calculated
13700 exactly within the requested precision. */
13702 static tree
13703 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13704 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13706 tree result = NULL_TREE;
13708 STRIP_NOPS (arg1);
13709 STRIP_NOPS (arg2);
13711 /* To proceed, MPFR must exactly represent the target floating point
13712 format, which only happens when the target base equals two. */
13713 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13714 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13715 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13717 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13718 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13720 if (real_isfinite (ra1) && real_isfinite (ra2))
13722 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13723 const int prec = fmt->p;
13724 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13725 int inexact;
13726 mpfr_t m1, m2;
13728 mpfr_inits2 (prec, m1, m2, NULL);
13729 mpfr_from_real (m1, ra1, GMP_RNDN);
13730 mpfr_from_real (m2, ra2, GMP_RNDN);
13731 mpfr_clear_flags ();
13732 inexact = func (m1, m1, m2, rnd);
13733 result = do_mpfr_ckconv (m1, type, inexact);
13734 mpfr_clears (m1, m2, NULL);
13738 return result;
13741 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13742 FUNC on it and return the resulting value as a tree with type TYPE.
13743 The mpfr precision is set to the precision of TYPE. We assume that
13744 function FUNC returns zero if the result could be calculated
13745 exactly within the requested precision. */
13747 static tree
13748 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13749 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13751 tree result = NULL_TREE;
13753 STRIP_NOPS (arg1);
13754 STRIP_NOPS (arg2);
13755 STRIP_NOPS (arg3);
13757 /* To proceed, MPFR must exactly represent the target floating point
13758 format, which only happens when the target base equals two. */
13759 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13760 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13761 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13762 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13764 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13765 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13766 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13768 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13770 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13771 const int prec = fmt->p;
13772 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13773 int inexact;
13774 mpfr_t m1, m2, m3;
13776 mpfr_inits2 (prec, m1, m2, m3, NULL);
13777 mpfr_from_real (m1, ra1, GMP_RNDN);
13778 mpfr_from_real (m2, ra2, GMP_RNDN);
13779 mpfr_from_real (m3, ra3, GMP_RNDN);
13780 mpfr_clear_flags ();
13781 inexact = func (m1, m1, m2, m3, rnd);
13782 result = do_mpfr_ckconv (m1, type, inexact);
13783 mpfr_clears (m1, m2, m3, NULL);
13787 return result;
13790 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13791 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13792 If ARG_SINP and ARG_COSP are NULL then the result is returned
13793 as a complex value.
13794 The type is taken from the type of ARG and is used for setting the
13795 precision of the calculation and results. */
13797 static tree
13798 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13800 tree const type = TREE_TYPE (arg);
13801 tree result = NULL_TREE;
13803 STRIP_NOPS (arg);
13805 /* To proceed, MPFR must exactly represent the target floating point
13806 format, which only happens when the target base equals two. */
13807 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13808 && TREE_CODE (arg) == REAL_CST
13809 && !TREE_OVERFLOW (arg))
13811 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13813 if (real_isfinite (ra))
13815 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13816 const int prec = fmt->p;
13817 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13818 tree result_s, result_c;
13819 int inexact;
13820 mpfr_t m, ms, mc;
13822 mpfr_inits2 (prec, m, ms, mc, NULL);
13823 mpfr_from_real (m, ra, GMP_RNDN);
13824 mpfr_clear_flags ();
13825 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13826 result_s = do_mpfr_ckconv (ms, type, inexact);
13827 result_c = do_mpfr_ckconv (mc, type, inexact);
13828 mpfr_clears (m, ms, mc, NULL);
13829 if (result_s && result_c)
13831 /* If we are to return in a complex value do so. */
13832 if (!arg_sinp && !arg_cosp)
13833 return build_complex (build_complex_type (type),
13834 result_c, result_s);
13836 /* Dereference the sin/cos pointer arguments. */
13837 arg_sinp = build_fold_indirect_ref (arg_sinp);
13838 arg_cosp = build_fold_indirect_ref (arg_cosp);
13839 /* Proceed if valid pointer type were passed in. */
13840 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13841 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13843 /* Set the values. */
13844 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13845 result_s);
13846 TREE_SIDE_EFFECTS (result_s) = 1;
13847 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13848 result_c);
13849 TREE_SIDE_EFFECTS (result_c) = 1;
13850 /* Combine the assignments into a compound expr. */
13851 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13852 result_s, result_c));
13857 return result;
13860 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13861 two-argument mpfr order N Bessel function FUNC on them and return
13862 the resulting value as a tree with type TYPE. The mpfr precision
13863 is set to the precision of TYPE. We assume that function FUNC
13864 returns zero if the result could be calculated exactly within the
13865 requested precision. */
13866 static tree
13867 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13868 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13869 const REAL_VALUE_TYPE *min, bool inclusive)
13871 tree result = NULL_TREE;
13873 STRIP_NOPS (arg1);
13874 STRIP_NOPS (arg2);
13876 /* To proceed, MPFR must exactly represent the target floating point
13877 format, which only happens when the target base equals two. */
13878 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13879 && host_integerp (arg1, 0)
13880 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13882 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13883 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13885 if (n == (long)n
13886 && real_isfinite (ra)
13887 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13889 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13890 const int prec = fmt->p;
13891 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13892 int inexact;
13893 mpfr_t m;
13895 mpfr_init2 (m, prec);
13896 mpfr_from_real (m, ra, GMP_RNDN);
13897 mpfr_clear_flags ();
13898 inexact = func (m, n, m, rnd);
13899 result = do_mpfr_ckconv (m, type, inexact);
13900 mpfr_clear (m);
13904 return result;
13907 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13908 the pointer *(ARG_QUO) and return the result. The type is taken
13909 from the type of ARG0 and is used for setting the precision of the
13910 calculation and results. */
13912 static tree
13913 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13915 tree const type = TREE_TYPE (arg0);
13916 tree result = NULL_TREE;
13918 STRIP_NOPS (arg0);
13919 STRIP_NOPS (arg1);
13921 /* To proceed, MPFR must exactly represent the target floating point
13922 format, which only happens when the target base equals two. */
13923 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13924 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13925 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13927 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13928 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13930 if (real_isfinite (ra0) && real_isfinite (ra1))
13932 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13933 const int prec = fmt->p;
13934 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13935 tree result_rem;
13936 long integer_quo;
13937 mpfr_t m0, m1;
13939 mpfr_inits2 (prec, m0, m1, NULL);
13940 mpfr_from_real (m0, ra0, GMP_RNDN);
13941 mpfr_from_real (m1, ra1, GMP_RNDN);
13942 mpfr_clear_flags ();
13943 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13944 /* Remquo is independent of the rounding mode, so pass
13945 inexact=0 to do_mpfr_ckconv(). */
13946 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13947 mpfr_clears (m0, m1, NULL);
13948 if (result_rem)
13950 /* MPFR calculates quo in the host's long so it may
13951 return more bits in quo than the target int can hold
13952 if sizeof(host long) > sizeof(target int). This can
13953 happen even for native compilers in LP64 mode. In
13954 these cases, modulo the quo value with the largest
13955 number that the target int can hold while leaving one
13956 bit for the sign. */
13957 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13958 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13960 /* Dereference the quo pointer argument. */
13961 arg_quo = build_fold_indirect_ref (arg_quo);
13962 /* Proceed iff a valid pointer type was passed in. */
13963 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13965 /* Set the value. */
13966 tree result_quo
13967 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13968 build_int_cst (TREE_TYPE (arg_quo),
13969 integer_quo));
13970 TREE_SIDE_EFFECTS (result_quo) = 1;
13971 /* Combine the quo assignment with the rem. */
13972 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13973 result_quo, result_rem));
13978 return result;
13981 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13982 resulting value as a tree with type TYPE. The mpfr precision is
13983 set to the precision of TYPE. We assume that this mpfr function
13984 returns zero if the result could be calculated exactly within the
13985 requested precision. In addition, the integer pointer represented
13986 by ARG_SG will be dereferenced and set to the appropriate signgam
13987 (-1,1) value. */
13989 static tree
13990 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13992 tree result = NULL_TREE;
13994 STRIP_NOPS (arg);
13996 /* To proceed, MPFR must exactly represent the target floating point
13997 format, which only happens when the target base equals two. Also
13998 verify ARG is a constant and that ARG_SG is an int pointer. */
13999 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
14000 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
14001 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
14002 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
14004 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
14006 /* In addition to NaN and Inf, the argument cannot be zero or a
14007 negative integer. */
14008 if (real_isfinite (ra)
14009 && ra->cl != rvc_zero
14010 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
14012 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14013 const int prec = fmt->p;
14014 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14015 int inexact, sg;
14016 mpfr_t m;
14017 tree result_lg;
14019 mpfr_init2 (m, prec);
14020 mpfr_from_real (m, ra, GMP_RNDN);
14021 mpfr_clear_flags ();
14022 inexact = mpfr_lgamma (m, &sg, m, rnd);
14023 result_lg = do_mpfr_ckconv (m, type, inexact);
14024 mpfr_clear (m);
14025 if (result_lg)
14027 tree result_sg;
14029 /* Dereference the arg_sg pointer argument. */
14030 arg_sg = build_fold_indirect_ref (arg_sg);
14031 /* Assign the signgam value into *arg_sg. */
14032 result_sg = fold_build2 (MODIFY_EXPR,
14033 TREE_TYPE (arg_sg), arg_sg,
14034 build_int_cst (TREE_TYPE (arg_sg), sg));
14035 TREE_SIDE_EFFECTS (result_sg) = 1;
14036 /* Combine the signgam assignment with the lgamma result. */
14037 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14038 result_sg, result_lg));
14043 return result;
14046 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14047 function FUNC on it and return the resulting value as a tree with
14048 type TYPE. The mpfr precision is set to the precision of TYPE. We
14049 assume that function FUNC returns zero if the result could be
14050 calculated exactly within the requested precision. */
14052 static tree
14053 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14055 tree result = NULL_TREE;
14057 STRIP_NOPS (arg);
14059 /* To proceed, MPFR must exactly represent the target floating point
14060 format, which only happens when the target base equals two. */
14061 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14062 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14063 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14065 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14066 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14068 if (real_isfinite (re) && real_isfinite (im))
14070 const struct real_format *const fmt =
14071 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14072 const int prec = fmt->p;
14073 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14074 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14075 int inexact;
14076 mpc_t m;
14078 mpc_init2 (m, prec);
14079 mpfr_from_real (mpc_realref(m), re, rnd);
14080 mpfr_from_real (mpc_imagref(m), im, rnd);
14081 mpfr_clear_flags ();
14082 inexact = func (m, m, crnd);
14083 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14084 mpc_clear (m);
14088 return result;
14091 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14092 mpc function FUNC on it and return the resulting value as a tree
14093 with type TYPE. The mpfr precision is set to the precision of
14094 TYPE. We assume that function FUNC returns zero if the result
14095 could be calculated exactly within the requested precision. If
14096 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14097 in the arguments and/or results. */
14099 tree
14100 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14101 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14103 tree result = NULL_TREE;
14105 STRIP_NOPS (arg0);
14106 STRIP_NOPS (arg1);
14108 /* To proceed, MPFR must exactly represent the target floating point
14109 format, which only happens when the target base equals two. */
14110 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14111 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14112 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14113 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14114 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14116 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14117 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14118 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14119 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14121 if (do_nonfinite
14122 || (real_isfinite (re0) && real_isfinite (im0)
14123 && real_isfinite (re1) && real_isfinite (im1)))
14125 const struct real_format *const fmt =
14126 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14127 const int prec = fmt->p;
14128 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14129 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14130 int inexact;
14131 mpc_t m0, m1;
14133 mpc_init2 (m0, prec);
14134 mpc_init2 (m1, prec);
14135 mpfr_from_real (mpc_realref(m0), re0, rnd);
14136 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14137 mpfr_from_real (mpc_realref(m1), re1, rnd);
14138 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14139 mpfr_clear_flags ();
14140 inexact = func (m0, m0, m1, crnd);
14141 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14142 mpc_clear (m0);
14143 mpc_clear (m1);
14147 return result;
14150 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14151 a normal call should be emitted rather than expanding the function
14152 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14154 static tree
14155 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14157 int nargs = gimple_call_num_args (stmt);
14159 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14160 (nargs > 0
14161 ? gimple_call_arg_ptr (stmt, 0)
14162 : &error_mark_node), fcode);
14165 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14166 a normal call should be emitted rather than expanding the function
14167 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14168 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14169 passed as second argument. */
14171 tree
14172 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14173 enum built_in_function fcode)
14175 int nargs = gimple_call_num_args (stmt);
14177 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14178 (nargs > 0
14179 ? gimple_call_arg_ptr (stmt, 0)
14180 : &error_mark_node), maxlen, fcode);
14183 /* Builtins with folding operations that operate on "..." arguments
14184 need special handling; we need to store the arguments in a convenient
14185 data structure before attempting any folding. Fortunately there are
14186 only a few builtins that fall into this category. FNDECL is the
14187 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14188 result of the function call is ignored. */
14190 static tree
14191 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14192 bool ignore ATTRIBUTE_UNUSED)
14194 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14195 tree ret = NULL_TREE;
14197 switch (fcode)
14199 case BUILT_IN_SPRINTF_CHK:
14200 case BUILT_IN_VSPRINTF_CHK:
14201 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14202 break;
14204 case BUILT_IN_SNPRINTF_CHK:
14205 case BUILT_IN_VSNPRINTF_CHK:
14206 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14208 default:
14209 break;
14211 if (ret)
14213 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14214 TREE_NO_WARNING (ret) = 1;
14215 return ret;
14217 return NULL_TREE;
14220 /* A wrapper function for builtin folding that prevents warnings for
14221 "statement without effect" and the like, caused by removing the
14222 call node earlier than the warning is generated. */
14224 tree
14225 fold_call_stmt (gimple stmt, bool ignore)
14227 tree ret = NULL_TREE;
14228 tree fndecl = gimple_call_fndecl (stmt);
14229 location_t loc = gimple_location (stmt);
14230 if (fndecl
14231 && TREE_CODE (fndecl) == FUNCTION_DECL
14232 && DECL_BUILT_IN (fndecl)
14233 && !gimple_call_va_arg_pack_p (stmt))
14235 int nargs = gimple_call_num_args (stmt);
14236 tree *args = (nargs > 0
14237 ? gimple_call_arg_ptr (stmt, 0)
14238 : &error_mark_node);
14240 if (avoid_folding_inline_builtin (fndecl))
14241 return NULL_TREE;
14242 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14244 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14246 else
14248 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14249 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14250 if (!ret)
14251 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14252 if (ret)
14254 /* Propagate location information from original call to
14255 expansion of builtin. Otherwise things like
14256 maybe_emit_chk_warning, that operate on the expansion
14257 of a builtin, will use the wrong location information. */
14258 if (gimple_has_location (stmt))
14260 tree realret = ret;
14261 if (TREE_CODE (ret) == NOP_EXPR)
14262 realret = TREE_OPERAND (ret, 0);
14263 if (CAN_HAVE_LOCATION_P (realret)
14264 && !EXPR_HAS_LOCATION (realret))
14265 SET_EXPR_LOCATION (realret, loc);
14266 return realret;
14268 return ret;
14272 return NULL_TREE;
14275 /* Look up the function in builtin_decl that corresponds to DECL
14276 and set ASMSPEC as its user assembler name. DECL must be a
14277 function decl that declares a builtin. */
14279 void
14280 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14282 tree builtin;
14283 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14284 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14285 && asmspec != 0);
14287 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14288 set_user_assembler_name (builtin, asmspec);
14289 switch (DECL_FUNCTION_CODE (decl))
14291 case BUILT_IN_MEMCPY:
14292 init_block_move_fn (asmspec);
14293 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14294 break;
14295 case BUILT_IN_MEMSET:
14296 init_block_clear_fn (asmspec);
14297 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14298 break;
14299 case BUILT_IN_MEMMOVE:
14300 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14301 break;
14302 case BUILT_IN_MEMCMP:
14303 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14304 break;
14305 case BUILT_IN_ABORT:
14306 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14307 break;
14308 case BUILT_IN_FFS:
14309 if (INT_TYPE_SIZE < BITS_PER_WORD)
14311 set_user_assembler_libfunc ("ffs", asmspec);
14312 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14313 MODE_INT, 0), "ffs");
14315 break;
14316 default:
14317 break;
14321 /* Return true if DECL is a builtin that expands to a constant or similarly
14322 simple code. */
14323 bool
14324 is_simple_builtin (tree decl)
14326 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14327 switch (DECL_FUNCTION_CODE (decl))
14329 /* Builtins that expand to constants. */
14330 case BUILT_IN_CONSTANT_P:
14331 case BUILT_IN_EXPECT:
14332 case BUILT_IN_OBJECT_SIZE:
14333 case BUILT_IN_UNREACHABLE:
14334 /* Simple register moves or loads from stack. */
14335 case BUILT_IN_ASSUME_ALIGNED:
14336 case BUILT_IN_RETURN_ADDRESS:
14337 case BUILT_IN_EXTRACT_RETURN_ADDR:
14338 case BUILT_IN_FROB_RETURN_ADDR:
14339 case BUILT_IN_RETURN:
14340 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14341 case BUILT_IN_FRAME_ADDRESS:
14342 case BUILT_IN_VA_END:
14343 case BUILT_IN_STACK_SAVE:
14344 case BUILT_IN_STACK_RESTORE:
14345 /* Exception state returns or moves registers around. */
14346 case BUILT_IN_EH_FILTER:
14347 case BUILT_IN_EH_POINTER:
14348 case BUILT_IN_EH_COPY_VALUES:
14349 return true;
14351 default:
14352 return false;
14355 return false;
14358 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14359 most probably expanded inline into reasonably simple code. This is a
14360 superset of is_simple_builtin. */
14361 bool
14362 is_inexpensive_builtin (tree decl)
14364 if (!decl)
14365 return false;
14366 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14367 return true;
14368 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14369 switch (DECL_FUNCTION_CODE (decl))
14371 case BUILT_IN_ABS:
14372 case BUILT_IN_ALLOCA:
14373 case BUILT_IN_ALLOCA_WITH_ALIGN:
14374 case BUILT_IN_BSWAP16:
14375 case BUILT_IN_BSWAP32:
14376 case BUILT_IN_BSWAP64:
14377 case BUILT_IN_CLZ:
14378 case BUILT_IN_CLZIMAX:
14379 case BUILT_IN_CLZL:
14380 case BUILT_IN_CLZLL:
14381 case BUILT_IN_CTZ:
14382 case BUILT_IN_CTZIMAX:
14383 case BUILT_IN_CTZL:
14384 case BUILT_IN_CTZLL:
14385 case BUILT_IN_FFS:
14386 case BUILT_IN_FFSIMAX:
14387 case BUILT_IN_FFSL:
14388 case BUILT_IN_FFSLL:
14389 case BUILT_IN_IMAXABS:
14390 case BUILT_IN_FINITE:
14391 case BUILT_IN_FINITEF:
14392 case BUILT_IN_FINITEL:
14393 case BUILT_IN_FINITED32:
14394 case BUILT_IN_FINITED64:
14395 case BUILT_IN_FINITED128:
14396 case BUILT_IN_FPCLASSIFY:
14397 case BUILT_IN_ISFINITE:
14398 case BUILT_IN_ISINF_SIGN:
14399 case BUILT_IN_ISINF:
14400 case BUILT_IN_ISINFF:
14401 case BUILT_IN_ISINFL:
14402 case BUILT_IN_ISINFD32:
14403 case BUILT_IN_ISINFD64:
14404 case BUILT_IN_ISINFD128:
14405 case BUILT_IN_ISNAN:
14406 case BUILT_IN_ISNANF:
14407 case BUILT_IN_ISNANL:
14408 case BUILT_IN_ISNAND32:
14409 case BUILT_IN_ISNAND64:
14410 case BUILT_IN_ISNAND128:
14411 case BUILT_IN_ISNORMAL:
14412 case BUILT_IN_ISGREATER:
14413 case BUILT_IN_ISGREATEREQUAL:
14414 case BUILT_IN_ISLESS:
14415 case BUILT_IN_ISLESSEQUAL:
14416 case BUILT_IN_ISLESSGREATER:
14417 case BUILT_IN_ISUNORDERED:
14418 case BUILT_IN_VA_ARG_PACK:
14419 case BUILT_IN_VA_ARG_PACK_LEN:
14420 case BUILT_IN_VA_COPY:
14421 case BUILT_IN_TRAP:
14422 case BUILT_IN_SAVEREGS:
14423 case BUILT_IN_POPCOUNTL:
14424 case BUILT_IN_POPCOUNTLL:
14425 case BUILT_IN_POPCOUNTIMAX:
14426 case BUILT_IN_POPCOUNT:
14427 case BUILT_IN_PARITYL:
14428 case BUILT_IN_PARITYLL:
14429 case BUILT_IN_PARITYIMAX:
14430 case BUILT_IN_PARITY:
14431 case BUILT_IN_LABS:
14432 case BUILT_IN_LLABS:
14433 case BUILT_IN_PREFETCH:
14434 return true;
14436 default:
14437 return is_simple_builtin (decl);
14440 return false;